starpufftx1d.c 25 KB

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