forked from BVLC/caffe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpus.cpp
More file actions
85 lines (67 loc) · 2.12 KB
/
Copy pathgpus.cpp
File metadata and controls
85 lines (67 loc) · 2.12 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
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/detail/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <caffe/net.hpp>
#include <caffe/parallel.hpp>
#include <caffe/proto/caffe.pb.h>
#include <caffe/solver.hpp>
#include <caffe/util/io.hpp>
#include <glog/logging.h>
#include <stdio.h>
#include <cstdlib>
#include <string>
#include <vector>
#include "base.hpp"
using namespace std;
using namespace caffe;
#ifndef CPU_ONLY
// Trains a net on multiple GPUs on one box. C.f. GPUSync in parallel.h.
//
// Example launch on GPU 0 and 1:
// make -j
// export LD_LIBRARY_PATH=/usr/local/lib:/usr/local/cuda/lib64
// export GLOG_logtostderr=1
// build/examples/parallel/gpus.bin examples/parallel/mnist_solver.prototxt 0:1
int main(int argc, char** argv) {
::google::InitGoogleLogging(argv[0]);
::google::InstallFailureSignalHandler();
if (argc != 3) {
printf("Usage: gpus.bin solver_proto_file gpu_id[:gpu_id][...]\n");
return 1;
}
SolverParameter solver_param;
ReadProtoFromTextFile(argv[1], &solver_param);
vector<int> gpus;
vector<string> gpu_strings;
boost::split(gpu_strings, argv[2], boost::is_any_of(":"));
for (int i = 0; i < gpu_strings.size(); ++i)
gpus.push_back(atoi(gpu_strings[i].c_str()));
solver_param.set_device_id(gpus[0]);
SGDSolver<float> main(solver_param);
// Shared network weights
Params<float> params(main.net()->params());
// Create contexts
vector<SolverContext*> solvers(gpus.size());
solvers[0] = new CPUGPUContext(params, solver_param, &main);
for (int i = 1; i < gpus.size(); ++i) {
solver_param.set_device_id(gpus[i]);
solvers[i] = new CPUGPUContext(params, solver_param);
solvers[i]->start();
}
// Start monitor
Monitor monitor(params, solvers);
monitor.start();
// Run main on current thread
solvers[0]->run();
monitor.stop();
LOG(INFO)<< "Monitor stop\n";
for (int i = 1; i < solvers.size(); ++i)
solvers[i]->stop();
for (int i = 1; i < solvers.size(); ++i)
delete solvers[i];
}
#else
int main(int argc, char *argv[]) {
}
#endif