sync_with_data_with_mem.c 3.4 KB

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