testx.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 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 size = cabs(out_fftw[i]);
  45. double size2 = size * size;
  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-8 || relavgdiff > 1e-8))
  60. exit(EXIT_FAILURE);
  61. if (!strcmp(TYPE, "") && (relmaxdiff > 1e-16 || relavgdiff > 1e-16))
  62. exit(EXIT_FAILURE);
  63. }
  64. #endif
  65. #ifdef STARPU_USE_CUDA
  66. static void check_cuda(STARPUFFT(complex) *out, STARPUFFT(complex) *out_fftw, int size)
  67. {
  68. int i;
  69. double max = 0., tot = 0., norm = 0., normdiff = 0.;
  70. for (i = 0; i < size; i++)
  71. {
  72. double diff = cabs(out_cuda[i]-out_fftw[i]);
  73. double diff2 = diff * diff;
  74. double size = cabs(out_fftw[i]);
  75. double size2 = size * size;
  76. if (diff > max)
  77. max = diff;
  78. tot += diff;
  79. normdiff += diff2;
  80. norm += size2;
  81. }
  82. fprintf(stderr, "\nmaximum difference %g\n", max);
  83. fprintf(stderr, "average difference %g\n", tot / size);
  84. fprintf(stderr, "difference norm %g\n", sqrt(normdiff));
  85. double relmaxdiff = max / sqrt(norm);
  86. fprintf(stderr, "relative maximum difference %g\n", relmaxdiff);
  87. double relavgdiff = (tot / size) / sqrt(norm);
  88. fprintf(stderr, "relative average difference %g\n", relavgdiff);
  89. if (!strcmp(TYPE, "f") && (relmaxdiff > 1e-8 || relavgdiff > 1e-8))
  90. exit(EXIT_FAILURE);
  91. if (!strcmp(TYPE, "") && (relmaxdiff > 1e-16 || relavgdiff > 1e-16))
  92. exit(EXIT_FAILURE);
  93. }
  94. #endif
  95. int main(int argc, char *argv[])
  96. {
  97. int i, ret;
  98. struct timeval begin, end;
  99. int size;
  100. size_t bytes;
  101. int n = 0, m = 0;
  102. STARPUFFT(plan) plan;
  103. starpu_data_handle_t in_handle, out_handle;
  104. #ifdef STARPU_HAVE_FFTW
  105. _FFTW(plan) fftw_plan;
  106. #endif
  107. #ifdef STARPU_USE_CUDA
  108. cufftHandle cuda_plan;
  109. cudaError_t cures;
  110. #endif
  111. double timing;
  112. if (argc < 2 || argc > 3)
  113. {
  114. fprintf(stderr,"need one or two size of vector\n");
  115. exit(EXIT_FAILURE);
  116. }
  117. ret = starpu_init(NULL);
  118. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  119. if (argc == 2)
  120. {
  121. n = atoi(argv[1]);
  122. /* 1D */
  123. size = n;
  124. }
  125. else if (argc == 3)
  126. {
  127. n = atoi(argv[1]);
  128. m = atoi(argv[2]);
  129. /* 2D */
  130. size = n * m;
  131. }
  132. else
  133. {
  134. assert(0);
  135. }
  136. bytes = size * sizeof(STARPUFFT(complex));
  137. STARPUFFT(complex) *in = STARPUFFT(malloc)(size * sizeof(*in));
  138. starpu_srand48(0);
  139. for (i = 0; i < size; i++)
  140. in[i] = starpu_drand48() + I * starpu_drand48();
  141. STARPUFFT(complex) *out = STARPUFFT(malloc)(size * sizeof(*out));
  142. #ifdef STARPU_HAVE_FFTW
  143. STARPUFFT(complex) *out_fftw = STARPUFFT(malloc)(size * sizeof(*out_fftw));
  144. #endif
  145. #ifdef STARPU_USE_CUDA
  146. STARPUFFT(complex) *out_cuda = STARPUFFT(malloc)(size * sizeof(*out_cuda));
  147. #endif
  148. if (argc == 2)
  149. {
  150. plan = STARPUFFT(plan_dft_1d)(n, SIGN, 0);
  151. #ifdef STARPU_HAVE_FFTW
  152. fftw_plan = _FFTW(plan_dft_1d)(n, NULL, (void*) 1, SIGN, FFTW_ESTIMATE);
  153. #endif
  154. #ifdef STARPU_USE_CUDA
  155. if (cufftPlan1d(&cuda_plan, n, _CUFFT_C2C, 1) != CUFFT_SUCCESS)
  156. printf("erf\n");
  157. #endif
  158. }
  159. else if (argc == 3)
  160. {
  161. plan = STARPUFFT(plan_dft_2d)(n, m, SIGN, 0);
  162. #ifdef STARPU_HAVE_FFTW
  163. fftw_plan = _FFTW(plan_dft_2d)(n, m, NULL, (void*) 1, SIGN, FFTW_ESTIMATE);
  164. #endif
  165. #ifdef STARPU_USE_CUDA
  166. STARPU_ASSERT(cufftPlan2d(&cuda_plan, n, m, _CUFFT_C2C) == CUFFT_SUCCESS);
  167. #endif
  168. }
  169. else
  170. {
  171. assert(0);
  172. }
  173. #ifdef STARPU_HAVE_FFTW
  174. gettimeofday(&begin, NULL);
  175. _FFTW(execute_dft)(fftw_plan, in, out_fftw);
  176. gettimeofday(&end, NULL);
  177. _FFTW(destroy_plan)(fftw_plan);
  178. timing = (double)((end.tv_sec - begin.tv_sec)*1000000 + (end.tv_usec - begin.tv_usec));
  179. printf("FFTW took %2.2f ms (%2.2f MB/s)\n\n", timing/1000, bytes/timing);
  180. #endif
  181. #ifdef STARPU_USE_CUDA
  182. gettimeofday(&begin, NULL);
  183. if (cufftExecC2C(cuda_plan, (cufftComplex*) in, (cufftComplex*) out_cuda, CUFFT_FORWARD) != CUFFT_SUCCESS)
  184. printf("erf2\n");
  185. if ((cures = cudaThreadSynchronize()) != cudaSuccess)
  186. STARPU_CUDA_REPORT_ERROR(cures);
  187. gettimeofday(&end, NULL);
  188. cufftDestroy(cuda_plan);
  189. timing = (double)((end.tv_sec - begin.tv_sec)*1000000 + (end.tv_usec - begin.tv_usec));
  190. printf("CUDA took %2.2f ms (%2.2f MB/s)\n\n", timing/1000, bytes/timing);
  191. #endif
  192. STARPUFFT(execute)(plan, in, out);
  193. STARPUFFT(showstats)(stdout);
  194. #ifdef STARPU_HAVE_FFTW
  195. check_fftw(out, out_fftw, size);
  196. #endif
  197. #ifdef STARPU_USE_CUDA
  198. check_cuda(out, out_cuda, size);
  199. #endif
  200. #if 1
  201. starpu_vector_data_register(&in_handle, 0, (uintptr_t) in, size, sizeof(*in));
  202. starpu_vector_data_register(&out_handle, 0, (uintptr_t) out, size, sizeof(*out));
  203. STARPUFFT(execute_handle)(plan, in_handle, out_handle);
  204. starpu_data_unregister(in_handle);
  205. starpu_data_unregister(out_handle);
  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. #endif
  213. STARPUFFT(showstats)(stdout);
  214. STARPUFFT(destroy_plan)(plan);
  215. printf("\n");
  216. #if 0
  217. for (i = 0; i < 16; i++)
  218. printf("(%f,%f) ", cimag(in[i]), creal(in[i]));
  219. printf("\n\n");
  220. for (i = 0; i < 16; i++)
  221. printf("(%f,%f) ", cimag(out[i]), creal(out[i]));
  222. printf("\n\n");
  223. #ifdef STARPU_HAVE_FFTW
  224. for (i = 0; i < 16; i++)
  225. printf("(%f,%f) ", cimag(out_fftw[i]), creal(out_fftw[i]));
  226. printf("\n\n");
  227. #endif
  228. #endif
  229. STARPUFFT(free)(in);
  230. STARPUFFT(free)(out);
  231. #ifdef STARPU_HAVE_FFTW
  232. STARPUFFT(free)(out_fftw);
  233. #endif
  234. #ifdef STARPU_USE_CUDA
  235. free(out_cuda);
  236. #endif
  237. starpu_shutdown();
  238. return EXIT_SUCCESS;
  239. }