-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmain.c
94 lines (80 loc) · 2.77 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#include <assert.h>
#include <xmmintrin.h>
#define TEST_W 4096
#define TEST_H 4096
/* provide the implementations of naive_transpose,
* sse_transpose, sse_prefetch_transpose
*/
#include "impl.c"
static long diff_in_us(struct timespec t1, struct timespec t2)
{
struct timespec diff;
if (t2.tv_nsec-t1.tv_nsec < 0) {
diff.tv_sec = t2.tv_sec - t1.tv_sec - 1;
diff.tv_nsec = t2.tv_nsec - t1.tv_nsec + 1000000000;
} else {
diff.tv_sec = t2.tv_sec - t1.tv_sec;
diff.tv_nsec = t2.tv_nsec - t1.tv_nsec;
}
return (diff.tv_sec * 1000000.0 + diff.tv_nsec / 1000.0);
}
int main(int argc, char *argv[])
{
/* verify the result of 4x4 matrix */
{
int testin[16] = { 0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15
};
int testout[16];
int expected[16] = { 0, 4, 8, 12, 1, 5, 9, 13,
2, 6, 10, 14, 3, 7, 11, 15
};
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++)
printf(" %2d", testin[y * 4 + x]);
printf("\n");
}
printf("\n");
sse_transpose(testin, testout, 4, 4);
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++)
printf(" %2d", testout[y * 4 + x]);
printf("\n");
}
assert(0 == memcmp(testout, expected, 16 * sizeof(int)) &&
"Verification fails");
}
{
struct timespec start, end;
int *src = (int *) malloc(sizeof(int) * TEST_W * TEST_H);
int *out0 = (int *) malloc(sizeof(int) * TEST_W * TEST_H);
int *out1 = (int *) malloc(sizeof(int) * TEST_W * TEST_H);
int *out2 = (int *) malloc(sizeof(int) * TEST_W * TEST_H);
srand(time(NULL));
for (int y = 0; y < TEST_H; y++)
for (int x = 0; x < TEST_W; x++)
*(src + y * TEST_W + x) = rand();
clock_gettime(CLOCK_REALTIME, &start);
sse_prefetch_transpose(src, out0, TEST_W, TEST_H);
clock_gettime(CLOCK_REALTIME, &end);
printf("sse prefetch: \t %ld us\n", diff_in_us(start, end));
clock_gettime(CLOCK_REALTIME, &start);
sse_transpose(src, out1, TEST_W, TEST_H);
clock_gettime(CLOCK_REALTIME, &end);
printf("sse: \t\t %ld us\n", diff_in_us(start, end));
clock_gettime(CLOCK_REALTIME, &start);
naive_transpose(src, out2, TEST_W, TEST_H);
clock_gettime(CLOCK_REALTIME, &end);
printf("naive: \t\t %ld us\n", diff_in_us(start, end));
free(src);
free(out0);
free(out1);
free(out2);
}
return 0;
}