用Fortran omp并行计算迭代次数来建立索引吗?

时间:2019-02-10 10:57:01

标签: fortran openmp

我想并行填充矩阵,但在弄清楚如何使用OpenMP跟踪迭代次数时遇到麻烦。

这是我当前的子程序:

subroutine predictions(N, N_samples, idx_new_a, idx_new_b, idx_new_c, K, &
                         pred_idx_a, pred_idx_b, pred_idx_c, X, meanX, sdX, &
                         a, b, c, beta, mu_a, sigma_a ,  sigma_b, sigma_c, &
                         y_pred_draws, nthreads) bind(C, name="predictions_")
  integer, intent(in)                                     :: N, N_samples, &
                                                             idx_new_a, &
                                                             idx_new_b, &
                                                             idx_new_c, &
                                                             K, nthreads
  integer, dimension(N), intent(in)                       :: pred_idx_a, &
                                                             pred_idx_b, &
                                                             pred_idx_c
  double precision, dimension(N,K), intent(in)            :: X
  double precision, dimension(K), intent(in)              :: meanX, sdX
  double precision, dimension(N_samples,idx_new_a-1), intent(in)      :: a
  double precision, dimension(N_samples,idx_new_b-1), intent(in)      :: b
  double precision, dimension(N_samples,idx_new_c-1), intent(in)      :: c
  double precision, dimension(N_samples,K), intent(in)    :: beta
  double precision, dimension(N_samples), intent(in)      :: mu_a, sigma_a , &
                                                             sigma_b, sigma_c
  integer, dimension(N_samples*N,4), intent(out)          :: y_pred_draws
  integer                                                 :: n_rows, idx, &
                                                             sample, iter
    iter = 0

    !$ call omp_set_num_threads(nthreads)
    !$omp parallel do
    do idx=1,N
       do sample=1,N_samples
              iter = iter + 1                 ! I KNOW THIS IS WRONG BUT I'M NOT SURE HOW TO FIX IT
          !WRITE(*,*)  'Iteration ', iter
          y_pred_draws(iter, 1) = idx
          y_pred_draws(iter, 2) = sample
          CALL pred_iter_idx(N, N_samples, idx_new_a, idx_new_b, idx_new_c, K, &
                             idx, sample,pred_idx_a, pred_idx_b, pred_idx_c, &
                             X, meanX, sdX,a, b, c, beta, mu_a, sigma_a ,  &
                             sigma_b, sigma_c, y_pred_draws(iter, 3), &
                             y_pred_draws(iter, 4))
       end do
    end do
    !$omp end parallel do



end subroutine predictions

我能想到的唯一解决方案是在OpenMP循环外部创建一个矩阵,该矩阵具有3列:N,N_samples和iter。然后,我将使用该矩阵进行OpenMP循环。有更有效的方法吗?

0 个答案:

没有答案