fblock.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011, 2013-2015 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. /*
  18. * This examplifies how to use partitioning filters. We here just split a 3D
  19. * matrix into 3D slices (along the X axis), and run a dumb kernel on them.
  20. */
  21. #include <starpu.h>
  22. #define NX 5
  23. #define NY 4
  24. #define NZ 3
  25. #define PARTS 2
  26. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  27. extern void cpu_func(void *buffers[], void *cl_arg);
  28. #ifdef STARPU_USE_CUDA
  29. extern void cuda_func(void *buffers[], void *cl_arg);
  30. #endif
  31. #ifdef STARPU_USE_OPENCL
  32. extern void opencl_func(void *buffers[], void *cl_arg);
  33. #endif
  34. void print_block(int *block, int nx, int ny, int nz, unsigned ldy, unsigned ldz)
  35. {
  36. int i, j, k;
  37. FPRINTF(stderr, "block=%p nx=%d ny=%d nz=%d ldy=%u ldz=%u\n", block, nx, ny, nz, ldy, ldz);
  38. for(k=0 ; k<nz ; k++)
  39. {
  40. for(j=0 ; j<ny ; j++)
  41. {
  42. for(i=0 ; i<nx ; i++)
  43. {
  44. FPRINTF(stderr, "%2d ", block[(k*ldz)+(j*ldy)+i]);
  45. }
  46. FPRINTF(stderr,"\n");
  47. }
  48. FPRINTF(stderr,"\n");
  49. }
  50. FPRINTF(stderr,"\n");
  51. }
  52. void print_data(starpu_data_handle_t block_handle)
  53. {
  54. int *block = (int *)starpu_block_get_local_ptr(block_handle);
  55. int nx = starpu_block_get_nx(block_handle);
  56. int ny = starpu_block_get_ny(block_handle);
  57. int nz = starpu_block_get_nz(block_handle);
  58. unsigned ldy = starpu_block_get_local_ldy(block_handle);
  59. unsigned ldz = starpu_block_get_local_ldz(block_handle);
  60. print_block(block, nx, ny, nz, ldy, ldz);
  61. }
  62. #ifdef STARPU_USE_OPENCL
  63. struct starpu_opencl_program opencl_program;
  64. #endif
  65. int main(int argc, char **argv)
  66. {
  67. int *block,n=0;
  68. int i, j, k;
  69. int ret;
  70. block = (int*)malloc(NX*NY*NZ*sizeof(block[0]));
  71. assert(block);
  72. for(k=0 ; k<NZ ; k++)
  73. {
  74. for(j=0 ; j<NY ; j++)
  75. {
  76. for(i=0 ; i<NX ; i++)
  77. {
  78. block[(k*NX*NY)+(j*NX)+i] = n++;
  79. }
  80. }
  81. }
  82. starpu_data_handle_t handle;
  83. struct starpu_codelet cl =
  84. {
  85. .cpu_funcs = {cpu_func},
  86. .cpu_funcs_name = {"cpu_func"},
  87. #ifdef STARPU_USE_CUDA
  88. .cuda_funcs = {cuda_func},
  89. .cuda_flags = {STARPU_CUDA_ASYNC},
  90. #endif
  91. #ifdef STARPU_USE_OPENCL
  92. .opencl_funcs = {opencl_func},
  93. .opencl_flags = {STARPU_OPENCL_ASYNC},
  94. #endif
  95. .nbuffers = 1,
  96. .modes = {STARPU_RW},
  97. .name = "block_scal"
  98. };
  99. ret = starpu_init(NULL);
  100. if (ret == -ENODEV)
  101. exit(77);
  102. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  103. #ifdef STARPU_USE_OPENCL
  104. ret = starpu_opencl_load_opencl_from_file("examples/filters/fblock_opencl_kernel.cl", &opencl_program, NULL);
  105. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  106. #endif
  107. /* Declare data to StarPU */
  108. starpu_block_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)block, NX, NX*NY, NX, NY, NZ, sizeof(int));
  109. FPRINTF(stderr, "IN Block\n");
  110. print_data(handle);
  111. /* Partition the block in PARTS sub-blocks */
  112. struct starpu_data_filter f =
  113. {
  114. .filter_func = starpu_block_filter_block,
  115. .nchildren = PARTS
  116. };
  117. starpu_data_partition(handle, &f);
  118. FPRINTF(stderr,"Nb of partitions : %d\n",starpu_data_get_nb_children(handle));
  119. for(i=0 ; i<starpu_data_get_nb_children(handle) ; i++)
  120. {
  121. starpu_data_handle_t sblock = starpu_data_get_sub_data(handle, 1, i);
  122. FPRINTF(stderr, "Sub block %d\n", i);
  123. print_data(sblock);
  124. }
  125. /* Submit a task on each sub-block */
  126. for(i=0 ; i<starpu_data_get_nb_children(handle) ; i++)
  127. {
  128. int multiplier=i;
  129. struct starpu_task *task = starpu_task_create();
  130. FPRINTF(stderr,"Dealing with sub-block %d\n", i);
  131. task->cl = &cl;
  132. task->synchronous = 1;
  133. task->callback_func = NULL;
  134. task->handles[0] = starpu_data_get_sub_data(handle, 1, i);
  135. task->cl_arg = &multiplier;
  136. task->cl_arg_size = sizeof(multiplier);
  137. ret = starpu_task_submit(task);
  138. if (ret)
  139. {
  140. FPRINTF(stderr, "Error when submitting task\n");
  141. exit(ret);
  142. }
  143. }
  144. /* Unpartition the data, unregister it from StarPU and shutdown */
  145. starpu_data_unpartition(handle, STARPU_MAIN_RAM);
  146. print_data(handle);
  147. starpu_data_unregister(handle);
  148. /* Print result block */
  149. FPRINTF(stderr, "OUT Block\n");
  150. print_block(block, NX, NY, NZ, NX, NX*NY);
  151. free(block);
  152. starpu_shutdown();
  153. return 0;
  154. }