fvector.c 3.1 KB

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