Skip to content

Instantly share code, notes, and snippets.

@juliantaylor
Created January 19, 2015 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juliantaylor/68e578d140f427ed80bb to your computer and use it in GitHub Desktop.
Save juliantaylor/68e578d140f427ed80bb to your computer and use it in GitHub Desktop.
avx align bechmark
// gcc avx.c -mavx -std=c99 -O2
// while true; do ./a.out; done > log
// analyze via np.genfromtxt, e.g. min, 10th percentile, median
#include <stdio.h>
#include <string.h>
#include <x86intrin.h>
#include <avxintrin.h>
#define N 5000
void __attribute__((noinline)) add(double * a, int warmup)
{
int dump;
long long ts = __rdtscp(&dump);
for (size_t i = 0; i < N; i+=4) {
__m256d av = _mm256_load_pd(&a[i]);
av = _mm256_add_pd(av, av);
_mm256_store_pd(&a[i], av);
}
if (!warmup)
printf("%lld ", __rdtscp(&dump) - ts);
}
void __attribute__((noinline)) add2(double * a, int warmup)
{
int dump;
long long ts = __rdtscp(&dump);
for (size_t i = 0; i < N; i+=4) {
__m256d av = _mm256_loadu_pd(&a[i]);
av = _mm256_add_pd(av, av);
_mm256_storeu_pd(&a[i], av);
}
if (!warmup)
printf("%lld ", __rdtscp(&dump) - ts);
}
void __attribute__((noinline)) add3(double * a, int warmup)
{
int dump;
long long ts = __rdtscp(&dump);
for (size_t i = 0; i < N; i+=1) {
a[i] = a[i] + a[i];
}
if (!warmup)
printf("%lld ", __rdtscp(&dump) - ts);
}
int main(int argc, const char *argv[])
{
double * a = _mm_malloc(N *8 + 10, 32);
memset(a, 0, N * 8 + 10);
add(a, 1);
add(a, 0);
add2(a + 2, 1);
add2(a + 2, 0);
add2(a + 1, 1);
add2(a + 1, 0);
add3(a, 1);
add3(a, 0);
puts("");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment