gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 01:35:29 26 4
gpt4 key购买 nike

这是我的代码,我想将一个二维数组与一个 vector 数组相乘:

#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)\2nd 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 应用程序,这应该是您的首要任务。多进程应用程序的一般方法是在应用程序开始时暂停,例如使用 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;
}

关于c++ - 程序过早结束,可能已经崩溃。退出代码 0xc0000005,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49476321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com