c - MPI_Scatter 无效指针错误:MPI_Type_vector 可能有问题
问题描述
我正在编写一个程序来将形状为 84*84 的矩阵分散到 4 个进程中。每个进程接收一个形状为 84*21 的子矩阵。它看起来像这样:
1 1 2 2 3 3 4 4
1 1 2 2 3 3 4 4
1 1 2 2 3 3 4 4
1 1 2 2 3 3 4 4
1 1 2 2 3 3 4 4
1 1 2 2 3 3 4 4
1 1 2 2 3 3 4 4
1 1 2 2 3 3 4 4
然后,我应该创建一个新的数据类型来表示这种离散的数据形式。我使用MPI_Type_vector
andMPI_Type_create_resized
来创建这样的数据类型。但是,在MPI_Scatter
函数中引发了无效指针异常,我认为我没有进行任何不正确的内存操作。有什么陷阱什么的吗?
顺便说一句,只有当 n 大于 80 时才会出现问题。
这是代码。
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
/*
* compile command:
* mpiicc -g -c matvectmul_col_mul_version.c -o matvectmul_col_mul_version.o
* mpiicc matvectmul_col_mul_version.o -o matvectmul_col_mul_version
*
* run command:
* srun -n 4 -l ./matvectmul_col_mul_version 84
*/
int main(int argc, char* argv[]) {
int n = atoi(argv[1]);
double *loc_matrix = NULL;
int my_rank, comm_sz;
MPI_Init(NULL, NULL);
MPI_Comm comm = MPI_COMM_WORLD;
MPI_Comm_size(comm, &comm_sz);
MPI_Comm_rank(comm, &my_rank);
int m = n / comm_sz;
MPI_Datatype vect_mpi_t;
loc_matrix = (double*)malloc(n*m*sizeof(double));
double *matrix = NULL;
double *vector = NULL;
if (my_rank == 0) {
matrix = (double*)malloc(n*n*sizeof(double));
memset(matrix,0,n*n*sizeof(double));
}
MPI_Datatype mpi_tmp_t;
MPI_Type_vector(n,m,n,MPI_DOUBLE,&mpi_tmp_t);
MPI_Type_create_resized(mpi_tmp_t,0,m*sizeof(double),&vect_mpi_t);
MPI_Type_commit(&vect_mpi_t);
MPI_Scatter(matrix,1,vect_mpi_t,loc_matrix,n*m,MPI_DOUBLE,0,MPI_COMM_WORLD);
//MPI_Scatter(matrix,1,vect_mpi_t,loc_matrix,1,vect_mpi_t,0,MPI_COMM_WORLD); Also not work
if (my_rank == 0) {
free(matrix);
}
MPI_Finalize();
return 0;
}
这是错误信息:
srun: error: cn003: task 0: Aborted (core dumped)
0: *** Error in `/home/2016011275/workspace/HW1/task2/./matvectmul_col_mul_version': free(): invalid pointer: 0x0000000001917840 $
**
0: ======= Backtrace: =========
0: /lib64/libc.so.6(+0x7c503)[0x7fce91e51503]
0: /apps/rm/intel/compilers_and_libraries_2018.0.128/linux/mpi/intel64/lib/libmpi.so.12(+0x2f08ab)[0x7fce92fc68ab]
0: /apps/rm/intel/compilers_and_libraries_2018.0.128/linux/mpi/intel64/lib/libmpi.so.12(+0x4d97c6)[0x7fce931af7c6]
0: /apps/rm/intel/compilers_and_libraries_2018.0.128/linux/mpi/intel64/lib/libmpi.so.12(+0x4de03f)[0x7fce931b403f]
0: /apps/rm/intel/compilers_and_libraries_2018.0.128/linux/mpi/intel64/lib/libmpi.so.12(PMPI_Scatter+0x360)[0x7fce931b2c00]
0: /home/2016011275/workspace/HW1/task2/./matvectmul_col_mul_version[0x400e63]
0: /lib64/libc.so.6(__libc_start_main+0xf5)[0x7fce91df6b35]
0: /home/2016011275/workspace/HW1/task2/./matvectmul_col_mul_version[0x400ae9]
0: ======= Memory map: ========
0: 00400000-00402000 r-xp 00000000 00:29 11118498385 /home/2016011275/workspace/HW1/task2/matvectmul_col_mu
l_version
0: 00601000-00602000 r--p 00001000 00:29 11118498385 /home/2016011275/workspace/HW1/task2/matvectmul_col_mu
l_version
0: 00602000-00603000 rw-p 00002000 00:29 11118498385 /home/2016011275/workspace/HW1/task2/matvectmul_col_mu
l_version
解决方案
推荐阅读
- java - 无法从 Maven 运行 testNG 测试
- react-native - React Native 驾照扫描仪
- php - 更改 Woocommerce 简码上的产品元查询
- node.js - 使用Angular 6在节点服务器中创建和上传文本文件
- android - 更改自定义键盘的布局
- javascript - 如何在 JavaScript 和 PHP 中迭代对象
- go - 在golang中将时间戳转换为ISO格式
- cordova - 任务':preReleaseBuild的构建错误“无法捕获输入文件的快照”
- mongodb - 如何在 MongoDB 中上传图像并将其显示在我的模板中?
- java - yml 中的 KeyStore 配置