WriteSimpleData.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <cstddef>
7 
10 #include "IO/H5/AccessType.hpp"
11 #include "IO/H5/File.hpp"
12 #include "IO/Observer/ArrayComponentId.hpp"
13 #include "IO/Observer/Tags.hpp"
15 #include "Parallel/Info.hpp"
16 #include "Parallel/Invoke.hpp"
17 #include "Parallel/NodeLock.hpp"
18 #include "Utilities/Requires.hpp"
19 #include "Utilities/TMPL.hpp"
20 
21 namespace observers {
22 namespace ThreadedActions {
23 
24 /*!
25  * \brief Append data to an h5::Dat subfile of `Tags::VolumeFileName`.
26  *
27  * \details This is a streamlined interface for getting data to the volume file
28  * associated with a node; it will simply write the `.dat` object
29  * `subfile_name`, giving it the `file_legend` if it does not yet exist,
30  * appending `data_row` to the end of the dat.
31  */
33  template <
34  typename ParallelComponent, typename DbTagsList, typename Metavariables,
35  typename ArrayIndex,
37  static void apply(db::DataBox<DbTagsList>& box,
39  const ArrayIndex& /*array_index*/,
40  const gsl::not_null<CmiNodeLock*> node_lock,
41  const std::vector<std::string>& file_legend,
42  const std::vector<double>& data_row,
43  const std::string& subfile_name) noexcept {
44  Parallel::lock(node_lock);
45  CmiNodeLock file_lock;
46  db::mutate<Tags::H5FileLock>(
47  make_not_null(&box), [&file_lock](const gsl::not_null<CmiNodeLock*>
48  in_file_lock) noexcept {
49  file_lock = *in_file_lock;
50  });
51  Parallel::unlock(node_lock);
52 
53  Parallel::lock(&file_lock);
54  // scoped to close file
55  {
56  const auto& file_prefix = Parallel::get<Tags::VolumeFileName>(cache);
58  file_prefix + std::to_string(Parallel::my_node()) + ".h5", true);
59  const size_t version_number = 0;
60  auto& output_dataset =
61  h5file.try_insert<h5::Dat>(subfile_name, file_legend, version_number);
62  output_dataset.append(data_row);
63  h5file.close_current_object();
64  }
65  Parallel::unlock(&file_lock);
66  }
67 };
68 } // namespace ThreadedActions
69 } // namespace observers
Definition: Actions.hpp:21
Defines functions for interfacing with the parallelization framework.
Defines class h5::H5File.
Represents a multicolumn dat file inside an HDF5 file.
Definition: Dat.hpp:42
Append data to an h5::Dat subfile of Tags::VolumeFileName.
Definition: WriteSimpleData.hpp:32
Defines the type alias Requires.
int my_node()
Index of my node.
Definition: Info.hpp:34
void unlock(const gsl::not_null< CmiNodeLock *> node_lock) noexcept
Unlock a converse CmiNodeLock.
Definition: NodeLock.hpp:62
HIDDEN_SYMBOLS void append(const std::vector< double > &data)
Definition: Dat.cpp:154
Defines enum for specifying whether the H5 file is ReadWrite or ReadOnly.
Defines classes and functions used for manipulating DataBox&#39;s.
Definition: InterpolationTargetWedgeSectionTorus.hpp:25
A Charm++ chare that caches constant data once per Charm++ node.
Definition: ConstGlobalCache.hpp:136
void lock(const gsl::not_null< CmiNodeLock *> node_lock) noexcept
Lock a converse CmiNodeLock.
Definition: NodeLock.hpp:34
Wraps the template metaprogramming library used (brigand)
gsl::not_null< T * > make_not_null(T *ptr) noexcept
Construct a not_null from a pointer. Often this will be done as an implicit conversion, but it may be necessary to perform the conversion explicitly when type deduction is desired.
Definition: Gsl.hpp:879
typename Requires_detail::requires_impl< B >::template_error_type_failed_to_meet_requirements_on_template_parameters Requires
Express requirements on the template parameters of a function or class, replaces std::enable_if_t ...
Definition: Requires.hpp:67
Defines classes SimpleTag, PrefixTag, ComputeTag and several functions for retrieving tag info...
Defines class template ConstGlobalCache.
Require a pointer to not be a nullptr
Definition: Gsl.hpp:182