| [3ff27cf] | 1 | #ifdef _CIVL
|
|---|
| 2 | #include <civlc.cvh>
|
|---|
| 3 | #endif
|
|---|
| [166a724] | 4 | /*************************************************************************
|
|---|
| 5 | C-DAC Tech Workshop : HeGaPa-2012
|
|---|
| 6 | July 16-20,2012
|
|---|
| 7 |
|
|---|
| 8 | Example 3 : Mpi-Omp_MatInf_blkstp.c
|
|---|
| 9 |
|
|---|
| 10 | Objective : Write parallel program using MPI and OPENMP to compute norm
|
|---|
| 11 | of a square matrix.
|
|---|
| 12 |
|
|---|
| 13 | Input : Read file (infndata.inp) for Matrix
|
|---|
| 14 |
|
|---|
| 15 | Output : Process with rank 0 prints the value of Infinity Norm
|
|---|
| 16 |
|
|---|
| 17 | Created : MAY-2012
|
|---|
| 18 |
|
|---|
| 19 | ************************************************************************/
|
|---|
| 20 |
|
|---|
| 21 | #include <stdio.h>
|
|---|
| 22 | #include <stdlib.h>
|
|---|
| 23 | #include "mpi.h"
|
|---|
| 24 | #include <omp.h>
|
|---|
| 25 |
|
|---|
| [cf6056d] | 26 | #ifdef _CIVL
|
|---|
| 27 | $input int NUM_ROWS_BOUND = 4;
|
|---|
| 28 | $input int NUM_COLS_BOUND = 4;
|
|---|
| 29 | #endif
|
|---|
| [166a724] | 30 |
|
|---|
| 31 | /* Main Program */
|
|---|
| 32 |
|
|---|
| 33 | int main(int argc, char **argv) // "int" inserted manually
|
|---|
| 34 | //main(int argc, char **argv)
|
|---|
| 35 | {
|
|---|
| 36 |
|
|---|
| 37 | int Numprocs, MyRank, iam;
|
|---|
| 38 | int NoofCols, NoofRows, ScatterSize;
|
|---|
| 39 | int index, irow, icol;
|
|---|
| 40 | int Root = 0;
|
|---|
| 41 | float **InputMatrix, *Buffer, *MyBuffer;
|
|---|
| 42 | float max = 0, sum = 0, Inf_norm = 0;
|
|---|
| 43 | FILE *fp;
|
|---|
| 44 | int MatrixFileStatus = 1;
|
|---|
| 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 |
|
|---|
| 54 | /* .......Read The Matrix Input File ...... */
|
|---|
| 55 | if ((fp = fopen("./data/infndata.inp", "r")) == NULL) {
|
|---|
| 56 | MatrixFileStatus = 0;
|
|---|
| 57 | }
|
|---|
| 58 | if (MatrixFileStatus != 0) {
|
|---|
| 59 | fscanf(fp, "%d %d\n", &NoofRows, &NoofCols);
|
|---|
| 60 |
|
|---|
| [cf6056d] | 61 | #ifdef _CIVL
|
|---|
| [3ff27cf] | 62 | $assume(NoofRows <= NUM_ROWS_BOUND);
|
|---|
| 63 | $assume(NoofRows <= NoofCols);
|
|---|
| [cf6056d] | 64 | #endif
|
|---|
| 65 |
|
|---|
| [166a724] | 66 | /* .......Allocate Memory For Matrix ..... */
|
|---|
| 67 | InputMatrix = (float **) malloc(NoofRows * sizeof(float *));
|
|---|
| 68 | for (irow = 0; irow < NoofRows; irow++)
|
|---|
| 69 | InputMatrix[irow] = (float *) malloc(NoofCols * sizeof(float));
|
|---|
| 70 |
|
|---|
| 71 | /* .......Read Data For Matrix ..... */
|
|---|
| 72 | for (irow = 0; irow < NoofRows; irow++) {
|
|---|
| 73 | for (icol = 0; icol < NoofCols; icol++)
|
|---|
| 74 | fscanf(fp, "%f", &InputMatrix[irow][icol]);
|
|---|
| 75 | }
|
|---|
| 76 | fclose(fp);
|
|---|
| 77 |
|
|---|
| 78 | /*
|
|---|
| 79 | * .......Convert 2-D InputMatrix Into 1-D Array
|
|---|
| 80 | * .....
|
|---|
| 81 | */
|
|---|
| 82 | Buffer = (float *) malloc(NoofRows * NoofCols * sizeof(float));
|
|---|
| 83 |
|
|---|
| 84 | index = 0;
|
|---|
| 85 | for (irow = 0; irow < NoofRows; irow++) {
|
|---|
| 86 | for (icol = 0; icol < NoofCols; icol++) {
|
|---|
| 87 | Buffer[index] = InputMatrix[irow][icol];
|
|---|
| 88 | index++;
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 | }/* MyRank == 0 */
|
|---|
| 93 | MPI_Barrier(MPI_COMM_WORLD);
|
|---|
| 94 | MPI_Bcast(&MatrixFileStatus, 1, MPI_INT, Root, MPI_COMM_WORLD);
|
|---|
| 95 | if (MatrixFileStatus == 0) {
|
|---|
| 96 | if (MyRank == Root)
|
|---|
| 97 | printf("Can't Open Matrix Input File");
|
|---|
| 98 | MPI_Finalize();
|
|---|
| 99 | exit(-1);
|
|---|
| 100 | }
|
|---|
| 101 | MPI_Bcast(&NoofRows, 1, MPI_INT, Root, MPI_COMM_WORLD);
|
|---|
| 102 |
|
|---|
| [59800b1] | 103 | #ifdef _CIVL
|
|---|
| [3ff27cf] | 104 | $assume(NoofRows >= Numprocs);
|
|---|
| [59800b1] | 105 | #endif
|
|---|
| [166a724] | 106 | if (NoofRows < Numprocs) {
|
|---|
| 107 | MPI_Finalize();
|
|---|
| 108 | if (MyRank == 0)
|
|---|
| 109 | printf("Noof Rows Should Be More Than No of Processors ... \n");
|
|---|
| 110 | exit(0);
|
|---|
| 111 | }
|
|---|
| [59800b1] | 112 | #ifdef _CIVL
|
|---|
| [3ff27cf] | 113 | $assume(NoofRows % Numprocs == 0);
|
|---|
| [59800b1] | 114 | #endif
|
|---|
| [166a724] | 115 | if (NoofRows % Numprocs != 0) {
|
|---|
| 116 | MPI_Finalize();
|
|---|
| 117 | if (MyRank == 0) {
|
|---|
| 118 | printf("Matrix Cannot Be Striped Evenly ..... \n");
|
|---|
| 119 | }
|
|---|
| 120 | exit(0);
|
|---|
| 121 | }
|
|---|
| 122 | MPI_Bcast(&NoofCols, 1, MPI_INT, Root, MPI_COMM_WORLD);
|
|---|
| 123 |
|
|---|
| 124 | ScatterSize = NoofRows / Numprocs;
|
|---|
| 125 | MyBuffer = (float *) malloc(ScatterSize * NoofCols * sizeof(float));
|
|---|
| 126 | MPI_Scatter(Buffer, ScatterSize * NoofCols, MPI_FLOAT,
|
|---|
| 127 | MyBuffer, ScatterSize * NoofCols, MPI_FLOAT,
|
|---|
| 128 | 0, MPI_COMM_WORLD);
|
|---|
| 129 |
|
|---|
| 130 | /* OpenMP Parallel Directive */
|
|---|
| 131 |
|
|---|
| 132 | max = 0;
|
|---|
| 133 | /*
|
|---|
| 134 | #pragma omp parallel private(iam) shared(max)
|
|---|
| 135 | {
|
|---|
| 136 | iam = omp_get_thread_num();
|
|---|
| 137 | printf("The Threadid Is %d With each Processor's Rank %d\n", iam, MyRank);
|
|---|
| 138 |
|
|---|
| 139 | OpenMP Parallel For Directive
|
|---|
| 140 | */
|
|---|
| 141 | omp_set_num_threads(4);
|
|---|
| 142 | #pragma omp parallel for private(sum,index,icol) shared(max)
|
|---|
| 143 | for (irow = 0; irow < ScatterSize; irow++) {
|
|---|
| 144 | printf("The Threadid Is %d With each Processor's Rank %d\n",omp_get_thread_num(), MyRank);
|
|---|
| 145 | sum = 0;
|
|---|
| 146 | index = irow * NoofCols;
|
|---|
| 147 | for (icol = 0; icol < NoofCols; icol++) {
|
|---|
| 148 | sum += (MyBuffer[index] >= 0) ? (MyBuffer[index]) : (0 - MyBuffer[index]);
|
|---|
| 149 | index++;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /* OpenMP Critical Section */
|
|---|
| 153 |
|
|---|
| 154 | #pragma omp critical
|
|---|
| 155 | if (sum > max)
|
|---|
| 156 | max = sum;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | MPI_Barrier(MPI_COMM_WORLD);
|
|---|
| 160 | MPI_Reduce(&max, &Inf_norm, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
|
|---|
| 161 | if (MyRank == 0) {
|
|---|
| 162 | max = 0;
|
|---|
| 163 |
|
|---|
| 164 | /* Serial Check */
|
|---|
| 165 |
|
|---|
| 166 | for (irow = 0; irow < NoofRows; irow++) {
|
|---|
| 167 | sum = 0;
|
|---|
| 168 | index = irow * NoofCols;
|
|---|
| 169 | for (icol = 0; icol < NoofCols; icol++) {
|
|---|
| 170 | sum += (Buffer[index] >= 0) ? (Buffer[index]) : (0 - Buffer[index]);
|
|---|
| 171 | index++;
|
|---|
| 172 | }
|
|---|
| 173 | max = max < sum ? sum : max;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | printf("\nThe Infinity Norm Is(Parallel Code) : %f\n", Inf_norm);
|
|---|
| 177 | printf("\nThe Infinity Norm Is(Serial Code) : %f\n\n", max);
|
|---|
| 178 |
|
|---|
| 179 | /* Freeing Allocated Memory */
|
|---|
| 180 |
|
|---|
| 181 | free(InputMatrix);
|
|---|
| 182 | free(Buffer);
|
|---|
| 183 | }
|
|---|
| 184 | /* MPI-Termination */
|
|---|
| 185 |
|
|---|
| 186 | free(MyBuffer);
|
|---|
| 187 |
|
|---|
| 188 | MPI_Finalize();
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|