forked from BVLC/caffe
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhogwild.cpp
More file actions
83 lines (67 loc) · 2.26 KB
/
Copy pathhogwild.cpp
File metadata and controls
83 lines (67 loc) · 2.26 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
#include <cstdlib>
#include <string>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <sstream>
#include <pthread.h>
#include <glog/logging.h>
#include <boost/shared_ptr.hpp>
#include <boost/algorithm/string.hpp>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netdb.h>
#include <caffe/caffe.hpp>
#include "caffe/filler.hpp"
#include "caffe/parallel.hpp"
#include "base.hpp"
using namespace std;
using namespace caffe;
// Trains a net in parallel on multiple CPU cores. C.f. CPUSync in parallel.h.
//
// Your BLAS library needs to let the application manage its threads, e.g.
// for OpenBLAS, compile with no threading (USE_THREAD = 0 in Makefile.rule).
// Performance is linear at first, but then plateaus on large nets as the number
// of cores is increased, probably as the CPU runs out of memory bandwidth.
//
// Example launch on 4 cores:
// make -j
// export LD_LIBRARY_PATH=<single thread BLAS>:/usr/local/lib:/usr/local/cuda/lib64
// export GLOG_logtostderr=1
// build/examples/parallel/hogwild.bin examples/parallel/mnist_solver.prototxt 4
int main(int argc, char** argv) {
::google::InitGoogleLogging(argv[0]);
::google::InstallFailureSignalHandler();
if (argc < 2 || argc > 3) {
printf("Usage: hogwild.bin solver_proto_file [number_of_cores]\n");
return 1;
}
SolverParameter solver_param;
ReadProtoFromTextFile(argv[1], &solver_param);
int cores = argc == 3 ? atoi(argv[2]) : sysconf(_SC_NPROCESSORS_ONLN);
// Override in code so that proto file can be shared with other examples
solver_param.set_solver_mode(SolverParameter::CPU);
// Main solver
SGDSolver<float> main(solver_param);
// Shared network weights
Params<float> params(main.net()->params());
// Create contexts
vector<SolverContext*> solvers(cores);
solvers[0] = new CPUContext(params, solver_param, &main);
for (int i = 1; i < cores; ++i) {
solvers[i] = new CPUContext(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];
}