Caffe2 - C++ API
A deep learning, cross platform ML framework
utility_ops_gpu.cc
1 
17 #include "caffe2/core/context_gpu.h"
18 #include "caffe2/operators/flatten_op.h"
19 #include "caffe2/operators/utility_ops.h"
20 #include "caffe2/utils/math.h"
21 
22 namespace caffe2 {
23 
24 template <>
25 bool WeightedSumOp<CUDAContext>::RunOnDevice() {
26  if (Input(0).IsType<float>()) {
27  return DoRunWithType<float>();
28  } else if (Input(0).IsType<float16>()) {
29  return DoRunWithType<float16>();
30  } else {
31  CAFFE_THROW("Unsupported inputs");
32  }
33  return false;
34 }
35 
36 template <>
37 bool SumOp<CUDAContext>::RunOnDevice() {
38  if (Input(0).IsType<float>()) {
39  return DoRunWithType<float, float>();
40  } else if (Input(0).IsType<float16>()) {
41  return DoRunWithType<float16, float16>();
42  } else {
43  CAFFE_THROW("Unsupported inputs");
44  }
45  return false;
46 }
47 
48 template <>
50  : public Operator<CUDAContext> {
51  public:
52  CopyOnDeviceLikeOp(const OperatorDef& operator_def, Workspace* ws)
53  : Operator<CUDAContext>(operator_def, ws) {}
54  USE_OPERATOR_FUNCTIONS(CUDAContext);
55 
56  bool RunOnDevice() override {
57  auto& input = Input(0);
58  auto* output = OperatorBase::Output<Tensor<CUDAContext>>(0);
59  CUDAContext context(GetGPUIDForPointer(Input(1).raw_data()));
60  output->ResizeLike(input);
61  context.template CopyItems<CUDAContext, CUDAContext>(
62  input.meta(),
63  input.size(),
64  input.raw_data(),
65  output->raw_mutable_data(input.meta()));
66  return true;
67  }
68 };
69 
70 REGISTER_CUDA_OPERATOR(Print, PrintOp<CUDAContext>);
71 REGISTER_CUDA_OPERATOR(Flatten, FlattenOp<CUDAContext>);
72 REGISTER_CUDA_OPERATOR(FlattenToVec, FlattenToVecOp<CUDAContext>);
73 REGISTER_CUDA_OPERATOR(Alias, AliasOp<CUDAContext>);
74 REGISTER_CUDA_OPERATOR(ResizeLike, ResizeLikeOp<CUDAContext>);
75 REGISTER_CUDA_OPERATOR(Sum, SumOp<CUDAContext>);
76 REGISTER_CUDA_OPERATOR(WeightedSum, WeightedSumOp<CUDAContext>);
77 // From whatever the current context, ensure the output is TensorCPU
78 REGISTER_CUDA_OPERATOR(
79  EnsureCPUOutput,
81 // From CPU, copy it to whatever the current context
82 REGISTER_CUDA_OPERATOR(
83  CopyFromCPUInput,
85 
86 // CopyGPUToCPU and CopyCPUToGPU should both be carried out in a cuda context,
87 // since gpu code will be involved.
88 REGISTER_CUDA_OPERATOR(
89  CopyGPUToCPU,
91 REGISTER_CUDA_OPERATOR(
92  CopyCPUToGPU,
94 // If we only specify Copy, we assume that it is a gpu to gpu copy - maybe
95 // involving different GPUs.
96 REGISTER_CUDA_OPERATOR(Copy, CopyOp<CUDAContext, CUDAContext, CUDAContext>);
97 
98 REGISTER_CUDA_OPERATOR(
99  CopyOnDeviceLike,
101 
102 REGISTER_CUDA_OPERATOR(UnsafeCoalesce, UnsafeCoalesceOp<CUDAContext>);
103 
104 } // namespace caffe2
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
Definition: workspace.h:63
Copyright (c) 2016-present, Facebook, Inc.
int GetGPUIDForPointer(const void *ptr)
Gets the GPU id that the current pointer is located at.
Definition: common_gpu.cc:149
Alias op makes the output and the input share the same underlying storage.
Definition: utility_ops.h:180