fvector.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2013 Inria
  4. * Copyright (C) 2010-2011,2013-2015 Université de Bordeaux
  5. * Copyright (C) 2010 Mehdi Juhoor
  6. * Copyright (C) 2010-2013,2015,2017 CNRS
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. /*
  20. * This examplifies how to use partitioning filters. We here just split a
  21. * vector into slices, and run a dumb kernel on them.
  22. */
  23. #include <starpu.h>
  24. #define NX 21
  25. #define PARTS 3
  26. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  27. void cpu_func(void *buffers[], void *cl_arg)
  28. {
  29. unsigned i;
  30. int *factor = (int *) cl_arg;
  31. /* length of the vector */
  32. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  33. /* local copy of the vector pointer */
  34. int *val = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  35. for (i = 0; i < n; i++)
  36. val[i] *= *factor;
  37. }
  38. int main(void)
  39. {
  40. int i;
  41. int vector[NX];
  42. starpu_data_handle_t handle;
  43. int factor=1;
  44. int ret;
  45. struct starpu_codelet cl =
  46. {
  47. .cpu_funcs = {cpu_func},
  48. .cpu_funcs_name = {"cpu_func"},
  49. .nbuffers = 1,
  50. .modes = {STARPU_RW},
  51. .name = "vector_scal"
  52. };
  53. for(i=0 ; i<NX ; i++) vector[i] = i;
  54. FPRINTF(stderr,"IN Vector: ");
  55. for(i=0 ; i<NX ; i++) FPRINTF(stderr, "%5d ", vector[i]);
  56. FPRINTF(stderr,"\n");
  57. ret = starpu_init(NULL);
  58. if (ret == -ENODEV)
  59. exit(77);
  60. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  61. /* Declare data to StarPU */
  62. starpu_vector_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)vector, NX, sizeof(vector[0]));
  63. /* Partition the vector in PARTS sub-vectors */
  64. struct starpu_data_filter f =
  65. {
  66. .filter_func = starpu_vector_filter_block,
  67. .nchildren = PARTS
  68. };
  69. starpu_data_partition(handle, &f);
  70. /* Submit a task on each sub-vector */
  71. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  72. {
  73. starpu_data_handle_t sub_handle = starpu_data_get_sub_data(handle, 1, i);
  74. struct starpu_task *task = starpu_task_create();
  75. factor *= 10;
  76. task->handles[0] = sub_handle;
  77. task->cl = &cl;
  78. task->synchronous = 1;
  79. task->cl_arg = &factor;
  80. task->cl_arg_size = sizeof(factor);
  81. ret = starpu_task_submit(task);
  82. if (ret == -ENODEV) goto enodev;
  83. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  84. }
  85. starpu_data_unpartition(handle, STARPU_MAIN_RAM);
  86. starpu_data_unregister(handle);
  87. starpu_shutdown();
  88. FPRINTF(stderr,"OUT Vector: ");
  89. for(i=0 ; i<NX ; i++) FPRINTF(stderr, "%5d ", vector[i]);
  90. FPRINTF(stderr,"\n");
  91. return 0;
  92. enodev:
  93. FPRINTF(stderr, "WARNING: No one can execute this task\n");
  94. starpu_shutdown();
  95. return 77;
  96. }