# How to catch Print and Monitor message dialog?

## Context

I am trying to parallelize a function which is verbose, and which as a consequence Mathematica does not seem to treat in parallel when doing something like ParallelTable[fun[i],{i,16}], which does not run faster than Table[fun[i],{i,16}] even though the evaluation of fun[i] should be independent.

I think I have pinned down the problem to not being able to prevent subKernels to talk to the masterKernel when the function fun wants to Print and/or Monitor things.

## Question

Let's say I have a function

 fun[x_] := (Print[x]; x)


I would like to be able to do something like

 Table[fun[i] // Quiet, {i, 6}]


and get

{1,2,3,4,5,6}

Rather than

1
2
3
4
5
6


{1,2,3,4,5,6}

I other words is there a SuperQuiet function which catches all outputs from it argument?

I hope this makes sense?

## Update

What about the situation where the dialog proceeds via Monitor ?

Say if I have a function

 fun[x_] := Monitor[x,x];


Can I also prevent the Call to Monitor to operate so that I can efficiently do

 ParallelTable[f[x],{x,10^4}];


You can turn off printing, at least, by setting $Output to {}, which can be done temporarily using Block. I don't know if this will address your issue with parallel performance, because I've never seen anything like that myself. An example: ClearAll[fun]; fun[x_] := (Print[x]; x); Block[{$Output = {}},
Table[fun[i], {i, 6}]]
(* {1, 2, 3, 4, 5, 6} *)


Alternatively, since you want to just turn off the output to subkernels, you could use try

ParallelEvaluate[\$Output = {}]


and just blow away the output for each one entirely. You could also have each one write to its own log file (which is usually what I do).

EDIT to add: In order to get rid of the Monitor calls, you can again use Block, this time to locally redefine Monitor to just return its first argument:

ClearAll[fun];
fun[x_] := Monitor[x, x];
ParallelTable[Block[{Monitor = # &}, fun[x]], {x, 10}]; // AbsoluteTiming
(* {0.02898, Null} *)


For comparison:

ParallelTable[x, {x, 10^4}]; // AbsoluteTiming
(* {0.02979, Null} *)


The trick of using Block to locally disable or redefine various system functions is one of my favorites, though it doesn't always work as you expect. It's still much less dangerous and crude than using Unprotect to change definitions.

• Wait, are you able to have Monitor work at all in subkernels? It needs a front end. It might help if you could show some more of your real code.... – Pillsy Mar 3 '17 at 13:43