Skip to content

Instantly share code, notes, and snippets.

@robertknight
Created March 14, 2024 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertknight/3e7bf748b5435657ca671930c30e4ec7 to your computer and use it in GitHub Desktop.
Save robertknight/3e7bf748b5435657ca671930c30e4ec7 to your computer and use it in GitHub Desktop.
Apple Accelerate sgemv benchmark
#include <algorithm>
#include <chrono>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>
#include <Accelerate/Accelerate.h>
#include <cblas.h>
using namespace std;
// Compile with:
//
// c++ -std=c++17 accelerate_gemv.cpp \
// -framework Accelerate \
// -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Accelerate.framework/Versions/Current/Frameworks/vecLib.framework/Headers/
int main(int, char **) {
int iters = 1000;
int m = 1;
/* int n = 2048; */
int n = 51865;
int k = 512;
int lda = n;
int incx = 1;
float alpha = 1.;
float beta = 0.;
int incy = 1;
random_device rnd_device;
mt19937 mersenne_engine{rnd_device()};
uniform_int_distribution<int> dist{1, 52};
auto gen = [&dist, &mersenne_engine]() { return dist(mersenne_engine); };
vector<float> a(n * k);
generate(begin(a), end(a), gen);
vector<float> x(k);
generate(begin(x), end(x), gen);
vector<float> y(n);
auto start = std::chrono::steady_clock::now();
for (int i = 0; i < iters; i++) {
cblas_sgemv(CblasRowMajor, CblasNoTrans, k, n, alpha, a.data(), lda,
x.data(), incx, beta, y.data(), incy);
}
auto end = std::chrono::steady_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(end - start)
.count();
auto flops =
static_cast<double>(2 * m * n * k * static_cast<int64_t>(iters)) /
(static_cast<double>(duration) / 1000000.0);
auto gflops = flops / 1e9;
std::cout << "duration (s) " << duration << std::endl;
std::cout << "gflops " << gflops << std::endl;
}
@robertknight
Copy link
Author

Created as part of comparisons with rten in robertknight/rten#42 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment