-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.cpp
156 lines (128 loc) · 4.08 KB
/
Controller.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
#include "Controller.h"
using namespace std;
Controller::Controller(vector<int>* datos, string info, int option){
this->datos = datos;
seqSize = datos->size();
string pathaux = info;
if(option == 0){
key_output_seq.open("./output/keys/keys_seq" + pathaux + ".txt", std::ofstream::out | std::ofstream::trunc); //limpia el contenido del fichero
key_output_seq.clear();
}
else{
key_output_par.open("./output/keys/keys_par" + pathaux + ".txt", std::ofstream::out | std::ofstream::trunc); //limpia el contenido del fichero
key_output_par.clear();
}
}
Controller::~Controller(){
key_output_par.close();
key_output_seq.close();
}
void Controller::Sequential(){
if(seqSize > maxSeq){
unsigned int parts = ( seqSize / maxSeq ) + 1;
vector< vector<int> > subDatos(parts, vector<int>(0,0));
cout << "Sequential steps: " << parts << endl;
long long int k = 0;
for(int i = 0; i < parts; i++){
if(k == seqSize) break;
for(int j = 0; j < maxSeq; j++){
if(k == seqSize) break;
subDatos.at(i).push_back(datos->at(k));
k++;
}
}
Repair* r;
vector<DList*> res(parts, new DList());
for(int i = 0 ; i < parts; i++){
cout << "Step: " << i << endl;
r = new Repair(&subDatos.at(i), &key_output_seq);
r->cambiar();
res.at(i) = r->getSeq();
delete(r);
}
for(int i = parts-1; i>0; i--){
if(res.at(i)->size() == 0) continue;
if(res.at(i)->size()==1){
res.at(i-1)->insertEnd(res.at(i)->first()->getNum());
continue;
}
mergeDList(res.at(i-1), res.at(i));
res.pop_back();
}
resSeq = res.at(0);
r = new Repair(res.at(0), &key_output_par);
int count = 0;
while(r->Compressible()){
r->cambiar();
resSeq = r->getSeq();
if(r!=NULL) delete(r);
r = new Repair(resSeq, &key_output_par);
cout << "ok "<< count++ << endl;
}
if(r!=NULL) delete(r);
}
else{
Repair* r = new Repair(datos, &key_output_seq);
r->cambiar();
resSeq = r->getSeq();
delete(r);
return;
}
}
void Controller::Parallel(int nThreads){
unsigned long long int nDatos = seqSize;
unsigned int nParts = nThreads;
unsigned int partSize = nDatos/nParts;
while(partSize > maxSeq){
nParts++;
partSize = nDatos/nParts;
}
vector< vector<int> > subDatos(nParts, vector<int>(0,0));
cout << "Parallel parts: " << nParts << ", size: " << partSize << endl;
unsigned long long int k = 0;
for(int i = 0; i < nParts; i++){
for(int j = 0; j < partSize; j++){
subDatos.at(i).push_back(datos->at(k));
k++;
}
}
while(k!=datos->size()){ //relleno el ultimo con los datos que queden.
subDatos.at(nParts-1).push_back(datos->at(k));
k++;
}
vector<DList*> res(nParts, new DList());
omp_set_num_threads(nThreads);
for(int i = 0; i < nParts; i+=nThreads){
#pragma omp parallel for
for(int j = i; j < nThreads; j++){
Repair* r = new Repair(&subDatos.at(j), &key_output_par);
r->cambiar();
res.at(j) = r->getSeq();
delete(r);
}
}
for(int i = nThreads-1; i>0; i--){
mergeDList(res.at(i-1), res.at(i));
res.pop_back();
}
Repair* r = NULL;
r = new Repair(res.at(0), &key_output_par);
while(r->Compressible()){
r->cambiar();
resSeq = r->getSeq();
if(r!=NULL) delete(r);
r = new Repair(resSeq, &key_output_par);
}
if(r!=NULL) delete(r);
return;
}
void Controller::mergeDList(DList* a, DList* b){
Nodo* tailA = a->back();
Nodo* firstB = b->first();
a->merge(a,b);
tailA->setNext(firstB);
firstB->setPrev(tailA);
}
DList* Controller::getSeq(){
return resSeq;
}