Skip to content

Commit

Permalink
Depreciated get_code() functions + updated Timer interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandroarmas committed Apr 20, 2022
1 parent bf6223a commit 04689e3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 94 deletions.
49 changes: 6 additions & 43 deletions include/m_algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,23 @@ namespace Matrix {
};


class BaseInterface {

public:
virtual ~BaseInterface() = default;
virtual std::unique_ptr<Matrix::Representation> operator()(
const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r = nullptr) = 0;
virtual Code get_operation_code() = 0;

};


namespace Unary {


template <class Implementation>
class UnaryAdapter : public BaseInterface {
class UnaryAdapter {

public:
std::unique_ptr<Matrix::Representation> operator()(
const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r = nullptr) override {

const std::unique_ptr<Matrix::Representation>& l) {
if (!l) {
throw std::invalid_argument("Left operand not referencing a matrix.");
}


if (r != nullptr) {
throw std::invalid_argument("Unary Operation needs one operand.");
}
return Impl().operate(l);
};

~UnaryAdapter() = default;
Code get_operation_code() override { return Impl().get_code(); };

private:
Implementation& Impl() { return *static_cast<Implementation*>(this); }
friend Implementation;
Expand All @@ -74,8 +55,6 @@ namespace Matrix {
public:
std::unique_ptr<Matrix::Representation> operate(
const std::unique_ptr<Matrix::Representation>& m);
Code get_code() { return Code::ReLU; };

};

static_assert(MatrixOperatable<ReLU>);
Expand All @@ -87,13 +66,13 @@ namespace Matrix {


template <class Implementation>
class BaseOp : public BaseInterface {
class BaseOp {

public:
BaseOp() = default;
virtual std::unique_ptr<Matrix::Representation> operator()(
const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r) override {
const std::unique_ptr<Matrix::Representation>& r) {

if (!l) {
throw std::invalid_argument("Left operand not referencing a matrix.");
Expand All @@ -104,9 +83,7 @@ namespace Matrix {

return Impl().operate(l, r);
};
virtual ~BaseOp() = default;
Code get_operation_code() override { return Impl().get_code(); };
private:
virtual ~BaseOp() = default; private:
Implementation& Impl() { return *static_cast<Implementation*>(this); }
friend Implementation;

Expand All @@ -125,8 +102,6 @@ namespace Matrix {
std::unique_ptr<Matrix::Representation> operate(
const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r);
Code get_code() { return Code::PLUS; };

};

}
Expand All @@ -143,8 +118,6 @@ namespace Matrix {
std::unique_ptr<Matrix::Representation> operate(
const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r);
Code get_code() { return Code::OUTER_PRODUCT; };

};


Expand All @@ -166,8 +139,6 @@ namespace Matrix {
std::unique_ptr<Matrix::Representation> operate(
const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r);
Code get_code() { return Code::HADAMARD; };

};


Expand All @@ -177,8 +148,6 @@ namespace Matrix {
std::unique_ptr<Matrix::Representation> operate(
const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r);
Code get_code() { return Code::HADAMARD; };

};


Expand Down Expand Up @@ -208,8 +177,6 @@ namespace Matrix {
std::unique_ptr<Matrix::Representation> operate(
const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r);
Code get_code() { return Code::MULTIPLY; };


};

Expand All @@ -220,8 +187,6 @@ namespace Matrix {
std::unique_ptr<Matrix::Representation> operate(
const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r) ;
Code get_code() { return Code::MULTIPLY; };

};


Expand All @@ -231,8 +196,6 @@ namespace Matrix {
std::unique_ptr<Matrix::Representation> operate(
const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r);
Code get_code() { return Code::MULTIPLY; };

};


Expand Down
59 changes: 11 additions & 48 deletions include/matrix_benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,6 @@ namespace Matrix {
namespace Operations {



template <class Implementation>
class Benchmark {

public:
Benchmark(std::unique_ptr<Operations::BaseInterface> _m) : matrix_operation(std::move(_m)) {}
Code get_operation_code() { return matrix_operation->get_operation_code(); };

protected:
Implementation* Impl() { return static_cast<Implementation*>(this);}
std::unique_ptr<Representation> operator()(
const std::unique_ptr<Representation>& l,
const std::unique_ptr<Representation>& r = nullptr) {
return Impl()->operator()(l, r);
};
~Benchmark() = default;
std::unique_ptr<Operations::BaseInterface> matrix_operation;


};



/*
DESCRIPTION:
Expand All @@ -58,43 +35,29 @@ namespace Matrix {
std::cout << "Performed in " << mul_bm_r.get_computation_duration_ms() << " ms." << std::endl;
*/
class Timer : public Benchmark<Timer> {
template <Matrix::Operations::MatrixOperatable Operator>
class Timer {

public:
// Timer() : Benchmark<Timer,>(std::move(
// std::make_unique<Operations::BaseInterface>())) {}
Timer(std::unique_ptr<Operations::BaseInterface> _m) :
Benchmark<Timer>(std::move(_m)) {}
Timer(Operator _m) :
matrix_operation(_m) {}

std::unique_ptr<Representation> operator()(
const std::unique_ptr<Representation>& l,
const std::unique_ptr<Representation>& r = nullptr);

int get_computation_duration_ms() {
return std::chrono::duration_cast<std::chrono::duration<int, std::micro>>(end - start).count(); }

std::chrono::steady_clock::time_point get_start() { return start; }
std::chrono::steady_clock::time_point get_end() { return end; }

int get_computation_duration_ms() {
return std::chrono::duration_cast<std::chrono::duration<int, std::micro>>(end - start).count(); }
std::chrono::steady_clock::time_point get_start() { return start; }
std::chrono::steady_clock::time_point get_end() { return end; }

private:
Operator matrix_operation;
std::chrono::steady_clock::time_point start;
std::chrono::steady_clock::time_point end;
};


// #ifdef CILKSCALE
// class ParallelMeasurer : public Benchmark<ParallelMeasurer> {

// public:
// ParallelMeasurer(std::unique_ptr<Operations::BaseInterface> _m) :
// Benchmark<ParallelMeasurer>(std::move(_m)) {}
// std::unique_ptr<Representation> operator()(
// const std::unique_ptr<Representation>& l,
// const std::unique_ptr<Representation>& r);

// };
// #endif



}

Expand Down
26 changes: 23 additions & 3 deletions matrix_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,37 @@
namespace Matrix {


std::unique_ptr<Representation> Operations::Timer::operator()(const std::unique_ptr<Matrix::Representation>& l,
template <Matrix::Operations::MatrixOperatable Operator>
std::unique_ptr<Representation> Operations::Timer<Operator>::operator()(const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r) {

start = std::chrono::steady_clock::now();
std::unique_ptr<Matrix::Representation> mc = this->matrix_operation->operator()(l, r);
std::unique_ptr<Matrix::Representation> mc;

start = std::chrono::steady_clock::now();
if constexpr (Matrix::Operations::UnaryMatrixOperatable<Operator>) {
mc = this->matrix_operation(l);
}
else if constexpr (Matrix::Operations::BinaryMatrixOperatable<Operator>) {
mc = this->matrix_operation(l, r);
}

end = std::chrono::steady_clock::now();


return mc;
}


template class Operations::Timer<Matrix::Operations::Unary::ReLU>;
template class Operations::Timer<Matrix::Operations::Binary::HadamardProduct::Std>;
template class Operations::Timer<Matrix::Operations::Binary::Multiplication::ParallelDNC>;
template class Operations::Timer<Matrix::Operations::Binary::Multiplication::Naive>;
template class Operations::Timer<Matrix::Operations::Binary::Multiplication::Square>;
template class Operations::Timer<Matrix::Operations::Binary::Addition::Std>;
template class Operations::Timer<Matrix::Operations::Binary::OuterProduct::Naive>;



// #ifdef CILKSCALE
/*
Cilkscale's command-line output includes work and span measurements for the Cilk program in terms of empirically measured times.
Expand Down

0 comments on commit 04689e3

Please sign in to comment.