vector_scal_c.texi 3.6 KB

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