Message Boards Message Boards

0
|
6826 Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Script ran with MathKernel -script much slower than in the notebook

Posted 10 years ago

Dear all,

This is my first post so please bear with me if I am a bit naïve about the netiquette or other conventions.

I am post processing lot of experimental data using Mathematica. I have created the notebook with all the instructions to load the data set from an Excel file, do the basic post processing and then run a MonteCarlo simulation on it to obtain the error propagation.

The MonteCarlo simulation is a pretty intensive calculation. Mainly due to the large number of iterations. My test data are transient data and for each second of data I run 10000 MonteCarlo iterations. To give you an example for 38 points in time, the 10000 MonteCarlo iterations require 1600 seconds. I am quite ok with this time for calculation as I am running on my PC and I can leave it running during the night. Now, since I have to repeat the same analysis over 20 or 30 data sets I transformed everything in a .m file. And set it so that I can pass some variables to it and I wanted to run it from command line on each data set. Everything works properly except that it is much much slower. While the notebook does one MC iter per time step in 4.2 milliseconds, the script does it in 25 milliseconds. So it took forever to complete one MC analysis and then I stopped it. Has anyone met the same strange behavior?

I am posting this from home and I haven't here a license for Mathematica but I will try to post a minimal working example from work tomorrow. I am using Mathematica 9.

Regards, Massimiliano

4 Replies

I was wondering if it was related to parallelization or something. I tried the Parallelize command on the table command itself but I received errors.

Hm, strange. With Mathematica 10 and Mathematica 9 on Windows I get about 12.1 sec in all cases.

I'm using a local directory (I called it "C:\Users\arnoudb.WRI\scriptslowdown\") because my Documents directory is a network backed directory, which can lead to slowness itself (you might want to see if you have the same issue, saving locally to a drive is usually faster, especially when you are writing a lot of data (which is not an issue in this case though)).

POSTED BY: Arnoud Buzing

Hi I don't think so. It is mainly numerical stuff. Since I cannot share my test data and my actual procedure I have created a minimal working example that shows the same trend though in a much smaller way.

Here is my example:

SetDirectory["C:\\Users\\username\\Documents\\"];


OutputBackup=$Output;
MessageBackup=$Messages;
debugFile=OpenWrite["Debug.txt",FormatType->OutputForm,TotalWidth -> Infinity];
logFile=OpenWrite["LogFile.txt",FormatType->OutputForm,TotalWidth -> Infinity];
AppendTo[$Output,debugFile];
AppendTo[$Messages,debugFile];


TotalPressure=30 10^5;
TotalPressureError=500 0.15/100 6894.75729; (*0.15% of full scale reading which is 500 psi *)
StaticPressure=2352011;
StaticPressureError=500 0.15/100 6894.75729; (*0.15% of full scale reading which is 500 psi *)
\[Gamma]=1.4;
TotalTemperature=20+273.15;
TotalTemperatureError=1;


WriteString[logFile,
StringJoin[
"MonteCarlo analysis took ",ExportString[
AbsoluteTiming[MCRes=Module[{P0x,px,Machx,T0x,Tx},
Table[
{
P0x=TotalPressure+RandomReal[{-TotalPressureError,TotalPressureError}],
px=StaticPressure+RandomReal[{-StaticPressureError,StaticPressureError}],
T0x=TotalTemperature+RandomReal[{-TotalTemperatureError,TotalTemperatureError}],
Machx=Sqrt[2/(\[Gamma]-1) ((P0x/px)^((\[Gamma]-1)/\[Gamma])-1)],
Tx=T0x/(1+(\[Gamma]-1)/2 Machx^2)},
{iMC,1,1000000}]];][[1]],"CSV"],"s to complete"]];


Close[logFile];
Close[debugFile];

When I run this from a brand new Mathematica 9.0 session it takes 20 seconds to execute. When I run it from command line

 "/cygdrive/c/Program\ Files/Wolfram\ Research/Mathematica/9.0/MathKernel.exe -script MinimalWorkingExample.m"

it takes 30 seconds to complete.

The type of operations performed are similar to the one I have to except that I have also a FindRoot and a loop that I made to converge over some values. Those will take time but my expectations were they ought to take the same time independently from the way you run it.

Does your code have any TimeConstrained or MemoryConstrained expressions in it? [#49655]

POSTED BY: Arnoud Buzing
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract