In order to make my computations faster I have developed a C/C++ application calling Mathematica kernel and performing many operations using Mathematica functions for equation solving.

Now I want to use multithreading and in particular I plan on taking advantage of the Posix library in order to do so (pthread). But how does the link behaves when many threads insist on it?

I guess that being a buffered/packet approach, if two threads try to send stuff to the same link without a mutex in guard (of the link), everything is a mess.

SO I should create as many links as many threads I create. So here my questions:

  1. Is there a limit to the number of links I can create to MathLink?
  2. Is there a way to perform parallel activities on the Mathematica kernel in an automated way? I know about the parallel core of Mathematica, but what happens when I am interfacing to Mathematica through MathLink from C/C++?
  • $\begingroup$ You can probably run N mathematica kernels and link to them. $\endgroup$
    – Ajasja
    May 14, 2013 at 20:32
  • 2
    $\begingroup$ You can't use a single kernel from multiple threads (at least not without mutex protection). You can launch as many kernels as you like though, and let each thread have their own. How many depends on your Mathematica license (check the $MaxLicenseProcesses variable). But as I said before, it looks like you are making things much too complicated and difficult for yourself. Since your C program seems to heavily rely on Mathematica anyway, why don't you drive it from Mathematica instead driving Mma from C? You can easily parallelize from Mma then and use $MaxLicenseSubprocesses threads. $\endgroup$
    – Szabolcs
    May 14, 2013 at 22:00
  • $\begingroup$ @Szabolcs: Yeah I am thinking about this change of plans. It is actually getting quite bothersome. Thankyou very much for your suggestion! $\endgroup$
    – Andry
    May 15, 2013 at 7:41
  • 1
    $\begingroup$ I'm curious to know why you want to do multithreaded communication using MathLink. Since the Mathematica interpreter runs in a single thread you aren't going to be able to do it concurrently anyway, so you may as well use a mutex. More to the point, though, it would seem better to try to transfer as much of the necessary data as possible at the beginning and end of your program, rather than continuously communicating--otherwise you'll probably end up being bottlenecked by this communication; MathLink isn't exactly a low-latency channel (not like MPI). $\endgroup$ May 15, 2013 at 12:08
  • 1
    $\begingroup$ @Szabolcs the kernel already provides the cycling function via MathLink`AddSharingLink, as I described here. (Anecdote: this is undocumented, so I did a Web search for it, to see if anything useful came up. The first result was my own post, which I'd already forgotten I had written. Another relevant instance here.) $\endgroup$ May 15, 2013 at 14:49


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.