starpufftx2d.c 25 KB

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