@@ -62,7 +62,7 @@ SUBROUTINE init_parallel_pdaf(dim_ens, screen,schismCount,petCountLocal,concurre
6262! Later revisions - see svn log
6363!
6464! !USES:
65- USE schism_msgp, only: comm,parallel_init,myrank,nproc,parallel_abort
65+ USE schism_msgp, only: comm,parallel_init,myrank,nproc,parallel_abort,nscribes,task_id_schism = >task_id
6666 USE mod_parallel_pdaf, &
6767 ONLY: mype_world, npes_world, mype_model, npes_model, &
6868 COMM_model, mype_filter, npes_filter, COMM_filter, filterpe, &
@@ -98,8 +98,12 @@ SUBROUTINE init_parallel_pdaf(dim_ens, screen,schismCount,petCountLocal,concurre
9898 INTEGER :: mype_couple, npes_couple ! Rank and size in COMM_couple
9999 INTEGER :: pe_index ! Index of PE
100100 INTEGER :: my_color, color_couple ! Variables for communicator-splitting
101+ INTEGER :: world_group
101102 LOGICAL :: iniflag ! Flag whether MPI is initialized
102103 CHARACTER (len= 32 ) :: handle ! handle for command line parser
104+ integer ,allocatable :: group_ranks(:),group_ranks_final(:)
105+ integer :: group_a,group_b,union_group,comm_pdaf,ihalf_group
106+ integer :: send_burf(2 ),ic_comm,mype_world2,npes_world2
103107
104108
105109 ! *** Initialize MPI if not yet initialized ***
@@ -149,10 +153,12 @@ SUBROUTINE init_parallel_pdaf(dim_ens, screen,schismCount,petCountLocal,concurre
149153 ! *** only used to generate model communicators ***
150154 COMM_ensemble = MPI_COMM_WORLD
151155
156+ npes_world = npes_world - n_modeltasks* nscribes ! # of PEs - ncribes cores
152157 npes_ens = npes_world ! # of PEs
153158 mype_ens = mype_world ! local rank
154159
155160
161+
156162 ! *** Store # PEs per ensemble ***
157163 ! *** used for info on PE 0 and for generation ***
158164 ! *** of model communicators on other Pes ***
@@ -175,19 +181,80 @@ SUBROUTINE init_parallel_pdaf(dim_ens, screen,schismCount,petCountLocal,concurre
175181 pe_index = 0
176182 doens1: DO i = 1 , n_modeltasks
177183 DO j = 1 , local_npes_model(i)
178- IF (mype_ens == pe_index) THEN
184+ IF (( mype_ens == pe_index) .and. (task_id_schism == 1 ) ) THEN
179185 task_id = i ! similar to our sequence index (shared among multiple tasks)
180186 EXIT doens1
181187 END IF
182188 pe_index = pe_index + 1
183189 END DO ! j
184190 END DO doens1
191+ local_npes_model= local_npes_model- nscribes ! Reset to correct number for scribes
185192
186193 ! Copy from SCHISM (init'ed under ESMF). May be shared among >1 task
187194 COMM_model= comm
188- ! call mpi_comm_dup(comm,COMM_model,MPIerr)
189195 npes_model= nproc
196+ ! if (task_id_schism==1) mype_model=myrank
190197 mype_model= myrank
198+ ! write(0,'(a,3i8)') "1. task_id_schism,myrank,mype_world=",task_id_schism,myrank,mype_world
199+ ! write(0,'(a,4i8)') "id,nproc,petCountLocal,npes_world = ", mype_model,nproc,petCountLocal, npes_world
200+
201+ ! Group comm for scribeIO
202+ if (schismCount.eq. concurrentCount) then ! Only full-parallel mode
203+ call MPI_COMM_GROUP(MPI_COMM_WORLD, world_group, MPIerr)
204+ allocate (group_ranks(schismCount* petCountLocal* 2 )) ! Local
205+ allocate (group_ranks_final(schismCount* (petCountLocal- nscribes))) ! Local
206+ ihalf_group= schismCount* (petCountLocal- nscribes)/ 2
207+ group_ranks= 0
208+ group_ranks_final= 0
209+ send_burf= (/ mype_ens,task_id_schism/ )
210+ call MPI_ALLGATHER(send_burf, 2 , MPI_INTEGER,group_ranks,2 ,MPI_INTEGER,MPI_COMM_WORLD, MPIerr)
211+ ! if (mype_ens==0) write(*,'(a,80(2i4))') 'group_rank:',group_ranks
212+ ic_comm= 0
213+ do i= 1 ,schismCount* petCountLocal
214+ if (group_ranks(i* 2 ).eq. 1 ) then ! task_id_schism=1
215+ ic_comm= ic_comm+1
216+ group_ranks_final(ic_comm)= group_ranks(i* 2-1 )
217+ end if
218+ end do
219+ ! if (ic_comm.ne.ihalf_group*2) write(*,*) 'ic_comm=', ic_comm,ihalf_group
220+ ! write(*,'(a,80(2i4))') 'group_rank_final:',group_ranks_final
221+
222+ ! Split into 2 groups for later group_union
223+ ! call MPI_GROUP_INCL(world_group, ihalf_group, group_ranks_final(1:ihalf_group), group_a, MPIerr)
224+ ! call MPI_GROUP_INCL(world_group, ihalf_group, group_ranks_final(ihalf_group+1:ihalf_group*2), group_b, MPIerr)
225+ call MPI_GROUP_INCL(world_group, ihalf_group* 2 , group_ranks_final, union_group, MPIerr)
226+
227+ ! Union & create new comm
228+ ! call MPI_GROUP_UNION(group_a, group_b, union_group, MPIerr)
229+
230+ ! Just create new comm with union_group
231+ call MPI_COMM_CREATE(MPI_COMM_WORLD, union_group, comm_pdaf, MPIerr)
232+ ! if (MPIerr.eq.0) write(*,*) 'Success create new comm_pdaf!',mype_world
233+ ! CALL MPI_Comm_Size(comm_pdaf, npes_world2, MPIerr)
234+ ! if (MPIerr.eq.0) write(*,*) 'Success create new npe!',npes_world2,mype_world
235+
236+ ! Free group to avoid memory leak
237+ ! call MPI_GROUP_FREE(group_a, MPIerr)
238+ ! call MPI_GROUP_FREE(group_b, MPIerr)
239+ call MPI_GROUP_FREE(union_group, MPIerr)
240+ call MPI_GROUP_FREE(world_group, MPIerr)
241+
242+ ! Re-rank and overwrite original mype_world
243+ if (comm_pdaf /= MPI_COMM_NULL) then
244+ CALL MPI_Comm_rank(comm_pdaf, mype_world2, MPIerr)
245+ ! if (MPIerr.eq.0) write(*,*) 'Success create new rank!',mype_world,mype_world2
246+ CALL MPI_Comm_Size(comm_pdaf, npes_world2, MPIerr)
247+ ! if (MPIerr.eq.0) write(*,*) 'Success create new rank size!',mype_world,mype_world2,npes_world2
248+ mype_world= mype_world2
249+ CALL MPI_Barrier(comm_pdaf, MPIerr)
250+ else
251+ mype_world=- 1 ! Force to specify
252+ mype_world2=- 1 ! Force to specify
253+ end if
254+
255+ ! set comm_pdaf for scribeIO
256+ call PDAF_set_comm_pdaf(comm_pdaf) ! Set to new comm for all cores
257+ end if
191258
192259! CALL MPI_Comm_split(COMM_ensemble, task_id, mype_ens, &
193260! COMM_model, MPIerr)
@@ -216,13 +283,24 @@ SUBROUTINE init_parallel_pdaf(dim_ens, screen,schismCount,petCountLocal,concurre
216283 ! *** For simplicity equal to COMM_couple (model?) ***
217284 my_color = task_id ! same as model, but only PEs of Task 1 are really used?
218285
219- CALL MPI_Comm_split(MPI_COMM_WORLD, my_color, mype_world, &
286+ if (schismCount.eq. concurrentCount) then ! Only full-parallel mode, scribeIO
287+ if (comm_pdaf /= MPI_COMM_NULL) then ! Only for compute cores
288+ CALL MPI_Comm_split(comm_pdaf, my_color, mype_world2, &
289+ COMM_filter, MPIerr)
290+ CALL MPI_Comm_Size(COMM_filter, npes_filter, MPIerr)
291+ CALL MPI_Comm_Rank(COMM_filter, mype_filter, MPIerr)
292+ ! write(*,'(a,(3i4))') 'npes_filter, mype_filter:',npes_filter, mype_filter,mype_world2
293+ end if
294+ else
295+ CALL MPI_Comm_split(MPI_COMM_WORLD, my_color, mype_world, &
220296 COMM_filter, MPIerr)
221297
222298 ! *** Initialize PE informations ***
223299 ! *** according to coupling communicator ***
224- CALL MPI_Comm_Size(COMM_filter, npes_filter, MPIerr)
225- CALL MPI_Comm_Rank(COMM_filter, mype_filter, MPIerr)
300+ CALL MPI_Comm_Size(COMM_filter, npes_filter, MPIerr)
301+ CALL MPI_Comm_Rank(COMM_filter, mype_filter, MPIerr)
302+
303+ end if
226304
227305
228306 ! *** COMM_COUPLE ***
@@ -232,24 +310,41 @@ SUBROUTINE init_parallel_pdaf(dim_ens, screen,schismCount,petCountLocal,concurre
232310
233311 color_couple = mype_filter + 1 ! shift ranks by 1 (not sure why)
234312
235- CALL MPI_Comm_split(MPI_COMM_WORLD, color_couple, mype_world, &
313+ if (schismCount.eq. concurrentCount) then ! Only full-parallel mode, scribeIO
314+ if (comm_pdaf /= MPI_COMM_NULL) then ! Only for compute cores
315+ CALL MPI_Comm_split(comm_pdaf, color_couple, mype_world2, &
316+ COMM_couple, MPIerr)
317+ CALL MPI_Comm_Size(COMM_couple, npes_couple, MPIerr)
318+ CALL MPI_Comm_Rank(COMM_couple, mype_couple, MPIerr)
319+ end if
320+ else
321+ CALL MPI_Comm_split(MPI_COMM_WORLD, color_couple, mype_world, &
236322 COMM_couple, MPIerr)
237323
238324 ! *** Initialize PE informations ***
239325 ! *** according to coupling communicator ***
240- CALL MPI_Comm_Size(COMM_couple, npes_couple, MPIerr)
241- CALL MPI_Comm_Rank(COMM_couple, mype_couple, MPIerr)
326+ CALL MPI_Comm_Size(COMM_couple, npes_couple, MPIerr)
327+ CALL MPI_Comm_Rank(COMM_couple, mype_couple, MPIerr)
328+ end if
242329
243330 IF (screen > 0 ) THEN
244- CALL MPI_Barrier(MPI_COMM_WORLD, MPIerr)
331+ if (schismCount.eq. concurrentCount) then ! Only full-parallel mode, scribeIO
332+ if (comm_pdaf /= MPI_COMM_NULL) CALL MPI_Barrier(comm_pdaf, MPIerr)
333+ else
334+ CALL MPI_Barrier(MPI_COMM_WORLD, MPIerr)
335+ end if
245336 IF (mype_world == 0 ) THEN
246337 WRITE (* , ' (/18x, a)' ) ' PE configuration:'
247338 WRITE (* , ' (2x, a6, a9, a10, a14, a13, /2x, a5, a9, a7, a7, a7, a7, a7, /2x, a)' ) &
248339 ' world' , ' filter' , ' model' , ' couple' , ' filterPE' , &
249340 ' rank' , ' rank' , ' task' , ' rank' , ' task' , ' rank' , ' T/F' , &
250341 ' ----------------------------------------------------------'
251342 END IF
252- CALL MPI_Barrier(MPI_COMM_WORLD, MPIerr)
343+ if (schismCount.eq. concurrentCount) then ! Only full-parallel mode, scribeIO
344+ if (comm_pdaf /= MPI_COMM_NULL) CALL MPI_Barrier(comm_pdaf, MPIerr)
345+ else
346+ CALL MPI_Barrier(MPI_COMM_WORLD, MPIerr)
347+ end if
253348 IF (task_id == 1 ) THEN
254349 WRITE (* , ' (2x, i4, 4x, i4, 4x, i3, 4x, i3, 4x, i3, 4x, i3, 5x, l3)' ) &
255350 mype_world, mype_filter, task_id, mype_model, color_couple, &
@@ -259,12 +354,21 @@ SUBROUTINE init_parallel_pdaf(dim_ens, screen,schismCount,petCountLocal,concurre
259354 WRITE (* ,' (2x, i4, 12x, i3, 4x, i3, 4x, i3, 4x, i3, 5x, l3)' ) &
260355 mype_world, task_id, mype_model, color_couple, mype_couple, filterpe
261356 END IF
262- CALL MPI_Barrier(MPI_COMM_WORLD, MPIerr)
357+ if (schismCount.eq. concurrentCount) then ! Only full-parallel mode, scribeIO
358+ if (comm_pdaf /= MPI_COMM_NULL) CALL MPI_Barrier(comm_pdaf, MPIerr)
359+ else
360+ CALL MPI_Barrier(MPI_COMM_WORLD, MPIerr)
361+ end if
263362
264363 IF (mype_world == 0 ) WRITE (* , ' (/a)' ) ' '
265364
266365 END IF
267366
367+ deallocate (group_ranks) ! scribeIO
368+ deallocate (group_ranks_final) ! scribeIO
369+
370+ ! write(*,*) 'In init_parallel_pdaf chk, myrank=',mype_world,mype_world2
371+ CALL MPI_Barrier(MPI_COMM_WORLD, MPIerr)
268372
269373! ******************************************************************************
270374! *** Initialize model equivalents to COMM_model, npes_model, and mype_model ***
0 commit comments