2017-03-27 49 views
1

我有一個簡單的程序,我想在多臺計算機上分散結構,但似乎我已經錯誤地定義了數據類型,即使程序編譯正常。我有以下代碼。運行mpirun時無效的數據類型

#include <mpi.h> 
#include <stdio.h> 
#include <stdlib.h> 

typedef struct small_pixel_s { 
    double red; 
    double green; 
    double blue; 
} SMALL_PIXEL; 

int main(int argc, char **argv) { 
    int size, rank; 

    MPI_Init(&argc, &argv); 
    MPI_Comm_size(MPI_COMM_WORLD, &size); 
    MPI_Comm_rank(MPI_COMM_WORLD, &rank); 

    SMALL_PIXEL global[8]; 
    SMALL_PIXEL local[2]; 
    const int root = 0; 

    MPI_Status status; 
    MPI_Datatype small_pixel_type; 
    MPI_Datatype type[3] = { MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE }; 
    int blocklen[3] = { 1, 1, 1 }; 
    MPI_Aint offsets[3]; 

    offsets[0] = offsetof(SMALL_PIXEL, red); 
    offsets[1] = offsetof(SMALL_PIXEL, green); 
    offsets[2] = offsetof(SMALL_PIXEL, blue); 

    MPI_Type_create_struct(8, blocklen, offsets, type, &small_pixel_type); 
    MPI_Type_commit(&small_pixel_type);  

    if (rank == root) { 
     for (int i=0; i<7; i++) { 
      global[i].red = i; 
      global[i].green = i; 
      global[i].blue = i; 
     } 
    } 

    MPI_Scatter(global, 2, small_pixel_type,  /* send everyone 2 ints from global */ 
       local, 2, small_pixel_type,  /* each proc receives 2 ints into local */ 
       root, MPI_COMM_WORLD); /* sending process is root, all procs in */ 
             /* MPI_COMM_WORLD participate */ 
    for (int i=0; i<2; i++) { 
     local[i].red = local[i].red + 4; 
     local[i].green = local[i].green + 4; 
     local[i].blue = local[i].blue + 4; 
    } 

    MPI_Gather(local, 2, small_pixel_type,  /* everyone sends 2 ints from local */ 
       global, 2, small_pixel_type,  /* root receives 2 ints each proc into global */ 
       root, MPI_COMM_WORLD); /* recv'ing process is root, all procs in */ 
             /* MPI_COMM_WORLD participate */ 
    if (rank == 0) { 
     for (int i=0; i<7; i++) { 
      printf("%f\n", global[i].red); 
     } 
    } 

    MPI_Finalize(); 
    return 0; 
} 

任何想法爲什麼?我應該如何定義它可以分散的結構?

當運行程序我得到*** MPI_ERR_TYPE: invalid datatype

回答

2

MPI_Type_create_struct,第一個參數是count

塊數目(整數)的---在陣列array_of_types,array_of_displacements和array_of_blocklengths也數量的條目的

在你的情況,這將是,不。因此,改變這一行:

MPI_Type_create_struct(8, blocklen, offsets, type, &small_pixel_type); 

這樣:

MPI_Type_create_struct(3, blocklen, offsets, type, &small_pixel_type); 
相關問題