Skip to content

Commit

Permalink
fftw plan creation is not thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliu committed Mar 13, 2014
1 parent ac54d1d commit 07c95b9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions bin/image-net.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,10 @@ int main(int argc, char** argv)
for (i = 0; i < 13; i++)
{
layer_params[i].w.decay = 0.0005;
layer_params[i].w.learn_rate = 0.01;
layer_params[i].w.learn_rate = 0.001;
layer_params[i].w.momentum = 0.9;
layer_params[i].bias.decay = 0;
layer_params[i].bias.learn_rate = 0.01;
layer_params[i].bias.learn_rate = 0.001;
layer_params[i].bias.momentum = 0.9;
}
layer_params[10].dor = 0.5;
Expand Down
11 changes: 11 additions & 0 deletions lib/ccv_numeric.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ccv_internal.h"
#include <complex.h>
#ifdef HAVE_FFTW3
#include <pthread.h>
#include <fftw3.h>
#else
#include "3rdparty/kissfft/kiss_fftndr.h"
Expand Down Expand Up @@ -570,6 +571,8 @@ static int _ccv_get_optimal_fft_size(int size)
return _ccv_optimal_fft_size[b];
}

static pthread_mutex_t fftw_plan_mutex = PTHREAD_MUTEX_INITIALIZER;

static void _ccv_filter_fftw(ccv_dense_matrix_t* a, ccv_dense_matrix_t* b, ccv_dense_matrix_t* d, int padding_pattern)
{
int ch = CCV_GET_CHANNEL(a->type);
Expand All @@ -587,6 +590,7 @@ static void _ccv_filter_fftw(ccv_dense_matrix_t* a, ccv_dense_matrix_t* b, ccv_d
fftw_a = fftwf_malloc(rows * cols_2c * ch * sizeof(float));
fftw_b = fftwf_malloc(rows * cols_2c * ch * sizeof(float));
fftw_d = fftwf_malloc(rows * cols_2c * ch * sizeof(float));
pthread_mutex_lock(&fftw_plan_mutex);
if (ch == 1)
{
pf = fftwf_plan_dft_r2c_2d(rows, cols, 0, 0, FFTW_ESTIMATE);
Expand All @@ -596,10 +600,12 @@ static void _ccv_filter_fftw(ccv_dense_matrix_t* a, ccv_dense_matrix_t* b, ccv_d
pf = fftwf_plan_many_dft_r2c(2, ndim, ch, 0, 0, ch, 1, 0, 0, ch, 1, FFTW_ESTIMATE);
pinvf = fftwf_plan_many_dft_c2r(2, ndim, ch, 0, 0, ch, 1, 0, 0, ch, 1, FFTW_ESTIMATE);
}
pthread_mutex_unlock(&fftw_plan_mutex);
} else {
fftw_a = fftw_malloc(rows * cols_2c * ch * sizeof(double));
fftw_b = fftw_malloc(rows * cols_2c * ch * sizeof(double));
fftw_d = fftw_malloc(rows * cols_2c * ch * sizeof(double));
pthread_mutex_lock(&fftw_plan_mutex);
if (ch == 1)
{
p = fftw_plan_dft_r2c_2d(rows, cols, 0, 0, FFTW_ESTIMATE);
Expand All @@ -609,6 +615,7 @@ static void _ccv_filter_fftw(ccv_dense_matrix_t* a, ccv_dense_matrix_t* b, ccv_d
p = fftw_plan_many_dft_r2c(2, ndim, ch, 0, 0, ch, 1, 0, 0, ch, 1, FFTW_ESTIMATE);
pinv = fftw_plan_many_dft_c2r(2, ndim, ch, 0, 0, ch, 1, 0, 0, ch, 1, FFTW_ESTIMATE);
}
pthread_mutex_unlock(&fftw_plan_mutex);
}
memset(fftw_b, 0, rows * cols_2c * ch * CCV_GET_DATA_TYPE_SIZE(fft_type));

Expand Down Expand Up @@ -728,8 +735,10 @@ static void _ccv_filter_fftw(ccv_dense_matrix_t* a, ccv_dense_matrix_t* b, ccv_d
ccv_matrix_setter(d->type, ccv_matrix_getter, a->type, for_block, float, fftwf_complex);
#undef fft_execute_dft_r2c
#undef fft_execute_dft_c2r
pthread_mutex_lock(&fftw_plan_mutex);
fftwf_destroy_plan(pf);
fftwf_destroy_plan(pinvf);
pthread_mutex_unlock(&fftw_plan_mutex);
fftwf_free(fftw_a);
fftwf_free(fftw_b);
fftwf_free(fftw_d);
Expand All @@ -739,8 +748,10 @@ static void _ccv_filter_fftw(ccv_dense_matrix_t* a, ccv_dense_matrix_t* b, ccv_d
ccv_matrix_setter(d->type, ccv_matrix_getter, a->type, for_block, double, fftw_complex);
#undef fft_execute_dft_r2c
#undef fft_execute_dft_c2r
pthread_mutex_lock(&fftw_plan_mutex);
fftw_destroy_plan(p);
fftw_destroy_plan(pinv);
pthread_mutex_unlock(&fftw_plan_mutex);
fftw_free(fftw_a);
fftw_free(fftw_b);
fftw_free(fftw_d);
Expand Down
2 changes: 1 addition & 1 deletion lib/configure
Original file line number Diff line number Diff line change
Expand Up @@ -3323,7 +3323,7 @@ fi
ac_fn_c_check_header_mongrel "$LINENO" "fftw3.h" "ac_cv_header_fftw3_h" "$ac_includes_default"
if test "x$ac_cv_header_fftw3_h" = xyes; then :
DEFINE_MACROS="$DEFINE_MACROS-D HAVE_FFTW3 "
MKLDFLAGS="$MKLDFLAGS-lfftw3 -lfftw3f "
MKLDFLAGS="$MKLDFLAGS-lfftw3 -lfftw3f -lpthread "
fi
Expand Down
2 changes: 1 addition & 1 deletion lib/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ AC_CHECK_HEADER(png.h,
AC_CHECK_HEADER(jpeglib.h,
[AC_SUBST(DEFINE_MACROS, ["$DEFINE_MACROS-D HAVE_LIBJPEG "]) AC_SUBST(MKLDFLAGS, ["$MKLDFLAGS-ljpeg "])])
AC_CHECK_HEADER(fftw3.h,
[AC_SUBST(DEFINE_MACROS, ["$DEFINE_MACROS-D HAVE_FFTW3 "]) AC_SUBST(MKLDFLAGS, ["$MKLDFLAGS-lfftw3 -lfftw3f "])])
[AC_SUBST(DEFINE_MACROS, ["$DEFINE_MACROS-D HAVE_FFTW3 "]) AC_SUBST(MKLDFLAGS, ["$MKLDFLAGS-lfftw3 -lfftw3f -lpthread "])])
AC_CHECK_HEADER(linear.h,
[AC_SUBST(DEFINE_MACROS, ["$DEFINE_MACROS-D HAVE_LIBLINEAR "]) AC_SUBST(MKLDFLAGS, ["$MKLDFLAGS-llinear "])])
AC_CHECK_HEADER(cblas.h,
Expand Down

0 comments on commit 07c95b9

Please sign in to comment.