-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu_benchmark.c
102 lines (93 loc) · 3.67 KB
/
cpu_benchmark.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#include <immintrin.h>
long NUM_OPERATIONS = 10000000;
char OPERATION='F';
int THREAD_COUNT=1;
void* calculate_FLOPS(void* arg){
// IF AVX is supported
__m256d vector_a = _mm256_set_pd(7.0, 4.0, 6.0, 5.0);
__m256d vector_b = _mm256_set_pd(1.0, 2.0, 3.0, 2.0);
__m256d vector_c = _mm256_set_pd(1.0, 1.0, 1.0, 1.0);
int i;
for (i=0;i<NUM_OPERATIONS;i++){ //2 operations - 1 comparison and one addition
// adding vector_a and vector_b and storing the result into vector_c
vector_c = _mm256_add_pd(vector_a, vector_b); //8 operations - 4 additions and 4 assignments
// subtracting vector_b from vector_a and storing the result into vector_c
vector_c = _mm256_sub_pd(vector_a, vector_b); //8 operations - 4 subtractions and 4 assignments
// multiplying vector_a and vector_b and storing the result into vector_c
vector_c = _mm256_mul_pd(vector_a, vector_b); //8 operations - 4 multiplications and 4 assignments
}
pthread_exit(NULL);
//Total operations = 24+2 = 26
}
void* calculate_IOPS(void* arg) {
// IF AVX2 is supported
__m256i vector_a = _mm256_set_epi32(7, 4, 6, 5, 5, 7, 8, 2);
__m256i vector_b = _mm256_set_epi32(1, 2, 3, 2, 8, 6, 5, 3);
__m256i vector_c = _mm256_set_epi32(1, 1, 1, 1, 8, 5, 6, 8);
int i;
for (i=0;i<NUM_OPERATIONS;i++){ //2 operations - 1 comparison and one addition
// adding vector_a and vector_b and storing the result into vector_c
vector_c = _mm256_add_epi32(vector_a, vector_b); //16 operations - 8 additions and 8 assignments
// subtracting vector_b from vector_a and storing the result into vector_c
vector_c = _mm256_sub_epi32(vector_a, vector_b); //16 operations - 8 subtractions and 8 assignments
// multiplying vector_a and vector_b and storing the result into vector_c
vector_c = _mm256_mul_epi32(vector_a, vector_b); //16 operations - 8 multiplications and 8 assignments
}
pthread_exit(NULL);
//Total operations = 48+2 = 50
}
void calculate_operations_per_sec(){
pthread_t thread_id[THREAD_COUNT];
int thread[THREAD_COUNT]; int i;
struct timeval start_time, end_time;
gettimeofday(&start_time, NULL );
if(OPERATION == 'F') {
for (i=0;i<THREAD_COUNT;i++) {
thread[i] = pthread_create(&(thread_id[i]),NULL,calculate_FLOPS,NULL);
}
} else {
for (i=0;i<THREAD_COUNT;i++) {
thread[i] = pthread_create(&(thread_id[i]),NULL,calculate_IOPS,NULL);
}
}
for (i=0;i<THREAD_COUNT;i++) {
pthread_join(thread_id[i], NULL);
}
gettimeofday(&end_time, NULL);
double total_time = ((end_time.tv_sec+(end_time.tv_usec/1000000.0))-(start_time.tv_sec+(start_time.tv_usec/1000000.0)));
double ops;
if (OPERATION == 'F') {
ops = (THREAD_COUNT*NUM_OPERATIONS*26)/total_time;
} else {
ops = (THREAD_COUNT*NUM_OPERATIONS*50)/total_time;
}
printf("CPU, %d threads, speed = %.2lf Giga %s\n",THREAD_COUNT,ops/1000000000,OPERATION == 'F'?"FLOPS/sec":"IOPS/sec");
}
int main(int argc, char *argv[]){
int arg;
while((arg=getopt(argc, argv, "ifo:t:")) != -1) {
switch(arg) {
case 'i':
OPERATION='I'; // for IOPS calculation
break;
case 'f':
OPERATION='F'; // for FLOPS calculation
break;
case 'o':
NUM_OPERATIONS=atoi(optarg);
case 't':
THREAD_COUNT=atoi(optarg);
break;
default:
break;
}
}
calculate_operations_per_sec();
return 0;
}