Skip to content
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

Feature single thread #14

Merged
merged 4 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ BinPackParameters: false
ColumnLimit: 100
DerivePointerAlignment: false
IndentWidth: 4
AccessModifierOffset: -4
MaxEmptyLinesToKeep: 1
PointerAlignment: Right
SortIncludes: true
2 changes: 1 addition & 1 deletion .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: clang-format Check
on: [push, pull_request]
on: [pull_request]
jobs:
formatting-check:
name: Formatting Check
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ jobs:
working-directory: ${{ steps.strings.outputs.build-output-dir }}/test
# Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest --build-config ${{ matrix.build_type }}
run: ctest --build-config ${{ matrix.build_type }} --output-on-failure
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@
# cache
*.cache*/
*__pycache__*/

# compile commands
compile_commands.json
15 changes: 14 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,28 @@ option(MCA_BUILD_SHARE_LIB "Build shared libraries rather than static libraries"
option(MCA_BUILD_TEST "Whether or not to build unit test" on)

aux_source_directory(src MCA_SOURCE)
aux_source_directory(src/include MCA_SOURCE)
aux_source_directory(src/single_thread MCA_SOURCE)
set(LIBRARY_OUTPUT_PATH lib)
if (MCA_BUILD_SHARE_LIB)
add_library(mca SHARED ${MCA_SOURCE})
else()
add_library(mca STATIC ${MCA_SOURCE})
endif()

target_include_directories(mca INTERFACE ${PROJECT_SOURCE_DIR}/include)
target_include_directories(mca PRIVATE ${PROJECT_SOURCE_DIR}/src/include)

if ((NOT WIN32) AND CMAKE_EXPORT_COMPILE_COMMANDS)
add_custom_command(
TARGET mca
PRE_BUILD
COMMAND
${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/compile_commands.json ${CMAKE_SOURCE_DIR})
endif()

find_program(CLANG_TIDY_EXE NAMES clang-tidy)
if (NOT CLANG_TIDY_EXE STREQUAL "CLANG_TIDY_EXE-NOTFOUND")
if (CLANG_TIDY_EXE)
set_target_properties(mca PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_EXE}")
endif()

Expand Down
333 changes: 333 additions & 0 deletions src/include/matrix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,333 @@
#ifndef MATRIX_H
#define MATRIX_H

#include <cassert>
#include <cstring>
#include <memory>
#include <type_traits>
#include <vector>

namespace mca {
struct Shape {
size_t rows = 0;
size_t columns = 0;

Shape() = default;
Shape(size_t rows, size_t columns) : rows(rows), columns(columns) {}
bool operator==(const Shape &other) const {
return rows == other.rows && columns == other.columns;
}
bool operator!=(const Shape &other) const { return !(*this == other); }

size_t size() const { return rows * columns; }
};

template <class ELEMENT_TYPE = double>
class Matrix {
public:
Matrix() = default;

Matrix(const std::vector<std::vector<ELEMENT_TYPE>> &init) {
shape.rows = init.size();
if (init.size() != 0) {
shape.columns = init[0].size();
data = std::shared_ptr<ELEMENT_TYPE[]>(new ELEMENT_TYPE[shape.size()]);
for (size_t i = 0; i < shape.rows; i++) {
for (size_t j = 0; j < shape.columns; j++) { get(i, j) = init[i][j]; }
}
// TODO: update with multi thread
// *this = init;
}
}

Matrix(const Shape &shape, const ELEMENT_TYPE &defaultValue) {
this->shape = shape;
if (shape.size() != 0) {
data = std::shared_ptr<ELEMENT_TYPE[]>(new ELEMENT_TYPE[shape.size()]);
std::fill(data.get(), data.get() + shape.size(), defaultValue);
// TODO: update with multi thread
// fill(defaultValue);
}
}

template <class T>
Matrix(const Matrix<T> &other) {
*this = other;
}

// only those which have the same ELEMENT_TYPE can use move constructor
Matrix(Matrix &&other) noexcept
: data(std::move(other.data)), shape(std::move(other.getShape())) {}

// only those which have the same ELEMENT_TYPE can use move assignment
Matrix &operator=(Matrix &&other) noexcept {
shape = std::move(other.shape);
data = std::move(other.data);
return *this;
}

ELEMENT_TYPE &get(size_t i, size_t j) {
assert(i < shape.rows && j < shape.columns);
return data[i * shape.columns + j];
}

ELEMENT_TYPE *dataPtr() { return data.get(); }
const ELEMENT_TYPE *dataPtr() const { return data.get(); }

const ELEMENT_TYPE &get(size_t i, size_t j) const {
assert(i < shape.rows && j < shape.columns);
return data[i * shape.columns + j];
}

const size_t rows() { return shape.rows; }
const size_t columns() { return shape.columns; }

Shape getShape() const { return shape; }

void reshape(const Shape &shape) {
assert(this->shape.size() == shape.size());
this->shape = shape;
}

void fill(const ELEMENT_TYPE &value);

template <class T>
Matrix<ELEMENT_TYPE> &operator=(const Matrix<T> &other);

template <class T>
Matrix<ELEMENT_TYPE> &operator=(const std::vector<std::vector<T>> &init);

private:
std::shared_ptr<ELEMENT_TYPE[]> data;
Shape shape;

template <class T1, class T2>
friend bool operator==(const Matrix<T1> &a, const Matrix<T2> &b);
template <class T1, class T2>
friend bool operator!=(const Matrix<T1> &a, const Matrix<T2> &b);
template <class T1, class T2>
friend bool operator<(const Matrix<T1> &a, const Matrix<T2> &b);
template <class T1, class T2>
friend bool operator<=(const Matrix<T1> &a, const Matrix<T2> &b);
template <class T1, class T2>
friend bool operator>(const Matrix<T1> &a, const Matrix<T2> &b);
template <class T1, class T2>
friend bool operator>=(const Matrix<T1> &a, const Matrix<T2> &b);

template <class T1, class T2>
friend Matrix<std::common_type_t<T1, T2>> operator+(const Matrix<T1> &a, const Matrix<T2> &b);
template <class T1, class T2>
friend Matrix<std::common_type_t<T1, T2>> operator-(const Matrix<T1> &a, const Matrix<T2> &b);
template <class T1, class T2>
friend Matrix<std::common_type_t<T1, T2>> operator*(const Matrix<T1> &a, const Matrix<T2> &b);

template <class T, class Number>
friend Matrix<std::common_type_t<T, Number>> operator+(const Matrix<T> &a,
const Number &number);
template <class Number, class T>
friend Matrix<std::common_type_t<T, Number>> operator+(const Number &number,
const Matrix<T> &a);
template <class T, class Number>
friend Matrix<std::common_type_t<T, Number>> operator-(const Matrix<T> &a,
const Number &number);
template <class Number, class T>
friend Matrix<std::common_type_t<T, Number>> operator-(const Number &number,
const Matrix<T> &a);
template <class T, class Number>
friend Matrix<std::common_type_t<T, Number>> operator*(const Matrix<T> &a,
const Number &number);
template <class Number, class T>
friend Matrix<std::common_type_t<T, Number>> operator*(const Number &number,
const Matrix<T> &a);
template <class T, class Number>
friend Matrix<std::common_type_t<T, Number>> operator/(const Matrix<T> &a,
const Number &number);
template <class Number, class T>
friend Matrix<std::common_type_t<T, Number>> operator/(const Number &number,
const Matrix<T> &a);

template <class T>
friend Matrix<T> operator-(Matrix<T> &a);

template <class T1, class T2>
friend void operator+=(Matrix<T1> &a, const Matrix<T2> &b);
template <class T1, class T2>
friend void operator-=(Matrix<T1> &a, const Matrix<T2> &b);
template <class T1, class T2>
friend void operator*=(Matrix<T1> &a, const Matrix<T2> &b);
template <class T1, class T2>
friend void operator/=(Matrix<T1> &a, const Matrix<T2> &b);

template <class T, class Number>
friend void operator+=(Matrix<T> &a, const Number &number);
template <class Number, class T>
friend void operator+=(const Number &number, Matrix<T> &a);
template <class T, class Number>
friend void operator-=(Matrix<T> &a, const Number &number);
template <class Number, class T>
friend void operator-=(const Number &number, Matrix<T> &a);
template <class T, class Number>
friend void operator*=(Matrix<T> &a, const Number &number);
template <class Number, class T>
friend void operator*=(const Number &number, Matrix<T> &a);
template <class T, class Number>
friend void operator/=(Matrix<T> &a, const Number &number);
template <class Number, class T>
friend void operator/=(const Number &number, Matrix<T> &a);

template <class Number, class T>
friend void pow(Number &&number, const Matrix<T> &a);
template <class T, class Number>
friend void pow(const Matrix<T> &a, Number &&number);

template <class T>
friend Matrix<T> transpose(const Matrix<T> &a);
template <class T>
friend void transpose(const Matrix<T> &a, Matrix<T> &output);

template <class T1, class T2>
friend bool equalSingleThread(const Matrix<T1> &a,
const Matrix<T2> &b,
const size_t &sx,
const size_t &sy,
const Shape &shape);
template <class T1, class T2>
friend bool lessSingleThread(const Matrix<T1> &a,
const Matrix<T2> &b,
const size_t &sx,
const size_t &sy,
const Shape &shape);
template <class T1, class T2>
friend bool lessEqualSingleThread(const Matrix<T1> &a,
const Matrix<T2> &b,
const size_t &sx,
const size_t &sy,
const Shape &shape);
template <class T1, class T2>
friend bool greateSingleThreadr(const Matrix<T1> &a,
const Matrix<T2> &b,
const size_t &sx,
const size_t &sy,
const Shape &shape);
template <class T1, class T2>
friend bool greaterEquaSingleThreadl(const Matrix<T1> &a,
const Matrix<T2> &b,
const size_t &sx,
const size_t &sy,
const Shape &shape);
template <class T1, class T2>
friend bool notEquaSingleThreadl(const Matrix<T1> &a,
const Matrix<T2> &b,
const size_t &sx,
const size_t &sy,
const Shape &shape);

template <class T1, class T2, class O>
friend void addSingleThread(const Matrix<T1> &a,
const Matrix<T2> &b,
Matrix<O> &output,
const size_t &sx,
const size_t &sy,
const Shape &shape);
template <class T1, class T2, class O>
friend void substractSingleThread(const Matrix<T1> &a,
const Matrix<T2> &b,
Matrix<O> &output,
const size_t &sx,
const size_t &sy,
const Shape &shape);
template <class T1, class T2, class O>
friend void multiplySingleThread(const Matrix<T1> &a,
const Matrix<T2> &b,
Matrix<O> &output,
const size_t &sx,
const size_t &sy,
const Shape &shape);

template <class Number, class T, class O>
friend void addSingleThread(const Number &number,
const Matrix<T> &a,
Matrix<O> &output,
const size_t &sx,
const size_t &sy,
const Shape &shape);
template <class Number, class T, class O>
friend void substractSingleThread(const Number &number,
const Matrix<T> &a,
Matrix<O> &output,
const size_t &sx,
const size_t &sy,
const Shape &shape);
template <class T, class Number, class O>
friend void substractSingleThread(const Matrix<T> &a,
const Number &number,
Matrix<O> &output,
const size_t &sx,
const size_t &sy,
const Shape &shape);
template <class Number, class T, class O>
friend void multiplySingleThread(const Number &number,
const Matrix<T> &a,
Matrix<O> &output,
const size_t &sx,
const size_t &sy,
const Shape &shape);
template <class T, class Number, class O>
friend void divideSingleThread(const Matrix<T> &a,
const Number &number,
Matrix<O> &output,
const size_t &sx,
const size_t &sy,
const Shape &shape);
template <class Number, class T, class O>
friend void divideSingleThread(const Number &number,
const Matrix<T> &a,
Matrix<O> &output,
const size_t &sx,
const size_t &sy,
const Shape &shape);

template <class Number, class T, class O>
friend void powSingleThread(Number &&number,
const Matrix<T> &a,
Matrix<O> &output,
const size_t &sx,
const size_t &sy,
const Shape &shape);
template <class T, class Number, class O>
friend void powSingleThread(const Matrix<T> &a,
Number &&number,
Matrix<O> &output,
const size_t &sx,
const size_t &sy,
const Shape &shape);

template <class T>
friend void transpose(const Matrix<T> &a,
Matrix<T> &output,
const size_t &sx,
const size_t &sy,
const Shape &shape);
};

// TODO
template <class ELEMENT_TYPE>
void Matrix<ELEMENT_TYPE>::fill(const ELEMENT_TYPE &value) {}

// TODO
template <class ELEMENT_TYPE>
template <class T>
Matrix<ELEMENT_TYPE> &Matrix<ELEMENT_TYPE>::operator=(const Matrix<T> &other) {
return *this;
}

// TODO
template <class ELEMENT_TYPE>
template <class T>
Matrix<ELEMENT_TYPE> &Matrix<ELEMENT_TYPE>::operator=(const std::vector<std::vector<T>> &init) {
return *this;
}

} // namespace mca

#endif
4 changes: 4 additions & 0 deletions src/include/mca_private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef MCA_PRIVATE_H
#define MCA_PRIVATE_H

#endif
Loading
Loading