vector_scal_c.texi 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * This example demonstrates how to use StarPU to scale an array by a factor.
  3. * It shows how to manipulate data with StarPU's data management library.
  4. * 1- how to declare a piece of data to StarPU (starpu_vector_data_register)
  5. * 2- how to describe which data are accessed by a task (task->buffers[0])
  6. * 3- how a kernel can manipulate the data (buffers[0].vector.ptr)
  7. */
  8. #include <stdio.h>
  9. #include <stdint.h>
  10. #include <starpu.h>
  11. #include <starpu_opencl.h>
  12. #define NX 2048
  13. extern void scal_cpu_func(void *buffers[], void *_args);
  14. extern void scal_cuda_func(void *buffers[], void *_args);
  15. extern void scal_opencl_func(void *buffers[], void *_args);
  16. static starpu_codelet cl = @{
  17. .where = STARPU_CPU
  18. #ifdef STARPU_USE_CUDA
  19. | STARPU_CUDA
  20. #endif
  21. #ifdef STARPU_USE_OPENCL
  22. | STARPU_OPENCL
  23. #endif
  24. ,
  25. /* CPU implementation of the codelet */
  26. .cpu_func = scal_cpu_func,
  27. #ifdef STARPU_USE_CUDA
  28. /* CUDA implementation of the codelet */
  29. .cuda_func = scal_cuda_func,
  30. #endif
  31. #ifdef STARPU_USE_OPENCL
  32. /* OpenCL implementation of the codelet */
  33. .opencl_func = scal_opencl_func,
  34. #endif
  35. .nbuffers = 1
  36. @};
  37. #ifdef STARPU_USE_OPENCL
  38. struct starpu_opencl_codelet codelet;
  39. #endif
  40. int main(int argc, char **argv)
  41. @{
  42. /* We consider a vector of float that is initialized just as any of C
  43. * data */
  44. float vector[NX];
  45. unsigned i;
  46. for (i = 0; i < NX; i++)
  47. vector[i] = 1.0f;
  48. fprintf(stderr, "BEFORE : First element was %f\n", vector[0]);
  49. /* Initialize StarPU with default configuration */
  50. starpu_init(NULL);
  51. #ifdef STARPU_USE_OPENCL
  52. starpu_opencl_load_opencl_from_file("examples/basic_examples/vector_scal_opencl_codelet.cl",
  53. &codelet);
  54. #endif
  55. /* Tell StaPU to associate the "vector" vector with the "vector_handle"
  56. * identifier. When a task needs to access a piece of data, it should
  57. * refer to the handle that is associated to it.
  58. * In the case of the "vector" data interface:
  59. * - the first argument of the registration method is a pointer to the
  60. * handle that should describe the data
  61. * - the second argument is the memory node where the data (ie. "vector")
  62. * resides initially: 0 stands for an address in main memory, as
  63. * opposed to an adress on a GPU for instance.
  64. * - the third argument is the adress of the vector in RAM
  65. * - the fourth argument is the number of elements in the vector
  66. * - the fifth argument is the size of each element.
  67. */
  68. starpu_data_handle vector_handle;
  69. starpu_vector_data_register(&vector_handle, 0, (uintptr_t)vector, NX, sizeof(float));
  70. float factor = 3.14;
  71. /* create a synchronous task: any call to starpu_task_submit will block
  72. * until it is terminated */
  73. struct starpu_task *task = starpu_task_create();
  74. task->synchronous = 1;
  75. task->cl = &cl;
  76. /* the codelet manipulates one buffer in RW mode */
  77. task->buffers[0].handle = vector_handle;
  78. task->buffers[0].mode = STARPU_RW;
  79. /* an argument is passed to the codelet, beware that this is a
  80. * READ-ONLY buffer and that the codelet may be given a pointer to a
  81. * COPY of the argument */
  82. task->cl_arg = &factor;
  83. task->cl_arg_size = sizeof(factor);
  84. /* execute the task on any eligible computational ressource */
  85. starpu_task_submit(task);
  86. /* StarPU does not need to manipulate the array anymore so we can stop
  87. * monitoring it */
  88. starpu_data_unregister(vector_handle);
  89. #ifdef STARPU_USE_OPENCL
  90. starpu_opencl_unload_opencl(&codelet);
  91. #endif
  92. /* terminate StarPU, no task can be submitted after */
  93. starpu_shutdown();
  94. fprintf(stderr, "AFTER First element is %f\n", vector[0]);
  95. return 0;
  96. @}