How to serialize NDArrays in C++ API?

#1

I’m new to mxnet and now I’m working on a C++ project, I want to serialize NDArrays when using RPC to exchange NDArrays but there is only Load&Save APIs in “ndarray.h”, any ideas?

#2

I don’t think C++ API provides method to serialize. However you can use C api for this MXNDArraySaveRawBytes/MXNDArrayLoadFromRawBytes

Here is an example to use them in your cpp code:

// save
size_t sz; const char *buf;
MXNDArraySaveRawBytes(arr.GetHandle(), &sz, &buf );
// Load
NDArrayHandle h;
MXNDArrayLoadFromRawBytes((const void *)buf, sz, &h );
NDarray arr2 = NDArray(h);
#3

Thanks for you help! I have another stupid question…may I directly use C++ APIs in {MXNET_ROOT}/include/mxnet? I find there are many useful APIs.

#4

I believe you can use it directly. (I only used these APIs when implementing custom operators, not from the external project)
In your Makefile make sure you use the same include paths (-I) and macro definitions (-DMSHADOW_USE_MKL=0, etc… ) as you used when compiling mxnet.