wt_host.c 3.8 KB

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