| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
module autompi |
| 19 |
|
| 20 |
use auto_constants, only: autoparameters, niap, nrap |
| 21 |
|
| 22 |
|
| 23 |
implicit none |
| 24 |
private |
| 25 |
|
| 26 |
public :: mpiini, mpiiap, mpiwfi, mpireduce, mpibcksub, mpisbv, mpicbv, mpibcast |
| 27 |
public :: mpibcasti, mpibcast1i, mpibcast1l, mpibcastap, mpireducemax, mpireducemin |
| 28 |
public :: mpigat, mpiscat, mpiend, mpitim, mpiiam, mpikwt, partition |
| 29 |
public :: mpigats |
| 30 |
|
| 31 |
integer, parameter :: AUTO_MPI_KILL_MESSAGE = 0, AUTO_MPI_SOLVBV_MESSAGE = 1 |
| 32 |
integer, parameter :: AUTO_MPI_INIT_MESSAGE = 2 |
| 33 |
|
| 34 |
include 'mpif.h' |
| 35 |
|
| 36 |
contains |
| 37 |
|
| 38 |
subroutine mpiini() |
| 39 |
|
| 40 |
integer ierr,iam,namelen,provided |
| 41 |
character(len=MPI_MAX_PROCESSOR_NAME) processor_name |
| 42 |
integer :: message_type |
| 43 |
|
| 44 |
provided = MPI_THREAD_SINGLE |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
if (provided == MPI_THREAD_SINGLE) then |
| 49 |
|
| 50 |
|
| 51 |
call MPI_Init(ierr) |
| 52 |
endif |
| 53 |
call MPI_Comm_rank(MPI_COMM_WORLD,iam,ierr) |
| 54 |
call MPI_Get_processor_name(processor_name,namelen,ierr) |
| 55 |
|
| 56 |
if(iam/=0)then |
| 57 |
call MPI_Bcast(message_type,1,MPI_INTEGER,0,MPI_COMM_WORLD,ierr) |
| 58 |
if(message_type /= AUTO_MPI_INIT_MESSAGE)then |
| 59 |
print *,'Fatal: no init message, message received: ', message_type |
| 60 |
call MPI_Finalize(ierr) |
| 61 |
stop |
| 62 |
endif |
| 63 |
endif |
| 64 |
|
| 65 |
end subroutine mpiini |
| 66 |
|
| 67 |
integer function mpiiam() |
| 68 |
integer ierr |
| 69 |
call MPI_Comm_rank(MPI_COMM_WORLD,mpiiam,ierr) |
| 70 |
end function mpiiam |
| 71 |
|
| 72 |
integer function mpikwt() |
| 73 |
integer ierr |
| 74 |
call MPI_Comm_size(MPI_COMM_WORLD,mpikwt,ierr) |
| 75 |
end function mpikwt |
| 76 |
|
| 77 |
subroutine mpiiap(ap) |
| 78 |
type(autoparameters) :: ap |
| 79 |
|
| 80 |
integer ierr |
| 81 |
|
| 82 |
|
| 83 |
call MPI_Bcast(AUTO_MPI_INIT_MESSAGE,1,MPI_INTEGER,0,MPI_COMM_WORLD,ierr) |
| 84 |
call mpibcastap(ap) |
| 85 |
|
| 86 |
end subroutine mpiiap |
| 87 |
|
| 88 |
logical function mpiwfi() |
| 89 |
integer :: message_type, ierr |
| 90 |
|
| 91 |
mpiwfi = .false. |
| 92 |
|
| 93 |
call MPI_Bcast(message_type,1,MPI_INTEGER,0,MPI_COMM_WORLD,ierr) |
| 94 |
|
| 95 |
select case(message_type) |
| 96 |
case(AUTO_MPI_KILL_MESSAGE) |
| 97 |
call MPI_Finalize(ierr) |
| 98 |
stop |
| 99 |
case(AUTO_MPI_INIT_MESSAGE) |
| 100 |
|
| 101 |
case(AUTO_MPI_SOLVBV_MESSAGE) |
| 102 |
mpiwfi = .true. |
| 103 |
case default |
| 104 |
print *,'Unknown message recieved: ', message_type |
| 105 |
end select |
| 106 |
end function mpiwfi |
| 107 |
|
| 108 |
|
| 109 |
subroutine partition(n,kwt,m) |
| 110 |
|
| 111 |
|
| 112 |
integer n,kwt,m(kwt) |
| 113 |
integer i |
| 114 |
do i=1,kwt |
| 115 |
m(i) = (i*n+kwt-1)/kwt - ((i-1)*n+kwt-1)/kwt |
| 116 |
enddo |
| 117 |
end subroutine partition |
| 118 |
|
| 119 |
subroutine mpireduce(a1,a2,bb,cc,c2,dd,faa,fcfc,ntst,nov,ncb,nrc,ifst,nllv,& |
| 120 |
lo,hi,level) |
| 121 |
integer, intent(in) :: ntst,nov,ncb,nrc,ifst,nllv,lo,hi,level |
| 122 |
double precision, intent(inout) :: a1(nov,nov,*),a2(nov,nov,*),bb(ncb,nov,*) |
| 123 |
double precision, intent(inout) :: cc(nov,nrc,*),c2(nov,nrc,*) |
| 124 |
double precision, intent(inout) :: dd(ncb,nrc,*),faa(nov,*) |
| 125 |
double precision, intent(inout) :: fcfc(nrc,*) |
| 126 |
|
| 127 |
integer :: iam,kwt,mid,nlo,nmid1,hi1,mid1,base,na |
| 128 |
|
| 129 |
iam=mpiiam() |
| 130 |
kwt=mpikwt() |
| 131 |
mid=(lo+hi)/2 |
| 132 |
nlo=(lo-1)*kwt/ntst |
| 133 |
nmid1=mid*kwt/ntst |
| 134 |
base=(iam*ntst+kwt-1)/kwt |
| 135 |
na=((iam+1)*ntst+kwt-1)/kwt-base |
| 136 |
mid1=mid-base |
| 137 |
if(mid1>na)then |
| 138 |
mid1=na+level+1 |
| 139 |
endif |
| 140 |
hi1=hi-base |
| 141 |
if(hi1>na)then |
| 142 |
hi1=na+level |
| 143 |
endif |
| 144 |
if(nmid1==iam.and.nlo<iam)then |
| 145 |
if(nllv.eq.0)then |
| 146 |
call mpisend(faa(1,hi1),nov,nlo) |
| 147 |
call mpisend(fcfc(1,hi1),nrc,nlo) |
| 148 |
endif |
| 149 |
if(ifst.eq.1)then |
| 150 |
|
| 151 |
call mpisend(a1(1,1,mid1+1),nov*nov,nlo) |
| 152 |
call mpisend(a2(1,1,hi1),nov*nov,nlo) |
| 153 |
call mpisend(bb(1,1,hi1),nov*ncb,nlo) |
| 154 |
call mpisend(cc(1,1,mid1+1),nov*nrc,nlo) |
| 155 |
call mpisend(c2(1,1,hi1),nov*nrc,nlo) |
| 156 |
call mpisend(dd(1,1,hi1),ncb*nrc,nlo) |
| 157 |
endif |
| 158 |
elseif(nmid1>iam.and.nlo==iam)then |
| 159 |
if(nllv.eq.0)then |
| 160 |
call mpirecv(faa(1,hi1),nov,nmid1) |
| 161 |
call mpirecv(fcfc(1,hi1),nrc,nmid1) |
| 162 |
else |
| 163 |
faa(:,hi1)=0 |
| 164 |
fcfc(:,hi1)=0 |
| 165 |
endif |
| 166 |
if(ifst.eq.1)then |
| 167 |
|
| 168 |
call mpirecv(a1(1,1,mid1+1),nov*nov,nmid1) |
| 169 |
call mpirecv(a2(1,1,hi1),nov*nov,nmid1) |
| 170 |
call mpirecv(bb(1,1,hi1),nov*ncb,nmid1) |
| 171 |
call mpirecv(cc(1,1,mid1+1),nov*nrc,nmid1) |
| 172 |
call mpirecv(c2(1,1,hi1),nov*nrc,nmid1) |
| 173 |
call mpirecv(dd(1,1,hi1),ncb*nrc,nmid1) |
| 174 |
endif |
| 175 |
endif |
| 176 |
end subroutine mpireduce |
| 177 |
|
| 178 |
subroutine mpibcksub(sol,fc,ntst,nov,ncb,lo,hi,level) |
| 179 |
integer, intent(in) :: ntst,nov,ncb,lo,hi,level |
| 180 |
double precision, intent(inout) :: sol(nov,*),fc(*) |
| 181 |
integer :: iam,kwt,mid,nlo,nmid1,hi1,mid1,base,na |
| 182 |
|
| 183 |
iam=mpiiam() |
| 184 |
kwt=mpikwt() |
| 185 |
mid=(lo+hi)/2 |
| 186 |
nlo=(lo-1)*kwt/ntst |
| 187 |
nmid1=mid*kwt/ntst |
| 188 |
base=(iam*ntst+kwt-1)/kwt |
| 189 |
na=((iam+1)*ntst+kwt-1)/kwt-base |
| 190 |
mid1=mid-base |
| 191 |
if(mid1>na)then |
| 192 |
mid1=na+level+1 |
| 193 |
endif |
| 194 |
hi1=hi-base |
| 195 |
if(hi1>na)then |
| 196 |
hi1=na+level |
| 197 |
endif |
| 198 |
if(nmid1==iam.and.nlo<iam)then |
| 199 |
call mpirecv(sol(1,mid1+1),nov,nlo) |
| 200 |
call mpirecv(sol(1,hi1+1),nov,nlo) |
| 201 |
call mpirecv(fc,nov+ncb,nlo) |
| 202 |
elseif(nmid1>iam.and.nlo==iam)then |
| 203 |
call mpisend(sol(1,mid1+1),nov,nmid1) |
| 204 |
call mpisend(sol(1,hi1+1),nov,nmid1) |
| 205 |
call mpisend(fc,nov+ncb,nmid1) |
| 206 |
endif |
| 207 |
end subroutine mpibcksub |
| 208 |
|
| 209 |
subroutine mpirecv(a,isize,isrc) |
| 210 |
integer, intent(in) :: isize, isrc |
| 211 |
double precision, intent(out) :: a(isize) |
| 212 |
|
| 213 |
integer ierr |
| 214 |
integer status(MPI_STATUS_SIZE) |
| 215 |
call MPI_Recv(a,isize,MPI_DOUBLE_PRECISION,isrc, & |
| 216 |
MPI_ANY_TAG, MPI_COMM_WORLD, status, ierr) |
| 217 |
end subroutine mpirecv |
| 218 |
|
| 219 |
subroutine mpisend(a,isize,idest) |
| 220 |
integer, intent(in) :: isize, idest |
| 221 |
double precision, intent(in) :: a(isize) |
| 222 |
|
| 223 |
integer ierr |
| 224 |
call MPI_Send(a,isize,MPI_DOUBLE_PRECISION,idest, & |
| 225 |
0, MPI_COMM_WORLD, ierr) |
| 226 |
end subroutine mpisend |
| 227 |
|
| 228 |
subroutine mpisbv(solvbv) |
| 229 |
|
| 230 |
logical, intent(in) :: solvbv |
| 231 |
|
| 232 |
integer :: ierr |
| 233 |
integer :: message |
| 234 |
|
| 235 |
|
| 236 |
if(solvbv)then |
| 237 |
message=AUTO_MPI_SOLVBV_MESSAGE |
| 238 |
else |
| 239 |
message=AUTO_MPI_INIT_MESSAGE |
| 240 |
endif |
| 241 |
call MPI_Bcast(message,1,MPI_INTEGER,0, & |
| 242 |
MPI_COMM_WORLD,ierr) |
| 243 |
|
| 244 |
end subroutine mpisbv |
| 245 |
|
| 246 |
subroutine mpicbv(npar,par,rds,ss) |
| 247 |
|
| 248 |
integer, intent(in) :: npar |
| 249 |
double precision :: par(npar) |
| 250 |
double precision :: rds,ss |
| 251 |
|
| 252 |
integer :: iam,bufsize |
| 253 |
double precision, allocatable :: buffer(:) |
| 254 |
|
| 255 |
iam=mpiiam() |
| 256 |
bufsize = npar+2 |
| 257 |
allocate(buffer(bufsize)) |
| 258 |
|
| 259 |
if(iam==0)then |
| 260 |
buffer(1:npar)=par(:) |
| 261 |
buffer(npar+1)=rds |
| 262 |
buffer(npar+2)=ss |
| 263 |
endif |
| 264 |
|
| 265 |
call mpibcast(buffer,bufsize) |
| 266 |
|
| 267 |
if(iam>0)then |
| 268 |
par(1:npar)=buffer(1:npar) |
| 269 |
rds=buffer(npar+1) |
| 270 |
ss=buffer(npar+2) |
| 271 |
endif |
| 272 |
|
| 273 |
deallocate(buffer) |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
end subroutine mpicbv |
| 278 |
|
| 279 |
subroutine mpibcast(buf,len) |
| 280 |
integer, intent(in) :: len |
| 281 |
double precision, intent(inout) :: buf(len) |
| 282 |
|
| 283 |
integer :: ierr |
| 284 |
|
| 285 |
call MPI_Bcast(buf,len,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr) |
| 286 |
end subroutine mpibcast |
| 287 |
|
| 288 |
subroutine mpibcasti(buf,len) |
| 289 |
integer, intent(in) :: len |
| 290 |
integer, intent(inout) :: buf(len) |
| 291 |
|
| 292 |
integer :: ierr |
| 293 |
|
| 294 |
call MPI_Bcast(buf,len,MPI_INTEGER,0,MPI_COMM_WORLD,ierr) |
| 295 |
end subroutine mpibcasti |
| 296 |
|
| 297 |
subroutine mpibcast1i(buf) |
| 298 |
integer, intent(inout) :: buf |
| 299 |
|
| 300 |
integer :: ierr |
| 301 |
|
| 302 |
call MPI_Bcast(buf,1,MPI_INTEGER,0,MPI_COMM_WORLD,ierr) |
| 303 |
end subroutine mpibcast1i |
| 304 |
|
| 305 |
subroutine mpibcast1l(buf) |
| 306 |
logical, intent(inout) :: buf |
| 307 |
|
| 308 |
integer :: ierr |
| 309 |
|
| 310 |
call MPI_Bcast(buf,1,MPI_LOGICAL,0,MPI_COMM_WORLD,ierr) |
| 311 |
end subroutine mpibcast1l |
| 312 |
|
| 313 |
subroutine mpibcastap(ap) |
| 314 |
type(autoparameters), intent(inout) :: ap |
| 315 |
|
| 316 |
integer :: ierr |
| 317 |
|
| 318 |
call MPI_Bcast(ap%ndim,NIAP,MPI_INTEGER,0,MPI_COMM_WORLD,ierr) |
| 319 |
call MPI_Bcast(ap%ds,NRAP,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr) |
| 320 |
|
| 321 |
end subroutine mpibcastap |
| 322 |
|
| 323 |
subroutine mpicounts(np,kwt,ndx,add,counts,displacements) |
| 324 |
integer, intent(in) :: np(kwt), kwt, ndx, add |
| 325 |
integer, intent(out) :: counts(kwt), displacements(kwt) |
| 326 |
|
| 327 |
integer i, loop_start |
| 328 |
|
| 329 |
loop_start = 0 |
| 330 |
do i=1,kwt |
| 331 |
counts(i) = ndx*np(i)+add |
| 332 |
displacements(i) = ndx*loop_start |
| 333 |
loop_start = loop_start + np(i) |
| 334 |
enddo |
| 335 |
|
| 336 |
end subroutine mpicounts |
| 337 |
|
| 338 |
subroutine mpiscat(buf,ndx,n,add) |
| 339 |
integer, intent(in) :: ndx,n,add |
| 340 |
double precision, intent(inout) :: buf(*) |
| 341 |
|
| 342 |
integer, allocatable :: counts(:), displacements(:), np(:) |
| 343 |
integer ierr, iam, kwt, na0 |
| 344 |
|
| 345 |
call MPI_Comm_size(MPI_COMM_WORLD,kwt,ierr) |
| 346 |
allocate(np(kwt)) |
| 347 |
call partition(n,kwt,np) |
| 348 |
|
| 349 |
call MPI_Comm_rank(MPI_COMM_WORLD,iam,ierr) |
| 350 |
if(iam==0)then |
| 351 |
allocate(counts(kwt),displacements(kwt)) |
| 352 |
call mpicounts(np,kwt,ndx,add,counts,displacements) |
| 353 |
call MPI_Scatterv(buf,counts,displacements,MPI_DOUBLE_PRECISION, & |
| 354 |
MPI_IN_PLACE,0,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr) |
| 355 |
deallocate(counts,displacements) |
| 356 |
else |
| 357 |
na0=np(iam+1)*ndx+add |
| 358 |
call MPI_Scatterv(buf,counts,displacements,MPI_DOUBLE_PRECISION, & |
| 359 |
buf,na0,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr) |
| 360 |
endif |
| 361 |
|
| 362 |
deallocate(np) |
| 363 |
end subroutine mpiscat |
| 364 |
|
| 365 |
subroutine mpigat(buf,ndx,n) |
| 366 |
integer, intent(in) :: ndx,n |
| 367 |
double precision, intent(inout) :: buf(ndx,*) |
| 368 |
|
| 369 |
integer, allocatable :: counts(:), displacements(:), np(:) |
| 370 |
integer ierr, iam, kwt, na0 |
| 371 |
|
| 372 |
call MPI_Comm_size(MPI_COMM_WORLD,kwt,ierr) |
| 373 |
allocate(np(kwt)) |
| 374 |
call partition(n,kwt,np) |
| 375 |
|
| 376 |
call MPI_Comm_rank(MPI_COMM_WORLD,iam,ierr) |
| 377 |
if(iam==0)then |
| 378 |
allocate(counts(kwt),displacements(kwt)) |
| 379 |
call mpicounts(np,kwt,ndx,0,counts,displacements) |
| 380 |
call MPI_Gatherv(MPI_IN_PLACE,0,MPI_DOUBLE_PRECISION, & |
| 381 |
buf,counts,displacements,MPI_DOUBLE_PRECISION, & |
| 382 |
0,MPI_COMM_WORLD,ierr) |
| 383 |
deallocate(counts,displacements) |
| 384 |
else |
| 385 |
na0=np(iam+1)*ndx |
| 386 |
call MPI_Gatherv(buf,na0,MPI_DOUBLE_PRECISION, & |
| 387 |
buf,counts,displacements,MPI_DOUBLE_PRECISION, & |
| 388 |
0,MPI_COMM_WORLD,ierr) |
| 389 |
endif |
| 390 |
|
| 391 |
deallocate(np) |
| 392 |
end subroutine mpigat |
| 393 |
|
| 394 |
subroutine mpigats(buf,ndx,n,linelen) |
| 395 |
|
| 396 |
integer, intent(in) :: ndx,n,linelen |
| 397 |
character(linelen), intent(inout) :: buf(*) |
| 398 |
|
| 399 |
integer, allocatable :: counts(:), displacements(:), np(:) |
| 400 |
integer ierr, iam, kwt, na0, line |
| 401 |
|
| 402 |
call MPI_Comm_size(MPI_COMM_WORLD,kwt,ierr) |
| 403 |
allocate(np(kwt)) |
| 404 |
call partition(n,kwt,np) |
| 405 |
|
| 406 |
call MPI_Comm_rank(MPI_COMM_WORLD,iam,ierr) |
| 407 |
call MPI_Type_contiguous(linelen,MPI_CHARACTER,line,ierr) |
| 408 |
call MPI_Type_commit(line,ierr) |
| 409 |
if(iam==0)then |
| 410 |
allocate(counts(kwt),displacements(kwt)) |
| 411 |
call mpicounts(np,kwt,ndx,0,counts,displacements) |
| 412 |
call MPI_Gatherv(MPI_IN_PLACE,0,line,buf,counts,displacements, & |
| 413 |
line,0,MPI_COMM_WORLD,ierr) |
| 414 |
deallocate(counts,displacements) |
| 415 |
else |
| 416 |
na0=np(iam+1)*ndx |
| 417 |
call MPI_Gatherv(buf,na0,line,buf,counts,displacements, & |
| 418 |
line,0,MPI_COMM_WORLD,ierr) |
| 419 |
endif |
| 420 |
call MPI_Type_free(line,ierr) |
| 421 |
|
| 422 |
deallocate(np) |
| 423 |
end subroutine mpigats |
| 424 |
|
| 425 |
subroutine mpireducemax(buf,n) |
| 426 |
integer, intent(in) :: n |
| 427 |
double precision, intent(inout) :: buf(n) |
| 428 |
|
| 429 |
integer ierr |
| 430 |
|
| 431 |
if (mpiiam()==0)then |
| 432 |
call MPI_Reduce(MPI_IN_PLACE, buf, n, MPI_DOUBLE_PRECISION, & |
| 433 |
MPI_MAX, 0, MPI_COMM_WORLD, ierr) |
| 434 |
else |
| 435 |
call MPI_Reduce(buf, buf, n, MPI_DOUBLE_PRECISION, & |
| 436 |
MPI_MAX, 0, MPI_COMM_WORLD, ierr) |
| 437 |
endif |
| 438 |
end subroutine mpireducemax |
| 439 |
|
| 440 |
subroutine mpireducemin(buf,n) |
| 441 |
integer, intent(in) :: n |
| 442 |
double precision, intent(inout) :: buf(n) |
| 443 |
|
| 444 |
integer ierr |
| 445 |
|
| 446 |
if (mpiiam()==0)then |
| 447 |
call MPI_Reduce(MPI_IN_PLACE, buf, n, MPI_DOUBLE_PRECISION, & |
| 448 |
MPI_MIN, 0, MPI_COMM_WORLD, ierr) |
| 449 |
else |
| 450 |
call MPI_Reduce(buf, buf, n, MPI_DOUBLE_PRECISION, & |
| 451 |
MPI_MIN, 0, MPI_COMM_WORLD, ierr) |
| 452 |
endif |
| 453 |
end subroutine mpireducemin |
| 454 |
|
| 455 |
subroutine mpiend() |
| 456 |
integer ierr |
| 457 |
|
| 458 |
call MPI_Bcast(AUTO_MPI_KILL_MESSAGE,1,MPI_INTEGER,0,MPI_COMM_WORLD,ierr) |
| 459 |
|
| 460 |
call MPI_Finalize(ierr) |
| 461 |
end subroutine mpiend |
| 462 |
|
| 463 |
subroutine mpitim(tim) |
| 464 |
double precision tim |
| 465 |
tim = MPI_Wtime() |
| 466 |
end subroutine mpitim |
| 467 |
|
| 468 |
end module autompi |