vector_scal_cpu.c 2.8 KB

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