filters.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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. #include <starpu.h>
  17. #define NX 21
  18. #define PARTS 3
  19. void cpu_func(void *buffers[], void *cl_arg)
  20. {
  21. unsigned i;
  22. int *factor = cl_arg;
  23. /* length of the vector */
  24. unsigned n = STARPU_GET_VECTOR_NX(buffers[0]);
  25. /* local copy of the vector pointer */
  26. int *val = (int *)STARPU_GET_VECTOR_PTR(buffers[0]);
  27. for (i = 0; i < n; i++)
  28. val[i] *= *factor;
  29. }
  30. int main(int argc, char **argv)
  31. {
  32. unsigned i;
  33. int vector[NX];
  34. starpu_data_handle handle;
  35. int factor=1;
  36. starpu_codelet cl = {
  37. .where = STARPU_CPU,
  38. .cpu_func = cpu_func,
  39. .nbuffers = 1
  40. };
  41. for(i=0 ; i<NX ; i++) vector[i] = i;
  42. fprintf(stderr,"IN Vector: ");
  43. for(i=0 ; i<NX ; i++) fprintf(stderr, "%d ", vector[i]);
  44. fprintf(stderr,"\n");
  45. starpu_init(NULL);
  46. /* Declare data to StarPU */
  47. starpu_vector_data_register(&handle, 0, (uintptr_t)vector, NX, sizeof(vector[0]));
  48. /* Partition the vector in PARTS sub-vectors */
  49. starpu_filter f =
  50. {
  51. .filter_func = starpu_block_filter_func_vector,
  52. .filter_arg = PARTS
  53. };
  54. starpu_data_partition(handle, &f);
  55. /* Submit a task on each sub-vector */
  56. for (i=0; i<PARTS; i++)
  57. {
  58. starpu_data_handle sub_handle = starpu_data_get_sub_data(handle, 1, i);
  59. struct starpu_task *task = starpu_task_create();
  60. factor *= 10;
  61. task->buffers[0].handle = sub_handle;
  62. task->buffers[0].mode = STARPU_R;
  63. task->cl = &cl;
  64. task->synchronous = 1;
  65. task->cl_arg = &factor;
  66. task->cl_arg_size = sizeof(factor);
  67. starpu_task_submit(task);
  68. }
  69. starpu_data_unregister(handle);
  70. starpu_shutdown();
  71. fprintf(stderr,"OUT Vector: ");
  72. for(i=0 ; i<NX ; i++) fprintf(stderr, "%d ", vector[i]);
  73. fprintf(stderr,"\n");
  74. return 0;
  75. }