wt_host.c 3.7 KB

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