xref: /petsc/src/sys/utils/pbarrier.c (revision b2566f29c2b6470df769aa9f7deb9e2726b0959e)
1e5c89e4eSSatish Balay 
2af0996ceSBarry Smith #include <petsc/private/petscimpl.h>              /*I "petscsys.h" I*/
3e5c89e4eSSatish Balay 
4e5c89e4eSSatish Balay /* Logging support */
597486a04SSatish Balay PetscLogEvent PETSC_Barrier=0;
6e5c89e4eSSatish Balay 
7*b2566f29SBarry Smith static int hash(const char *str)
8*b2566f29SBarry Smith {
9*b2566f29SBarry Smith   int c,hash = 5381;
10*b2566f29SBarry Smith 
11*b2566f29SBarry Smith   while ((c = *str++)) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
12*b2566f29SBarry Smith   return hash;
13*b2566f29SBarry Smith }
14*b2566f29SBarry Smith 
15*b2566f29SBarry Smith PetscErrorCode PetscAllreduceBarrierCheck(MPI_Comm comm,PetscMPIInt ctn,int line,const char *func,const char *file)
16*b2566f29SBarry Smith {
17*b2566f29SBarry Smith   PetscMPIInt err;
18*b2566f29SBarry Smith   PetscMPIInt b1[2],b2[2];
19*b2566f29SBarry Smith   b1[0] = -(PetscMPIInt)line; b1[1] = (PetscMPIInt)line;
20*b2566f29SBarry Smith   err = MPI_Allreduce(b1,b2,2,MPI_INT,MPI_MAX,comm);
21*b2566f29SBarry Smith   if (err) {
22*b2566f29SBarry Smith     return PetscError(PETSC_COMM_SELF,line,func,file,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL,"MPI_Allreduced() failed");
23*b2566f29SBarry Smith   }
24*b2566f29SBarry Smith   if (-b2[0] != b2[1]) {
25*b2566f29SBarry Smith     return PetscError(PETSC_COMM_SELF,line,func,file,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL,"MPI_Allreduced() called in different locations on different processors");
26*b2566f29SBarry Smith   }
27*b2566f29SBarry Smith   b1[0] = -(PetscMPIInt)hash(func); b1[1] = -b1[0];
28*b2566f29SBarry Smith   err = MPI_Allreduce(b1,b2,2,MPI_INT,MPI_MAX,comm);
29*b2566f29SBarry Smith   if (err) {
30*b2566f29SBarry Smith     return PetscError(PETSC_COMM_SELF,line,func,file,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL,"MPI_Allreduced() failed");
31*b2566f29SBarry Smith   }
32*b2566f29SBarry Smith   if (-b2[0] != b2[1]) {
33*b2566f29SBarry Smith     return PetscError(PETSC_COMM_SELF,line,func,file,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL,"MPI_Allreduced() called in different locations on different processors");
34*b2566f29SBarry Smith   }
35*b2566f29SBarry Smith   b1[0] = -(PetscMPIInt)ctn; b1[1] = (PetscMPIInt)ctn;
36*b2566f29SBarry Smith   err = MPI_Allreduce(b1,b2,2,MPI_INT,MPI_MAX,comm);
37*b2566f29SBarry Smith   if (err) {
38*b2566f29SBarry Smith     return PetscError(PETSC_COMM_SELF,line,func,file,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL,"MPI_Allreduced() failed");
39*b2566f29SBarry Smith   }
40*b2566f29SBarry Smith   if (-b2[0] != b2[1]) {
41*b2566f29SBarry Smith     return PetscError(PETSC_COMM_SELF,line,func,file,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL,"MPI_Allreduced() called with different counts %d on different processors",ctn);
42*b2566f29SBarry Smith   }
43*b2566f29SBarry Smith   return 0;
44*b2566f29SBarry Smith }
45*b2566f29SBarry Smith 
46e5c89e4eSSatish Balay #undef __FUNCT__
47e5c89e4eSSatish Balay #define __FUNCT__ "PetscBarrier"
485075c446SSatish Balay /*@C
49e5c89e4eSSatish Balay     PetscBarrier - Blocks until this routine is executed by all
50e5c89e4eSSatish Balay                    processors owning the object A.
51e5c89e4eSSatish Balay 
52e5c89e4eSSatish Balay    Input Parameters:
53e5c89e4eSSatish Balay .  A - PETSc object  (Mat, Vec, IS, SNES etc...)
540298fd71SBarry Smith         Must be caste with a (PetscObject), can use NULL (for MPI_COMM_WORLD)
55e5c89e4eSSatish Balay 
56e5c89e4eSSatish Balay   Level: intermediate
57e5c89e4eSSatish Balay 
58e5c89e4eSSatish Balay   Notes:
59e5c89e4eSSatish Balay   This routine calls MPI_Barrier with the communicator of the PETSc Object "A".
60e5c89e4eSSatish Balay 
610298fd71SBarry Smith   With fortran Use NULL_OBJECT (instead of NULL)
625075c446SSatish Balay 
63e5c89e4eSSatish Balay    Concepts: barrier
64e5c89e4eSSatish Balay 
65e5c89e4eSSatish Balay @*/
667087cfbeSBarry Smith PetscErrorCode  PetscBarrier(PetscObject obj)
67e5c89e4eSSatish Balay {
68e5c89e4eSSatish Balay   PetscErrorCode ierr;
69e5c89e4eSSatish Balay   MPI_Comm       comm;
70e5c89e4eSSatish Balay 
71e5c89e4eSSatish Balay   PetscFunctionBegin;
72e5c89e4eSSatish Balay   if (obj) PetscValidHeader(obj,1);
73e5c89e4eSSatish Balay   ierr = PetscLogEventBegin(PETSC_Barrier,obj,0,0,0);CHKERRQ(ierr);
74e5c89e4eSSatish Balay   if (obj) {
75e5c89e4eSSatish Balay     ierr = PetscObjectGetComm(obj,&comm);CHKERRQ(ierr);
76a297a907SKarl Rupp   } else comm = PETSC_COMM_WORLD;
77e5c89e4eSSatish Balay   ierr = MPI_Barrier(comm);CHKERRQ(ierr);
78e5c89e4eSSatish Balay   ierr = PetscLogEventEnd(PETSC_Barrier,obj,0,0,0);CHKERRQ(ierr);
79e5c89e4eSSatish Balay   PetscFunctionReturn(0);
80e5c89e4eSSatish Balay }
81e5c89e4eSSatish Balay 
82