OriginalParallel.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*-----------------------------------------------------------
  2. ** ge_p.c -- The program is to solve a linear system Ax = b
  3. ** by using Gaussian Elimination. The algorithm on page 101
  4. ** ("Foundations of Parallel Programming") is used.
  5. ** The sequential version is ge_s.c. This parallel
  6. ** implementation converts three independent for() loops
  7. ** into three Fans. Use the data file ge_3.dat to verify
  8. ** the correction of the output.
  9. **
  10. ** Written by Andreas Kura, 02/15/95
  11. ** Modified by Chong-wei Xu, /04/20/95
  12. **-----------------------------------------------------------
  13. */
  14. #include <us.h>
  15. #include <stdio.h>
  16. int Size, t;
  17. float **a, *b;
  18. BEGIN_SHARED_DECL
  19. float **m;
  20. END_SHARED_DECL;
  21. FILE *fp;
  22. void InitProblemOnce();
  23. void InitPerRun();
  24. void ForwardSub();
  25. void Fan1();
  26. void Fan2();
  27. void Fan3();
  28. void InitMat();
  29. void InitAry();
  30. void PrintMat();
  31. void PrintAry();
  32. main ()
  33. {
  34. InitializeUs();
  35. MakeSharedVariables; /* to make SHARED m */
  36. InitProblemOnce();
  37. InitPerRun();
  38. ForwardSub();
  39. printf("The result of matrix m is: \n");
  40. PrintMat(SHARED m, Size, Size);
  41. printf("The result of matrix a is: \n");
  42. PrintMat(a, Size, Size);
  43. printf("The result of array b is: \n");
  44. PrintAry(b, Size);
  45. }
  46. /*------------------------------------------------------
  47. ** InitProblemOnce -- Initialize all of matrices and
  48. ** vectors by opening a data file specified by the user.
  49. **
  50. ** We used dynamic array **a, *b, and **m to allocate
  51. ** the memory storages.
  52. **------------------------------------------------------
  53. */
  54. void InitProblemOnce()
  55. {
  56. char filename[30];
  57. printf("Enter the data file name: ");
  58. scanf("%s", filename);
  59. printf("The file name is: %s\n", filename);
  60. fp = fopen(filename, "r");
  61. fscanf(fp, "%d", &Size);
  62. a = (float **) UsAllocScatterMatrix(Size, Size, sizeof(float));
  63. /*
  64. a = (float **) malloc(Size * sizeof(float *));
  65. for (i=0; i<Size; i++) {
  66. a[i] = (float *) malloc(Size * sizeof(float));
  67. }
  68. */
  69. InitMat(a, Size, Size);
  70. printf("The input matrix a is:\n");
  71. PrintMat(a, Size, Size);
  72. b = (float *) UsAlloc(Size * sizeof(float));
  73. /*
  74. b = (float *) malloc(Size * sizeof(float));
  75. */
  76. InitAry(b, Size);
  77. printf("The input array b is:\n");
  78. PrintAry(b, Size);
  79. SHARED m = (float **) UsAllocScatterMatrix(Size, Size, sizeof(float));
  80. /*
  81. m = (float **) malloc(Size * sizeof(float *));
  82. for (i=0; i<Size; i++) {
  83. m[i] = (float *) malloc(Size * sizeof(float));
  84. }
  85. */
  86. Share(&Size);
  87. Share(&a);
  88. Share(&b);
  89. }
  90. /*------------------------------------------------------
  91. ** InitPerRun() -- Initialize the contents of the
  92. ** multipier matrix **m
  93. **------------------------------------------------------
  94. */
  95. void InitPerRun()
  96. {
  97. int i, j;
  98. for (i=0; i<Size; i++)
  99. for (j=0; j<Size; j++)
  100. SHARED m[i][j] = 0.0;
  101. }
  102. /*------------------------------------------------------
  103. ** ForwardSub() -- Forward substitution of Gaussian
  104. ** elimination.
  105. **------------------------------------------------------
  106. */
  107. void ForwardSub()
  108. {
  109. for (t=0; t<(Size-1); t++) {
  110. Share(&t);
  111. GenOnI(Fan1, Size-1-t); /* t=0 to (Size-2), the range is
  112. ** Size-2-t+1 = Size-1-t
  113. */
  114. GenOnA(Fan2, Size-1-t, Size-t);
  115. GenOnI(Fan3, Size-1-t);
  116. }
  117. }
  118. /*-------------------------------------------------------
  119. ** Fan1() -- Calculate multiplier matrix
  120. ** Pay attention to the index. Index i give the range
  121. ** which starts from 0 to range-1. The real values of
  122. ** the index should be adjust and related with the value
  123. ** of t which is defined on the ForwardSub().
  124. **-------------------------------------------------------
  125. */
  126. void Fan1(dummy, i)
  127. int dummy, i;
  128. {
  129. /* Use these printf() to display the nodes and index */
  130. printf("from node #%d\n", PhysProcToUsProc(Proc_Node));
  131. SHARED m[i+t+1][t] = a[i+t+1][t] / a[t][t];
  132. printf("i=%d, a[%d][%d]=%.2f, a[%d][%d]=%.2f, m[%d][%d]=%.2f\n",
  133. (i+t+1),t,t,a[t][t],(i+t+1),t,a[i+t+1][t],(i+t+1),t,
  134. SHARED m[i+t+1][t]);
  135. }
  136. /*-------------------------------------------------------
  137. ** Fan2() -- Modify the matrix A into LUD
  138. **-------------------------------------------------------
  139. */
  140. void Fan2(dummy, i, j)
  141. int dummy, i, j;
  142. {
  143. a[i+1+t][j+t] -= SHARED m[i+1+t][t] * a[t][j+t];
  144. Share (&a);
  145. }
  146. /*-------------------------------------------------------
  147. ** Fan3() -- Modify the array b
  148. **-------------------------------------------------------
  149. */
  150. void Fan3(dummy, i)
  151. int dummy, i;
  152. {
  153. b[i+1+t] -= SHARED m[i+1+t][t] * b[t];
  154. }
  155. /*------------------------------------------------------
  156. ** InitMat() -- Initialize the matrix by reading data
  157. ** from the data file
  158. **------------------------------------------------------
  159. */
  160. void InitMat(ary, nrow, ncol)
  161. float **ary;
  162. int nrow, ncol;
  163. {
  164. int i, j;
  165. for (i=0; i<nrow; i++) {
  166. for (j=0; j<ncol; j++) {
  167. fscanf(fp, "%f", &ary[i][j]);
  168. }
  169. }
  170. }
  171. /*------------------------------------------------------
  172. ** PrintMat() -- Print the contents of the matrix
  173. **------------------------------------------------------
  174. */
  175. void PrintMat(ary, nrow, ncol)
  176. float **ary;
  177. int nrow, ncol;
  178. {
  179. int i, j;
  180. for (i=0; i<nrow; i++) {
  181. for (j=0; j<ncol; j++) {
  182. printf("%8.2f ", ary[i][j]);
  183. }
  184. printf("\n");
  185. }
  186. printf("\n");
  187. }
  188. /*------------------------------------------------------
  189. ** InitAry() -- Initialize the array (vector) by reading
  190. ** data from the data file
  191. **------------------------------------------------------
  192. */
  193. void InitAry(ary, ary_size)
  194. float *ary;
  195. int ary_size;
  196. {
  197. int i;
  198. for (i=0; i<ary_size; i++) {
  199. fscanf(fp, "%f", &ary[i]);
  200. }
  201. }
  202. /*------------------------------------------------------
  203. ** PrintAry() -- Print the contents of the array (vector)
  204. **------------------------------------------------------
  205. */
  206. void PrintAry(ary, ary_size)
  207. float *ary;
  208. int ary_size;
  209. {
  210. int i;
  211. for (i=0; i<ary_size; i++) {
  212. printf("%.2f ", ary[i]);
  213. }
  214. printf("\n");
  215. }