sync_with_data_with_mem.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  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. #define NBUFFERS 64
  24. #define NITER 128
  25. #define VECTORSIZE 1024
  26. float *buffer[NBUFFERS];
  27. starpu_data_handle_t v_handle[NBUFFERS];
  28. static void dummy_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  29. {
  30. }
  31. static struct starpu_codelet cl =
  32. {
  33. .modes[0] = STARPU_RW,
  34. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  35. .cpu_funcs = {dummy_codelet, NULL},
  36. #ifdef STARPU_USE_CUDA
  37. .cuda_funcs = {dummy_codelet, NULL},
  38. #endif
  39. #ifdef STARPU_USE_OPENCL
  40. .opencl_funcs = {dummy_codelet, NULL},
  41. #endif
  42. .nbuffers = 1
  43. };
  44. int use_handle(starpu_data_handle_t handle)
  45. {
  46. int ret;
  47. struct starpu_task *task;
  48. task = starpu_task_create();
  49. task->cl = &cl;
  50. task->handles[0] = handle;
  51. task->detach = 0;
  52. ret = starpu_task_submit(task);
  53. return ret;
  54. }
  55. int main(int argc, char **argv)
  56. {
  57. int ret;
  58. ret = starpu_init(NULL);
  59. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  60. /* Allocate all buffers and register them to StarPU */
  61. unsigned b;
  62. for (b = 0; b < NBUFFERS; b++)
  63. {
  64. ret = starpu_malloc((void **)&buffer[b], VECTORSIZE);
  65. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  66. starpu_vector_data_register(&v_handle[b], 0,
  67. (uintptr_t)buffer[b], VECTORSIZE, sizeof(char));
  68. }
  69. unsigned iter;
  70. for (iter = 0; iter < NITER; iter++)
  71. {
  72. /* Use the buffers on the different workers so that it may not
  73. * be in main memory anymore */
  74. for (b = 0; b < NBUFFERS; b++)
  75. {
  76. ret = use_handle(v_handle[b]);
  77. if (ret == -ENODEV) goto enodev;
  78. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  79. }
  80. ret = starpu_task_wait_for_all();
  81. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  82. /* Grab the different pieces of data into main memory */
  83. for (b = 0; b < NBUFFERS; b++)
  84. {
  85. ret = starpu_data_acquire(v_handle[b], STARPU_RW);
  86. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  87. }
  88. /* Release them */
  89. for (b = 0; b < NBUFFERS; b++)
  90. starpu_data_release(v_handle[b]);
  91. }
  92. /* do some cleanup */
  93. for (b = 0; b < NBUFFERS; b++)
  94. {
  95. starpu_data_unregister(v_handle[b]);
  96. starpu_free(buffer[b]);
  97. }
  98. starpu_shutdown();
  99. return EXIT_SUCCESS;
  100. enodev:
  101. fprintf(stderr, "WARNING: No one can execute this task\n");
  102. /* yes, we do not perform the computation but we did detect that no one
  103. * could perform the kernel, so this is not an error from StarPU */
  104. starpu_shutdown();
  105. return STARPU_TEST_SKIPPED;
  106. }