wt_host.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2016 Université de Bordeaux
  4. * Copyright (C) 2012,2013 Inria
  5. * Copyright (C) 2011-2013,2017 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include "../helper.h"
  20. /*
  21. * Test writing back the result into main memory as soon as it is available
  22. */
  23. static unsigned var = 0;
  24. static starpu_data_handle_t handle;
  25. /*
  26. * Increment codelet
  27. */
  28. #ifdef STARPU_USE_OPENCL
  29. /* dummy OpenCL implementation */
  30. static void increment_opencl_kernel(void *descr[], void *cl_arg)
  31. {
  32. (void)cl_arg;
  33. STARPU_SKIP_IF_VALGRIND;
  34. cl_mem d_token = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  35. unsigned h_token;
  36. cl_command_queue queue;
  37. starpu_opencl_get_current_queue(&queue);
  38. clEnqueueReadBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  39. h_token++;
  40. clEnqueueWriteBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  41. }
  42. #endif
  43. #ifdef STARPU_USE_CUDA
  44. static void increment_cuda_kernel(void *descr[], void *cl_arg)
  45. {
  46. (void)cl_arg;
  47. STARPU_SKIP_IF_VALGRIND;
  48. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  49. unsigned host_token;
  50. /* This is a dummy technique of course */
  51. cudaMemcpyAsync(&host_token, tokenptr, sizeof(unsigned), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  52. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  53. host_token++;
  54. cudaMemcpyAsync(tokenptr, &host_token, sizeof(unsigned), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  55. }
  56. #endif
  57. void increment_cpu_kernel(void *descr[], void *cl_arg)
  58. {
  59. (void)cl_arg;
  60. STARPU_SKIP_IF_VALGRIND;
  61. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  62. *tokenptr = *tokenptr + 1;
  63. }
  64. static struct starpu_codelet increment_cl =
  65. {
  66. #ifdef STARPU_USE_CUDA
  67. .cuda_funcs = {increment_cuda_kernel},
  68. .cuda_flags = {STARPU_CUDA_ASYNC},
  69. #endif
  70. #ifdef STARPU_USE_OPENCL
  71. .opencl_funcs = {increment_opencl_kernel},
  72. .opencl_flags = {STARPU_OPENCL_ASYNC},
  73. #endif
  74. .cpu_funcs = {increment_cpu_kernel},
  75. .cpu_funcs_name = {"increment_cpu_kernel"},
  76. .nbuffers = 1,
  77. .modes = {STARPU_RW}
  78. };
  79. int main(void)
  80. {
  81. int ret;
  82. ret = starpu_init(NULL);
  83. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  84. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  85. starpu_variable_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)&var, sizeof(unsigned));
  86. /* Copy the handle in main memory every time it is modified */
  87. uint32_t wt_mask = (1<<STARPU_MAIN_RAM);
  88. starpu_data_set_wt_mask(handle, wt_mask);
  89. #ifdef STARPU_QUICK_CHECK
  90. unsigned ntasks = 32;
  91. unsigned nloops = 4;
  92. #else
  93. unsigned ntasks = 1024;
  94. unsigned nloops = 16;
  95. #endif
  96. unsigned loop;
  97. unsigned t;
  98. for (loop = 0; loop < nloops; loop++)
  99. {
  100. for (t = 0; t < ntasks; t++)
  101. {
  102. struct starpu_task *task = starpu_task_create();
  103. task->cl = &increment_cl;
  104. task->handles[0] = handle;
  105. ret = starpu_task_submit(task);
  106. if (ret == -ENODEV) goto enodev;
  107. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  108. }
  109. }
  110. starpu_data_unregister(handle);
  111. ret = EXIT_SUCCESS;
  112. if (var != ntasks*nloops)
  113. {
  114. ret = EXIT_FAILURE;
  115. FPRINTF(stderr, "VAR is %u should be %u\n", var, ntasks);
  116. }
  117. starpu_shutdown();
  118. STARPU_RETURN(ret);
  119. enodev:
  120. starpu_data_unregister(handle);
  121. fprintf(stderr, "WARNING: No one can execute this task\n");
  122. /* yes, we do not perform the computation but we did detect that no one
  123. * could perform the kernel, so this is not an error from StarPU */
  124. starpu_shutdown();
  125. return STARPU_TEST_SKIPPED;
  126. }