-
Notifications
You must be signed in to change notification settings - Fork 1
/
opt.cpp
259 lines (192 loc) · 5.98 KB
/
opt.cpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <algorithm>
#include <iostream>
#include <fstream>
#include <omp.h>
using namespace std;
double bad_base(int ** matrix, long int size){
double total_begin = omp_get_wtime();
for(int x = 0; x < size; x++){
for(int y = 0; y < size; y++){
matrix[y][x] = matrix[y][x] * 2;
}
}
double total_end = omp_get_wtime();
return double(total_end - total_begin);
}
double good_base(int ** matrix, long int size){
double total_begin = omp_get_wtime();
for(int x = 0; x < size; x++){
for(int y = 0; y < size; y++){
matrix[x][y] = matrix[x][y] * 2;
}
}
double total_end = omp_get_wtime();
return double(total_end - total_begin);
}
double cache_opt(int ** matrix, long int size, int blocksize){
double total_begin = omp_get_wtime();
for(int x = 0; x < size; x += blocksize){
for(int i = x; i < x + blocksize; i++){
for(int y = 0; y < size; y+= blocksize){
__builtin_prefetch(&matrix[i][y + blocksize]);
for(int j = y; j < y + blocksize; j++){
matrix[i][j] = matrix[i][j] * 2;
}
}
}
}
double total_end = omp_get_wtime();
return double(total_end - total_begin);
}
double good_openmp_cache_opt(int ** matrix, long int size, int blocksize){
double total_begin = omp_get_wtime();
#pragma omp parallel for
for(int x = 0; x < size; x++){
for(int y = 0; y < size; y++){
matrix[x][y] = matrix[x][y] * 2;
}
}
double total_end = omp_get_wtime();
return double(total_end - total_begin);
}
double bad_openmp_cache_opt(int ** matrix, long int size, int blocksize){
double total_begin = omp_get_wtime();
for(int x = 0; x < size; x++){
if(x + 1 < size){ __builtin_prefetch(matrix[x+1]); }
#pragma omp parallel for
for(int y = 0; y < size; y++){
matrix[x][y] = matrix[x][y] * 2;
}
}
double total_end = omp_get_wtime();
return double(total_end - total_begin);
}
double thrash_openmp(int ** matrix, long int size){
double total_begin = omp_get_wtime();
#pragma omp parallel for
for(int x = 0; x < size; x++){
for(int y = 0; y < size; y++){
matrix[y][x] = matrix[y][x] * 2;
}
}
double total_end = omp_get_wtime();
return double(total_end - total_begin);
}
double possible_thrash_openmp(int ** matrix, long int size){
double total_begin = omp_get_wtime();
#pragma omp parallel for schedule(dynamic)
for(int x = 0; x < size; x++){
for(int y = 0; y < size; y++){
matrix[x][y] = matrix[x][y] * 2;
}
}
double total_end = omp_get_wtime();
return double(total_end - total_begin);
}
void cache(){
long int size = 32768;
size = 2048;
ofstream myfile;
myfile.open("speed_comparison.txt");
myfile << "Matrix Size,";
myfile << "Poor Base Case,";
myfile << "Proper Base Case,";
myfile << "'Supposed' Cache Optimized,";
myfile << "Poor OpenMP,";
myfile << "Thrashing OpenMP,";
myfile << "Correct OpenMP" << endl;
while(size > 16){
int blocksize = 1024;
while(blocksize % size == 0){ blocksize = blocksize >> 1; }
blocksize = blocksize >> 1;
int ** matrix;
matrix = new int* [size];
for(long int i = 0; i < size; i++)
matrix[i] = new int[size];
for(int x = 0; x < size; x++){
for(int y = 0; y < size; y++){
matrix[x][y] = 1;
}
}
double avg_good_base = 0;
double avg_bad_base = 0;
double avg_cache = 0;
double avg_good_mp = 0;
double avg_bad_mp = 0;
double avg_p_thrash_mp = 0;
double avg_thrash_mp = 0;
double num = 5.0;
for(int i = 0; i < num; i++){
avg_bad_base += bad_base(matrix, size);
avg_good_base += good_base(matrix, size);
avg_cache += cache_opt(matrix, size, blocksize);
avg_bad_mp += bad_openmp_cache_opt(matrix, size, blocksize);
avg_thrash_mp += thrash_openmp(matrix, size);
avg_good_mp += good_openmp_cache_opt(matrix, size, blocksize);
avg_p_thrash_mp += possible_thrash_openmp(matrix, size);
}
cout << "\nmatrix size: " << size << " x " << size << endl;
myfile << size << ",";
myfile << avg_bad_base / num << ",";
myfile << avg_good_base / num << ",";
myfile << avg_cache / num << ",";
myfile << avg_p_thrash_mp / num << ",";
myfile << avg_thrash_mp / num << ",";
myfile << avg_bad_mp / num << ",";
myfile << avg_good_mp / num << endl;
size = size >> 1;
}
myfile.close();
}
void openmp(){
long int size = 32768;
int min = 2048;
ofstream myfile;
myfile.open("mp_comparison.txt");
myfile << "Matrix Size,";
myfile << "Poor OpenMP,";
myfile << "Thrashing OpenMP,";
myfile << "'Possible' Thrashing OpenMP,";
myfile << "Correct OpenMP" << endl;
while(size > min){
int blocksize = 1024;
while(blocksize % size == 0){ blocksize = blocksize >> 1; }
blocksize = blocksize >> 1;
int ** matrix;
matrix = new int* [size];
for(long int i = 0; i < size; i++)
matrix[i] = new int[size];
for(int x = 0; x < size; x++){
for(int y = 0; y < size; y++){
matrix[x][y] = 1;
}
}
double avg_good_mp = 0;
double avg_bad_mp = 0;
double avg_thrash_mp = 0;
double avg_p_thrash_mp = 0;
double num = 5.0;
for(int i = 0; i < num; i++){
avg_bad_mp += bad_openmp_cache_opt(matrix, size, blocksize);
avg_thrash_mp += thrash_openmp(matrix, size);
avg_good_mp += good_openmp_cache_opt(matrix, size, blocksize);
avg_p_thrash_mp += possible_thrash_openmp(matrix, size);
}
cout << "\nmatrix size: " << size << " x " << size << endl;
myfile << size << ",";
myfile << avg_bad_mp / num << ",";
myfile << avg_thrash_mp / num << ",";
myfile << avg_p_thrash_mp / num << ",";
myfile << avg_good_mp / num << endl;
for (int i = 0; i < size; i++)
delete [] matrix[i];
delete matrix;
size = size - min;
}
myfile.close();
}
int main(){
cache();
openmp();
return 0;
}