xref: /petsc/src/sys/utils/pbarrier.c (revision 95c0884e6f7665b705eebf88174e89dc920c2fc0)
1e5c89e4eSSatish Balay 
2af0996ceSBarry Smith #include <petsc/private/petscimpl.h>              /*I "petscsys.h" I*/
3e5c89e4eSSatish Balay 
4e5c89e4eSSatish Balay /* Logging support */
5*95c0884eSLisandro Dalcin PetscLogEvent PETSC_Barrier;
6e5c89e4eSSatish Balay 
7b2566f29SBarry Smith static int hash(const char *str)
8b2566f29SBarry Smith {
9b2566f29SBarry Smith   int c,hash = 5381;
10b2566f29SBarry Smith 
11b2566f29SBarry Smith   while ((c = *str++)) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
12b2566f29SBarry Smith   return hash;
13b2566f29SBarry Smith }
14b2566f29SBarry Smith 
1577a5e07dSBarry Smith /*
1677a5e07dSBarry Smith    This is used by MPIU_Allreduce() to insure that all callers originated from the same place in the PETSc code
1777a5e07dSBarry Smith */
18b2566f29SBarry Smith PetscErrorCode PetscAllreduceBarrierCheck(MPI_Comm comm,PetscMPIInt ctn,int line,const char *func,const char *file)
19b2566f29SBarry Smith {
20b2566f29SBarry Smith   PetscMPIInt err;
218f5db7efSBarry Smith   PetscMPIInt b1[6],b2[6];
228f5db7efSBarry Smith 
238f5db7efSBarry Smith   b1[0] = -(PetscMPIInt)line;       b1[1] = -b1[0];
248f5db7efSBarry Smith   b1[2] = -(PetscMPIInt)hash(func); b1[3] = -b1[2];
258f5db7efSBarry Smith   b1[4] = -(PetscMPIInt)ctn;        b1[5] = -b1[4];
268f5db7efSBarry Smith   err = MPI_Allreduce(b1,b2,6,MPI_INT,MPI_MAX,comm);
271590dd87SBarry Smith   if (err) return PetscError(PETSC_COMM_SELF,line,func,file,PETSC_ERR_LIB,PETSC_ERROR_INITIAL,"MPI_Allreduce() failed with error code %d",err);
288f5db7efSBarry Smith   if (-b2[0] != b2[1]) return PetscError(PETSC_COMM_SELF,line,func,file,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL,"MPI_Allreduce() called in different locations (code lines) on different processors");
298f5db7efSBarry Smith   if (-b2[2] != b2[3]) return PetscError(PETSC_COMM_SELF,line,func,file,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL,"MPI_Allreduce() called in different locations (functions) on different processors");
308f5db7efSBarry Smith   if (-b2[4] != b2[5]) return PetscError(PETSC_COMM_SELF,line,func,file,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL,"MPI_Allreduce() called with different counts %d on different processors",ctn);
31b2566f29SBarry Smith   return 0;
32b2566f29SBarry Smith }
33b2566f29SBarry Smith 
345075c446SSatish Balay /*@C
35e5c89e4eSSatish Balay     PetscBarrier - Blocks until this routine is executed by all
3610e053e3SBarry Smith                    processors owning the object obj.
37e5c89e4eSSatish Balay 
38e5c89e4eSSatish Balay    Input Parameters:
3910e053e3SBarry Smith .  obj - PETSc object  (Mat, Vec, IS, SNES etc...)
4010e053e3SBarry Smith         The object be caste with a (PetscObject). NULL can be used to indicate the barrier should be across MPI_COMM_WORLD
41e5c89e4eSSatish Balay 
42e5c89e4eSSatish Balay   Level: intermediate
43e5c89e4eSSatish Balay 
44e5c89e4eSSatish Balay   Notes:
4510e053e3SBarry Smith   This routine calls MPI_Barrier with the communicator of the PETSc Object obj
4610e053e3SBarry Smith 
4710e053e3SBarry Smith   Fortran Usage:
4810e053e3SBarry Smith     You may pass PETSC_NULL_VEC or any other PETSc null object, such as PETSC_NULL_MAT, to indicate the barrier should be
4910e053e3SBarry Smith     across MPI_COMM_WORLD.
50e5c89e4eSSatish Balay 
51e5c89e4eSSatish Balay    Concepts: barrier
52e5c89e4eSSatish Balay 
53e5c89e4eSSatish Balay @*/
547087cfbeSBarry Smith PetscErrorCode  PetscBarrier(PetscObject obj)
55e5c89e4eSSatish Balay {
56e5c89e4eSSatish Balay   PetscErrorCode ierr;
57e5c89e4eSSatish Balay   MPI_Comm       comm;
58e5c89e4eSSatish Balay 
59e5c89e4eSSatish Balay   PetscFunctionBegin;
60e5c89e4eSSatish Balay   if (obj) PetscValidHeader(obj,1);
61e5c89e4eSSatish Balay   ierr = PetscLogEventBegin(PETSC_Barrier,obj,0,0,0);CHKERRQ(ierr);
62e5c89e4eSSatish Balay   if (obj) {
63e5c89e4eSSatish Balay     ierr = PetscObjectGetComm(obj,&comm);CHKERRQ(ierr);
64a297a907SKarl Rupp   } else comm = PETSC_COMM_WORLD;
65e5c89e4eSSatish Balay   ierr = MPI_Barrier(comm);CHKERRQ(ierr);
66e5c89e4eSSatish Balay   ierr = PetscLogEventEnd(PETSC_Barrier,obj,0,0,0);CHKERRQ(ierr);
67e5c89e4eSSatish Balay   PetscFunctionReturn(0);
68e5c89e4eSSatish Balay }
69e5c89e4eSSatish Balay 
70