-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/eckit sparsematrix allocators reorganization, new in-place allocator #148
Open
pmaciel
wants to merge
4
commits into
develop
Choose a base branch
from
feature/eckit-sparsematrix-allocators
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
db5aecc
eckit::linalg::SparseMatrix::Allocator reorganize
pmaciel 1d9c8f1
eckit::linalg::SparseMatrix::Allocator new InPlaceAllocator
pmaciel cfcb66c
eckit::linalg::SparseMatrix::Allocator new InPlaceAllocator tests
pmaciel e59c541
eckit::linalg::SparseMatrix::Allocator #pragma once
pmaciel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* (C) Copyright 1996- ECMWF. | ||
* | ||
* This software is licensed under the terms of the Apache Licence Version 2.0 | ||
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
* In applying this licence, ECMWF does not waive the privileges and immunities | ||
* granted to it by virtue of its status as an intergovernmental organisation | ||
* nor does it submit to any jurisdiction. | ||
*/ | ||
|
||
|
||
#include "eckit/linalg/allocator/BufferAllocator.h" | ||
|
||
#include <ostream> | ||
|
||
#include "eckit/log/Bytes.h" | ||
|
||
|
||
namespace eckit::linalg::allocator { | ||
|
||
|
||
BufferAllocator::BufferAllocator(const MemoryBuffer& buffer) : buffer_(buffer, buffer.size()) {} | ||
|
||
|
||
SparseMatrix::Layout BufferAllocator::allocate(SparseMatrix::Shape& shape) { | ||
SparseMatrix::Layout layout; | ||
SparseMatrix::load(buffer_.data(), buffer_.size(), layout, shape); | ||
return layout; | ||
} | ||
|
||
|
||
void BufferAllocator::deallocate(SparseMatrix::Layout, SparseMatrix::Shape) {} | ||
|
||
|
||
bool BufferAllocator::inSharedMemory() const { | ||
return false; | ||
} | ||
|
||
|
||
void BufferAllocator::print(std::ostream& out) const { | ||
out << "BufferAllocator[" << Bytes{static_cast<double>(buffer_.size())} << "]"; | ||
} | ||
|
||
|
||
} // namespace eckit::linalg::allocator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* (C) Copyright 1996- ECMWF. | ||
* | ||
* This software is licensed under the terms of the Apache Licence Version 2.0 | ||
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
* In applying this licence, ECMWF does not waive the privileges and immunities | ||
* granted to it by virtue of its status as an intergovernmental organisation | ||
* nor does it submit to any jurisdiction. | ||
*/ | ||
|
||
|
||
#pragma once | ||
|
||
#include "eckit/linalg/SparseMatrix.h" | ||
|
||
#include "eckit/memory/MemoryBuffer.h" | ||
|
||
|
||
namespace eckit::linalg::allocator { | ||
|
||
|
||
class BufferAllocator : public SparseMatrix::Allocator { | ||
public: | ||
BufferAllocator(const MemoryBuffer&); | ||
|
||
SparseMatrix::Layout allocate(SparseMatrix::Shape&) override; | ||
|
||
void deallocate(SparseMatrix::Layout, SparseMatrix::Shape) override; | ||
bool inSharedMemory() const override; | ||
void print(std::ostream&) const override; | ||
|
||
MemoryBuffer buffer_; | ||
}; | ||
|
||
|
||
} // namespace eckit::linalg::allocator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* (C) Copyright 2024- ECMWF. | ||
* | ||
* This software is licensed under the terms of the Apache Licence Version 2.0 | ||
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
* In applying this licence, ECMWF does not waive the privileges and immunities | ||
* granted to it by virtue of its status as an intergovernmental organisation | ||
* nor does it submit to any jurisdiction. | ||
*/ | ||
|
||
|
||
#include "eckit/linalg/allocator/InPlaceAllocator.h" | ||
|
||
#include <ostream> | ||
|
||
#include "eckit/exception/Exceptions.h" | ||
|
||
|
||
namespace eckit::linalg::allocator { | ||
|
||
|
||
InPlaceAllocator::InPlaceAllocator(Size Nr, Size Nc, Size nnz, Index* ia, Index* ja, Scalar* a) : | ||
Nr_(Nr), Nc_(Nc), nnz_(nnz), ia_(ia), ja_(ja), a_(a) { | ||
ASSERT(ia_ != nullptr); | ||
ASSERT(ja_ != nullptr); | ||
ASSERT(a_ != nullptr); | ||
} | ||
|
||
|
||
SparseMatrix::Layout InPlaceAllocator::InPlaceAllocator::allocate(SparseMatrix::Shape& shape) { | ||
shape.size_ = nnz_; | ||
shape.rows_ = Nr_; | ||
shape.cols_ = Nc_; | ||
|
||
SparseMatrix::Layout layout; | ||
layout.outer_ = reinterpret_cast<decltype(SparseMatrix::Layout::outer_)>(ia_); | ||
layout.inner_ = ja_; | ||
layout.data_ = a_; | ||
|
||
return layout; | ||
} | ||
|
||
|
||
void InPlaceAllocator::deallocate(SparseMatrix::Layout, SparseMatrix::Shape) {} | ||
|
||
|
||
bool InPlaceAllocator::inSharedMemory() const { | ||
return false; | ||
} | ||
|
||
|
||
void InPlaceAllocator::print(std::ostream& out) const { | ||
out << "InPlaceAllocator[Nr=" << Nr_ << ",Nc=" << Nc_ << ",nnz=" << nnz_ << "]"; | ||
} | ||
|
||
|
||
} // namespace eckit::linalg::allocator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* (C) Copyright 2024- ECMWF. | ||
* | ||
* This software is licensed under the terms of the Apache Licence Version 2.0 | ||
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
* In applying this licence, ECMWF does not waive the privileges and immunities | ||
* granted to it by virtue of its status as an intergovernmental organisation | ||
* nor does it submit to any jurisdiction. | ||
*/ | ||
|
||
|
||
#pragma once | ||
|
||
#include "eckit/linalg/SparseMatrix.h" | ||
|
||
|
||
namespace eckit::linalg::allocator { | ||
|
||
|
||
/** | ||
* @brief In-place allocator for sparse matrices, directly mapping supporting arrays including from another | ||
* SparseMatrix. It is able to provide a "view". | ||
*/ | ||
class InPlaceAllocator : public SparseMatrix::Allocator { | ||
public: | ||
InPlaceAllocator(Size Nr, Size Nc, Size nnz, Index* ia, Index* ja, Scalar* a); | ||
|
||
SparseMatrix::Layout allocate(SparseMatrix::Shape&) override; | ||
|
||
void deallocate(SparseMatrix::Layout, SparseMatrix::Shape) override; | ||
bool inSharedMemory() const override; | ||
void print(std::ostream&) const override; | ||
|
||
private: | ||
const Size Nr_; | ||
const Size Nc_; | ||
const Size nnz_; | ||
Index* ia_; // NOTE: not owned | ||
Index* ja_; // NOTE: not owned | ||
Scalar* a_; // NOTE: not owned | ||
}; | ||
|
||
|
||
} // namespace eckit::linalg::allocator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
/* | ||
* (C) Copyright 1996- ECMWF. | ||
* | ||
* This software is licensed under the terms of the Apache Licence Version 2.0 | ||
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
* In applying this licence, ECMWF does not waive the privileges and immunities | ||
* granted to it by virtue of its status as an intergovernmental organisation | ||
* nor does it submit to any jurisdiction. | ||
*/ | ||
|
||
|
||
#include "eckit/linalg/allocator/StandardAllocator.h" | ||
|
||
#include <ostream> | ||
|
||
#include "eckit/log/Bytes.h" | ||
|
||
|
||
namespace eckit::linalg::allocator { | ||
|
||
|
||
StandardAllocator::StandardAllocator() : buffer_(0) {} | ||
|
||
|
||
SparseMatrix::Layout StandardAllocator::allocate(SparseMatrix::Shape& shape) { | ||
if (shape.allocSize() > buffer_.size()) { | ||
buffer_.resize(shape.allocSize()); | ||
} | ||
|
||
SparseMatrix::Layout layout; | ||
|
||
char* addr = buffer_; | ||
|
||
layout.data_ = reinterpret_cast<Scalar*>(addr); | ||
layout.outer_ = reinterpret_cast<SparseMatrix::UIndex*>(addr + shape.sizeofData()); | ||
layout.inner_ = reinterpret_cast<Index*>(addr + shape.sizeofData() + shape.sizeofOuter()); | ||
|
||
return layout; | ||
} | ||
|
||
|
||
void StandardAllocator::deallocate(SparseMatrix::Layout p, SparseMatrix::Shape) {} | ||
|
||
|
||
bool StandardAllocator::inSharedMemory() const { | ||
return false; | ||
} | ||
|
||
|
||
void StandardAllocator::print(std::ostream& out) const { | ||
out << "StandardAllocator[" << Bytes{static_cast<double>(buffer_.size())} << "]"; | ||
} | ||
|
||
|
||
} // namespace eckit::linalg::allocator |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe a reinterpret_cast or const_cast or any other casting should not be necessary.