-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
482 lines (438 loc) · 11.9 KB
/
main.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
* File: main.cpp
* Author: gregy
*
* otazky: musi se lokalne seradit pred kazdym krokem shearsortu?
*/
#include <cstdlib>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <string.h>
#include <omp.h>
#include <sstream>
#define VTYPE int
using namespace std;
class CLIArgumentsParser {
public:
static char* getCmdOption(char ** begin, char ** end, const std::string & option)
{
char ** itr = std::find(begin, end, option);
if (itr != end && ++itr != end)
{
return *itr;
}
return 0;
}
static bool cmdOptionExists(char** begin, char** end, const std::string& option)
{
return std::find(begin, end, option) != end;
}
};
class CPU {
vector<VTYPE>::iterator startdata;
vector<VTYPE>::iterator enddata;
vector<VTYPE> temp;
public:
CPU(vector<VTYPE>::iterator startdata, vector<VTYPE>::iterator enddata, size_t maxbucket):temp(2*maxbucket) {
this->startdata = startdata;
this->enddata = enddata;
}
void sort(bool reverse) {
if(reverse)
std::sort(this->startdata, this->enddata, std::greater<VTYPE>());
else
std::sort(this->startdata, this->enddata);
}
void mergesplit(CPU * other, bool reverse) {
if(reverse)
std::merge(this->startdata, this->enddata, other->startdata, other->enddata, this->temp.begin(), greater<VTYPE>());
else
std::merge(this->startdata, this->enddata, other->startdata, other->enddata, this->temp.begin());
vector<VTYPE>::iterator tempit = this->temp.begin();
for(vector<VTYPE>::iterator i = this->startdata; i != this->enddata; ++i,++tempit) {
*i = *tempit;
}
for(vector<VTYPE>::iterator i = other->startdata; i != other->enddata; ++i,++tempit) {
*i = *tempit;
}
}
void printData(char delim = ' ', ostream & out = cout) {
for(vector<VTYPE>::iterator i = this->startdata;i!=this->enddata; i++) {
out << *i << delim;
}
}
void printDataReverse(char delim = ' ', ostream & out = cout) {
if(this->startdata == this->enddata)
return;
for(vector<VTYPE>::iterator i = this->enddata-1;i!=this->startdata; i--) {
out << *i << delim;
}
out << *(this->startdata) << delim;
}
};
class Coordinates {
public:
int x;
int y;
int z;
Coordinates(int x, int y, int z) {
this->x = x;
this->y = y;
this->z = z;
}
Coordinates& operator+=(const Coordinates& rhs) {
this->x += rhs.x;
this->y += rhs.y;
this->z += rhs.z;
return *this;
}
Coordinates& operator*=(const int& rhs) {
this->x *= rhs;
this->y *= rhs;
this->z *= rhs;
return *this;
}
Coordinates operator+(const Coordinates& rhs) const{
Coordinates n = (*this);
n += rhs;
return n;
}
Coordinates operator*(const int& rhs) const{
Coordinates n = (*this);
n *= rhs;
return n;
}
};
static const Coordinates XVector(1,0,0);
static const Coordinates YVector(0,1,0);
static const Coordinates ZVector(0,0,1);
class CPUMatrix {
size_t x,y,z = 0;
vector<CPU*> CPUs;
size_t bucket = 0;
public:
CPUMatrix(char * dimensions, vector<VTYPE> &numbers) {
stringstream ss;
ss<< dimensions;
int x = 1,y = 1,z = 1;
ss >> x;
if(ss.peek() == 'x') {
ss.ignore();
}
else {
throw "spatny format dimenzi mantice ma byt NxN[xN]";
}
ss >> y;
if(ss.good()) {
if(ss.peek() == 'x') {
ss.ignore();
}
else {
throw "spatny format dimenzi mantice ma byt NxN[xN]";
}
ss >> z;
}
cout << "Dimenze jsou " << x << "x" << y <<"x"<<z<<endl;
if(x<1 || y<1 || z<1) {
throw "Dimenze musi byt kladne!";
}
this->CPUs.resize(x*y*z);
init(x,y,z, numbers);
}
CPUMatrix(int x, int y, int z, vector<VTYPE> &numbers): CPUs(x*y*z) {
init(x,y,z,numbers);
}
void init(int x, int y, int z, vector<VTYPE> &numbers) {
this->x = x;
this->y=y;
this->z = z;
if(numbers.size() < CPUs.size()) {
cout << "Grid procesoru je vetsi nez pocet cisel...fail" << endl;
throw "grid procesoru je vetsi nez pocet cisel";
}
size_t bucket = numbers.size()/this->CPUs.size()+ (numbers.size() % this->CPUs.size() != 0);
this->bucket = bucket;
int pos = 0;
for(vector<CPU*>::iterator i = this->CPUs.begin(); i!=this->CPUs.end(); ++i,++pos) {
//handle crazy case where there are cpus with no numbers
if(pos*bucket > numbers.size()) {
cout << "Warning, cpus without work..." << endl;
*i = new CPU(numbers.end(), numbers.end(), bucket);
}
else if((pos+1)*bucket > numbers.size()) {
*i = new CPU(numbers.begin()+pos*bucket, numbers.end(), bucket);
}
else {
*i = new CPU(numbers.begin()+pos*bucket, numbers.begin()+(pos+1)*bucket, bucket);
}
}
}
CPU*& get(size_t x, size_t y, size_t z) {
if(!this->coorValid(x,y,z)) {
throw "mimo rozmer procesoru";
}
return CPUs.at(x + y * this->x + z * this->x * this->y);
}
CPU*& get(Coordinates c) {
return this->get(c.x,c.y,c.z);
}
size_t getBucketSize() {
return this->bucket;
}
bool coorValid(size_t x, size_t y, size_t z) {
if(x>=this->x || y>=this->y || z>=this->z) {
return false;
}
return true;
}
CPU*& get(size_t i) {
return CPUs.at(i);
}
size_t size(){
return this->CPUs.size();
}
size_t sizeForVector(Coordinates vector) {
return this->x*vector.x+this->y*vector.y+this->z*vector.z;
}
void printMatrix() {
for(size_t z=0;z<this->z; z++) {
cout << "-----------------------" << endl;
for(int y = this->y-1;y>=0;y--) {
cout << endl;
for(size_t x=0;x<this->x;x++) {
cout << "|";
this->get(x,y,z)->printData();
cout << "|";
}
cout << endl;
}
cout << "-----------------------" << endl;
}
}
};
size_t logbin(size_t number) {
size_t ret = 0;
while (number >>= 1) { ++ret; }
return ret;
}
void eotSortOpenMP(Coordinates line, Coordinates dynvector, CPUMatrix * matrix, bool reverse = false, bool justOneRun = false) {
size_t dimension_size = matrix->sizeForVector(dynvector);
//locally sort
#pragma omp parallel num_threads(dimension_size)
{
#pragma omp for
for(size_t i=0; i<dimension_size; i++) {
matrix->get(line+dynvector*i)->sort(reverse);
}
}
size_t runtimes;
if(justOneRun) {
runtimes = 1;
}
else {
runtimes = dimension_size/2+1;
}
//TODO: prozkoumat to +1
for(size_t count=0;count<runtimes;count++) {
#pragma omp parallel num_threads(dimension_size/2)
{
//even pairs
#pragma omp for
for(size_t i =0; i<dimension_size-1; i+=2) {
matrix->get(line+dynvector*i)->mergesplit(matrix->get(line+dynvector*(i+1)), reverse);
}
#pragma omp barrier
//odd pairs
#pragma omp for
for(size_t i=1; i<dimension_size-1; i+=2) {
matrix->get(line+dynvector*i)->mergesplit(matrix->get(line+dynvector*(i+1)), reverse);
}
}
}
}
void shearSortOpenMP(Coordinates plane, Coordinates rowvector, Coordinates colvector, CPUMatrix * matrix, bool reverse = false) {
size_t rowsize = matrix->sizeForVector(rowvector);
size_t colsize = matrix->sizeForVector(colvector);
for(size_t count=0;count<logbin(colsize)+2;count++) {
//faze schvalne opacne, posledni musi vzdy byt snake faze
#pragma omp parallel num_threads(rowsize)
{
//down phase
#pragma omp for
for(size_t i=0; i<rowsize; i++) {
eotSortOpenMP(plane+rowvector*i, colvector, matrix, reverse);
}
}
#pragma omp parallel num_threads(colsize)
{
//snake phase
#pragma omp for
for(size_t i =0; i<colsize; i++) {
if(i%2==0)
eotSortOpenMP(plane+colvector*i, rowvector, matrix, reverse);
else
eotSortOpenMP(plane+colvector*i, rowvector, matrix, !reverse);
}
}
}
}
void dSortOpenMP(CPUMatrix * matrix, bool reverse = false) {
size_t xsize = matrix->sizeForVector(XVector);
size_t ysize = matrix->sizeForVector(YVector);
size_t zsize = matrix->sizeForVector(ZVector);
Coordinates zero(0,0,0);
#pragma omp parallel num_threads(zsize)
{
#pragma omp for
for(size_t xyplane=0;xyplane<zsize;xyplane++) {
shearSortOpenMP(zero+ZVector*xyplane, XVector, YVector, matrix, reverse);
}
}
#pragma omp parallel num_threads(xsize)
{
#pragma omp for
for(size_t yzplane=0;yzplane<xsize;yzplane++) {
shearSortOpenMP(zero+XVector*yzplane, ZVector, YVector, matrix, reverse);
}
}
#pragma omp parallel num_threads(ysize)
{
#pragma omp for
for(size_t xzplane=0;xzplane<ysize;xzplane++) {
if(xzplane%2 == 0) {
shearSortOpenMP(zero+YVector*xzplane, XVector, ZVector, matrix, reverse);
}
else {
shearSortOpenMP(zero+YVector*xzplane, XVector, ZVector, matrix, !reverse);
}
}
}
#pragma omp parallel num_threads(xsize*zsize)
{
#pragma omp for
for(size_t column=0;column<xsize*zsize;column++) {
eotSortOpenMP(zero+XVector*(column%xsize)+ZVector*(column/xsize), YVector, matrix,reverse,true);
}
}
#pragma omp parallel num_threads(ysize)
{
#pragma omp for
for(size_t xzplane=0;xzplane<ysize;xzplane++) {
shearSortOpenMP(zero+YVector*xzplane, XVector, ZVector, matrix, reverse);
}
}
}
void readFile(char * filename, vector<VTYPE> &data) {
ifstream inputFile(filename, std::ifstream::in);
if (inputFile) {
VTYPE value;
while ( inputFile >> value ) {
data.push_back(value);
}
if(!inputFile.eof()) {
throw "chyba nacitani dat ze souboru - fakt tam jsou jen cisla?";
}
inputFile.close();
}
else {
throw "chyba nacitani dat ze souboru";
}
}
void writeFile(char * filename, vector<VTYPE>& data) {
streambuf * buf;
ofstream of;
if(filename) {
of.open(filename);
buf = of.rdbuf();
} else {
buf = std::cout.rdbuf();
}
ostream out(buf);
if (out) {
for(vector<VTYPE>::iterator i = data.begin(); i!=data.end(); i++) {
out << *i << endl;
}
}
else {
throw "chyba vypisu dat";
}
}
void getOutStream(char * filename, ostream &ost){
}
void writeFileSnake(ostream &out, Coordinates plane, Coordinates rowvector, Coordinates colvector, CPUMatrix * matrix) {
if (out) {
size_t rows = matrix->sizeForVector(colvector);
size_t cols = matrix->sizeForVector(rowvector);
for(size_t row =0;row<rows;row++) {
if(row%2==0) {
for(size_t col = 0; col<cols;col++) {
matrix->get(plane+rowvector*col+colvector*row)->printData('\n', out);
}
}
else {
for(size_t col = cols-1; col>0;col--) {
matrix->get(plane+rowvector*col+colvector*row)->printDataReverse('\n', out);
}
matrix->get(plane+colvector*row)->printDataReverse('\n', out);
}
}
}
else {
throw "chyba vypisu dat";
}
}
int main(int argc, char** argv) {
omp_set_nested(1);
try {
char * filename = CLIArgumentsParser::getCmdOption(argv, argv + argc, "-f");
if (!filename) {
cout << "Chybi argument -f se souborem cisel" << endl;
return -1;
}
char * output = CLIArgumentsParser::getCmdOption(argv, argv + argc, "-o");
streambuf * buf;
ofstream of;
if(output) {
of.open(output);
buf = of.rdbuf();
} else {
buf = std::cout.rdbuf();
}
ostream outputStream(buf);
char * dimensions = CLIArgumentsParser::getCmdOption(argv, argv + argc, "-S");
if(dimensions) {
//SHEAR
vector<VTYPE> data;
readFile(filename, data);
CPUMatrix matrix(dimensions, data);
if(matrix.sizeForVector(ZVector) != 1) {
cout << "Shear funguje jen na 2d mrizce..." << endl;
return -1;
}
shearSortOpenMP(Coordinates(0,0,0), XVector, YVector,&matrix);
writeFileSnake(outputStream, Coordinates(0,0,0), XVector,YVector, &matrix);
return 0;
}
dimensions = CLIArgumentsParser::getCmdOption(argv, argv + argc, "-3");
if(dimensions) {
//3D
vector<VTYPE> data;
readFile(filename, data);
CPUMatrix matrix(dimensions, data);
dSortOpenMP(&matrix, false);
for(size_t xzplanes=0;xzplanes<matrix.sizeForVector(YVector); xzplanes++) {
writeFileSnake(outputStream, Coordinates(0,0,0)+YVector*xzplanes, XVector,ZVector, &matrix);
}
return 0;
}
cout << "Nespecifikovan ani shear (-S) ani 3dsort (-3). Syntax [-S|-3] NxN[xN] -- rozmer matice procesoru" << endl;
return -1;
}
catch (char const * e) {
cout << e << endl;
return -1;
}
}