aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDeterminant <ted.sybil@gmail.com>2015-06-24 22:48:24 +0800
committerDeterminant <ted.sybil@gmail.com>2015-06-24 22:48:24 +0800
commit5e407d74130accfbbf94d2cabcb03fc126a89410 (patch)
tree6d8998e904a31a95f85a6e64ac7f3940fb61af80
parent8f13607cba9d6cf4fc4a213ba5ae4bcd46f7e18d (diff)
separate non-Lua part of matrix code to a dedicated dir
-rw-r--r--nerv/Makefile43
-rw-r--r--nerv/common.c25
-rw-r--r--nerv/common.h45
-rw-r--r--nerv/lib/io/chunk_file.c18
-rw-r--r--nerv/lib/matrix/cuda_helper.h (renamed from nerv/matrix/cuda_helper.h)55
-rw-r--r--nerv/lib/matrix/cukernel.cu (renamed from nerv/matrix/cukernel.cu)0
-rw-r--r--nerv/lib/matrix/cukernel.h (renamed from nerv/matrix/cukernel.h)0
-rw-r--r--nerv/lib/matrix/cumatrix.c69
-rw-r--r--nerv/lib/matrix/cumatrix.h6
-rw-r--r--nerv/lib/matrix/generic/cukernel.cu571
-rw-r--r--nerv/lib/matrix/generic/cumatrix.c403
-rw-r--r--nerv/lib/matrix/generic/cumatrix.h50
-rw-r--r--nerv/lib/matrix/generic/elem_type.h22
-rw-r--r--nerv/lib/matrix/generic/matrix.c57
-rw-r--r--nerv/lib/matrix/generic/matrix.h4
-rw-r--r--nerv/lib/matrix/generic/mmatrix.c82
-rw-r--r--nerv/lib/matrix/generic/mmatrix.h7
-rw-r--r--nerv/lib/matrix/init.lua77
-rw-r--r--nerv/lib/matrix/matrix.h (renamed from nerv/matrix/generic/matrix.h)2
-rw-r--r--nerv/lib/matrix/mmatrix.c53
-rw-r--r--nerv/lib/matrix/mmatrix.h4
-rw-r--r--nerv/matrix/cumatrix.c44
-rw-r--r--nerv/matrix/generic/cumatrix.c479
-rw-r--r--nerv/matrix/generic/matrix.c102
-rw-r--r--nerv/matrix/generic/mmatrix.c80
-rw-r--r--nerv/matrix/init.c9
-rw-r--r--nerv/matrix/mmatrix.c34
-rw-r--r--nerv/nerv-scm-1.rockspec1
m---------speech0
29 files changed, 1805 insertions, 537 deletions
diff --git a/nerv/Makefile b/nerv/Makefile
index 4008453..06a439a 100644
--- a/nerv/Makefile
+++ b/nerv/Makefile
@@ -1,58 +1,65 @@
.PHONY: build install clean
SHELL := /bin/bash
BUILD_DIR := $(CURDIR)/build
-OBJS := nerv.o lib/luaT/luaT.o lib/io/chunk_file.o common.o \
- matrix/mmatrix.o matrix/cumatrix.o matrix/init.o matrix/cukernel.o \
+LIB_PATH := $(LUA_BINDIR)/../lib
+OBJ_DIR := $(BUILD_DIR)/objs
+CORE_OBJS := common.o lib/io/chunk_file.o lib/matrix/mmatrix.o lib/matrix/cumatrix.o lib/matrix/cukernel.o
+NERV_OBJS := nerv.o \
+ matrix/mmatrix.o matrix/cumatrix.o matrix/init.o \
io/init.o io/chunk_file.o \
examples/oop_example.o
-LIBS := libnerv.so
+LUAT_OBJS := lib/luaT/luaT.o
+CORE_OBJS := $(addprefix $(OBJ_DIR)/,$(CORE_OBJS))
+NERV_OBJS := $(addprefix $(OBJ_DIR)/,$(NERV_OBJS))
+LUAT_OBJS := $(addprefix $(OBJ_DIR)/,$(LUAT_OBJS))
+OBJS := $(CORE_OBJS) $(NERV_OBJS) $(LUAT_OBJS)
+LIBS := $(INST_LIBDIR)/libnerv.so $(LIB_PATH)/libnervcore.so $(LIB_PATH)/libluaT.so
LUA_LIBS := matrix/init.lua io/init.lua init.lua \
layer/init.lua layer/affine.lua layer/sigmoid.lua layer/softmax_ce.lua \
layer/window.lua layer/bias.lua layer/combiner.lua layer/mse.lua \
nn/init.lua nn/layer_repo.lua nn/param_repo.lua nn/layer_dag.lua \
io/sgd_buffer.lua
INCLUDE := -I $(LUA_INCDIR) -DLUA_USE_APICHECK
-CUDA_BASE := /usr/local/cuda-6.5
-#CUDA_BASE := /usr/local/cuda-5.0
+#CUDA_BASE := /usr/local/cuda-6.5
+CUDA_BASE := /usr/local/cuda-5.0
CUDA_INCLUDE := -I $(CUDA_BASE)/include/
INCLUDE += $(CUDA_INCLUDE)
LDFLAGS := -L$(CUDA_BASE)/lib64/ -Wl,-rpath=$(CUDA_BASE)/lib64/ -lcudart -lcublas
CFLAGS := -Wall -Wextra -O2
-OBJ_DIR := $(BUILD_DIR)/objs
-SUBDIR := matrix io layer examples nn lib/io lib/luaT
+SUBDIR := matrix io layer examples nn lib/io lib/luaT lib/matrix
NVCC := $(CUDA_BASE)/bin/nvcc
NVCC_FLAGS := -Xcompiler -fPIC,-Wall,-Wextra
LUA_DIR = $(INST_LUADIR)/nerv
-OBJS := $(addprefix $(OBJ_DIR)/,$(OBJS))
OBJ_SUBDIR := $(addprefix $(OBJ_DIR)/,$(SUBDIR))
LUA_SUBDIR := $(addprefix $(LUA_DIR)/,$(SUBDIR))
-LIBS := $(addprefix $(INST_LIBDIR)/,$(LIBS))
LUA_LIBS := $(addprefix $(LUA_DIR)/,$(LUA_LIBS))
build: $(OBJ_DIR) $(OBJ_SUBDIR) $(OBJS)
+
$(OBJ_DIR) $(LUA_DIR) $(OBJ_SUBDIR) $(LUA_SUBDIR):
-mkdir -p $@
$(OBJ_DIR)/%.o: %.c $(patsubst /%.o,/%.c,$@)
gcc -c -o $@ $< $(INCLUDE) -fPIC $(CFLAGS)
-$(OBJ_DIR)/matrix/cukernel.o: matrix/cukernel.cu
+$(OBJ_DIR)/lib/matrix/cukernel.o: lib/matrix/cukernel.cu
$(NVCC) -c -o $@ $< $(INCLUDE) $(NVCC_FLAGS)
$(LUA_DIR)/%.lua: %.lua
cp $< $@
#$(OBJ_DIR)/luaT.o:
# gcc -c -o $@ luaT/luaT.c $(INCLUDE) -fPIC
-$(LIBS): $(OBJS)
- gcc -shared -o $@ $(OBJS) $(LDFLAGS)
+$(LIB_PATH)/libnervcore.so: $(CORE_OBJS)
+ gcc -shared -o $@ $^ $(LDFLAGS)
+$(LIB_PATH)/libluaT.so: $(LUAT_OBJS)
+ gcc -shared -o $@ $^ $(LDFLAGS)
+$(INST_LIBDIR)/libnerv.so: $(NERV_OBJS) $(LIB_PATH)/libnervcore.so $(LIB_PATH)/libluaT.so
+ gcc -shared -o $@ $(NERV_OBJS) $(LDFLAGS) -Wl,-rpath=$(LIB_PATH) -L$(LIB_PATH) -lnervcore -lluaT
$(OBJ_DIR)/matrix/cumatrix.o: matrix/generic/cumatrix.c matrix/generic/matrix.c matrix/generic/cukernel.cu
$(OBJ_DIR)/matrix/mmatrix.o: matrix/generic/mmatrix.c matrix/generic/matrix.c
-$(OBJ_DIR)/matrix/cukernel.o: matrix/generic/cukernel.cu
-
-.PHONY: speech
-speech:
- -mkdir -p build/objs/speech/tnet_io
- $(MAKE) -C speech/ BUILD_DIR=$(BUILD_DIR) LIB_DIR=$(LIB_DIR) OBJ_DIR=$(CURDIR)/build/objs/speech/ LUA_DIR=$(LUA_DIR)
+$(OBJ_DIR)/lib/matrix/cumatrix.o: lib/matrix/generic/cumatrix.c lib/matrix/generic/matrix.c lib/matrix/generic/cukernel.cu
+$(OBJ_DIR)/lib/matrix/mmatrix.o: lib/matrix/generic/mmatrix.c lib/matrix/generic/matrix.c
+$(OBJ_DIR)/lib/matrix/cukernel.o: lib/matrix/generic/cukernel.cu
clean:
-rm -rf $(OBJ_DIR)
diff --git a/nerv/common.c b/nerv/common.c
index b4e39e6..0584438 100644
--- a/nerv/common.c
+++ b/nerv/common.c
@@ -11,6 +11,31 @@ int nerv_error(lua_State *L, const char *err_mesg_fmt, ...) {
return 0;
}
+int nerv_error_status(lua_State *L, Status *status) {
+ const char *mmesg = NULL;
+ switch (status->err_code)
+ {
+ case MAT_GENERAL_ERR: mmesg = "general error"; break;
+ case MAT_INSUF_MEM: mmesg = "insufficient memory"; break;
+ case MAT_INVALID_FORMAT: mmesg = "invalid matrix format"; break;
+ case MAT_WRITE_ERROR: mmesg = "error while writing matrix"; break;
+ case MAT_INVALID_COPY_INTERVAL: mmesg = "invalid copy interval"; break;
+ case MAT_MISMATCH_DIM: mmesg = "mismatching matrix dimension"; break;
+ case MAT_WRONG_MULT_DIM: mmesg = "wrong multipier dimension"; break;
+ case MAT_ROW_VECTOR_EXP: mmesg = "row vector expected"; break;
+ case MAT_COL_VECTOR_EXP: mmesg = "column vector expected"; break;
+ case MAT_IDX_VECTOR_EXP: mmesg = "index vector expected"; break;
+ case MAT_INVALID_IDX: mmesg = "invalid index"; break;
+ case MAT_CUDA_ERR: mmesg = "cuda error"; break;
+ case MAT_CUBLAS_ERR: mmesg = "cublas error"; break;
+ }
+ if (status->msg)
+ nerv_error(L, "%s: %s @%s:%d", mmesg, status->msg,
+ status->file, status->lineno);
+ else
+ nerv_error(L, "%s @%s:%d", mmesg, status->file, status->lineno);
+}
+
int nerv_error_method_not_implemented(lua_State *L) {
return nerv_error(L, "method not implemented");
}
diff --git a/nerv/common.h b/nerv/common.h
index 6657dc4..413ca51 100644
--- a/nerv/common.h
+++ b/nerv/common.h
@@ -7,6 +7,50 @@
#include <stdio.h>
#include <stdlib.h>
+enum {
+ MAT_NORMAL,
+ MAT_GENERAL_ERR,
+ MAT_INSUF_MEM,
+ MAT_INVALID_FORMAT,
+ MAT_WRITE_ERROR,
+ MAT_INVALID_COPY_INTERVAL,
+ MAT_MISMATCH_DIM,
+ MAT_WRONG_MULT_DIM,
+ MAT_ROW_VECTOR_EXP,
+ MAT_COL_VECTOR_EXP,
+ MAT_IDX_VECTOR_EXP,
+ MAT_INVALID_IDX,
+ MAT_CUDA_ERR,
+ MAT_CUBLAS_ERR
+};
+
+typedef struct Status {
+ int err_code;
+ const char *file;
+ int lineno;
+ const char *msg;
+} Status;
+
+#define NERV_SET_STATUS(status, code, m) \
+ do { \
+ (status)->err_code = code; \
+ (status)->msg = m; \
+ (status)->file = __FILE__; \
+ (status)->lineno = __LINE__; \
+ } while (0)
+
+#define NERV_EXIT_STATUS(status, code, msg) \
+ do { \
+ NERV_SET_STATUS(status, code, msg); \
+ return; \
+ } while (0)
+
+#define NERV_LUA_CHECK_STATUS(L, status) \
+ do { \
+ if (status.err_code != MAT_NORMAL) \
+ nerv_error_status(L, &status); \
+ } while (0)
+
typedef struct HashNode {
const char *key;
void *val;
@@ -31,6 +75,7 @@ void hashmap_clear(HashMap *h);
size_t bkdr_hash(const char *key);
int nerv_error(lua_State *L, const char *err_mesg_fmt, ...);
+int nerv_error_status(lua_State *L, Status *status);
int nerv_error_method_not_implemented(lua_State *L);
void luaN_append_methods(lua_State *L, const luaL_Reg *mlist);
#endif
diff --git a/nerv/lib/io/chunk_file.c b/nerv/lib/io/chunk_file.c
index a305962..e70ffc9 100644
--- a/nerv/lib/io/chunk_file.c
+++ b/nerv/lib/io/chunk_file.c
@@ -91,6 +91,7 @@ static ChunkFile *open_write(const char *fn, int *status) {
}
cfp = (ChunkFile *)malloc(sizeof(ChunkFile));
cfp->fp = fp;
+ cfp->info = NULL;
cfp->status = CF_WRITE;
*status = CF_NORMAL;
return cfp;
@@ -111,8 +112,6 @@ static ChunkFile *open_read(const char *fn, int *status) {
return NULL;
}
cfp = (ChunkFile *)malloc(sizeof(ChunkFile));
- cfp->fp = fp;
- cfp->status = CF_READ;
offset = ftello(fp);
/* fprintf(stderr, "%d\n", (int)offset); */
for (i = 0;; offset += chunk_len, i++)
@@ -144,7 +143,9 @@ static ChunkFile *open_read(const char *fn, int *status) {
head = cip;
}
*status = CF_NORMAL;
+ cfp->fp = fp;
cfp->info = head;
+ cfp->status = CF_READ;
return cfp;
}
@@ -208,13 +209,16 @@ void nerv_chunk_file_close(ChunkFile *cfp) {
void nerv_chunk_file_destroy(ChunkFile *cfp) {
ChunkInfo *i, *ni;
- if (cfp->status != CF_CLOSED) fclose(cfp->fp);
- for (i = cfp->info; i; i = ni)
+ if (cfp->info)
{
- ni = i->next;
- free(i->metadata);
- free(i);
+ for (i = cfp->info; i; i = ni)
+ {
+ ni = i->next;
+ free(i->metadata);
+ free(i);
+ }
}
+ if (cfp->status != CF_CLOSED) fclose(cfp->fp);
free(cfp);
}
diff --git a/nerv/matrix/cuda_helper.h b/nerv/lib/matrix/cuda_helper.h
index fde6f18..8041efb 100644
--- a/nerv/matrix/cuda_helper.h
+++ b/nerv/lib/matrix/cuda_helper.h
@@ -4,33 +4,68 @@
#include "cuda_runtime.h"
#include "driver_types.h"
#include "cublas_v2.h"
-#define CUBLAS_SAFE_SYNC_CALL(call) \
+
+#define CUBLAS_SAFE_SYNC_CALL_RET(call, status) \
+ do { \
+ cublasStatus_t err = (call); \
+ if (err != CUBLAS_STATUS_SUCCESS) \
+ { \
+ NERV_SET_STATUS(status, MAT_CUBLAS_ERR, cublasGetErrorString(err)); \
+ return 0; \
+ } \
+ cudaDeviceSynchronize(); \
+ } while (0)
+
+#define CUBLAS_SAFE_SYNC_CALL(call, status) \
do { \
cublasStatus_t err = (call); \
if (err != CUBLAS_STATUS_SUCCESS) \
- nerv_error(L, "cumatrix cublas error: %s at %s:%d", \
- cublasGetErrorString(err), __FILE__, __LINE__); \
+ NERV_EXIT_STATUS(status, MAT_CUBLAS_ERR, cublasGetErrorString(err)); \
cudaDeviceSynchronize(); \
} while (0)
-#define CUDA_SAFE_CALL(call) \
+#define CUDA_SAFE_CALL_RET(call, status) \
+ do { \
+ cudaError_t err = (call); \
+ if (err != cudaSuccess) \
+ { \
+ NERV_SET_STATUS(status, MAT_CUDA_ERR, cudaGetErrorString(err)); \
+ return 0; \
+ } \
+ } while (0)
+
+#define CUDA_SAFE_CALL(call, status) \
do { \
cudaError_t err = (call); \
if (err != cudaSuccess) \
- nerv_error(L, "cumatrix CUDA error: %s at %s:%d", \
- cudaGetErrorString(err), __FILE__, __LINE__); \
+ NERV_EXIT_STATUS(status, MAT_CUDA_ERR, cudaGetErrorString(err)); \
+ } while (0)
+
+#define CUDA_SAFE_SYNC_CALL(call, status) \
+ do { \
+ CUDA_SAFE_CALL(call, status); \
+ cudaDeviceSynchronize(); \
} while (0)
-#define CUDA_SAFE_SYNC_CALL(call) \
+#define CUDA_SAFE_SYNC_CALL_RET(call, status) \
do { \
- CUDA_SAFE_CALL(call); \
+ CUDA_SAFE_CALL_RET(call, status); \
cudaDeviceSynchronize(); \
} while (0)
-#define CHECK_SAME_DIMENSION(a, b) \
+#define CHECK_SAME_DIMENSION(a, b, status) \
+ do { \
+ if (!(a->nrow == b->nrow && a->ncol == b->ncol)) \
+ NERV_EXIT_STATUS(status, MAT_MISMATCH_DIM, 0); \
+ } while (0)
+
+#define CHECK_SAME_DIMENSION_RET(a, b, status) \
do { \
if (!(a->nrow == b->nrow && a->ncol == b->ncol)) \
- nerv_error(L, "matrices should be of the same dimension"); \
+ { \
+ NERV_SET_STATUS(status, MAT_MISMATCH_DIM, 0); \
+ return 0; \
+ } \
} while (0)
static const char *cublasGetErrorString(cublasStatus_t err) {
diff --git a/nerv/matrix/cukernel.cu b/nerv/lib/matrix/cukernel.cu
index a19030a..a19030a 100644
--- a/nerv/matrix/cukernel.cu
+++ b/nerv/lib/matrix/cukernel.cu
diff --git a/nerv/matrix/cukernel.h b/nerv/lib/matrix/cukernel.h
index 8a1494f..8a1494f 100644
--- a/nerv/matrix/cukernel.h
+++ b/nerv/lib/matrix/cukernel.h
diff --git a/nerv/lib/matrix/cumatrix.c b/nerv/lib/matrix/cumatrix.c
new file mode 100644
index 0000000..9641197
--- /dev/null
+++ b/nerv/lib/matrix/cumatrix.c
@@ -0,0 +1,69 @@
+#define NERV_GENERIC_CUMATRIX
+#include "../../common.h"
+#include "cuda_helper.h"
+#include <string.h>
+#define PROFILE_HASHMAP_SIZE 123457
+static cublasHandle_t cublas_handle;
+static cudaEvent_t profile_start, profile_stop;
+static HashMap *profile;
+
+void nerv_cumatrix_print_profile() {
+ size_t i;
+ fprintf(stderr, "*** [nerv cumatrix profile] **\n");
+ for (i = 0; i < profile->size; i++)
+ {
+ HashNode *ptr;
+ for (ptr = profile->bucket[i]; ptr; ptr = ptr->next)
+ {
+ fprintf(stderr, "%s:\t%.6f\n", ptr->key, *(float *)ptr->val);
+ }
+ }
+}
+
+void nerv_cumatrix_clear_profile() {
+ hashmap_clear(profile);
+}
+
+void accu_profile(const char *name, float delta) {
+ float *val = hashmap_getval(profile, name);
+ if (!val)
+ {
+ val = malloc(sizeof(float));
+ *val = 0;
+ hashmap_setval(profile, name, val);
+ }
+ *val += delta;
+}
+
+void nerv_cumatrix_init() {
+ cublasCreate(&cublas_handle);
+ cudaEventCreate(&profile_start);
+ cudaEventCreate(&profile_stop);
+ profile = hashmap_create(PROFILE_HASHMAP_SIZE, bkdr_hash, strcmp);
+}
+
+#define MATRIX_USE_FLOAT
+#define cuda_matrix_(NAME) cuda_matrix_float_##NAME
+#define nerv_matrix_(NAME) nerv_matrix_cuda_float_##NAME
+#define cudak_(NAME) cudak_float_ ## NAME
+#define NERV_CUBLAS_(NAME) cublasS##NAME
+#define MATRIX_CUMATRIX_HOST_TNAME nerv_matrix_host_float_tname
+#include "generic/cumatrix.c"
+#undef NERV_CUBLAS_
+#undef cudak_
+#undef nerv_matrix_
+#undef cuda_matrix_
+#undef MATRIX_USE_FLOAT
+#undef MATRIX_ELEM
+#undef MATRIX_ELEM_PTR
+#undef MATRIX_ELEM_FMT
+#undef MATRIX_ELEM_WRITE_FMT
+#undef MATRIX_CUMATRIX_HOST_TNAME
+
+#define MATRIX_USE_DOUBLE
+#define cuda_matrix_(NAME) cuda_matrix_double_##NAME
+#define nerv_matrix_(NAME) nerv_matrix_cuda_double_##NAME
+#define cudak_(NAME) cudak_double_ ## NAME
+#define NERV_CUBLAS_(NAME) cublasD##NAME
+#define MATRIX_CUMATRIX_HOST_TNAME nerv_matrix_host_double_tname
+#include "generic/cumatrix.c"
diff --git a/nerv/lib/matrix/cumatrix.h b/nerv/lib/matrix/cumatrix.h
new file mode 100644
index 0000000..9f71507
--- /dev/null
+++ b/nerv/lib/matrix/cumatrix.h
@@ -0,0 +1,6 @@
+#ifndef NERV_CUMATRIX_H
+#define NERV_CUMATRIX_H
+void nerv_cumatrix_print_profile();
+void nerv_cumatrix_clear_profile();
+void nerv_cumatrix_init();
+#endif
diff --git a/nerv/lib/matrix/generic/cukernel.cu b/nerv/lib/matrix/generic/cukernel.cu
new file mode 100644
index 0000000..6111193
--- /dev/null
+++ b/nerv/lib/matrix/generic/cukernel.cu
@@ -0,0 +1,571 @@
+#ifdef NERV_GENERIC_CUKERNEL
+#include <assert.h>
+#include <stdio.h>
+#include "../matrix.h"
+#include "cuda.h"
+#include "float.h"
+#define CUDA_THREADS_N 16
+#define CUDA_THREADS_NN ((CUDA_THREADS_N) * (CUDA_THREADS_N))
+#define CEIL_DIV(a, b) (((a) + (b) - 1) / (b))
+__global__ void cudak_(log_elem)(const MATRIX_ELEM *a, MATRIX_ELEM *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;
+ MATRIX_ELEM tmp;
+ if (i >= nrow || j >= ncol) return;
+ idx = j + i * stride;
+ tmp = a[idx];
+ if(tmp < FLT_MIN) tmp = FLT_MIN;
+ b[idx] = log(tmp);
+}
+
+__global__ void cudak_(mul_elem)(const MATRIX_ELEM *a, const MATRIX_ELEM *b,
+ MATRIX_ELEM *c,
+ 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;
+ c[idx] = a[idx] * b[idx];
+}
+
+__global__ void cudak_(sigmoid)(const MATRIX_ELEM *a, MATRIX_ELEM *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]));
+}
+
+__global__ void cudak_(sigmoid_grad)(const MATRIX_ELEM *output,
+ const MATRIX_ELEM *err,
+ MATRIX_ELEM *nerr,
+ 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;
+ nerr[idx] = output[idx] * (1.0 - output[idx]) * err[idx];
+}
+
+__global__ void cudak_(softmax_final)(const MATRIX_ELEM *a, MATRIX_ELEM *b,
+ const MATRIX_ELEM *max, const MATRIX_ELEM *deno,
+ int nrow, int ncol, int stride, int mstride) {
+ 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] = exp(a[idx] - max[0 + i * mstride]) / deno[0 + i * mstride];
+}
+
+__global__ void cudak_(block_reduce_rowsum)(const MATRIX_ELEM *input,
+ MATRIX_ELEM *output,
+ const int istride, const int ostride,
+ const int n) {
+ extern __shared__ MATRIX_ELEM cudak_(arr)[];
+ int j = blockIdx.x * blockDim.x + threadIdx.x;
+ cudak_(arr)[threadIdx.x] = j < n ? input[j + istride * blockIdx.y] : 0;
+ __syncthreads();
+ for (int offset = blockDim.x >> 1; offset; offset >>= 1)
+ {
+ if (threadIdx.x < offset)
+ cudak_(arr)[threadIdx.x] += cudak_(arr)[threadIdx.x + offset];
+ __syncthreads();
+ }
+ if (threadIdx.x == 0)
+ output[blockIdx.x + ostride * blockIdx.y] = cudak_(arr)[0];
+}
+
+__global__ void cudak_(block_reduce_colsum)(const MATRIX_ELEM *input,
+ MATRIX_ELEM *output,
+ const int istride, const int ostride,
+ const int n) {
+ extern __shared__ MATRIX_ELEM cudak_(arr)[];
+ int i = blockIdx.y * blockDim.y + threadIdx.y;
+ cudak_(arr)[threadIdx.y] = i < n ? input[blockIdx.x + istride * i] : 0;
+ __syncthreads();
+ for (int offset = blockDim.y >> 1; offset; offset >>= 1)
+ {
+ if (threadIdx.y < offset)
+ cudak_(arr)[threadIdx.y] += cudak_(arr)[threadIdx.y + offset];
+ __syncthreads();
+ }
+ if (threadIdx.y == 0)
+ output[blockIdx.x + ostride * blockIdx.y] = cudak_(arr)[0];
+}
+
+__global__ void cudak_(block_reduce_colsame)(const MATRIX_ELEM *input,
+ const MATRIX_ELEM *ref_input,
+ MATRIX_ELEM *output,
+ const int istride, const int ostride,
+ const int n) {
+ extern __shared__ MATRIX_ELEM cudak_(arr)[];
+ int i = blockIdx.y * blockDim.y + threadIdx.y;
+ cudak_(arr)[threadIdx.y] = (i < n && input[blockIdx.x + istride * i] == \
+ ref_input[blockIdx.x + istride * i]) ? 1.0 : 0;
+ __syncthreads();
+ for (int offset = blockDim.y >> 1; offset; offset >>= 1)
+ {
+ if (threadIdx.y < offset)
+ cudak_(arr)[threadIdx.y] += cudak_(arr)[threadIdx.y + offset];
+ __syncthreads();
+ }
+ if (threadIdx.y == 0)
+ output[blockIdx.x + ostride * blockIdx.y] = cudak_(arr)[0];
+}
+
+__global__ void cudak_(block_reduce_softmax_rowsum)(const MATRIX_ELEM *input,
+ MATRIX_ELEM *output,
+ const MATRIX_ELEM *max,
+ const int istride, const int ostride,
+ const int mstride, const int n) {
+ extern __shared__ MATRIX_ELEM cudak_(arr)[];
+ int j = blockIdx.x * blockDim.x + threadIdx.x;
+ cudak_(arr)[threadIdx.x] = j < n ? exp(input[j + istride * blockIdx.y] - \
+ max[0 + mstride * blockIdx.y]) : 0;
+ __syncthreads();
+ for (int offset = blockDim.x >> 1; offset; offset >>= 1)
+ {
+ if (threadIdx.x < offset)
+ cudak_(arr)[threadIdx.x] += cudak_(arr)[threadIdx.x + offset];
+ __syncthreads();
+ }
+ if (threadIdx.x == 0)
+ output[blockIdx.x + ostride * blockIdx.y] = cudak_(arr)[0];
+}
+
+__global__ void cudak_(block_reduce_rowmax)(const MATRIX_ELEM *input,
+ MATRIX_ELEM *output,
+ const int istride, const int ostride,
+ const int n) {
+ extern __shared__ MATRIX_ELEM cudak_(arr)[];
+ int j = blockIdx.x * blockDim.x + threadIdx.x;
+ cudak_(arr)[threadIdx.x] = j < n ? input[j + istride * blockIdx.y] : -FLT_MAX;
+ __syncthreads();
+ for (int offset = blockDim.x >> 1; offset; offset >>= 1)
+ {
+ if (threadIdx.x < offset)
+ {
+ MATRIX_ELEM l = cudak_(arr)[threadIdx.x],
+ r = cudak_(arr)[threadIdx.x + offset];
+ if (r > l)
+ cudak_(arr)[threadIdx.x] = r;
+ }
+ __syncthreads();
+ }
+ if (threadIdx.x == 0)
+ output[blockIdx.x + ostride * blockIdx.y] = cudak_(arr)[0];
+}
+
+__global__ void cudak_(block_reduce_rowmax_idx)(const MATRIX_ELEM *input,
+ const MATRIX_ELEM *idx_input,
+ MATRIX_ELEM *output,
+ MATRIX_ELEM *idx_output,
+ const int istride, const int ostride,
+ const int n) {
+ extern __shared__ MATRIX_ELEM cudak_(arr)[];
+ MATRIX_ELEM *arr_val = cudak_(arr);
+ MATRIX_ELEM *arr_idx = arr_val + blockDim.x;
+ int j = blockIdx.x * blockDim.x + threadIdx.x;
+ arr_val[threadIdx.x] = j < n ? input[j + istride * blockIdx.y] : -FLT_MAX;
+ arr_idx[threadIdx.x] = j < n ? idx_input[j + istride * blockIdx.y] : 0;
+ __syncthreads();
+ for (int offset = blockDim.x >> 1; offset; offset >>= 1)
+ {
+ if (threadIdx.x < offset)
+ {
+ MATRIX_ELEM l = arr_val[threadIdx.x],
+ r = arr_val[threadIdx.x + offset];
+ if (r > l)
+ {
+ arr_val[threadIdx.x] = r;
+ arr_idx[threadIdx.x] = arr_idx[threadIdx.x + offset];
+ }
+ }
+ __syncthreads();
+ }
+ if (threadIdx.x == 0)
+ {
+ output[blockIdx.x + ostride * blockIdx.y] = arr_val[0];
+ idx_output[blockIdx.x + ostride * blockIdx.y] = arr_idx[0];
+ }
+}
+
+__global__ void cudak_(add_row)(const MATRIX_ELEM *a, MATRIX_ELEM *b,
+ int nrow, int ncol, int stride, double beta) {
+ int j = blockIdx.x * blockDim.x + threadIdx.x;
+ int i = blockIdx.y * blockDim.y + threadIdx.y;
+ if (i >= nrow || j >= ncol) return;
+ b[j + i * stride] += beta * a[j];
+}
+
+__global__ void cudak_(fill)(MATRIX_ELEM *a,
+ int nrow, int ncol, int stride, double val) {
+ int j = blockIdx.x * blockDim.x + threadIdx.x;
+ int i = blockIdx.y * blockDim.y + threadIdx.y;
+ if (i >= nrow || j >= ncol) return;
+ a[j + i * stride] = val;
+}
+
+__global__ void cudak_(expand_frm)(const MATRIX_ELEM *a, MATRIX_ELEM *b,
+ int nrow, int ncol,
+ int enrow, int encol,
+ int stride, int estride,
+ int context) {
+ int j = blockIdx.x * blockDim.x + threadIdx.x;
+ int i = blockIdx.y * blockDim.y + threadIdx.y;
+ int ridx;
+ if (i >= enrow || j >= encol) return;
+ ridx = i + j / ncol - context;
+ if (ridx < 0) ridx = 0;
+ else if (ridx >= nrow) ridx = nrow - 1;
+ b[j + i * estride] = a[j % ncol + ridx * stride];
+}
+
+__global__ void cudak_(rearrange_frm)(const MATRIX_ELEM *a, MATRIX_ELEM *b,
+ int nrow, int ncol,
+ int stride, int step, int orig_dim) {
+ int j = blockIdx.x * blockDim.x + threadIdx.x;
+ int i = blockIdx.y * blockDim.y + threadIdx.y;
+ if (i >= nrow || j >= ncol) return;
+ b[j + i * stride] = a[j / step + (j % step) * orig_dim + i * stride];
+}
+
+__global__ void cudak_(scale_rows_by_col)(const MATRIX_ELEM *a, MATRIX_ELEM *b,
+ int nrow, int ncol,
+ int astride, int bstride) {
+ int j = blockIdx.x * blockDim.x + threadIdx.x;
+ int i = blockIdx.y * blockDim.y + threadIdx.y;
+ if (i >= nrow || j >= ncol) return;
+ b[j + i * bstride] *= a[i * astride];
+}
+
+__global__ void cudak_(scale_rows_by_row)(const MATRIX_ELEM *a, MATRIX_ELEM *b,
+ int nrow, int ncol,
+ int stride) {
+ int j = blockIdx.x * blockDim.x + threadIdx.x;
+ int i = blockIdx.y * blockDim.y + threadIdx.y;
+ if (i >= nrow || j >= ncol) return;
+ b[j + i * stride] *= a[j];
+}
+
+__global__ void cudak_(decompress)(const MATRIX_ELEM *a, MATRIX_ELEM *b,
+ int nrow, int ncol,
+ int stride_a, int stride_b) {
+ int j = blockIdx.x * blockDim.x + threadIdx.x;
+ int i = blockIdx.y * blockDim.y + threadIdx.y;
+ if (i >= nrow || j >= ncol) return;
+ b[lrintf(a[j + i * stride_a]) + i * stride_b] = 1.0;
+}
+
+__global__ void cudak_(gen_col_idx)(MATRIX_ELEM *b,
+ int nrow, int ncol, int stride) {
+ int j = blockIdx.x * blockDim.x + threadIdx.x;
+ int i = blockIdx.y * blockDim.y + threadIdx.y;
+ if (i >= nrow || j >= ncol) return;
+ b[j + i * stride] = j;
+}
+
+extern "C" {
+#include "../cukernel.h"
+ void cudak_(cuda_log_elem)(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));
+ cudak_(log_elem)<<<numBlocks, threadsPerBlock>>> \
+ (MATRIX_ELEM_PTR(a), MATRIX_ELEM_PTR(b),
+ b->nrow, b->ncol, b->stride / sizeof(MATRIX_ELEM));
+ cudaStreamSynchronize(0);
+ }
+
+ void cudak_(cuda_mul_elem)(const Matrix *a, const Matrix *b,
+ Matrix *c) {
+ dim3 threadsPerBlock(CUDA_THREADS_N, CUDA_THREADS_N);
+ dim3 numBlocks(CEIL_DIV(b->ncol, threadsPerBlock.x),
+ CEIL_DIV(b->nrow, threadsPerBlock.y));
+ cudak_(mul_elem)<<<numBlocks, threadsPerBlock>>> \
+ (MATRIX_ELEM_PTR(a), MATRIX_ELEM_PTR(b),
+ MATRIX_ELEM_PTR(c),
+ b->nrow, b->ncol, b->stride / sizeof(MATRIX_ELEM));
+ cudaStreamSynchronize(0);
+ }
+
+ void cudak_(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));
+ cudak_(sigmoid)<<<numBlocks, threadsPerBlock>>> \
+ (MATRIX_ELEM_PTR(a), MATRIX_ELEM_PTR(b), b->nrow, b->ncol,
+ b->stride / sizeof(MATRIX_ELEM));
+ cudaStreamSynchronize(0);
+ }
+
+ void cudak_(cuda_sigmoid_grad)(const Matrix *output,
+ const Matrix *err, Matrix *nerr) {
+ dim3 threadsPerBlock(CUDA_THREADS_N, CUDA_THREADS_N);
+ dim3 numBlocks(CEIL_DIV(nerr->ncol, threadsPerBlock.x),
+ CEIL_DIV(nerr->nrow, threadsPerBlock.y));
+ cudak_(sigmoid_grad)<<<numBlocks, threadsPerBlock>>> \
+ (MATRIX_ELEM_