starpufftx2d.c 21 KB

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