diff options
-rw-r--r-- | matrix/cukernel.cu | 4 | ||||
-rw-r--r-- | matrix/generic/mmatrix.c | 10 | ||||
-rw-r--r-- | matrix/init.c | 2 | ||||
-rw-r--r-- | matrix/mmatrix.c | 13 | ||||
-rw-r--r-- | mmatrix_example.lua | 21 |
5 files changed, 32 insertions, 18 deletions
diff --git a/matrix/cukernel.cu b/matrix/cukernel.cu index 1f97b41..e71ae49 100644 --- a/matrix/cukernel.cu +++ b/matrix/cukernel.cu @@ -13,7 +13,3 @@ #define MATRIX_USE_DOUBLE #include "generic/elem_type.h" #include "generic/cukernel.cu" -#undef cudak_ -#undef MATRIX_USE_DOUBLE -#undef MATRIX_ELEM -#undef MATRIX_ELEM_PTR diff --git a/matrix/generic/mmatrix.c b/matrix/generic/mmatrix.c index ac71c3d..e981ffc 100644 --- a/matrix/generic/mmatrix.c +++ b/matrix/generic/mmatrix.c @@ -4,17 +4,15 @@ #define MATRIX_DATA_FREE(ptr) free(ptr) #define MATRIX_DATA_ALLOC(dptr, stride, width, height) \ host_matrix_(alloc)(dptr, stride, width, height) -#define MATRIX_DATA_STRIDE(ncol) (sizeof(float) * (ncol)) +#define MATRIX_DATA_STRIDE(ncol) (sizeof(MATRIX_ELEM) * (ncol)) #define MATRIX_DATA_WRITE(data, idx, val) (data[idx] = val) #define MATRIX_DATA_READ(data, idx) (data[idx]) #define NERV_GENERIC_MATRIX #include "../../common.h" -const char *nerv_matrix_(tname) = "nerv.FloatMMatrix"; - -static void host_matrix_(alloc)(float **dptr, size_t *stride, +static void host_matrix_(alloc)(MATRIX_ELEM **dptr, size_t *stride, long width, long height) { - *dptr = (float *)malloc(width * height); + *dptr = (MATRIX_ELEM *)malloc(width * height); *stride = width; } @@ -30,7 +28,7 @@ int nerv_matrix_(get_elem)(lua_State *L) { int nerv_matrix_(set_elem)(lua_State *L) { Matrix *self = luaT_checkudata(L, 1, nerv_matrix_(tname)); int idx = luaL_checkinteger(L, 2); - float v = luaL_checknumber(L, 3); + MATRIX_ELEM v = luaL_checknumber(L, 3); if (idx < 0 || idx >= self->nmax) nerv_error(L, "index must be within range [0, %d)", self->nmax); self->data.f[idx] = v; diff --git a/matrix/init.c b/matrix/init.c index fb1c287..297f6af 100644 --- a/matrix/init.c +++ b/matrix/init.c @@ -22,6 +22,6 @@ void nerv_matrix_init(lua_State *L) { lua_pop(L, 1); nerv_matrix_float_host_init(L); nerv_matrix_float_cuda_init(L); -/* nerv_matrix_double_host_init(L); */ + nerv_matrix_double_host_init(L); nerv_matrix_double_cuda_init(L); } diff --git a/matrix/mmatrix.c b/matrix/mmatrix.c index f616d51..6f5ef3f 100644 --- a/matrix/mmatrix.c +++ b/matrix/mmatrix.c @@ -2,4 +2,17 @@ #define MATRIX_USE_FLOAT #define host_matrix_(NAME) host_matrix_float_ ## NAME #define nerv_matrix_(NAME) nerv_matrix_float_host_ ## NAME +const char *nerv_matrix_(tname) = "nerv.FloatMMatrix"; +#include "generic/mmatrix.c" +#undef nerv_matrix_ +#undef host_matrix_ +#undef MATRIX_USE_FLOAT +#undef MATRIX_ELEM +#undef MATRIX_ELEM_PTR + +#define NERV_GENERIC_MMATRIX +#define MATRIX_USE_DOUBLE +#define host_matrix_(NAME) host_matrix_double_ ## NAME +#define nerv_matrix_(NAME) nerv_matrix_double_host_ ## NAME +const char *nerv_matrix_(tname) = "nerv.DoubleMMatrix"; #include "generic/mmatrix.c" diff --git a/mmatrix_example.lua b/mmatrix_example.lua index 5b34779..39cd28e 100644 --- a/mmatrix_example.lua +++ b/mmatrix_example.lua @@ -1,9 +1,16 @@ -t = nerv.FloatMMatrix(5, 10) -a = t[1] -for i = 0, 4 do - for j = 0, 9 do - t[i][j] = i + j +m = 10 +n = 10 +fm = nerv.FloatMMatrix(m, n) +dm = nerv.DoubleMMatrix(m, n) +for i = 0, m - 1 do + for j = 0, n - 1 do + -- local t = math.random(10) + t = i / (j + 1) + fm[i][j] = t + dm[i][j] = t end end -print(t) -print(a) +print(fm) +-- print(fm:softmax()) +print(dm) +-- print(dm:softmax()) |