Actual source code: aoreduced.c
1: /*$Id: aoreduced.c,v 1.28 2001/03/23 23:24:52 balay Exp $*/
3: #include src/dm/ao/aoimpl.h
4: #include petscsys.h
5: #include petscbt.h
7: int AODataSegmentGetReduced_Basic(AOData ao,char *name,char *segname,int n,int *keys,IS *is)
8: {
9: AODataSegment *segment;
10: AODataKey *key;
11: int ierr,dsize,i,bs,*found,count,imin,imax,*out;
12: char *idata,*odata;
13: PetscBT mask;
14: PetscTruth flag;
17: /* find the correct segment */
18: AODataSegmentFind_Private(ao,name,segname,&flag,&key,&segment);
19: if (!flag) SETERRQ(PETSC_ERR_ARG_WRONG,"Cannot locate segment");
21: if (segment->datatype != PETSC_INT) SETERRQ(PETSC_ERR_ARG_WRONG,"Only for PETSC_INT data");
23: /*
24: Copy the found values into a contiguous location, keeping them in the
25: order of the requested keys
26: */
27: ierr = PetscDataTypeGetSize(segment->datatype,&dsize);
28: bs = segment->bs;
29: ierr = PetscMalloc((n+1)*bs*dsize,&odata);
30: idata = (char*)segment->data;
31: for (i=0; i<n; i++) {
32: PetscMemcpy(odata + i*bs*dsize,idata + keys[i]*bs*dsize,bs*dsize);
33: }
35: found = (int*)odata;
36: n = n*bs;
38: /* Determine the max and min values */
39: if (n) {
40: imin = PETSC_MAX_INT;
41: imax = 0;
42: for (i=0; i<n; i++) {
43: if (found[i] < 0) continue;
44: imin = PetscMin(imin,found[i]);
45: imax = PetscMax(imax,found[i]);
46: }
47: } else {
48: imin = imax = 0;
49: }
50: PetscBTCreate(imax-imin,mask);
51: /* Put the values into the mask and count them */
52: count = 0;
53: for (i=0; i<n; i++) {
54: if (found[i] < 0) continue;
55: if (!PetscBTLookupSet(mask,found[i] - imin)) count++;
56: }
57: PetscBTMemzero(imax-imin,mask);
58: PetscMalloc((count+1)*sizeof(int),&out);
59: count = 0;
60: for (i=0; i<n; i++) {
61: if (found[i] < 0) continue;
62: if (!PetscBTLookupSet(mask,found[i] - imin)) {out[count++] = found[i];}
63: }
64: PetscBTDestroy(mask);
65: PetscFree(found);
67: ISCreateGeneral(ao->comm,count,out,is);
68: PetscFree(out);
69: return(0);
70: }