Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "mpi.h"
- #include <stdio.h>
- #include <tchar.h>
- #include <vector>
- #include "conio.h"
- #include <iostream>
- #include "time.h"
- #define ROOT 0
- #define R2A 10
- #define A2B 20
- #define B2A 30
- #define A2R 40
- int gSET = 0;
- bool isA(int &id, int& total) {
- if(id<(total/2) && id!=0) return true;
- return false;
- }
- bool isB(int &id, int&total) {
- return (!isA(id,total) && id!=0);
- }
- void showtab(int *tab, int size) {
- std::cout << "\ntab=\n";
- for(int i=0; i<size;++i) {
- std::cout << tab[i] << "\n";
- }
- std::cout << "\n";
- }
- void swap(int &a, int &b) {
- int tmp = a;
- a = b;
- b = a;
- }
- int main(int argc, char** argv)
- {
- int mynode, totalnodes;
- int *tab = NULL;
- int temp = 0;
- int buffA = 0; //wieksza
- int buffB = 0; // mniejsaz
- MPI_Status status;
- MPI_Init(&argc, &argv);
- MPI_Comm_size(MPI_COMM_WORLD, &totalnodes);
- MPI_Comm_rank(MPI_COMM_WORLD, &mynode);
- srand((unsigned int)time(NULL) * mynode);
- int N = (totalnodes/2) -1; // bo A=N+1, B=N+1, dla N
- int koniec = 2*N;
- int licznik = 0;
- gSET = (totalnodes)/2;
- if(mynode == ROOT) {
- tab = new int[N];
- //for(int i=0; i<N; ++i) tab[i] = rand()%100;
- for(int i=0; i<N; ++i) tab[i] = i;
- showtab(tab,N);
- for(int i=0; i<N; ++i) {
- temp = tab[i];
- MPI_Send(&temp,1,MPI_INT,(i+1),R2A,MPI_COMM_WORLD);
- }
- }
- else if(isA(mynode,totalnodes)) {
- MPI_Recv(&buffA,1,MPI_INT,ROOT,R2A,MPI_COMM_WORLD,&status);
- buffB = buffA;
- }
- if(isA(mynode,totalnodes)) {
- MPI_Send(&buffA,1,MPI_INT,(mynode+gSET),A2B,MPI_COMM_WORLD); // duza
- MPI_Send(&buffB,1,MPI_INT,(mynode-1+gSET),A2B,MPI_COMM_WORLD); // mala
- } else if(isB(mynode,totalnodes)) {
- if(mynode != 6 && mynode != 11) {
- MPI_Recv(&buffA,1,MPI_INT,(mynode-gSET),A2B,MPI_COMM_WORLD,&status);
- MPI_Recv(&buffB,1,MPI_INT,(mynode-gSET+1),A2B,MPI_COMM_WORLD,&status);
- if(buffA < buffB) swap(buffA,buffB);
- }
- }
- if(mynode != 0) {
- //while(licznik < koniec) {
- if(isA(mynode,totalnodes)) {
- if(mynode != 5) {
- MPI_Recv(&buffA,1,MPI_INT,(mynode+gSET),B2A,MPI_COMM_WORLD,&status);
- MPI_Recv(&buffB,1,MPI_INT,(mynode-1+gSET),B2A,MPI_COMM_WORLD,&status);
- if(buffA < buffB) swap(buffA,buffB);
- MPI_Send(&buffA,1,MPI_INT,(mynode+gSET),A2B,MPI_COMM_WORLD); // duza
- MPI_Send(&buffB,1,MPI_INT,(mynode-1+gSET),A2B,MPI_COMM_WORLD); // mala
- }
- }
- else if(isB(mynode,totalnodes)) {
- if(mynode != 6 && mynode != 11) {
- MPI_Send(&buffB,1,MPI_INT,(mynode-gSET),B2A,MPI_COMM_WORLD);
- MPI_Send(&buffA,1,MPI_INT,(mynode-gSET+1),B2A,MPI_COMM_WORLD);
- MPI_Recv(&buffA,1,MPI_INT,(mynode-gSET),A2B,MPI_COMM_WORLD,&status);
- MPI_Recv(&buffB,1,MPI_INT,(mynode-gSET+1),A2B,MPI_COMM_WORLD,&status);
- if(buffA < buffB) swap(buffA,buffB);
- }
- else if(mynode == 6) {
- MPI_Send(&buffB,1,MPI_INT,1,B2A,MPI_COMM_WORLD);
- MPI_Recv(&buffB,1,MPI_INT,1,A2B,MPI_COMM_WORLD,&status);
- }
- }
- ++licznik;
- //if(licznik > 5) break;
- std::cout << "PID|licznik|koniec| " << mynode <<"|"<<licznik<<"|"<<koniec<<"|\n";
- //}
- }
- //if(isA(mynode,totalnodes)) {
- // MPI_Send(&buffA,1,MPI_INT,ROOT,A2R,MPI_COMM_WORLD);
- //} else if(mynode == ROOT) {
- // for(int i=0; i<N; ++i) {
- // MPI_Recv(&buffA,1,MPI_INT,(i+1),A2R,MPI_COMM_WORLD,&status);
- // tab[i] = buffA;
- // }
- // showtab(tab,N);
- //}
- std::cout << "koniec";
- delete [] tab;
- MPI_Finalize();
- return 0;
- }
Add Comment
Please, Sign In to add comment