vector_scal_task_insert.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. /*
  17. * This example demonstrates how to use StarPU to scale an array by a factor.
  18. * It shows how to manipulate data with StarPU's data management library.
  19. * 1- how to declare a piece of data to StarPU (starpu_vector_data_register)
  20. * 2- how to submit a task to StarPU
  21. * 3- how a kernel can manipulate the data (buffers[0].vector.ptr)
  22. */
  23. #include <starpu.h>
  24. #define NX 2048
  25. extern void vector_scal_cpu(void *buffers[], void *_args);
  26. extern void vector_scal_cuda(void *buffers[], void *_args);
  27. extern void vector_scal_opencl(void *buffers[], void *_args);
  28. static struct starpu_codelet cl = {
  29. /* CPU implementation of the codelet */
  30. .cpu_funcs = {vector_scal_cpu},
  31. #ifdef STARPU_USE_CUDA
  32. /* CUDA implementation of the codelet */
  33. .cuda_funcs = {vector_scal_cuda},
  34. #endif
  35. #ifdef STARPU_USE_OPENCL
  36. /* OpenCL implementation of the codelet */
  37. .opencl_funcs = {vector_scal_opencl},
  38. #endif
  39. .nbuffers = 1,
  40. .modes = {STARPU_RW}
  41. };
  42. #ifdef STARPU_USE_OPENCL
  43. struct starpu_opencl_program programs;
  44. #endif
  45. int main(int argc, char **argv)
  46. {
  47. /* We consider a vector of float that is initialized just as any of C
  48. * data */
  49. float vector[NX];
  50. unsigned i;
  51. for (i = 0; i < NX; i++)
  52. vector[i] = 1.0f;
  53. fprintf(stderr, "BEFORE : First element was %f\n", vector[0]);
  54. /* Initialize StarPU with default configuration */
  55. int ret = starpu_init(NULL);
  56. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  57. #ifdef STARPU_USE_OPENCL
  58. starpu_opencl_load_opencl_from_file("vector_scal_opencl_kernel.cl", &programs, NULL);
  59. #endif
  60. /* Tell StaPU to associate the "vector" vector with the "vector_handle"
  61. * identifier. When a task needs to access a piece of data, it should
  62. * refer to the handle that is associated to it.
  63. * In the case of the "vector" data interface:
  64. * - the first argument of the registration method is a pointer to the
  65. * handle that should describe the data
  66. * - the second argument is the memory node where the data (ie. "vector")
  67. * resides initially: STARPU_MAIN_RAM stands for an address in main memory, as
  68. * opposed to an adress on a GPU for instance.
  69. * - the third argument is the adress of the vector in RAM
  70. * - the fourth argument is the number of elements in the vector
  71. * - the fifth argument is the size of each element.
  72. */
  73. starpu_data_handle_t vector_handle;
  74. starpu_vector_data_register(&vector_handle, STARPU_MAIN_RAM, (uintptr_t)vector,
  75. NX, sizeof(vector[0]));
  76. float factor = 3.14;
  77. ret = starpu_task_insert(&cl,
  78. /* an argument is passed to the codelet, beware that this is a
  79. * READ-ONLY buffer and that the codelet may be given a pointer to a
  80. * COPY of the argument */
  81. STARPU_VALUE, &factor, sizeof(factor),
  82. /* the codelet manipulates one buffer in RW mode */
  83. STARPU_RW, vector_handle,
  84. 0);
  85. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  86. /* Wait for tasks completion */
  87. starpu_task_wait_for_all();
  88. /* StarPU does not need to manipulate the array anymore so we can stop
  89. * monitoring it */
  90. starpu_data_unregister(vector_handle);
  91. #ifdef STARPU_USE_OPENCL
  92. starpu_opencl_unload_opencl(&programs);
  93. #endif
  94. /* terminate StarPU, no task can be submitted after */
  95. starpu_shutdown();
  96. fprintf(stderr, "AFTER First element is %f\n", vector[0]);
  97. return 0;
  98. }