Skip to content

Commit

Permalink
Created Adaptor object for compability between objects and Binary/Una…
Browse files Browse the repository at this point in the history
…ry Operators.
  • Loading branch information
alejandroarmas committed Apr 13, 2022
1 parent f88489f commit ba91010
Show file tree
Hide file tree
Showing 9 changed files with 459 additions and 380 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CC = clang++

PERFORMANCE_PROFILE_DIR = profiles

CPPFLAGS = -Werror -I include -fopencilk -std=c++2a -pthread
CPPFLAGS = -Werror -I include -fopencilk -std=c++2a -pthread
LDFLAGS = -L$(CURDIR)/include -lstdc++ -lm -fopencilk

ifeq ($(DEBUG), 1)
Expand Down
16 changes: 5 additions & 11 deletions activation_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "matrix.h"
#include "m_algorithms.h"
#include "matrix_printer.h"
#include "matrix_benchmark.h"
#include "config.h"


Expand All @@ -15,21 +16,14 @@ std::unique_ptr<Matrix::Representation> NeuralNetwork::ActivationFunctions::ReLU
throw std::invalid_argument("Matrix has no data (pointing to null).");
}

Matrix::Operations::Timer relu(
std::make_unique<Matrix::Operations::Unary::ReLU>());

auto f = [](std::unique_ptr<Matrix::Representation> input) {

std::unique_ptr<Matrix::Representation> output = std::make_unique<Matrix::Representation>(
Matrix::Rows(input->num_rows()),
Matrix::Columns(input->num_cols())
);

std::replace_copy_if(input->scanStart(), input->scanEnd(), output->scanStart(),
[](float z){ return z < 0;}, 0);
// Matrix::Operations::Unary::ReLU relu;

return output;
};

std::unique_ptr<Matrix::Representation> output = f(std::move(input));
std::unique_ptr<Matrix::Representation> output = relu(std::move(input));

#if DEBUG
Matrix::Printer m_printer;
Expand Down
210 changes: 129 additions & 81 deletions include/m_algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <algorithm>
#include <functional>
#include <exception>

#include "matrix.h"

Expand All @@ -12,151 +13,198 @@ namespace Matrix {

namespace Operations {

class BaseBinaryOpInterface {

public:
virtual ~BaseBinaryOpInterface() = default;
virtual std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation>& l,
std::unique_ptr<Matrix::Representation>& r) = 0;


};

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

public:
virtual ~BaseInterface() = default;
virtual std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation>& l,
std::unique_ptr<Matrix::Representation>& r) { return Impl().operator()(l, r); };
virtual ~BaseOp() = default;
private:
BaseOp& Impl() { return *static_cast<Implementation*>(this); }
BaseOp() = default;
friend Implementation;
std::unique_ptr<Matrix::Representation> l,
std::unique_ptr<Matrix::Representation> r = nullptr) = 0;


};

namespace Unary {


std::string debug_message(std::unique_ptr<Matrix::Representation>& l,
std::unique_ptr<Matrix::Representation>& r);

std::string debug_message_2(std::unique_ptr<Matrix::Representation>& l,
std::unique_ptr<Matrix::Representation>& r);
template <class Implementation>
class UnaryAdapter : public BaseInterface {

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

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

~UnaryAdapter() = default;
private:
Implementation& Impl() { return *static_cast<Implementation*>(this); }
friend Implementation;


};

namespace Addition {

class ReLU : public UnaryAdapter<ReLU> {

class Std : public BaseOp<Std> {
public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation>& l,
std::unique_ptr<Matrix::Representation>& r) override;
std::unique_ptr<Matrix::Representation> m);

};



}

namespace OuterProduct {
namespace Binary {


template <class Implementation>
class BaseOp : public BaseInterface {

class Naive : public BaseOp<Naive> {
public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation>& l,
std::unique_ptr<Matrix::Representation>& r) override;
BaseOp() = default;
virtual std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation> l,
std::unique_ptr<Matrix::Representation> r) { return Impl().operator()(std::move(l), std::move(r)); };
virtual ~BaseOp() = default;
private:
Implementation& Impl() { return *static_cast<Implementation*>(this); }
friend Implementation;

};



}
std::string debug_message(std::unique_ptr<Matrix::Representation> l,
std::unique_ptr<Matrix::Representation> r);

std::string debug_message_2(std::unique_ptr<Matrix::Representation> l,
std::unique_ptr<Matrix::Representation> r);


namespace HadamardProduct {


class Naive : public BaseOp<Naive> {
namespace Addition {

public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation>& l,
std::unique_ptr<Matrix::Representation>& r) override;

};
class Std : public BaseOp<Std> {
public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation> l,
std::unique_ptr<Matrix::Representation> r);

};

class Std : public BaseOp<Naive> {
}

public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation>& l,
std::unique_ptr<Matrix::Representation>& r) override;
namespace OuterProduct {

};


}
class Naive : public BaseOp<Naive> {
public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation> l,
std::unique_ptr<Matrix::Representation> r);

};



/*
Matrix Multiplication Usage:
}

std::unique_ptr<Matrix::Representation> ma = std::make_unique<Matrix::Representation>(2000, 100);
std::unique_ptr<Matrix::Representation> mb = std::make_unique<Matrix::Representation>(100, 3000);

Matrix::Operations::Multiplication::Naive mul;

std::unique_ptr<Matrix::Representation> mc = mul(ma, mb);
*/
namespace Multiplication {


class Naive : public BaseOp<Naive> {

public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation>& l,
std::unique_ptr<Matrix::Representation>& r) override;
namespace HadamardProduct {

};

class Naive : public BaseOp<Naive> {

public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation> l,
std::unique_ptr<Matrix::Representation> r);

class Square : public BaseOp<Square> {
};

public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation>& l,
std::unique_ptr<Matrix::Representation>& r) override;

};
class Std : public BaseOp<Naive> {

public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation> l,
std::unique_ptr<Matrix::Representation> r);

class ParallelDNC : public BaseOp<ParallelDNC> {
};

public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation>& l,
std::unique_ptr<Matrix::Representation>& r) override;

};
}


void add_matmul_rec(std::vector<float>::iterator c, std::vector<float>::iterator a, std::vector<float>::iterator b,
int m, int n, int p, int fdA, int fdB, int fdC);


}

}
/*
Matrix Multiplication Usage:
std::unique_ptr<Matrix::Representation> ma = std::make_unique<Matrix::Representation>(2000, 100);
std::unique_ptr<Matrix::Representation> mb = std::make_unique<Matrix::Representation>(100, 3000);
Matrix::Operations::Multiplication::Naive mul;
std::unique_ptr<Matrix::Representation> mc = mul(ma, mb);
*/
namespace Multiplication {


class Naive : public BaseOp<Naive> {

public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation> l,
std::unique_ptr<Matrix::Representation> r);

};


class Square : public BaseOp<Square> {

public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation> l,
std::unique_ptr<Matrix::Representation> r) ;

};


class ParallelDNC : public BaseOp<ParallelDNC> {

public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation> l,
std::unique_ptr<Matrix::Representation> r);

};


void add_matmul_rec(std::vector<float>::iterator c, std::vector<float>::iterator a, std::vector<float>::iterator b,
int m, int n, int p, int fdA, int fdB, int fdC);


} // namespace Multiplication

} // namespace Binary

} // namespace Operations

}
} // namespace Matrix

#endif // MATRIX_ALGORITHMS_H
Loading

0 comments on commit ba91010

Please sign in to comment.