source: CIVL/examples/mpi/mpiFeature/matmat_mw_bad.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: 3.0 KB
RevLine 
[056aaa8]1/* matmat_mw_mpi.c : Parallel matrix multiplication program
2 * implemented with manager-worker pattern using MPI.
3 */
4#include <stdlib.h>
5#include <stdio.h>
6#include <string.h>
7#include <mpi.h>
8
9#define comm MPI_COMM_WORLD
10
11#ifdef _CIVL
12
13#include <civlc.cvh>
14$input int _mpi_nprocs_hi = 5; // upper bound of number of processes
15$input int _mpi_nprocs_lo = 2; // lower bound of number of processes
16/* Dimensions of 2 matrices: a[N][L] * b[L][M] */
17$input int NB = 5; // upper bound of N
18$input int N;
19$assume(0 < N && N <= NB);
20$input int LB = 5; // upper bound of L
21$input int L;
22$assume(0 < L && L <= LB);
23$input int MB = 5; // upper bound of M
24$input int M;
25$assume(0 < M && M <= MB);
26$input double a[N][L]; // input data for matrix a
27$input double b[L][M]; // input data for matrix b
28$output double output[N][M]; // matrix stores results of a sequential run
29
30#else
31
32int N = 3, L = 3, M = 3;
33
34#endif
35
36/* prints a matrix.*/
37void printMatrix(int numRows, int numCols, double *m) {
38 for (int i = 0; i < numRows; i++) {
39 for (int j = 0; j < numCols; j++)
40 printf("%f ", m[i*numCols + j]);
41 printf("\n");
42 }
43 printf("\n");
44}
45
46/* Computes a vetor with length L times a matrix with dimensions [L][M] */
47void vecmat(double vector[L], double matrix[L][M], double result[M]) {
48 for (int j = 0; j < M; j++) {
49 result[j] = 0.0;
50 for (int k = 0; k < L; k++)
51 result[j] += vector[k]*matrix[k][j];
52 }
53}
54
55int main(int argc, char *argv[]) {
56 int rank, nprocs;
57 MPI_Status status;
58
59 MPI_Init(&argc, &argv);
60 MPI_Comm_rank(comm, &rank);
61 MPI_Comm_size(comm, &nprocs);
62
63 if (rank == 0) {
64 double c[N][M], tmp[M];
65 int count;
66
67#ifndef _CIVL
68 double a[N][L], b[L][M];
69
70 // random initialization:
71 for (int i = 0; i < N; i++)
72 for (int j = 0; j < L; j++)
73 a[i][j] = i * N + j;
74 for (int i = 0; i < L; i++)
75 for (int j = 0; j < M; j++)
76 b[i][j] = i * L + j;
77#else
78 $elaborate(L*M);
79#endif
80 MPI_Bcast(&b[0][0], L*M, MPI_DOUBLE, 0, comm);
81 for (count = 0; count < nprocs-1 && count < N; count++)
82 MPI_Send(&a[count][0], L, MPI_DOUBLE, count+1, count+1, comm);
83 for (int i = 0; i < N; i++) {
84 MPI_Recv(tmp, M, MPI_DOUBLE, MPI_ANY_SOURCE, MPI_ANY_TAG, comm, &status);
85 for (int j = 0; j < M; j++) c[status.MPI_TAG-1][j] = tmp[j];
86 if (count < N) {
87 MPI_Send(&a[count][0], L, MPI_DOUBLE, status.MPI_SOURCE, count+1, comm);
88 count++;
89 }
90 }
91 for (int i = 1; i < nprocs; i++) MPI_Send(NULL, 0, MPI_INT, i, 0, comm);
92 printMatrix(N, M, &c[0][0]);
93#ifdef _CIVL
94 for (int i = 0; i < N; i++)
95 for (int j = 0; j < M; j++)
96 output[i][j] = c[i][j];
97#endif
98 } else {
99 double b[L][M], in[L], out[M];
100
101 MPI_Bcast(&b[0][0], L*M, MPI_DOUBLE, 0, comm);
102 while (1) {
103 MPI_Recv(in, L, MPI_DOUBLE, 0, MPI_ANY_TAG, comm, &status);
104 if (status.MPI_TAG == 0) break;
105 vecmat(in, b, out);
106 MPI_Send(out, M, MPI_DOUBLE, 0, 1, comm);
107 }
108 }
109 MPI_Finalize();
110 return 0;
111}
112
Note: See TracBrowser for help on using the repository browser.