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

Fix append mode double attributes #1302

Merged
merged 17 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 11 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
11 changes: 10 additions & 1 deletion include/openPMD/IO/ADIOS/ADIOS2IOHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "openPMD/IO/InvalidatableFile.hpp"
#include "openPMD/IterationEncoding.hpp"
#include "openPMD/auxiliary/JSON_internal.hpp"
#include "openPMD/auxiliary/Mpi.hpp"
#include "openPMD/backend/Writable.hpp"
#include "openPMD/config.hpp"

Expand Down Expand Up @@ -150,6 +151,11 @@ class ADIOS2IOHandlerImpl
void
createFile(Writable *, Parameter<Operation::CREATE_FILE> const &) override;

void checkFile(Writable *, Parameter<Operation::CHECK_FILE> &) override;

// MPI Collective
bool checkFile(std::string fullFilePath) const;

void
createPath(Writable *, Parameter<Operation::CREATE_PATH> const &) override;

Expand Down Expand Up @@ -226,6 +232,9 @@ class ADIOS2IOHandlerImpl

private:
adios2::ADIOS m_ADIOS;
#if openPMD_HAVE_MPI
std::optional<MPI_Comm> m_communicator;
#endif
/*
* If the iteration encoding is variableBased, we default to using the
* 2021_02_09 schema since it allows mutable attributes.
Expand Down Expand Up @@ -329,7 +338,7 @@ class ADIOS2IOHandlerImpl
// use m_config
std::optional<std::vector<ParameterizedOperator> > getOperators();

std::string fileSuffix() const;
std::string fileSuffix(bool verbose = true) const;

/*
* We need to give names to IO objects. These names are irrelevant
Expand Down
1 change: 1 addition & 0 deletions include/openPMD/IO/ADIOS/CommonADIOS1IOHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class CommonADIOS1IOHandlerImpl : public AbstractIOHandlerImpl
public:
void
createFile(Writable *, Parameter<Operation::CREATE_FILE> const &) override;
void checkFile(Writable *, Parameter<Operation::CHECK_FILE> &) override;
void
createPath(Writable *, Parameter<Operation::CREATE_PATH> const &) override;
void createDataset(
Expand Down
17 changes: 17 additions & 0 deletions include/openPMD/IO/AbstractIOHandlerImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ class AbstractIOHandlerImpl
deref_dynamic_cast<Parameter<Operation::CREATE_FILE> >(
i.parameter.get()));
break;
case O::CHECK_FILE:
checkFile(
i.writable,
deref_dynamic_cast<Parameter<Operation::CHECK_FILE> >(
i.parameter.get()));
break;
case O::CREATE_PATH:
createPath(
i.writable,
Expand Down Expand Up @@ -220,6 +226,17 @@ class AbstractIOHandlerImpl
virtual void
closeFile(Writable *, Parameter<Operation::CLOSE_FILE> const &) = 0;

/**
* Check if the file specified by the parameter is already present on disk.
* The Writable is irrelevant for this method.
* A backend can choose to ignore this task and specify FileExists::DontKnow
* in the out parameter.
* The consequence will be that some top-level attributes might be defined
* a second time when appending to an existing file, because the frontend
* cannot be sure that the file already has these attributes.
*/
virtual void checkFile(Writable *, Parameter<Operation::CHECK_FILE> &) = 0;

/** Advance the file/stream that this writable belongs to.
*
* If the backend is based around usage of IO steps (especially streaming
Expand Down
11 changes: 11 additions & 0 deletions include/openPMD/IO/HDF5/HDF5IOHandlerImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "openPMD/IO/AbstractIOHandlerImpl.hpp"

#include "openPMD/auxiliary/JSON_internal.hpp"
#include "openPMD/auxiliary/Mpi.hpp"

#include <hdf5.h>
#include <optional>
Expand All @@ -43,6 +44,7 @@ class HDF5IOHandlerImpl : public AbstractIOHandlerImpl

void
createFile(Writable *, Parameter<Operation::CREATE_FILE> const &) override;
void checkFile(Writable *, Parameter<Operation::CHECK_FILE> &) override;
void
createPath(Writable *, Parameter<Operation::CREATE_PATH> const &) override;
void createDataset(
Expand Down Expand Up @@ -92,6 +94,15 @@ class HDF5IOHandlerImpl : public AbstractIOHandlerImpl
hid_t m_H5T_CDOUBLE;
hid_t m_H5T_CLONG_DOUBLE;

protected:
#if openPMD_HAVE_MPI
/*
* Not defined in ParallelHDF5IOHandlerImpl, so we don't have to write
* some methods twice.
*/
std::optional<MPI_Comm> m_communicator;
#endif

private:
json::TracingJSON m_config;
std::string m_chunks = "auto";
Expand Down
29 changes: 28 additions & 1 deletion include/openPMD/IO/IOTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ Writable *getWritable(Attributable *);
/** Type of IO operation between logical and persistent data.
*/
OPENPMDAPI_EXPORT_ENUM_CLASS(Operation){
CREATE_FILE, OPEN_FILE, CLOSE_FILE, DELETE_FILE,
CREATE_FILE, CHECK_FILE, OPEN_FILE, CLOSE_FILE,
DELETE_FILE,

CREATE_PATH, CLOSE_PATH, OPEN_PATH, DELETE_PATH,
LIST_PATHS,
Expand Down Expand Up @@ -118,6 +119,32 @@ struct OPENPMDAPI_EXPORT Parameter<Operation::CREATE_FILE>
IterationEncoding encoding = IterationEncoding::groupBased;
};

template <>
struct OPENPMDAPI_EXPORT Parameter<Operation::CHECK_FILE>
: public AbstractParameter
{
Parameter() = default;
Parameter(Parameter const &p)
: AbstractParameter(), name(p.name), fileExists(p.fileExists)
{}

std::unique_ptr<AbstractParameter> clone() const override
{
return std::unique_ptr<AbstractParameter>(
new Parameter<Operation::CHECK_FILE>(*this));
}

std::string name = "";
enum class FileExists
{
DontKnow,
Yes,
No
};
std::shared_ptr<FileExists> fileExists =
std::make_shared<FileExists>(FileExists::DontKnow);
};

template <>
struct OPENPMDAPI_EXPORT Parameter<Operation::OPEN_FILE>
: public AbstractParameter
Expand Down
2 changes: 2 additions & 0 deletions include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ class JSONIOHandlerImpl : public AbstractIOHandlerImpl
void
createFile(Writable *, Parameter<Operation::CREATE_FILE> const &) override;

void checkFile(Writable *, Parameter<Operation::CHECK_FILE> &) override;

void
createPath(Writable *, Parameter<Operation::CREATE_PATH> const &) override;

Expand Down
4 changes: 3 additions & 1 deletion include/openPMD/Series.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ namespace internal
* the same instance.
*/
std::optional<WriteIterations> m_writeIterations;

Attributable mockedDefaults;
ax3l marked this conversation as resolved.
Show resolved Hide resolved
/**
* Needed if reading a single iteration of a file-based series.
* Users may specify the concrete filename of one iteration instead of
Expand Down Expand Up @@ -534,7 +536,7 @@ OPENPMD_private
bool hasExpansionPattern(std::string filenameWithExtension);
bool reparseExpansionPattern(std::string filenameWithExtension);
void init(std::shared_ptr<AbstractIOHandler>, std::unique_ptr<ParsedInput>);
void initDefaults(IterationEncoding);
void initDefaults(IterationEncoding, bool initAll = false);
/**
* @brief Internal call for flushing a Series.
*
Expand Down
83 changes: 83 additions & 0 deletions include/openPMD/auxiliary/Mpi.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* Copyright 2022 Franz Poeschel
*
* This file is part of openPMD-api.
*
* openPMD-api is free software: you can redistribute it and/or modify
* it under the terms of of either the GNU General Public License or
* the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* openPMD-api is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License and the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* and the GNU Lesser General Public License along with openPMD-api.
* If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once

#include "openPMD/config.hpp"

#include <optional>
#include <stdexcept>
#include <type_traits>
#include <utility>
ax3l marked this conversation as resolved.
Show resolved Hide resolved

#if openPMD_HAVE_MPI
#include <mpi.h>
#endif

namespace openPMD::auxiliary
{
#if openPMD_HAVE_MPI

using Mock_MPI_Comm = MPI_Comm;
ax3l marked this conversation as resolved.
Show resolved Hide resolved

namespace
{
template <typename>
struct MPI_Types;
Copy link
Member

@ax3l ax3l Sep 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Future refactoring: since this is an openPMD internal type trait, we should consider renaming this so it does not collide with the MPI_.... prefix used as convention in the MPI C libraries for all its functions.

Suggestions: MPI_Types -> openPMD_MPI_Type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this, I'm asking myself if this code is even correct. If an MPI application runs on a heterogeneous system with differing CPU architectures, there will be undefined behavior, no?
Maybe, we should just use the largest numeric type for those cases? Or is there a better way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This depends a bit and would require more work, but I think not in the location here.

heterogeneous system with differing CPU architectures

The questions here is not really the CPU (micro) architecture but the data model: https://en.cppreference.com/w/cpp/language/types Essentially all modern systems you would put on an HPC system are LP64.

If you would like to run with MPI on a system with heterogeneous hosts, you would have to compile your binaries for each of them and spawn them with MPI. That's the easy part, the hard part would be if someone ever implemented an MPI that would allow to communicate between LP64 and, let's say ILP32, or LP64 with big and little endian.

Either way, it would not change the local code in this location.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can actually simplify this by using static constexpr MPI_Datatype const value = ...;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably even make this a simpler constexpr function now, which might also benefit compile time.

Copy link
Contributor Author

@franzpoeschel franzpoeschel Sep 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constexpr will probably not work fully since the MPI datatypes are not constexpr in every implementation, but we can still replace the specialized class template with if constexpr, yeah.

If you would like to run with MPI on a system with heterogeneous hosts, you would have to compile your binaries for each of them and spawn them with MPI. That's the easy part, the hard part would be if someone ever implemented an MPI that would allow to communicate between LP64 and, let's say ILP32, or LP64 with big and little endian.

Either way, it would not change the local code in this location.

Fair enough, thanks for clarifying!


template <>
struct MPI_Types<unsigned long>
{
static MPI_Datatype const value;
};

template <>
struct MPI_Types<unsigned long long>
{
static MPI_Datatype const value;
};

template <>
struct MPI_Types<unsigned>
{
static MPI_Datatype const value;
};

template <>
struct MPI_Types<char>
{
static MPI_Datatype const value;
};

/*
* Only some of these are actually instanciated,
* so suppress warnings for the others.
*/
[[maybe_unused]] MPI_Datatype const MPI_Types<unsigned>::value =
MPI_UNSIGNED;
[[maybe_unused]] MPI_Datatype const MPI_Types<unsigned long>::value =
MPI_UNSIGNED_LONG;
[[maybe_unused]] MPI_Datatype const MPI_Types<unsigned long long>::value =
MPI_UNSIGNED_LONG_LONG;
[[maybe_unused]] MPI_Datatype const MPI_Types<char>::value = MPI_CHAR;
} // namespace

#endif
} // namespace openPMD::auxiliary
7 changes: 7 additions & 0 deletions src/IO/ADIOS/ADIOS1IOHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ std::future<void> ADIOS1IOHandlerImpl::flush()
deref_dynamic_cast<Parameter<Operation::CREATE_FILE> >(
i.parameter.get()));
break;
case O::CHECK_FILE:
checkFile(
i.writable,
deref_dynamic_cast<Parameter<Operation::CHECK_FILE> >(
i.parameter.get()));
break;
case O::CREATE_PATH:
createPath(
i.writable,
Expand Down Expand Up @@ -346,6 +352,7 @@ void ADIOS1IOHandler::enqueue(IOTask const &i)
switch (i.operation)
{
case Operation::CREATE_FILE:
case Operation::CHECK_FILE:
case Operation::CREATE_PATH:
case Operation::OPEN_PATH:
case Operation::CREATE_DATASET:
Expand Down
Loading