sync_with_data_with_mem.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-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 <stdio.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <starpu.h>
  20. #include <stdlib.h>
  21. #include "../helper.h"
  22. /*
  23. * Mix submitting tasks and synchronously acquiring the corresponding data.
  24. */
  25. #define NBUFFERS_DEF 64
  26. #define NITER_DEF 128
  27. #define VECTORSIZE_DEF 1024
  28. static int nbuffers = NBUFFERS_DEF;
  29. static int niter = NITER_DEF;
  30. static int vectorsize = VECTORSIZE_DEF;
  31. float *buffer[NBUFFERS_DEF];
  32. starpu_data_handle_t v_handle[NBUFFERS_DEF];
  33. void dummy_codelet(void *descr[], void *_args)
  34. {
  35. (void)descr;
  36. (void)_args;
  37. }
  38. static struct starpu_codelet cl =
  39. {
  40. .modes = { STARPU_RW },
  41. .cpu_funcs = {dummy_codelet},
  42. #ifdef STARPU_USE_CUDA
  43. .cuda_funcs = {dummy_codelet},
  44. #endif
  45. #ifdef STARPU_USE_OPENCL
  46. .opencl_funcs = {dummy_codelet},
  47. #endif
  48. .cpu_funcs_name = {"dummy_codelet"},
  49. .nbuffers = 1
  50. };
  51. static
  52. int use_handle(starpu_data_handle_t handle)
  53. {
  54. int ret;
  55. struct starpu_task *task;
  56. task = starpu_task_create();
  57. task->cl = &cl;
  58. task->handles[0] = handle;
  59. ret = starpu_task_submit(task);
  60. return ret;
  61. }
  62. int main(int argc, char **argv)
  63. {
  64. int ret;
  65. #ifdef STARPU_QUICK_CHECK
  66. nbuffers /= 4;
  67. niter /= 4;
  68. vectorsize /= 8;
  69. #endif
  70. ret = starpu_initialize(NULL, &argc, &argv);
  71. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  72. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  73. /* Allocate all buffers and register them to StarPU */
  74. int b;
  75. for (b = 0; b < nbuffers; b++)
  76. {
  77. ret = starpu_malloc((void **)&buffer[b], vectorsize);
  78. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  79. starpu_vector_data_register(&v_handle[b], STARPU_MAIN_RAM,
  80. (uintptr_t)buffer[b], vectorsize, sizeof(char));
  81. }
  82. int iter;
  83. for (iter = 0; iter < niter; iter++)
  84. {
  85. /* Use the buffers on the different workers so that it may not
  86. * be in main memory anymore */
  87. for (b = 0; b < nbuffers; b++)
  88. {
  89. ret = use_handle(v_handle[b]);
  90. if (ret == -ENODEV) goto enodev;
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  92. }
  93. ret = starpu_task_wait_for_all();
  94. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  95. /* Grab the different pieces of data into main memory */
  96. for (b = 0; b < nbuffers; b++)
  97. {
  98. ret = starpu_data_acquire(v_handle[b], STARPU_RW);
  99. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  100. }
  101. /* Release them */
  102. for (b = 0; b < nbuffers; b++)
  103. starpu_data_release(v_handle[b]);
  104. }
  105. /* do some cleanup */
  106. for (b = 0; b < nbuffers; b++)
  107. {
  108. starpu_data_unregister(v_handle[b]);
  109. starpu_free(buffer[b]);
  110. }
  111. starpu_shutdown();
  112. return EXIT_SUCCESS;
  113. enodev:
  114. fprintf(stderr, "WARNING: No one can execute this task\n");
  115. /* yes, we do not perform the computation but we did detect that no one
  116. * could perform the kernel, so this is not an error from StarPU */
  117. starpu_shutdown();
  118. return STARPU_TEST_SKIPPED;
  119. }