vector_scal_c.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013,2015,2017 CNRS
  4. * Copyright (C) 2013 Inria
  5. * Copyright (C) 2010-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. //! [To be included. You should update doxygen if you see this text.]
  19. /*
  20. * This example demonstrates how to use StarPU to scale an array by a factor.
  21. * It shows how to manipulate data with StarPU's data management library.
  22. * 1- how to declare a piece of data to StarPU (starpu_vector_data_register)
  23. * 2- how to describe which data are accessed by a task (task->handles[0])
  24. * 3- how a kernel can manipulate the data (buffers[0].vector.ptr)
  25. */
  26. #include <starpu.h>
  27. #define NX 2048
  28. extern void scal_cpu_func(void *buffers[], void *_args);
  29. extern void scal_sse_func(void *buffers[], void *_args);
  30. extern void scal_cuda_func(void *buffers[], void *_args);
  31. extern void scal_opencl_func(void *buffers[], void *_args);
  32. static struct starpu_codelet cl =
  33. {
  34. .where = STARPU_CPU | STARPU_CUDA | STARPU_OPENCL,
  35. /* CPU implementation of the codelet */
  36. .cpu_funcs = { scal_cpu_func, scal_sse_func },
  37. .cpu_funcs_name = { "scal_cpu_func", "scal_sse_func" },
  38. #ifdef STARPU_USE_CUDA
  39. /* CUDA implementation of the codelet */
  40. .cuda_funcs = { scal_cuda_func },
  41. #endif
  42. #ifdef STARPU_USE_OPENCL
  43. /* OpenCL implementation of the codelet */
  44. .opencl_funcs = { scal_opencl_func },
  45. #endif
  46. .nbuffers = 1,
  47. .modes = { STARPU_RW }
  48. };
  49. #ifdef STARPU_USE_OPENCL
  50. struct starpu_opencl_program programs;
  51. #endif
  52. int main(int argc, char **argv)
  53. {
  54. /* We consider a vector of float that is initialized just as any of C
  55. * data */
  56. float vector[NX];
  57. unsigned i;
  58. for (i = 0; i < NX; i++)
  59. vector[i] = 1.0f;
  60. fprintf(stderr, "BEFORE: First element was %f\n", vector[0]);
  61. /* Initialize StarPU with default configuration */
  62. starpu_init(NULL);
  63. #ifdef STARPU_USE_OPENCL
  64. starpu_opencl_load_opencl_from_file(
  65. "examples/basic_examples/vector_scal_opencl_kernel.cl", &programs, NULL);
  66. #endif
  67. /* Tell StaPU to associate the "vector" vector with the "vector_handle"
  68. * identifier. When a task needs to access a piece of data, it should
  69. * refer to the handle that is associated to it.
  70. * In the case of the "vector" data interface:
  71. * - the first argument of the registration method is a pointer to the
  72. * handle that should describe the data
  73. * - the second argument is the memory node where the data (ie. "vector")
  74. * resides initially: STARPU_MAIN_RAM stands for an address in main memory, as
  75. * opposed to an adress on a GPU for instance.
  76. * - the third argument is the adress of the vector in RAM
  77. * - the fourth argument is the number of elements in the vector
  78. * - the fifth argument is the size of each element.
  79. */
  80. starpu_data_handle_t vector_handle;
  81. starpu_vector_data_register(&vector_handle, STARPU_MAIN_RAM, (uintptr_t)vector,
  82. NX, sizeof(vector[0]));
  83. float factor = 3.14;
  84. /* create a synchronous task: any call to starpu_task_submit will block
  85. * until it is terminated */
  86. struct starpu_task *task = starpu_task_create();
  87. task->synchronous = 1;
  88. task->cl = &cl;
  89. /* the codelet manipulates one buffer in RW mode */
  90. task->handles[0] = vector_handle;
  91. /* an argument is passed to the codelet, beware that this is a
  92. * READ-ONLY buffer and that the codelet may be given a pointer to a
  93. * COPY of the argument */
  94. task->cl_arg = &factor;
  95. task->cl_arg_size = sizeof(factor);
  96. /* execute the task on any eligible computational ressource */
  97. starpu_task_submit(task);
  98. /* StarPU does not need to manipulate the array anymore so we can stop
  99. * monitoring it */
  100. starpu_data_unregister(vector_handle);
  101. #ifdef STARPU_USE_OPENCL
  102. starpu_opencl_unload_opencl(&programs);
  103. #endif
  104. /* terminate StarPU, no task can be submitted after */
  105. starpu_shutdown();
  106. fprintf(stderr, "AFTER First element is %f\n", vector[0]);
  107. return 0;
  108. }
  109. //! [To be included. You should update doxygen if you see this text.]