axpy.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  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. #include <starpu.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <assert.h>
  22. #include <sys/time.h>
  23. #include <common/blas.h>
  24. #ifdef STARPU_USE_CUDA
  25. #include <cublas.h>
  26. #endif
  27. #define TYPE float
  28. #define AXPY SAXPY
  29. #define CUBLASAXPY cublasSaxpy
  30. #define N (16*1024*1024)
  31. #define NBLOCKS 8
  32. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  33. TYPE *vec_x, *vec_y;
  34. /* descriptors for StarPU */
  35. starpu_data_handle_t handle_y, handle_x;
  36. void axpy_cpu(void *descr[], __attribute__((unused)) void *arg)
  37. {
  38. TYPE alpha = *((TYPE *)arg);
  39. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  40. TYPE *block_x = (TYPE *)STARPU_VECTOR_GET_PTR(descr[0]);
  41. TYPE *block_y = (TYPE *)STARPU_VECTOR_GET_PTR(descr[1]);
  42. AXPY((int)n, alpha, block_x, 1, block_y, 1);
  43. }
  44. #ifdef STARPU_USE_CUDA
  45. void axpy_gpu(void *descr[], __attribute__((unused)) void *arg)
  46. {
  47. TYPE alpha = *((TYPE *)arg);
  48. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  49. TYPE *block_x = (TYPE *)STARPU_VECTOR_GET_PTR(descr[0]);
  50. TYPE *block_y = (TYPE *)STARPU_VECTOR_GET_PTR(descr[1]);
  51. CUBLASAXPY((int)n, alpha, block_x, 1, block_y, 1);
  52. cudaThreadSynchronize();
  53. }
  54. #endif
  55. static struct starpu_codelet axpy_cl =
  56. {
  57. .where =
  58. #ifdef STARPU_USE_CUDA
  59. STARPU_CUDA|
  60. #endif
  61. STARPU_CPU,
  62. .cpu_funcs = {axpy_cpu, NULL},
  63. #ifdef STARPU_USE_CUDA
  64. .cuda_funcs = {axpy_gpu, NULL},
  65. #endif
  66. .nbuffers = 2,
  67. .modes = {STARPU_R, STARPU_RW}
  68. };
  69. int main(int argc, char **argv)
  70. {
  71. int ret;
  72. /* Initialize StarPU */
  73. ret = starpu_init(NULL);
  74. if (ret == -ENODEV)
  75. return 77;
  76. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  77. starpu_helper_cublas_init();
  78. /* This is equivalent to
  79. vec_a = malloc(N*sizeof(TYPE));
  80. vec_b = malloc(N*sizeof(TYPE));
  81. */
  82. starpu_malloc((void **)&vec_x, N*sizeof(TYPE));
  83. assert(vec_x);
  84. starpu_malloc((void **)&vec_y, N*sizeof(TYPE));
  85. assert(vec_y);
  86. unsigned i;
  87. for (i = 0; i < N; i++)
  88. {
  89. vec_x[i] = 1.0f; /*(TYPE)starpu_drand48(); */
  90. vec_y[i] = 4.0f; /*(TYPE)starpu_drand48(); */
  91. }
  92. FPRINTF(stderr, "BEFORE x[0] = %2.2f\n", vec_x[0]);
  93. FPRINTF(stderr, "BEFORE y[0] = %2.2f\n", vec_y[0]);
  94. /* Declare the data to StarPU */
  95. starpu_vector_data_register(&handle_x, 0, (uintptr_t)vec_x, N, sizeof(TYPE));
  96. starpu_vector_data_register(&handle_y, 0, (uintptr_t)vec_y, N, sizeof(TYPE));
  97. /* Divide the vector into blocks */
  98. struct starpu_data_filter block_filter =
  99. {
  100. .filter_func = starpu_block_filter_func_vector,
  101. .nchildren = NBLOCKS
  102. };
  103. starpu_data_partition(handle_x, &block_filter);
  104. starpu_data_partition(handle_y, &block_filter);
  105. TYPE alpha = 3.41;
  106. struct timeval start;
  107. struct timeval end;
  108. gettimeofday(&start, NULL);
  109. unsigned b;
  110. for (b = 0; b < NBLOCKS; b++)
  111. {
  112. struct starpu_task *task = starpu_task_create();
  113. task->cl = &axpy_cl;
  114. task->cl_arg = &alpha;
  115. task->handles[0] = starpu_data_get_sub_data(handle_x, 1, b);
  116. task->handles[1] = starpu_data_get_sub_data(handle_y, 1, b);
  117. ret = starpu_task_submit(task);
  118. if (ret == -ENODEV)
  119. {
  120. ret = 77;
  121. goto enodev;
  122. }
  123. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  124. }
  125. starpu_task_wait_for_all();
  126. enodev:
  127. starpu_data_unpartition(handle_x, 0);
  128. starpu_data_unpartition(handle_y, 0);
  129. starpu_data_unregister(handle_x);
  130. starpu_data_unregister(handle_y);
  131. gettimeofday(&end, NULL);
  132. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 +
  133. (end.tv_usec - start.tv_usec));
  134. FPRINTF(stderr, "timing -> %2.2f us %2.2f MB/s\n", timing, 3*N*sizeof(TYPE)/timing);
  135. FPRINTF(stderr, "AFTER y[0] = %2.2f (ALPHA = %2.2f)\n", vec_y[0], alpha);
  136. starpu_free((void *)vec_x);
  137. starpu_free((void *)vec_y);
  138. /* Stop StarPU */
  139. starpu_shutdown();
  140. return ret;
  141. }