Notice removed Reward existing answer by Szabolcs
    Bounty Ended with halirutan's answer chosen by Szabolcs
    Notice added Reward existing answer by Szabolcs
    Bounty Started worth 50 reputation by Szabolcs
3 added 1 character in body
source | link

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" preventpromises the library function from changingto 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?

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" prevent the library function from changing 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?

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?

    Tweeted twitter.com/#!/StackMma/status/438125476566274049
2 added 197 characters in body
source | link

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" prevent the library function from changing 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?

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" prevent the library function from changing 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?

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" prevent the library function from changing 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?

1
source | link

How to understand shared passing of a MTensor in LibraryLink?

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" prevent the library function from changing 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?