Actual source code: isreg.c

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

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

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

      9:   Collective

     11:   Input Parameter:
     12: . comm - the MPI communicator

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

     17:   Level: beginner

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

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

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

     39: /*@
     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; see `ISType`

     51:   Level: intermediate

     53:   Notes:
     54:   See `ISType` for available types (for instance, `ISGENERAL`, `ISSTRIDE`, or `ISBLOCK`).

     56:   Often convenience constructors such as `ISCreateGeneral()`, `ISCreateStride()` or  `ISCreateBlock()` can be used to construct the desired `IS` in one step

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

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

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

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

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

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

     86:   Not Collective

     88:   Input Parameter:
     89: . is - The index set

     91:   Output Parameter:
     92: . type - The index set type name

     94:   Level: intermediate

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

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

    111:   Not Collective, No Fortran Support

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

    117:   Example 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()`
    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: }