Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mpi.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <signal.h>
- #define MYTAG 1
- int rank, j;
- double startwtime = 0.0, endwtime;
- int main(int argc, char *argv[])
- {
- int total, n, size, i, dest;
- double result;
- MPI_Status status;
- MPI_Init(&argc, &argv);
- MPI_Comm_size(MPI_COMM_WORLD, &size);
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- double a[10][10];
- total = 10;
- if (rank == 0)
- n = total / size + 1;
- MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
- //a = new double[total];
- if (rank == 0) {
- for (dest=1; dest < size; dest++) {
- for (i = 0; i < n; i++) {
- for(int j = 0; j < total;j++) {
- a[i][j] = 5;
- }
- }
- MPI_Send(a, n, MPI_DOUBLE, dest, MYTAG, MPI_COMM_WORLD);
- }
- n = total - n*(size-1);
- } else {
- MPI_Recv(a, n, MPI_DOUBLE, 0, MYTAG, MPI_COMM_WORLD, &status);
- }
- double max = a[0][0];
- double min = a[0][0];
- for (i = 0; i < n; i++) {
- for(int j = 0; j < total;j++) {
- if (a[i][j] < min)
- min = a[i][j];
- }
- if (min > max)
- max = min;
- }
- MPI_Reduce(&max, &result, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
- if (rank == 0) {
- printf("Answer is %.4f\n", result);
- }
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement