starpufftx2d.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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 DIV_2D_N 8
  18. #define DIV_2D_M 8
  19. #define I_SHIFT (I_BITS/2)
  20. #define J_BITS I_SHIFT
  21. #define STEP_TAG_2D(plan, step, i, j) _STEP_TAG(plan, step, ((starpu_tag_t) i << I_SHIFT) | (starpu_tag_t) j)
  22. #ifdef STARPU_USE_CUDA
  23. /* Twist the full vector into a n2,m2 chunk */
  24. static void
  25. STARPUFFT(twist1_2d_kernel_gpu)(void *descr[], void *_args)
  26. {
  27. struct STARPUFFT(args) *args = _args;
  28. STARPUFFT(plan) plan = args->plan;
  29. int i = args->i;
  30. int j = args->j;
  31. int n1 = plan->n1[0];
  32. int n2 = plan->n2[0];
  33. int m1 = plan->n1[1];
  34. int m2 = plan->n2[1];
  35. _cufftComplex * restrict in = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[0]);
  36. _cufftComplex * restrict twisted1 = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[1]);
  37. STARPUFFT(cuda_twist1_2d_host)(in, twisted1, i, j, n1, n2, m1, m2);
  38. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  39. }
  40. /* fft1:
  41. *
  42. * Perform one fft of size n2,m2 */
  43. static void
  44. STARPUFFT(fft1_2d_plan_gpu)(void *args)
  45. {
  46. STARPUFFT(plan) plan = args;
  47. int n2 = plan->n2[0];
  48. int m2 = plan->n2[1];
  49. int workerid = starpu_worker_get_id();
  50. cufftResult cures;
  51. cures = cufftPlan2d(&plan->plans[workerid].plan1_cuda, n2, m2, _CUFFT_C2C);
  52. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  53. cufftSetStream(plan->plans[workerid].plan1_cuda, starpu_cuda_get_local_stream());
  54. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  55. }
  56. static void
  57. STARPUFFT(fft1_2d_kernel_gpu)(void *descr[], void *_args)
  58. {
  59. struct STARPUFFT(args) *args = _args;
  60. STARPUFFT(plan) plan = args->plan;
  61. int i = args->i;
  62. int j = args->j;
  63. int n2 = plan->n2[0];
  64. int m2 = plan->n2[1];
  65. cufftResult cures;
  66. _cufftComplex * restrict in = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[0]);
  67. _cufftComplex * restrict out = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[1]);
  68. const _cufftComplex * restrict roots0 = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[2]);
  69. const _cufftComplex * restrict roots1 = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[3]);
  70. int workerid = starpu_worker_get_id();
  71. task_per_worker[workerid]++;
  72. cures = _cufftExecC2C(plan->plans[workerid].plan1_cuda, in, out, plan->sign == -1 ? CUFFT_FORWARD : CUFFT_INVERSE);
  73. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  74. /* synchronization is done after the twiddling */
  75. STARPUFFT(cuda_twiddle_2d_host)(out, roots0, roots1, n2, m2, i, j);
  76. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  77. }
  78. /* fft2:
  79. *
  80. * Perform n3*m3 ffts of size n1,m1 */
  81. static void
  82. STARPUFFT(fft2_2d_plan_gpu(void *args))
  83. {
  84. STARPUFFT(plan) plan = args;
  85. int n1 = plan->n1[0];
  86. int m1 = plan->n1[1];
  87. cufftResult cures;
  88. int workerid = starpu_worker_get_id();
  89. cures = cufftPlan2d(&plan->plans[workerid].plan2_cuda, n1, m1, _CUFFT_C2C);
  90. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  91. cufftSetStream(plan->plans[workerid].plan2_cuda, starpu_cuda_get_local_stream());
  92. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  93. }
  94. static void
  95. STARPUFFT(fft2_2d_kernel_gpu)(void *descr[], void *_args)
  96. {
  97. struct STARPUFFT(args) *args = _args;
  98. STARPUFFT(plan) plan = args->plan;
  99. int n1 = plan->n1[0];
  100. int n2 = plan->n2[0];
  101. int m1 = plan->n1[1];
  102. int m2 = plan->n2[1];
  103. int n3 = n2/DIV_2D_N;
  104. int m3 = m2/DIV_2D_M;
  105. int n;
  106. cufftResult cures;
  107. _cufftComplex * restrict in = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[0]);
  108. _cufftComplex * restrict out = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[1]);
  109. int workerid = starpu_worker_get_id();
  110. task_per_worker[workerid]++;
  111. for (n = 0; n < n3*m3; n++) {
  112. cures = _cufftExecC2C(plan->plans[workerid].plan2_cuda, in + n * n1*m1, out + n * n1*m1, plan->sign == -1 ? CUFFT_FORWARD : CUFFT_INVERSE);
  113. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  114. }
  115. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  116. }
  117. #endif
  118. /* Twist the full vector into a n2,m2 chunk */
  119. static void
  120. STARPUFFT(twist1_2d_kernel_cpu)(void *descr[], void *_args)
  121. {
  122. struct STARPUFFT(args) *args = _args;
  123. STARPUFFT(plan) plan = args->plan;
  124. int i = args->i;
  125. int j = args->j;
  126. int k, l;
  127. int n1 = plan->n1[0];
  128. int n2 = plan->n2[0];
  129. int m1 = plan->n1[1];
  130. int m2 = plan->n2[1];
  131. int m = plan->n[1];
  132. STARPUFFT(complex) * restrict in = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[0]);
  133. STARPUFFT(complex) * restrict twisted1 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[1]);
  134. /* printf("twist1 %d %d %g\n", i, j, (double) cabs(plan->in[i+j])); */
  135. for (k = 0; k < n2; k++)
  136. for (l = 0; l < m2; l++)
  137. twisted1[k*m2+l] = in[i*m+j+k*m*n1+l*m1];
  138. }
  139. #ifdef STARPU_HAVE_FFTW
  140. /* Perform an n2,m2 fft */
  141. static void
  142. STARPUFFT(fft1_2d_kernel_cpu)(void *descr[], void *_args)
  143. {
  144. struct STARPUFFT(args) *args = _args;
  145. STARPUFFT(plan) plan = args->plan;
  146. int i = args->i;
  147. int j = args->j;
  148. int k, l;
  149. int n2 = plan->n2[0];
  150. int m2 = plan->n2[1];
  151. int workerid = starpu_worker_get_id();
  152. task_per_worker[workerid]++;
  153. STARPUFFT(complex) *twisted1 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[0]);
  154. STARPUFFT(complex) *fft1 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[1]);
  155. /* printf("fft1 %d %d %g\n", i, j, (double) cabs(twisted1[0])); */
  156. _FFTW(execute_dft)(plan->plans[workerid].plan1_cpu, twisted1, fft1);
  157. for (k = 0; k < n2; k++)
  158. for (l = 0; l < m2; l++)
  159. fft1[k*m2 + l] = fft1[k*m2 + l] * plan->roots[0][i*k] * plan->roots[1][j*l];
  160. }
  161. #endif
  162. /* Twist the full vector into a package of n2/DIV_2D_N,m2/DIV_2D_M (n1,m1) chunks */
  163. static void
  164. STARPUFFT(twist2_2d_kernel_cpu)(void *descr[], void *_args)
  165. {
  166. struct STARPUFFT(args) *args = _args;
  167. STARPUFFT(plan) plan = args->plan;
  168. int kk = args->kk; /* between 0 and DIV_2D_N */
  169. int ll = args->ll; /* between 0 and DIV_2D_M */
  170. int kkk, lll; /* beetween 0,0 and n3,m3 */
  171. int i, j;
  172. int n1 = plan->n1[0];
  173. int n2 = plan->n2[0];
  174. int m1 = plan->n1[1];
  175. int m2 = plan->n2[1];
  176. int n3 = n2/DIV_2D_N;
  177. int m3 = m2/DIV_2D_M;
  178. STARPUFFT(complex) * restrict twisted2 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[0]);
  179. /* printf("twist2 %d %d %g\n", kk, ll, (double) cabs(plan->fft1[kk+ll])); */
  180. for (kkk = 0; kkk < n3; kkk++) {
  181. int k = kk * n3 + kkk;
  182. for (lll = 0; lll < m3; lll++) {
  183. int l = ll * m3 + lll;
  184. for (i = 0; i < n1; i++)
  185. for (j = 0; j < m1; j++)
  186. twisted2[kkk*m3*n1*m1+lll*n1*m1+i*m1+j] = plan->fft1[i*n1*n2*m2+j*n2*m2+k*m2+l];
  187. }
  188. }
  189. }
  190. #ifdef STARPU_HAVE_FFTW
  191. /* Perform (n2/DIV_2D_N)*(m2/DIV_2D_M) (n1,m1) ffts */
  192. static void
  193. STARPUFFT(fft2_2d_kernel_cpu)(void *descr[], void *_args)
  194. {
  195. struct STARPUFFT(args) *args = _args;
  196. STARPUFFT(plan) plan = args->plan;
  197. /* int kk = args->kk; */
  198. /* int ll = args->ll; */
  199. int workerid = starpu_worker_get_id();
  200. task_per_worker[workerid]++;
  201. STARPUFFT(complex) *twisted2 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[0]);
  202. STARPUFFT(complex) *fft2 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[1]);
  203. /* printf("fft2 %d %d %g\n", kk, ll, (double) cabs(twisted2[plan->totsize4-1])); */
  204. _FFTW(execute_dft)(plan->plans[workerid].plan2_cpu, twisted2, fft2);
  205. }
  206. #endif
  207. /* Spread the package of (n2/DIV_2D_N)*(m2/DIV_2D_M) (n1,m1) chunks into the full vector */
  208. static void
  209. STARPUFFT(twist3_2d_kernel_cpu)(void *descr[], void *_args)
  210. {
  211. struct STARPUFFT(args) *args = _args;
  212. STARPUFFT(plan) plan = args->plan;
  213. int kk = args->kk; /* between 0 and DIV_2D_N */
  214. int ll = args->ll; /* between 0 and DIV_2D_M */
  215. int kkk, lll; /* beetween 0,0 and n3,m3 */
  216. int i, j;
  217. int n1 = plan->n1[0];
  218. int n2 = plan->n2[0];
  219. int m1 = plan->n1[1];
  220. int m2 = plan->n2[1];
  221. int n3 = n2/DIV_2D_N;
  222. int m3 = m2/DIV_2D_M;
  223. int m = plan->n[1];
  224. const STARPUFFT(complex) * restrict fft2 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[0]);
  225. /* printf("twist3 %d %d %g\n", kk, ll, (double) cabs(fft2[0])); */
  226. for (kkk = 0; kkk < n3; kkk++) {
  227. int k = kk * n3 + kkk;
  228. for (lll = 0; lll < m3; lll++) {
  229. int l = ll * m3 + lll;
  230. for (i = 0; i < n1; i++)
  231. for (j = 0; j < m1; j++)
  232. plan->out[i*n2*m+j*m2+k*m+l] = fft2[kkk*m3*n1*m1+lll*n1*m1+i*m1+j];
  233. }
  234. }
  235. }
  236. struct starpu_perfmodel STARPUFFT(twist1_2d_model) = {
  237. .type = STARPU_HISTORY_BASED,
  238. .symbol = TYPE"twist1_2d"
  239. };
  240. struct starpu_perfmodel STARPUFFT(fft1_2d_model) = {
  241. .type = STARPU_HISTORY_BASED,
  242. .symbol = TYPE"fft1_2d"
  243. };
  244. struct starpu_perfmodel STARPUFFT(twist2_2d_model) = {
  245. .type = STARPU_HISTORY_BASED,
  246. .symbol = TYPE"twist2_2d"
  247. };
  248. struct starpu_perfmodel STARPUFFT(fft2_2d_model) = {
  249. .type = STARPU_HISTORY_BASED,
  250. .symbol = TYPE"fft2_2d"
  251. };
  252. struct starpu_perfmodel STARPUFFT(twist3_2d_model) = {
  253. .type = STARPU_HISTORY_BASED,
  254. .symbol = TYPE"twist3_2d"
  255. };
  256. static struct starpu_codelet STARPUFFT(twist1_2d_codelet) = {
  257. .where =
  258. #ifdef STARPU_USE_CUDA
  259. STARPU_CUDA|
  260. #endif
  261. STARPU_CPU,
  262. #ifdef STARPU_USE_CUDA
  263. .cuda_funcs = {STARPUFFT(twist1_2d_kernel_gpu), NULL},
  264. #endif
  265. .cpu_funcs = {STARPUFFT(twist1_2d_kernel_cpu), NULL},
  266. .model = &STARPUFFT(twist1_2d_model),
  267. .nbuffers = 2,
  268. .modes = {STARPU_R, STARPU_W}
  269. };
  270. static struct starpu_codelet STARPUFFT(fft1_2d_codelet) = {
  271. .where =
  272. #ifdef STARPU_USE_CUDA
  273. STARPU_CUDA|
  274. #endif
  275. #ifdef STARPU_HAVE_FFTW
  276. STARPU_CPU|
  277. #endif
  278. 0,
  279. #ifdef STARPU_USE_CUDA
  280. .cuda_funcs = {STARPUFFT(fft1_2d_kernel_gpu), NULL},
  281. #endif
  282. #ifdef STARPU_HAVE_FFTW
  283. .cpu_funcs = {STARPUFFT(fft1_2d_kernel_cpu), NULL},
  284. #endif
  285. .model = &STARPUFFT(fft1_2d_model),
  286. .nbuffers = 4,
  287. .modes = {STARPU_R, STARPU_W, STARPU_R, STARPU_R}
  288. };
  289. static struct starpu_codelet STARPUFFT(twist2_2d_codelet) = {
  290. .where = STARPU_CPU,
  291. .cpu_funcs = {STARPUFFT(twist2_2d_kernel_cpu), NULL},
  292. .model = &STARPUFFT(twist2_2d_model),
  293. .nbuffers = 1,
  294. .modes = {STARPU_W}
  295. };
  296. static struct starpu_codelet STARPUFFT(fft2_2d_codelet) = {
  297. .where =
  298. #ifdef STARPU_USE_CUDA
  299. STARPU_CUDA|
  300. #endif
  301. #ifdef STARPU_HAVE_FFTW
  302. STARPU_CPU|
  303. #endif
  304. 0,
  305. #ifdef STARPU_USE_CUDA
  306. .cuda_funcs = {STARPUFFT(fft2_2d_kernel_gpu), NULL},
  307. #endif
  308. #ifdef STARPU_HAVE_FFTW
  309. .cpu_funcs = {STARPUFFT(fft2_2d_kernel_cpu), NULL},
  310. #endif
  311. .model = &STARPUFFT(fft2_2d_model),
  312. .nbuffers = 2,
  313. .modes = {STARPU_R, STARPU_W}
  314. };
  315. static struct starpu_codelet STARPUFFT(twist3_2d_codelet) = {
  316. .where = STARPU_CPU,
  317. .cpu_funcs = {STARPUFFT(twist3_2d_kernel_cpu), NULL},
  318. .model = &STARPUFFT(twist3_2d_model),
  319. .nbuffers = 1,
  320. .modes = {STARPU_R}
  321. };
  322. /*
  323. *
  324. * Sequential version
  325. *
  326. */
  327. #ifdef STARPU_USE_CUDA
  328. /* Perform one fft of size n,m */
  329. static void
  330. STARPUFFT(fft_2d_plan_gpu)(void *args)
  331. {
  332. STARPUFFT(plan) plan = args;
  333. cufftResult cures;
  334. int n = plan->n[0];
  335. int m = plan->n[1];
  336. int workerid = starpu_worker_get_id();
  337. cures = cufftPlan2d(&plan->plans[workerid].plan_cuda, n, m, _CUFFT_C2C);
  338. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  339. cufftSetStream(plan->plans[workerid].plan_cuda, starpu_cuda_get_local_stream());
  340. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  341. }
  342. static void
  343. STARPUFFT(fft_2d_kernel_gpu)(void *descr[], void *args)
  344. {
  345. STARPUFFT(plan) plan = args;
  346. cufftResult cures;
  347. _cufftComplex * restrict in = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[0]);
  348. _cufftComplex * restrict out = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[1]);
  349. int workerid = starpu_worker_get_id();
  350. task_per_worker[workerid]++;
  351. cures = _cufftExecC2C(plan->plans[workerid].plan_cuda, in, out, plan->sign == -1 ? CUFFT_FORWARD : CUFFT_INVERSE);
  352. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  353. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  354. }
  355. #endif
  356. #ifdef STARPU_HAVE_FFTW
  357. /* Perform one fft of size n,m */
  358. static void
  359. STARPUFFT(fft_2d_kernel_cpu)(void *descr[], void *_args)
  360. {
  361. STARPUFFT(plan) plan = _args;
  362. int workerid = starpu_worker_get_id();
  363. task_per_worker[workerid]++;
  364. STARPUFFT(complex) * restrict in = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[0]);
  365. STARPUFFT(complex) * restrict out = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[1]);
  366. _FFTW(execute_dft)(plan->plans[workerid].plan_cpu, in, out);
  367. }
  368. #endif
  369. static struct starpu_perfmodel STARPUFFT(fft_2d_model) = {
  370. .type = STARPU_HISTORY_BASED,
  371. .symbol = TYPE"fft_2d"
  372. };
  373. static struct starpu_codelet STARPUFFT(fft_2d_codelet) = {
  374. .where =
  375. #ifdef STARPU_USE_CUDA
  376. STARPU_CUDA|
  377. #endif
  378. #ifdef STARPU_HAVE_FFTW
  379. STARPU_CPU|
  380. #endif
  381. 0,
  382. #ifdef STARPU_USE_CUDA
  383. .cuda_funcs = {STARPUFFT(fft_2d_kernel_gpu), NULL},
  384. #endif
  385. #ifdef STARPU_HAVE_FFTW
  386. .cpu_funcs = {STARPUFFT(fft_2d_kernel_cpu), NULL},
  387. #endif
  388. .model = &STARPUFFT(fft_2d_model),
  389. .nbuffers = 2,
  390. .modes = {STARPU_R, STARPU_W}
  391. };
  392. STARPUFFT(plan)
  393. STARPUFFT(plan_dft_2d)(int n, int m, int sign, unsigned flags)
  394. {
  395. int workerid;
  396. int n1 = DIV_2D_N;
  397. int n2 = n / n1;
  398. int n3;
  399. int m1 = DIV_2D_M;
  400. int m2 = m / m1;
  401. int m3;
  402. int z;
  403. struct starpu_task *task;
  404. if (PARALLEL) {
  405. /*
  406. * Simple strategy:
  407. *
  408. * - twist1: twist input in n1*m1 (n2,m2) chunks
  409. * - fft1: perform n1*m1 (n2,m2) ffts
  410. * - twist2: twist into n2*m2 (n1,m1) chunks distributed in
  411. * DIV_2D_N*DIV_2D_M groups
  412. * - fft2: perform DIV_2D_N*DIV_2D_M times n3*m3 (n1,m1) ffts
  413. * - twist3: twist back into output
  414. */
  415. #ifdef STARPU_USE_CUDA
  416. /* cufft 2D-3D limited to [2,16384] */
  417. while (n2 > 16384) {
  418. n1 *= 2;
  419. n2 /= 2;
  420. }
  421. #endif
  422. STARPU_ASSERT(n == n1*n2);
  423. STARPU_ASSERT(n1 < (1ULL << J_BITS));
  424. #ifdef STARPU_USE_CUDA
  425. /* cufft 2D-3D limited to [2,16384] */
  426. while (m2 > 16384) {
  427. m1 *= 2;
  428. m2 /= 2;
  429. }
  430. #endif
  431. STARPU_ASSERT(m == m1*m2);
  432. STARPU_ASSERT(m1 < (1ULL << J_BITS));
  433. /* distribute the n2*m2 second ffts into DIV_2D_N*DIV_2D_M packages */
  434. n3 = n2 / DIV_2D_N;
  435. STARPU_ASSERT(n2 == n3*DIV_2D_N);
  436. m3 = m2 / DIV_2D_M;
  437. STARPU_ASSERT(m2 == m3*DIV_2D_M);
  438. }
  439. /* TODO: flags? Automatically set FFTW_MEASURE on calibration? */
  440. STARPU_ASSERT(flags == 0);
  441. STARPUFFT(plan) plan = malloc(sizeof(*plan));
  442. memset(plan, 0, sizeof(*plan));
  443. if (PARALLEL) {
  444. plan->number = STARPU_ATOMIC_ADD(&starpufft_last_plan_number, 1) - 1;
  445. /* 4bit limitation in the tag space */
  446. STARPU_ASSERT(plan->number < (1ULL << NUMBER_BITS));
  447. }
  448. plan->dim = 2;
  449. plan->n = malloc(plan->dim * sizeof(*plan->n));
  450. plan->n[0] = n;
  451. plan->n[1] = m;
  452. if (PARALLEL) {
  453. check_dims(plan);
  454. plan->n1 = malloc(plan->dim * sizeof(*plan->n1));
  455. plan->n1[0] = n1;
  456. plan->n1[1] = m1;
  457. plan->n2 = malloc(plan->dim * sizeof(*plan->n2));
  458. plan->n2[0] = n2;
  459. plan->n2[1] = m2;
  460. }
  461. plan->totsize = n * m;
  462. if (PARALLEL) {
  463. plan->totsize1 = n1 * m1;
  464. plan->totsize2 = n2 * m2;
  465. plan->totsize3 = DIV_2D_N * DIV_2D_M;
  466. plan->totsize4 = plan->totsize / plan->totsize3;
  467. }
  468. plan->type = C2C;
  469. plan->sign = sign;
  470. if (PARALLEL) {
  471. /* Compute the w^k just once. */
  472. compute_roots(plan);
  473. }
  474. /* Initialize per-worker working set */
  475. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++) {
  476. switch (starpu_worker_get_type(workerid)) {
  477. case STARPU_CPU_WORKER:
  478. #ifdef STARPU_HAVE_FFTW
  479. if (PARALLEL) {
  480. /* first fft plan: one n2*m2 fft */
  481. plan->plans[workerid].plan1_cpu = _FFTW(plan_dft_2d)(n2, m2, NULL, (void*) 1, sign, _FFTW_FLAGS);
  482. STARPU_ASSERT(plan->plans[workerid].plan1_cpu);
  483. /* second fft plan: n3*m3 n1*m1 ffts */
  484. plan->plans[workerid].plan2_cpu = _FFTW(plan_many_dft)(plan->dim,
  485. plan->n1, n3*m3,
  486. NULL, NULL, 1, plan->totsize1,
  487. (void*) 1, NULL, 1, plan->totsize1,
  488. sign, _FFTW_FLAGS);
  489. STARPU_ASSERT(plan->plans[workerid].plan2_cpu);
  490. } else {
  491. /* fft plan: one fft of size n, m. */
  492. plan->plans[workerid].plan_cpu = _FFTW(plan_dft_2d)(n, m, NULL, (void*) 1, sign, _FFTW_FLAGS);
  493. STARPU_ASSERT(plan->plans[workerid].plan_cpu);
  494. }
  495. #else
  496. /* #warning libstarpufft can not work correctly if libfftw3 is not installed */
  497. #endif
  498. break;
  499. case STARPU_CUDA_WORKER:
  500. break;
  501. default:
  502. /* Do not care, we won't be executing anything there. */
  503. break;
  504. }
  505. }
  506. #ifdef STARPU_USE_CUDA
  507. if (PARALLEL) {
  508. starpu_execute_on_each_worker(STARPUFFT(fft1_2d_plan_gpu), plan, STARPU_CUDA);
  509. starpu_execute_on_each_worker(STARPUFFT(fft2_2d_plan_gpu), plan, STARPU_CUDA);
  510. } else {
  511. starpu_execute_on_each_worker(STARPUFFT(fft_2d_plan_gpu), plan, STARPU_CUDA);
  512. }
  513. #endif
  514. if (PARALLEL) {
  515. /* Allocate buffers. */
  516. plan->twisted1 = STARPUFFT(malloc)(plan->totsize * sizeof(*plan->twisted1));
  517. memset(plan->twisted1, 0, plan->totsize * sizeof(*plan->twisted1));
  518. plan->fft1 = STARPUFFT(malloc)(plan->totsize * sizeof(*plan->fft1));
  519. memset(plan->fft1, 0, plan->totsize * sizeof(*plan->fft1));
  520. plan->twisted2 = STARPUFFT(malloc)(plan->totsize * sizeof(*plan->twisted2));
  521. memset(plan->twisted2, 0, plan->totsize * sizeof(*plan->twisted2));
  522. plan->fft2 = STARPUFFT(malloc)(plan->totsize * sizeof(*plan->fft2));
  523. memset(plan->fft2, 0, plan->totsize * sizeof(*plan->fft2));
  524. /* Allocate handle arrays */
  525. plan->twisted1_handle = malloc(plan->totsize1 * sizeof(*plan->twisted1_handle));
  526. plan->fft1_handle = malloc(plan->totsize1 * sizeof(*plan->fft1_handle));
  527. plan->twisted2_handle = malloc(plan->totsize3 * sizeof(*plan->twisted2_handle));
  528. plan->fft2_handle = malloc(plan->totsize3 * sizeof(*plan->fft2_handle));
  529. /* Allocate task arrays */
  530. plan->twist1_tasks = malloc(plan->totsize1 * sizeof(*plan->twist1_tasks));
  531. plan->fft1_tasks = malloc(plan->totsize1 * sizeof(*plan->fft1_tasks));
  532. plan->twist2_tasks = malloc(plan->totsize3 * sizeof(*plan->twist2_tasks));
  533. plan->fft2_tasks = malloc(plan->totsize3 * sizeof(*plan->fft2_tasks));
  534. plan->twist3_tasks = malloc(plan->totsize3 * sizeof(*plan->twist3_tasks));
  535. /* Allocate codelet argument arrays */
  536. plan->fft1_args = malloc(plan->totsize1 * sizeof(*plan->fft1_args));
  537. plan->fft2_args = malloc(plan->totsize3 * sizeof(*plan->fft2_args));
  538. /* Create first-round tasks */
  539. for (z = 0; z < plan->totsize1; z++) {
  540. int i = z / m1, j = z % m1;
  541. #define STEP_TAG(step) STEP_TAG_2D(plan, step, i, j)
  542. /* TODO: get rid of tags */
  543. plan->fft1_args[z].plan = plan;
  544. plan->fft1_args[z].i = i;
  545. plan->fft1_args[z].j = j;
  546. /* Register (n2,m2) chunks */
  547. starpu_vector_data_register(&plan->twisted1_handle[z], 0, (uintptr_t) &plan->twisted1[z*plan->totsize2], plan->totsize2, sizeof(*plan->twisted1));
  548. starpu_vector_data_register(&plan->fft1_handle[z], 0, (uintptr_t) &plan->fft1[z*plan->totsize2], plan->totsize2, sizeof(*plan->fft1));
  549. /* We'll need it on the CPU for the second twist anyway */
  550. starpu_data_set_wt_mask(plan->fft1_handle[z], 1<<0);
  551. /* Create twist1 task */
  552. plan->twist1_tasks[z] = task = starpu_task_create();
  553. task->cl = &STARPUFFT(twist1_2d_codelet);
  554. /* task->handles[0] = to be filled at execution */
  555. task->handles[1] = plan->twisted1_handle[z];
  556. task->cl_arg = &plan->fft1_args[z];
  557. task->tag_id = STEP_TAG(TWIST1);
  558. task->use_tag = 1;
  559. task->destroy = 0;
  560. /* Tell that fft1 depends on twisted1 */
  561. starpu_tag_declare_deps(STEP_TAG(FFT1),
  562. 1, STEP_TAG(TWIST1));
  563. /* Create FFT1 task */
  564. plan->fft1_tasks[z] = task = starpu_task_create();
  565. task->cl = &STARPUFFT(fft1_2d_codelet);
  566. task->handles[0] = plan->twisted1_handle[z];
  567. task->handles[1] = plan->fft1_handle[z];
  568. task->handles[2] = plan->roots_handle[0];
  569. task->handles[3] = plan->roots_handle[1];
  570. task->cl_arg = &plan->fft1_args[z];
  571. task->tag_id = STEP_TAG(FFT1);
  572. task->use_tag = 1;
  573. task->destroy = 0;
  574. /* Tell that to be done with first step we need to have
  575. * finished this fft1 */
  576. starpu_tag_declare_deps(STEP_TAG_2D(plan, JOIN, 0, 0),
  577. 1, STEP_TAG(FFT1));
  578. #undef STEP_TAG
  579. }
  580. /* Create join task */
  581. plan->join_task = task = starpu_task_create();
  582. task->cl = NULL;
  583. task->tag_id = STEP_TAG_2D(plan, JOIN, 0, 0);
  584. task->use_tag = 1;
  585. task->destroy = 0;
  586. /* Create second-round tasks */
  587. for (z = 0; z < plan->totsize3; z++) {
  588. int kk = z / DIV_2D_M, ll = z % DIV_2D_M;
  589. #define STEP_TAG(step) STEP_TAG_2D(plan, step, kk, ll)
  590. plan->fft2_args[z].plan = plan;
  591. plan->fft2_args[z].kk = kk;
  592. plan->fft2_args[z].ll = ll;
  593. /* Register n3*m3 (n1,m1) chunks */
  594. starpu_vector_data_register(&plan->twisted2_handle[z], 0, (uintptr_t) &plan->twisted2[z*plan->totsize4], plan->totsize4, sizeof(*plan->twisted2));
  595. starpu_vector_data_register(&plan->fft2_handle[z], 0, (uintptr_t) &plan->fft2[z*plan->totsize4], plan->totsize4, sizeof(*plan->fft2));
  596. /* We'll need it on the CPU for the last twist anyway */
  597. starpu_data_set_wt_mask(plan->fft2_handle[z], 1<<0);
  598. /* Tell that twisted2 depends on the whole first step to be
  599. * done */
  600. starpu_tag_declare_deps(STEP_TAG(TWIST2),
  601. 1, STEP_TAG_2D(plan, JOIN, 0, 0));
  602. /* Create twist2 task */
  603. plan->twist2_tasks[z] = task = starpu_task_create();
  604. task->cl = &STARPUFFT(twist2_2d_codelet);
  605. task->handles[0] = plan->twisted2_handle[z];
  606. task->cl_arg = &plan->fft2_args[z];
  607. task->tag_id = STEP_TAG(TWIST2);
  608. task->use_tag = 1;
  609. task->destroy = 0;
  610. /* Tell that fft2 depends on twisted2 */
  611. starpu_tag_declare_deps(STEP_TAG(FFT2),
  612. 1, STEP_TAG(TWIST2));
  613. /* Create FFT2 task */
  614. plan->fft2_tasks[z] = task = starpu_task_create();
  615. task->cl = &STARPUFFT(fft2_2d_codelet);
  616. task->handles[0] = plan->twisted2_handle[z];
  617. task->handles[1] = plan->fft2_handle[z];
  618. task->cl_arg = &plan->fft2_args[z];
  619. task->tag_id = STEP_TAG(FFT2);
  620. task->use_tag = 1;
  621. task->destroy = 0;
  622. /* Tell that twist3 depends on fft2 */
  623. starpu_tag_declare_deps(STEP_TAG(TWIST3),
  624. 1, STEP_TAG(FFT2));
  625. /* Create twist3 tasks */
  626. /* These run only on CPUs and thus write directly into the
  627. * application output buffer. */
  628. plan->twist3_tasks[z] = task = starpu_task_create();
  629. task->cl = &STARPUFFT(twist3_2d_codelet);
  630. task->handles[0] = plan->fft2_handle[z];
  631. task->cl_arg = &plan->fft2_args[z];
  632. task->tag_id = STEP_TAG(TWIST3);
  633. task->use_tag = 1;
  634. task->destroy = 0;
  635. /* Tell that to be completely finished we need to have finished this twisted3 */
  636. starpu_tag_declare_deps(STEP_TAG_2D(plan, END, 0, 0),
  637. 1, STEP_TAG(TWIST3));
  638. #undef STEP_TAG
  639. }
  640. /* Create end task */
  641. plan->end_task = task = starpu_task_create();
  642. task->cl = NULL;
  643. task->tag_id = STEP_TAG_2D(plan, END, 0, 0);
  644. task->use_tag = 1;
  645. task->destroy = 0;
  646. }
  647. return plan;
  648. }
  649. /* Actually submit all the tasks. */
  650. static struct starpu_task *
  651. STARPUFFT(start2dC2C)(STARPUFFT(plan) plan, starpu_data_handle_t in, starpu_data_handle_t out)
  652. {
  653. STARPU_ASSERT(plan->type == C2C);
  654. int z;
  655. int ret;
  656. if (PARALLEL) {
  657. for (z=0; z < plan->totsize1; z++) {
  658. ret = starpu_task_submit(plan->twist1_tasks[z]);
  659. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  660. ret = starpu_task_submit(plan->fft1_tasks[z]);
  661. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  662. }
  663. ret = starpu_task_submit(plan->join_task);
  664. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  665. for (z=0; z < plan->totsize3; z++) {
  666. ret = starpu_task_submit(plan->twist2_tasks[z]);
  667. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  668. ret = starpu_task_submit(plan->fft2_tasks[z]);
  669. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  670. ret = starpu_task_submit(plan->twist3_tasks[z]);
  671. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  672. }
  673. ret = starpu_task_submit(plan->end_task);
  674. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  675. return plan->end_task;
  676. } else /* !PARALLEL */ {
  677. struct starpu_task *task;
  678. /* Create FFT task */
  679. task = starpu_task_create();
  680. task->cl = &STARPUFFT(fft_2d_codelet);
  681. task->handles[0] = in;
  682. task->handles[1] = out;
  683. task->cl_arg = plan;
  684. ret = starpu_task_submit(task);
  685. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  686. return task;
  687. }
  688. }
  689. /* Free all the tags. The generic code handles freeing the buffers. */
  690. static void
  691. STARPUFFT(free_2d_tags)(STARPUFFT(plan) plan)
  692. {
  693. unsigned i, j;
  694. int n1 = plan->n1[0];
  695. int m1 = plan->n1[1];
  696. if (!PARALLEL)
  697. return;
  698. for (i = 0; i < n1; i++) {
  699. for (j = 0; j < m1; j++) {
  700. starpu_tag_remove(STEP_TAG_2D(plan, TWIST1, i, j));
  701. starpu_tag_remove(STEP_TAG_2D(plan, FFT1, i, j));
  702. }
  703. }
  704. starpu_tag_remove(STEP_TAG_2D(plan, JOIN, 0, 0));
  705. for (i = 0; i < DIV_2D_N; i++) {
  706. for (j = 0; j < DIV_2D_M; j++) {
  707. starpu_tag_remove(STEP_TAG_2D(plan, TWIST2, i, j));
  708. starpu_tag_remove(STEP_TAG_2D(plan, FFT2, i, j));
  709. starpu_tag_remove(STEP_TAG_2D(plan, TWIST3, i, j));
  710. }
  711. }
  712. starpu_tag_remove(STEP_TAG_2D(plan, END, 0, 0));
  713. }