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

avoid some index arithmetic #236

Open
richfitz opened this issue Aug 13, 2021 · 1 comment
Open

avoid some index arithmetic #236

richfitz opened this issue Aug 13, 2021 · 1 comment

Comments

@richfitz
Copy link
Member

p_G_D[, , ] <- if (as.integer(step) >= n_p_G_D_steps)
  p_G_D_step[n_p_G_D_steps, i] * rel_p_G_D[i, j, k] else
    p_G_D_step[step + 1, i] * rel_p_G_D[i, j, k]

We currently generate code that does a lot of work to create the index into p_G_D and from rel_p_G_D

    for (int i = 1; i <= shared->dim_p_G_D_1; ++i) {
      for (int j = 1; j <= shared->dim_p_G_D_2; ++j) {
        for (int k = 1; k <= shared->dim_p_G_D_3; ++k) {
          internal.p_G_D[i - 1 + shared->dim_p_G_D_1 * (j - 1) + shared->dim_p_G_D_12 * (k - 1)] = (static_cast<int>(step) >= shared->n_p_G_D_steps ? shared->p_G_D_step[shared->dim_p_G_D_step_1 * (i - 1) + shared->n_p_G_D_steps - 1] * shared->rel_p_G_D[shared->dim_rel_p_G_D_12 * (k - 1) + shared->dim_rel_p_G_D_1 * (j - 1) + i - 1] : shared->p_G_D_step[shared->dim_p_G_D_step_1 * (i - 1) + step + 1 - 1] * shared->rel_p_G_D[shared->dim_rel_p_G_D_12 * (k - 1) + shared->dim_rel_p_G_D_1 * (j - 1) + i - 1]);
        }
      }
    }

This might be simpifiable to:

    for (int i = 1, idx = 0; i <= shared->dim_p_G_D_1; ++i) {
      for (int j = 1; j <= shared->dim_p_G_D_2; ++j) {
        for (int k = 1; k <= shared->dim_p_G_D_3; ++k, ++idx) {
          internal.p_G_D[idx] = (static_cast<int>(step) >= shared->n_p_G_D_steps ? shared->p_G_D_step[shared->dim_p_G_D_step_1 * (i - 1) + shared->n_p_G_D_steps - 1] * shared->rel_p_G_D[idx] : shared->p_G_D_step[shared->dim_p_G_D_step_1 * (i - 1) + step + 1 - 1] * shared->rel_p_G_D[idx]);
        }
      }
    }
@richfitz
Copy link
Member Author

richfitz commented Aug 18, 2021

Looking at this, I think that loops run the wrong way around to support this, we should be doing k, j, i not i, j, k so that i moves fastest - this might be causing some access issues at the moment actually.

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

No branches or pull requests

1 participant