首页 > 解决方案 > Optimization of conditional Fortran loop

问题描述

I would like to optimize for speed the following block of code:

DO i=1, dim1
  DO j=1, dim2
    DO k=1, dim3
      IF (A(k,j,i)>0) &
        B(k,j,i) = exp(C(k))/A(k,j,i)
    ENDDO
  ENDDO
ENDDO

Very importantly, A is an INTEGER and B and C are COMPLEX!

There are two issues: 1) How to replace this by a BLAS/LAPACK call? The issue is the condition. 2) Calculation of the exp is slow. How to speed that up?

标签: optimizationfortranlapackblas

解决方案


idim[1-3]我用各种排列进行了几次测试,[40,40,1000]发现使用临时数组作为指数并保持原始循环排序比提供的其他答案快 2 倍或更多。您的里程可能因编译器等而异。

d=exp(c)
DO i=1, dim1
  DO j=1, dim2
    DO k=1, dim3
      IF (A(k,j,i)>0) &
        B(k,j,i) = d(k)/A(k,j,i)
    ENDDO
  ENDDO
ENDDO

推荐阅读