c_sync_comm_stream_op.h 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2022 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. */
14 15 16
#ifndef PADDLE_FLUID_OPERATORS_COLLECTIVE_C_SYNC_COMM_STREAM_OP_H_
#define PADDLE_FLUID_OPERATORS_COLLECTIVE_C_SYNC_COMM_STREAM_OP_H_

17 18 19 20 21 22 23 24 25 26 27 28
#include <string>

#include "paddle/fluid/framework/op_registry.h"

#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
#include "paddle/fluid/platform/device/gpu/nccl_helper.h"
#endif

#if defined(PADDLE_WITH_CNCL)
#include "paddle/fluid/platform/device/mlu/cncl_helper.h"
#endif

张春乔 已提交
29
#if defined(PADDLE_WITH_XPU_BKCL)
30 31 32 33 34 35
#include "paddle/fluid/platform/collective_helper.h"
#endif

namespace paddle {
namespace operators {

36
template <typename T, typename DeviceContext>
37 38 39 40 41 42 43 44 45 46 47 48 49
class CSyncCommStreamKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
    auto place = ctx.GetPlace();
    int ring_id = ctx.Attr<int>("ring_id");
    auto stream =
        platform::NCCLCommContext::Instance().Get(ring_id, place)->stream();

    platform::GpuStreamSync(stream);

#elif defined(PADDLE_WITH_CNCL)
    auto place = ctx.GetPlace();
50 51
    PADDLE_ENFORCE_EQ(platform::is_mlu_place(place),
                      true,
52 53 54 55 56 57 58 59
                      platform::errors::PreconditionNotMet(
                          "Sync stream op can run on mlu place only for now."));
    int ring_id = ctx.Attr<int>("ring_id");
    auto stream =
        platform::CNCLCommContext::Instance().Get(ring_id, place)->stream();
    platform::MLUStreamSync(stream);
#elif defined(PADDLE_WITH_XPU_BKCL)
    auto place = ctx.GetPlace();
60 61
    PADDLE_ENFORCE_EQ(platform::is_xpu_place(place),
                      true,
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
                      platform::errors::PreconditionNotMet(
                          "Sync stream op can run on xpu place only for now."));
    int ring_id = ctx.Attr<int>("ring_id");
    auto comm_dev_ctx = platform::BKCLCommContext::Instance()
                            .Get(ring_id, place)
                            ->dev_context();
    comm_dev_ctx->Wait();
#else
    PADDLE_THROW(platform::errors::PreconditionNotMet(
        "PaddlePaddle should compile with GPU."));
#endif
  }
};

}  // namespace operators
}  // namespace paddle
78
#endif  // PADDLE_FLUID_OPERATORS_COLLECTIVE_C_SYNC_COMM_STREAM_OP_H_