diff options
author | Determinant <[email protected]> | 2015-05-25 17:27:29 +0800 |
---|---|---|
committer | Determinant <[email protected]> | 2015-05-25 17:27:29 +0800 |
commit | 81c115e09f9449ae61c7352edd77c68e9029f2dc (patch) | |
tree | 284196680a794d4317d667e6a6e04fa5b3ee8c75 /matrix/generic/cumatrix.c | |
parent | c1e714052f0c2654ebe2a92d6961382146450b9e (diff) |
add "copy from/to cumatrix" functionality
Diffstat (limited to 'matrix/generic/cumatrix.c')
-rw-r--r-- | matrix/generic/cumatrix.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/matrix/generic/cumatrix.c b/matrix/generic/cumatrix.c index f846a73..557e4c1 100644 --- a/matrix/generic/cumatrix.c +++ b/matrix/generic/cumatrix.c @@ -126,6 +126,32 @@ static int nerv_matrix_(rowmax)(lua_State *L) { return 1; } +extern const char *MATRIX_CUMATRIX_HOST_TNAME; +static int nerv_matrix_(copy_from)(lua_State *L) { + Matrix *a = luaT_checkudata(L, 1, nerv_matrix_(tname)); + Matrix *b = luaT_checkudata(L, 2, MATRIX_CUMATRIX_HOST_TNAME); + if (!(a->nrow == b->nrow && a->ncol == b->ncol)) + nerv_error(L, "Matrices should be of the same dimension"); + cudaMemcpy2D(MATRIX_ELEM_PTR(a), a->stride, + MATRIX_ELEM_PTR(b), b->stride, + sizeof(MATRIX_ELEM) * b->ncol, b->nrow, + cudaMemcpyHostToDevice); + return 0; +} + +static int nerv_matrix_(copy_to)(lua_State *L) { + Matrix *a = luaT_checkudata(L, 1, nerv_matrix_(tname)); + Matrix *b = luaT_checkudata(L, 2, MATRIX_CUMATRIX_HOST_TNAME); + if (!(a->nrow == b->nrow && a->ncol == b->ncol)) + nerv_error(L, "Matrices should be of the same dimension"); + cudaMemcpy2D(MATRIX_ELEM_PTR(b), b->stride, + MATRIX_ELEM_PTR(a), a->stride, + sizeof(MATRIX_ELEM) * a->ncol, a->nrow, + cudaMemcpyDeviceToHost); + return 0; +} + + static const luaL_Reg nerv_matrix_(extra_methods)[] = { {"add", nerv_matrix_(add)}, {"mul", nerv_matrix_(mul)}, @@ -135,6 +161,8 @@ static const luaL_Reg nerv_matrix_(extra_methods)[] = { {"colsum", nerv_matrix_(colsum)}, {"rowsum", nerv_matrix_(rowsum)}, {"rowmax", nerv_matrix_(rowmax)}, + {"copy_from", nerv_matrix_(copy_from)}, + {"copy_to", nerv_matrix_(copy_to)}, {NULL, NULL} }; |