I am incorporating some c++ code via Mathlink into my simulations in Mathematica. My questions involves the execution efficiency of the c++ code on different machines.

For example, on my windows laptop i see (via task manager) that the Mathlink executable with the code i compiled accounts for roughly 98% cpu usage, while the mathematica kernel makes up the difference. This is what i expect, since the most calculation-intensive portion of the simulation is in the c++ code. However, running the exact same code on various linux machines, i see (via the "top" command) the cpu usage of the Mathlink executable ranging from 20% to 30%, with very little contribution due to the mathematica kernel. There are no competing processes making demands on the cpu in question.

This is upsetting because I want to squeeze out every drop of cpu power I have available. Am I correct in thinking that my program is, in a sense, wasting cpu cycles? Is there a way to make the Mathlink executable approach 100% cpu usage?

Possible Solution (but I hope not)

My internet research into Mathlink program speed has primarily turned up discussions on the slow passing of data between the kernel and the Mathlink program. I suppose it is possible that the transfer of data (< 1 megabyte for each mathlink program call) is the bottleneck here. It could be that the linux computers are more efficient at performing the actual computation, but cannot pass the data fast enough for full cpu usage.

Evidence against this solution comes from the following observations where, in the last column, I am comparing my rough estimates of how quickly (in how many seconds) the computation finishes.

OS      Mma Version Cpu         Usage   Computation Speed
------- ----------- ---------   ----    ------------------------
Windows 7.0         2.2 GHz     98%     100% as fast as windows
Linux   8.0         2.8 GHz     30%     60%  as fast as windows
Linux   9.0         3.4 GHz     22%     40%  as fast as windows

Even if there is some kind of data passing bottlneck, shouldn't the faster computer still finish the computation faster in real time?


On every machine, I am running the same Mathematica script (".m" file), which will Install[] the appropriate Mathlink executable depending on the machine's architecture. So the Linux slowdown is probably not due to different code.

One relevant point may be the Mathematica version, 7.0 on my Windows machine (~100% cpu), 8.0 and 9.0 on the Linux machines (~25% cpu).

Compilation Details (may not be relevant)

For all I know, the details of the compilation of the Mathlink executable on linux is responsible for the slowdown. On both windows and linux, I am compiling my c++ code and template files using mprep and g++ (I cannot get mcc to work for me).

On windows I am using:

mprep.exe function.tm -o function.tm.cpp 
g++ -I <path-to-mathlink.h-folder> function.cpp function.tm.cpp  <path-to-libML32i3.a> -lm -lpthread -mwindows -lstdc++ -o mathlinkexecutable.exe

On different linux machines I am forced to use either the 32 bit or the 64 bit version of the Mathlink library (libML32i3.a and libML64i3.a respectively):

mprep function.tm -o function.tm.cpp 
g++ -I <path-to-mathlink.h-folder> function.cpp function.tm.cpp  <path-to-correct-libML> -lm -lpthread -lrt -o mathlinkexecutable.bin
  • $\begingroup$ You'd better compile your code with symbols included and link against the debug versions of the MathLink libraries. Then, profile your code. Otherwise, without the code (or another example that demonstrates this issue) and the ability to isolate the problem, it sounds like it would be very difficult to answer you conclusively. $\endgroup$ – Oleksandr R. Jul 23 '14 at 1:40
  • 1
    $\begingroup$ If your assumption is correct and the copying and transferring of your data into the MathLink program is the problem, then you might think of using the LibraryLink instead of MathLink. There, you can set up C functions that use the data of Mathematica directly without making a copy first. If you are unsure, you might first consider to write a small program for MathLink and LibraryLink to compare whether the data transfer is indeed the problem. $\endgroup$ – halirutan Jul 23 '14 at 6:21
  • $\begingroup$ Are you sure you are measuring CPU usage correctly on multi-core CPUs? E.g. some process monitors will display 25% when 1 out of 4 cores is fully used. Others would display 100% for 1 core and 400% for 4 cores. Are you compiling the MathLink program with the same version of Mathematica that it is being used with? What is your precise Mathematica version? (E.g. 8.0.4 64-bit.) $\endgroup$ – Szabolcs Jul 23 '14 at 20:25
  • $\begingroup$ Thanks to everyone for the comments! $\endgroup$ – Alex Gurfinkel Jul 24 '14 at 18:43
  • $\begingroup$ @Szabolcs: Yes, I have already accounted for the quirks of different process monitors in the reported values for cpu usage. You caught my failure to mention the versions correctly; I have added the value of the $Version variable for each installation in the table. As for compilation, I have used the copies of mprep, mathlink.h, and libML**i3.a within each intallation's directory structure. Do you have any ideas about how this information can help me? @halirutan:This seems like a good idea. I will search for a LibraryLink tutorial. @OleksandrR.: Makes sense. I was hoping to avoid this! $\endgroup$ – Alex Gurfinkel Jul 24 '14 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.