mult.c 6.2 KB

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