starpufftx1d.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 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. };
  281. static struct starpu_codelet STARPUFFT(fft1_1d_codelet) = {
  282. .where =
  283. #ifdef STARPU_USE_CUDA
  284. STARPU_CUDA|
  285. #endif
  286. #ifdef STARPU_HAVE_FFTW
  287. STARPU_CPU|
  288. #endif
  289. 0,
  290. #ifdef STARPU_USE_CUDA
  291. .cuda_funcs = {STARPUFFT(fft1_1d_kernel_gpu), NULL},
  292. #endif
  293. #ifdef STARPU_HAVE_FFTW
  294. .cpu_funcs = {STARPUFFT(fft1_1d_kernel_cpu), NULL},
  295. #endif
  296. .model = &STARPUFFT(fft1_1d_model),
  297. .nbuffers = 3
  298. };
  299. static struct starpu_codelet STARPUFFT(twist2_1d_codelet) = {
  300. .where = STARPU_CPU,
  301. .cpu_funcs = {STARPUFFT(twist2_1d_kernel_cpu), NULL},
  302. .model = &STARPUFFT(twist2_1d_model),
  303. .nbuffers = 1
  304. };
  305. static struct starpu_codelet STARPUFFT(fft2_1d_codelet) = {
  306. .where =
  307. #ifdef STARPU_USE_CUDA
  308. STARPU_CUDA|
  309. #endif
  310. #ifdef STARPU_HAVE_FFTW
  311. STARPU_CPU|
  312. #endif
  313. 0,
  314. #ifdef STARPU_USE_CUDA
  315. .cuda_funcs = {STARPUFFT(fft2_1d_kernel_gpu), NULL},
  316. #endif
  317. #ifdef STARPU_HAVE_FFTW
  318. .cpu_funcs = {STARPUFFT(fft2_1d_kernel_cpu), NULL},
  319. #endif
  320. .model = &STARPUFFT(fft2_1d_model),
  321. .nbuffers = 2
  322. };
  323. static struct starpu_codelet STARPUFFT(twist3_1d_codelet) = {
  324. .where = STARPU_CPU,
  325. .cpu_funcs = {STARPUFFT(twist3_1d_kernel_cpu), NULL},
  326. .model = &STARPUFFT(twist3_1d_model),
  327. .nbuffers = 1
  328. };
  329. /*
  330. *
  331. * Sequential version
  332. *
  333. */
  334. #ifdef STARPU_USE_CUDA
  335. /* Perform one fft of size n */
  336. static void
  337. STARPUFFT(fft_1d_plan_gpu)(void *args)
  338. {
  339. STARPUFFT(plan) plan = args;
  340. cufftResult cures;
  341. int n = plan->n[0];
  342. int workerid = starpu_worker_get_id();
  343. cures = cufftPlan1d(&plan->plans[workerid].plan_cuda, n, _CUFFT_C2C, 1);
  344. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  345. cufftSetStream(plan->plans[workerid].plan_cuda, starpu_cuda_get_local_stream());
  346. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  347. }
  348. static void
  349. STARPUFFT(fft_1d_kernel_gpu)(void *descr[], void *args)
  350. {
  351. STARPUFFT(plan) plan = args;
  352. cufftResult cures;
  353. _cufftComplex * restrict in = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[0]);
  354. _cufftComplex * restrict out = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[1]);
  355. int workerid = starpu_worker_get_id();
  356. task_per_worker[workerid]++;
  357. cures = _cufftExecC2C(plan->plans[workerid].plan_cuda, in, out, plan->sign == -1 ? CUFFT_FORWARD : CUFFT_INVERSE);
  358. STARPU_ASSERT(cures == CUFFT_SUCCESS);
  359. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  360. }
  361. #endif
  362. #ifdef STARPU_HAVE_FFTW
  363. /* Perform one fft of size n */
  364. static void
  365. STARPUFFT(fft_1d_kernel_cpu)(void *descr[], void *_args)
  366. {
  367. STARPUFFT(plan) plan = _args;
  368. int workerid = starpu_worker_get_id();
  369. task_per_worker[workerid]++;
  370. STARPUFFT(complex) * restrict in = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[0]);
  371. STARPUFFT(complex) * restrict out = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[1]);
  372. _FFTW(execute_dft)(plan->plans[workerid].plan_cpu, in, out);
  373. }
  374. #endif
  375. static struct starpu_perfmodel STARPUFFT(fft_1d_model) = {
  376. .type = STARPU_HISTORY_BASED,
  377. .symbol = TYPE"fft_1d"
  378. };
  379. static struct starpu_codelet STARPUFFT(fft_1d_codelet) = {
  380. .where =
  381. #ifdef STARPU_USE_CUDA
  382. STARPU_CUDA|
  383. #endif
  384. #ifdef STARPU_HAVE_FFTW
  385. STARPU_CPU|
  386. #endif
  387. 0,
  388. #ifdef STARPU_USE_CUDA
  389. .cuda_funcs = {STARPUFFT(fft_1d_kernel_gpu), NULL},
  390. #endif
  391. #ifdef STARPU_HAVE_FFTW
  392. .cpu_funcs = {STARPUFFT(fft_1d_kernel_cpu), NULL},
  393. #endif
  394. .model = &STARPUFFT(fft_1d_model),
  395. .nbuffers = 2
  396. };
  397. /* Planning:
  398. *
  399. * - For each CPU worker, we need to plan the two fftw stages.
  400. * - For GPU workers, we need to do the planning in the CUDA context, so we do
  401. * this lazily through the initialised1 and initialised2 flags ; TODO: use
  402. * starpu_execute_on_each_worker instead (done in the omp branch).
  403. * - We allocate all the temporary buffers and register them to starpu.
  404. * - We create all the tasks, but do not submit them yet. It will be possible
  405. * to reuse them at will to perform several ffts with the same planning.
  406. */
  407. STARPUFFT(plan)
  408. STARPUFFT(plan_dft_1d)(int n, int sign, unsigned flags)
  409. {
  410. int workerid;
  411. int n1 = DIV_1D;
  412. int n2 = n / n1;
  413. int n3;
  414. int z;
  415. struct starpu_task *task;
  416. if (PARALLEL) {
  417. #ifdef STARPU_USE_CUDA
  418. /* cufft 1D limited to 8M elements */
  419. while (n2 > 8 << 20) {
  420. n1 *= 2;
  421. n2 /= 2;
  422. }
  423. #endif
  424. STARPU_ASSERT(n == n1*n2);
  425. STARPU_ASSERT(n1 < (1ULL << I_BITS));
  426. /* distribute the n2 second ffts into DIV_1D packages */
  427. n3 = n2 / DIV_1D;
  428. STARPU_ASSERT(n2 == n3*DIV_1D);
  429. }
  430. /* TODO: flags? Automatically set FFTW_MEASURE on calibration? */
  431. STARPU_ASSERT(flags == 0);
  432. STARPUFFT(plan) plan = malloc(sizeof(*plan));
  433. memset(plan, 0, sizeof(*plan));
  434. if (PARALLEL) {
  435. plan->number = STARPU_ATOMIC_ADD(&starpufft_last_plan_number, 1) - 1;
  436. /* The plan number has a limited size */
  437. STARPU_ASSERT(plan->number < (1ULL << NUMBER_BITS));
  438. }
  439. /* Just one dimension */
  440. plan->dim = 1;
  441. plan->n = malloc(plan->dim * sizeof(*plan->n));
  442. plan->n[0] = n;
  443. if (PARALLEL) {
  444. check_dims(plan);
  445. plan->n1 = malloc(plan->dim * sizeof(*plan->n1));
  446. plan->n1[0] = n1;
  447. plan->n2 = malloc(plan->dim * sizeof(*plan->n2));
  448. plan->n2[0] = n2;
  449. }
  450. /* Note: this is for coherency with the 2D case */
  451. plan->totsize = n;
  452. if (PARALLEL) {
  453. plan->totsize1 = n1;
  454. plan->totsize2 = n2;
  455. plan->totsize3 = DIV_1D;
  456. plan->totsize4 = plan->totsize / plan->totsize3;
  457. }
  458. plan->type = C2C;
  459. plan->sign = sign;
  460. if (PARALLEL) {
  461. /* Compute the w^k just once. */
  462. compute_roots(plan);
  463. }
  464. /* Initialize per-worker working set */
  465. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++) {
  466. switch (starpu_worker_get_type(workerid)) {
  467. case STARPU_CPU_WORKER:
  468. #ifdef STARPU_HAVE_FFTW
  469. if (PARALLEL) {
  470. /* first fft plan: one fft of size n2.
  471. * FFTW imposes that buffer pointers are known at
  472. * planning time. */
  473. plan->plans[workerid].plan1_cpu = _FFTW(plan_dft_1d)(n2, NULL, (void*) 1, sign, _FFTW_FLAGS);
  474. STARPU_ASSERT(plan->plans[workerid].plan1_cpu);
  475. /* second fft plan: n3 ffts of size n1 */
  476. plan->plans[workerid].plan2_cpu = _FFTW(plan_many_dft)(plan->dim,
  477. plan->n1, n3,
  478. NULL, NULL, 1, plan->totsize1,
  479. (void*) 1, NULL, 1, plan->totsize1,
  480. sign, _FFTW_FLAGS);
  481. STARPU_ASSERT(plan->plans[workerid].plan2_cpu);
  482. } else {
  483. /* fft plan: one fft of size n. */
  484. plan->plans[workerid].plan_cpu = _FFTW(plan_dft_1d)(n, NULL, (void*) 1, sign, _FFTW_FLAGS);
  485. STARPU_ASSERT(plan->plans[workerid].plan_cpu);
  486. }
  487. #else
  488. /* #warning libstarpufft can not work correctly if libfftw3 is not installed */
  489. #endif
  490. break;
  491. case STARPU_CUDA_WORKER:
  492. break;
  493. default:
  494. /* Do not care, we won't be executing anything there. */
  495. break;
  496. }
  497. }
  498. #ifdef STARPU_USE_CUDA
  499. if (PARALLEL) {
  500. starpu_execute_on_each_worker(STARPUFFT(fft1_1d_plan_gpu), plan, STARPU_CUDA);
  501. starpu_execute_on_each_worker(STARPUFFT(fft2_1d_plan_gpu), plan, STARPU_CUDA);
  502. } else {
  503. starpu_execute_on_each_worker(STARPUFFT(fft_1d_plan_gpu), plan, STARPU_CUDA);
  504. }
  505. #endif
  506. if (PARALLEL) {
  507. /* Allocate buffers. */
  508. plan->twisted1 = STARPUFFT(malloc)(plan->totsize * sizeof(*plan->twisted1));
  509. memset(plan->twisted1, 0, plan->totsize * sizeof(*plan->twisted1));
  510. plan->fft1 = STARPUFFT(malloc)(plan->totsize * sizeof(*plan->fft1));
  511. memset(plan->fft1, 0, plan->totsize * sizeof(*plan->fft1));
  512. plan->twisted2 = STARPUFFT(malloc)(plan->totsize * sizeof(*plan->twisted2));
  513. memset(plan->twisted2, 0, plan->totsize * sizeof(*plan->twisted2));
  514. plan->fft2 = STARPUFFT(malloc)(plan->totsize * sizeof(*plan->fft2));
  515. memset(plan->fft2, 0, plan->totsize * sizeof(*plan->fft2));
  516. /* Allocate handle arrays */
  517. plan->twisted1_handle = malloc(plan->totsize1 * sizeof(*plan->twisted1_handle));
  518. plan->fft1_handle = malloc(plan->totsize1 * sizeof(*plan->fft1_handle));
  519. plan->twisted2_handle = malloc(plan->totsize3 * sizeof(*plan->twisted2_handle));
  520. plan->fft2_handle = malloc(plan->totsize3 * sizeof(*plan->fft2_handle));
  521. /* Allocate task arrays */
  522. plan->twist1_tasks = malloc(plan->totsize1 * sizeof(*plan->twist1_tasks));
  523. plan->fft1_tasks = malloc(plan->totsize1 * sizeof(*plan->fft1_tasks));
  524. plan->twist2_tasks = malloc(plan->totsize3 * sizeof(*plan->twist2_tasks));
  525. plan->fft2_tasks = malloc(plan->totsize3 * sizeof(*plan->fft2_tasks));
  526. plan->twist3_tasks = malloc(plan->totsize3 * sizeof(*plan->twist3_tasks));
  527. /* Allocate codelet argument arrays */
  528. plan->fft1_args = malloc(plan->totsize1 * sizeof(*plan->fft1_args));
  529. plan->fft2_args = malloc(plan->totsize3 * sizeof(*plan->fft2_args));
  530. /* Create first-round tasks: DIV_1D tasks of type twist1 and fft1 */
  531. for (z = 0; z < plan->totsize1; z++) {
  532. int i = z;
  533. #define STEP_TAG(step) STEP_TAG_1D(plan, step, i)
  534. /* TODO: get rid of tags */
  535. plan->fft1_args[z].plan = plan;
  536. plan->fft1_args[z].i = i;
  537. /* Register the twisted1 buffer of size n2. */
  538. starpu_vector_data_register(&plan->twisted1_handle[z], 0, (uintptr_t) &plan->twisted1[z*plan->totsize2], plan->totsize2, sizeof(*plan->twisted1));
  539. /* Register the fft1 buffer of size n2. */
  540. starpu_vector_data_register(&plan->fft1_handle[z], 0, (uintptr_t) &plan->fft1[z*plan->totsize2], plan->totsize2, sizeof(*plan->fft1));
  541. /* We'll need the result of fft1 on the CPU for the second
  542. * twist anyway, so tell starpu to not keep the fft1 buffer in
  543. * the GPU. */
  544. starpu_data_set_wt_mask(plan->fft1_handle[z], 1<<0);
  545. /* Create twist1 task */
  546. plan->twist1_tasks[z] = task = starpu_task_create();
  547. task->cl = &STARPUFFT(twist1_1d_codelet);
  548. /* task->buffers[0].handle = to be filled at execution to point
  549. to the application input. */
  550. task->buffers[0].mode = STARPU_R;
  551. task->buffers[1].handle = plan->twisted1_handle[z];
  552. task->buffers[1].mode = STARPU_W;
  553. task->cl_arg = &plan->fft1_args[z];
  554. task->tag_id = STEP_TAG(TWIST1);
  555. task->use_tag = 1;
  556. task->destroy = 0;
  557. /* Tell that fft1 depends on twisted1 */
  558. starpu_tag_declare_deps(STEP_TAG(FFT1),
  559. 1, STEP_TAG(TWIST1));
  560. /* Create FFT1 task */
  561. plan->fft1_tasks[z] = task = starpu_task_create();
  562. task->cl = &STARPUFFT(fft1_1d_codelet);
  563. task->buffers[0].handle = plan->twisted1_handle[z];
  564. task->buffers[0].mode = STARPU_R;
  565. task->buffers[1].handle = plan->fft1_handle[z];
  566. task->buffers[1].mode = STARPU_W;
  567. task->buffers[2].handle = plan->roots_handle[0];
  568. task->buffers[2].mode = STARPU_R;
  569. task->cl_arg = &plan->fft1_args[z];
  570. task->tag_id = STEP_TAG(FFT1);
  571. task->use_tag = 1;
  572. task->destroy = 0;
  573. /* Tell that the join task will depend on the fft1 task. */
  574. starpu_tag_declare_deps(STEP_TAG_1D(plan, JOIN, 0),
  575. 1, STEP_TAG(FFT1));
  576. #undef STEP_TAG
  577. }
  578. /* Create the join task, only serving as a dependency point between
  579. * fft1 and twist2 tasks */
  580. plan->join_task = task = starpu_task_create();
  581. task->cl = NULL;
  582. task->tag_id = STEP_TAG_1D(plan, JOIN, 0);
  583. task->use_tag = 1;
  584. task->destroy = 0;
  585. /* Create second-round tasks: DIV_1D batches of n2/DIV_1D twist2, fft2,
  586. * and twist3 */
  587. for (z = 0; z < plan->totsize3; z++) {
  588. int jj = z;
  589. #define STEP_TAG(step) STEP_TAG_1D(plan, step, jj)
  590. plan->fft2_args[z].plan = plan;
  591. plan->fft2_args[z].jj = jj;
  592. /* Register n3 twisted2 buffers of size n1 */
  593. starpu_vector_data_register(&plan->twisted2_handle[z], 0, (uintptr_t) &plan->twisted2[z*plan->totsize4], plan->totsize4, sizeof(*plan->twisted2));
  594. starpu_vector_data_register(&plan->fft2_handle[z], 0, (uintptr_t) &plan->fft2[z*plan->totsize4], plan->totsize4, sizeof(*plan->fft2));
  595. /* We'll need the result of fft2 on the CPU for the third
  596. * twist anyway, so tell starpu to not keep the fft2 buffer in
  597. * the GPU. */
  598. starpu_data_set_wt_mask(plan->fft2_handle[z], 1<<0);
  599. /* Tell that twisted2 depends on the join task */
  600. starpu_tag_declare_deps(STEP_TAG(TWIST2),
  601. 1, STEP_TAG_1D(plan, JOIN, 0));
  602. /* Create twist2 task */
  603. plan->twist2_tasks[z] = task = starpu_task_create();
  604. task->cl = &STARPUFFT(twist2_1d_codelet);
  605. task->buffers[0].handle = plan->twisted2_handle[z];
  606. task->buffers[0].mode = STARPU_W;
  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->buffers[0].handle = plan->twisted2_handle[z];
  618. task->buffers[0].mode = STARPU_R;
  619. task->buffers[1].handle = plan->fft2_handle[z];
  620. task->buffers[1].mode = STARPU_W;
  621. task->cl_arg = &plan->fft2_args[z];
  622. task->tag_id = STEP_TAG(FFT2);
  623. task->use_tag = 1;
  624. task->destroy = 0;
  625. /* Tell that twist3 depends on fft2 */
  626. starpu_tag_declare_deps(STEP_TAG(TWIST3),
  627. 1, STEP_TAG(FFT2));
  628. /* Create twist3 tasks */
  629. /* These run only on CPUs and thus write directly into the
  630. * application output buffer. */
  631. plan->twist3_tasks[z] = task = starpu_task_create();
  632. task->cl = &STARPUFFT(twist3_1d_codelet);
  633. task->buffers[0].handle = plan->fft2_handle[z];
  634. task->buffers[0].mode = STARPU_R;
  635. task->cl_arg = &plan->fft2_args[z];
  636. task->tag_id = STEP_TAG(TWIST3);
  637. task->use_tag = 1;
  638. task->destroy = 0;
  639. /* Tell that to be completely finished we need to have finished
  640. * this twisted3 */
  641. starpu_tag_declare_deps(STEP_TAG_1D(plan, END, 0),
  642. 1, STEP_TAG(TWIST3));
  643. #undef STEP_TAG
  644. }
  645. /* Create end task, only serving as a join point. */
  646. plan->end_task = task = starpu_task_create();
  647. task->cl = NULL;
  648. task->tag_id = STEP_TAG_1D(plan, END, 0);
  649. task->use_tag = 1;
  650. task->destroy = 0;
  651. }
  652. return plan;
  653. }
  654. /* Actually submit all the tasks. */
  655. static struct starpu_task *
  656. STARPUFFT(start1dC2C)(STARPUFFT(plan) plan, starpu_data_handle_t in, starpu_data_handle_t out)
  657. {
  658. STARPU_ASSERT(plan->type == C2C);
  659. int z;
  660. if (PARALLEL) {
  661. for (z=0; z < plan->totsize1; z++) {
  662. starpu_task_submit(plan->twist1_tasks[z]);
  663. starpu_task_submit(plan->fft1_tasks[z]);
  664. }
  665. starpu_task_submit(plan->join_task);
  666. for (z=0; z < plan->totsize3; z++) {
  667. starpu_task_submit(plan->twist2_tasks[z]);
  668. starpu_task_submit(plan->fft2_tasks[z]);
  669. starpu_task_submit(plan->twist3_tasks[z]);
  670. }
  671. starpu_task_submit(plan->end_task);
  672. return plan->end_task;
  673. } else /* !PARALLEL */ {
  674. struct starpu_task *task;
  675. /* Create FFT task */
  676. task = starpu_task_create();
  677. task->cl = &STARPUFFT(fft_1d_codelet);
  678. task->buffers[0].handle = in;
  679. task->buffers[0].mode = STARPU_R;
  680. task->buffers[1].handle = out;
  681. task->buffers[1].mode = STARPU_W;
  682. task->cl_arg = plan;
  683. starpu_task_submit(task);
  684. return task;
  685. }
  686. }
  687. /* Free all the tags. The generic code handles freeing the buffers. */
  688. static void
  689. STARPUFFT(free_1d_tags)(STARPUFFT(plan) plan)
  690. {
  691. unsigned i;
  692. int n1 = plan->n1[0];
  693. if (!PARALLEL)
  694. return;
  695. for (i = 0; i < n1; i++) {
  696. starpu_tag_remove(STEP_TAG_1D(plan, TWIST1, i));
  697. starpu_tag_remove(STEP_TAG_1D(plan, FFT1, i));
  698. }
  699. starpu_tag_remove(STEP_TAG_1D(plan, JOIN, 0));
  700. for (i = 0; i < DIV_1D; i++) {
  701. starpu_tag_remove(STEP_TAG_1D(plan, TWIST2, i));
  702. starpu_tag_remove(STEP_TAG_1D(plan, FFT2, i));
  703. starpu_tag_remove(STEP_TAG_1D(plan, TWIST3, i));
  704. }
  705. starpu_tag_remove(STEP_TAG_1D(plan, END, 0));
  706. }