|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include <unistd.h> |
| 16 | +#include <string> |
| 17 | +#include <thread> // NOLINT |
| 18 | + |
| 19 | +#include "gtest/gtest.h" |
| 20 | +#include "paddle/fluid/framework/op_registry.h" |
| 21 | +#include "paddle/fluid/framework/operator.h" |
| 22 | +#include "paddle/fluid/framework/program_desc.h" |
| 23 | +#include "paddle/fluid/operators/detail/grpc_client.h" |
| 24 | +#include "paddle/fluid/operators/listen_and_serv_op.h" |
| 25 | +#include "paddle/fluid/operators/math/math_function.h" |
| 26 | +#include "paddle/fluid/operators/math/selected_rows_functor.h" |
| 27 | +#include "paddle/fluid/string/printf.h" |
| 28 | + |
| 29 | +USE_NO_KERNEL_OP(listen_and_serv); |
| 30 | + |
| 31 | +namespace f = paddle::framework; |
| 32 | +namespace p = paddle::platform; |
| 33 | +namespace m = paddle::operators::math; |
| 34 | +namespace detail = paddle::operators::detail; |
| 35 | +namespace string = paddle::string; |
| 36 | + |
| 37 | +std::unique_ptr<detail::AsyncGRPCServer> rpc_service; |
| 38 | + |
| 39 | +void StartServer() { |
| 40 | + f::Scope scope; |
| 41 | + p::CPUPlace place; |
| 42 | + scope.Var("NCCLID"); |
| 43 | + p::DeviceContextPool& pool = p::DeviceContextPool::Instance(); |
| 44 | + auto& dev_ctx = *pool.Get(p::CPUPlace()); |
| 45 | + |
| 46 | + rpc_service.reset(new detail::AsyncGRPCServer("127.0.0.1:0", true)); |
| 47 | + |
| 48 | + f::ProgramDesc empty_program; |
| 49 | + f::Executor executor(dev_ctx.GetPlace()); |
| 50 | + rpc_service->SetScope(&scope); |
| 51 | + rpc_service->SetDevCtx(&dev_ctx); |
| 52 | + rpc_service->SetProgram(&empty_program); |
| 53 | + rpc_service->SetExecutor(&executor); |
| 54 | + |
| 55 | + std::thread server_thread( |
| 56 | + std::bind(&detail::AsyncGRPCServer::RunSyncUpdate, rpc_service.get())); |
| 57 | + rpc_service->SetCond(0); |
| 58 | + auto recv = rpc_service->Get(); |
| 59 | + LOG(INFO) << "got nccl id and stop server..."; |
| 60 | + rpc_service->ShutDown(); |
| 61 | + server_thread.join(); |
| 62 | +} |
| 63 | + |
| 64 | +TEST(SendNcclId, Normal) { |
| 65 | + std::thread server_thread(StartServer); |
| 66 | + // wait server to start |
| 67 | + sleep(2); |
| 68 | + |
| 69 | + f::Scope scope; |
| 70 | + p::CPUPlace place; |
| 71 | + p::DeviceContextPool& pool = p::DeviceContextPool::Instance(); |
| 72 | + auto& dev_ctx = *pool.Get(p::CPUPlace()); |
| 73 | + |
| 74 | + auto var = scope.Var("NCCLID"); |
| 75 | + // var->SetType(f::proto::VarType_Type_RAW); |
| 76 | + auto id = var->GetMutable<ncclUniqueId>(); |
| 77 | + p::dynload::ncclGetUniqueId(id); |
| 78 | + |
| 79 | + int port = rpc_service->GetSelectedPort(); |
| 80 | + std::string ep = string::Sprintf("127.0.0.1:%d", port); |
| 81 | + detail::RPCClient client; |
| 82 | + |
| 83 | + client.AsyncSendVariable(ep, dev_ctx, scope, "NCCLID"); |
| 84 | + client.Wait(); |
| 85 | + server_thread.join(); |
| 86 | + auto* ptr = rpc_service.release(); |
| 87 | + delete ptr; |
| 88 | +} |
0 commit comments