starpufftx2d.c 21 KB

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