LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
cnpy_utils.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2023, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory.
4 // Written by the LBANN Research Team (B. Van Essen, et al.) listed in
5 // the CONTRIBUTORS file. <lbann-dev@llnl.gov>
6 //
7 // LLNL-CODE-697807.
8 // All rights reserved.
9 //
10 // This file is part of LBANN: Livermore Big Artificial Neural Network
11 // Toolkit. For details, see http://software.llnl.gov/LBANN or
12 // https://github.com/LLNL/LBANN.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "Licensee"); you
15 // may not use this file except in compliance with the License. You may
16 // obtain a copy of the License at:
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
23 // implied. See the License for the specific language governing
24 // permissions and limitations under the license.
26 
27 #ifndef _LBANN_CNPY_UTILS_HPP_
28 #define _LBANN_CNPY_UTILS_HPP_
29 
30 #include "cnpy.h"
32 #include <string>
33 #include <vector>
34 
35 namespace lbann {
36 namespace cnpy_utils {
37 
44 size_t compute_cnpy_array_offset(const cnpy::NpyArray& na,
45  std::vector<size_t> indices);
46 
58 template <typename T>
59 inline size_t ptr_offset(const cnpy::NpyArray& na, std::vector<size_t> indices)
60 {
61  if ((sizeof(T) != na.word_size) && (sizeof(T) != 1u)) {
62  throw lbann_exception(
63  std::string("cnpy_utils::ptr_offset() :") +
64  "The data type is not consistent with the word size of the array.");
65  }
66  return (compute_cnpy_array_offset(na, indices) *
67  ((sizeof(T) == 1u) ? na.word_size : 1u));
68 }
69 
74 template <typename T>
75 inline T& data(const cnpy::NpyArray& na, const std::vector<size_t> indices)
76 {
77  return *(reinterpret_cast<T*>(&(*na.data_holder)[0]) +
78  ptr_offset<T>(na, indices));
79 }
80 
85 template <typename T>
86 inline T* data_ptr(const cnpy::NpyArray& na, const std::vector<size_t> indices)
87 {
88  return (reinterpret_cast<T*>(&(*na.data_holder)[0]) +
89  ptr_offset<T>(na, indices));
90 }
91 
92 template <>
93 inline void* data_ptr<void>(const cnpy::NpyArray& na,
94  const std::vector<size_t> indices)
95 {
96  return data_ptr<uint8_t>(na, indices);
97 }
98 
103 void shrink_to_fit(cnpy::NpyArray& na, size_t sz);
104 
106 std::string show_shape(const cnpy::NpyArray& na);
107 
108 } // end of namespace cnpy_utils
109 } // end of namespace lbann
110 
111 #endif // _LBANN_CNPY_UTILS_HPP_
void shrink_to_fit(cnpy::NpyArray &na, size_t sz)
void * data_ptr< void >(const cnpy::NpyArray &na, const std::vector< size_t > indices)
Definition: cnpy_utils.hpp:93
T & data(const cnpy::NpyArray &na, const std::vector< size_t > indices)
Definition: cnpy_utils.hpp:75
size_t ptr_offset(const cnpy::NpyArray &na, std::vector< size_t > indices)
Definition: cnpy_utils.hpp:59
T * data_ptr(const cnpy::NpyArray &na, const std::vector< size_t > indices)
Definition: cnpy_utils.hpp:86
size_t compute_cnpy_array_offset(const cnpy::NpyArray &na, std::vector< size_t > indices)
exception lbann_exception
Definition: exception.hpp:145
std::string show_shape(const cnpy::NpyArray &na)
Show the dimensions of loaded data.