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 Jan 4, 2024
1 parent ab263ae commit 7cc629e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/openPMD/IO/IOTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ struct OPENPMDAPI_EXPORT Parameter<Operation::CREATE_FILE>
}

std::string name = "";
std::string openPMDversion; // @todo: Maybe move this to AbstractIOHandler
};

template <>
Expand Down
51 changes: 47 additions & 4 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 Down Expand Up @@ -93,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 @@ -160,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
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 @@ -925,6 +925,7 @@ void Series::flushGorVBased(
}
Parameter<Operation::CREATE_FILE> fCreate;
fCreate.name = series.m_name;
fCreate.openPMDversion = openPMD();
IOHandler()->enqueue(IOTask(this, fCreate));
}

Expand Down

0 comments on commit 7cc629e

Please sign in to comment.