vector_scal_cpu.texi 2.0 KB

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