target_wrapper.cc 3.1 KB
Newer Older
R
root 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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.
#include "lite/backends/bm/target_wrapper.h"
#include "bmlib_runtime.h"
C
cen.li 已提交
16
#include "bmcompiler_if.h"
R
root 已提交
17 18 19 20

namespace paddle {
namespace lite {

B
baolei.an 已提交
21 22
int TargetWrapperBM::device_id_ = 0;
std::map<int, void*> TargetWrapperBM::bm_hds_; 
C
cen.li 已提交
23

R
root 已提交
24 25 26 27 28 29 30
size_t TargetWrapperBM::num_devices() {
  int count = 0;
  bm_dev_getcount(&count);
  return count;
}

void TargetWrapperBM::SetDevice(int id) {
B
baolei.an 已提交
31 32 33 34 35 36 37
/*
  if (id < 0 || (size_t)id >= num_devices()) {
    LOG(FATAL) << "Failed with invalid device id " << id;
  }
*/   
  device_id_ = id;
  if (bm_hds_.find(id) == bm_hds_.end()) {
R
root 已提交
38
    bm_handle_t bm_handle;
C
cen.li 已提交
39 40
    bm_status_t ret = bm_dev_request(&bm_handle, id);
    CHECK_EQ(ret, BM_SUCCESS) << "Failed with error code: " << (int)ret;
B
baolei.an 已提交
41
    bm_hds_.insert(std::pair<int, bm_handle_t>(id, bm_handle));
R
root 已提交
42 43 44
  }
  return;
}
B
baolei.an 已提交
45 46 47 48 49 50 51
    
void* TargetWrapperBM::GetHandle() {
  if (bm_hds_.find(device_id_) == bm_hds_.end()) {
    LOG(FATAL) << "device not initialized " << device_id_;
  }
  return bm_hds_.at(device_id_);
}
R
root 已提交
52 53 54 55

void* TargetWrapperBM::Malloc(size_t size) {
  void* ptr{};

B
baolei.an 已提交
56 57
  if (bm_hds_.find(device_id_) == bm_hds_.end()) {
      SetDevice(device_id_);
C
cen.li 已提交
58 59
  } 

B
baolei.an 已提交
60
  bm_handle_t bm_handle = static_cast<bm_handle_t>(bm_hds_.at(device_id_));
R
root 已提交
61 62 63 64 65 66 67 68
  bm_device_mem_t* p_mem = (bm_device_mem_t*)malloc(sizeof(bm_device_mem_t));
  bm_malloc_device_byte(bm_handle, p_mem, size);
  ptr = (void*)p_mem;
  return ptr;
}

void TargetWrapperBM::Free(void* ptr) {
  if (ptr != NULL) {
B
baolei.an 已提交
69
    bm_handle_t bm_handle = static_cast<bm_handle_t>(bm_hds_.at(device_id_));
R
root 已提交
70 71 72 73 74 75 76 77 78 79 80
    bm_device_mem_t* mem = static_cast<bm_device_mem_t*>(ptr);
    bm_free_device(bm_handle, *mem);
    free(ptr);
  }
  return;
}

void TargetWrapperBM::MemcpySync(void* dst,
                                   const void* src,
                                   size_t size,
                                   IoDirection dir) {
B
baolei.an 已提交
81
  if (bm_hds_.find(device_id_) == bm_hds_.end()){
R
root 已提交
82 83 84
    return;
  }

B
baolei.an 已提交
85
  bm_handle_t bm_handle = static_cast<bm_handle_t>(bm_hds_.at(device_id_));
R
root 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  bm_device_mem_t* pmem{};
  const bm_device_mem_t* pcst_mem{};

  switch (dir) {
    case IoDirection::HtoD:
      pmem = static_cast<bm_device_mem_t*>(dst);
      bm_memcpy_s2d_partial_offset(bm_handle, *pmem, (void*)src, size, 0);
      break;
    case IoDirection::DtoH:
      pcst_mem = static_cast<const bm_device_mem_t*>(src);
      bm_memcpy_d2s_partial_offset(bm_handle, (void*)(dst), *pcst_mem, size, 0);
      break;
    default:
      LOG(FATAL) << "Unsupported IoDirection " << static_cast<int>(dir);
      break;
  }
  return;
}
    
}  // namespace lite
}  // namespace paddle