Reputation: 111
I'm trying to write a parallel program that implements a pipeline version of Gaussian elimination, using MPI and C language...
However I'm encountering some difficulties early in the implementation of the code....
I use a root process to read a data matrix from a text file... this process gives-me the size of this matrix and I broadcast the size of it to all other processes in order for them to allocate it in memory... However, the slave processes are trying to allocate it before the broadcast from the root... How can I make them wait?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mpi.h>
int CalcInd(int i, int j, int dimL)
{
return i*dimL +j;
}
int main (int argc, char **argv)
{
FILE *fin, *fout;
char fA[] = "Matrix.txt";
int rank, size, i, ii, j, k, m, n, picked, tmp, total;
int counter=0, elements=0;
int * RightNeigbhor, * LeftNeigbhor, * loc;
float f, magnitude, t;
float * A, * x;
MPI_Status status;
MPI_Request request;
// MPI initialization
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Barrier(MPI_COMM_WORLD);
if(rank == 0)
{
// Defenição dos processos vizinhos pelo master
RightNeigbhor = (int *)calloc(size,sizeof(int));
if(RightNeigbhor==NULL)
{printf("!!! Could not allocate memory !!!\n"); exit(-1);}
LeftNeigbhor = (int *)calloc(size,sizeof(int));
if(RightNeigbhor==NULL)
{printf("!!! Could not allocate memory !!!\n"); exit(-1);}
for(i = 0; i < size; i++ )
{
RightNeigbhor[i] = (rank + 1) % size;
LeftNeigbhor[i] = (rank - 1) % size;
}
// Broadcast os processos vizinhos para todos os processos
MPI_Bcast ( RightNeigbhor, size, MPI_INTEGER, rank, MPI_COMM_WORLD );
MPI_Bcast ( LeftNeigbhor, size, MPI_INTEGER, rank, MPI_COMM_WORLD );
// Leitura da matriz A pelo master
fin = fopen ( fA, "r" );
if (fin == NULL){ printf("!!! FILE NOT FOUND !!!"); exit(-1); }
while( !feof(fin))
{
fscanf (fin, "%f", &f);
elements++;
}
rewind(fin);
f = 0;
while( !feof(fin))
{
if(fgetc(fin) == '\n')
{
counter++;
}
}
rewind(fin);
n = counter;
m = (elements-1) / counter;
total = n*m;
MPI_Bcast ( &total, 1, MPI_INT, rank, MPI_COMM_WORLD );
MPI_Bcast ( &n, 1, MPI_INT, rank, MPI_COMM_WORLD );
}
// Alocação de variaveis
A = (float *)calloc(total,sizeof(float));
if(A==NULL){printf("!!! Could not allocate memory !!!\n"); exit(-1);}
loc = (int *)calloc(n,sizeof(int*));
if(loc==NULL){printf("!!! Could not allocate memory !!!\n"); exit(-1);}
// AND IT GOES ON AND ON
Upvotes: 2
Views: 2774
Reputation: 2491
Everything in your rank == 0
block runs only in process 0. While process rank == 1 ... n
just skip that block. Therefore, you have to put your MPI_Bcast
calls in an environment which is visible for all process in MPI_Comm comm
here MPI_COMM_WORLD
. When process 1...n skip all the initialization and jump to the broadcast before process 0 reaches it, they will wait till the bcast has occured.
Upvotes: 4