fvector.c 3.3 KB

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