gpu_partition.c 6.1 KB

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