Skip to content

Commit 04b635a

Browse files
authored
Merge pull request #516 from ValeevGroup/evaleev/feature/size-of
`TA::size_of`
2 parents 2cb5ae1 + 131ed04 commit 04b635a

File tree

17 files changed

+340
-13
lines changed

17 files changed

+340
-13
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
CCACHE_COMPRESS : true
3232
CCACHE_COMPRESSLEVEL : 6
3333
OMPI_MCA_btl_vader_single_copy_mechanism : none
34-
PARSEC_MCA_runtime_bind_threads : 0
34+
PARSEC_MCA_bind_threads : 0
3535
BUILD_CONFIG : >
3636
-DMADNESS_TASK_BACKEND=${{ matrix.task_backend }}
3737
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ TiledArray/error.h
3838
TiledArray/initialize.h
3939
TiledArray/perm_index.h
4040
TiledArray/permutation.h
41+
TiledArray/platform.h
4142
TiledArray/proc_grid.h
4243
TiledArray/range.h
4344
TiledArray/range1.h
@@ -239,7 +240,6 @@ if(TILEDARRAY_HAS_HIP OR TILEDARRAY_HAS_CUDA)
239240
TiledArray/device/kernel/reduce_kernel.h
240241
TiledArray/device/kernel/thrust/mult_kernel.h
241242
TiledArray/device/kernel/thrust/reduce_kernel.h
242-
TiledArray/device/platform.h
243243
TiledArray/device/thrust.h
244244
TiledArray/device/um_storage.h
245245
)

src/TiledArray/dense_shape.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
#define TILEDARRAY_DENSE_SHAPE_H__INCLUDED
2828

2929
#include <TiledArray/config.h>
30+
31+
#include <TiledArray/platform.h>
3032
#include <TiledArray/type_traits.h>
33+
3134
#include <cstdint>
3235

3336
namespace madness {
@@ -391,6 +394,11 @@ class DenseShape {
391394
std::numeric_limits<value_type>::epsilon();
392395
}; // class DenseShape
393396

397+
template <MemorySpace S>
398+
std::size_t size_of(const DenseShape& shape) {
399+
return sizeof(shape);
400+
}
401+
394402
constexpr inline bool operator==(const DenseShape& a, const DenseShape& b) {
395403
return true;
396404
}

src/TiledArray/device/btas.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737

3838
#include <TiledArray/device/kernel/mult_kernel.h>
3939
#include <TiledArray/device/kernel/reduce_kernel.h>
40-
#include <TiledArray/device/platform.h>
4140
#include <TiledArray/device/um_storage.h>
4241
#include <TiledArray/math/gemm_helper.h>
42+
#include <TiledArray/platform.h>
4343

4444
namespace TiledArray {
4545

src/TiledArray/device/cpu_cuda_vector.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
#include <btas/array_adaptor.h>
66

7-
#include <TiledArray/device/platform.h>
87
#include <TiledArray/device/thrust.h>
8+
#include <TiledArray/platform.h>
99

1010
#include <madness/world/archive.h>
1111

@@ -213,18 +213,18 @@ struct ArchiveLoadImpl<Archive, TiledArray::cpu_cuda_vector<T>> {
213213
static inline void load(const Archive& ar,
214214
TiledArray::cpu_cuda_vector<T>& x) {
215215
typename TiledArray::cpu_cuda_vector<T>::size_type n(0);
216-
ar& n;
216+
ar & n;
217217
x.resize(n);
218-
for (auto& xi : x) ar& xi;
218+
for (auto& xi : x) ar & xi;
219219
}
220220
};
221221

222222
template <class Archive, typename T>
223223
struct ArchiveStoreImpl<Archive, TiledArray::cpu_cuda_vector<T>> {
224224
static inline void store(const Archive& ar,
225225
const TiledArray::cpu_cuda_vector<T>& x) {
226-
ar& x.size();
227-
for (const auto& xi : x) ar& xi;
226+
ar & x.size();
227+
for (const auto& xi : x) ar & xi;
228228
}
229229
};
230230

src/TiledArray/device/um_storage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include <btas/array_adaptor.h>
3232
#include <btas/varray/varray.h>
3333

34-
#include <TiledArray/device/platform.h>
34+
#include <TiledArray/platform.h>
3535
#include <TiledArray/utility.h>
3636

3737
#include <madness/world/archive.h>

src/TiledArray/dist_array.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,20 @@ class DistArray : public madness::archive::ParallelSerializableObject {
17431743

17441744
}; // class DistArray
17451745

1746+
/// \return the approximate number of bytes used by \p t in this rank's
1747+
/// memory space `S`
1748+
/// \note this does not account for the TiledRange and some other metadata
1749+
template <MemorySpace S, typename Tile, typename Policy>
1750+
std::size_t size_of(const DistArray<Tile, Policy>& da) {
1751+
std::size_t result = 0;
1752+
result += size_of<S>(da.shape());
1753+
// add up local tile's contributions
1754+
for (const auto& tile_ref : da) {
1755+
result += size_of<S>(tile_ref.get());
1756+
}
1757+
return result;
1758+
}
1759+
17461760
#ifndef TILEDARRAY_HEADER_ONLY
17471761

17481762
extern template class DistArray<Tensor<double>, DensePolicy>;
Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@
2121
*
2222
*/
2323

24-
#ifndef TILEDARRAY_DEVICE_PLATFORM_H__INCLUDED
25-
#define TILEDARRAY_DEVICE_PLATFORM_H__INCLUDED
24+
#ifndef TILEDARRAY_PLATFORM_H__INCLUDED
25+
#define TILEDARRAY_PLATFORM_H__INCLUDED
26+
27+
#include <TiledArray/fwd.h>
28+
29+
#include <TiledArray/type_traits.h>
2630

2731
namespace TiledArray {
2832

@@ -54,6 +58,40 @@ constexpr bool overlap(MemorySpace space1, MemorySpace space2) {
5458
return (space1 & space2) != MemorySpace::Null;
5559
}
5660

61+
// customization point: is_constexpr_size_of_v<S,T> reports whether
62+
// size_of<S>(T) is the same for all T
63+
template <MemorySpace S, typename T>
64+
inline constexpr bool is_constexpr_size_of_v = detail::is_numeric_v<T>;
65+
66+
// customization point: size_of<S>(O) -> std::size_t reports the number of
67+
// bytes occupied by O in S
68+
template <MemorySpace S, typename T,
69+
typename = std::enable_if_t<is_constexpr_size_of_v<S, T>>>
70+
constexpr std::size_t size_of(const T& t) {
71+
return sizeof(T);
72+
}
73+
74+
// customization point: allocates_memory_space<S>(A) -> bool reports whether
75+
// allocator A allocates memory in space S
76+
template <MemorySpace S, typename T>
77+
constexpr bool allocates_memory_space(const std::allocator<T>& a) {
78+
return S == MemorySpace::Host;
79+
}
80+
template <MemorySpace S, typename T>
81+
constexpr bool allocates_memory_space(const Eigen::aligned_allocator<T>& a) {
82+
return S == MemorySpace::Host;
83+
}
84+
template <MemorySpace S, typename T>
85+
constexpr bool allocates_memory_space(const host_allocator<T>& a) {
86+
return S == MemorySpace::Host;
87+
}
88+
#ifdef TILEDARRAY_HAS_DEVICE
89+
template <MemorySpace S, typename T>
90+
constexpr bool allocates_memory_space(const device_um_allocator<T>& a) {
91+
return S == MemorySpace::Device_UM;
92+
}
93+
#endif
94+
5795
/// enumerates the execution spaces
5896
enum class ExecutionSpace { Host, Device };
5997

@@ -62,4 +100,4 @@ enum class ExecutionSpace { Host, Device };
62100

63101
} // namespace TiledArray
64102

65-
#endif // TILEDARRAY_DEVICE_PLATFORM_H__INCLUDED
103+
#endif // TILEDARRAY_PLATFORM_H__INCLUDED

src/TiledArray/range.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define TILEDARRAY_RANGE_H__INCLUDED
2222

2323
#include <TiledArray/permutation.h>
24+
#include <TiledArray/platform.h>
2425
#include <TiledArray/range1.h>
2526
#include <TiledArray/range_iterator.h>
2627
#include <TiledArray/size_array.h>
@@ -1247,6 +1248,20 @@ class Range {
12471248
return ordinal(last) - ordinal(first);
12481249
}
12491250

1251+
template <MemorySpace S>
1252+
friend constexpr std::size_t size_of(const Range& r) {
1253+
std::size_t sz = 0;
1254+
if constexpr (S == MemorySpace::Host) {
1255+
sz += sizeof(r);
1256+
}
1257+
// correct for optional dynamic allocation of datavec_
1258+
if constexpr (S == MemorySpace::Host) {
1259+
sz -= sizeof(r.datavec_);
1260+
}
1261+
sz += size_of<S>(r.datavec_);
1262+
return sz;
1263+
}
1264+
12501265
}; // class Range
12511266

12521267
// lift Range::index_type and Range::index_view_type into user-land

src/TiledArray/range1.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <boost/iterator/iterator_facade.hpp>
2727

2828
#include <TiledArray/error.h>
29+
#include <TiledArray/platform.h>
2930

3031
namespace TiledArray {
3132

@@ -138,7 +139,7 @@ struct Range1 {
138139
/// \brief dereferences this iterator
139140
/// \return const reference to the current index
140141
const auto& dereference() const { return v; }
141-
};
142+
}; // class Iterator
142143
friend class Iterator;
143144

144145
typedef Iterator const_iterator; ///< Iterator type
@@ -201,6 +202,15 @@ struct Range1 {
201202
void serialize(Archive& ar) const {
202203
ar & first & second;
203204
}
205+
206+
template <MemorySpace S>
207+
friend constexpr std::size_t size_of(const Range1& r) {
208+
std::size_t sz = 0;
209+
if constexpr (S == MemorySpace::Host) {
210+
sz += sizeof(r);
211+
}
212+
return sz;
213+
}
204214
};
205215

206216
inline bool operator==(const Range1& x, const Range1& y) {

0 commit comments

Comments
 (0)