aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2015-05-15 03:13:31 +0800
committerDeterminant <[email protected]>2015-05-15 03:13:31 +0800
commit902faa66e3da52a1a4cf7b1dc7da6e6bf0d47d34 (patch)
treed3c5bad9e01ecc13a510fd552b97b080e70c38ad
parentd77baabfd42d169c2cf7318b770c1f2547ebe17d (diff)
add cumatrix
-rw-r--r--Makefile5
-rw-r--r--cumatrix_example.lua10
-rw-r--r--matrix/cumatrix.c15
-rw-r--r--matrix/generic/matrix.c18
-rw-r--r--matrix/init.c3
-rw-r--r--matrix/init.lua38
-rw-r--r--matrix/matrix.c5
-rw-r--r--matrix/matrix.lua17
-rwxr-xr-xnerv2
-rw-r--r--nerv.lua2
10 files changed, 83 insertions, 32 deletions
diff --git a/Makefile b/Makefile
index 0ebb91a..33219ce 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
.PHONY: all clean luajit
-OBJS := oop_example.o nerv.o luaT.o common.o matrix/matrix.o matrix/init.o
+OBJS := oop_example.o nerv.o luaT.o common.o matrix/matrix.o matrix/cumatrix.o matrix/init.o
LIBS := libnerv.so
-LUA_LIBS := matrix/matrix.lua nerv.lua
+LUA_LIBS := matrix/init.lua nerv.lua
INCLUDE := -I build/luajit-2.0/include/luajit-2.0/ -DLUA_USE_APICHECK
LDFLAGS := -L luajit-2.0/build/lib/ -llua -lm
CFLAGS :=
@@ -33,3 +33,4 @@ $(LIBS): $(OBJS)
gcc -shared -o $@ $(OBJS)
clean:
-rm -rf $(OBJ_DIR)
+ -rm -rf $(LUA_DIR)
diff --git a/cumatrix_example.lua b/cumatrix_example.lua
new file mode 100644
index 0000000..4b6fb4a
--- /dev/null
+++ b/cumatrix_example.lua
@@ -0,0 +1,10 @@
+t = nerv.FloatCuMatrix(10, 20)
+print(t)
+a = t[1]
+for i = 0, 9 do
+ for j = 0, 19 do
+ t[i][j] = i + j
+ end
+end
+print(t)
+print(a)
diff --git a/matrix/cumatrix.c b/matrix/cumatrix.c
new file mode 100644
index 0000000..87ad57b
--- /dev/null
+++ b/matrix/cumatrix.c
@@ -0,0 +1,15 @@
+#define MATRIX_DATA_FREE(ptr) free(ptr)
+#define MATRIX_DATA_ALLOC(size) malloc(size)
+#define MATRIX_DATA_STRIDE(ncol) (sizeof(float) * (ncol))
+#define MATRIX_GENERIC
+#define nerv_float_matrix_(NAME) nerv_float_matrix_cuda_ ## NAME
+#include "generic/matrix.c"
+
+const char *nerv_float_matrix_(tname) = "nerv.FloatCuMatrix";
+int nerv_float_matrix_(get_elem)(lua_State *L) {
+ return nerv_error_method_not_implemented(L);
+}
+
+int nerv_float_matrix_(set_elem)(lua_State *L) {
+ return nerv_error_method_not_implemented(L);
+}
diff --git a/matrix/generic/matrix.c b/matrix/generic/matrix.c
index 3bb12ef..29919d8 100644
--- a/matrix/generic/matrix.c
+++ b/matrix/generic/matrix.c
@@ -3,7 +3,7 @@
#include "matrix.h"
extern const char *nerv_matrix_tname;
-const char *nerv_float_matrix_tname = "nerv.FloatMatrix";
+extern const char *nerv_float_matrix_(tname);
void nerv_float_matrix_(data_free)(Matrix *self) {
if (--(*self->data_ref) == 0)
@@ -24,12 +24,12 @@ int nerv_float_matrix_(new)(lua_State *L) {
self->data_ref = (long *)malloc(sizeof(long));
*self->data_ref = 0;
nerv_float_matrix_(data_retain)(self);
- luaT_pushudata(L, self, nerv_float_matrix_tname);
+ luaT_pushudata(L, self, nerv_float_matrix_(tname));
return 1;
}
int nerv_float_matrix_(destroy)(lua_State *L) {
- Matrix *self = luaT_checkudata(L, 1, nerv_float_matrix_tname);
+ Matrix *self = luaT_checkudata(L, 1, nerv_float_matrix_(tname));
nerv_float_matrix_(data_free)(self);
return 0;
}
@@ -50,7 +50,7 @@ static Matrix *nerv_float_matrix_(getrow)(Matrix *self, int row) {
}
static int nerv_float_matrix_(newindex)(lua_State *L) {
- Matrix *self = luaT_checkudata(L, 1, nerv_float_matrix_tname);
+ Matrix *self = luaT_checkudata(L, 1, nerv_float_matrix_(tname));
if (lua_isnumber(L, 2))
{
int idx = luaL_checkinteger(L, 2);
@@ -74,7 +74,7 @@ static int nerv_float_matrix_(newindex)(lua_State *L) {
static int nerv_float_matrix_(index)(lua_State *L) {
- Matrix *self = luaT_checkudata(L, 1, nerv_float_matrix_tname);
+ Matrix *self = luaT_checkudata(L, 1, nerv_float_matrix_(tname));
if (lua_isnumber(L, 2))
{
int idx = luaL_checkinteger(L, 2);
@@ -88,7 +88,7 @@ static int nerv_float_matrix_(index)(lua_State *L) {
{
if (idx < 0 || idx >= self->nrow)
nerv_error(L, "index must be within range [0, %d)", self->nrow);
- luaT_pushudata(L, nerv_float_matrix_(getrow)(self, idx), nerv_float_matrix_tname);
+ luaT_pushudata(L, nerv_float_matrix_(getrow)(self, idx), nerv_float_matrix_(tname));
}
lua_pushboolean(L, 1);
return 2;
@@ -101,13 +101,13 @@ static int nerv_float_matrix_(index)(lua_State *L) {
}
static int nerv_float_matrix_(ncol)(lua_State *L) {
- Matrix *self = luaT_checkudata(L, 1, nerv_float_matrix_tname);
+ Matrix *self = luaT_checkudata(L, 1, nerv_float_matrix_(tname));
lua_pushinteger(L, self->ncol);
return 1;
}
static int nerv_float_matrix_(nrow)(lua_State *L) {
- Matrix *self = luaT_checkudata(L, 1, nerv_float_matrix_tname);
+ Matrix *self = luaT_checkudata(L, 1, nerv_float_matrix_(tname));
lua_pushinteger(L, self->nrow);
return 1;
}
@@ -124,7 +124,7 @@ static const luaL_Reg nerv_float_matrix_(methods)[] = {
};
void nerv_float_matrix_(init)(lua_State *L) {
- luaT_newmetatable(L, nerv_float_matrix_tname, nerv_matrix_tname,
+ luaT_newmetatable(L, nerv_float_matrix_(tname), nerv_matrix_tname,
nerv_float_matrix_(new), nerv_float_matrix_(destroy), NULL);
luaL_register(L, NULL, nerv_float_matrix_(methods));
lua_pop(L, 1);
diff --git a/matrix/init.c b/matrix/init.c
index e251628..e723f55 100644
--- a/matrix/init.c
+++ b/matrix/init.c
@@ -2,6 +2,8 @@
#include "generic/matrix.h"
const char *nerv_matrix_tname = "nerv.Matrix";
+void nerv_float_matrix_host_init(lua_State *L);
+void nerv_float_matrix_cuda_init(lua_State *L);
static const luaL_Reg matrix_methods[] = {
{"__tostring__", nerv_error_method_not_implemented },
{"__add__", nerv_error_method_not_implemented },
@@ -16,4 +18,5 @@ void nerv_matrix_init(lua_State *L) {
luaL_register(L, NULL, matrix_methods);
lua_pop(L, 1);
nerv_float_matrix_host_init(L);
+ nerv_float_matrix_cuda_init(L);
}
diff --git a/matrix/init.lua b/matrix/init.lua
new file mode 100644
index 0000000..59b8384
--- /dev/null
+++ b/matrix/init.lua
@@ -0,0 +1,38 @@
+function nerv.FloatCuMatrix:__tostring__()
+ local ncol = self:ncol()
+ local nrow = self:nrow()
+ local strt = {}
+
+ if nrow == 1 then
+ for col = 0, ncol - 1 do
+ table.insert(strt, string.format("%f ", self[col]))
+ end
+ table.insert(strt, "\n")
+ else
+ for row = 0, nrow - 1 do
+ local rp = self[row]
+ for col = 0, ncol - 1 do
+ table.insert(strt, string.format("%f ", rp[col]))
+ end
+ table.insert(strt, "\n")
+ end
+ end
+ table.insert(strt, string.format("[Float Matrix %d x %d]", nrow, ncol))
+ return table.concat(strt)
+end
+
+function nerv.FloatMatrix:__tostring__()
+ local ncol = self:ncol()
+ local nrow = self:nrow()
+ local i = 0
+ local strt = {}
+ for row = 0, nrow - 1 do
+ for col = 0, ncol - 1 do
+ table.insert(strt, string.format("%f ", self:get_elem(i)))
+ i = i + 1
+ end
+ table.insert(strt, "\n")
+ end
+ table.insert(strt, string.format("[Float Matrix %d x %d]", nrow, ncol))
+ return table.concat(strt)
+end
diff --git a/matrix/matrix.c b/matrix/matrix.c
index 3a593e5..0e5f75f 100644
--- a/matrix/matrix.c
+++ b/matrix/matrix.c
@@ -5,8 +5,9 @@
#define nerv_float_matrix_(NAME) nerv_float_matrix_host_ ## NAME
#include "generic/matrix.c"
+const char *nerv_float_matrix_(tname) = "nerv.FloatMatrix";
int nerv_float_matrix_(get_elem)(lua_State *L) {
- Matrix *self = luaT_checkudata(L, 1, nerv_float_matrix_tname);
+ Matrix *self = luaT_checkudata(L, 1, nerv_float_matrix_(tname));
int idx = luaL_checkinteger(L, 2);
if (idx < 0 || idx >= self->nmax)
nerv_error(L, "index must be within range [0, %d)", self->nmax);
@@ -15,7 +16,7 @@ int nerv_float_matrix_(get_elem)(lua_State *L) {
}
int nerv_float_matrix_(set_elem)(lua_State *L) {
- Matrix *self = luaT_checkudata(L, 1, nerv_float_matrix_tname);
+ Matrix *self = luaT_checkudata(L, 1, nerv_float_matrix_(tname));
int idx = luaL_checkinteger(L, 2);
float v = luaL_checknumber(L, 3);
long upper = self->nrow * self->ncol;
diff --git a/matrix/matrix.lua b/matrix/matrix.lua
deleted file mode 100644
index b9e4876..0000000
--- a/matrix/matrix.lua
+++ /dev/null
@@ -1,17 +0,0 @@
-function nerv.FloatMatrix:__tostring__()
- local ncol = self:ncol()
- local nrow = self:nrow()
- local i = 0
- local strt = {}
- for row = 0, nrow - 1 do
--- local rp = self[row]
- for col = 0, ncol - 1 do
--- table.insert(strt, string.format("%f ", rp[col]))
- table.insert(strt, string.format("%f ", self:get_elem(i)))
- i = i + 1
- end
- table.insert(strt, "\n")
- end
- table.insert(strt, string.format("[Float Matrix %d x %d]", nrow, ncol))
- return table.concat(strt)
-end
diff --git a/nerv b/nerv
index 1072710..50e2e0b 100755
--- a/nerv
+++ b/nerv
@@ -1,2 +1,2 @@
#!/bin/bash
-exec 'build/luajit-2.0/bin/luajit' -e "package.cpath=\"${PWD}/build/objs/?.so\"" -e "package.path=\"${PWD}/build/lua/?.lua\"" -e "require 'nerv'" "$@"
+exec 'build/luajit-2.0/bin/luajit' -e "package.cpath=\"${PWD}/build/objs/?.so\"" -e "package.path=\"${PWD}/build/lua/?/init.lua;${PWD}/build/lua/?.lua\"" -e "require 'nerv'" "$@"
diff --git a/nerv.lua b/nerv.lua
index ed56c8f..5b53306 100644
--- a/nerv.lua
+++ b/nerv.lua
@@ -1,2 +1,2 @@
require 'libnerv'
-require 'matrix'
+require 'matrix.init'