starpufftx.c 13 KB

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