Monday, May 7, 2012

Parallel Programming with Fortran 101 - continued 2

My second OpenMP program:

This is an application I built based on my day-to-day work.  We were often required to run a model with different input parameters. This process if done in serial, cost a processor a very long processing time.  In the past, I often use batch file to do this.  Now all our computers are at least two cores, I would like to run these models in parallel. 

My first attempt failed but the second successed with help from the OpenMP forum members. Failing is a frustrating and time consuming process of learning. This link contains my issues and solution.
http://www.openmp.org/forum/viewtopic.php?f=3&t=1418

The success program is copied below. Anyone may slightly alter it for your Monte Carlo Type of simulations.

program ompHelloWorld
use omp_lib
integer NTHREADS, TID, I, TID1

!$OMP PARALLEL PRIVATE(TID,TID1), SHARED(NTHREADS)
!$OMP MASTER  
    TID1 = OMP_GET_THREAD_NUM()
    PRINT *, 'Master computer id is ', TID1
    NTHREADS = OMP_GET_NUM_THREADS()
    PRINT *, 'Number of threads = ', NTHREADS
!$OMP END MASTER
    TID = OMP_GET_THREAD_NUM()
    PRINT *, 'Hello World from thread = ', TID   
!    CALL POOH(TID)   
    CALL RUNMODEL(TID)
    pause

!$OMP END PARALLEL

end program ompHelloWorld

!Run a model
!Note that under home directory there has to be a cdandrun.bat
!contains the following two lines
!cd %1
!MODHMS1 >lst

SUBROUTINE RUNMODEL(ID)
   INTEGER ID
   CHARACTER*200 CPATH, cpath2  
   write(*,*) 'Run model using thread#',ID
  
!  Change current path  
   WRITE(CPATH,100) ID+1
100 FORMAT(I2)  
   CPATH='cdandrun.bat gvtest'//ADJUSTL(CPATH)
   WRITE(*,*) TRIM(CPATH)
   call getcwd(cpath2)
   WRITE(*,*) 'The current path is ', TRIM(CPATH2)
  CALL SYSTEM(cpath)

END SUBROUTINE RUNMODEL

No comments: