Actual source code: dlregisvec.c


  2: #include <petsc/private/vecimpl.h>
  3: #include <petsc/private/isimpl.h>
  4: #include <petscpf.h>
  5: #include <petscsf.h>
  6: #include <petscsection.h>
  7: #include <petscao.h>

  9: static PetscBool         ISPackageInitialized = PETSC_FALSE;
 10: extern PetscFunctionList ISLocalToGlobalMappingList;
 11: const char              *ISInfos[] = {"SORTED", "UNIQUE", "PERMUTATION", "INTERVAL", "IDENTITY", "ISInfo", "IS_", NULL};

 13: /*@C
 14:   ISFinalizePackage - This function destroys everything in the `IS` package. It is
 15:   called from `PetscFinalize()`.

 17:   Level: developer

 19: .seealso: `PetscFinalize()`
 20: @*/
 21: PetscErrorCode ISFinalizePackage(void)
 22: {
 23:   PetscFunctionBegin;
 24:   PetscCall(PetscFunctionListDestroy(&ISList));
 25:   PetscCall(PetscFunctionListDestroy(&ISLocalToGlobalMappingList));
 26:   PetscCall(PetscFunctionListDestroy(&PetscSectionSymList));
 27:   ISPackageInitialized                    = PETSC_FALSE;
 28:   ISRegisterAllCalled                     = PETSC_FALSE;
 29:   ISLocalToGlobalMappingRegisterAllCalled = PETSC_FALSE;
 30:   PetscFunctionReturn(PETSC_SUCCESS);
 31: }

 33: /*@C
 34:       ISInitializePackage - This function initializes everything in the `IS` package. It is called
 35:   from PetscDLLibraryRegister_petscvec() when using dynamic libraries, and on the first call to ISCreateXXXX()
 36:   when using shared or static libraries.

 38:   Level: developer

 40: .seealso: `PetscInitialize()`
 41: @*/
 42: PetscErrorCode ISInitializePackage(void)
 43: {
 44:   char      logList[256];
 45:   PetscBool opt, pkg;

 47:   PetscFunctionBegin;
 48:   if (ISPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
 49:   ISPackageInitialized = PETSC_TRUE;
 50:   /* Register Classes */
 51:   PetscCall(PetscClassIdRegister("Index Set", &IS_CLASSID));
 52:   PetscCall(PetscClassIdRegister("IS L to G Mapping", &IS_LTOGM_CLASSID));
 53:   PetscCall(PetscClassIdRegister("Section", &PETSC_SECTION_CLASSID));
 54:   PetscCall(PetscClassIdRegister("Section Symmetry", &PETSC_SECTION_SYM_CLASSID));
 55:   /* Register Constructors */
 56:   PetscCall(ISRegisterAll());
 57:   PetscCall(ISLocalToGlobalMappingRegisterAll());
 58:   /* Register Events */
 59:   PetscCall(PetscLogEventRegister("ISView", IS_CLASSID, &IS_View));
 60:   PetscCall(PetscLogEventRegister("ISLoad", IS_CLASSID, &IS_Load));
 61:   /* Process Info */
 62:   {
 63:     PetscClassId classids[4];

 65:     classids[0] = IS_CLASSID;
 66:     classids[1] = IS_LTOGM_CLASSID;
 67:     classids[2] = PETSC_SECTION_CLASSID;
 68:     classids[3] = PETSC_SECTION_SYM_CLASSID;
 69:     PetscCall(PetscInfoProcessClass("is", 2, classids));
 70:     PetscCall(PetscInfoProcessClass("section", 2, &classids[2]));
 71:   }
 72:   /* Process summary exclusions */
 73:   PetscCall(PetscOptionsGetString(NULL, NULL, "-log_exclude", logList, sizeof(logList), &opt));
 74:   if (opt) {
 75:     PetscCall(PetscStrInList("is", logList, ',', &pkg));
 76:     if (pkg) PetscCall(PetscLogEventExcludeClass(IS_CLASSID));
 77:     if (pkg) PetscCall(PetscLogEventExcludeClass(IS_LTOGM_CLASSID));
 78:     PetscCall(PetscStrInList("section", logList, ',', &pkg));
 79:     if (pkg) PetscCall(PetscLogEventExcludeClass(PETSC_SECTION_CLASSID));
 80:     if (pkg) PetscCall(PetscLogEventExcludeClass(PETSC_SECTION_SYM_CLASSID));
 81:   }
 82:   /* Register package finalizer */
 83:   PetscCall(PetscRegisterFinalize(ISFinalizePackage));
 84:   PetscFunctionReturn(PETSC_SUCCESS);
 85: }

 87: extern MPI_Op PetscSplitReduction_Op;

 89: /*
 90:        These two functions are the MPI reduction operation used for max and min with index
 91:    A call to MPI_Op_create() converts the function Vec[Max,Min]_Local() to the MPI operator Vec[Max,Min]_Local_Op.

 93: */
 94: MPI_Op MPIU_MAXLOC = 0;
 95: MPI_Op MPIU_MINLOC = 0;

 97: static void MPIAPI MPIU_MaxIndex_Local(void *in, void *out, PetscMPIInt *cnt, MPI_Datatype *datatype)
 98: {
 99:   struct PetscRealInt {
100:     PetscReal v;
101:     PetscInt  i;
102:   };
103:   struct PetscRealInt *xin  = (struct PetscRealInt *)in;
104:   struct PetscRealInt *xout = (struct PetscRealInt *)out;
105:   int                  c;

107:   PetscFunctionBegin;
108:   if (*datatype != MPIU_REAL_INT) {
109:     PetscCallAbort(MPI_COMM_SELF, (*PetscErrorPrintf)("Can only handle MPIU_REAL_INT data types"));
110:     PETSCABORT(MPI_COMM_SELF, PETSC_ERR_ARG_WRONG);
111:   }
112:   for (c = 0; c < *cnt; c++) {
113:     if (xin[c].v > xout[c].v) {
114:       xout[c].v = xin[c].v;
115:       xout[c].i = xin[c].i;
116:     } else if (xin[c].v == xout[c].v) {
117:       xout[c].i = PetscMin(xin[c].i, xout[c].i);
118:     }
119:   }
120:   PetscFunctionReturnVoid(); /* cannot return a value */
121: }

123: static void MPIAPI MPIU_MinIndex_Local(void *in, void *out, PetscMPIInt *cnt, MPI_Datatype *datatype)
124: {
125:   struct PetscRealInt {
126:     PetscReal v;
127:     PetscInt  i;
128:   };
129:   struct PetscRealInt *xin  = (struct PetscRealInt *)in;
130:   struct PetscRealInt *xout = (struct PetscRealInt *)out;
131:   int                  c;

133:   PetscFunctionBegin;
134:   if (*datatype != MPIU_REAL_INT) {
135:     PetscCallAbort(MPI_COMM_SELF, (*PetscErrorPrintf)("Can only handle MPIU_REAL_INT data types"));
136:     PETSCABORT(MPI_COMM_SELF, PETSC_ERR_ARG_WRONG);
137:   }
138:   for (c = 0; c < *cnt; c++) {
139:     if (xin[c].v < xout[c].v) {
140:       xout[c].v = xin[c].v;
141:       xout[c].i = xin[c].i;
142:     } else if (xin[c].v == xout[c].v) {
143:       xout[c].i = PetscMin(xin[c].i, xout[c].i);
144:     }
145:   }
146:   PetscFunctionReturnVoid(); /* cannot return a value */
147: }

149: PETSC_EXTERN void MPIAPI PetscSplitReduction_Local(void *, void *, PetscMPIInt *, MPI_Datatype *);

151: const char *const NormTypes[] = {"1", "2", "FROBENIUS", "INFINITY", "1_AND_2", "NormType", "NORM_", NULL};
152: PetscInt          NormIds[7]; /* map from NormType to IDs used to cache Normvalues */

154: static PetscBool VecPackageInitialized = PETSC_FALSE;

156: /*@C
157:   VecInitializePackage - This function initializes everything in the `Vec` package. It is called
158:   from PetscDLLibraryRegister_petscvec() when using dynamic libraries, and on the first call to `VecCreate()`
159:   when using shared or static libraries.

161:   Level: developer

163: .seealso: `PetscInitialize()`
164: @*/
165: PetscErrorCode VecInitializePackage(void)
166: {
167:   char      logList[256];
168:   PetscBool opt, pkg;
169:   PetscInt  i;

171:   PetscFunctionBegin;
172:   if (VecPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
173:   VecPackageInitialized = PETSC_TRUE;
174:   /* Register Classes */
175:   PetscCall(PetscClassIdRegister("Vector", &VEC_CLASSID));
176:   /* Register Constructors */
177:   PetscCall(VecRegisterAll());
178:   /* Register Events */
179:   PetscCall(PetscLogEventRegister("VecView", VEC_CLASSID, &VEC_View));
180:   PetscCall(PetscLogEventRegister("VecMax", VEC_CLASSID, &VEC_Max));
181:   PetscCall(PetscLogEventRegister("VecMin", VEC_CLASSID, &VEC_Min));
182:   PetscCall(PetscLogEventRegister("VecDot", VEC_CLASSID, &VEC_Dot));
183:   PetscCall(PetscLogEventRegister("VecDotNorm2", VEC_CLASSID, &VEC_DotNorm2));
184:   PetscCall(PetscLogEventRegister("VecMDot", VEC_CLASSID, &VEC_MDot));
185:   PetscCall(PetscLogEventRegister("VecTDot", VEC_CLASSID, &VEC_TDot));
186:   PetscCall(PetscLogEventRegister("VecMTDot", VEC_CLASSID, &VEC_MTDot));
187:   PetscCall(PetscLogEventRegister("VecNorm", VEC_CLASSID, &VEC_Norm));
188:   PetscCall(PetscLogEventRegister("VecScale", VEC_CLASSID, &VEC_Scale));
189:   PetscCall(PetscLogEventRegister("VecCopy", VEC_CLASSID, &VEC_Copy));
190:   PetscCall(PetscLogEventRegister("VecSet", VEC_CLASSID, &VEC_Set));
191:   PetscCall(PetscLogEventRegister("VecAXPY", VEC_CLASSID, &VEC_AXPY));
192:   PetscCall(PetscLogEventRegister("VecAYPX", VEC_CLASSID, &VEC_AYPX));
193:   PetscCall(PetscLogEventRegister("VecAXPBYCZ", VEC_CLASSID, &VEC_AXPBYPCZ));
194:   PetscCall(PetscLogEventRegister("VecWAXPY", VEC_CLASSID, &VEC_WAXPY));
195:   PetscCall(PetscLogEventRegister("VecMAXPY", VEC_CLASSID, &VEC_MAXPY));
196:   PetscCall(PetscLogEventRegister("VecSwap", VEC_CLASSID, &VEC_Swap));
197:   PetscCall(PetscLogEventRegister("VecOps", VEC_CLASSID, &VEC_Ops));
198:   PetscCall(PetscLogEventRegister("VecAssemblyBegin", VEC_CLASSID, &VEC_AssemblyBegin));
199:   PetscCall(PetscLogEventRegister("VecAssemblyEnd", VEC_CLASSID, &VEC_AssemblyEnd));
200:   PetscCall(PetscLogEventRegister("VecPointwiseMult", VEC_CLASSID, &VEC_PointwiseMult));
201:   PetscCall(PetscLogEventRegister("VecSetValues", VEC_CLASSID, &VEC_SetValues));
202:   PetscCall(PetscLogEventRegister("VecSetPreallCOO", VEC_CLASSID, &VEC_SetPreallocateCOO));
203:   PetscCall(PetscLogEventRegister("VecSetValuesCOO", VEC_CLASSID, &VEC_SetValuesCOO));
204:   PetscCall(PetscLogEventRegister("VecLoad", VEC_CLASSID, &VEC_Load));
205:   PetscCall(PetscLogEventRegister("VecScatterBegin", VEC_CLASSID, &VEC_ScatterBegin));
206:   PetscCall(PetscLogEventRegister("VecScatterEnd", VEC_CLASSID, &VEC_ScatterEnd));
207:   PetscCall(PetscLogEventRegister("VecSetRandom", VEC_CLASSID, &VEC_SetRandom));
208:   PetscCall(PetscLogEventRegister("VecReduceArith", VEC_CLASSID, &VEC_ReduceArithmetic));
209:   PetscCall(PetscLogEventRegister("VecReduceComm", VEC_CLASSID, &VEC_ReduceCommunication));
210:   PetscCall(PetscLogEventRegister("VecReduceBegin", VEC_CLASSID, &VEC_ReduceBegin));
211:   PetscCall(PetscLogEventRegister("VecReduceEnd", VEC_CLASSID, &VEC_ReduceEnd));
212:   PetscCall(PetscLogEventRegister("VecNormalize", VEC_CLASSID, &VEC_Normalize));
213: #if defined(PETSC_HAVE_VIENNACL)
214:   PetscCall(PetscLogEventRegister("VecVCLCopyTo", VEC_CLASSID, &VEC_ViennaCLCopyToGPU));
215:   PetscCall(PetscLogEventRegister("VecVCLCopyFrom", VEC_CLASSID, &VEC_ViennaCLCopyFromGPU));
216: #endif
217: #if defined(PETSC_HAVE_CUDA)
218:   PetscCall(PetscLogEventRegister("VecCUDACopyTo", VEC_CLASSID, &VEC_CUDACopyToGPU));
219:   PetscCall(PetscLogEventRegister("VecCUDACopyFrom", VEC_CLASSID, &VEC_CUDACopyFromGPU));
220: #endif
221: #if defined(PETSC_HAVE_HIP)
222:   PetscCall(PetscLogEventRegister("VecHIPCopyTo", VEC_CLASSID, &VEC_HIPCopyToGPU));
223:   PetscCall(PetscLogEventRegister("VecHIPCopyFrom", VEC_CLASSID, &VEC_HIPCopyFromGPU));
224: #endif

226:   /* Mark non-collective events */
227:   PetscCall(PetscLogEventSetCollective(VEC_SetValues, PETSC_FALSE));
228: #if defined(PETSC_HAVE_VIENNACL)
229:   PetscCall(PetscLogEventSetCollective(VEC_ViennaCLCopyToGPU, PETSC_FALSE));
230:   PetscCall(PetscLogEventSetCollective(VEC_ViennaCLCopyFromGPU, PETSC_FALSE));
231: #endif
232: #if defined(PETSC_HAVE_CUDA)
233:   PetscCall(PetscLogEventSetCollective(VEC_CUDACopyToGPU, PETSC_FALSE));
234:   PetscCall(PetscLogEventSetCollective(VEC_CUDACopyFromGPU, PETSC_FALSE));
235: #endif
236: #if defined(PETSC_HAVE_HIP)
237:   PetscCall(PetscLogEventSetCollective(VEC_HIPCopyToGPU, PETSC_FALSE));
238:   PetscCall(PetscLogEventSetCollective(VEC_HIPCopyFromGPU, PETSC_FALSE));
239: #endif
240:   /* Turn off high traffic events by default */
241:   PetscCall(PetscLogEventSetActiveAll(VEC_SetValues, PETSC_FALSE));
242:   /* Process Info */
243:   {
244:     PetscClassId classids[1];

246:     classids[0] = VEC_CLASSID;
247:     PetscCall(PetscInfoProcessClass("vec", 1, classids));
248:   }
249:   /* Process summary exclusions */
250:   PetscCall(PetscOptionsGetString(NULL, NULL, "-log_exclude", logList, sizeof(logList), &opt));
251:   if (opt) {
252:     PetscCall(PetscStrInList("vec", logList, ',', &pkg));
253:     if (pkg) PetscCall(PetscLogEventExcludeClass(VEC_CLASSID));
254:     if (pkg) PetscCall(PetscLogEventExcludeClass(PETSCSF_CLASSID));
255:   }

257:   /*
258:     Create the special MPI reduction operation that may be used by VecNorm/DotBegin()
259:   */
260:   PetscCallMPI(MPI_Op_create(PetscSplitReduction_Local, 1, &PetscSplitReduction_Op));
261:   PetscCallMPI(MPI_Op_create(MPIU_MaxIndex_Local, 1, &MPIU_MAXLOC));
262:   PetscCallMPI(MPI_Op_create(MPIU_MinIndex_Local, 1, &MPIU_MINLOC));

264:   /* Register the different norm types for cached norms */
265:   for (i = 0; i < 4; i++) PetscCall(PetscObjectComposedDataRegister(NormIds + i));

267:   /* Register package finalizer */
268:   PetscCall(PetscRegisterFinalize(VecFinalizePackage));
269:   PetscFunctionReturn(PETSC_SUCCESS);
270: }

272: /*@C
273:   VecFinalizePackage - This function finalizes everything in the Vec package. It is called
274:   from PetscFinalize().

276:   Level: developer

278: .seealso: `PetscInitialize()`
279: @*/
280: PetscErrorCode VecFinalizePackage(void)
281: {
282:   PetscFunctionBegin;
283:   PetscCall(PetscFunctionListDestroy(&VecList));
284:   PetscCallMPI(MPI_Op_free(&PetscSplitReduction_Op));
285:   PetscCallMPI(MPI_Op_free(&MPIU_MAXLOC));
286:   PetscCallMPI(MPI_Op_free(&MPIU_MINLOC));
287:   if (Petsc_Reduction_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Reduction_keyval));
288:   VecPackageInitialized = PETSC_FALSE;
289:   VecRegisterAllCalled  = PETSC_FALSE;
290:   PetscFunctionReturn(PETSC_SUCCESS);
291: }

293: #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
294: /*
295:   PetscDLLibraryRegister - This function is called when the dynamic library it is in is opened.

297:   This one registers all the methods that are in the PETSc `Vec` library.

299:  */
300: PETSC_EXTERN PetscErrorCode PetscDLLibraryRegister_petscvec(void)
301: {
302:   PetscFunctionBegin;
303:   PetscCall(PetscSFInitializePackage());
304:   PetscCall(ISInitializePackage());
305:   PetscCall(AOInitializePackage());
306:   PetscCall(VecInitializePackage());
307:   PetscCall(PFInitializePackage());
308:   PetscFunctionReturn(PETSC_SUCCESS);
309: }

311: #endif /* PETSC_HAVE_DYNAMIC_LIBRARIES */