From 39834d8f45c0f9d4009da2a5505d02b6f516984f Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@esi-group.com> Date: Mon, 29 Jul 2019 12:01:34 +0200 Subject: [PATCH] ENH: add readContiguous detail (#1378) - allows some internal handling for reading dissimilar storage types. Eg, scalars written as float (WM_SP), but read as double (WM_DP) - reading binary parcel coordinates with dissimilar storage types is still pending --- .../containers/Bits/PackedList/PackedListIO.C | 2 + .../containers/Lists/FixedList/FixedListIO.C | 7 +++- src/OpenFOAM/containers/Lists/List/ListIO.C | 7 +++- src/OpenFOAM/containers/Lists/UList/UListIO.C | 7 +++- src/OpenFOAM/db/IOstreams/IOstreams/Istream.H | 41 +++++++++++++++++++ src/OpenFOAM/matrices/Matrix/MatrixIO.C | 7 +++- src/OpenFOAM/meshes/boundBox/boundBox.C | 3 +- .../meshShapes/labelledTri/labelledTriI.H | 7 +++- .../polyTopoChange/refinementData.C | 5 ++- src/meshTools/AABBTree/AABBTree.C | 21 +++++----- 10 files changed, 88 insertions(+), 19 deletions(-) diff --git a/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C b/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C index 0e36d3aec17..92f979ab498 100644 --- a/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C +++ b/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C @@ -119,6 +119,8 @@ Foam::Istream& Foam::PackedList<Width>::read(Istream& is) } else { + // NOTE: binary content should be independent of WM_LABEL_SIZE + if (len) { is.read diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C b/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C index 337736e54a4..851767a933a 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C @@ -223,7 +223,12 @@ Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, N>& list) { // Binary and contiguous - is.read(reinterpret_cast<char*>(list.data()), N*sizeof(T)); + Detail::readContiguous<T> + ( + is, + reinterpret_cast<char*>(list.data()), + N*sizeof(T) + ); is.fatalCheck ( diff --git a/src/OpenFOAM/containers/Lists/List/ListIO.C b/src/OpenFOAM/containers/Lists/List/ListIO.C index 696d90ad8ed..ca2065e43e5 100644 --- a/src/OpenFOAM/containers/Lists/List/ListIO.C +++ b/src/OpenFOAM/containers/Lists/List/ListIO.C @@ -126,7 +126,12 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& list) { // Non-empty, binary, contiguous - is.read(reinterpret_cast<char*>(list.data()), len*sizeof(T)); + Detail::readContiguous<T> + ( + is, + reinterpret_cast<char*>(list.data()), + len*sizeof(T) + ); is.fatalCheck ( diff --git a/src/OpenFOAM/containers/Lists/UList/UListIO.C b/src/OpenFOAM/containers/Lists/UList/UListIO.C index 1560032ff4d..bc5b436bdea 100644 --- a/src/OpenFOAM/containers/Lists/UList/UListIO.C +++ b/src/OpenFOAM/containers/Lists/UList/UListIO.C @@ -259,7 +259,12 @@ Foam::Istream& Foam::operator>>(Istream& is, UList<T>& list) { // Non-empty, binary, contiguous - is.read(reinterpret_cast<char*>(list.data()), len*sizeof(T)); + Detail::readContiguous<T> + ( + is, + reinterpret_cast<char*>(list.data()), + len*sizeof(T) + ); is.fatalCheck ( diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/Istream.H b/src/OpenFOAM/db/IOstreams/IOstreams/Istream.H index c178447ab47..e158091fb39 100644 --- a/src/OpenFOAM/db/IOstreams/IOstreams/Istream.H +++ b/src/OpenFOAM/db/IOstreams/IOstreams/Istream.H @@ -47,6 +47,7 @@ SourceFiles #include "IOstream.H" #include "token.H" +#include "contiguous.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -194,10 +195,50 @@ inline Istream& operator>>(Istream& is, IOstreamManip f) } +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Detail +{ + //- Read binary block of contiguous data, possibly with conversion + template<class T> + void readContiguous(Istream& is, char* data, std::streamsize byteCount) + { + is.beginRawRead(); + + if (is_contiguous_label<T>::value) + { + readRawLabel + ( + is, + reinterpret_cast<label*>(data), + byteCount/sizeof(label) + ); + } + else if (is_contiguous_scalar<T>::value) + { + readRawScalar + ( + is, + reinterpret_cast<scalar*>(data), + byteCount/sizeof(scalar) + ); + } + else + { + is.readRaw(data, byteCount); + } + + is.endRawRead(); + } + +} // End namespace Detail + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #ifdef NoRepository diff --git a/src/OpenFOAM/matrices/Matrix/MatrixIO.C b/src/OpenFOAM/matrices/Matrix/MatrixIO.C index 7f363acfe4a..06fad30be4a 100644 --- a/src/OpenFOAM/matrices/Matrix/MatrixIO.C +++ b/src/OpenFOAM/matrices/Matrix/MatrixIO.C @@ -112,7 +112,12 @@ bool Foam::Matrix<Form, Type>::readMatrix(Istream& is) { if (len) { - is.read(reinterpret_cast<char*>(v_), len*sizeof(Type)); + Detail::readContiguous<Type> + ( + is, + reinterpret_cast<char*>(v_), + len*sizeof(Type) + ); is.fatalCheck("readMatrix : reading the binary block"); } diff --git a/src/OpenFOAM/meshes/boundBox/boundBox.C b/src/OpenFOAM/meshes/boundBox/boundBox.C index 63022fa8572..9d7d594e8d3 100644 --- a/src/OpenFOAM/meshes/boundBox/boundBox.C +++ b/src/OpenFOAM/meshes/boundBox/boundBox.C @@ -305,8 +305,9 @@ Foam::Istream& Foam::operator>>(Istream& is, boundBox& bb) } else { - is.read + Detail::readContiguous<boundBox> ( + is, reinterpret_cast<char*>(&bb.min_), sizeof(boundBox) ); diff --git a/src/OpenFOAM/meshes/meshShapes/labelledTri/labelledTriI.H b/src/OpenFOAM/meshes/meshShapes/labelledTri/labelledTriI.H index 75017a13bbe..cfeebd69fb1 100644 --- a/src/OpenFOAM/meshes/meshShapes/labelledTri/labelledTriI.H +++ b/src/OpenFOAM/meshes/meshShapes/labelledTri/labelledTriI.H @@ -134,7 +134,12 @@ inline Foam::Istream& Foam::operator>>(Istream& is, labelledTri& t) } else { - is.read(reinterpret_cast<char*>(&t), sizeof(labelledTri)); + Detail::readContiguous<labelledTri> + ( + is, + reinterpret_cast<char*>(&t), + sizeof(labelledTri) + ); } is.check(FUNCTION_NAME); diff --git a/src/dynamicMesh/polyTopoChange/polyTopoChange/refinementData.C b/src/dynamicMesh/polyTopoChange/polyTopoChange/refinementData.C index 87472ce2fcc..d796f09367f 100644 --- a/src/dynamicMesh/polyTopoChange/polyTopoChange/refinementData.C +++ b/src/dynamicMesh/polyTopoChange/polyTopoChange/refinementData.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011 OpenFOAM Foundation @@ -61,8 +61,9 @@ Foam::Istream& Foam::operator>>(Foam::Istream& is, Foam::refinementData& wDist) } else { - is.read + Detail::readContiguous<refinementData> ( + is, reinterpret_cast<char*>(&wDist.refinementCount_), sizeof(refinementData) ); diff --git a/src/meshTools/AABBTree/AABBTree.C b/src/meshTools/AABBTree/AABBTree.C index 4a5247460ec..e2b15f77de0 100644 --- a/src/meshTools/AABBTree/AABBTree.C +++ b/src/meshTools/AABBTree/AABBTree.C @@ -495,22 +495,21 @@ Foam::Istream& Foam::operator>>(Istream& is, AABBTree<Type>& tree) if (is.format() == IOstream::ASCII) { is >> tree.maxLevel_ - >> tree.minLeafSize_ - >> tree.boundBoxes_ - >> tree.addressing_; + >> tree.minLeafSize_; } else { - is.read - ( - reinterpret_cast<char*>(&tree.maxLevel_), - sizeof(tree.maxLevel_) - + sizeof(tree.minLeafSize_) - ); - is >> tree.boundBoxes_ - >> tree.addressing_; + is.beginRawRead(); + + readRawLabel(is, &tree.maxLevel_); + readRawLabel(is, &tree.minLeafSize_); + + is.endRawRead(); } + is >> tree.boundBoxes_ + >> tree.addressing_; + is.check(FUNCTION_NAME); return is; } -- GitLab