plu_example.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2010 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <time.h>
  20. #include <math.h>
  21. #include <starpu.h>
  22. #include "pxlu.h"
  23. //#include "pxlu_kernels.h"
  24. static unsigned long size = 16384;
  25. static unsigned nblocks = 16;
  26. static unsigned check = 0;
  27. static unsigned p = 1;
  28. static unsigned q = 1;
  29. static starpu_data_handle *dataA_handles;
  30. static TYPE **dataA;
  31. /* In order to implement the distributed LU decomposition, we allocate
  32. * temporary buffers */
  33. static starpu_data_handle tmp_11_block_handle;
  34. static TYPE **tmp_11_block;
  35. static starpu_data_handle *tmp_12_block_handles;
  36. static TYPE **tmp_12_block;
  37. static starpu_data_handle *tmp_21_block_handles;
  38. static TYPE *tmp_21_block;
  39. static void parse_args(int argc, char **argv)
  40. {
  41. int i;
  42. for (i = 1; i < argc; i++) {
  43. if (strcmp(argv[i], "-size") == 0) {
  44. char *argptr;
  45. size = strtol(argv[++i], &argptr, 10);
  46. }
  47. if (strcmp(argv[i], "-nblocks") == 0) {
  48. char *argptr;
  49. nblocks = strtol(argv[++i], &argptr, 10);
  50. }
  51. if (strcmp(argv[i], "-check") == 0) {
  52. check = 1;
  53. }
  54. if (strcmp(argv[i], "-p") == 0) {
  55. char *argptr;
  56. p = strtol(argv[++i], &argptr, 10);
  57. }
  58. if (strcmp(argv[i], "-q") == 0) {
  59. char *argptr;
  60. q = strtol(argv[++i], &argptr, 10);
  61. }
  62. }
  63. }
  64. static void fill_block_with_random(TYPE *blockptr, unsigned size, unsigned nblocks)
  65. {
  66. const unsigned block_size = (size/nblocks);
  67. unsigned i, j;
  68. for (j = 0; j < block_size; j++)
  69. for (i = 0; i < block_size; i++)
  70. {
  71. blockptr[i+j*block_size] = (TYPE)drand48();
  72. // blockptr[i+j*block_size] = (i == j)?2.0:0.0;
  73. }
  74. }
  75. starpu_data_handle STARPU_PLU(get_block_handle)(unsigned j, unsigned i)
  76. {
  77. return dataA_handles[i+j*nblocks];
  78. }
  79. starpu_data_handle STARPU_PLU(get_tmp_11_block_handle)(void)
  80. {
  81. return tmp_11_block_handle;
  82. }
  83. starpu_data_handle STARPU_PLU(get_tmp_12_block_handle)(unsigned j)
  84. {
  85. return tmp_12_block_handles[j];
  86. }
  87. starpu_data_handle STARPU_PLU(get_tmp_21_block_handle)(unsigned i)
  88. {
  89. return tmp_21_block_handles[i];
  90. }
  91. TYPE *STARPU_PLU(get_block)(unsigned j, unsigned i)
  92. {
  93. return dataA[i+j*nblocks];
  94. }
  95. static void init_matrix(int rank)
  96. {
  97. /* Allocate a grid of data handles, not all of them have to be allocated later on */
  98. dataA_handles = calloc(nblocks*nblocks, sizeof(starpu_data_handle));
  99. dataA = calloc(nblocks*nblocks, sizeof(TYPE *));
  100. size_t blocksize = (size_t)(size/nblocks)*(size/nblocks)*sizeof(TYPE);
  101. /* Allocate all the blocks that belong to this mpi node */
  102. unsigned long i,j;
  103. for (j = 0; j < nblocks; j++)
  104. {
  105. for (i = 0; i < nblocks; i++)
  106. {
  107. if (get_block_rank(i, j) == rank)
  108. {
  109. /* This blocks should be treated by the current MPI process */
  110. /* Allocate and fill it */
  111. starpu_malloc_pinned_if_possible((void **)&dataA[i+j*nblocks], blocksize);
  112. fill_block_with_random(STARPU_PLU(get_block)(j, i), size, nblocks);
  113. /* Register it to StarPU */
  114. starpu_register_blas_data(&dataA_handles[i+nblocks*j], 0,
  115. (uintptr_t)dataA[i+nblocks*j], size/nblocks,
  116. size/nblocks, size/nblocks, sizeof(TYPE));
  117. }
  118. }
  119. }
  120. /* Allocate the temporary buffers required for the distributed algorithm */
  121. /* tmp buffer 11 */
  122. starpu_malloc_pinned_if_possible((void **)&tmp_11_block, blocksize);
  123. starpu_register_blas_data(&tmp_11_block_handle, 0, (uintptr_t)tmp_11_block,
  124. size/nblocks, size/nblocks, size/nblocks, sizeof(TYPE));
  125. /* tmp buffers 12 and 21 */
  126. tmp_12_block_handles = malloc(nblocks*sizeof(starpu_data_handle));
  127. tmp_21_block_handles = malloc(nblocks*sizeof(starpu_data_handle));
  128. tmp_12_block = malloc(nblocks*sizeof(TYPE *));
  129. tmp_21_block = malloc(nblocks*sizeof(TYPE *));
  130. unsigned k;
  131. for (k = 0; k < nblocks; k++)
  132. {
  133. starpu_malloc_pinned_if_possible((void **)&tmp_12_block[k], blocksize);
  134. starpu_register_blas_data(&tmp_12_block_handles[k], 0,
  135. (uintptr_t)tmp_12_block[k],
  136. size/nblocks, size/nblocks, size/nblocks, sizeof(TYPE));
  137. starpu_malloc_pinned_if_possible((void **)&tmp_21_block[k], blocksize);
  138. starpu_register_blas_data(&tmp_21_block_handles[k], 0,
  139. (uintptr_t)tmp_21_block[k],
  140. size/nblocks, size/nblocks, size/nblocks, sizeof(TYPE));
  141. }
  142. }
  143. int get_block_rank(unsigned i, unsigned j)
  144. {
  145. /* Take a 2D block cyclic distribution */
  146. /* NB: p (resp. q) is for "direction" i (resp. j) */
  147. return (j % q) * p + (i % p);
  148. }
  149. int main(int argc, char **argv)
  150. {
  151. int rank;
  152. int world_size;
  153. /*
  154. * Initialization
  155. */
  156. MPI_Init(&argc, &argv);
  157. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  158. MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  159. srand48((long int)time(NULL));
  160. parse_args(argc, argv);
  161. STARPU_ASSERT(p*q == world_size);
  162. starpu_init(NULL);
  163. starpu_mpi_initialize();
  164. starpu_helper_init_cublas();
  165. int barrier_ret = MPI_Barrier(MPI_COMM_WORLD);
  166. STARPU_ASSERT(barrier_ret == MPI_SUCCESS);
  167. /*
  168. * Problem Init
  169. */
  170. init_matrix(rank);
  171. TYPE *x, *y;
  172. if (check)
  173. {
  174. if (rank == 0)
  175. fprintf(stderr, "Compute AX = B\n");
  176. x = calloc(size, sizeof(TYPE));
  177. STARPU_ASSERT(x);
  178. y = calloc(size, sizeof(TYPE));
  179. STARPU_ASSERT(y);
  180. unsigned ind;
  181. for (ind = 0; ind < size; ind++)
  182. {
  183. //x[ind] = (TYPE)1.0;
  184. x[ind] = (TYPE)drand48();
  185. y[ind] = (TYPE)0.0;
  186. }
  187. STARPU_PLU(compute_ax)(size, x, y, nblocks, rank);
  188. if (rank == 0)
  189. for (ind = 0; ind < 10; ind++)
  190. {
  191. fprintf(stderr, "y[%d] = %f\n", ind, (float)y[ind]);
  192. }
  193. }
  194. barrier_ret = MPI_Barrier(MPI_COMM_WORLD);
  195. STARPU_ASSERT(barrier_ret == MPI_SUCCESS);
  196. fprintf(stderr, "Rank %d PID %d\n", rank, getpid());
  197. sleep(10);
  198. STARPU_PLU(plu_main)(nblocks, rank, world_size);
  199. /*
  200. * Termination
  201. */
  202. barrier_ret = MPI_Barrier(MPI_COMM_WORLD);
  203. STARPU_ASSERT(barrier_ret == MPI_SUCCESS);
  204. starpu_helper_shutdown_cublas();
  205. starpu_mpi_shutdown();
  206. starpu_shutdown();
  207. MPI_Finalize();
  208. return 0;
  209. }