25.06.2013 Views

Il Linguaggio Fortran 90/95

Il Linguaggio Fortran 90/95

Il Linguaggio Fortran 90/95

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

146 Array<br />

! Sezione dichiarativa<br />

IMPLICIT NONE<br />

INTEGER , PARAMETER :: long=SELECTED_INT_KIND(9)<br />

INTEGER(KIND=long) :: j<br />

INTEGER :: i<br />

REAL(KIND(1.D0)), DIMENSION(5) :: x=1.0<br />

REAL :: inizio, fine, somma<br />

! Sezione esecutiva<br />

somma=0.<br />

CALL CPU_TIME(inizio)<br />

DO j=1_long,1000000_long<br />

DO i=1,5<br />

somma=somma+x(i)<br />

END DO<br />

somma=0.<br />

END DO<br />

CALL CPU_TIME(fine)<br />

WRITE(*,’(1X,A17,G14.7)’) "DO loop: ", fine-inizio<br />

somma=0.<br />

CALL CPU_TIME(inizio)<br />

DO j=1_long,1000000_long<br />

somma=x(1)+x(2)+x(3)+x(4)+x(5)<br />

somma=0.<br />

END DO<br />

CALL CPU_TIME(fine)<br />

WRITE(*,’(1X,A17,G14.7)’) "Loop unrolling: ", fine-inizio<br />

STOP<br />

END PROGRAM test_loop_unrolling<br />

compilato con il compilatore Digital Visual <strong>Fortran</strong> 6.0 e mandato in esecuzione su un PC con<br />

processore Intel Pentium III, produce il seguente output:<br />

DO loop: 0.1301872<br />

Loop unrolling: 0.2002880E-01<br />

che evidenzia i benefici associati al meccanismo di loop unrolling. Naturalmente l’operazione<br />

di srotolamento degli array può essere estesa anche ai casi in cui le dimensioni dell’array non<br />

consentono di esplicitarne tutti gli elementi. Allo scopo basta eseguire l’unrolling un certo<br />

numero di volte su un numero ben preciso di elementi. Ad esempio, dato il costrutto:<br />

n = 500<br />

somma = 0.<br />

DO i = 1,n<br />

somma = somma+x(i)<br />

END DO

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!