Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to use LibraryLink in Mathematica, and come across some problems.

Consider if we have a MTensor m1 which contains a one dimensional double array, and I want to get the data of that array. I come across two methods. One is to use library callback functions libData->MTensor_getRealData(m1) as shown here , the other is use the micros MTensor_getRealDataMacro(m1) in the WolframCompileLibrary.h file as shown here.

What are the differences between them? Does the library call back functions are slower then the macro since they need to "call back" to Mathematica?

share|improve this question

1 Answer 1

up vote 7 down vote accepted

Does the library call back functions are slower then the macro since they need to "call back" to Mathematica?

I'm pretty sure this is not the case. I guess that the difference is really that the WolframLibrary.h is a public interface which hides all implementation, while the WolframCompileLibrary.h is used by the CCodeGenerator` and gives you at least some insight.

That being said, while you find the full declaration of the MTensor struct in the WolframCompileLibrary.h

struct M_TENSOR_STRUCT
{
        void *data;
        TensorProperty properties;
        mint flattened_length;
};

this is not exposed in the WolframLinkLibrary.h.

The biggest indicator that you should only use the WolframLibrary is that there is no documentation about the CompileLibrary at all. You only learn about their functionality when you look on the code of functions like CCodeStringGenerate.

Does the library call back functions are slower then the macro since they need to "call back" to Mathematica?

I guess you mean, slower because it uses MathLink to talk to the kernel? No, I'm pretty sure this doesn't happen, because exactly this is the advantage and disadvantage of the WolframLibrary. An advantage, because you have direct C-level access on the internal data of the kernel, like an MTensor. Disadvantage, because when you screw up, you crash the kernel. No layer of communication between you and the fragile data of the kernel.

By the way

if you want, you can easily do benchmarks by yourself. The code for showing that both, the WolframLibrary and the CompileLibrary point to the same data buffer when you extract it from an MTensor is as short as

#include <stdio.h>
#include "WolframRTL.h"

DLLEXPORT int tensor(WolframLibraryData libData, mint Argc, 
  MArgument *Args, MArgument Res) {
    char buffer[1024];
    MTensor T0;
    mint *I0, *I1;
    T0 = MArgument_getMTensor(Args[0]);
    I0 = libData->MTensor_getIntegerData(T0);
    I1 = (mint*) (T0->data);
    snprintf(buffer, sizeof(buffer), "MTensor_getIntegerData: %p, T0->data %p",I0, I1);
    MArgument_setUTF8String(Res, buffer);
    return LIBRARY_NO_ERROR;
}

Creating a library from that and calling the function with an integer list gives

"MTensor_getIntegerData: 0x7fc3206cbad8, T0->data 0x7fc3206cbad8"

And before you ask (mint*) (T0->data) is the macro expansion of getIntegerDataMacro.

share|improve this answer
    
Thanks a lot, it's very helpful! –  xslittlegrass Feb 17 at 19:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.