2012-12-06 193 views
2

我想通过使用MPI和C的向量乘以方阵。我必须使用MPI_Allgather将矩阵的所有部分发送到所有进程。这是我到目前为止矩阵向量乘法在MPI和C

#include "mpi.h" 
#include <stdlib.h> 
#include <stdio.h> 
#include <math.h> 
#include <string.h> 
#include <time.h> 
#define DIM 500 

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

     int i, j, n; 
     int nlocal;  /* Number of locally stored rows of A */ 
     double *fb; 
     double a[DIM*DIM], b[DIM], x[DIM];  /* Will point to a buffer that stores the entire vector b */ 
     int npes, myrank; 
     MPI_Status status; 

     MPI_Init(&argc,&argv); 

    /* Get information about the communicator */ 
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank); 
    MPI_Comm_size(MPI_COMM_WORLD, &npes); 

    /* Allocate the memory that will store the entire vector b */ 
    fb = (double*)malloc(n*sizeof(double)); 

    nlocal = n/npes; 

    /* Gather the entire vector b on each processor using MPI's ALLGATHER operation */ 
    MPI_Allgather(b, nlocal, MPI_DOUBLE, fb, nlocal, MPI_DOUBLE, MPI_COMM_WORLD); 

    /* Perform the matrix-vector multiplication involving the locally stored submatrix */ 
    for (i=0; i<nlocal; i++) { 
     x[i] = 0.0; 
     for (j=0; j<n; j++) 
     x[i] += a[i*n+j]*fb[j]; 
    } 


    free(fb); 

    MPI_Finalize(); 
}//end main 

OK现在,它的工作原理!它编译,但然后我得到一个mpi allgather内部错误!我也尝试了其他解决方案。

Fatal error in MPI_Allgather: Internal MPI error!, error stack: 
MPI_Allgather(961).......: MPI_Allgather(sbuf=0xa1828, scount=407275437, MPI_DOU                            BLE, rbuf=0xf61d0008, rcount=407275437, MPI_DOUBLE, MPI_COMM_WORLD) failed 
MPIR_Allgather_impl(807).: 
MPIR_Allgather(766)......: 
MPIR_Allgather_intra(560): 
MPIR_Localcopy(357)......: memcpy arguments alias each other, dst=0xb8513d70 src                            =0xa1828 len=-1036763800 
Fatal error in MPI_Allgather: Internal MPI error!, error stack: 
MPI_Allgather(961).......: MPI_Allgather(sbuf=0xa1828, scount=407275437, MPI_DOU                            BLE, rbuf=0xf61d0008, rcount=407275437, MPI_DOUBLE, MPI_COMM_WORLD) failed 
MPIR_Allgather_impl(807).: 
MPIR_Allgather(766)......: 
MPIR_Allgather_intra(560): 
MPIR_Localcopy(357)......: memcpy arguments alias each other, dst=0x3cb9b840 src                            =0xa1828 len=-1036763800 
Fatal error in MPI_Allgather: Internal MPI error!, error stack: 
MPI_Allgather(961).......: MPI_Allgather(sbuf=0xa1828, scount=407275437, MPI_DOU                            BLE, rbuf=0xf61d0008, rcount=407275437, MPI_DOUBLE, MPI_COMM_WORLD) failed 
MPIR_Allgather_impl(807).: 
MPIR_Allgather(766)......: 
MPIR_Allgather_intra(560): 
MPIR_Localcopy(357)......: memcpy arguments alias each other, dst=0x7a857ad8 src                            =0xa1828 len=-1036763800 

任何人都可以帮我吗?

+0

'a'是一个二维数组,但您正在访问它,因为它只有一个维度。语法是'a [??] [??]'而不是'a [??]',但是你放置什么取决于你的数据布局。 – user786653

+0

我喜欢一维,因为它在不同的过程中通过行分解。 –

+0

我试过,但仍然给我那个错误。不知道如何打开这个圆环 –

回答

1

任何方式要访问使用一个维(行或列主要), 可以分配二维阵列作为a[DIM*DIM]代替a[DIM][DIM]和访问此以线性方式的二维阵列。 此解决方案正在为我工​​作。

+0

它工作!谢谢!但现在我得到一个内部的mpi错误 –