starpufftx2d.c 21 KB

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