0
$\begingroup$

I am currently working on mathematica script on a Raspberry Pi, which will periodically (10 secs) be launched by a bash script. However the runtime of my script really isn't the best.

It's actually around 19-20 secs with a CPU load of 25%. I noticed that only one CPU core is used for my calculations so I tried to parallel the "bottlenecks" in my script with Parallelize[]. Now I achieve a CPU load of 100%, however the runtime extends to 23 secs! (launching the kernels takes more time than I win). Additionaly I'm not sure which commands are worth to be processed in parallel.

One data file (256kb) contains 16000 samples per variabel (2 variables:volt, ampere). I tried to limit the amount of samples imported (I actually only need about 5000) to enhance the runtime with methods suggested here. But still seems as the whole file gets imported.

How can I improve the import effectively? Are there any additional improvements possible which enhance my runtime?

#!/usr/bin/wolfram -script

Print["Mathematica : Importing Config"]
cali = StringSplit[Import["/grapher/calibration.plt"], {"\n","="} ]
fileconfig = StringSplit[Import["/grapher/realtime_config.plt"], "'"]
filepath = fileconfig[[2]] <> fileconfig[[4]] <> ".csv"


Print["Mathematica : Importing Data"]
Paralellize[data = Import[filepath,"Table"]]


Print["Mathematica : Calculating Phaseshift"]
filenumber = StringSplit[fileconfig[[4]], "_"];
filenumber = StringSplit[filenumber[[2]], "."];
filenumber = filenumber[[1]];

voltcal = ToExpression[cali[[4]]];
amperecal = ToExpression[cali[[6]]] * voltcal ;

fv = data[[All, 1]];
fi = data[[All, 2]];

Paralellize[ffts1 = Fourier[fv, FourierParameters -> {-1, 1}];]
Paralellize[ffts2 = Fourier[fi, FourierParameters -> {-1, 1}];]
max = Max[Abs[ffts1]];
pos = First[First[Position[Abs[ffts1], max]]];
shiftrad = Arg[ffts1[[pos]]] - Arg[ffts2[[pos]]];
shiftdegree = shiftrad *180 /Pi;
shiftcos = Cos[shiftrad *-1];

shiftrad = ToString[shiftrad];
shiftdegree = ToString[shiftdegree];
shiftcos = ToString[shiftcos];

ologname = "logname='" <> fileconfig[[4]] <> "'";
odatetime = "datetime='" <> fileconfig[[8]] <> "'";
ophaserad = "phaserad='" <> shiftrad <> "'";
ophasedgr = "phasedgr='" <> shiftdegree <> "'";
ocos = "cos='" <> shiftcos <> "'" ;
oumax = "umax='" <> ToString[Max[fv]* voltcal] <> "'";
oumin = "umin='" <> ToString[Min[fv]* voltcal] <> "'";
oimax = "imax='" <> ToString[Max[fi]* amperecal] <> "'";
oimin = "imin='" <> ToString[Min[fi]* amperecal] <> "'";

outputname = fileconfig[[2]] <> "math/math_" <> filenumber <> ".dat";
Export[outputname, {ologname, odatetime , ophaserad , ophasedgr,
   ocos, oumax, oumin, oimax, oimin}]
$\endgroup$
  • 3
    $\begingroup$ Running a Unix filter (awk, grep, perl, ...) on the file before the Mma script is the first thing I would try to solve the "restricted Import" problem... $\endgroup$ – Dr. belisarius Feb 22 '16 at 16:02
  • $\begingroup$ Used grep to store the data in a temp file. 19 sec of processing time -> 16 secs. Thanks! However I still only use 1 CPU core for the whole calculation. $\endgroup$ – TheCheatsrichter Feb 22 '16 at 19:22
  • $\begingroup$ low level Readoperations are typically faster than Import, assuming its a simple file structure. $\endgroup$ – george2079 Feb 22 '16 at 21:28
0
$\begingroup$

First of all I used the head -n 5000 > temp.csv filter to store the first 5000 lines of my data in a temporary file (thanks @Dr. belisarius. for the tip) I didn't additionaly modify the mathematica script itself. I however let the script be executed as an background task in my bash script! This way a new shell is created which runs on a other CPU core! So even if the first mathematica script isn't finished in the 10 second period the new and the old instance of the script both will be calculated on different CPU cores! Also see this.

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.