Vector.cpp 7.6 KB
Newer Older
Z
zhangjinchao01 已提交
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
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.

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 "PaddleAPI.h"

#include "paddle/math/Vector.h"

#include <cstring>

struct IVectorPrivate {
  paddle::IVectorPtr vec;
};

IVector::IVector() : m(new IVectorPrivate()) {}

IVector* IVector::createZero(size_t sz, bool useGpu) {
  auto v = new IVector();
  v->m->vec = paddle::IVector::create(sz, useGpu);
  v->m->vec->zeroMem();
  return v;
}

IVector* IVector::create(const std::vector<int>& data, bool useGpu) {
  auto v = new IVector();
  v->m->vec = paddle::IVector::create(data.size(), useGpu);
  v->m->vec->copyFrom(data.data(), data.size());
  return v;
}

42 43 44 45 46 47 48 49 50 51 52
IVector* IVector::createVectorFromNumpy(int* data, int dim, bool copy,
                                        bool useGpu) {
  if (useGpu) {
    /// if use gpu only copy=true is supported
    CHECK(copy);
    return IVector::createGpuVectorFromNumpy(data, dim);
  } else {
    return IVector::createCpuVectorFromNumpy(data, dim, copy);
  }
}

Z
zhangjinchao01 已提交
53 54 55 56 57 58 59 60 61 62 63
IVector* IVector::createCpuVectorFromNumpy(int* data, int dim, bool copy) {
  auto v = new IVector();
  if (copy) {
    v->m->vec = paddle::IVector::create(dim, false);
    v->m->vec->copyFrom(data, dim);
  } else {
    v->m->vec = paddle::IVector::create(data, dim, false);
  }
  return v;
}

64
IVector* IVector::createGpuVectorFromNumpy(int* data, int dim) {
Z
zhangjinchao01 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
  auto v = new IVector();
  v->m->vec = paddle::IVector::create(dim, true);
  v->m->vec->copyFrom(data, dim);
  return v;
}

bool IVector::isGpu() const {
  return dynamic_cast<paddle::GpuIVector*>(m->vec.get()) != nullptr;
}

IntArray IVector::getData() const {
  if (this->isGpu()) {
    int* src = m->vec->getData();
    size_t len = m->vec->getSize();
    int* dest = new int[len];
    hl_memcpy_device2host(dest, src, len * sizeof(int));
    return IntArray(dest, len, true);
  } else {
    return IntArray(m->vec->getData(), m->vec->getSize());
  }
}

int& IVector::operator[](const size_t idx) throw(RangeError, UnsupportError) {
  if (this->isGpu()) {
    UnsupportError e;
    throw e;
  } else {
    if (idx >= m->vec->getSize()) {
      RangeError e;
      throw e;
    }
  }
  return m->vec->getData()[idx];
}

const int& IVector::operator[](const size_t idx) const
    throw(RangeError, UnsupportError) {
  return (*const_cast<IVector*>(this))[idx];
}

IVector* IVector::createByPaddleVectorPtr(void* ptr) {
  auto* p = (paddle::IVectorPtr*)ptr;
  if ((*p) != nullptr) {
    IVector* vec = new IVector();
    vec->m->vec = *p;
    return vec;
  } else {
    return nullptr;
  }
}

IVector::~IVector() { delete m; }

void* IVector::getSharedPtr() const { return &m->vec; }

size_t IVector::getSize() const { return m->vec->getSize(); }

void IVector::toNumpyArrayInplace(int** data, int* dim1) throw(UnsupportError) {
  auto v = std::dynamic_pointer_cast<paddle::CpuIVector>(m->vec);
  if (v) {
    *data = v->getData();
    *dim1 = v->getSize();
  } else {
    throw UnsupportError();
  }
}

void IVector::copyToNumpyArray(int** view_m_data, int* dim1) {
  *dim1 = m->vec->getSize();
  *view_m_data = new int[*dim1];
  if (auto cpuVec = dynamic_cast<paddle::CpuIVector*>(m->vec.get())) {
    std::memcpy(*view_m_data, cpuVec->getData(), sizeof(int) * (*dim1));
  } else if (auto gpuVec = dynamic_cast<paddle::GpuIVector*>(m->vec.get())) {
    hl_memcpy_device2host(*view_m_data, gpuVec->getData(),
                          sizeof(int) * (*dim1));
  } else {
    LOG(INFO) << "Unexpected situation";
  }
}

void IVector::copyFromNumpyArray(int* data, int dim) {
  m->vec->resize(dim);
  m->vec->copyFrom(data, dim);
}

struct VectorPrivate {
  paddle::VectorPtr vec;

  void safeAccessData(const size_t idx,
                      const std::function<void(float&)>& func) const
      throw(RangeError, UnsupportError) {
    auto cpuVec = std::dynamic_pointer_cast<const paddle::CpuVector>(vec);
    if (cpuVec != nullptr) {
      if (idx < vec->getSize()) {
        func(vec->getData()[idx]);
      } else {
        throw RangeError();
      }
    } else {
      throw UnsupportError();
    }
  }
};

Vector::Vector() : m(new VectorPrivate()) {}

Vector::~Vector() {
  if (m) {
    delete m;
  }
}

Vector* Vector::createZero(size_t sz, bool useGpu) {
  auto retVec = new Vector();
  retVec->m->vec = paddle::Vector::create(sz, useGpu);
  retVec->m->vec->zero();
  return retVec;
}

Vector* Vector::create(const std::vector<float>& data, bool useGpu) {
  auto retVec = new Vector();
  retVec->m->vec = paddle::Vector::create(data.size(), useGpu);
  retVec->m->vec->copyFrom(data.data(), data.size());
  return retVec;
}

Vector* Vector::createByPaddleVectorPtr(void* ptr) {
  auto& v = *(paddle::VectorPtr*)(ptr);
  if (v == nullptr) {
    return nullptr;
  } else {
    auto retVec = new Vector();
    retVec->m->vec = v;
    return retVec;
  }
}

202 203 204 205 206 207 208 209 210 211 212
Vector* Vector::createVectorFromNumpy(float* data, int dim, bool copy,
                                      bool useGpu) {
  if (useGpu) {
    /// if use gpu only copy=True is supported
    CHECK(copy);
    return Vector::createGpuVectorFromNumpy(data, dim);
  } else {
    return Vector::createCpuVectorFromNumpy(data, dim, copy);
  }
}

Z
zhangjinchao01 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
Vector* Vector::createCpuVectorFromNumpy(float* data, int dim, bool copy) {
  CHECK_GT(dim, 0);
  auto retVec = new Vector();
  if (copy) {
    retVec->m->vec = paddle::Vector::create((size_t)dim, false);
    return retVec;
  } else {
    retVec->m->vec = paddle::Vector::create(data, (size_t)dim, false);
  }
  return retVec;
}

Vector* Vector::createGpuVectorFromNumpy(float* data, int dim) {
  CHECK_GT(dim, 0);
  auto retVec = new Vector();
  retVec->m->vec = paddle::Vector::create((size_t)dim, true);
  retVec->m->vec->copyFrom(data, (size_t)dim);
  return retVec;
}

void Vector::toNumpyArrayInplace(float** view_data,
                                 int* dim1) throw(UnsupportError) {
  auto v = std::dynamic_pointer_cast<paddle::CpuVector>(m->vec);
  if (v != nullptr) {
    *view_data = v->getData();
    *dim1 = (int)v->getSize();
  } else {
    throw UnsupportError();
  }
}

void Vector::copyToNumpyArray(float** view_m_data, int* dim1) {
  *dim1 = m->vec->getSize();
  *view_m_data = new float[*dim1];
  if (auto cpuVec = dynamic_cast<paddle::CpuVector*>(m->vec.get())) {
    std::memcpy(*view_m_data, cpuVec->getData(), sizeof(float) * (*dim1));
  } else if (auto gpuVec = dynamic_cast<paddle::CpuVector*>(m->vec.get())) {
    hl_memcpy_device2host(*view_m_data, gpuVec->getData(),
                          sizeof(float) * (*dim1));
  } else {
    LOG(INFO) << "Unexpected situation";
  }
}

void Vector::copyFromNumpyArray(float* data, int dim) {
  m->vec->resize(dim);
  m->vec->copyFrom(data, dim);
}

bool Vector::isGpu() const {
  return std::dynamic_pointer_cast<paddle::GpuVector>(m->vec) != nullptr;
}

float Vector::get(const size_t idx) const throw(RangeError, UnsupportError) {
  float r;
  m->safeAccessData(idx, [&](float& o) { r = o; });
  return r;
}

void Vector::set(const size_t idx, float val) throw(RangeError,
                                                    UnsupportError) {
  m->safeAccessData(idx, [&](float& o) { o = val; });
}

size_t Vector::getSize() const { return m->vec->getSize(); }

void* Vector::getSharedPtr() { return &m->vec; }