c_wait_compute_op.cc 3.2 KB
Newer Older
W
WangXi 已提交
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
/* Copyright (c) 2021 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 <string>

#include "paddle/fluid/framework/op_registry.h"
namespace paddle {
namespace framework {
class Scope;
}  // namespace framework
}  // namespace paddle
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
#include "paddle/fluid/platform/collective_helper.h"
#endif

namespace paddle {
namespace operators {

class CWaitComputeOp : public framework::OperatorBase {
 public:
  CWaitComputeOp(const std::string& type,
                 const framework::VariableNameMap& inputs,
                 const framework::VariableNameMap& outputs,
                 const framework::AttributeMap& attrs)
      : OperatorBase(type, inputs, outputs, attrs) {}

  void RunImpl(const framework::Scope& scope,
               const platform::Place& place) const override {
    PADDLE_ENFORCE_EQ(
40 41
        platform::is_gpu_place(place),
        true,
W
WangXi 已提交
42
        platform::errors::PreconditionNotMet(
43 44
            "wait_compute op can run on gpu place only for now, but got %s",
            place.DebugString()));
W
WangXi 已提交
45 46 47 48 49

#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
    int ring_id = Attr<int>("ring_id");

    auto compute_stream =
L
Leo Chen 已提交
50
        static_cast<phi::GPUContext*>(
W
WangXi 已提交
51 52 53 54 55 56 57 58 59 60 61
            platform::DeviceContextPool::Instance().Get(place))
            ->stream();
    auto comm_stream =
        platform::NCCLCommContext::Instance().Get(ring_id, place)->stream();

    auto event = platform::NCCLCommContext::Instance()
                     .Get(ring_id, place)
                     ->compute_event();

// compute_stream-->event-->comm_stream
#ifdef PADDLE_WITH_HIP
62 63
    PADDLE_ENFORCE_GPU_SUCCESS(hipEventRecord(event, compute_stream));
    PADDLE_ENFORCE_GPU_SUCCESS(hipStreamWaitEvent(comm_stream, event, 0));
W
WangXi 已提交
64
#else
65 66
    PADDLE_ENFORCE_GPU_SUCCESS(cudaEventRecord(event, compute_stream));
    PADDLE_ENFORCE_GPU_SUCCESS(cudaStreamWaitEvent(comm_stream, event, 0));
W
WangXi 已提交
67 68 69 70 71 72 73 74 75 76
#endif
#else
    PADDLE_THROW(platform::errors::PreconditionNotMet(
        "PaddlePaddle should compile with GPU."));
#endif
  }
};

class CWaitComputeOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
77
  void Make() override {
W
WangXi 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    AddInput("X", "(Tensor) Dependency of the variable need to sync")
        .AsDuplicable();
    AddOutput("Out", "(Tensor) Dependency of the variable need to sync")
        .AsDuplicable();
    AddAttr<int>("ring_id", "(int default 0) ring id.").SetDefault(0);
    AddComment(R"DOC(
CWaitCompute Operator

Comm stream wait Compute Stream with async event.
)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

96 97
REGISTER_OPERATOR(c_wait_compute,
                  ops::CWaitComputeOp,
W
WangXi 已提交
98
                  ops::CWaitComputeOpMaker);