incrementer.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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 <string.h>
  17. #include <sys/types.h>
  18. #include <pthread.h>
  19. /* for USE_CUDA */
  20. #include <starpu_config.h>
  21. #include <starpu.h>
  22. #ifdef USE_CUDA
  23. #include <cuda.h>
  24. #include <cublas.h>
  25. #endif
  26. #define NITER 50000
  27. static starpu_data_handle my_float_state;
  28. static starpu_data_handle unity_state;
  29. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  30. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  31. unsigned size __attribute__ ((aligned (16))) = 4*sizeof(float);
  32. float my_lovely_float[4] __attribute__ ((aligned (16))) = { 0.0f, 0.0f, 0.0f};
  33. float unity[4] __attribute__ ((aligned (16))) = { 1.0f, 0.0f, 1.0f};
  34. void callback_func(void *argcb)
  35. {
  36. unsigned cnt = STARPU_ATOMIC_ADD((unsigned *)argcb, 1);
  37. if (cnt == NITER)
  38. {
  39. pthread_mutex_lock(&mutex);
  40. pthread_cond_signal(&cond);
  41. pthread_mutex_unlock(&mutex);
  42. }
  43. }
  44. void core_codelet(starpu_data_interface_t *buffers, __attribute__ ((unused)) void *_args)
  45. {
  46. float *val = (float *)buffers[0].vector.ptr;
  47. val[0] += 1.0f; val[1] += 1.0f;
  48. }
  49. #ifdef USE_CUDA
  50. void cublas_codelet(starpu_data_interface_t *buffers, __attribute__ ((unused)) void *_args)
  51. {
  52. float *val = (float *)buffers[0].vector.ptr;
  53. float *dunity = (float *)buffers[1].vector.ptr;
  54. cublasSaxpy(3, 1.0f, dunity, 1, val, 1);
  55. }
  56. #endif
  57. #ifdef USE_CUDA
  58. static struct starpu_cuda_module_s cuda_module;
  59. static struct starpu_cuda_function_s cuda_function;
  60. static starpu_cuda_codelet_t cuda_codelet;
  61. void initialize_cuda(void)
  62. {
  63. char module_path[1024];
  64. sprintf(module_path,
  65. "%s/examples/cuda/incrementer_cuda.cubin", STARPUDIR);
  66. char *function_symbol = "cuda_incrementer";
  67. starpu_init_cuda_module(&cuda_module, module_path);
  68. starpu_init_cuda_function(&cuda_function, &cuda_module, function_symbol);
  69. cuda_codelet.func = &cuda_function;
  70. cuda_codelet.gridx = 1;
  71. cuda_codelet.gridy = 1;
  72. cuda_codelet.blockx = 1;
  73. cuda_codelet.blocky = 1;
  74. cuda_codelet.shmemsize = 1024;
  75. }
  76. #endif
  77. void init_data(void)
  78. {
  79. starpu_register_vector_data(&my_float_state, 0 /* home node */,
  80. (uintptr_t)&my_lovely_float, 4, sizeof(float));
  81. starpu_register_vector_data(&unity_state, 0 /* home node */,
  82. (uintptr_t)&unity, 4, sizeof(float));
  83. }
  84. int main(int argc, char **argv)
  85. {
  86. unsigned counter = 0;
  87. starpu_init(NULL);
  88. init_data();
  89. #ifdef USE_CUDA
  90. initialize_cuda();
  91. #endif
  92. starpu_codelet cl =
  93. {
  94. .core_func = core_codelet,
  95. .where = CORE|CUDA,
  96. #ifdef USE_CUDA
  97. .cuda_func = &cuda_codelet,
  98. #endif
  99. .nbuffers = 2
  100. };
  101. unsigned i;
  102. for (i = 0; i < NITER; i++)
  103. {
  104. struct starpu_task *task = starpu_task_create();
  105. task->cl = &cl;
  106. task->callback_func = callback_func;
  107. task->callback_arg = &counter;
  108. task->cl_arg = &size;
  109. task->cl_arg_size = sizeof(unsigned);
  110. task->buffers[0].handle = my_float_state;
  111. task->buffers[0].mode = STARPU_RW;
  112. task->buffers[1].handle = unity_state;
  113. task->buffers[1].mode = STARPU_R;
  114. int ret = starpu_submit_task(task);
  115. if (STARPU_UNLIKELY(ret == -ENODEV))
  116. {
  117. fprintf(stderr, "No worker may execute this task\n");
  118. exit(0);
  119. }
  120. }
  121. pthread_mutex_lock(&mutex);
  122. pthread_cond_wait(&cond, &mutex);
  123. pthread_mutex_unlock(&mutex);
  124. starpu_sync_data_with_mem(my_float_state);
  125. fprintf(stderr, "array -> %f, %f, %f\n", my_lovely_float[0],
  126. my_lovely_float[1], my_lovely_float[2]);
  127. if (my_lovely_float[0] != my_lovely_float[1] + my_lovely_float[2])
  128. return 1;
  129. starpu_shutdown();
  130. return 0;
  131. }