From 874a856e34b87cce714be4110ab697725017f332 Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Mon, 27 Dec 2021 12:42:03 +0100 Subject: [PATCH] [v1.01] Run multiple independent instructions in the pipeline for AVX freq too. Fixes incorrect frequency measures under certain CPUs --- Makefile | 6 +++--- src/x86/freq/freq.h | 2 +- src/x86/freq/freq_avx.c | 21 +++++++++++++++++---- src/x86/freq/freq_avx512.c | 6 ------ 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index a24e774..c689e69 100644 --- a/Makefile +++ b/Makefile @@ -70,13 +70,13 @@ static: $(OUTPUT) strict: CFLAGS += -O2 -Werror -fsanitize=undefined -D_FORTIFY_SOURCE=2 strict: $(OUTPUT) -freq_nov.o: Makefile $(SRC_DIR)freq/freq_nov.c $(SRC_DIR)freq/freq_nov.h +freq_nov.o: Makefile $(SRC_DIR)freq/freq_nov.c $(SRC_DIR)freq/freq_nov.h $(SRC_FREQ)/freq.h $(SRC_DIR)freq/freq.h $(CC) $(CFLAGS) $(SANITY_FLAGS) -c -pthread $(SRC_DIR)freq/freq_nov.c -o $@ -freq_avx.o: Makefile $(SRC_DIR)freq/freq_avx.c $(SRC_DIR)freq/freq_avx.h +freq_avx.o: Makefile $(SRC_DIR)freq/freq_avx.c $(SRC_DIR)freq/freq_avx.h $(SRC_DIR)freq/freq.h $(CC) $(CFLAGS) $(SANITY_FLAGS) -c -mavx -pthread $(SRC_DIR)freq/freq_avx.c -o $@ -freq_avx512.o: Makefile $(SRC_DIR)freq/freq_avx512.c $(SRC_DIR)freq/freq_avx512.h +freq_avx512.o: Makefile $(SRC_DIR)freq/freq_avx512.c $(SRC_DIR)freq/freq_avx512.h $(SRC_DIR)freq/freq.h $(CC) $(CFLAGS) $(SANITY_FLAGS) -c -mavx512f -pthread $(SRC_DIR)freq/freq_avx512.c -o $@ $(OUTPUT): Makefile $(SOURCE) $(HEADERS) diff --git a/src/x86/freq/freq.h b/src/x86/freq/freq.h index d757e63..80f99d2 100644 --- a/src/x86/freq/freq.h +++ b/src/x86/freq/freq.h @@ -5,7 +5,7 @@ #include "../../common/cpu.h" #define MEASURE_TIME_SECONDS 5 -#define LOOP_ITERS 1000000000 +#define LOOP_ITERS 100000000 int64_t measure_frequency(struct cpuInfo* cpu); diff --git a/src/x86/freq/freq_avx.c b/src/x86/freq/freq_avx.c index 4935972..29bf667 100644 --- a/src/x86/freq/freq_avx.c +++ b/src/x86/freq/freq_avx.c @@ -15,13 +15,25 @@ void* compute_avx() { struct timeval begin, now; - __m256 a = _mm256_set1_ps(1.5); - __m256 b = _mm256_set1_ps(1.2); + __m256 a[8]; + __m256 b[8]; + + for(int i=0; i < 8; i++) { + a[i] = _mm256_set1_ps(1.5); + b[i] = _mm256_set1_ps(1.2); + } gettimeofday(&begin, NULL); while(!end) { for(uint64_t i=0; i < LOOP_ITERS; i++) { - a = _mm256_add_ps(a, b); + a[0] = _mm256_add_ps(a[0], b[0]); + a[1] = _mm256_add_ps(a[1], b[1]); + a[2] = _mm256_add_ps(a[2], b[2]); + a[3] = _mm256_add_ps(a[3], b[3]); + a[4] = _mm256_add_ps(a[4], b[4]); + a[5] = _mm256_add_ps(a[5], b[5]); + a[6] = _mm256_add_ps(a[6], b[6]); + a[7] = _mm256_add_ps(a[7], b[7]); } gettimeofday(&now, NULL); @@ -34,7 +46,8 @@ void* compute_avx() { printf("fopen: %s", strerror(errno)); } else { - fprintf(fp, "%f", a[0]); + for(int i=0; i < 8; i++) + fprintf(fp, "%f", a[i][0]); fclose(fp); } diff --git a/src/x86/freq/freq_avx512.c b/src/x86/freq/freq_avx512.c index b546574..df3ace1 100644 --- a/src/x86/freq/freq_avx512.c +++ b/src/x86/freq/freq_avx512.c @@ -10,12 +10,6 @@ #include #include "freq.h" -/* - * For AVX512, it seems that multiple independent - * instructions are needed to force the CPU to - * use AVX512 frequency, since with only one instruction - * (as the AVX implementaion) it still uses AVX frequency - */ void* compute_avx512() { bool end = false;