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 {
41  kCPU = 1,
42  kGPU = 2,
44 };
45 
49 class Context {
50  public:
56  Context(const DeviceType &type, int id) : type_(type), id_(id) {}
60  DeviceType GetDeviceType() const { return type_; }
64  int GetDeviceId() const { return id_; }
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) {}
106  ~NDBlob() { MXNDArrayFree(handle_); }
111 
112  private:
113  NDBlob(const NDBlob &);
114  NDBlob &operator=(const NDBlob &);
115 };
116 
120 class NDArray {
121  public:
125  NDArray();
129  explicit NDArray(const NDArrayHandle &handle);
137  NDArray(const std::vector<mx_uint> &shape, const Context &context,
138  bool delay_alloc = true, int dtype = 0);
146  NDArray(const Shape &shape, const Context &context,
147  bool delay_alloc = true, int dtype = 0);
148  NDArray(const mx_float *data, size_t size);
155  NDArray(const mx_float *data, const Shape &shape, const Context &context);
162  NDArray(const std::vector<mx_float> &data, const Shape &shape,
163  const Context &context);
164  explicit NDArray(const std::vector<mx_float> &data);
166  NDArray operator-(mx_float scalar);
167  NDArray operator*(mx_float scalar);
168  NDArray operator/(mx_float scalar);
169  NDArray operator%(mx_float scalar);
170  NDArray operator+(const NDArray &);
171  NDArray operator-(const NDArray &);
172  NDArray operator*(const NDArray &);
173  NDArray operator/(const NDArray &);
174  NDArray operator%(const NDArray &);
180  NDArray &operator=(mx_float scalar);
187  NDArray &operator+=(mx_float scalar);
194  NDArray &operator-=(mx_float scalar);
201  NDArray &operator*=(mx_float scalar);
208  NDArray &operator/=(mx_float scalar);
215  NDArray &operator%=(mx_float scalar);
222  NDArray &operator+=(const NDArray &src);
229  NDArray &operator-=(const NDArray &src);
236  NDArray &operator*=(const NDArray &src);
243  NDArray &operator/=(const NDArray &src);
250  NDArray &operator%=(const NDArray &src);
251  NDArray ArgmaxChannel();
262  void SyncCopyFromCPU(const mx_float *data, size_t size);
272  void SyncCopyFromCPU(const std::vector<mx_float> &data);
283  void SyncCopyToCPU(mx_float *data, size_t size = 0);
294  void SyncCopyToCPU(std::vector<mx_float> *data, size_t size = 0);
300  NDArray CopyTo(NDArray * other) const;
306  NDArray Copy(const Context &) const;
313  size_t Offset(size_t h = 0, size_t w = 0) const;
321  size_t Offset(size_t c, size_t h, size_t w) const;
327  mx_float At(size_t index) const;
334  mx_float At(size_t h, size_t w) const;
342  mx_float At(size_t c, size_t h, size_t w) const;
349  NDArray Slice(mx_uint begin, mx_uint end) const;
355  NDArray Reshape(const Shape &new_shape) const;
360  void WaitToRead() const;
365  void WaitToWrite();
370  static void WaitAll();
377  static void SampleGaussian(mx_float mu, mx_float sigma, NDArray *out);
384  static void SampleUniform(mx_float begin, mx_float end, NDArray *out);
393  static void Load(const std::string &file_name,
394  std::vector<NDArray> *array_list = nullptr,
395  std::map<std::string, NDArray> *array_map = nullptr);
401  static std::map<std::string, NDArray> LoadToMap(const std::string &file_name);
407  static std::vector<NDArray> LoadToList(const std::string &file_name);
417  static void LoadFromBuffer(const void *buffer, size_t size,
418  std::vector<NDArray> *array_list = nullptr,
419  std::map<std::string, NDArray> *array_map = nullptr);
426  static std::map<std::string, NDArray> LoadFromBufferToMap(const void *buffer, size_t size);
433  static std::vector<NDArray> LoadFromBufferToList(const void *buffer, size_t size);
439  static void Save(const std::string &file_name,
440  const std::map<std::string, NDArray> &array_map);
446  static void Save(const std::string &file_name,
447  const std::vector<NDArray> &array_list);
451  size_t Size() const;
455  std::vector<mx_uint> GetShape() const;
459  int GetDType() const;
464  const mx_float *GetData() const;
465 
469  Context GetContext() const;
470 
474  NDArrayHandle GetHandle() const { return blob_ptr_->handle_; }
475 
476  private:
477  std::shared_ptr<NDBlob> blob_ptr_;
478 };
479 
480 std::ostream& operator<<(std::ostream& out, const NDArray &ndarray);
481 } // namespace cpp
482 } // namespace mxnet
483 
484 #endif // MXNET_CPP_NDARRAY_H_
NDBlob()
default constructor
Definition: ndarray.h:97
Symbol operator/(mx_float lhs, const Symbol &rhs)
ScalarExp< DType > scalar(DType s)
create an scalar expression
Definition: expression.h:103
float mx_float
manually define float
Definition: c_api.h:59
Shape< 2 > GetShape(const Shape< 2 > &shape, bool transpose)
Definition: dot_engine-inl.h:852
namespace of mxnet
Definition: api_registry.h:33
void Copy(Tensor< cpu, dim, DType > dst, const Tensor< cpu, dim, DType > &src, Stream< cpu > *stream=NULL)
copy data from one tensor to another, with same shape
Definition: tensor_cpu-inl.h:145
Definition: ndarray.h:43
dynamic shape class that can hold shape of arbirary dimension
Definition: shape.h:42
~NDBlob()
destructor, free the NDArrayHandle
Definition: ndarray.h:106
static Context cpu(int device_id=0)
Return a CPU context.
Definition: ndarray.h:80
MXNET_DLL int MXNDArrayFree(NDArrayHandle handle)
free the narray handle
Symbol operator%(mx_float lhs, const Symbol &rhs)
int GetDeviceId() const
Definition: ndarray.h:64
Definition: ndarray.h:41
DeviceType
Definition: ndarray.h:40
NDArray interface.
Definition: ndarray.h:120
NDBlob(NDArrayHandle handle)
construct with a NDArrayHandle
Definition: ndarray.h:102
void SampleGaussian(real_t mu, real_t sigma, NDArray *out)
Sample gaussian distribution for each elements of out.
NDArrayHandle handle_
the NDArrayHandle
Definition: ndarray.h:110
Symbol operator+(mx_float lhs, const Symbol &rhs)
NDArrayHandle GetHandle() const
Definition: ndarray.h:474
void SampleUniform(real_t begin, real_t end, NDArray *out)
Sample uniform distribution for each elements of out.
void * NDArrayHandle
handle to NDArray
Definition: c_api.h:66
std::ostream & operator<<(std::ostream &out, const NDArray &ndarray)
Definition: ndarray.h:42
Symbol operator-(mx_float lhs, const Symbol &rhs)
struct to store NDArrayHandle
Definition: ndarray.h:92
definition of shape
static Context gpu(int device_id=0)
Return a GPU context.
Definition: ndarray.h:71
DeviceType GetDeviceType() const
Definition: ndarray.h:60
Context interface.
Definition: ndarray.h:49
Symbol operator*(mx_float lhs, const Symbol &rhs)
Context(const DeviceType &type, int id)
Context constructor.
Definition: ndarray.h:56
uint32_t mx_uint
manually define unsigned int
Definition: c_api.h:57