Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/devicequery.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2014 Sergio Guadarrama


#include "caffe/common.hpp"
#include "caffe/net.hpp"


using namespace caffe;

int main(int argc, char** argv) {
if (argc > 2) {
LOG(ERROR) << "device_query [device_id=0]";
return 0;
}
if (argc == 2) {
LOG(INFO) << "Querying device_id=" << argv[1];
Caffe::SetDevice(atoi(argv[1]));
Caffe::DeviceQuery();
} else {
Caffe::DeviceQuery();
}
return 0;
}
1 change: 1 addition & 0 deletions src/caffe/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ void Caffe::DeviceQuery() {
return;
}
CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
printf("Device id: %d\n", device);
printf("Major revision number: %d\n", prop.major);
printf("Minor revision number: %d\n", prop.minor);
printf("Name: %s\n", prop.name);
Expand Down
2 changes: 2 additions & 0 deletions src/caffe/proto/caffe.proto
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ message SolverParameter {
optional bool snapshot_diff = 16 [ default = false];
// the mode solver will use: 0 for CPU and 1 for GPU. Use GPU in default.
optional int32 solver_mode = 17 [default = 1];
// the device_id will that be used in GPU mode. Use device_id=0 in default.
optional int32 device_id = 18 [default = 0];
}

// A message that stores the solver snapshots
Expand Down
3 changes: 3 additions & 0 deletions src/caffe/solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ Solver<Dtype>::Solver(const SolverParameter& param)
template <typename Dtype>
void Solver<Dtype>::Solve(const char* resume_file) {
Caffe::set_mode(Caffe::Brew(param_.solver_mode()));
if (param_.solver_mode() && param_.has_device_id()) {
Caffe::SetDevice(param_.device_id());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that a slightly safer way is to do

if (param_.has_device_id()) {
Caffe::SetDevice(param_.device_id());
}

just in case a user has hard-coded a device id outside the solver and does not specify the device id in the solver param. Currently, if nothing is set, the solver will always use the 0th device, which might not be desired.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea behind the condition was to set device_id only if solver_mode was set to 1 or GPU mode. In CPU mode we don't want to try SetDevice since it can fail if there is not GPU device.
But we could check both conditions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, sorry for the confusion - I meant two if's nested together, so
effectively it is

if (param_.solver_mode() && param_.has_device_id())

thanks for the changes :)

Yangqing

On Mon, Feb 3, 2014 at 1:12 PM, Sergio Guadarrama
[email protected]:

In src/caffe/solver.cpp:

@@ -40,6 +40,9 @@
template
void Solver::Solve(const char* resume_file) {
Caffe::set_mode(Caffe::Brew(param_.solver_mode()));

  • if (param_.solver_mode()) {
  • Caffe::SetDevice(param_.device_id());

The idea behind the condition was to set device_id only if solver_mode was
set to 1 or GPU mode. In CPU mode we don't want to try SetDevice since it
can fail if there is not GPU device.
But we could check both conditions

Reply to this email directly or view it on GitHubhttps://github.com//pull/69/files#r9399603
.

}
Caffe::set_phase(Caffe::TRAIN);
LOG(INFO) << "Solving " << net_->name();
PreSolve();
Expand Down