Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for degenerate dimensions #271

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/serialbox-c/FortranWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void make_4D(std::vector<int>& v) {
throw Exception(
"The FortranWrapper supports up to 4 dimensions (field with %i dimensions was passed).",
v.size());
v.resize(4, 0);
v.resize(4, -1);
}

std::vector<int> make_strides(int istride, int jstride, int kstride, int lstride) {
Expand Down
8 changes: 4 additions & 4 deletions src/serialbox-c/Serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ static std::string vecToString(VecType&& vec) {

std::vector<int> make_dims(int iSize, int jSize, int kSize, int lSize) {
std::vector<int> dims;
if(iSize > 0 or jSize > 0 or kSize > 0 or lSize > 0)
if(iSize >= 0 or jSize >= 0 or kSize >= 0 or lSize >= 0)
dims.push_back(iSize);
if(jSize > 0 or kSize > 0 or lSize > 0)
if(jSize >= 0 or kSize >= 0 or lSize >= 0)
dims.push_back(jSize);
if(kSize > 0 or lSize > 0)
if(kSize >= 0 or lSize >= 0)
dims.push_back(kSize);
if(lSize > 0)
if(lSize >= 0)
dims.push_back(lSize);
return dims;
}
Expand Down
44 changes: 29 additions & 15 deletions src/serialbox-fortran/m_serialize.f90
Original file line number Diff line number Diff line change
Expand Up @@ -1186,8 +1186,8 @@ END FUNCTION fs_get_rank
!==============================================================================
!+ Module function that returns the size of the requested field
! Always returns an array with 4 elements.
! For fields with a rank less than 4, the upper dimensions are given with size 0.
! For scalars the result is {1,0,0,0}.
! For fields with a rank less than 4, the upper dimensions are given with size -1.
! For scalars the result is {0,0,0,0}.
!------------------------------------------------------------------------------
FUNCTION fs_get_size(serializer, fieldname)
TYPE(t_serializer) :: serializer
Expand Down Expand Up @@ -1219,7 +1219,7 @@ END FUNCTION fs_get_size
!==============================================================================
!+ Module function that returns the total size of the requested field,
! that is the product of the sizes of the individual dimensions
! For scalars the result is 1.
! For scalars the result is 0.
!------------------------------------------------------------------------------
FUNCTION fs_get_total_size(serializer, fieldname)
TYPE(t_serializer) :: serializer
Expand Down Expand Up @@ -1822,7 +1822,7 @@ SUBROUTINE fs_write_int_0d(serializer, savepoint, fieldname, field)
! This workaround is needed for gcc < 4.9
padd=>field

CALL fs_check_size(serializer, fieldname, "int", fs_intsize(), 1, 0, 0, 0)
CALL fs_check_size(serializer, fieldname, "int", fs_intsize(), 1, -1, -1, -1)
CALL fs_compute_strides(serializer%serializer_ptr, TRIM(fieldname)//C_NULL_CHAR, &
C_LOC(padd), C_LOC(padd), C_LOC(padd), C_LOC(padd), C_LOC(padd), &
istride, jstride, kstride, lstride)
Expand All @@ -1846,17 +1846,24 @@ SUBROUTINE fs_write_int_1d(serializer, savepoint, fieldname, field, minushalos,
! This workaround is needed for gcc < 4.9
padd=>field

CALL fs_check_size(serializer, fieldname, "int", fs_intsize(), SIZE(field, 1), 0, 0, 0, minushalos, plushalos)
CALL fs_compute_strides(serializer%serializer_ptr, TRIM(fieldname)//C_NULL_CHAR, &
IF (SIZE(field) > 0) THEN
CALL fs_check_size(serializer, fieldname, "int", fs_intsize(), SIZE(field, 1), -1, -1, -1, minushalos, plushalos)
CALL fs_compute_strides(serializer%serializer_ptr, TRIM(fieldname)//C_NULL_CHAR, &
C_LOC(padd(1)), &
C_LOC(padd(MIN(2, SIZE(field, 1)))), &
C_LOC(padd(1)), &
C_LOC(padd(1)), &
C_LOC(padd(1)), &
istride, jstride, kstride, lstride)
CALL fs_write_field_(serializer%serializer_ptr, savepoint%savepoint_ptr, &
CALL fs_write_field_(serializer%serializer_ptr, savepoint%savepoint_ptr, &
TRIM(fieldname)//C_NULL_CHAR, &
C_LOC(padd(1)), istride, -1, -1, -1)
ELSE
CALL fs_check_size(serializer, fieldname, "int", fs_intsize(), 0, -1, -1, -1, minushalos, plushalos)
CALL fs_write_field_(serializer%serializer_ptr, savepoint%savepoint_ptr, &
TRIM(fieldname)//C_NULL_CHAR, &
C_LOC(padd), 0, -1, -1, -1)
END IF
END SUBROUTINE fs_write_int_1d


Expand All @@ -1874,7 +1881,7 @@ SUBROUTINE fs_write_int_2d(serializer, savepoint, fieldname, field, minushalos,
! This workaround is needed for gcc < 4.9
padd=>field

CALL fs_check_size(serializer, fieldname, "int", fs_intsize(), SIZE(field, 1), SIZE(field, 2), 0, 0, minushalos, plushalos)
CALL fs_check_size(serializer, fieldname, "int", fs_intsize(), SIZE(field, 1), SIZE(field, 2), -1, -1, minushalos, plushalos)
CALL fs_compute_strides(serializer%serializer_ptr, TRIM(fieldname)//C_NULL_CHAR, &
C_LOC(padd(1, 1)), &
C_LOC(padd(MIN(2, SIZE(field, 1)), 1)), &
Expand Down Expand Up @@ -1902,7 +1909,7 @@ SUBROUTINE fs_write_int_3d(serializer, savepoint, fieldname, field, minushalos,
! This workaround is needed for gcc < 4.9
padd=>field

CALL fs_check_size(serializer, fieldname, "int", fs_intsize(), SIZE(field, 1), SIZE(field, 2), SIZE(field, 3), 0, &
CALL fs_check_size(serializer, fieldname, "int", fs_intsize(), SIZE(field, 1), SIZE(field, 2), SIZE(field, 3), -1, &
minushalos, plushalos)
CALL fs_compute_strides(serializer%serializer_ptr, TRIM(fieldname)//C_NULL_CHAR, &
C_LOC(padd(1, 1, 1)), &
Expand Down Expand Up @@ -2633,7 +2640,7 @@ SUBROUTINE fs_read_int_0d(serializer, savepoint, fieldname, field, rperturb)
! This workaround is needed for gcc < 4.9
padd=>field

CALL fs_check_size(serializer, fieldname, "int", fs_intsize(), 1, 0, 0, 0)
CALL fs_check_size(serializer, fieldname, "int", fs_intsize(), 1, -1, -1, -1)
CALL fs_compute_strides(serializer%serializer_ptr, TRIM(fieldname)//C_NULL_CHAR, &
C_LOC(padd), C_LOC(padd), C_LOC(padd), C_LOC(padd), C_LOC(padd), &
istride, jstride, kstride, lstride)
Expand All @@ -2657,17 +2664,24 @@ SUBROUTINE fs_read_int_1d(serializer, savepoint, fieldname, field, rperturb)
! This workaround is needed for gcc < 4.9
padd=>field

CALL fs_check_size(serializer, fieldname, "int", fs_intsize(), SIZE(field, 1), 0, 0, 0)
CALL fs_compute_strides(serializer%serializer_ptr, TRIM(fieldname)//C_NULL_CHAR, &
IF (SIZE(field) > 0) THEN
CALL fs_check_size(serializer, fieldname, "int", fs_intsize(), SIZE(field, 1), -1, -1, -1)
CALL fs_compute_strides(serializer%serializer_ptr, TRIM(fieldname)//C_NULL_CHAR, &
C_LOC(padd(1)), &
C_LOC(padd(MIN(2, SIZE(field, 1)))), &
C_LOC(padd(1)), &
C_LOC(padd(1)), &
C_LOC(padd(1)), &
istride, jstride, kstride, lstride)
CALL fs_read_field_(serializer%serializer_ptr, savepoint%savepoint_ptr, &
CALL fs_read_field_(serializer%serializer_ptr, savepoint%savepoint_ptr, &
TRIM(fieldname)//C_NULL_CHAR, &
C_LOC(padd(1)), istride, -1, -1, -1)
ELSE
CALL fs_check_size(serializer, fieldname, "int", fs_intsize(), 0, -1, -1, -1)
CALL fs_read_field_(serializer%serializer_ptr, savepoint%savepoint_ptr, &
TRIM(fieldname)//C_NULL_CHAR, &
C_LOC(padd), 0, -1, -1, -1)
END IF
END SUBROUTINE fs_read_int_1d


Expand All @@ -2685,7 +2699,7 @@ SUBROUTINE fs_read_int_2d(serializer, savepoint, fieldname, field, rperturb)
! This workaround is needed for gcc < 4.9
padd=>field

CALL fs_check_size(serializer, fieldname, "int", fs_intsize(), SIZE(field, 1), SIZE(field, 2), 0, 0)
CALL fs_check_size(serializer, fieldname, "int", fs_intsize(), SIZE(field, 1), SIZE(field, 2), -1, -1)
CALL fs_compute_strides(serializer%serializer_ptr, TRIM(fieldname)//C_NULL_CHAR, &
C_LOC(padd(1, 1)), &
C_LOC(padd(MIN(2, SIZE(field, 1)), 1)), &
Expand Down Expand Up @@ -2713,7 +2727,7 @@ SUBROUTINE fs_read_int_3d(serializer, savepoint, fieldname, field, rperturb)
! This workaround is needed for gcc < 4.9
padd=>field

CALL fs_check_size(serializer, fieldname, "int", fs_intsize(), SIZE(field, 1), SIZE(field, 2), SIZE(field, 3), 0)
CALL fs_check_size(serializer, fieldname, "int", fs_intsize(), SIZE(field, 1), SIZE(field, 2), SIZE(field, 3), -1)
CALL fs_compute_strides(serializer%serializer_ptr, TRIM(fieldname)//C_NULL_CHAR, &
C_LOC(padd(1, 1, 1)), &
C_LOC(padd(MIN(2, SIZE(field, 1)), 1, 1)), &
Expand Down
59 changes: 32 additions & 27 deletions test/serialbox-c/UnittestFortranWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,33 +361,38 @@ TEST_F(CFortranWrapperTest, Rank) {
serialboxSerializerCreate(Write, directory->path().c_str(), "Rank", "Binary");
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();

serialboxFortranSerializerRegisterField(serializer, "int0", Int32, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();
serialboxFortranSerializerRegisterField(serializer, "int1", Int32, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();
serialboxFortranSerializerRegisterField(serializer, "int2", Int32, 4, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();
serialboxFortranSerializerRegisterField(serializer, "int3", Int32, 4, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();
serialboxFortranSerializerRegisterField(serializer, "int4", Int32, 4, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0);
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();

int rank;
serialboxFortranSerializerGetFieldRank(serializer, "int0", &rank);
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();
EXPECT_EQ(rank, 1);
serialboxFortranSerializerGetFieldRank(serializer, "int1", &rank);
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();
EXPECT_EQ(rank, 1);
serialboxFortranSerializerGetFieldRank(serializer, "int2", &rank);
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();
EXPECT_EQ(rank, 2);
serialboxFortranSerializerGetFieldRank(serializer, "int3", &rank);
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();
EXPECT_EQ(rank, 3);
serialboxFortranSerializerGetFieldRank(serializer, "int4", &rank);
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();
EXPECT_EQ(rank, 4);
serialboxFortranSerializerRegisterField(serializer, "int0", Int32, 4, -1, -1, -1, -1, 0,
0, 0, 0, 0, 0, 0, 0);
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();
serialboxFortranSerializerRegisterField(serializer, "int1", Int32, 4, 1, -1, -1, -1, 0, 0,
0, 0, 0, 0, 0, 0);
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();
serialboxFortranSerializerRegisterField(serializer, "int2", Int32, 4, 1, 1, -1, -1, 0, 0,
0, 0, 0, 0, 0, 0);
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();
serialboxFortranSerializerRegisterField(serializer, "int3", Int32, 4, 1, 1, 1, -1, 0, 0,
0, 0, 0, 0, 0, 0);
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();
serialboxFortranSerializerRegisterField(serializer, "int4", Int32, 4, 1, 1, 1, 1, 0, 0, 0,
0, 0, 0, 0, 0);
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();

int rank;
serialboxFortranSerializerGetFieldRank(serializer, "int0", &rank);
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();
EXPECT_EQ(rank, 0);
serialboxFortranSerializerGetFieldRank(serializer, "int1", &rank);
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();
EXPECT_EQ(rank, 1);
serialboxFortranSerializerGetFieldRank(serializer, "int2", &rank);
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();
EXPECT_EQ(rank, 2);
serialboxFortranSerializerGetFieldRank(serializer, "int3", &rank);
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();
EXPECT_EQ(rank, 3);
serialboxFortranSerializerGetFieldRank(serializer, "int4", &rank);
ASSERT_FALSE(this->hasErrorAndReset()) << this->getLastErrorMsg();
EXPECT_EQ(rank, 4);
}

TEST_F(CFortranWrapperTest, PermanentMetaInfo) {
Expand Down
Loading