mult.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. *
  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. static float *A, *B, *C;
  37. static starpu_data_handle_t A_handle, B_handle, C_handle;
  38. static unsigned nslicesx = 4;
  39. static unsigned nslicesy = 4;
  40. #ifdef STARPU_QUICK_CHECK
  41. static unsigned xdim = 512;
  42. static unsigned ydim = 512;
  43. static unsigned zdim = 256;
  44. #else
  45. static unsigned xdim = 1024;
  46. static unsigned ydim = 1024;
  47. static unsigned zdim = 512;
  48. #endif
  49. /*
  50. * That program should compute C = A * B
  51. *
  52. * A of size (z,y)
  53. * B of size (x,z)
  54. * C of size (x,y)
  55. |---------------|
  56. z | B |
  57. |---------------|
  58. z x
  59. |----| |---------------|
  60. | | | |
  61. | | | |
  62. | A | y | C |
  63. | | | |
  64. | | | |
  65. |----| |---------------|
  66. */
  67. /*
  68. * The codelet is passed 3 matrices, the "descr" union-type field gives a
  69. * description of the layout of those 3 matrices in the local memory (ie. RAM
  70. * in the case of CPU, GPU frame buffer in the case of GPU etc.). Since we have
  71. * registered data with the "matrix" data interface, we use the matrix macros.
  72. */
  73. void cpu_mult(void *descr[], void *arg)
  74. {
  75. (void)arg;
  76. float *subA, *subB, *subC;
  77. uint32_t nxC, nyC, nyA;
  78. uint32_t ldA, ldB, ldC;
  79. /* .blas.ptr gives a pointer to the first element of the local copy */
  80. subA = (float *)STARPU_MATRIX_GET_PTR(descr[0]);
  81. subB = (float *)STARPU_MATRIX_GET_PTR(descr[1]);
  82. subC = (float *)STARPU_MATRIX_GET_PTR(descr[2]);
  83. /* .blas.nx is the number of rows (consecutive elements) and .blas.ny
  84. * is the number of lines that are separated by .blas.ld elements (ld
  85. * stands for leading dimension).
  86. * NB: in case some filters were used, the leading dimension is not
  87. * guaranteed to be the same in main memory (on the original matrix)
  88. * and on the accelerator! */
  89. nxC = STARPU_MATRIX_GET_NX(descr[2]);
  90. nyC = STARPU_MATRIX_GET_NY(descr[2]);
  91. nyA = STARPU_MATRIX_GET_NY(descr[0]);
  92. ldA = STARPU_MATRIX_GET_LD(descr[0]);
  93. ldB = STARPU_MATRIX_GET_LD(descr[1]);
  94. ldC = STARPU_MATRIX_GET_LD(descr[2]);
  95. /* we assume a FORTRAN-ordering! */
  96. unsigned i,j,k;
  97. for (i = 0; i < nyC; i++)
  98. {
  99. for (j = 0; j < nxC; j++)
  100. {
  101. float sum = 0.0;
  102. for (k = 0; k < nyA; k++)
  103. {
  104. sum += subA[j+k*ldA]*subB[k+i*ldB];
  105. }
  106. subC[j + i*ldC] = sum;
  107. }
  108. }
  109. }
  110. static void init_problem_data(void)
  111. {
  112. unsigned i,j;
  113. /* we initialize matrices A, B and C in the usual way */
  114. A = (float *) malloc(zdim*ydim*sizeof(float));
  115. B = (float *) malloc(xdim*zdim*sizeof(float));
  116. C = (float *) malloc(xdim*ydim*sizeof(float));
  117. /* fill the A and B matrices */
  118. starpu_srand48(2009);
  119. for (j=0; j < ydim; j++)
  120. {
  121. for (i=0; i < zdim; i++)
  122. {
  123. A[j+i*ydim] = (float)(starpu_drand48());
  124. }
  125. }
  126. for (j=0; j < zdim; j++)
  127. {
  128. for (i=0; i < xdim; i++)
  129. {
  130. B[j+i*zdim] = (float)(starpu_drand48());
  131. }
  132. }
  133. for (j=0; j < ydim; j++)
  134. {
  135. for (i=0; i < xdim; i++)
  136. {
  137. C[j+i*ydim] = (float)(0);
  138. }
  139. }
  140. }
  141. static void partition_mult_data(void)
  142. {
  143. /* note that we assume a FORTRAN ordering here! */
  144. /* The BLAS data interface is described by 4 parameters:
  145. * - the location of the first element of the matrix to monitor (3rd
  146. * argument)
  147. * - the number of elements between columns, aka leading dimension
  148. * (4th arg)
  149. * - the number of (contiguous) elements per column, ie. contiguous
  150. * elements (5th arg)
  151. * - the number of columns (6th arg)
  152. * The first elements is a pointer to the data_handle that will be
  153. * associated to the matrix, and the second elements gives the memory
  154. * node in which resides the matrix: 0 means that the 3rd argument is
  155. * an adress in main memory.
  156. */
  157. starpu_matrix_data_register(&A_handle, STARPU_MAIN_RAM, (uintptr_t)A,
  158. ydim, ydim, zdim, sizeof(float));
  159. starpu_matrix_data_register(&B_handle, STARPU_MAIN_RAM, (uintptr_t)B,
  160. zdim, zdim, xdim, sizeof(float));
  161. starpu_matrix_data_register(&C_handle, STARPU_MAIN_RAM, (uintptr_t)C,
  162. ydim, ydim, xdim, sizeof(float));
  163. /* A filter is a method to partition a data into disjoint chunks, it is
  164. * described by the means of the "struct starpu_data_filter" structure that
  165. * contains a function that is applied on a data handle to partition it
  166. * into smaller chunks, and an argument that is passed to the function
  167. * (eg. the number of blocks to create here).
  168. */
  169. /* StarPU supplies some basic filters such as the partition of a matrix
  170. * into blocks, note that we are using a FORTRAN ordering so that the
  171. * name of the filters are a bit misleading */
  172. struct starpu_data_filter vert =
  173. {
  174. .filter_func = starpu_matrix_filter_vertical_block,
  175. .nchildren = nslicesx
  176. };
  177. struct starpu_data_filter horiz =
  178. {
  179. .filter_func = starpu_matrix_filter_block,
  180. .nchildren = nslicesy
  181. };
  182. /*
  183. * Illustration with nslicex = 4 and nslicey = 2, it is possible to access
  184. * sub-data by using the "starpu_data_get_sub_data" method, which takes a data handle,
  185. * the number of filters to apply, and the indexes for each filters, for
  186. * instance:
  187. *
  188. * A' handle is starpu_data_get_sub_data(A_handle, 1, 1);
  189. * B' handle is starpu_data_get_sub_data(B_handle, 1, 2);
  190. * C' handle is starpu_data_get_sub_data(C_handle, 2, 2, 1);
  191. *
  192. * Note that here we applied 2 filters recursively onto C.
  193. *
  194. * "starpu_data_get_sub_data(C_handle, 1, 3)" would return a handle to the 4th column
  195. * of blocked matrix C for example.
  196. *
  197. * |---|---|---|---|
  198. * | | | B'| | B
  199. * |---|---|---|---|
  200. * 0 1 2 3
  201. * |----| |---|---|---|---|
  202. * | | | | | | |
  203. * | | 0 | | | | |
  204. * |----| |---|---|---|---|
  205. * | A' | | | | C'| |
  206. * | | | | | | |
  207. * |----| |---|---|---|---|
  208. * A C
  209. *
  210. * IMPORTANT: applying filters is equivalent to partitionning a piece of
  211. * data in a hierarchical manner, so that memory consistency is enforced
  212. * for each of the elements independantly. The tasks should therefore NOT
  213. * access inner nodes (eg. one column of C or the whole C) but only the
  214. * leafs of the tree (ie. blocks here). Manipulating inner nodes is only
  215. * possible by disapplying the filters (using starpu_data_unpartition), to
  216. * enforce memory consistency.
  217. */
  218. starpu_data_partition(B_handle, &vert);
  219. starpu_data_partition(A_handle, &horiz);
  220. /* starpu_data_map_filters is a variable-arity function, the first argument
  221. * is the handle of the data to partition, the second argument is the
  222. * number of filters to apply recursively. Filters are applied in the
  223. * same order as the arguments.
  224. * This would be equivalent to starpu_data_partition(C_handle, &vert) and
  225. * then applying horiz on each sub-data (ie. each column of C)
  226. */
  227. starpu_data_map_filters(C_handle, 2, &vert, &horiz);
  228. }
  229. static struct starpu_perfmodel mult_perf_model =
  230. {
  231. .type = STARPU_HISTORY_BASED,
  232. .symbol = "mult_perf_model"
  233. };
  234. static struct starpu_codelet cl =
  235. {
  236. /* we can only execute that kernel on a CPU yet */
  237. /* CPU implementation of the codelet */
  238. .cpu_funcs = {cpu_mult},
  239. .cpu_funcs_name = {"cpu_mult"},
  240. /* the codelet manipulates 3 buffers that are managed by the
  241. * DSM */
  242. .nbuffers = 3,
  243. .modes = {STARPU_R, STARPU_R, STARPU_W},
  244. /* in case the scheduling policy may use performance models */
  245. .model = &mult_perf_model
  246. };
  247. static int launch_tasks(void)
  248. {
  249. int ret;
  250. /* partition the work into slices */
  251. unsigned taskx, tasky;
  252. for (taskx = 0; taskx < nslicesx; taskx++)
  253. {
  254. for (tasky = 0; tasky < nslicesy; tasky++)
  255. {
  256. /* C[taskx, tasky] = A[tasky] B[taskx] */
  257. /* by default, starpu_task_create() returns an
  258. * asynchronous task (ie. task->synchronous = 0) */
  259. struct starpu_task *task = starpu_task_create();
  260. /* this task implements codelet "cl" */
  261. task->cl = &cl;
  262. /*
  263. * |---|---|---|---|
  264. * | | * | | | B
  265. * |---|---|---|---|
  266. * X
  267. * |----| |---|---|---|---|
  268. * |****| Y | |***| | |
  269. * |****| | |***| | |
  270. * |----| |---|---|---|---|
  271. * | | | | | | |
  272. * | | | | | | |
  273. * |----| |---|---|---|---|
  274. * A C
  275. */
  276. /* there was a single filter applied to matrices A
  277. * (respectively B) so we grab the handle to the chunk
  278. * identified by "tasky" (respectively "taskx). The "1"
  279. * tells StarPU that there is a single argument to the
  280. * variable-arity function starpu_data_get_sub_data */
  281. task->handles[0] = starpu_data_get_sub_data(A_handle, 1, tasky);
  282. task->handles[1] = starpu_data_get_sub_data(B_handle, 1, taskx);
  283. /* 2 filters were applied on matrix C, so we give
  284. * starpu_data_get_sub_data 2 arguments. The order of the arguments
  285. * must match the order in which the filters were
  286. * applied.
  287. * NB: starpu_data_get_sub_data(C_handle, 1, k) would have returned
  288. * a handle to the column number k of matrix C.
  289. * NB2: starpu_data_get_sub_data(C_handle, 2, taskx, tasky) is
  290. * equivalent to
  291. * starpu_data_get_sub_data(starpu_data_get_sub_data(C_handle, 1, taskx), 1, tasky)*/
  292. task->handles[2] = starpu_data_get_sub_data(C_handle, 2, taskx, tasky);
  293. /* this is not a blocking call since task->synchronous = 0 */
  294. ret = starpu_task_submit(task);
  295. if (ret == -ENODEV) return ret;
  296. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  297. }
  298. }
  299. return 0;
  300. }
  301. int main(void)
  302. {
  303. int ret;
  304. /* start the runtime */
  305. ret = starpu_init(NULL);
  306. if (ret == -ENODEV)
  307. return 77;
  308. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  309. /* initialize matrices A, B and C and register them to StarPU */
  310. init_problem_data();
  311. /* partition matrices into blocks that can be manipulated by the
  312. * codelets */
  313. partition_mult_data();
  314. /* submit all tasks in an asynchronous fashion */
  315. ret = launch_tasks();
  316. if (ret == -ENODEV) goto enodev;
  317. /* wait for termination */
  318. starpu_task_wait_for_all();
  319. /* remove the filters applied by the means of starpu_data_map_filters; now
  320. * it's not possible to manipulate a subset of C using starpu_data_get_sub_data until
  321. * starpu_data_map_filters is called again on C_handle.
  322. * The second argument is the memory node where the different subsets
  323. * should be reassembled, 0 = main memory (RAM) */
  324. starpu_data_unpartition(A_handle, STARPU_MAIN_RAM);
  325. starpu_data_unpartition(B_handle, STARPU_MAIN_RAM);
  326. starpu_data_unpartition(C_handle, STARPU_MAIN_RAM);
  327. /* stop monitoring matrix C : after this, it is not possible to pass C
  328. * (or any subset of C) as a codelet input/output. This also implements
  329. * a barrier so that the piece of data is put back into main memory in
  330. * case it was only available on a GPU for instance. */
  331. starpu_data_unregister(A_handle);
  332. starpu_data_unregister(B_handle);
  333. starpu_data_unregister(C_handle);
  334. free(A);
  335. free(B);
  336. free(C);
  337. starpu_shutdown();
  338. return 0;
  339. enodev:
  340. starpu_shutdown();
  341. return 77;
  342. }