starpufftx1d.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 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. /*
  18. *
  19. * Dumb parallel version
  20. *
  21. */
  22. #define DIV_1D 64
  23. /*
  24. * Overall strategy for an fft of size n:
  25. * - perform n1 ffts of size n2
  26. * - twiddle
  27. * - perform n2 ffts of size n1
  28. *
  29. * - n1 defaults to DIV_1D, thus n2 defaults to n / DIV_1D.
  30. *
  31. * Precise tasks:
  32. *
  33. * - twist1: twist the whole n-element input (called "in") into n1 chunks of
  34. * size n2, by using n1 tasks taking the whole n-element input as a
  35. * R parameter and one n2 output as a W parameter. The result is
  36. * called twisted1.
  37. * - fft1: perform n1 (n2) ffts, by using n1 tasks doing one fft each. Also
  38. * twiddle the result to prepare for the fft2. The result is called
  39. * fft1.
  40. * - join: depends on all the fft1s, to gather the n1 results of size n2 in
  41. * the fft1 vector.
  42. * - twist2: twist the fft1 vector into n2 chunks of size n1, called twisted2.
  43. * since n2 is typically very large, this step is divided in DIV_1D
  44. * tasks, each of them performing n2/DIV_1D of them
  45. * - fft2: perform n2 ffts of size n1. This is divided in DIV_1D tasks of
  46. * n2/DIV_1D ffts, to be performed in batches. The result is called
  47. * fft2.
  48. * - twist3: twist back the result of the fft2s above into the output buffer.
  49. * Only implemented on CPUs for simplicity of the gathering.
  50. *
  51. * The tag space thus uses 3 dimensions:
  52. * - the number of the plan.
  53. * - the step (TWIST1, FFT1, JOIN, TWIST2, FFT2, TWIST3, END)
  54. * - an index i between 0 and DIV_1D-1.
  55. */
  56. #define STEP_TAG_1D(plan, step, i) _STEP_TAG(plan, step, i)
  57. #ifdef STARPU_USE_CUDA
  58. /* twist1:
  59. *
  60. * Twist the full input vector (first parameter) into one chunk of size n2
  61. * (second parameter) */
  62. static void
  63. STARPUFFT(twist1_1d_kernel_gpu)(void *descr[], void *_args)
  64. {
  65. struct STARPUFFT(args) *args = _args;
  66. STARPUFFT(plan) plan = args->plan;
  67. int i = args->i;
  68. int n1 = plan->n1[0];
  69. int n2 = plan->n2[0];
  70. _cufftComplex * restrict in = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[0]);
  71. _cufftComplex * restrict twisted1 = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[1]);
  72. STARPUFFT(cuda_twist1_1d_host)(in, twisted1, i, n1, n2);
  73. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  74. }
  75. /* fft1:
  76. *
  77. * Perform one fft of size n2 */
  78. static void
  79. STARPUFFT(fft1_1d_plan_gpu)(void *args)
  80. {
  81. STARPUFFT(plan) plan = args;
  82. int n2 = plan->n2[0];
  83. int workerid = starpu_worker_get_id();
  84. cufftResult cures;
  85. cures = cufftPlan1d(&plan->plans[workerid].plan1_cuda, n2, _CUFFT_C2C, 1);
  86. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  87. cufftSetStream(plan->plans[workerid].plan1_cuda, starpu_cuda_get_local_stream());
  88. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  89. }
  90. static void
  91. STARPUFFT(fft1_1d_kernel_gpu)(void *descr[], void *_args)
  92. {
  93. struct STARPUFFT(args) *args = _args;
  94. STARPUFFT(plan) plan = args->plan;
  95. int i = args->i;
  96. int n2 = plan->n2[0];
  97. cufftResult cures;
  98. _cufftComplex * restrict in = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[0]);
  99. _cufftComplex * restrict out = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[1]);
  100. const _cufftComplex * restrict roots = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[2]);
  101. int workerid = starpu_worker_get_id();
  102. task_per_worker[workerid]++;
  103. cures = _cufftExecC2C(plan->plans[workerid].plan1_cuda, in, out, plan->sign == -1 ? CUFFT_FORWARD : CUFFT_INVERSE);
  104. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  105. STARPUFFT(cuda_twiddle_1d_host)(out, roots, n2, i);
  106. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  107. }
  108. /* fft2:
  109. *
  110. * Perform n3 = n2/DIV_1D ffts of size n1 */
  111. static void
  112. STARPUFFT(fft2_1d_plan_gpu)(void *args)
  113. {
  114. STARPUFFT(plan) plan = args;
  115. int n1 = plan->n1[0];
  116. int n2 = plan->n2[0];
  117. int n3 = n2/DIV_1D;
  118. cufftResult cures;
  119. int workerid = starpu_worker_get_id();
  120. cures = cufftPlan1d(&plan->plans[workerid].plan2_cuda, n1, _CUFFT_C2C, n3);
  121. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  122. cufftSetStream(plan->plans[workerid].plan2_cuda, starpu_cuda_get_local_stream());
  123. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  124. }
  125. static void
  126. STARPUFFT(fft2_1d_kernel_gpu)(void *descr[], void *_args)
  127. {
  128. struct STARPUFFT(args) *args = _args;
  129. STARPUFFT(plan) plan = args->plan;
  130. cufftResult cures;
  131. _cufftComplex * restrict in = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[0]);
  132. _cufftComplex * restrict out = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[1]);
  133. int workerid = starpu_worker_get_id();
  134. task_per_worker[workerid]++;
  135. /* NOTE using batch support */
  136. cures = _cufftExecC2C(plan->plans[workerid].plan2_cuda, in, out, plan->sign == -1 ? CUFFT_FORWARD : CUFFT_INVERSE);
  137. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  138. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  139. }
  140. #endif
  141. /* twist1:
  142. *
  143. * Twist the full input vector (first parameter) into one chunk of size n2
  144. * (second parameter) */
  145. static void
  146. STARPUFFT(twist1_1d_kernel_cpu)(void *descr[], void *_args)
  147. {
  148. struct STARPUFFT(args) *args = _args;
  149. STARPUFFT(plan) plan = args->plan;
  150. int i = args->i;
  151. int j;
  152. int n1 = plan->n1[0];
  153. int n2 = plan->n2[0];
  154. STARPUFFT(complex) * restrict in = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[0]);
  155. STARPUFFT(complex) * restrict twisted1 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[1]);
  156. /* printf("twist1 %d %g\n", i, (double) cabs(plan->in[i])); */
  157. for (j = 0; j < n2; j++)
  158. twisted1[j] = in[i+j*n1];
  159. }
  160. #ifdef STARPU_HAVE_FFTW
  161. /* fft1:
  162. *
  163. * Perform one fft of size n2 */
  164. static void
  165. STARPUFFT(fft1_1d_kernel_cpu)(void *descr[], void *_args)
  166. {
  167. struct STARPUFFT(args) *args = _args;
  168. STARPUFFT(plan) plan = args->plan;
  169. int i = args->i;
  170. int j;
  171. int n2 = plan->n2[0];
  172. int workerid = starpu_worker_get_id();
  173. task_per_worker[workerid]++;
  174. STARPUFFT(complex) * restrict twisted1 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[0]);
  175. STARPUFFT(complex) * restrict fft1 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[1]);
  176. /* printf("fft1 %d %g\n", i, (double) cabs(twisted1[0])); */
  177. _FFTW(execute_dft)(plan->plans[workerid].plan1_cpu, twisted1, fft1);
  178. /* twiddle fft1 buffer */
  179. for (j = 0; j < n2; j++)
  180. fft1[j] = fft1[j] * plan->roots[0][i*j];
  181. }
  182. #endif
  183. /* twist2:
  184. *
  185. * Twist the full vector (results of the fft1s) into one package of n2/DIV_1D
  186. * chunks of size n1 */
  187. static void
  188. STARPUFFT(twist2_1d_kernel_cpu)(void *descr[], void *_args)
  189. {
  190. struct STARPUFFT(args) *args = _args;
  191. STARPUFFT(plan) plan = args->plan;
  192. int jj = args->jj; /* between 0 and DIV_1D */
  193. int jjj; /* beetween 0 and n3 */
  194. int i;
  195. int n1 = plan->n1[0];
  196. int n2 = plan->n2[0];
  197. int n3 = n2/DIV_1D;
  198. STARPUFFT(complex) * restrict twisted2 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[0]);
  199. /* printf("twist2 %d %g\n", jj, (double) cabs(plan->fft1[jj])); */
  200. for (jjj = 0; jjj < n3; jjj++) {
  201. int j = jj * n3 + jjj;
  202. for (i = 0; i < n1; i++)
  203. twisted2[jjj*n1+i] = plan->fft1[i*n2+j];
  204. }
  205. }
  206. #ifdef STARPU_HAVE_FFTW
  207. /* fft2:
  208. *
  209. * Perform n3 = n2/DIV_1D ffts of size n1 */
  210. static void
  211. STARPUFFT(fft2_1d_kernel_cpu)(void *descr[], void *_args)
  212. {
  213. struct STARPUFFT(args) *args = _args;
  214. STARPUFFT(plan) plan = args->plan;
  215. /* int jj = args->jj; */
  216. int workerid = starpu_worker_get_id();
  217. task_per_worker[workerid]++;
  218. STARPUFFT(complex) * restrict twisted2 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[0]);
  219. STARPUFFT(complex) * restrict fft2 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[1]);
  220. /* printf("fft2 %d %g\n", jj, (double) cabs(twisted2[plan->totsize4-1])); */
  221. _FFTW(execute_dft)(plan->plans[workerid].plan2_cpu, twisted2, fft2);
  222. }
  223. #endif
  224. /* twist3:
  225. *
  226. * Spread the package of n2/DIV_1D chunks of size n1 into the output vector */
  227. static void
  228. STARPUFFT(twist3_1d_kernel_cpu)(void *descr[], void *_args)
  229. {
  230. struct STARPUFFT(args) *args = _args;
  231. STARPUFFT(plan) plan = args->plan;
  232. int jj = args->jj; /* between 0 and DIV_1D */
  233. int jjj; /* beetween 0 and n3 */
  234. int i;
  235. int n1 = plan->n1[0];
  236. int n2 = plan->n2[0];
  237. int n3 = n2/DIV_1D;
  238. const STARPUFFT(complex) * restrict fft2 = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[0]);
  239. /* printf("twist3 %d %g\n", jj, (double) cabs(fft2[0])); */
  240. for (jjj = 0; jjj < n3; jjj++) {
  241. int j = jj * n3 + jjj;
  242. for (i = 0; i < n1; i++)
  243. plan->out[i*n2+j] = fft2[jjj*n1+i];
  244. }
  245. }
  246. /* Performance models for the 5 kinds of tasks */
  247. static struct starpu_perfmodel STARPUFFT(twist1_1d_model) = {
  248. .type = STARPU_HISTORY_BASED,
  249. .symbol = TYPE"twist1_1d"
  250. };
  251. static struct starpu_perfmodel STARPUFFT(fft1_1d_model) = {
  252. .type = STARPU_HISTORY_BASED,
  253. .symbol = TYPE"fft1_1d"
  254. };
  255. static struct starpu_perfmodel STARPUFFT(twist2_1d_model) = {
  256. .type = STARPU_HISTORY_BASED,
  257. .symbol = TYPE"twist2_1d"
  258. };
  259. static struct starpu_perfmodel STARPUFFT(fft2_1d_model) = {
  260. .type = STARPU_HISTORY_BASED,
  261. .symbol = TYPE"fft2_1d"
  262. };
  263. static struct starpu_perfmodel STARPUFFT(twist3_1d_model) = {
  264. .type = STARPU_HISTORY_BASED,
  265. .symbol = TYPE"twist3_1d"
  266. };
  267. /* codelet pointers for the 5 kinds of tasks */
  268. static struct starpu_codelet STARPUFFT(twist1_1d_codelet) = {
  269. .where =
  270. #ifdef STARPU_USE_CUDA
  271. STARPU_CUDA|
  272. #endif
  273. STARPU_CPU,
  274. #ifdef STARPU_USE_CUDA
  275. .cuda_funcs = {STARPUFFT(twist1_1d_kernel_gpu), NULL},
  276. #endif
  277. .cpu_funcs = {STARPUFFT(twist1_1d_kernel_cpu), NULL},
  278. .model = &STARPUFFT(twist1_1d_model),
  279. .nbuffers = 2,
  280. .modes = {STARPU_R, STARPU_W}
  281. };
  282. static struct starpu_codelet STARPUFFT(fft1_1d_codelet) = {
  283. .where =
  284. #ifdef STARPU_USE_CUDA
  285. STARPU_CUDA|
  286. #endif
  287. #ifdef STARPU_HAVE_FFTW
  288. STARPU_CPU|
  289. #endif
  290. 0,
  291. #ifdef STARPU_USE_CUDA
  292. .cuda_funcs = {STARPUFFT(fft1_1d_kernel_gpu), NULL},
  293. #endif
  294. #ifdef STARPU_HAVE_FFTW
  295. .cpu_funcs = {STARPUFFT(fft1_1d_kernel_cpu), NULL},
  296. #endif
  297. .model = &STARPUFFT(fft1_1d_model),
  298. .nbuffers = 3,
  299. .modes = {STARPU_R, STARPU_W, STARPU_R}
  300. };
  301. static struct starpu_codelet STARPUFFT(twist2_1d_codelet) = {
  302. .where = STARPU_CPU,
  303. .cpu_funcs = {STARPUFFT(twist2_1d_kernel_cpu), NULL},
  304. .model = &STARPUFFT(twist2_1d_model),
  305. .nbuffers = 1,
  306. .modes = {STARPU_W}
  307. };
  308. static struct starpu_codelet STARPUFFT(fft2_1d_codelet) = {
  309. .where =
  310. #ifdef STARPU_USE_CUDA
  311. STARPU_CUDA|
  312. #endif
  313. #ifdef STARPU_HAVE_FFTW
  314. STARPU_CPU|
  315. #endif
  316. 0,
  317. #ifdef STARPU_USE_CUDA
  318. .cuda_funcs = {STARPUFFT(fft2_1d_kernel_gpu), NULL},
  319. #endif
  320. #ifdef STARPU_HAVE_FFTW
  321. .cpu_funcs = {STARPUFFT(fft2_1d_kernel_cpu), NULL},
  322. #endif
  323. .model = &STARPUFFT(fft2_1d_model),
  324. .nbuffers = 2,
  325. .modes = {STARPU_R, STARPU_W}
  326. };
  327. static struct starpu_codelet STARPUFFT(twist3_1d_codelet) = {
  328. .where = STARPU_CPU,
  329. .cpu_funcs = {STARPUFFT(twist3_1d_kernel_cpu), NULL},
  330. .model = &STARPUFFT(twist3_1d_model),
  331. .nbuffers = 1,
  332. .modes = {STARPU_R}
  333. };
  334. /*
  335. *
  336. * Sequential version
  337. *
  338. */
  339. #ifdef STARPU_USE_CUDA
  340. /* Perform one fft of size n */
  341. static void
  342. STARPUFFT(fft_1d_plan_gpu)(void *args)
  343. {
  344. STARPUFFT(plan) plan = args;
  345. cufftResult cures;
  346. int n = plan->n[0];
  347. int workerid = starpu_worker_get_id();
  348. cures = cufftPlan1d(&plan->plans[workerid].plan_cuda, n, _CUFFT_C2C, 1);
  349. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  350. cufftSetStream(plan->plans[workerid].plan_cuda, starpu_cuda_get_local_stream());
  351. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  352. }
  353. static void
  354. STARPUFFT(fft_1d_kernel_gpu)(void *descr[], void *args)
  355. {
  356. STARPUFFT(plan) plan = args;
  357. cufftResult cures;
  358. _cufftComplex * restrict in = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[0]);
  359. _cufftComplex * restrict out = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[1]);
  360. int workerid = starpu_worker_get_id();
  361. task_per_worker[workerid]++;
  362. cures = _cufftExecC2C(plan->plans[workerid].plan_cuda, in, out, plan->sign == -1 ? CUFFT_FORWARD : CUFFT_INVERSE);
  363. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  364. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  365. }
  366. #endif
  367. #ifdef STARPU_HAVE_FFTW
  368. /* Perform one fft of size n */
  369. static void
  370. STARPUFFT(fft_1d_kernel_cpu)(void *descr[], void *_args)
  371. {
  372. STARPUFFT(plan) plan = _args;
  373. int workerid = starpu_worker_get_id();
  374. task_per_worker[workerid]++;
  375. STARPUFFT(complex) * restrict in = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[0]);
  376. STARPUFFT(complex) * restrict out = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[1]);
  377. _FFTW(execute_dft)(plan->plans[workerid].plan_cpu, in, out);
  378. }
  379. #endif
  380. static struct starpu_perfmodel STARPUFFT(fft_1d_model) = {
  381. .type = STARPU_HISTORY_BASED,
  382. .symbol = TYPE"fft_1d"
  383. };
  384. static struct starpu_codelet STARPUFFT(fft_1d_codelet) = {
  385. .where =
  386. #ifdef STARPU_USE_CUDA
  387. STARPU_CUDA|
  388. #endif
  389. #ifdef STARPU_HAVE_FFTW
  390. STARPU_CPU|
  391. #endif
  392. 0,
  393. #ifdef STARPU_USE_CUDA
  394. .cuda_funcs = {STARPUFFT(fft_1d_kernel_gpu), NULL},
  395. #endif
  396. #ifdef STARPU_HAVE_FFTW
  397. .cpu_funcs = {STARPUFFT(fft_1d_kernel_cpu), NULL},
  398. #endif
  399. .model = &STARPUFFT(fft_1d_model),
  400. .nbuffers = 2,
  401. .modes = {STARPU_R, STARPU_W}
  402. };
  403. /* Planning:
  404. *
  405. * - For each CPU worker, we need to plan the two fftw stages.
  406. * - For GPU workers, we need to do the planning in the CUDA context, so we do
  407. * this lazily through the initialised1 and initialised2 flags ; TODO: use
  408. * starpu_execute_on_each_worker instead (done in the omp branch).
  409. * - We allocate all the temporary buffers and register them to starpu.
  410. * - We create all the tasks, but do not submit them yet. It will be possible
  411. * to reuse them at will to perform several ffts with the same planning.
  412. */
  413. STARPUFFT(plan)
  414. STARPUFFT(plan_dft_1d)(int n, int sign, unsigned flags)
  415. {
  416. int workerid;
  417. int n1 = DIV_1D;
  418. int n2 = n / n1;
  419. int n3;
  420. int z;
  421. struct starpu_task *task;
  422. if (PARALLEL) {
  423. #ifdef STARPU_USE_CUDA
  424. /* cufft 1D limited to 8M elements */
  425. while (n2 > 8 << 20) {
  426. n1 *= 2;
  427. n2 /= 2;
  428. }
  429. #endif
  430. STARPU_ASSERT(n == n1*n2);
  431. STARPU_ASSERT(n1 < (1ULL << I_BITS));
  432. /* distribute the n2 second ffts into DIV_1D packages */
  433. n3 = n2 / DIV_1D;
  434. STARPU_ASSERT(n2 == n3*DIV_1D);
  435. }
  436. /* TODO: flags? Automatically set FFTW_MEASURE on calibration? */
  437. STARPU_ASSERT(flags == 0);
  438. STARPUFFT(plan) plan = malloc(sizeof(*plan));
  439. memset(plan, 0, sizeof(*plan));
  440. if (PARALLEL) {
  441. plan->number = STARPU_ATOMIC_ADD(&starpufft_last_plan_number, 1) - 1;
  442. /* The plan number has a limited size */
  443. STARPU_ASSERT(plan->number < (1ULL << NUMBER_BITS));
  444. }
  445. /* Just one dimension */
  446. plan->dim = 1;
  447. plan->n = malloc(plan->dim * sizeof(*plan->n));
  448. plan->n[0] = n;
  449. if (PARALLEL) {
  450. check_dims(plan);
  451. plan->n1 = malloc(plan->dim * sizeof(*plan->n1));
  452. plan->n1[0] = n1;
  453. plan->n2 = malloc(plan->dim * sizeof(*plan->n2));
  454. plan->n2[0] = n2;
  455. }
  456. /* Note: this is for coherency with the 2D case */
  457. plan->totsize = n;
  458. if (PARALLEL) {
  459. plan->totsize1 = n1;
  460. plan->totsize2 = n2;
  461. plan->totsize3 = DIV_1D;
  462. plan->totsize4 = plan->totsize / plan->totsize3;
  463. }
  464. plan->type = C2C;
  465. plan->sign = sign;
  466. if (PARALLEL) {
  467. /* Compute the w^k just once. */
  468. compute_roots(plan);
  469. }
  470. /* Initialize per-worker working set */
  471. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++) {
  472. switch (starpu_worker_get_type(workerid)) {
  473. case STARPU_CPU_WORKER:
  474. #ifdef STARPU_HAVE_FFTW
  475. if (PARALLEL) {
  476. /* first fft plan: one fft of size n2.
  477. * FFTW imposes that buffer pointers are known at
  478. * planning time. */
  479. plan->plans[workerid].plan1_cpu = _FFTW(plan_dft_1d)(n2, NULL, (void*) 1, sign, _FFTW_FLAGS);
  480. STARPU_ASSERT(plan->plans[workerid].plan1_cpu);
  481. /* second fft plan: n3 ffts of size n1 */
  482. plan->plans[workerid].plan2_cpu = _FFTW(plan_many_dft)(plan->dim,
  483. plan->n1, n3,
  484. NULL, NULL, 1, plan->totsize1,
  485. (void*) 1, NULL, 1, plan->totsize1,
  486. sign, _FFTW_FLAGS);
  487. STARPU_ASSERT(plan->plans[workerid].plan2_cpu);
  488. } else {
  489. /* fft plan: one fft of size n. */
  490. plan->plans[workerid].plan_cpu = _FFTW(plan_dft_1d)(n, NULL, (void*) 1, sign, _FFTW_FLAGS);
  491. STARPU_ASSERT(plan->plans[workerid].plan_cpu);
  492. }
  493. #else
  494. /* #warning libstarpufft can not work correctly if libfftw3 is not installed */
  495. #endif
  496. break;
  497. case STARPU_CUDA_WORKER:
  498. break;
  499. default:
  500. /* Do not care, we won't be executing anything there. */
  501. break;
  502. }
  503. }
  504. #ifdef STARPU_USE_CUDA
  505. if (PARALLEL) {
  506. starpu_execute_on_each_worker(STARPUFFT(fft1_1d_plan_gpu), plan, STARPU_CUDA);
  507. starpu_execute_on_each_worker(STARPUFFT(fft2_1d_plan_gpu), plan, STARPU_CUDA);
  508. } else {
  509. starpu_execute_on_each_worker(STARPUFFT(fft_1d_plan_gpu), plan, STARPU_CUDA);
  510. }
  511. #endif
  512. if (PARALLEL) {
  513. /* Allocate buffers. */
  514. plan->twisted1 = STARPUFFT(malloc)(plan->totsize * sizeof(*plan->twisted1));
  515. memset(plan->twisted1, 0, plan->totsize * sizeof(*plan->twisted1));
  516. plan->fft1 = STARPUFFT(malloc)(plan->totsize * sizeof(*plan->fft1));
  517. memset(plan->fft1, 0, plan->totsize * sizeof(*plan->fft1));
  518. plan->twisted2 = STARPUFFT(malloc)(plan->totsize * sizeof(*plan->twisted2));
  519. memset(plan->twisted2, 0, plan->totsize * sizeof(*plan->twisted2));
  520. plan->fft2 = STARPUFFT(malloc)(plan->totsize * sizeof(*plan->fft2));
  521. memset(plan->fft2, 0, plan->totsize * sizeof(*plan->fft2));
  522. /* Allocate handle arrays */
  523. plan->twisted1_handle = malloc(plan->totsize1 * sizeof(*plan->twisted1_handle));
  524. plan->fft1_handle = malloc(plan->totsize1 * sizeof(*plan->fft1_handle));
  525. plan->twisted2_handle = malloc(plan->totsize3 * sizeof(*plan->twisted2_handle));
  526. plan->fft2_handle = malloc(plan->totsize3 * sizeof(*plan->fft2_handle));
  527. /* Allocate task arrays */
  528. plan->twist1_tasks = malloc(plan->totsize1 * sizeof(*plan->twist1_tasks));
  529. plan->fft1_tasks = malloc(plan->totsize1 * sizeof(*plan->fft1_tasks));
  530. plan->twist2_tasks = malloc(plan->totsize3 * sizeof(*plan->twist2_tasks));
  531. plan->fft2_tasks = malloc(plan->totsize3 * sizeof(*plan->fft2_tasks));
  532. plan->twist3_tasks = malloc(plan->totsize3 * sizeof(*plan->twist3_tasks));
  533. /* Allocate codelet argument arrays */
  534. plan->fft1_args = malloc(plan->totsize1 * sizeof(*plan->fft1_args));
  535. plan->fft2_args = malloc(plan->totsize3 * sizeof(*plan->fft2_args));
  536. /* Create first-round tasks: DIV_1D tasks of type twist1 and fft1 */
  537. for (z = 0; z < plan->totsize1; z++) {
  538. int i = z;
  539. #define STEP_TAG(step) STEP_TAG_1D(plan, step, i)
  540. /* TODO: get rid of tags */
  541. plan->fft1_args[z].plan = plan;
  542. plan->fft1_args[z].i = i;
  543. /* Register the twisted1 buffer of size n2. */
  544. starpu_vector_data_register(&plan->twisted1_handle[z], 0, (uintptr_t) &plan->twisted1[z*plan->totsize2], plan->totsize2, sizeof(*plan->twisted1));
  545. /* Register the fft1 buffer of size n2. */
  546. starpu_vector_data_register(&plan->fft1_handle[z], 0, (uintptr_t) &plan->fft1[z*plan->totsize2], plan->totsize2, sizeof(*plan->fft1));
  547. /* We'll need the result of fft1 on the CPU for the second
  548. * twist anyway, so tell starpu to not keep the fft1 buffer in
  549. * the GPU. */
  550. starpu_data_set_wt_mask(plan->fft1_handle[z], 1<<0);
  551. /* Create twist1 task */
  552. plan->twist1_tasks[z] = task = starpu_task_create();
  553. task->cl = &STARPUFFT(twist1_1d_codelet);
  554. /* task->handles[0] = to be filled at execution to point
  555. to the application input. */
  556. task->handles[1] = plan->twisted1_handle[z];
  557. task->cl_arg = &plan->fft1_args[z];
  558. task->tag_id = STEP_TAG(TWIST1);
  559. task->use_tag = 1;
  560. task->destroy = 0;
  561. /* Tell that fft1 depends on twisted1 */
  562. starpu_tag_declare_deps(STEP_TAG(FFT1),
  563. 1, STEP_TAG(TWIST1));
  564. /* Create FFT1 task */
  565. plan->fft1_tasks[z] = task = starpu_task_create();
  566. task->cl = &STARPUFFT(fft1_1d_codelet);
  567. task->handles[0] = plan->twisted1_handle[z];
  568. task->handles[1] = plan->fft1_handle[z];
  569. task->handles[2] = plan->roots_handle[0];
  570. task->cl_arg = &plan->fft1_args[z];
  571. task->tag_id = STEP_TAG(FFT1);
  572. task->use_tag = 1;
  573. task->destroy = 0;
  574. /* Tell that the join task will depend on the fft1 task. */
  575. starpu_tag_declare_deps(STEP_TAG_1D(plan, JOIN, 0),
  576. 1, STEP_TAG(FFT1));
  577. #undef STEP_TAG
  578. }
  579. /* Create the join task, only serving as a dependency point between
  580. * fft1 and twist2 tasks */
  581. plan->join_task = task = starpu_task_create();
  582. task->cl = NULL;
  583. task->tag_id = STEP_TAG_1D(plan, JOIN, 0);
  584. task->use_tag = 1;
  585. task->destroy = 0;
  586. /* Create second-round tasks: DIV_1D batches of n2/DIV_1D twist2, fft2,
  587. * and twist3 */
  588. for (z = 0; z < plan->totsize3; z++) {
  589. int jj = z;
  590. #define STEP_TAG(step) STEP_TAG_1D(plan, step, jj)
  591. plan->fft2_args[z].plan = plan;
  592. plan->fft2_args[z].jj = jj;
  593. /* Register n3 twisted2 buffers of size n1 */
  594. starpu_vector_data_register(&plan->twisted2_handle[z], 0, (uintptr_t) &plan->twisted2[z*plan->totsize4], plan->totsize4, sizeof(*plan->twisted2));
  595. starpu_vector_data_register(&plan->fft2_handle[z], 0, (uintptr_t) &plan->fft2[z*plan->totsize4], plan->totsize4, sizeof(*plan->fft2));
  596. /* We'll need the result of fft2 on the CPU for the third
  597. * twist anyway, so tell starpu to not keep the fft2 buffer in
  598. * the GPU. */
  599. starpu_data_set_wt_mask(plan->fft2_handle[z], 1<<0);
  600. /* Tell that twisted2 depends on the join task */
  601. starpu_tag_declare_deps(STEP_TAG(TWIST2),
  602. 1, STEP_TAG_1D(plan, JOIN, 0));
  603. /* Create twist2 task */
  604. plan->twist2_tasks[z] = task = starpu_task_create();
  605. task->cl = &STARPUFFT(twist2_1d_codelet);
  606. task->handles[0] = plan->twisted2_handle[z];
  607. task->cl_arg = &plan->fft2_args[z];
  608. task->tag_id = STEP_TAG(TWIST2);
  609. task->use_tag = 1;
  610. task->destroy = 0;
  611. /* Tell that fft2 depends on twisted2 */
  612. starpu_tag_declare_deps(STEP_TAG(FFT2),
  613. 1, STEP_TAG(TWIST2));
  614. /* Create FFT2 task */
  615. plan->fft2_tasks[z] = task = starpu_task_create();
  616. task->cl = &STARPUFFT(fft2_1d_codelet);
  617. task->handles[0] = plan->twisted2_handle[z];
  618. task->handles[1] = plan->fft2_handle[z];
  619. task->cl_arg = &plan->fft2_args[z];
  620. task->tag_id = STEP_TAG(FFT2);
  621. task->use_tag = 1;
  622. task->destroy = 0;
  623. /* Tell that twist3 depends on fft2 */
  624. starpu_tag_declare_deps(STEP_TAG(TWIST3),
  625. 1, STEP_TAG(FFT2));
  626. /* Create twist3 tasks */
  627. /* These run only on CPUs and thus write directly into the
  628. * application output buffer. */
  629. plan->twist3_tasks[z] = task = starpu_task_create();
  630. task->cl = &STARPUFFT(twist3_1d_codelet);
  631. task->handles[0] = plan->fft2_handle[z];
  632. task->cl_arg = &plan->fft2_args[z];
  633. task->tag_id = STEP_TAG(TWIST3);
  634. task->use_tag = 1;
  635. task->destroy = 0;
  636. /* Tell that to be completely finished we need to have finished
  637. * this twisted3 */
  638. starpu_tag_declare_deps(STEP_TAG_1D(plan, END, 0),
  639. 1, STEP_TAG(TWIST3));
  640. #undef STEP_TAG
  641. }
  642. /* Create end task, only serving as a join point. */
  643. plan->end_task = task = starpu_task_create();
  644. task->cl = NULL;
  645. task->tag_id = STEP_TAG_1D(plan, END, 0);
  646. task->use_tag = 1;
  647. task->destroy = 0;
  648. }
  649. return plan;
  650. }
  651. /* Actually submit all the tasks. */
  652. static struct starpu_task *
  653. STARPUFFT(start1dC2C)(STARPUFFT(plan) plan, starpu_data_handle_t in, starpu_data_handle_t out)
  654. {
  655. STARPU_ASSERT(plan->type == C2C);
  656. int z;
  657. int ret;
  658. if (PARALLEL) {
  659. for (z=0; z < plan->totsize1; z++) {
  660. ret = starpu_task_submit(plan->twist1_tasks[z]);
  661. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  662. ret = starpu_task_submit(plan->fft1_tasks[z]);
  663. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  664. }
  665. ret = starpu_task_submit(plan->join_task);
  666. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  667. for (z=0; z < plan->totsize3; z++) {
  668. ret = starpu_task_submit(plan->twist2_tasks[z]);
  669. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  670. ret = starpu_task_submit(plan->fft2_tasks[z]);
  671. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  672. ret = starpu_task_submit(plan->twist3_tasks[z]);
  673. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  674. }
  675. ret = starpu_task_submit(plan->end_task);
  676. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  677. return plan->end_task;
  678. } else /* !PARALLEL */ {
  679. struct starpu_task *task;
  680. /* Create FFT task */
  681. task = starpu_task_create();
  682. task->cl = &STARPUFFT(fft_1d_codelet);
  683. task->handles[0] = in;
  684. task->handles[1] = out;
  685. task->cl_arg = plan;
  686. ret = starpu_task_submit(task);
  687. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  688. return task;
  689. }
  690. }
  691. /* Free all the tags. The generic code handles freeing the buffers. */
  692. static void
  693. STARPUFFT(free_1d_tags)(STARPUFFT(plan) plan)
  694. {
  695. unsigned i;
  696. int n1 = plan->n1[0];
  697. if (!PARALLEL)
  698. return;
  699. for (i = 0; i < n1; i++) {
  700. starpu_tag_remove(STEP_TAG_1D(plan, TWIST1, i));
  701. starpu_tag_remove(STEP_TAG_1D(plan, FFT1, i));
  702. }
  703. starpu_tag_remove(STEP_TAG_1D(plan, JOIN, 0));
  704. for (i = 0; i < DIV_1D; i++) {
  705. starpu_tag_remove(STEP_TAG_1D(plan, TWIST2, i));
  706. starpu_tag_remove(STEP_TAG_1D(plan, FFT2, i));
  707. starpu_tag_remove(STEP_TAG_1D(plan, TWIST3, i));
  708. }
  709. starpu_tag_remove(STEP_TAG_1D(plan, END, 0));
  710. }