Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include "mpi.h"
- #include <cstdio>
- using namespace std;
- int main(int argc, char *argv[])
- {
- int ierr = MPI_Init(&argc, &argv);
- int numprocs;
- int myid;
- int a;
- int senderID = 0;
- MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
- MPI_Comm_rank(MPI_COMM_WORLD, &myid);
- // process 1-5 receivess the send a, adds or substracts a, and sends it further. process 5 prints all the changes.
- if (myid != 0) {
- MPI_Recv(&a, 1, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- if (myid == 1 || myid == 2 || myid == 3)
- {
- a += 10;
- senderID = myid;
- MPI_Send(&a, 1, MPI_INT, MPI_ANY_TAG, 0, MPI_COMM_WORLD);
- MPI_Send(&senderID, 1, MPI_INT, 5, 0, MPI_COMM_WORLD);
- }
- else if (myid == 4)
- {
- a -= 50;
- senderID = myid;
- MPI_Send(&a, 1, MPI_INT, MPI_ANY_TAG, 0, MPI_COMM_WORLD);
- MPI_Send(&senderID, 1, MPI_INT, 5, 0, MPI_COMM_WORLD);
- }
- else if (myid == 5)
- {
- while (a >= 0)
- {
- MPI_Recv(&senderID, 1, MPI_INT, 1, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- if (senderID == 0)
- {
- cout << "Spausdino" << endl;
- }
- if (1 <= senderID <= 3)
- {
- cout << "Pridėjo" << endl;
- }
- else
- {
- cout << "Atėmė" << endl;
- }
- }
- }
- }
- // process P0 initializes a and sends it to all other processes.
- if (myid == 0) {
- a = 100;
- MPI_Send(&a, 1, MPI_INT, MPI_ANY_TAG, 0,MPI_COMM_WORLD);
- MPI_Recv(&a, 1, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- if (a <= 0)
- MPI_Send(&senderID, 1, MPI_INT, 5, 0, MPI_COMM_WORLD);
- }
- while (a >= 0)
- {
- MPI_Send(&a, 1, MPI_INT, MPI_ANY_TAG, 0, MPI_COMM_WORLD);
- }
- MPI_Finalize();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement