File indexing completed on 2026-04-09 07:49:30
0001 #pragma once
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 #include <string>
0032 #include <vector>
0033 #include <sstream>
0034 #include <cassert>
0035 #include <cstring>
0036 #include <iostream>
0037 #include <iomanip>
0038
0039 struct salloc_item
0040 {
0041 uint64_t size ;
0042 uint64_t num_items ;
0043 uint64_t sizeof_item ;
0044 uint64_t spare ;
0045 };
0046
0047 #include "NP.hh"
0048
0049 struct salloc
0050 {
0051 static constexpr const char* ALLOC = "salloc.npy" ;
0052 static constexpr const char* RELDIR = "salloc" ;
0053
0054 std::string meta ;
0055 std::vector<std::string> label ;
0056 std::vector<salloc_item> alloc ;
0057 void add(const char* label, uint64_t num_items, uint64_t sizeof_item );
0058 uint64_t get_total() const ;
0059
0060 NP* make_array() const ;
0061
0062
0063 void save(const char* base, const char* reldir=RELDIR);
0064 void import(const NP* aa);
0065 static salloc* Load(const char* base, const char* reldir=RELDIR);
0066
0067 std::string desc() const ;
0068
0069 template<typename T> void set_meta(const char* key, T value);
0070 template<typename T> T get_meta(const char* key, T fallback) const ;
0071
0072
0073
0074 };
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091 inline void salloc::add( const char* label_, uint64_t num_items, uint64_t sizeof_item )
0092 {
0093 uint64_t size = num_items*sizeof_item ;
0094 uint64_t spare = 0 ;
0095 label.push_back(label_ );
0096 alloc.push_back( {size, num_items, sizeof_item, spare } );
0097 }
0098
0099 inline uint64_t salloc::get_total() const
0100 {
0101 uint64_t tot = 0 ;
0102 for(unsigned i=0 ; i < alloc.size() ; i++) tot += alloc[i].size ;
0103 return tot ;
0104 }
0105
0106
0107 inline NP* salloc::make_array() const
0108 {
0109 NP* a = NP::Make<uint64_t>( alloc.size(), 4 );
0110 a->read2<uint64_t>( (uint64_t*)alloc.data() );
0111 a->set_names( label );
0112 if(!meta.empty()) a->meta = meta ;
0113 return a ;
0114 }
0115 inline void salloc::save(const char* base, const char* reldir )
0116 {
0117 NP* a = make_array();
0118 a->save(base, reldir, ALLOC );
0119 }
0120
0121 inline void salloc::import(const NP* a)
0122 {
0123 assert( a );
0124 alloc.resize( a->shape[0] );
0125 memcpy( alloc.data(), a->bytes(), a->arr_bytes() );
0126 a->get_names(label);
0127 if(!a->meta.empty()) meta = a->meta ;
0128 }
0129
0130 inline salloc* salloc::Load(const char* base, const char* reldir )
0131 {
0132 NP* aa = NP::Load(base, reldir, ALLOC) ;
0133 if( aa == nullptr ) return nullptr ;
0134
0135
0136 salloc* a = new salloc ;
0137 a->import(aa) ;
0138 return a ;
0139 }
0140
0141 inline std::string salloc::desc() const
0142 {
0143 uint64_t tot = get_total() ;
0144 double tot_GB = double(tot)/1e9 ;
0145
0146 std::stringstream ss ;
0147 ss
0148 << "[salloc::desc"
0149 << " alloc.size " << alloc.size()
0150 << " label.size " << label.size()
0151 << "\n"
0152 ;
0153
0154 ss
0155 << "[salloc.meta\n"
0156 << ( meta.empty() ? "-" : meta )
0157 << "]salloc.meta\n"
0158 ;
0159
0160 ss
0161 << "[salloc.DescMetaKVS\n"
0162 << NP::DescMetaKVS(meta, nullptr, nullptr)
0163 << "]salloc.DescMetaKVS\n"
0164 ;
0165
0166 const char* spacer = " " ;
0167 ss
0168 << std::endl
0169 << spacer
0170 << "[" << std::setw(15) << "size"
0171 << " " << std::setw(11) << "num_items"
0172 << " " << std::setw(11) << "sizeof_item"
0173 << " " << std::setw(11) << "spare"
0174 << "]"
0175 << " " << std::setw(10) << "size_GB"
0176 << " " << std::setw(10) << "percent"
0177 << " " << "label"
0178 << std::endl
0179 << spacer
0180 << "[" << std::setw(15) << "(bytes)"
0181 << " " << std::setw(11) << ""
0182 << " " << std::setw(11) << ""
0183 << " " << std::setw(11) << ""
0184 << "]"
0185 << " " << std::setw(10) << "size/1e9"
0186 << " " << std::setw(10) << ""
0187 << " " << ""
0188 << std::endl
0189 << std::endl
0190 ;
0191
0192 assert( alloc.size() == label.size() );
0193 for(unsigned i=0 ; i < alloc.size() ; i++ )
0194 {
0195 const char* lab = label[i].c_str() ;
0196 const salloc_item& item = alloc[i] ;
0197 uint64_t _size = item.size ;
0198 uint64_t _num_items = item.num_items ;
0199 uint64_t _sizeof_item = item.sizeof_item ;
0200 uint64_t _spare = item.spare ;
0201 double size = double(_size) ;
0202 double size_GB = size/1e9 ;
0203 float size_percent = 100.*size/double(tot) ;
0204
0205 ss
0206 << spacer
0207 << "[" << std::setw(15) << _size
0208 << " " << std::setw(11) << _num_items
0209 << " " << std::setw(11) << _sizeof_item
0210 << " " << std::setw(11) << _spare
0211 << "]"
0212 << " " << std::setw(10) << std::fixed << std::setprecision(2) << size_GB
0213 << " " << std::setw(10) << std::fixed << std::setprecision(2) << size_percent
0214 << " " << lab
0215 << std::endl
0216 ;
0217 }
0218 ss << std::endl ;
0219 ss << " tot " << std::setw(15) << tot
0220 << " " << std::setw(11) << ""
0221 << " " << std::setw(11) << ""
0222 << " " << std::setw(11) << ""
0223 << " "
0224 << " " << std::setw(10) << std::fixed << std::setprecision(2) << tot_GB
0225 << "\n"
0226 << "]salloc::desc"
0227 << "\n"
0228 ;
0229 std::string s = ss.str();
0230 return s ;
0231 }
0232
0233 template<typename T>
0234 inline void salloc::set_meta(const char* key, T value)
0235 {
0236 NP::SetMeta<T>(meta, key, value );
0237 }
0238
0239 template<typename T>
0240 inline T salloc::get_meta(const char* key, T fallback) const
0241 {
0242 return NP::GetMeta<T>(meta, key, fallback );
0243 }
0244
0245
0246 template void salloc::set_meta<uint64_t>(const char*, uint64_t );
0247 template uint64_t salloc::get_meta<uint64_t>(const char*, uint64_t ) const;
0248
0249