fvector.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011 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. #include <starpu.h>
  17. #define NX 21
  18. #define PARTS 3
  19. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  20. void cpu_func(void *buffers[], void *cl_arg)
  21. {
  22. unsigned i;
  23. int *factor = cl_arg;
  24. /* length of the vector */
  25. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  26. /* local copy of the vector pointer */
  27. int *val = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  28. for (i = 0; i < n; i++)
  29. val[i] *= *factor;
  30. }
  31. int main(int argc, char **argv)
  32. {
  33. unsigned i;
  34. int vector[NX];
  35. starpu_data_handle handle;
  36. int factor=1;
  37. starpu_codelet cl = {
  38. .where = STARPU_CPU,
  39. .cpu_func = cpu_func,
  40. .nbuffers = 1
  41. };
  42. for(i=0 ; i<NX ; i++) vector[i] = i;
  43. FPRINTF(stderr,"IN Vector: ");
  44. for(i=0 ; i<NX ; i++) FPRINTF(stderr, "%5d ", vector[i]);
  45. FPRINTF(stderr,"\n");
  46. starpu_init(NULL);
  47. /* Declare data to StarPU */
  48. starpu_vector_data_register(&handle, 0, (uintptr_t)vector, NX, sizeof(vector[0]));
  49. /* Partition the vector in PARTS sub-vectors */
  50. struct starpu_data_filter f =
  51. {
  52. .filter_func = starpu_block_filter_func_vector,
  53. .nchildren = PARTS,
  54. .get_nchildren = NULL,
  55. .get_child_ops = NULL
  56. };
  57. starpu_data_partition(handle, &f);
  58. /* Submit a task on each sub-vector */
  59. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  60. {
  61. starpu_data_handle sub_handle = starpu_data_get_sub_data(handle, 1, i);
  62. struct starpu_task *task = starpu_task_create();
  63. factor *= 10;
  64. task->buffers[0].handle = sub_handle;
  65. task->buffers[0].mode = STARPU_RW;
  66. task->cl = &cl;
  67. task->synchronous = 1;
  68. task->cl_arg = &factor;
  69. task->cl_arg_size = sizeof(factor);
  70. starpu_task_submit(task);
  71. }
  72. starpu_data_unpartition(handle, 0);
  73. starpu_data_unregister(handle);
  74. starpu_shutdown();
  75. FPRINTF(stderr,"OUT Vector: ");
  76. for(i=0 ; i<NX ; i++) FPRINTF(stderr, "%5d ", vector[i]);
  77. FPRINTF(stderr,"\n");
  78. return 0;
  79. }