程序过早结束,可能已经崩溃。退出代码 0xc0000005

program ended prematurely and may have crashed. exit code 0xc0000005

这是我的代码,我想将二维数组与向量数组相乘:

#include<iostream>
#include<mpi.h>
using namespace std;
int v_array[10] ;
int ch_size, start, close;
int res ;
int rows, cols;

int main(int argc, char *argv[])
{
    int pro_id, tot_pros;
    MPI_Init(&argc, &argv); 
    MPI_Comm_rank(MPI_COMM_WORLD, &pro_id); 
    MPI_Comm_size(MPI_COMM_WORLD, &tot_pros); 

    if (pro_id == 0) {
        cout << "Enter rows and columns: ";
        cin >> rows >> cols;

        int **array = new int*[rows];
        int size1 = rows * cols;
        array[0] = new int[size1];
        for (int j = 1; j < rows; j++) {
            array[j] = &array[0][j*cols];
        }
        for (int i = 0; i < rows; i++) {
            v_array[i] = 1;
            for (int j = 0; j < cols; j++) {
                array[i][j] = 1;
            }
        }
        for (int i = 1; i < tot_pros; i++) {
            ch_size = (rows / (tot_pros - 1));
            start = (i - 1) * ch_size;
            if (((i + 1) == tot_pros) && ((rows % (tot_pros - 1)) != 0)) {
                close = rows;
            }
            else {
                close = start + ch_size;
            }
            MPI_Send(&start, 1, MPI_INT, i, 1, MPI_COMM_WORLD);
            MPI_Send(&close, 1, MPI_INT, i, 2, MPI_COMM_WORLD);
            MPI_Send(&cols, 1, MPI_INT, i, 4, MPI_COMM_WORLD);
            MPI_Send(&array[start][0], ch_size *cols, MPI_INT, i, 3, MPI_COMM_WORLD);
        }
    }
    else
     {
        int cols;
        MPI_Recv(&start, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
        MPI_Recv(&close, 1, MPI_INT, 0, 2, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
        MPI_Recv(&cols, 1, MPI_INT, 0, 4, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
        int **array = new int*[(close - start)*cols];
        MPI_Recv(array, (close - start) *cols , MPI_INT, 0, 3, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
        for (int i = start; i < close; i++) {
            for (int j = 0; j < cols; j++) {
                cout << array[i]<<array[j];
                    res += array[i][j] * v_array[i];
                    cout << res;
                }
            }
         }
    MPI_Finalize(); 
    return 0;
}

同一个程序在我有静态数组时工作正常,但在动态数组中我得到了这个错误。

E:\MS(CS)nd Semester\Parallel Programing\programs\arr_multi\Debug>mpiexec -n 4 arr_multi.exe Enter rows and columns: 3 2

job aborted: [ranks] message

[0-1] terminated

[2] process exited without calling finalize

[3] terminated

---- error analysis -----

[2] on RAMISHA-PC arr_multi.exe ended prematurely and may have crashed. exit code 0xc0000005

---- error analysis -----

我声明了一个具有连续位置的数组,并且我的行在进程之间正确划分。我认为我的数据结构有问题并尝试了很多解决方案但徒劳无功。

首先, 种调试 MPI 应用程序的方法,这应该是您的首要任务。 multi-process 应用程序的一般方法是在应用程序开始时暂停,例如使用 getchar() 然后使用调试器附加到每个进程,如 here:

所述
  • compile, link and start running your MPI program (you may wish to put a read statement early on to hold the program while you do the next steps)
  • attach to one of the currently running MPI processes: Debug - Attach to Process brings up a dialogue box which lists Available Processes. You should see NUM instances (where N is from mpiexec -n NUM) of your executable. Select all of these and click on Attach. You can now debug by adding breakpoints etc. To move between MPI processes use the Process drop-down menu just above the code listing.

话虽如此,至少有一个问题与此部分有关:int **array = new int*[(close - start)*cols];(在应用程序的接收部分)。您分配了第一个维度而不是第二个维度,因此第一个维度中的所有指针都未初始化。

将其更改为:

        int *array = new int[(close - start) * cols];
        MPI_Recv(array, (close - start) *cols, MPI_INT, 0, 3, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
        for (int i = start; i < close; i++) {
            for (int j = 0; j < cols; j++) {
                cout << array[(i - start) * cols];
                res += array[(i - start) * cols] * v_array[i];
                cout << res;
            }
        }
        delete[] array;

或者如果你真的想使用二维数组,从发送部分复制初始化代码:

        int rows = close - start;
        int **array = new int*[rows];
        int size1 = rows * cols;
        array[0] = new int[size1];
        for (int j = 1; j < rows; j++) {
            array[j] = &array[0][j*cols];
        }

第二个问题是 v_array,作为一个全局变量没有在你的接收进程中被初始化。请记住,在 MPI 中,每个进程都是一个独立的程序。所以你应该总是初始化 v_array,即不管 pro_id.

        for (int i = 0; i < rows; i++) {
            v_array[i] = 1;
        }