Created
August 6, 2016 22:26
-
-
Save nebgnahz/8c76feeee2ac3382dd7dd1cfa953de5c to your computer and use it in GitHub Desktop.
Use BLAS for matrix multiplication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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