gpu_partition.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2016 Uppsala University
  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. /*
  17. * This creates two dumb vectors & run axpy on them.
  18. * You have to set STARPU_NWORKER_PER_CUDA=2
  19. */
  20. #include <starpu.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <assert.h>
  24. #include <math.h>
  25. #include <common/blas.h>
  26. #ifdef STARPU_USE_CUDA
  27. #include <cublas.h>
  28. #endif
  29. #define N 512*512
  30. #define NITER 100
  31. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  32. #define EPSILON 1e-6
  33. float *_vec_x[NITER], *_vec_y[NITER];
  34. float _alpha = 3.41;
  35. /* descriptors for StarPU */
  36. starpu_data_handle_t _handle_y[NITER], _handle_x[NITER];
  37. void axpy_cpu(void *descr[], STARPU_ATTRIBUTE_UNUSED void *arg)
  38. {
  39. float alpha = *((float *)arg);
  40. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  41. float *block_x = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  42. float *block_y = (float *)STARPU_VECTOR_GET_PTR(descr[1]);
  43. unsigned i;
  44. for( i = 0; i < n; i++)
  45. block_y[i] = alpha * block_x[i] + block_y[i];
  46. }
  47. #ifdef STARPU_USE_CUDA
  48. extern void cuda_axpy(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args);
  49. #endif
  50. static struct starpu_perfmodel axpy_model =
  51. {
  52. .type = STARPU_HISTORY_BASED,
  53. .symbol = "axpy"
  54. };
  55. static struct starpu_codelet axpy_cl =
  56. {
  57. /* .cpu_funcs = {axpy_cpu}, */
  58. /* .cpu_funcs_name = {"axpy_cpu"}, */
  59. #ifdef STARPU_USE_CUDA
  60. .cuda_funcs = {cuda_axpy},
  61. #elif defined(STARPU_SIMGRID)
  62. .cuda_funcs = {(void*)1},
  63. #endif
  64. .cuda_flags = {STARPU_CUDA_ASYNC},
  65. .nbuffers = 2,
  66. .modes = {STARPU_R, STARPU_RW},
  67. .name = "axpy",
  68. .model = &axpy_model
  69. };
  70. static int
  71. check(int niter)
  72. {
  73. int i;
  74. for (i = 0; i < N; i++)
  75. {
  76. float expected_value = _alpha * _vec_x[niter][i] + 4.0;
  77. if (fabs(_vec_y[niter][i] - expected_value) > expected_value * EPSILON)
  78. {
  79. FPRINTF(stderr,"[error] at %d, %f*%f+%f==%f, expected %f\n", i, _alpha, _vec_x[niter][i], 4.0, _vec_y[niter][i], expected_value);
  80. return EXIT_FAILURE;
  81. }
  82. }
  83. return EXIT_SUCCESS;
  84. }
  85. int main(int argc, char **argv)
  86. {
  87. int ret, exit_value = 0;
  88. int iter;
  89. int ncuda = 0;
  90. int gpu_devid = -1;
  91. #ifndef STARPU_HAVE_SETENV
  92. return STARPU_TEST_SKIPPED;
  93. #endif
  94. /* Request separate threads for streams */
  95. setenv("STARPU_ONE_THREAD_PER_STREAM", "1");
  96. /* Initialize StarPU */
  97. ret = starpu_init(NULL);
  98. if (ret == -ENODEV)
  99. return 77;
  100. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  101. #ifdef STARPU_USE_CUDA
  102. ncuda = starpu_worker_get_devids(STARPU_CUDA_WORKER, &gpu_devid, 1);
  103. FPRINTF(stderr, "gpu_devid found %d \n", gpu_devid);
  104. #endif
  105. if (ncuda == 0)
  106. {
  107. starpu_shutdown();
  108. return 77;
  109. }
  110. /* This is equivalent to
  111. vec_a = malloc(N*sizeof(float));
  112. vec_b = malloc(N*sizeof(float));
  113. */
  114. for(iter = 0; iter < NITER; iter++)
  115. {
  116. starpu_malloc((void **)&_vec_x[iter], N*sizeof(float));
  117. assert(_vec_x[iter]);
  118. starpu_malloc((void **)&_vec_y[iter], N*sizeof(float));
  119. assert(_vec_y[iter]);
  120. unsigned i;
  121. for (i = 0; i < N; i++)
  122. {
  123. _vec_x[iter][i] = 1.0f; /*(float)starpu_drand48(); */
  124. _vec_y[iter][i] = 4.0f; /*(float)starpu_drand48(); */
  125. }
  126. /* Declare the data to StarPU */
  127. starpu_vector_data_register(&_handle_x[iter], STARPU_MAIN_RAM, (uintptr_t)_vec_x[iter], N, sizeof(float));
  128. starpu_vector_data_register(&_handle_y[iter], STARPU_MAIN_RAM, (uintptr_t)_vec_y[iter], N, sizeof(float));
  129. }
  130. double start;
  131. double end;
  132. #ifdef STARPU_USE_CUDA
  133. unsigned nworkers = starpu_worker_get_count();
  134. int stream_workerids[nworkers];
  135. int nstreams = starpu_worker_get_stream_workerids(gpu_devid, stream_workerids, STARPU_CUDA_WORKER);
  136. int s;
  137. for(s = 0; s < nstreams; s++)
  138. FPRINTF(stderr, "stream w %d \n", stream_workerids[s]);
  139. int ncpus = starpu_cpu_worker_get_count();
  140. int workers[ncpus+nstreams];
  141. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, workers, ncpus);
  142. int sched_ctxs[nstreams];
  143. int nsms[nstreams];
  144. nsms[0] = 6;
  145. nsms[1] = 7;
  146. for(s = 0; s < nstreams; s++)
  147. {
  148. sched_ctxs[s] = starpu_sched_ctx_create(&stream_workerids[s], 1, "subctx", STARPU_SCHED_CTX_CUDA_NSMS, nsms[s], 0);
  149. workers[ncpus+s] = stream_workerids[s];
  150. }
  151. unsigned sched_ctx1 = starpu_sched_ctx_create(workers, ncpus+nstreams, "ctx1", STARPU_SCHED_CTX_SUB_CTXS, sched_ctxs, nstreams, STARPU_SCHED_CTX_POLICY_NAME, "dmdas", 0);
  152. FPRINTF(stderr, "parent ctx %d\n", sched_ctx1);
  153. starpu_sched_ctx_set_context(&sched_ctx1);
  154. #endif
  155. start = starpu_timing_now();
  156. for (iter = 0; iter < NITER; iter++)
  157. {
  158. struct starpu_task *task = starpu_task_create();
  159. task->cl = &axpy_cl;
  160. task->cl_arg = &_alpha;
  161. task->cl_arg_size = sizeof(_alpha);
  162. task->handles[0] = _handle_x[iter];
  163. task->handles[1] = _handle_y[iter];
  164. ret = starpu_task_submit(task);
  165. if (ret == -ENODEV)
  166. {
  167. exit_value = 77;
  168. goto enodev;
  169. }
  170. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  171. }
  172. starpu_task_wait_for_all();
  173. enodev:
  174. for(iter = 0; iter < NITER; iter++)
  175. {
  176. starpu_data_unregister(_handle_x[iter]);
  177. starpu_data_unregister(_handle_y[iter]);
  178. }
  179. end = starpu_timing_now();
  180. double timing = end - start;
  181. FPRINTF(stderr, "timing -> %2.2f us %2.2f MB/s\n", timing, 3*N*sizeof(float)/timing);
  182. // FPRINTF(stderr, "AFTER y[0] = %2.2f (ALPHA = %2.2f)\n", _vec_y[iter][0], _alpha);
  183. if (exit_value != 77)
  184. {
  185. for(iter = 0; iter < NITER; iter++)
  186. {
  187. exit_value = check(iter);
  188. if(exit_value != EXIT_SUCCESS)
  189. break;
  190. }
  191. }
  192. for(iter = 0; iter < NITER; iter++)
  193. {
  194. starpu_free((void *)_vec_x[iter]);
  195. starpu_free((void *)_vec_y[iter]);
  196. }
  197. /* Stop StarPU */
  198. starpu_shutdown();
  199. return exit_value;
  200. }