starpufftx2d.c 21 KB

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