2323#include " eckit/exception/Exceptions.h"
2424#include " eckit/io/AutoCloser.h"
2525#include " eckit/io/MemoryHandle.h"
26- #include " eckit/log/Bytes.h"
26+ #include " eckit/linalg/allocator/BufferAllocator.h"
27+ #include " eckit/linalg/allocator/StandardAllocator.h"
2728#include " eckit/log/Log.h"
2829#include " eckit/memory/MemoryBuffer.h"
2930#include " eckit/serialisation/FileStream.h"
@@ -37,64 +38,6 @@ static_assert(sizeof(Index) == sizeof(SparseMatrix::UIndex), "sizeof(sizeof(Inde
3738static constexpr bool littleEndian = eckit_LITTLE_ENDIAN != 0 ;
3839
3940
40- // ----------------------------------------------------------------------------------------------------------------------
41-
42- namespace detail {
43-
44- class StandardAllocator : public SparseMatrix ::Allocator {
45- public:
46- StandardAllocator () : membuff_(0 ) {}
47-
48- SparseMatrix::Layout allocate (SparseMatrix::Shape& shape) override {
49- if (shape.allocSize () > membuff_.size ()) {
50- membuff_.resize (shape.allocSize ());
51- }
52-
53- SparseMatrix::Layout p;
54-
55- char * addr = membuff_;
56-
57- p.data_ = reinterpret_cast <Scalar*>(addr);
58- p.outer_ = reinterpret_cast <SparseMatrix::UIndex*>(addr + shape.sizeofData ());
59- p.inner_ = reinterpret_cast <Index*>(addr + shape.sizeofData () + shape.sizeofOuter ());
60-
61- return p;
62- }
63-
64- void deallocate (SparseMatrix::Layout p, SparseMatrix::Shape) override {}
65- bool inSharedMemory () const override { return false ; }
66- void print (std::ostream& out) const override {
67- out << " StandardAllocator[" << Bytes{static_cast <double >(membuff_.size ())} << " ]" ;
68- }
69-
70- MemoryBuffer membuff_;
71- };
72-
73-
74- class BufferAllocator : public SparseMatrix ::Allocator {
75- public:
76- BufferAllocator (const MemoryBuffer& buffer) : buffer_(buffer, buffer.size()) {}
77-
78- SparseMatrix::Layout allocate (SparseMatrix::Shape& shape) override {
79- SparseMatrix::Layout layout;
80-
81- SparseMatrix::load (buffer_.data (), buffer_.size (), layout, shape);
82-
83- return layout;
84- }
85-
86- void deallocate (SparseMatrix::Layout, SparseMatrix::Shape) override {}
87- bool inSharedMemory () const override { return false ; }
88- void print (std::ostream& out) const override {
89- out << " BufferAllocator[" << Bytes{static_cast <double >(buffer_.size ())} << " ]" ;
90- }
91-
92- MemoryBuffer buffer_;
93- };
94-
95-
96- } // namespace detail
97-
9841// ----------------------------------------------------------------------------------------------------------------------
9942
10043void SparseMatrix::Shape::print (std::ostream& os) const {
@@ -105,19 +48,19 @@ void SparseMatrix::Shape::print(std::ostream& os) const {
10548}
10649
10750
108- SparseMatrix::SparseMatrix (Allocator* alloc) : owner_(alloc != nullptr ? alloc : new detail ::StandardAllocator) {
51+ SparseMatrix::SparseMatrix (Allocator* alloc) : owner_(alloc != nullptr ? alloc : new allocator ::StandardAllocator) {
10952 spm_ = owner_->allocate (shape_);
11053}
11154
11255
11356SparseMatrix::SparseMatrix (Size rows, Size cols, Allocator* alloc) :
114- owner_ (alloc != nullptr ? alloc : new detail ::StandardAllocator) {
57+ owner_ (alloc != nullptr ? alloc : new allocator ::StandardAllocator) {
11558 reserve (rows, cols, 1 );
11659}
11760
11861
11962SparseMatrix::SparseMatrix (Size rows, Size cols, const std::vector<Triplet>& triplets) :
120- owner_ (new detail ::StandardAllocator) {
63+ owner_ (new allocator ::StandardAllocator) {
12164
12265 // Count number of non-zeros, allocate memory 1 triplet per non-zero
12366 Size nnz = std::count_if (triplets.begin (), triplets.end (), [](const auto & tri) { return tri.nonZero (); });
@@ -163,17 +106,17 @@ SparseMatrix::SparseMatrix(Size rows, Size cols, const std::vector<Triplet>& tri
163106}
164107
165108
166- SparseMatrix::SparseMatrix (Stream& s) : owner_(new detail ::StandardAllocator) {
109+ SparseMatrix::SparseMatrix (Stream& s) : owner_(new allocator ::StandardAllocator) {
167110 decode (s);
168111}
169112
170113
171- SparseMatrix::SparseMatrix (const MemoryBuffer& buffer) : owner_(new detail ::BufferAllocator(buffer)) {
114+ SparseMatrix::SparseMatrix (const MemoryBuffer& buffer) : owner_(new allocator ::BufferAllocator(buffer)) {
172115 spm_ = owner_->allocate (shape_);
173116}
174117
175118
176- SparseMatrix::SparseMatrix (const SparseMatrix& other) : owner_(new detail ::StandardAllocator) {
119+ SparseMatrix::SparseMatrix (const SparseMatrix& other) : owner_(new allocator ::StandardAllocator) {
177120 if (!other.empty ()) { // in case we copy an other that was constructed empty
178121
179122 reserve (other.rows (), other.cols (), other.nonZeros ());
@@ -539,7 +482,7 @@ void SparseMatrix::decode(Stream& s) {
539482
540483 reset ();
541484
542- owner_ = std::make_unique<detail ::StandardAllocator>();
485+ owner_ = std::make_unique<allocator ::StandardAllocator>();
543486
544487 reserve (rows, cols, nnz);
545488
0 commit comments