fvector.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011, 2012, 2013 CNRS
  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. /*
  17. * This examplifies how to use partitioning filters. We here just split a
  18. * vector into slices, and run a dumb kernel on them.
  19. */
  20. #include <starpu.h>
  21. #define NX 21
  22. #define PARTS 3
  23. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  24. void cpu_func(void *buffers[], void *cl_arg)
  25. {
  26. unsigned i;
  27. int *factor = (int *) cl_arg;
  28. /* length of the vector */
  29. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  30. /* local copy of the vector pointer */
  31. int *val = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  32. for (i = 0; i < n; i++)
  33. val[i] *= *factor;
  34. }
  35. int main(int argc, char **argv)
  36. {
  37. int i;
  38. int vector[NX];
  39. starpu_data_handle_t handle;
  40. int factor=1;
  41. int ret;
  42. struct starpu_codelet cl =
  43. {
  44. .cpu_funcs = {cpu_func},
  45. .cpu_funcs_name = {"cpu_func"},
  46. .nbuffers = 1,
  47. .modes = {STARPU_RW},
  48. .name = "vector_scal"
  49. };
  50. for(i=0 ; i<NX ; i++) vector[i] = i;
  51. FPRINTF(stderr,"IN Vector: ");
  52. for(i=0 ; i<NX ; i++) FPRINTF(stderr, "%5d ", vector[i]);
  53. FPRINTF(stderr,"\n");
  54. ret = starpu_init(NULL);
  55. if (ret == -ENODEV)
  56. exit(77);
  57. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  58. /* Declare data to StarPU */
  59. starpu_vector_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)vector, NX, sizeof(vector[0]));
  60. /* Partition the vector in PARTS sub-vectors */
  61. struct starpu_data_filter f =
  62. {
  63. .filter_func = starpu_vector_filter_block,
  64. .nchildren = PARTS
  65. };
  66. starpu_data_partition(handle, &f);
  67. /* Submit a task on each sub-vector */
  68. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  69. {
  70. starpu_data_handle_t sub_handle = starpu_data_get_sub_data(handle, 1, i);
  71. struct starpu_task *task = starpu_task_create();
  72. factor *= 10;
  73. task->handles[0] = sub_handle;
  74. task->cl = &cl;
  75. task->synchronous = 1;
  76. task->cl_arg = &factor;
  77. task->cl_arg_size = sizeof(factor);
  78. ret = starpu_task_submit(task);
  79. if (ret == -ENODEV) goto enodev;
  80. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  81. }
  82. starpu_data_unpartition(handle, STARPU_MAIN_RAM);
  83. starpu_data_unregister(handle);
  84. starpu_shutdown();
  85. FPRINTF(stderr,"OUT Vector: ");
  86. for(i=0 ; i<NX ; i++) FPRINTF(stderr, "%5d ", vector[i]);
  87. FPRINTF(stderr,"\n");
  88. return 0;
  89. enodev:
  90. FPRINTF(stderr, "WARNING: No one can execute this task\n");
  91. starpu_shutdown();
  92. return 77;
  93. }