write_only_tmp_buffer.c 3.8 KB

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