1

Este programa es un ejemplo para probar a paralelizar una matriz tam*tam. Cada proceso debe de tener la misma carga, excepto el proceso 0 que hace tam%size iteraciones de más. Cada trozo de Al debe recibir todo 1 y luego enviar de nuevo a A todo 2.

Lo he estado probando ejecutándolo con:

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

En el que lo he probado con un tam de 3 y me da esto (deben de salir todos 2). Además me sale un error

2 2 2
2 2 2
2 2 1

Con un valor de 5, esta vez no da 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

Con un tam de 6

Introduce valor de tam: 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

Este es el programa

#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);
}

He estado bastante tiempo para ver donde falla, y no encuentro el problema de por qué no recibe todos 1 y no envía todo 2.

1
  • Sin embargo declarando las matrices double A[tam][tam]; double Al[tam/size][tam]; con memoria estática si que funciona correctamente. Lo que pasa que necesito punteros para usar las cualidades de la memoria dinámica para crear matrices grandes. ¿Hay alguna solución con memoria dinámica? el 3 may. 2018 a las 14:09

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.