2

I'd like to know whether the cpv function within the trotter package works with %dopar%? I'm getting the following error:

task 1 failed - "object of type 'S4' is not subsettable"

Here's a small example:

library(doParallel)
library(trotter)

registerDoParallel(cores = 2)

x <- letters

combos <- cpv(2, 1:4)
print(combos)
num_combos <- length(combos)

results_list <- foreach(combo_num=1:num_combos) %dopar% { # many iterations
  y <- x[combos[combo_num]]
  # time consuming stuff follows that involves using y
}

Replacing %dopar% with %do% (or simply using a for loop) and it works fine.

kit
  • 1,166
  • 5
  • 16
  • 23
  • Hi, from the author of trotter (who has been very lazy in maintaining the R version of this library!). I copied-and-pasted your code and it worked as expected. Might your error come from something else you have done? Try restarting the R session... – Richard Ambler Nov 17 '18 at 04:55
  • I think the difference is that the OP is using windows, and you're not. – F. Privé Nov 17 '18 at 07:35
  • The problem here is that apparently you can't use subsetting on this kind of object without loading the package first. Maybe @RichardAmbler could fixed this, or maybe it's impossible depending on the kind of object system that's used. – F. Privé Nov 17 '18 at 07:42
  • @F.Privé Updating the R package has been added to my list of things to do! :) There shouldn't be an issue with subsetting, though, because the combinations are not actually stored anywhere; rather the cpv instance is just a mapping of integers to combinations that behaves *as if* it were a list of combinations. As long as the interpreter has a constructed cpv instance it should be able to access any combination "stored" without needing to explicitly partition the combinations for each core. – Richard Ambler Nov 17 '18 at 08:04

1 Answers1

1

Depending on the cluster type one needs to explicitly specify the used packages via the .packages argument. The following should work:

library(doParallel)
library(trotter)
cl <- makePSOCKcluster(2)
registerDoParallel(cl=cl)

x <- letters
combos <- cpv(2, 1:4)
num_combos <- length(combos)

rl <- foreach(combo_num=1:num_combos, .packages="trotter") %dopar% {
  x[combos[combo_num]]
}
Nairolf
  • 2,418
  • 20
  • 34