1
$\begingroup$

Doing something that throws out an error (warning):

ParallelTable[NIntegrate[1/Sin[10 t], {t, 0, W}], {W, 0, 10, 1}]

In parallel somehow limits a possiblity to harvest error messages like I usually do:

myMessageList = {};
Internal`InheritedBlock[{Message, $InMsg = False}, Unprotect[Message];
Message[msg_, vars___] /; ! $InMsg := 
Block[{$InMsg = True}, 
AppendTo[myMessageList, {HoldForm[msg], vars}];
Message[msg, vars]];
ParallelTable[NIntegrate[1/Sin[10 t], {t, 0, W}], {W, 0, 10, 1}]] (*this is the string that is being executed*)
myMessageList

This works for Table just fine. Is there a way to avoid that? What I really need is to discard any output that produces an error.

$\endgroup$
  • $\begingroup$ Somewhat related: mathematica.stackexchange.com/questions/27262/… $\endgroup$ – Michael E2 Mar 25 '18 at 0:44
  • $\begingroup$ @MichaelE2 that tags error messages just fine, but how do I harvest them into some list for example? $\endgroup$ – Vsevolod A. Mar 25 '18 at 0:50
  • $\begingroup$ I don't know to answer the other question. I just thought the two questions were similar enough that linking them might be helpful. Please see if my answer solves your problem. $\endgroup$ – Michael E2 Mar 25 '18 at 1:53
  • $\begingroup$ @MichaelE2 like a charm, thank you! $\endgroup$ – Vsevolod A. Mar 25 '18 at 2:29
2
$\begingroup$

Two changes make it work. Collect the messages on each subkernel and alter Message on each subkernel. The messages can be harvested with ParallelEvaluate.

ParallelEvaluate[myMessageList = {}];
ParallelTable[
 Internal`InheritedBlock[{Message, $InMsg = False},
  Unprotect[Message];
  Message[msg_, vars___] /; ! $InMsg := Block[{$InMsg = True},
    AppendTo[myMessageList, {HoldForm[msg], vars}];
    Message[msg, vars]];
  NIntegrate[1/Sin[10 t], {t, 0, W}]
  ],
 {W, 0, 10, 1}]
ParallelEvaluate[myMessageList]  (* Flatten[%, 1] if desired *)
$\endgroup$

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.