diff options
Diffstat (limited to 'nerv/lib')
-rw-r--r-- | nerv/lib/common.c | 108 | ||||
-rw-r--r-- | nerv/lib/common.h | 89 | ||||
-rw-r--r-- | nerv/lib/io/chunk_file.c | 116 | ||||
-rw-r--r-- | nerv/lib/io/chunk_file.h | 32 | ||||
-rw-r--r-- | nerv/lib/matrix/cumatrix.c | 2 | ||||
-rw-r--r-- | nerv/lib/matrix/generic/cumatrix.c | 76 | ||||
-rw-r--r-- | nerv/lib/matrix/generic/cumatrix.h | 2 | ||||
-rw-r--r-- | nerv/lib/matrix/generic/matrix.c | 8 | ||||
-rw-r--r-- | nerv/lib/matrix/generic/mmatrix.c | 14 | ||||
-rw-r--r-- | nerv/lib/matrix/generic/mmatrix.h | 3 | ||||
-rw-r--r-- | nerv/lib/matrix/mmatrix.c | 4 |
11 files changed, 316 insertions, 138 deletions
diff --git a/nerv/lib/common.c b/nerv/lib/common.c new file mode 100644 index 0000000..db667b2 --- /dev/null +++ b/nerv/lib/common.c @@ -0,0 +1,108 @@ +#include "common.h" +#include <stdarg.h> +int nerv_error(lua_State *L, const char *err_mesg_fmt, ...) { + va_list ap; + va_start(ap, err_mesg_fmt); + lua_pushstring(L, "[nerv] internal error: "); + lua_pushvfstring(L, err_mesg_fmt, ap); + lua_concat(L, 2); + lua_error(L); + va_end(ap); + return 0; +} + +int nerv_error_status(lua_State *L, Status *status) { + const char *mmesg = NULL; + switch (status->err_code) + { + case MAT_GENERAL_ERR: mmesg = "general error"; break; + case MAT_INSUF_MEM: mmesg = "insufficient memory"; break; + case MAT_INVALID_FORMAT: mmesg = "invalid matrix format"; break; + case MAT_WRITE_ERROR: mmesg = "error while writing matrix"; break; + case MAT_INVALID_COPY_INTERVAL: mmesg = "invalid copy interval"; break; + case MAT_MISMATCH_DIM: mmesg = "mismatching matrix dimension"; break; + case MAT_WRONG_MULT_DIM: mmesg = "wrong multipier dimension"; break; + case MAT_ROW_VECTOR_EXP: mmesg = "row vector expected"; break; + case MAT_COL_VECTOR_EXP: mmesg = "column vector expected"; break; + case MAT_IDX_VECTOR_EXP: mmesg = "index vector expected"; break; + case MAT_INVALID_IDX: mmesg = "invalid index"; break; + case MAT_CUDA_ERR: mmesg = "cuda error"; break; + case MAT_CUBLAS_ERR: mmesg = "cublas error"; break; + case CF_INVALID_FORMAT: mmesg = "invalid format"; break; + case CF_END_OF_FILE: mmesg = "unexpected end of file"; break; + case CF_SECTION_OVERFLOW: mmesg = "section overflow"; break; + case CF_WRITE_ERROR: mmesg = "error while writing"; break; + case CF_ERR_OPEN_FILE: mmesg = "error while opening file"; break; + case CF_INVALID_OP: mmesg = "invalid operation"; break; + default: mmesg = "unknown"; break; + } + if (status->msg) + nerv_error(L, "%s: %s @%s:%d", mmesg, status->msg, + status->file, status->lineno); + else + nerv_error(L, "%s @%s:%d", mmesg, status->file, status->lineno); +} + +int nerv_error_method_not_implemented(lua_State *L) { + return nerv_error(L, "method not implemented"); +} + +void luaN_append_methods(lua_State *L, const luaL_Reg *mlist) { + for (; mlist->func; mlist++) + { + lua_pushcfunction(L, mlist->func); + lua_setfield(L, -2, mlist->name); + } +} + +HashMap *hashmap_create(size_t size, HashKey_t hfunc, HashMapCmp_t cmp) { + HashMap *res = (HashMap *)malloc(sizeof(HashMap)); + res->bucket = calloc(size, sizeof(HashNode)); + res->cmp = cmp; + res->hfunc = hfunc; + res->size = size; + return res; +} + +void *hashmap_getval(HashMap *h, const char *key) { + size_t idx = h->hfunc(key) % h->size; + HashNode *ptr; + for (ptr = h->bucket[idx]; ptr; ptr = ptr->next) + { + if (!h->cmp(ptr->key, key)) + return ptr->val; + } + return NULL; +} + +void hashmap_setval(HashMap *h, const char *key, void *val) { + size_t idx = h->hfunc(key) % h->size; + HashNode *ptr = malloc(sizeof(HashNode)); + ptr->next = h->bucket[idx]; + h->bucket[idx] = ptr; + ptr->key = key; + ptr->val = val; +} + +void hashmap_clear(HashMap *h) { + size_t i; + for (i = 0; i < h->size; i++) + { + HashNode *ptr, *nptr; + for (ptr = h->bucket[i]; ptr; ptr = nptr) + { + nptr = ptr->next; + free(ptr->val); + free(ptr); + } + h->bucket[i] = NULL; + } +} + +size_t bkdr_hash(const char *key) { + unsigned int seed = 131; + unsigned int res = 0; + while (*key) + res = res * seed + *key++; + return res; +} diff --git a/nerv/lib/common.h b/nerv/lib/common.h new file mode 100644 index 0000000..1c588d1 --- /dev/null +++ b/nerv/lib/common.h @@ -0,0 +1,89 @@ +#ifndef NERV_COMMON_H +#define NERV_COMMON_H +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" +#include "luaT/luaT.h" +#include <stdio.h> +#include <stdlib.h> + +typedef enum ErrCode { + NERV_NORMAL, + /* matrix err */ + MAT_GENERAL_ERR, + MAT_INSUF_MEM, + MAT_INVALID_FORMAT, + MAT_WRITE_ERROR, + MAT_INVALID_COPY_INTERVAL, + MAT_MISMATCH_DIM, + MAT_WRONG_MULT_DIM, + MAT_ROW_VECTOR_EXP, + MAT_COL_VECTOR_EXP, + MAT_IDX_VECTOR_EXP, + MAT_INVALID_IDX, + MAT_CUDA_ERR, + MAT_CUBLAS_ERR, + /* chunk file err */ + CF_INVALID_FORMAT, + CF_END_OF_FILE, + CF_SECTION_OVERFLOW, + CF_WRITE_ERROR, + CF_ERR_OPEN_FILE, + CF_INVALID_OP, +} ErrCode; + +typedef struct Status { + ErrCode err_code; + const char *file; + int lineno; + const char *msg; +} Status; + +#define NERV_SET_STATUS(status, code, m) \ + do { \ + (status)->err_code = (code); \ + (status)->msg = (m); \ + (status)->file = __FILE__; \ + (status)->lineno = __LINE__; \ + } while (0) + +#define NERV_EXIT_STATUS(status, code, msg) \ + do { \ + NERV_SET_STATUS(status, code, msg); \ + return; \ + } while (0) + +#define NERV_LUA_CHECK_STATUS(L, status) \ + do { \ + if (status.err_code != NERV_NORMAL) \ + nerv_error_status(L, &status); \ + } while (0) + +typedef struct HashNode { + const char *key; + void *val; + struct HashNode *next; +} HashNode; + +typedef int (*HashMapCmp_t)(const char *a, const char *b); +typedef size_t (*HashKey_t)(const char *key); + +typedef struct HashMap { + HashNode **bucket; + HashMapCmp_t cmp; + HashKey_t hfunc; + size_t size; +} HashMap; + +HashMap *hashmap_create(size_t size, HashKey_t hfunc, HashMapCmp_t cmp); +void *hashmap_getval(HashMap *h, const char *key); +void hashmap_setval(HashMap *h, const char *key, void *val); +void hashmap_clear(HashMap *h); + +size_t bkdr_hash(const char *key); + +int nerv_error(lua_State *L, const char *err_mesg_fmt, ...); +int nerv_error_status(lua_State *L, Status *status); +int nerv_error_method_not_implemented(lua_State *L); +void luaN_append_methods(lua_State *L, const luaL_Reg *mlist); +#endif diff --git a/nerv/lib/io/chunk_file.c b/nerv/lib/io/chunk_file.c index e70ffc9..4e00b0b 100644 --- a/nerv/lib/io/chunk_file.c +++ b/nerv/lib/io/chunk_file.c @@ -1,23 +1,25 @@ -#include "../../common.h" +#include "../common.h" #include "chunk_file.h" #include <stdlib.h> #include <string.h> #define PARAM_HEADER_SIZE 16 -static size_t read_chunk_header_plain(FILE *fp, int *status) { +static size_t read_chunk_header_plain(FILE *fp, Status *status) { static char buff[PARAM_HEADER_SIZE]; int i; size_t size = 0; if (fread(buff, 1, PARAM_HEADER_SIZE, fp) != PARAM_HEADER_SIZE) { - if (feof(fp)) *status = CF_END_OF_FILE; + if (feof(fp)) + NERV_SET_STATUS(status, CF_END_OF_FILE, 0); else { - *status = CF_INVALID_FORMAT; + NERV_SET_STATUS(status, CF_INVALID_FORMAT, 0); return 0; } } - else *status = CF_NORMAL; + else + NERV_SET_STATUS(status, NERV_NORMAL, 0); for (i = 0; i < PARAM_HEADER_SIZE; i++) if (isdigit(buff[i])) size = size * 10 + buff[i] - '0'; @@ -25,25 +27,19 @@ static size_t read_chunk_header_plain(FILE *fp, int *status) { return size; } -static void write_chunk_header_plain(FILE *fp, size_t size, int *status) { +static void write_chunk_header_plain(FILE *fp, size_t size, Status *status) { static char buff[PARAM_HEADER_SIZE]; int i; for (i = PARAM_HEADER_SIZE - 3; i > 0; i--, size /= 10) buff[i] = size % 10 + '0'; if (size) - { - *status = CF_SECTION_OVERFLOW; - return; - } + NERV_EXIT_STATUS(status, CF_SECTION_OVERFLOW, 0); buff[0] = '['; buff[PARAM_HEADER_SIZE - 2] = ']'; buff[PARAM_HEADER_SIZE - 1] = '\n'; if (fwrite(buff, 1, PARAM_HEADER_SIZE, fp) != PARAM_HEADER_SIZE) - { - *status = CF_WRITE_ERROR; - return; - } - *status = CF_NORMAL; + NERV_EXIT_STATUS(status, CF_WRITE_ERROR, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } static ChunkData *get_chunk_data(FILE *fp, ChunkInfo *info) { @@ -56,48 +52,47 @@ static ChunkData *get_chunk_data(FILE *fp, ChunkInfo *info) { return cdp; } -static const char *read_chunk_metadata(FILE *fp, const char *fn, int *status) { +static const char *read_chunk_metadata(FILE *fp, const char *fn, + Status *status) { #define LINEBUFF_SIZE 1024 #define LUA_RETURN "return " #define LUA_RETURN_LEN (sizeof(LUA_RETURN) - 1) static char buff[LUA_RETURN_LEN + LINEBUFF_SIZE] = LUA_RETURN; - *status = fgets(buff + LUA_RETURN_LEN, - LINEBUFF_SIZE, fp) == (buff + LUA_RETURN_LEN) ? \ - CF_NORMAL : CF_INVALID_FORMAT; + NERV_SET_STATUS(status, (fgets(buff + LUA_RETURN_LEN, + LINEBUFF_SIZE, fp) == (buff + LUA_RETURN_LEN) ? \ + NERV_NORMAL : CF_INVALID_FORMAT), 0); fprintf(stderr, "metadata: %s\n", buff); return buff; } -static void write_chunk_metadata(FILE *fp, const char *mdstr, int *status) { +static void write_chunk_metadata(FILE *fp, const char *mdstr, + Status *status) { size_t size = strlen(mdstr); if (fwrite(mdstr, 1, size, fp) != size || fprintf(fp, "\n") < 0) - { - *status = CF_WRITE_ERROR; - return; - } + NERV_EXIT_STATUS(status, CF_WRITE_ERROR, 0); /* fprintf(stderr, "metadata: %s\n", metadata_str); */ - *status = CF_NORMAL; + NERV_SET_STATUS(status, NERV_NORMAL, 0); } -static ChunkFile *open_write(const char *fn, int *status) { +static ChunkFile *open_write(const char *fn, Status *status) { ChunkFile *cfp; FILE *fp = fopen(fn, "w"); if (!fp) { - *status = CF_ERR_OPEN_FILE; + NERV_SET_STATUS(status, CF_ERR_OPEN_FILE, 0); return NULL; } cfp = (ChunkFile *)malloc(sizeof(ChunkFile)); cfp->fp = fp; cfp->info = NULL; cfp->status = CF_WRITE; - *status = CF_NORMAL; + NERV_SET_STATUS(status, NERV_NORMAL, 0); return cfp; } -static ChunkFile *open_read(const char *fn, int *status) { +static ChunkFile *open_read(const char *fn, Status *status) { size_t chunk_len; off_t offset; int i; @@ -108,7 +103,7 @@ static ChunkFile *open_read(const char *fn, int *status) { if (!fp) { - *status = CF_ERR_OPEN_FILE; + NERV_SET_STATUS(status, CF_ERR_OPEN_FILE, 0); return NULL; } cfp = (ChunkFile *)malloc(sizeof(ChunkFile)); @@ -121,18 +116,18 @@ static ChunkFile *open_read(const char *fn, int *status) { /* skip to the begining of chunk i */ if (fseeko(fp, offset, SEEK_SET) != 0) { - *status = CF_INVALID_FORMAT; + NERV_SET_STATUS(status, CF_INVALID_FORMAT, 0); return NULL; } /* read header */ chunk_len = read_chunk_header_plain(fp, status); - if (*status == CF_END_OF_FILE) break; - if (*status != CF_NORMAL) + if (status->err_code == CF_END_OF_FILE) break; + if (status->err_code != NERV_NORMAL) return NULL; cip = (ChunkInfo *)malloc(sizeof(ChunkInfo)); /* read metadata */ mdstr = read_chunk_metadata(fp, fn, status); - if (*status != CF_NORMAL) + if (status->err_code != NERV_NORMAL) return NULL; cip->metadata = strdup(mdstr); cip->offset = ftello(fp); @@ -142,14 +137,15 @@ static ChunkFile *open_read(const char *fn, int *status) { cip->next = head; head = cip; } - *status = CF_NORMAL; cfp->fp = fp; cfp->info = head; cfp->status = CF_READ; + NERV_SET_STATUS(status, NERV_NORMAL, 0); return cfp; } -ChunkFile *nerv_chunk_file_create(const char *fn, const char *mode, int *status) { +ChunkFile *nerv_chunk_file_create(const char *fn, const char *mode, + Status *status) { int rd = 1, bin = 0; size_t i, len = strlen(mode); for (i = 0; i < len; i++) @@ -163,41 +159,45 @@ ChunkFile *nerv_chunk_file_create(const char *fn, const char *mode, int *status) open_write(fn, status); } -int nerv_chunk_file_write_chunkdata(ChunkFile *cfp, const char *mdstr, - ChunkDataWriter_t writer, void *writer_arg) { - int status; +void nerv_chunk_file_write_chunkdata(ChunkFile *cfp, const char *mdstr, + ChunkDataWriter_t writer, void *writer_arg, + Status *status) { off_t start; size_t size; if (cfp->status != CF_WRITE) - return CF_INVALID_OP; + NERV_EXIT_STATUS(status, CF_INVALID_OP, 0); start = ftello(cfp->fp); - write_chunk_header_plain(cfp->fp, 0, &status); /* fill zeros */ - if (status != CF_NORMAL) return status; - write_chunk_metadata(cfp->fp, mdstr, &status); - if (status != CF_NORMAL) return status; + write_chunk_header_plain(cfp->fp, 0, status); /* fill zeros */ + if (status->err_code != NERV_NORMAL) + return; + write_chunk_metadata(cfp->fp, mdstr, status); + if (status->err_code != NERV_NORMAL) + return; writer(writer_arg); size = ftello(cfp->fp) - start; fseeko(cfp->fp, start, SEEK_SET); /* write the calced size */ - write_chunk_header_plain(cfp->fp, size, &status); - if (status != CF_NORMAL) return status; + write_chunk_header_plain(cfp->fp, size, status); + if (status->err_code != NERV_NORMAL) + return; fseeko(cfp->fp, 0, SEEK_END); - return CF_NORMAL; + NERV_SET_STATUS(status, NERV_NORMAL, 0); } -ChunkData *nerv_chunk_file_get_chunkdata(ChunkFile *cfp, ChunkInfo *cip, int *status) { +ChunkData *nerv_chunk_file_get_chunkdata(ChunkFile *cfp, ChunkInfo *cip, + Status *status) { ChunkData *cdp; if (cfp->status != CF_READ) { - *status = CF_INVALID_OP; + NERV_SET_STATUS(status, CF_INVALID_OP, 0); return NULL; } if (!(cdp = get_chunk_data(cfp->fp, cip))) { - *status = CF_END_OF_FILE; + NERV_SET_STATUS(status, CF_END_OF_FILE, 0); return NULL; } - *status = CF_NORMAL; + NERV_SET_STATUS(status, NERV_NORMAL, 0); return cdp; } @@ -227,17 +227,3 @@ void nerv_chunk_data_destroy(ChunkData *cdp) { free(cdp->data); free(cdp); } - -const char *nerv_chunk_file_errstr(int status) { - switch (status) - { - case CF_INVALID_FORMAT: return "invalid format"; - case CF_END_OF_FILE: return "unexpected end of file"; - case CF_SECTION_OVERFLOW: return "section overflow"; - case CF_WRITE_ERROR: return "error while writing"; - case CF_ERR_OPEN_FILE: return "error while opening file"; - case CF_INVALID_OP: return "invalid operation"; - default: return "unknown"; - } - return NULL; -} diff --git a/nerv/lib/io/chunk_file.h b/nerv/lib/io/chunk_file.h index 71f0d03..f08d838 100644 --- a/nerv/lib/io/chunk_file.h +++ b/nerv/lib/io/chunk_file.h @@ -1,18 +1,6 @@ #ifndef NERV_CHUNK_FILE_H #define NERV_CHUNK_FILE_H -#include "../../common.h" -enum { - CF_NORMAL, - CF_INVALID_FORMAT, - CF_END_OF_FILE, - CF_SECTION_OVERFLOW, - CF_WRITE_ERROR, - CF_ERR_OPEN_FILE, - CF_INVALID_OP, - CF_READ, - CF_WRITE, - CF_CLOSED -}; +#include "../common.h" typedef struct ChunkInfo { struct ChunkInfo *next; @@ -23,7 +11,11 @@ typedef struct ChunkInfo { typedef struct ChunkFile { FILE *fp; ChunkInfo *info; - int status; + enum { + CF_READ, + CF_WRITE, + CF_CLOSED + } status; } ChunkFile; typedef struct ChunkData { @@ -32,12 +24,14 @@ typedef struct ChunkData { } ChunkData; typedef void (*ChunkDataWriter_t)(void *); -ChunkFile *nerv_chunk_file_create(const char *fn, const char *mode, int *status); -int nerv_chunk_file_write_chunkdata(ChunkFile *cfp, const char *mdstr, - ChunkDataWriter_t writer, void *writer_arg); -ChunkData *nerv_chunk_file_get_chunkdata(ChunkFile *cfp, ChunkInfo *cip, int *status); +ChunkFile *nerv_chunk_file_create(const char *fn, const char *mode, + Status *status); +void nerv_chunk_file_write_chunkdata(ChunkFile *cfp, const char *mdstr, + ChunkDataWriter_t writer, void *writer_arg, + Status *status); +ChunkData *nerv_chunk_file_get_chunkdata(ChunkFile *cfp, ChunkInfo *cip, + Status *status); void nerv_chunk_file_close(ChunkFile *cfp); void nerv_chunk_file_destroy(ChunkFile *cfp); void nerv_chunk_data_destroy(ChunkData *cdp); -const char *nerv_chunk_file_errstr(int status); #endif diff --git a/nerv/lib/matrix/cumatrix.c b/nerv/lib/matrix/cumatrix.c index 9641197..aa81bfc 100644 --- a/nerv/lib/matrix/cumatrix.c +++ b/nerv/lib/matrix/cumatrix.c @@ -1,5 +1,5 @@ #define NERV_GENERIC_CUMATRIX -#include "../../common.h" +#include "../common.h" #include "cuda_helper.h" #include <string.h> #define PROFILE_HASHMAP_SIZE 123457 diff --git a/nerv/lib/matrix/generic/cumatrix.c b/nerv/lib/matrix/generic/cumatrix.c index 11aacec..772b78d 100644 --- a/nerv/lib/matrix/generic/cumatrix.c +++ b/nerv/lib/matrix/generic/cumatrix.c @@ -7,7 +7,7 @@ #define NERV_GENERIC_MATRIX #define NERV_GENERIC_CUKERNEL -#include "../../../common.h" +#include "../../common.h" #include "../cukernel.h" #include "../cuda_helper.h" @@ -27,7 +27,7 @@ void nerv_matrix_(add)(Matrix *c, const Matrix *a, const Matrix *b, MATRIX_ELEM_PTR(c), c->stride / sizeof(MATRIX_ELEM)), status); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } void nerv_matrix_(mul)(Matrix *c, const Matrix *a, const Matrix *b, @@ -54,7 +54,7 @@ void nerv_matrix_(mul)(Matrix *c, const Matrix *a, const Matrix *b, MATRIX_ELEM_PTR(c), c->stride / sizeof(MATRIX_ELEM)), status); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } void nerv_matrix_(sigmoid)(Matrix *a, const Matrix *b, Status *status) { @@ -62,7 +62,7 @@ void nerv_matrix_(sigmoid)(Matrix *a, const Matrix *b, Status *status) { PROFILE_START cudak_(cuda_sigmoid)(b, a); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } void nerv_matrix_(sigmoid_grad)(Matrix *nerr, const Matrix *err, @@ -72,7 +72,7 @@ void nerv_matrix_(sigmoid_grad)(Matrix *nerr, const Matrix *err, PROFILE_START cudak_(cuda_sigmoid_grad)(output, err, nerr); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } Matrix *nerv_matrix_(softmax)(Matrix *b, const Matrix *a, Status *status) { @@ -80,16 +80,16 @@ Matrix *nerv_matrix_(softmax)(Matrix *b, const Matrix *a, Status *status) { Matrix *dno; CHECK_SAME_DIMENSION_RET(a, b, status); max = nerv_matrix_(create)(a->nrow, 1, status); - if (status->err_code != MAT_NORMAL) + if (status->err_code != NERV_NORMAL) return NULL; max_idx = nerv_matrix_(create)(a->nrow, 1, status); - if (status->err_code != MAT_NORMAL) + if (status->err_code != NERV_NORMAL) { nerv_matrix_(destroy)(max, status); return NULL; } dno = nerv_matrix_(create)(a->nrow, 1, status); - if (status->err_code != MAT_NORMAL) + if (status->err_code != NERV_NORMAL) { /* FIXME: destroy may also fail? */ nerv_matrix_(destroy)(max, status); nerv_matrix_(destroy)(max_idx, status); @@ -102,63 +102,63 @@ Matrix *nerv_matrix_(softmax)(Matrix *b, const Matrix *a, Status *status) { PROFILE_STOP nerv_matrix_(destroy)(max, status); nerv_matrix_(destroy)(dno, status); - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); return max_idx; } Matrix *nerv_matrix_(rowsum)(Matrix *a, Status *status) { Matrix *b = nerv_matrix_(create)(a->nrow, 1, status); - if (status->err_code != MAT_NORMAL) + if (status->err_code != NERV_NORMAL) return NULL; PROFILE_START cudak_(cuda_rowsum)(a, b); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); return b; } Matrix *nerv_matrix_(colsum)(Matrix *a, Status *status) { Matrix *b = nerv_matrix_(create)(1, a->ncol, status); - if (status->err_code != MAT_NORMAL) + if (status->err_code != NERV_NORMAL) return NULL; PROFILE_START cudak_(cuda_colsum)(a, b); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); return b; } Matrix *nerv_matrix_(colsame)(Matrix *a, const Matrix *ref, Status *status) { Matrix *b = nerv_matrix_(create)(1, a->ncol, status); - if (status->err_code != MAT_NORMAL) + if (status->err_code != NERV_NORMAL) return NULL; CHECK_SAME_DIMENSION_RET(a, ref, status); PROFILE_START cudak_(cuda_colsame)(a, ref, b); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); return b; } Matrix *nerv_matrix_(rowmax)(Matrix *a, Status *status) { Matrix *b = nerv_matrix_(create)(a->nrow, 1, status); - if (status->err_code != MAT_NORMAL) + if (status->err_code != NERV_NORMAL) return NULL; PROFILE_START cudak_(cuda_rowmax)(a, b); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); return b; } void nerv_matrix_(rowmax_idx)(Matrix *a, Matrix **b, Matrix **idx, Status *status) { *b = nerv_matrix_(create)(a->nrow, 1, status); - if (status->err_code != MAT_NORMAL) + if (status->err_code != NERV_NORMAL) return; *idx = nerv_matrix_(create)(a->nrow, 1, status); - if (status->err_code != MAT_NORMAL) + if (status->err_code != NERV_NORMAL) { /* FIXME: destroy may also fail? */ nerv_matrix_(destroy)(*b, status); @@ -167,7 +167,7 @@ void nerv_matrix_(rowmax_idx)(Matrix *a, Matrix **b, Matrix **idx, PROFILE_START cudak_(cuda_rowmax_idx)(a, *b, *idx); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } void nerv_matrix_(add_row)(Matrix *b, const Matrix *a, double beta, @@ -179,14 +179,14 @@ void nerv_matrix_(add_row)(Matrix *b, const Matrix *a, double beta, PROFILE_START cudak_(cuda_add_row)(a, b, beta); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } void nerv_matrix_(fill)(Matrix *self, double val, Status *status) { PROFILE_START cudak_(cuda_fill)(self, val); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } void nerv_matrix_(copy_fromd)(Matrix *a, const Matrix *b, @@ -205,7 +205,7 @@ void nerv_matrix_(copy_fromd)(Matrix *a, const Matrix *b, cudaMemcpyDeviceToDevice), status); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } void nerv_matrix_(copy_fromh)(Matrix *a, const Matrix *b, @@ -224,7 +224,7 @@ void nerv_matrix_(copy_fromh)(Matrix *a, const Matrix *b, cudaMemcpyHostToDevice), status); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } void nerv_matrix_(copy_toh)(Matrix *a, const Matrix *b, @@ -243,13 +243,13 @@ void nerv_matrix_(copy_toh)(Matrix *a, const Matrix *b, cudaMemcpyDeviceToHost), status); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } Matrix *nerv_matrix_(trans)(Matrix *a, Status *status) { MATRIX_ELEM alpha = 1, beta = 0; Matrix *b = nerv_matrix_(create)(a->ncol, a->nrow, status); - if (status->err_code != MAT_NORMAL) + if (status->err_code != NERV_NORMAL) return NULL; /* FIXME: possible memory leak when lua error is raised */ PROFILE_START @@ -263,7 +263,7 @@ Matrix *nerv_matrix_(trans)(Matrix *a, Status *status) { MATRIX_ELEM_PTR(b), b->stride / sizeof(MATRIX_ELEM)), status); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); return b; } @@ -274,7 +274,7 @@ void nerv_matrix_(mul_elem)(Matrix *c, const Matrix *a, const Matrix *b, PROFILE_START cudak_(cuda_mul_elem)(a, b, c); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } void nerv_matrix_(log_elem)(Matrix *b, const Matrix *a, Status *status) { @@ -282,7 +282,7 @@ void nerv_matrix_(log_elem)(Matrix *b, const Matrix *a, Status *status) { PROFILE_START cudak_(cuda_log_elem)(a, b); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } Matrix *nerv_matrix_(decompress)(const Matrix *a, int orig_col, Status *status) { @@ -293,13 +293,13 @@ Matrix *nerv_matrix_(decompress)(const Matrix *a, int orig_col, Status *status) return NULL; } b = nerv_matrix_(create)(a->nrow, orig_col, status); - if (status->err_code != MAT_NORMAL) + if (status->err_code != NERV_NORMAL) return NULL; PROFILE_START cudak_(cuda_fill)(b, 0.0); cudak_(cuda_decompress)(a, b); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); return b; } @@ -332,7 +332,7 @@ void nerv_matrix_(copy_rows_fromh_by_idx)(Matrix *a, const Matrix *b, CUDA_SAFE_CALL(cudaStreamDestroy(streams[i]), status); } free(streams); - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } void nerv_matrix_(expand_frm)(Matrix *a, const Matrix *b, @@ -345,7 +345,7 @@ void nerv_matrix_(expand_frm)(Matrix *a, const Matrix *b, PROFILE_START cudak_(cuda_expand_frm)(b, a, context); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } void nerv_matrix_(rearrange_frm)(Matrix *a, const Matrix *b, @@ -357,7 +357,7 @@ void nerv_matrix_(rearrange_frm)(Matrix *a, const Matrix *b, PROFILE_START cudak_(cuda_rearrange_frm)(b, a, step); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } void nerv_matrix_(scale_rows_by_col)(Matrix *a, const Matrix *b, @@ -369,7 +369,7 @@ void nerv_matrix_(scale_rows_by_col)(Matrix *a, const Matrix *b, PROFILE_START cudak_(cuda_scale_rows_by_col)(b, a); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } void nerv_matrix_(scale_rows_by_row)(Matrix *a, const Matrix *b, @@ -381,12 +381,12 @@ void nerv_matrix_(scale_rows_by_row)(Matrix *a, const Matrix *b, PROFILE_START cudak_(cuda_scale_rows_by_row)(b, a); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } static void cuda_matrix_(free)(MATRIX_ELEM *ptr, Status *status) { CUDA_SAFE_SYNC_CALL(cudaFree(ptr), status); - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } static void cuda_matrix_(alloc)(MATRIX_ELEM **dptr, @@ -396,7 +396,7 @@ static void cuda_matrix_(alloc)(MATRIX_ELEM **dptr, CUDA_SAFE_SYNC_CALL(cudaMallocPitch((void **)dptr, stride, width, height), status); PROFILE_STOP - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } #include "matrix.c" diff --git a/nerv/lib/matrix/generic/cumatrix.h b/nerv/lib/matrix/generic/cumatrix.h index 9a4f87e..5cfe9d5 100644 --- a/nerv/lib/matrix/generic/cumatrix.h +++ b/nerv/lib/matrix/generic/cumatrix.h @@ -1,4 +1,4 @@ -#include "../../../common.h" +#include "../../common.h" void nerv_matrix_(add)(Matrix *c, const Matrix *a, const Matrix *b, MATRIX_ELEM alpha, MATRIX_ELEM beta, diff --git a/nerv/lib/matrix/generic/matrix.c b/nerv/lib/matrix/generic/matrix.c index 91577e1..a64759e 100644 --- a/nerv/lib/matrix/generic/matrix.c +++ b/nerv/lib/matrix/generic/matrix.c @@ -1,5 +1,5 @@ #ifdef NERV_GENERIC_MATRIX -#include "../../../common.h" +#include "../../common.h" #include "matrix.h" /* FIXME: malloc failure detection */ @@ -12,7 +12,7 @@ static void nerv_matrix_(data_free)(Matrix *self, Status *status) { free(self->data_ref); free(self); } - else NERV_SET_STATUS(status, MAT_NORMAL, 0); + else NERV_SET_STATUS(status, NERV_NORMAL, 0); } static void nerv_matrix_(data_retain)(Matrix *self) { @@ -27,7 +27,7 @@ Matrix *nerv_matrix_(create)(long nrow, long ncol, Status *status) { MATRIX_DATA_ALLOC(&MATRIX_ELEM_PTR(self), &self->stride, sizeof(MATRIX_ELEM) * self->ncol, self->nrow, status); - if (status->err_code != MAT_NORMAL) + if (status->err_code != NERV_NORMAL) { free(self); return NULL; @@ -35,7 +35,7 @@ Matrix *nerv_matrix_(create)(long nrow, long ncol, Status *status) { self->data_ref = (long *)malloc(sizeof(long)); *self->data_ref = 0; nerv_matrix_(data_retain)(self); - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); return self; } diff --git a/nerv/lib/matrix/generic/mmatrix.c b/nerv/lib/matrix/generic/mmatrix.c index e3d1f93..225079e 100644 --- a/nerv/lib/matrix/generic/mmatrix.c +++ b/nerv/lib/matrix/generic/mmatrix.c @@ -5,13 +5,13 @@ #define MATRIX_DATA_ALLOC(dptr, stride, width, height, status) \ host_matrix_(alloc)(dptr, stride, width, height, status) #define NERV_GENERIC_MATRIX -#include "../../../common.h" +#include "../../common.h" #include "../../io/chunk_file.h" #include "string.h" static void host_matrix_(free)(MATRIX_ELEM *ptr, Status *status) { free(ptr); - NERV_SET_STATUS(status, MAT_NORMAL, 0); + NERV_SET_STATUS(status, NERV_NORMAL, 0); } static void host_matrix_(alloc)(MATRIX_ELEM **dptr, size_t *stride, @@ -19,7 +19,7 @@ static void host_matrix_(alloc)(MATRIX_ELEM **dptr, size_t *stride, if ((*dptr = (MATRIX_ELEM *)malloc(width * height)) == NULL) NERV_EXIT_STATUS(status, MAT_INSUF_MEM, 0); |