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.

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++?
share|improve this question
    
You can probably run N mathematica kernels and link to them. –  Ajasja May 14 '13 at 20:32
2  
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. –  Szabolcs May 14 '13 at 22:00
    
@Szabolcs: Yeah I am thinking about this change of plans. It is actually getting quite bothersome. Thankyou very much for your suggestion! –  Andry May 15 '13 at 7:41
1  
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). –  Oleksandr R. May 15 '13 at 12:08
1  
@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.) –  Oleksandr R. May 15 '13 at 14:49

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.