mult.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2010 Mehdi Juhoor
  5. * Copyright (C) 2018 Alexis Juven
  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 example shows a simple implementation of a blocked matrix
  20. * multiplication. Note that this is NOT intended to be an efficient
  21. * implementation of sgemm! In this example, we show:
  22. * - how to declare dense matrices (starpu_matrix_data_register)
  23. * - how to manipulate matrices within codelets (eg. descr[0].blas.ld)
  24. * - how to use filters to partition the matrices into blocks
  25. * (starpu_data_partition and starpu_data_map_filters)
  26. * - how to unpartition data (starpu_data_unpartition) and how to stop
  27. * monitoring data (starpu_data_unregister)
  28. * - how to manipulate subsets of data (starpu_data_get_sub_data)
  29. * - how to construct an autocalibrated performance model (starpu_perfmodel)
  30. * - how to submit asynchronous tasks
  31. */
  32. #include <string.h>
  33. #include <math.h>
  34. #include <sys/types.h>
  35. #include <signal.h>
  36. #include <starpu.h>
  37. /*
  38. * That program should compute C = A * B
  39. *
  40. * A of size (z,y)
  41. * B of size (x,z)
  42. * C of size (x,y)
  43. |---------------|
  44. z | B |
  45. |---------------|
  46. z x
  47. |----| |---------------|
  48. | | | |
  49. | | | |
  50. | A | y | C |
  51. | | | |
  52. | | | |
  53. |----| |---------------|
  54. */
  55. void gpu_mult(void **, void *);
  56. void cpu_mult(void **, void *);
  57. static struct starpu_perfmodel model =
  58. {
  59. .type = STARPU_HISTORY_BASED,
  60. .symbol = "history_perf"
  61. };
  62. static struct starpu_codelet cl =
  63. {
  64. .cpu_funcs = {cpu_mult},
  65. .cpu_funcs_name = {"cpu_mult"},
  66. .cuda_funcs = {gpu_mult},
  67. .nbuffers = 3,
  68. .modes = {STARPU_R, STARPU_R, STARPU_W},
  69. .model = &model
  70. };
  71. void multiply_with_starpu(float *A, float *B, float *C, unsigned xdim, unsigned ydim, unsigned zdim, unsigned nslicesx, unsigned nslicesy)
  72. {
  73. starpu_data_handle_t A_handle, B_handle, C_handle;
  74. starpu_matrix_data_register(&A_handle, STARPU_MAIN_RAM, (uintptr_t)A,
  75. ydim, ydim, zdim, sizeof(float));
  76. starpu_matrix_data_register(&B_handle, STARPU_MAIN_RAM, (uintptr_t)B,
  77. zdim, zdim, xdim, sizeof(float));
  78. starpu_matrix_data_register(&C_handle, STARPU_MAIN_RAM, (uintptr_t)C,
  79. ydim, ydim, xdim, sizeof(float));
  80. struct starpu_data_filter vert =
  81. {
  82. .filter_func = starpu_matrix_filter_vertical_block,
  83. .nchildren = nslicesx
  84. };
  85. struct starpu_data_filter horiz =
  86. {
  87. .filter_func = starpu_matrix_filter_block,
  88. .nchildren = nslicesy
  89. };
  90. starpu_data_partition(B_handle, &vert);
  91. starpu_data_partition(A_handle, &horiz);
  92. starpu_data_map_filters(C_handle, 2, &vert, &horiz);
  93. unsigned taskx, tasky;
  94. for (taskx = 0; taskx < nslicesx; taskx++){
  95. for (tasky = 0; tasky < nslicesy; tasky++){
  96. struct starpu_task *task = starpu_task_create();
  97. task->cl = &cl;
  98. task->handles[0] = starpu_data_get_sub_data(A_handle, 1, tasky);
  99. task->handles[1] = starpu_data_get_sub_data(B_handle, 1, taskx);
  100. task->handles[2] = starpu_data_get_sub_data(C_handle, 2, taskx, tasky);
  101. starpu_task_submit(task);
  102. }
  103. }
  104. starpu_task_wait_for_all();
  105. starpu_data_unpartition(A_handle, STARPU_MAIN_RAM);
  106. starpu_data_unpartition(B_handle, STARPU_MAIN_RAM);
  107. starpu_data_unpartition(C_handle, STARPU_MAIN_RAM);
  108. starpu_data_unregister(A_handle);
  109. starpu_data_unregister(B_handle);
  110. starpu_data_unregister(C_handle);
  111. }
  112. void init_rand(float * m, unsigned width, unsigned height)
  113. {
  114. unsigned i,j;
  115. for (j = 0 ; j < height ; j++){
  116. for (i = 0 ; i < width ; i++){
  117. m[j+i*height] = (float)(starpu_drand48());
  118. }
  119. }
  120. }
  121. void init_zero(float * m, unsigned width, unsigned height)
  122. {
  123. memset(m, 0, sizeof(float) * width * height);
  124. }
  125. void sort(unsigned int size, double t[])
  126. {
  127. unsigned int j;
  128. int is_sort = 0;
  129. while(!is_sort){
  130. is_sort = 1;
  131. for (j = 0 ; j < size - 1 ; j++){
  132. if (t[j] > t[j+1]){
  133. double tmp = t[j];
  134. t[j] = t[j+1];
  135. t[j+1] = tmp;
  136. is_sort = 0;
  137. }
  138. }
  139. }
  140. }
  141. double median_time(unsigned nb_test, unsigned xdim, unsigned ydim, unsigned zdim, unsigned nsclicesx, unsigned nsclicesy)
  142. {
  143. unsigned i;
  144. float * A = (float *) malloc(zdim*ydim*sizeof(float));
  145. float * B = (float *) malloc(xdim*zdim*sizeof(float));
  146. float * C = (float *) malloc(xdim*ydim*sizeof(float));
  147. double exec_times[nb_test];
  148. for (i = 0 ; i < nb_test ; i++){
  149. double start, stop, exec_t;
  150. init_rand(A, zdim, ydim);
  151. init_rand(B, xdim, zdim);
  152. init_zero(C, xdim, ydim);
  153. start = starpu_timing_now();
  154. multiply_with_starpu(A, B, C, xdim, ydim, zdim, nsclicesx, nsclicesy);
  155. stop = starpu_timing_now();
  156. exec_t = (stop - start)/1.e6;
  157. exec_times[i] = exec_t;
  158. }
  159. sort(nb_test, exec_times);
  160. free(A);
  161. free(B);
  162. free(C);
  163. return exec_times[nb_test/2];
  164. }
  165. void display_times(unsigned start_dim, unsigned step_dim, unsigned stop_dim, unsigned nb_tests, unsigned nsclicesx, unsigned nsclicesy)
  166. {
  167. unsigned dim;
  168. for (dim = start_dim ; dim <= stop_dim ; dim += step_dim){
  169. double t = median_time(nb_tests, dim, dim, dim, nsclicesx, nsclicesy);
  170. printf("%u ; %f\n", dim, t);
  171. }
  172. }
  173. int main(int argc, char * argv[])
  174. {
  175. if (argc != 7){
  176. printf("Usage : %s start_dim step_dim stop_dim nb_tests nsclicesx nsclicesy\n", argv[0]);
  177. return 1;
  178. }
  179. if (starpu_init(NULL) != EXIT_SUCCESS){
  180. fprintf(stderr, "ERROR\n");
  181. return 77;
  182. }
  183. unsigned start_dim = (unsigned) atoi(argv[1]);
  184. unsigned step_dim = (unsigned) atoi(argv[2]);
  185. unsigned stop_dim = (unsigned) atoi(argv[3]);
  186. unsigned nb_tests = (unsigned) atoi(argv[4]);
  187. unsigned nsclicesx = (unsigned) atoi(argv[5]);
  188. unsigned nsclicesy = (unsigned) atoi(argv[6]);
  189. display_times(start_dim, step_dim, stop_dim, nb_tests, nsclicesx, nsclicesy);
  190. starpu_shutdown();
  191. return 0;
  192. }