-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinteger.c
86 lines (75 loc) · 2.73 KB
/
integer.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
#include <xmmintrin.h> //Header for intrinsic functions
#include <stdio.h>
#include <time.h>
#include <x86intrin.h>
#include <smmintrin.h>
int main()
{
// Variables definition and initialization
int MAX_DIM = 100;
int a[MAX_DIM][MAX_DIM] __attribute__ ((aligned(16)));
int b[MAX_DIM][MAX_DIM] __attribute__ ((aligned(16)));
int c[MAX_DIM][MAX_DIM] __attribute__ ((aligned(16)));
int d[MAX_DIM][MAX_DIM] __attribute__ ((aligned(16)));
for (int i = 0; i < MAX_DIM; ++i)
{
for (int j = 0; j < MAX_DIM; ++j)
{
a[i][j] = 13; // Arbitrary initializations - Replace to test your multiplication
b[i][j] = 22; // Arbitrary initializations - Replace to test your multiplication
c[i][j] = 0; // Necessary Initialization - Don't change
d[i][j] = 0; // Necessary Initialization - Don't change
}
}
// Unoptimized Matrix Multiplication
clock_t Time1 = clock();
for (int i = 0; i < MAX_DIM; ++i)
{
for (int j = 0; j < MAX_DIM; ++j)
{
for (int k = 0; k < MAX_DIM; k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
clock_t Time2 = clock();
//=================================================================================================
clock_t Time3 = clock();
/*
float *p1 ;float *p2;float *p3;
p1 =&a[0];
p2 =&b[0];
p3 =&c[0];
float* m; float* v; float* result;
m =&a[0];
v =&b[0];
result =&c[0];
*/
for(int i=0;i<MAX_DIM;i+=1){
for(int j=0;j<MAX_DIM;j+=4){
for(int k=0;k<MAX_DIM;k+=2){
__m128i result = _mm_load_si128(&d[i][j]);
__m128i a_line = _mm_load_si128(&a[i][k]);
__m128i b_line0 = _mm_load_si128(&b[k][j+0]);
__m128i b_line1 = _mm_load_si128(&b[k][j+1]);
__m128i b_line2 = _mm_load_si128(&b[k][j+2]);
__m128i b_line3 = _mm_load_si128(&b[k][j+3]);
result = _mm_add_epi16(result, _mm_mul_ps(_mm_shuffle_ps(a_line, a_line, 0x00), b_line0));
result = _mm_add_epi16(result, _mm_mul_ps(_mm_shuffle_ps(a_line, a_line, 0x55), b_line1));
result = _mm_add_epi16(result, _mm_mul_ps(_mm_shuffle_ps(a_line, a_line, 0xaa), b_line2));
result = _mm_add_epi16(result, _mm_mul_ps(_mm_shuffle_ps(a_line, a_line, 0xff), b_line3));
_mm_store_si128(&d[i][j],result);
}
}
}
// YOUR CODE HERE
clock_t Time4 = clock();
// Calculate and print execution times
double TotalTimeLoop = ((double) Time2 - (double) Time1) / CLOCKS_PER_SEC;
double TotalTimeSIMD = ((double) Time4 - (double) Time3) / CLOCKS_PER_SEC;
printf("Performance gain is %.7f \n", TotalTimeLoop/TotalTimeSIMD);
printf(" Time taken by loop is %.7f \n", TotalTimeLoop);
printf(" Time taken by SIMD optimized code is %.7f \n", TotalTimeSIMD);
return 0;
}