aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile3
-rw-r--r--cumatrix_example.lua12
-rw-r--r--matrix/cumatrix.c12
-rw-r--r--matrix/generic/cumatrix.c38
-rw-r--r--matrix/generic/matrix.c6
-rw-r--r--matrix/generic/mmatrix.c1
-rw-r--r--matrix/init.c29
-rw-r--r--matrix/init.lua12
-rw-r--r--matrix/mmatrix.c12
-rw-r--r--mmatrix_example.lua4
10 files changed, 76 insertions, 53 deletions
diff --git a/Makefile b/Makefile
index 927dfaf..27f3380 100644
--- a/Makefile
+++ b/Makefile
@@ -38,6 +38,9 @@ $(OBJ_DIR)/luaT.o:
gcc -c -o $@ luaT/luaT.c $(INCLUDE) -fPIC
$(LIBS): $(OBJS)
gcc -shared -o $@ $(OBJS) $(LDFLAGS)
+matrix/cumatrix.c: matrix/generic/cumatrix.c
+matrix/mmatrix.c: matrix/generic/mmatrix.c
+matrix/generic/mmatrix.c matrix/generic/cumatrix.c: matrix/generic/matrix.c
clean:
-rm -rf $(OBJ_DIR)
-rm -rf $(LUA_DIR)
diff --git a/cumatrix_example.lua b/cumatrix_example.lua
index aeb5828..26e1dc4 100644
--- a/cumatrix_example.lua
+++ b/cumatrix_example.lua
@@ -1,7 +1,7 @@
m = 10
n = 10
-fm = nerv.FloatCuMatrix(m, n)
-dm = nerv.DoubleCuMatrix(m, n)
+fm = nerv.CuMatrixFloat(m, n)
+dm = nerv.CuMatrixDouble(m, n)
for i = 0, m - 1 do
for j = 0, n - 1 do
-- local t = math.random(10)
@@ -10,11 +10,11 @@ for i = 0, m - 1 do
dm[i][j] = t
end
end
-print(fm)
+-- print(fm)
fs = fm:softmax()
-print(fs)
-print(dm)
+-- print(fs)
+-- print(dm)
ds = dm:softmax()
-print(ds)
+-- print(ds)
print(fs + fs)
print(fs - fs)
diff --git a/matrix/cumatrix.c b/matrix/cumatrix.c
index 90a6703..838183a 100644
--- a/matrix/cumatrix.c
+++ b/matrix/cumatrix.c
@@ -1,11 +1,11 @@
#define NERV_GENERIC_CUMATRIX
#define MATRIX_USE_FLOAT
-#define cuda_matrix_(NAME) cuda_matrix_float_ ## NAME
-#define nerv_matrix_(NAME) nerv_matrix_float_cuda_ ## NAME
+#define cuda_matrix_(NAME) cuda_matrix_float_##NAME
+#define nerv_matrix_(NAME) nerv_matrix_cuda_float_##NAME
#define cudak_(NAME) cudak_float_ ## NAME
#define NERV_CUBLAS_(NAME) cublasS##NAME
-const char *nerv_matrix_(tname) = "nerv.FloatCuMatrix";
+const char *nerv_matrix_(tname) = "nerv.CuMatrixFloat";
#include "generic/cumatrix.c"
#undef NERV_CUBLAS_
#undef cudak_
@@ -16,9 +16,9 @@ const char *nerv_matrix_(tname) = "nerv.FloatCuMatrix";
#undef MATRIX_ELEM_PTR
#define MATRIX_USE_DOUBLE
-#define cuda_matrix_(NAME) cuda_matrix_double_ ## NAME
-#define nerv_matrix_(NAME) nerv_matrix_double_cuda_ ## NAME
+#define cuda_matrix_(NAME) cuda_matrix_double_##NAME
+#define nerv_matrix_(NAME) nerv_matrix_cuda_double_##NAME
#define cudak_(NAME) cudak_double_ ## NAME
#define NERV_CUBLAS_(NAME) cublasD##NAME
-const char *nerv_matrix_(tname) = "nerv.DoubleCuMatrix";
+const char *nerv_matrix_(tname) = "nerv.CuMatrixDouble";
#include "generic/cumatrix.c"
diff --git a/matrix/generic/cumatrix.c b/matrix/generic/cumatrix.c
index 7afa51a..90c6d6a 100644
--- a/matrix/generic/cumatrix.c
+++ b/matrix/generic/cumatrix.c
@@ -8,6 +8,7 @@
#define MATRIX_DATA_WRITE(data, idx, val) cuda_matrix_(write)(data, idx, val)
#define MATRIX_DATA_READ(data, idx) cuda_matrix_(read)(data, idx)
#define MATRIX_INIT(L) cuda_matrix_(init)(L)
+#define MATRIX_BASE_TNAME nerv_matrix_cuda_tname
#define NERV_GENERIC_MATRIX
#define NERV_GENERIC_CUKERNEL
#include "../../common.h"
@@ -34,30 +35,18 @@ static void nerv_matrix_(add_)(const Matrix *a, const Matrix *b,
}
static int nerv_matrix_(add)(lua_State *L) {
- Matrix *a = luaT_checkudata(L, 1, nerv_matrix_(tname));
- Matrix *b = luaT_checkudata(L, 2, nerv_matrix_(tname));
- Matrix *c;
+ Matrix *c = luaT_checkudata(L, 1, nerv_matrix_(tname));
+ Matrix *a = luaT_checkudata(L, 2, nerv_matrix_(tname));
+ Matrix *b = luaT_checkudata(L, 3, nerv_matrix_(tname));
+ MATRIX_ELEM alpha = luaL_checknumber(L, 4); /* alpha */
+ MATRIX_ELEM beta = luaL_checknumber(L, 5); /* alpha */
if (!(a->nrow == b->nrow && a->ncol == b->ncol))
nerv_error(L, "Matrices should be of the same dimension");
- c = nerv_matrix_(new_)(a->nrow, a->ncol);
- nerv_matrix_(add_)(a, b, c, 1.0f, 1.0f);
+ nerv_matrix_(add_)(a, b, c, alpha, beta);
luaT_pushudata(L, c, nerv_matrix_(tname));
return 1;
}
-static int nerv_matrix_(sub)(lua_State *L) {
- Matrix *a = luaT_checkudata(L, 1, nerv_matrix_(tname));
- Matrix *b = luaT_checkudata(L, 2, nerv_matrix_(tname));
- Matrix *c;
- if (!(a->nrow == b->nrow && a->ncol == b->ncol))
- nerv_error(L, "Matrices should be of the same dimension");
- c = nerv_matrix_(new_)(a->nrow, a->ncol);
- nerv_matrix_(add_)(a, b, c, 1.0f, -1.0f);
- luaT_pushudata(L, c, nerv_matrix_(tname));
- return 1;
-}
-
-
static int nerv_matrix_(mul)(lua_State *L) {
Matrix *a = luaT_checkudata(L, 1, nerv_matrix_(tname));
Matrix *b = luaT_checkudata(L, 2, nerv_matrix_(tname));
@@ -77,6 +66,13 @@ static int nerv_matrix_(mul)(lua_State *L) {
return 1;
}
+static int nerv_matrix_(create)(lua_State *L) {
+ Matrix *a = luaT_checkudata(L, 1, nerv_matrix_(tname));
+ Matrix *b = nerv_matrix_(new_)(a->nrow, a->ncol);
+ luaT_pushudata(L, b, nerv_matrix_(tname));
+ return 1;
+}
+
static int nerv_matrix_(sigmoid)(lua_State *L) {
Matrix *a = luaT_checkudata(L, 1, nerv_matrix_(tname));
Matrix *b = nerv_matrix_(new_)(a->nrow, a->ncol);
@@ -114,9 +110,9 @@ static int nerv_matrix_(colmax)(lua_State *L) {
}
static const luaL_Reg nerv_matrix_(extra_methods)[] = {
- {"__add__", nerv_matrix_(add)},
- {"__sub__", nerv_matrix_(sub)},
- {"__mul__", nerv_matrix_(mul)},
+ {"add", nerv_matrix_(add)},
+ {"mul", nerv_matrix_(mul)},
+ {"create", nerv_matrix_(create)},
{"sigmoid", nerv_matrix_(sigmoid)},
{"softmax", nerv_matrix_(softmax)},
{"colsum", nerv_matrix_(colsum)},
diff --git a/matrix/generic/matrix.c b/matrix/generic/matrix.c
index f0f81a9..b06ed89 100644
--- a/matrix/generic/matrix.c
+++ b/matrix/generic/matrix.c
@@ -2,8 +2,8 @@
#include "../../common.h"
#include "matrix.h"
-extern const char *nerv_matrix_tname;
extern const char *nerv_matrix_(tname);
+extern const char *MATRIX_BASE_TNAME;
void nerv_matrix_(data_free)(Matrix *self) {
if (--(*self->data_ref) == 0)
@@ -29,7 +29,7 @@ Matrix *nerv_matrix_(new_)(long nrow, long ncol) {
int nerv_matrix_(new)(lua_State *L) {
luaT_pushudata(L, nerv_matrix_(new_)(luaL_checkinteger(L, 1),
- luaL_checkinteger(L, 2)),
+ luaL_checkinteger(L, 2)),
nerv_matrix_(tname));
return 1;
}
@@ -132,7 +132,7 @@ static const luaL_Reg nerv_matrix_(methods)[] = {
};
void nerv_matrix_(init)(lua_State *L) {
- luaT_newmetatable(L, nerv_matrix_(tname), nerv_matrix_tname,
+ luaT_newmetatable(L, nerv_matrix_(tname), MATRIX_BASE_TNAME,
nerv_matrix_(new), nerv_matrix_(destroy), NULL);
luaL_register(L, NULL, nerv_matrix_(methods));
#ifdef MATRIX_INIT
diff --git a/matrix/generic/mmatrix.c b/matrix/generic/mmatrix.c
index e981ffc..c301e23 100644
--- a/matrix/generic/mmatrix.c
+++ b/matrix/generic/mmatrix.c
@@ -7,6 +7,7 @@
#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 MATRIX_BASE_TNAME nerv_matrix_host_tname
#define NERV_GENERIC_MATRIX
#include "../../common.h"
diff --git a/matrix/init.c b/matrix/init.c
index 297f6af..e55558a 100644
--- a/matrix/init.c
+++ b/matrix/init.c
@@ -2,10 +2,13 @@
#include "generic/matrix.h"
const char *nerv_matrix_tname = "nerv.Matrix";
-void nerv_matrix_float_host_init(lua_State *L);
-void nerv_matrix_float_cuda_init(lua_State *L);
-void nerv_matrix_double_host_init(lua_State *L);
-void nerv_matrix_double_cuda_init(lua_State *L);
+const char *nerv_matrix_cuda_tname = "nerv.CuMatrix";
+const char *nerv_matrix_host_tname = "nerv.MMatrix";
+
+void nerv_matrix_host_float_init(lua_State *L);
+void nerv_matrix_cuda_float_init(lua_State *L);
+void nerv_matrix_host_double_init(lua_State *L);
+void nerv_matrix_cuda_double_init(lua_State *L);
static const luaL_Reg matrix_methods[] = {
{"__tostring__", nerv_error_method_not_implemented },
@@ -16,12 +19,20 @@ static const luaL_Reg matrix_methods[] = {
};
void nerv_matrix_init(lua_State *L) {
- /* abstract class */
+ /* abstract base class: Matrix */
luaT_newmetatable(L, nerv_matrix_tname, NULL, NULL, NULL, NULL);
luaL_register(L, NULL, matrix_methods);
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_cuda_init(L);
+
+ /* CuMatrix inherits from Matrix */
+ luaT_newmetatable(L, nerv_matrix_cuda_tname, nerv_matrix_tname,
+ NULL, NULL, NULL);
+ nerv_matrix_cuda_float_init(L);
+ nerv_matrix_cuda_double_init(L);
+
+ /* MMatrix inherits from Matrix */
+ luaT_newmetatable(L, nerv_matrix_host_tname, nerv_matrix_tname,
+ NULL, NULL, NULL);
+ nerv_matrix_host_float_init(L);
+ nerv_matrix_host_double_init(L);
}
diff --git a/matrix/init.lua b/matrix/init.lua
index d6aab73..c33cf7e 100644
--- a/matrix/init.lua
+++ b/matrix/init.lua
@@ -20,3 +20,15 @@ function nerv.Matrix:__tostring__()
table.insert(strt, string.format("[Matrix %d x %d]", nrow, ncol))
return table.concat(strt)
end
+
+function nerv.CuMatrix:__add__(b)
+ c = self:create()
+ c:add(self, b, 1.0, 1.0)
+ return c
+end
+
+function nerv.CuMatrix:__sub__(b)
+ c = self:create()
+ c:add(self, b, 1.0, -1.0)
+ return c
+end
diff --git a/matrix/mmatrix.c b/matrix/mmatrix.c
index 6f5ef3f..ffb02ac 100644
--- a/matrix/mmatrix.c
+++ b/matrix/mmatrix.c
@@ -1,8 +1,8 @@
#define NERV_GENERIC_MMATRIX
#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";
+#define host_matrix_(NAME) host_matrix_float_##NAME
+#define nerv_matrix_(NAME) nerv_matrix_host_float_##NAME
+const char *nerv_matrix_(tname) = "nerv.MMatrixFloat";
#include "generic/mmatrix.c"
#undef nerv_matrix_
#undef host_matrix_
@@ -12,7 +12,7 @@ const char *nerv_matrix_(tname) = "nerv.FloatMMatrix";
#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";
+#define host_matrix_(NAME) host_matrix_double_##NAME
+#define nerv_matrix_(NAME) nerv_matrix_host_double_##NAME
+const char *nerv_matrix_(tname) = "nerv.MMatrixDouble";
#include "generic/mmatrix.c"
diff --git a/mmatrix_example.lua b/mmatrix_example.lua
index 39cd28e..679fde3 100644
--- a/mmatrix_example.lua
+++ b/mmatrix_example.lua
@@ -1,7 +1,7 @@
m = 10
n = 10
-fm = nerv.FloatMMatrix(m, n)
-dm = nerv.DoubleMMatrix(m, n)
+fm = nerv.MMatrixFloat(m, n)
+dm = nerv.MMatrixDouble(m, n)
for i = 0, m - 1 do
for j = 0, n - 1 do
-- local t = math.random(10)