-
Notifications
You must be signed in to change notification settings - Fork 2
/
simulate_driver_generate_Overlap_Iterations.cpp
221 lines (197 loc) · 7.09 KB
/
simulate_driver_generate_Overlap_Iterations.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
/************************************************
*
* Created: 20th July, 2017
* Updated: 27th July, 2017
*
************************************************/
#include <errno.h>
#include <omp.h>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <random>
#include <string>
#include <vector>
#include "Matrix.h"
#include "analysis.h"
#include "apply_Htarget.h"
//#define DEBUG_PATCHES 0
int main(int argc, char **argv) {
char buf[256];
int nrow, ncol, nnz;
int ii, jj;
double val;
int n_c_rows = 0;
int n_c_cols = 0;
int vec_size = 0;
auto C = new Block_Matrix_t;
if (argc != 5) {
printf(
"Format: simulate_driver_generate SYSTEM_SIZE NUM_STATES "
"SWEEP_LOCATION NUM_K_MAT\n");
exit(1);
}
const int is_grow_left = 1;
const int max_keep_states = std::stoi(std::string(argv[2]));
const int sys_size = std::stoi(std::string(argv[1]));
int keep_left_states = 0, keep_right_states = 0;
if (is_grow_left) {
// left side is growing
keep_left_states = 4*max_keep_states;
keep_right_states = max_keep_states;
}
else {
// right side is growing
keep_left_states = max_keep_states;
keep_right_states = 4*max_keep_states;
};
const int left_size = std::stoi(std::string(argv[3]));
const int NUM_K_MAT = std::stoi(std::string(argv[4]));
const int right_size = sys_size - left_size;
const int target_up = (left_size + right_size) / 2;
const int target_down = target_up;
const int max_patches = (1 + target_up) * (1 + target_down);
int left_patch_size_[max_patches + 1];
int left_patch_up_[max_patches + 1];
int left_patch_down_[max_patches + 1];
int right_patch_size_[max_patches + 1];
int right_patch_up_[max_patches + 1];
int right_patch_down_[max_patches + 1];
int power_max_patches = (max_patches+1)*(max_patches+1);
int interaction_matrix_[power_max_patches];
for(int i = 0; i < power_max_patches; i++)
interaction_matrix_[i] = -1;
#define left_patch_size(i) left_patch_size_[(i)-1]
#define right_patch_size(i) right_patch_size_[(i)-1]
#define interaction_matrix(ipatch,jpatch) interaction_matrix_[ indx2f((ipatch+1),(jpatch+1),npatches) ]
int npatches = gen_patches_comb(
left_size, right_size, target_up, target_down, keep_left_states,
keep_right_states, left_patch_size_, right_patch_size_, left_patch_up_,
left_patch_down_, right_patch_up_, right_patch_down_, &interaction_matrix_[0]);
/* printf(
"system_size = %d left_states = %d right_states = %d left_size "
"= %d ",
sys_size, keep_left_states, keep_right_states, left_size);
printf(
"right_size = %d target_up = %d target_down = %d npatches = "
"%d\n",
right_size, target_up, target_down, npatches);
*/
double lower_bound = 0.0;
double upper_bound = 1.0;
std::uniform_real_distribution<double> unif(lower_bound, upper_bound);
std::default_random_engine re;
n_c_rows = npatches;
n_c_cols = n_c_rows;
C->cij.resize(n_c_rows);
for (auto &a : C->cij) {
a.resize(n_c_cols, nullptr);
}
std::vector<int> l_patch_size;
l_patch_size.reserve(n_c_rows);
std::vector<int> r_patch_size;
r_patch_size.reserve(n_c_rows);
#ifdef DEBUG_PATCHES
vec_size = 0;
for (int ic = 0; ic < npatches; ic++) {
vec_size += left_patch_size_[ic] * right_patch_size_[ic];
for (int jc = 0; jc < npatches; jc++) {
int nrowA, ncolA, nrowB, ncolB;
nrowA = l_patch_size[ic] = left_patch_size_[ic];
ncolA = l_patch_size[jc] = left_patch_size_[jc];
nrowB = r_patch_size[ic] = right_patch_size_[ic];
ncolB = r_patch_size[jc] = right_patch_size_[jc];
printf("%d %d A %d %d B %d %d \n", ic+1, jc+1, nrowA, ncolA, nrowB,
ncolB);
}
}
printf("vector_size = %d \n", vec_size);
printf("Interaction matrix :\n");
for (int ic = 0; ic < npatches; ic++){
for (int jc = 0; jc < npatches; jc++){
printf("%1d", interaction_matrix(ic, jc));
}
printf("\n");
}
exit(0);
#endif
// for (auto line = 0; line < n_c_rows * n_c_cols; line++) {
for (int ic = 0; ic < npatches; ic++) {
vec_size += left_patch_size_[ic] * right_patch_size_[ic];
for (int jc = 0; jc < npatches; jc++) {
if (interaction_matrix(ic, jc) == 0){
continue;
}
int nrowA, ncolA, nrowB, ncolB;
nrowA = l_patch_size[ic] = left_patch_size_[ic];
ncolA = l_patch_size[jc] = left_patch_size_[jc];
nrowB = r_patch_size[ic] = right_patch_size_[ic];
ncolB = r_patch_size[jc] = right_patch_size_[jc];
//printf("%d %d A %d %d B %d %d \n", ic+1, jc+1, nrowA, ncolA, nrowB,
// ncolB);
if (C->cij[ic][jc] == nullptr) {
C->cij[ic][jc] = new CIJ_Elem_t;
}
CIJ_Elem celem = C->cij[ic][jc];
celem->A.resize(NUM_K_MAT);
celem->B.resize(NUM_K_MAT);
for (int k = 0; k < NUM_K_MAT; k++) {
auto newmat = new Matrix_t;
newmat->nrow = nrowA;
newmat->ncol = ncolA;
newmat->is_dense = true;
newmat->val.resize(nrowA * ncolA);
for (int ii = 0; ii < nrowA * ncolA; ii++) {
newmat->val[ii] = ii * ii;
}
celem->A[k] = newmat;
auto newmatB = new Matrix_t;
newmatB->nrow = nrowB;
newmatB->ncol = ncolB;
newmatB->is_dense = true;
newmatB->val.resize(nrowB * ncolB);
for (int ii = 0; ii < nrowB * ncolB; ii++) {
newmatB->val[ii] = ii * ii;
}
celem->B[k] = newmatB;
}
}
}
std::vector<double> X, Y;
X.reserve(vec_size);
Y.reserve(vec_size);
for (int i = 0; i < vec_size; i++) {
X[i] = unif(re);
Y[i] = 0.0;
}
//New code to overlap iterations
int patch = (*C).cij[0].size();
std::vector<int> vsize(patch);
std::vector<int> vstart(patch);
int ip = 0;
for(int i = 0; i < patch; i++){
int nrows = l_patch_size[i] * r_patch_size[i];
vsize[i] = nrows;
vstart[i] = ip;
ip += nrows;
}
double start, end, t, inicio, fin;
inicio = omp_get_wtime();
int trials = 1;
//#pragma omp parallel
//#pragma omp single
for (int num = 0; num < trials; num++) {
//start = omp_get_wtime();
apply_Htarget(*C, vsize, vstart, X, Y);
//end = omp_get_wtime();
//t = end - start;
//std::cout << " Execution Time it" << num << ": " << t << std::endl;
}
//for(int i = 0; i < vec_size; i++) std::cout << Y[i] << std::endl;
//for(int i = 0; i < vec_size; i++) std::cout << Y[i] << ' ';
//std::cout << std::endl;
fin = omp_get_wtime();
t = fin - inicio;
std::cout << " Execution Time: " << t << std::endl;
}