gpu_partition.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2015 Université de Bordeaux
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011, 2012, 2013, 2015 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. /*
  19. * This creates two dumb vectors, splits them into chunks, and for each pair of
  20. * chunk, run axpy on them.
  21. */
  22. #include <starpu.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <assert.h>
  26. #include <math.h>
  27. #include <common/blas.h>
  28. #ifdef STARPU_USE_CUDA
  29. #include <cublas.h>
  30. #endif
  31. #define N 512*512
  32. #define NITER 100
  33. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  34. #define EPSILON 1e-6
  35. float *_vec_x[NITER], *_vec_y[NITER];
  36. float _alpha = 3.41;
  37. /* descriptors for StarPU */
  38. starpu_data_handle_t _handle_y[NITER], _handle_x[NITER];
  39. void axpy_cpu(void *descr[], STARPU_ATTRIBUTE_UNUSED void *arg)
  40. {
  41. float alpha = *((float *)arg);
  42. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  43. float *block_x = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  44. float *block_y = (float *)STARPU_VECTOR_GET_PTR(descr[1]);
  45. unsigned i;
  46. for( i = 0; i < n; i++)
  47. block_y[i] = alpha * block_x[i] + block_y[i];
  48. }
  49. #ifdef STARPU_USE_CUDA
  50. extern void cuda_axpy(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args);
  51. #endif
  52. static struct starpu_perfmodel axpy_model =
  53. {
  54. .type = STARPU_HISTORY_BASED,
  55. .symbol = "axpy"
  56. };
  57. static struct starpu_codelet axpy_cl =
  58. {
  59. /* .cpu_funcs = {axpy_cpu}, */
  60. /* .cpu_funcs_name = {"axpy_cpu"}, */
  61. #ifdef STARPU_USE_CUDA
  62. .cuda_funcs = {cuda_axpy},
  63. #elif defined(STARPU_SIMGRID)
  64. .cuda_funcs = {(void*)1},
  65. #endif
  66. .cuda_flags = {STARPU_CUDA_ASYNC},
  67. .nbuffers = 2,
  68. .modes = {STARPU_R, STARPU_RW},
  69. .name = "axpy",
  70. .model = &axpy_model
  71. };
  72. static int
  73. check(int niter)
  74. {
  75. int i;
  76. for (i = 0; i < N; i++)
  77. {
  78. float expected_value = _alpha * _vec_x[niter][i] + 4.0;
  79. if (fabs(_vec_y[niter][i] - expected_value) > expected_value * EPSILON)
  80. {
  81. FPRINTF(stderr,"at %d, %f*%f+%f=%f, expected %f\n", i, _alpha, _vec_x[niter][i], 4.0, _vec_y[niter][i], expected_value);
  82. return EXIT_FAILURE;
  83. }
  84. }
  85. return EXIT_SUCCESS;
  86. }
  87. int main(int argc, char **argv)
  88. {
  89. int ret, exit_value = 0;
  90. int iter;
  91. /* Initialize StarPU */
  92. ret = starpu_init(NULL);
  93. if (ret == -ENODEV)
  94. return 77;
  95. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  96. /* This is equivalent to
  97. vec_a = malloc(N*sizeof(float));
  98. vec_b = malloc(N*sizeof(float));
  99. */
  100. for(iter = 0; iter < NITER; iter++)
  101. {
  102. starpu_malloc((void **)&_vec_x[iter], N*sizeof(float));
  103. assert(_vec_x[iter]);
  104. starpu_malloc((void **)&_vec_y[iter], N*sizeof(float));
  105. assert(_vec_y[iter]);
  106. unsigned i;
  107. for (i = 0; i < N; i++)
  108. {
  109. _vec_x[iter][i] = 1.0f; /*(float)starpu_drand48(); */
  110. _vec_y[iter][i] = 4.0f; /*(float)starpu_drand48(); */
  111. }
  112. /* Declare the data to StarPU */
  113. starpu_vector_data_register(&_handle_x[iter], STARPU_MAIN_RAM, (uintptr_t)_vec_x[iter], N, sizeof(float));
  114. starpu_vector_data_register(&_handle_y[iter], STARPU_MAIN_RAM, (uintptr_t)_vec_y[iter], N, sizeof(float));
  115. }
  116. double start;
  117. double end;
  118. #ifdef STARPU_USE_CUDA
  119. int gpu_devid = -1;
  120. int nfound_gpus = starpu_worker_get_devids(STARPU_CUDA_WORKER, &gpu_devid, 1);
  121. printf("gpu_devid found %d \n", gpu_devid);
  122. if(nfound_gpus == 0)
  123. return 0;
  124. unsigned nworkers = starpu_worker_get_count();
  125. int stream_workerids[nworkers];
  126. int nstreams = starpu_worker_get_stream_workerids(gpu_devid, stream_workerids, STARPU_CUDA_WORKER);
  127. int s;
  128. for(s = 0; s < nstreams; s++)
  129. printf("stream w %d \n", stream_workerids[s]);
  130. int ncpus = starpu_cpu_worker_get_count();
  131. int workers[ncpus+nstreams];
  132. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, workers, ncpus);
  133. int sched_ctxs[nstreams];
  134. int nsms[nstreams];
  135. nsms[0] = 6;
  136. nsms[1] = 7;
  137. for(s = 0; s < nstreams; s++)
  138. {
  139. sched_ctxs[s] = starpu_sched_ctx_create(&stream_workerids[s], 1, "subctx", STARPU_SCHED_CTX_CUDA_NSMS, nsms[s], 0);
  140. workers[ncpus+s] = stream_workerids[s];
  141. }
  142. 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);
  143. printf("parent ctx %d\n", sched_ctx1);
  144. starpu_sched_ctx_set_context(&sched_ctx1);
  145. #endif
  146. start = starpu_timing_now();
  147. for (iter = 0; iter < NITER; iter++)
  148. {
  149. struct starpu_task *task = starpu_task_create();
  150. task->cl = &axpy_cl;
  151. task->cl_arg = &_alpha;
  152. task->cl_arg_size = sizeof(_alpha);
  153. task->handles[0] = _handle_x[iter];
  154. task->handles[1] = _handle_y[iter];
  155. ret = starpu_task_submit(task);
  156. if (ret == -ENODEV)
  157. {
  158. exit_value = 77;
  159. goto enodev;
  160. }
  161. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  162. }
  163. starpu_task_wait_for_all();
  164. enodev:
  165. for(iter = 0; iter < NITER; iter++)
  166. {
  167. starpu_data_unregister(_handle_x[iter]);
  168. starpu_data_unregister(_handle_y[iter]);
  169. }
  170. end = starpu_timing_now();
  171. double timing = end - start;
  172. FPRINTF(stderr, "timing -> %2.2f us %2.2f MB/s\n", timing, 3*N*sizeof(float)/timing);
  173. // FPRINTF(stderr, "AFTER y[0] = %2.2f (ALPHA = %2.2f)\n", _vec_y[iter][0], _alpha);
  174. if (exit_value != 77)
  175. {
  176. for(iter = 0; iter < NITER; iter++)
  177. {
  178. exit_value = check(iter);
  179. if(exit_value != EXIT_SUCCESS)
  180. break;
  181. }
  182. }
  183. for(iter = 0; iter < NITER; iter++)
  184. {
  185. starpu_free((void *)_vec_x[iter]);
  186. starpu_free((void *)_vec_y[iter]);
  187. }
  188. /* Stop StarPU */
  189. starpu_shutdown();
  190. return exit_value;
  191. }