sync_with_data_with_mem.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2013 Inria
  4. * Copyright (C) 2010-2014,2016 Université de Bordeaux
  5. * Copyright (C) 2010-2013,2015,2017 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <errno.h>
  21. #include <starpu.h>
  22. #include <stdlib.h>
  23. #include "../helper.h"
  24. /*
  25. * Mix submitting tasks and synchronously acquiring the corresponding data.
  26. */
  27. #define NBUFFERS_DEF 64
  28. #define NITER_DEF 128
  29. #define VECTORSIZE_DEF 1024
  30. static int nbuffers = NBUFFERS_DEF;
  31. static int niter = NITER_DEF;
  32. static int vectorsize = VECTORSIZE_DEF;
  33. float *buffer[NBUFFERS_DEF];
  34. starpu_data_handle_t v_handle[NBUFFERS_DEF];
  35. void dummy_codelet(void *descr[], void *_args)
  36. {
  37. (void)descr;
  38. (void)_args;
  39. }
  40. static struct starpu_codelet cl =
  41. {
  42. .modes = { STARPU_RW },
  43. .cpu_funcs = {dummy_codelet},
  44. #ifdef STARPU_USE_CUDA
  45. .cuda_funcs = {dummy_codelet},
  46. #endif
  47. #ifdef STARPU_USE_OPENCL
  48. .opencl_funcs = {dummy_codelet},
  49. #endif
  50. .cpu_funcs_name = {"dummy_codelet"},
  51. .nbuffers = 1
  52. };
  53. static
  54. int use_handle(starpu_data_handle_t handle)
  55. {
  56. int ret;
  57. struct starpu_task *task;
  58. task = starpu_task_create();
  59. task->cl = &cl;
  60. task->handles[0] = handle;
  61. ret = starpu_task_submit(task);
  62. return ret;
  63. }
  64. int main(int argc, char **argv)
  65. {
  66. int ret;
  67. #ifdef STARPU_QUICK_CHECK
  68. nbuffers /= 4;
  69. niter /= 4;
  70. vectorsize /= 8;
  71. #endif
  72. ret = starpu_initialize(NULL, &argc, &argv);
  73. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  74. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  75. /* Allocate all buffers and register them to StarPU */
  76. int b;
  77. for (b = 0; b < nbuffers; b++)
  78. {
  79. ret = starpu_malloc((void **)&buffer[b], vectorsize);
  80. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  81. starpu_vector_data_register(&v_handle[b], STARPU_MAIN_RAM,
  82. (uintptr_t)buffer[b], vectorsize, sizeof(char));
  83. }
  84. int iter;
  85. for (iter = 0; iter < niter; iter++)
  86. {
  87. /* Use the buffers on the different workers so that it may not
  88. * be in main memory anymore */
  89. for (b = 0; b < nbuffers; b++)
  90. {
  91. ret = use_handle(v_handle[b]);
  92. if (ret == -ENODEV) goto enodev;
  93. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  94. }
  95. ret = starpu_task_wait_for_all();
  96. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  97. /* Grab the different pieces of data into main memory */
  98. for (b = 0; b < nbuffers; b++)
  99. {
  100. ret = starpu_data_acquire(v_handle[b], STARPU_RW);
  101. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  102. }
  103. /* Release them */
  104. for (b = 0; b < nbuffers; b++)
  105. starpu_data_release(v_handle[b]);
  106. }
  107. /* do some cleanup */
  108. for (b = 0; b < nbuffers; b++)
  109. {
  110. starpu_data_unregister(v_handle[b]);
  111. starpu_free(buffer[b]);
  112. }
  113. starpu_shutdown();
  114. return EXIT_SUCCESS;
  115. enodev:
  116. fprintf(stderr, "WARNING: No one can execute this task\n");
  117. /* yes, we do not perform the computation but we did detect that no one
  118. * could perform the kernel, so this is not an error from StarPU */
  119. starpu_shutdown();
  120. return STARPU_TEST_SKIPPED;
  121. }