starpufftx.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2011 Université de Bordeaux 1
  4. * Copyright (C) 2010 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. #include <math.h>
  18. #include <pthread.h>
  19. #include <unistd.h>
  20. #include <sys/time.h>
  21. #include <starpu.h>
  22. #include <config.h>
  23. #include "starpufft.h"
  24. #ifdef STARPU_USE_CUDA
  25. #define _externC extern
  26. #include "cudax_kernels.h"
  27. #endif
  28. #define _FFTW_FLAGS FFTW_ESTIMATE
  29. enum steps {
  30. SPECIAL, TWIST1, FFT1, JOIN, TWIST2, FFT2, TWIST3, END
  31. };
  32. #define NUMBER_BITS 5
  33. #define NUMBER_SHIFT (64 - NUMBER_BITS)
  34. #define STEP_BITS 3
  35. #define STEP_SHIFT (NUMBER_SHIFT - STEP_BITS)
  36. #define _STEP_TAG(plan, step, i) (((starpu_tag) plan->number << NUMBER_SHIFT) | ((starpu_tag)(step) << STEP_SHIFT) | (starpu_tag) (i))
  37. #define I_BITS STEP_SHIFT
  38. enum type {
  39. R2C,
  40. C2R,
  41. C2C
  42. };
  43. static unsigned task_per_worker[STARPU_NMAXWORKERS];
  44. static unsigned samples_per_worker[STARPU_NMAXWORKERS];
  45. static struct timeval start, submit_tasks, end;
  46. /*
  47. *
  48. * The actual kernels
  49. *
  50. */
  51. struct STARPUFFT(plan) {
  52. int number; /* uniquely identifies the plan, for starpu tags */
  53. int *n;
  54. int *n1;
  55. int *n2;
  56. int totsize;
  57. int totsize1; /* Number of first-round tasks */
  58. int totsize2; /* Size of first-round tasks */
  59. int totsize3; /* Number of second-round tasks */
  60. int totsize4; /* Size of second-round tasks */
  61. int dim;
  62. enum type type;
  63. int sign;
  64. STARPUFFT(complex) *roots[2];
  65. starpu_data_handle roots_handle[2];
  66. /* For each worker, we need some data */
  67. struct {
  68. #ifdef STARPU_USE_CUDA
  69. /* CUFFT plans */
  70. cufftHandle plan1_cuda, plan2_cuda;
  71. /* Whether the plans above are initialized */
  72. int initialized1, initialized2;
  73. #endif
  74. #ifdef STARPU_HAVE_FFTW
  75. /* FFTW plans */
  76. _fftw_plan plan1_cpu, plan2_cpu;
  77. /* Buffers used by the plans above */
  78. _fftw_complex *in1, *out1;
  79. _fftw_complex *in2, *out2;
  80. #endif
  81. } plans[STARPU_NMAXWORKERS];
  82. /* Buffers for codelets */
  83. STARPUFFT(complex) *in, *twisted1, *fft1, *twisted2, *fft2, *out;
  84. /* corresponding starpu DSM handles */
  85. starpu_data_handle in_handle, *twisted1_handle, *fft1_handle, *twisted2_handle, *fft2_handle;
  86. /* Tasks */
  87. struct starpu_task **twist1_tasks, **fft1_tasks, **twist2_tasks, **fft2_tasks, **twist3_tasks;
  88. struct starpu_task *join_task, *end_task;
  89. /* Arguments for tasks */
  90. struct STARPUFFT(args) *fft1_args, *fft2_args;
  91. };
  92. struct STARPUFFT(args) {
  93. struct STARPUFFT(plan) *plan;
  94. int i, j, jj, kk, ll, *iv, *kkv;
  95. };
  96. static void
  97. check_dims(STARPUFFT(plan) plan)
  98. {
  99. int dim;
  100. for (dim = 0; dim < plan->dim; dim++)
  101. if (plan->n[dim] & (plan->n[dim]-1)) {
  102. fprintf(stderr,"can't cope with non-power-of-2\n");
  103. STARPU_ABORT();
  104. }
  105. }
  106. static void
  107. compute_roots(STARPUFFT(plan) plan)
  108. {
  109. int dim, k;
  110. /* Compute the n-roots and m-roots of unity for twiddling */
  111. for (dim = 0; dim < plan->dim; dim++) {
  112. STARPUFFT(complex) exp = (plan->sign * 2. * 4.*atan(1.)) * _Complex_I / (STARPUFFT(complex)) plan->n[dim];
  113. plan->roots[dim] = malloc(plan->n[dim] * sizeof(**plan->roots));
  114. for (k = 0; k < plan->n[dim]; k++)
  115. plan->roots[dim][k] = cexp(exp*k);
  116. starpu_vector_data_register(&plan->roots_handle[dim], 0, (uintptr_t) plan->roots[dim], plan->n[dim], sizeof(**plan->roots));
  117. #ifdef STARPU_USE_CUDA
  118. if (plan->n[dim] > 100000) {
  119. /* prefetch the big root array on GPUs */
  120. unsigned worker;
  121. unsigned nworkers = starpu_worker_get_count();
  122. for (worker = 0; worker < nworkers; worker++)
  123. {
  124. unsigned node = starpu_worker_get_memory_node(worker);
  125. if (starpu_worker_get_type(worker) == STARPU_CUDA_WORKER)
  126. starpu_data_prefetch_on_node(plan->roots_handle[dim], node, 0);
  127. }
  128. }
  129. #endif
  130. }
  131. }
  132. #include "starpufftx1d.c"
  133. #include "starpufftx2d.c"
  134. starpu_tag
  135. STARPUFFT(start)(STARPUFFT(plan) plan, void *_in, void *_out)
  136. {
  137. starpu_tag tag;
  138. int z;
  139. plan->in = _in;
  140. plan->out = _out;
  141. switch (plan->dim) {
  142. case 1: {
  143. switch (plan->type) {
  144. case C2C:
  145. starpu_vector_data_register(&plan->in_handle, 0, (uintptr_t) plan->in, plan->totsize, sizeof(STARPUFFT(complex)));
  146. for (z = 0; z < plan->totsize1; z++)
  147. plan->twist1_tasks[z]->buffers[0].handle = plan->in_handle;
  148. tag = STARPUFFT(start1dC2C)(plan);
  149. break;
  150. default:
  151. STARPU_ABORT();
  152. break;
  153. }
  154. break;
  155. }
  156. case 2:
  157. starpu_vector_data_register(&plan->in_handle, 0, (uintptr_t) plan->in, plan->totsize, sizeof(STARPUFFT(complex)));
  158. for (z = 0; z < plan->totsize1; z++)
  159. plan->twist1_tasks[z]->buffers[0].handle = plan->in_handle;
  160. tag = STARPUFFT(start2dC2C)(plan);
  161. break;
  162. default:
  163. STARPU_ABORT();
  164. break;
  165. }
  166. return tag;
  167. }
  168. void
  169. STARPUFFT(cleanup)(STARPUFFT(plan) plan)
  170. {
  171. starpu_data_unregister(plan->in_handle);
  172. }
  173. void
  174. STARPUFFT(execute)(STARPUFFT(plan) plan, void *in, void *out)
  175. {
  176. memset(task_per_worker, 0, sizeof(task_per_worker));
  177. memset(samples_per_worker, 0, sizeof(task_per_worker));
  178. gettimeofday(&start, NULL);
  179. starpu_tag tag = STARPUFFT(start)(plan, in, out);
  180. gettimeofday(&submit_tasks, NULL);
  181. starpu_tag_wait(tag);
  182. STARPUFFT(cleanup)(plan);
  183. gettimeofday(&end, NULL);
  184. }
  185. /* Destroy FFTW plans, unregister and free buffers, and free tags */
  186. void
  187. STARPUFFT(destroy_plan)(STARPUFFT(plan) plan)
  188. {
  189. int workerid, dim, i;
  190. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++) {
  191. switch (starpu_worker_get_type(workerid)) {
  192. case STARPU_CPU_WORKER:
  193. #ifdef STARPU_HAVE_FFTW
  194. _FFTW(free)(plan->plans[workerid].in1);
  195. _FFTW(free)(plan->plans[workerid].out1);
  196. _FFTW(destroy_plan)(plan->plans[workerid].plan1_cpu);
  197. _FFTW(free)(plan->plans[workerid].in2);
  198. _FFTW(free)(plan->plans[workerid].out2);
  199. _FFTW(destroy_plan)(plan->plans[workerid].plan2_cpu);
  200. #endif
  201. break;
  202. case STARPU_CUDA_WORKER:
  203. #ifdef STARPU_USE_CUDA
  204. /* FIXME: Can't deallocate */
  205. #endif
  206. break;
  207. default:
  208. STARPU_ABORT();
  209. break;
  210. }
  211. }
  212. for (i = 0; i < plan->totsize1; i++) {
  213. starpu_data_unregister(plan->twisted1_handle[i]);
  214. free(plan->twist1_tasks[i]);
  215. starpu_data_unregister(plan->fft1_handle[i]);
  216. free(plan->fft1_tasks[i]);
  217. }
  218. free(plan->twisted1_handle);
  219. free(plan->twist1_tasks);
  220. free(plan->fft1_handle);
  221. free(plan->fft1_tasks);
  222. free(plan->fft1_args);
  223. free(plan->join_task);
  224. for (i = 0; i < plan->totsize3; i++) {
  225. starpu_data_unregister(plan->twisted2_handle[i]);
  226. free(plan->twist2_tasks[i]);
  227. starpu_data_unregister(plan->fft2_handle[i]);
  228. free(plan->fft2_tasks[i]);
  229. free(plan->twist3_tasks[i]);
  230. }
  231. free(plan->end_task);
  232. free(plan->twisted2_handle);
  233. free(plan->twist2_tasks);
  234. free(plan->fft2_handle);
  235. free(plan->fft2_tasks);
  236. free(plan->twist3_tasks);
  237. free(plan->fft2_args);
  238. for (dim = 0; dim < plan->dim; dim++) {
  239. starpu_data_unregister(plan->roots_handle[dim]);
  240. free(plan->roots[dim]);
  241. }
  242. switch (plan->dim) {
  243. case 1:
  244. STARPUFFT(free_1d_tags)(plan);
  245. break;
  246. case 2:
  247. STARPUFFT(free_2d_tags)(plan);
  248. break;
  249. default:
  250. STARPU_ABORT();
  251. break;
  252. }
  253. free(plan->n);
  254. free(plan->n1);
  255. free(plan->n2);
  256. STARPUFFT(free)(plan->twisted1);
  257. STARPUFFT(free)(plan->fft1);
  258. STARPUFFT(free)(plan->twisted2);
  259. STARPUFFT(free)(plan->fft2);
  260. free(plan);
  261. }
  262. void *
  263. STARPUFFT(malloc)(size_t n)
  264. {
  265. #ifdef STARPU_USE_CUDA
  266. void *res;
  267. starpu_malloc(&res, n);
  268. return res;
  269. #else
  270. # ifdef STARPU_HAVE_FFTW
  271. return _FFTW(malloc)(n);
  272. # else
  273. return malloc(n);
  274. # endif
  275. #endif
  276. }
  277. void
  278. STARPUFFT(free)(void *p)
  279. {
  280. #ifdef STARPU_USE_CUDA
  281. /* TODO: FIXME */
  282. #else
  283. # ifdef STARPU_HAVE_FFTW
  284. _FFTW(free)(p);
  285. # else
  286. free(p);
  287. # endif
  288. #endif
  289. }
  290. void
  291. STARPUFFT(showstats)(FILE *out)
  292. {
  293. int worker;
  294. unsigned total;
  295. #define TIMING(begin,end) (double)((end.tv_sec - begin.tv_sec)*1000000 + (end.tv_usec - begin.tv_usec))
  296. #define MSTIMING(begin,end) (TIMING(begin,end)/1000.)
  297. double paratiming = TIMING(start,end);
  298. fprintf(out, "Tasks submission took %2.2f ms\n", MSTIMING(start,submit_tasks));
  299. fprintf(out, "Tasks termination took %2.2f ms\n", MSTIMING(submit_tasks,end));
  300. fprintf(out, "Total %2.2f ms\n", MSTIMING(start,end));
  301. for (worker = 0, total = 0; worker < starpu_worker_get_count(); worker++)
  302. total += task_per_worker[worker];
  303. for (worker = 0; worker < starpu_worker_get_count(); worker++)
  304. {
  305. if (task_per_worker[worker])
  306. {
  307. char name[32];
  308. starpu_worker_get_name(worker, name, sizeof(name));
  309. unsigned long bytes = sizeof(STARPUFFT(complex))*samples_per_worker[worker];
  310. 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);
  311. }
  312. }
  313. }