fvector.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011, 2012, 2013 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. int i;
  34. int vector[NX];
  35. starpu_data_handle_t handle;
  36. int factor=1;
  37. int ret;
  38. struct starpu_codelet cl =
  39. {
  40. .cpu_funcs = {cpu_func, NULL},
  41. .nbuffers = 1,
  42. .modes = {STARPU_RW}
  43. };
  44. for(i=0 ; i<NX ; i++) vector[i] = i;
  45. FPRINTF(stderr,"IN Vector: ");
  46. for(i=0 ; i<NX ; i++) FPRINTF(stderr, "%5d ", vector[i]);
  47. FPRINTF(stderr,"\n");
  48. ret = starpu_init(NULL);
  49. if (ret == -ENODEV)
  50. exit(77);
  51. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  52. /* Declare data to StarPU */
  53. starpu_vector_data_register(&handle, 0, (uintptr_t)vector, NX, sizeof(vector[0]));
  54. /* Partition the vector in PARTS sub-vectors */
  55. struct starpu_data_filter f =
  56. {
  57. .filter_func = starpu_vector_filter_block,
  58. .nchildren = PARTS
  59. };
  60. starpu_data_partition(handle, &f);
  61. /* Submit a task on each sub-vector */
  62. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  63. {
  64. starpu_data_handle_t sub_handle = starpu_data_get_sub_data(handle, 1, i);
  65. struct starpu_task *task = starpu_task_create();
  66. factor *= 10;
  67. task->handles[0] = sub_handle;
  68. task->cl = &cl;
  69. task->synchronous = 1;
  70. task->cl_arg = &factor;
  71. task->cl_arg_size = sizeof(factor);
  72. ret = starpu_task_submit(task);
  73. if (ret == -ENODEV) goto enodev;
  74. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  75. }
  76. starpu_data_unpartition(handle, 0);
  77. starpu_data_unregister(handle);
  78. starpu_shutdown();
  79. FPRINTF(stderr,"OUT Vector: ");
  80. for(i=0 ; i<NX ; i++) FPRINTF(stderr, "%5d ", vector[i]);
  81. FPRINTF(stderr,"\n");
  82. return 0;
  83. enodev:
  84. FPRINTF(stderr, "WARNING: No one can execute this task\n");
  85. starpu_shutdown();
  86. return 77;
  87. }