diff options
author | Determinant <ted.sybil@gmail.com> | 2016-03-15 15:46:05 +0800 |
---|---|---|
committer | Determinant <ted.sybil@gmail.com> | 2016-03-15 15:46:05 +0800 |
commit | 07fc1e2794027d44c255e1062c4491346b101a08 (patch) | |
tree | 8e7217b9c5e9570b94af5aaad3f94d1a37cfe40b /nerv/lib | |
parent | a5a4d2735b595fc9fadc9c7e91198786d3c0e078 (diff) | |
parent | e15307f071813e2eb56f7f83229b91141961325a (diff) |
Merge branch 'master' of github.com:liuq901/nerv into liuq901-master
Diffstat (limited to 'nerv/lib')
-rw-r--r-- | nerv/lib/matrix/cumatrix.c | 6 | ||||
-rw-r--r-- | nerv/lib/matrix/generic/cukernel.cu | 22 | ||||
-rw-r--r-- | nerv/lib/matrix/generic/cumatrix.c | 14 | ||||
-rw-r--r-- | nerv/lib/matrix/generic/cumatrix.h | 2 | ||||
-rw-r--r-- | nerv/lib/matrix/generic/mmatrix.c | 23 | ||||
-rw-r--r-- | nerv/lib/matrix/generic/mmatrix.h | 2 | ||||
-rw-r--r-- | nerv/lib/matrix/mmatrix.c | 6 |
7 files changed, 72 insertions, 3 deletions
diff --git a/nerv/lib/matrix/cumatrix.c b/nerv/lib/matrix/cumatrix.c index aec4d60..43448bf 100644 --- a/nerv/lib/matrix/cumatrix.c +++ b/nerv/lib/matrix/cumatrix.c @@ -9,15 +9,19 @@ void nerv_cuda_context_print_profile(CuContext *context) { HashMap *profile = context->profile; size_t i; + float tmp, tot = 0; 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); + tmp = *(float *)ptr->val; + fprintf(stderr, "%s:\t%.6f\n", ptr->key, tmp); + tot += tmp; } } + fprintf(stderr, "Total time:\t%.6f\n", tot); } void nerv_cuda_context_clear_profile(CuContext *context) { diff --git a/nerv/lib/matrix/generic/cukernel.cu b/nerv/lib/matrix/generic/cukernel.cu index fc630ad..cf9d213 100644 --- a/nerv/lib/matrix/generic/cukernel.cu +++ b/nerv/lib/matrix/generic/cukernel.cu @@ -328,6 +328,15 @@ __global__ void cudak_(rearrange_frm)(const MATRIX_ELEM *a, MATRIX_ELEM *b, b[j + i * stride] = a[j / step + (j % step) * orig_dim + i * stride]; } +__global__ void cudak_(set_values_by_mask)(const MATRIX_ELEM *a, MATRIX_ELEM *b, + int nrow, int ncol, + int astride, int bstride, double val) { + int j = blockIdx.x * blockDim.x + threadIdx.x; + int i = blockIdx.y * blockDim.y + threadIdx.y; + if (i >= nrow || j >= ncol || a[i * astride] != 0.0) return; + b[j + i * bstride] = val; +} + __global__ void cudak_(scale_rows_by_col)(const MATRIX_ELEM *a, MATRIX_ELEM *b, int nrow, int ncol, int astride, int bstride) { @@ -772,6 +781,19 @@ extern "C" { cudaStreamSynchronize(0); } + void cudak_(cuda_set_values_by_mask)(const Matrix *a, Matrix *b, double val) { + dim3 threadsPerBlock(CUDA_THREADS_N, CUDA_THREADS_N); + dim3 numBlocks(CEIL_DIV(b->ncol, threadsPerBlock.x), + CEIL_DIV(b->nrow, threadsPerBlock.y)); + cudak_(set_values_by_mask)<<<numBlocks, threadsPerBlock>>> \ + (MATRIX_ELEM_PTR(a), MATRIX_ELEM_PTR(b), + b->nrow, b->ncol, + a->stride / sizeof(MATRIX_ELEM), + b->stride / sizeof(MATRIX_ELEM), + val); + cudaStreamSynchronize(0); + } + void cudak_(cuda_scale_rows_by_row)(const Matrix *a, Matrix *b) { dim3 threadsPerBlock(CUDA_THREADS_N, CUDA_THREADS_N); dim3 numBlocks(CEIL_DIV(b->ncol, threadsPerBlock.x), diff --git a/nerv/lib/matrix/generic/cumatrix.c b/nerv/lib/matrix/generic/cumatrix.c index 6d84663..bc5f285 100644 --- a/nerv/lib/matrix/generic/cumatrix.c +++ b/nerv/lib/matrix/generic/cumatrix.c @@ -515,7 +515,7 @@ void nerv_matrix_(prefixsum_row)(Matrix *a, const Matrix *b, NERV_SET_STATUS(status, NERV_NORMAL, 0); } -void nerv_matrix_(diagonalize)(Matrix *a, CuContext * context, Status *status) { +void nerv_matrix_(diagonalize)(Matrix *a, CuContext *context, Status *status) { if (a->nrow != a->ncol) NERV_EXIT_STATUS(status, MAT_MISMATCH_DIM, 0); PROFILE_START @@ -524,6 +524,18 @@ void nerv_matrix_(diagonalize)(Matrix *a, CuContext * context, Status *status) { NERV_SET_STATUS(status, NERV_NORMAL, 0); } +void nerv_matrix_(set_values_by_mask)(Matrix *a, const Matrix *b, double val, + CuContext *context, Status *status) { + if (a->nrow != b->nrow) + NERV_EXIT_STATUS(status, MAT_MISMATCH_DIM, 0); + if (b->ncol != 1) + NERV_EXIT_STATUS(status, MAT_COL_VECTOR_EXP, 0); + PROFILE_START + cudak_(cuda_set_values_by_mask)(b, a, val); + PROFILE_STOP + NERV_SET_STATUS(status, NERV_NORMAL, 0); +} + static void cuda_matrix_(free)(MATRIX_ELEM *ptr, CuContext *context, Status *status) { CUDA_SAFE_SYNC_CALL(cudaFree(ptr), status); NERV_SET_STATUS(status, NERV_NORMAL, 0); diff --git a/nerv/lib/matrix/generic/cumatrix.h b/nerv/lib/matrix/generic/cumatrix.h index de3a09e..79bfc76 100644 --- a/nerv/lib/matrix/generic/cumatrix.h +++ b/nerv/lib/matrix/generic/cumatrix.h @@ -35,6 +35,8 @@ void nerv_matrix_(fill)(Matrix *self, double val, CuContext *context, Status *status); void nerv_matrix_(diagonalize)(Matrix *self, CuContext *context, Status *status); +void nerv_matrix_(set_values_by_mask)(Matrix *self, Matrix *mask, double val, + CuContext *context, Status *status); void nerv_matrix_(copy_fromd)(Matrix *a, const Matrix *b, int a_begin, int b_begin, int b_end, CuContext *context, Status *status); diff --git a/nerv/lib/matrix/generic/mmatrix.c b/nerv/lib/matrix/generic/mmatrix.c index badddbd..e356de7 100644 --- a/nerv/lib/matrix/generic/mmatrix.c +++ b/nerv/lib/matrix/generic/mmatrix.c @@ -507,6 +507,29 @@ void nerv_matrix_(scale_rows_by_col)(Matrix *a, const Matrix *b, NERV_SET_STATUS(status, NERV_NORMAL, 0); } +void nerv_matrix_(set_values_by_mask)(Matrix *a, const Matrix *b, double val, + MContext *context, Status *status) { + if (a->nrow != b->nrow) + NERV_EXIT_STATUS(status, MAT_MISMATCH_DIM, 0); + if (b->ncol != 1) + NERV_EXIT_STATUS(status, MAT_COL_VECTOR_EXP, 0); + int i, j; + size_t astride = a->stride, bstride = b->stride; + MATRIX_ELEM *arow = MATRIX_ELEM_PTR(a), + *brow = MATRIX_ELEM_PTR(b); + for (i = 0; i < a->nrow; i++) + { + if (brow[0] == 0.0) + { + for (j = 0; j < a->ncol; j++) + arow[j] = val; + } + arow = MATRIX_NEXT_ROW_PTR(arow, astride); + brow = MATRIX_NEXT_ROW_PTR(brow, bstride); + } + NERV_SET_STATUS(status, NERV_NORMAL, 0); +} + static void host_matrix_(free)(MATRIX_ELEM *ptr, MContext *context, Status *status) { free(ptr); NERV_SET_STATUS(status, NERV_NORMAL, 0); diff --git a/nerv/lib/matrix/generic/mmatrix.h b/nerv/lib/matrix/generic/mmatrix.h index 6d17c99..41c39f6 100644 --- a/nerv/lib/matrix/generic/mmatrix.h +++ b/nerv/lib/matrix/generic/mmatrix.h @@ -48,6 +48,8 @@ void nerv_matrix_(expand_frm)(Matrix *a, const Matrix *b, int cont, MContext *context, Status *status); void nerv_matrix_(rearrange_frm)(Matrix *a, const Matrix *b, int step, MContext *context, Status *status); +void nerv_matrix_(set_values_by_mask)(Matrix *a, const Matrix *b, double val, + MContext *context, Status *status); void nerv_matrix_(scale_rows_by_col)(Matrix *a, const Matrix *b, MContext *context, Status *status); void nerv_matrix_(scale_rows_by_row)(Matrix *a, const Matrix *b, diff --git a/nerv/lib/matrix/mmatrix.c b/nerv/lib/matrix/mmatrix.c index e40b160..006735d 100644 --- a/nerv/lib/matrix/mmatrix.c +++ b/nerv/lib/matrix/mmatrix.c @@ -8,15 +8,19 @@ void nerv_host_context_print_profile(MContext *context) { HashMap *profile = context->profile; size_t i; + float tmp, tot = 0; fprintf(stderr, "*** [nerv mmatrix 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); + tmp = *(float *)ptr->val; + fprintf(stderr, "%s:\t%.6f\n", ptr->key, tmp); + tot += tmp; } } + fprintf(stderr, "Total time:\t%.6f\n", tot); } void nerv_host_context_clear_profile(MContext *context) { |