vector_scal_c.texi 3.7 KB

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