aboutsummaryrefslogtreecommitdiff
path: root/nerv/matrix/generic/cumatrix.c
blob: 3481ede50aa77b1db775b2aa02aee906aadc5f4d (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#ifdef NERV_GENERIC_CUMATRIX
#include "../matrix.h"
#include "../../lib/matrix/generic/matrix.h"
#include "../../lib/matrix/generic/elem_type.h"
#define MATRIX_DATA_WRITE(L, data, idx, val) cuda_matrix_(write)(L, data, idx, val)
#define MATRIX_DATA_READ(L, data, idx) cuda_matrix_(read)(L, 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 "../../lib/common.h"
#include "../../lib/matrix/generic/cumatrix.h"

#define BLAS_OP_N CUBLAS_OP_N
static int nerv_matrix_(lua_get_blas_op)(char ch) {
    return (ch == 'T' || ch == 't') ? CUBLAS_OP_T : CUBLAS_OP_N;
}

static int nerv_matrix_(lua_prefixsum_row)(lua_State *L) {
    Status status;
    MATRIX_CONTEXT *context;
    MATRIX_GET_CONTEXT(L, 3);
    Matrix *a = luaT_checkudata(L, 1, nerv_matrix_(tname));
    Matrix *b = luaT_checkudata(L, 2, nerv_matrix_(tname));
    nerv_matrix_(prefixsum_row)(a, b, context, &status);
    NERV_LUA_CHECK_STATUS(L, status);
    return 0;
}

static int nerv_matrix_(lua_thres_mask)(lua_State *L) {
    Status status;
    MATRIX_CONTEXT *context;
    MATRIX_GET_CONTEXT(L, 6);
    Matrix *a = luaT_checkudata(L, 1, nerv_matrix_(tname));
    Matrix *b = luaT_checkudata(L, 2, nerv_matrix_(tname));
    MATRIX_ELEM thres = luaL_checknumber(L, 3);
    MATRIX_ELEM low = luaL_checknumber(L, 4);
    MATRIX_ELEM high = luaL_checknumber(L, 5);
    nerv_matrix_(thres_mask)(a, b, thres, low, high, context, &status);
    NERV_LUA_CHECK_STATUS(L, status);
    return 0;
}

static int nerv_matrix_(lua_rand_uniform)(lua_State *L) {
    Status status;
    MATRIX_CONTEXT *context;
    MATRIX_GET_CONTEXT(L, 2);
    Matrix *a = luaT_checkudata(L, 1, nerv_matrix_(tname));
    nerv_matrix_(rand_uniform)(a, context, &status);
    NERV_LUA_CHECK_STATUS(L, status);
    return 0;
}

extern const char *MATRIX_CUMATRIX_HOST_TNAME;
static int nerv_matrix_(lua_copy_fromh)(lua_State *L) { 
    Status status;
    MATRIX_CONTEXT *context;
    MATRIX_GET_CONTEXT(L, 6);
    Matrix *a = luaT_checkudata(L, 1, nerv_matrix_(tname));
    const Matrix *b = luaT_checkudata(L, 2, MATRIX_CUMATRIX_HOST_TNAME);
    int nargs = lua_gettop(L);
    int b_begin = nargs > 2 ? luaL_checkinteger(L, 3) : 0;
    int b_end = nargs > 3 ? luaL_checkinteger(L, 4) : b->nrow;
    int a_begin = nargs > 4 ? luaL_checkinteger(L, 5) : 0;
    nerv_matrix_(copy_fromh)(a, b, a_begin, b_begin, b_end, context, &status);
    NERV_LUA_CHECK_STATUS(L, status);
    return 0;
}

static int nerv_matrix_(lua_copy_toh)(lua_State *L) {
    Status status;
    MATRIX_CONTEXT *context;
    MATRIX_GET_CONTEXT(L, 6);
    Matrix *a = luaT_checkudata(L, 1, nerv_matrix_(tname));
    const Matrix *b = luaT_checkudata(L, 2, MATRIX_CUMATRIX_HOST_TNAME);
    int nargs = lua_gettop(L);
    int a_begin = nargs > 2 ? luaL_checkinteger(L, 3) : 0;
    int a_end = nargs > 3 ? luaL_checkinteger(L, 4) : a->nrow;
    int b_begin = nargs > 4 ? luaL_checkinteger(L, 5) : 0;
    nerv_matrix_(copy_toh)(a, b, a_begin, a_end, b_begin, context, &status);
    NERV_LUA_CHECK_STATUS(L, status);
    return 0;
}

static int nerv_matrix_(lua_copy_fromd)(lua_State *L) {
    Status status;
    MATRIX_CONTEXT *context;
    MATRIX_GET_CONTEXT(L, 6);
    Matrix *a = luaT_checkudata(L, 1, nerv_matrix_(tname));
    const Matrix *b = luaT_checkudata(L, 2, nerv_matrix_(tname));
    int nargs = lua_gettop(L);
    int b_begin = nargs > 2 ? luaL_checkinteger(L, 3) : 0;
    int b_end = nargs > 3 ? luaL_checkinteger(L, 4) : b->nrow;
    int a_begin = nargs > 4 ? luaL_checkinteger(L, 5) : 0;
    nerv_matrix_(copy_fromd)(a, b, a_begin, b_begin, b_end, context, &status);
    NERV_LUA_CHECK_STATUS(L, status);
    return 0;
}

extern const char *nerv_matrix_host_float_tname;
static int nerv_matrix_(lua_copy_rows_fromh_by_idx)(lua_State *L) {
    Status status;
    MATRIX_CONTEXT *context;
    MATRIX_GET_CONTEXT(L, 5);
    Matrix *a = luaT_checkudata(L, 1, nerv_matrix_(tname));
    const Matrix *b = luaT_checkudata(L, 2, MATRIX_CUMATRIX_HOST_TNAME);
    const Matrix *idx = luaT_checkudata(L, 3, nerv_matrix_host_float_tname);
    long nrow = a->nrow;
    int b_begin = lua_gettop(L) > 3 ? luaL_checkinteger(L, 4) : 0;
    nerv_matrix_(copy_rows_fromh_by_idx)(a, b, idx, b_begin, context, &status);
    NERV_LUA_CHECK_STATUS(L, status);
    return 0;
}

static int nerv_matrix_(lua_copy_rows_fromd_by_idx)(lua_State *L) {
    Status status;
    MATRIX_CONTEXT *context;
    MATRIX_GET_CONTEXT(L, 5);
    Matrix *a = luaT_checkudata(L, 1, nerv_matrix_(tname));
    const Matrix *b = luaT_checkudata(L, 2, nerv_matrix_(tname));
    const Matrix *idx = luaT_checkudata(L, 3, nerv_matrix_(tname));
    long nrow = a->nrow;
    int idx_begin = lua_gettop(L) > 3 ? luaL_checkinteger(L, 4) : 0;
    nerv_matrix_(copy_rows_fromd_by_idx)(a, b, idx, idx_begin, context, &status);
    NERV_LUA_CHECK_STATUS(L, status);
    return 0;
}

static int nerv_matrix_(lua_copy_rows_fromd_by_colidx)(lua_State *L) {
    Status status;
    MATRIX_CONTEXT *context;
    MATRIX_GET_CONTEXT(L, 5);
    Matrix *a = luaT_checkudata(L, 1, nerv_matrix_(tname));
    const Matrix *b = luaT_checkudata(L, 2, nerv_matrix_(tname));
    const Matrix *idx = luaT_checkudata(L, 3, nerv_matrix_(tname));
    long nrow = a->nrow;
    int idx_begin = lua_gettop(L) > 3 ? luaL_checkinteger(L, 4) : 0;
    nerv_matrix_(copy_rows_fromd_by_colidx)(a, b, idx, idx_begin, context, &status);
    NERV_LUA_CHECK_STATUS(L, status);
    return 0;
}

#ifdef __NERV_FUTURE_CUDA_7
static int nerv_matrix_(lua_update_select_rows_by_rowidx)(lua_State *L) {
    /* update c's select rows,
     * i.e. c[idx[i]] = c[idx[i]] * (1 - beta * alpha) + a[i] * alpha */
    Status status;
    MATRIX_CONTEXT *context;
    MATRIX_GET_CONTEXT(L, 6);
    Matrix *c = luaT_checkudata(L, 1, nerv_matrix_(tname));
    const Matrix *a = luaT_checkudata(L, 2, nerv_matrix_(tname));
    const Matrix *idx = luaT_checkudata(L, 3, nerv_matrix_(tname));
    MATRIX_ELEM alpha = luaL_checknumber(L, 4);
    MATRIX_ELEM beta = luaL_checknumber(L, 5);
    nerv_matrix_(update_select_rows_by_rowidx)(c, a, idx, alpha, beta, context, &status);
    NERV_LUA_CHECK_STATUS(L, status);
    return 0;
}

static int nerv_matrix_(lua_update_select_rows_by_colidx)(lua_State *L) {
    /* update c's select rows,
     * i.e. c[idx[i]] = c[idx[i]] * (1 - beta * alpha) + a[i] * alpha */
    Status status;
    MATRIX_CONTEXT *context;
    MATRIX_GET_CONTEXT(L, 6);
    Matrix *c = luaT_checkudata(L, 1, nerv_matrix_(tname));
    const Matrix *a = luaT_checkudata(L, 2, nerv_matrix_(tname));
    const Matrix *idx = luaT_checkudata(L, 3, nerv_matrix_(tname));
    MATRIX_ELEM alpha = luaL_checknumber(L, 4);
    MATRIX_ELEM beta = luaL_checknumber(L, 5);
    nerv_matrix_(update_select_rows_by_colidx)(c, a, idx, alpha, beta, context, &status);
    NERV_LUA_CHECK_STATUS(L,