vector_scal_cpu.texi 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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, 2012 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->handles array: for instance
  16. * task->handles[0] 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 *). 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. struct starpu_vector_interface *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. __m128 *VECTOR = (__m128*) vector;
  41. __m128 FACTOR __attribute__((aligned(16)));
  42. float factor = *(float *) cl_arg;
  43. FACTOR = _mm_set1_ps(factor);
  44. unsigned int i;
  45. for (i = 0; i < n_iterations; i++)
  46. VECTOR[i] = _mm_mul_ps(FACTOR, VECTOR[i]);
  47. unsigned int remainder = n%4;
  48. if (remainder != 0)
  49. @{
  50. unsigned int start = 4 * n_iterations;
  51. for (i = start; i < start+remainder; ++i)
  52. @{
  53. vector[i] = factor * vector[i];
  54. @}
  55. @}
  56. @}
  57. @end smallexample