fblock.c 5.2 KB

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