Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #14216
| From | Peter Pein <petsie@dordos.net> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Running an external program inside a Do loop |
| Date | 2013-04-17 06:29 +0000 |
| Message-ID | <kklfgu$7op$1@smc.vnet.net> (permalink) |
| References | <kk88r9$c1b$1@smc.vnet.net> |
| Organization | Time-Warner Telecom |
Am 12.04.2013 08:16, schrieb Luiz Melo:
> Hi
> I have to run an external program inside a Do loop, as in the example
> below. The .exe program takes the parameter i as input from the
> Input.dat file, and generates a two-column numerical .dat file as
> output. Next, the Output.dat file has to be imported and manipulated.
> Since the external program takes some time to run and the Do loop runs
> much faster, the external program is terminating abnormaly. I would
> like to know how to tell Mathematica to wait until the external
> program finishes its evaluation before to execute the Import command.
> Thank you.
> Luiz Melo.
>
> Do [
> Export["Input.dat", i];
> Run["SomeMath.exe"];
> t = Import["Output.dat"];
> tpos[i] = Select[t, #[[1]] > 0 &];
> tneg[i] = Select[t, #[[1]] < 0 &], {i, 100}];
>
Hi Luiz,
I can not reproduce this behaviour. I wrote a simple program
'SomeMath.c" whichs sleeps three seconds before it writes the output to
disk:
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int number;
FILE *ifile = fopen("input.dat", "r");
FILE *ofile = fopen("output.dat", "w");
fscanf(ifile, "%d", &number);
fclose(ifile);
sleep(3);
fprintf(ofile, "%d\n", number*number);
fclose(ofile);
return(0);
}
and with
SetDirectory[NotebookDirectory[]];
Do[
Export["input.dat", i];
timerr = AbsoluteTiming@Run["SomeMath.exe"];
{t} = Import["output.dat"][[1]];
Print[Grid[{{"Time", "err-code", "input", "output"},
Join[timerr, {i, t}]}]];
, {i, 2}
];
DeleteFile[# <> "put.dat" & /@ {"in", "out"}];
ResetDirectory[];
I get:
Time err-code input output
3.095177 0 1 1
Time err-code input output
3.092177 0 2 4
you could try ReadList as in:
SetDirectory[NotebookDirectory[]];
Do[
Export["input.dat",i];
Print[First@AbsoluteTiming[
t=First@ReadList["!SomeMath.exe & type output.dat",
{Number}]
]];
Print[i,", ",t];,
{i,2}];
DeleteFile[(#1<>"put.dat"&)/@{"in","out"}];
ResetDirectory[];
which prints
3.093177
1, {1}
3.082176
2, {4}
and has got at least the advantage that these annoying console windows
do not appear. This calls SomeMath.exe and types the output to stdout as
_one_ command. Maybe this helps in your case.
Good luck,
Peter
Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Running an external program inside a Do loop Luiz Melo <lmelo@ufsj.edu.br> - 2013-04-12 06:16 +0000
Re: Running an external program inside a Do loop Peter Pein <petsie@dordos.net> - 2013-04-17 06:29 +0000
Re: Running an external program inside a Do loop debguy <johnandsara2@cox.net> - 2013-04-19 05:14 +0000
csiph-web