Actual source code: isreg.c


  2: #include <petsc/private/isimpl.h>

  4: PetscFunctionList ISList              = NULL;
  5: PetscBool         ISRegisterAllCalled = PETSC_FALSE;

  7: /*@
  8:    ISCreate - Creates an index set object. `IS` are objects used to do efficient indexing into other data structures such as `Vec` and `Mat`

 10:    Collective

 12:    Input Parameters:
 13: .  comm - the MPI communicator

 15:    Output Parameter:
 16: .  is - the new index set

 18:    Notes:
 19:    When the communicator is not `MPI_COMM_SELF`, the operations on `IS` are NOT
 20:    conceptually the same as `MPI_Group` operations. The `IS` are then
 21:    distributed sets of indices and thus certain operations on them are
 22:    collective.

 24:    Level: beginner

 26: .seealso: [](sec_scatter), `IS`, `ISType()`, `ISSetType()`, `ISCreateGeneral()`, `ISCreateStride()`, `ISCreateBlock()`, `ISAllGather()`
 27: @*/
 28: PetscErrorCode ISCreate(MPI_Comm comm, IS *is)
 29: {
 30:   PetscFunctionBegin;
 32:   PetscCall(ISInitializePackage());

 34:   PetscCall(PetscHeaderCreate(*is, IS_CLASSID, "IS", "Index Set", "IS", comm, ISDestroy, ISView));
 35:   PetscCall(PetscLayoutCreate(comm, &(*is)->map));
 36:   PetscFunctionReturn(PETSC_SUCCESS);
 37: }

 39: /*@C
 40:   ISSetType - Builds a index set, for a particular `ISType`

 42:   Collective

 44:   Input Parameters:
 45: + is    - The index set object
 46: - method - The name of the index set type

 48:   Options Database Key:
 49: . -is_type <type> - Sets the index set type; use -help for a list of available types

 51:   Notes:
 52:   See "petsc/include/petscis.h" for available istor types (for instance, ISGENERAL, ISSTRIDE, or ISBLOCK).

 54:   Use `ISDuplicate()` to make a duplicate

 56:   Level: intermediate

 58: .seealso: [](sec_scatter), `IS`, `ISGENERAL`, `ISBLOCK`, `ISGetType()`, `ISCreate()`
 59: @*/
 60: PetscErrorCode ISSetType(IS is, ISType method)
 61: {
 62:   PetscErrorCode (*r)(IS);
 63:   PetscBool match;

 65:   PetscFunctionBegin;
 67:   PetscCall(PetscObjectTypeCompare((PetscObject)is, method, &match));
 68:   if (match) PetscFunctionReturn(PETSC_SUCCESS);

 70:   PetscCall(ISRegisterAll());
 71:   PetscCall(PetscFunctionListFind(ISList, method, &r));
 72:   PetscCheck(r, PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown IS type: %s", method);
 73:   PetscTryTypeMethod(is, destroy);
 74:   is->ops->destroy = NULL;

 76:   PetscCall((*r)(is));
 77:   PetscCall(PetscObjectChangeTypeName((PetscObject)is, method));
 78:   PetscFunctionReturn(PETSC_SUCCESS);
 79: }

 81: /*@C
 82:   ISGetType - Gets the index set type name, `ISType`, (as a string) from the `IS`.

 84:   Not Collective

 86:   Input Parameter:
 87: . is  - The index set

 89:   Output Parameter:
 90: . type - The index set type name

 92:   Level: intermediate

 94: .seealso: [](sec_scatter), `IS`, `ISType`, `ISSetType()`, `ISCreate()`
 95: @*/
 96: PetscErrorCode ISGetType(IS is, ISType *type)
 97: {
 98:   PetscFunctionBegin;
101:   if (!ISRegisterAllCalled) PetscCall(ISRegisterAll());
102:   *type = ((PetscObject)is)->type_name;
103:   PetscFunctionReturn(PETSC_SUCCESS);
104: }

106: /*--------------------------------------------------------------------------------------------------------------------*/

108: /*@C
109:   ISRegister - Adds a new index set implementation

111:   Not Collective

113:   Input Parameters:
114: + name        - The name of a new user-defined creation routine
115: - create_func - The creation routine itself

117:   Sample usage:
118: .vb
119:     ISRegister("my_is_name",  MyISCreate);
120: .ve

122:   Then, your vector type can be chosen with the procedural interface via
123: .vb
124:     ISCreate(MPI_Comm, IS *);
125:     ISSetType(IS,"my_is_name");
126: .ve
127:    or at runtime via the option
128: .vb
129:     -is_type my_is_name
130: .ve

132:   Level: developer

134:   Notes:
135:   `ISRegister()` may be called multiple times to add several user-defined vectors

137:   This is no `ISSetFromOptions()` and the current implementations do not have a way to dynamically determine type, so
138:   dynamic registration of custom `IS` types will be of limited use to users.

140: .seealso: [](sec_scatter), `IS`, `ISType`, `ISSetType()`, `ISRegisterAll()`, `ISRegisterDestroy()`, `ISRegister()`
141: @*/
142: PetscErrorCode ISRegister(const char sname[], PetscErrorCode (*function)(IS))
143: {
144:   PetscFunctionBegin;
145:   PetscCall(ISInitializePackage());
146:   PetscCall(PetscFunctionListAdd(&ISList, sname, function));
147:   PetscFunctionReturn(PETSC_SUCCESS);
148: }