fblock.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (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. void opencl_func(void *buffers[], void *cl_arg)
  23. {
  24. return;
  25. }
  26. extern void cuda_func(void *buffers[], void *cl_arg);
  27. void cpu_func(void *buffers[], void *cl_arg)
  28. {
  29. unsigned i, j, k;
  30. int *factor = cl_arg;
  31. int *block = (int *)STARPU_GET_BLOCK_PTR(buffers[0]);
  32. int nx = (int)STARPU_GET_BLOCK_NX(buffers[0]);
  33. int ny = (int)STARPU_GET_BLOCK_NY(buffers[0]);
  34. int nz = (int)STARPU_GET_BLOCK_NZ(buffers[0]);
  35. unsigned ldy = STARPU_GET_BLOCK_LDY(buffers[0]);
  36. unsigned ldz = STARPU_GET_BLOCK_LDZ(buffers[0]);
  37. for(k=0; k<nz ; k++) {
  38. for(j=0; j<ny ; j++) {
  39. for(i=0; i<nx ; i++)
  40. block[(k*ldz)+(j*ldy)+i] = *factor;
  41. }
  42. }
  43. }
  44. void print_block(int *block, int nx, int ny, int nz)
  45. {
  46. int i, j, k;
  47. for(k=0 ; k<nz ; k++) {
  48. for(j=0 ; j<ny ; j++) {
  49. for(i=0 ; i<nx ; i++) {
  50. fprintf(stderr, "%2d ", block[(k*nx*ny)+(j*nx)+i]);
  51. }
  52. fprintf(stderr,"\n");
  53. }
  54. fprintf(stderr,"\n");
  55. }
  56. fprintf(stderr,"\n");
  57. }
  58. int main(int argc, char **argv)
  59. {
  60. int *block,n=0;
  61. int i, j, k;
  62. block = (int*)malloc(NX*NY*NZ*sizeof(block[0]));
  63. assert(block);
  64. for(k=0 ; k<NZ ; k++) {
  65. for(j=0 ; j<NY ; j++) {
  66. for(i=0 ; i<NX ; i++) {
  67. block[(k*NX*NY)+(j*NX)+i] = n++;
  68. }
  69. }
  70. }
  71. fprintf(stderr, "IN Block\n");
  72. print_block(block, NX, NY, NZ);
  73. starpu_data_handle handle;
  74. starpu_codelet cl =
  75. {
  76. .where = STARPU_CPU|STARPU_CUDA,
  77. .cpu_func = cpu_func,
  78. .cuda_func = cuda_func,
  79. .nbuffers = 1
  80. };
  81. starpu_init(NULL);
  82. /* Declare data to StarPU */
  83. starpu_block_data_register(&handle, 0, (uintptr_t)block, NX, NX*NY, NX, NY, NZ, sizeof(int));
  84. /* Partition the block in PARTS sub-blocks */
  85. struct starpu_data_filter f =
  86. {
  87. .filter_func = starpu_block_filter_func_block,
  88. .nchildren = PARTS,
  89. .get_nchildren = NULL,
  90. .get_child_ops = NULL
  91. };
  92. starpu_data_partition(handle, &f);
  93. fprintf(stderr,"Nb of partitions : %d\n",starpu_data_get_nb_children(handle));
  94. /* Submit a task on each sub-block */
  95. for(i=0 ; i<starpu_data_get_nb_children(handle) ; i++)
  96. {
  97. int ret,multiplier=i;
  98. struct starpu_task *task = starpu_task_create();
  99. fprintf(stderr,"Dealing with sub-block %d\n", i);
  100. task->cl = &cl;
  101. task->synchronous = 1;
  102. task->callback_func = NULL;
  103. task->buffers[0].handle = starpu_data_get_sub_data(handle, 1, i);
  104. task->buffers[0].mode = STARPU_RW;
  105. task->cl_arg = &multiplier;
  106. ret = starpu_task_submit(task);
  107. if (ret) {
  108. fprintf(stderr, "Error when submitting task\n");
  109. exit(ret);
  110. }
  111. }
  112. /* Unpartition the data, unregister it from StarPU and shutdown */
  113. starpu_data_unpartition(handle, 0);
  114. starpu_data_unregister(handle);
  115. starpu_shutdown();
  116. /* Print result block */
  117. fprintf(stderr, "OUT Block\n");
  118. print_block(block, NX, NY, NZ);
  119. }