write_only_tmp_buffer.c 4.0 KB

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