The CPU is selected when the -gpu flag is not used in the command line. I solved the issue by adding [this line](https://github.com/BVLC/caffe/blob/master/tools/caffe.cpp#L170), as in caffe's original code.