ext/ruby_gumath/gumath/libgumath/thread.c in gumath-0.2.0dev5 vs ext/ruby_gumath/gumath/libgumath/thread.c in gumath-0.2.0dev8

- old
+ new

@@ -70,11 +70,11 @@ static void clear_all_slices(xnd_t *slices[], int *nslices, int stop) { for (int i = 0; i < stop; i++) { for (int k = 0; k < nslices[i]; k++) { - ndt_del((ndt_t *)slices[i][k].type); + ndt_decref(slices[i][k].type); } ndt_free(slices[i]); } } @@ -92,20 +92,31 @@ return NULL; } int gm_apply_thread(const gm_kernel_t *kernel, xnd_t stack[], int outer_dims, - uint32_t flags, const int64_t nthreads, ndt_context_t *ctx) + const int64_t nthreads, ndt_context_t *ctx) { const int nrows = (int)kernel->set->sig->Function.nargs; ALLOCA(xnd_t *, slices, nrows); ALLOCA(int, nslices, nrows); struct thread_info *tinfo; int ncols, tnum; + bool use_threads = true; - if (nthreads <= 1 || nrows == 0 || outer_dims == 0 || - !(flags & NDT_STRIDED)) { + if (nthreads <= 1 || nrows == 0 || outer_dims == 0) { + use_threads = false; + } + + for (int i = 0; i < nrows; i++) { + const ndt_t *t = stack[i].type; + if (!ndt_is_ndarray(t) || ndt_nelem(t) < GM_THREAD_CUTOFF) { + use_threads = false; + } + } + + if (!use_threads) { return gm_apply(kernel, stack, outer_dims, ctx); } for (int i = 0; i < nrows; i++) { int64_t ncols = nthreads; @@ -145,10 +156,11 @@ int ret = pthread_create(&tinfo[tnum].tid, NULL, &apply_thread, &tinfo[tnum]); if (ret != 0) { clear_all_slices(slices, nslices, nrows); + ndt_free(tinfo); ndt_err_format(ctx, NDT_RuntimeError, "could not create thread"); return -1; } } @@ -167,9 +179,10 @@ if (ret != 0 && !ndt_err_occurred(ctx)) { ndt_err_format(ctx, NDT_RuntimeError, "error in thread"); } clear_all_slices(slices, nslices, nrows); + ndt_free(tinfo); return ndt_err_occurred(ctx) ? -1 : 0; } #endif