Alexandria  2.19
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NpyMmap.icpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2021 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifdef NPYMMAP_IMPL
20 
21 #include "NpyCommon.h"
22 #include <boost/filesystem/path.hpp>
23 #include <boost/iostreams/stream.hpp>
24 #include <numeric>
25 
26 namespace Euclid {
27 namespace NdArray {
28 
29 typedef boost::iostreams::stream<boost::iostreams::mapped_file> MappedStream;
30 
31 template <typename T>
32 NdArray<T> mmapNpy(const boost::filesystem::path& path, boost::iostreams::mapped_file_base::mapmode mode, size_t max_size) {
33  std::string dtype;
34  size_t n_elements = 0;
35  std::vector<size_t> shape;
37 
38  boost::iostreams::mapped_file_params map_params;
39  map_params.path = path.native();
40  map_params.flags = mode;
41  if (max_size)
42  map_params.length = max_size;
43  else
44  max_size = boost::filesystem::file_size(path);
45 
46  boost::iostreams::mapped_file input(map_params);
47  MappedStream stream(input);
48  stream.set_auto_close(false);
49  readNpyHeader(stream, dtype, shape, attrs, n_elements);
50 
51  if (dtype != NpyDtype<T>::str)
52  throw Elements::Exception() << "Can not cast " << dtype << " into " << typeid(T).name();
53 
54  if (!attrs.empty()) {
55  n_elements *= attrs.size();
56  }
57 
58  return {shape, attrs, std::move(MappedContainer<T>(path, stream.tellg(), n_elements, attrs, std::move(input), max_size))};
59 }
60 
61 template <typename T>
62 NdArray<T> createMmapNpy(const boost::filesystem::path& path, const std::vector<size_t>& shape,
63  const std::vector<std::string>& attrs, size_t max_size) {
64  // Pre-generate header
65  std::stringstream header;
66  writeNpyHeader<T>(header, appendAttrShape(shape, attrs.size()), attrs);
67  auto header_str = header.str();
68  auto header_size = header_str.size();
69 
70  assert(header_size % 64 == 0);
71 
72  // Compute file expected size
73  size_t n_elements = std::accumulate(shape.begin(), shape.end(), 1, std::multiplies<size_t>());
74  if (!attrs.empty())
75  n_elements *= attrs.size();
76  size_t data_size = n_elements * sizeof(T);
77  size_t total_size = header_size + data_size;
78 
79  boost::iostreams::mapped_file_params map_params;
80  map_params.path = path.native();
81  map_params.flags = boost::iostreams::mapped_file_base::readwrite;
82  map_params.new_file_size = total_size;
83  if (max_size >= total_size)
84  map_params.length = max_size;
85  else
86  max_size = total_size;
87 
88  boost::iostreams::mapped_file output(map_params);
89  std::copy(header_str.begin(), header_str.end(), output.begin());
90  return {shape, attrs, std::move(MappedContainer<T>(path, header_size, n_elements, attrs, std::move(output), max_size))};
91 }
92 
93 } // end of namespace NdArray
94 } // end of namespace Euclid
95 
96 #endif // NPYMMAP_IMPL
void readNpyHeader(std::istream &input, std::string &dtype, std::vector< size_t > &shape, std::vector< std::string > &attrs, size_t &n_elements)
Definition: NpyCommon.h:203
T empty(T...args)
T copy(T...args)
T end(T...args)
STL class.
NdArray< T > mmapNpy(const boost::filesystem::path &path, boost::iostreams::mapped_file_base::mapmode mode=boost::iostreams::mapped_file_base::readwrite, size_t max_size=0)
NdArray< T > createMmapNpy(const boost::filesystem::path &path, const std::vector< size_t > &shape, const std::vector< std::string > &attr_names, size_t max_size=0)
T move(T...args)
T size(T...args)
T begin(T...args)
T accumulate(T...args)
Elements::Path::Item path