How to output a message in snowfall?
Asked Answered
P

1

8

I am conducting a simulation study using snowfall package on Windows 7.

I like to print out a message for every 10 runs to main R console to monitor the progress, but it fails to do so. ie. nothing is printed

Any help will be much appreciated.

runsim = function(nsim,n,mean,var){
cov = 0
for(i in 1:nsim){
if ( i %% 10==0) 
cat("\n Running simulation",i)
dat = function1(n,mean,var)

cov = ...
}
cov / nsim
}
sfExport("function1","runsim")
sfLibrary(library1)

wrapper = function(n){
runsim(100,n,0.5,0.25)
}

Out<-sfLapply(1:100,wrapper)
Pachalic answered 14/1, 2012 at 5:55 Comment(4)
You will probably need sfCat.Lillie
@Roman: I tried your suggestion, replacing cat vt sfCat. I got the following error message Error in checkForRemoteErrors(val) : 2 nodes produced errors; first error: could not find function "sfCat" Any idea on how to fix it? ThanksPachalic
You can try to load snowfall into the workers using sfLibrary. sfCat is part of snowfall and packages aren't loaded into workers by default.Ferreous
@Paul Thanks for the suggestion. There are no error message this time, but again nothing is printedPachalic
S
10

Check ?sfCat and find the line where it says:

sfCat is a debugging function printing a message on all slaves (which appear in the logfiles).

This tells us that we need to enable logging in the call to sfInit (argument slaveOutfile). Then, each call to sfCat() will dump stuff to the log file you specified. It took me a while to figure that out as well ;-)

Code Example

if (!require("snowfall")) {
    install.packages("snowfall")
    require("snowfall")
}

sfInit(parallel=TRUE, cpus=4, slaveOutfile="test.txt")
sfLibrary("snowfall", character.only=TRUE)

res <- sfClusterApplyLB(1:100, function(x) {
    sfCat(paste("Iteration ", x), sep="\n")
})
sfStop()

Output of ./test.txt

[...]

Calling a snowfall function without calling 'sfInit' first or after sfStop().
'sfInit()' is called now.
snowfall 1.84 initialized: sequential execution, one CPU.

Iteration  4
Type: EXEC 
Iteration  92
Type: EXEC 
Iteration  94
Type: EXEC 
Iteration  96
Type: EXEC 
Iteration  98
Type: EXEC 
Iteration  100
ype: EXEC 
Iteration  29
Type: EXEC 
Iteration  31
Type: EXEC 
Iteration  33
Type: EXEC 
Iteration  35
Type: EXEC 
Iteration  37
Type: EXEC 
Iteration  39
Type: EXEC 
Iteration  41
Type: EXEC 
Iteration  43
Type: EXEC 
Iteration  45
Type: EXEC 
Iteration  47
Type: EXEC 
Iteration  49
Type: EXEC 
Iteration  51
Type: EXEC 
Iteration  53
Type: EXEC 
Iteration  55
Type: EXEC 
Iteration  57
Type: EXEC 
Iteration  59
Type: EXEC 
Iteration  61
Type: EXEC 
Iteration  63
Type: EXEC 
Iteration  65
Type: EXEC 
Iteration  67
Type: EXEC 
Iteration  69
Type: EXEC 
Iteration  71
Type: EXEC 
Iteration  73
Type: EXEC 
Iteration  75
Type: EXEC 
Iteration  77
Type: EXEC 
Iteration  79
Type: EXEC 
Iteration  81
Type: EXEC 
Iteration  83
Type: EXEC 
Iteration  85
Type: EXEC 
Iteration  87
Type: EXEC 
Iteration  89
Type: EXEC 
Iteration  91
Type: EXEC 
Iteration  93
Type: EXEC 
Iteration  95
Type: EXEC 
Iteration  97
Type: EXEC 
Iteration  99
 EXEC 
Iteration  74
Type: EXEC 
Iteration  76
Type: EXEC 
Iteration  78
Type: EXEC 
Iteration  80
Type: EXEC 
Iteration  82
Type: EXEC 
Iteration  84
Type: EXEC 
Iteration  86
Type: EXEC 
Iteration  88
Type: EXEC 
Iteration  90

Own question

Does anyone have a clue how to "control" the specific things that go to the log file? E.g., what would be nice is to include information about which worker finished which job etc.

Sharolynsharon answered 7/3, 2012 at 19:43 Comment(2)
Wht's the difference between snowfall and doSnow?Viscera
I've found the function Sys.getpid() which output I append to sfCat to identify the worker. For instance: sfCat(Sys.getpid(), msg)Gigolo

© 2022 - 2024 — McMap. All rights reserved.