vector_scal_cpu.texi 2.0 KB

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