|
| 1 | +/* -*- Mode: C; c-basic-offset:4 ; -*- */ |
| 2 | +/* |
| 3 | + * Copyright (c) 2016 The University of Tennessee and The University |
| 4 | + * of Tennessee Research Foundation. All rights |
| 5 | + * reserved. |
| 6 | + * $COPYRIGHT$ |
| 7 | + * |
| 8 | + * Additional copyrights may follow |
| 9 | + * |
| 10 | + * $HEADER$ |
| 11 | + */ |
| 12 | + |
| 13 | +#include <stdio.h> |
| 14 | +#include <stdlib.h> |
| 15 | +#include <mpi.h> |
| 16 | + |
| 17 | +void dump_hex(void* what, size_t length) |
| 18 | +{ |
| 19 | + int i; |
| 20 | + for( i = 0; i < length; i++ ) { |
| 21 | + printf("%02x", (unsigned int)(((unsigned char*)what)[i])); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +int pack_unpack_datatype( void* send_data, MPI_Datatype datatype, int count, |
| 26 | + void* recv_data ) |
| 27 | +{ |
| 28 | + MPI_Aint position = 0, buffer_size; |
| 29 | + void* buffer; |
| 30 | + int error; |
| 31 | + |
| 32 | + error = MPI_Pack_external_size("external32", |
| 33 | + count, datatype, &buffer_size); |
| 34 | + if( MPI_SUCCESS != error ) goto return_error_code; |
| 35 | + |
| 36 | + buffer = (void*)malloc(buffer_size); |
| 37 | + if( NULL == buffer ) { error = MPI_ERR_UNKNOWN; goto return_error_code; } |
| 38 | + |
| 39 | + error = MPI_Pack_external("external32", (void*)send_data, count, datatype, |
| 40 | + buffer, buffer_size, &position); |
| 41 | + if( MPI_SUCCESS != error ) goto return_error_code; |
| 42 | + |
| 43 | + printf("packed "); dump_hex(buffer, buffer_size); printf("\n"); |
| 44 | + |
| 45 | + position = 0; |
| 46 | + error = MPI_Unpack_external("external32", buffer, buffer_size, &position, |
| 47 | + recv_data, count, datatype); |
| 48 | + if( MPI_SUCCESS != error ) goto return_error_code; |
| 49 | + free(buffer); |
| 50 | + |
| 51 | + return_error_code: |
| 52 | + return (error == MPI_SUCCESS ? 0 : -1); |
| 53 | +} |
| 54 | + |
| 55 | +int main(int argc, char *argv[]) |
| 56 | +{ |
| 57 | + MPI_Init(&argc, &argv); |
| 58 | + |
| 59 | + /* Simple contiguous data */ |
| 60 | + { |
| 61 | + int send_data[2] = {1234, 5678}; |
| 62 | + int recv_data[2] = {-1, -1}; |
| 63 | + |
| 64 | + printf("send data %08x %08x \n", send_data[0], send_data[1]); |
| 65 | + printf("data "); dump_hex(&send_data, sizeof(int) * 2); printf("\n"); |
| 66 | + |
| 67 | + (void)pack_unpack_datatype( send_data, MPI_INT, 2, |
| 68 | + recv_data ); |
| 69 | + |
| 70 | + printf("recv "); dump_hex(&recv_data, sizeof(int) * 2); printf("\n"); |
| 71 | + printf("recv data %08x %08x \n", recv_data[0], recv_data[1]); |
| 72 | + if( (send_data[0] != recv_data[0]) || (send_data[1] != recv_data[1]) ) { |
| 73 | + printf("Error duing external32 pack/unack for contiguous types\n"); |
| 74 | + exit(-1); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /* Vector datatype */ |
| 79 | + printf("\n\nVector datatype\n\n"); |
| 80 | + { |
| 81 | + int send_data[3] = {1234, 0, 5678}; |
| 82 | + int recv_data[3] = {-1, -1, -1}; |
| 83 | + MPI_Datatype ddt; |
| 84 | + |
| 85 | + MPI_Type_vector(2, 1, 2, MPI_INT, &ddt); |
| 86 | + MPI_Type_commit(&ddt); |
| 87 | + printf("send data %08x %x08x %08x \n", send_data[0], send_data[1], send_data[2]); |
| 88 | + printf("data "); dump_hex(&send_data, sizeof(int) * 3); printf("\n"); |
| 89 | + |
| 90 | + (void)pack_unpack_datatype( send_data, ddt, 1, recv_data ); |
| 91 | + |
| 92 | + printf("recv "); dump_hex(&recv_data, sizeof(int) * 3); printf("\n"); |
| 93 | + printf("recv data %08x %08x %08x \n", recv_data[0], recv_data[1], recv_data[2]); |
| 94 | + MPI_Type_free(&ddt); |
| 95 | + if( (send_data[0] != recv_data[0]) || (send_data[2] != recv_data[2]) ) { |
| 96 | + printf("Error duing external32 pack/unack for vector types\n"); |
| 97 | + exit(-1); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + MPI_Finalize(); |
| 102 | + |
| 103 | + return 0; |
| 104 | +} |
0 commit comments