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 = (int *) 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_t handle;
  36. int factor=1;
  37. struct starpu_codelet cl =
  38. {
  39. .where = STARPU_CPU,
  40. .cpu_funcs = {cpu_func, NULL},
  41. .nbuffers = 1
  42. };
  43. for(i=0 ; i<NX ; i++) vector[i] = i;
  44. FPRINTF(stderr,"IN Vector: ");
  45. for(i=0 ; i<NX ; i++) FPRINTF(stderr, "%5d ", vector[i]);
  46. FPRINTF(stderr,"\n");
  47. starpu_init(NULL);
  48. /* Declare data to StarPU */
  49. starpu_vector_data_register(&handle, 0, (uintptr_t)vector, NX, sizeof(vector[0]));
  50. /* Partition the vector in PARTS sub-vectors */
  51. struct starpu_data_filter f =
  52. {
  53. .filter_func = starpu_block_filter_func_vector,
  54. .nchildren = PARTS
  55. };
  56. starpu_data_partition(handle, &f);
  57. /* Submit a task on each sub-vector */
  58. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  59. {
  60. starpu_data_handle_t 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. starpu_task_destroy(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. }