Skip to content

Instantly share code, notes, and snippets.

@rhaas80

rhaas80/Makefile Secret

Created October 13, 2021 17:50
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 rhaas80/3eef8605b26ec76035b766f2420e4f6e to your computer and use it in GitHub Desktop.
Save rhaas80/3eef8605b26ec76035b766f2420e4f6e to your computer and use it in GitHub Desktop.
all-in-one makefile to check speed of sqrt
bench: sqrttest
./sqrttest
sqrttest: sqrttest.c tst.c
rm -f sqrttest.o tst.o $@
mpicc -std=gnu99 -march=native -Ofast -g3 -fopenmp -fno-lto -c sqrttest.c
mpicc -std=gnu99 -march=native -Ofast -g3 -fopenmp -fno-lto -c tst.c
mpicc -O0 -g3 -fopenmp -fno-lto sqrttest.o tst.o -o $@ -lm
define SQRTTEST_BODY
#include <math.h>
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#define SZ (1UL<<26)
#define ALIGN 64
static double *a; /* input */
static double *b; /* output */
extern void tst1(double * restrict a, double * restrict b, const size_t sz);
extern void tst2(double * restrict a, double * restrict b, const size_t sz);
int main(void) {
posix_memalign((void**)&a, ALIGN, sizeof(*a)*SZ);
posix_memalign((void**)&b, ALIGN, sizeof(*b)*SZ);
for(size_t i = 0 ; i < SZ ; i++) {
a[i] = 1.234567890; /* something non-zero to make sqrt do work */
b[i] = 0.; /* touch destination to get memory pages */
}
{
const double start = MPI_Wtime();
tst1(a, b, SZ);
const double end = MPI_Wtime();
double sum = 0.;
for(size_t i = 0 ; i < SZ ; i++) {
sum += b[i];
}
printf("sqrt took %gms for %g\n", (end-start)*1e3, sum);
}
{
const double start = MPI_Wtime();
tst2(a, b, SZ);
const double end = MPI_Wtime();
double sum = 0.;
for(size_t i = 0 ; i < SZ ; i++) {
sum += b[i];
}
printf("copy took %gms for %g\n", (end-start)*1e3, sum);
}
return 0;
}
endef
export SQRTTEST_BODY
sqrttest.c: Makefile
echo "$$SQRTTEST_BODY" >$@
define TST_BODY
#include <math.h>
#include <string.h>
#include <stddef.h>
#define ALIGN 64
void tst1(double * restrict a, double * restrict b, const size_t sz)
{
a = __builtin_assume_aligned(a, ALIGN);
b = __builtin_assume_aligned(b, ALIGN);
#pragma omd simd
for(size_t i = 0 ; i < sz ; i++) {
b[i] = sqrt(a[i]);
}
}
void tst2(double * restrict a, double * restrict b, const size_t sz)
{
a = __builtin_assume_aligned(a, ALIGN);
b = __builtin_assume_aligned(b, ALIGN);
#pragma omd simd
for(size_t i = 0 ; i < sz ; i++) {
b[i] = a[i];
}
}
endef
export TST_BODY
tst.c: Makefile
echo "$$TST_BODY" >$@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment