Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

equivalence of nested do and nested forall

Status
Not open for further replies.

tservas

Programmer
Mar 29, 2005
6
DE
hello everybody,

... I am new to fortran and I would like to implement a function/subroutine wich performes the direct matrix product ...

!
! The following nested do loops are used to
! calculate the direct product of two square
! matrices. This calculation is based on the following
! formula: C(i,j) = A(n,m)*B(i_prime, j_prime), with
! (n-1)*num_rows_B + 1 <= i <= n*num_rows_B = dim_2,
! (m-1)*num_columns_B + 1 <= i <= m*num_columns_B = dim_2,
! i_prime = i - (n-1)*num_rows_B + 1,
! j_prime = j - (m-1)*num_columns_B + 1,
! The resulting matrix C is build up block by block, according to the
! definition of the direct matrix product, i.e.
! the (num_rows_B x num_columns_B) block A(1,1)*B gets in the upper left corner, etc.
! The two outer do loops ( iterating n, m) thus go from one block to the next one.
! The two inner do loops ( iterating i, j) thus operate only within one block
!
DO n=1,dim_1, 1 ! dim_1 = num_rows_A = num_columns_A
DO m=1, dim_1, 1 ! dim_1 = num_rows_A = num_columns_A
row_min = (n-1)*dim_2 + 1
row_max = n*dim_2
column_min = (m-1)*dim_2 + 1
column_max = m*dim_2
DO i=row_min, row_max, 1 ! dim_1 = num_rows_B = num_columns_B
DO j=column_min, column_max, 1 ! dim_1 = num_rows_B = num_columns_B
i_prime = i - row_min + 1
j_prime = j - column_min +1
res_matrix%array(i,j) = sq_matrix_1%array(n, m) * sq_matrix_2%array(i_prime, j_prime)
ENDDO
ENDDO
ENDDO
ENDDO


!
! This forall construct is not equivalent to the do loops above ... WHY ???????
!
! FORALL ( n=1:dim_1, m=1:dim_1 )
! row_min = (n-1)*dim_2 + 1
! row_max = n*dim_2
! column_min = (m-1)*dim_2 + 1
! column_max = m*dim_2
! FORALL ( i=(n-1)*dim_2:n*dim_2, j=(m-1)*dim_2:m*dim_2 )
! i_prime = i - row_min + 1
! j_prime = j - column_min +1
! res_matrix%array(i,j) = sq_matrix_1%array(n, m) * sq_matrix_2%array(i_prime, j_prime)
! END FORALL
! END FORALL

... it seems that this statments are not equivalent ... I dont know why ...

thanks in advance

tservas
 
What language does forall come from and what is it meant to do?
 
... forall is a fortran 95 statement ...

FORALL (i=1:n, j=1:m, a(i,j) /= 0.0 )
a(i,j) = 1.0 / a(i,j)
END FORALL

... is equivalent to ...

DO i = 1, n
DO j = 1,m
IF ( a(i,j) /= 0) THEN
a(i,j) = 1.0 / a(i,j)
ENDIF
ENDDO
ENDDO

... the order of executing the statements contained in the do loop is well defined -> strict ordering ... BUT the order of execution of the FORALL construct is selected by the processor -> parallel computing is much faster with the forall construct ...

... I think I figured out why the nested forall is not equivalent to the nested do loop ... it's because any statement within the forall construct is executed independently of all the others ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top