Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in map_rect when using 2 threads #2708

Closed
syclik opened this issue Dec 4, 2018 · 4 comments
Closed

Bug in map_rect when using 2 threads #2708

syclik opened this issue Dec 4, 2018 · 4 comments
Assignees
Labels
Milestone

Comments

@syclik
Copy link
Member

syclik commented Dec 4, 2018

Summary:

When running a Stan program with map_rect with threading and number of threads is 2, the result is incorrect.

Description:

For STAN_NUM_THREADS = 2, map_rect() returns a vector with the wrong number of elements: it's one short of what it should be.

As far as I can tell, if the number of threads is any other number, it's fine.

Reproducible Steps:

Set up Stan with threading. Run this program:

functions {
  vector mapped_function(vector beta, vector theta, real[] x, int [] y) {
    return [0]';
  }
}
data {
}
transformed data {
  int shards = 3;
  vector[0] theta[shards];
  real x_r[shards, 0];
  int x_i[shards, 0];
}
parameters {
  vector[1] beta;
}
transformed parameters {
  vector[shards] lps = map_rect(mapped_function, beta, theta, x_r, x_i);
}
model {
  beta ~ normal(0, 1);
  target += lps;
}

Current Output:

With STAN_NUM_THREADS = 2, this fails.

$ STAN_NUM_THREADS=2 ./parallel sample
num_threads = 2
method = sample (Default)
  sample
    num_samples = 1000 (Default)
    num_warmup = 1000 (Default)
    save_warmup = 0 (Default)
    thin = 1 (Default)
    adapt
      engaged = 1 (Default)
      gamma = 0.050000000000000003 (Default)
      delta = 0.80000000000000004 (Default)
      kappa = 0.75 (Default)
      t0 = 10 (Default)
      init_buffer = 75 (Default)
      term_buffer = 50 (Default)
      window = 25 (Default)
    algorithm = hmc (Default)
      hmc
        engine = nuts (Default)
          nuts
            max_depth = 10 (Default)
        metric = diag_e (Default)
        metric_file =  (Default)
        stepsize = 1 (Default)
        stepsize_jitter = 0 (Default)
id = 0 (Default)
data
  file =  (Default)
init = 2 (Default)
random
  seed = 2187044319
output
  file = output.csv (Default)
  diagnostic_file =  (Default)
  refresh = 100 (Default)

Unrecoverable error evaluating the log probability at the initial value.
Exception: assign: Rows of left-hand-side (21) and rows of right-hand-side (20) must match in size  (in '../withinchain_surv_stan/parallel.stan' at line 18)

Exception: assign: Rows of left-hand-side (21) and rows of right-hand-side (20) must match in size  (in '../withinchain_surv_stan/parallel.stan' at line 18)

Expected Output:

It should run as usual.

Additional Information:

This will need to be fixed in the Math repo, but should not be closed until that happens.

Current Version:

v2.18.0

@ahartikainen
Copy link

ahartikainen commented Dec 6, 2018

Will this fail if shards = 5 and STAN_NUM_THREADS = 3 or any

shards = N*k - 1
STAN_NUM_THREADS = N

where k = 2, 3, ...

@ahartikainen
Copy link

Ok, I tested and with

shards = 5
STAN_NUM_THREADS = 3

shards = 9
STAN_NUM_THREADS = 5

both fail

latter error msg:

RuntimeError: Exception: assign: Rows of left-hand-side (9) and rows of right-hand-side (5) must match in size  (in 'unknown file name' at line 20)

After changing

const int num_big_threads
    = (num_jobs - num_jobs_per_thread) % (num_threads - 1);

to

const int num_big_threads = num_jobs % num_threads;

sampling works.

@wds15
Copy link
Contributor

wds15 commented Dec 8, 2018

BTW, I have checked the MPI code and there this problem does not exist as far as I can tell. So I tested this example and had no problem and I also checked the code which I think is fine.

@mitzimorris
Copy link
Member

closed by fix to math - stan-dev/math#1089

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants