/* 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. */ #ifdef PADDLE_WITH_CUDA #include #endif #include #include // NOLINT #include "paddle/fluid/framework/data_type.h" #include "paddle/fluid/operators/distributed/sendrecvop_utils.h" #include "paddle/fluid/operators/distributed/variable_response.h" namespace paddle { namespace operators { namespace distributed { using VarMsg = sendrecv::VariableMessage; static std::shared_ptr GetCommunicationAllocationFromTensor( const platform::DeviceContext& ctx, const framework::Tensor& tensor) { if (is_gpu_place(ctx.GetPlace())) { #ifdef PADDLE_WITH_CUDA PADDLE_ENFORCE(is_gpu_place(tensor.place())); auto& gpu_dev_ctx = reinterpret_cast(ctx); auto copy_size = tensor.numel() * framework::SizeOfType(tensor.type()); platform::CUDAPinnedPlace cuda_pinned; auto result = memory::AllocShared( cuda_pinned, copy_size, memory::allocation::Allocator::kCrossDevice); memory::Copy(cuda_pinned, result->ptr(), boost::get(tensor.place()), reinterpret_cast(tensor.data()), copy_size, gpu_dev_ctx.stream()); ctx.Wait(); return result; #else return nullptr; // THIS SHOULD NOT HAPPENED. #endif } else { return tensor.Holder(); } } std::shared_ptr GetTensorPayload( framework::Variable* var, const platform::DeviceContext& ctx, VarMsg* request) { auto tensor = var->Get(); // FIXME(wuyi): data types in send_recv.proto is copied from // framework.proto request->set_data_type( static_cast(framework::ToDataType(tensor.type()))); for (auto& dim : framework::vectorize(tensor.dims())) { request->add_dims(dim); } const framework::LoD lod = tensor.lod(); if (lod.size() > 0) { request->set_lod_level(lod.size()); for (auto& each : lod) { VarMsg::LodData* lod_inner = request->add_lod(); for (auto& d : each) { lod_inner->add_lod_data(d); } } } return GetCommunicationAllocationFromTensor(ctx, tensor); } std::shared_ptr GetSelectedRowsPayload( framework::Variable* var, const platform::DeviceContext& ctx, VarMsg* request) { auto* slr = var->GetMutable(); request->set_data_type( static_cast(framework::ToDataType(slr->value().type()))); request->set_lod_level(0); request->set_slr_height(slr->height()); for (auto& dim : framework::vectorize(slr->value().dims())) { request->add_dims(dim); } auto* tensor = slr->mutable_value(); return GetCommunicationAllocationFromTensor(ctx, *tensor); } } // namespace distributed } // namespace operators } // namespace paddle