vector-scal.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_register_vector_data)
  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(starpu_data_interface_t *buffers, void *arg)
  29. {
  30. unsigned i;
  31. float *factor = arg;
  32. /* length of the vector */
  33. unsigned n = buffers[0].vector.nx;
  34. /* get a pointer to the local copy of the vector */
  35. float *val = (float *)buffers[0].vector.ptr;
  36. /* scale the vector */
  37. for (i = 0; i < n; i++)
  38. val[i] *= *factor;
  39. }
  40. int main(int argc, char **argv)
  41. {
  42. /* We consider a vector of float that is initialized just as any of C
  43. * data */
  44. float tab[N];
  45. unsigned i;
  46. for (i = 0; i < N; i++)
  47. tab[i] = 1.0f;
  48. fprintf(stderr, "BEFORE : First element was %f\n", tab[0]);
  49. /* Initialize StarPU with default configuration */
  50. starpu_init(NULL);
  51. /* Tell StaPU to associate the "tab" vector to the "tab_handle"
  52. * identifier. When a task needs to access a piece of data, it should
  53. * refer to the handle that is associated to it.
  54. * In the case of the "vector" data interface:
  55. * - the first argument of the registration method is a pointer to the
  56. * handle that should describe the data
  57. * - the second argument is the memory node where the data (ie. "tab")
  58. * resides initially: 0 stands for an address in main memory, as
  59. * opposed to an adress on a GPU for instance.
  60. * - the third argument is the adress of the vector in RAM
  61. * - the fourth argument is the number of elements in the vector
  62. * - the fifth argument is the size of each element.
  63. */
  64. starpu_data_handle tab_handle;
  65. starpu_register_vector_data(&tab_handle, 0, (uintptr_t)tab, N, sizeof(float));
  66. float factor = 3.14;
  67. /* create a synchronous task: any call to starpu_submit_task will block
  68. * until it is terminated */
  69. struct starpu_task *task = starpu_task_create();
  70. task->synchronous = 1;
  71. starpu_codelet cl = {
  72. .where = CORE,
  73. /* CPU implementation of the codelet */
  74. .core_func = scal_func,
  75. .nbuffers = 1
  76. };
  77. task->cl = &cl;
  78. /* the codelet manipulates one buffer in RW mode */
  79. task->buffers[0].handle = tab_handle;
  80. task->buffers[0].mode = STARPU_RW;
  81. /* an argument is passed to the codelet, beware that this is a
  82. * READ-ONLY buffer and that the codelet may be given a pointer to a
  83. * COPY of the argument */
  84. task->cl_arg = &factor;
  85. task->cl_arg_size = sizeof(float);
  86. /* execute the task on any eligible computational ressource */
  87. starpu_submit_task(task);
  88. /* StarPU does not need to manipulate the array anymore so we can stop
  89. * to monitor it */
  90. starpu_delete_data(tab_handle);
  91. /* terminate StarPU, no task can be submitted after */
  92. starpu_shutdown();
  93. fprintf(stderr, "AFTER First element is %f\n", tab[0]);
  94. return 0;
  95. }