fblock.c 5.3 KB

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