使用 MPI 进行并行编程以使用动态二维数组进行矩阵乘法时如何解决问题?

How to fix issue while doing parallel programming with MPI for Matrix-Multiplication with dynamic 2D array?

我正在尝试使用 MPI 创建三个矩阵 a、b、c,其中 c = a*b。另外,我将这些矩阵的长度取为 N(对所有矩阵都是通用的),因为我必须创建一个方阵。但是,每当我在运行时输入 N 的值时,都会出现分段错误,如果我在程序中输入 N 的值,那么它就可以正常工作。

我已经按照此处给出的分散和聚集进行了尝试: .现在我必须动态地 di,这样才能检查程序执行它所花费的时间。只是想告知我已经完成了这个 OpenMP,这很棒,但想比较哪个真的好,即 OpenMP 或 MPI。

#include <iostream>
#include <math.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stddef.h>
#include "mpi.h"


int main(int argc, char *argv[])
{
    int i, j, k, rank, size, tag = 99, blksz, sum = 0,N=0;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    int aa[N],cc[N];
    if(rank ==0)
    {
      std::cout << "input value of N" << '\n';
      std::cin >> N;
    }
    MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD);

    int **a = new int*[N];
    for (int i = 0; i < N; i++)
        a[i] = new int[N];

    int **b = new int*[N];
    for (int i = 0; i < N; i++)
        b[i] = new int[N];

    int **c = new int*[N];
    for (int i = 0; i < N; i++)
        c[i] = new int[N];

        if (rank == 0)
        {

            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                        a[i][j] =rand() % 10;
                        std::cout << a[i][j];
                }
                std::cout << '\n';
            }

            std::cout << '\n';

            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                        b[i][j] =rand() % 10;
                        std::cout << b[i][j];
                }
                std::cout << '\n';
            }
        }

    MPI_Scatter(a, N*N/size, MPI_INT, aa, N*N/size, MPI_INT,0,MPI_COMM_WORLD);

    //broadcast second matrix to all processes
    MPI_Bcast(b, N*N, MPI_INT, 0, MPI_COMM_WORLD);

    MPI_Barrier(MPI_COMM_WORLD);

          //perform vector multiplication by all processes
          for (i = 0; i < N; i++)
          {
                    for (j = 0; j < N; j++)
                    {
                            sum = sum + aa[j] * b[j][i];  //MISTAKE_WAS_HERE
                    }
                    cc[i] = sum;
                    sum = 0;
            }

    MPI_Gather(cc, N*N/size, MPI_INT, c, N*N/size, MPI_INT, 0, MPI_COMM_WORLD);

    MPI_Barrier(MPI_COMM_WORLD);

    MPI_Finalize();


    if (rank == 0)                         //I_ADDED_THIS
    {
      for (i = 0; i < N; i++) {
              for (j = 0; j < N; j++)
              {
                      std::cout << a[i][j]<< '\n';
              }
              std::cout << '\n';
      }
      std::cout << '\n' << '\n';

    }
    delete *a;
    delete *b;
    delete *c;
}


我得到的错误是:

mpirun 注意到节点 localhost 上 PID 为 3580 的进程等级 3 在信号 11(分段错误)上退出。

我只是想在这里完成矩阵乘法。

像这样声明数组

 int **a = new int*[N];
    for (int i = 0; i < N; i++)
        a[i] = new int[N]; 

不会在连续的内存位置分配它。将上面的声明替换为以下之一将使应用程序工作。

 int a[N][N]; // or
 int **a=malloc(N*N*sizeof(int));

MPI_Scatter、Gather 等在具有连续内存位置的数组上工作。

#include <iostream>
#include <math.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stddef.h>
#include "mpi.h"


int main(int argc, char *argv[])
{
    int i, j, k, rank, size, tag = 99, blksz, sum = 0,N=0;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    if(rank ==0)
    {
      std::cout << "input value of N" << '\n';
      std::cin >> N;
    }
    MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD);
int size_array=(N*N)/size;
int aa[size_array],cc[size_array]; // Declare arrays here since value of N is 0 otherwise 
    int a[N][N];
    int b[N][N];
    int c[N][N];
    for (int i = 0; i < N; i++)
        c[i] = new int[N];

        if (rank == 0)
        {

            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                        a[i][j] =rand() % 10;
                        std::cout << a[i][j];
                }
                std::cout << '\n';
            }

            std::cout << '\n';

            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                        b[i][j] =rand() % 10;
                        std::cout << b[i][j];
                }
                std::cout << '\n';
            }
        }

    MPI_Scatter(a, N*N/size, MPI_INT, aa, N*N/size, MPI_INT,0,MPI_COMM_WORLD);

    //broadcast second matrix to all processes
    MPI_Bcast(b, N*N, MPI_INT, 0, MPI_COMM_WORLD);

    MPI_Barrier(MPI_COMM_WORLD);

          //perform vector multiplication by all processes
          for (i = 0; i < N; i++)
          {
                    for (j = 0; j < N; j++)
                    {
                            sum = sum + aa[j] * b[j][i];  //MISTAKE_WAS_HERE
                    }
                    cc[i] = sum;
                    sum = 0;
            }

    MPI_Gather(cc, N*N/size, MPI_INT, c, N*N/size, MPI_INT, 0, MPI_COMM_WORLD);

    MPI_Barrier(MPI_COMM_WORLD);

    MPI_Finalize();


    if (rank == 0)                         //I_ADDED_THIS
    {
      for (i = 0; i < N; i++) {
              for (j = 0; j < N; j++)
              {
                      std::cout << a[i][j]<< '\n';
              }
              std::cout << '\n';
      }
      std::cout << '\n' << '\n';

    }
}

scanf之后声明数组int aa[N],cc[N];