pi.c 4.6 KB

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