starpu_audio_processing.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <math.h>
  21. #include <string.h>
  22. #include <pthread.h>
  23. #include <sys/types.h>
  24. #include <sys/time.h>
  25. #include <starpu.h>
  26. #include <fftw3.h>
  27. #ifdef STARPU_USE_CUDA
  28. #include <cufft.h>
  29. #endif
  30. /* #define SAVE_RAW 1 */
  31. #define DEFAULTINPUTFILE "input.wav"
  32. #define DEFAULTOUTPUTFILE "output.wav"
  33. #define NSAMPLES (256*1024)
  34. #define SAMPLERATE 44100
  35. static unsigned nsamples = NSAMPLES;
  36. /* This is a band filter, we want to stop everything that is not between LOWFREQ and HIGHFREQ*/
  37. /* LOWFREQ < i * SAMPLERATE / NSAMPLE */
  38. #define LOWFREQ 500U
  39. #define HIFREQ 800U
  40. static const size_t headersize = 37+9;
  41. static FILE *infile, *outfile;
  42. static FILE *infile_raw, *outfile_raw;
  43. static char *inputfilename = DEFAULTINPUTFILE;
  44. static char *outputfilename = DEFAULTOUTPUTFILE;
  45. static unsigned use_pin = 0;
  46. unsigned length_data;
  47. /* buffer containing input WAV data */
  48. float *A;
  49. starpu_data_handle_t A_handle;
  50. /* For performance evaluation */
  51. static struct timeval start;
  52. static struct timeval end;
  53. static unsigned task_per_worker[STARPU_NMAXWORKERS] = {0};
  54. /*
  55. * Functions to Manipulate WAV files
  56. */
  57. unsigned get_wav_data_bytes_length(FILE *file)
  58. {
  59. /* this is clearly suboptimal !! */
  60. fseek(file, headersize, SEEK_SET);
  61. unsigned cnt = 0;
  62. while (fgetc(file) != EOF)
  63. cnt++;
  64. return cnt;
  65. }
  66. void copy_wav_header(FILE *srcfile, FILE *dstfile)
  67. {
  68. unsigned char buffer[128];
  69. fseek(srcfile, 0, SEEK_SET);
  70. fseek(dstfile, 0, SEEK_SET);
  71. fread(buffer, 1, headersize, infile);
  72. fwrite(buffer, 1, headersize, outfile);
  73. }
  74. void read_16bit_wav(FILE *infile, unsigned size, float *arrayout, FILE *save_file)
  75. {
  76. int v;
  77. #if SAVE_RAW
  78. unsigned currentpos = 0;
  79. #endif
  80. /* we skip the header to only keep the data */
  81. fseek(infile, headersize, SEEK_SET);
  82. for (v=0;v<size;v++)
  83. {
  84. signed char val = (signed char)fgetc(infile);
  85. signed char val2 = (signed char)fgetc(infile);
  86. arrayout[v] = 256*val2 + val;
  87. #if SAVE_RAW
  88. fprintf(save_file, "%d %f\n", currentpos++, arrayout[v]);
  89. #endif
  90. }
  91. }
  92. /* we only write the data, not the header !*/
  93. void write_16bit_wav(FILE *outfile, unsigned size, float *arrayin, FILE *save_file)
  94. {
  95. int v;
  96. #if SAVE_RAW
  97. unsigned currentpos = 0;
  98. #endif
  99. /* we assume that the header is copied using copy_wav_header */
  100. fseek(outfile, headersize, SEEK_SET);
  101. for (v=0;v<size;v++)
  102. {
  103. signed char val = ((int)arrayin[v]) % 256;
  104. signed char val2 = ((int)arrayin[v]) / 256;
  105. fputc(val, outfile);
  106. fputc(val2, outfile);
  107. #if SAVE_RAW
  108. if (save_file)
  109. fprintf(save_file, "%d %f\n", currentpos++, arrayin[v]);
  110. #endif
  111. }
  112. }
  113. /*
  114. *
  115. * The actual kernels
  116. *
  117. */
  118. /* we don't reinitialize the CUFFT plan for every kernel, so we "cache" it */
  119. typedef struct
  120. {
  121. unsigned is_initialized;
  122. #ifdef STARPU_USE_CUDA
  123. cufftHandle plan;
  124. cufftHandle inv_plan;
  125. cufftComplex *localout;
  126. #endif
  127. fftwf_complex *localout_cpu;
  128. float *Acopy;
  129. fftwf_plan plan_cpu;
  130. fftwf_plan inv_plan_cpu;
  131. } fft_plan_cache;
  132. static fft_plan_cache plans[STARPU_NMAXWORKERS];
  133. #ifdef STARPU_USE_CUDA
  134. static void band_filter_kernel_gpu(void *descr[], __attribute__((unused)) void *arg)
  135. {
  136. cufftResult cures;
  137. float *localA = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  138. cufftComplex *localout;
  139. int workerid = starpu_worker_get_id();
  140. /* initialize the plane only during the first iteration */
  141. if (!plans[workerid].is_initialized)
  142. {
  143. cures = cufftPlan1d(&plans[workerid].plan, nsamples, CUFFT_R2C, 1);
  144. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  145. cufftSetStream(plans[workerid].plan, starpu_cuda_get_local_stream());
  146. cures = cufftPlan1d(&plans[workerid].inv_plan, nsamples, CUFFT_C2R, 1);
  147. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  148. cufftSetStream(plans[workerid].inv_plan, starpu_cuda_get_local_stream());
  149. cudaMalloc((void **)&plans[workerid].localout,
  150. nsamples*sizeof(cufftComplex));
  151. STARPU_ASSERT(plans[workerid].localout);
  152. plans[workerid].is_initialized = 1;
  153. }
  154. localout = plans[workerid].localout;
  155. /* FFT */
  156. cures = cufftExecR2C(plans[workerid].plan, localA, localout);
  157. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  158. /* filter low freqs */
  159. unsigned lowfreq_index = (LOWFREQ*nsamples)/SAMPLERATE;
  160. cudaMemsetAsync(&localout[0], 0, lowfreq_index*sizeof(fftwf_complex), starpu_cuda_get_local_stream());
  161. /* filter high freqs */
  162. unsigned hifreq_index = (HIFREQ*nsamples)/SAMPLERATE;
  163. cudaMemsetAsync(&localout[hifreq_index], nsamples/2, (nsamples/2 - hifreq_index)*sizeof(fftwf_complex), starpu_cuda_get_local_stream());
  164. /* inverse FFT */
  165. cures = cufftExecC2R(plans[workerid].inv_plan, localout, localA);
  166. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  167. /* FFTW does not normalize its output ! */
  168. cublasSscal (nsamples, 1.0f/nsamples, localA, 1);
  169. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  170. }
  171. #endif
  172. static pthread_mutex_t fftw_mutex = PTHREAD_MUTEX_INITIALIZER;
  173. static void band_filter_kernel_cpu(void *descr[], __attribute__((unused)) void *arg)
  174. {
  175. float *localA = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  176. int workerid = starpu_worker_get_id();
  177. /* initialize the plane only during the first iteration */
  178. if (!plans[workerid].is_initialized)
  179. {
  180. plans[workerid].localout_cpu = malloc(nsamples*sizeof(fftwf_complex));
  181. plans[workerid].Acopy = malloc(nsamples*sizeof(float));
  182. /* create plans, only "fftwf_execute" is thread safe in FFTW ... */
  183. pthread_mutex_lock(&fftw_mutex);
  184. plans[workerid].plan_cpu = fftwf_plan_dft_r2c_1d(nsamples,
  185. plans[workerid].Acopy,
  186. plans[workerid].localout_cpu,
  187. FFTW_ESTIMATE);
  188. plans[workerid].inv_plan_cpu = fftwf_plan_dft_c2r_1d(nsamples,
  189. plans[workerid].localout_cpu,
  190. plans[workerid].Acopy,
  191. FFTW_ESTIMATE);
  192. pthread_mutex_unlock(&fftw_mutex);
  193. plans[workerid].is_initialized = 1;
  194. }
  195. fftwf_complex *localout = plans[workerid].localout_cpu;
  196. /* copy data into the temporary buffer */
  197. memcpy(plans[workerid].Acopy, localA, nsamples*sizeof(float));
  198. /* FFT */
  199. fftwf_execute(plans[workerid].plan_cpu);
  200. /* filter low freqs */
  201. unsigned lowfreq_index = (LOWFREQ*nsamples)/SAMPLERATE;
  202. memset(&localout[0], 0, lowfreq_index*sizeof(fftwf_complex));
  203. /* filter high freqs */
  204. unsigned hifreq_index = (HIFREQ*nsamples)/SAMPLERATE;
  205. memset(&localout[hifreq_index], nsamples/2, (nsamples/2 - hifreq_index)*sizeof(fftwf_complex));
  206. /* inverse FFT */
  207. fftwf_execute(plans[workerid].inv_plan_cpu);
  208. /* copy data into the temporary buffer */
  209. memcpy(localA, plans[workerid].Acopy, nsamples*sizeof(float));
  210. /* FFTW does not normalize its output ! */
  211. /* TODO use BLAS ?*/
  212. int i;
  213. for (i = 0; i < nsamples; i++)
  214. localA[i] /= nsamples;
  215. }
  216. struct starpu_perfmodel band_filter_model =
  217. {
  218. .type = STARPU_HISTORY_BASED,
  219. .symbol = "FFT_band_filter"
  220. };
  221. static struct starpu_codelet band_filter_cl =
  222. {
  223. .modes = { STARPU_RW },
  224. .where = STARPU_CPU|STARPU_CUDA,
  225. #ifdef STARPU_USE_CUDA
  226. .cuda_funcs = {band_filter_kernel_gpu, NULL},
  227. #endif
  228. .cpu_funcs = {band_filter_kernel_cpu, NULL},
  229. .model = &band_filter_model,
  230. .nbuffers = 1
  231. };
  232. void callback(void *arg)
  233. {
  234. /* do some accounting */
  235. int id = starpu_worker_get_id();
  236. task_per_worker[id]++;
  237. }
  238. void create_starpu_task(unsigned iter)
  239. {
  240. int ret;
  241. struct starpu_task *task = starpu_task_create();
  242. task->cl = &band_filter_cl;
  243. task->handles[0] = starpu_data_get_sub_data(A_handle, 1, iter);
  244. task->callback_func = callback;
  245. task->callback_arg = NULL;
  246. ret = starpu_task_submit(task);
  247. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  248. }
  249. static void init_problem(void)
  250. {
  251. infile = fopen(inputfilename, "r");
  252. if (outputfilename)
  253. outfile = fopen(outputfilename, "w+");
  254. #if SAVE_RAW
  255. infile_raw = fopen("input.raw", "w");
  256. outfile_raw = fopen("output.raw", "w");
  257. #endif
  258. /* copy input's header into output WAV */
  259. if (outputfilename)
  260. copy_wav_header(infile, outfile);
  261. /* read length of input WAV's data */
  262. /* each element is 2 bytes long (16bits)*/
  263. length_data = get_wav_data_bytes_length(infile)/2;
  264. /* allocate a buffer to store the content of input file */
  265. if (use_pin)
  266. {
  267. starpu_malloc((void **)&A, length_data*sizeof(float));
  268. }
  269. else
  270. {
  271. A = malloc(length_data*sizeof(float));
  272. }
  273. /* allocate working buffer (this could be done online, but we'll keep it simple) */
  274. /* starpu_data_malloc_pinned_if_possible((void **)&outdata, length_data*sizeof(fftwf_complex)); */
  275. /* read input data into buffer "A" */
  276. read_16bit_wav(infile, length_data, A, infile_raw);
  277. }
  278. static void parse_args(int argc, char **argv)
  279. {
  280. int i;
  281. for (i = 1; i < argc; i++)
  282. {
  283. if (strcmp(argv[i], "-h") == 0)
  284. {
  285. fprintf(stderr, "Usage: %s [-pin] [-nsamples block_size] [-i input.wav] [-o output.wav | -no-output] [-h]\n", argv[0]);
  286. exit(-1);
  287. }
  288. if (strcmp(argv[i], "-i") == 0)
  289. {
  290. inputfilename = argv[++i];;
  291. }
  292. if (strcmp(argv[i], "-o") == 0)
  293. {
  294. outputfilename = argv[++i];;
  295. }
  296. if (strcmp(argv[i], "-no-output") == 0)
  297. {
  298. outputfilename = NULL;;
  299. }
  300. /* block size */
  301. if (strcmp(argv[i], "-nsamples") == 0)
  302. {
  303. char *argptr;
  304. nsamples = strtol(argv[++i], &argptr, 10);
  305. }
  306. if (strcmp(argv[i], "-pin") == 0)
  307. {
  308. use_pin = 1;
  309. }
  310. }
  311. }
  312. int main(int argc, char **argv)
  313. {
  314. unsigned iter;
  315. int ret;
  316. parse_args(argc, argv);
  317. fprintf(stderr, "Reading input data\n");
  318. init_problem();
  319. unsigned niter = length_data/nsamples;
  320. fprintf(stderr, "input: %s\noutput: %s\n#chunks %d\n", inputfilename, outputfilename, niter);
  321. /* launch StarPU */
  322. ret = starpu_init(NULL);
  323. if (ret == -ENODEV)
  324. return 77;
  325. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  326. starpu_helper_cublas_init();
  327. starpu_vector_data_register(&A_handle, 0, (uintptr_t)A, niter*nsamples, sizeof(float));
  328. struct starpu_data_filter f =
  329. {
  330. .filter_func = starpu_block_filter_func_vector,
  331. .nchildren = niter
  332. };
  333. starpu_data_partition(A_handle, &f);
  334. for (iter = 0; iter < niter; iter++)
  335. starpu_data_set_wt_mask(starpu_data_get_sub_data(A_handle, 1, iter), 1<<0);
  336. gettimeofday(&start, NULL);
  337. for (iter = 0; iter < niter; iter++)
  338. {
  339. create_starpu_task(iter);
  340. }
  341. starpu_task_wait_for_all();
  342. gettimeofday(&end, NULL);
  343. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  344. fprintf(stderr, "Computation took %2.2f ms\n", timing/1000);
  345. int worker;
  346. for (worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  347. {
  348. if (task_per_worker[worker])
  349. {
  350. char name[32];
  351. starpu_worker_get_name(worker, name, 32);
  352. unsigned long bytes = nsamples*sizeof(float)*task_per_worker[worker];
  353. fprintf(stderr, "\t%s -> %2.2f MB\t%2.2f\tMB/s\t%2.2f %%\n", name, (1.0*bytes)/(1024*1024), bytes/timing, (100.0*task_per_worker[worker])/niter);
  354. }
  355. }
  356. if (outputfilename)
  357. fprintf(stderr, "Writing output data\n");
  358. /* make sure that the output is in RAM before quitting StarPU */
  359. starpu_data_unpartition(A_handle, 0);
  360. starpu_data_unregister(A_handle);
  361. starpu_helper_cublas_shutdown();
  362. /* we are done ! */
  363. starpu_shutdown();
  364. fclose(infile);
  365. if (outputfilename)
  366. {
  367. write_16bit_wav(outfile, length_data, A, outfile_raw);
  368. fclose(outfile);
  369. }
  370. #if SAVE_RAW
  371. fclose(infile_raw);
  372. fclose(outfile_raw);
  373. #endif
  374. return 0;
  375. }