write_only_tmp_buffer.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (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. static void cuda_codelet_null(starpu_data_interface_t *buffers, __attribute__ ((unused)) void *_args)
  24. {
  25. int *buf = (int *)buffers[0].vector.ptr;
  26. cudaMemset(buf, 42, sizeof(int));
  27. }
  28. static void core_codelet_null(starpu_data_interface_t *buffers, __attribute__ ((unused)) void *_args)
  29. {
  30. int *buf = (int *)buffers[0].vector.ptr;
  31. *buf = 42;
  32. }
  33. static void display_var(starpu_data_interface_t *buffers, __attribute__ ((unused)) void *_args)
  34. {
  35. int *buf = (int *)buffers[0].vector.ptr;
  36. if (*buf != 42)
  37. {
  38. fprintf(stderr, "Value = %d (should be %d)\n", *buf, 42);
  39. exit(-1);
  40. }
  41. }
  42. static starpu_codelet cl = {
  43. .where = CORE|CUDA,
  44. .core_func = core_codelet_null,
  45. .cuda_func = cuda_codelet_null,
  46. .nbuffers = 1
  47. };
  48. static starpu_codelet display_cl = {
  49. .where = CORE,
  50. .core_func = display_var,
  51. .nbuffers = 1
  52. };
  53. int main(int argc, char **argv)
  54. {
  55. int ret;
  56. starpu_init(NULL);
  57. /* The buffer should never be explicitely allocated */
  58. starpu_register_vector_data(&v_handle, (uint32_t)-1, (uintptr_t)NULL, VECTORSIZE, sizeof(int));
  59. struct starpu_task *task = starpu_task_create();
  60. task->cl = &cl;
  61. task->buffers[0].handle = v_handle;
  62. task->buffers[0].mode = STARPU_W;
  63. task->detach = 0;
  64. ret = starpu_submit_task(task);
  65. if (ret == -ENODEV)
  66. goto enodev;
  67. ret = starpu_wait_task(task);
  68. if (ret)
  69. exit(-1);
  70. task = starpu_task_create();
  71. task->cl = &display_cl;
  72. task->buffers[0].handle = v_handle;
  73. task->buffers[0].mode = STARPU_R;
  74. task->detach = 0;
  75. ret = starpu_submit_task(task);
  76. if (ret == -ENODEV)
  77. goto enodev;
  78. ret = starpu_wait_task(task);
  79. if (ret)
  80. exit(-1);
  81. /* this should get rid of automatically allocated buffers */
  82. starpu_delete_data(v_handle);
  83. starpu_shutdown();
  84. return 0;
  85. enodev:
  86. fprintf(stderr, "WARNING: No one can execute this task\n");
  87. /* yes, we do not perform the computation but we did detect that no one
  88. * could perform the kernel, so this is not an error from StarPU */
  89. return 0;
  90. }