Skip to content

Commit

Permalink
Merge logger for mixin classes fix
Browse files Browse the repository at this point in the history
This fixes the logging done by mixin classes. Previously, classes derived from `EnablePolymorphicObject` and `EnableDefaultLinOpFactor` would not log their corresponding events correctly. 

Related PR: #1037
  • Loading branch information
MarcelKoch committed May 9, 2022
2 parents 04829b7 + d610a9f commit 374bd6f
Show file tree
Hide file tree
Showing 14 changed files with 638 additions and 111 deletions.
2 changes: 1 addition & 1 deletion core/test/base/abstract_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct IntFactory
base>::EnableDefaultFactory;
};

struct MyInt {
struct MyInt : gko::log::EnableLogging<MyInt> {
MyInt(const IntFactory* factory, int orig_value)
: value{orig_value * factory->get_parameters().coefficient}
{}
Expand Down
12 changes: 6 additions & 6 deletions core/test/base/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ TYPED_TEST(Array, CanBeMoveConstructedToADifferentExecutor)

TYPED_TEST(Array, MoveConstructedFromArrayExecutorlessIsEmpty)
{
gko::Array<TypeParam> a{std::move(this->x)};
gko::array<TypeParam> a{std::move(this->x)};

a = std::move(this->x);

Expand All @@ -211,7 +211,7 @@ TYPED_TEST(Array, MoveConstructedFromArrayExecutorlessIsEmpty)

TYPED_TEST(Array, MoveConstructedFromArraySameExecutorIsEmpty)
{
gko::Array<TypeParam> a{this->exec, std::move(this->x)};
gko::array<TypeParam> a{this->exec, std::move(this->x)};

ASSERT_EQ(this->x.get_executor(), this->exec);
ASSERT_EQ(this->x.get_num_elems(), 0);
Expand All @@ -220,7 +220,7 @@ TYPED_TEST(Array, MoveConstructedFromArraySameExecutorIsEmpty)

TYPED_TEST(Array, MoveConstructedFromArrayDifferentExecutorIsEmpty)
{
gko::Array<TypeParam> a{gko::ReferenceExecutor::create(),
gko::array<TypeParam> a{gko::ReferenceExecutor::create(),
std::move(this->x)};

ASSERT_EQ(this->x.get_executor(), this->exec);
Expand Down Expand Up @@ -297,7 +297,7 @@ TYPED_TEST(Array, CanBeMovedFromExecutorlessArray)

TYPED_TEST(Array, MovedFromArrayExecutorlessIsEmpty)
{
gko::Array<TypeParam> a;
gko::array<TypeParam> a;

a = std::move(this->x);

Expand All @@ -308,7 +308,7 @@ TYPED_TEST(Array, MovedFromArrayExecutorlessIsEmpty)

TYPED_TEST(Array, MovedFromArraySameExecutorIsEmpty)
{
gko::Array<TypeParam> a{this->exec};
gko::array<TypeParam> a{this->exec};

a = std::move(this->x);

Expand All @@ -319,7 +319,7 @@ TYPED_TEST(Array, MovedFromArraySameExecutorIsEmpty)

TYPED_TEST(Array, MovedFromArrayDifferentExecutorIsEmpty)
{
gko::Array<TypeParam> a{gko::ReferenceExecutor::create()};
gko::array<TypeParam> a{gko::ReferenceExecutor::create()};

a = std::move(this->x);

Expand Down
127 changes: 127 additions & 0 deletions core/test/base/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,4 +651,131 @@ TEST(ExecutorDeleter, AvoidsDeletionForNullExecutor)
}


struct DummyLogger : public gko::log::Logger {
DummyLogger(std::shared_ptr<const gko::Executor> exec)
: gko::log::Logger(std::move(exec),
gko::log::Logger::executor_events_mask |
gko::log::Logger::operation_events_mask)
{}

void on_allocation_started(const gko::Executor* exec,
const gko::size_type& num_bytes) const override
{
allocation_started++;
}
void on_allocation_completed(const gko::Executor* exec,
const gko::size_type& num_bytes,
const gko::uintptr& location) const override
{
allocation_completed++;
}

void on_free_started(const gko::Executor* exec,
const gko::uintptr& location) const override
{
free_started++;
}


void on_free_completed(const gko::Executor* exec,
const gko::uintptr& location) const override
{
free_completed++;
}

void on_copy_started(const gko::Executor* exec_from,
const gko::Executor* exec_to,
const gko::uintptr& loc_from,
const gko::uintptr& loc_to,
const gko::size_type& num_bytes) const override
{
copy_started++;
}

void on_copy_completed(const gko::Executor* exec_from,
const gko::Executor* exec_to,
const gko::uintptr& loc_from,
const gko::uintptr& loc_to,
const gko::size_type& num_bytes) const override
{
copy_completed++;
}

void on_operation_launched(const gko::Executor* exec,
const gko::Operation* op) const override
{
operation_launched++;
}


void on_operation_completed(const gko::Executor* exec,
const gko::Operation* op) const override
{
operation_completed++;
}

mutable int allocation_started = 0;
mutable int allocation_completed = 0;
mutable int free_started = 0;
mutable int free_completed = 0;
mutable int copy_started = 0;
mutable int copy_completed = 0;
mutable int operation_launched = 0;
mutable int operation_completed = 0;
};


class ExecutorLogging : public ::testing::Test {
protected:
ExecutorLogging()
: exec(gko::ReferenceExecutor::create()),
logger(std::make_shared<DummyLogger>(exec))
{
exec->add_logger(logger);
}

std::shared_ptr<gko::ReferenceExecutor> exec;
std::shared_ptr<DummyLogger> logger;
};


TEST_F(ExecutorLogging, LogsAllocationAndFree)
{
auto before_logger = *logger;

auto p = exec->alloc<int>(1);
exec->free(p);

ASSERT_EQ(logger->allocation_started, before_logger.allocation_started + 1);
ASSERT_EQ(logger->allocation_completed,
before_logger.allocation_completed + 1);
ASSERT_EQ(logger->free_started, before_logger.free_started + 1);
ASSERT_EQ(logger->free_completed, before_logger.free_completed + 1);
}


TEST_F(ExecutorLogging, LogsCopy)
{
auto before_logger = *logger;

exec->copy<std::nullptr_t>(0, nullptr, nullptr);

ASSERT_EQ(logger->copy_started, before_logger.copy_started + 1);
ASSERT_EQ(logger->copy_completed, before_logger.copy_completed + 1);
}


TEST_F(ExecutorLogging, LogsOperation)
{
auto before_logger = *logger;
int value = 0;

exec->run(ExampleOperation(value));

ASSERT_EQ(logger->operation_launched, before_logger.operation_launched + 1);
ASSERT_EQ(logger->operation_completed,
before_logger.operation_completed + 1);
}


} // namespace
126 changes: 123 additions & 3 deletions core/test/base/lin_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,68 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <gtest/gtest.h>


#include <ginkgo/core/base/executor.hpp>
#include <ginkgo/core/base/math.hpp>


namespace {


struct DummyLogger : gko::log::Logger {
DummyLogger(std::shared_ptr<const gko::Executor> exec)
: gko::log::Logger(std::move(exec),
gko::log::Logger::linop_events_mask |
gko::log::Logger::linop_factory_events_mask)
{}

void on_linop_apply_started(const gko::LinOp*, const gko::LinOp*,
const gko::LinOp*) const override
{
linop_apply_started++;
}

void on_linop_apply_completed(const gko::LinOp*, const gko::LinOp*,
const gko::LinOp*) const override
{
linop_apply_completed++;
}

void on_linop_advanced_apply_started(const gko::LinOp*, const gko::LinOp*,
const gko::LinOp*, const gko::LinOp*,
const gko::LinOp*) const override
{
linop_advanced_apply_started++;
}

void on_linop_advanced_apply_completed(const gko::LinOp*, const gko::LinOp*,
const gko::LinOp*, const gko::LinOp*,
const gko::LinOp*) const override
{
linop_advanced_apply_completed++;
}

void on_linop_factory_generate_started(const gko::LinOpFactory*,
const gko::LinOp*) const override
{
linop_factory_generate_started++;
}

void on_linop_factory_generate_completed(const gko::LinOpFactory*,
const gko::LinOp*,
const gko::LinOp*) const override
{
linop_factory_generate_completed++;
}

int mutable linop_apply_started = 0;
int mutable linop_apply_completed = 0;
int mutable linop_advanced_apply_started = 0;
int mutable linop_advanced_apply_completed = 0;
int mutable linop_factory_generate_started = 0;
int mutable linop_factory_generate_completed = 0;
};


class DummyLinOp : public gko::EnableLinOp<DummyLinOp>,
public gko::EnableCreateMethod<DummyLinOp> {
public:
Expand Down Expand Up @@ -98,8 +154,11 @@ class EnableLinOp : public ::testing::Test {
alpha{DummyLinOp::create(ref, gko::dim<2>{1})},
beta{DummyLinOp::create(ref, gko::dim<2>{1})},
b{DummyLinOp::create(ref, gko::dim<2>{5, 4})},
x{DummyLinOp::create(ref, gko::dim<2>{3, 4})}
{}
x{DummyLinOp::create(ref, gko::dim<2>{3, 4})},
logger{std::make_shared<DummyLogger>(ref)}
{
op->add_logger(logger);
}

std::shared_ptr<const gko::ReferenceExecutor> ref;
std::shared_ptr<const gko::ReferenceExecutor> ref2;
Expand All @@ -108,6 +167,7 @@ class EnableLinOp : public ::testing::Test {
std::unique_ptr<DummyLinOp> beta;
std::unique_ptr<DummyLinOp> b;
std::unique_ptr<DummyLinOp> x;
std::shared_ptr<DummyLogger> logger;
};


Expand Down Expand Up @@ -251,6 +311,32 @@ TEST_F(EnableLinOp, ApplyUsesInitialGuessReturnsFalse)
}


TEST_F(EnableLinOp, ApplyIsLogged)
{
auto before_logger = *logger;

op->apply(gko::lend(b), gko::lend(x));

ASSERT_EQ(logger->linop_apply_started,
before_logger.linop_apply_started + 1);
ASSERT_EQ(logger->linop_apply_completed,
before_logger.linop_apply_completed + 1);
}


TEST_F(EnableLinOp, AdvancedApplyIsLogged)
{
auto before_logger = *logger;

op->apply(gko::lend(alpha), gko::lend(b), gko::lend(beta), gko::lend(x));

ASSERT_EQ(logger->linop_advanced_apply_started,
before_logger.linop_advanced_apply_started + 1);
ASSERT_EQ(logger->linop_advanced_apply_completed,
before_logger.linop_advanced_apply_completed + 1);
}


template <typename T = int>
class DummyLinOpWithFactory
: public gko::EnableLinOp<DummyLinOpWithFactory<T>> {
Expand Down Expand Up @@ -286,9 +372,13 @@ class DummyLinOpWithFactory

class EnableLinOpFactory : public ::testing::Test {
protected:
EnableLinOpFactory() : ref{gko::ReferenceExecutor::create()} {}
EnableLinOpFactory()
: ref{gko::ReferenceExecutor::create()},
logger{std::make_shared<DummyLogger>(ref)}
{}

std::shared_ptr<const gko::ReferenceExecutor> ref;
std::shared_ptr<DummyLogger> logger;
};


Expand Down Expand Up @@ -323,6 +413,36 @@ TEST_F(EnableLinOpFactory, PassesParametersToLinOp)
}


TEST_F(EnableLinOpFactory, FactoryGenerateIsLogged)
{
auto before_logger = *logger;
auto factory = DummyLinOpWithFactory<>::build().on(ref);
factory->add_logger(logger);
factory->generate(DummyLinOp::create(ref, gko::dim<2>{3, 5}));

ASSERT_EQ(logger->linop_factory_generate_started,
before_logger.linop_factory_generate_started + 1);
ASSERT_EQ(logger->linop_factory_generate_completed,
before_logger.linop_factory_generate_completed + 1);
}


TEST_F(EnableLinOpFactory, CopiesLinOpToOtherExecutor)
{
auto ref2 = gko::ReferenceExecutor::create();
auto dummy = gko::share(DummyLinOp::create(ref2, gko::dim<2>{3, 5}));
auto factory = DummyLinOpWithFactory<>::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 DummyLinOp*>(op->op_.get()));
}


template <typename Type>
class DummyLinOpWithType
: public gko::EnableLinOp<DummyLinOpWithType<Type>>,
Expand Down
Loading

0 comments on commit 374bd6f

Please sign in to comment.