diff options
Diffstat (limited to 'matrix')
-rw-r--r-- | matrix/cukernel.cu | 21 | ||||
-rw-r--r-- | matrix/cukernel.h | 4 | ||||
-rw-r--r-- | matrix/cumatrix.c | 10 |
3 files changed, 35 insertions, 0 deletions
diff --git a/matrix/cukernel.cu b/matrix/cukernel.cu new file mode 100644 index 0000000..91e7e35 --- /dev/null +++ b/matrix/cukernel.cu @@ -0,0 +1,21 @@ +#include "generic/matrix.h" +#define CUDA_THREADS_N 16 +#define CEIL_DIV(a, b) (((a) + (b) - 1) / (b)) +__global__ void sigmoid(const float *a, float *b, + int nrow, int ncol, int stride) { + int j = blockIdx.x * blockDim.x + threadIdx.x; + int i = blockIdx.y * blockDim.y + threadIdx.y; + long idx; + if (i >= nrow || j >= ncol) return; + idx = j + i * stride; + b[idx] = 1.0 / (1.0 + exp(-a[idx])); +} + +extern "C" void cuda_sigmoid(const Matrix *a, Matrix *b) { + dim3 threadsPerBlock(CUDA_THREADS_N, + CUDA_THREADS_N); + dim3 numBlocks(CEIL_DIV(b->ncol, threadsPerBlock.x), + CEIL_DIV(b->nrow, threadsPerBlock.y)); + sigmoid<<<numBlocks, threadsPerBlock>>>(a->data.f, b->data.f, b->nrow, b->ncol, + b->stride / sizeof(float)); +} diff --git a/matrix/cukernel.h b/matrix/cukernel.h new file mode 100644 index 0000000..5b9e3a6 --- /dev/null +++ b/matrix/cukernel.h @@ -0,0 +1,4 @@ +#ifndef NERV_CUKERNEL_H +#define NERV_CUKERNEL_H +void cuda_sigmoid(const Matrix *a, Matrix *b); +#endif diff --git a/matrix/cumatrix.c b/matrix/cumatrix.c index 9c2878a..7759ca1 100644 --- a/matrix/cumatrix.c +++ b/matrix/cumatrix.c @@ -7,6 +7,7 @@ #define nerv_float_matrix_(NAME) nerv_float_matrix_cuda_ ## NAME #include "../common.h" #include "generic/matrix.h" +#include "cukernel.h" #include "cuda.h" #include "driver_types.h" #include "cublas_v2.h" @@ -56,9 +57,18 @@ static int nerv_float_matrix_(mul)(lua_State *L) { return 1; } +static int nerv_float_matrix_(sigmoid)(lua_State *L) { + Matrix *a = luaT_checkudata(L, 1, nerv_float_matrix_(tname)); + Matrix *b = nerv_float_matrix_(new_)(a->nrow, a->ncol); + cuda_sigmoid(a, b); + luaT_pushudata(L, b, nerv_float_matrix_(tname)); + return 1; +} + static const luaL_Reg nerv_float_matrix_(extra_methods)[] = { {"__add__", nerv_float_matrix_(add)}, {"__mul__", nerv_float_matrix_(mul)}, + {"sigmoid", nerv_float_matrix_(sigmoid)}, {NULL, NULL} }; |