vector_scal.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #define NX 2048
  39. extern void scal_cpu_func(void *buffers[], void *_args);
  40. extern void scal_cuda_func(void *buffers[], void *_args);
  41. extern void scal_opencl_func(void *buffers[], void *_args);
  42. static struct starpu_codelet cl = {
  43. .where = STARPU_CPU | STARPU_CUDA | STARPU_OPENCL,
  44. /* CPU implementation of the codelet */
  45. .cpu_funcs = {scal_cpu_func, NULL},
  46. #ifdef STARPU_USE_CUDA
  47. /* CUDA implementation of the codelet */
  48. .cuda_funcs = {scal_cuda_func, NULL},
  49. #endif
  50. #ifdef STARPU_USE_OPENCL
  51. /* OpenCL implementation of the codelet */
  52. .opencl_funcs = {scal_opencl_func, NULL},
  53. #endif
  54. .nbuffers = 1,
  55. .modes = {STARPU_RW}
  56. };
  57. #ifdef STARPU_USE_OPENCL
  58. struct starpu_opencl_program programs;
  59. #endif
  60. int main(int argc, char **argv)
  61. {
  62. /* We consider a vector of float that is initialized just as any of C
  63. * data */
  64. float vector[NX];
  65. unsigned i;
  66. for (i = 0; i < NX; i++)
  67. vector[i] = 1.0f;
  68. fprintf(stderr, "BEFORE : First element was %f\n", vector[0]);
  69. /* Initialize StarPU with default configuration */
  70. starpu_init(NULL);
  71. #ifdef STARPU_USE_OPENCL
  72. starpu_opencl_load_opencl_from_file("vector_scal_opencl_kernel.cl", &programs, NULL);
  73. #endif
  74. /* Tell StaPU to associate the "vector" vector with the "vector_handle"
  75. * identifier. When a task needs to access a piece of data, it should
  76. * refer to the handle that is associated to it.
  77. * In the case of the "vector" data interface:
  78. * - the first argument of the registration method is a pointer to the
  79. * handle that should describe the data
  80. * - the second argument is the memory node where the data (ie. "vector")
  81. * resides initially: 0 stands for an address in main memory, as
  82. * opposed to an adress on a GPU for instance.
  83. * - the third argument is the adress of the vector in RAM
  84. * - the fourth argument is the number of elements in the vector
  85. * - the fifth argument is the size of each element.
  86. */
  87. starpu_data_handle_t vector_handle;
  88. starpu_vector_data_register(&vector_handle, 0, (uintptr_t)vector,
  89. NX, sizeof(vector[0]));
  90. float factor = 3.14;
  91. /* create a synchronous task: any call to starpu_task_submit will block
  92. * until it is terminated */
  93. struct starpu_task *task = starpu_task_create();
  94. task->synchronous = 1;
  95. task->cl = &cl;
  96. /* the codelet manipulates one buffer in RW mode */
  97. task->handles[0] = vector_handle;
  98. /* an argument is passed to the codelet, beware that this is a
  99. * READ-ONLY buffer and that the codelet may be given a pointer to a
  100. * COPY of the argument */
  101. task->cl_arg = &factor;
  102. task->cl_arg_size = sizeof(factor);
  103. /* execute the task on any eligible computational ressource */
  104. starpu_task_submit(task);
  105. /* StarPU does not need to manipulate the array anymore so we can stop
  106. * monitoring it */
  107. starpu_data_unregister(vector_handle);
  108. #ifdef STARPU_USE_OPENCL
  109. starpu_opencl_unload_opencl(&programs);
  110. #endif
  111. /* terminate StarPU, no task can be submitted after */
  112. starpu_shutdown();
  113. fprintf(stderr, "AFTER First element is %f\n", vector[0]);
  114. return 0;
  115. }