aboutsummaryrefslogtreecommitdiff
path: root/nerv/matrix/mmatrix.c
blob: 45cb238e10f088a4a1c551a989d0a5fe0b714c4d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#define NERV_GENERIC_MMATRIX
#include <stdlib.h>
#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
#define MATRIX_MMATRIX_CUDA_TNAME nerv_matrix_cuda_float_tname
const char *nerv_matrix_(tname) = "nerv.MMatrixFloat";
#define MMATRIX_INIT(L) host_matrix_(init_extra)(L)

static const luaL_Reg nerv_matrix_(extra_methods_int)[];
static void host_matrix_(init_extra)(lua_State *L) {
    luaN_append_methods(L, nerv_matrix_(extra_methods_int));
}

#include "generic/mmatrix.c"
#include "../lib/matrix/mmatrix.h"

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, context, &status);
    NERV_LUA_CHECK_STATUS(L, status);
    luaT_pushudata(L, self, nerv_matrix_(tname));
    return 1;
}

static const luaL_Reg nerv_matrix_(extra_methods_int)[] = {
    {"perm_gen", nerv_matrix_(lua_perm_gen)},
    {NULL, NULL}
};

#undef nerv_matrix_
#undef host_matrix_
#undef MATRIX_USE_FLOAT
#undef MATRIX_ELEM
#undef MATRIX_ELEM_PTR
#undef MATRIX_ELEM_PTR_BASE
#undef MATRIX_ELEM_FMT
#undef MATRIX_ELEM_WRITE_FMT
#undef MMATRIX_INIT
#undef MATRIX_MMATRIX_CUDA_TNAME

#define NERV_GENERIC_MMATRIX
#define MATRIX_USE_DOUBLE
#define host_matrix_(NAME) host_matrix_double_##NAME
#define nerv_matrix_(NAME) nerv_matrix_host_double_##NAME
#define MATRIX_MMATRIX_CUDA_TNAME nerv_matrix_cuda_double_tname
const char *nerv_matrix_(tname) = "nerv.MMatrixDouble";
#include "generic/mmatrix.c"
#undef nerv_matrix_
#undef host_matrix_
#undef MATRIX_USE_DOUBLE
#undef MATRIX_ELEM
#undef MATRIX_ELEM_PTR
#undef MATRIX_ELEM_PTR_BASE
#undef MATRIX_ELEM_FMT
#undef MATRIX_ELEM_WRITE_FMT

#define NERV_GENERIC_MMATRIX
#define MATRIX_USE_INT
#define host_matrix_(NAME) host_matrix_int_##NAME
#define nerv_matrix_(NAME) nerv_matrix_host_int_##NAME
const char *nerv_matrix_(tname) = "nerv.MMatrixInt";
#include "generic/mmatrix.c"