vector_scal_c.texi 4.3 KB

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