testx.c 5.9 KB

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