forked from DCP-CloudMesh/PeerToPeer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
126 lines (106 loc) · 4.47 KB
/
Copy pathmain.cpp
File metadata and controls
126 lines (106 loc) · 4.47 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
122
123
124
125
126
#include "include/Peers/bootstrap_node.h"
#include "include/Peers/provider.h"
#include "include/Peers/requester.h"
#include "include/utility.h"
#include <boost/program_options.hpp>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#define DEFAULT_PORT 8080
#define DEFAULT_WORKERS 2
#define DEFAULT_EPOCHS 10
using namespace std;
namespace po = boost::program_options;
int main(int argc, char* argv[]) {
unsigned short port = DEFAULT_PORT;
string uuid = uuid::generate_uuid_v4();
cout << "\n=== INITIALIZING NODE ===\n" << endl;
#if defined(BOOTSTRAP)
unsigned int bootstrapPort = BootstrapNode::getServerIpAddr().port;
cout << "Running as bootstrap node on port " << bootstrapPort << "."
<< endl;
BootstrapNode b = BootstrapNode(uuid);
cout << "\n=== SERVING NEW PEERS ===" << endl;
b.listen();
#elif defined(PROVIDER)
try {
// Define available program argument options
po::options_description desc("Allowed options");
desc.add_options()(
"port,p", po::value<unsigned short>()->default_value(DEFAULT_PORT),
"set P2P server port")("help,h", "produce help message");
// Parse command-line arguments
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
// Handle options
if (vm.count("help")) {
cout << desc << endl;
return 0;
}
port = vm["port"].as<unsigned short>();
} catch (const exception& e) {
cerr << "Error parsing program arguments: " << e.what() << endl;
return 1;
}
cout << "Running as provider on port " << port << "." << endl;
Provider p = Provider(port, uuid);
cout << "\n=== JOINING P2P NETWORK ===\n" << endl;
p.registerWithBootstrap();
p.listen();
#elif defined(REQUESTER)
// Define available program argument options
po::options_description desc("Allowed options");
desc.add_options()("port,p",
po::value<unsigned short>()->default_value(DEFAULT_PORT),
"set P2P server port")(
"mode,m", po::value<string>()->default_value("c"),
"set mode to run requester: 'c' for compute task or 'r' for receive "
"results")("workers,w",
po::value<unsigned int>()->default_value(DEFAULT_WORKERS),
"set number of workers to assign training task")(
"epochs,e", po::value<unsigned int>()->default_value(DEFAULT_EPOCHS),
"set number of epochs to run training task")("help,h",
"produce help message");
// Parse command-line arguments
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
// Handle options
if (vm.count("help")) {
cout << desc << endl;
return 0;
}
port = vm["port"].as<unsigned short>();
string mode = vm["mode"].as<string>();
cout << "Running as requester on port " << port << "." << endl;
Requester r = Requester(port);
if (mode == "c") {
unsigned int numRequestedWorkers = vm["workers"].as<unsigned int>();
unsigned int numEpochs = vm["epochs"].as<unsigned int>();
cout << "\n=== INITIALIZING TRAINING TASK ===\n" << endl;
cout << "Creating training task for " << numRequestedWorkers
<< " workers and " << numEpochs << " epochs" << endl;
TaskRequest request = TaskRequest(numRequestedWorkers, ".*\\.jpg$",
numEpochs, TaskRequest::GLOB_PATTERN);
r.setTaskRequest(request);
// sends the task request to the leader and provider peers
cout << "\n=== SENDING TRAINING REQUEST TO PROVIDER NETWORK ===\n" << endl;
r.sendTaskRequest();
cout << "Sent task request." << endl;
} else if (mode == "r") {
TaskResponse response = r.getResults();
std::string result = response.getTrainingData();
cout << "Received training results" << endl;
// output result.modelStateDict to a file as a binary
std::filesystem::create_directories("output");
std::ofstream file("output/modelStateDict.bin", std::ios::binary);
file.write(reinterpret_cast<const char*>(result.data()), result.size());
file.close();
std::cout << "Successfully wrote to output/modelStateDict" << endl;
}
#else
cout << "Please specify either --provider or --requester flag." << endl;
#endif
}