pi_redux.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011 Université de Bordeaux 1
  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 <starpu.h>
  17. #include <stdlib.h>
  18. #include <sys/time.h>
  19. #include <starpu_config.h>
  20. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  21. #define PI 3.14159265358979323846
  22. #if defined(STARPU_USE_CUDA) && !defined(STARPU_HAVE_CURAND)
  23. #warning CURAND is required to run that example on CUDA devices
  24. #endif
  25. #ifdef STARPU_HAVE_CURAND
  26. #include <cuda.h>
  27. #include <curand.h>
  28. #include <starpu_cuda.h>
  29. #endif
  30. #define NSHOT_PER_TASK (1024*1024)
  31. /* default value */
  32. static unsigned long ntasks = 1024;
  33. /*
  34. * Initialization of the Random Number Generators (RNG)
  35. */
  36. #ifdef STARPU_HAVE_CURAND
  37. /* RNG for the CURAND library */
  38. static curandGenerator_t curandgens[STARPU_NMAXWORKERS];
  39. #endif
  40. /* state for the erand48 function */
  41. static unsigned short xsubi[3*STARPU_NMAXWORKERS];
  42. /* Function to initialize the random number generator in the current worker */
  43. static void init_rng(void *arg __attribute__((unused)))
  44. {
  45. #ifdef STARPU_HAVE_CURAND
  46. curandStatus_t res;
  47. #endif
  48. int workerid = starpu_worker_get_id();
  49. switch (starpu_worker_get_type(workerid)) {
  50. case STARPU_CPU_WORKER:
  51. /* create a seed */
  52. xsubi[0 + 3*workerid] = (unsigned short)workerid;
  53. xsubi[1 + 3*workerid] = (unsigned short)workerid;
  54. xsubi[2 + 3*workerid] = (unsigned short)workerid;
  55. break;
  56. #ifdef STARPU_HAVE_CURAND
  57. case STARPU_CUDA_WORKER:
  58. /* Create a RNG */
  59. res = curandCreateGenerator(&curandgens[workerid],
  60. CURAND_RNG_PSEUDO_DEFAULT);
  61. STARPU_ASSERT(res == CURAND_STATUS_SUCCESS);
  62. /* Seed it with worker's id */
  63. res = curandSetPseudoRandomGeneratorSeed(curandgens[workerid],
  64. (unsigned long long)workerid);
  65. STARPU_ASSERT(res == CURAND_STATUS_SUCCESS);
  66. break;
  67. #endif
  68. default:
  69. STARPU_ABORT();
  70. break;
  71. }
  72. }
  73. static void parse_args(int argc, char **argv)
  74. {
  75. int i;
  76. for (i = 1; i < argc; i++) {
  77. if (strcmp(argv[i], "-ntasks") == 0) {
  78. char *argptr;
  79. ntasks = strtol(argv[++i], &argptr, 10);
  80. }
  81. }
  82. }
  83. /*
  84. * Monte-carlo kernel
  85. */
  86. static void pi_func_cpu(void *descr[], void *cl_arg __attribute__ ((unused)))
  87. {
  88. int workerid = starpu_worker_get_id();
  89. unsigned short *worker_xsub;
  90. worker_xsub = &xsubi[3*workerid];
  91. unsigned long local_cnt = 0;
  92. /* Fill the scratchpad with random numbers */
  93. int i;
  94. for (i = 0; i < NSHOT_PER_TASK; i++)
  95. {
  96. float x = (float)(2.0*starpu_erand48(worker_xsub) - 1.0);
  97. float y = (float)(2.0*starpu_erand48(worker_xsub) - 1.0);
  98. float dist = x*x + y*y;
  99. if (dist < 1.0)
  100. local_cnt++;
  101. }
  102. /* Put the contribution of that task into the counter */
  103. unsigned long *cnt = (unsigned long *)STARPU_VARIABLE_GET_PTR(descr[1]);
  104. *cnt = *cnt + local_cnt;
  105. }
  106. extern void pi_redux_cuda_kernel(float *x, float *y, unsigned n, unsigned long *shot_cnt);
  107. #ifdef STARPU_HAVE_CURAND
  108. static void pi_func_cuda(void *descr[], void *cl_arg __attribute__ ((unused)))
  109. {
  110. cudaError_t cures;
  111. curandStatus_t res;
  112. int workerid = starpu_worker_get_id();
  113. /* CURAND is a bit silly: it assumes that any error is fatal. Calling
  114. * cudaGetLastError resets the last error value. */
  115. cures = cudaGetLastError();
  116. /* if (cures)
  117. STARPU_CUDA_REPORT_ERROR(cures); */
  118. /* Fill the scratchpad with random numbers. Note that both x and y
  119. * arrays are in stored the same vector. */
  120. float *scratchpad_xy = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  121. res = curandGenerateUniform(curandgens[workerid], scratchpad_xy, 2*NSHOT_PER_TASK);
  122. STARPU_ASSERT(res == CURAND_STATUS_SUCCESS);
  123. float *x = &scratchpad_xy[0];
  124. float *y = &scratchpad_xy[NSHOT_PER_TASK];
  125. unsigned long *shot_cnt = (unsigned long *)STARPU_VARIABLE_GET_PTR(descr[1]);
  126. pi_redux_cuda_kernel(x, y, NSHOT_PER_TASK, shot_cnt);
  127. }
  128. #endif
  129. static struct starpu_codelet_t pi_cl = {
  130. .where =
  131. #ifdef STARPU_HAVE_CURAND
  132. STARPU_CUDA|
  133. #endif
  134. STARPU_CPU,
  135. .cpu_func = pi_func_cpu,
  136. #ifdef STARPU_HAVE_CURAND
  137. .cuda_func = pi_func_cuda,
  138. #endif
  139. .nbuffers = 2,
  140. .model = NULL
  141. };
  142. /*
  143. * Codelets to implement reduction
  144. */
  145. static void init_cpu_func(void *descr[], void *cl_arg)
  146. {
  147. unsigned long *val = (unsigned long *)STARPU_VARIABLE_GET_PTR(descr[0]);
  148. *val = 0;
  149. }
  150. #ifdef STARPU_HAVE_CURAND
  151. static void init_cuda_func(void *descr[], void *cl_arg)
  152. {
  153. unsigned long *val = (unsigned long *)STARPU_VARIABLE_GET_PTR(descr[0]);
  154. cudaMemset(val, 0, sizeof(unsigned long));
  155. cudaThreadSynchronize();
  156. }
  157. #endif
  158. static struct starpu_codelet_t init_codelet = {
  159. .where =
  160. #ifdef STARPU_HAVE_CURAND
  161. STARPU_CUDA|
  162. #endif
  163. STARPU_CPU,
  164. .cpu_func = init_cpu_func,
  165. #ifdef STARPU_HAVE_CURAND
  166. .cuda_func = init_cuda_func,
  167. #endif
  168. .nbuffers = 1
  169. };
  170. #ifdef STARPU_HAVE_CURAND
  171. /* Dummy implementation of the addition of two unsigned longs in CUDA */
  172. static void redux_cuda_func(void *descr[], void *cl_arg)
  173. {
  174. unsigned long *d_a = (unsigned long *)STARPU_VARIABLE_GET_PTR(descr[0]);
  175. unsigned long *d_b = (unsigned long *)STARPU_VARIABLE_GET_PTR(descr[1]);
  176. unsigned long h_a, h_b;
  177. cudaMemcpy(&h_a, d_a, sizeof(h_a), cudaMemcpyDeviceToHost);
  178. cudaMemcpy(&h_b, d_b, sizeof(h_b), cudaMemcpyDeviceToHost);
  179. h_a += h_b;
  180. cudaMemcpy(d_a, &h_a, sizeof(h_a), cudaMemcpyHostToDevice);
  181. };
  182. #endif
  183. static void redux_cpu_func(void *descr[], void *cl_arg)
  184. {
  185. unsigned long *a = (unsigned long *)STARPU_VARIABLE_GET_PTR(descr[0]);
  186. unsigned long *b = (unsigned long *)STARPU_VARIABLE_GET_PTR(descr[1]);
  187. *a = *a + *b;
  188. };
  189. static struct starpu_codelet_t redux_codelet = {
  190. .where =
  191. #ifdef STARPU_HAVE_CURAND
  192. STARPU_CUDA|
  193. #endif
  194. STARPU_CPU,
  195. .cpu_func = redux_cpu_func,
  196. #ifdef STARPU_HAVE_CURAND
  197. .cuda_func = redux_cuda_func,
  198. #endif
  199. .nbuffers = 2
  200. };
  201. /*
  202. * Main program
  203. */
  204. int main(int argc, char **argv)
  205. {
  206. unsigned i;
  207. parse_args(argc, argv);
  208. starpu_init(NULL);
  209. /* Launch a Random Number Generator (RNG) on each worker */
  210. starpu_execute_on_each_worker(init_rng, NULL, STARPU_CPU|STARPU_CUDA);
  211. /* Create a scratchpad data */
  212. starpu_data_handle xy_scratchpad_handle;
  213. starpu_vector_data_register(&xy_scratchpad_handle, -1, (uintptr_t)NULL,
  214. 2*NSHOT_PER_TASK, sizeof(float));
  215. /* Create a variable that will be used to count the number of shots
  216. * that actually hit the unit circle when shooting randomly in
  217. * [-1,1]^2. */
  218. unsigned long shot_cnt = 0;
  219. starpu_data_handle shot_cnt_handle;
  220. starpu_variable_data_register(&shot_cnt_handle, 0,
  221. (uintptr_t)&shot_cnt, sizeof(shot_cnt));
  222. starpu_data_set_reduction_methods(shot_cnt_handle,
  223. &redux_codelet, &init_codelet);
  224. struct timeval start;
  225. struct timeval end;
  226. gettimeofday(&start, NULL);
  227. for (i = 0; i < ntasks; i++)
  228. {
  229. struct starpu_task *task = starpu_task_create();
  230. task->cl = &pi_cl;
  231. task->buffers[0].handle = xy_scratchpad_handle;
  232. task->buffers[0].mode = STARPU_SCRATCH;
  233. task->buffers[1].handle = shot_cnt_handle;
  234. task->buffers[1].mode = STARPU_REDUX;
  235. int ret = starpu_task_submit(task);
  236. STARPU_ASSERT(!ret);
  237. }
  238. starpu_data_acquire(shot_cnt_handle, STARPU_R);
  239. gettimeofday(&end, NULL);
  240. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  241. /* Total surface : Pi * r^ 2 = Pi*1^2, total square surface : 2^2 = 4,
  242. * probability to impact the disk: pi/4 */
  243. unsigned long total = ntasks*NSHOT_PER_TASK;
  244. double pi_approx = ((double)shot_cnt*4.0)/total;
  245. starpu_data_release(shot_cnt_handle);
  246. FPRINTF(stderr, "Pi approximation : %lf (%ld / %ld)\n", pi_approx, shot_cnt, total);
  247. FPRINTF(stderr, "Error %le \n", pi_approx - PI);
  248. FPRINTF(stderr, "Total time : %f ms\n", timing/1000.0);
  249. FPRINTF(stderr, "Speed : %f GShot/s\n", total/(1e3*timing));
  250. starpu_data_unregister(shot_cnt_handle);
  251. starpu_shutdown();
  252. if (abs(pi_approx - PI) > 1.0)
  253. return 1;
  254. return 0;
  255. }