testx.c 8.1 KB

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