vector_scal.c 4.2 KB

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