From e91fc2ddaa74dd2c46ce93c9e92020d66c037c8e Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 24 Feb 2016 16:58:32 +0800 Subject: add CuContext/MContext --- nerv/matrix/mmatrix.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) (limited to 'nerv/matrix/mmatrix.c') diff --git a/nerv/matrix/mmatrix.c b/nerv/matrix/mmatrix.c index a68506d..45cb238 100644 --- a/nerv/matrix/mmatrix.c +++ b/nerv/matrix/mmatrix.c @@ -1,17 +1,64 @@ #define NERV_GENERIC_MMATRIX #include +#include "../lib/matrix/mmatrix.h" #include "../lib/common.h" + +const char *nerv_host_context_tname = "nerv.MContext"; + +int nerv_host_context_lua_print_profile(lua_State *L) { + nerv_host_context_print_profile(luaT_checkudata(L, 1, nerv_host_context_tname)); + return 0; +} + +int nerv_host_context_lua_clear_profile(lua_State *L) { + nerv_host_context_clear_profile(luaT_checkudata(L, 1, nerv_host_context_tname)); + return 0; +} + +int nerv_host_context_lua_new(lua_State *L) { + Status status; + MContext *self = nerv_host_context_create(&status); + NERV_LUA_CHECK_STATUS(L, status); + luaT_pushudata(L, self, nerv_host_context_tname); + return 1; +} + +int nerv_host_context_lua_destroy(lua_State *L) { + Status status; + MContext *self = luaT_checkudata(L, 1, nerv_host_context_tname); + nerv_host_context_destroy(self, &status); + NERV_LUA_CHECK_STATUS(L, status); + return 1; +} + +static const luaL_Reg nerv_host_context_methods[] = { + {"print_profile", nerv_host_context_lua_print_profile}, + {"clear_profile", nerv_host_context_lua_clear_profile}, + {NULL, NULL} +}; + +void nerv_host_context_lua_init(lua_State *L) { + luaT_newmetatable(L, nerv_host_context_tname, NULL, + nerv_host_context_lua_new, + nerv_host_context_lua_destroy, NULL); + luaL_register(L, NULL, nerv_host_context_methods); +} + void nerv_matrix_host_float_lua_init(lua_State *L); void nerv_matrix_host_double_lua_init(lua_State *L); void nerv_matrix_host_int_lua_init(lua_State *L); void nerv_lua_mmatrix_init(lua_State *L) { srand(1); + nerv_host_context_lua_init(L); nerv_matrix_host_float_lua_init(L); nerv_matrix_host_double_lua_init(L); nerv_matrix_host_int_lua_init(L); } +#define MATRIX_CONTEXT MContext +#define MATRIX_CONTEXT_TNAME nerv_host_context_tname + #define MATRIX_USE_FLOAT #define host_matrix_(NAME) host_matrix_float_##NAME #define nerv_matrix_(NAME) nerv_matrix_host_float_##NAME @@ -29,8 +76,10 @@ static void host_matrix_(init_extra)(lua_State *L) { static int nerv_matrix_(lua_perm_gen)(lua_State *L) { Status status; + MATRIX_CONTEXT *context; + MATRIX_GET_CONTEXT(L, 2); int i, ncol = luaL_checkinteger(L, 1); - Matrix *self = nerv_matrix_(perm_gen)(ncol, &status); + Matrix *self = nerv_matrix_(perm_gen)(ncol, context, &status); NERV_LUA_CHECK_STATUS(L, status); luaT_pushudata(L, self, nerv_matrix_(tname)); return 1; -- cgit v1.2.3