pi.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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 "SobolQRNG/sobol.h"
  17. #include "pi.h"
  18. #include <sys/time.h>
  19. #ifdef STARPU_USE_CUDA
  20. void cuda_kernel(void **descr, void *cl_arg);
  21. #endif
  22. /* default value */
  23. static unsigned ntasks = 1024;
  24. static void cpu_kernel(void *descr[], void *cl_arg)
  25. {
  26. unsigned *directions = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  27. unsigned nx = NSHOT_PER_TASK;
  28. TYPE *random_numbers = malloc(2*nx*sizeof(TYPE));
  29. sobolCPU(2*nx/n_dimensions, n_dimensions, directions, random_numbers);
  30. TYPE *random_numbers_x = &random_numbers[0];
  31. TYPE *random_numbers_y = &random_numbers[nx];
  32. unsigned current_cnt = 0;
  33. unsigned i;
  34. for (i = 0; i < nx; i++)
  35. {
  36. TYPE x = random_numbers_x[i];
  37. TYPE y = random_numbers_y[i];
  38. TYPE dist = (x*x + y*y);
  39. unsigned success = (dist <= 1.0);
  40. current_cnt += success;
  41. }
  42. unsigned *cnt = (unsigned *)STARPU_VECTOR_GET_PTR(descr[1]);
  43. *cnt = current_cnt;
  44. free(random_numbers);
  45. }
  46. static void parse_args(int argc, char **argv)
  47. {
  48. int i;
  49. for (i = 1; i < argc; i++) {
  50. if (strcmp(argv[i], "-ntasks") == 0) {
  51. char *argptr;
  52. ntasks = strtol(argv[++i], &argptr, 10);
  53. }
  54. }
  55. }
  56. int main(int argc, char **argv)
  57. {
  58. unsigned i;
  59. parse_args(argc, argv);
  60. starpu_init(NULL);
  61. /* Initialize the random number generator */
  62. unsigned *sobol_qrng_directions = malloc(n_dimensions*n_directions*sizeof(unsigned));
  63. STARPU_ASSERT(sobol_qrng_directions);
  64. initSobolDirectionVectors(n_dimensions, sobol_qrng_directions);
  65. /* Any worker may use that array now */
  66. starpu_data_handle sobol_qrng_direction_handle;
  67. starpu_vector_data_register(&sobol_qrng_direction_handle, 0,
  68. (uintptr_t)sobol_qrng_directions, n_dimensions*n_directions, sizeof(unsigned));
  69. unsigned *cnt_array = malloc(ntasks*sizeof(unsigned));
  70. STARPU_ASSERT(cnt_array);
  71. starpu_data_handle cnt_array_handle;
  72. starpu_vector_data_register(&cnt_array_handle, 0, (uintptr_t)cnt_array, ntasks, sizeof(unsigned));
  73. /* Use a write-through policy : when the data is modified on an
  74. * accelerator, we know that it will only be modified once and be
  75. * accessed by the CPU later on */
  76. starpu_data_set_wt_mask(cnt_array_handle, (1<<0));
  77. struct starpu_data_filter f = {
  78. .filter_func = starpu_block_filter_func_vector,
  79. .nchildren = ntasks,
  80. .get_nchildren = NULL,
  81. .get_child_ops = NULL
  82. };
  83. starpu_data_partition(cnt_array_handle, &f);
  84. static struct starpu_perfmodel_t model = {
  85. .type = STARPU_HISTORY_BASED,
  86. .symbol = "monte_carlo_pi"
  87. };
  88. struct starpu_codelet_t cl = {
  89. .where = STARPU_CPU|STARPU_CUDA,
  90. .cpu_func = cpu_kernel,
  91. #ifdef STARPU_USE_CUDA
  92. .cuda_func = cuda_kernel,
  93. #endif
  94. .nbuffers = 2,
  95. .model = &model
  96. };
  97. struct timeval start;
  98. struct timeval end;
  99. gettimeofday(&start, NULL);
  100. for (i = 0; i < ntasks; i++)
  101. {
  102. struct starpu_task *task = starpu_task_create();
  103. task->cl = &cl;
  104. STARPU_ASSERT(starpu_data_get_sub_data(cnt_array_handle, 1, i));
  105. task->buffers[0].handle = sobol_qrng_direction_handle;
  106. task->buffers[0].mode = STARPU_R;
  107. task->buffers[1].handle = starpu_data_get_sub_data(cnt_array_handle, 1, i);
  108. task->buffers[1].mode = STARPU_W;
  109. int ret = starpu_task_submit(task);
  110. STARPU_ASSERT(!ret);
  111. }
  112. starpu_task_wait_for_all();
  113. /* Get the cnt_array back in main memory */
  114. starpu_data_unpartition(cnt_array_handle, 0);
  115. starpu_data_acquire(cnt_array_handle, STARPU_RW);
  116. /* Count the total number of entries */
  117. unsigned long total_cnt = 0;
  118. for (i = 0; i < ntasks; i++)
  119. total_cnt += cnt_array[i];
  120. gettimeofday(&end, NULL);
  121. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  122. unsigned long total_shot_cnt = ntasks * NSHOT_PER_TASK;
  123. /* Total surface : Pi * r^ 2 = Pi*1^2, total square surface : 2^2 = 4, probability to impact the disk: pi/4 */
  124. fprintf(stderr, "Pi approximation : %f (%ld / %ld)\n", ((TYPE)total_cnt*4)/(total_shot_cnt), total_cnt, total_shot_cnt);
  125. fprintf(stderr, "Total time : %f ms\n", timing/1000.0);
  126. fprintf(stderr, "Speed : %f GShot/s\n", total_shot_cnt/(1e3*timing));
  127. starpu_data_release(cnt_array_handle);
  128. starpu_display_codelet_stats(&cl);
  129. starpu_shutdown();
  130. return 0;
  131. }