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 am asking this question to save some time and I am expecting answers only from people who have tried something like this before. (I realize that I could just try it and see if it works, but that takes a non-trivial amount of time.)

Suppose you wrote a simulation in pure Mathematica. As the simulation progresses, a vector is being periodically updated changed. One could then have a Dynamic@ListPlot[vector] and watch the progress "live".

Now the question is: is this possible when using LibraryLink? With LibraryLink it's possible to access a kernel variable (vector) directly. Is it possible to get the system to register the changes to that variable and to run the Dynamic when necessary? Or is there an alternative simple (low effort) way to achieve the same, i.e. use Mathematica to visualize a simulation "live"?

One way I see that certainly work is periodically calling back to the kernel through a MathLink connection and setting a variable through Mma code, which contains the information to be visualized. Is there a simpler/better way?

share|improve this question

1 Answer 1

up vote 17 down vote accepted

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 shared tensor. It looks like this and if you click on the image you can watch a video showing the thing in action

enter image description here

The video shows an image of dimension 768x768 and the green bars are my 16 threads working.

To answer your question let me give some code from the implementation. The whole DynamicModule is only some initialisations for the roots, the colours etc and every time something changes the function

cNewtonFractalShared[Complex @@@ roots, {-b, b, -b, b}, colors, colorGamma, raster]  

The important parameter is raster because in the definition of cNewtonFractalShared it is defined as being shared by Mathematica and C++

cNewtonFractalShared = LibraryFunctionLoad[$lib, "calculateNewtonFractal",
  {{_Complex, 1, "Constant"}, {_Real, 1, "Constant"}, {_Real, 1, "Constant"},
   {_Real, 0, "Constant"}, {_Integer, 3, "Shared"}}, 
  "Void"
]  

In the C++ function calculateNewtonFractal I just access this raster with

MTensor m_image = MArgument_getMTensor(args[4]);
mint *output_image_data = lib->MTensor_getIntegerData(m_image);

In the core calculation itself I use output_image_data to store the result without (re-)allocating anything. That's all what I did.

share|improve this answer
1  
Wow. Just, wow. +1 –  rcollyer Jun 10 '13 at 12:28
    
It's impressive! +1 –  Silvia Jun 10 '13 at 14:01
    
The updating still happens when cNewtonFractalShared returns, right? Do you know if it possible to force updating before it returns? Perhaps the way to force updating is exactly to return to Mathematica, and then call the function again from Mathematica to continue? (There should be some way to limit updating speed, and this could be the number of calculation steps made inbetween returns.) –  Szabolcs Jun 10 '13 at 15:08
    
@Szabolcs Yes. I haven't tested it but what I would try is the following: Look at the demo_shared.c in the LibrayLink examples source. There you see that they have separat functions store the pointer, get values and set values of tensor. Your dynamic monitor of your partial result should only be read operations, maybe this can be done while your calculating the result. Therefore, call your calculation function and start this in a new thread and return immediatly. Then you can check periodically the value of your tensor as often as you like. You'll need another function indicating .. –  halirutan Jun 10 '13 at 17:43
    
..when your calculation is done. –  halirutan Jun 10 '13 at 17:45

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.