C 用于测试带有seg故障的MPI类型向量崩溃的简单MPI代码-为什么?
我有下面的代码,我用它来测试我如何在另一个程序中使用MPI_Type_vector。我编写了这个小测试程序,这样我就可以检查我给MPI_Type_vector的参数,确保它们提取了数组的正确部分。然而,它似乎不能正常工作——它在运行时会出现分段错误(即使它先执行一些输出),我似乎不知道为什么 有什么想法吗 代码如下。第一个函数(C 用于测试带有seg故障的MPI类型向量崩溃的简单MPI代码-为什么?,c,mpi,parallel-processing,hpc,C,Mpi,Parallel Processing,Hpc,我有下面的代码,我用它来测试我如何在另一个程序中使用MPI_Type_vector。我编写了这个小测试程序,这样我就可以检查我给MPI_Type_vector的参数,确保它们提取了数组的正确部分。然而,它似乎不能正常工作——它在运行时会出现分段错误(即使它先执行一些输出),我似乎不知道为什么 有什么想法吗 代码如下。第一个函数(alloc\u 3d\u int)是由其他人提供给我的,但经过了很好的测试 #include <stdio.h> #include <stdlib.h&
alloc\u 3d\u int
)是由其他人提供给我的,但经过了很好的测试
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "array_alloc.h"
#include <mpi.h>
/* 3D array allocation program given to me by someone else */
int ***alloc_3d_int ( int ndim1, int ndim2, int ndim3 ) {
int *space = malloc( ndim1 * ndim2 * ndim3 * sizeof( int ) );
int ***array3 = malloc( ndim1 * sizeof( int ** ) );
int i, j;
if( space == NULL || array3 == NULL )
return NULL;
for( j = 0; j < ndim1; j++ ) {
array3[ j ] = malloc( ndim2 * sizeof( int * ) );
if( array3[ j ] == NULL )
return NULL;
for( i = 0; i < ndim2; i++ )
array3[ j ][ i ] = space + j * ( ndim3 * ndim2 ) + i * ndim3;
}
return array3;
}
void print_data(int *start, int count, int blocklen, int stride)
{
int i, j;
int *curr;
int *new;
MPI_Datatype new_type;
/* Create an array to store the output in - just a 1D array */
new = alloc_1d_int(count*blocklen);
/* Create the vector type using the parameters given to the function (came from the cmd line args) */
MPI_Type_vector(count, blocklen, stride, MPI_INT, &new_type);
MPI_Type_commit(&new_type);
/* Do the send and receive to this process */
MPI_Sendrecv(&start, 1, new_type, 0, 0, &new, count*blocklen, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
/* Loop through the array it was received into, printing values */
for (i = 0; i < count*blocklen; i++)
{
printf("%d\n", new[i]);
}
printf("Done loop");
}
int main(int argc, char ** argv)
{
int ***data;
int i, j, k;
int num;
int a, b, c;
MPI_Init(&argc, &argv);
/* Create a 3D array */
data = alloc_3d_int(2, 3, 4);
num = 1;
/* Fill array with test values */
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
for (k = 0; k < 4; k++)
{
data[i][j][k] = num;
num++;
}
}
}
/* Get values from cmd line arguments */
a = atoi(argv[1]);
b = atoi(argv[2]);
c = atoi(argv[3]);
printf("Using count = %d, blocklength = %d and stride = %d\n", a, b, c);
/* Do the communication and print results */
print_data(&data[0][0][0], a, b, c);
MPI_Finalize();
}
#包括
#包括
#包括
#包括“数组_alloc.h”
#包括
/*其他人给我的3D阵列分配程序*/
int***alloc_3d_int(int ndim1、int ndim2、int ndim3){
int*space=malloc(ndim1*ndim2*ndim3*sizeof(int));
int***array3=malloc(ndim1*sizeof(int**));
int i,j;
if(空格==NULL | |数组3==NULL)
返回NULL;
对于(j=0;j
您希望接收到new,not&new,并从start发送,not&start。习惯的力量,我知道,也一直吸引着我 您希望接收到new,not&new,并从start开始发送,not&start。习惯的力量,我知道,也一直吸引着我 这把它修好了。干杯我可以问一下为什么会出现这种情况吗?对于缓冲区,您希望向MPI发送一个指针,指向数据所在的位置(或您希望它去的位置)send
和new
已经是分配内存的指针;您将send
作为&(数据[0][0][0])
传入,它是指向内存中第一个元素的指针,new
是指向malloc()ed
块开头的指针。取消对它们的引用意味着您现在不再传递指向该数据的指针,而是指向指针本身的指针。在本例中,这两个指针实际上都存在于堆栈上(作为局部变量和参数),因此您最终会写入所有堆栈内容。这就解决了这个问题。干杯我可以问一下为什么会出现这种情况吗?对于缓冲区,您希望向MPI发送一个指针,指向数据所在的位置(或您希望它去的位置)send
和new
已经是分配内存的指针;您将send
作为&(数据[0][0][0])
传入,它是指向内存中第一个元素的指针,new
是指向malloc()ed
块开头的指针。取消对它们的引用意味着您现在不再传递指向该数据的指针,而是指向指针本身的指针。在本例中,这两个指针实际上都位于堆栈上(作为局部变量和参数),因此您最终会写入所有堆栈内容。