parallel_distributed_diago.f90 11.5 KB
Newer Older
Marco Govoni's avatar
Marco Govoni committed
1
!
Marco Govoni's avatar
Marco Govoni committed
2
! Copyright (C) 2015-2021 M. Govoni 
Marco Govoni's avatar
Marco Govoni committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
! This file is distributed under the terms of the
! GNU General Public License. See the file `License'
! in the root directory of the present distribution,
! or http://www.gnu.org/copyleft/gpl.txt .
!
! This file is part of WEST.
!
! Contributors to this file: 
! Marco Govoni
!
#define ZERO ( 0.D0, 0.D0 )
#define ONE  ( 1.D0, 0.D0 )
!
#if defined __SCALAPACK
!----------------------------------------------------------------------------
SUBROUTINE parallel_distributed_diago_dsy ( glob_nselect,glob_ndim,glob_ndimx,a_distr,v_distr,e,NPROW,NPCOL,idistr ) 
  !----------------------------------------------------------------------------
  !
  ! Diagox -- parallel
  !   global_nselect : number of wanted ev
  !   global_ndim    : actual dimension of a
  !   global_ndimx   : leading dimension of a
  !   a_distr        : matrix to be diago
  !   v_distr        : unitary trans. 
  !   e              : eigenval 
  !
  USE kinds,                 ONLY : DP
  USE mp,                    ONLY : mp_bcast, mp_sum, mp_circular_shift_left
  USE mp_global,             ONLY : me_bgrp, root_bgrp, intra_bgrp_comm, nproc_bgrp, inter_image_comm, nimage
  USE mp_world,              ONLY : nproc, world_comm, mpime
  USE class_idistribute,     ONLY : idistribute
  !
  IMPLICIT NONE
  !
  ! I/O
  !
  TYPE(idistribute),INTENT(IN) :: idistr
  INTEGER,INTENT(IN) :: glob_nselect, glob_ndim, glob_ndimx 
  REAL(DP),INTENT(IN) :: a_distr(glob_ndimx,idistr%nlocx)
  REAL(DP),INTENT(OUT) :: v_distr(glob_ndimx,idistr%nlocx)
  REAL(DP),INTENT(OUT) :: e(glob_nselect)
  INTEGER,INTENT(IN) :: NPROW,NPCOL
  !
  ! Workspace
  !
  INTEGER :: il1, ig1
  INTEGER :: il2, ig2
  INTEGER :: i_loc, j_loc, i_glob, j_glob
  INTEGER :: icycl
  INTEGER,ALLOCATABLE :: tmp_igl(:)
  !
  ! Workspace for SCALAPACK
  !
  REAL(DP),ALLOCATABLE :: la(:,:),lv(:,:)
  REAL(DP) :: ge(glob_ndim)
  INTEGER :: NB, MYROW, MYCOL, LDROW, LDCOL
  INTEGER :: BLACS_CONTEXT, INFO
  INTEGER :: DESCA(9),DESCV(9)
  INTEGER :: ifail,LWORK
  REAL(DP),ALLOCATABLE :: work(:)
  INTEGER,EXTERNAL :: INDXL2G
  INTEGER,EXTERNAL :: NUMROC
  !
  ! ===============================
  ! 1) Definisco una griglia locale
  ! =============================== 
  !
  NB= MAX(glob_ndim / NPCOL,1)
  !
  CALL BLACS_GET( -1, 0, BLACS_CONTEXT )
  CALL BLACS_GRIDINIT( BLACS_CONTEXT, 'R', NPROW, NPCOL )
  CALL BLACS_GRIDINFO( BLACS_CONTEXT, NPROW, NPCOL, MYROW, MYCOL )
  !
  IF( MYROW/=-1 .OR. MYCOL/=-1) THEN 
     !
     LDROW = NUMROC(glob_ndim, NB, MYROW, 0, NPROW)   ! number of elements in this element of the grid 
     LDCOL = NUMROC(glob_ndim, NB, MYCOL, 0, NPCOL)   ! number of elements in this element of the grid
     CALL DESCINIT( DESCA, glob_ndim, glob_ndim, NB, NB, 0, 0, BLACS_CONTEXT, LDROW, INFO )
     CALL DESCINIT( DESCV, glob_ndim, glob_ndim, NB, NB, 0, 0, BLACS_CONTEXT, LDROW, INFO )
     !
     ALLOCATE( la( LDROW, LDCOL), lv( LDROW, LDCOL) )
     !
  ENDIF
  !
  ! ========================
  ! 2) Ripartisco la matrice
  ! ========================
  !
  v_distr = a_distr
  ALLOCATE( tmp_igl(1:idistr%nlocx) ) 
  tmp_igl = 0
  DO il1 = 1, idistr%nloc 
     tmp_igl(il1) = idistr%l2g(il1)
  ENDDO
  !
  DO icycl=0,nimage-1
     !
     IF( MYROW/=-1 .OR. MYCOL/=-1) THEN 
        !
        ! Filling of the local array distributed in the processor grid
        !
        DO il2 = 1,idistr%nloc
           ig2 = tmp_igl(il2)
           IF(ig2<1) CYCLE 
           ! 
           DO j_loc=1,LDCOL
              j_glob=INDXL2G(j_loc, NB, MYCOL, 0, NPCOL)
              IF(j_glob.NE.ig2) CYCLE 
              DO i_loc=1,LDROW
                 i_glob=INDXL2G(i_loc, NB, MYROW, 0, NPROW)
                 la(i_loc,j_loc) = v_distr(i_glob,il2)
              ENDDO
           ENDDO
           !
        ENDDO
        !
     ENDIF
     !
     ! Cycle the v_distr array 
     ! 
     CALL mp_circular_shift_left( v_distr,        icycl, inter_image_comm)
     CALL mp_circular_shift_left( tmp_igl, icycl+nimage, inter_image_comm)
     !
  ENDDO
  !
  DEALLOCATE( tmp_igl )
  !
  ! ===============
  ! 3) DIAGONALIZZO
  ! ===============
  !
  ge = 0._DP
  IF( MYROW/=-1 .OR. MYCOL/=-1) THEN 
     !
     LWORK=-1 
     ALLOCATE( work(1) )
     CALL PDSYEV('V','U',glob_ndim,la,1,1,DESCA,ge,lv,1,1,DESCV,work,LWORK,IFAIL )
     ! 
     LWORK=INT(work(1))+1
     DEALLOCATE( work )
     !
     ALLOCATE( work(LWORK) )
     CALL PDSYEV('V','U',glob_ndim,la,1,1,DESCA,ge,lv,1,1,DESCV,work,LWORK,IFAIL )
     !
     DEALLOCATE( work, la )
     !
  ENDIF
  !
  ! =========================
  ! 4) DISTRIBUISCO RISULTATO
  ! =========================
  !
  ! Distribuisco eigenvalues
  !
  DO i_glob = 1, glob_nselect
     e(i_glob) = ge(i_glob)
  ENDDO
  CALL mp_bcast(e,0,world_comm)
  !
  ! Distribuisco eigenvectors
  !
  v_distr = 0._DP
  ALLOCATE( tmp_igl(1:idistr%nlocx) ) 
  tmp_igl = 0
  DO il1 = 1, idistr%nloc 
     tmp_igl(il1) = idistr%l2g(il1)
  ENDDO
  !
  DO icycl=0,nimage-1
     !
     IF( MYROW/=-1 .OR. MYCOL/=-1) THEN 
        !
        ! Filling of the local array distributed in the processor grid
        !
        DO il2 = 1,idistr%nloc
           ig2 = tmp_igl(il2) 
           IF(ig2<1) CYCLE 
           ! 
           DO j_loc=1,LDCOL
              j_glob=INDXL2G(j_loc, NB, MYCOL, 0, NPCOL)
              IF(j_glob.NE.ig2) CYCLE 
              DO i_loc=1,LDROW
                 i_glob=INDXL2G(i_loc, NB, MYROW, 0, NPROW)
                 v_distr(i_glob,il2) = lv(i_loc,j_loc)
              ENDDO
           ENDDO
           !
        ENDDO
        !
     ENDIF
     !
     ! Cycle the v_distr array 
     ! 
     CALL mp_circular_shift_left( v_distr,        icycl, inter_image_comm)
     CALL mp_circular_shift_left( tmp_igl, icycl+nimage, inter_image_comm)
     !
  ENDDO
  !
  DEALLOCATE( tmp_igl )
  !
  IF( MYROW/=-1 .OR. MYCOL/=-1) THEN 
     DEALLOCATE( lv )
     CALL BLACS_GRIDEXIT(BLACS_CONTEXT)
  ENDIF
  !
  CALL mp_sum(v_distr,intra_bgrp_comm)
  !
END SUBROUTINE
!
!----------------------------------------------------------------------------
SUBROUTINE parallel_distributed_diago_zhe ( glob_nselect,glob_ndim,glob_ndimx,a_distr,v_distr,e,NPROW,NPCOL,idistr ) 
  !----------------------------------------------------------------------------
  !
  ! Diagox -- parallel
  !   global_nselect : number of wanted ev
  !   global_ndim    : actual dimension of a
  !   global_ndimx   : leading dimension of a
  !   a_distr        : matrix to be diago
  !   v_distr        : unitary trans. 
  !   e              : eigenval 
  !
  USE kinds,                 ONLY : DP
  USE mp,                    ONLY : mp_bcast, mp_sum, mp_circular_shift_left
  USE mp_global,             ONLY : me_bgrp, root_bgrp, intra_bgrp_comm, nproc_bgrp, inter_image_comm, nimage
  USE mp_world,              ONLY : nproc, world_comm, mpime
  USE class_idistribute,     ONLY : idistribute
  !
  IMPLICIT NONE
  !
  ! I/O
  !
  TYPE(idistribute),INTENT(IN) :: idistr
  INTEGER,INTENT(IN) :: glob_nselect, glob_ndim, glob_ndimx
  COMPLEX(DP),INTENT(IN) :: a_distr(glob_ndimx,idistr%nlocx)
  COMPLEX(DP),INTENT(OUT) :: v_distr(glob_ndimx,idistr%nlocx)
  REAL(DP),INTENT(OUT) :: e(glob_nselect)
  INTEGER,INTENT(IN) :: NPROW,NPCOL
  !
  ! Workspace
  !
  INTEGER :: il1, ig1
  INTEGER :: il2, ig2
  INTEGER :: i_loc, j_loc, i_glob, j_glob
  INTEGER :: icycl
  INTEGER,ALLOCATABLE :: tmp_igl(:)
  !
  ! Workspace for SCALAPACK
  !
  COMPLEX(DP),ALLOCATABLE :: la(:,:),lv(:,:)
  REAL(DP) :: ge(glob_ndim)
  INTEGER :: NB, MYROW, MYCOL, LDROW, LDCOL
  INTEGER :: BLACS_CONTEXT, INFO
  INTEGER :: DESCA(9),DESCV(9)
  INTEGER :: ifail,LWORK,LRWORK
  COMPLEX(DP),ALLOCATABLE :: rwork(:)
  COMPLEX(DP),ALLOCATABLE :: work(:)
  INTEGER,EXTERNAL :: INDXL2G
  INTEGER,EXTERNAL :: NUMROC
  !
  ! ===============================
  ! 1) Definisco una griglia locale
  ! =============================== 
  !
  NB= MAX(glob_ndim / NPCOL,1)
  !
  CALL BLACS_GET( -1, 0, BLACS_CONTEXT )
  CALL BLACS_GRIDINIT( BLACS_CONTEXT, 'R', NPROW, NPCOL )
  CALL BLACS_GRIDINFO( BLACS_CONTEXT, NPROW, NPCOL, MYROW, MYCOL )
  !
  IF( MYROW/=-1 .OR. MYCOL/=-1) THEN 
     !
     LDROW = NUMROC(glob_ndim, NB, MYROW, 0, NPROW)   ! number of elements in this element of the grid 
     LDCOL = NUMROC(glob_ndim, NB, MYCOL, 0, NPCOL)   ! number of elements in this element of the grid
     CALL DESCINIT( DESCA, glob_ndim, glob_ndim, NB, NB, 0, 0, BLACS_CONTEXT, LDROW, INFO )
     CALL DESCINIT( DESCV, glob_ndim, glob_ndim, NB, NB, 0, 0, BLACS_CONTEXT, LDROW, INFO )
     !
     ALLOCATE( la( LDROW, LDCOL), lv( LDROW, LDCOL) )
     !
  ENDIF
  !
  ! ========================
  ! 2) Ripartisco la matrice
  ! ========================
  !
  v_distr = a_distr
  ALLOCATE( tmp_igl(1:idistr%nlocx) ) 
  tmp_igl = 0
  DO il1 = 1, idistr%nloc 
     tmp_igl(il1) = idistr%l2g(il1)
  ENDDO
  !
  DO icycl=0,nimage-1
     !
     IF( MYROW/=-1 .OR. MYCOL/=-1) THEN 
        !
        ! Filling of the local array distributed in the processor grid
        !
        DO il2 = 1,idistr%nloc
           ig2 = tmp_igl(il2)
           IF(ig2<1) CYCLE 
           ! 
           DO j_loc=1,LDCOL
              j_glob=INDXL2G(j_loc, NB, MYCOL, 0, NPCOL)
              IF(j_glob.NE.ig2) CYCLE 
              DO i_loc=1,LDROW
                 i_glob=INDXL2G(i_loc, NB, MYROW, 0, NPROW)
                 la(i_loc,j_loc) = v_distr(i_glob,il2)
              ENDDO
           ENDDO
           !
        ENDDO
        !
     ENDIF
     !
     ! Cycle the v_distr array 
     ! 
     CALL mp_circular_shift_left( v_distr,        icycl, inter_image_comm)
     CALL mp_circular_shift_left( tmp_igl, icycl+nimage, inter_image_comm)
     !
  ENDDO
  !
  DEALLOCATE( tmp_igl )
  !
  ! ===============
  ! 3) DIAGONALIZZO
  ! ===============
  !
  ge = 0._DP
  IF( MYROW/=-1 .OR. MYCOL/=-1) THEN 
     !
     LWORK=-1 
     LRWORK=-1 
     ALLOCATE( work(1) )
     ALLOCATE( rwork(1) )
     CALL PZHEEV('V','U',glob_ndim,la,1,1,DESCA,ge,lv,1,1,DESCV,work,LWORK,rwork,LRWORK,IFAIL )
     ! 
     LWORK=INT(DBLE(work(1)))+1
     DEALLOCATE( work )
     LRWORK=INT(DBLE(rwork(1)))+1
     DEALLOCATE( rwork )
     !
     ALLOCATE( work(LWORK) )
     ALLOCATE( rwork(LRWORK) )
     CALL PZHEEV('V','U',glob_ndim,la,1,1,DESCA,ge,lv,1,1,DESCV,work,LWORK,rwork,LRWORK,IFAIL )
     !
     DEALLOCATE( work, rwork, la )
     !
  ENDIF
  !
  ! =========================
  ! 4) DISTRIBUISCO RISULTATO
  ! =========================
  !
  ! Distribuisco eigenvalues
  !
  DO i_glob = 1, glob_nselect
     e(i_glob) = ge(i_glob)
  ENDDO
  CALL mp_bcast(e,0,world_comm)
  !
  ! Distribuisco eigenvectors
  !
  v_distr = 0._DP
  ALLOCATE( tmp_igl(1:idistr%nlocx) ) 
  tmp_igl = 0
  DO il1 = 1, idistr%nloc 
     tmp_igl(il1) = idistr%l2g(il1)
  ENDDO
  !
  DO icycl=0,nimage-1
     !
     IF( MYROW/=-1 .OR. MYCOL/=-1) THEN 
        !
        ! Filling of the local array distributed in the processor grid
        !
        DO il2 = 1,idistr%nloc
           ig2 = tmp_igl(il2) 
           IF(ig2<1) CYCLE 
           ! 
           DO j_loc=1,LDCOL
              j_glob=INDXL2G(j_loc, NB, MYCOL, 0, NPCOL)
              IF(j_glob.NE.ig2) CYCLE 
              DO i_loc=1,LDROW
                 i_glob=INDXL2G(i_loc, NB, MYROW, 0, NPROW)
                 v_distr(i_glob,il2) = lv(i_loc,j_loc)
              ENDDO
           ENDDO
           !
        ENDDO
        !
     ENDIF
     !
     ! Cycle the v_distr array 
     ! 
     CALL mp_circular_shift_left( v_distr,        icycl, inter_image_comm)
     CALL mp_circular_shift_left( tmp_igl, icycl+nimage, inter_image_comm)
     !
  ENDDO
  !
  DEALLOCATE( tmp_igl )
  !
  IF( MYROW/=-1 .OR. MYCOL/=-1) THEN 
     DEALLOCATE( lv )
     CALL BLACS_GRIDEXIT(BLACS_CONTEXT)
  ENDIF
  !
  CALL mp_sum(v_distr,intra_bgrp_comm)
  !
END SUBROUTINE
#endif