mult.c 12 KB

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