Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

When using a function that includes Print statements, one can suppress the output by

Print; Unprotect[Print]; Print = Null &

This doesn't work when the function is evaluated in

ParallelTable[function[i],{i,1,10}]

Is there a way to do that? Thank you!

share|improve this question
1  
Have you tried executing the redefinition of Print in all the kernels you are using for evaluation ? –  image_doctor May 2 '12 at 10:37
add comment

3 Answers

up vote 5 down vote accepted

You can use dynamic scoping, i.e. the Block construct, to temporarily "forget" or redefine Print.

ParallelTable[
Block[{Print},
Print; Unprotect[Print]; Print = Null &
function[x],
{x,0,10}
]

This works with ParallelTable for me.

share|improve this answer
    
works! thank you. –  user13655 May 2 '12 at 10:47
    
What about Block[{Print = Null&}, ...]? That doesn't even need an Unprotect. –  celtschk May 2 '12 at 11:04
2  
I would find it unusual if this worked, because the definition of Print is not synchronized across kernels, and indeed it does not work here. I tried Block[{}, Print; Unprotect[Print]; Print = Null &; ParallelDo[Print[i], {i, 1, 10}]] which is completely analogous with your example, yet it does print the output. Furthermore, in your example the modification of Print is not temporary. –  Szabolcs May 2 '12 at 11:30
1  
@noe Try in a fresh kernel. If you use the exact same code that you posted, the modification cannot be temporary, because you didn't pass Print to Block in the first argument. Then there's the question of synchronization between subkernels. –  Szabolcs May 2 '12 at 11:47
1  
The only way I can see this working is if you put this code inside ParallelTable, but even then you will change the definition of Print permanently for the parallel kernels. Try for example running ParallelDo[Print[i], {i,10}] afterwards. A safer option would be to do something like ParallelTable[Block[{Print}, Print = Null &;...], ...]. –  Heike May 2 '12 at 12:31
show 3 more comments

Use

ParallelDo[Block[{Print = Null&}, Print[{i, $KernelID}]], {i,1,10}]

You can also make it into a function:

SetAttributes[ParallelDoSilent, HoldAll]
ParallelDoSilent[expr_, iter_] :=
  ParallelDo[Block[{Print = Null&}, expr], iter]

ParallelDoSilent[Print[{i, $KernelID}], {i,1,10}]
share|improve this answer
1  
+1, yes, this is the correct and general solution. This is good reading on Block (for the OP): stackoverflow.com/questions/6661393/… With is not going to act inside functions, and permanently redefining Print is error-prone and inconvenient. –  Szabolcs May 2 '12 at 13:31
    
@Szabolcs Thanks for pointing out some of the subtle properties of Block –  image_doctor May 3 '12 at 12:56
add comment

This seemed to work for me.

ParallelEvaluate[Unprotect@Print];
ParallelEvaluate[Print = Null &];
ParallelDo[Print[{i, $KernelID}], {i, 1, 10}]

This also seemed to work:

ParallelDo[With[{Print = Null &}, Print[{i, $KernelID}]], {i, 1, 10}]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.