1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2004-2017 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2011-2013 Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2014-2015 Intel, Inc. All rights reserved.
* Copyright (c) 2018 Triad National Security, LLC. All rights
* reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "ompi_config.h"
#include <stdio.h>
#include <string.h>
#include "opal/datatype/opal_convertor.h"
#include "ompi/datatype/ompi_datatype.h"
#include "opal/util/output.h"
#include "opal/runtime/opal.h"
/**
* The purpose of this test is to simulate the multi-network packing and
* unpacking process. The pack operation will happens in-order while the
* unpack will be done randomly. Therefore, before each unpack the correct
* position in the user buffer has to be set.
*/
static int fragment_size = 113;
typedef struct {
size_t position;
size_t size;
void* buffer;
} ddt_segment_t;
static int
create_segments( ompi_datatype_t* datatype, int count,
size_t segment_length,
ddt_segment_t** segments, int* seg_count )
{
size_t data_size, total_length, position;
opal_convertor_t* convertor;
int i;
ddt_segment_t* segment;
ompi_datatype_type_size( datatype, &data_size );
data_size *= count;
*seg_count = data_size / segment_length;
if( ((*seg_count) * segment_length) != data_size )
*seg_count += 1;
allocate_segments:
*segments = (ddt_segment_t*)malloc( (*seg_count) * sizeof(ddt_segment_t) );
convertor = opal_convertor_create( opal_local_arch, 0 );
opal_convertor_prepare_for_send( convertor, &(datatype->super), count, NULL );
position = 0;
total_length = 0;
for( i = 0; i < (*seg_count); i++ ) {
segment = &((*segments)[i]);
segment->buffer = malloc(segment_length);
segment->position = position;
/* Find the end of the segment */
position += segment_length;
opal_convertor_set_position( convertor, &position );
segment->size = position - segment->position;
total_length += segment->size;
}
OBJ_RELEASE(convertor);
if( total_length != data_size ) {
for( i = 0; i < (*seg_count); i++ ) {
segment = &((*segments)[i]);
free(segment->buffer);
}
free( *segments );
(*seg_count) += 1;
goto allocate_segments;
}
return 0;
}
static int
shuffle_segments( ddt_segment_t* segments, int seg_count )
{
ddt_segment_t temporary;
int i;
for( i = 0; i < (seg_count/2); i += 2 ) {
temporary = segments[i];
segments[i] = segments[seg_count - i - 1];
segments[seg_count - i - 1] = temporary;
}
return 0;
}
static int
pack_segments( ompi_datatype_t* datatype, int count,
size_t segment_size,
ddt_segment_t* segments, int seg_count,
void* buffer )
{
size_t max_size, position;
opal_convertor_t* convertor;
struct iovec iov;
int i;
uint32_t iov_count;
convertor = opal_convertor_create( opal_local_arch, 0 );
opal_convertor_prepare_for_send( convertor, &(datatype->super), count, buffer );
for( i = 0; i < seg_count; i++ ) {
iov.iov_len = segments[i].size;
iov.iov_base = segments[i].buffer;
max_size = iov.iov_len;
position = segments[i].position;
opal_convertor_set_position( convertor, &position );
if( position != segments[i].position ) {
opal_output( 0, "Setting position failed (%lu != %lu)\n",
(unsigned long)segments[i].position, (unsigned long)position );
break;
}
iov_count = 1;
opal_convertor_pack( convertor, &iov, &iov_count, &max_size );
if( max_size != segments[i].size ) {
opal_output( 0, "Amount of packed data do not match (%lu != %lu)\n",
(unsigned long)max_size, (unsigned long)segments[i].size );
opal_output( 0, "Segment %d position %lu size %lu\n", i,
(unsigned long)segments[i].position, segments[i].size );
}
}
OBJ_RELEASE(convertor);
return i;
}
static int
unpack_segments( ompi_datatype_t* datatype, int count,
size_t segment_size,
ddt_segment_t* segments, int seg_count,
void* buffer )
{
opal_convertor_t* convertor;
size_t max_size, position;
int i;
uint32_t iov_count;
struct iovec iov;
convertor = opal_convertor_create( opal_local_arch, 0 );
opal_convertor_prepare_for_recv( convertor, &(datatype->super), count, buffer );
for( i = 0; i < seg_count; i++ ) {
iov.iov_len = segments[i].size;
iov.iov_base = segments[i].buffer;
max_size = iov.iov_len;
position = segments[i].position;
opal_convertor_set_position( convertor, &position );
if( position != segments[i].position ) {
opal_output( 0, "Setting position failed (%lu != %lu)\n",
(unsigned long)segments[i].position, (unsigned long)position );
break;
}
iov_count = 1;
opal_convertor_unpack( convertor, &iov, &iov_count, &max_size );
if( max_size != segments[i].size ) {
opal_output( 0, "Amount of unpacked data do not match (%lu != %lu)\n",
(unsigned long)max_size, (unsigned long)segments[i].size );
opal_output( 0, "Segment %d position %lu size %lu\n", i,
(unsigned long)segments[i].position, segments[i].size );
}
}
OBJ_RELEASE(convertor);
return 0;
}
#if (OPAL_ENABLE_DEBUG == 1) && (OPAL_C_HAVE_VISIBILITY == 0)
extern bool opal_ddt_unpack_debug;
extern bool opal_ddt_pack_debug;
extern bool opal_ddt_position_debug ;
#endif /* OPAL_ENABLE_DEBUG */
int main( int argc, char* argv[] )
{
ddt_segment_t* segments;
int *send_buffer, *recv_buffer;
int i, seg_count, errors;
int show_only_first_error = 1;
ompi_datatype_t* datatype = MPI_DATATYPE_NULL;
#define NELT (300)
send_buffer = malloc(NELT*sizeof(int));
recv_buffer = malloc(NELT*sizeof(int));
for (i = 0; i < NELT; ++i) {
send_buffer[i] = i;
recv_buffer[i] = 0xdeadbeef;
}
opal_init(NULL, NULL);
ompi_datatype_init();
ompi_datatype_create_vector(NELT/2, 1, 2, MPI_INT, &datatype);
ompi_datatype_commit(&datatype);
#if (OPAL_ENABLE_DEBUG == 1) && (OPAL_C_HAVE_VISIBILITY == 0)
opal_ddt_unpack_debug = false;
opal_ddt_pack_debug = false;
opal_ddt_position_debug = false;
#endif /* OPAL_ENABLE_DEBUG */
create_segments( datatype, 1, fragment_size,
&segments, &seg_count );
/* shuffle the segments */
shuffle_segments( segments, seg_count );
/* pack the data */
pack_segments( datatype, 1, fragment_size, segments, seg_count,
send_buffer );
/* unpack the data back in the user space (recv buffer) */
unpack_segments( datatype, 1, fragment_size, segments, seg_count,
recv_buffer );
/* And now check the data */
for( errors = i = 0; i < NELT; i++ ) {
int expected = ((i % 2) ? (int)0xdeadbeef : i);
if (recv_buffer[i] != expected) {
if( (show_only_first_error && (0 == errors)) ||
!show_only_first_error ) {
printf("error at index %4d: 0x%08x != 0x%08x\n", i, recv_buffer[i], expected);
}
errors++;
}
}
printf( "Found %d errors\n", errors );
free(send_buffer); free(recv_buffer);
for( i = 0; i < seg_count; i++ ) {
free( segments[i].buffer );
}
free(segments);
opal_finalize_util ();
return (0 == errors ? 0 : -1);
}
|