source: palm/trunk/SOURCE/pmc_handle_communicator.f90 @ 1808

Last change on this file since 1808 was 1808, checked in by raasch, 8 years ago

preprocessor directives using machine dependent flags (lc, ibm, etc.) mostly removed from the code

  • Property svn:keywords set to Id
File size: 16.7 KB
Line 
1 MODULE PMC_handle_communicator
2
3!--------------------------------------------------------------------------------!
4! This file is part of PALM.
5!
6! PALM is free software: you can redistribute it and/or modify it under the terms
7! of the GNU General Public License as published by the Free Software Foundation,
8! either version 3 of the License, or (at your option) any later version.
9!
10! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
11! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13!
14! You should have received a copy of the GNU General Public License along with
15! PALM. If not, see <http://www.gnu.org/licenses/>.
16!
17! Copyright 1997-2015 Leibniz Universitaet Hannover
18!--------------------------------------------------------------------------------!
19!
20! Current revisions:
21! ------------------
22! MPI module used by default on all machines
23!
24! Former revisions:
25! -----------------
26! $Id: pmc_handle_communicator.f90 1808 2016-04-05 19:44:00Z raasch $
27!
28! 1797 2016-03-21 16:50:28Z raasch
29! introduction of different datatransfer modes,
30! export of comm_world_nesting
31!
32! 1791 2016-03-11 10:41:25Z raasch
33! m_nrofcpl renamed m_ncpl,
34! pmc_get_local_model_info renamed pmc_get_model_info, some keywords also
35! renamed and some added,
36! debug write-statements commented out
37!
38! 1786 2016-03-08 05:49:27Z raasch
39! Bugfix: nesting_mode is broadcast now
40!
41! 1779 2016-03-03 08:01:28Z raasch
42! only the total number of PEs is given in the nestpar-NAMELIST,
43! additional comments included
44!
45! 1764 2016-02-28 12:45:19Z raasch
46! pmc_layout type: comm_cpl and comm_parent removed, character "name" moved at
47! the beginning of the variable list,
48! domain layout is read with new NAMELIST nestpar from standard file PARIN,
49! MPI-datatype REAL8 replaced by REAL, kind=8 replaced by wp,
50! variable domain_layouts instead of m_couplers introduced for this NAMELIST,
51! general format changed to PALM style
52!
53! 1762 2016-02-25 12:31:13Z hellstea
54! Initial revision by K. Ketelsen
55!
56! Description:
57! ------------
58! Handle MPI communicator in PALM model coupler
59!------------------------------------------------------------------------------!
60
61#if defined( __parallel )
62    USE kinds
63
64#if defined( __mpifh )
65    INCLUDE "mpif.h"
66#else
67    USE MPI
68#endif
69
70   USE pmc_general,                                                            &
71       ONLY: pmc_status_ok, pmc_status_error, pmc_max_modell
72
73   IMPLICIT NONE
74
75   TYPE pmc_layout
76
77      CHARACTER(len=32) ::  name
78
79      INTEGER  ::  id
80      INTEGER  ::  parent_id
81      INTEGER  ::  npe_total
82
83      REAL(wp) ::  lower_left_x
84      REAL(wp) ::  lower_left_y
85
86   END TYPE pmc_layout
87
88   PUBLIC  pmc_status_ok, pmc_status_error
89
90   INTEGER, PARAMETER, PUBLIC ::  pmc_error_npes          = 1  ! illegal number of PEs
91   INTEGER, PARAMETER, PUBLIC ::  pmc_namelist_error      = 2  ! error(s) in nestpar namelist
92   INTEGER, PARAMETER, PUBLIC ::  pmc_no_namelist_found   = 3  ! No couple layout file found
93
94   ! Coupler Setup
95
96   INTEGER                                    :: m_world_comm !global nesting communicator
97   INTEGER                                    :: m_my_CPL_id  !Coupler id of this model
98   INTEGER                                    :: m_Parent_id  !Coupler id of parent of this model
99   INTEGER                                    :: m_ncpl       !Number of Couplers in layout file
100
101   TYPE(PMC_layout),DIMENSION(PMC_MAX_MODELL) :: m_couplers   !Information of all couplers
102
103   ! MPI settings
104
105   INTEGER,PUBLIC                    :: m_model_comm          !Communicator of this model
106   INTEGER,PUBLIC                    :: m_to_server_comm      !Communicator to the server
107   INTEGER,DIMENSION(PMC_MAX_MODELL) :: m_to_client_comm      !Communicator to the client(s)
108   INTEGER,PUBLIC                    :: m_world_rank
109   INTEGER                           :: m_world_npes
110   INTEGER,PUBLIC                    :: m_model_rank
111   INTEGER,PUBLIC                    :: m_model_npes
112   INTEGER                           :: m_server_remote_size  !Number of Server PE's
113
114   PUBLIC m_to_client_comm
115
116   !Indicates this PE is server for Cleint NR
117
118   INTEGER,DIMENSION(:),POINTER,PUBLIC :: PMC_Server_for_Client
119
120   INTERFACE pmc_is_rootmodel
121      MODULE PROCEDURE pmc_is_rootmodel
122   END INTERFACE pmc_is_rootmodel
123
124   INTERFACE pmc_get_model_info
125      MODULE PROCEDURE pmc_get_model_info
126   END INTERFACE pmc_get_model_info
127
128   PUBLIC pmc_get_model_info, pmc_init_model, pmc_is_rootmodel
129
130 CONTAINS
131
132   SUBROUTINE pmc_init_model( comm, nesting_datatransfer_mode, nesting_mode,   &
133                              pmc_status )
134
135      USE control_parameters,                                                  &
136          ONLY:  message_string
137
138      USE pegrid,                                                              &
139          ONLY:  myid
140
141      IMPLICIT NONE
142
143      CHARACTER(LEN=7), INTENT(OUT) ::  nesting_mode
144      CHARACTER(LEN=7), INTENT(OUT) ::  nesting_datatransfer_mode
145
146      INTEGER, INTENT(OUT)                ::  comm
147      INTEGER, INTENT(OUT)                ::  pmc_status
148
149      INTEGER                             ::  i, ierr, istat
150      INTEGER,DIMENSION(pmc_max_modell+1) ::  start_pe
151      INTEGER                             ::  m_my_cpl_rank
152      INTEGER                             ::  tag, clientcount
153      INTEGER,DIMENSION(pmc_max_modell)   ::  activeserver  ! I am active server for this client ID
154
155      pmc_status   = pmc_status_ok
156      comm         = -1
157      m_world_comm = MPI_COMM_WORLD
158      m_my_cpl_id  = -1
159      clientcount  =  0
160      activeserver = -1
161      start_pe(:)  =  0
162
163      CALL  MPI_COMM_RANK( MPI_COMM_WORLD, m_world_rank, istat )
164      CALL  MPI_COMM_SIZE( MPI_COMM_WORLD, m_world_npes, istat )
165!
166!--   Only PE 0 of root model reads
167      IF ( m_world_rank == 0 )  THEN
168
169         CALL read_coupling_layout( nesting_datatransfer_mode, nesting_mode,   &
170                                    pmc_status )
171
172         IF ( pmc_status /= pmc_no_namelist_found  .AND.                       &
173              pmc_status /= pmc_namelist_error )                               &
174         THEN
175!
176!--         Calculate start PE of every model
177            start_pe(1) = 0
178            DO  i = 2, m_ncpl+1
179               start_pe(i) = start_pe(i-1) + m_couplers(i-1)%npe_total
180            ENDDO
181
182!
183!--         The number of cores provided with the run must be the same as the
184!--         total sum of cores required by all nest domains
185            IF ( start_pe(m_ncpl+1) /= m_world_npes )  THEN
186               WRITE ( message_string, '(A,I6,A,I6,A)' )                       &
187                               'nesting-setup requires more MPI procs (',      &
188                               start_pe(m_ncpl+1), ') than provided (',        &
189                               m_world_npes,')'
190               CALL message( 'pmc_init_model', 'PA0229', 3, 2, 0, 6, 0 )
191            ENDIF
192
193         ENDIF
194
195      ENDIF
196!
197!--   Broadcast the read status. This synchronises all other PEs with PE 0 of
198!--   the root model. Without synchronisation, they would not behave in the
199!--   correct way (e.g. they would not return in case of a missing NAMELIST)
200      CALL MPI_BCAST( pmc_status, 1, MPI_INTEGER, 0, MPI_COMM_WORLD, istat )
201
202      IF ( pmc_status == pmc_no_namelist_found )  THEN
203!
204!--      Not a nested run; return the MPI_WORLD communicator
205         comm = MPI_COMM_WORLD
206         RETURN
207
208      ELSEIF ( pmc_status == pmc_namelist_error )  THEN
209!
210!--      Only the root model gives the error message. Others are aborted by the
211!--      message-routine with MPI_ABORT. Must be done this way since myid and
212!--      comm2d have not yet been assigned at this point.
213         IF ( m_world_rank == 0 )  THEN
214            message_string = 'errors in \$nestpar'
215            CALL message( 'pmc_init_model', 'PA0223', 3, 2, 0, 6, 0 )
216         ENDIF
217
218      ENDIF
219
220      CALL MPI_BCAST( m_ncpl,          1, MPI_INTEGER, 0, MPI_COMM_WORLD, istat)
221      CALL MPI_BCAST( start_pe, m_ncpl+1, MPI_INTEGER, 0, MPI_COMM_WORLD, istat)
222
223!
224!--   Broadcast coupling layout
225      DO  i = 1, m_ncpl
226         CALL MPI_BCAST( m_couplers(i)%name, LEN( m_couplers(i)%name ), MPI_CHARACTER, 0, MPI_COMM_WORLD, istat )
227         CALL MPI_BCAST( m_couplers(i)%id,           1, MPI_INTEGER, 0, MPI_COMM_WORLD, istat )
228         CALL MPI_BCAST( m_couplers(i)%Parent_id,    1, MPI_INTEGER, 0, MPI_COMM_WORLD, istat )
229         CALL MPI_BCAST( m_couplers(i)%npe_total,    1, MPI_INTEGER, 0, MPI_COMM_WORLD, istat )
230         CALL MPI_BCAST( m_couplers(i)%lower_left_x, 1, MPI_REAL,    0, MPI_COMM_WORLD, istat )
231         CALL MPI_BCAST( m_couplers(i)%lower_left_y, 1, MPI_REAL,    0, MPI_COMM_WORLD, istat )
232!--      TO_DO: the next two calls can and should probably moved outside this loop
233         CALL MPI_BCAST( nesting_mode, LEN( nesting_mode ), MPI_CHARACTER, 0, MPI_COMM_WORLD, istat )
234         CALL MPI_BCAST( nesting_datatransfer_mode, LEN(nesting_datatransfer_mode), MPI_CHARACTER, 0, MPI_COMM_WORLD, istat )
235      ENDDO
236
237!
238!--   Assign global MPI processes to individual models by setting the couple id
239      DO  i = 1, m_ncpl
240         IF ( m_world_rank >= start_pe(i)  .AND.  m_world_rank < start_pe(i+1) ) &
241         THEN
242            m_my_cpl_id = i
243            EXIT
244         ENDIF
245      ENDDO
246      m_my_cpl_rank = m_world_rank - start_pe(i)
247
248!
249!--   MPI_COMM_WORLD is the communicator for ALL models (MPI-1 approach).
250!--   The communictors for the individual models as created by MPI_COMM_SPLIT.
251!--   The color of the model is represented by the coupler id
252      CALL MPI_COMM_SPLIT( MPI_COMM_WORLD, m_my_cpl_id, m_my_cpl_rank, comm,   &
253                           istat )
254!
255!--   Get size and rank of the model running on this PE
256      CALL  MPI_COMM_RANK( comm, m_model_rank, istat )
257      CALL  MPI_COMM_SIZE( comm, m_model_npes, istat )
258
259!
260!--   Broadcast (from PE 0) the parent id and id of every model
261      DO  i = 1, m_ncpl
262         CALL MPI_BCAST( m_couplers(i)%parent_id, 1, MPI_INTEGER, 0,           &
263                         MPI_COMM_WORLD, istat )
264         CALL MPI_BCAST( m_couplers(i)%id,        1, MPI_INTEGER, 0,           &
265                         MPI_COMM_WORLD, istat )
266      ENDDO
267
268!
269!--   Save the current model communicator for PMC internal use
270      m_model_comm = comm
271
272!
273!--   Create intercommunicator between server and clients.
274!--   MPI_INTERCOMM_CREATE creates an intercommunicator between 2 groups of
275!--   different colors.
276!--   The grouping was done above with MPI_COMM_SPLIT
277      DO  i = 2, m_ncpl
278
279         IF ( m_couplers(i)%parent_id == m_my_cpl_id )  THEN
280!
281!--         Collect server PEs.
282!--         Every model exept the root model has a parent model which acts as
283!--         server model. Create an intercommunicator to connect current PE to
284!--         all client PEs
285            tag = 500 + i
286            CALL MPI_INTERCOMM_CREATE( comm, 0, MPI_COMM_WORLD, start_pe(i),   &
287                                       tag, m_to_client_comm(i), istat)
288            clientcount = clientcount + 1
289            activeserver(i) = 1
290
291         ELSEIF ( i == m_my_cpl_id)  THEN
292!
293!--         Collect client PEs.
294!--         Every model exept the root model has a paremt model which acts as
295!--         server model. Create an intercommunicator to connect current PE to
296!--         all server PEs
297            tag = 500 + i
298            CALL MPI_INTERCOMM_CREATE( comm, 0, MPI_COMM_WORLD,                &
299                                       start_pe(m_couplers(i)%parent_id),      &
300                                       tag, m_to_server_comm, istat )
301         ENDIF
302
303      ENDDO
304
305!
306!--   If I am server, count the number of clients that I have
307!--   Although this loop is symmetric on all processes, the "activeserver" flag
308!--   is true (==1) on the respective individual PE only.
309      ALLOCATE( pmc_server_for_client(clientcount+1) )
310
311      clientcount = 0
312      DO  i = 2, m_ncpl
313         IF ( activeserver(i) == 1 )  THEN
314            clientcount = clientcount + 1
315            pmc_server_for_client(clientcount) = i
316         ENDIF
317      ENDDO
318!
319!--   Get the size of the server model
320      IF ( m_my_cpl_id > 1 )  THEN
321         CALL MPI_COMM_REMOTE_SIZE( m_to_server_comm, m_server_remote_size,    &
322                                    istat)
323      ELSE
324!
325!--      The root model does not have a server
326         m_server_remote_size = -1             !
327      ENDIF
328!
329!--   Set myid to non-tero value except for the root domain. This is a setting
330!--   for the message routine which is called at the end of pmci_init. That
331!--   routine outputs messages for myid = 0, only. However, myid has not been
332!--   assigened so far, so that all PEs of the root model would output a
333!--   message. To avoid this, set myid to some other value except for PE0 of the
334!--   root domain.
335      IF ( m_world_rank /= 0 )  myid = 1
336
337   END SUBROUTINE PMC_init_model
338
339
340!
341!-- Provide module private variables of the pmc for PALM
342    SUBROUTINE pmc_get_model_info( comm_world_nesting, cpl_id, cpl_name,       &
343                                   cpl_parent_id, lower_left_x, lower_left_y,  &
344                                   ncpl, npe_total, request_for_cpl_id )
345
346      USE kinds
347
348      IMPLICIT NONE
349
350      CHARACTER(LEN=*), INTENT(OUT), OPTIONAL ::  cpl_name
351
352      INTEGER, INTENT(IN), OPTIONAL ::  request_for_cpl_id
353
354      INTEGER, INTENT(OUT), OPTIONAL ::  comm_world_nesting
355      INTEGER, INTENT(OUT), OPTIONAL ::  cpl_id
356      INTEGER, INTENT(OUT), OPTIONAL ::  cpl_parent_id
357      INTEGER, INTENT(OUT), OPTIONAL ::  ncpl
358      INTEGER, INTENT(OUT), OPTIONAL ::  npe_total
359
360      INTEGER ::  requested_cpl_id
361
362      REAL(wp), INTENT(OUT), OPTIONAL ::  lower_left_x
363      REAL(wp), INTENT(OUT), OPTIONAL ::  lower_left_y
364
365!
366!--   Set the requested coupler id
367      IF ( PRESENT( request_for_cpl_id ) )  THEN
368         requested_cpl_id = request_for_cpl_id
369!
370!--      Check for allowed range of values
371         IF ( requested_cpl_id < 1 .OR. requested_cpl_id > m_ncpl )  RETURN
372      ELSE
373         requested_cpl_id = m_my_cpl_id
374      ENDIF
375
376!
377!--   Return the requested information
378      IF ( PRESENT( comm_world_nesting )  )  THEN
379         comm_world_nesting = m_world_comm
380      ENDIF
381      IF ( PRESENT( cpl_id )        )  THEN
382         cpl_id = requested_cpl_id
383      ENDIF
384      IF ( PRESENT( cpl_parent_id ) )  THEN
385         cpl_parent_id = m_couplers(requested_cpl_id)%parent_id
386      ENDIF
387      IF ( PRESENT( cpl_name )      )  THEN
388         cpl_name = m_couplers(requested_cpl_id)%name
389      ENDIF
390      IF ( PRESENT( ncpl )          )  THEN
391         ncpl = m_ncpl
392      ENDIF
393      IF ( PRESENT( npe_total )     )  THEN
394         npe_total = m_couplers(requested_cpl_id)%npe_total
395      ENDIF
396      IF ( PRESENT( lower_left_x )  )  THEN
397         lower_left_x = m_couplers(requested_cpl_id)%lower_left_x
398      ENDIF
399      IF ( PRESENT( lower_left_y )  )  THEN
400         lower_left_y = m_couplers(requested_cpl_id)%lower_left_y
401      ENDIF
402
403   END SUBROUTINE pmc_get_model_info
404
405
406
407   LOGICAL function pmc_is_rootmodel( )
408
409      IMPLICIT NONE
410
411      pmc_is_rootmodel = ( m_my_cpl_id == 1 )
412
413   END FUNCTION pmc_is_rootmodel
414
415
416
417 SUBROUTINE read_coupling_layout( nesting_datatransfer_mode, nesting_mode,     &
418                                  pmc_status )
419
420    IMPLICIT NONE
421
422    CHARACTER(LEN=7), INTENT(INOUT) ::  nesting_mode
423    CHARACTER(LEN=7), INTENT(INOUT) ::  nesting_datatransfer_mode
424
425    INTEGER, INTENT(INOUT) ::  pmc_status
426    INTEGER                ::  i, istat
427
428    TYPE(pmc_layout), DIMENSION(pmc_max_modell) ::  domain_layouts
429
430
431    NAMELIST /nestpar/  domain_layouts, nesting_datatransfer_mode, nesting_mode
432
433!
434!-- Initialize some coupling variables
435    domain_layouts(1:pmc_max_modell)%id = -1
436    m_ncpl =   0
437
438    pmc_status = pmc_status_ok
439
440!
441!-- Open the NAMELIST-file and read the nesting layout
442    CALL check_open( 11 )
443    READ ( 11, nestpar, IOSTAT=istat )
444
445    IF ( istat < 0 )  THEN
446!
447!--    No nestpar-NAMELIST found
448       pmc_status = pmc_no_namelist_found
449!
450!--    Set filepointer to the beginning of the file. Otherwise PE0 will later
451!--    be unable to read the inipar-NAMELIST
452       REWIND ( 11 )
453       RETURN
454
455    ELSEIF ( istat > 0 )  THEN
456!
457!--    Errors in reading nestpar-NAMELIST
458       pmc_status = pmc_namelist_error
459       RETURN
460
461    ENDIF
462
463!
464!-- Output location message
465    CALL location_message( 'initialize communicators for nesting', .FALSE. )
466!
467!-- Assign the layout to the internally used variable
468    m_couplers = domain_layouts
469
470!
471!-- Get the number of nested models given in the nestpar-NAMELIST
472    DO  i = 1, pmc_max_modell
473!
474!--    When id=-1 is found for the first time, the list of domains is finished
475       IF ( m_couplers(i)%id == -1  .OR.  i == pmc_max_modell )  THEN
476          IF ( m_couplers(i)%id == -1 )  THEN
477             m_ncpl = i - 1
478             EXIT
479          ELSE
480             m_ncpl = pmc_max_modell
481          ENDIF
482       ENDIF
483
484    ENDDO
485
486 END SUBROUTINE read_coupling_layout
487
488#endif
489 END MODULE pmc_handle_communicator
Note: See TracBrowser for help on using the repository browser.