-
Notifications
You must be signed in to change notification settings - Fork 94
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
Add a batch::BatchLinOp hierarchy and core tests #1379
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3b92d76
Add a batch::BatchLinOp hierarchy and core tests
pratikvn 0c2ead9
Review updates.
pratikvn cd2b5de
Remove apply functionality from BatchLinOp
pratikvn 6663701
Update docs
pratikvn e22eb64
Remove BatchLinOp apply log events
pratikvn bb0a194
Format files
ginkgo-bot 35ff225
add common size getter
pratikvn 390c26c
Move lin_op_helpers back to lin_op
pratikvn aa45d0c
Review updates
pratikvn 5407a60
Format files
ginkgo-bot cc626b0
Add batch_linop_fac mask to logger.
pratikvn 175db29
Review updates
pratikvn b5ff8ca
Doc clarifications
pratikvn 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,233 @@ | ||
/*******************************<GINKGO LICENSE>****************************** | ||
Copyright (c) 2017-2023, the Ginkgo authors | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions | ||
are met: | ||
|
||
1. Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
|
||
2. Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in the | ||
documentation and/or other materials provided with the distribution. | ||
|
||
3. Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
******************************<GINKGO LICENSE>*******************************/ | ||
|
||
#include <ginkgo/core/base/batch_lin_op.hpp> | ||
|
||
|
||
#include <complex> | ||
#include <memory> | ||
#include <type_traits> | ||
|
||
|
||
#include <gtest/gtest.h> | ||
|
||
|
||
#include <ginkgo/core/base/executor.hpp> | ||
#include <ginkgo/core/base/lin_op.hpp> | ||
#include <ginkgo/core/base/math.hpp> | ||
#include <ginkgo/core/log/logger.hpp> | ||
|
||
|
||
namespace { | ||
|
||
|
||
struct DummyLogger : gko::log::Logger { | ||
DummyLogger() | ||
: gko::log::Logger(gko::log::Logger::batch_linop_factory_events_mask) | ||
{} | ||
|
||
void on_batch_linop_factory_generate_started( | ||
const gko::batch::BatchLinOpFactory*, | ||
const gko::batch::BatchLinOp*) const override | ||
{ | ||
batch_linop_factory_generate_started++; | ||
} | ||
|
||
void on_batch_linop_factory_generate_completed( | ||
const gko::batch::BatchLinOpFactory*, const gko::batch::BatchLinOp*, | ||
const gko::batch::BatchLinOp*) const override | ||
{ | ||
batch_linop_factory_generate_completed++; | ||
} | ||
|
||
int mutable batch_linop_factory_generate_started = 0; | ||
int mutable batch_linop_factory_generate_completed = 0; | ||
}; | ||
|
||
|
||
class DummyBatchLinOp : public gko::batch::EnableBatchLinOp<DummyBatchLinOp>, | ||
public gko::EnableCreateMethod<DummyBatchLinOp> { | ||
public: | ||
DummyBatchLinOp(std::shared_ptr<const gko::Executor> exec, | ||
gko::batch_dim<2> size = gko::batch_dim<2>{}) | ||
: gko::batch::EnableBatchLinOp<DummyBatchLinOp>(exec, size) | ||
{} | ||
}; | ||
|
||
|
||
class EnableBatchLinOp : public ::testing::Test { | ||
protected: | ||
EnableBatchLinOp() | ||
: ref{gko::ReferenceExecutor::create()}, | ||
op{DummyBatchLinOp::create(ref, | ||
gko::batch_dim<2>(1, gko::dim<2>{3, 5}))} | ||
{} | ||
|
||
std::shared_ptr<const gko::ReferenceExecutor> ref; | ||
std::unique_ptr<DummyBatchLinOp> op; | ||
}; | ||
|
||
|
||
TEST_F(EnableBatchLinOp, KnowsNumBatchItems) | ||
{ | ||
ASSERT_EQ(op->get_num_batch_items(), 1); | ||
} | ||
|
||
|
||
TEST_F(EnableBatchLinOp, KnowsItsSizes) | ||
{ | ||
auto op1_sizes = gko::batch_dim<2>(1, gko::dim<2>{3, 5}); | ||
ASSERT_EQ(op->get_size(), op1_sizes); | ||
} | ||
|
||
|
||
template <typename T = int> | ||
class DummyBatchLinOpWithFactory | ||
: public gko::batch::EnableBatchLinOp<DummyBatchLinOpWithFactory<T>> { | ||
public: | ||
DummyBatchLinOpWithFactory(std::shared_ptr<const gko::Executor> exec) | ||
: gko::batch::EnableBatchLinOp<DummyBatchLinOpWithFactory>(exec) | ||
{} | ||
|
||
GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory) | ||
{ | ||
T GKO_FACTORY_PARAMETER_SCALAR(value, T{5}); | ||
}; | ||
GKO_ENABLE_BATCH_LIN_OP_FACTORY(DummyBatchLinOpWithFactory, parameters, | ||
Factory); | ||
GKO_ENABLE_BUILD_METHOD(Factory); | ||
|
||
DummyBatchLinOpWithFactory(const Factory* factory, | ||
std::shared_ptr<const gko::batch::BatchLinOp> op) | ||
: gko::batch::EnableBatchLinOp<DummyBatchLinOpWithFactory>( | ||
factory->get_executor()), | ||
parameters_{factory->get_parameters()}, | ||
op_{op} | ||
{} | ||
|
||
std::shared_ptr<const gko::batch::BatchLinOp> op_; | ||
}; | ||
|
||
|
||
class EnableBatchLinOpFactory : public ::testing::Test { | ||
protected: | ||
EnableBatchLinOpFactory() | ||
: ref{gko::ReferenceExecutor::create()}, | ||
logger{std::make_shared<DummyLogger>()} | ||
|
||
{} | ||
|
||
std::shared_ptr<const gko::ReferenceExecutor> ref; | ||
std::shared_ptr<DummyLogger> logger; | ||
}; | ||
|
||
|
||
TEST_F(EnableBatchLinOpFactory, CreatesDefaultFactory) | ||
{ | ||
auto factory = DummyBatchLinOpWithFactory<>::build().on(ref); | ||
|
||
ASSERT_EQ(factory->get_parameters().value, 5); | ||
ASSERT_EQ(factory->get_executor(), ref); | ||
} | ||
|
||
|
||
TEST_F(EnableBatchLinOpFactory, CreatesFactoryWithParameters) | ||
{ | ||
auto factory = DummyBatchLinOpWithFactory<>::build().with_value(7).on(ref); | ||
|
||
ASSERT_EQ(factory->get_parameters().value, 7); | ||
ASSERT_EQ(factory->get_executor(), ref); | ||
} | ||
|
||
|
||
TEST_F(EnableBatchLinOpFactory, PassesParametersToBatchLinOp) | ||
{ | ||
auto dummy = gko::share( | ||
DummyBatchLinOp::create(ref, gko::batch_dim<2>(1, gko::dim<2>{3, 5}))); | ||
auto factory = DummyBatchLinOpWithFactory<>::build().with_value(6).on(ref); | ||
|
||
auto op = factory->generate(dummy); | ||
|
||
ASSERT_EQ(op->get_executor(), ref); | ||
ASSERT_EQ(op->get_parameters().value, 6); | ||
ASSERT_EQ(op->op_.get(), dummy.get()); | ||
} | ||
|
||
pratikvn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
TEST_F(EnableBatchLinOpFactory, FactoryGenerateIsLogged) | ||
{ | ||
auto before_logger = *logger; | ||
auto factory = DummyBatchLinOpWithFactory<>::build().on(ref); | ||
factory->add_logger(logger); | ||
factory->generate( | ||
DummyBatchLinOp::create(ref, gko::batch_dim<2>(1, gko::dim<2>{3, 5}))); | ||
|
||
ASSERT_EQ(logger->batch_linop_factory_generate_started, | ||
before_logger.batch_linop_factory_generate_started + 1); | ||
ASSERT_EQ(logger->batch_linop_factory_generate_completed, | ||
before_logger.batch_linop_factory_generate_completed + 1); | ||
} | ||
|
||
|
||
TEST_F(EnableBatchLinOpFactory, WithLoggersWorksAndPropagates) | ||
{ | ||
auto before_logger = *logger; | ||
auto factory = | ||
DummyBatchLinOpWithFactory<>::build().with_loggers(logger).on(ref); | ||
auto op = factory->generate( | ||
DummyBatchLinOp::create(ref, gko::batch_dim<2>(1, gko::dim<2>{3, 5}))); | ||
|
||
ASSERT_EQ(logger->batch_linop_factory_generate_started, | ||
before_logger.batch_linop_factory_generate_started + 1); | ||
ASSERT_EQ(logger->batch_linop_factory_generate_completed, | ||
before_logger.batch_linop_factory_generate_completed + 1); | ||
} | ||
|
||
|
||
TEST_F(EnableBatchLinOpFactory, CopiesLinOpToOtherExecutor) | ||
{ | ||
auto ref2 = gko::ReferenceExecutor::create(); | ||
auto dummy = gko::share( | ||
DummyBatchLinOp::create(ref2, gko::batch_dim<2>(1, gko::dim<2>{3, 5}))); | ||
auto factory = DummyBatchLinOpWithFactory<>::build().with_value(6).on(ref); | ||
|
||
auto op = factory->generate(dummy); | ||
|
||
ASSERT_EQ(op->get_executor(), ref); | ||
ASSERT_EQ(op->get_parameters().value, 6); | ||
ASSERT_EQ(op->op_->get_executor(), ref); | ||
ASSERT_NE(op->op_.get(), dummy.get()); | ||
ASSERT_TRUE(dynamic_cast<const DummyBatchLinOp*>(op->op_.get())); | ||
} | ||
|
||
|
||
} // namespace |
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.
nit: because it is a public member
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 think it is okay in the tests here as it makes it easier to differentiate that it is a member of the
op
object.