Skip to content

Commit 4211925

Browse files
committed
Fix incorrect behavior with length == 0
Fixes #6575. Signed-off-by: George Bosilca <[email protected]>
1 parent d141bf7 commit 4211925

5 files changed

+68
-87
lines changed

ompi/datatype/ompi_datatype_create_contiguous.c

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright (c) 2004-2006 The Trustees of Indiana University and Indiana
44
* University Research and Technology
55
* Corporation. All rights reserved.
6-
* Copyright (c) 2004-2013 The University of Tennessee and The University
6+
* Copyright (c) 2004-2019 The University of Tennessee and The University
77
* of Tennessee Research Foundation. All rights
88
* reserved.
99
* Copyright (c) 2004-2006 High Performance Computing Center Stuttgart,
@@ -29,13 +29,12 @@ int32_t ompi_datatype_create_contiguous( int count, const ompi_datatype_t* oldTy
2929
{
3030
ompi_datatype_t* pdt;
3131

32-
if( 0 == count ) {
33-
pdt = ompi_datatype_create( 0 );
34-
ompi_datatype_add( pdt, &ompi_mpi_datatype_null.dt, 0, 0, 0 );
35-
} else {
36-
pdt = ompi_datatype_create( oldType->super.desc.used + 2 );
37-
opal_datatype_add( &(pdt->super), &(oldType->super), count, 0, (oldType->super.ub - oldType->super.lb) );
32+
if( (0 == count) || (0 == oldType->super.size) ) {
33+
return ompi_datatype_duplicate( &ompi_mpi_datatype_null.dt, newType);
3834
}
35+
36+
pdt = ompi_datatype_create( oldType->super.desc.used + 2 );
37+
opal_datatype_add( &(pdt->super), &(oldType->super), count, 0, (oldType->super.ub - oldType->super.lb) );
3938
*newType = pdt;
4039
return OMPI_SUCCESS;
4140
}

ompi/datatype/ompi_datatype_create_darray.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,7 @@ int32_t ompi_datatype_create_darray(int size,
192192
if (ndims < 1) {
193193
/* Don't just return MPI_DATATYPE_NULL as that can't be
194194
MPI_TYPE_FREE()ed, and that seems bad */
195-
*newtype = ompi_datatype_create(0);
196-
ompi_datatype_add(*newtype, &ompi_mpi_datatype_null.dt, 0, 0, 0);
197-
return MPI_SUCCESS;
195+
return ompi_datatype_duplicate( &ompi_mpi_datatype_null.dt, newtype);
198196
}
199197

200198
rc = ompi_datatype_type_extent(oldtype, &orig_extent);

ompi/datatype/ompi_datatype_create_indexed.c

+37-42
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright (c) 2004-2006 The Trustees of Indiana University and Indiana
44
* University Research and Technology
55
* Corporation. All rights reserved.
6-
* Copyright (c) 2004-2013 The University of Tennessee and The University
6+
* Copyright (c) 2004-2019 The University of Tennessee and The University
77
* of Tennessee Research Foundation. All rights
88
* reserved.
99
* Copyright (c) 2004-2006 High Performance Computing Center Stuttgart,
@@ -34,24 +34,28 @@
3434
int32_t ompi_datatype_create_indexed( int count, const int* pBlockLength, const int* pDisp,
3535
const ompi_datatype_t* oldType, ompi_datatype_t** newType )
3636
{
37-
ompi_datatype_t* pdt;
38-
int i;
3937
ptrdiff_t extent, disp, endat;
38+
ompi_datatype_t* pdt;
4039
size_t dLength;
40+
int i;
4141

42-
if( 0 == count ) {
42+
/* ignore all cases that lead to an empty type */
43+
ompi_datatype_type_size(oldType, &dLength);
44+
for( i = 0; (i < count) && (0 == pBlockLength[i]); i++ ); /* find first non zero */
45+
if( (i == count) || (0 == dLength) ) {
4346
return ompi_datatype_duplicate( &ompi_mpi_datatype_null.dt, newType);
4447
}
4548

46-
disp = pDisp[0];
47-
dLength = pBlockLength[0];
49+
disp = pDisp[i];
50+
dLength = pBlockLength[i];
4851
endat = disp + dLength;
4952
ompi_datatype_type_extent( oldType, &extent );
5053

51-
pdt = ompi_datatype_create( count * (2 + oldType->super.desc.used) );
52-
for( i = 1; i < count; i++ ) {
53-
if( endat == pDisp[i] ) {
54-
/* contiguous with the previsious */
54+
pdt = ompi_datatype_create( (count - i) * (2 + oldType->super.desc.used) );
55+
for( i += 1; i < count; i++ ) {
56+
if( 0 == pBlockLength[i] ) /* ignore empty length */
57+
continue;
58+
if( endat == pDisp[i] ) { /* contiguous with the previsious */
5559
dLength += pBlockLength[i];
5660
endat += pBlockLength[i];
5761
} else {
@@ -71,26 +75,28 @@ int32_t ompi_datatype_create_indexed( int count, const int* pBlockLength, const
7175
int32_t ompi_datatype_create_hindexed( int count, const int* pBlockLength, const ptrdiff_t* pDisp,
7276
const ompi_datatype_t* oldType, ompi_datatype_t** newType )
7377
{
74-
ompi_datatype_t* pdt;
75-
int i;
7678
ptrdiff_t extent, disp, endat;
79+
ompi_datatype_t* pdt;
7780
size_t dLength;
81+
int i;
7882

79-
if( 0 == count ) {
80-
*newType = ompi_datatype_create( 0 );
81-
ompi_datatype_add( *newType, &ompi_mpi_datatype_null.dt, 0, 0, 0);
82-
return OMPI_SUCCESS;
83+
/* ignore all cases that lead to an empty type */
84+
ompi_datatype_type_size(oldType, &dLength);
85+
for( i = 0; (i < count) && (0 == pBlockLength[i]); i++ ); /* find first non zero */
86+
if( (i == count) || (0 == dLength) ) {
87+
return ompi_datatype_duplicate( &ompi_mpi_datatype_null.dt, newType);
8388
}
8489

85-
ompi_datatype_type_extent( oldType, &extent );
86-
pdt = ompi_datatype_create( count * (2 + oldType->super.desc.used) );
87-
disp = pDisp[0];
88-
dLength = pBlockLength[0];
90+
disp = pDisp[i];
91+
dLength = pBlockLength[i];
8992
endat = disp + dLength * extent;
93+
ompi_datatype_type_extent( oldType, &extent );
9094

91-
for( i = 1; i < count; i++ ) {
92-
if( endat == pDisp[i] ) {
93-
/* contiguous with the previsious */
95+
pdt = ompi_datatype_create( (count - i) * (2 + oldType->super.desc.used) );
96+
for( i += 1; i < count; i++ ) {
97+
if( 0 == pBlockLength[i] ) /* ignore empty length */
98+
continue;
99+
if( endat == pDisp[i] ) { /* contiguous with the previsious */
94100
dLength += pBlockLength[i];
95101
endat += pBlockLength[i] * extent;
96102
} else {
@@ -110,21 +116,15 @@ int32_t ompi_datatype_create_hindexed( int count, const int* pBlockLength, const
110116
int32_t ompi_datatype_create_indexed_block( int count, int bLength, const int* pDisp,
111117
const ompi_datatype_t* oldType, ompi_datatype_t** newType )
112118
{
113-
ompi_datatype_t* pdt;
114-
int i;
115119
ptrdiff_t extent, disp, endat;
120+
ompi_datatype_t* pdt;
116121
size_t dLength;
122+
int i;
117123

118-
ompi_datatype_type_extent( oldType, &extent );
119124
if( (count == 0) || (bLength == 0) ) {
120-
if( 0 == count ) {
121-
return ompi_datatype_duplicate(&ompi_mpi_datatype_null.dt, newType);
122-
} else {
123-
*newType = ompi_datatype_create(1);
124-
ompi_datatype_add( *newType, oldType, 0, pDisp[0] * extent, extent );
125-
return OMPI_SUCCESS;
126-
}
125+
return ompi_datatype_duplicate(&ompi_mpi_datatype_null.dt, newType);
127126
}
127+
ompi_datatype_type_extent( oldType, &extent );
128128
pdt = ompi_datatype_create( count * (2 + oldType->super.desc.used) );
129129
disp = pDisp[0];
130130
dLength = bLength;
@@ -150,20 +150,15 @@ int32_t ompi_datatype_create_indexed_block( int count, int bLength, const int* p
150150
int32_t ompi_datatype_create_hindexed_block( int count, int bLength, const ptrdiff_t* pDisp,
151151
const ompi_datatype_t* oldType, ompi_datatype_t** newType )
152152
{
153-
ompi_datatype_t* pdt;
154-
int i;
155153
ptrdiff_t extent, disp, endat;
154+
ompi_datatype_t* pdt;
156155
size_t dLength;
156+
int i;
157157

158-
ompi_datatype_type_extent( oldType, &extent );
159158
if( (count == 0) || (bLength == 0) ) {
160-
*newType = ompi_datatype_create(1);
161-
if( 0 == count )
162-
ompi_datatype_add( *newType, &ompi_mpi_datatype_null.dt, 0, 0, 0 );
163-
else
164-
ompi_datatype_add( *newType, oldType, 0, pDisp[0] * extent, extent );
165-
return OMPI_SUCCESS;
159+
return ompi_datatype_duplicate(&ompi_mpi_datatype_null.dt, newType);
166160
}
161+
ompi_datatype_type_extent( oldType, &extent );
167162
pdt = ompi_datatype_create( count * (2 + oldType->super.desc.used) );
168163
disp = pDisp[0];
169164
dLength = bLength;

ompi/datatype/ompi_datatype_create_struct.c

+19-19
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright (c) 2004-2006 The Trustees of Indiana University and Indiana
44
* University Research and Technology
55
* Corporation. All rights reserved.
6-
* Copyright (c) 2004-2013 The University of Tennessee and The University
6+
* Copyright (c) 2004-2019 The University of Tennessee and The University
77
* of Tennessee Research Foundation. All rights
88
* reserved.
99
* Copyright (c) 2004-2006 High Performance Computing Center Stuttgart,
@@ -31,27 +31,27 @@
3131
int32_t ompi_datatype_create_struct( int count, const int* pBlockLength, const ptrdiff_t* pDisp,
3232
ompi_datatype_t* const * pTypes, ompi_datatype_t** newType )
3333
{
34-
int i;
3534
ptrdiff_t disp = 0, endto, lastExtent, lastDisp;
36-
int lastBlock;
3735
ompi_datatype_t *pdt, *lastType;
36+
int lastBlock;
37+
int i, start_from;
3838

39-
if( 0 == count ) {
40-
*newType = ompi_datatype_create( 0 );
41-
ompi_datatype_add( *newType, &ompi_mpi_datatype_null.dt, 0, 0, 0);
42-
return OMPI_SUCCESS;
39+
/* Find first non-zero length element */
40+
for( i = 0; (i < count) && (0 == pBlockLength[i]); i++ );
41+
if( i == count ) { /* either nothing or nothing relevant */
42+
return ompi_datatype_duplicate( &ompi_mpi_datatype_null.dt, newType);
4343
}
44-
45-
/* if we compute the total number of elements before we can
44+
/* compute the total number of elements before we can
4645
* avoid increasing the size of the desc array often.
4746
*/
48-
lastType = (ompi_datatype_t*)pTypes[0];
49-
lastBlock = pBlockLength[0];
47+
start_from = i;
48+
lastType = (ompi_datatype_t*)pTypes[start_from];
49+
lastBlock = pBlockLength[start_from];
5050
lastExtent = lastType->super.ub - lastType->super.lb;
51-
lastDisp = pDisp[0];
52-
endto = pDisp[0] + lastExtent * lastBlock;
51+
lastDisp = pDisp[start_from];
52+
endto = pDisp[start_from] + lastExtent * lastBlock;
5353

54-
for( i = 1; i < count; i++ ) {
54+
for( i = (start_from + 1); i < count; i++ ) {
5555
if( (pTypes[i] == lastType) && (pDisp[i] == endto) ) {
5656
lastBlock += pBlockLength[i];
5757
endto = lastDisp + lastBlock * lastExtent;
@@ -68,16 +68,16 @@ int32_t ompi_datatype_create_struct( int count, const int* pBlockLength, const p
6868
disp += lastType->super.desc.used;
6969
if( lastBlock != 1 ) disp += 2;
7070

71-
lastType = (ompi_datatype_t*)pTypes[0];
72-
lastBlock = pBlockLength[0];
71+
lastType = (ompi_datatype_t*)pTypes[start_from];
72+
lastBlock = pBlockLength[start_from];
7373
lastExtent = lastType->super.ub - lastType->super.lb;
74-
lastDisp = pDisp[0];
75-
endto = pDisp[0] + lastExtent * lastBlock;
74+
lastDisp = pDisp[start_from];
75+
endto = pDisp[start_from] + lastExtent * lastBlock;
7676

7777
pdt = ompi_datatype_create( (int32_t)disp );
7878

7979
/* Do again the same loop but now add the elements */
80-
for( i = 1; i < count; i++ ) {
80+
for( i = (start_from + 1); i < count; i++ ) {
8181
if( (pTypes[i] == lastType) && (pDisp[i] == endto) ) {
8282
lastBlock += pBlockLength[i];
8383
endto = lastDisp + lastBlock * lastExtent;

ompi/datatype/ompi_datatype_create_vector.c

+5-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright (c) 2004-2006 The Trustees of Indiana University and Indiana
44
* University Research and Technology
55
* Corporation. All rights reserved.
6-
* Copyright (c) 2004-2013 The University of Tennessee and The University
6+
* Copyright (c) 2004-2019 The University of Tennessee and The University
77
* of Tennessee Research Foundation. All rights
88
* reserved.
99
* Copyright (c) 2004-2006 High Performance Computing Center Stuttgart,
@@ -28,23 +28,14 @@
2828

2929
#include "ompi/datatype/ompi_datatype.h"
3030

31-
/* Open questions ...
32-
* - how to improuve the handling of these vectors (creating a temporary datatype
33-
* can be ONLY a initial solution.
34-
*
35-
*/
36-
3731
int32_t ompi_datatype_create_vector( int count, int bLength, int stride,
3832
const ompi_datatype_t* oldType, ompi_datatype_t** newType )
3933
{
4034
ompi_datatype_t *pTempData, *pData;
4135
ptrdiff_t extent = oldType->super.ub - oldType->super.lb;
4236

43-
44-
if( 0 == count ) {
45-
*newType = ompi_datatype_create( 0 );
46-
ompi_datatype_add( *newType, &ompi_mpi_datatype_null.dt, 0, 0, 0);
47-
return OMPI_SUCCESS;
37+
if( (0 == count) || (0 == bLength) ) {
38+
return ompi_datatype_duplicate( &ompi_mpi_datatype_null.dt, newType);
4839
}
4940

5041
pData = ompi_datatype_create( oldType->super.desc.used + 2 );
@@ -72,10 +63,8 @@ int32_t ompi_datatype_create_hvector( int count, int bLength, ptrdiff_t stride,
7263
ompi_datatype_t *pTempData, *pData;
7364
ptrdiff_t extent = oldType->super.ub - oldType->super.lb;
7465

75-
if( 0 == count ) {
76-
*newType = ompi_datatype_create( 0 );
77-
ompi_datatype_add( *newType, &ompi_mpi_datatype_null.dt, 0, 0, 0);
78-
return OMPI_SUCCESS;
66+
if( (0 == count) || (0 == bLength) ) {
67+
return ompi_datatype_duplicate( &ompi_mpi_datatype_null.dt, newType);
7968
}
8069

8170
pTempData = ompi_datatype_create( oldType->super.desc.used + 2 );

0 commit comments

Comments
 (0)