Skip to content

Implementation of matching_variables and remaining_columns_to_read is inefficient and can cause hangs #735

@sbfnk

Description

@sbfnk

Describe the bug
The draws() function becomes prohibitively slow when used with many variables, e.g. large arrays. This is due to looping over all variables for matching in matching_variables and remaining_columns_to_read from read_cmdstan_csv.

To Reproduce
Example:

library("cmdstanr")

model_code <- '
parameters {
  array[100, 1000] real x;
}

model {
  for (i in 1:100) {
    for (j in 1:1000) {
      x[i, j] ~ std_normal();
    }
  }
}
'

stan_file <- tempfile(fileext = ".stan")
writeLines(model_code, stan_file)

model <- cmdstan_model(stan_file)

sim <- model$sample(chains = 1, iter_sampling = 1, fixed_param = TRUE)

dr <- R.utils::withTimeout(sim$draws(), timeout = 120)

running into a timeout in my instance.

Expected behavior
Ideally: improved run times.

CmdStanR version number
0.5.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions