fvector.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. struct starpu_data_filter f =
  50. {
  51. .filter_func = starpu_block_filter_func_vector,
  52. .nchildren = PARTS,
  53. .get_nchildren = NULL,
  54. .get_child_ops = NULL
  55. };
  56. starpu_data_partition(handle, &f);
  57. /* Submit a task on each sub-vector */
  58. for (i=0; i<PARTS; i++)
  59. {
  60. starpu_data_handle sub_handle = starpu_data_get_sub_data(handle, 1, i);
  61. struct starpu_task *task = starpu_task_create();
  62. factor *= 10;
  63. task->buffers[0].handle = sub_handle;
  64. task->buffers[0].mode = STARPU_RW;
  65. task->cl = &cl;
  66. task->synchronous = 1;
  67. task->cl_arg = &factor;
  68. task->cl_arg_size = sizeof(factor);
  69. starpu_task_submit(task);
  70. }
  71. starpu_data_unregister(handle);
  72. starpu_shutdown();
  73. fprintf(stderr,"OUT Vector: ");
  74. for(i=0 ; i<NX ; i++) fprintf(stderr, "%d ", vector[i]);
  75. fprintf(stderr,"\n");
  76. return 0;
  77. }