vector_scal.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  4. * Copyright (C) 2010, 2011 Université de Bordeaux 1
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * * The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /*
  31. * This example demonstrates how to use StarPU to scale an array by a factor.
  32. * It shows how to manipulate data with StarPU's data management library.
  33. * 1- how to declare a piece of data to StarPU (starpu_vector_data_register)
  34. * 2- how to describe which data are accessed by a task (task->handle[0])
  35. * 3- how a kernel can manipulate the data (buffers[0].vector.ptr)
  36. */
  37. #include <starpu.h>
  38. #include <starpu_opencl.h>
  39. #define NX 2048
  40. extern void scal_cpu_func(void *buffers[], void *_args);
  41. extern void scal_cuda_func(void *buffers[], void *_args);
  42. extern void scal_opencl_func(void *buffers[], void *_args);
  43. static struct starpu_codelet cl = {
  44. .where = STARPU_CPU | STARPU_CUDA | STARPU_OPENCL,
  45. /* CPU implementation of the codelet */
  46. .cpu_funcs = {scal_cpu_func, NULL},
  47. #ifdef STARPU_USE_CUDA
  48. /* CUDA implementation of the codelet */
  49. .cuda_funcs = {scal_cuda_func, NULL},
  50. #endif
  51. #ifdef STARPU_USE_OPENCL
  52. /* OpenCL implementation of the codelet */
  53. .opencl_funcs = {scal_opencl_func, NULL},
  54. #endif
  55. .nbuffers = 1,
  56. .modes = {STARPU_RW}
  57. };
  58. #ifdef STARPU_USE_OPENCL
  59. struct starpu_opencl_program programs;
  60. #endif
  61. int main(int argc, char **argv)
  62. {
  63. /* We consider a vector of float that is initialized just as any of C
  64. * data */
  65. float vector[NX];
  66. unsigned i;
  67. for (i = 0; i < NX; i++)
  68. vector[i] = 1.0f;
  69. fprintf(stderr, "BEFORE : First element was %f\n", vector[0]);
  70. /* Initialize StarPU with default configuration */
  71. starpu_init(NULL);
  72. #ifdef STARPU_USE_OPENCL
  73. starpu_opencl_load_opencl_from_file("vector_scal_opencl_kernel.cl", &programs, NULL);
  74. #endif
  75. /* Tell StaPU to associate the "vector" vector with the "vector_handle"
  76. * identifier. When a task needs to access a piece of data, it should
  77. * refer to the handle that is associated to it.
  78. * In the case of the "vector" data interface:
  79. * - the first argument of the registration method is a pointer to the
  80. * handle that should describe the data
  81. * - the second argument is the memory node where the data (ie. "vector")
  82. * resides initially: 0 stands for an address in main memory, as
  83. * opposed to an adress on a GPU for instance.
  84. * - the third argument is the adress of the vector in RAM
  85. * - the fourth argument is the number of elements in the vector
  86. * - the fifth argument is the size of each element.
  87. */
  88. starpu_data_handle_t vector_handle;
  89. starpu_vector_data_register(&vector_handle, 0, (uintptr_t)vector,
  90. NX, sizeof(vector[0]));
  91. float factor = 3.14;
  92. /* create a synchronous task: any call to starpu_task_submit will block
  93. * until it is terminated */
  94. struct starpu_task *task = starpu_task_create();
  95. task->synchronous = 1;
  96. task->cl = &cl;
  97. /* the codelet manipulates one buffer in RW mode */
  98. task->handles[0] = vector_handle;
  99. /* an argument is passed to the codelet, beware that this is a
  100. * READ-ONLY buffer and that the codelet may be given a pointer to a
  101. * COPY of the argument */
  102. task->cl_arg = &factor;
  103. task->cl_arg_size = sizeof(factor);
  104. /* execute the task on any eligible computational ressource */
  105. starpu_task_submit(task);
  106. /* StarPU does not need to manipulate the array anymore so we can stop
  107. * monitoring it */
  108. starpu_data_unregister(vector_handle);
  109. #ifdef STARPU_USE_OPENCL
  110. starpu_opencl_unload_opencl(&programs);
  111. #endif
  112. /* terminate StarPU, no task can be submitted after */
  113. starpu_shutdown();
  114. fprintf(stderr, "AFTER First element is %f\n", vector[0]);
  115. return 0;
  116. }