# Mathlink program with low cpu usage on linux machines only

## Question

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?

## Details

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

mprep function.tm -o function.tm.cpp

• @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! – Alex Gurfinkel Jul 24 '14 at 18:53