twocmp.con.F90 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2. ! Math and Computer Science Division, Argonne National Laboratory !
  3. !-----------------------------------------------------------------------
  4. ! CVS twocmp.con.F90,v 1.4 2006-07-25 22:31:34 jacob Exp
  5. ! CVS MCT_2_8_0
  6. !BOP -------------------------------------------------------------------
  7. !
  8. ! !ROUTINE: twocomponent.concurrent
  9. !
  10. ! !DESCRIPTION: Provide a simple example of using MCT to connect two
  11. ! components executing concurrently in a single executable.
  12. !
  13. !
  14. ! !INTERFACE:
  15. !
  16. program twocon
  17. !
  18. ! !USES:
  19. !
  20. !--- Use only the things needed from MCT
  21. use m_MCTWorld,only: MCTWorld_init => init
  22. use m_GlobalSegMap,only: GlobalSegMap
  23. use m_GlobalSegMap,only: MCT_GSMap_init => init
  24. use m_GlobalSegMap,only: MCT_GSMap_lsize => lsize
  25. use m_AttrVect,only : AttrVect
  26. use m_AttrVect,only : MCT_AtrVt_init => init
  27. use m_AttrVect,only : MCT_AtrVt_zero => zero
  28. use m_AttrVect,only : MCT_AtrVt_lsize => lsize
  29. use m_AttrVect,only : MCT_AtrVt_indexRA => indexRA
  30. use m_AttrVect,only : MCT_AtrVt_importRA => importRAttr
  31. use m_Router,only: Router
  32. use m_Router,only: MCT_Router_init => init
  33. use m_Transfer,only : MCT_Send => send
  34. use m_Transfer,only : MCT_Recv => recv
  35. implicit none
  36. include 'mpif.h'
  37. !-----------------------------------------------------------------------
  38. ! Local variables
  39. integer,parameter :: npoints = 24 ! number of grid points
  40. integer ier,nprocs
  41. integer color,myrank,mycomm
  42. !-----------------------------------------------------------------------
  43. ! The Main program.
  44. ! We are implementing a single-executable, concurrent-execution system.
  45. ! This small main program carves up MPI_COMM_WORLD and then starts
  46. ! each component on its own processor set.
  47. call MPI_init(ier)
  48. call mpi_comm_size(MPI_COMM_WORLD, nprocs,ier)
  49. call mpi_comm_rank(MPI_COMM_WORLD, myrank,ier)
  50. if((nprocs .gt. 14).or.(nprocs .lt. 3)) then
  51. write(6,*)"The small problem size in this example &
  52. &requires between 3 and 14 processors."
  53. write(6,*)"nprocs =",nprocs
  54. stop
  55. endif
  56. ! Force the model1 to run on the first 2 processors
  57. color =1
  58. if (myrank .lt. 2) then
  59. color = 0
  60. endif
  61. ! Split MPI_COMM_WORLD into a communicator for each model
  62. call mpi_comm_split(MPI_COMM_WORLD,color,0,mycomm,ier)
  63. ! Start up the the models, pass in the communicators
  64. if(color .eq. 0) then
  65. call model1(mycomm)
  66. else
  67. call model2(mycomm)
  68. endif
  69. ! Models are finished.
  70. call mpi_finalize(ier)
  71. contains
  72. !-----------------------------------------------------------------------
  73. !-----------------------------------------------------------------------
  74. ! !ROUTINE:
  75. subroutine model1(comm1) ! the first model
  76. implicit none
  77. integer :: comm1,mysize,ier,asize,myproc
  78. integer :: fieldindx,avsize,i
  79. integer,dimension(1) :: start,length
  80. real,pointer :: testarray(:)
  81. type(GlobalSegMap) :: GSmap
  82. type(AttrVect) :: av1
  83. type(Router) :: Rout
  84. !---------------------------
  85. ! find local rank and size
  86. call mpi_comm_size(comm1,mysize,ier)
  87. call mpi_comm_rank(comm1,myproc,ier)
  88. write(6,*)"model1 size",mysize
  89. ! initialize ThisMCTWorld
  90. call MCTWorld_init(2,MPI_COMM_WORLD,comm1,1)
  91. ! set up a grid and decomposition
  92. asize = npoints/mysize
  93. start(1)= (myproc*asize) +1
  94. length(1)=asize
  95. ! describe decomposition with MCT GSmap type
  96. call MCT_GSMap_init(GSMap,start,length,0,comm1,1)
  97. write(6,*)"model 1 GSMap ngseg",myproc,GSMap%ngseg,start(1)
  98. ! Initialize an Attribute Vector
  99. call MCT_AtrVt_init(av1,rList="field1:field2",lsize=MCT_GSMap_lsize(GSMap,comm1))
  100. avsize = MCT_AtrVt_lsize(av1)
  101. write(6,*)"model 1 av size", avsize
  102. ! Fill Av with some data
  103. ! fill first attribute the direct way
  104. fieldindx = MCT_AtrVt_indexRA(av1,"field1")
  105. do i=1,avsize
  106. av1%rAttr(fieldindx,i) = float(i)
  107. enddo
  108. ! fill second attribute using Av import function
  109. allocate(testarray(avsize))
  110. do i=1,avsize
  111. testarray(i)= cos((float(i)/npoints) * 3.14)
  112. enddo
  113. call MCT_AtrVt_importRA(av1,"field2",testarray)
  114. ! initialize a Router
  115. call MCT_Router_init(2,GSMap,comm1,Rout)
  116. ! print out Av data
  117. do i=1,asize
  118. write(6,*) "model 1 data", myproc,i,av1%rAttr(1,i),av1%rAttr(2,i)
  119. enddo
  120. ! send the data
  121. call MCT_Send(av1,Rout)
  122. end subroutine model1
  123. !-----------------------------------------------------------------------
  124. !-----------------------------------------------------------------------
  125. ! !ROUTINE:
  126. subroutine model2(comm2)
  127. implicit none
  128. integer :: comm2,mysize,ier,asize,myproc
  129. integer :: i
  130. integer,dimension(1) :: start,length
  131. type(GlobalSegMap) :: GSmap
  132. type(AttrVect) :: av1
  133. type(Router) :: Rout
  134. !---------------------------
  135. ! find local rank and size
  136. call mpi_comm_size(comm2,mysize,ier)
  137. call mpi_comm_rank(comm2,myproc,ier)
  138. write(6,*)"model2 size",mysize
  139. ! initialize ThisMCTWorld
  140. call MCTWorld_init(2,MPI_COMM_WORLD,comm2,2)
  141. ! set up a grid and decomposition
  142. asize = npoints/mysize
  143. start(1)= (myproc*asize) +1
  144. length(1)=asize
  145. ! describe decomposition with MCT GSmap type
  146. call MCT_GSMap_init(GSMap,start,length,0,comm2,2)
  147. write(6,*)"model 2 GSMap ngseg",myproc,GSMap%ngseg,start(1)
  148. ! Initialize an Attribute Vector
  149. call MCT_AtrVt_init(av1,rList="field1:field2",lsize=MCT_GSMap_lsize(GSMap,comm2))
  150. write(6,*)"model 2 av size", MCT_AtrVt_lsize(av1)
  151. ! initialize Av to be zero everywhere
  152. call MCT_AtrVt_zero(av1)
  153. ! initialize a Router
  154. call MCT_Router_init(1,GSMap,comm2,Rout)
  155. ! print out Av data before Recv
  156. do i=1,asize
  157. write(6,*) "model 2 data", myproc,i,av1%rAttr(1,i),av1%rAttr(2,i)
  158. enddo
  159. ! Recv the data
  160. call MCT_Recv(av1,Rout)
  161. ! print out Av data after Recv.
  162. do i=1,asize
  163. write(6,*) "model 2 data after", myproc,i,av1%rAttr(1,i),av1%rAttr(2,i)
  164. enddo
  165. end subroutine model2
  166. end