vector_scal_cpu.texi 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. @copying
  2. Copyright (C) 2009-2011 Université de Bordeaux 1
  3. Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3
  6. or any later version published by the Free Software Foundation;
  7. with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
  8. See the GNU Free Documentation License in COPYING.GFDL for more details.
  9. @end copying
  10. @smallexample
  11. #include <starpu.h>
  12. #include <xmmintrin.h>
  13. /* This kernel takes a buffer and scales it by a constant factor */
  14. void scal_cpu_func(void *buffers[], void *cl_arg)
  15. @{
  16. unsigned i;
  17. float *factor = cl_arg;
  18. /*
  19. * The "buffers" array matches the task->buffers array: for instance
  20. * task->buffers[0].handle is a handle that corresponds to a data with
  21. * vector "interface", so that the first entry of the array in the
  22. * codelet is a pointer to a structure describing such a vector (ie.
  23. * struct starpu_vector_interface_s *). Here, we therefore manipulate
  24. * the buffers[0] element as a vector: nx gives the number of elements
  25. * in the array, ptr gives the location of the array (that was possibly
  26. * migrated/replicated), and elemsize gives the size of each elements.
  27. */
  28. starpu_vector_interface_t *vector = buffers[0];
  29. /* length of the vector */
  30. unsigned n = STARPU_VECTOR_GET_NX(vector);
  31. /* get a pointer to the local copy of the vector : note that we have to
  32. * cast it in (float *) since a vector could contain any type of
  33. * elements so that the .ptr field is actually a uintptr_t */
  34. float *val = (float *)STARPU_VECTOR_GET_PTR(vector);
  35. /* scale the vector */
  36. for (i = 0; i < n; i++)
  37. val[i] *= *factor;
  38. @}
  39. void scal_sse_func(void *buffers[], void *cl_arg)
  40. @{
  41. float *vector = (float *) STARPU_VECTOR_GET_PTR(buffers[0]);
  42. unsigned int n = STARPU_VECTOR_GET_NX(buffers[0]);
  43. unsigned int n_iterations = n/4;
  44. if (n % 4 != 0)
  45. n_iterations++;
  46. __m128 *VECTOR = (__m128*) vector;
  47. __m128 factor __attribute__((aligned(16)));
  48. factor = _mm_set1_ps(*(float *) cl_arg);
  49. unsigned int i;
  50. for (i = 0; i < n_iterations; i++)
  51. VECTOR[i] = _mm_mul_ps(factor, VECTOR[i]);
  52. @}
  53. @end smallexample