average_accumulates_op.cu 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/fluid/operators/average_accumulates_op.h"
#include "paddle/fluid/platform/gpu_info.h"

namespace paddle {
namespace operators {
template <>
W
wanghaoshuang 已提交
21
void GetAccumulators<paddle::platform::CUDADeviceContext>(
22 23
    const framework::ExecutionContext& ctx, int64_t& num_updates_,
    int64_t& num_accumulates_, int64_t& old_num_accumulates_) {
W
wanghaoshuang 已提交
24 25 26 27
  auto* in_old_num_accumulates = ctx.Input<Tensor>("in_old_num_accumulates");
  auto* in_num_accumulates = ctx.Input<Tensor>("in_num_accumulates");
  auto* in_num_updates = ctx.Input<Tensor>("in_num_updates");
  auto stream = ctx.cuda_device_context().stream();
28 29
  memory::Copy(platform::CPUPlace(), &old_num_accumulates_,
               platform::CUDAPlace(), in_old_num_accumulates->data<int64_t>(),
W
wanghaoshuang 已提交
30
               sizeof(int64_t), stream);
31
  memory::Copy(platform::CPUPlace(), &num_accumulates_, platform::CUDAPlace(),
W
wanghaoshuang 已提交
32
               in_num_accumulates->data<int64_t>(), sizeof(int64_t), stream);
33
  memory::Copy(platform::CPUPlace(), &num_updates_, platform::CUDAPlace(),
W
wanghaoshuang 已提交
34
               in_num_updates->data<int64_t>(), sizeof(int64_t), stream);
35 36 37
}

template <>
W
wanghaoshuang 已提交
38
void SetAccumulators<paddle::platform::CUDADeviceContext>(
39 40
    const framework::ExecutionContext& ctx, int64_t num_updates_,
    int64_t num_accumulates_, int64_t old_num_accumulates_) {
W
wanghaoshuang 已提交
41 42 43 44
  auto stream = ctx.cuda_device_context().stream();
  auto* out_old_num_accumulates = ctx.Output<Tensor>("out_old_num_accumulates");
  auto* out_num_accumulates = ctx.Output<Tensor>("out_num_accumulates");
  auto* out_num_updates = ctx.Output<Tensor>("out_num_updates");
45 46

  memory::Copy(platform::CUDAPlace(), out_old_num_accumulates->data<int64_t>(),
W
wanghaoshuang 已提交
47 48
               platform::CPUPlace(), &old_num_accumulates_, sizeof(int64_t),
               stream);
49
  memory::Copy(platform::CUDAPlace(), out_num_accumulates->data<int64_t>(),
W
wanghaoshuang 已提交
50 51
               platform::CPUPlace(), &num_accumulates_, sizeof(int64_t),
               stream);
52
  memory::Copy(platform::CUDAPlace(), out_num_updates->data<int64_t>(),
W
wanghaoshuang 已提交
53
               platform::CPUPlace(), &num_updates_, sizeof(int64_t), stream);
54 55
}

W
wanghaoshuang 已提交
56 57 58
}  // namespace operators
}  // namespace paddle

59 60
namespace ops = paddle::operators;
REGISTER_OP_CUDA_KERNEL(
W
wanghaoshuang 已提交
61
    average_accumulates,
62 63
    ops::AverageAccumulatesKernel<paddle::platform::CUDADeviceContext, float>,
    ops::AverageAccumulatesKernel<paddle::platform::CUDADeviceContext, double>);