xref: /petsc/src/vec/is/utils/isltog.h (revision 9dd11ecf0918283bb567d8b33a92f53ac4ea7840)
1413f72f0SBarry Smith /*
2413f72f0SBarry Smith      This is a terrible way of doing "templates" in C.
3413f72f0SBarry Smith */
4d6a2e6abSJacob Faibussowitsch #define PETSCMAPNAME(a) PetscConcat(a, GTOLNAME)
5d6a2e6abSJacob Faibussowitsch #define PETSCMAPTYPE(a) PetscConcat(a, GTOLTYPE)
6413f72f0SBarry Smith 
ISGlobalToLocalMappingApply(ISLocalToGlobalMapping mapping,ISGlobalToLocalMappingMode type,PetscInt n,const PetscInt idx[],PetscInt * nout,PetscInt idxout[])7d71ae5a4SJacob Faibussowitsch static PetscErrorCode PETSCMAPNAME(ISGlobalToLocalMappingApply)(ISLocalToGlobalMapping mapping, ISGlobalToLocalMappingMode type, PetscInt n, const PetscInt idx[], PetscInt *nout, PetscInt idxout[])
8d71ae5a4SJacob Faibussowitsch {
9413f72f0SBarry Smith   PetscInt i, nf = 0, tmp, start, end, bs;
10413f72f0SBarry Smith   PETSCMAPTYPE(ISLocalToGlobalMapping) *map = (PETSCMAPTYPE(ISLocalToGlobalMapping) *)mapping->data;
11413f72f0SBarry Smith 
12413f72f0SBarry Smith   PetscFunctionBegin;
13413f72f0SBarry Smith   PetscValidHeaderSpecific(mapping, IS_LTOGM_CLASSID, 1);
14413f72f0SBarry Smith   if (!map) {
159566063dSJacob Faibussowitsch     PetscCall(ISGlobalToLocalMappingSetUp(mapping));
16413f72f0SBarry Smith     map = (PETSCMAPTYPE(ISLocalToGlobalMapping) *)mapping->data;
17413f72f0SBarry Smith   }
18413f72f0SBarry Smith   start = mapping->globalstart;
19413f72f0SBarry Smith   end   = mapping->globalend;
20541bf97eSAdrian Croucher   bs    = GTOLBS;
21413f72f0SBarry Smith 
22413f72f0SBarry Smith   if (type == IS_GTOLM_MASK) {
23413f72f0SBarry Smith     if (idxout) {
24413f72f0SBarry Smith       for (i = 0; i < n; i++) {
25413f72f0SBarry Smith         if (idx[i] < 0) idxout[i] = idx[i];
26541bf97eSAdrian Croucher         else if (idx[i] < bs * start) idxout[i] = -1;
27541bf97eSAdrian Croucher         else if (idx[i] > bs * (end + 1) - 1) idxout[i] = -1;
28413f72f0SBarry Smith         else GTOL(idx[i], idxout[i]);
29413f72f0SBarry Smith       }
30413f72f0SBarry Smith     }
31413f72f0SBarry Smith     if (nout) *nout = n;
32413f72f0SBarry Smith   } else {
33413f72f0SBarry Smith     if (idxout) {
34413f72f0SBarry Smith       for (i = 0; i < n; i++) {
35413f72f0SBarry Smith         if (idx[i] < 0) continue;
36541bf97eSAdrian Croucher         if (idx[i] < bs * start) continue;
37541bf97eSAdrian Croucher         if (idx[i] > bs * (end + 1) - 1) continue;
38413f72f0SBarry Smith         GTOL(idx[i], tmp);
39413f72f0SBarry Smith         if (tmp < 0) continue;
40413f72f0SBarry Smith         idxout[nf++] = tmp;
41413f72f0SBarry Smith       }
42413f72f0SBarry Smith     } else {
43413f72f0SBarry Smith       for (i = 0; i < n; i++) {
44413f72f0SBarry Smith         if (idx[i] < 0) continue;
45541bf97eSAdrian Croucher         if (idx[i] < bs * start) continue;
46541bf97eSAdrian Croucher         if (idx[i] > bs * (end + 1) - 1) continue;
47413f72f0SBarry Smith         GTOL(idx[i], tmp);
48413f72f0SBarry Smith         if (tmp < 0) continue;
49413f72f0SBarry Smith         nf++;
50413f72f0SBarry Smith       }
51413f72f0SBarry Smith     }
52413f72f0SBarry Smith     if (nout) *nout = nf;
53413f72f0SBarry Smith   }
54*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
55413f72f0SBarry Smith }
56413f72f0SBarry Smith 
57413f72f0SBarry Smith #undef PETSCMAPTYPE
58413f72f0SBarry Smith #undef PETSCMAPNAME
59413f72f0SBarry Smith #undef GTOLTYPE
60413f72f0SBarry Smith #undef GTOLNAME
61541bf97eSAdrian Croucher #undef GTOLBS
62413f72f0SBarry Smith #undef GTOL
63