starpufftx2d.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010 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. /* Perform an n2,m2 fft */
  41. static void
  42. STARPUFFT(fft1_2d_kernel_gpu)(void *descr[], void *_args)
  43. {
  44. struct STARPUFFT(args) *args = _args;
  45. STARPUFFT(plan) plan = args->plan;
  46. int i = args->i;
  47. int j = args->j;
  48. int n2 = plan->n2[0];
  49. int m2 = plan->n2[1];
  50. cufftResult cures;
  51. _cufftComplex * restrict in = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[0]);
  52. _cufftComplex * restrict out = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[1]);
  53. const _cufftComplex * restrict roots0 = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[2]);
  54. const _cufftComplex * restrict roots1 = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[3]);
  55. int workerid = starpu_worker_get_id();
  56. task_per_worker[workerid]++;
  57. if (!plan->plans[workerid].initialized1) {
  58. cures = cufftPlan2d(&plan->plans[workerid].plan1_cuda, n2, m2, _CUFFT_C2C);
  59. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  60. cufftSetStream(plan->plans[workerid].plan1_cuda, starpu_cuda_get_local_stream());
  61. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  62. plan->plans[workerid].initialized1 = 1;
  63. }
  64. cures = _cufftExecC2C(plan->plans[workerid].plan1_cuda, in, out, plan->sign == -1 ? CUFFT_FORWARD : CUFFT_INVERSE);
  65. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  66. /* synchronization is done after the twiddling */
  67. STARPUFFT(cuda_twiddle_2d_host)(out, roots0, roots1, n2, m2, i, j);
  68. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  69. }
  70. static void
  71. STARPUFFT(fft2_2d_kernel_gpu)(void *descr[], void *_args)
  72. {
  73. struct STARPUFFT(args) *args = _args;
  74. STARPUFFT(plan) plan = args->plan;
  75. int n1 = plan->n1[0];
  76. int n2 = plan->n2[0];
  77. int m1 = plan->n1[1];
  78. int m2 = plan->n2[1];
  79. int n3 = n2/DIV_2D_N;
  80. int m3 = m2/DIV_2D_M;
  81. int n;
  82. cufftResult cures;
  83. _cufftComplex * restrict in = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[0]);
  84. _cufftComplex * restrict out = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[1]);
  85. int workerid = starpu_worker_get_id();
  86. task_per_worker[workerid]++;
  87. if (!plan->plans[workerid].initialized2) {
  88. cures = cufftPlan2d(&plan->plans[workerid].plan2_cuda, n1, m1, _CUFFT_C2C);
  89. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  90. cufftSetStream(plan->plans[workerid].plan2_cuda, starpu_cuda_get_local_stream());
  91. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  92. plan->plans[workerid].initialized2 = 1;
  93. }
  94. for (n = 0; n < n3*m3; n++) {
  95. cures = _cufftExecC2C(plan->plans[workerid].plan2_cuda, in + n * n1*m1, out + n * n1*m1, plan->sign == -1 ? CUFFT_FORWARD : CUFFT_INVERSE);
  96. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  97. }
  98. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  99. }
  100. #endif
  101. /* Twist the full vector into a n2,m2 chunk */
  102. static void
  103. STARPUFFT(twist1_2d_kernel_cpu)(void *descr[], void *_args)
  104. {
  105. struct STARPUFFT(args) *args = _args;
  106. STARPUFFT(plan) plan = args->plan;
  107. int i = args->i;
  108. int j = args->j;
  109. int k, l;
  110. int n1 = plan->n1[0];
  111. int n2 = plan->n2[0];
  112. int m1 = plan->n1[1];
  113. int m2 = plan->n2[1];
  114. int m = plan->n[1];
  115. STARPUFFT(complex) * restrict in = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[0]);
  116. STARPUFFT(complex) * restrict twisted1 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[1]);
  117. /* printf("twist1 %d %d %g\n", i, j, (double) cabs(plan->in[i+j])); */
  118. for (k = 0; k < n2; k++)
  119. for (l = 0; l < m2; l++)
  120. twisted1[k*m2+l] = in[i*m+j+k*m*n1+l*m1];
  121. }
  122. #ifdef STARPU_HAVE_FFTW
  123. /* Perform an n2,m2 fft */
  124. static void
  125. STARPUFFT(fft1_2d_kernel_cpu)(void *descr[], void *_args)
  126. {
  127. struct STARPUFFT(args) *args = _args;
  128. STARPUFFT(plan) plan = args->plan;
  129. int i = args->i;
  130. int j = args->j;
  131. int k, l;
  132. int n2 = plan->n2[0];
  133. int m2 = plan->n2[1];
  134. int workerid = starpu_worker_get_id();
  135. task_per_worker[workerid]++;
  136. const STARPUFFT(complex) *twisted1 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[0]);
  137. STARPUFFT(complex) *fft1 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[1]);
  138. _fftw_complex * restrict worker_in1 = (STARPUFFT(complex) *)plan->plans[workerid].in1;
  139. _fftw_complex * restrict worker_out1 = (STARPUFFT(complex) *)plan->plans[workerid].out1;
  140. /* printf("fft1 %d %d %g\n", i, j, (double) cabs(twisted1[0])); */
  141. memcpy(worker_in1, twisted1, plan->totsize2 * sizeof(*worker_in1));
  142. _FFTW(execute)(plan->plans[workerid].plan1_cpu);
  143. for (k = 0; k < n2; k++)
  144. for (l = 0; l < m2; l++)
  145. fft1[k*m2 + l] = worker_out1[k*m2 + l] * plan->roots[0][i*k] * plan->roots[1][j*l];
  146. }
  147. #endif
  148. /* Twist the full vector into a package of n2/DIV_2D_N,m2/DIV_2D_M (n1,m1) chunks */
  149. static void
  150. STARPUFFT(twist2_2d_kernel_cpu)(void *descr[], void *_args)
  151. {
  152. struct STARPUFFT(args) *args = _args;
  153. STARPUFFT(plan) plan = args->plan;
  154. int kk = args->kk; /* between 0 and DIV_2D_N */
  155. int ll = args->ll; /* between 0 and DIV_2D_M */
  156. int kkk, lll; /* beetween 0,0 and n3,m3 */
  157. int i, j;
  158. int n1 = plan->n1[0];
  159. int n2 = plan->n2[0];
  160. int m1 = plan->n1[1];
  161. int m2 = plan->n2[1];
  162. int n3 = n2/DIV_2D_N;
  163. int m3 = m2/DIV_2D_M;
  164. STARPUFFT(complex) * restrict twisted2 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[0]);
  165. /* printf("twist2 %d %d %g\n", kk, ll, (double) cabs(plan->fft1[kk+ll])); */
  166. for (kkk = 0; kkk < n3; kkk++) {
  167. int k = kk * n3 + kkk;
  168. for (lll = 0; lll < m3; lll++) {
  169. int l = ll * m3 + lll;
  170. for (i = 0; i < n1; i++)
  171. for (j = 0; j < m1; j++)
  172. twisted2[kkk*m3*n1*m1+lll*n1*m1+i*m1+j] = plan->fft1[i*n1*n2*m2+j*n2*m2+k*m2+l];
  173. }
  174. }
  175. }
  176. #ifdef STARPU_HAVE_FFTW
  177. /* Perform (n2/DIV_2D_N)*(m2/DIV_2D_M) (n1,m1) ffts */
  178. static void
  179. STARPUFFT(fft2_2d_kernel_cpu)(void *descr[], void *_args)
  180. {
  181. struct STARPUFFT(args) *args = _args;
  182. STARPUFFT(plan) plan = args->plan;
  183. /* int kk = args->kk; */
  184. /* int ll = args->ll; */
  185. int workerid = starpu_worker_get_id();
  186. task_per_worker[workerid]++;
  187. const STARPUFFT(complex) *twisted2 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[0]);
  188. STARPUFFT(complex) *fft2 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[1]);
  189. /* printf("fft2 %d %d %g\n", kk, ll, (double) cabs(twisted2[plan->totsize4-1])); */
  190. _fftw_complex * restrict worker_in2 = (STARPUFFT(complex) *)plan->plans[workerid].in2;
  191. _fftw_complex * restrict worker_out2 = (STARPUFFT(complex) *)plan->plans[workerid].out2;
  192. memcpy(worker_in2, twisted2, plan->totsize4 * sizeof(*worker_in2));
  193. _FFTW(execute)(plan->plans[workerid].plan2_cpu);
  194. /* no twiddle */
  195. memcpy(fft2, worker_out2, plan->totsize4 * sizeof(*worker_out2));
  196. }
  197. #endif
  198. /* Spread the package of (n2/DIV_2D_N)*(m2/DIV_2D_M) (n1,m1) chunks into the full vector */
  199. static void
  200. STARPUFFT(twist3_2d_kernel_cpu)(void *descr[], void *_args)
  201. {
  202. struct STARPUFFT(args) *args = _args;
  203. STARPUFFT(plan) plan = args->plan;
  204. int kk = args->kk; /* between 0 and DIV_2D_N */
  205. int ll = args->ll; /* between 0 and DIV_2D_M */
  206. int kkk, lll; /* beetween 0,0 and n3,m3 */
  207. int i, j;
  208. int n1 = plan->n1[0];
  209. int n2 = plan->n2[0];
  210. int m1 = plan->n1[1];
  211. int m2 = plan->n2[1];
  212. int n3 = n2/DIV_2D_N;
  213. int m3 = m2/DIV_2D_M;
  214. int m = plan->n[1];
  215. const STARPUFFT(complex) * restrict fft2 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[0]);
  216. /* printf("twist3 %d %d %g\n", kk, ll, (double) cabs(fft2[0])); */
  217. for (kkk = 0; kkk < n3; kkk++) {
  218. int k = kk * n3 + kkk;
  219. for (lll = 0; lll < m3; lll++) {
  220. int l = ll * m3 + lll;
  221. for (i = 0; i < n1; i++)
  222. for (j = 0; j < m1; j++)
  223. plan->out[i*n2*m+j*m2+k*m+l] = fft2[kkk*m3*n1*m1+lll*n1*m1+i*m1+j];
  224. }
  225. }
  226. }
  227. struct starpu_perfmodel_t STARPUFFT(twist1_2d_model) = {
  228. .type = STARPU_HISTORY_BASED,
  229. .symbol = TYPE"twist1_2d"
  230. };
  231. struct starpu_perfmodel_t STARPUFFT(fft1_2d_model) = {
  232. .type = STARPU_HISTORY_BASED,
  233. .symbol = TYPE"fft1_2d"
  234. };
  235. struct starpu_perfmodel_t STARPUFFT(twist2_2d_model) = {
  236. .type = STARPU_HISTORY_BASED,
  237. .symbol = TYPE"twist2_2d"
  238. };
  239. struct starpu_perfmodel_t STARPUFFT(fft2_2d_model) = {
  240. .type = STARPU_HISTORY_BASED,
  241. .symbol = TYPE"fft2_2d"
  242. };
  243. struct starpu_perfmodel_t STARPUFFT(twist3_2d_model) = {
  244. .type = STARPU_HISTORY_BASED,
  245. .symbol = TYPE"twist3_2d"
  246. };
  247. static starpu_codelet STARPUFFT(twist1_2d_codelet) = {
  248. .where =
  249. #ifdef STARPU_USE_CUDA
  250. STARPU_CUDA|
  251. #endif
  252. STARPU_CPU,
  253. #ifdef STARPU_USE_CUDA
  254. .cuda_func = STARPUFFT(twist1_2d_kernel_gpu),
  255. #endif
  256. .cpu_func = STARPUFFT(twist1_2d_kernel_cpu),
  257. .model = &STARPUFFT(twist1_2d_model),
  258. .nbuffers = 2
  259. };
  260. static starpu_codelet STARPUFFT(fft1_2d_codelet) = {
  261. .where =
  262. #ifdef STARPU_USE_CUDA
  263. STARPU_CUDA|
  264. #endif
  265. #ifdef STARPU_HAVE_FFTW
  266. STARPU_CPU|
  267. #endif
  268. 0,
  269. #ifdef STARPU_USE_CUDA
  270. .cuda_func = STARPUFFT(fft1_2d_kernel_gpu),
  271. #endif
  272. #ifdef STARPU_HAVE_FFTW
  273. .cpu_func = STARPUFFT(fft1_2d_kernel_cpu),
  274. #endif
  275. .model = &STARPUFFT(fft1_2d_model),
  276. .nbuffers = 4
  277. };
  278. static starpu_codelet STARPUFFT(twist2_2d_codelet) = {
  279. .where = STARPU_CPU,
  280. .cpu_func = STARPUFFT(twist2_2d_kernel_cpu),
  281. .model = &STARPUFFT(twist2_2d_model),
  282. .nbuffers = 1
  283. };
  284. static starpu_codelet STARPUFFT(fft2_2d_codelet) = {
  285. .where =
  286. #ifdef STARPU_USE_CUDA
  287. STARPU_CUDA|
  288. #endif
  289. #ifdef STARPU_HAVE_FFTW
  290. STARPU_CPU|
  291. #endif
  292. 0,
  293. #ifdef STARPU_USE_CUDA
  294. .cuda_func = STARPUFFT(fft2_2d_kernel_gpu),
  295. #endif
  296. #ifdef STARPU_HAVE_FFTW
  297. .cpu_func = STARPUFFT(fft2_2d_kernel_cpu),
  298. #endif
  299. .model = &STARPUFFT(fft2_2d_model),
  300. .nbuffers = 2
  301. };
  302. static starpu_codelet STARPUFFT(twist3_2d_codelet) = {
  303. .where = STARPU_CPU,
  304. .cpu_func = STARPUFFT(twist3_2d_kernel_cpu),
  305. .model = &STARPUFFT(twist3_2d_model),
  306. .nbuffers = 1
  307. };
  308. STARPUFFT(plan)
  309. STARPUFFT(plan_dft_2d)(int n, int m, int sign, unsigned flags)
  310. {
  311. int workerid;
  312. int n1 = DIV_2D_N;
  313. int n2 = n / n1;
  314. int n3;
  315. int m1 = DIV_2D_M;
  316. int m2 = m / m1;
  317. int m3;
  318. int z;
  319. struct starpu_task *task;
  320. /*
  321. * Simple strategy:
  322. *
  323. * - twist1: twist input in n1*m1 (n2,m2) chunks
  324. * - fft1: perform n1*m1 (n2,m2) ffts
  325. * - twist2: twist into n2*m2 (n1,m1) chunks distributed in
  326. * DIV_2D_N*DIV_2D_M groups
  327. * - fft2: perform DIV_2D_N*DIV_2D_M times n3*m3 (n1,m1) ffts
  328. * - twist3: twist back into output
  329. */
  330. #ifdef STARPU_USE_CUDA
  331. /* cufft 2D-3D limited to [2,16384] */
  332. while (n2 > 16384) {
  333. n1 *= 2;
  334. n2 /= 2;
  335. }
  336. #endif
  337. STARPU_ASSERT(n == n1*n2);
  338. STARPU_ASSERT(n1 < (1ULL << J_BITS));
  339. #ifdef STARPU_USE_CUDA
  340. /* cufft 2D-3D limited to [2,16384] */
  341. while (m2 > 16384) {
  342. m1 *= 2;
  343. m2 /= 2;
  344. }
  345. #endif
  346. STARPU_ASSERT(m == m1*m2);
  347. STARPU_ASSERT(m1 < (1ULL << J_BITS));
  348. /* distribute the n2*m2 second ffts into DIV_2D_N*DIV_2D_M packages */
  349. n3 = n2 / DIV_2D_N;
  350. STARPU_ASSERT(n2 == n3*DIV_2D_N);
  351. m3 = m2 / DIV_2D_M;
  352. STARPU_ASSERT(m2 == m3*DIV_2D_M);
  353. /* TODO: flags? Automatically set FFTW_MEASURE on calibration? */
  354. STARPU_ASSERT(flags == 0);
  355. STARPUFFT(plan) plan = malloc(sizeof(*plan));
  356. memset(plan, 0, sizeof(*plan));
  357. plan->number = STARPU_ATOMIC_ADD(&starpufft_last_plan_number, 1) - 1;
  358. /* 4bit limitation in the tag space */
  359. STARPU_ASSERT(plan->number < (1ULL << NUMBER_BITS));
  360. plan->dim = 2;
  361. plan->n = malloc(plan->dim * sizeof(*plan->n));
  362. plan->n[0] = n;
  363. plan->n[1] = m;
  364. check_dims(plan);
  365. plan->n1 = malloc(plan->dim * sizeof(*plan->n1));
  366. plan->n1[0] = n1;
  367. plan->n1[1] = m1;
  368. plan->n2 = malloc(plan->dim * sizeof(*plan->n2));
  369. plan->n2[0] = n2;
  370. plan->n2[1] = m2;
  371. plan->totsize = n * m;
  372. plan->totsize1 = n1 * m1;
  373. plan->totsize2 = n2 * m2;
  374. plan->totsize3 = DIV_2D_N * DIV_2D_M;
  375. plan->totsize4 = plan->totsize / plan->totsize3;
  376. plan->type = C2C;
  377. plan->sign = sign;
  378. compute_roots(plan);
  379. /* Initialize per-worker working set */
  380. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++) {
  381. switch (starpu_worker_get_type(workerid)) {
  382. case STARPU_CPU_WORKER:
  383. #ifdef STARPU_HAVE_FFTW
  384. /* first fft plan: one n2*m2 fft */
  385. plan->plans[workerid].in1 = _FFTW(malloc)(plan->totsize2 * sizeof(_fftw_complex));
  386. memset(plan->plans[workerid].in1, 0, plan->totsize2 * sizeof(_fftw_complex));
  387. plan->plans[workerid].out1 = _FFTW(malloc)(plan->totsize2 * sizeof(_fftw_complex));
  388. memset(plan->plans[workerid].out1, 0, plan->totsize2 * sizeof(_fftw_complex));
  389. plan->plans[workerid].plan1_cpu = _FFTW(plan_dft_2d)(n2, m2, plan->plans[workerid].in1, plan->plans[workerid].out1, sign, _FFTW_FLAGS);
  390. STARPU_ASSERT(plan->plans[workerid].plan1_cpu);
  391. /* second fft plan: n3*m3 n1*m1 ffts */
  392. plan->plans[workerid].in2 = _FFTW(malloc)(plan->totsize4 * sizeof(_fftw_complex));
  393. memset(plan->plans[workerid].in2, 0, plan->totsize4 * sizeof(_fftw_complex));
  394. plan->plans[workerid].out2 = _FFTW(malloc)(plan->totsize4 * sizeof(_fftw_complex));
  395. memset(plan->plans[workerid].out2, 0, plan->totsize4 * sizeof(_fftw_complex));
  396. plan->plans[workerid].plan2_cpu = _FFTW(plan_many_dft)(plan->dim,
  397. plan->n1, n3*m3,
  398. /* input */ plan->plans[workerid].in2, NULL, 1, plan->totsize1,
  399. /* output */ plan->plans[workerid].out2, NULL, 1, plan->totsize1,
  400. sign, _FFTW_FLAGS);
  401. STARPU_ASSERT(plan->plans[workerid].plan2_cpu);
  402. #else
  403. #warning libstarpufft can not work correctly if libfftw3 is not installed
  404. #endif
  405. break;
  406. case STARPU_CUDA_WORKER:
  407. #ifdef STARPU_USE_CUDA
  408. plan->plans[workerid].initialized1 = 0;
  409. plan->plans[workerid].initialized2 = 0;
  410. #endif
  411. break;
  412. default:
  413. STARPU_ABORT();
  414. break;
  415. }
  416. }
  417. plan->twisted1 = STARPUFFT(malloc)(plan->totsize * sizeof(*plan->twisted1));
  418. memset(plan->twisted1, 0, plan->totsize * sizeof(*plan->twisted1));
  419. plan->fft1 = STARPUFFT(malloc)(plan->totsize * sizeof(*plan->fft1));
  420. memset(plan->fft1, 0, plan->totsize * sizeof(*plan->fft1));
  421. plan->twisted2 = STARPUFFT(malloc)(plan->totsize * sizeof(*plan->twisted2));
  422. memset(plan->twisted2, 0, plan->totsize * sizeof(*plan->twisted2));
  423. plan->fft2 = STARPUFFT(malloc)(plan->totsize * sizeof(*plan->fft2));
  424. memset(plan->fft2, 0, plan->totsize * sizeof(*plan->fft2));
  425. plan->twisted1_handle = malloc(plan->totsize1 * sizeof(*plan->twisted1_handle));
  426. plan->fft1_handle = malloc(plan->totsize1 * sizeof(*plan->fft1_handle));
  427. plan->twisted2_handle = malloc(plan->totsize3 * sizeof(*plan->twisted2_handle));
  428. plan->fft2_handle = malloc(plan->totsize3 * sizeof(*plan->fft2_handle));
  429. plan->twist1_tasks = malloc(plan->totsize1 * sizeof(*plan->twist1_tasks));
  430. plan->fft1_tasks = malloc(plan->totsize1 * sizeof(*plan->fft1_tasks));
  431. plan->twist2_tasks = malloc(plan->totsize3 * sizeof(*plan->twist2_tasks));
  432. plan->fft2_tasks = malloc(plan->totsize3 * sizeof(*plan->fft2_tasks));
  433. plan->twist3_tasks = malloc(plan->totsize3 * sizeof(*plan->twist3_tasks));
  434. plan->fft1_args = malloc(plan->totsize1 * sizeof(*plan->fft1_args));
  435. plan->fft2_args = malloc(plan->totsize3 * sizeof(*plan->fft2_args));
  436. /* Create first-round tasks */
  437. for (z = 0; z < plan->totsize1; z++) {
  438. int i = z / m1, j = z % m1;
  439. #define STEP_TAG(step) STEP_TAG_2D(plan, step, i, j)
  440. plan->fft1_args[z].plan = plan;
  441. plan->fft1_args[z].i = i;
  442. plan->fft1_args[z].j = j;
  443. /* Register (n2,m2) chunks */
  444. starpu_vector_data_register(&plan->twisted1_handle[z], 0, (uintptr_t) &plan->twisted1[z*plan->totsize2], plan->totsize2, sizeof(*plan->twisted1));
  445. starpu_vector_data_register(&plan->fft1_handle[z], 0, (uintptr_t) &plan->fft1[z*plan->totsize2], plan->totsize2, sizeof(*plan->fft1));
  446. /* We'll need it on the CPU for the second twist anyway */
  447. starpu_data_set_wt_mask(plan->fft1_handle[z], 1<<0);
  448. /* Create twist1 task */
  449. plan->twist1_tasks[z] = task = starpu_task_create();
  450. task->cl = &STARPUFFT(twist1_2d_codelet);
  451. /* task->buffers[0].handle = to be filled at execution */
  452. task->buffers[0].mode = STARPU_R;
  453. task->buffers[1].handle = plan->twisted1_handle[z];
  454. task->buffers[1].mode = STARPU_W;
  455. task->cl_arg = &plan->fft1_args[z];
  456. task->tag_id = STEP_TAG(TWIST1);
  457. task->use_tag = 1;
  458. task->detach = 1;
  459. task->destroy = 0;
  460. /* Tell that fft1 depends on twisted1 */
  461. starpu_tag_declare_deps(STEP_TAG(FFT1),
  462. 1, STEP_TAG(TWIST1));
  463. /* Create FFT1 task */
  464. plan->fft1_tasks[z] = task = starpu_task_create();
  465. task->cl = &STARPUFFT(fft1_2d_codelet);
  466. task->buffers[0].handle = plan->twisted1_handle[z];
  467. task->buffers[0].mode = STARPU_R;
  468. task->buffers[1].handle = plan->fft1_handle[z];
  469. task->buffers[1].mode = STARPU_W;
  470. task->buffers[2].handle = plan->roots_handle[0];
  471. task->buffers[2].mode = STARPU_R;
  472. task->buffers[3].handle = plan->roots_handle[1];
  473. task->buffers[3].mode = STARPU_R;
  474. task->cl_arg = &plan->fft1_args[z];
  475. task->tag_id = STEP_TAG(FFT1);
  476. task->use_tag = 1;
  477. task->detach = 1;
  478. task->destroy = 0;
  479. /* Tell that to be done with first step we need to have
  480. * finished this fft1 */
  481. starpu_tag_declare_deps(STEP_TAG_2D(plan, JOIN, 0, 0),
  482. 1, STEP_TAG(FFT1));
  483. #undef STEP_TAG
  484. }
  485. /* Create join task */
  486. plan->join_task = task = starpu_task_create();
  487. task->cl = NULL;
  488. task->tag_id = STEP_TAG_2D(plan, JOIN, 0, 0);
  489. task->use_tag = 1;
  490. task->detach = 1;
  491. task->destroy = 0;
  492. /* Create second-round tasks */
  493. for (z = 0; z < plan->totsize3; z++) {
  494. int kk = z / DIV_2D_M, ll = z % DIV_2D_M;
  495. #define STEP_TAG(step) STEP_TAG_2D(plan, step, kk, ll)
  496. plan->fft2_args[z].plan = plan;
  497. plan->fft2_args[z].kk = kk;
  498. plan->fft2_args[z].ll = ll;
  499. /* Register n3*m3 (n1,m1) chunks */
  500. starpu_vector_data_register(&plan->twisted2_handle[z], 0, (uintptr_t) &plan->twisted2[z*plan->totsize4], plan->totsize4, sizeof(*plan->twisted2));
  501. starpu_vector_data_register(&plan->fft2_handle[z], 0, (uintptr_t) &plan->fft2[z*plan->totsize4], plan->totsize4, sizeof(*plan->fft2));
  502. /* We'll need it on the CPU for the last twist anyway */
  503. starpu_data_set_wt_mask(plan->fft2_handle[z], 1<<0);
  504. /* Tell that twisted2 depends on the whole first step to be
  505. * done */
  506. starpu_tag_declare_deps(STEP_TAG(TWIST2),
  507. 1, STEP_TAG_2D(plan, JOIN, 0, 0));
  508. /* Create twist2 task */
  509. plan->twist2_tasks[z] = task = starpu_task_create();
  510. task->cl = &STARPUFFT(twist2_2d_codelet);
  511. task->buffers[0].handle = plan->twisted2_handle[z];
  512. task->buffers[0].mode = STARPU_W;
  513. task->cl_arg = &plan->fft2_args[z];
  514. task->tag_id = STEP_TAG(TWIST2);
  515. task->use_tag = 1;
  516. task->detach = 1;
  517. task->destroy = 0;
  518. /* Tell that fft2 depends on twisted2 */
  519. starpu_tag_declare_deps(STEP_TAG(FFT2),
  520. 1, STEP_TAG(TWIST2));
  521. /* Create FFT2 task */
  522. plan->fft2_tasks[z] = task = starpu_task_create();
  523. task->cl = &STARPUFFT(fft2_2d_codelet);
  524. task->buffers[0].handle = plan->twisted2_handle[z];
  525. task->buffers[0].mode = STARPU_R;
  526. task->buffers[1].handle = plan->fft2_handle[z];
  527. task->buffers[1].mode = STARPU_W;
  528. task->cl_arg = &plan->fft2_args[z];
  529. task->tag_id = STEP_TAG(FFT2);
  530. task->use_tag = 1;
  531. task->detach = 1;
  532. task->destroy = 0;
  533. /* Tell that twist3 depends on fft2 */
  534. starpu_tag_declare_deps(STEP_TAG(TWIST3),
  535. 1, STEP_TAG(FFT2));
  536. /* Create twist3 tasks */
  537. plan->twist3_tasks[z] = task = starpu_task_create();
  538. task->cl = &STARPUFFT(twist3_2d_codelet);
  539. task->buffers[0].handle = plan->fft2_handle[z];
  540. task->buffers[0].mode = STARPU_R;
  541. task->cl_arg = &plan->fft2_args[z];
  542. task->tag_id = STEP_TAG(TWIST3);
  543. task->use_tag = 1;
  544. task->detach = 1;
  545. task->destroy = 0;
  546. /* Tell that to be completely finished we need to have finished this twisted3 */
  547. starpu_tag_declare_deps(STEP_TAG_2D(plan, END, 0, 0),
  548. 1, STEP_TAG(TWIST3));
  549. #undef STEP_TAG
  550. }
  551. /* Create end task */
  552. plan->end_task = task = starpu_task_create();
  553. task->cl = NULL;
  554. task->tag_id = STEP_TAG_2D(plan, END, 0, 0);
  555. task->use_tag = 1;
  556. task->detach = 1;
  557. task->destroy = 0;
  558. return plan;
  559. }
  560. static starpu_tag_t
  561. STARPUFFT(start2dC2C)(STARPUFFT(plan) plan)
  562. {
  563. STARPU_ASSERT(plan->type == C2C);
  564. int z;
  565. for (z=0; z < plan->totsize1; z++) {
  566. starpu_task_submit(plan->twist1_tasks[z]);
  567. starpu_task_submit(plan->fft1_tasks[z]);
  568. }
  569. starpu_task_submit(plan->join_task);
  570. for (z=0; z < plan->totsize3; z++) {
  571. starpu_task_submit(plan->twist2_tasks[z]);
  572. starpu_task_submit(plan->fft2_tasks[z]);
  573. starpu_task_submit(plan->twist3_tasks[z]);
  574. }
  575. starpu_task_submit(plan->end_task);
  576. return STEP_TAG_2D(plan, END, 0, 0);
  577. }
  578. static void
  579. STARPUFFT(free_2d_tags)(STARPUFFT(plan) plan)
  580. {
  581. unsigned i, j;
  582. int n1 = plan->n1[0];
  583. int m1 = plan->n1[1];
  584. for (i = 0; i < n1; i++) {
  585. for (j = 0; j < m1; j++) {
  586. starpu_tag_remove(STEP_TAG_2D(plan, TWIST1, i, j));
  587. starpu_tag_remove(STEP_TAG_2D(plan, FFT1, i, j));
  588. }
  589. }
  590. starpu_tag_remove(STEP_TAG_2D(plan, JOIN, 0, 0));
  591. for (i = 0; i < DIV_2D_N; i++) {
  592. for (j = 0; j < DIV_2D_M; j++) {
  593. starpu_tag_remove(STEP_TAG_2D(plan, TWIST2, i, j));
  594. starpu_tag_remove(STEP_TAG_2D(plan, FFT2, i, j));
  595. starpu_tag_remove(STEP_TAG_2D(plan, TWIST3, i, j));
  596. }
  597. }
  598. starpu_tag_remove(STEP_TAG_2D(plan, END, 0, 0));
  599. }