-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.cpp
More file actions
121 lines (97 loc) · 3.58 KB
/
Copy pathdemo.cpp
File metadata and controls
121 lines (97 loc) · 3.58 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <numeric>
#include <cmath>
using namespace std;
struct Task {
int id;
vector<double> data;
};
struct Result {
int task_id, worker_id;
double mean, variance, std_dev, min_val, max_val, execution_time;
};
class Worker {
public:
int id, tasks_completed;
double total_time;
Worker(int w) : id(w), tasks_completed(0), total_time(0.0) {}
Result executeTask(const Task& task) {
clock_t start = clock();
Result r;
r.task_id = task.id;
r.worker_id = id;
double sum = 0.0;
for (size_t i = 0; i < task.data.size(); i++) sum += task.data[i];
r.mean = sum / task.data.size();
double sq_sum = 0.0;
for (size_t i = 0; i < task.data.size(); i++) sq_sum += task.data[i] * task.data[i];
r.variance = sq_sum / task.data.size() - r.mean * r.mean;
r.std_dev = sqrt(r.variance);
r.min_val = *min_element(task.data.begin(), task.data.end());
r.max_val = *max_element(task.data.begin(), task.data.end());
r.execution_time = double(clock() - start) / CLOCKS_PER_SEC;
tasks_completed++;
total_time += r.execution_time;
return r;
}
};
class LoadBalancer {
int num_workers, current_worker;
public:
LoadBalancer(int w) : num_workers(w), current_worker(0) {}
int assignTask() {
int a = current_worker;
current_worker = (current_worker + 1) % num_workers;
return a;
}
};
int main() {
cout << "==========================================\n";
cout << " Distributed Computing Framework Demo\n";
cout << " Aug 2022 - Nov 2022 Project\n";
cout << "==========================================\n\n";
const int NUM_WORKERS = 4, NUM_TASKS = 20;
cout << "Config: " << NUM_WORKERS << " workers, " << NUM_TASKS << " tasks\n\n";
vector<Worker> workers;
for (int i = 0; i < NUM_WORKERS; i++) workers.push_back(Worker(i));
LoadBalancer balancer(NUM_WORKERS);
cout << "Creating tasks...\n";
srand((unsigned)time(0));
vector<Task> tasks;
for (int i = 0; i < NUM_TASKS; i++) {
Task t;
t.id = i;
for (int j = 0; j < 1000; j++) t.data.push_back((double)rand() / RAND_MAX * 100.0);
tasks.push_back(t);
}
cout << "Distributing tasks...\n\n";
clock_t start = clock();
vector<Result> results;
for (int i = 0; i < NUM_TASKS; i++) {
int w = balancer.assignTask();
cout << "Task " << i << " -> Worker " << w << "... ";
Result r = workers[w].executeTask(tasks[i]);
results.push_back(r);
cout << "done\n";
}
double total = double(clock() - start) / CLOCKS_PER_SEC;
cout << "\n==========================================\n";
cout << " Execution Complete!\n";
cout << "==========================================\n\n";
cout << "Total Time: " << total << "s\n";
cout << "Tasks: " << results.size() << "/" << NUM_TASKS << "\n\n";
cout << "Worker Performance:\n";
for (int i = 0; i < NUM_WORKERS; i++) {
printf(" Worker %d: %d tasks, %.4fs total\n",
i, workers[i].tasks_completed, workers[i].total_time);
}
cout << "\nSample Result (Task 0):\n";
printf(" Mean: %.2f, StdDev: %.2f\n", results[0].mean, results[0].std_dev);
printf(" Min: %.2f, Max: %.2f\n", results[0].min_val, results[0].max_val);
cout << "\nFeatures: Load Balancing, Data Partitioning, Monitoring\n";
return 0;
}