67

I wrote a package to automatically generate all the boilerplate needed for LibraryLink and for managed library expressions based on a template that describes a class interface: LTemplate Here's how it works Write a template (a Mathematica) expression that describes a C++ class interface This template is used to generate LibraryLink-compatible C functions ...


36

Here are three very simple examples to show how to call a Fortran subroutine using LibraryLink. First the subroutine is compiled into object file. Then a wrapper is used to call the Fortran subroutine and compiled into dynamic library. At the end, the library is loaded into Mathematica and run. In the examples Mathematica Version 8 is used. FIRST EXAMPLE ...


34

Let me give you a very basic example, how you can employ an asynchronously running LibraryLink function for this specific task. I will not do any real packet listen, but only explain the general setup. Usually, a LibraryFunction in Mathematica is written in C and wrapped with a very small boiler plate code that is needed to attach the function directly to ...


33

This is a community wiki answer. Feel free to improve it. Introduction RawArray is an atomic array type that can hold data in any of the following formats: "Integer8", "UnsignedInteger8", "Integer16", "UnsignedInteger16", "Integer32", "UnsignedInteger32", "Integer64", "UnsignedInteger64", "Real32", "Real64", "Complex64", "Complex128" (Some aliases can ...


25

Taking user5601's suggestion to do a little demo, I quickly whipped this up as an example of ProcessLink being used to do non-trivial communication between Mathematica and an external program, but with much less ceremony than using ProcessLink or MathLink. Let's take this little Go program: package main import "net/http" import "bufio" import "os" import "...


21

Yes, I did something like that and it runs very very nicely. What I implemented is a dynamic Newton fractale visualizer where you can manipulate the number and position of the complex roots, the colours and the gamma correction settings from the Mathematica side. These values are sent to a parallel C++ implementation which calculates the fractale into a ...


21

Note that ListContourPlot3D takes the coordinates to be the position indices by default. If you want to keep the coordinates used in generating the data, then you have to include it. data = Flatten[ Table[{x, y, z, x^2 + y^2 + z^2 + RandomReal[0.1]}, {x, -2, 2, 0.2}, {y, -2, 2, 0.2}, {z, -2, 2, 0.2}], 2]; plot = ListContourPlot3D[data, Contours -&...


20

First of all, you must be relatively comfortable with the C language. That is absolutely a prerequisite. If you are not comfortable with C, brush up your C skills first. Next, look at concrete examples while reading the LibraryLink user guide. The examples are described in the last section. Start with the simplest ones. The user guide is meant more as ...


18

Here is a more general approach. It is based on the 2D method from here. It assumes the polyhedron is not self-intersecting but imposes no requirement of convexity or even connectedness, other than that it be closed and bounded. Strictly speaking, I think this will work for an unbounded polyhedron provided it contains no vertical ray. For ease of exposition ...


17

The purpose of managed library expressions is to provide "garbage collection" (i.e. automatic cleanup) for data structures created in C through LibraryLink. The documentation is here: Managed Library Expressions It comes with a full demo with source code which you should read. What are managed library expressions and what are they good for? Why they are ...


17

Using Compile with CompilationTarget->"C" does generate C-Code to be compiled in a generalized way, the resulting code will contain some overhead due to that compared to hand-written code which can easily explain any difference in runtimes. Even for cases where that overhead is minimal or non-existent automatic code generation will always produce ...


16

In terms of loading C++ functions, I would strongly suggest LibraryLink. It is a great tool except that it requires you to write sometimes intimidating C code. To make LibraryLink easier to use, I have developed a package called wll-interface, available in this github repository. It is a header-only library written in C++, doing only one thing—...


16

At the end of this post you'll find the code for a small benchmark to compare LibraryLink with standard passing vs LibraryLink with MathLink based passing on two counts: function call overhead; use a function that does nothing, has no arguments, returns nothing (llNone and mlNone for LibraryLink and MathLink, respectively) passing real arrays; use a ...


16

Update: The very likely reason for the garbled error messages is that you have a Chinese version of Visual Studio printing errors in Chinese, and there is a mismatch in the character encoding of these messages and how Mathematica tried to interpret them. CreateLibrary has two very useful options: "ShellCommandFunction" and "ShellOutputFunction". Set them ...


14

My advice is to not rely on the code samples in the documentation. It has proven to be unreliable. For instance In the documentation to MTensor_getComplexData the example doesn't even use the function. The documentation to MTensor_free can barely be called documentation since it does not explain about memory allocation etc The documentation to AbortQ states ...


14

There is a package called LTemplate that automates writing some of the boilerplate code for LibraryLink: How to simplify writing LibraryLink code? I consider this less effort than writing standard LibraryLink code. In this sense it is a fitting answer for this question. However, I do recommend familiarizing yourself with the standard way of using ...


14

I can partially answer to my own questions. Amazingly, but it is easier to use the internal MKL. Let us consider my question about multiplication a band matrix by a dense matrix. The corresponding function is mkl_zdiamm. I wrote the following code (diamm.c) #include <stdio.h> #include <stdlib.h> #include <WolframLibrary.h> #include <...


14

Simple solution There is an easier solution than the one I gave almost 2 years ago. In principle, you wrap your library function inside another CompiledFunction that is listable. Let the code speak: fun = LibraryFunctionLoad["demo", "demo_I_I", {Integer}, Integer]; With[{fc = fun}, funListable = Compile[{{i, _Integer, 0}}, fc[i], RuntimeAttributes -&...


14

This cannot be answered very well without knowing your C++ library much better. As you said, you have a choice between MathLink and LibraryLink. Generally, I recommend LibraryLink because: It runs in the same process, and data transfer is much faster than with MathLink It provides features that MathLink does not have, such as direct manipulation of packed ...


13

Let me describe a way which works through all systems and simplifies the distribution of library code within a package a lot. First I want to point out that there are two major scenarios here: You are currently developing a package containing LibraryLink functions. When you are actively working on such a package, it is most likely not installed in your $...


12

Here is a fast method that will "often" work. Roughly, it requires that the convex polygon have no sharp angles between faces. Preprocessing goes as follows. Create triangles from the polygons. So a 5-gon with vertices {a,b,c,d,e} would become the set of triangles {{a,b,c},{a,c,d},{a,d,e}}. For each vertex we average it's star (set of points connected by ...


12

I'm not sure whether I will get everything right here, but to my knowledge the key-point is indeed MTensor_disown. When you call loadFun you basically move the write-priviliges for the array to your library functions. This means, changing values inside the library will be transparent on the Mathematica side. Let's load the library functions: loadFun = ...


12

You can look here for more examples and details: http://community.wolfram.com/groups/-/m/t/189092 http://community.wolfram.com/groups/-/m/t/189735 In short, do the following (call the file DoubleIt.c): #include "WolframLibrary.h" DLLEXPORT int DoubleIt(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res) { mint x; ...


11

Mathematicas invocation of the compiler doesn't know about where to find the Fortran library. With a little help, however, we can point the way. Mind you this was done on a Mac but the Linux variant of Unix will behave similar. Needs["CCompilerDriver`"]; CreateLibrary[{"MMA.cc", "fadd.o"}, "myadd", "Debug" -> True, "TargetDirectory" -> ".", ...


10

Preface I will give a complete solution that shows how a dynamic file watcher can be implemented in Mathematica. The file watcher will track the size of the file and when it changes automatically reload the contents. It will work as an asynchronous library function that does not block the kernel from other evaluations. The example here will additionally ...


9

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 ...


9

You have not defined any message text in Mathematica. The text you supply in the C code is the message tag, e.g libData->Message("myerror"); Then you need to define the actual message content in Mathematica: LibraryFunction::myerror = "Here's my message" The relevant documentation page is here.


9

when I need to compile it, I used the following method: Copy the total code to Mathematica notebook with src = all_code_of_C_file, and compile it with CreateLibrary[src, "lib_link"] This is not how CreateLibrary is meant to be used. You would only type the C code in a Mathematica string if it is so short and so simple that you can't be bothered ...


Only top voted, non community-wiki answers of a minimum length are eligible