Matrix.cpp 1.7 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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
#include "PaddleCAPI.h"
#include "PaddleCAPIPrivate.h"
#include "hl_cuda.h"

#define cast(v) paddle::capi::cast<paddle::capi::CMatrix>(v)
extern "C" {
int PDMatCreate(PD_Matrix* mat, uint64_t height, uint64_t width, bool useGpu) {
  auto ptr = new paddle::capi::CMatrix();
  ptr->mat = paddle::Matrix::create(height, width, false, useGpu);
  *mat = ptr;
  return PD_NO_ERROR;
}

int PDMatCreateNone(PD_Matrix* mat) {
  auto ptr = new paddle::capi::CMatrix();
  *mat = ptr;
  return PD_NO_ERROR;
}

int PDMatDestroy(PD_Matrix mat) {
  if (mat == nullptr) return PD_NULLPTR;
  auto ptr = cast(mat);
  delete ptr;
  return PD_NO_ERROR;
}

int PDMatCopyToRow(PD_Matrix mat, uint64_t rowID, pd_real* rowArray) {
  if (mat == nullptr) return PD_NULLPTR;
  auto ptr = cast(mat);
  if (ptr->mat == nullptr) return PD_NULLPTR;
  if (rowID >= ptr->mat->getHeight()) return PD_OUT_OF_RANGE;
  paddle::real* buf = ptr->mat->getRowBuf(rowID);
  size_t width = ptr->mat->getWidth();
#ifndef PADDLE_ONLY_CPU
  hl_memcpy(buf, rowArray, sizeof(paddle::real) * width);
#else
  std::copy(rowArray, rowArray + width, buf);
#endif
  return PD_NO_ERROR;
}

int PDMatGetRow(PD_Matrix mat, uint64_t rowID, pd_real** rawRowBuffer) {
  if (mat == nullptr) return PD_NULLPTR;
  auto ptr = cast(mat);
  if (ptr->mat == nullptr) return PD_NULLPTR;
  if (rowID >= ptr->mat->getHeight()) return PD_OUT_OF_RANGE;
  *rawRowBuffer = ptr->mat->getRowBuf(rowID);
  return PD_NO_ERROR;
}

int PDMatGetShape(PD_Matrix mat, uint64_t* height, uint64_t* width) {
  if (mat == nullptr) return PD_NULLPTR;
  if (height != nullptr) {
    *height = cast(mat)->mat->getHeight();
  }
  if (width != nullptr) {
    *width = cast(mat)->mat->getWidth();
  }
  return PD_NO_ERROR;
}
}