Although I'd generally suggest using ParallelTable
instead, I can imagine a scenario where you would like to use ParallelDo
with a function that is really lengthy and could potentially hang or crash. Then you would want to save the results of each successful parallel computation to avoid losing it in case of a crash.
But to do that, it's safest to create a separate file for each result, because we don't usually have control over the order and timing with which ParallelDo
produces results.
Since the loop can always be parameterized by integer indices, we can use these indices to label the individual result files, too. Then these files can later be combined into a single file when all the results have been written out:
ParallelDo[
Export["out-" <>
ToString@NumberForm[i, 2, NumberPadding -> {"0", " "}] <> "-" <>
ToString@NumberForm[j, 2, NumberPadding -> {"0", " "}],
ToString[Row[{i, ",", j, ",", integral1[i, j]}]] <> "\n",
"Text"], {i, 10}, {j, 1, 10}]
Here, I used NumberForm
with 2
digits and leading 0
as padding, to produce file names in which the integers i
and j
appear with constant length. You would have to change the 2
to something larger if the integers range over more digits.
I've inserted a ToString
before the integral1
function because I don't know what that function is.
When the loop is done, you'll have files ranging from out-001-001
to out-010-010
. Under Unix, these can be combined easily by issuing the command cat out* > results
which creates a file called results
where you have everything in the desired format.
ParallelDo
and notParallelTable
, can't you simply transmit the data back to the main kernel using a shared variable? $\endgroup$