summaryrefslogtreecommitdiff
path: root/main.c
blob: 99a0eca3827ce095fa8667b664bfb1568060e58d (plain)
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
#ifdef USE_VECTOR
#include <immintrin.h>
#endif
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define STRIDE (256/32) // How many integers fit in a 256 vector register
#define VSIZE (STRIDE*200000)

int main(int argc, char *argv[]) {

	float start, end, duration;

	int *v1;
	posix_memalign((void**)&v1, 32, VSIZE * sizeof(int));
	int *v2;
	posix_memalign((void**)&v2, 32, VSIZE * sizeof(int));
	int result[VSIZE];

#ifndef USE_VECTOR
	printf("Sequential..");
	start = (float)clock()/CLOCKS_PER_SEC;
	for(int i=0;i<VSIZE;i++){
		result[i]=v1[i]+v2[i];
	}
	end = (float)clock()/CLOCKS_PER_SEC;
	duration= end - start;
	printf(" done! (duration=%.6fs)\n", duration);
#else
	printf("Vectorize...");
	__m256i a,b;
	// e == extended (for historical reasons just brandy name)
	// p == packed (multiple packed elements)
	// i == integer
	// 32 == each integer is 32bits
	__m256i c = _mm256_add_epi32(a, b);
	start = (float)clock()/CLOCKS_PER_SEC;
	for(int i=0;i<VSIZE;i+=STRIDE){
		a = _mm256_load_si256((__m256i*)&v1[i]);
		b = _mm256_load_si256((__m256i*)&v2[i]);
		c = _mm256_add_epi32(a,b);

		_mm256_store_si256((__m256i*)&result[i],c);
	}
	end = (float)clock()/CLOCKS_PER_SEC;
	duration= end - start;
	printf(" done! (duration=%.6fs)\n", duration);
#endif

	free(v1);
	free(v2);

	return 0;
}