twocmp.seqNB.F90 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2. ! Math and Computer Science Division, Argonne National Laboratory !
  3. !-----------------------------------------------------------------------
  4. ! CVS twocmp.seqNB.F90,v 1.4 2004-06-24 21:07:01 eong Exp
  5. ! CVS MCT_2_8_0
  6. !BOP -------------------------------------------------------------------
  7. !
  8. ! !ROUTINE: twocmp.seqNB
  9. !
  10. ! !DESCRIPTION: Provide a simple example of using MCT to connect to
  11. ! components executing sequentially in a single executable using
  12. ! the non-blocking communications to transfer data.
  13. !
  14. !
  15. ! !INTERFACE:
  16. !
  17. program twocmpseqNB
  18. !
  19. ! !USES:
  20. !
  21. !--- Use only the things needed from MCT
  22. use m_MCTWorld,only: MCTWorld_init => init
  23. use m_GlobalSegMap,only: GlobalSegMap
  24. use m_GlobalSegMap,only: MCT_GSMap_init => init
  25. use m_GlobalSegMap,only: MCT_GSMap_lsize => lsize
  26. use m_GlobalSegMapComms,only: MCT_GSMap_recv => recv
  27. use m_GlobalSegMapComms,only: MCT_GSMap_isend => isend
  28. use m_GlobalSegMapComms,only: MCT_GSMap_bcast => bcast
  29. use m_AttrVect,only : AttrVect
  30. use m_AttrVect,only : MCT_AtrVt_init => init
  31. use m_AttrVect,only : MCT_AtrVt_zero => zero
  32. use m_AttrVect,only : MCT_AtrVt_lsize => lsize
  33. use m_AttrVect,only : MCT_AtrVt_indexRA => indexRA
  34. use m_AttrVect,only : MCT_AtrVt_importRA => importRAttr
  35. use m_Router,only: Router
  36. use m_Router,only: MCT_Router_init => init
  37. use m_Transfer,only : MCT_ISend => isend
  38. use m_Transfer,only : MCT_Recv => recv
  39. implicit none
  40. include 'mpif.h'
  41. integer,parameter :: npoints = 24 ! total number of grid points
  42. integer ier,nprocs,i
  43. integer color,myrank,comm1,comm2
  44. integer,dimension(:),pointer :: myids
  45. integer,dimension(:),pointer :: req1,req2
  46. !-----------------------------------------------------------------------
  47. ! The Main program.
  48. ! We are implementing a single-executable, seqeuntial-execution system.
  49. ! This small main program sets up MCTWorld, calls each "init" method
  50. ! and then calls each component in turn.
  51. type(GlobalSegMap) :: GSMap1,GSMap2
  52. type(AttrVect) :: Av1,Av2
  53. call MPI_init(ier)
  54. call mpi_comm_size(MPI_COMM_WORLD, nprocs,ier)
  55. call mpi_comm_rank(MPI_COMM_WORLD, myrank,ier)
  56. ! Duplicate MPI_COMM_WORLD into a communicator for each model
  57. call mpi_comm_dup(MPI_COMM_WORLD,comm1,ier)
  58. call mpi_comm_dup(MPI_COMM_WORLD,comm2,ier)
  59. allocate(myids(2))
  60. myids(1)=1
  61. myids(2)=2
  62. ! Initialize MCT world
  63. call MCTWorld_init(2,MPI_COMM_WORLD,comm1,myids=myids)
  64. ! Initialize the models, pass in the communicators
  65. call model1init(comm1,req1,GSMap1,Av1)
  66. call model2init(comm2,req2,GSMap2,Av2)
  67. !-----------------end of initialization phase ------
  68. ! Run the models, pass in the communicators
  69. do i=1,5
  70. write(6,*) " "
  71. write(6,*) "Step ",i
  72. call model1(comm1,GSMap1,Av1)
  73. call model2(comm2,GSMap2,Av2)
  74. enddo
  75. ! Models are finished.
  76. call mpi_finalize(ier)
  77. contains
  78. !-----------------------------------------------------------------------
  79. !-----------------------------------------------------------------------
  80. ! !ROUTINE:
  81. subroutine model1init(comm1,req1,GSmap,av1) ! init the first model
  82. implicit none
  83. integer :: comm1,mysize,ier,asize,myproc
  84. integer :: fieldindx,avsize,i
  85. integer,dimension(1) :: start,length
  86. real,pointer :: testarray(:)
  87. integer,pointer :: req1(:)
  88. type(GlobalSegMap) :: GSmap
  89. type(AttrVect) :: av1
  90. !---------------------------
  91. ! find local rank and size
  92. call mpi_comm_size(comm1,mysize,ier)
  93. call mpi_comm_rank(comm1,myproc,ier)
  94. write(6,*)myproc,"model1 size",mysize
  95. ! set up a grid and decomposition
  96. asize = npoints/mysize
  97. start(1)= (myproc*asize) +1
  98. length(1)=asize
  99. ! describe decomposition with MCT GSmap type
  100. call MCT_GSMap_init(GSMap,start,length,0,comm1,1)
  101. write(6,*)myproc,"model 1 GSMap ngseg",GSMap%ngseg,start(1)
  102. if(myproc .eq. 0) call MCT_GSMap_Isend(GSMap,2,100,req1)
  103. ! Initialize an Attribute Vector
  104. call MCT_AtrVt_init(av1,rList="field1:field2",lsize=MCT_GSMap_lsize(GSMap,comm1))
  105. write(6,*)myproc,"model1 got an aV"
  106. avsize = MCT_AtrVt_lsize(av1)
  107. write(6,*)myproc,"model 1 av size", avsize
  108. end subroutine model1init
  109. !-----------------------------------------------------------------------
  110. !-----------------------------------------------------------------------
  111. subroutine model1(comm1,GSmap,av1) ! run the first model
  112. implicit none
  113. integer :: comm1,mysize,ier,asize,myproc
  114. integer :: fieldindx,avsize,i
  115. integer,dimension(1) :: start,length
  116. real,pointer :: testarray(:)
  117. type(GlobalSegMap) :: GSmap,GSmap2
  118. type(AttrVect) :: av1
  119. type(Router),save :: Rout
  120. logical,save :: firsttime=.FALSE.
  121. call mpi_comm_rank(comm1,myproc,ier)
  122. if(.not.firsttime) then
  123. ! get other GSMap
  124. if(myproc .eq. 0) call MCT_GSMap_recv(GSmap2,2,110)
  125. call MCT_GSMap_bcast(GSmap2,0,comm1)
  126. ! initialize a router
  127. call MCT_Router_init(GSMap,GSmap2,comm1,Rout)
  128. endif
  129. firsttime=.TRUE.
  130. avsize = MCT_AtrVt_lsize(av1)
  131. ! Fill Av with some data
  132. ! fill first attribute the direct way
  133. fieldindx = MCT_AtrVt_indexRA(av1,"field1")
  134. do i=1,avsize
  135. av1%rAttr(fieldindx,i) = float(i +20*myproc)
  136. enddo
  137. ! fill second attribute using Av import function
  138. allocate(testarray(avsize))
  139. do i=1,avsize
  140. testarray(i)= cos((float(i+ 20*myproc)/npoints) * 3.14)
  141. enddo
  142. call MCT_AtrVt_importRA(av1,"field2",testarray)
  143. ! print out Av data
  144. do i=1,avsize
  145. write(6,*)myproc, "model 1 data", i,av1%rAttr(1,i),av1%rAttr(2,i)
  146. enddo
  147. ! send the data
  148. call MCT_ISend(av1,Rout)
  149. end subroutine model1
  150. !-----------------------------------------------------------------------
  151. !-----------------------------------------------------------------------
  152. ! !ROUTINE:
  153. subroutine model2init(comm2,req2,GSmap,av1) ! init model 2
  154. implicit none
  155. integer :: comm2,mysize,ier,asize,myproc
  156. integer :: i
  157. integer,dimension(1) :: start,length
  158. type(GlobalSegMap) :: GSmap
  159. type(AttrVect) :: av1
  160. integer,pointer :: req2(:)
  161. !---------------------------
  162. ! find local rank and size
  163. call mpi_comm_size(comm2,mysize,ier)
  164. call mpi_comm_rank(comm2,myproc,ier)
  165. write(6,*)myproc,"model2 size",mysize
  166. ! set up a grid and decomposition
  167. asize = npoints/mysize
  168. start(1)= (myproc*asize) +1
  169. length(1)=asize
  170. ! describe decomposition with MCT GSmap type
  171. call MCT_GSMap_init(GSMap,start,length,0,comm2,2)
  172. write(6,*)myproc, "model 2 GSMap ngseg",GSMap%ngseg,start(1)
  173. if(myproc .eq. 0) call MCT_GSMap_Isend(GSMap,1,110,req2)
  174. ! Initialize an Attribute Vector
  175. call MCT_AtrVt_init(av1,rList="field1:field2",lsize=MCT_GSMap_lsize(GSMap,comm2))
  176. write(6,*)myproc,"model2 got an aV"
  177. write(6,*)myproc, "model 2 av size", MCT_AtrVt_lsize(av1)
  178. end subroutine model2init
  179. !-----------------------------------------------------------------------
  180. !-----------------------------------------------------------------------
  181. ! !ROUTINE:
  182. subroutine model2(comm2,GSmap,av1)
  183. implicit none
  184. integer :: comm2,mysize,ier,avsize,myproc
  185. integer :: i
  186. integer,dimension(1) :: start,length
  187. type(GlobalSegMap) :: GSmap,GSmap2
  188. type(AttrVect) :: av1
  189. type(Router),save :: Rout
  190. logical,save :: firsttime=.FALSE.
  191. !---------------------------
  192. ! initialize Av to be zero everywhere
  193. call MCT_AtrVt_zero(av1)
  194. call mpi_comm_rank(comm2,myproc,ier)
  195. if(.not.firsttime) then
  196. ! receive other GSMap
  197. if(myproc .eq. 0) call MCT_GSMap_recv(GSmap2,1,100)
  198. call MCT_GSMap_bcast(GSmap2,0,comm2)
  199. ! initialize a Router
  200. call MCT_Router_init(GSMap,GSmap2,comm2,Rout)
  201. endif
  202. firsttime=.TRUE.
  203. avsize = MCT_AtrVt_lsize(av1)
  204. ! print out Av data before Recv
  205. do i=1,avsize
  206. write(6,*) myproc,"model 2 data", i,av1%rAttr(1,i),av1%rAttr(2,i)
  207. enddo
  208. ! Recv the data
  209. call MCT_Recv(av1,Rout)
  210. ! print out Av data after Recv.
  211. do i=1,avsize
  212. write(6,*) myproc,"model 2 data after", i,av1%rAttr(1,i),av1%rAttr(2,i)
  213. enddo
  214. end subroutine model2
  215. end