root/test/datatype/external32.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. dump_hex
  2. check_contiguous
  3. check_vector
  4. pack_unpack_datatype
  5. main

   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 
  16 #include "ompi_config.h"
  17 #include "ompi/datatype/ompi_datatype.h"
  18 #include "opal/runtime/opal.h"
  19 #include "opal/datatype/opal_convertor.h"
  20 #include "opal/datatype/opal_datatype_internal.h"
  21 #include <arpa/inet.h>
  22 
  23 static int verbose = 0;
  24  
  25 typedef int (*checker_t)(void*, void*, ompi_datatype_t *, int, void*);
  26 
  27 int check_contiguous( void* send_buffer, void* packed,
  28                       ompi_datatype_t * datatype, int count, void* arg );
  29 
  30 int check_vector( void* send_buffer, void* packed,
  31                   ompi_datatype_t * datatype, int count, void* arg );
  32 
  33 static int pack_unpack_datatype( void* send_data, ompi_datatype_t *datatype, int count,
  34                                  void* recv_data, checker_t validator, void *validator_arg );
  35 
  36 static void dump_hex(void* what, size_t length);
  37 
  38 static void dump_hex(void* what, size_t length)
  39 {
  40     size_t i;
  41     for( i = 0; i < length; i++ ) {
  42         printf("%02x", (unsigned int)(((unsigned char*)what)[i]));
  43     }
  44 }
  45 
  46 int check_contiguous( void* send_buffer, void* packed,
  47                       ompi_datatype_t* datatype, int count, void* arg )
  48 {
  49     int i;
  50 
  51     if( (datatype == &ompi_mpi_int.dt) || (datatype == &ompi_mpi_int32_t.dt) ) {
  52         uint32_t val;
  53         for( i = 0 ; i < count; i++ ) {
  54             val = htonl(((uint32_t*)send_buffer)[i]);
  55             if( val != ((uint32_t*)packed)[i] ) {
  56                 printf("Error at position %d expected %x found %x (type %s)\n",
  57                        i, ((uint32_t*)packed)[i], ((uint32_t*)send_buffer)[i], datatype->name);
  58                 return -1;
  59             }
  60         }
  61     } else if( (datatype == &ompi_mpi_short.dt) || (datatype == &ompi_mpi_int16_t.dt) ) {
  62         uint16_t val;
  63         for( i = 0 ; i < count; i++ ) {
  64             val = htons(((uint16_t*)send_buffer)[i]);
  65             if( val != ((uint16_t*)packed)[i] ) {
  66                 printf("Error at position %d expected %x found %x (type %s)\n",
  67                        i, ((uint16_t*)packed)[i], ((uint16_t*)send_buffer)[i], datatype->name);
  68                 return -1;
  69             }
  70         }
  71     } else {
  72         printf("Unknown type\n");
  73         return -1;
  74     }
  75     return 0;
  76 }
  77 
  78 int check_vector( void* send_buffer, void* packed,
  79                   ompi_datatype_t* datatype, int count, void* arg )
  80 {
  81     int i;
  82     ompi_datatype_t *origtype = (ompi_datatype_t *)arg;
  83 
  84     if( (origtype == &ompi_mpi_int.dt) || (origtype == &ompi_mpi_int32_t.dt) ) {
  85         uint32_t val;
  86         for( i = 0 ; i < count; i++ ) {
  87             val = htonl(((uint32_t*)send_buffer)[2*i]);
  88             if( val != ((uint32_t*)packed)[i] ) {
  89                 printf("Error at position %d expected %x found %x (type %s)\n",
  90                        i, ((uint32_t*)packed)[i], ((uint32_t*)send_buffer)[2*i], datatype->name);
  91                 return -1;
  92             }
  93         }
  94     } else if( (origtype == &ompi_mpi_short.dt) || (origtype == &ompi_mpi_int16_t.dt) ) {
  95         uint16_t val;
  96         for( i = 0 ; i < count; i++ ) {
  97             val = htons(((uint16_t*)send_buffer)[2*i]);
  98             if( val != ((uint16_t*)packed)[i] ) {
  99                 printf("Error at position %d expected %x found %x (type %s)\n",
 100                        i, ((uint16_t*)packed)[i], ((uint16_t*)send_buffer)[2*i], datatype->name);
 101                 return -1;
 102             }
 103         }
 104     } else {
 105         printf("Unknown %s type\n", datatype->name);
 106         return -1;
 107     }
 108     return 0;
 109 }
 110 
 111 static int pack_unpack_datatype( void* send_data, ompi_datatype_t *datatype, int count,
 112                                  void* recv_data,
 113                                  checker_t validator, void* validator_arg)
 114 {
 115     MPI_Aint position = 0, buffer_size;
 116     void* buffer;
 117     int error;
 118 
 119     error = ompi_datatype_pack_external_size("external32",
 120                                              count, datatype, &buffer_size);
 121     if( MPI_SUCCESS != error ) goto return_error_code;
 122 
 123     buffer = (void*)malloc(buffer_size);
 124     if( NULL == buffer ) { error = MPI_ERR_UNKNOWN; goto return_error_code; }
 125 
 126     error = ompi_datatype_pack_external("external32", (void*)send_data, count, datatype,
 127                                         buffer, buffer_size, &position);
 128     if( MPI_SUCCESS != error ) goto return_error_code;
 129     if( 0 != validator(send_data, buffer, datatype, count, validator_arg) ) {
 130         printf("Error during pack external. Bailing out\n");
 131         return -1;
 132     }
 133 
 134     printf("packed %ld bytes into a %ld bytes buffer ", position, buffer_size); dump_hex(buffer, position); printf("\n");
 135 
 136     position = 0;
 137     error = ompi_datatype_unpack_external("external32", buffer, buffer_size, &position,
 138                                           recv_data, count, datatype);
 139     if( MPI_SUCCESS != error ) goto return_error_code;
 140     free(buffer);
 141 
 142  return_error_code:
 143     return (error == MPI_SUCCESS ? 0 : -1);
 144 }
 145 
 146 int main(int argc, char *argv[])
 147 {
 148     opal_init_util(&argc, &argv);
 149     ompi_datatype_init();
 150 
 151     /* Simple contiguous data: MPI_INT32_T */
 152     {
 153         int32_t send_data[2] = {1234, 5678};
 154         int32_t recv_data[2] = {-1, -1};
 155 
 156         if( verbose ) {
 157             printf("send data %08x %08x \n", send_data[0], send_data[1]);
 158             printf("data "); dump_hex(&send_data, sizeof(int32_t) * 2); printf("\n");
 159         }
 160         (void)pack_unpack_datatype( send_data, &ompi_mpi_int32_t.dt, 2,
 161                                     recv_data, check_contiguous, (void*)&ompi_mpi_int32_t.dt );
 162         if( verbose ) {
 163             printf("recv "); dump_hex(&recv_data, sizeof(int32_t) * 2); printf("\n");
 164             printf("recv data %08x %08x \n", recv_data[0], recv_data[1]);
 165         }
 166         if( (send_data[0] != recv_data[0]) || (send_data[1] != recv_data[1]) ) {
 167             printf("Error during external32 pack/unack for contiguous types (MPI_INT32_T)\n");
 168             exit(-1);
 169         }
 170     }
 171     /* Simple contiguous data: MPI_INT16_T */
 172     {
 173         int16_t send_data[2] = {1234, 5678};
 174         int16_t recv_data[2] = {-1, -1};
 175 
 176         if( verbose ) {
 177             printf("send data %08x %08x \n", send_data[0], send_data[1]);
 178             printf("data "); dump_hex(&send_data, sizeof(int16_t) * 2); printf("\n");
 179         }
 180         (void)pack_unpack_datatype( send_data, &ompi_mpi_int16_t.dt, 2,
 181                                     recv_data, check_contiguous, (void*)&ompi_mpi_int16_t.dt );
 182         if( verbose ) {
 183             printf("recv "); dump_hex(&recv_data, sizeof(int16_t) * 2); printf("\n");
 184             printf("recv data %08x %08x \n", recv_data[0], recv_data[1]);
 185         }
 186         if( (send_data[0] != recv_data[0]) || (send_data[1] != recv_data[1]) ) {
 187             printf("Error during external32 pack/unack for contiguous types\n");
 188             exit(-1);
 189         }
 190     }
 191 
 192     /* Vector datatype */
 193     printf("\n\nVector datatype\n\n");
 194     {
 195         int count=2, blocklength=1, stride=2;
 196         int send_data[3] = {1234, 0, 5678};
 197         int recv_data[3] = {-1, -1, -1};
 198         ompi_datatype_t *ddt;
 199 
 200         ompi_datatype_create_vector ( count, blocklength, stride, &ompi_mpi_int.dt, &ddt );
 201         {
 202             const int* a_i[3] = {&count, &blocklength, &stride};
 203             ompi_datatype_t *type = &ompi_mpi_int.dt;
 204 
 205             ompi_datatype_set_args( ddt, 3, a_i, 0, NULL, 1, &type, MPI_COMBINER_VECTOR );
 206         }
 207         ompi_datatype_commit(&ddt);
 208 
 209         if( verbose ) {
 210             printf("send data %08x %x08x %08x \n", send_data[0], send_data[1], send_data[2]);
 211             printf("data "); dump_hex(&send_data, sizeof(int32_t) * 3); printf("\n");
 212         }
 213         (void)pack_unpack_datatype( send_data, ddt, 1, recv_data, check_vector, (void*)&ompi_mpi_int32_t.dt );
 214         if( verbose ) {
 215             printf("recv "); dump_hex(&recv_data, sizeof(int32_t) * 3); printf("\n");
 216             printf("recv data %08x %08x %08x \n", recv_data[0], recv_data[1], recv_data[2]);
 217         }
 218         ompi_datatype_destroy(&ddt);
 219         if( (send_data[0] != recv_data[0]) || (send_data[2] != recv_data[2]) ) {
 220             printf("Error during external32 pack/unack for vector types (MPI_INT32_T)\n");
 221             exit(-1);
 222         }
 223     }
 224 
 225     ompi_datatype_finalize();
 226 
 227     return 0;
 228 }

/* [<][>][^][v][top][bottom][index][help] */