vector_scal.c 4.3 KB

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