write_only_tmp_buffer.c 3.4 KB

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