Actual source code: isdiff.c

  2: #include <petscis.h>                    /*I "petscis.h"  I*/
  3: #include <petscbt.h>

  7: /*@
  8:    ISDifference - Computes the difference between two index sets.

 10:    Collective on IS

 12:    Input Parameter:
 13: +  is1 - first index, to have items removed from it
 14: -  is2 - index values to be removed

 16:    Output Parameters:
 17: .  isout - is1 - is2

 19:    Notes:
 20:    Negative values are removed from the lists. is2 may have values
 21:    that are not in is1. This requires O(imax-imin) memory and O(imax-imin)
 22:    work, where imin and imax are the bounds on the indices in is1.

 24:    Level: intermediate

 26:    Concepts: index sets^difference
 27:    Concepts: IS^difference

 29: .seealso: ISDestroy(), ISView(), ISSum(), ISExpand()

 31: @*/
 32: PetscErrorCode  ISDifference(IS is1,IS is2,IS *isout)
 33: {
 35:   PetscInt       i,n1,n2,imin,imax,nout,*iout;
 36:   const PetscInt *i1,*i2;
 37:   PetscBT        mask;
 38:   MPI_Comm       comm;


 45:   ISGetIndices(is1,&i1);
 46:   ISGetLocalSize(is1,&n1);

 48:   /* Create a bit mask array to contain required values */
 49:   if (n1) {
 50:     imin = PETSC_MAX_INT;
 51:     imax = 0;
 52:     for (i=0; i<n1; i++) {
 53:       if (i1[i] < 0) continue;
 54:       imin = PetscMin(imin,i1[i]);
 55:       imax = PetscMax(imax,i1[i]);
 56:     }
 57:   } else {
 58:     imin = imax = 0;
 59:   }
 60:   PetscBTCreate(imax-imin,mask);
 61:   /* Put the values from is1 */
 62:   for (i=0; i<n1; i++) {
 63:     if (i1[i] < 0) continue;
 64:     PetscBTSet(mask,i1[i] - imin);
 65:   }
 66:   ISRestoreIndices(is1,&i1);
 67:   /* Remove the values from is2 */
 68:   ISGetIndices(is2,&i2);
 69:   ISGetLocalSize(is2,&n2);
 70:   for (i=0; i<n2; i++) {
 71:     if (i2[i] < imin || i2[i] > imax) continue;
 72:     PetscBTClear(mask,i2[i] - imin);
 73:   }
 74:   ISRestoreIndices(is2,&i2);
 75: 
 76:   /* Count the number in the difference */
 77:   nout = 0;
 78:   for (i=0; i<imax-imin+1; i++) {
 79:     if (PetscBTLookup(mask,i)) nout++;
 80:   }

 82:   /* create the new IS containing the difference */
 83:   PetscMalloc(nout*sizeof(PetscInt),&iout);
 84:   nout = 0;
 85:   for (i=0; i<imax-imin+1; i++) {
 86:     if (PetscBTLookup(mask,i)) iout[nout++] = i + imin;
 87:   }
 88:   PetscObjectGetComm((PetscObject)is1,&comm);
 89:   ISCreateGeneral(comm,nout,iout,PETSC_OWN_POINTER,isout);

 91:   PetscBTDestroy(mask);
 92:   return(0);
 93: }

 97: /*@
 98:    ISSum - Computes the sum (union) of two index sets.

100:    Only sequential version (at the moment)

102:    Input Parameter:
103: +  is1 - index set to be extended
104: -  is2 - index values to be added

106:    Output Parameter:
107: .   is3 - the sum; this can not be is1 or is2

109:    Notes:
110:    If n1 and n2 are the sizes of the sets, this takes O(n1+n2) time;

112:    Both index sets need to be sorted on input.

114:    Level: intermediate

116: .seealso: ISDestroy(), ISView(), ISDifference(), ISExpand()

118:    Concepts: index sets^union
119:    Concepts: IS^union

121: @*/
122: PetscErrorCode  ISSum(IS is1,IS is2,IS *is3)
123: {
124:   MPI_Comm       comm;
125:   PetscBool      f;
126:   PetscMPIInt    size;
127:   const PetscInt *i1,*i2;
128:   PetscInt       n1,n2,n3, p1,p2, *iout;

134:   PetscObjectGetComm((PetscObject)(is1),&comm);
135:   MPI_Comm_size(comm,&size);
136:   if (size>1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Currently only for uni-processor IS");

138:   ISSorted(is1,&f);
139:   if (!f) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Arg 1 is not sorted");
140:   ISSorted(is2,&f);
141:   if (!f) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Arg 2 is not sorted");

143:   ISGetLocalSize(is1,&n1);
144:   ISGetLocalSize(is2,&n2);
145:   if (!n2) return(0);
146:   ISGetIndices(is1,&i1);
147:   ISGetIndices(is2,&i2);

149:   p1 = 0; p2 = 0; n3 = 0;
150:   do {
151:     if (p1==n1) { /* cleanup for is2 */ n3 += n2-p2; break;
152:     } else {
153:       while (p2<n2 && i2[p2]<i1[p1]) {n3++; p2++;}
154:       if (p2==n2) { /* cleanup for is1 */ n3 += n1-p1; break;
155:       } else {
156:         if (i2[p2]==i1[p1]) {n3++; p1++; p2++;}
157:       }
158:     }
159:     if (p2==n2) { /* cleanup for is1 */ n3 += n1-p1; break;
160:     } else {
161:       while (p1<n1 && i1[p1]<i2[p2]) {n3++; p1++;}
162:       if (p1==n1) { /* clean up for is2 */ n3 += n2-p2; break;
163:       } else {
164:         if (i1[p1]==i2[p2]) {n3++; p1++; p2++;}
165:       }
166:     }
167:   } while (p1<n1 || p2<n2);

169:   if (n3==n1) { /* no new elements to be added */
170:     ISRestoreIndices(is1,&i1);
171:     ISRestoreIndices(is2,&i2);
172:     ISDuplicate(is1,is3);
173:     return(0);
174:   }
175:   PetscMalloc(n3*sizeof(PetscInt),&iout);

177:   p1 = 0; p2 = 0; n3 = 0;
178:   do {
179:     if (p1==n1) { /* cleanup for is2 */
180:       while (p2<n2) iout[n3++] = i2[p2++];
181:       break;
182:     } else {
183:       while (p2<n2 && i2[p2]<i1[p1]) iout[n3++] = i2[p2++];
184:       if (p2==n2) { /* cleanup for is1 */
185:         while (p1<n1) iout[n3++] = i1[p1++];
186:         break;
187:       } else {
188:         if (i2[p2]==i1[p1]) {iout[n3++] = i1[p1++]; p2++;}
189:       }
190:     }
191:     if (p2==n2) { /* cleanup for is1 */
192:       while (p1<n1) iout[n3++] = i1[p1++];
193:       break;
194:     } else {
195:       while (p1<n1 && i1[p1]<i2[p2]) iout[n3++] = i1[p1++];
196:       if (p1==n1) { /* clean up for is2 */
197:         while (p2<n2) iout[n3++] = i2[p2++];
198:         break;
199:       } else {
200:         if (i1[p1]==i2[p2]) {iout[n3++] = i1[p1++]; p2++;}
201:       }
202:     }
203:   } while (p1<n1 || p2<n2);

205:   ISRestoreIndices(is1,&i1);
206:   ISRestoreIndices(is2,&i2);
207:   ISCreateGeneral(comm,n3,iout,PETSC_OWN_POINTER,is3);
208:   return(0);
209: }

213: /*@
214:    ISExpand - Computes the union of two index sets, by concatenating 2 lists and
215:    removing duplicates.

217:    Collective on IS

219:    Input Parameter:
220: +  is1 - first index set
221: -  is2 - index values to be added

223:    Output Parameters:
224: .  isout - is1 + is2 The index set is2 is appended to is1 removing duplicates

226:    Notes:
227:    Negative values are removed from the lists. This requires O(imax-imin) 
228:    memory and O(imax-imin) work, where imin and imax are the bounds on the 
229:    indices in is1 and is2.

231:    The IS's do not need to be sorted.

233:    Level: intermediate

235: .seealso: ISDestroy(), ISView(), ISDifference(), ISSum()

237:    Concepts: index sets^difference
238:    Concepts: IS^difference

240: @*/
241: PetscErrorCode ISExpand(IS is1,IS is2,IS *isout)
242: {
244:   PetscInt       i,n1,n2,imin,imax,nout,*iout;
245:   const PetscInt *i1,*i2;
246:   PetscBT        mask;
247:   MPI_Comm       comm;


254:   ISGetIndices(is1,&i1);
255:   ISGetLocalSize(is1,&n1);
256:   ISGetIndices(is2,&i2);
257:   ISGetLocalSize(is2,&n2);

259:   /* Create a bit mask array to contain required values */
260:   if (n1 || n2) {
261:     imin = PETSC_MAX_INT;
262:     imax = 0;
263:     for (i=0; i<n1; i++) {
264:       if (i1[i] < 0) continue;
265:       imin = PetscMin(imin,i1[i]);
266:       imax = PetscMax(imax,i1[i]);
267:     }
268:     for (i=0; i<n2; i++) {
269:       if (i2[i] < 0) continue;
270:       imin = PetscMin(imin,i2[i]);
271:       imax = PetscMax(imax,i2[i]);
272:     }
273:   } else {
274:     imin = imax = 0;
275:   }
276:   PetscMalloc((n1+n2)*sizeof(PetscInt),&iout);
277:   nout = 0;
278:   PetscBTCreate(imax-imin,mask);
279:   /* Put the values from is1 */
280:   for (i=0; i<n1; i++) {
281:     if (i1[i] < 0) continue;
282:     if (!PetscBTLookupSet(mask,i1[i] - imin)) {
283:       iout[nout++] = i1[i];
284:     }
285:   }
286:   ISRestoreIndices(is1,&i1);
287:   /* Put the values from is2 */
288:   for (i=0; i<n2; i++) {
289:     if (i2[i] < 0) continue;
290:     if (!PetscBTLookupSet(mask,i2[i] - imin)) {
291:       iout[nout++] = i2[i];
292:     }
293:   }
294:   ISRestoreIndices(is2,&i2);

296:   /* create the new IS containing the sum */
297:   PetscObjectGetComm((PetscObject)is1,&comm);
298:   ISCreateGeneral(comm,nout,iout,PETSC_OWN_POINTER,isout);

300:   PetscBTDestroy(mask);
301:   return(0);
302: }