mxnet
ndarray.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
26 #ifndef MXNET_CPP_NDARRAY_H_
27 #define MXNET_CPP_NDARRAY_H_
28 
29 #include <map>
30 #include <memory>
31 #include <string>
32 #include <vector>
33 #include <iostream>
34 #include "mxnet-cpp/base.h"
35 #include "mxnet-cpp/shape.h"
36 
37 namespace mxnet {
38 namespace cpp {
39 
40 enum DeviceType { kCPU = 1, kGPU = 2, kCPUPinned = 3 };
41 
45 class Context {
46  public:
52  Context(const DeviceType& type, int id) : type_(type), id_(id) {}
57  return type_;
58  }
62  int GetDeviceId() const {
63  return id_;
64  }
65 
71  static Context gpu(int device_id = 0) {
72  return Context(DeviceType::kGPU, device_id);
73  }
74 
80  static Context cpu(int device_id = 0) {
81  return Context(DeviceType::kCPU, device_id);
82  }
83 
84  private:
85  DeviceType type_;
86  int id_;
87 };
88 
92 struct NDBlob {
93  public:
97  NDBlob() : handle_(nullptr) {}
102  explicit NDBlob(NDArrayHandle handle) : handle_(handle) {}
108  }
113 
114  private:
115  NDBlob(const NDBlob&);
116  NDBlob& operator=(const NDBlob&);
117 };
118 
122 class NDArray {
123  public:
127  NDArray();
131  explicit NDArray(const NDArrayHandle& handle);
139  NDArray(const std::vector<mx_uint>& shape,
140  const Context& context,
141  bool delay_alloc = true,
142  int dtype = 0);
150  NDArray(const Shape& shape, const Context& context, bool delay_alloc = true, int dtype = 0);
151  NDArray(const mx_float* data, size_t size);
158  NDArray(const mx_float* data, const Shape& shape, const Context& context);
165  NDArray(const std::vector<mx_float>& data, const Shape& shape, const Context& context);
166  explicit NDArray(const std::vector<mx_float>& data);
172  NDArray operator+(const NDArray&);
173  NDArray operator-(const NDArray&);
174  NDArray operator*(const NDArray&);
175  NDArray operator/(const NDArray&);
176  NDArray operator%(const NDArray&);
224  NDArray& operator+=(const NDArray& src);
231  NDArray& operator-=(const NDArray& src);
238  NDArray& operator*=(const NDArray& src);
245  NDArray& operator/=(const NDArray& src);
252  NDArray& operator%=(const NDArray& src);
264  void SyncCopyFromCPU(const mx_float* data, size_t size);
274  void SyncCopyFromCPU(const std::vector<mx_float>& data);
285  void SyncCopyToCPU(mx_float* data, size_t size = 0);
296  void SyncCopyToCPU(std::vector<mx_float>* data, size_t size = 0);
302  NDArray CopyTo(NDArray* other) const;
308  NDArray Copy(const Context&) const;
315  size_t Offset(size_t h = 0, size_t w = 0) const;
323  size_t Offset(size_t c, size_t h, size_t w) const;
329  mx_float At(size_t index) const;
336  mx_float At(size_t h, size_t w) const;
344  mx_float At(size_t c, size_t h, size_t w) const;
351  NDArray Slice(mx_uint begin, mx_uint end) const;
357  NDArray Reshape(const Shape& new_shape) const;
362  void WaitToRead() const;
367  void WaitToWrite();
372  static void WaitAll();
379  static void SampleGaussian(mx_float mu, mx_float sigma, NDArray* out);
386  static void SampleUniform(mx_float begin, mx_float end, NDArray* out);
395  static void Load(const std::string& file_name,
396  std::vector<NDArray>* array_list = nullptr,
397  std::map<std::string, NDArray>* array_map = nullptr);
403  static std::map<std::string, NDArray> LoadToMap(const std::string& file_name);
409  static std::vector<NDArray> LoadToList(const std::string& file_name);
419  static void LoadFromBuffer(const void* buffer,
420  size_t size,
421  std::vector<NDArray>* array_list = nullptr,
422  std::map<std::string, NDArray>* array_map = nullptr);
429  static std::map<std::string, NDArray> LoadFromBufferToMap(const void* buffer, size_t size);
436  static std::vector<NDArray> LoadFromBufferToList(const void* buffer, size_t size);
442  static void Save(const std::string& file_name, const std::map<std::string, NDArray>& array_map);
448  static void Save(const std::string& file_name, const std::vector<NDArray>& array_list);
452  size_t Size() const;
456  std::vector<mx_uint> GetShape() const;
460  int GetDType() const;
465  const mx_float* GetData() const;
466 
470  Context GetContext() const;
471 
476  return blob_ptr_->handle_;
477  }
478 
479  private:
480  std::shared_ptr<NDBlob> blob_ptr_;
481 };
482 
483 std::ostream& operator<<(std::ostream& out, const NDArray& ndarray);
484 } // namespace cpp
485 } // namespace mxnet
486 
487 #endif // MXNET_CPP_NDARRAY_H_
mxnet
namespace of mxnet
Definition: api_registry.h:33
mxnet::cpp::NDArray::WaitToWrite
void WaitToWrite()
Block until all the pending read/write operations with respect to current NDArray are finished,...
shape.h
definition of shape
mxnet::cpp::Context::cpu
static Context cpu(int device_id=0)
Return a CPU context.
Definition: ndarray.h:80
mxnet::cpp::kGPU
@ kGPU
Definition: ndarray.h:40
mxnet::cpp::NDArray::SyncCopyFromCPU
void SyncCopyFromCPU(const mx_float *data, size_t size)
Do a synchronize copy from a contiguous CPU memory region.
mshadow::expr::scalar
ScalarExp< DType > scalar(DType s)
create an scalar expression
Definition: expression.h:103
mxnet::cpp::NDBlob::NDBlob
NDBlob()
default constructor
Definition: ndarray.h:97
mxnet::cpp::NDArray::SampleGaussian
static void SampleGaussian(mx_float mu, mx_float sigma, NDArray *out)
Sample gaussian distribution for each elements of out.
mxnet::cpp::NDArray::LoadFromBuffer
static void LoadFromBuffer(const void *buffer, size_t size, std::vector< NDArray > *array_list=nullptr, std::map< std::string, NDArray > *array_map=nullptr)
Load NDArrays from buffer.
mxnet::cpp::NDArray::SyncCopyToCPU
void SyncCopyToCPU(mx_float *data, size_t size=0)
Do a synchronize copy to a contiguous CPU memory region.
mxnet::cpp::NDArray::operator-=
NDArray & operator-=(mx_float scalar)
elementwise subtract from current ndarray this mutate the current NDArray
mxnet::cpp::NDArray::NDArray
NDArray()
construct with a none handle
mxnet::cpp::NDArray::GetDType
int GetDType() const
mxnet::cpp::NDBlob::~NDBlob
~NDBlob()
destructor, free the NDArrayHandle
Definition: ndarray.h:106
mxnet::cpp::NDArray::WaitAll
static void WaitAll()
Block until all the pending read/write operations with respect to current NDArray are finished,...
mxnet::cpp::NDArray::Save
static void Save(const std::string &file_name, const std::map< std::string, NDArray > &array_map)
save a map of string->NDArray to binary file.
mxnet::cpp::operator<<
std::ostream & operator<<(std::ostream &out, const NDArray &ndarray)
mxnet::cpp::Context::GetDeviceType
DeviceType GetDeviceType() const
Definition: ndarray.h:56
mxnet::cpp::NDArray
NDArray interface.
Definition: ndarray.h:122
mxnet::cpp::NDArray::Size
size_t Size() const
mxnet::cpp::NDArray::Offset
size_t Offset(size_t h=0, size_t w=0) const
return offset of the element at (h, w)
mxnet::cpp::NDArray::SampleUniform
static void SampleUniform(mx_float begin, mx_float end, NDArray *out)
Sample uniform distribution for each elements of out.
mxnet::cpp::Context
Context interface.
Definition: ndarray.h:45
mxnet::cpp::NDBlob
struct to store NDArrayHandle
Definition: ndarray.h:92
mxnet::cpp::Context::GetDeviceId
int GetDeviceId() const
Definition: ndarray.h:62
mxnet::cpp::NDArray::ArgmaxChannel
NDArray ArgmaxChannel()
mxnet::cpp::NDArray::operator+=
NDArray & operator+=(mx_float scalar)
elementwise add to current space this mutate the current NDArray
mxnet::cpp::NDArray::operator*
NDArray operator*(mx_float scalar)
mxnet::cpp::NDBlob::NDBlob
NDBlob(NDArrayHandle handle)
construct with a NDArrayHandle
Definition: ndarray.h:102
mxnet::cpp::NDArray::operator%=
NDArray & operator%=(mx_float scalar)
elementwise modulo from current ndarray this mutate the current NDArray
mxnet::cpp::kCPUPinned
@ kCPUPinned
Definition: ndarray.h:40
mxnet::cpp::NDArray::operator=
NDArray & operator=(mx_float scalar)
set all the elements in ndarray to be scalar
mxnet::cpp::NDArray::LoadToList
static std::vector< NDArray > LoadToList(const std::string &file_name)
Load list of NDArrays from binary file.
mx_float
float mx_float
manually define float
Definition: c_api.h:67
mxnet::cpp::NDBlob::handle_
NDArrayHandle handle_
the NDArrayHandle
Definition: ndarray.h:112
mxnet::NDArray
ndarray interface
Definition: ndarray.h:82
mxnet::cpp::NDArray::operator/=
NDArray & operator/=(mx_float scalar)
elementwise division from current ndarray this mutate the current NDArray
mxnet::cpp::NDArray::Slice
NDArray Slice(mx_uint begin, mx_uint end) const
Slice a NDArray.
mxnet::cpp::NDArray::LoadFromBufferToMap
static std::map< std::string, NDArray > LoadFromBufferToMap(const void *buffer, size_t size)
Load map of NDArrays from buffer.
MXNDArrayFree
MXNET_DLL int MXNDArrayFree(NDArrayHandle handle)
free the narray handle
mxnet::cpp::NDArray::operator+
NDArray operator+(mx_float scalar)
mxnet::cpp::NDArray::GetShape
std::vector< mx_uint > GetShape() const
mxnet::cpp::NDArray::operator/
NDArray operator/(mx_float scalar)
mxnet::cpp::NDArray::Reshape
NDArray Reshape(const Shape &new_shape) const
Return a reshaped NDArray that shares memory with current one.
mxnet::cpp::Shape
dynamic shape class that can hold shape of arbirary dimension
Definition: shape.h:42
mxnet::cpp::NDArray::CopyTo
NDArray CopyTo(NDArray *other) const
copy the content of current array to a target array.
mxnet::NDArrayHandle
Definition: ndarray_handle.h:40
mxnet::cpp::NDArray::GetHandle
NDArrayHandle GetHandle() const
Definition: ndarray.h:475
mxnet::cpp::NDArray::operator-
NDArray operator-(mx_float scalar)
mxnet::cpp::NDArray::Load
static void Load(const std::string &file_name, std::vector< NDArray > *array_list=nullptr, std::map< std::string, NDArray > *array_map=nullptr)
Load NDArrays from binary file.
mxnet::cpp::Context::gpu
static Context gpu(int device_id=0)
Return a GPU context.
Definition: ndarray.h:71
mxnet::cpp::NDArray::At
mx_float At(size_t index) const
return value of the element at (index)
mxnet::cpp::NDArray::operator*=
NDArray & operator*=(mx_float scalar)
elementwise multiplication to current ndarray this mutate the current NDArray
mxnet::cpp::NDArray::Copy
NDArray Copy(const Context &) const
return a new copy to this NDArray
base.h
base definitions for mxnetcpp
mxnet::cpp::NDArray::LoadFromBufferToList
static std::vector< NDArray > LoadFromBufferToList(const void *buffer, size_t size)
Load list of NDArrays from buffer.
mxnet::cpp::NDArray::operator%
NDArray operator%(mx_float scalar)
mxnet::cpp::NDArray::WaitToRead
void WaitToRead() const
Block until all the pending write operations with respect to current NDArray are finished,...
mx_uint
uint32_t mx_uint
manually define unsigned int
Definition: c_api.h:65
mxnet::cpp::kCPU
@ kCPU
Definition: ndarray.h:40
mxnet::cpp::NDArray::LoadToMap
static std::map< std::string, NDArray > LoadToMap(const std::string &file_name)
Load map of NDArrays from binary file.
mxnet::cpp::DeviceType
DeviceType
Definition: ndarray.h:40
mxnet::cpp::NDArray::GetContext
Context GetContext() const
mxnet::cpp::NDArray::GetData
const mx_float * GetData() const
Get the pointer to data (IMPORTANT: The ndarray should not be in GPU)
mxnet::cpp::Context::Context
Context(const DeviceType &type, int id)
Context constructor.
Definition: ndarray.h:52