fread.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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 20
  18. #define PARTS 2
  19. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  20. void display_func(void *buffers[], void *cl_arg)
  21. {
  22. unsigned i;
  23. /* length of the vector */
  24. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  25. /* local copy of the vector pointer */
  26. int *val = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  27. FPRINTF(stderr, "vector with n=%u : ", n);
  28. for (i = 0; i < n; i++)
  29. FPRINTF(stderr, "%5d ", val[i]);
  30. FPRINTF(stderr, "\n");
  31. }
  32. void cpu_func(void *buffers[], void *cl_arg)
  33. {
  34. unsigned i;
  35. /* length of the vector */
  36. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  37. /* local copy of the vector pointer */
  38. int *val = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  39. FPRINTF(stderr, "computing on vector with n=%u\n", n);
  40. for (i = 0; i < n; i++)
  41. val[i] *= 2;
  42. }
  43. int main(void)
  44. {
  45. int i;
  46. int vector[NX];
  47. starpu_data_handle_t handle;
  48. starpu_data_handle_t subhandles[PARTS];
  49. int ret;
  50. struct starpu_codelet cl =
  51. {
  52. .cpu_funcs = {cpu_func},
  53. .cpu_funcs_name = {"cpu_func"},
  54. .nbuffers = 1,
  55. .modes = {STARPU_RW},
  56. .name = "vector_scal"
  57. };
  58. struct starpu_codelet print_cl =
  59. {
  60. .cpu_funcs = {display_func},
  61. .cpu_funcs_name = {"display_func"},
  62. .nbuffers = 1,
  63. .modes = {STARPU_R},
  64. .name = "vector_display"
  65. };
  66. for(i=0 ; i<NX ; i++) vector[i] = i;
  67. ret = starpu_init(NULL);
  68. if (ret == -ENODEV)
  69. exit(77);
  70. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  71. /* Declare data to StarPU */
  72. starpu_vector_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)vector, NX, sizeof(vector[0]));
  73. /* Partition the vector in PARTS sub-vectors */
  74. struct starpu_data_filter f =
  75. {
  76. .filter_func = starpu_vector_filter_block,
  77. .nchildren = PARTS
  78. };
  79. starpu_data_partition_plan(handle, &f, subhandles);
  80. ret = starpu_task_insert(&print_cl,
  81. STARPU_R, handle,
  82. 0);
  83. if (ret == -ENODEV) goto enodev;
  84. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  85. /* Submit a task on each sub-vector */
  86. for (i=0; i<PARTS; i++)
  87. {
  88. ret = starpu_task_insert(&cl,
  89. STARPU_RW, subhandles[i],
  90. 0);
  91. if (ret == -ENODEV) goto enodev;
  92. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  93. }
  94. /* Submit a read on the whole vector */
  95. ret = starpu_task_insert(&print_cl,
  96. STARPU_R, handle,
  97. 0);
  98. if (ret == -ENODEV) goto enodev;
  99. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  100. /* Submit a read on each sub-vector */
  101. for (i=0; i<PARTS; i++)
  102. {
  103. ret = starpu_task_insert(&print_cl,
  104. STARPU_R, subhandles[i],
  105. 0);
  106. if (ret == -ENODEV) goto enodev;
  107. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  108. }
  109. /* Submit a read on the whole vector */
  110. ret = starpu_task_insert(&print_cl,
  111. STARPU_R, handle,
  112. 0);
  113. if (ret == -ENODEV) goto enodev;
  114. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  115. starpu_data_partition_clean(handle, PARTS, subhandles);
  116. starpu_data_unregister(handle);
  117. starpu_shutdown();
  118. return 0;
  119. enodev:
  120. FPRINTF(stderr, "WARNING: No one can execute this task\n");
  121. starpu_shutdown();
  122. return 77;
  123. }