wt_broadcast.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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 "../helper.h"
  18. /*
  19. * Test using starpu_data_set_wt_mask(handle, ~0);, i.e. broadcasting the
  20. * result on all devices 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. cl_mem d_token = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  33. unsigned h_token;
  34. cl_command_queue queue;
  35. starpu_opencl_get_current_queue(&queue);
  36. clEnqueueReadBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  37. h_token++;
  38. clEnqueueWriteBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  39. }
  40. #endif
  41. #ifdef STARPU_USE_CUDA
  42. static void increment_cuda_kernel(void *descr[], void *cl_arg)
  43. {
  44. (void)cl_arg;
  45. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  46. unsigned host_token;
  47. /* This is a dummy technique of course */
  48. cudaMemcpyAsync(&host_token, tokenptr, sizeof(unsigned), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  49. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  50. host_token++;
  51. cudaMemcpyAsync(tokenptr, &host_token, sizeof(unsigned), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  52. }
  53. #endif
  54. void increment_cpu_kernel(void *descr[], void *cl_arg)
  55. {
  56. (void)cl_arg;
  57. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  58. *tokenptr = *tokenptr + 1;
  59. }
  60. static struct starpu_codelet increment_cl =
  61. {
  62. #ifdef STARPU_USE_CUDA
  63. .cuda_funcs = {increment_cuda_kernel},
  64. .cuda_flags = {STARPU_CUDA_ASYNC},
  65. #endif
  66. #ifdef STARPU_USE_OPENCL
  67. .opencl_funcs = {increment_opencl_kernel},
  68. .opencl_flags = {STARPU_OPENCL_ASYNC},
  69. #endif
  70. .cpu_funcs = {increment_cpu_kernel},
  71. .cpu_funcs_name = {"increment_cpu_kernel"},
  72. .nbuffers = 1,
  73. .modes = {STARPU_RW}
  74. };
  75. int main(void)
  76. {
  77. int ret;
  78. ret = starpu_init(NULL);
  79. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  80. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  81. starpu_variable_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)&var, sizeof(unsigned));
  82. /* Create a mask with all the memory nodes, so that we can ask StarPU
  83. * to broadcast the handle whenever it is modified. */
  84. starpu_data_set_wt_mask(handle, ~0);
  85. #ifdef STARPU_QUICK_CHECK
  86. unsigned ntasks = 32;
  87. unsigned nloops = 4;
  88. #else
  89. unsigned ntasks = 1024;
  90. unsigned nloops = 16;
  91. #endif
  92. unsigned loop;
  93. unsigned t;
  94. for (loop = 0; loop < nloops; loop++)
  95. {
  96. for (t = 0; t < ntasks; t++)
  97. {
  98. struct starpu_task *task = starpu_task_create();
  99. task->cl = &increment_cl;
  100. task->handles[0] = handle;
  101. ret = starpu_task_submit(task);
  102. if (ret == -ENODEV) goto enodev;
  103. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  104. }
  105. }
  106. starpu_data_unregister(handle);
  107. ret = EXIT_SUCCESS;
  108. if (var != ntasks*nloops)
  109. {
  110. FPRINTF(stderr, "VAR is %u should be %u\n", var, ntasks);
  111. ret = EXIT_FAILURE;
  112. }
  113. starpu_shutdown();
  114. return ret;
  115. enodev:
  116. starpu_data_unregister(handle);
  117. fprintf(stderr, "WARNING: No one can execute this task\n");
  118. /* yes, we do not perform the computation but we did detect that no one
  119. * could perform the kernel, so this is not an error from StarPU */
  120. starpu_shutdown();
  121. return STARPU_TEST_SKIPPED;
  122. }