root/test/datatype/ddt_lib.c

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

DEFINITIONS

This source file includes following definitions.
  1. cache_trash
  2. create_inversed_vector
  3. print_double_mat
  4. init_random_upper_matrix
  5. check_diag_matrix
  6. upper_matrix
  7. lower_matrix
  8. test_matrix_borders
  9. test_contiguous
  10. test_struct_char_double
  11. test_create_twice_two_doubles
  12. test_create_blacs_type
  13. test_create_blacs_type1
  14. test_create_blacs_type2
  15. test_struct
  16. create_struct_constant_gap_resized_ddt
  17. create_strange_dt
  18. create_contiguous_type
  19. create_vector_type

   1 /* -*- Mode: C; c-basic-offset:4 ; -*- */
   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-2014 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) 2009      Oak Ridge National Labs.  All rights reserved.
  15  * Copyright (c) 2018      Los Alamos National Security, LLC. All rights
  16  *                         reserved.
  17  * $COPYRIGHT$
  18  *
  19  * Additional copyrights may follow
  20  *
  21  * $HEADER$
  22  */
  23 
  24 #include "ompi_config.h"
  25 #include "ddt_lib.h"
  26 #include "ompi/constants.h"
  27 #include <time.h>
  28 #include <stdlib.h>
  29 #ifdef HAVE_SYS_TIME_H
  30 #include <sys/time.h>
  31 #endif
  32 #include <stdio.h>
  33 #include <string.h>
  34 
  35 #define TIMER_DATA_TYPE struct timeval
  36 #define GET_TIME(TV)   gettimeofday( &(TV), NULL )
  37 #define ELAPSED_TIME(TSTART, TEND)  (((TEND).tv_sec - (TSTART).tv_sec) * 1000000 + ((TEND).tv_usec - (TSTART).tv_usec))
  38 
  39 #define DUMP_DATA_AFTER_COMMIT 0x00000001
  40 #define CHECK_PACK_UNPACK      0x00000002
  41 #define VALIDATE_DATA          0x00000004
  42 uint32_t outputFlags = CHECK_PACK_UNPACK | VALIDATE_DATA;
  43 
  44 /**
  45  * Cache cleanup.
  46  */
  47 #define CACHE_SIZE (4*1024*1024)
  48 void cache_trash( void )
  49 {
  50     char* buffer;
  51 
  52     buffer = (char*)malloc( sizeof(char) * CACHE_SIZE );
  53     memset( buffer, 1, CACHE_SIZE );
  54     memset( buffer, 0xff, CACHE_SIZE );
  55     free( buffer );
  56 }
  57 
  58 /**
  59  * Data-type functions.
  60  */
  61 ompi_datatype_t* create_inversed_vector( const ompi_datatype_t* type, int length )
  62 {
  63    ompi_datatype_t* type1;
  64 
  65    ompi_datatype_create_vector( length, 1, 2, type, &type1 );
  66 
  67    ompi_datatype_commit( &type1 );
  68    return type1;
  69 }
  70 
  71 void print_double_mat( unsigned int N, double* mat )
  72 {
  73    unsigned int i, j;
  74    double* pMat;
  75 
  76    for( i = 0; i < N; i++ ) {
  77       printf( "(%4d) :", i * N * (int)sizeof(double) );
  78       pMat = mat + i * N;
  79       for( j = 0; j < N; j++ ) {
  80          printf( "%5.1f ", *pMat );
  81          pMat++;
  82       }
  83       printf( "\n" );
  84    }
  85 }
  86 
  87 int init_random_upper_matrix( unsigned int N, double* mat )
  88 {
  89     unsigned int i, j;
  90 
  91     srand( time(NULL) );
  92     for( i = 0; i < N; i++ ) {
  93         mat += i;
  94         for( j = i; j < N; j++ ) {
  95             *mat = (double)random();
  96             mat++;
  97         }
  98     }
  99     return OMPI_SUCCESS;
 100 }
 101 
 102 int check_diag_matrix( unsigned int N, double* mat1, double* mat2 )
 103 {
 104    unsigned int i, j;
 105 
 106    for( i = 0; i < N; i++ ) {
 107       mat1 += i;
 108       mat2 += i;
 109       for( j = i; j < N; j++ ) {
 110          if( *mat1 != *mat2 ) {
 111             printf( "error in position (%d, %d) expect %f and find %f\n",
 112                     i, j, *mat1, *mat2 );
 113             printf( "hex %lx != %lx\n", *(long*)mat1, *(long*)mat2 );
 114             return OMPI_ERROR;
 115          }
 116          mat1++; mat2++;
 117       }
 118    }
 119    return OMPI_SUCCESS;
 120 }
 121 
 122 ompi_datatype_t* upper_matrix( unsigned int mat_size )
 123 {
 124     int *disp, *blocklen;
 125     unsigned int i;
 126     ompi_datatype_t* upper;
 127 
 128     disp = (int*)malloc( sizeof(int) * mat_size );
 129     blocklen = (int*)malloc( sizeof(int) * mat_size );
 130 
 131     for( i = 0; i < mat_size; i++ ) {
 132         disp[i] = i * mat_size + i;
 133         blocklen[i] = mat_size - i;
 134     }
 135 
 136     ompi_datatype_create_indexed( mat_size, blocklen, disp, &ompi_mpi_double.dt,
 137                              &upper );
 138     ompi_datatype_commit( &upper );
 139     if( outputFlags & DUMP_DATA_AFTER_COMMIT ) {
 140         ompi_datatype_dump( upper );
 141     }
 142     free( disp );
 143     free( blocklen );
 144     return upper;
 145 }
 146 
 147 ompi_datatype_t* lower_matrix( unsigned int mat_size )
 148 {
 149     int *disp, *blocklen;
 150     unsigned int i;
 151     ompi_datatype_t* upper;
 152 
 153     disp = (int*)malloc( sizeof(int) * mat_size );
 154     blocklen = (int*)malloc( sizeof(int) * mat_size );
 155 
 156     for( i = 0; i < mat_size; i++ ) {
 157         disp[i] = i * mat_size;
 158         blocklen[i] = i;
 159     }
 160 
 161     ompi_datatype_create_indexed( mat_size, blocklen, disp, &ompi_mpi_double.dt,
 162                              &upper );
 163     free( disp );
 164     free( blocklen );
 165     return upper;
 166 }
 167 
 168 ompi_datatype_t* test_matrix_borders( unsigned int size, unsigned int width )
 169 {
 170    ompi_datatype_t *pdt, *pdt_line;
 171    int disp[2];
 172    int blocklen[2];
 173 
 174    disp[0] = 0;
 175    blocklen[0] = width;
 176    disp[1] = (size - width) * sizeof(double);
 177    blocklen[1] = width;
 178 
 179    ompi_datatype_create_indexed( 2, blocklen, disp, &ompi_mpi_double.dt,
 180                             &pdt_line );
 181    ompi_datatype_create_contiguous( size, pdt_line, &pdt );
 182    OBJ_RELEASE( pdt_line ); /*assert( pdt_line == NULL );*/
 183    return pdt;
 184 }
 185 
 186 ompi_datatype_t* test_contiguous( void )
 187 {
 188     ompi_datatype_t *pdt, *pdt1, *pdt2;
 189 
 190     printf( "test contiguous (alignment)\n" );
 191     ompi_datatype_create_contiguous(0, &ompi_mpi_datatype_null.dt, &pdt1);
 192     ompi_datatype_add( pdt1, &ompi_mpi_double.dt, 1, 0, -1 );
 193     if( outputFlags & DUMP_DATA_AFTER_COMMIT ) {
 194         ompi_datatype_dump( pdt1 );
 195     }
 196     ompi_datatype_add( pdt1, &ompi_mpi_char.dt, 1, 8, -1 );
 197     if( outputFlags & DUMP_DATA_AFTER_COMMIT ) {
 198         ompi_datatype_dump( pdt1 );
 199     }
 200     ompi_datatype_create_contiguous( 4, pdt1, &pdt2 );
 201     OBJ_RELEASE( pdt1 ); /*assert( pdt1 == NULL );*/
 202     if( outputFlags & DUMP_DATA_AFTER_COMMIT ) {
 203         ompi_datatype_dump( pdt2 );
 204     }
 205     ompi_datatype_create_contiguous( 2, pdt2, &pdt );
 206     OBJ_RELEASE( pdt2 ); /*assert( pdt2 == NULL );*/
 207     if( outputFlags & DUMP_DATA_AFTER_COMMIT ) {
 208         ompi_datatype_dump( pdt );
 209     }
 210     return pdt;
 211 }
 212 
 213 typedef struct __struct_char_double {
 214     char c;
 215     double d;
 216 } char_double_t;
 217 
 218 ompi_datatype_t* test_struct_char_double( void )
 219 {
 220     char_double_t data;
 221     int lengths[] = {1, 1};
 222     MPI_Aint displ[] = {0, 0};
 223     ompi_datatype_t *pdt;
 224     ompi_datatype_t* types[] = { &ompi_mpi_char.dt, &ompi_mpi_double.dt};
 225 
 226     displ[0] = (char*)&(data.c) - (char*)&(data);
 227     displ[1] = (char*)&(data.d) - (char*)&(data);
 228 
 229     ompi_datatype_create_struct( 2, lengths, displ, types, &pdt );
 230     ompi_datatype_commit( &pdt );
 231     if( outputFlags & DUMP_DATA_AFTER_COMMIT ) {
 232         ompi_datatype_dump( pdt );
 233     }
 234     return pdt;
 235 }
 236 
 237 ompi_datatype_t* test_create_twice_two_doubles( void )
 238 {
 239     ompi_datatype_t* pdt;
 240 
 241     ompi_datatype_create_vector( 2, 2, 5, &ompi_mpi_double.dt, &pdt );
 242     ompi_datatype_commit( &pdt );
 243     if( outputFlags & DUMP_DATA_AFTER_COMMIT ) {
 244         ompi_datatype_dump( pdt );
 245     }
 246     return pdt;
 247 }
 248 
 249 /*
 250   Datatype 0x832cf28 size 0 align 1 id 0 length 4 used 0
 251   true_lb 0 true_ub 0 (true_extent 0) lb 0 ub 0 (extent 0)
 252   nbElems 0 loops 0 flags 6 (committed contiguous )-cC--------[---][---]
 253   contain 13 disp 0x420 (1056) extent 4
 254   --C-----D*-[ C ][INT]        MPI_INT count 13 disp 0x478 (1144) extent 4
 255   --C-----D*-[ C ][INT]        MPI_INT count 13 disp 0x4d0 (1232) extent 4
 256   --C-----D*-[ C ][INT]        MPI_INT count 13 disp 0x528 (1320) extent 4
 257   --C-----D*-[ C ][INT]        MPI_INT count 13 disp 0x580 (1408) extent 4
 258   --C-----D*-[ C ][INT]        MPI_INT count 13 disp 0x5d8 (1496) extent 4
 259   --C-----D*-[ C ][INT]        MPI_INT count 13 disp 0x630 (1584) extent 4
 260   --C-----D*-[ C ][INT]        MPI_INT count 12 disp 0x68c (1676) extent 4
 261   --C-----D*-[ C ][INT]        MPI_INT count 11 disp 0x6e8 (1768) extent 4
 262   --C-----D*-[ C ][INT]        MPI_INT count 10 disp 0x744 (1860) extent 4
 263   --C-----D*-[ C ][INT]        MPI_INT count 9 disp 0x7a0 (1952) extent 4
 264   --C-----D*-[ C ][INT]        MPI_INT count 8 disp 0x7fc (2044) extent 4
 265   --C-----D*-[ C ][INT]        MPI_INT count 7 disp 0x858 (2136) extent 4
 266   --C-----D*-[ C ][INT]        MPI_INT count 6 disp 0x8b4 (2228) extent 4
 267   --C-----D*-[ C ][INT]        MPI_INT count 5 disp 0x910 (2320) extent 4
 268   --C-----D*-[ C ][INT]        MPI_INT count 4 disp 0x96c (2412) extent 4
 269   --C-----D*-[ C ][INT]        MPI_INT count 3 disp 0x9c8 (2504) extent 4
 270   --C-----D*-[ C ][INT]        MPI_INT count 2 disp 0xa24 (2596) extent 4
 271   --C-----D*-[ C ][INT]        MPI_INT count 1 disp 0xa80 (2688) extent 4
 272 */
 273 static int blacs_length[] = { 13, 13, 13, 13, 13, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
 274 static int blacs_indices[] = { 1144/4, 1232/4, 1320/4, 1408/4, 1496/4, 1584/4, 1676/4, 1768/4,
 275                                1860/4, 1952/4, 2044/4, 2136/4, 2228/4, 2320/4, 2412/4, 2504/4,
 276                                2596/4, 2688/4 };
 277 ompi_datatype_t* test_create_blacs_type( void )
 278 {
 279     ompi_datatype_t *pdt;
 280 
 281     ompi_datatype_create_indexed( 18, blacs_length, blacs_indices, &ompi_mpi_int.dt, &pdt );
 282     ompi_datatype_commit( &pdt );
 283     if( outputFlags & DUMP_DATA_AFTER_COMMIT ) {
 284         ompi_datatype_dump( pdt );
 285     }
 286     return pdt;
 287 }
 288 
 289 ompi_datatype_t* test_create_blacs_type1( const ompi_datatype_t* base_type )
 290 {
 291     ompi_datatype_t *pdt;
 292 
 293     ompi_datatype_create_vector( 7, 1, 3, base_type, &pdt );
 294     ompi_datatype_commit( &pdt );
 295     if( outputFlags & DUMP_DATA_AFTER_COMMIT ) {
 296         ompi_datatype_dump( pdt );
 297     }
 298     return pdt;
 299 }
 300 
 301 ompi_datatype_t* test_create_blacs_type2( const ompi_datatype_t* base_type )
 302 {
 303     ompi_datatype_t *pdt;
 304 
 305     ompi_datatype_create_vector( 7, 1, 2, base_type, &pdt );
 306     ompi_datatype_commit( &pdt );
 307     if( outputFlags & DUMP_DATA_AFTER_COMMIT ) {
 308         ompi_datatype_dump( pdt );
 309     }
 310     return pdt;
 311 }
 312 
 313 ompi_datatype_t* test_struct( void )
 314 {
 315     ompi_datatype_t* types[] = { &ompi_mpi_float.dt  /* ompi_datatype_basicDatatypes[DT_FLOAT] */,
 316                                  NULL,
 317                                  &ompi_mpi_char.dt  /* ompi_datatype_basicDatatypes[DT_CHAR] */ };
 318     int lengths[] = { 2, 1, 3 };
 319     MPI_Aint disp[] = { 0, 16, 26 };
 320     ompi_datatype_t* pdt, *pdt1;
 321 
 322     printf( "test struct\n" );
 323     ompi_datatype_create_contiguous(0, &ompi_mpi_datatype_null.dt, &pdt1);
 324     ompi_datatype_add( pdt1, &ompi_mpi_double.dt, 1, 0, -1 );
 325     ompi_datatype_add( pdt1, &ompi_mpi_char.dt, 1, 8, -1 );
 326     if( outputFlags & DUMP_DATA_AFTER_COMMIT ) {
 327         ompi_datatype_dump( pdt1 );
 328     }
 329 
 330     types[1] = pdt1;
 331 
 332     ompi_datatype_create_struct( 3, lengths, disp, types, &pdt );
 333     OBJ_RELEASE( pdt1 ); /*assert( pdt1 == NULL );*/
 334     if( outputFlags & DUMP_DATA_AFTER_COMMIT ) {
 335         ompi_datatype_dump( pdt );
 336     }
 337     return pdt;
 338 }
 339 
 340 /* Create a non-contiguous resized datatype */
 341 struct structure {
 342     double not_transfered;
 343     double transfered_1;
 344     double transfered_2;
 345 };
 346 
 347 ompi_datatype_t* create_struct_constant_gap_resized_ddt( ompi_datatype_t* type )
 348 {
 349     struct structure data[1];
 350     ompi_datatype_t *struct_type, *temp_type;
 351     ompi_datatype_t *types[2] = {type, type};
 352     int blocklens[2] = {1, 1};
 353     MPI_Aint disps[3];
 354 
 355     MPI_Get_address(&data[0].transfered_1, &disps[0]);
 356     MPI_Get_address(&data[0].transfered_2, &disps[1]);
 357     MPI_Get_address(&data[0], &disps[2]);
 358     disps[1] -= disps[2]; /*  8 */
 359     disps[0] -= disps[2]; /* 16 */
 360 
 361     ompi_datatype_create_struct(2, blocklens, disps, types, &temp_type);
 362     ompi_datatype_create_resized(temp_type, 0, sizeof(data[0]), &struct_type);
 363     ompi_datatype_commit(&struct_type);
 364     OBJ_RELEASE(temp_type); assert( temp_type == NULL );
 365     if( outputFlags & DUMP_DATA_AFTER_COMMIT ) {
 366         ompi_datatype_dump( struct_type );
 367     }
 368 
 369     return struct_type;
 370 }
 371 
 372 typedef struct {
 373     int i1;
 374     int gap;
 375     int i2;
 376 } sdata_intern;
 377 
 378 typedef struct {
 379     int counter;
 380     sdata_intern v[10];
 381     int last;
 382 } sstrange;
 383 
 384 #define SSTRANGE_CNT 10
 385 #define USE_RESIZED
 386 
 387 ompi_datatype_t* create_strange_dt( void )
 388 {
 389     sdata_intern v[2];
 390     MPI_Aint displ[3];
 391     ompi_datatype_t* types[3] = { &ompi_mpi_int.dt };
 392     sstrange t[2];
 393     int pBlock[3] = {1, 10, 1}, dispi[3];
 394     ompi_datatype_t *pdt, *pdt1, *pdt2, *pdtTemp;
 395 
 396     dispi[0] = (int)((char*)&(v[0].i1) - (char*)&(v[0]));  /* 0 */
 397     dispi[1] = (int)(((char*)(&(v[0].i2)) - (char*)&(v[0])) / sizeof(int));  /* 2 */
 398     ompi_datatype_create_indexed_block( 2, 1, dispi, &ompi_mpi_int.dt, &pdtTemp );
 399 #ifdef USE_RESIZED
 400     /* optional */
 401     displ[0] = 0;
 402     displ[1] = (char*)&(v[1]) - (char*)&(v[0]);
 403     ompi_datatype_create_resized( pdtTemp, displ[0], displ[1], &pdt1 );
 404     OBJ_RELEASE( pdtTemp ); assert( pdtTemp == NULL );
 405 #else
 406     pdt1 = pdtTemp;
 407 #endif  /* USE_RESIZED */
 408 
 409     types[1] = pdt1;
 410     types[2] = &ompi_mpi_int.dt;
 411     displ[0] = 0;
 412     displ[1] = (long)((char*)&(t[0].v[0]) - (char*)&(t[0]));
 413     displ[2] = (long)((char*)&(t[0].last) - (char*)&(t[0]));
 414     ompi_datatype_create_struct( 3, pBlock, displ, types, &pdtTemp );
 415 #ifdef USE_RESIZED
 416     /* optional */
 417     displ[1] = (char*)&(t[1]) - (char*)&(t[0]);
 418     ompi_datatype_create_resized( pdtTemp, displ[0], displ[1], &pdt2 );
 419     OBJ_RELEASE( pdtTemp ); assert( pdtTemp == NULL );
 420 #else
 421     pdt2 = pdtTemp;
 422 #endif  /* USE_RESIZED */
 423 
 424     ompi_datatype_create_contiguous( SSTRANGE_CNT, pdt2, &pdt );
 425 
 426     OBJ_RELEASE( pdt1 );
 427     OBJ_RELEASE( pdt2 );
 428     printf( "\nStrange datatype BEFORE COMMIT\n" );
 429     if( outputFlags & DUMP_DATA_AFTER_COMMIT ) {
 430         ompi_datatype_dump( pdt );
 431     }
 432     ompi_datatype_commit( &pdt );
 433     printf( "\nStrange datatype AFTER COMMIT\n" );
 434     if( outputFlags & DUMP_DATA_AFTER_COMMIT ) {
 435         ompi_datatype_dump( pdt );
 436     }
 437     return pdt;
 438 }
 439 
 440 ompi_datatype_t* create_contiguous_type( const ompi_datatype_t* data, int count )
 441 {
 442     ompi_datatype_t* contiguous;
 443 
 444     ompi_datatype_create_contiguous( count, data, &contiguous );
 445     ompi_datatype_commit( &contiguous );
 446     return contiguous;
 447 }
 448 
 449 ompi_datatype_t* create_vector_type( const ompi_datatype_t* data, int count, int length, int stride )
 450 {
 451     ompi_datatype_t* vector;
 452 
 453     ompi_datatype_create_vector( count, length, stride, data, &vector );
 454     ompi_datatype_commit( &vector );
 455     return vector;
 456 }
 457 

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