mult.c 6.6 KB

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