starpu_audio_processing.c 12 KB

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