Skip to content

Instantly share code, notes, and snippets.

@nebgnahz
Created August 6, 2016 22:26
Show Gist options
  • Save nebgnahz/8c76feeee2ac3382dd7dd1cfa953de5c to your computer and use it in GitHub Desktop.
Save nebgnahz/8c76feeee2ac3382dd7dd1cfa953de5c to your computer and use it in GitHub Desktop.
Use BLAS for matrix multiplication
#include <stdio.h>
#if __APPLE__
#include <Accelerate/Accelerate.h>
#elif __linux__
#include <cblas.h>
#endif
// Calculate a * b
float a[4][4] = {
{1.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 1.0f, 0.0f, 0.0f},
{1.0f, 1.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 1.0f, 1.0f},
};
float b[4][4] = {
{1.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 1.0f, 0.0f, 0.0f},
{1.0f, 1.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 1.0f, 1.0f},
};
float c[4][4] = {
{0.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 0.0f},
};
int main() {
cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, 4, 4, 4,
1.0f, (float*)a, 4, (float*)b, 4, 1.0f, (float*)c, 4);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
printf("%.2f ", c[i][j]);
}
printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment