MTensor supports shared pass, where the argument can be modified in the library function and that modification can be seen in Mathematica side. Is it possible to do the same thing to a integer or double type?

For example, in the following code, the library function takes an integer and set it to 2.

MMASrc = "#include <WolframLibrary.h>
  #include <WolframCompileLibrary.h>

  DLLEXPORT mint WolframLibrary_getVersion(){
       return WolframLibraryVersion;

  DLLEXPORT int WolframLibrary_initialize(WolframLibraryData libData){
       return 0;
  EXTERN_C DLLEXPORT int test(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res){

  int  a = MArgument_getInteger(Args[0]);
  a=2; //library function set a to 2

lib = CreateLibrary[MMASrc, "test", "Debug" -> True, 
   "TargetDirectory" -> "/tmp"];
testFunc = LibraryFunctionLoad[lib, "test", {Integer}, "Void"];
a = 3;


However, I get 3 as output. So it is possible to have shared pass so that I Mathematica can reflect the modify of a by the library function, except making it into a MTensor with one element?

  • 1
    $\begingroup$ I think it is not possible since integer is not a reference to the memory like arrays. $\endgroup$ – ybeltukov Sep 11 '14 at 18:03
  • $\begingroup$ mathematica.stackexchange.com/questions/96413/… $\endgroup$ – Szabolcs Oct 7 '15 at 15:51
  • $\begingroup$ My guess was: yes, it should be possible. That's because there's such as thing as a rank-0 MTensor. It's used in MSparseArray. I can load a LibraryFunction with an argument type of {Integer, 0, "Shared"}. But if I try to call it, I get an error: LibraryFunction::cfsa: Argument 1 at position 2 should be a machine-size integer. So: I don't know the answer. $\endgroup$ – Szabolcs Oct 7 '15 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.