testx.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 CNRS
  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. struct starpu_conf conf;
  120. starpu_conf_init(&conf);
  121. /* FIXME: the testcase needs to be updated to properly support cuFFT */
  122. conf.ncuda = 0;
  123. ret = starpu_init(&conf);
  124. ret = starpu_init(NULL);
  125. if (ret == -ENODEV) return 77;
  126. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  127. if (argc == 1)
  128. {
  129. n = 42;
  130. /* 1D */
  131. size = n;
  132. }
  133. else if (argc == 2)
  134. {
  135. n = atoi(argv[1]);
  136. /* 1D */
  137. size = n;
  138. }
  139. else if (argc == 3)
  140. {
  141. n = atoi(argv[1]);
  142. m = atoi(argv[2]);
  143. /* 2D */
  144. size = n * m;
  145. }
  146. else
  147. {
  148. assert(0);
  149. }
  150. #if defined(STARPU_HAVE_FFTW) || defined(STARPU_USE_CUDA)
  151. bytes = size * sizeof(STARPUFFT(complex));
  152. #endif
  153. STARPUFFT(complex) *in = STARPUFFT(malloc)(size * sizeof(*in));
  154. starpu_srand48(0);
  155. for (i = 0; i < size; i++)
  156. in[i] = starpu_drand48() + I * starpu_drand48();
  157. STARPUFFT(complex) *out = STARPUFFT(malloc)(size * sizeof(*out));
  158. #ifdef STARPU_HAVE_FFTW
  159. STARPUFFT(complex) *out_fftw = STARPUFFT(malloc)(size * sizeof(*out_fftw));
  160. #endif
  161. #ifdef STARPU_USE_CUDA
  162. STARPUFFT(complex) *out_cuda = STARPUFFT(malloc)(size * sizeof(*out_cuda));
  163. #endif
  164. if (argc <= 2)
  165. {
  166. plan = STARPUFFT(plan_dft_1d)(n, SIGN, 0);
  167. #ifdef STARPU_HAVE_FFTW
  168. fftw_plan = _FFTW(plan_dft_1d)(n, NULL, (void*) 1, SIGN, FFTW_ESTIMATE);
  169. #endif
  170. #ifdef STARPU_USE_CUDA
  171. if (cufftPlan1d(&cuda_plan, n, _CUFFT_C2C, 1) != CUFFT_SUCCESS)
  172. printf("erf\n");
  173. #endif
  174. }
  175. else if (argc == 3)
  176. {
  177. plan = STARPUFFT(plan_dft_2d)(n, m, SIGN, 0);
  178. #ifdef STARPU_HAVE_FFTW
  179. fftw_plan = _FFTW(plan_dft_2d)(n, m, NULL, (void*) 1, SIGN, FFTW_ESTIMATE);
  180. #endif
  181. #ifdef STARPU_USE_CUDA
  182. STARPU_ASSERT(cufftPlan2d(&cuda_plan, n, m, _CUFFT_C2C) == CUFFT_SUCCESS);
  183. #endif
  184. }
  185. else
  186. {
  187. assert(0);
  188. }
  189. #ifdef STARPU_HAVE_FFTW
  190. gettimeofday(&begin, NULL);
  191. _FFTW(execute_dft)(fftw_plan, in, out_fftw);
  192. gettimeofday(&end, NULL);
  193. _FFTW(destroy_plan)(fftw_plan);
  194. timing = (double)((end.tv_sec - begin.tv_sec)*1000000 + (end.tv_usec - begin.tv_usec));
  195. printf("FFTW took %2.2f ms (%2.2f MB/s)\n\n", timing/1000, bytes/timing);
  196. #endif
  197. #ifdef STARPU_USE_CUDA
  198. gettimeofday(&begin, NULL);
  199. if (cufftExecC2C(cuda_plan, (cufftComplex*) in, (cufftComplex*) out_cuda, CUFFT_FORWARD) != CUFFT_SUCCESS)
  200. printf("erf2\n");
  201. if ((cures = cudaThreadSynchronize()) != cudaSuccess)
  202. STARPU_CUDA_REPORT_ERROR(cures);
  203. gettimeofday(&end, NULL);
  204. cufftDestroy(cuda_plan);
  205. timing = (double)((end.tv_sec - begin.tv_sec)*1000000 + (end.tv_usec - begin.tv_usec));
  206. printf("CUDA took %2.2f ms (%2.2f MB/s)\n\n", timing/1000, bytes/timing);
  207. #endif
  208. ret = STARPUFFT(execute)(plan, in, out);
  209. if (ret == -1) return 77;
  210. STARPUFFT(showstats)(stdout);
  211. #ifdef STARPU_HAVE_FFTW
  212. check_fftw(out, out_fftw, size);
  213. #endif
  214. #ifdef STARPU_USE_CUDA
  215. check_cuda(out, out_cuda, size);
  216. #endif
  217. #if 1
  218. starpu_vector_data_register(&in_handle, STARPU_MAIN_RAM, (uintptr_t) in, size, sizeof(*in));
  219. starpu_vector_data_register(&out_handle, STARPU_MAIN_RAM, (uintptr_t) out, size, sizeof(*out));
  220. ret = STARPUFFT(execute_handle)(plan, in_handle, out_handle);
  221. if (ret == -1) return 77;
  222. starpu_data_unregister(in_handle);
  223. starpu_data_unregister(out_handle);
  224. #ifdef STARPU_HAVE_FFTW
  225. check_fftw(out, out_fftw, size);
  226. #endif
  227. #ifdef STARPU_USE_CUDA
  228. check_cuda(out, out_cuda, size);
  229. #endif
  230. #endif
  231. STARPUFFT(showstats)(stdout);
  232. STARPUFFT(destroy_plan)(plan);
  233. printf("\n");
  234. #if 0
  235. for (i = 0; i < 16; i++)
  236. printf("(%f,%f) ", cimag(in[i]), creal(in[i]));
  237. printf("\n\n");
  238. for (i = 0; i < 16; i++)
  239. printf("(%f,%f) ", cimag(out[i]), creal(out[i]));
  240. printf("\n\n");
  241. #ifdef STARPU_HAVE_FFTW
  242. for (i = 0; i < 16; i++)
  243. printf("(%f,%f) ", cimag(out_fftw[i]), creal(out_fftw[i]));
  244. printf("\n\n");
  245. #endif
  246. #endif
  247. STARPUFFT(free)(in);
  248. STARPUFFT(free)(out);
  249. #ifdef STARPU_HAVE_FFTW
  250. STARPUFFT(free)(out_fftw);
  251. #endif
  252. #ifdef STARPU_USE_CUDA
  253. free(out_cuda);
  254. #endif
  255. starpu_shutdown();
  256. return EXIT_SUCCESS;
  257. }