rstan icon indicating copy to clipboard operation
rstan copied to clipboard

Failure to dump large array with "stan_rdump"

Open Augustpan opened this issue 6 years ago • 0 comments

Summary:

Failure to allocate memory when dumping large array with function stan_rdump.

Description:

I tried to dump results extracted from a stanfit object into file using stan_rdump function. When I do so, I encountered an error:

Error in gsub(addnlpat, "\1\n", str) : 'Calloc' could not allocate memory (18446744071562067968 of 1 bytes)

I thought the problem is that the array is too large to dump (77112000 elements, 588.3Mb). Hope someone could rewrite the stan_rdump function to work with large objects.

e.g. something like this:

rdump <- function (list, file = "", envir=parent.frame())
{
  file <- file(file, "a")
  for (v in list) {
    vv <- get(v, envir)
    vvdim <- dim(vv)
    cat(v, " <- \n", file = file, sep = "")
    if (length(vv) == 0) {
      str <- paste0("structure(integer(0), ")
    }
    else {
      str <- paste0("structure(c(", paste(as.vector(vv),
                                          collapse = ", "), "),")
    }
    cat(str, "\n", ".Dim = c(", paste(vvdim, collapse = ", "), "))\n",
      file = file,
      sep = "")
  }
}

RStan Version:

2.19.2

R Version:

The version of R you are running (e.g., from R.version.string) R version 3.6.1 (2019-07-05)

Operating System:

macOS 10.15.3

Augustpan avatar Feb 12 '20 11:02 Augustpan