testx.c 7.2 KB

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