shadow.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2014 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. /*
  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=8, 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. /* Shadow width */
  43. #define SHADOW 2
  44. #define NX 30
  45. #define PARTS 3
  46. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  47. void cpu_func(void *buffers[], void *cl_arg)
  48. {
  49. unsigned i;
  50. /* length of the shadowed source vector */
  51. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  52. /* local copy of the shadowed source vector pointer */
  53. int *val = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  54. /* length of the destination vector */
  55. unsigned n2 = STARPU_VECTOR_GET_NX(buffers[1]);
  56. /* local copy of the destination vector pointer */
  57. int *val2 = (int *)STARPU_VECTOR_GET_PTR(buffers[1]);
  58. /* If things go right, sizes should match */
  59. STARPU_ASSERT(n == n2);
  60. for (i = 0; i < n; i++)
  61. val2[i] = val[i];
  62. }
  63. #ifdef STARPU_USE_CUDA
  64. void cuda_func(void *buffers[], void *cl_arg)
  65. {
  66. /* length of the shadowed source vector */
  67. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  68. /* local copy of the shadowed source vector pointer */
  69. int *val = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  70. /* length of the destination vector */
  71. unsigned n2 = STARPU_VECTOR_GET_NX(buffers[1]);
  72. /* local copy of the destination vector pointer */
  73. int *val2 = (int *)STARPU_VECTOR_GET_PTR(buffers[1]);
  74. /* If things go right, sizes should match */
  75. STARPU_ASSERT(n == n2);
  76. cudaMemcpyAsync(val2, val, n*sizeof(*val), cudaMemcpyDeviceToDevice, starpu_cuda_get_local_stream());
  77. }
  78. #endif
  79. int main(int argc, char **argv)
  80. {
  81. unsigned j;
  82. int vector[NX + 2*SHADOW];
  83. int vector2[NX + PARTS*2*SHADOW];
  84. starpu_data_handle_t handle, handle2;
  85. int ret, i;
  86. struct starpu_codelet cl =
  87. {
  88. .cpu_funcs = {cpu_func, NULL},
  89. .cpu_funcs_name = {"cpu_func", NULL},
  90. #ifdef STARPU_USE_CUDA
  91. .cuda_funcs = {cuda_func, NULL},
  92. .cuda_flags = {STARPU_CUDA_ASYNC},
  93. #endif
  94. .nbuffers = 2,
  95. .modes = {STARPU_R, STARPU_W}
  96. };
  97. for(i=0 ; i<NX ; i++) vector[SHADOW+i] = i;
  98. for(i=0 ; i<SHADOW ; i++) vector[i] = vector[i+NX];
  99. for(i=0 ; i<SHADOW ; i++) vector[SHADOW+NX+i] = vector[SHADOW+i];
  100. FPRINTF(stderr,"IN Vector: ");
  101. for(i=0 ; i<NX + 2*SHADOW ; i++) FPRINTF(stderr, "%5d ", vector[i]);
  102. FPRINTF(stderr,"\n");
  103. ret = starpu_init(NULL);
  104. if (ret == -ENODEV)
  105. exit(77);
  106. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  107. /* Declare source vector to StarPU */
  108. starpu_vector_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)vector, NX + 2*SHADOW, sizeof(vector[0]));
  109. /* Declare destination vector to StarPU */
  110. starpu_vector_data_register(&handle2, STARPU_MAIN_RAM, (uintptr_t)vector2, NX + PARTS*2*SHADOW, sizeof(vector[0]));
  111. /* Partition the source vector in PARTS sub-vectors with shadows */
  112. /* NOTE: the resulting handles should only be used in read-only mode,
  113. * as StarPU will not know how the overlapping parts would have to be
  114. * combined. */
  115. struct starpu_data_filter f =
  116. {
  117. .filter_func = starpu_vector_filter_block_shadow,
  118. .nchildren = PARTS,
  119. .filter_arg_ptr = (void*)(uintptr_t) SHADOW /* Shadow width */
  120. };
  121. starpu_data_partition(handle, &f);
  122. /* Partition the destination vector in PARTS sub-vectors */
  123. struct starpu_data_filter f2 =
  124. {
  125. .filter_func = starpu_vector_filter_block,
  126. .nchildren = PARTS,
  127. };
  128. starpu_data_partition(handle2, &f2);
  129. /* Submit a task on each sub-vector */
  130. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  131. {
  132. starpu_data_handle_t sub_handle = starpu_data_get_sub_data(handle, 1, i);
  133. starpu_data_handle_t sub_handle2 = starpu_data_get_sub_data(handle2, 1, i);
  134. struct starpu_task *task = starpu_task_create();
  135. task->handles[0] = sub_handle;
  136. task->handles[1] = sub_handle2;
  137. task->cl = &cl;
  138. task->synchronous = 1;
  139. ret = starpu_task_submit(task);
  140. if (ret == -ENODEV) goto enodev;
  141. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  142. }
  143. starpu_data_unpartition(handle, STARPU_MAIN_RAM);
  144. starpu_data_unpartition(handle2, STARPU_MAIN_RAM);
  145. starpu_data_unregister(handle);
  146. starpu_data_unregister(handle2);
  147. starpu_shutdown();
  148. FPRINTF(stderr,"OUT Vector: ");
  149. for(i=0 ; i<NX + PARTS*2*SHADOW ; i++) FPRINTF(stderr, "%5d ", vector2[i]);
  150. FPRINTF(stderr,"\n");
  151. for(i=0 ; i<PARTS ; i++)
  152. for (j=0 ; j<NX/PARTS ; j++)
  153. STARPU_ASSERT(vector2[i*(NX/PARTS+2*SHADOW)+j] == vector[i*(NX/PARTS)+j]);
  154. return 0;
  155. enodev:
  156. FPRINTF(stderr, "WARNING: No one can execute this task\n");
  157. starpu_shutdown();
  158. return 77;
  159. }