Stan Connection problem Error in unserialize(socklist[[n]]) : error reading from connection

I am have been modelling in stan and everything was running perfectly fine. I had run my models with
N=40, N=10 and N=3 and it worked fine every time.
Yesterday i ran my script and i’ve started to get this error

Error in unserialize(socklist[[n]]) : error reading from connection
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
‘C:/rtools40/usr/mingw_/bin/g++’ not found
Error in serialize(data, node$con, xdr = FALSE) :
error writing to connection

I’m using parallel computing, using cl ← makeCluster(n_cluster, outfile=“output.txt”)
output.txt does not show the error
my log file shows that each worker is basically quitting when it starts sampling
i have cleanly uninstalled r,rstudio and rtools, deleting all the appdata and libraries remaining
i cant find or dont seem to have a ‘makeVars.win’ file so I don’t think its the -mtune and -march issue that I’ve seen on other topics
I am pasting my code but I doubt it has anything to do with it since it was working perfectly yesterday
Any help would be greatly appreciated :)
i hope my topic creation isnt abberant this is my first post

if (run) {
  
  duration <- Sys.time()
  cl <- makeCluster(n_cluster, outfile="output.txt")
  registerDoParallel(cl)
  
  writeLines(c(""), "log.txt")
  dir.create(res_dir)
  rm(i)
  out <- foreach(i = 1:nrow(it)) %dopar% {
    
    w <- it$TrainingDay[i]
    f <- it$Fold[i]
    
    library(tidyverse)
    library(rstan)
    rstan_options(auto_write = TRUE) # Save compiled model
    options(mc.cores = parallel::detectCores()) # Parallel computing
    source("functions.R")
    
    sink("log.txt", append = TRUE)
    cat(paste0("Starting training at week ", w, ", fold ", f, " \n"))
    
    ###########
    
    w <- it$TrainingDay[i]
    f <- it$Fold[i]
    
  
    
    dt_wf <- data.frame(Patient = rep(N,times=1,each=80),
                        Day = timearray,
                        S = sevdata,
                        S_train = sevdata)
    
    idx_pred <- which(!is.na(dt_wf$S) & (dt_wf$Day > w) & (dt_wf$Patient %in% N[folds == f]))
    
    dt_wf$S_train[idx_pred] <- NA
    
    
    data_stan <- format_data(dt_wf, N, idx_pred)
    
    perf <- data.frame(Patient = dt_wf$Patient[idx_pred],
                       TrainingDay = w,
                       TestingDay = dt_wf$Day[idx_pred],
                       Fold = f,
                       S = dt_wf$S[idx_pred])
    
    ## Fit
    fit <- sampling(compiled_model,
                    data = data_stan,
                    pars = param,
                    iter = n_it,
                    chains = n_chains,
                    seed = seed,
                    control = list(adapt_delta = case_when(model_name %in% c("SSM", "SSMX") ~ 0.99,
                                                           TRUE ~ 0.9)))
    
    ## Prepare ouput
    lpd <- extract(fit, pars = "lpd")[[1]]
    pred <- extract(fit, pars = "S_pred")[[1]]
    smp <- sapply(1:ncol(pred), function(i) {list(pred[, i])})
    
    perf <- perf %>%
      mutate(Mean_pred = apply(pred, 2, mean), # cf. point prediction (mean)
             lpd = apply(lpd, 2, function(x) {log(mean(exp(x)))}), # marginalise lpd
             CRPS = scoringRules::crps_sample(perf[["S"]], t(pred)),
             Samples = smp)
    
    ## Save (intermediate results)
    saveRDS(perf, file = file.path(res_dir, paste0("val_", i, ".rds")))
    
    cat(paste0("Ending training at day ", w, ", fold ", f, " \n"))
  }
  stopCluster(cl)
  (duration = Sys.time() - duration)
  
  # Recombine results
  files <- list.files(res_dir)
  if (length(files) < nrow(it)) {
    warning("Number of files (", length(files), ") less than the number of iterations (", max_it + 1, "). Some runs may have failed.")
  }
  res <- do.call(rbind,
                 lapply(files,
                        function(f) {
                          readRDS(file.path(res_dir, f))
                        }))
  saveRDS(res, file = res_file)
} else {
  res <- readRDS(res_file)
} 

1 Like

This is almost certainly a problem, as g++ likely resides in C:/rtools40/usr/mingw64/bin/g++

I would guess that you updated your rstan/Rtools version? Please revisit the steps at Configuring C Toolchain for Windows · stan-dev/rstan Wiki · GitHub (which explicitly mention this issue) and feel free to ask for clarifications if you are unable to follow the steps…

Best of luck setting everything up!