pi.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "pi.h"
  17. #ifdef STARPU_USE_CUDA
  18. void cuda_kernel(void **descr, void *cl_arg);
  19. #endif
  20. static void cpu_kernel(void *descr[], void *cl_arg)
  21. {
  22. TYPE *random_numbers_x = (TYPE *)STARPU_GET_VECTOR_PTR(descr[0]);
  23. TYPE *random_numbers_y = (TYPE *)STARPU_GET_VECTOR_PTR(descr[1]);
  24. unsigned nx = STARPU_GET_VECTOR_NX(descr[0]);
  25. unsigned current_cnt = 0;
  26. unsigned i;
  27. for (i = 0; i < nx; i++)
  28. {
  29. TYPE x = random_numbers_x[i];
  30. TYPE y = random_numbers_y[i];
  31. TYPE dist = (x*x + y*y);
  32. unsigned success = (dist <= 1.0);
  33. current_cnt += success;
  34. }
  35. unsigned *cnt = (unsigned *)STARPU_GET_VECTOR_PTR(descr[2]);
  36. *cnt = current_cnt;
  37. }
  38. int main(int argc, char **argv)
  39. {
  40. unsigned i;
  41. starpu_init(NULL);
  42. TYPE *random_array_x;
  43. starpu_malloc_pinned_if_possible((void **)&random_array_x, SIZE*sizeof(TYPE));
  44. STARPU_ASSERT(random_array_x);
  45. TYPE *random_array_y;
  46. starpu_malloc_pinned_if_possible((void **)&random_array_y, SIZE*sizeof(TYPE));
  47. STARPU_ASSERT(random_array_y);
  48. unsigned *cnt_array;
  49. starpu_malloc_pinned_if_possible((void **)&cnt_array, NTASKS*sizeof(unsigned));
  50. STARPU_ASSERT(cnt_array);
  51. /* First generate an array of random numbers */
  52. for (i = 0; i < SIZE; i++)
  53. {
  54. random_array_x[i] = (((TYPE)rand()/(TYPE)RAND_MAX)*2.0 - 1.0);
  55. random_array_y[i] = (((TYPE)rand()/(TYPE)RAND_MAX)*2.0 - 1.0);
  56. }
  57. /* Register the entire array */
  58. starpu_data_handle random_array_handle_x;
  59. starpu_register_vector_data(&random_array_handle_x, 0, (uintptr_t)random_array_x, SIZE, sizeof(TYPE));
  60. starpu_data_handle random_array_handle_y;
  61. starpu_register_vector_data(&random_array_handle_y, 0, (uintptr_t)random_array_y, SIZE, sizeof(TYPE));
  62. starpu_data_handle cnt_array_handle;
  63. starpu_register_vector_data(&cnt_array_handle, 0, (uintptr_t)cnt_array, NTASKS, sizeof(unsigned));
  64. /* TODO use a write-back mechanism */
  65. struct starpu_filter_t f = {
  66. .filter_func = starpu_block_filter_func_vector,
  67. .filter_arg = NTASKS
  68. };
  69. starpu_partition_data(random_array_handle_x, &f);
  70. starpu_partition_data(random_array_handle_y, &f);
  71. starpu_partition_data(cnt_array_handle, &f);
  72. struct starpu_codelet_t cl = {
  73. .where = STARPU_CPU|STARPU_CUDA,
  74. .cpu_func = cpu_kernel,
  75. .cuda_func = cuda_kernel,
  76. .nbuffers = 3,
  77. .model = NULL /* TODO */
  78. };
  79. for (i = 0; i < NTASKS; i++)
  80. {
  81. struct starpu_task *task = starpu_task_create();
  82. task->cl = &cl;
  83. task->buffers[0].handle = starpu_get_sub_data(random_array_handle_x, 1, i);
  84. task->buffers[0].mode = STARPU_R;
  85. task->buffers[1].handle = starpu_get_sub_data(random_array_handle_y, 1, i);
  86. task->buffers[1].mode = STARPU_R;
  87. task->buffers[2].handle = starpu_get_sub_data(cnt_array_handle, 1, i);
  88. task->buffers[2].mode = STARPU_W;
  89. int ret = starpu_submit_task(task);
  90. STARPU_ASSERT(!ret);
  91. }
  92. starpu_wait_all_tasks();
  93. /* Get the cnt_array back in main memory */
  94. starpu_unpartition_data(cnt_array_handle, 0);
  95. starpu_sync_data_with_mem(cnt_array_handle, STARPU_RW);
  96. /* Count the total number of entries */
  97. unsigned total_cnt = 0;
  98. for (i = 0; i < NTASKS; i++)
  99. total_cnt += cnt_array[i];
  100. starpu_release_data_from_mem(cnt_array_handle);
  101. starpu_shutdown();
  102. /* Total surface : Pi * r^ 2 = Pi*1^2, total square surface : 2^2 = 4, probability to impact the disk: pi/4 */
  103. fprintf(stderr, "Pi approximation : %f (%d / %d)\n", ((TYPE)total_cnt*4)/(SIZE), total_cnt, SIZE);
  104. return 0;
  105. }