source: CIVL/examples/mpi-omp/mpi-omp-mat-vect-mult-blkstp.c

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 6.3 KB
Line 
1#ifdef _CIVL
2#include <civlc.cvh>
3#endif
4/*********************************************************************
5 C-DAC Tech Workshop : HeGaPa-2012
6 July 16-20,2012
7
8 Example 4 : Mpi-Omp_MatVect_Mult_blkstp.c
9
10 Objective : Write an MPI-OpenMP Program to perform Matrix-Vector
11 Multiplication
12
13 Input : Process 0 reads files (mdata.inp) for Matrix and
14 (vdata.inp) for Vector
15
16 Output : Process 0 prints the result of Matrix_Vector
17 Multiplication
18
19 Created : MAY-2012
20**********************************************************************/
21
22
23
24#include <stdio.h>
25#include "mpi.h"
26#include <stdlib.h>
27#include<omp.h>
28#include<math.h>
29
30/* Main Program */
31
32int main(int argc, char **argv)
33{
34
35 int Numprocs, MyRank, iam;
36 int NoofCols, NoofRows, VectorSize, ScatterSize;
37 int index, irow, icol, iproc;
38 int Root = 0, ValidOutput = 1;
39 float **Matrix, *Buffer, *Mybuffer, *Vector, *MyFinalVector,
40 *FinalVector;
41 float *CheckResultVector;
42 FILE *fp;
43 int MatrixFileStatus = 1, VectorFileStatus = 1;
44
45
46 /* ........MPI Initialisation ....... */
47
48 MPI_Init(&argc, &argv);
49 MPI_Comm_rank(MPI_COMM_WORLD, &MyRank);
50 MPI_Comm_size(MPI_COMM_WORLD, &Numprocs);
51
52 if (MyRank == 0) {
53 /* .......Read The Input File ...... */
54 if ((fp = fopen("./data/mdata.inp", "r")) == NULL) {
55 MatrixFileStatus = 0;
56 }
57 if (MatrixFileStatus != 0) {
58 fscanf(fp, "%d %d\n", &NoofRows, &NoofCols);
59
60 /*
61 * ...Allocate Memory And Read Matrix From File
62 * .......
63 */
64 Matrix = (float **) malloc(NoofRows * sizeof(float *));
65 for (irow = 0; irow < NoofRows; irow++) {
66 Matrix[irow] = (float *) malloc(NoofCols * sizeof(float));
67 for (icol = 0; icol < NoofCols; icol++) {
68 fscanf(fp, "%f", &Matrix[irow][icol]);
69 }
70 }
71 fclose(fp);
72
73 /* .......Convert 2-D Matrix Into 1-D Array ..... */
74 Buffer = (float *) malloc(NoofRows * NoofCols * sizeof(float));
75
76 index = 0;
77 for (irow = 0; irow < NoofRows; irow++) {
78 for (icol = 0; icol < NoofCols; icol++) {
79 Buffer[index] = Matrix[irow][icol];
80 index++;
81 }
82 }
83 }
84 /* Read Vector From Input File */
85 if ((fp = fopen("./data/vdata.inp", "r")) == NULL) {
86 VectorFileStatus = 0;
87 }
88 if (VectorFileStatus != 0) {
89 fscanf(fp, "%d\n", &VectorSize);
90
91 Vector = (float *) malloc(VectorSize * sizeof(float));
92 for (index = 0; index < VectorSize; index++)
93 fscanf(fp, "%f", &Vector[index]);
94
95 }
96 }/* End Of If Myrank = 0 */
97 MPI_Barrier(MPI_COMM_WORLD);
98
99 MPI_Bcast(&MatrixFileStatus, 1, MPI_INT, Root, MPI_COMM_WORLD);
100 if (MatrixFileStatus == 0) {
101 if (MyRank == Root)
102 printf("Can't Open Input File For Matrix ..... \n");
103 MPI_Finalize();
104 exit(-1);
105 }
106 MPI_Bcast(&VectorFileStatus, 1, MPI_INT, Root, MPI_COMM_WORLD);
107 if (VectorFileStatus == 0) {
108 if (MyRank == Root)
109 printf("Can't Open Input File For Vector ..... \n");
110 MPI_Finalize();
111 exit(-1);
112 }
113 MPI_Bcast(&NoofRows, 1, MPI_INT, Root, MPI_COMM_WORLD);
114
115 #ifdef _CIVL
116 $assume(NoofRows >= Numprocs);
117 #endif
118 if (NoofRows < Numprocs) {
119 if (MyRank == 0)
120 printf("No Of Rows Should Be More Than No Of Processors ... \n");
121 MPI_Finalize();
122 exit(0);
123 }
124 #ifdef _CIVL
125 $assume(NoofRows % Numprocs == 0);
126 #endif
127 if (NoofRows % Numprocs != 0) {
128 if (MyRank == 0)
129 printf("Matrix Cannot Be Striped Evenly ..... \n");
130 MPI_Finalize();
131 exit(0);
132 }
133 MPI_Bcast(&NoofCols, 1, MPI_INT, Root, MPI_COMM_WORLD);
134 MPI_Bcast(&VectorSize, 1, MPI_INT, Root, MPI_COMM_WORLD);
135
136 if (VectorSize != NoofCols) {
137 if (MyRank == 0) {
138 printf("Invalid Input Data..... \n");
139 printf("NoofCols Should Be Equal To VectorSize\n");
140 }
141 MPI_Finalize();
142 exit(0);
143 }
144 if (MyRank != 0)
145 Vector = (float *) malloc(VectorSize * sizeof(float));
146 MPI_Bcast(Vector, VectorSize, MPI_FLOAT, Root, MPI_COMM_WORLD);
147
148 ScatterSize = NoofRows / Numprocs;
149 Mybuffer = (float *) malloc(ScatterSize * NoofCols * sizeof(float));
150 MPI_Scatter(Buffer, ScatterSize * NoofCols, MPI_FLOAT, Mybuffer,
151 ScatterSize * NoofCols, MPI_FLOAT, 0, MPI_COMM_WORLD);
152
153 MyFinalVector = (float *) malloc(ScatterSize * sizeof(float));
154
155 for (irow = 0; irow < ScatterSize; irow++)
156 MyFinalVector[irow] = 0;
157
158 printf("\n");
159
160
161 /* OpenMP Parallel Directive */
162
163 /*
164#pragma omp parallel private(iam)
165{
166
167OpenMP Parallel For Directive */
168
169 omp_set_num_threads(4);
170#pragma omp parallel for private(index,icol,iam)
171 for (irow = 0; irow < ScatterSize; irow++) {
172 printf("The Threadid is %d with each processor Rank %d\n", omp_get_thread_num(), MyRank);
173 MyFinalVector[irow] = 0;
174 index = irow * NoofCols;
175 for (icol = 0; icol < NoofCols; icol++)
176 MyFinalVector[irow] += (Mybuffer[index++] * Vector[icol]);
177 }
178
179 MPI_Barrier(MPI_COMM_WORLD);
180
181 if (MyRank == 0)
182 FinalVector = (float *) malloc(NoofRows * sizeof(float));
183
184
185 MPI_Gather(MyFinalVector, ScatterSize, MPI_FLOAT, FinalVector,
186 ScatterSize, MPI_FLOAT, Root, MPI_COMM_WORLD);
187
188 if (MyRank == 0) {
189 printf("\n");
190 printf(" --------------------------------------------------- \n");
191 /* printf("Results of Gathering Data %d: \n", MyRank); */
192 printf("\n");
193
194 for (index = 0; index < NoofRows; index++)
195 printf(" FinalVector[%d] = %f \n", index, FinalVector[index]);
196 printf(" --------------------------------------------------- \n");
197 }
198 if (MyRank == 0) {
199 CheckResultVector = (float *) malloc(NoofRows * sizeof(float));
200 for (irow = 0; irow < NoofRows; irow++) {
201 CheckResultVector[irow] = 0;
202 for (icol = 0; icol < NoofCols; icol++) {
203 CheckResultVector[irow] += (Matrix[irow][icol] * Vector[icol]);
204 }
205 if (fabs((double) (FinalVector[irow] - CheckResultVector[irow])) >
206 1.0E-10) {
207 printf("Error %d\n", irow);
208 ValidOutput = 0;
209 }
210 }
211 if (ValidOutput)
212 printf("\n-------Correct Result------\n");
213 /* Freeing Allocated Memory */
214 free(Matrix);
215 free(Vector);
216 free(Buffer);
217 free(FinalVector);
218 free(CheckResultVector);
219 }
220
221 /* Freeing Allocated Memory */
222 free(Mybuffer);
223 free(MyFinalVector);
224
225 /* MPI-Termination */
226
227 MPI_Finalize();
228
229}
Note: See TracBrowser for help on using the repository browser.