write_only_tmp_buffer.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <starpu.h>
  20. #include <stdlib.h>
  21. #define VECTORSIZE 1024
  22. starpu_data_handle v_handle;
  23. #ifdef STARPU_USE_OPENCL
  24. #include <CL/cl.h>
  25. static void opencl_codelet_null(void *descr[], __attribute__ ((unused)) void *_args)
  26. {
  27. cl_mem buf = (cl_mem)STARPU_VECTOR_GET_PTR(descr[0]);
  28. char ptr = 42;
  29. cl_command_queue queue;
  30. int id = starpu_worker_get_id();
  31. int devid = starpu_worker_get_devid(id);
  32. starpu_opencl_get_queue(devid, &queue);
  33. clEnqueueWriteBuffer(queue, buf, CL_TRUE, 0, sizeof(char), &ptr, 0, NULL, NULL);
  34. }
  35. #endif
  36. #ifdef STARPU_USE_CUDA
  37. static void cuda_codelet_null(void *descr[], __attribute__ ((unused)) void *_args)
  38. {
  39. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  40. cudaMemset(buf, 42, 1);
  41. }
  42. #endif
  43. static void cpu_codelet_null(void *descr[], __attribute__ ((unused)) void *_args)
  44. {
  45. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  46. *buf = 42;
  47. }
  48. static void display_var(void *descr[], __attribute__ ((unused)) void *_args)
  49. {
  50. char *buf = (char *)STARPU_VECTOR_GET_PTR(descr[0]);
  51. if (*buf != 42)
  52. {
  53. fprintf(stderr, "Value = %c (should be %c)\n", *buf, 42);
  54. exit(-1);
  55. }
  56. }
  57. static starpu_codelet cl = {
  58. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  59. .cpu_func = cpu_codelet_null,
  60. #ifdef STARPU_USE_CUDA
  61. .cuda_func = cuda_codelet_null,
  62. #endif
  63. #ifdef STARPU_USE_OPENCL
  64. .opencl_func = opencl_codelet_null,
  65. #endif
  66. .nbuffers = 1
  67. };
  68. static starpu_codelet display_cl = {
  69. .where = STARPU_CPU,
  70. .cpu_func = display_var,
  71. .nbuffers = 1
  72. };
  73. int main(int argc, char **argv)
  74. {
  75. int ret;
  76. starpu_init(NULL);
  77. /* The buffer should never be explicitely allocated */
  78. starpu_vector_data_register(&v_handle, (uint32_t)-1, (uintptr_t)NULL, VECTORSIZE, sizeof(char));
  79. struct starpu_task *task = starpu_task_create();
  80. task->cl = &cl;
  81. task->buffers[0].handle = v_handle;
  82. task->buffers[0].mode = STARPU_W;
  83. task->detach = 0;
  84. ret = starpu_task_submit(task);
  85. if (ret == -ENODEV)
  86. goto enodev;
  87. ret = starpu_task_wait(task);
  88. if (ret)
  89. exit(-1);
  90. task = starpu_task_create();
  91. task->cl = &display_cl;
  92. task->buffers[0].handle = v_handle;
  93. task->buffers[0].mode = STARPU_R;
  94. task->detach = 0;
  95. ret = starpu_task_submit(task);
  96. if (ret == -ENODEV)
  97. goto enodev;
  98. ret = starpu_task_wait(task);
  99. if (ret)
  100. exit(-1);
  101. /* this should get rid of automatically allocated buffers */
  102. starpu_data_unregister(v_handle);
  103. starpu_shutdown();
  104. return 0;
  105. enodev:
  106. fprintf(stderr, "WARNING: No one can execute this task\n");
  107. /* yes, we do not perform the computation but we did detect that no one
  108. * could perform the kernel, so this is not an error from StarPU */
  109. return 0;
  110. }