| 1 | #ifdef _CIVL
|
|---|
| 2 | #include <civlc.cvh>
|
|---|
| 3 | #endif
|
|---|
| 4 | /* FEVS: A Functional Equivalence Verification Suite for High-Performance
|
|---|
| 5 | * Scientific Computing
|
|---|
| 6 | *
|
|---|
| 7 | * Copyright (C) 2010, Stephen F. Siegel, Timothy K. Zirkel,
|
|---|
| 8 | * University of Delaware
|
|---|
| 9 | *
|
|---|
| 10 | * This program is free software; you can redistribute it and/or
|
|---|
| 11 | * modify it under the terms of the GNU General Public License as
|
|---|
| 12 | * published by the Free Software Foundation; either version 3 of the
|
|---|
| 13 | * License, or (at your option) any later version.
|
|---|
| 14 | *
|
|---|
| 15 | * This program is distributed in the hope that it will be useful, but
|
|---|
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 18 | * General Public License for more details.
|
|---|
| 19 | *
|
|---|
| 20 | * You should have received a copy of the GNU General Public License
|
|---|
| 21 | * along with this program; if not, write to the Free Software
|
|---|
| 22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|---|
| 23 | * 02110-1301 USA.
|
|---|
| 24 | */
|
|---|
| 25 |
|
|---|
| 26 | #include <stdlib.h>
|
|---|
| 27 | #include <stdio.h>
|
|---|
| 28 | #include <mpi.h>
|
|---|
| 29 |
|
|---|
| 30 | #define comm MPI_COMM_WORLD
|
|---|
| 31 |
|
|---|
| 32 | int N, L, M;
|
|---|
| 33 |
|
|---|
| 34 | void printMatrix(int numRows, int numCols, double *m) {
|
|---|
| 35 | int i, j;
|
|---|
| 36 | for (i = 0; i < numRows; i++) {
|
|---|
| 37 | for (j = 0; j < numCols; j++)
|
|---|
| 38 | printf("%f ", m[i*numCols + j]);
|
|---|
| 39 | printf("\n");
|
|---|
| 40 | }
|
|---|
| 41 | printf("\n");
|
|---|
| 42 | fflush(stdout);
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | void vecmat(double vector[L], double matrix[L][M], double result[M]) {
|
|---|
| 46 | int j, k;
|
|---|
| 47 | for (j = 0; j < M; j++)
|
|---|
| 48 | for (k = 0, result[j] = 0.0; k < L; k++)
|
|---|
| 49 | result[j] += vector[k]*matrix[k][j];
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | int main(int argc, char *argv[]) {
|
|---|
| 53 | int rank, nprocs, i, j;
|
|---|
| 54 | MPI_Status status;
|
|---|
| 55 |
|
|---|
| 56 | N = atoi(argv[1]);
|
|---|
| 57 | L = atoi(argv[2]);
|
|---|
| 58 | M = atoi(argv[3]);
|
|---|
| 59 | #ifdef _CIVL
|
|---|
| 60 | $assume(N && N <=2);
|
|---|
| 61 | $assume(M && M <=2);
|
|---|
| 62 | $assume(L && L <=2);
|
|---|
| 63 | #endif
|
|---|
| 64 | MPI_Init(&argc, &argv);
|
|---|
| 65 | MPI_Comm_rank(comm, &rank);
|
|---|
| 66 | MPI_Comm_size(comm, &nprocs);
|
|---|
| 67 | if (rank == 0) {
|
|---|
| 68 | double a[N][L], b[L][M], c[N][M], tmp[M];
|
|---|
| 69 | int count;
|
|---|
| 70 |
|
|---|
| 71 | FILE *fp =fopen("data", "r");
|
|---|
| 72 | for (i = 0; i < N; i++)
|
|---|
| 73 | for (j = 0; j < L; j++)
|
|---|
| 74 | fscanf(fp,"%lf", &a[i][j]);
|
|---|
| 75 | for (i = 0; i < L; i++)
|
|---|
| 76 | for (j = 0; j < M; j++)
|
|---|
| 77 | fscanf(fp,"%lf",&b[i][j]);
|
|---|
| 78 | MPI_Bcast(b, L*M, MPI_DOUBLE, 0, comm);
|
|---|
| 79 | for (count = 0; count < nprocs-1 && count < N; count++)
|
|---|
| 80 | MPI_Send(&a[count][0], L, MPI_DOUBLE, count+1, count+1, comm);
|
|---|
| 81 | for (i = 0; i < N; i++) {
|
|---|
| 82 | MPI_Recv(tmp, M, MPI_DOUBLE, MPI_ANY_SOURCE, MPI_ANY_TAG, comm, &status);
|
|---|
| 83 | for (j = 0; j < M; j++) c[status.MPI_TAG-1][j] = tmp[j];
|
|---|
| 84 | if (count < N) {
|
|---|
| 85 | MPI_Send(&a[count][0], L, MPI_DOUBLE, status.MPI_SOURCE, count+1, comm);
|
|---|
| 86 | count++;
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 | for (i = 1; i < nprocs; i++) MPI_Send(NULL, 0, MPI_INT, i, 0, comm);
|
|---|
| 90 | printMatrix(N, M, &c[0][0]);
|
|---|
| 91 | fclose(fp);
|
|---|
| 92 | } else {
|
|---|
| 93 | double b[L][M], in[L], out[M];
|
|---|
| 94 |
|
|---|
| 95 | MPI_Bcast(b, L*M, MPI_DOUBLE, 0, comm);
|
|---|
| 96 | while (1) {
|
|---|
| 97 | MPI_Recv(in, L, MPI_DOUBLE, 0, MPI_ANY_TAG, comm, &status);
|
|---|
| 98 | if (status.MPI_TAG == 0) break;
|
|---|
| 99 | vecmat(in, b, out);
|
|---|
| 100 | MPI_Send(out, M, MPI_DOUBLE, 0, status.MPI_TAG, comm);
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 | MPI_Finalize();
|
|---|
| 104 | return 0;
|
|---|
| 105 | }
|
|---|