fblock.c 5.0 KB

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