wt_broadcast.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2013 Inria
  4. * Copyright (C) 2012-2013,2017 CNRS
  5. * Copyright (C) 2011-2016 Université de Bordeaux
  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 using starpu_data_set_wt_mask(handle, ~0);, i.e. broadcasting the
  22. * result on all devices as soon as it is available.
  23. */
  24. static unsigned var = 0;
  25. static starpu_data_handle_t 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)
  32. {
  33. (void)cl_arg;
  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. 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. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  60. *tokenptr = *tokenptr + 1;
  61. }
  62. static struct starpu_codelet increment_cl =
  63. {
  64. #ifdef STARPU_USE_CUDA
  65. .cuda_funcs = {increment_cuda_kernel},
  66. .cuda_flags = {STARPU_CUDA_ASYNC},
  67. #endif
  68. #ifdef STARPU_USE_OPENCL
  69. .opencl_funcs = {increment_opencl_kernel},
  70. .opencl_flags = {STARPU_OPENCL_ASYNC},
  71. #endif
  72. .cpu_funcs = {increment_cpu_kernel},
  73. .cpu_funcs_name = {"increment_cpu_kernel"},
  74. .nbuffers = 1,
  75. .modes = {STARPU_RW}
  76. };
  77. int main(void)
  78. {
  79. int ret;
  80. ret = starpu_init(NULL);
  81. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  82. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  83. starpu_variable_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)&var, sizeof(unsigned));
  84. /* Create a mask with all the memory nodes, so that we can ask StarPU
  85. * to broadcast the handle whenever it is modified. */
  86. starpu_data_set_wt_mask(handle, ~0);
  87. #ifdef STARPU_QUICK_CHECK
  88. unsigned ntasks = 32;
  89. unsigned nloops = 4;
  90. #else
  91. unsigned ntasks = 1024;
  92. unsigned nloops = 16;
  93. #endif
  94. unsigned loop;
  95. unsigned t;
  96. for (loop = 0; loop < nloops; loop++)
  97. {
  98. for (t = 0; t < ntasks; t++)
  99. {
  100. struct starpu_task *task = starpu_task_create();
  101. task->cl = &increment_cl;
  102. task->handles[0] = handle;
  103. ret = starpu_task_submit(task);
  104. if (ret == -ENODEV) goto enodev;
  105. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  106. }
  107. }
  108. starpu_data_unregister(handle);
  109. ret = EXIT_SUCCESS;
  110. if (var != ntasks*nloops)
  111. {
  112. FPRINTF(stderr, "VAR is %u should be %u\n", var, ntasks);
  113. ret = EXIT_FAILURE;
  114. }
  115. starpu_shutdown();
  116. return ret;
  117. enodev:
  118. starpu_data_unregister(handle);
  119. fprintf(stderr, "WARNING: No one can execute this task\n");
  120. /* yes, we do not perform the computation but we did detect that no one
  121. * could perform the kernel, so this is not an error from StarPU */
  122. starpu_shutdown();
  123. return STARPU_TEST_SKIPPED;
  124. }