aboutsummaryrefslogtreecommitdiff
path: root/matrix
diff options
context:
space:
mode:
Diffstat (limited to 'matrix')
-rw-r--r--matrix/cuda_helper.h13
-rw-r--r--matrix/cukernel.cu1
-rw-r--r--matrix/cumatrix.c29
-rw-r--r--matrix/generic/cumatrix.c4
-rw-r--r--matrix/generic/matrix.c1
-rw-r--r--matrix/generic/mmatrix.c6
-rw-r--r--matrix/init.c21
-rw-r--r--matrix/mmatrix.c13
8 files changed, 53 insertions, 35 deletions
diff --git a/matrix/cuda_helper.h b/matrix/cuda_helper.h
index d6effdb..fde6f18 100644
--- a/matrix/cuda_helper.h
+++ b/matrix/cuda_helper.h
@@ -62,17 +62,14 @@ static const char *cublasGetErrorString(cublasStatus_t err) {
#define PROFILE_START \
do { \
- cudaEvent_t start, stop; \
- cudaEventCreate(&start); \
- cudaEventCreate(&stop); \
- cudaEventRecord(start, 0);
+ cudaEventRecord(profile_start, 0);
#define PROFILE_STOP \
- cudaEventRecord(stop, 0); \
- cudaEventSynchronize(stop); \
+ cudaEventRecord(profile_stop, 0); \
+ cudaEventSynchronize(profile_stop); \
float milliseconds = 0; \
- cudaEventElapsedTime(&milliseconds, start, stop); \
+ cudaEventElapsedTime(&milliseconds, profile_start, profile_stop); \
accu_profile(__func__, milliseconds / 1000); \
} while (0);
-#define PROFILE_END
+#define PROFILE_END
#endif
diff --git a/matrix/cukernel.cu b/matrix/cukernel.cu
index fbac369..a19030a 100644
--- a/matrix/cukernel.cu
+++ b/matrix/cukernel.cu
@@ -9,6 +9,7 @@
#undef MATRIX_ELEM
#undef MATRIX_ELEM_PTR
#undef MATRIX_ELEM_FMT
+#undef MATRIX_ELEM_WRITE_FMT
#define cudak_(NAME) cudak_double_ ## NAME
#define MATRIX_USE_DOUBLE
diff --git a/matrix/cumatrix.c b/matrix/cumatrix.c
index 4ebc5ff..af34fb4 100644
--- a/matrix/cumatrix.c
+++ b/matrix/cumatrix.c
@@ -1,10 +1,14 @@
#define NERV_GENERIC_CUMATRIX
#include "../common.h"
#include "cuda_helper.h"
+#include <string.h>
+#define PROFILE_HASHMAP_SIZE 123457
static cublasHandle_t cublas_handle;
+static cudaEvent_t profile_start, profile_stop;
static HashMap *profile;
-int print_profile(lua_State *L) {
+static int print_profile(lua_State *L) {
+ (void)L;
size_t i;
fprintf(stderr, "*** [nerv cumatrix profile] **\n");
for (i = 0; i < profile->size; i++)
@@ -18,7 +22,8 @@ int print_profile(lua_State *L) {
return 0;
}
-int clear_profile(lua_State *L) {
+static int clear_profile(lua_State *L) {
+ (void)L;
hashmap_clear(profile);
return 0;
}
@@ -34,6 +39,25 @@ void accu_profile(const char *name, float delta) {
*val += delta;
}
+static const luaL_Reg cumatrix_methods[] = {
+ {"print_profile", print_profile},
+ {"clear_profile", clear_profile},
+ {NULL, NULL}
+};
+
+extern void nerv_matrix_cuda_float_init(lua_State *L);
+extern void nerv_matrix_cuda_double_init(lua_State *L);
+
+void nerv_cumatrix_init(lua_State *L) {
+ luaL_register(L, NULL, cumatrix_methods);
+ cublasCreate(&cublas_handle);
+ cudaEventCreate(&profile_start);
+ cudaEventCreate(&profile_stop);
+ profile = hashmap_create(PROFILE_HASHMAP_SIZE, bkdr_hash, strcmp);
+ nerv_matrix_cuda_float_init(L);
+ nerv_matrix_cuda_double_init(L);
+}
+
#define MATRIX_USE_FLOAT
#define cuda_matrix_(NAME) cuda_matrix_float_##NAME
#define nerv_matrix_(NAME) nerv_matrix_cuda_float_##NAME
@@ -50,6 +74,7 @@ const char *nerv_matrix_(tname) = "nerv.CuMatrixFloat";
#undef MATRIX_ELEM
#undef MATRIX_ELEM_PTR
#undef MATRIX_ELEM_FMT
+#undef MATRIX_ELEM_WRITE_FMT
#undef MATRIX_CUMATRIX_HOST_TNAME
#define MATRIX_USE_DOUBLE
diff --git a/matrix/generic/cumatrix.c b/matrix/generic/cumatrix.c
index 956e1e6..a8e18e0 100644
--- a/matrix/generic/cumatrix.c
+++ b/matrix/generic/cumatrix.c
@@ -11,11 +11,9 @@
#define MATRIX_BASE_TNAME nerv_matrix_cuda_tname
#define NERV_GENERIC_MATRIX
#define NERV_GENERIC_CUKERNEL
-#define PROFILE_HASHMAP_SIZE 123457
#include "../../common.h"
#include "../cukernel.h"
#include "../cuda_helper.h"
-#include <string.h>
Matrix *nerv_matrix_(new_)(lua_State *L, long nrow, long ncol);
void nerv_matrix_(data_free)(lua_State *L, Matrix *self);
@@ -442,8 +440,6 @@ static const luaL_Reg nerv_matrix_(extra_methods)[] = {
static void cuda_matrix_(init)(lua_State *L) {
luaN_append_methods(L, nerv_matrix_(extra_methods));
- cublasCreate(&cublas_handle);
- profile = hashmap_create(PROFILE_HASHMAP_SIZE, bkdr_hash, strcmp);
}
static void cuda_matrix_(free)(lua_State *L, MATRIX_ELEM *ptr) {
diff --git a/matrix/generic/matrix.c b/matrix/generic/matrix.c
index d6b0aea..e17fb42 100644
--- a/matrix/generic/matrix.c
+++ b/matrix/generic/matrix.c
@@ -6,6 +6,7 @@ extern const char *nerv_matrix_(tname);
extern const char *MATRIX_BASE_TNAME;
void nerv_matrix_(data_free)(lua_State *L, Matrix *self) {
+ (void)L;
assert(*self->data_ref > 0);
if (--(*self->data_ref) == 0)
{
diff --git a/matrix/generic/mmatrix.c b/matrix/generic/mmatrix.c
index 2045d65..b0f0791 100644
--- a/matrix/generic/mmatrix.c
+++ b/matrix/generic/mmatrix.c
@@ -87,15 +87,9 @@ int nerv_matrix_(save)(lua_State *L) {
MATRIX_ELEM *row = MATRIX_ROW_PTR(self, i);
for (j = 0; j < ncol; j++)
if (fprintf(fp, MATRIX_ELEM_WRITE_FMT " ", row[j]) < 0)
- {
- free(self);
return 0;
- }
if (fprintf(fp, "\n") < 0)
- {
- free(self);
return 0;
- }
}
return 0;
}
diff --git a/matrix/init.c b/matrix/init.c
index 7b7f478..c29d7e9 100644
--- a/matrix/init.c
+++ b/matrix/init.c
@@ -5,21 +5,14 @@ const char *nerv_matrix_tname = "nerv.Matrix";
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);
-void nerv_matrix_host_int_init(lua_State *L);
-int print_profile(lua_State *L);
-int clear_profile(lua_State *L);
+void nerv_cumatrix_init(lua_State *L);
+void nerv_mmatrix_init(lua_State *L);
static const luaL_Reg matrix_methods[] = {
{"__tostring__", nerv_error_method_not_implemented },
{"__add__", nerv_error_method_not_implemented },
{"__sub__", nerv_error_method_not_implemented },
{"__mul__", nerv_error_method_not_implemented },
- {"print_profile", print_profile},
- {"clear_profile", clear_profile},
{NULL, NULL}
};
@@ -32,13 +25,11 @@ void nerv_matrix_init(lua_State *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);
-
+ nerv_cumatrix_init(L);
+ lua_pop(L, 1);
/* 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);
- nerv_matrix_host_int_init(L);
+ nerv_mmatrix_init(L);
+ lua_pop(L, 1);
}
diff --git a/matrix/mmatrix.c b/matrix/mmatrix.c
index 81f8dfc..ffc058d 100644
--- a/matrix/mmatrix.c
+++ b/matrix/mmatrix.c
@@ -1,4 +1,15 @@
#define NERV_GENERIC_MMATRIX
+#include "../common.h"
+void nerv_matrix_host_float_init(lua_State *L);
+void nerv_matrix_host_double_init(lua_State *L);
+void nerv_matrix_host_int_init(lua_State *L);
+
+void nerv_mmatrix_init(lua_State *L) {
+ nerv_matrix_host_float_init(L);
+ nerv_matrix_host_double_init(L);
+ nerv_matrix_host_int_init(L);
+}
+
#define MATRIX_USE_FLOAT
#define host_matrix_(NAME) host_matrix_float_##NAME
#define nerv_matrix_(NAME) nerv_matrix_host_float_##NAME
@@ -10,6 +21,7 @@ const char *nerv_matrix_(tname) = "nerv.MMatrixFloat";
#undef MATRIX_ELEM
#undef MATRIX_ELEM_PTR
#undef MATRIX_ELEM_FMT
+#undef MATRIX_ELEM_WRITE_FMT
#define NERV_GENERIC_MMATRIX
#define MATRIX_USE_DOUBLE
@@ -23,6 +35,7 @@ const char *nerv_matrix_(tname) = "nerv.MMatrixDouble";
#undef MATRIX_ELEM
#undef MATRIX_ELEM_PTR
#undef MATRIX_ELEM_FMT
+#undef MATRIX_ELEM_WRITE_FMT
#define NERV_GENERIC_MMATRIX
#define MATRIX_USE_INT