testx.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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,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. #include <complex.h>
  19. #include <math.h>
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <assert.h>
  23. #include <sys/time.h>
  24. #include <starpu.h>
  25. #include <starpu_config.h>
  26. #include "starpufft.h"
  27. #undef STARPU_USE_CUDA
  28. #ifdef STARPU_HAVE_FFTW
  29. #include <fftw3.h>
  30. #endif
  31. #ifdef STARPU_USE_CUDA
  32. #include <cufft.h>
  33. #endif
  34. #define SIGN (-1)
  35. /* #define SIGN (1) */
  36. #ifdef STARPU_HAVE_FFTW
  37. static void check_fftw(STARPUFFT(complex) *out, STARPUFFT(complex) *out_fftw, int size)
  38. {
  39. int i;
  40. double max = 0., tot = 0., norm = 0., normdiff = 0.;
  41. for (i = 0; i < size; i++)
  42. {
  43. double diff = cabs(out[i]-out_fftw[i]);
  44. double diff2 = diff * diff;
  45. double dsize = cabs(out_fftw[i]);
  46. double size2 = dsize * dsize;
  47. if (diff > max)
  48. max = diff;
  49. tot += diff;
  50. normdiff += diff2;
  51. norm += size2;
  52. }
  53. fprintf(stderr, "\nmaximum difference %g\n", max);
  54. fprintf(stderr, "average difference %g\n", tot / size);
  55. fprintf(stderr, "difference norm %g\n", sqrt(normdiff));
  56. double relmaxdiff = max / sqrt(norm);
  57. fprintf(stderr, "relative maximum difference %g\n", relmaxdiff);
  58. double relavgdiff = (tot / size) / sqrt(norm);
  59. fprintf(stderr, "relative average difference %g\n", relavgdiff);
  60. if (!strcmp(TYPE, "f") && (relmaxdiff > 1e-7 || relavgdiff > 1e-7)) {
  61. fprintf(stderr, "Failure: Difference too big (TYPE f)\n");
  62. exit(EXIT_FAILURE);
  63. }
  64. if (!strcmp(TYPE, "") && (relmaxdiff > 1e-16 || relavgdiff > 1e-16))
  65. {
  66. fprintf(stderr, "Failure: Difference too big\n");
  67. exit(EXIT_FAILURE);
  68. }
  69. }
  70. #endif
  71. #ifdef STARPU_USE_CUDA
  72. static void check_cuda(STARPUFFT(complex) *out, STARPUFFT(complex) *out_fftw, int size)
  73. {
  74. int i;
  75. double max = 0., tot = 0., norm = 0., normdiff = 0.;
  76. for (i = 0; i < size; i++)
  77. {
  78. double diff = cabs(out_cuda[i]-out_fftw[i]);
  79. double diff2 = diff * diff;
  80. double size = cabs(out_fftw[i]);
  81. double size2 = size * size;
  82. if (diff > max)
  83. max = diff;
  84. tot += diff;
  85. normdiff += diff2;
  86. norm += size2;
  87. }
  88. fprintf(stderr, "\nmaximum difference %g\n", max);
  89. fprintf(stderr, "average difference %g\n", tot / size);
  90. fprintf(stderr, "difference norm %g\n", sqrt(normdiff));
  91. double relmaxdiff = max / sqrt(norm);
  92. fprintf(stderr, "relative maximum difference %g\n", relmaxdiff);
  93. double relavgdiff = (tot / size) / sqrt(norm);
  94. fprintf(stderr, "relative average difference %g\n", relavgdiff);
  95. if (!strcmp(TYPE, "f") && (relmaxdiff > 1e-8 || relavgdiff > 1e-8))
  96. exit(EXIT_FAILURE);
  97. if (!strcmp(TYPE, "") && (relmaxdiff > 1e-16 || relavgdiff > 1e-16))
  98. exit(EXIT_FAILURE);
  99. }
  100. #endif
  101. int main(int argc, char *argv[])
  102. {
  103. int i, ret;
  104. int size;
  105. int n = 0, m = 0, p = 0;
  106. STARPUFFT(plan) plan;
  107. starpu_data_handle_t in_handle, out_handle;
  108. #ifdef STARPU_HAVE_FFTW
  109. _FFTW(plan) fftw_plan;
  110. #endif
  111. #ifdef STARPU_USE_CUDA
  112. cufftHandle cuda_plan;
  113. cudaError_t cures;
  114. #endif
  115. #if defined(STARPU_HAVE_FFTW) || defined(STARPU_USE_CUDA)
  116. struct timeval begin, end;
  117. double timing;
  118. size_t bytes;
  119. #endif
  120. struct starpu_conf conf;
  121. starpu_conf_init(&conf);
  122. /* FIXME: the testcase needs to be updated to properly support cuFFT */
  123. conf.ncuda = 0;
  124. ret = starpu_init(&conf);
  125. ret = starpu_init(NULL);
  126. if (ret == -ENODEV) return 77;
  127. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  128. if (argc == 1)
  129. {
  130. n = 42;
  131. /* 1D */
  132. size = n;
  133. }
  134. else if (argc == 2)
  135. {
  136. n = atoi(argv[1]);
  137. /* 1D */
  138. size = n;
  139. }
  140. else if (argc == 3)
  141. {
  142. n = atoi(argv[1]);
  143. m = atoi(argv[2]);
  144. /* 2D */
  145. size = n * m;
  146. }
  147. else if (argc == 4)
  148. {
  149. n = atoi(argv[1]);
  150. m = atoi(argv[2]);
  151. p = atoi(argv[3]);
  152. /* 3D */
  153. size = n * m * p;
  154. }
  155. else
  156. {
  157. assert(0);
  158. }
  159. #if defined(STARPU_HAVE_FFTW) || defined(STARPU_USE_CUDA)
  160. bytes = size * sizeof(STARPUFFT(complex));
  161. #endif
  162. STARPUFFT(complex) *in_orig = STARPUFFT(malloc)(size * sizeof(*in_orig));
  163. starpu_srand48(0);
  164. for (i = 0; i < size; i++)
  165. in_orig[i] = starpu_drand48() + I * starpu_drand48();
  166. STARPUFFT(complex) *in = STARPUFFT(malloc)(size * sizeof(*in));
  167. STARPUFFT(complex) *out = STARPUFFT(malloc)(size * sizeof(*out));
  168. #ifdef STARPU_HAVE_FFTW
  169. STARPUFFT(complex) *out_fftw = STARPUFFT(malloc)(size * sizeof(*out_fftw));
  170. #endif
  171. #ifdef STARPU_USE_CUDA
  172. STARPUFFT(complex) *out_cuda = STARPUFFT(malloc)(size * sizeof(*out_cuda));
  173. #endif
  174. if (argc <= 2)
  175. {
  176. plan = STARPUFFT(plan_dft_1d)(n, SIGN, 0);
  177. #ifdef STARPU_HAVE_FFTW
  178. fftw_plan = _FFTW(plan_dft_1d)(n, NULL, (void*) 1, SIGN, FFTW_ESTIMATE);
  179. #endif
  180. #ifdef STARPU_USE_CUDA
  181. if (cufftPlan1d(&cuda_plan, n, _CUFFT_C2C, 1) != CUFFT_SUCCESS)
  182. printf("erf\n");
  183. #endif
  184. }
  185. else if (argc == 3)
  186. {
  187. plan = STARPUFFT(plan_dft_2d)(n, m, SIGN, 0);
  188. #ifdef STARPU_HAVE_FFTW
  189. fftw_plan = _FFTW(plan_dft_2d)(n, m, NULL, (void*) 1, SIGN, FFTW_ESTIMATE);
  190. #endif
  191. #ifdef STARPU_USE_CUDA
  192. STARPU_ASSERT(cufftPlan2d(&cuda_plan, n, m, _CUFFT_C2C) == CUFFT_SUCCESS);
  193. #endif
  194. }
  195. else if (argc == 4)
  196. {
  197. plan = STARPUFFT(plan_dft_3d)(n, m, p, SIGN, 0);
  198. #ifdef STARPU_HAVE_FFTW
  199. fftw_plan = _FFTW(plan_dft_3d)(n, m, p, NULL, (void*) 1, SIGN, FFTW_ESTIMATE);
  200. #endif
  201. #ifdef STARPU_USE_CUDA
  202. STARPU_ASSERT(cufftPlan3d(&cuda_plan, n, m, p, _CUFFT_C2C) == CUFFT_SUCCESS);
  203. #endif
  204. }
  205. else
  206. {
  207. assert(0);
  208. }
  209. #ifdef STARPU_HAVE_FFTW
  210. memcpy(in, in_orig, size * sizeof(*in));
  211. gettimeofday(&begin, NULL);
  212. _FFTW(execute_dft)(fftw_plan, in, out_fftw);
  213. gettimeofday(&end, NULL);
  214. _FFTW(destroy_plan)(fftw_plan);
  215. timing = (double)((end.tv_sec - begin.tv_sec)*1000000 + (end.tv_usec - begin.tv_usec));
  216. printf("FFTW took %2.2f ms (%2.2f MB/s)\n\n", timing/1000, bytes/timing);
  217. #endif
  218. #ifdef STARPU_USE_CUDA
  219. memcpy(in, in_orig, size * sizeof(*in));
  220. gettimeofday(&begin, NULL);
  221. if (cufftExecC2C(cuda_plan, (cufftComplex*) in, (cufftComplex*) out_cuda, CUFFT_FORWARD) != CUFFT_SUCCESS)
  222. printf("erf2\n");
  223. if ((cures = cudaThreadSynchronize()) != cudaSuccess)
  224. STARPU_CUDA_REPORT_ERROR(cures);
  225. gettimeofday(&end, NULL);
  226. cufftDestroy(cuda_plan);
  227. timing = (double)((end.tv_sec - begin.tv_sec)*1000000 + (end.tv_usec - begin.tv_usec));
  228. printf("CUDA took %2.2f ms (%2.2f MB/s)\n\n", timing/1000, bytes/timing);
  229. #endif
  230. memcpy(in, in_orig, size * sizeof(*in));
  231. ret = STARPUFFT(execute)(plan, in, out);
  232. if (ret == -1) return 77;
  233. STARPUFFT(showstats)(stdout);
  234. #ifdef STARPU_HAVE_FFTW
  235. check_fftw(out, out_fftw, size);
  236. #endif
  237. #ifdef STARPU_USE_CUDA
  238. check_cuda(out, out_cuda, size);
  239. #endif
  240. #if 1
  241. memcpy(in, in_orig, size * sizeof(*in));
  242. starpu_vector_data_register(&in_handle, STARPU_MAIN_RAM, (uintptr_t) in, size, sizeof(*in));
  243. starpu_vector_data_register(&out_handle, STARPU_MAIN_RAM, (uintptr_t) out, size, sizeof(*out));
  244. ret = STARPUFFT(execute_handle)(plan, in_handle, out_handle);
  245. if (ret == -1) return 77;
  246. starpu_data_unregister(in_handle);
  247. starpu_data_unregister(out_handle);
  248. #ifdef STARPU_HAVE_FFTW
  249. check_fftw(out, out_fftw, size);
  250. #endif
  251. #ifdef STARPU_USE_CUDA
  252. check_cuda(out, out_cuda, size);
  253. #endif
  254. #endif
  255. STARPUFFT(showstats)(stdout);
  256. STARPUFFT(destroy_plan)(plan);
  257. printf("\n");
  258. #if 0
  259. for (i = 0; i < 16; i++)
  260. printf("(%f,%f) ", cimag(in[i]), creal(in[i]));
  261. printf("\n\n");
  262. for (i = 0; i < 16; i++)
  263. printf("(%f,%f) ", cimag(out[i]), creal(out[i]));
  264. printf("\n\n");
  265. #ifdef STARPU_HAVE_FFTW
  266. for (i = 0; i < 16; i++)
  267. printf("(%f,%f) ", cimag(out_fftw[i]), creal(out_fftw[i]));
  268. printf("\n\n");
  269. #endif
  270. #endif
  271. STARPUFFT(free)(in_orig);
  272. STARPUFFT(free)(in);
  273. STARPUFFT(free)(out);
  274. #ifdef STARPU_HAVE_FFTW
  275. STARPUFFT(free)(out_fftw);
  276. #endif
  277. #ifdef STARPU_USE_CUDA
  278. free(out_cuda);
  279. #endif
  280. starpu_shutdown();
  281. return EXIT_SUCCESS;
  282. }