starpu_audio_processing.c 12 KB

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