added source code
[unres.git] / source / cluster / wham / src-M / work_partition.F
1 #ifdef MPI
2       subroutine work_partition(lprint,ncon_work)
3 c Split the conformations between processors
4       implicit none
5       include "DIMENSIONS"
6       include "sizesclu.dat"
7       include "mpif.h"
8       include "COMMON.IOUNITS"
9       include "COMMON.CLUSTER"
10       include "COMMON.MPI"
11       integer n,chunk,i,j,ii,remainder
12       integer kolor,key,ierror,errcode,ncon_work
13       logical lprint
14 C
15 C Divide conformations between processors; the first and
16 C the last conformation to handle by ith processor is stored in 
17 C indstart(i) and indend(i), respectively.
18 C
19 C First try to assign equal number of conformations to each processor.
20 C
21         n=ncon_work
22         write (iout,*) "n=",n," nprocs=",nprocs
23         indstart(0)=1
24         chunk = N/nprocs
25         scount(0) = chunk
26 c        print *,"i",0," indstart",indstart(0)," scount",
27 c     &     scount(0)
28         do i=1,nprocs-1
29           indstart(i)=chunk+indstart(i-1) 
30           scount(i)=scount(i-1)
31 c          print *,"i",i," indstart",indstart(i)," scount",
32 c     &     scount(i)
33         enddo 
34 C
35 C Determine how many conformations remained yet unassigned.
36 C
37         remainder=N-(indstart(nprocs-1)
38      &    +scount(nprocs-1)-1)
39 c        print *,"remainder",remainder
40 C
41 C Assign the remainder conformations to consecutive processors, starting
42 C from the lowest rank; this continues until the list is exhausted.
43 C
44         if (remainder .gt. 0) then 
45           do i=1,remainder
46             scount(i-1) = scount(i-1) + 1
47             indstart(i) = indstart(i) + i
48           enddo
49           do i=remainder+1,nprocs-1
50             indstart(i) = indstart(i) + remainder
51           enddo
52         endif
53
54         indstart(nprocs)=N+1
55         scount(nprocs)=0
56
57         do i=0,NProcs
58           indend(i)=indstart(i)+scount(i)-1
59           idispl(i)=indstart(i)-1
60         enddo
61
62         N=0
63         do i=0,Nprocs-1
64           N=N+indend(i)-indstart(i)+1
65         enddo
66
67 c        print *,"N",n," NCON_WORK",ncon_work
68         if (N.ne.ncon_work) then
69           write (iout,*) "!!! Checksum error on processor",me,
70      &      n,ncon_work
71           call flush(iout)
72           call MPI_Abort( MPI_COMM_WORLD, Ierror, Errcode )
73         endif
74
75       if (lprint) then
76         write (iout,*) "Partition of work between processors"
77           do i=0,nprocs-1
78             write (iout,'(a,i5,a,i7,a,i7,a,i7)')
79      &        "Processor",i," indstart",indstart(i),
80      &        " indend",indend(i)," count",scount(i)
81           enddo
82       endif
83       return
84       end
85 #endif