Skip to content

trying to collect all useful tutorials for famous C math and linear algebra libraries such as CBLAS, CLAPACK, GSL...

License

Notifications You must be signed in to change notification settings

Foadsf/Cmathtuts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cmathtuts: A Collection of Math Tutorials in C

GitHub stars GitHub forks GitHub Sponsors

Note: The dev branch contains the most organized and up-to-date content.

Introduction

When I started doing linear algebra in C, I was surprised to find so few tutorials for BLAS, LAPACK, and other fundamental APIs, despite the fact that they are the building blocks of many scientific computing libraries. For that reason, I started collecting all the examples and tutorials I could find across the internet.

This repository covers:

  • BLAS (Basic Linear Algebra Subprograms)
  • CBLAS (C interface to BLAS)
  • LAPACK (Linear Algebra PACKage)
  • CLAPACK (C interface to LAPACK)
  • LAPACKE (C interface to LAPACK, newer standard)
  • ATLAS (Automatically Tuned Linear Algebra Software)
  • OpenBLAS (Optimized BLAS library)

I should warn you that as a mechanical engineer, I had little experience managing a git repository when I started this project. If you manage to get past the somewhat messy structure, you'll find a wealth of examples and instructions that might help you.

Background: Understanding BLAS, LAPACK, and Their Implementations

This repository was created in response to a Stack Overflow question I answered about the relationship between these libraries:

BLAS and LAPACK are not specific SDKs—they are specifications or language extensions rather than implementations or libraries. The original implementations by Netlib were written in FORTRAN 77, which is why you'll see some strange conventions when using these APIs: you're actually calling FORTRAN routines in C.

Some of the best implementations of BLAS and LAPACK include:

  • ATLAS and OpenBLAS — High-performance CPU implementations conforming to the original API, implemented in C/C++ from scratch
  • GPGPU implementations using OpenCL: CLBlast, clBLAS, clMAGMA, ArrayFire, and ViennaCL
  • Vendor-specific implementations optimized for specific hardware (Intel MKL, cuBLAS, etc.)

My recommendation to anyone who wants to learn using BLAS and LAPACK in C is to learn FORTRAN-C mixed programming first. The first chapter of this repository is dedicated to this matter, with many different examples.

What's Included

  • Makefiles you can study to learn how to call individual Fortran routines in C or C++ programs
  • Examples modified to compile with GNU compilers (gcc, g++, and gfortran)
  • Installation instructions for macOS and Linux
  • Bash scripts (.sh) for automatic compilation of some libraries
  • Fortran-C mixed programming examples

Repository History

This project has evolved over nearly a decade (2016–2025). Here's a visualization of the git history:

gitGraph
   commit id: "3717a6d"
   commit id: "a4fa578"
   commit id: "19e4b80"
   commit id: "3616e1c"
   commit id: "2ed5943"
   commit id: "cc4cd0d"
   commit id: "540f2eb"
   commit id: "0b1c735"
   commit id: "3515e2a"
   commit id: "f1294c1"
   commit id: "4594bb2"
   commit id: "32aca00"
   commit id: "d83be81"
   commit id: "b3764b8"
   commit id: "f833803"
   commit id: "ae2814e"
   branch dev
   checkout dev
   commit id: "4847fa7"
   commit id: "b91ea59"
   checkout main
   commit id: "12c5ce4"
   checkout dev
   commit id: "328dc65"
   commit id: "076360e"
   commit id: "63aa6d8"
   commit id: "7401dcd"
   branch dev-remote
   commit id: "10a32cf"
   commit id: "277badd"
   checkout dev
   commit id: "bf89b2d"
   merge dev-remote id: "cbb75e7"
   commit id: "9f3b7ad"
   commit id: "8fc1f22"
   commit id: "e67541a"
   commit id: "7448b6b"
   branch dev-remote-2
   commit id: "ff7ebb1"
   commit id: "ff246b5"
   checkout dev
   commit id: "c4c4020"
   merge dev-remote-2 id: "0a58ab8"
   commit id: "5af3dba"
   commit id: "6581371"
   commit id: "8503557"
   commit id: "0b611d7"
   commit id: "7fc2de0"
   commit id: "e991a92"
   commit id: "c1917c5"
   commit id: "4b222e0"
   commit id: "4cb6cf4"
   commit id: "a5af85a"
   commit id: "403bf5d" tag: "origin/dev"
   checkout main
   merge dev id: "76acde4"
   commit id: "6894fd1"
   commit id: "a361050" tag: "master"
Loading

Evolution Timeline

2016: Inception and Foundations

  • Initial commit with repository name correction to "Cmathtuts"
  • Added Jochen Voss examples from seehuhn.de/pages/linear
  • Successfully compiled BLAS on macOS X
  • Added CLAPACK examples and created Makefiles for BLAS examples
  • Successfully compiled LAPACK, CBLAS, and LAPACKE
  • Created Makefiles for BLAS, CBLAS, CLAPACK, LAPACK (LAPACKE had issues)
  • Added Netlib BLAS bash script for compilation and installation
  • Major expansion: Fortran-C mixed programming examples added
  • Repository cleanup: binary files removed using BFG Repo-Cleaner

2017: Cleanup and Reorganization

  • Further cleanup and folder restructuring
  • Updated README documentation
  • Branches diverged: master for stable releases, dev for active development

2018: Major Development on dev Branch

  • Explained FORTRAN concepts (write/format commands, common blocks)
  • Added .gitignore for swap files and compiled objects
  • Renamed folders to standardized format (Ex01, Ex02, etc.)
  • Fixed platform-specific issues (apt-get install commands for Linux)
  • Clarified library distinctions ("this is LAPACK not BLAS")
  • Added multiple new examples (Ex25, Ex26)

2019–2021: Maintenance

  • Updated README on master branch
  • Added GitHub Sponsors funding configuration
  • Minor edits and improvements

Known Issues

  1. Unable to compile ATLAS on macOS X — a bug report was submitted on SourceForge
  2. For some BLAS and CBLAS examples, -lgfortran is required to compile. On macOS X, you need to install gfortran and link against libgfortran.dylib

TODO

  • Create compilation/installation scripts for all libraries across different platforms
  • Test Makefiles on other platforms (currently only tested on macOS X)
  • Merge improvements from dev branch into master
  • Add Windows compilation instructions

Resources and References

Official Documentation

  1. Netlib BLAS
  2. Netlib LAPACK
  3. LAPACKE Documentation

Tutorials and Examples

  1. Oregon State - LAPACK C Programming
  2. FSU - CLAPACK Examples
  3. UCSC Pleiades - LAPACK
  4. UCSC Pleiades - BLAS
  5. Intel MKL LAPACK Examples

Stack Overflow Discussions

  1. How to call CLAPACK from C
  2. Relation between BLAS, LAPACK, and ATLAS
  3. How does BLAS get such extreme performance
  4. Most widely used C++ vector/matrix libraries
  5. Purpose of LDA argument in BLAS dgemm
  6. Replicating BLAS matrix multiplication performance

Additional Libraries

  1. Cephes Mathematical Library
  2. CRlibm - Correctly Rounded Mathematical Library

Build Guides

  1. Compiling LAPACK as shared library in Linux
  2. Linux Questions - Scientific Libraries Installation

Support This Project

If you find this repository helpful, please consider:

  • Starring the repository
  • 🍴 Forking and contributing improvements
  • 💖 Sponsoring via GitHub Sponsors or Patreon

License

This repository contains examples collected from various sources across the internet. Individual examples may have their own licenses. Please check the source references for specific licensing information.

Author

Dr. Foad Sojoodi Farimani

About

trying to collect all useful tutorials for famous C math and linear algebra libraries such as CBLAS, CLAPACK, GSL...

Topics

Resources

License

Stars

Watchers

Forks