wt_broadcast.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. unsigned nworkers = starpu_worker_get_count();
  74. starpu_variable_data_register(&handle, 0, (uintptr_t)&var, sizeof(unsigned));
  75. /* Create a mask with all the memory nodes, so that we can ask StarPU
  76. * to broadcast the handle whenever it is modified. */
  77. uint32_t wt_mask = 0;
  78. int id;
  79. for (id = 0; id < nworkers; id++)
  80. {
  81. unsigned node = starpu_worker_get_memory_node(id);
  82. wt_mask |= (1<<node);
  83. }
  84. starpu_data_set_wt_mask(handle, wt_mask);
  85. unsigned ntasks = 1024;
  86. unsigned nloops = 16;
  87. unsigned loop;
  88. unsigned t;
  89. for (loop = 0; loop < nloops; loop++)
  90. {
  91. for (t = 0; t < ntasks; t++)
  92. {
  93. struct starpu_task *task = starpu_task_create();
  94. task->cl = &increment_cl;
  95. task->buffers[0].mode = STARPU_RW;
  96. task->buffers[0].handle = handle;
  97. int ret = starpu_task_submit(task);
  98. STARPU_ASSERT(!ret);
  99. }
  100. }
  101. starpu_data_unregister(handle);
  102. STARPU_ASSERT(var == ntasks*nloops);
  103. starpu_shutdown();
  104. return 0;
  105. }