memory.cpp 3.1 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2019 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.

W
wangguibao 已提交
15 16
#include "predictor/framework/memory.h"
#include "predictor/common/inner_common.h"
W
wangguibao 已提交
17 18 19 20 21

namespace baidu {
namespace paddle_serving {
namespace predictor {

X
xulongteng 已提交
22
struct MempoolRegion {
23 24 25 26
  MempoolRegion(im::fugue::memory::Region* region, im::Mempool* mempool)
      : _region(region), _mempool(mempool) {}
  im::fugue::memory::Region* region() { return _region; }
  im::Mempool* mempool() { return _mempool; }
X
xulongteng 已提交
27 28 29

  im::fugue::memory::Region* _region;
  im::Mempool* _mempool;
X
xulongteng 已提交
30 31 32 33 34 35 36 37 38 39
  ~MempoolRegion() {
    if (_region) {
      delete _region;
      _region = NULL;
    }
    if (_mempool) {
      delete _mempool;
      _mempool = NULL;
    }
  }
X
xulongteng 已提交
40 41
};

W
wangguibao 已提交
42
int MempoolWrapper::initialize() {
W
wangguibao 已提交
43 44 45 46 47 48 49 50 51 52
  if (THREAD_KEY_CREATE(&_bspec_key, NULL) != 0) {
    LOG(ERROR) << "unable to create thread_key of thrd_data";
    return -1;
  }
  if (THREAD_SETSPECIFIC(_bspec_key, NULL) != 0) {
    LOG(ERROR) << "failed initialize bsepecific key to null";
    return -1;
  }

  return 0;
W
wangguibao 已提交
53 54 55
}

int MempoolWrapper::thread_initialize() {
56
  im::fugue::memory::Region* region = new im::fugue::memory::Region();
X
xulongteng 已提交
57 58
  region->init();
  im::Mempool* mempool = new (std::nothrow) im::Mempool(region);
59
  MempoolRegion* mempool_region = new MempoolRegion(region, mempool);
X
xulongteng 已提交
60
  if (mempool == NULL) {
W
wangguibao 已提交
61 62 63 64
    LOG(ERROR) << "Failed create thread mempool";
    return -1;
  }

X
xulongteng 已提交
65
  if (THREAD_SETSPECIFIC(_bspec_key, mempool_region) != 0) {
W
wangguibao 已提交
66
    LOG(ERROR) << "unable to set the thrd_data";
X
xulongteng 已提交
67 68
    delete region;
    delete mempool;
X
xulongteng 已提交
69
    delete mempool_region;
W
wangguibao 已提交
70 71 72 73 74
    return -1;
  }

  LOG(WARNING) << "Succ thread initialize mempool wrapper";
  return 0;
W
wangguibao 已提交
75 76 77
}

int MempoolWrapper::thread_clear() {
78 79
  MempoolRegion* mempool_region =
      (MempoolRegion*)THREAD_GETSPECIFIC(_bspec_key);
X
xulongteng 已提交
80 81 82 83 84 85 86 87 88
  if (mempool_region == NULL) {
    LOG(WARNING) << "THREAD_GETSPECIFIC() returned NULL";
    return -1;
  }
  im::Mempool* mempool = mempool_region->mempool();
  im::fugue::memory::Region* region = mempool_region->region();
  if (mempool) {
    mempool->release_block();
    region->reset();
W
wangguibao 已提交
89 90
  }
  return 0;
W
wangguibao 已提交
91 92 93
}

void* MempoolWrapper::malloc(size_t size) {
94 95
  MempoolRegion* mempool_region =
      (MempoolRegion*)THREAD_GETSPECIFIC(_bspec_key);
X
xulongteng 已提交
96 97 98 99 100 101 102
  if (mempool_region == NULL) {
    LOG(WARNING) << "THREAD_GETSPECIFIC() returned NULL";
    return NULL;
  }

  im::Mempool* mempool = mempool_region->mempool();
  if (!mempool) {
W
wangguibao 已提交
103 104 105 106
    LOG(WARNING) << "Cannot malloc memory:" << size
                 << ", since mempool is not thread initialized";
    return NULL;
  }
X
xulongteng 已提交
107
  return mempool->malloc(size);
W
wangguibao 已提交
108 109
}

W
wangguibao 已提交
110 111 112
}  // namespace predictor
}  // namespace paddle_serving
}  // namespace baidu