2

我正在尝试使用 MPI 和 C 语言编写一个并行程序来实现高斯消除的管道版本...

但是我在代码实现的早期遇到了一些困难......

我使用根进程从文本文件中读取数据矩阵......这个过程给了我这个矩阵的大小,我将它的大小广播给所有其他进程,以便他们在内存中分配它......但是,从属进程正试图在根广播之前分配它......我怎样才能让它们等待?

#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
4

1 回答 1

4

块中的所有内容rank == 0仅在进程 0 中运行。而进程rank == 1 ... n只是跳过该块。因此,您必须将呼叫置于此处MPI_Bcast所有进程可见的环境中。当进程 1...n 跳过所有初始化并在进程 0 到达之前跳转到广播时,它们将等待直到广播发生。MPI_Comm commMPI_COMM_WORLD

于 2013-01-22T13:46:23.047 回答