Простой код MPI для проверки сбоя MPI_Type_vector с ошибкой сегмента - почему? - PullRequest
1 голос
/ 12 мая 2011

У меня есть следующий код, который я использую, чтобы проверить, как я использую MPI_Type_vector в другой программе. Я написал эту небольшую тестовую программу, чтобы можно было проверить параметры, которые я даю MPI_Type_vector, чтобы убедиться, что они извлекают правильные части массива. Однако, похоже, что он не работает должным образом - он выдает ошибки сегментации при запуске (даже если сначала выполняет некоторые выходные данные), и я не могу понять, почему.

Есть идеи?

Код ниже. Первая функция (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();
}

1 Ответ

1 голос
/ 12 мая 2011

Вы хотите получать новые, а не & новые и отправлять с начала, а не с начала. Я знаю, что сила привычки захватывает меня все время.

...