vector_scal_c.texi 4.0 KB

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