Matt Knepley avatar Matt Knepley committed 0511a64

PetscSF: Fixed PetscSFCreateEmbeddedSF()
- We were using sf->nleaves for the leaf buffer, which is completely wrong. We have to use the largest thing in sf->mine[]

Comments (0)

Files changed (1)

src/vec/is/sf/interface/sf.c

 @*/
 PetscErrorCode PetscSFCreateEmbeddedSF(PetscSF sf,PetscInt nroots,const PetscInt *selected,PetscSF *newsf)
 {
+  PetscInt      *rootdata, *leafdata, *ilocal;
+  PetscSFNode   *iremote;
+  PetscInt       leafsize = 0, nleaves = 0, i;
   PetscErrorCode ierr;
-  PetscInt       i,nleaves,*ilocal,*rootdata,*leafdata;
-  PetscSFNode    *iremote;
 
   PetscFunctionBegin;
   PetscValidHeaderSpecific(sf,PETSCSF_CLASSID,1);
   if (nroots) PetscValidPointer(selected,3);
   PetscValidPointer(newsf,4);
-  ierr = PetscMalloc2(sf->nroots,PetscInt,&rootdata,sf->nleaves,PetscInt,&leafdata);CHKERRQ(ierr);
+  if (sf->mine) for (i = 0; i < sf->nleaves; ++i) {leafsize = PetscMax(leafsize, sf->mine[i]+1);}
+  else leafsize = sf->nleaves;
+  ierr = PetscMalloc2(sf->nroots,PetscInt,&rootdata,leafsize,PetscInt,&leafdata);CHKERRQ(ierr);
   ierr = PetscMemzero(rootdata,sf->nroots*sizeof(PetscInt));CHKERRQ(ierr);
-  ierr = PetscMemzero(leafdata,sf->nleaves*sizeof(PetscInt));CHKERRQ(ierr);
-  for (i=0; i<nroots; i++) rootdata[selected[i]] = 1;
+  ierr = PetscMemzero(leafdata,leafsize*sizeof(PetscInt));CHKERRQ(ierr);
+  for (i=0; i<nroots; ++i) rootdata[selected[i]] = 1;
   ierr = PetscSFBcastBegin(sf,MPIU_INT,rootdata,leafdata);CHKERRQ(ierr);
   ierr = PetscSFBcastEnd(sf,MPIU_INT,rootdata,leafdata);CHKERRQ(ierr);
 
-  for (i=0,nleaves=0; i<sf->nleaves; i++) nleaves += leafdata[i];
+  for (i = 0; i < leafsize; ++i) nleaves += leafdata[i];
   ierr = PetscMalloc(nleaves*sizeof(PetscInt),&ilocal);CHKERRQ(ierr);
   ierr = PetscMalloc(nleaves*sizeof(PetscSFNode),&iremote);CHKERRQ(ierr);
-  for (i=0,nleaves=0; i<sf->nleaves; i++) {
-    if (leafdata[i]) {
-      ilocal[nleaves]        = sf->mine ? sf->mine[i] : i;
+  for (i = 0, nleaves = 0; i < sf->nleaves; ++i) {
+    const PetscInt lidx = sf->mine ? sf->mine[i] : i;
+
+    if (leafdata[lidx]) {
+      ilocal[nleaves]        = lidx;
       iremote[nleaves].rank  = sf->remote[i].rank;
       iremote[nleaves].index = sf->remote[i].index;
-      nleaves++;
+      ++nleaves;
     }
   }
   ierr = PetscSFDuplicate(sf,PETSCSF_DUPLICATE_RANKS,newsf);CHKERRQ(ierr);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.