mult.c 12 KB

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