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

  9: int AODataSegmentGetReduced_Basic(AOData ao,const char name[],const char segname[],int n,int *keys,IS *is)
 10: {
 11:   AODataSegment *segment;
 12:   AODataKey     *key;
 13:   int           ierr,dsize,i,bs,*found,count,imin,imax,*out;
 14:   char          *idata,*odata;
 15:   PetscBT       mask;
 16:   PetscTruth    flag;

 19:   /* find the correct segment */
 20:   AODataSegmentFind_Private(ao,name,segname,&flag,&key,&segment);
 21:   if (!flag) SETERRQ(PETSC_ERR_ARG_WRONG,"Cannot locate segment");

 23:   if (segment->datatype != PETSC_INT) SETERRQ(PETSC_ERR_ARG_WRONG,"Only for PETSC_INT data");

 25:   /*
 26:      Copy the found values into a contiguous location, keeping them in the 
 27:      order of the requested keys
 28:   */
 29:   PetscDataTypeGetSize(segment->datatype,&dsize);
 30:   bs    = segment->bs;
 31:   PetscMalloc((n+1)*bs*dsize,&odata);
 32:   idata = (char*)segment->data;
 33:   for (i=0; i<n; i++) {
 34:     PetscMemcpy(odata + i*bs*dsize,idata + keys[i]*bs*dsize,bs*dsize);
 35:   }

 37:   found = (int*)odata;
 38:   n     = n*bs;

 40:   /*  Determine the max and min values */
 41:   if (n) {
 42:     imin = PETSC_MAX_INT;
 43:     imax = 0;
 44:     for (i=0; i<n; i++) {
 45:       if (found[i] < 0) continue;
 46:       imin = PetscMin(imin,found[i]);
 47:       imax = PetscMax(imax,found[i]);
 48:     }
 49:   } else {
 50:     imin = imax = 0;
 51:   }
 52:   PetscBTCreate(imax-imin,mask);
 53:   /* Put the values into the mask and count them */
 54:   count = 0;
 55:   for (i=0; i<n; i++) {
 56:     if (found[i] < 0) continue;
 57:     if (!PetscBTLookupSet(mask,found[i] - imin)) count++;
 58:   }
 59:   PetscBTMemzero(imax-imin,mask);
 60:   PetscMalloc((count+1)*sizeof(int),&out);
 61:   count = 0;
 62:   for (i=0; i<n; i++) {
 63:     if (found[i] < 0) continue;
 64:     if (!PetscBTLookupSet(mask,found[i] - imin)) {out[count++] = found[i];}
 65:   }
 66:   PetscBTDestroy(mask);
 67:   PetscFree(found);

 69:   ISCreateGeneral(ao->comm,count,out,is);
 70:   PetscFree(out);
 71:   return(0);
 72: }