starpufftx.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  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. #define PARALLEL 0
  18. #include <math.h>
  19. #include <pthread.h>
  20. #include <unistd.h>
  21. #include <sys/time.h>
  22. #include <starpu.h>
  23. #include <config.h>
  24. #include "starpufft.h"
  25. #ifdef STARPU_USE_CUDA
  26. #define _externC extern
  27. #include "cudax_kernels.h"
  28. #endif
  29. #define _FFTW_FLAGS FFTW_ESTIMATE
  30. /* Steps for the parallel variant */
  31. enum steps
  32. {
  33. SPECIAL, TWIST1, FFT1, JOIN, TWIST2, FFT2, TWIST3, END
  34. };
  35. #define NUMBER_BITS 5
  36. #define NUMBER_SHIFT (64 - NUMBER_BITS)
  37. #define STEP_BITS 3
  38. #define STEP_SHIFT (NUMBER_SHIFT - STEP_BITS)
  39. /* Tags for the steps of the parallel variant */
  40. #define _STEP_TAG(plan, step, i) (((starpu_tag_t) plan->number << NUMBER_SHIFT) | ((starpu_tag_t)(step) << STEP_SHIFT) | (starpu_tag_t) (i))
  41. #define I_BITS STEP_SHIFT
  42. enum type
  43. {
  44. R2C,
  45. C2R,
  46. C2C
  47. };
  48. static unsigned task_per_worker[STARPU_NMAXWORKERS];
  49. static unsigned samples_per_worker[STARPU_NMAXWORKERS];
  50. static struct timeval start, submit_tasks, end;
  51. /*
  52. *
  53. * The actual kernels
  54. *
  55. */
  56. struct STARPUFFT(plan)
  57. {
  58. int number; /* uniquely identifies the plan, for starpu tags */
  59. int *n;
  60. int *n1;
  61. int *n2;
  62. int totsize;
  63. int totsize1; /* Number of first-round tasks */
  64. int totsize2; /* Size of first-round tasks */
  65. int totsize3; /* Number of second-round tasks */
  66. int totsize4; /* Size of second-round tasks */
  67. int dim;
  68. enum type type;
  69. int sign;
  70. STARPUFFT(complex) *roots[2];
  71. starpu_data_handle_t roots_handle[2];
  72. /* For each worker, we need some data */
  73. struct
  74. {
  75. #ifdef STARPU_USE_CUDA
  76. /* CUFFT plans */
  77. cufftHandle plan1_cuda, plan2_cuda;
  78. /* Sequential version */
  79. cufftHandle plan_cuda;
  80. #endif
  81. #ifdef STARPU_HAVE_FFTW
  82. /* FFTW plans */
  83. _fftw_plan plan1_cpu, plan2_cpu;
  84. /* Sequential version */
  85. _fftw_plan plan_cpu;
  86. #endif
  87. } plans[STARPU_NMAXWORKERS];
  88. /* Buffers for codelets */
  89. STARPUFFT(complex) *in, *twisted1, *fft1, *twisted2, *fft2, *out;
  90. /* corresponding starpu DSM handles */
  91. starpu_data_handle_t in_handle, *twisted1_handle, *fft1_handle, *twisted2_handle, *fft2_handle, out_handle;
  92. /* Tasks */
  93. struct starpu_task **twist1_tasks, **fft1_tasks, **twist2_tasks, **fft2_tasks, **twist3_tasks;
  94. struct starpu_task *join_task, *end_task;
  95. /* Arguments for tasks */
  96. struct STARPUFFT(args) *fft1_args, *fft2_args;
  97. };
  98. struct STARPUFFT(args)
  99. {
  100. struct STARPUFFT(plan) *plan;
  101. int i, j, jj, kk, ll, *iv, *kkv;
  102. };
  103. static void
  104. check_dims(STARPUFFT(plan) plan)
  105. {
  106. int dim;
  107. for (dim = 0; dim < plan->dim; dim++)
  108. if (plan->n[dim] & (plan->n[dim]-1))
  109. {
  110. fprintf(stderr,"can't cope with non-power-of-2\n");
  111. STARPU_ABORT();
  112. }
  113. }
  114. static void
  115. compute_roots(STARPUFFT(plan) plan)
  116. {
  117. int dim, k;
  118. /* Compute the n-roots and m-roots of unity for twiddling */
  119. for (dim = 0; dim < plan->dim; dim++)
  120. {
  121. STARPUFFT(complex) exp = (plan->sign * 2. * 4.*atan(1.)) * _Complex_I / (STARPUFFT(complex)) plan->n[dim];
  122. plan->roots[dim] = malloc(plan->n[dim] * sizeof(**plan->roots));
  123. for (k = 0; k < plan->n[dim]; k++)
  124. plan->roots[dim][k] = cexp(exp*k);
  125. starpu_vector_data_register(&plan->roots_handle[dim], 0, (uintptr_t) plan->roots[dim], plan->n[dim], sizeof(**plan->roots));
  126. #ifdef STARPU_USE_CUDA
  127. if (plan->n[dim] > 100000)
  128. {
  129. /* prefetch the big root array on GPUs */
  130. unsigned worker;
  131. unsigned nworkers = starpu_worker_get_count();
  132. for (worker = 0; worker < nworkers; worker++)
  133. {
  134. unsigned node = starpu_worker_get_memory_node(worker);
  135. if (starpu_worker_get_type(worker) == STARPU_CUDA_WORKER)
  136. starpu_data_prefetch_on_node(plan->roots_handle[dim], node, 0);
  137. }
  138. }
  139. #endif
  140. }
  141. }
  142. #include "starpufftx1d.c"
  143. #include "starpufftx2d.c"
  144. struct starpu_task *
  145. STARPUFFT(start)(STARPUFFT(plan) plan, void *_in, void *_out)
  146. {
  147. struct starpu_task *task;
  148. int z;
  149. plan->in = _in;
  150. plan->out = _out;
  151. switch (plan->dim)
  152. {
  153. case 1:
  154. {
  155. switch (plan->type)
  156. {
  157. case C2C:
  158. starpu_vector_data_register(&plan->in_handle, 0, (uintptr_t) plan->in, plan->totsize, sizeof(STARPUFFT(complex)));
  159. if (!PARALLEL)
  160. starpu_vector_data_register(&plan->out_handle, 0, (uintptr_t) plan->out, plan->totsize, sizeof(STARPUFFT(complex)));
  161. if (PARALLEL)
  162. {
  163. for (z = 0; z < plan->totsize1; z++)
  164. plan->twist1_tasks[z]->handles[0] = plan->in_handle;
  165. }
  166. task = STARPUFFT(start1dC2C)(plan, plan->in_handle, plan->out_handle);
  167. break;
  168. default:
  169. STARPU_ABORT();
  170. break;
  171. }
  172. break;
  173. }
  174. case 2:
  175. starpu_vector_data_register(&plan->in_handle, 0, (uintptr_t) plan->in, plan->totsize, sizeof(STARPUFFT(complex)));
  176. if (!PARALLEL)
  177. starpu_vector_data_register(&plan->out_handle, 0, (uintptr_t) plan->out, plan->totsize, sizeof(STARPUFFT(complex)));
  178. if (PARALLEL)
  179. {
  180. for (z = 0; z < plan->totsize1; z++)
  181. plan->twist1_tasks[z]->handles[0] = plan->in_handle;
  182. }
  183. task = STARPUFFT(start2dC2C)(plan, plan->in_handle, plan->out_handle);
  184. break;
  185. default:
  186. STARPU_ABORT();
  187. break;
  188. }
  189. return task;
  190. }
  191. void
  192. STARPUFFT(cleanup)(STARPUFFT(plan) plan)
  193. {
  194. if (plan->in_handle)
  195. starpu_data_unregister(plan->in_handle);
  196. if (!PARALLEL)
  197. {
  198. if (plan->out_handle)
  199. starpu_data_unregister(plan->out_handle);
  200. }
  201. }
  202. struct starpu_task *
  203. STARPUFFT(start_handle)(STARPUFFT(plan) plan, starpu_data_handle_t in, starpu_data_handle_t out)
  204. {
  205. return STARPUFFT(start1dC2C)(plan, in, out);
  206. }
  207. void
  208. STARPUFFT(execute)(STARPUFFT(plan) plan, void *in, void *out)
  209. {
  210. memset(task_per_worker, 0, sizeof(task_per_worker));
  211. memset(samples_per_worker, 0, sizeof(task_per_worker));
  212. gettimeofday(&start, NULL);
  213. struct starpu_task *task = STARPUFFT(start)(plan, in, out);
  214. gettimeofday(&submit_tasks, NULL);
  215. starpu_task_wait(task);
  216. STARPUFFT(cleanup)(plan);
  217. gettimeofday(&end, NULL);
  218. }
  219. void
  220. STARPUFFT(execute_handle)(STARPUFFT(plan) plan, starpu_data_handle_t in, starpu_data_handle_t out)
  221. {
  222. struct starpu_task *task = STARPUFFT(start_handle)(plan, in, out);
  223. starpu_task_wait(task);
  224. }
  225. /* Destroy FFTW plans, unregister and free buffers, and free tags */
  226. void
  227. STARPUFFT(destroy_plan)(STARPUFFT(plan) plan)
  228. {
  229. int workerid, dim, i;
  230. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++)
  231. {
  232. switch (starpu_worker_get_type(workerid))
  233. {
  234. case STARPU_CPU_WORKER:
  235. #ifdef STARPU_HAVE_FFTW
  236. if (PARALLEL)
  237. {
  238. _FFTW(destroy_plan)(plan->plans[workerid].plan1_cpu);
  239. _FFTW(destroy_plan)(plan->plans[workerid].plan2_cpu);
  240. }
  241. else
  242. {
  243. _FFTW(destroy_plan)(plan->plans[workerid].plan_cpu);
  244. }
  245. #endif
  246. break;
  247. case STARPU_CUDA_WORKER:
  248. #ifdef STARPU_USE_CUDA
  249. /* FIXME: Can't deallocate */
  250. #endif
  251. break;
  252. default:
  253. /* Do not care, we won't be executing anything there. */
  254. break;
  255. }
  256. }
  257. if (PARALLEL)
  258. {
  259. for (i = 0; i < plan->totsize1; i++)
  260. {
  261. starpu_data_unregister(plan->twisted1_handle[i]);
  262. free(plan->twist1_tasks[i]);
  263. starpu_data_unregister(plan->fft1_handle[i]);
  264. free(plan->fft1_tasks[i]);
  265. }
  266. free(plan->twisted1_handle);
  267. free(plan->twist1_tasks);
  268. free(plan->fft1_handle);
  269. free(plan->fft1_tasks);
  270. free(plan->fft1_args);
  271. free(plan->join_task);
  272. for (i = 0; i < plan->totsize3; i++)
  273. {
  274. starpu_data_unregister(plan->twisted2_handle[i]);
  275. free(plan->twist2_tasks[i]);
  276. starpu_data_unregister(plan->fft2_handle[i]);
  277. free(plan->fft2_tasks[i]);
  278. free(plan->twist3_tasks[i]);
  279. }
  280. free(plan->end_task);
  281. free(plan->twisted2_handle);
  282. free(plan->twist2_tasks);
  283. free(plan->fft2_handle);
  284. free(plan->fft2_tasks);
  285. free(plan->twist3_tasks);
  286. free(plan->fft2_args);
  287. for (dim = 0; dim < plan->dim; dim++)
  288. {
  289. starpu_data_unregister(plan->roots_handle[dim]);
  290. free(plan->roots[dim]);
  291. }
  292. switch (plan->dim)
  293. {
  294. case 1:
  295. STARPUFFT(free_1d_tags)(plan);
  296. break;
  297. case 2:
  298. STARPUFFT(free_2d_tags)(plan);
  299. break;
  300. default:
  301. STARPU_ABORT();
  302. break;
  303. }
  304. free(plan->n1);
  305. free(plan->n2);
  306. STARPUFFT(free)(plan->twisted1);
  307. STARPUFFT(free)(plan->fft1);
  308. STARPUFFT(free)(plan->twisted2);
  309. STARPUFFT(free)(plan->fft2);
  310. }
  311. free(plan->n);
  312. free(plan);
  313. }
  314. void *
  315. STARPUFFT(malloc)(size_t n)
  316. {
  317. #ifdef STARPU_USE_CUDA
  318. void *res;
  319. starpu_malloc(&res, n);
  320. return res;
  321. #else
  322. # ifdef STARPU_HAVE_FFTW
  323. return _FFTW(malloc)(n);
  324. # else
  325. return malloc(n);
  326. # endif
  327. #endif
  328. }
  329. void
  330. STARPUFFT(free)(void *p)
  331. {
  332. #ifdef STARPU_USE_CUDA
  333. starpu_free(p);
  334. #else
  335. # ifdef STARPU_HAVE_FFTW
  336. _FFTW(free)(p);
  337. # else
  338. free(p);
  339. # endif
  340. #endif
  341. }
  342. void
  343. STARPUFFT(showstats)(FILE *out)
  344. {
  345. int worker;
  346. unsigned total;
  347. #define TIMING(begin,end) (double)((end.tv_sec - begin.tv_sec)*1000000 + (end.tv_usec - begin.tv_usec))
  348. #define MSTIMING(begin,end) (TIMING(begin,end)/1000.)
  349. double paratiming = TIMING(start,end);
  350. fprintf(out, "Tasks submission took %2.2f ms\n", MSTIMING(start,submit_tasks));
  351. fprintf(out, "Tasks termination took %2.2f ms\n", MSTIMING(submit_tasks,end));
  352. fprintf(out, "Total %2.2f ms\n", MSTIMING(start,end));
  353. for (worker = 0, total = 0; worker < starpu_worker_get_count(); worker++)
  354. total += task_per_worker[worker];
  355. for (worker = 0; worker < starpu_worker_get_count(); worker++)
  356. {
  357. if (task_per_worker[worker])
  358. {
  359. char name[32];
  360. starpu_worker_get_name(worker, name, sizeof(name));
  361. unsigned long bytes = sizeof(STARPUFFT(complex))*samples_per_worker[worker];
  362. fprintf(stderr, "\t%s -> %2.2f MB\t%2.2f\tMB/s\t%u %2.2f %%\n", name, (1.0*bytes)/(1024*1024), bytes/paratiming, task_per_worker[worker], (100.0*task_per_worker[worker])/total);
  363. }
  364. }
  365. }