vector_scal_cpu.c 2.8 KB

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