brpc_rdma_pool.cc 2.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2018 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_BRPC_RDMA

W
Wu Yi 已提交
17
#include "paddle/fluid/operators/distributed/brpc/brpc_rdma_pool.h"
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#include "brpc/channel.h"
#include "brpc/rdma/rdma_helper.h"
#include "paddle/fluid/platform/enforce.h"

namespace paddle {
namespace operators {
namespace distributed {

RdmaMemPool& RdmaMemPool::Instance() {
  static RdmaMemPool* g_rdma_mem_pool = new RdmaMemPool();
  return *g_rdma_mem_pool;
}

void* RdmaMemPool::Find(const std::string& varname, int64_t size) {
  pthread_rwlock_rdlock(&access_);
  auto it = pool_.find(varname);
  if (it == pool_.end()) {
    pthread_rwlock_unlock(&access_);
    return nullptr;
  }

  auto info = it->second;
  if (info.data_size != size) {
    pthread_rwlock_unlock(&access_);
    PADDLE_ENFORCE(false, "var:%s size:%ld != %ld", varname, size,
                   info.data_size);
    return nullptr;
  }

  pthread_rwlock_unlock(&access_);
  return info.data;
}

void RdmaMemPool::Register(const std::string& varname, void* data,
                           int64_t data_size) {
  void* old = Find(varname, data_size);
  if (old != nullptr) {
    if (data != old) {
      PADDLE_ENFORCE(false, "var:%s data:%ld != %ld", varname, data, old);
    }
    VLOG(7) << "Find on rdma:" << varname << " data:" << data
            << " data_size:" << data_size;
    return;
  }

  VarInfo info;
  info.data = data;
  info.data_size = data_size;

  pthread_rwlock_wrlock(&access_);
  pool_[varname] = info;
  pthread_rwlock_unlock(&access_);

  if (brpc::rdma::RegisterMemoryForRdma(data, data_size)) {
72 73 74 75
    PADDLE_THROW(platform::errors::Unavailable(
        "Register memory for RDMA failed. Register %s data: %s data size %d "
        "error.",
        varname, data, data_size));
76 77 78 79 80 81 82 83 84 85 86
  }

  VLOG(4) << "register on rdma:" << varname << " data:" << data
          << " data_size:" << data_size;
}

}  // namespace distributed
}  // namespace operators
}  // namespace paddle

#endif