vector_scal.c 4.2 KB

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