vector-scal.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #include <stdio.h>
  17. #include <stdint.h>
  18. #include <starpu.h>
  19. #define N 2048
  20. static void scal_func(starpu_data_interface_t *buffers, void *arg)
  21. {
  22. unsigned i;
  23. float *factor = arg;
  24. /* length of the vector */
  25. unsigned n = buffers[0].vector.nx;
  26. /* get a pointer to the local copy of the vector */
  27. float *val = (float *)buffers[0].vector.ptr;
  28. for (i = 0; i < n; i++)
  29. val[i] *= *factor;
  30. }
  31. static starpu_codelet cl = {
  32. .where = CORE,
  33. .core_func = scal_func,
  34. .nbuffers = 1
  35. };
  36. int main(int argc, char **argv)
  37. {
  38. starpu_init(NULL);
  39. float tab[N];
  40. unsigned i;
  41. for (i = 0; i < N; i++)
  42. tab[i] = 1.0f;
  43. starpu_data_handle tab_handle;
  44. starpu_register_vector_data(&tab_handle, 0, (uintptr_t)tab, N, sizeof(float));
  45. float factor = 3.14;
  46. struct starpu_task *task = starpu_task_create();
  47. task->cl = &cl;
  48. task->buffers[0].handle = tab_handle;
  49. task->buffers[0].mode = STARPU_RW;
  50. task->cl_arg = &factor;
  51. task->cl_arg_size = sizeof(float);
  52. task->synchronous = 1;
  53. fprintf(stderr, "BEFORE : First element was %f\n", tab[0]);
  54. starpu_submit_task(task);
  55. fprintf(stderr, "AFTER First element is %f\n", tab[0]);
  56. starpu_shutdown();
  57. return 0;
  58. }