vector_scal.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (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 <stdio.h>
  24. #include <stdint.h>
  25. #include <starpu.h>
  26. #define N 2048
  27. /* This kernel takes a buffer and scales it by a constant factor */
  28. static void scal_func(void *buffers[], void *cl_arg)
  29. {
  30. unsigned i;
  31. float *factor = cl_arg;
  32. /*
  33. * The "buffers" array matches the task->buffers array: for instance
  34. * task->buffers[0].handle is a handle that corresponds to a data with
  35. * vector "interface", so that the first entry of the array in the
  36. * codelet is a pointer to a structure describing such a vector (ie.
  37. * struct starpu_vector_interface_s *). Here, we therefore manipulate
  38. * the buffers[0] element as a vector: nx gives the number of elements
  39. * in the array, ptr gives the location of the array (that was possibly
  40. * migrated/replicated), and elemsize gives the size of each elements.
  41. */
  42. starpu_vector_interface_t *vector = buffers[0];
  43. /* length of the vector */
  44. unsigned n = STARPU_GET_VECTOR_NX(vector);
  45. /* get a pointer to the local copy of the vector : note that we have to
  46. * cast it in (float *) since a vector could contain any type of
  47. * elements so that the .ptr field is actually a uintptr_t */
  48. float *val = (float *)STARPU_GET_VECTOR_PTR(vector);
  49. /* scale the vector */
  50. for (i = 0; i < n; i++)
  51. val[i] *= *factor;
  52. }
  53. extern void scal_cuda_func(void *buffers[], void *_args);
  54. static starpu_codelet cl = {
  55. .where = STARPU_CPU
  56. #ifdef STARPU_USE_CUDA
  57. | STARPU_CUDA
  58. #endif
  59. ,
  60. /* CPU implementation of the codelet */
  61. .cpu_func = scal_func,
  62. #ifdef STARPU_USE_CUDA
  63. /* CUDA implementation of the codelet */
  64. .cuda_func = scal_cuda_func,
  65. #endif
  66. .nbuffers = 1
  67. };
  68. int main(int argc, char **argv)
  69. {
  70. /* We consider a vector of float that is initialized just as any of C
  71. * data */
  72. float tab[N];
  73. unsigned i;
  74. for (i = 0; i < N; i++)
  75. tab[i] = 1.0f;
  76. fprintf(stderr, "BEFORE : First element was %f\n", tab[0]);
  77. /* Initialize StarPU with default configuration */
  78. starpu_init(NULL);
  79. /* Tell StaPU to associate the "tab" vector with the "tab_handle"
  80. * identifier. When a task needs to access a piece of data, it should
  81. * refer to the handle that is associated to it.
  82. * In the case of the "vector" data interface:
  83. * - the first argument of the registration method is a pointer to the
  84. * handle that should describe the data
  85. * - the second argument is the memory node where the data (ie. "tab")
  86. * resides initially: 0 stands for an address in main memory, as
  87. * opposed to an adress on a GPU for instance.
  88. * - the third argument is the adress of the vector in RAM
  89. * - the fourth argument is the number of elements in the vector
  90. * - the fifth argument is the size of each element.
  91. */
  92. starpu_data_handle tab_handle;
  93. starpu_vector_data_register(&tab_handle, 0, (uintptr_t)tab, N, sizeof(float));
  94. float factor = 3.14;
  95. /* create a synchronous task: any call to starpu_task_submit will block
  96. * until it is terminated */
  97. struct starpu_task *task = starpu_task_create();
  98. task->synchronous = 1;
  99. task->cl = &cl;
  100. /* the codelet manipulates one buffer in RW mode */
  101. task->buffers[0].handle = tab_handle;
  102. task->buffers[0].mode = STARPU_RW;
  103. /* an argument is passed to the codelet, beware that this is a
  104. * READ-ONLY buffer and that the codelet may be given a pointer to a
  105. * COPY of the argument */
  106. task->cl_arg = &factor;
  107. task->cl_arg_size = sizeof(float);
  108. /* execute the task on any eligible computational ressource */
  109. starpu_task_submit(task);
  110. /* StarPU does not need to manipulate the array anymore so we can stop
  111. * monitoring it */
  112. starpu_data_unregister(tab_handle);
  113. /* terminate StarPU, no task can be submitted after */
  114. starpu_shutdown();
  115. fprintf(stderr, "AFTER First element is %f\n", tab[0]);
  116. return 0;
  117. }