starpufftx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #define PARALLEL 0
  17. #include <math.h>
  18. #include <unistd.h>
  19. #include <sys/time.h>
  20. #include <starpu.h>
  21. #include <common/config.h>
  22. #include "starpufft.h"
  23. #ifdef STARPU_USE_CUDA
  24. #define _externC extern
  25. #include "cudax_kernels.h"
  26. #if defined(FLOAT) || defined(STARPU_HAVE_CUFFTDOUBLECOMPLEX)
  27. # define __STARPU_USE_CUDA
  28. #else
  29. # undef __STARPU_USE_CUDA
  30. #endif
  31. #endif
  32. #define _FFTW_FLAGS FFTW_ESTIMATE
  33. /* Steps for the parallel variant */
  34. enum steps
  35. {
  36. SPECIAL, TWIST1, FFT1, JOIN, TWIST2, FFT2, TWIST3, END
  37. };
  38. #define NUMBER_BITS 5
  39. #define NUMBER_SHIFT (64 - NUMBER_BITS)
  40. #define STEP_BITS 3
  41. #define STEP_SHIFT (NUMBER_SHIFT - STEP_BITS)
  42. /* Tags for the steps of the parallel variant */
  43. #define _STEP_TAG(plan, step, i) (((starpu_tag_t) plan->number << NUMBER_SHIFT) | ((starpu_tag_t)(step) << STEP_SHIFT) | (starpu_tag_t) (i))
  44. #define I_BITS STEP_SHIFT
  45. enum type
  46. {
  47. R2C,
  48. C2R,
  49. C2C
  50. };
  51. static unsigned task_per_worker[STARPU_NMAXWORKERS];
  52. static unsigned samples_per_worker[STARPU_NMAXWORKERS];
  53. static struct timeval start, submit_tasks, end;
  54. /*
  55. *
  56. * The actual kernels
  57. *
  58. */
  59. struct STARPUFFT(plan)
  60. {
  61. int number; /* uniquely identifies the plan, for starpu tags */
  62. int *n;
  63. int *n1;
  64. int *n2;
  65. int totsize;
  66. int totsize1; /* Number of first-round tasks */
  67. int totsize2; /* Size of first-round tasks */
  68. int totsize3; /* Number of second-round tasks */
  69. int totsize4; /* Size of second-round tasks */
  70. int dim;
  71. enum type type;
  72. int sign;
  73. STARPUFFT(complex) *roots[2];
  74. starpu_data_handle_t roots_handle[2];
  75. /* For each worker, we need some data */
  76. struct
  77. {
  78. #ifdef STARPU_USE_CUDA
  79. /* CUFFT plans */
  80. cufftHandle plan1_cuda, plan2_cuda;
  81. /* Sequential version */
  82. cufftHandle plan_cuda;
  83. #endif
  84. #ifdef STARPU_HAVE_FFTW
  85. /* FFTW plans */
  86. _fftw_plan plan1_cpu, plan2_cpu;
  87. /* Sequential version */
  88. _fftw_plan plan_cpu;
  89. #endif
  90. } plans[STARPU_NMAXWORKERS];
  91. /* Buffers for codelets */
  92. STARPUFFT(complex) *in, *twisted1, *fft1, *twisted2, *fft2, *out;
  93. /* corresponding starpu DSM handles */
  94. starpu_data_handle_t in_handle, *twisted1_handle, *fft1_handle, *twisted2_handle, *fft2_handle, out_handle;
  95. /* Tasks */
  96. struct starpu_task **twist1_tasks, **fft1_tasks, **twist2_tasks, **fft2_tasks, **twist3_tasks;
  97. struct starpu_task *join_task, *end_task;
  98. /* Arguments for tasks */
  99. struct STARPUFFT(args) *fft1_args, *fft2_args;
  100. };
  101. struct STARPUFFT(args)
  102. {
  103. struct STARPUFFT(plan) *plan;
  104. int i, j, jj, kk, ll, *iv, *kkv;
  105. };
  106. static void
  107. check_dims(STARPUFFT(plan) plan)
  108. {
  109. int dim;
  110. for (dim = 0; dim < plan->dim; dim++)
  111. if (plan->n[dim] & (plan->n[dim]-1))
  112. {
  113. fprintf(stderr,"can't cope with non-power-of-2\n");
  114. STARPU_ABORT();
  115. }
  116. }
  117. static void
  118. compute_roots(STARPUFFT(plan) plan)
  119. {
  120. int dim, k;
  121. /* Compute the n-roots and m-roots of unity for twiddling */
  122. for (dim = 0; dim < plan->dim; dim++)
  123. {
  124. STARPUFFT(complex) exp = (plan->sign * 2. * 4.*atan(1.)) * _Complex_I / (STARPUFFT(complex)) plan->n[dim];
  125. plan->roots[dim] = malloc(plan->n[dim] * sizeof(**plan->roots));
  126. for (k = 0; k < plan->n[dim]; k++)
  127. plan->roots[dim][k] = cexp(exp*k);
  128. starpu_vector_data_register(&plan->roots_handle[dim], STARPU_MAIN_RAM, (uintptr_t) plan->roots[dim], plan->n[dim], sizeof(**plan->roots));
  129. #ifdef STARPU_USE_CUDA
  130. if (plan->n[dim] > 100000)
  131. {
  132. /* prefetch the big root array on GPUs */
  133. unsigned worker;
  134. unsigned nworkers = starpu_worker_get_count();
  135. for (worker = 0; worker < nworkers; worker++)
  136. {
  137. unsigned node = starpu_worker_get_memory_node(worker);
  138. if (starpu_worker_get_type(worker) == STARPU_CUDA_WORKER)
  139. starpu_data_prefetch_on_node(plan->roots_handle[dim], node, 0);
  140. }
  141. }
  142. #endif
  143. }
  144. }
  145. /* Only CUDA capability >= 1.3 supports doubles, rule old card out. */
  146. #ifdef DOUBLE
  147. static int can_execute(unsigned workerid, struct starpu_task *task STARPU_ATTRIBUTE_UNUSED, unsigned nimpl STARPU_ATTRIBUTE_UNUSED) {
  148. if (starpu_worker_get_type(workerid) == STARPU_CPU_WORKER)
  149. return 1;
  150. #ifdef STARPU_USE_CUDA
  151. {
  152. /* Cuda device */
  153. const struct cudaDeviceProp *props;
  154. props = starpu_cuda_get_device_properties(workerid);
  155. if (props->major >= 2 || props->minor >= 3)
  156. /* At least compute capability 1.3, supports doubles */
  157. return 1;
  158. /* Old card does not support doubles */
  159. return 0;
  160. }
  161. #endif
  162. return 0;
  163. }
  164. #define CAN_EXECUTE .can_execute = can_execute,
  165. #else
  166. #define CAN_EXECUTE
  167. #endif
  168. #include "starpufftx1d.c"
  169. #include "starpufftx2d.c"
  170. #include "starpufftx3d.c"
  171. struct starpu_task *
  172. STARPUFFT(start)(STARPUFFT(plan) plan, void *_in, void *_out)
  173. {
  174. struct starpu_task *task;
  175. int z;
  176. plan->in = _in;
  177. plan->out = _out;
  178. switch (plan->dim)
  179. {
  180. case 1:
  181. {
  182. switch (plan->type)
  183. {
  184. case C2C:
  185. starpu_vector_data_register(&plan->in_handle, STARPU_MAIN_RAM, (uintptr_t) plan->in, plan->totsize, sizeof(STARPUFFT(complex)));
  186. if (!PARALLEL)
  187. starpu_vector_data_register(&plan->out_handle, STARPU_MAIN_RAM, (uintptr_t) plan->out, plan->totsize, sizeof(STARPUFFT(complex)));
  188. if (PARALLEL)
  189. {
  190. for (z = 0; z < plan->totsize1; z++)
  191. plan->twist1_tasks[z]->handles[0] = plan->in_handle;
  192. }
  193. task = STARPUFFT(start1dC2C)(plan, plan->in_handle, plan->out_handle);
  194. break;
  195. default:
  196. STARPU_ABORT();
  197. break;
  198. }
  199. break;
  200. }
  201. case 2:
  202. starpu_vector_data_register(&plan->in_handle, STARPU_MAIN_RAM, (uintptr_t) plan->in, plan->totsize, sizeof(STARPUFFT(complex)));
  203. if (!PARALLEL)
  204. starpu_vector_data_register(&plan->out_handle, STARPU_MAIN_RAM, (uintptr_t) plan->out, plan->totsize, sizeof(STARPUFFT(complex)));
  205. if (PARALLEL)
  206. {
  207. for (z = 0; z < plan->totsize1; z++)
  208. plan->twist1_tasks[z]->handles[0] = plan->in_handle;
  209. }
  210. task = STARPUFFT(start2dC2C)(plan, plan->in_handle, plan->out_handle);
  211. break;
  212. case 3:
  213. starpu_vector_data_register(&plan->in_handle, STARPU_MAIN_RAM, (uintptr_t) plan->in, plan->totsize, sizeof(STARPUFFT(complex)));
  214. if (!PARALLEL)
  215. starpu_vector_data_register(&plan->out_handle, STARPU_MAIN_RAM, (uintptr_t) plan->out, plan->totsize, sizeof(STARPUFFT(complex)));
  216. if (PARALLEL)
  217. {
  218. for (z = 0; z < plan->totsize1; z++)
  219. plan->twist1_tasks[z]->handles[0] = plan->in_handle;
  220. }
  221. task = STARPUFFT(start3dC2C)(plan, plan->in_handle, plan->out_handle);
  222. break;
  223. default:
  224. STARPU_ABORT();
  225. break;
  226. }
  227. return task;
  228. }
  229. void
  230. STARPUFFT(cleanup)(STARPUFFT(plan) plan)
  231. {
  232. if (plan->in_handle)
  233. starpu_data_unregister(plan->in_handle);
  234. if (!PARALLEL)
  235. {
  236. if (plan->out_handle)
  237. starpu_data_unregister(plan->out_handle);
  238. }
  239. }
  240. struct starpu_task *
  241. STARPUFFT(start_handle)(STARPUFFT(plan) plan, starpu_data_handle_t in, starpu_data_handle_t out)
  242. {
  243. return STARPUFFT(start1dC2C)(plan, in, out);
  244. }
  245. int
  246. STARPUFFT(execute)(STARPUFFT(plan) plan, void *in, void *out)
  247. {
  248. int ret;
  249. memset(task_per_worker, 0, sizeof(task_per_worker));
  250. memset(samples_per_worker, 0, sizeof(task_per_worker));
  251. gettimeofday(&start, NULL);
  252. struct starpu_task *task = STARPUFFT(start)(plan, in, out);
  253. gettimeofday(&submit_tasks, NULL);
  254. if (task)
  255. {
  256. ret = starpu_task_wait(task);
  257. STARPU_ASSERT(ret == 0);
  258. }
  259. STARPUFFT(cleanup)(plan);
  260. gettimeofday(&end, NULL);
  261. return (task == NULL ? -1 : 0);
  262. }
  263. int
  264. STARPUFFT(execute_handle)(STARPUFFT(plan) plan, starpu_data_handle_t in, starpu_data_handle_t out)
  265. {
  266. int ret;
  267. struct starpu_task *task = STARPUFFT(start_handle)(plan, in, out);
  268. if (!task) return -1;
  269. ret = starpu_task_wait(task);
  270. STARPU_ASSERT(ret == 0);
  271. return 0;
  272. }
  273. /* Destroy FFTW plans, unregister and free buffers, and free tags */
  274. void
  275. STARPUFFT(destroy_plan)(STARPUFFT(plan) plan)
  276. {
  277. unsigned workerid;
  278. int dim, i;
  279. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++)
  280. {
  281. switch (starpu_worker_get_type(workerid))
  282. {
  283. case STARPU_CPU_WORKER:
  284. #ifdef STARPU_HAVE_FFTW
  285. if (PARALLEL)
  286. {
  287. _FFTW(destroy_plan)(plan->plans[workerid].plan1_cpu);
  288. _FFTW(destroy_plan)(plan->plans[workerid].plan2_cpu);
  289. }
  290. else
  291. {
  292. _FFTW(destroy_plan)(plan->plans[workerid].plan_cpu);
  293. }
  294. #endif
  295. break;
  296. case STARPU_CUDA_WORKER:
  297. #ifdef STARPU_USE_CUDA
  298. /* FIXME: Can't deallocate */
  299. #endif
  300. break;
  301. default:
  302. /* Do not care, we won't be executing anything there. */
  303. break;
  304. }
  305. }
  306. if (PARALLEL)
  307. {
  308. for (i = 0; i < plan->totsize1; i++)
  309. {
  310. starpu_data_unregister(plan->twisted1_handle[i]);
  311. free(plan->twist1_tasks[i]);
  312. starpu_data_unregister(plan->fft1_handle[i]);
  313. free(plan->fft1_tasks[i]);
  314. }
  315. free(plan->twisted1_handle);
  316. free(plan->twist1_tasks);
  317. free(plan->fft1_handle);
  318. free(plan->fft1_tasks);
  319. free(plan->fft1_args);
  320. free(plan->join_task);
  321. for (i = 0; i < plan->totsize3; i++)
  322. {
  323. starpu_data_unregister(plan->twisted2_handle[i]);
  324. free(plan->twist2_tasks[i]);
  325. starpu_data_unregister(plan->fft2_handle[i]);
  326. free(plan->fft2_tasks[i]);
  327. free(plan->twist3_tasks[i]);
  328. }
  329. free(plan->end_task);
  330. free(plan->twisted2_handle);
  331. free(plan->twist2_tasks);
  332. free(plan->fft2_handle);
  333. free(plan->fft2_tasks);
  334. free(plan->twist3_tasks);
  335. free(plan->fft2_args);
  336. for (dim = 0; dim < plan->dim; dim++)
  337. {
  338. starpu_data_unregister(plan->roots_handle[dim]);
  339. free(plan->roots[dim]);
  340. }
  341. switch (plan->dim)
  342. {
  343. case 1:
  344. STARPUFFT(free_1d_tags)(plan);
  345. break;
  346. case 2:
  347. STARPUFFT(free_2d_tags)(plan);
  348. break;
  349. default:
  350. STARPU_ABORT();
  351. break;
  352. }
  353. free(plan->n1);
  354. free(plan->n2);
  355. STARPUFFT(free)(plan->twisted1);
  356. STARPUFFT(free)(plan->fft1);
  357. STARPUFFT(free)(plan->twisted2);
  358. STARPUFFT(free)(plan->fft2);
  359. }
  360. free(plan->n);
  361. free(plan);
  362. }
  363. void *
  364. STARPUFFT(malloc)(size_t n)
  365. {
  366. #ifdef STARPU_USE_CUDA
  367. void *res;
  368. starpu_malloc(&res, n);
  369. return res;
  370. #else
  371. # ifdef STARPU_HAVE_FFTW
  372. return _FFTW(malloc)(n);
  373. # else
  374. return malloc(n);
  375. # endif
  376. #endif
  377. }
  378. void
  379. STARPUFFT(free)(void *p)
  380. {
  381. #ifdef STARPU_USE_CUDA
  382. starpu_free(p);
  383. #else
  384. # ifdef STARPU_HAVE_FFTW
  385. _FFTW(free)(p);
  386. # else
  387. free(p);
  388. # endif
  389. #endif
  390. }
  391. void
  392. STARPUFFT(showstats)(FILE *out)
  393. {
  394. unsigned worker;
  395. unsigned total;
  396. #define TIMING(begin,end) (double)((end.tv_sec - begin.tv_sec)*1000000 + (end.tv_usec - begin.tv_usec))
  397. #define MSTIMING(begin,end) (TIMING(begin,end)/1000.)
  398. double paratiming = TIMING(start,end);
  399. fprintf(out, "Tasks submission took %2.2f ms\n", MSTIMING(start,submit_tasks));
  400. fprintf(out, "Tasks termination took %2.2f ms\n", MSTIMING(submit_tasks,end));
  401. fprintf(out, "Total %2.2f ms\n", MSTIMING(start,end));
  402. for (worker = 0, total = 0; worker < starpu_worker_get_count(); worker++)
  403. total += task_per_worker[worker];
  404. if (!total)
  405. return;
  406. for (worker = 0; worker < starpu_worker_get_count(); worker++)
  407. {
  408. if (task_per_worker[worker])
  409. {
  410. char name[32];
  411. starpu_worker_get_name(worker, name, sizeof(name));
  412. unsigned long bytes = sizeof(STARPUFFT(complex))*samples_per_worker[worker];
  413. fprintf(stderr, "\t%s -> %2.2f MB\t%2.2f\tMB/s\t%u %2.2f %%\n", name, (1.0*bytes)/(1024*1024), bytes/paratiming, task_per_worker[worker], (100.0*task_per_worker[worker])/total);
  414. }
  415. }
  416. }
  417. #ifdef STARPU_USE_CUDA
  418. void
  419. STARPUFFT(report_error)(const char *func, const char *file, int line, cufftResult status)
  420. {
  421. char *errormsg;
  422. switch (status)
  423. {
  424. case CUFFT_SUCCESS:
  425. errormsg = "success"; /* It'd be weird to get here. */
  426. break;
  427. case CUFFT_INVALID_PLAN:
  428. errormsg = "invalid plan";
  429. break;
  430. case CUFFT_ALLOC_FAILED:
  431. errormsg = "alloc failed";
  432. break;
  433. case CUFFT_INVALID_TYPE:
  434. errormsg = "invalid type";
  435. break;
  436. case CUFFT_INVALID_VALUE:
  437. errormsg = "invalid value";
  438. break;
  439. case CUFFT_INTERNAL_ERROR:
  440. errormsg = "internal error";
  441. break;
  442. case CUFFT_EXEC_FAILED:
  443. errormsg = "exec failed";
  444. break;
  445. case CUFFT_SETUP_FAILED:
  446. errormsg = "setup failed";
  447. break;
  448. case CUFFT_INVALID_SIZE:
  449. errormsg = "invalid size";
  450. break;
  451. case CUFFT_UNALIGNED_DATA:
  452. errormsg = "unaligned data";
  453. break;
  454. #if defined(MAX_CUFFT_ERROR) && (MAX_CUFFT_ERROR >= 0xE)
  455. case CUFFT_INCOMPLETE_PARAMETER_LIST:
  456. errormsg = "incomplete parameter list";
  457. break;
  458. case CUFFT_INVALID_DEVICE:
  459. errormsg = "invalid device";
  460. break;
  461. case CUFFT_PARSE_ERROR:
  462. errormsg = "parse error";
  463. break;
  464. case CUFFT_NO_WORKSPACE:
  465. errormsg = "no workspace";
  466. break;
  467. #endif /* MAX_CUFFT_ERROR >= 0xE */
  468. default:
  469. errormsg = "unknown error";
  470. break;
  471. }
  472. fprintf(stderr, "oops in %s (%s:%d)... %d: %s\n",
  473. func, file, line, status, errormsg);
  474. STARPU_ABORT();
  475. }
  476. #endif /* !STARPU_USE_CUDA */