wt_host.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 Université de Bordeaux 1
  4. *
  5. * StarPU 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. * StarPU 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 <starpu.h>
  17. #include "../../common/helper.h"
  18. #ifdef STARPU_USE_CUDA
  19. #include <starpu_cuda.h>
  20. #endif
  21. #ifdef STARPU_USE_OPENCL
  22. #include <starpu_opencl.h>
  23. #endif
  24. static unsigned var = 0;
  25. static starpu_data_handle handle;
  26. /*
  27. * Increment codelet
  28. */
  29. #ifdef STARPU_USE_OPENCL
  30. /* dummy OpenCL implementation */
  31. static void increment_opencl_kernel(void *descr[], void *cl_arg __attribute__((unused)))
  32. {
  33. cl_mem d_token = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  34. unsigned h_token;
  35. cl_command_queue queue;
  36. starpu_opencl_get_current_queue(&queue);
  37. clEnqueueReadBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  38. h_token++;
  39. clEnqueueWriteBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  40. }
  41. #endif
  42. #ifdef STARPU_USE_CUDA
  43. static void increment_cuda_kernel(void *descr[], void *arg)
  44. {
  45. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  46. unsigned host_token;
  47. /* This is a dummy technique of course */
  48. cudaMemcpy(&host_token, tokenptr, sizeof(unsigned), cudaMemcpyDeviceToHost);
  49. cudaThreadSynchronize();
  50. host_token++;
  51. cudaMemcpy(tokenptr, &host_token, sizeof(unsigned), cudaMemcpyHostToDevice);
  52. cudaThreadSynchronize();
  53. }
  54. #endif
  55. static void increment_cpu_kernel(void *descr[], void *arg)
  56. {
  57. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  58. *tokenptr = *tokenptr + 1;
  59. }
  60. static starpu_codelet increment_cl = {
  61. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  62. #ifdef STARPU_USE_CUDA
  63. .cuda_func = increment_cuda_kernel,
  64. #endif
  65. #ifdef STARPU_USE_OPENCL
  66. .opencl_func = increment_opencl_kernel,
  67. #endif
  68. .cpu_func = increment_cpu_kernel,
  69. .nbuffers = 1
  70. };
  71. int main(int argc, char **argv)
  72. {
  73. int ret;
  74. ret = starpu_init(NULL);
  75. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  76. starpu_variable_data_register(&handle, 0, (uintptr_t)&var, sizeof(unsigned));
  77. /* Copy the handle in main memory every time it is modified */
  78. uint32_t wt_mask = (1<<0);
  79. starpu_data_set_wt_mask(handle, wt_mask);
  80. unsigned ntasks = 2;
  81. unsigned t;
  82. for (t = 0; t < ntasks; t++)
  83. {
  84. struct starpu_task *task = starpu_task_create();
  85. task->cl = &increment_cl;
  86. task->buffers[0].mode = STARPU_RW;
  87. task->buffers[0].handle = handle;
  88. int ret = starpu_task_submit(task);
  89. if (ret == -ENODEV) goto enodev;
  90. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  91. }
  92. starpu_data_unregister(handle);
  93. if (var != ntasks)
  94. fprintf(stderr, "VAR is %d should be %d\n", var, ntasks);
  95. STARPU_ASSERT(var == ntasks);
  96. starpu_shutdown();
  97. return 0;
  98. enodev:
  99. starpu_data_unregister(handle);
  100. fprintf(stderr, "WARNING: No one can execute this task\n");
  101. /* yes, we do not perform the computation but we did detect that no one
  102. * could perform the kernel, so this is not an error from StarPU */
  103. starpu_shutdown();
  104. return 77;
  105. }