wt_host.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2012, 2014-2016 Université de Bordeaux
  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. #include <starpu.h>
  18. #include "../helper.h"
  19. /*
  20. * Test writing back the result into main memory as soon as it is available
  21. */
  22. static unsigned var = 0;
  23. static starpu_data_handle_t handle;
  24. /*
  25. * Increment codelet
  26. */
  27. #ifdef STARPU_USE_OPENCL
  28. /* dummy OpenCL implementation */
  29. static void increment_opencl_kernel(void *descr[], void *cl_arg)
  30. {
  31. (void)cl_arg;
  32. STARPU_SKIP_IF_VALGRIND;
  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 *cl_arg)
  44. {
  45. (void)cl_arg;
  46. STARPU_SKIP_IF_VALGRIND;
  47. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  48. unsigned host_token;
  49. /* This is a dummy technique of course */
  50. cudaMemcpyAsync(&host_token, tokenptr, sizeof(unsigned), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  51. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  52. host_token++;
  53. cudaMemcpyAsync(tokenptr, &host_token, sizeof(unsigned), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  54. }
  55. #endif
  56. void increment_cpu_kernel(void *descr[], void *cl_arg)
  57. {
  58. (void)cl_arg;
  59. STARPU_SKIP_IF_VALGRIND;
  60. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  61. *tokenptr = *tokenptr + 1;
  62. }
  63. static struct starpu_codelet increment_cl =
  64. {
  65. #ifdef STARPU_USE_CUDA
  66. .cuda_funcs = {increment_cuda_kernel},
  67. .cuda_flags = {STARPU_CUDA_ASYNC},
  68. #endif
  69. #ifdef STARPU_USE_OPENCL
  70. .opencl_funcs = {increment_opencl_kernel},
  71. .opencl_flags = {STARPU_OPENCL_ASYNC},
  72. #endif
  73. .cpu_funcs = {increment_cpu_kernel},
  74. .cpu_funcs_name = {"increment_cpu_kernel"},
  75. .nbuffers = 1,
  76. .modes = {STARPU_RW}
  77. };
  78. int main(void)
  79. {
  80. int ret;
  81. ret = starpu_init(NULL);
  82. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  83. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  84. starpu_variable_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)&var, sizeof(unsigned));
  85. /* Copy the handle in main memory every time it is modified */
  86. uint32_t wt_mask = (1<<STARPU_MAIN_RAM);
  87. starpu_data_set_wt_mask(handle, wt_mask);
  88. #ifdef STARPU_QUICK_CHECK
  89. unsigned ntasks = 32;
  90. unsigned nloops = 4;
  91. #else
  92. unsigned ntasks = 1024;
  93. unsigned nloops = 16;
  94. #endif
  95. unsigned loop;
  96. unsigned t;
  97. for (loop = 0; loop < nloops; loop++)
  98. {
  99. for (t = 0; t < ntasks; t++)
  100. {
  101. struct starpu_task *task = starpu_task_create();
  102. task->cl = &increment_cl;
  103. task->handles[0] = handle;
  104. ret = starpu_task_submit(task);
  105. if (ret == -ENODEV) goto enodev;
  106. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  107. }
  108. }
  109. starpu_data_unregister(handle);
  110. ret = EXIT_SUCCESS;
  111. if (var != ntasks*nloops)
  112. {
  113. ret = EXIT_FAILURE;
  114. FPRINTF(stderr, "VAR is %u should be %u\n", var, ntasks);
  115. }
  116. starpu_shutdown();
  117. STARPU_RETURN(ret);
  118. enodev:
  119. starpu_data_unregister(handle);
  120. fprintf(stderr, "WARNING: No one can execute this task\n");
  121. /* yes, we do not perform the computation but we did detect that no one
  122. * could perform the kernel, so this is not an error from StarPU */
  123. starpu_shutdown();
  124. return STARPU_TEST_SKIPPED;
  125. }