According to the documentation, there are four different type of memory managing when passing a MTensor with LibraryLink: Automatic, "Constant", "Manual" and "Shared".
The Automatic and "Manual" is easy to understand, since in both of them, the MTensor get copied and passed into the library function. The difference between them is wether the tensor will be automatically clean or not. The "Constant" and "Shared" will pass a reference of the MTensor into the library function, and "Constant" promises the library function to not change the MTensor.
I find "Shared" is difficult for me to understand. Does the "Shared" passing means that variable in Mathematica and my library function point to the same data buffer in the memory? And if I change on one side, the changes will be transparent on the other side ?
Consider this example from the document:
loadFun =
LibraryFunctionLoad["demo_shared",
"loadArray", {{Real, _, "Shared"}}, Integer];
unloadFun =
LibraryFunctionLoad["demo_shared", "unloadArray", {}, Integer];
getFunVector =
LibraryFunctionLoad["demo_shared", "getElementVector", {Integer},
Real];
If we load the library function with an array
array = Range[1., 20];
loadFun[ array]
and change the array on the Mathematica side
array[[1]] = 2.;
getFunVector[ 1]
(* 1. *)
why the data on the library side not changed?
Also, in the documentation, it says after call unload, which disowns the MTensor, array cannot be used anymore,
unloadFun[ ]
(* 0 *)
getFunVector[1]
(* 1. *)
array[[1]]
(* 2. *)
but why I can still use it? What does disown do?
setFunVector
to change the "Constant" MTensor without problem. $\endgroup$