-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcesses_Generator.cpp
More file actions
106 lines (88 loc) · 2.44 KB
/
Processes_Generator.cpp
File metadata and controls
106 lines (88 loc) · 2.44 KB
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
/*************************************************************
* Small generator of Two-processor distributed model
* - of size N processes
* - max execution cost C of any process
*
* Pedro T. Monteiro - Pedro.Tiago.Monteiro@tecnico.ulisboa.pt
*************************************************************/
#include <cmath>
#include <iostream>
#include <vector>
#include <list>
using namespace std;
// Adj list c_ij
int _N, _C;
list<pair<int,int>> *_g;
int _nedges = 0;
vector<int> _X, _Y;
//-------------------------------------------------------------------
void printUsage() {
cout << "Usage: gen2procs #N #C seed" << endl;
cout << "\t#N: number of processes (N >= 2)" << endl;
cout << "\t#C: max cost of any process execution (C>0)" << endl;
cout << "\tseed: random seed number (optional)" << endl;
exit(0);
}
void parseArgs(int argc, char **argv) {
int seed = 0;
if (argc < 3) printUsage();
sscanf(argv[1], "%d", &_N);
if (_N < 2) {
cout << "ERROR: # N must be >= 2" << endl;
printUsage();
}
sscanf(argv[2], "%d", &_C);
if (_C <= 0) {
cout << "ERROR: # C must be > 0" << endl;
printUsage();
}
if (argc > 3) {
// Init rand seed
sscanf(argv[3], "%d", &seed);
srand(seed);
} else {
srand((unsigned int)time(NULL));
}
}
int randomValue(int max) {
return 1 + rand() % max; // [1, max]
}
void addPotentialCost(int u, int v, int w) {
if (w <= 0) return;
_g[u].push_back(make_pair(v, w));
_nedges++;
}
int main(int argc, char *argv[]) {
// parse arguments
parseArgs(argc, argv);
_X.resize(_N, 0);
_Y.resize(_N, 0);
for (int i = 0; i < _N; i++) { // 1st and 2nd processor
_X[i] = randomValue(_C);
_Y[i] = randomValue(_C);
}
// init graph
_g = new list<pair<int,int>>[_N];
// add c_ij's as a bidirectional graph
for (int i = 0; i < _N; i++) { // upper triangular matrix - O(N^2)
for (int j = i+1; j < _N; j++) {
if ((_X[i] < _Y[i] && _X[j] > _Y[j]) ||
(_X[i] > _Y[i] && _X[j] < _Y[j])) {
addPotentialCost(i, j, (int)min(_X[i],_X[j])/2);
}
}
}
// print header
printf("%d %d\n", _N, _nedges);
// print execution costs
for (int i = 0; i < _N; i++) // 1st and 2nd processor
printf("%d %d\n", _X[i], _Y[i]);
// print communication costs
for (int u = 0; u < _N; u++) { // O(N + nedges)
for (list<pair<int,int>>::iterator iter = _g[u].begin(); iter != _g[u].end(); iter++) {
int v = iter->first, w = iter->second;
printf("%d %d %d\n", u+1, v+1, w);
}
}
return 0;
}