vector_scal_cpu.texi 1.7 KB

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