vector_scal_cpu.texi 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. @c StarPU --- Runtime system for heterogeneous multicore architectures.
  2. @c
  3. @c Copyright (C) 2009-2011 Université de Bordeaux 1
  4. @c Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  5. @c
  6. @c StarPU is free software; you can redistribute it and/or modify
  7. @c it under the terms of the GNU Lesser General Public License as published by
  8. @c the Free Software Foundation; either version 2.1 of the License, or (at
  9. @c your option) any later version.
  10. @c
  11. @c StarPU is distributed in the hope that it will be useful, but
  12. @c WITHOUT ANY WARRANTY; without even the implied warranty of
  13. @c MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. @c
  15. @c See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. #include <starpu.h>
  17. #include <xmmintrin.h>
  18. /* This kernel takes a buffer and scales it by a constant factor */
  19. void scal_cpu_func(void *buffers[], void *cl_arg)
  20. @{
  21. unsigned i;
  22. float *factor = cl_arg;
  23. /*
  24. * The "buffers" array matches the task->buffers array: for instance
  25. * task->buffers[0].handle is a handle that corresponds to a data with
  26. * vector "interface", so that the first entry of the array in the
  27. * codelet is a pointer to a structure describing such a vector (ie.
  28. * struct starpu_vector_interface_s *). Here, we therefore manipulate
  29. * the buffers[0] element as a vector: nx gives the number of elements
  30. * in the array, ptr gives the location of the array (that was possibly
  31. * migrated/replicated), and elemsize gives the size of each elements.
  32. */
  33. starpu_vector_interface_t *vector = buffers[0];
  34. /* length of the vector */
  35. unsigned n = STARPU_VECTOR_GET_NX(vector);
  36. /* get a pointer to the local copy of the vector : note that we have to
  37. * cast it in (float *) since a vector could contain any type of
  38. * elements so that the .ptr field is actually a uintptr_t */
  39. float *val = (float *)STARPU_VECTOR_GET_PTR(vector);
  40. /* scale the vector */
  41. for (i = 0; i < n; i++)
  42. val[i] *= *factor;
  43. @}
  44. void scal_sse_func(void *buffers[], void *cl_arg)
  45. @{
  46. float *vector = (float *) STARPU_VECTOR_GET_PTR(buffers[0]);
  47. unsigned int n = STARPU_VECTOR_GET_NX(buffers[0]);
  48. unsigned int n_iterations = n/4;
  49. if (n % 4 != 0)
  50. n_iterations++;
  51. __m128 *VECTOR = (__m128*) vector;
  52. __m128 factor __attribute__((aligned(16)));
  53. factor = _mm_set1_ps(*(float *) cl_arg);
  54. unsigned int i;
  55. for (i = 0; i < n_iterations; i++)
  56. VECTOR[i] = _mm_mul_ps(factor, VECTOR[i]);
  57. @}