Alexandria  2.27.0
SDC-CH common library for the Euclid project
NpyMmap.icpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2022 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_source> MappedStream;
30 
31 template <typename T>
32 NdArray<T> mmapNpy(const boost::filesystem::path& path, boost::iostreams::mapped_file_base::mapmode mode,
33  size_t max_size) {
34  std::string dtype;
35  size_t n_elements = 0;
36  std::vector<size_t> shape;
37  std::vector<std::string> attrs;
38 
39  boost::iostreams::mapped_file_params map_params;
40  map_params.path = path.native();
41  map_params.flags = mode;
42  max_size = std::max(boost::uintmax_t{max_size}, boost::filesystem::file_size(path));
43  map_params.length = max_size;
44 
45  boost::iostreams::mapped_file input(map_params);
46  MappedStream stream(input.operator boost::iostreams::mapped_file_source&());
47  stream.set_auto_close(false);
48  readNpyHeader(stream, dtype, shape, attrs, n_elements);
49 
50  if (dtype != NpyDtype<T>::str)
51  throw Elements::Exception() << "Can not cast " << dtype << " into " << typeid(T).name();
52 
53  if (!attrs.empty()) {
54  n_elements *= attrs.size();
55  }
56 
57  return {shape, attrs,
58  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,
91  std::move(MappedContainer<T>(path, header_size, n_elements, attrs, std::move(output), max_size))};
92 }
93 
94 } // end of namespace NdArray
95 } // end of namespace Euclid
96 
97 #endif // NPYMMAP_IMPL