-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_copy.cpp
157 lines (128 loc) · 3.05 KB
/
matrix_copy.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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
#include <chrono>
#define N_TRIALS 4
// To reduce spikes, each b will be retested N_TRIALS times, and an averege will be performed
#define SIZE 12
// It means n = 2^SIZE
#define MIN_BLOCK 2
#define MAX_BLOCK 8
// They mean b can vary from 2^MIN_BLOCK to 2^MAX_BLOCK
using namespace std;
float matrix_copy(int, int);
float routine1(float**, float**, int, int);
int main()
{
srand(time(NULL));
ofstream report_file("report_matrix_copy.csv");
float execution_time;
int i, j;
int n = 1; // 2^0
for (i=0;i<SIZE;++i) n *= 2;
int b = 1;
for (i=0;i<MIN_BLOCK-1;++i) b *= 2;
report_file << fixed << setprecision(6);
report_file << "size,block,time" << endl;
for (i=MIN_BLOCK;i<=MAX_BLOCK;++i){
b *= 2;
execution_time = 0;
for (j=0;j<N_TRIALS;++j) execution_time += matrix_copy(n, b) * (1.0 / N_TRIALS);
//report_file << "size: " << n << ", block: " << setw(3) << b << ", time: " << execution_time << " s" << endl;
report_file << n << "," << b << "," << execution_time << endl;
}
report_file.close();
return 0;
}
float matrix_copy(int n, int b)
{
int i, j;
float execution_time;
float **M, **O;
M = new float*[n];
for (i=0;i<n;++i){
M[i] = new float[n];
}
O = new float*[n];
for (i=0;i<n;++i){
O[i] = new float[n];
}
for (i=0;i<n;++i){
for (j=0;j<n;++j){
M[i][j] = (float)rand() / (float)rand();
}
}
/* Debug *
cout << fixed << setprecision(5);
cout << "Initial matrix M:" << endl;
for (i=0;i<n;++i){
for (j=0;j<n;++j){
cout << M[i][j] << " ";
}
cout << endl;
}
cout << endl;
/**/
execution_time = routine1(M, O, n, b);
/* Debug *
cout << "Reverse matrix O:" << endl;
for (i=0;i<n;++i){
for (j=0;j<n;++j){
cout << O[i][j] << " ";
}
cout << endl;
}
cout << endl;
/**/
for (i=0;i<n;++i){
delete[] M[i];
}
delete[] M;
for (i=0;i<n;++i){
delete[] O[i];
}
delete[] O;
/* Debug *
cout << fixed << setprecision(6);
cout << "Execution time: " << execution_time << " s" << endl;
/**/
return execution_time;
}
float routine1(float** M, float** O, int n, int b)
{
int i, j, ib, jb;
float execution_time;
int a = n / b;
/* Debug *
for (i=0;i<a;++i){ // for each block
for (j=0;j<a;++j){
cout << "Block i=" << i << " j=" << j << ":" << endl;
for (ib=0;ib<b;++ib){ // for each element of the block
for (jb=0;jb<b;++jb){
cout << M[i*b+ib][j*b+jb] << " ";
}
cout << endl;
}
cout << endl;
}
}
/**/
//cout << "For loop start" << endl; // Debug
auto start_time = chrono::high_resolution_clock::now();
for (i=0;i<a;++i){ // for each block
for (j=0;j<a;++j){
for (ib=0;ib<b;++ib){ // for each element of the block
for (jb=0;jb<b;++jb){
O[(a-1-i)*b+ib][(a-1-j)*b+jb] = M[i*b+ib][j*b+jb];
}
}
}
}
auto end_time = chrono::high_resolution_clock::now();
//cout << "For loop end" << endl << endl; // Debug
auto difference_time = chrono::duration_cast<chrono::microseconds>(end_time - start_time);
execution_time = difference_time.count() * 1e-6;
return execution_time;
}