-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_lab.c
More file actions
149 lines (121 loc) · 3.91 KB
/
cache_lab.c
File metadata and controls
149 lines (121 loc) · 3.91 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*
* Cache Behavior Lab
*
* This program demonstrates how memory access patterns affect performance.
* You will implement two functions that perform identical computation
* (summing all elements in a matrix) but traverse the data differently.
*/
// Global matrix and size for current test
int** matrix = NULL;
int current_size = 0;
/*
* Allocates a square matrix of the given size.
* Returns 1 on success, 0 on failure.
*/
int allocate_matrix(int size) {
matrix = (int**)malloc(size * sizeof(int*));
if (matrix == NULL) return 0;
for (int i = 0; i < size; i++) {
matrix[i] = (int*)malloc(size * sizeof(int));
if (matrix[i] == NULL) return 0;
// Initialize with values
for (int j = 0; j < size; j++) {
matrix[i][j] = (i + j) % 100;
}
}
current_size = size;
return 1;
}
/*
* Frees the matrix memory.
*/
void free_matrix() {
if (matrix == NULL) return;
for (int i = 0; i < current_size; i++) {
free(matrix[i]);
}
free(matrix);
matrix = NULL;
current_size = 0;
}
/*
* Traverses the matrix in ROW-MAJOR order.
* Visits every element in row 0, then row 1, then row 2, etc.
* Returns the sum of all elements.
*
* TODO: Implement this function.
* Hint: The outer loop should iterate over rows, the inner loop over columns.
*/
long long traverse_row_major() {
long long sum = 0;
// ============================================
// TODO: Implement row-major traversal
// Visit elements in order: [0][0], [0][1], [0][2], ... [1][0], [1][1], ...
// ============================================
return sum;
}
/*
* Traverses the matrix in COLUMN-MAJOR order.
* Visits every element in column 0, then column 1, then column 2, etc.
* Returns the sum of all elements.
*
* TODO: Implement this function.
* Hint: The outer loop should iterate over columns, the inner loop over rows.
*/
long long traverse_column_major() {
long long sum = 0;
// ============================================
// TODO: Implement column-major traversal
// Visit elements in order: [0][0], [1][0], [2][0], ... [0][1], [1][1], ...
// ============================================
return sum;
}
/*
* Measures execution time of a traversal function in milliseconds.
*/
double measure_time(long long (*traverse_func)(), long long* result) {
clock_t start = clock();
*result = traverse_func();
clock_t end = clock();
return ((double)(end - start) / CLOCKS_PER_SEC) * 1000.0;
}
/*
* Runs performance test for a given matrix size.
*/
void run_test(int size) {
printf("\n--- Matrix Size: %d x %d ---\n", size, size);
if (!allocate_matrix(size)) {
printf("Failed to allocate matrix of size %d\n", size);
return;
}
long long row_sum, col_sum;
double row_time = measure_time(traverse_row_major, &row_sum);
double col_time = measure_time(traverse_column_major, &col_sum);
printf("Row-major: %.2f ms (sum: %lld)\n", row_time, row_sum);
printf("Column-major: %.2f ms (sum: %lld)\n", col_time, col_sum);
if (row_time > 0) {
printf("Ratio (col/row): %.2fx\n", col_time / row_time);
}
// Verify both methods produce the same result
if (row_sum != col_sum) {
printf("WARNING: Sums don't match! Check your implementations.\n");
}
free_matrix();
}
int main() {
printf("Cache Behavior Lab\n");
printf("==================\n");
printf("Comparing row-major vs column-major matrix traversal\n");
// Test with increasing matrix sizes
int sizes[] = {1000, 2000, 4000, 8000};
int num_sizes = sizeof(sizes) / sizeof(sizes[0]);
for (int i = 0; i < num_sizes; i++) {
run_test(sizes[i]);
}
printf("\n==================\n");
printf("Tests complete.\n");
return 0;
}