1
다른 프로그램에서 MPI_Type_vector를 사용하는 방법을 테스트하기 위해 다음 코드를 사용했습니다. 필자는이 작은 테스트 프로그램을 작성하여 MPI_Type_vector에 배열의 올바른 부분을 추출했는지 확인하는 매개 변수를 확인할 수있게했습니다. 그러나 제대로 작동하지 않는 것 같습니다. 출력이 나올 때 세그멘테이션 오류가 발생하고 그 이유를 알아낼 수 없습니다.seg 결함으로 MPI_Type_vector가 충돌하는지 테스트하는 간단한 MPI 코드 - 이유는 무엇입니까?
아이디어가 있으십니까?
코드는 다음과 같습니다. 첫 번째 기능 (alloc_3d_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();
}
이 문제가 해결되었습니다. 건배. 왜 이런 경우인지 물어봐도 될까요? – robintw
버퍼의 경우 MPI에 데이터가있는 위치 (또는 원하는 위치)에 대한 포인터를 보내려고합니다. 'send'와'new'는 이미 할당 된 메모리에 대한 포인터입니다; 메모리의 첫 번째 요소에 대한 포인터 인'& (data [0] [0] [0])'을'send '로 전달했고,'new'는'malloc() ed '블록. 이를 역 참조한다는 것은 이제 더 이상 포인터에 대한 데이터를 전달하지 않고 포인터 자체에 대한 포인터임을 의미합니다. 이 경우 두 포인터는 실제로 스택에 (로컬 변수 및 인수로) 저장되므로 결국 스택 내용 전체에 쓰게됩니다. –