c - MPI 和 C 中的矩阵向​​量乘法

标签 c mpi matrix-multiplication

我正在尝试使用 MPI 和 C 将方阵乘以 vector 。我必须使用 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[DIM*DIM] 而不是 a[DIM][DIM] 并以线性方式访问它。 这个解决方案对我有用。

关于c - MPI 和 C 中的矩阵向​​量乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13749580/

相关文章:

c - 如何在c中获取文件长度?

compilation - 编译期间openmpi错误

julia - Julia 中的特征分解和 "composition"

c - scanf() 将换行符保留在缓冲区中

c - 程序崩溃,无法解释原因?

sockets - 在不同的 MPI 程序之间进行通信

c - MPI 如何决定我们创建多少个进程

java - 如何生成所有矩阵乘法顺序组合

multithreading - Matlab中的多线程稀疏矩阵乘法

c - C中的重复符号错误