Skip to content

Commit

Permalink
Fixed Softmax typo, implemented Matrix Transpose, and made appropriat…
Browse files Browse the repository at this point in the history
…e changes to matrix printer/benchmarker.
  • Loading branch information
alejandroarmas committed May 20, 2022
1 parent 3a30c97 commit cca5d2e
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 49 deletions.
21 changes: 14 additions & 7 deletions include/m_algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ namespace Matrix {
public:
Matrix::Representation operator()(
const Matrix::Representation& l) const noexcept {

return Impl().operate(l);
};

Expand All @@ -58,7 +57,7 @@ namespace Matrix {
static_assert(MatrixOperatable<ReLU>);


class SoftMax : public UnaryAdapter<ReLU> {
class SoftMax : public UnaryAdapter<SoftMax> {

public:
Matrix::Representation operate(
Expand All @@ -68,12 +67,20 @@ namespace Matrix {
static_assert(MatrixOperatable<SoftMax>);


// class Transpose : public UnaryAdapter<ReLU> {
class Transpose : public UnaryAdapter<Transpose> {

public:
Matrix::Representation operate(
const Matrix::Representation& m) const noexcept;
};

static_assert(MatrixOperatable<Transpose>);


// public:
// Matrix::Representation operate(
// const Matrix::Representation& m) const noexcept;
// };
void transpose_helper(
std::vector<float>::const_iterator in,
std::vector<float>::iterator out,
int rb, int re, int cb, int ce, int rows, int cols) noexcept;

}

Expand Down
41 changes: 33 additions & 8 deletions include/matrix_benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <memory>
#include <iostream>
#include <chrono>
#include <time.h>

#include "matrix.h"
#include "m_algorithms.h"
Expand Down Expand Up @@ -37,28 +39,51 @@ namespace Matrix {
std::cout << std::endl << "Performed in " << mul_bm_r.get_computation_duration_ms() << " ms." << std::endl;
*/
template <Matrix::Operations::MatrixOperatable Operator>

template <Matrix::Operations::MatrixOperatable T>
class Timer {

public:
Timer(Operator _m) :
explicit Timer(T _m) :
matrix_operation(_m) {}

Representation operator()(
const Representation& l,
const Representation& r);

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; }



template <Matrix::Operations::BinaryMatrixOperatable U = T>
Representation operator()(
const Matrix::Representation& l,
const Matrix::Representation& r) noexcept {

start = std::chrono::steady_clock::now();
Matrix::Representation mc = matrix_operation(l, r);
end = std::chrono::steady_clock::now();

return Matrix::Representation{mc};
}

template <Matrix::Operations::UnaryMatrixOperatable U = T>
Representation operator()(
const Matrix::Representation& l) noexcept {

start = std::chrono::steady_clock::now();
Matrix::Representation mc = matrix_operation(l);
end = std::chrono::steady_clock::now();

return Matrix::Representation{mc};
}

private:
Operator matrix_operation;

T matrix_operation;
std::chrono::steady_clock::time_point start;
std::chrono::steady_clock::time_point end;
};




}
Expand Down
2 changes: 1 addition & 1 deletion include/matrix_printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Matrix {
class Printer {

public:
std::unique_ptr<Matrix::Representation> operator()(std::unique_ptr<Matrix::Representation> m);
void operator()(const Matrix::Representation& m) noexcept;
};


Expand Down
71 changes: 71 additions & 0 deletions m_algorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,73 @@ namespace Matrix {
return Matrix::Representation{output};
}

Matrix::Representation Transpose::operate(
const Matrix::Representation& m) const noexcept {

Matrix::Representation output = Matrix::Representation{
Matrix::Rows(m.num_cols()),
Matrix::Columns(m.num_rows())
};

transpose_helper(
m.constScanStart(),
output.scanStart(),
0, m.num_rows(),
0, m.num_cols(),
m.num_rows(), m.num_cols());

return Matrix::Representation{output};
}

void transpose_helper(
std::vector<float>::const_iterator in,
std::vector<float>::iterator out,
int rb, int re, int cb, int ce, int rows, int cols) noexcept {

int r = re - rb, c = ce - cb;
if (r <= 16 && c <= 16) {
for (int i = rb; i < re; i++) {
for (int j = cb; j < ce; j++) {
*(out + (j * rows + i)) = *(in + (i * cols + j));
}
}
} else if (r >= c) {
cilk_spawn transpose_helper(in, out, rb, rb + (r / 2), cb, ce, rows, cols);
transpose_helper(in, out, rb + (r / 2), re, cb, ce, rows, cols);
cilk_sync;
} else {
cilk_spawn transpose_helper(in, out, rb, re, cb, cb + (c / 2), rows, cols);
transpose_helper(in, out, rb, re, cb + (c / 2), ce, rows, cols);
cilk_sync;
}
}


// int transpose( double *a, int ndra, int nr, int nc, double *b, int ndrb ) {
// if (nr < 32) {
// for (int i = 0; i < n; i++)
// for (int j = 0; j < i; j++)
// a[j * N + i]
// a[i * N + j];
// transposeBase(a, ndra, nr, nc, b, ndrb );
// }
// else {
// /* subdivide the long side */
// if (nr > nc) {
// transpose(a, ndra, nr/2, nc, b, ndrb );
// transpose(a + nr/2 ,ndra, nr-nr/2, nc, b+(nr/2)*ndrb, ndrb );
// }
// else {
// transpose(a, ndra, nr, nc/2, b, ndrb );
// transpose(a + ndra*(nc/2), ndra, nr, nc-nc/2, b+nc/2, ndrb );
// }
// }
// }

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

// }


} // Unary
Expand Down Expand Up @@ -174,6 +241,8 @@ namespace Matrix {
return Matrix::Representation{output};
}



}


Expand Down Expand Up @@ -279,6 +348,8 @@ namespace Matrix {

/*
Adapted from https://ocw.mit.edu/courses/mathematics/18-335j-introduction-to-numerical-methods-spring-2019/week-5/MIT18_335JS19_lec12.pdf
We need to divide the data until it fits into lowest cache.
*/
void add_matmul_rec(std::vector<float>::const_iterator a, std::vector<float>::const_iterator b, std::vector<float>::iterator c,
int m, int n, int p, int fdA, int fdB, int fdC) noexcept {
Expand Down
54 changes: 30 additions & 24 deletions matrix_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,42 @@
namespace Matrix {


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

Matrix::Representation mc;
// start = std::chrono::steady_clock::now();
// Matrix::Representation mc = matrix_operation(l);
// end = std::chrono::steady_clock::now();

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

// return Matrix::Representation{mc};
// }

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


return Matrix::Representation{mc};
}
// template <Operations::BinaryMatrixOperatable Operator>
// Representation Operations::Timer<Operator>::operator()(const Matrix::Representation& l,
// const Matrix::Representation& r) {

// start = std::chrono::steady_clock::now();
// Matrix::Representation mc = matrix_operation(l, r);
// end = std::chrono::steady_clock::now();


// return Matrix::Representation{mc};
// }


template class Operations::Timer<Matrix::Operations::Unary::ReLU>;
template class Operations::Timer<Matrix::Operations::Unary::SoftMax>;
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>;
template class Operations::Timer<Matrix::Operations::Metric::CrossEntropy>;
// template class Operations::Timer<Operations::Unary::ReLU>;
// template class Operations::Timer<Operations::Unary::SoftMax>;
// template class Operations::Timer<Operations::Unary::Transpose>;
// template class Operations::Timer<Operations::Binary::HadamardProduct::Std>;
// template class Operations::Timer<Operations::Binary::Multiplication::ParallelDNC>;
// template class Operations::Timer<Operations::Binary::Multiplication::Naive>;
// template class Operations::Timer<Operations::Binary::Multiplication::Square>;
// template class Operations::Timer<Operations::Binary::Addition::Std>;
// template class Operations::Timer<Operations::Binary::OuterProduct::Naive>;
// template class Operations::Timer<Operations::Metric::CrossEntropy>;



Expand Down
13 changes: 4 additions & 9 deletions matrix_printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,16 @@
#define MAX_PRINT_LENGTH 5


std::unique_ptr<Matrix::Representation> Matrix::Printer::operator()(std::unique_ptr<Matrix::Representation> m) {
void Matrix::Printer::operator()(const Matrix::Representation& m) noexcept {


if (m == nullptr) {
throw std::invalid_argument("Matrix has no data (pointing to null).");
}

bool valid_column_print = true;
bool reached_end_row = true;
bool before_max_width = true;
bool last_column_val = true;

u_int64_t n_cols = m->num_cols();
u_int64_t n_rows = m->num_rows();
u_int64_t n_cols = m.num_cols();
u_int64_t n_rows = m.num_rows();
uint64_t total_iter = n_rows * n_cols;

std::cout << "Matrix[R=" << n_rows << "][C=" << n_cols << "]:";
Expand All @@ -41,7 +37,7 @@ std::unique_ptr<Matrix::Representation> Matrix::Printer::operator()(std::unique_

if (before_max_width || last_column_val) {

std::cout << m->get(i / n_cols, i % n_cols) << " ";
std::cout << m.get(i / n_cols, i % n_cols) << " ";
valid_column_print = true;
}
else if (!valid_column_print) {}
Expand All @@ -54,5 +50,4 @@ std::unique_ptr<Matrix::Representation> Matrix::Printer::operator()(std::unique_

std::cout << std::endl << std::endl;

return m;
}

0 comments on commit cca5d2e

Please sign in to comment.