3
$\begingroup$

Consider the following code

gammaList = Table[gamma, {gamma, 1, 10^8, 1}];

Monitor[Table[{gamma}, {gamma, gammaList}], 
 Row[{ProgressIndicator[gamma, {gammaList[[1]], gammaList[[-1]]}], 
   gamma}, " "]]

As you can see, there is no progressbar displaying and I don't understand why. For me everything is correct here

How to fix in the most simple way my problem ?

$\endgroup$
  • 1
    $\begingroup$ Table[{gamma}, {gamma, gammaList}] got auto-compiled so it's not being evaluated on the main loop (that'd be brutally slow anyway--keep as much off the main loop as possible). This means Monitor can't monitor it since it's happening squarely at the C/SIMD level. Try cutting 10^8 down to 10^2 and replacing {gamma} with RandomReal[{}, {1000, 1000}]; {gamma}. That's still a compilable expression but slow enough that you can see the indicator churn. $\endgroup$ – b3m2a1 Oct 28 '19 at 16:18
  • $\begingroup$ @b3m2a1 so just to understand: as my table is "simple" (not complicated own made function that I evaluate), the function is auto-compiled and then not possible to monitor it. If I had a different function to put in my table, then no auto-compiled version would exist and monitoring would be possible. Is that it ? Is there still a way to follow the process of the Table ? $\endgroup$ – StarBucK Oct 28 '19 at 16:21
  • 1
    $\begingroup$ Pretty much. Also because you have more than some threshold of elements (250 I think but it's in SystemOptions). If your call were non-compilable (say using FactorInteger), yes, you'd be able to follow it, but you wouldn't want Monitor to be updating too often so it'd make sense to run it on some smaller Table with a coarser step-size (say by evaluating 1000 list elements in a chunk). $\endgroup$ – b3m2a1 Oct 28 '19 at 16:23
  • $\begingroup$ @b3m2a1 in conclusion, for an "auto compilable" expression that still takes time to be evaluated, is there a way to follow its progress ? $\endgroup$ – StarBucK Oct 28 '19 at 16:24
  • $\begingroup$ In my answer I show how to chunk a call so that you can watch it. $\endgroup$ – b3m2a1 Oct 28 '19 at 16:34
1
$\begingroup$

To take some comments and put them into an answer, the reason this happens is auto-compilation. It's been extensively discussed on the site, but basically Mathematica will try to compile any call into Table that's large enough and with a simple enough function directly down to C before evaluating.

This means it's impervious to inspection by things like Monitor since it's not happening in the main evaluator loop. Consider this:

gammaList = Table[gamma, {gamma, 1, 10^2, 1}];
bigList = RandomReal[{}, {2000, 1000}];
Monitor[
  Table[
   bigList + gamma,
   {gamma, gammaList}], 
  Row[{ProgressIndicator[gamma, {gammaList[[1]], gammaList[[-1]]}], gamma}, 
   " "]
  ];

My old, slow computer can watch that churn. Now let's try bumping up the number of elements in gammaList and bumping down the bigList size:

gammaList = Table[gamma, {gamma, 1, 10^3, 1}];
bigList = RandomReal[{}, {1000, 1000}];

Now I just see this:

ProgressIndicator[gamma,{1,1000}]\[ThinSpace]gamma

Since gamma isn't in the main evaluator it can't be watched. A way around this is to use two calls into Table:

gammaList = Table[gamma, {gamma, 1, 10^3, 1}];
bigList = RandomReal[{}, {1000, 1000}];
chunkSize = 251;
numChunks = Ceiling[Length[gammaList]/chunkSize];
Monitor[
  Table[
   Table[
    bigList + gamma,
    {gamma, gammaList[[(chunk - 1)*chunkSize + 1 ;; UpTo[chunkSize*chunk]]]}
    ],
   {chunk, numChunks}
   ],
  Row[{ProgressIndicator[chunk, {1, numChunks}], chunk}, " "]
  ];

My computer complains vociferously about being asked to evaluate that, but you can watch it go.

|improve this answer|||||
$\endgroup$
0
$\begingroup$

A simple way to keep track of table iterators, I usually use, is:

Dynamic[ProgressIndicator[gamma/gammaMax]]

or

Dynamic[gamma]

Let us define the largest gamma value to be a bit smaller than 10^8 for the sake of example:

gammaMax=10^5;

Then, if you would like to actually see the increments being updated dynamically in the progress bar, you have to slow down the routine by, e.g., a Pause command:

gammaList = Table[Pause[0.00001]; gamma, {gamma, 1, gammaMax, 1}];

If you do not include a Pause, the Table is constructed so quickly, that the Dynamic routine has no chance to pick up on every individual step. Alternatively, if your actual Table components involve more complicated steps that naturally take a longer time to evaluate, then the Pause is not needed and you will be able to see increments dynamically as they come in.

As mentioned by b3m2a1, if the function in each element of Table is too simple, Mathematica may compile the table and make the iterator not accessible to the main window. In such a case the routine again is likely to run so fast that monitoring the iterator may be pointless. In any case where the Table construction involves more complicated input that is not auto-compiled, the above Dynamic will work as desired.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Thank you for your answer. I am not sure how to make use of this Dynamic. Could you give a working example of what you suggest I'm a little bit confused (there is no display in your code). $\endgroup$ – StarBucK Oct 28 '19 at 16:31
  • 1
    $\begingroup$ @StarBucK Quite literally, place the Dynamic in a separate box and evaluate this box first. While gammaMax or gamma have no values, this will return the input. Once you start evaluating the Table in a later box, the previous output of Dynamic is then going to change dynamically. Make sure that you have 'Evaluation->Dynamic Updating Enabled' checked in the settings. Does this help? $\endgroup$ – Kagaratsch Oct 28 '19 at 16:35
  • 1
    $\begingroup$ Monitor is essentially doing just this. It could be mostly re-written as myMonitor[a_, b_]:=(PrintTemporary[Dynamic[b]];a). $\endgroup$ – b3m2a1 Oct 28 '19 at 16:55

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.