starpu_audio_processing.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2010 Mehdi Juhoor
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. /*
  18. * This reads a wave file, splits it into chunks, and on each of them run a
  19. * task which performs an fft, drop some high and low frequencies, and performs
  20. * the inverse fft. It then writes the output to a wave file.
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <math.h>
  25. #include <string.h>
  26. #include <sys/types.h>
  27. #include <starpu.h>
  28. #include <fftw3.h>
  29. #ifdef STARPU_USE_CUDA
  30. #include <cufft.h>
  31. #include <starpu_cublas_v2.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[], 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. float scal = 1.0f/nsamples;
  172. cublasStatus_t status = cublasSscal (starpu_cublas_get_local_handle(), nsamples, &scal, localA, 1);
  173. if (status != CUBLAS_STATUS_SUCCESS)
  174. STARPU_CUBLAS_REPORT_ERROR(status);
  175. }
  176. #endif
  177. static starpu_pthread_mutex_t fftw_mutex = PTHREAD_MUTEX_INITIALIZER;
  178. static void band_filter_kernel_cpu(void *descr[], void *arg)
  179. {
  180. float *localA = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  181. int workerid = starpu_worker_get_id();
  182. /* initialize the plane only during the first iteration */
  183. if (!plans[workerid].is_initialized)
  184. {
  185. plans[workerid].localout_cpu = malloc(nsamples*sizeof(fftwf_complex));
  186. plans[workerid].Acopy = malloc(nsamples*sizeof(float));
  187. /* create plans, only "fftwf_execute" is thread safe in FFTW ... */
  188. STARPU_PTHREAD_MUTEX_LOCK(&fftw_mutex);
  189. plans[workerid].plan_cpu = fftwf_plan_dft_r2c_1d(nsamples,
  190. plans[workerid].Acopy,
  191. plans[workerid].localout_cpu,
  192. FFTW_ESTIMATE);
  193. plans[workerid].inv_plan_cpu = fftwf_plan_dft_c2r_1d(nsamples,
  194. plans[workerid].localout_cpu,
  195. plans[workerid].Acopy,
  196. FFTW_ESTIMATE);
  197. STARPU_PTHREAD_MUTEX_UNLOCK(&fftw_mutex);
  198. plans[workerid].is_initialized = 1;
  199. }
  200. fftwf_complex *localout = plans[workerid].localout_cpu;
  201. /* copy data into the temporary buffer */
  202. memcpy(plans[workerid].Acopy, localA, nsamples*sizeof(float));
  203. /* FFT */
  204. fftwf_execute(plans[workerid].plan_cpu);
  205. /* filter low freqs */
  206. unsigned lowfreq_index = (LOWFREQ*nsamples)/SAMPLERATE;
  207. memset(&localout[0], 0, lowfreq_index*sizeof(fftwf_complex));
  208. /* filter high freqs */
  209. unsigned hifreq_index = (HIFREQ*nsamples)/SAMPLERATE;
  210. memset(&localout[hifreq_index], nsamples/2, (nsamples/2 - hifreq_index)*sizeof(fftwf_complex));
  211. /* inverse FFT */
  212. fftwf_execute(plans[workerid].inv_plan_cpu);
  213. /* copy data into the temporary buffer */
  214. memcpy(localA, plans[workerid].Acopy, nsamples*sizeof(float));
  215. /* FFTW does not normalize its output ! */
  216. /* TODO use BLAS ?*/
  217. int i;
  218. for (i = 0; i < nsamples; i++)
  219. localA[i] /= nsamples;
  220. }
  221. struct starpu_perfmodel band_filter_model =
  222. {
  223. .type = STARPU_HISTORY_BASED,
  224. .symbol = "FFT_band_filter"
  225. };
  226. static struct starpu_codelet band_filter_cl =
  227. {
  228. .modes = { STARPU_RW },
  229. #ifdef STARPU_USE_CUDA
  230. .cuda_funcs = {band_filter_kernel_gpu},
  231. .cuda_flags = {STARPU_CUDA_ASYNC},
  232. #endif
  233. .cpu_funcs = {band_filter_kernel_cpu},
  234. .model = &band_filter_model,
  235. .nbuffers = 1
  236. };
  237. void callback(void *arg)
  238. {
  239. /* do some accounting */
  240. int id = starpu_worker_get_id();
  241. task_per_worker[id]++;
  242. }
  243. void create_starpu_task(unsigned iter)
  244. {
  245. int ret;
  246. struct starpu_task *task = starpu_task_create();
  247. task->cl = &band_filter_cl;
  248. task->handles[0] = starpu_data_get_sub_data(A_handle, 1, iter);
  249. task->callback_func = callback;
  250. task->callback_arg = NULL;
  251. ret = starpu_task_submit(task);
  252. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  253. }
  254. static void init_problem(void)
  255. {
  256. infile = fopen(inputfilename, "r");
  257. if (outputfilename)
  258. outfile = fopen(outputfilename, "w+");
  259. #if SAVE_RAW
  260. infile_raw = fopen("input.raw", "w");
  261. outfile_raw = fopen("output.raw", "w");
  262. #endif
  263. /* copy input's header into output WAV */
  264. if (outputfilename)
  265. copy_wav_header(infile, outfile);
  266. /* read length of input WAV's data */
  267. /* each element is 2 bytes long (16bits)*/
  268. length_data = get_wav_data_bytes_length(infile)/2;
  269. while (nsamples > length_data)
  270. nsamples /= 2;
  271. /* allocate a buffer to store the content of input file */
  272. if (use_pin)
  273. {
  274. starpu_malloc((void **)&A, length_data*sizeof(float));
  275. }
  276. else
  277. {
  278. A = malloc(length_data*sizeof(float));
  279. }
  280. /* allocate working buffer (this could be done online, but we'll keep it simple) */
  281. /* starpu_data_malloc_pinned_if_possible((void **)&outdata, length_data*sizeof(fftwf_complex)); */
  282. /* read input data into buffer "A" */
  283. read_16bit_wav(infile, length_data, A, infile_raw);
  284. }
  285. static void parse_args(int argc, char **argv)
  286. {
  287. int i;
  288. for (i = 1; i < argc; i++)
  289. {
  290. if (strcmp(argv[i], "-h") == 0)
  291. {
  292. fprintf(stderr, "Usage: %s [-pin] [-nsamples block_size] [-i input.wav] [-o output.wav | -no-output] [-h]\n", argv[0]);
  293. exit(-1);
  294. }
  295. if (strcmp(argv[i], "-i") == 0)
  296. {
  297. inputfilename = argv[++i];
  298. }
  299. if (strcmp(argv[i], "-o") == 0)
  300. {
  301. outputfilename = argv[++i];
  302. }
  303. if (strcmp(argv[i], "-no-output") == 0)
  304. {
  305. outputfilename = NULL;
  306. }
  307. /* block size */
  308. if (strcmp(argv[i], "-nsamples") == 0)
  309. {
  310. char *argptr;
  311. nsamples = strtol(argv[++i], &argptr, 10);
  312. }
  313. if (strcmp(argv[i], "-pin") == 0)
  314. {
  315. use_pin = 1;
  316. }
  317. }
  318. }
  319. int main(int argc, char **argv)
  320. {
  321. unsigned iter;
  322. int ret;
  323. parse_args(argc, argv);
  324. fprintf(stderr, "Reading input data\n");
  325. init_problem();
  326. unsigned niter = length_data/nsamples;
  327. fprintf(stderr, "input: %s\noutput: %s\n#chunks %u\n", inputfilename, outputfilename, niter);
  328. /* launch StarPU */
  329. ret = starpu_init(NULL);
  330. if (ret == -ENODEV)
  331. return 77;
  332. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  333. starpu_cublas_init();
  334. starpu_vector_data_register(&A_handle, STARPU_MAIN_RAM, (uintptr_t)A, niter*nsamples, sizeof(float));
  335. struct starpu_data_filter f =
  336. {
  337. .filter_func = starpu_vector_filter_block,
  338. .nchildren = niter
  339. };
  340. starpu_data_partition(A_handle, &f);
  341. for (iter = 0; iter < niter; iter++)
  342. starpu_data_set_wt_mask(starpu_data_get_sub_data(A_handle, 1, iter), 1<<0);
  343. start = starpu_timing_now();
  344. for (iter = 0; iter < niter; iter++)
  345. {
  346. create_starpu_task(iter);
  347. }
  348. starpu_task_wait_for_all();
  349. end = starpu_timing_now();
  350. double timing = end - start;
  351. fprintf(stderr, "Computation took %2.2f ms\n", timing/1000);
  352. int worker;
  353. for (worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  354. {
  355. if (task_per_worker[worker])
  356. {
  357. char name[32];
  358. starpu_worker_get_name(worker, name, 32);
  359. unsigned long bytes = nsamples*sizeof(float)*task_per_worker[worker];
  360. 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);
  361. }
  362. }
  363. if (outputfilename)
  364. fprintf(stderr, "Writing output data\n");
  365. /* make sure that the output is in RAM before quitting StarPU */
  366. starpu_data_unpartition(A_handle, STARPU_MAIN_RAM);
  367. starpu_data_unregister(A_handle);
  368. starpu_cublas_shutdown();
  369. /* we are done ! */
  370. starpu_shutdown();
  371. fclose(infile);
  372. if (outputfilename)
  373. {
  374. write_16bit_wav(outfile, length_data, A, outfile_raw);
  375. fclose(outfile);
  376. }
  377. #if SAVE_RAW
  378. fclose(infile_raw);
  379. fclose(outfile_raw);
  380. #endif
  381. return 0;
  382. }