Parallel matrix calculations with MPI (Scatter Gatter)

1

This program is an example to try to parallelize a matrix tam * tam. Each process must have the same load, except the process 0 that makes size too many iterations. Each piece of Al must receive all 1 and then send it back to All 2.

I've been testing it by running it with:

 mpic++ -o hola matriz.cpp
 mpirun -np 2  -hostfile maquinas2 ./hola

In which I have tried it with a tam of 3 and it gives me this (all 2 must go out). I also get an error

2 2 2
2 2 2
2 2 1

With a value of 5, this time does not give an error

2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
1 1 1 1 1
1 1 1 1 1

With a tam of 6

Enter tam value: 6

2 2 2 2 2 2
2 2 2 2 2 2
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1

This is the program

#include <mpi.h>
#include <iostream>
#include <sys/time.h>
#include <time.h>  /* time */
#include <math.h>  /* sqrt */
#include<stdlib.h> /* srand, rand */
using namespace std;

const int valores = 50;

int main(int argc, char* argv[]){
   int rank, size;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank); // Obté valor id propi
    MPI_Comm_size (MPI_COMM_WORLD, &size);

    int tam, i, l, j;

    if(rank == 0){
        cout << "Introduce valor de tam: ";
        cin >> tam;
    }

    MPI_Bcast(&tam, 1, MPI_INT, 0, MPI_COMM_WORLD);

    double ** A = new double * [tam];
    double ** Al = new double * [tam/size];

    for(l = 0; l < tam; l++){
        A[l] = new double[tam];
    }

    for(l = 0; l < tam/size; l++){
        Al[l] = new double[tam];
    }

    if(rank == 0){
        for(i = 0; i < tam; i++){
            for(j = 0; j < tam; j++){
                A[i][j] = 1;
            }
        }
    }

/****************************************/

    MPI_Scatter(&A[tam%size][0],(tam/size)*tam, MPI_DOUBLE, &Al[0][0], (tam/size)*tam, MPI_DOUBLE,0,MPI_COMM_WORLD);


    if(rank == 0){
        for(i = 0; i < tam%size; i++){
            for(j = 0; j < tam; j++){
                A[i][j] = 2;
            }
        }
    }



    for(i = 0; i < tam/size; i++){
        for(j = 0; j < tam; j++){
            //cout << Al[i][j] << " ";
            Al[i][j] = 2;
        }
        //cout << endl;
    }

    MPI_Gather(&Al[0][0], (tam/size)*tam, MPI_DOUBLE, &A[tam%size][0], (tam/size)*tam, MPI_DOUBLE, 0, MPI_COMM_WORLD);

    /***********************************/

    if(rank == 0){
        for(i = 0; i < tam; i++){
            for(j = 0; j < tam; j++){
                cout << A[i][j] << " ";
            }
            cout << endl;
        }
    }

    MPI_Finalize();

    return (0);
}

I've been long enough to see where it fails, and I do not find the problem of why it does not receive all 1 and it does not send all 2.

    
asked by Jose Ferrús 03.05.2018 в 00:23
source

0 answers