shadow.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 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. /*
  18. * This examplifies the use of the shadow filter: a source "vector" of NX
  19. * elements (plus 2*SHADOW wrap-around elements) is partitioned into vectors
  20. * with some shadowing, and these are copied into a destination "vector2" of
  21. * NRPARTS*(NX/NPARTS+2*SHADOW) elements, partitioned in the traditionnal way,
  22. * thus showing how shadowing shows up.
  23. *
  24. * For instance, with NX=2, SHADOW=1, and NPARTS=4:
  25. *
  26. * vector
  27. * x0 x1 x2 x3 x4 x5 x6 x7 x8 x9
  28. *
  29. * is partitioned into 4 pieces:
  30. *
  31. * x0 x1 x2 x3
  32. * x2 x3 x4 x5
  33. * x4 x5 x6 x7
  34. * x6 x7 x8 x9
  35. *
  36. * which are copied into the 4 destination subparts of vector2, thus getting in
  37. * the end:
  38. *
  39. * x0 x1 x2 x3 x2 x3 x4 x5 x4 x5 x6 x7 x6 x7 x8 x9
  40. */
  41. #include <starpu.h>
  42. #include <starpu_cuda.h>
  43. /* Shadow width */
  44. #define SHADOW 2
  45. #define NX 30
  46. #define PARTS 3
  47. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  48. void cpu_func(void *buffers[], void *cl_arg)
  49. {
  50. unsigned i;
  51. /* length of the shadowed source vector */
  52. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  53. /* local copy of the shadowed source vector pointer */
  54. int *val = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  55. /* length of the destination vector */
  56. unsigned n2 = STARPU_VECTOR_GET_NX(buffers[1]);
  57. /* local copy of the destination vector pointer */
  58. int *val2 = (int *)STARPU_VECTOR_GET_PTR(buffers[1]);
  59. /* If things go right, sizes should match */
  60. STARPU_ASSERT(n == n2);
  61. for (i = 0; i < n; i++)
  62. val2[i] = val[i];
  63. }
  64. #ifdef STARPU_USE_CUDA
  65. void cuda_func(void *buffers[], void *cl_arg)
  66. {
  67. /* length of the shadowed source vector */
  68. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  69. /* local copy of the shadowed source vector pointer */
  70. int *val = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  71. /* length of the destination vector */
  72. unsigned n2 = STARPU_VECTOR_GET_NX(buffers[1]);
  73. /* local copy of the destination vector pointer */
  74. int *val2 = (int *)STARPU_VECTOR_GET_PTR(buffers[1]);
  75. /* If things go right, sizes should match */
  76. STARPU_ASSERT(n == n2);
  77. cudaMemcpy(val2, val, n*sizeof(*val), cudaMemcpyDeviceToDevice);
  78. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  79. }
  80. #endif
  81. int main(int argc, char **argv)
  82. {
  83. unsigned i, j;
  84. int vector[NX + 2*SHADOW];
  85. int vector2[NX + PARTS*2*SHADOW];
  86. starpu_data_handle_t handle, handle2;
  87. int factor=1;
  88. int ret;
  89. struct starpu_codelet cl =
  90. {
  91. .where = STARPU_CPU
  92. #ifdef STARPU_USE_CUDA
  93. |STARPU_CUDA
  94. #endif
  95. ,
  96. .cpu_funcs = {cpu_func, NULL},
  97. #ifdef STARPU_USE_CUDA
  98. .cuda_funcs = {cuda_func, NULL},
  99. #endif
  100. .nbuffers = 2,
  101. .modes = {STARPU_R, STARPU_W}
  102. };
  103. for(i=0 ; i<NX ; i++) vector[SHADOW+i] = i;
  104. for(i=0 ; i<SHADOW ; i++) vector[i] = vector[i+NX];
  105. for(i=0 ; i<SHADOW ; i++) vector[SHADOW+NX+i] = vector[SHADOW+i];
  106. FPRINTF(stderr,"IN Vector: ");
  107. for(i=0 ; i<NX + 2*SHADOW ; i++) FPRINTF(stderr, "%5d ", vector[i]);
  108. FPRINTF(stderr,"\n");
  109. ret = starpu_init(NULL);
  110. if (ret == -ENODEV)
  111. exit(77);
  112. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  113. /* Declare source vector to StarPU */
  114. starpu_vector_data_register(&handle, 0, (uintptr_t)vector, NX + 2*SHADOW, sizeof(vector[0]));
  115. /* Declare destination vector to StarPU */
  116. starpu_vector_data_register(&handle2, 0, (uintptr_t)vector2, NX + PARTS*2*SHADOW, sizeof(vector[0]));
  117. /* Partition the source vector in PARTS sub-vectors with shadows */
  118. /* NOTE: the resulting handles should only be used in read-only mode,
  119. * as StarPU will not know how the overlapping parts would have to be
  120. * combined. */
  121. struct starpu_data_filter f =
  122. {
  123. .filter_func = starpu_block_shadow_filter_func_vector,
  124. .nchildren = PARTS,
  125. .filter_arg_ptr = (void*)(uintptr_t) SHADOW /* Shadow width */
  126. };
  127. starpu_data_partition(handle, &f);
  128. /* Partition the destination vector in PARTS sub-vectors */
  129. struct starpu_data_filter f2 =
  130. {
  131. .filter_func = starpu_block_filter_func_vector,
  132. .nchildren = PARTS,
  133. };
  134. starpu_data_partition(handle2, &f2);
  135. /* Submit a task on each sub-vector */
  136. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  137. {
  138. starpu_data_handle_t sub_handle = starpu_data_get_sub_data(handle, 1, i);
  139. starpu_data_handle_t sub_handle2 = starpu_data_get_sub_data(handle2, 1, i);
  140. struct starpu_task *task = starpu_task_create();
  141. factor *= 10;
  142. task->handles[0] = sub_handle;
  143. task->handles[1] = sub_handle2;
  144. task->cl = &cl;
  145. task->synchronous = 1;
  146. task->cl_arg = &factor;
  147. task->cl_arg_size = sizeof(factor);
  148. ret = starpu_task_submit(task);
  149. if (ret == -ENODEV) goto enodev;
  150. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  151. }
  152. starpu_data_unpartition(handle, 0);
  153. starpu_data_unpartition(handle2, 0);
  154. starpu_data_unregister(handle);
  155. starpu_data_unregister(handle2);
  156. starpu_shutdown();
  157. FPRINTF(stderr,"OUT Vector: ");
  158. for(i=0 ; i<NX + PARTS*2*SHADOW ; i++) FPRINTF(stderr, "%5d ", vector2[i]);
  159. FPRINTF(stderr,"\n");
  160. for(i=0 ; i<PARTS ; i++)
  161. for (j=0 ; j<NX/PARTS ; j++)
  162. STARPU_ASSERT(vector2[i*(NX/PARTS+2*SHADOW)+j] == vector[i*(NX/PARTS)+j]);
  163. return 0;
  164. enodev:
  165. FPRINTF(stderr, "WARNING: No one can execute this task\n");
  166. starpu_shutdown();
  167. return 77;
  168. }