write_only_tmp_buffer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012 Inria
  4. * Copyright (C) 2009-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. * Test initializing a buffer with a task, then printing it with another task
  26. */
  27. #define VECTORSIZE 1024
  28. starpu_data_handle_t v_handle;
  29. #ifdef STARPU_USE_OPENCL
  30. static void opencl_codelet_null(void *descr[], void *arg)
  31. {
  32. (void)arg;
  33. STARPU_SKIP_IF_VALGRIND;
  34. cl_mem buf = (cl_mem)STARPU_VECTOR_GET_DEV_HANDLE(descr[0]);
  35. char ptr = 42;
  36. cl_command_queue queue;
  37. int id = starpu_worker_get_id_check();
  38. int devid = starpu_worker_get_devid(id);
  39. starpu_opencl_get_queue(devid, &queue);
  40. clEnqueueWriteBuffer(queue, buf, CL_TRUE, 0, sizeof(char), &ptr, 0, NULL, NULL);
  41. }
  42. #endif
  43. #ifdef STARPU_USE_CUDA
  44. static void cuda_codelet_null(void *descr[], void *arg)
  45. {
  46. (void)arg;
  47. STARPU_SKIP_IF_VALGRIND;
  48. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  49. cudaMemsetAsync(buf, 42, 1, starpu_cuda_get_local_stream());
  50. }
  51. #endif
  52. void cpu_codelet_null(void *descr[], void *arg)
  53. {
  54. (void)arg;
  55. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  56. *buf = 42;
  57. }
  58. void display_var(void *descr[], void *arg)
  59. {
  60. (void)arg;
  61. STARPU_SKIP_IF_VALGRIND;
  62. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  63. if (*buf != 42)
  64. {
  65. FPRINTF(stderr, "Value = <%c> (should be <%c>)\n", *buf, 42);
  66. exit(-1);
  67. }
  68. }
  69. static struct starpu_codelet cl =
  70. {
  71. .cpu_funcs = {cpu_codelet_null},
  72. #ifdef STARPU_USE_CUDA
  73. .cuda_funcs = {cuda_codelet_null},
  74. .cuda_flags = {STARPU_CUDA_ASYNC},
  75. #endif
  76. #ifdef STARPU_USE_OPENCL
  77. .opencl_funcs = {opencl_codelet_null},
  78. .opencl_flags = {STARPU_OPENCL_ASYNC},
  79. #endif
  80. .cpu_funcs_name = {"cpu_codelet_null"},
  81. .nbuffers = 1,
  82. .modes = {STARPU_W}
  83. };
  84. static struct starpu_codelet display_cl =
  85. {
  86. .cpu_funcs = {display_var},
  87. .cpu_funcs_name = {"display_var"},
  88. .nbuffers = 1,
  89. .modes = {STARPU_R}
  90. };
  91. int main(int argc, char **argv)
  92. {
  93. int ret;
  94. ret = starpu_initialize(NULL, &argc, &argv);
  95. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  96. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  97. /* The buffer should never be explicitely allocated */
  98. starpu_vector_data_register(&v_handle, (uint32_t)-1, (uintptr_t)NULL, VECTORSIZE, sizeof(char));
  99. struct starpu_task *task = starpu_task_create();
  100. task->cl = &cl;
  101. task->handles[0] = v_handle;
  102. task->detach = 0;
  103. ret = starpu_task_submit(task);
  104. if (ret == -ENODEV) goto enodev;
  105. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  106. ret = starpu_task_wait(task);
  107. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait");
  108. task = starpu_task_create();
  109. task->cl = &display_cl;
  110. task->handles[0] = v_handle;
  111. task->detach = 0;
  112. ret = starpu_task_submit(task);
  113. if (ret == -ENODEV) goto enodev;
  114. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  115. ret = starpu_task_wait(task);
  116. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait");
  117. /* this should get rid of automatically allocated buffers */
  118. starpu_data_unregister(v_handle);
  119. starpu_shutdown();
  120. return EXIT_SUCCESS;
  121. enodev:
  122. starpu_data_unregister(v_handle);
  123. starpu_shutdown();
  124. fprintf(stderr, "WARNING: No one can execute this task\n");
  125. /* yes, we do not perform the computation but we did detect that no one
  126. * could perform the kernel, so this is not an error from StarPU */
  127. return STARPU_TEST_SKIPPED;
  128. }