Question: How does Threads[Seq] work?

Hello,

I am having some trouble with the "Seq" command of the "Threads" package. This is a parallel implementation of the usual "seq" command.

I namely defined a (very long) procedure "f" which defines a (very complicated) function $f: R^2 -> R$, taking as arguments an $x$ and a $y$ and returning $f(x,y)$. I want to plot its graph in 3D, which I try to do by making a 3D data structure consisting of points making up the $x$ range, $y$ range, and the evaluations $f(x,y)$. I then want to plot it with the "SurfacePlot" command from the "Statistics" package.

If I define my data structure as

 data := [seq([seq([i/10, j/10, f(i/10, j/10)], i = 1 .. 10)], j = 1 .. 10)]

the SurfacePlot(data) works. However, it is very slow because it runs in serial and the function evaluation "f(x,y)" takes a long time to compute.

I therefore wanted to compute the data in parallel. For this, I use

 data := [Seq([Seq([i/10, j/10, f(i/10, j/10)], i = 1 .. 10)], j = 1 .. 10)]

Sometimes this works, but often it does not and I get the following error after a number of datapoints have been computed (i.e. after evaluations of f have been made).

Error, (in simplify/float) invalid arguments for Float constructor

This is the first time I use parallel computing, so I am not familiar with potential problems that could trigger errors and don't understand very well how parallel computing works. Does "Seq" only compute the different data points [x,y,f(x,y)] in parallel, or does it compute stuff within the procedure "f" also in parallel when evaluating "f(x,y)" at some fixed arguments "(x,y)"? Because another procedure is defined within "f" and it is called multiple times within one "f" call, so in that case it could be a problem with lexical scope. Any suggestion to help me understand and correct my error would be greatly appreciated.

Please Wait...