简体   繁体   English

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

[英]Matrix Vector multiplication in MPI and C

i'm trying to multiply a square matrix by a vector using MPI and C. I must use MPI_Allgather to send all the parts of the matrix to all the processes. 我正在尝试使用MPI和C将方形矩阵乘以向量。我必须使用MPI_Allgather将矩阵的所有部分发送到所有进程。 This is what i have so far 这就是我到目前为止所拥有的

#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 Now it works! 好现在它有效! It compiles but then i get an mpi allgather internal error! 它编译但然后我得到一个mpi allgather内部错误! I got this also with other solution i tried. 我也尝试了其他解决方案。

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

Can anyone help me? 谁能帮我?

Any way you are accessing the two dimensional array using one dimension(row or column major), you can allocate two dimensional array as a[DIM*DIM] instead of a[DIM][DIM] and access this in a linear fashion. 无论您使用一个维度(行或列主要)访问二维数组,您都可以将二维数组分配为a[DIM*DIM]而不是a[DIM][DIM]并以线性方式访问它。 This solution is working for me. 这个解决方案对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM