From 81c115e09f9449ae61c7352edd77c68e9029f2dc Mon Sep 17 00:00:00 2001 From: Determinant Date: Mon, 25 May 2015 17:27:29 +0800 Subject: add "copy from/to cumatrix" functionality --- matrix/generic/cumatrix.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'matrix/generic') 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} }; -- cgit v1.2.3