vector_scal_cpu.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2010 (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 complements vector_scale.c: here we implement a CPU version.
  18. */
  19. #include <starpu.h>
  20. /* This kernel takes a buffer and scales it by a constant factor */
  21. void scal_cpu_func(void *buffers[], void *cl_arg)
  22. {
  23. unsigned i;
  24. float *factor = cl_arg;
  25. /*
  26. * The "buffers" array matches the task->buffers array: for instance
  27. * task->buffers[0].handle is a handle that corresponds to a data with
  28. * vector "interface", so that the first entry of the array in the
  29. * codelet is a pointer to a structure describing such a vector (ie.
  30. * struct starpu_vector_interface_s *). Here, we therefore manipulate
  31. * the buffers[0] element as a vector: nx gives the number of elements
  32. * in the array, ptr gives the location of the array (that was possibly
  33. * migrated/replicated), and elemsize gives the size of each elements.
  34. */
  35. starpu_vector_interface_t *vector = buffers[0];
  36. /* length of the vector */
  37. unsigned n = STARPU_GET_VECTOR_NX(vector);
  38. /* get a pointer to the local copy of the vector : note that we have to
  39. * cast it in (float *) since a vector could contain any type of
  40. * elements so that the .ptr field is actually a uintptr_t */
  41. float *val = (float *)STARPU_GET_VECTOR_PTR(vector);
  42. /* scale the vector */
  43. for (i = 0; i < n; i++)
  44. val[i] *= *factor;
  45. }