Coder Social home page Coder Social logo

matmul.c's Introduction

High-Performance Matrix Multiplication on CPUs

Important! The code should be compiled with Clang. GCC causes the program to run 1.5 to 2 times slower on my machine. Please don’t expect peak performance without fine-tuning the hyperparameters, such as the number of threads, kernel and block sizes, unless you are running it on a Ryzen 7700(X). More on this in the tutorial.

Key Features

  • Simple and scalable C code (<150 LOC)
  • Step by step tutorial
  • Targets x86 processors with AVX and FMA instructions (=all modern Intel Core and AMD Ryzen CPUs)
  • Faster than OpenBLAS when fine-tuned for Ryzen 7700
  • Efficiently parallelized with just 3 lines of OpenMP directives
  • Follows the BLIS design
  • Works for arbitrary matrix sizes
  • Intuitive API void matmul(float* A, float* B, float* C, const int M, const int N, const int K)

How to use

For quick testing, fine-tuning, and prototyping, use the standalone file matmul.c in the main folder:

clang-17 -O2 -mno-avx512f -fopenmp -march=native matmul.c -o matmul.out && ./matmul.out

To verify the numerial accuracy of the implementation, add -DTEST:

clang-17 -O2 -mno-avx512f -fopenmp -march=native -DTEST matmul.c -o matmul.out && ./matmul.out

Performance

mt1

Tested on:

  • CPU: Ryzen 7 7700 8 Cores, 16 Threads
  • RAM: 32GB DDR5 6000 MHz CL36
  • Numpy 1.26.4
  • Compiler: clang-17
  • Compiler flags: -O2 -mno-avx512f -march=native
  • OS: Ubuntu 22.04.4 LTS

To reproduce the results, run:

python benchmark_numpy.py

clang-17 -O2 -mno-avx512f -march=native -fopenmp benchmark.c -o benchmark.out && ./benchmark.out

python plot_benchmark.py

htop

matmul.c's People

Contributors

salykova 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.