wt_host.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 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 <starpu.h>
  17. #ifdef STARPU_USE_CUDA
  18. #include <starpu_cuda.h>
  19. #endif
  20. #ifdef STARPU_USE_OPENCL
  21. #include <starpu_opencl.h>
  22. #endif
  23. static unsigned var = 0;
  24. static starpu_data_handle 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 __attribute__((unused)))
  31. {
  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 *arg)
  43. {
  44. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  45. unsigned host_token;
  46. /* This is a dummy technique of course */
  47. cudaMemcpy(&host_token, tokenptr, sizeof(unsigned), cudaMemcpyDeviceToHost);
  48. cudaThreadSynchronize();
  49. host_token++;
  50. cudaMemcpy(tokenptr, &host_token, sizeof(unsigned), cudaMemcpyHostToDevice);
  51. cudaThreadSynchronize();
  52. }
  53. #endif
  54. static void increment_cpu_kernel(void *descr[], void *arg)
  55. {
  56. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  57. *tokenptr = *tokenptr + 1;
  58. }
  59. static starpu_codelet increment_cl = {
  60. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  61. #ifdef STARPU_USE_CUDA
  62. .cuda_func = increment_cuda_kernel,
  63. #endif
  64. #ifdef STARPU_USE_OPENCL
  65. .opencl_func = increment_opencl_kernel,
  66. #endif
  67. .cpu_func = increment_cpu_kernel,
  68. .nbuffers = 1
  69. };
  70. int main(int argc, char **argv)
  71. {
  72. starpu_init(NULL);
  73. starpu_variable_data_register(&handle, 0, (uintptr_t)&var, sizeof(unsigned));
  74. /* Copy the handle in main memory every time it is modified */
  75. uint32_t wt_mask = (1<<0);
  76. starpu_data_set_wt_mask(handle, wt_mask);
  77. unsigned ntasks = 2;
  78. unsigned t;
  79. for (t = 0; t < ntasks; t++)
  80. {
  81. struct starpu_task *task = starpu_task_create();
  82. task->cl = &increment_cl;
  83. task->buffers[0].mode = STARPU_RW;
  84. task->buffers[0].handle = handle;
  85. int ret = starpu_task_submit(task);
  86. STARPU_ASSERT(!ret);
  87. }
  88. starpu_data_unregister(handle);
  89. if (var != ntasks)
  90. fprintf(stderr, "VAR is %d should be %d\n", var, ntasks);
  91. STARPU_ASSERT(var == ntasks);
  92. starpu_shutdown();
  93. return 0;
  94. }