pi_redux.c 9.8 KB

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