forked from embedded2015/prefetcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
133 lines (111 loc) · 4.32 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#include <assert.h>
#define TEST_W 4096
#define TEST_H 4096
/* provide the implementations of naive_transpose,
* sse_transpose, sse_prefetch_transpose
*/
#include "impl.h"
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 */
{
if(argc != 2) {
fprintf(stderr, "Usage: ./main test_matrix_raw\nExample: ./main 32\n");
exit(-1);
}
int rows, cols, size;
rows = cols = atoi(argv[1]);
size = rows * cols;
int* testin =(int *) malloc(sizeof(int) * size);
int* testout =(int *) malloc(sizeof(int) * size);
int* expected =(int *) malloc(sizeof(int) * size);
/*init testin and expected*/
for (int y = 0; y < rows; y++)
for (int x = 0; x < cols; x++)
testin[y * cols + x] = y * cols + x;
for (int x = 0; x < cols; x++)
for (int y = 0; y < rows; y++)
expected[y * cols + x] = x * rows + y;
/*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 < rows; y++) {
for (int x = 0; x < cols; x++)
printf(" %2d", testin[y * cols + x]);
printf("\n");
}
printf("\n");
//sse_transpose(testin, testout, cols, rows);
//avx_transpose(testin, testout, cols, rows);
//sse_pthread_transpose(testin, testout, cols, rows, 2);
sse_unpack32_transpose(testin, testout, cols, rows);
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++)
printf(" %2d", testout[y * cols + x]);
printf("\n");
}
assert(0 == memcmp(testout, expected, size * 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);
sse_unpack32_transpose(src, out1, TEST_W, TEST_H);
clock_gettime(CLOCK_REALTIME, &end);
printf("sse unpack32: \t\t %ld us\n", diff_in_us(start, end));
clock_gettime(CLOCK_REALTIME, &start);
sse_pthread_transpose(src, out1, TEST_W, TEST_H, 8);
clock_gettime(CLOCK_REALTIME, &end);
printf("sse pthread: \t %ld us\n", diff_in_us(start, end));
clock_gettime(CLOCK_REALTIME, &start);
sse_pthread_transpose(src, out1, TEST_W, TEST_H, 8);
clock_gettime(CLOCK_REALTIME, &end);
printf("sse pthread prefetch: \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;
}