fvector.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. .where = STARPU_CPU,
  39. .cpu_funcs = {cpu_func, NULL},
  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. };
  55. starpu_data_partition(handle, &f);
  56. /* Submit a task on each sub-vector */
  57. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  58. {
  59. starpu_data_handle_t sub_handle = starpu_data_get_sub_data(handle, 1, i);
  60. struct starpu_task *task = starpu_task_create();
  61. factor *= 10;
  62. task->buffers[0].handle = sub_handle;
  63. task->buffers[0].mode = STARPU_RW;
  64. task->cl = &cl;
  65. task->synchronous = 1;
  66. task->cl_arg = &factor;
  67. task->cl_arg_size = sizeof(factor);
  68. starpu_task_submit(task);
  69. starpu_task_destroy(task);
  70. }
  71. starpu_data_unpartition(handle, 0);
  72. starpu_data_unregister(handle);
  73. starpu_shutdown();
  74. FPRINTF(stderr,"OUT Vector: ");
  75. for(i=0 ; i<NX ; i++) FPRINTF(stderr, "%5d ", vector[i]);
  76. FPRINTF(stderr,"\n");
  77. return 0;
  78. }