Skip to content

Commit

Permalink
Short mode in default in openPMD >= 2.
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Nov 27, 2023
1 parent 3a6dbe1 commit 914aaaa
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
1 change: 1 addition & 0 deletions include/openPMD/IO/IOTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ struct OPENPMDAPI_EXPORT Parameter<Operation::CREATE_FILE>

std::string name = "";
IterationEncoding encoding = IterationEncoding::groupBased;
std::string openPMDversion;
};

template <>
Expand Down
54 changes: 49 additions & 5 deletions include/openPMD/RecordComponent.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#pragma once

#include "openPMD/Datatype.hpp"
#include "openPMD/Error.hpp"
#include "openPMD/RecordComponent.hpp"
#include "openPMD/Span.hpp"
#include "openPMD/auxiliary/Memory.hpp"
Expand All @@ -29,6 +31,7 @@
#include "openPMD/auxiliary/UniquePtr.hpp"

#include <memory>
#include <type_traits>

namespace openPMD
{
Expand Down Expand Up @@ -92,12 +95,38 @@ inline std::shared_ptr<T> RecordComponent::loadChunk(Offset o, Extent e)
#endif
}

namespace detail
{
template <typename To>
struct do_convert
{
template <typename From>
static std::optional<To> call(Attribute &attr)
{
if constexpr (std::is_convertible_v<From, To>)
{
return std::make_optional<To>(attr.get<From>());
}
else
{
return std::nullopt;
}
}

static constexpr char const *errorMsg = "is_conversible";
};
} // namespace detail

template <typename T>
inline void
RecordComponent::loadChunk(std::shared_ptr<T> data, Offset o, Extent e)
{
Datatype dtype = determineDatatype(data);
if (dtype != getDatatype())
/*
* For constant components, we implement type conversion, so there is
* a separate check further below.
*/
if (dtype != getDatatype() && !constant())
if (!isSameInteger<T>(getDatatype()) &&
!isSameFloatingPoint<T>(getDatatype()) &&
!isSameComplexFloatingPoint<T>(getDatatype()) &&
Expand Down Expand Up @@ -159,10 +188,25 @@ RecordComponent::loadChunk(std::shared_ptr<T> data, Offset o, Extent e)
for (auto const &dimensionSize : extent)
numPoints *= dimensionSize;

T value = rc.m_constantValue.get<T>();
std::optional<T> val =
switchNonVectorType<detail::do_convert</* To = */ T>>(
/* from = */ getDatatype(), rc.m_constantValue);

T *raw_ptr = data.get();
std::fill(raw_ptr, raw_ptr + numPoints, value);
if (val.has_value())
{
T *raw_ptr = data.get();
std::fill(raw_ptr, raw_ptr + numPoints, *val);
}
else
{
std::string const data_type_str = datatypeToString(getDatatype());
std::string const requ_type_str =
datatypeToString(determineDatatype<T>());
std::string err_msg =
"Type conversion during chunk loading not possible! ";
err_msg += "Data: " + data_type_str + "; Load as: " + requ_type_str;
throw error::WrongAPIUsage(err_msg);
}
}
else
{
Expand Down Expand Up @@ -250,7 +294,7 @@ void RecordComponent::storeChunkRaw(T *ptr, Offset offset, Extent extent)

template <typename T_ContiguousContainer>
inline typename std::enable_if_t<
auxiliary::IsContiguousContainer_v<T_ContiguousContainer> >
auxiliary::IsContiguousContainer_v<T_ContiguousContainer>>
RecordComponent::storeChunk(T_ContiguousContainer &data, Offset o, Extent e)
{
uint8_t dim = getDimensionality();
Expand Down
7 changes: 7 additions & 0 deletions src/IO/JSON/JSONIOHandlerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,13 @@ void JSONIOHandlerImpl::createFile(
access::write(m_handler->m_backendAccess),
"[JSON] Creating a file in read-only mode is not possible.");

if (m_attributeModeSpecificationVia == SpecificationVia::DefaultValue)
{
m_attributeMode = parameters.openPMDversion >= "2."
? AttributeMode::Short
: AttributeMode::Long;
}

if (!writable->written)
{
std::string name = parameters.name + m_originalExtension;
Expand Down
1 change: 1 addition & 0 deletions src/Iteration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ void Iteration::flushFileBased(
/* create file */
Parameter<Operation::CREATE_FILE> fCreate;
fCreate.name = filename;
fCreate.openPMDversion = s.openPMD();
IOHandler()->enqueue(IOTask(&s.writable(), fCreate));

/* create basePath */
Expand Down
1 change: 1 addition & 0 deletions src/Series.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,7 @@ void Series::flushGorVBased(
Parameter<Operation::CREATE_FILE> fCreate;
fCreate.name = series.m_name;
fCreate.encoding = iterationEncoding();
fCreate.openPMDversion = openPMD();
IOHandler()->enqueue(IOTask(this, fCreate));
}

Expand Down

0 comments on commit 914aaaa

Please sign in to comment.