MPI和C中的矩阵向量乘法

5

我正在尝试使用MPI和C将一个矩阵与一个向量相乘。我必须使用MPI_Allgather将矩阵的所有部分发送到所有进程。目前为止,这是我的代码:

#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 

好的,现在可以了!它已经编译成功,但是然后我遇到了一个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

有人能帮帮我吗?

a 是一个二维数组,但你却像它只有一维那样访问它。正确的语法应该是 a[??][??] 而不是 a[??],但你需要根据你的数据布局来填写 ?? 的位置。 - user786653
我像一维数组一样访问它,因为它在不同的进程中被分成了行。 - Mário Santana
我已经尝试过了,但仍然出现了错误。不知道该如何扭转局面。 - Mário Santana
1个回答

2

无论您是使用一维数组(行主序或列主序)访问二维数组,都可以将二维数组分配为a[DIM*DIM]而不是a[DIM][DIM],并以线性方式访问它。这个解决方案对我有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接