forked from dquigley533/mc_water_ls_mw
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomms_mpi.f90
More file actions
620 lines (448 loc) · 22.6 KB
/
comms_mpi.f90
File metadata and controls
620 lines (448 loc) · 22.6 KB
1
2
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
! -*- mode: F90 ; mode: font-lock ; column-number-mode: true ; vc-back-end: Git -*-
!=============================================================================!
! C O M M S !
!=============================================================================!
! Abstracted comms layer for implementing parallel tempering Monte-Carlo !
! This is the MPI version. !
!=============================================================================!
module comms
use constants, only : dp
implicit none
include 'mpif.h'
! buffers for sending and recieving all molecule data during parallel
! tempering moves.
real(kind=dp),allocatable,dimension(:),save :: Rbuffer,Sbuffer
real(kind=dp),allocatable,dimension(:),save :: hist_last_sync,eta_last_sync
real(kind=dp),allocatable,dimension(:),save :: uhist_last_sync
integer :: myrank,size
contains
subroutine Comms_Initialise()
!=========================================================================!
! Initialises MPI and gets the rank of this task, and the size of the !
! global communicator. !
!-------------------------------------------------------------------------!
! David Quigley June 2003 !
!=========================================================================!
implicit none
integer :: ierr !,provided
! Initialise MPI
call mpi_init(ierr)
!!$ ! Initialise MPI - checking thread support
!!$ call mpi_init_thread(MPI_THREAD_FUNNELED,provided,ierr)
!!$
!!$ if (provided==MPI_THREAD_FUNNELED) then
!!$ write(0,'("MPI provided MPI_THREAD_FUNNELED : OK")')
!!$ else
!!$ write(0,'("-------- W A R N I N G ------------------")')
!!$ write(0,'(" MPI did not provide MPI_THREAD_FUNNELED ")')
!!$ if (provided==MPI_THREAD_SINGLE) then
!!$ write(0,'(" MPI provided MPI_THREAD_SINGLE")')
!!$ else if (provided==MPI_THREAD_SERIALIZED) then
!!$ write(0,'(" MPI provided MPI_THREAD_SERIALIZED")')
!!$ else if (provided==MPI_THREAD_MULTIPLE) then
!!$ write(0,'(" MPI provided MPI_THREAD_MULTIPLE")')
!!$ end if
!!$ write(0,'("-----------------------------------------")')
!!$ end if
!!$
!!$ if (ierr.ne.MPI_SUCCESS) stop 'Error initialising MPI - exiting!'
! Get the size of the communicator
call mpi_comm_size(MPI_COMM_WORLD,size,ierr)
if (ierr.ne.MPI_SUCCESS) stop 'Could not get size of communicator'
! Get my rank in the communicator
call mpi_comm_rank(MPI_COMM_WORLD,myrank,ierr)
if (ierr.ne.MPI_SUCCESS) stop 'Could not get rank'
return
end subroutine Comms_Initialise
subroutine comms_allocate()
!=========================================================================!
! Allocates arrays used in parallel tempering send/receive moves !
! Must be called after water data structures are created. !
!-------------------------------------------------------------------------!
! David Quigley September 2006 !
!=========================================================================!
use userparams, only : nwater,mc_ensemble,nbins,samplerun
use model, only : cspm,ljspm
implicit none
integer :: dim,ierr
! 3 reals for each site + 4 for quaternions
dim = nwater* (3*cspm + 3*ljspm + 4)
if ( mc_ensemble == 'npt' ) dim = dim + 9
allocate(Rbuffer(1:dim),stat=ierr)
allocate(Sbuffer(1:dim),stat=ierr)
if (ierr/=0) stop 'Error allocating send and recieve buffers'
allocate(eta_last_sync(1:nbins),stat=ierr)
allocate(hist_last_sync(1:nbins),stat=ierr)
if (samplerun) allocate(uhist_last_sync(1:nbins),stat=ierr)
if (ierr/=0) stop 'Error allocating bin sync arrays'
eta_last_sync = 0.0_dp
hist_last_sync = 0.0_dp
if (samplerun) uhist_last_sync = 0.0_dp
return
end subroutine comms_allocate
subroutine Comms_BcastReal(Rvalue,Length)
!=========================================================================!
! Routine to broadcast a single real value from the root node (task 0) !
! to all other nodes. !
!-------------------------------------------------------------------------!
! Written by David Quigley June 2003 !
!=========================================================================!
implicit none
real(kind=dp),intent(INOUT) :: Rvalue
integer,intent(IN) :: Length
integer :: ierr
call MPI_BCAST(Rvalue,Length,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
if (ierr.ne.MPI_SUCCESS) stop 'Error in Comms_BcastReal - comms operation failed'
return
end subroutine Comms_BcastReal
subroutine Comms_BcastChar(Cvalue,Length)
!=========================================================================!
! Routine to broadcast a single character from the root node (task 0) !
! to all other nodes. !
!-------------------------------------------------------------------------!
! Written by David Quigley June 2003 !
!=========================================================================!
implicit none
character,intent(inout) :: Cvalue
integer,intent(in) :: length
integer :: ierr
call MPI_BCAST(Cvalue,Length,MPI_CHARACTER,0,MPI_COMM_WORLD,ierr)
if (ierr.ne.MPI_SUCCESS) stop 'Error in Comms_BcastChar - comms operation failed'
return
end subroutine Comms_BcastChar
subroutine Comms_BcastLog(Lvalue,Length)
!=========================================================================!
! Routine to broadcast a single logical from the root node (task 0) !
! to all other nodes. !
!-------------------------------------------------------------------------!
! Written by David Quigley June 2003 !
!=========================================================================!
implicit none
logical,intent(inout) :: Lvalue
integer,intent(in) :: length
integer :: ierr
call MPI_BCAST(Lvalue,Length,MPI_LOGICAL,0,MPI_COMM_WORLD,ierr)
if (ierr.ne.MPI_SUCCESS) stop 'Error in Comms_BcastLog - comms operation failed'
return
end subroutine Comms_BcastLog
subroutine comms_p2preal(Rvalue,length,snode,rnode)
!=========================================================================!
! Sends real data from snode to rnode. Must be called by both snode and !
! rnode. !
!-------------------------------------------------------------------------!
! Written by David Quigley June 2003 !
!=========================================================================!
implicit none
real(kind=dp),intent(INOUT) :: Rvalue
integer,intent(IN) :: Length,snode,rnode
integer :: ierr
integer,dimension(1:MPI_STATUS_SIZE) :: status
if (myrank==snode) then
call MPI_Send(Rvalue,length,MPI_DOUBLE_PRECISION,rnode,25,MPI_COMM_WORLD,ierr)
if (ierr/=0) stop 'Error during send'
elseif(myrank==rnode) then
call MPI_Recv(Rvalue,length,MPI_DOUBLE_PRECISION,snode,25,MPI_COMM_WORLD,status,ierr)
if (ierr/=0) stop 'Error during receive'
end if
return
end subroutine comms_p2preal
subroutine comms_p2pint(Ivalue,length,snode,rnode)
!=========================================================================!
! Sends integer data from snode to rnode. Must be called by both snode !
! rnode. !
!-------------------------------------------------------------------------!
! Written by David Quigley June 2003 !
!=========================================================================!
implicit none
integer,intent(INOUT) :: Ivalue
integer,intent(IN) :: Length,snode,rnode
integer :: ierr
integer,dimension(1:MPI_STATUS_SIZE) :: status
if (myrank==snode) then
call MPI_Send(Ivalue,length,MPI_DOUBLE_PRECISION,rnode,25,MPI_COMM_WORLD,ierr)
if (ierr/=0) stop 'Error during send'
elseif(myrank==rnode) then
call MPI_Recv(Ivalue,length,MPI_DOUBLE_PRECISION,snode,25,MPI_COMM_WORLD,status,ierr)
if (ierr/=0) stop 'Error during receive'
end if
return
end subroutine comms_p2pint
subroutine Comms_BcastInt(Ivalue,Length)
!=========================================================================!
! Routine to broadcast a single real value from the root node (task 0) !
! to all other nodes. !
!-------------------------------------------------------------------------!
! Written by David Quigley June 2003 !
!=========================================================================!
implicit none
integer,intent(INOUT) :: Ivalue
integer,intent(IN) :: Length
integer :: ierr
call MPI_BCAST(Ivalue,Length,MPI_INTEGER,0,MPI_COMM_WORLD,ierr)
if (ierr.ne.MPI_SUCCESS) stop 'Error in Comms_BcastInt - comms operation failed'
return
end subroutine Comms_BcastInt
subroutine comms_allreduce_eta(weight,Length)
!=========================================================================!
! Wrapper for mpi_allreduce (real - double precision) !
!-------------------------------------------------------------------------!
! Written by David Quigley June 2003 !
!=========================================================================!
implicit none
integer,intent(in) :: Length
real(kind=dp),intent(inout),dimension(1:Length) :: weight
real(kind=dp),dimension(:),allocatable :: buff
integer :: ierr
weight = weight - eta_last_sync
! sanity check
!!$ do i = 1,2
!!$ do j = 1,length
!!$ if (weight(j,i)<0.0_dp) stop 'Negative growth of eta since last sync'
!!$ end do
!!$ end do
allocate(buff(1:length))
call MPI_ALLreduce(weight(1),buff(1),length,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,ierr)
if (ierr.ne.MPI_SUCCESS) stop 'Error in comms_allreducereal - comms operation failed'
weight = buff + eta_last_sync
eta_last_sync = weight
deallocate(buff)
return
end subroutine comms_allreduce_eta
subroutine comms_get_max(myval,maxval)
!=========================================================================!
! Find the maximum of a double over all MPI ranks. !
!-------------------------------------------------------------------------!
! Written by David Quigley February 2019 !
!=========================================================================!
implicit none
real(kind=dp),intent(in) :: myval
real(kind=dp),intent(out) :: maxval
integer :: ierr
call MPI_Allreduce(myval,maxval,1,MPI_DOUBLE_PRECISION,MPI_MAX,MPI_COMM_WORLD,ierr)
if (ierr/=0) stop 'Error finding maximum value over all nodes'
return
end subroutine comms_get_max
subroutine comms_join_uhist(uhist,length,overlap,joined)
!=========================================================================!
! Join unbiased histogram together from multiple overlapping windows. !
!-------------------------------------------------------------------------!
! Written by David Quigley February 2019 !
!=========================================================================!
implicit none
integer,intent(in) :: length,overlap
real(kind=dp),intent(in),dimension(1:length) :: uhist
real(kind=dp),intent(out),dimension(1:length) :: joined
real(kind=dp),dimension(1:length) :: recvbuff
real(kind=dp) :: shift,myave,nextav
integer,dimension(MPI_STATUS_SIZE) :: status
integer :: bins_per_window,irank,ierr,k,my_start_bin,my_end_bin
! Clunky - replace with something useing MBAR?
bins_per_window = length/size
if (myrank==0) joined = uhist
do irank = 1,size-1
if (myrank == 0) then
! Receive from irank at every iteration
call MPI_Recv(recvbuff,length,MPI_DOUBLE_PRECISION,irank,99,MPI_COMM_WORLD,status,ierr)
if (ierr/=0) stop 'Error receiving from partner in comms_join_uhist'
elseif (myrank==irank) then
! Send to rank zero if iteration counter matches current rank
call MPI_Send(uhist,length,MPI_DOUBLE_PRECISION,0,99,MPI_COMM_WORLD,ierr)
if (ierr/=0) stop 'Error sending to partner in comms_join_uhist'
end if
if (myrank==0) then
! Range of bins rank 0 already has data for
my_start_bin = 1
my_end_bin = irank*bins_per_window
myave = 0.0_dp
do k = my_end_bin-overlap,my_end_bin+overlap
myave = myave + log(joined(k))
end do
myave = myave/real(2*overlap+1,kind=dp)
! Average from the overlapping bins in the next window
nextav = 0.0_dp
do k = my_end_bin-overlap,my_end_bin+overlap
nextav = nextav + log(recvbuff(k))
end do
nextav = nextav/real(2*overlap+1,kind=dp)
shift = myave-nextav
if (isnan(shift)) shift = 0.0_dp
do k = my_end_bin+1,length
joined(k) = recvbuff(k)*exp(shift)
end do
end if
end do ! end loop over ranks
! Broadcast joined to everyone
call MPI_Bcast(joined,length,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
if (ierr/=0) stop 'Error broadcasting joined unbiased histogram'
return
end subroutine comms_join_uhist
subroutine comms_join_eta(weight,Length,overlap,joined)
!=========================================================================!
! Join weight functions together from multiple overlapping windows. !
!-------------------------------------------------------------------------!
! Written by David Quigley February 2019 !
!=========================================================================!
implicit none
integer,intent(in) :: length,overlap
real(kind=dp),intent(in),dimension(1:length) :: weight
real(kind=dp),intent(out),dimension(1:length) :: joined
real(kind=dp),dimension(1:length) :: recvbuff
real(kind=dp) :: shift,myave,nextav
integer,dimension(MPI_STATUS_SIZE) :: status
integer :: bins_per_window,irank,ierr,k,my_start_bin,my_end_bin
! Clunky - replace with something useing MBAR?
bins_per_window = length/size
if (myrank==0) joined = weight
do irank = 1,size-1
if (myrank == 0) then
! Receive from irank at every iteration
call MPI_Recv(recvbuff,length,MPI_DOUBLE_PRECISION,irank,99,MPI_COMM_WORLD,status,ierr)
if (ierr/=0) stop 'Error receiving from partner in comms_join_eta'
elseif (myrank==irank) then
! Send to rank zero if iteration counter matches current rank
call MPI_Send(weight,length,MPI_DOUBLE_PRECISION,0,99,MPI_COMM_WORLD,ierr)
if (ierr/=0) stop 'Error sending to partner in comms_join_eta'
end if
if (myrank==0) then
! Range of bins rank 0 already has data for
my_start_bin = 1
my_end_bin = irank*bins_per_window
myave = sum(joined(my_end_bin-overlap:my_end_bin+overlap))
myave = myave/real(2*overlap+1,kind=dp)
! Average from the overlapping bins in the next window
nextav = sum(recvbuff(my_end_bin-overlap:my_end_bin+overlap))
nextav = nextav/real(2*overlap+1,kind=dp)
shift = myave-nextav
do k = my_end_bin+1,length
joined(k) = recvbuff(k) + shift
end do
end if
end do ! end loop over ranks
shift = joined(length/2+1)
do k = 1,length
joined(k) = joined(k)-shift
end do
! Broadcast joined to everyone
call MPI_Bcast(joined,length,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
if (ierr/=0) stop 'Error broadcasting joined weight function'
return
end subroutine comms_join_eta
subroutine comms_allreduce_hist(histogram,Length)
!=========================================================================!
! Wrapper for mpi_allreduce (real - double precision) !
!-------------------------------------------------------------------------!
! Written by David Quigley June 2003 !
!=========================================================================!
implicit none
integer,intent(in) :: Length
real(kind=dp),intent(inout),dimension(Length) :: histogram
real(kind=dp),dimension(:),allocatable :: buff
integer :: ierr
call MPI_Barrier(MPI_COMM_WORLD,ierr)
histogram = histogram - hist_last_sync
! sanity check
!!$ do j = 1,length
!!$ if (histogram(j)<0) stop 'Negative growth of histogram since last sync'
!!$ end do
allocate(buff(1:length))
call MPI_ALLreduce(histogram,buff,length,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,ierr)
if (ierr.ne.MPI_SUCCESS) stop 'Error in comms_allreducereal - comms operation failed'
histogram = buff + hist_last_sync
hist_last_sync = histogram
deallocate(buff)
return
end subroutine comms_allreduce_hist
subroutine comms_allreduce_uhist(histogram,Length)
!=========================================================================!
! Wrapper for mpi_allreduce (real - double precision) !
!-------------------------------------------------------------------------!
! Written by David Quigley June 2003 !
! Unbiased histogram version February 2019 !
!=========================================================================!
implicit none
integer,intent(in) :: Length
real(kind=dp),intent(inout),dimension(Length) :: histogram
real(kind=dp),dimension(:),allocatable :: buff
integer :: ierr
call MPI_Barrier(MPI_COMM_WORLD,ierr)
histogram = histogram - uhist_last_sync
! sanity check
!!$ do j = 1,length
!!$ if (histogram(j)<0) stop 'Negative growth of histogram since last sync'
!!$ end do
allocate(buff(1:length))
call MPI_ALLreduce(histogram,buff,length,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,ierr)
if (ierr.ne.MPI_SUCCESS) stop 'Error in comms_allreducereal - comms operation failed'
histogram = buff + uhist_last_sync
uhist_last_sync = histogram
deallocate(buff)
return
end subroutine comms_allreduce_uhist
subroutine comms_set_histogram(hist_in,length)
!=========================================================================!
! Sets the internal array hist_last_sync. To be used when zeroing !
! the histogram across all nodes. !
!-------------------------------------------------------------------------!
! Written by David Quigley June 2003 !
!=========================================================================!
implicit none
integer,intent(in) :: length
real(kind=dp),dimension(length),intent(in) :: hist_in
hist_last_sync = hist_in
return
end subroutine comms_set_histogram
subroutine comms_set_uhistogram(hist_in,length)
!=========================================================================!
! Sets the internal array uhist_last_sync. To be used when zeroing !
! the histogram across all nodes. !
!-------------------------------------------------------------------------!
! Written by David Quigley June 2003 !
! Unbiased histogram version February 2019 !
!=========================================================================!
implicit none
integer,intent(in) :: length
real(kind=dp),dimension(length),intent(in) :: hist_in
uhist_last_sync = hist_in
return
end subroutine comms_set_uhistogram
subroutine Comms_Finalise()
!=========================================================================!
! Initialises MPI and gets the rank of this task, and the size of the !
! global communicator. !
!-------------------------------------------------------------------------!
! Necessary conditions: !
! MPI must have been initialised !
!-------------------------------------------------------------------------!
! Written by David Quigley June 2003 !
!=========================================================================!
use userparams, only : samplerun
implicit none
integer :: ierr
!Finalise comms
call MPI_FINALIZE(ierr)
if (ierr.ne.MPI_SUCCESS) stop 'Error finalising MPI!'
deallocate(RBuffer,stat=ierr)
deallocate(SBuffer,stat=ierr)
if (ierr/=0) stop 'Error deallocating send and recieve buffer'
deallocate(hist_last_sync,stat=ierr)
deallocate(eta_last_sync,stat=ierr)
if (samplerun) deallocate(uhist_last_sync,stat=ierr)
if (ierr/=0) stop 'Error deallocating bin arrays'
return
end subroutine Comms_Finalise
subroutine comms_barrier()
!=========================================================================!
! Initialises MPI and gets the rank of this task, and the size of the !
! global communicator. !
!-------------------------------------------------------------------------!
! Necessary conditions: !
! MPI must have been initialised !
!-------------------------------------------------------------------------!
! Written by David Quigley May 2011 !
!=========================================================================!
implicit none
integer :: ierr
call MPI_BARRIER(MPI_COMM_WORLD,ierr)
return
end subroutine comms_barrier
end module comms