【发布时间】:2018-03-26 20:35:10
【问题描述】:
这是我想将二维数组与向量数组相乘的代码:
#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)\第二学期\并行 编程\程序\arr_multi\Debug>mpiexec -n 4 arr_multi.exe 回车 行和列:3 2
作业中止:[ranks] 消息
[0-1] 终止
[2] 进程退出而不调用 finalize
[3] 终止
----错误分析-----
[2] 在 RAMISHA-PC arr_multi.exe 上过早结束并且可能已经 坠毁。退出代码0xc0000005
----错误分析-----
我声明了一个位置连续的数组,并且我的行在进程之间正确划分。我认为我的数据结构有问题,并尝试了很多解决方案但徒劳无功。
【问题讨论】:
-
当你使用你的调试器时,程序崩溃时是哪一行?
-
我无法摆脱你用
MPI_Send发送的信息与用MPI_Recv接收的信息不同的感觉。请记住,两个分支中的array都是指针 数组,通常不能在进程之间传输指针(即使是从同一个程序创建的)。 -
请不要粘贴文字截图,粘贴文字本身
-
当您使用调试器执行程序时,一次一行,并检查每一步中所有变量的值,您做了什么观察?
-
在非根级别上,您应该访问
array[i-start][j]吗?您的调试语句也不正确。一般来说,你应该MPI_Scatterv(array, ...),你不需要传输start、close或cols。
标签: c++ parallel-processing mpi