1 #include <petscsys.h>
2 #include <petscmatelemental.h>
3 #include <petsc/private/petscimpl.h>
4 
5 /*@
6    PetscElementalInitializePackage - Initialize Elemental package
7 
8    Collective on MPI_COMM_WORLD, not PETSC_COMM_WORLD
9 
10    Level: developer
11 
12    Note:
13    Can be called outside of PetscInitialize() and PetscFinalize().
14    If called outside of these functions, it is the user's responsability
15    to make sure that PETSC_COMM_WORLD is either unset (default value is MPI_COMM_NULL),
16    or that it is not MPI_UNEQUAL to MPI_COMM_WORLD.
17    Users who do not have a custom PETSC_COMM_WORLD do not have to call this function.
18 
19 .seealso: MATELEMENTAL, PetscElementalFinalizePackage()
20 @*/
PetscElementalInitializePackage(void)21 PetscErrorCode PetscElementalInitializePackage(void)
22 {
23   PetscMPIInt    result;
24   PetscErrorCode ierr;
25 
26   if (El::Initialized()) return 0;
27   if (PETSC_COMM_WORLD != MPI_COMM_NULL) { /* MPI has been initialized and PETSC_COMM_WORLD has been set */
28     ierr = MPI_Comm_compare(PETSC_COMM_WORLD,MPI_COMM_WORLD,&result);if (ierr) return ierr;
29     if (result == MPI_UNEQUAL) return result; /* cannot use Elemental with PETSC_COMM_WORLD and MPI_COMM_WORLD comparing to MPI_UNEQUAL, call PetscElementalInitializePackage()/PetscElementalFinalizePackage() collectively */
30   }
31   El::Initialize(); /* called by PetscInitialize_DynamicLibraries(void) or users */
32   if (PetscInitializeCalled) { /* true if MPI is initialized by PETSc, false if MPI has been initialized outside and thus PETSC_COMM_WORLD can't be set to something else than MPI_COMM_NULL, see src/sys/objects/pinit.c */
33     ierr = PetscRegisterFinalize(PetscElementalFinalizePackage);if (ierr) return ierr;
34   }
35   return 0;
36 }
37 
38 /*@
39    PetscElementalInitialized - Determine whether Elemental is initialized
40 
41    Not Collective
42 
43    Level: developer
44 
45    Note:
46    Can be called outside of PetscInitialize() and PetscFinalize().
47 
48 .seealso: MATELEMENTAL, PetscElementalInitializePackage()
49 @*/
PetscElementalInitialized(PetscBool * isInitialized)50 PetscErrorCode PetscElementalInitialized(PetscBool *isInitialized)
51 {
52   if (isInitialized) *isInitialized = (PetscBool)El::Initialized();
53   return 0;
54 }
55 
56 /*@
57    PetscElementalFinalizePackage - Finalize Elemental package
58 
59    Collective on MPI_COMM_WORLD, not PETSC_COMM_WORLD
60 
61    Level: developer
62 
63    Note:
64    Can be called outside of PetscInitialize() and PetscFinalize().
65    Users who do not call PetscElementalInitializePackage() do not have to call this function.
66 
67 .seealso: MATELEMENTAL, PetscElementalInitializePackage()
68 @*/
PetscElementalFinalizePackage(void)69 PetscErrorCode PetscElementalFinalizePackage(void)
70 {
71   if (El::Initialized()) El::Finalize();
72   return 0;
73 }
74