root/test/datatype/ddt_pack.c

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

DEFINITIONS

This source file includes following definitions.
  1. get_extents
  2. main

   1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
   2 /*
   3  * Copyright (c) 2004-2006 The Trustees of Indiana University and Indiana
   4  *                         University Research and Technology
   5  *                         Corporation.  All rights reserved.
   6  * Copyright (c) 2004-2006 The University of Tennessee and The University
   7  *                         of Tennessee Research Foundation.  All rights
   8  *                         reserved.
   9  * Copyright (c) 2004-2006 High Performance Computing Center Stuttgart,
  10  *                         University of Stuttgart.  All rights reserved.
  11  * Copyright (c) 2004-2006 The Regents of the University of California.
  12  *                         All rights reserved.
  13  * Copyright (c) 2006      Sun Microsystems Inc. All rights reserved.
  14  * Copyright (c) 2015-2017 Research Organization for Information Science
  15  *                         and Technology (RIST). All rights reserved.
  16  * Copyright (c) 2018      Triad National Security, LLC. All rights
  17  *                         reserved.
  18  * $COPYRIGHT$
  19  *
  20  * Additional copyrights may follow
  21  *
  22  * $HEADER$
  23  */
  24 
  25 #include "ompi_config.h"
  26 #include "ompi/datatype/ompi_datatype.h"
  27 #include "opal/datatype/opal_convertor.h"
  28 #include "ompi/proc/proc.h"
  29 
  30 #include <stdlib.h>
  31 #include <string.h>
  32 
  33 #include <poll.h>
  34 
  35 static int get_extents(ompi_datatype_t * type, ptrdiff_t *lb, ptrdiff_t *extent, ptrdiff_t *true_lb, ptrdiff_t *true_extent) {
  36     int ret;
  37 
  38     ret = ompi_datatype_get_extent(type, lb, extent);
  39     if (MPI_SUCCESS != ret) return ret;
  40     ret = ompi_datatype_get_true_extent(type, true_lb, true_extent);
  41     if (MPI_SUCCESS != ret) return ret;
  42 
  43     return 0;
  44 }
  45 
  46 int
  47 main(int argc, char* argv[])
  48 {
  49     size_t packed_ddt_len;
  50     const void *packed_ddt;
  51     void *payload, *ptr;
  52     struct ompi_datatype_t *unpacked_dt;
  53     int ret = 0;
  54     int         blen[4];
  55     ptrdiff_t    disp[4];
  56     ompi_datatype_t *newType, *types[4], *struct_type, *vec_type, *dup_type;
  57     ptrdiff_t    old_lb, old_extent, old_true_lb, old_true_extent;
  58     ptrdiff_t    lb, extent, true_lb, true_extent;
  59 
  60     /* make ompi_proc_local () work ... */
  61     struct ompi_proc_t dummy_proc;
  62     ompi_proc_local_proc = &dummy_proc;
  63 
  64 
  65     int _dbg = 0;
  66     while (_dbg) poll(NULL, 0, 1);
  67 
  68     opal_init_util (NULL, NULL);
  69     ompi_datatype_init();
  70 
  71     /**
  72      *
  73      *                 TEST 1
  74      *
  75      */
  76 
  77     /* Basic test... */
  78     printf("---> Basic test with MPI_INT\n");
  79 
  80     packed_ddt_len = ompi_datatype_pack_description_length(&ompi_mpi_int.dt);
  81     ptr = payload = malloc(packed_ddt_len);
  82     ret = ompi_datatype_get_pack_description(&ompi_mpi_int.dt, &packed_ddt);
  83     if (ret != 0) goto cleanup;
  84 
  85     memcpy(payload, packed_ddt, packed_ddt_len);
  86     unpacked_dt = ompi_datatype_create_from_packed_description(&payload,
  87                                                                ompi_proc_local());
  88     free(ptr);
  89     if (unpacked_dt == &ompi_mpi_int32_t.dt) {
  90         printf("\tPASSED\n");
  91     } else {
  92         printf("\tFAILED: datatypes don't match\n");
  93         ret = 1;
  94         goto cleanup;
  95     }
  96 
  97     /**
  98      *
  99      *                 TEST 2
 100      *
 101      */
 102 
 103     printf("---> Simple test using a struct and few predefined datatype (4 * MPI_INT).\n");
 104     blen[0] = 1;         blen[1] = 2;        blen[2] = 3;        blen[3] = 4;
 105     disp[0] = 0;         disp[1] = 4;        disp[2] = 8;        disp[3] = 12;
 106     types[0] = &ompi_mpi_int.dt; types[1] = &ompi_mpi_int.dt; types[2] = &ompi_mpi_int.dt; types[3] = &ompi_mpi_int.dt;
 107     ret = ompi_datatype_create_struct( 4, blen, disp, types, &struct_type );
 108     if (ret != 0) goto cleanup;
 109 
 110     {
 111         int count = 4;
 112         const int* a_i[2] = {&count, blen};
 113         ret = ompi_datatype_set_args( struct_type, count + 1, a_i, count, disp,
 114                                       count, types, MPI_COMBINER_STRUCT);
 115         if (ret != 0) goto cleanup;
 116     }
 117 
 118     ret = ompi_datatype_commit(&struct_type);
 119     if (ret != 0) goto cleanup;
 120 
 121     ret = get_extents(struct_type, &old_lb, &old_extent, &old_true_lb, &old_true_extent);
 122     if (ret != 0) goto cleanup;
 123 
 124     packed_ddt_len = ompi_datatype_pack_description_length(struct_type);
 125     ptr = payload = malloc(packed_ddt_len);
 126     ret = ompi_datatype_get_pack_description(struct_type, &packed_ddt);
 127     if (ret != 0) goto cleanup;
 128 
 129     memcpy(payload, packed_ddt, packed_ddt_len);
 130 
 131     unpacked_dt = ompi_datatype_create_from_packed_description(&payload, ompi_proc_local());
 132     free(ptr);
 133     if (unpacked_dt == NULL) {
 134         printf("\tFAILED: could not unpack datatype\n");
 135         ret = 1;
 136         goto cleanup;
 137     } else {
 138         ret = get_extents(unpacked_dt, &lb, &extent, &true_lb, &true_extent);
 139         if (ret != 0) goto cleanup;
 140 
 141         if (old_lb != lb || old_extent != extent ||
 142             old_true_lb != true_lb || old_true_extent != extent) {
 143             printf("\tFAILED: datatypes don't match\n");
 144             ret = 1;
 145             goto cleanup;
 146         }
 147         printf("\tPASSED\n");
 148     }
 149     ret = ompi_datatype_destroy(&struct_type);
 150     if (ret != 0) goto cleanup;
 151 
 152     ret = ompi_datatype_destroy(&unpacked_dt);
 153     if (ret != 0) goto cleanup;
 154 
 155     /**
 156      *
 157      *                 TEST 3
 158      *
 159      */
 160 
 161     printf("---> Less Basic test with MPI_Type_vector\n");
 162 
 163     ret = ompi_datatype_create_vector(2, 1, 1, &ompi_mpi_int.dt, &vec_type);
 164     if (ret != 0) goto cleanup;
 165 
 166     {
 167         int count = 2;
 168         int blocklength = 1;
 169         int stride = 1;
 170         const int* a_i[3] = {&count, &blocklength, &stride};
 171         ompi_datatype_t * type = &ompi_mpi_int.dt;
 172         ret = ompi_datatype_set_args(vec_type, 3, a_i, 0, NULL, 1, &type, MPI_COMBINER_VECTOR );
 173         if (ret != 0) goto cleanup;
 174     }
 175 
 176     ret = ompi_datatype_commit(&vec_type);
 177     if (ret != 0) goto cleanup;
 178 
 179     ret = get_extents(vec_type, &old_lb, &old_extent, &old_true_lb, &old_true_extent);
 180     if (ret != 0) goto cleanup;
 181 
 182     packed_ddt_len = ompi_datatype_pack_description_length(vec_type);
 183     ptr = payload = malloc(packed_ddt_len);
 184     ret = ompi_datatype_get_pack_description(vec_type, &packed_ddt);
 185     if (ret != 0) goto cleanup;
 186 
 187     memcpy(payload, packed_ddt, packed_ddt_len);
 188 
 189     unpacked_dt = ompi_datatype_create_from_packed_description(&payload,
 190                                                           ompi_proc_local());
 191     free(ptr);
 192     if (unpacked_dt == NULL) {
 193         printf("\tFAILED: could not unpack datatype\n");
 194         ret = 1;
 195         goto cleanup;
 196     } else {
 197         ret = get_extents(unpacked_dt, &lb, &extent, &true_lb, &true_extent);
 198         if (ret != 0) goto cleanup;
 199 
 200         if (old_lb != lb || old_extent != extent ||
 201             old_true_lb != true_lb || old_true_extent != extent) {
 202             printf("\tFAILED: datatypes don't match\n");
 203             ret = 1;
 204             goto cleanup;
 205         }
 206         printf("\tPASSED\n");
 207     }
 208     ret = ompi_datatype_destroy(&vec_type);
 209     if (ret != 0) goto cleanup;
 210 
 211     ret = ompi_datatype_destroy(&unpacked_dt);
 212     if (ret != 0) goto cleanup;
 213 
 214     /**
 215      *
 216      *                 TEST 4
 217      *
 218      */
 219 
 220     printf("---> Test with MPI_Type_create_indexed_block\n");
 221 
 222     blen[0] = 0;
 223     blen[1] = 20*sizeof(double);
 224 
 225     ret = ompi_datatype_create_indexed_block(2, 10, blen, &ompi_mpi_double.dt, &newType);
 226     if (ret != 0) goto cleanup;
 227 
 228     {
 229         int count = 2;
 230         int blocklength = 10;
 231         const int* a_i[3] = {&count, &blocklength, blen};
 232         ompi_datatype_t * oldtype = &ompi_mpi_double.dt;
 233         ompi_datatype_set_args( newType, 2 + count, a_i, 0, NULL, 1, &oldtype,
 234                                 MPI_COMBINER_INDEXED_BLOCK );
 235         if (ret != 0) goto cleanup;
 236     }
 237 
 238     ret = ompi_datatype_commit(&newType);
 239     if (ret != 0) goto cleanup;
 240 
 241     ret = get_extents(newType, &old_lb, &old_extent, &old_true_lb, &old_true_extent);
 242     if (ret != 0) goto cleanup;
 243 
 244     packed_ddt_len = ompi_datatype_pack_description_length(newType);
 245 
 246     ptr = payload = malloc(packed_ddt_len);
 247     ret = ompi_datatype_get_pack_description(newType, &packed_ddt);
 248     if (ret != 0) goto cleanup;
 249 
 250     memcpy(payload, packed_ddt, packed_ddt_len);
 251     unpacked_dt = ompi_datatype_create_from_packed_description(&payload,
 252                                                                ompi_proc_local());
 253     free(ptr);
 254     if (unpacked_dt == NULL) {
 255         printf("\tFAILED: could not unpack datatype\n");
 256         ret = 1;
 257         goto cleanup;
 258     } else {
 259         ret = get_extents(unpacked_dt, &lb, &extent, &true_lb, &true_extent);
 260         if (ret != 0) goto cleanup;
 261 
 262         if (old_lb != lb || old_extent != extent ||
 263             old_true_lb != true_lb || old_true_extent != extent) {
 264             printf("\tFAILED: datatypes don't match\n");
 265             ret = 1;
 266             goto cleanup;
 267         }
 268         printf("\tPASSED\n");
 269     }
 270     ret = ompi_datatype_destroy(&newType);
 271     if (ret != 0) goto cleanup;
 272 
 273     ret = ompi_datatype_destroy(&unpacked_dt);
 274     if (ret != 0) goto cleanup;
 275 
 276     /**
 277      *
 278      *                 TEST 5
 279      *
 280      */
 281 
 282     printf("---> Advanced test with hindexed\n");
 283 
 284     blen[0] = 10;
 285     blen[1] = 10;
 286     disp[0] = 0;
 287     disp[1] = 20*sizeof(double);
 288 
 289     ret = ompi_datatype_create_hindexed(2, blen, disp, &ompi_mpi_double.dt, &newType);
 290     if (ret != 0) goto cleanup;
 291 
 292     {
 293         int count = 2;
 294         const int* a_i[2] = {&count, blen};
 295         ompi_datatype_t * oldtype = &ompi_mpi_double.dt;
 296         ret = ompi_datatype_set_args( newType, count + 1, a_i, count, disp,
 297                                       1, &oldtype, MPI_COMBINER_HINDEXED );
 298         if (ret != 0) goto cleanup;
 299     }
 300 
 301     ret = ompi_datatype_commit(&newType);
 302     if (ret != 0) goto cleanup;
 303 
 304     ret = get_extents(newType, &old_lb, &old_extent, &old_true_lb, &old_true_extent);
 305     if (ret != 0) goto cleanup;
 306 
 307     packed_ddt_len = ompi_datatype_pack_description_length(newType);
 308     ptr = payload = malloc(packed_ddt_len);
 309     ret = ompi_datatype_get_pack_description(newType, &packed_ddt);
 310     if (ret != 0) goto cleanup;
 311     memcpy(payload, packed_ddt, packed_ddt_len);
 312     unpacked_dt = ompi_datatype_create_from_packed_description(&payload,
 313                                                           ompi_proc_local());
 314     free(ptr);
 315     if (unpacked_dt == NULL) {
 316         printf("\tFAILED: could not unpack datatype\n");
 317         ret = 1;
 318         goto cleanup;
 319     } else {
 320         ret = get_extents(unpacked_dt, &lb, &extent, &true_lb, &true_extent);
 321         if (ret != 0) goto cleanup;
 322 
 323         if (old_lb != lb || old_extent != extent ||
 324             old_true_lb != true_lb || old_true_extent != extent) {
 325             printf("\tFAILED: datatypes don't match\n");
 326             ret = 1;
 327             goto cleanup;
 328         }
 329         printf("\tPASSED\n");
 330     }
 331     ret = ompi_datatype_destroy(&newType);
 332     if (ret != 0) goto cleanup;
 333 
 334     newType = unpacked_dt;  /* save it for later */
 335 
 336     /**
 337      *
 338      *                 TEST 6
 339      *
 340      */
 341 
 342     printf("---> Even more advanced test using the previous type and struct\n");
 343     blen[0] = 11;
 344     blen[1] = 2;
 345     disp[0] = 0;
 346     disp[1] = 64;
 347     types[0] = &ompi_mpi_int.dt;
 348     types[1] = newType;
 349     ret = ompi_datatype_create_struct( 2, blen, disp, types, &struct_type );
 350     if (ret != 0) goto cleanup;
 351 
 352     {
 353         int count = 2;
 354         const int* a_i[2] = {&count, blen};
 355         ret = ompi_datatype_set_args( struct_type, count + 1, a_i, count, disp,
 356                                       count, types, MPI_COMBINER_STRUCT );
 357         if (ret != 0) goto cleanup;
 358     }
 359 
 360     ret = ompi_datatype_commit(&struct_type);
 361     if (ret != 0) goto cleanup;
 362 
 363     ret = ompi_datatype_destroy(&newType);
 364     if (ret != 0) goto cleanup;
 365 
 366     ret = get_extents(struct_type, &old_lb, &old_extent, &old_true_lb, &old_true_extent);
 367     if (ret != 0) goto cleanup;
 368 
 369     packed_ddt_len = ompi_datatype_pack_description_length(struct_type);
 370     ptr = payload = malloc(packed_ddt_len);
 371     ret = ompi_datatype_get_pack_description(struct_type, &packed_ddt);
 372     if (ret != 0) goto cleanup;
 373     memcpy(payload, packed_ddt, packed_ddt_len);
 374 
 375     unpacked_dt = ompi_datatype_create_from_packed_description(&payload,
 376                                                           ompi_proc_local());
 377     free(ptr);
 378     if (unpacked_dt == NULL) {
 379         printf("\tFAILED: could not unpack datatype\n");
 380         ret = 1;
 381         goto cleanup;
 382     } else {
 383         ret = get_extents(unpacked_dt, &lb, &extent, &true_lb, &true_extent);
 384         if (ret != 0) goto cleanup;
 385 
 386         if (old_lb != lb || old_extent != extent ||
 387             old_true_lb != true_lb || old_true_extent != extent) {
 388             printf("\tFAILED: datatypes don't match\n");
 389             ret = 1;
 390             goto cleanup;
 391         }
 392         printf("\tPASSED\n");
 393     }
 394     ret = ompi_datatype_destroy(&struct_type);
 395     if (ret != 0) goto cleanup;
 396 
 397     ret = ompi_datatype_destroy(&unpacked_dt);
 398     if (ret != 0) goto cleanup;
 399 
 400     /**
 401      *
 402      *                 TEST 7
 403      *
 404      */
 405     printf("---> Basic test with dup'ed MPI_INT\n");
 406 
 407     ret = get_extents(&ompi_mpi_int.dt, &old_lb, &old_extent, &old_true_lb, &old_true_extent);
 408     if (ret != 0) goto cleanup;
 409     ret = ompi_datatype_duplicate(&ompi_mpi_int.dt, &dup_type);
 410     if (ret != 0) goto cleanup;
 411     ompi_datatype_t * type = &ompi_mpi_int.dt;
 412     ret = ompi_datatype_set_args(dup_type, 0, NULL, 0, NULL, 1, &type, MPI_COMBINER_DUP);
 413     if (ret != 0) goto cleanup;
 414     packed_ddt_len = ompi_datatype_pack_description_length(dup_type);
 415     ptr = payload = malloc(packed_ddt_len);
 416     ret = ompi_datatype_get_pack_description(dup_type, &packed_ddt);
 417     if (ret != 0) goto cleanup;
 418 
 419     memcpy(payload, packed_ddt, packed_ddt_len);
 420     unpacked_dt = ompi_datatype_create_from_packed_description(&payload,
 421                                                                ompi_proc_local());
 422     free(ptr);
 423     if (unpacked_dt == NULL) {
 424         printf("\tFAILED: could not unpack datatype\n");
 425         ret = 1;
 426         goto cleanup;
 427     } else {
 428         ret = get_extents(unpacked_dt, &lb, &extent, &true_lb, &true_extent);
 429         if (ret != 0) goto cleanup;
 430 
 431         if (old_lb != lb || old_extent != extent ||
 432             old_true_lb != true_lb || old_true_extent != extent) {
 433             printf("\tFAILED: datatypes don't match\n");
 434             ret = 1;
 435             goto cleanup;
 436         }
 437         printf("\tPASSED\n");
 438     }
 439     if (unpacked_dt == &ompi_mpi_int32_t.dt) {
 440         printf("\tPASSED\n");
 441     } else {
 442         printf("\tFAILED: datatypes don't match\n");
 443         ret = 1;
 444         goto cleanup;
 445     }
 446     ompi_datatype_destroy(&dup_type);
 447 
 448  cleanup:
 449     ompi_datatype_finalize();
 450     opal_finalize_util ();
 451 
 452     return ret;
 453 }

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