Git Product home page Git Product logo

matrixmultiplication's Introduction

Matrix Multiplication

This repository explores multiple implementations of performing a squared matrix multiplication and documents the performance of each implementation.

The implementations are written in C++ with CMAKE as the build system to offer portability. The Google benchmark and test frameworks are used to evaluate the performance of the implementations. To profile the memory and cache usage of each implementation, the implementations are cross-compiled to linux and Valgrind in WSL is used to profile the executables manually.

Implementations

Naive

The naive implementation is the most straightforward implementation of matrix multiplication. It iterates over all elements of the result matrix and calculates the value by summing up the products of the corresponding row and column of the input matrices.

In Python the algorithm for multiplying two square row-major matrices with each other would look like the following:

for i in range(n):
  for j in range(n):
    for k in range(n):
      outputMatrix[i][j] += leftMatrix[i][k] * rightMatrix[k][j]

Note that in my implementation the input matrices are one dimensional row-major as opposed to the usual two dimensions row-major in a matrix.

Two implementations are provided, one using a vector and one using a raw array:

// Vector implementation
std::vector<int_fast64_t> naive_vector_matrix_mul(std::vector<int_fast64_t> A, std::vector<int_fast64_t> B, uint_fast32_t n);

// Array implementation
std::unique_ptr<int_fast64_t[]> naive_array_matrix_mul(int_fast64_t* A, int_fast64_t* B, uint_fast32_t n);

Inspecting the assembly in compiler explorer shows that both the vector implementation and the array implementation both enable some form of vectorization in their inner loops.

Cache-optimized

To make use of the cache, we can reorder the loops to iterate over the matrix in a cache-friendly way. In the naive implementation the k index on the inner-most loop causes a cache miss on the right matrix on every iteration. If the k and j indices are swapped, the output matrix and right matrix will both be accessed contiguously. Instead of completing the dot product of a row and a column in one operation, it is now performed in a cache-friendly manner, where the partial sums are stored in the output matrix.

for i in range(n):
  for k in range(n):
    for j in range(n):
      outputMatrix[i][j] += leftMatrix[i][k] * rightMatrix[k][j]

Only one implementation is provided since it was concluded that the array implementation was faster than the vector implementation:

std::unique_ptr<int_fast64_t[]> cache_optimized_array_matrix_mul(int_fast64_t* A, int_fast64_t* B, uint_fast32_t n);

matrixmultiplication's People

Contributors

ancientkingg avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.