fblock.c 5.4 KB

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