Skip to content

Instantly share code, notes, and snippets.

@x42
Created July 13, 2015 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x42/07391de61b99e872ad9d to your computer and use it in GitHub Desktop.
Save x42/07391de61b99e872ad9d to your computer and use it in GitHub Desktop.
// gcc -o fftw_concurrency_test fftw_concurrency_test.c -Wall -pthread `pkg-config --cflags --libs fftw3f` -lfftw3f_threads
/* 2 is sufficient to produce crashes, 8 makes it more likely */
#define MAXTHREAD 8
/* there is no c-header for this, yet */
extern void fftwf_make_planner_thread_safe ();
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <fftw3.h>
struct FFTAnalysis {
float *fft_in;
float *fft_out;
fftwf_plan fftplan;
};
static void test_fftw(int window_size) {
struct FFTAnalysis *ft = calloc (1, sizeof(struct FFTAnalysis));
ft->fft_in = (float *) fftwf_malloc(sizeof(float) * window_size);
ft->fft_out = (float *) fftwf_malloc(sizeof(float) * window_size);
ft->fftplan = fftwf_plan_r2r_1d(window_size, ft->fft_in, ft->fft_out, FFTW_R2HC, FFTW_MEASURE);
fftwf_destroy_plan(ft->fftplan);
fftwf_free(ft->fft_in);
fftwf_free(ft->fft_out);
free(ft);
}
static void * main_thread (void *arg) {
int v = *((int*)arg);
printf("window size: %d\n", v);
usleep(1000);
test_fftw (v);
pthread_exit (0);
return 0;
}
int main (int argc, char **argv) {
int i;
int bs[MAXTHREAD];
pthread_t thread[MAXTHREAD];
fftwf_make_planner_thread_safe ();
for (i = 0; i < MAXTHREAD; ++i) {
bs[i] = 1 << (6 + (i % 5));
pthread_create (&thread[i], NULL, &main_thread, &bs[i]);
}
for (i = 0; i < MAXTHREAD; ++i) {
pthread_join (thread[i], NULL);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment