Actual source code: sfbasic.c

petsc-master 2019-05-21
Report Typos and Errors

  2:  #include <petsc/private/sfimpl.h>

  4: typedef struct _n_PetscSFBasicPack *PetscSFBasicPack;
  5: struct _n_PetscSFBasicPack {
  6:   void (*Pack)(PetscInt,PetscInt,const PetscInt*,const void*,void*);
  7:   void (*UnpackInsert)(PetscInt,PetscInt,const PetscInt*,void*,const void*);
  8:   void (*UnpackAdd)(PetscInt,PetscInt,const PetscInt*,void*,const void*);
  9:   void (*UnpackMin)(PetscInt,PetscInt,const PetscInt*,void*,const void*);
 10:   void (*UnpackMax)(PetscInt,PetscInt,const PetscInt*,void*,const void*);
 11:   void (*UnpackMinloc)(PetscInt,PetscInt,const PetscInt*,void*,const void*);
 12:   void (*UnpackMaxloc)(PetscInt,PetscInt,const PetscInt*,void*,const void*);
 13:   void (*UnpackMult)(PetscInt,PetscInt,const PetscInt*,void*,const void *);
 14:   void (*UnpackLAND)(PetscInt,PetscInt,const PetscInt*,void*,const void *);
 15:   void (*UnpackBAND)(PetscInt,PetscInt,const PetscInt*,void*,const void *);
 16:   void (*UnpackLOR)(PetscInt,PetscInt,const PetscInt*,void*,const void *);
 17:   void (*UnpackBOR)(PetscInt,PetscInt,const PetscInt*,void*,const void *);
 18:   void (*UnpackLXOR)(PetscInt,PetscInt,const PetscInt*,void*,const void *);
 19:   void (*UnpackBXOR)(PetscInt,PetscInt,const PetscInt*,void*,const void *);
 20:   void (*FetchAndInsert)(PetscInt,PetscInt,const PetscInt*,void*,void*);
 21:   void (*FetchAndAdd)(PetscInt,PetscInt,const PetscInt*,void*,void*);
 22:   void (*FetchAndMin)(PetscInt,PetscInt,const PetscInt*,void*,void*);
 23:   void (*FetchAndMax)(PetscInt,PetscInt,const PetscInt*,void*,void*);
 24:   void (*FetchAndMinloc)(PetscInt,PetscInt,const PetscInt*,void*,void*);
 25:   void (*FetchAndMaxloc)(PetscInt,PetscInt,const PetscInt*,void*,void*);
 26:   void (*FetchAndMult)(PetscInt,PetscInt,const PetscInt*,void*,void*);
 27:   void (*FetchAndLAND)(PetscInt,PetscInt,const PetscInt*,void*,void*);
 28:   void (*FetchAndBAND)(PetscInt,PetscInt,const PetscInt*,void*,void*);
 29:   void (*FetchAndLOR)(PetscInt,PetscInt,const PetscInt*,void*,void*);
 30:   void (*FetchAndBOR)(PetscInt,PetscInt,const PetscInt*,void*,void*);
 31:   void (*FetchAndLXOR)(PetscInt,PetscInt,const PetscInt*,void*,void*);
 32:   void (*FetchAndBXOR)(PetscInt,PetscInt,const PetscInt*,void*,void*);

 34:   MPI_Datatype     unit;
 35:   PetscBool        isbuiltin;   /* Is unit an MPI builtin datatype? */
 36:   size_t           unitbytes;   /* Number of bytes in a unit */
 37:   PetscInt         bs;          /* Number of basic units in a unit */
 38:   const void       *key;        /* Array used as key for operation */
 39:   char             **root;      /* Packed root data, indexed by leaf rank */
 40:   char             **leaf;      /* Packed leaf data, indexed by root rank */
 41:   MPI_Request      *requests;   /* Array of root requests followed by leaf requests */
 42:   PetscSFBasicPack next;
 43: };

 45: typedef struct {
 46:   PetscMPIInt      tag;
 47:   PetscMPIInt      niranks;     /* Number of incoming ranks (ranks accessing my roots) */
 48:   PetscMPIInt      ndiranks;    /* Number of incoming ranks (ranks accessing my roots) in distinguished set */
 49:   PetscMPIInt      *iranks;     /* Array of ranks that reference my roots */
 50:   PetscInt         itotal;      /* Total number of graph edges referencing my roots */
 51:   PetscInt         *ioffset;    /* Array of length niranks+1 holding offset in irootloc[] for each rank */
 52:   PetscInt         *irootloc;   /* Incoming roots referenced by ranks starting at ioffset[rank] */
 53:   PetscSFBasicPack avail;       /* One or more entries per MPI Datatype, lazily constructed */
 54:   PetscSFBasicPack inuse;       /* Buffers being used for transactions that have not yet completed */
 55: } PetscSF_Basic;

 57: #if !defined(PETSC_HAVE_MPI_TYPE_DUP)
 58: PETSC_STATIC_INLINE int MPI_Type_dup(MPI_Datatype datatype,MPI_Datatype *newtype)
 59: {
 60:   int ierr;
 61:   MPI_Type_contiguous(1,datatype,newtype); if (ierr) return ierr;
 62:   MPI_Type_commit(newtype); if (ierr) return ierr;
 63:   return MPI_SUCCESS;
 64: }
 65: #endif

 67: /*
 68:  * MPI_Reduce_local is not really useful because it can't handle sparse data and it vectorizes "in the wrong direction",
 69:  * therefore we pack data types manually. This section defines packing routines for the standard data types.
 70:  */

 72: #define CPPJoin2_exp(a,b) a ## b
 73: #define CPPJoin2(a,b) CPPJoin2_exp(a,b)
 74: #define CPPJoin3_exp_(a,b,c) a ## b ## _ ## c
 75: #define CPPJoin3_(a,b,c) CPPJoin3_exp_(a,b,c)

 77: /* Basic types without addition */
 78: #define DEF_PackNoInit(type,BS)                                         \
 79:   static void CPPJoin3_(Pack_,type,BS)(PetscInt n,PetscInt bs,const PetscInt *idx,const void *unpacked,void *packed) { \
 80:     const type *u = (const type*)unpacked;                              \
 81:     type *p = (type*)packed;                                            \
 82:     PetscInt i,j,k;                                                     \
 83:     for (i=0; i<n; i++)                                                 \
 84:       for (j=0; j<bs; j+=BS)                                            \
 85:         for (k=j; k<j+BS; k++)                                          \
 86:           p[i*bs+k] = u[idx[i]*bs+k];                                   \
 87:   }                                                                     \
 88:   static void CPPJoin3_(UnpackInsert_,type,BS)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,const void *packed) { \
 89:     type *u = (type*)unpacked;                                          \
 90:     const type *p = (const type*)packed;                                \
 91:     PetscInt i,j,k;                                                     \
 92:     for (i=0; i<n; i++)                                                 \
 93:       for (j=0; j<bs; j+=BS)                                            \
 94:         for (k=j; k<j+BS; k++)                                          \
 95:           u[idx[i]*bs+k] = p[i*bs+k];                                   \
 96:   }                                                                     \
 97:   static void CPPJoin3_(FetchAndInsert_,type,BS)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,void *packed) { \
 98:     type *u = (type*)unpacked;                                          \
 99:     type *p = (type*)packed;                                            \
100:     PetscInt i,j,k;                                                     \
101:     for (i=0; i<n; i++) {                                               \
102:       PetscInt ii = idx[i];                                             \
103:       for (j=0; j<bs; j+=BS)                                            \
104:         for (k=j; k<j+BS; k++) {                                        \
105:           type t = u[ii*bs+k];                                          \
106:           u[ii*bs+k] = p[i*bs+k];                                       \
107:           p[i*bs+k] = t;                                                \
108:         }                                                               \
109:     }                                                                   \
110:   }

112: /* Basic types defining addition */
113: #define DEF_PackAddNoInit(type,BS)                                      \
114:   DEF_PackNoInit(type,BS)                                               \
115:   static void CPPJoin3_(UnpackAdd_,type,BS)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,const void *packed) { \
116:     type *u = (type*)unpacked;                                          \
117:     const type *p = (const type*)packed;                                \
118:     PetscInt i,j,k;                                                     \
119:     for (i=0; i<n; i++)                                                 \
120:       for (j=0; j<bs; j+=BS)                                            \
121:         for (k=j; k<j+BS; k++)                                          \
122:           u[idx[i]*bs+k] += p[i*bs+k];                                  \
123:   }                                                                     \
124:   static void CPPJoin3_(FetchAndAdd_,type,BS)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,void *packed) { \
125:     type *u = (type*)unpacked;                                          \
126:     type *p = (type*)packed;                                            \
127:     PetscInt i,j,k;                                                     \
128:     for (i=0; i<n; i++) {                                               \
129:       PetscInt ii = idx[i];                                             \
130:       for (j=0; j<bs; j+=BS)                                            \
131:         for (k=j; k<j+BS; k++) {                                        \
132:           type t = u[ii*bs+k];                                          \
133:           u[ii*bs+k] = t + p[i*bs+k];                                   \
134:           p[i*bs+k] = t;                                                \
135:         }                                                               \
136:     }                                                                   \
137:   }                                                                     \
138:   static void CPPJoin3_(UnpackMult_,type,BS)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,const void *packed) { \
139:     type *u = (type*)unpacked;                                          \
140:     const type *p = (const type*)packed;                                \
141:     PetscInt i,j,k;                                                     \
142:     for (i=0; i<n; i++)                                                 \
143:       for (j=0; j<bs; j+=BS)                                            \
144:         for (k=j; k<j+BS; k++)                                          \
145:           u[idx[i]*bs+k] *= p[i*bs+k];                                  \
146:   }                                                                     \
147:   static void CPPJoin3_(FetchAndMult_,type,BS)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,void *packed) { \
148:     type *u = (type*)unpacked;                                          \
149:     type *p = (type*)packed;                                            \
150:     PetscInt i,j,k;                                                     \
151:     for (i=0; i<n; i++) {                                               \
152:       PetscInt ii = idx[i];                                             \
153:       for (j=0; j<bs; j+=BS)                                            \
154:         for (k=j; k<j+BS; k++) {                                        \
155:           type t = u[ii*bs+k];                                          \
156:           u[ii*bs+k] = t * p[i*bs+k];                                   \
157:           p[i*bs+k] = t;                                                \
158:         }                                                               \
159:     }                                                                   \
160:   }
161: #define DEF_Pack(type,BS)                                               \
162:   DEF_PackAddNoInit(type,BS)                                            \
163:   static void CPPJoin3_(PackInit_,type,BS)(PetscSFBasicPack link) {     \
164:     link->Pack = CPPJoin3_(Pack_,type,BS);                              \
165:     link->UnpackInsert = CPPJoin3_(UnpackInsert_,type,BS);              \
166:     link->UnpackAdd = CPPJoin3_(UnpackAdd_,type,BS);                    \
167:     link->UnpackMult = CPPJoin3_(UnpackMult_,type,BS);                  \
168:     link->FetchAndInsert = CPPJoin3_(FetchAndInsert_,type,BS);          \
169:     link->FetchAndAdd = CPPJoin3_(FetchAndAdd_,type,BS);                \
170:     link->FetchAndMult = CPPJoin3_(FetchAndMult_,type,BS);              \
171:     link->unitbytes = sizeof(type);                                     \
172:   }
173: /* Comparable types */
174: #define DEF_PackCmp(type)                                               \
175:   DEF_PackAddNoInit(type,1)                                             \
176:   static void CPPJoin2(UnpackMax_,type)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,const void *packed) { \
177:     type *u = (type*)unpacked;                                          \
178:     const type *p = (const type*)packed;                                \
179:     PetscInt i;                                                         \
180:     for (i=0; i<n; i++) {                                               \
181:       type v = u[idx[i]];                                               \
182:       u[idx[i]] = PetscMax(v,p[i]);                                     \
183:     }                                                                   \
184:   }                                                                     \
185:   static void CPPJoin2(UnpackMin_,type)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,const void *packed) { \
186:     type *u = (type*)unpacked;                                          \
187:     const type *p = (const type*)packed;                                \
188:     PetscInt i;                                                         \
189:     for (i=0; i<n; i++) {                                               \
190:       type v = u[idx[i]];                                               \
191:       u[idx[i]] = PetscMin(v,p[i]);                                     \
192:     }                                                                   \
193:   }                                                                     \
194:   static void CPPJoin2(FetchAndMax_,type)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,void *packed) { \
195:     type *u = (type*)unpacked;                                          \
196:     type *p = (type*)packed;                                            \
197:     PetscInt i;                                                         \
198:     for (i=0; i<n; i++) {                                               \
199:       PetscInt j = idx[i];                                              \
200:       type v = u[j];                                                    \
201:       u[j] = PetscMax(v,p[i]);                                          \
202:       p[i] = v;                                                         \
203:     }                                                                   \
204:   }                                                                     \
205:   static void CPPJoin2(FetchAndMin_,type)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,void *packed) { \
206:     type *u = (type*)unpacked;                                          \
207:     type *p = (type*)packed;                                            \
208:     PetscInt i;                                                         \
209:     for (i=0; i<n; i++) {                                               \
210:       PetscInt j = idx[i];                                              \
211:       type v = u[j];                                                    \
212:       u[j] = PetscMin(v,p[i]);                                          \
213:       p[i] = v;                                                         \
214:     }                                                                   \
215:   }                                                                     \
216:   static void CPPJoin2(PackInit_,type)(PetscSFBasicPack link) {         \
217:     link->Pack = CPPJoin3_(Pack_,type,1);                               \
218:     link->UnpackInsert = CPPJoin3_(UnpackInsert_,type,1);               \
219:     link->UnpackAdd  = CPPJoin3_(UnpackAdd_,type,1);                    \
220:     link->UnpackMax  = CPPJoin2(UnpackMax_,type);                       \
221:     link->UnpackMin  = CPPJoin2(UnpackMin_,type);                       \
222:     link->UnpackMult = CPPJoin3_(UnpackMult_,type,1);                   \
223:     link->FetchAndInsert = CPPJoin3_(FetchAndInsert_,type,1);           \
224:     link->FetchAndAdd = CPPJoin3_(FetchAndAdd_ ,type,1);                \
225:     link->FetchAndMax = CPPJoin2(FetchAndMax_ ,type);                   \
226:     link->FetchAndMin = CPPJoin2(FetchAndMin_ ,type);                   \
227:     link->FetchAndMult = CPPJoin3_(FetchAndMult_,type,1);               \
228:     link->unitbytes = sizeof(type);                                     \
229:   }

231: /* Logical Types */
232: #define DEF_PackLog(type)                                               \
233:   static void CPPJoin2(UnpackLAND_,type)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,const void *packed) { \
234:     type *u = (type*)unpacked;                                          \
235:     const type *p = (const type*)packed;                                \
236:     PetscInt i;                                                         \
237:     for (i=0; i<n; i++) {                                               \
238:       type v = u[idx[i]];                                               \
239:       u[idx[i]] = v && p[i];                                            \
240:     }                                                                   \
241:   }                                                                     \
242:   static void CPPJoin2(UnpackLOR_,type)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,const void *packed) { \
243:     type *u = (type*)unpacked;                                          \
244:     const type *p = (const type*)packed;                                \
245:     PetscInt i;                                                         \
246:     for (i=0; i<n; i++) {                                               \
247:       type v = u[idx[i]];                                               \
248:       u[idx[i]] = v || p[i];                                            \
249:     }                                                                   \
250:   }                                                                     \
251:   static void CPPJoin2(UnpackLXOR_,type)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,const void *packed) { \
252:     type *u = (type*)unpacked;                                          \
253:     const type *p = (const type*)packed;                                \
254:     PetscInt i;                                                         \
255:     for (i=0; i<n; i++) {                                               \
256:       type v = u[idx[i]];                                               \
257:       u[idx[i]] = (!v)!=(!p[i]);                                        \
258:     }                                                                   \
259:   }                                                                     \
260:   static void CPPJoin2(FetchAndLAND_,type)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,void *packed) { \
261:     type *u = (type*)unpacked;                                          \
262:     type *p = (type*)packed;                                            \
263:     PetscInt i;                                                         \
264:     for (i=0; i<n; i++) {                                               \
265:       PetscInt j = idx[i];                                              \
266:       type v = u[j];                                                    \
267:       u[j] = v && p[i];                                                 \
268:       p[i] = v;                                                         \
269:     }                                                                   \
270:   }                                                                     \
271:   static void CPPJoin2(FetchAndLOR_,type)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,void *packed) { \
272:     type *u = (type*)unpacked;                                          \
273:     type *p = (type*)packed;                                            \
274:     PetscInt i;                                                         \
275:     for (i=0; i<n; i++) {                                               \
276:       PetscInt j = idx[i];                                              \
277:       type v = u[j];                                                    \
278:       u[j] = v || p[i];                                                 \
279:       p[i] = v;                                                         \
280:     }                                                                   \
281:   }                                                                     \
282:   static void CPPJoin2(FetchAndLXOR_,type)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,void *packed) { \
283:     type *u = (type*)unpacked;                                          \
284:     type *p = (type*)packed;                                            \
285:     PetscInt i;                                                         \
286:     for (i=0; i<n; i++) {                                               \
287:       PetscInt j = idx[i];                                              \
288:       type v = u[j];                                                    \
289:       u[j] = (!v)!=(!p[i]);                                             \
290:       p[i] = v;                                                         \
291:     }                                                                   \
292:   }                                                                     \
293:   static void CPPJoin2(PackInit_Logical_,type)(PetscSFBasicPack link) { \
294:     link->UnpackLAND = CPPJoin2(UnpackLAND_,type);                      \
295:     link->UnpackLOR  = CPPJoin2(UnpackLOR_,type);                       \
296:     link->UnpackLXOR = CPPJoin2(UnpackLXOR_,type);                      \
297:     link->FetchAndLAND = CPPJoin2(FetchAndLAND_,type);                  \
298:     link->FetchAndLOR  = CPPJoin2(FetchAndLOR_,type);                   \
299:     link->FetchAndLXOR = CPPJoin2(FetchAndLXOR_,type);                  \
300:   }


303: /* Bitwise Types */
304: #define DEF_PackBit(type)                                               \
305:   static void CPPJoin2(UnpackBAND_,type)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,const void *packed) { \
306:     type *u = (type*)unpacked;                                          \
307:     const type *p = (const type*)packed;                                \
308:     PetscInt i;                                                         \
309:     for (i=0; i<n; i++) {                                               \
310:       type v = u[idx[i]];                                               \
311:       u[idx[i]] = v & p[i];                                             \
312:     }                                                                   \
313:   }                                                                     \
314:   static void CPPJoin2(UnpackBOR_,type)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,const void *packed) { \
315:     type *u = (type*)unpacked;                                          \
316:     const type *p = (const type*)packed;                                \
317:     PetscInt i;                                                         \
318:     for (i=0; i<n; i++) {                                               \
319:       type v = u[idx[i]];                                               \
320:       u[idx[i]] = v | p[i];                                             \
321:     }                                                                   \
322:   }                                                                     \
323:   static void CPPJoin2(UnpackBXOR_,type)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,const void *packed) { \
324:     type *u = (type*)unpacked;                                          \
325:     const type *p = (const type*)packed;                                \
326:     PetscInt i;                                                         \
327:     for (i=0; i<n; i++) {                                               \
328:       type v = u[idx[i]];                                               \
329:       u[idx[i]] = v^p[i];                                               \
330:     }                                                                   \
331:   }                                                                     \
332:   static void CPPJoin2(FetchAndBAND_,type)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,void *packed) { \
333:     type *u = (type*)unpacked;                                          \
334:     type *p = (type*)packed;                                            \
335:     PetscInt i;                                                         \
336:     for (i=0; i<n; i++) {                                               \
337:       PetscInt j = idx[i];                                              \
338:       type v = u[j];                                                    \
339:       u[j] = v & p[i];                                                  \
340:       p[i] = v;                                                         \
341:     }                                                                   \
342:   }                                                                     \
343:   static void CPPJoin2(FetchAndBOR_,type)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,void *packed) { \
344:     type *u = (type*)unpacked;                                          \
345:     type *p = (type*)packed;                                            \
346:     PetscInt i;                                                         \
347:     for (i=0; i<n; i++) {                                               \
348:       PetscInt j = idx[i];                                              \
349:       type v = u[j];                                                    \
350:       u[j] = v | p[i];                                                  \
351:       p[i] = v;                                                         \
352:     }                                                                   \
353:   }                                                                     \
354:   static void CPPJoin2(FetchAndBXOR_,type)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,void *packed) { \
355:     type *u = (type*)unpacked;                                          \
356:     type *p = (type*)packed;                                            \
357:     PetscInt i;                                                         \
358:     for (i=0; i<n; i++) {                                               \
359:       PetscInt j = idx[i];                                              \
360:       type v = u[j];                                                    \
361:       u[j] = v^p[i];                                                    \
362:       p[i] = v;                                                         \
363:     }                                                                   \
364:   }                                                                     \
365:   static void CPPJoin2(PackInit_Bitwise_,type)(PetscSFBasicPack link) { \
366:     link->UnpackBAND = CPPJoin2(UnpackBAND_,type);                      \
367:     link->UnpackBOR  = CPPJoin2(UnpackBOR_,type);                       \
368:     link->UnpackBXOR = CPPJoin2(UnpackBXOR_,type);                      \
369:     link->FetchAndBAND = CPPJoin2(FetchAndBAND_,type);                  \
370:     link->FetchAndBOR  = CPPJoin2(FetchAndBOR_,type);                   \
371:     link->FetchAndBXOR = CPPJoin2(FetchAndBXOR_,type);                  \
372:   }

374: /* Pair types */
375: #define CPPJoinloc_exp(base,op,t1,t2) base ## op ## loc_ ## t1 ## _ ## t2
376: #define CPPJoinloc(base,op,t1,t2) CPPJoinloc_exp(base,op,t1,t2)
377: #define PairType(type1,type2) CPPJoin3_(_pairtype_,type1,type2)
378: #define DEF_UnpackXloc(type1,type2,locname,op)                              \
379:   static void CPPJoinloc(Unpack,locname,type1,type2)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,const void *packed) { \
380:     PairType(type1,type2) *u = (PairType(type1,type2)*)unpacked;        \
381:     const PairType(type1,type2) *p = (const PairType(type1,type2)*)packed; \
382:     PetscInt i;                                                         \
383:     for (i=0; i<n; i++) {                                               \
384:       PetscInt j = idx[i];                                              \
385:       if (p[i].a op u[j].a) {                                           \
386:         u[j].a = p[i].a;                                                \
387:         u[j].b = p[i].b;                                                \
388:       } else if (u[j].a == p[i].a) {                                    \
389:         u[j].b = PetscMin(u[j].b,p[i].b);                               \
390:       }                                                                 \
391:     }                                                                   \
392:   }                                                                     \
393:   static void CPPJoinloc(FetchAnd,locname,type1,type2)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,void *packed) { \
394:     PairType(type1,type2) *u = (PairType(type1,type2)*)unpacked;        \
395:     PairType(type1,type2) *p = (PairType(type1,type2)*)packed;          \
396:     PetscInt i;                                                         \
397:     for (i=0; i<n; i++) {                                               \
398:       PetscInt j = idx[i];                                              \
399:       PairType(type1,type2) v;                                          \
400:       v.a = u[j].a;                                                     \
401:       v.b = u[j].b;                                                     \
402:       if (p[i].a op u[j].a) {                                           \
403:         u[j].a = p[i].a;                                                \
404:         u[j].b = p[i].b;                                                \
405:       } else if (u[j].a == p[i].a) {                                    \
406:         u[j].b = PetscMin(u[j].b,p[i].b);                               \
407:       }                                                                 \
408:       p[i].a = v.a;                                                     \
409:       p[i].b = v.b;                                                     \
410:     }                                                                   \
411:   }
412: #define DEF_PackPair(type1,type2)                                       \
413:   typedef struct {type1 a; type2 b;} PairType(type1,type2);             \
414:   static void CPPJoin3_(Pack_,type1,type2)(PetscInt n,PetscInt bs,const PetscInt *idx,const void *unpacked,void *packed) { \
415:     const PairType(type1,type2) *u = (const PairType(type1,type2)*)unpacked; \
416:     PairType(type1,type2) *p = (PairType(type1,type2)*)packed;          \
417:     PetscInt i;                                                         \
418:     for (i=0; i<n; i++) {                                               \
419:       p[i].a = u[idx[i]].a;                                             \
420:       p[i].b = u[idx[i]].b;                                             \
421:     }                                                                   \
422:   }                                                                     \
423:   static void CPPJoin3_(UnpackInsert_,type1,type2)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,const void *packed) { \
424:     PairType(type1,type2) *u = (PairType(type1,type2)*)unpacked;       \
425:     const PairType(type1,type2) *p = (const PairType(type1,type2)*)packed; \
426:     PetscInt i;                                                         \
427:     for (i=0; i<n; i++) {                                               \
428:       u[idx[i]].a = p[i].a;                                             \
429:       u[idx[i]].b = p[i].b;                                             \
430:     }                                                                   \
431:   }                                                                     \
432:   static void CPPJoin3_(UnpackAdd_,type1,type2)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,const void *packed) { \
433:     PairType(type1,type2) *u = (PairType(type1,type2)*)unpacked;       \
434:     const PairType(type1,type2) *p = (const PairType(type1,type2)*)packed; \
435:     PetscInt i;                                                         \
436:     for (i=0; i<n; i++) {                                               \
437:       u[idx[i]].a += p[i].a;                                            \
438:       u[idx[i]].b += p[i].b;                                            \
439:     }                                                                   \
440:   }                                                                     \
441:   static void CPPJoin3_(FetchAndInsert_,type1,type2)(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,void *packed) { \
442:     PairType(type1,type2) *u = (PairType(type1,type2)*)unpacked;        \
443:     PairType(type1,type2) *p = (PairType(type1,type2)*)packed;          \
444:     PetscInt i;                                                         \
445:     for (i=0; i<n; i++) {                                               \
446:       PetscInt j = idx[i];                                              \
447:       PairType(type1,type2) v;                                          \
448:       v.a = u[j].a;                                                     \
449:       v.b = u[j].b;                                                     \
450:       u[j].a = p[i].a;                                                  \
451:       u[j].b = p[i].b;                                                  \
452:       p[i].a = v.a;                                                     \
453:       p[i].b = v.b;                                                     \
454:     }                                                                   \
455:   }                                                                     \
456:   static void FetchAndAdd_ ## type1 ## _ ## type2(PetscInt n,PetscInt bs,const PetscInt *idx,void *unpacked,void *packed) { \
457:     PairType(type1,type2) *u = (PairType(type1,type2)*)unpacked;       \
458:     PairType(type1,type2) *p = (PairType(type1,type2)*)packed;         \
459:     PetscInt i;                                                         \
460:     for (i=0; i<n; i++) {                                               \
461:       PetscInt j = idx[i];                                              \
462:       PairType(type1,type2) v;                                          \
463:       v.a = u[j].a;                                                     \
464:       v.b = u[j].b;                                                     \
465:       u[j].a = v.a + p[i].a;                                            \
466:       u[j].b = v.b + p[i].b;                                            \
467:       p[i].a = v.a;                                                     \
468:       p[i].b = v.b;                                                     \
469:     }                                                                   \
470:   }                                                                     \
471:   DEF_UnpackXloc(type1,type2,Max,>)                                     \
472:   DEF_UnpackXloc(type1,type2,Min,<)                                     \
473:   static void CPPJoin3_(PackInit_,type1,type2)(PetscSFBasicPack link) { \
474:     link->Pack = CPPJoin3_(Pack_,type1,type2);                          \
475:     link->UnpackInsert = CPPJoin3_(UnpackInsert_,type1,type2);          \
476:     link->UnpackAdd = CPPJoin3_(UnpackAdd_,type1,type2);                \
477:     link->UnpackMaxloc = CPPJoin3_(UnpackMaxloc_,type1,type2);          \
478:     link->UnpackMinloc = CPPJoin3_(UnpackMinloc_,type1,type2);          \
479:     link->FetchAndInsert = CPPJoin3_(FetchAndInsert_,type1,type2);      \
480:     link->FetchAndAdd = CPPJoin3_(FetchAndAdd_,type1,type2);            \
481:     link->FetchAndMaxloc = CPPJoin3_(FetchAndMaxloc_,type1,type2);      \
482:     link->FetchAndMinloc = CPPJoin3_(FetchAndMinloc_,type1,type2);      \
483:     link->unitbytes = sizeof(PairType(type1,type2));                    \
484:   }

486: /* Currently only dumb blocks of data */
487: #define BlockType(unit,count) CPPJoin3_(_blocktype_,unit,count)
488: #define DEF_Block(unit,count)                                           \
489:   typedef struct {unit v[count];} BlockType(unit,count);                \
490:   DEF_PackNoInit(BlockType(unit,count),1)                               \
491:   static void CPPJoin3_(PackInit_block_,unit,count)(PetscSFBasicPack link) { \
492:     link->Pack = CPPJoin3_(Pack_,BlockType(unit,count),1);               \
493:     link->UnpackInsert = CPPJoin3_(UnpackInsert_,BlockType(unit,count),1); \
494:     link->FetchAndInsert = CPPJoin3_(FetchAndInsert_,BlockType(unit,count),1); \
495:     link->unitbytes = sizeof(BlockType(unit,count));                    \
496:   }

498: /* The typedef is used to get a typename without space that CPPJoin can handle */
499: typedef signed char SignedChar;
500: typedef unsigned char UnsignedChar;

502: DEF_PackCmp(SignedChar)
503: DEF_PackBit(SignedChar)
504: DEF_PackLog(SignedChar)
505: DEF_PackCmp(UnsignedChar)
506: DEF_PackBit(UnsignedChar)
507: DEF_PackLog(UnsignedChar)
508: DEF_PackCmp(int)
509: DEF_PackBit(int)
510: DEF_PackLog(int)
511: DEF_PackCmp(PetscInt)
512: DEF_PackBit(PetscInt)
513: DEF_PackLog(PetscInt)
514: DEF_Pack(PetscInt,2)
515: DEF_Pack(PetscInt,3)
516: DEF_Pack(PetscInt,4)
517: DEF_Pack(PetscInt,5)
518: DEF_Pack(PetscInt,7)
519: DEF_PackCmp(PetscReal)
520: DEF_PackLog(PetscReal)
521: DEF_Pack(PetscReal,2)
522: DEF_Pack(PetscReal,3)
523: DEF_Pack(PetscReal,4)
524: DEF_Pack(PetscReal,5)
525: DEF_Pack(PetscReal,7)
526: #if defined(PETSC_HAVE_COMPLEX)
527: DEF_Pack(PetscComplex,1)
528: DEF_Pack(PetscComplex,2)
529: DEF_Pack(PetscComplex,3)
530: DEF_Pack(PetscComplex,4)
531: DEF_Pack(PetscComplex,5)
532: DEF_Pack(PetscComplex,7)
533: #endif
534: DEF_PackPair(int,int)
535: DEF_PackPair(PetscInt,PetscInt)
536: DEF_Block(int,1)
537: DEF_Block(int,2)
538: DEF_Block(int,3)
539: DEF_Block(int,4)
540: DEF_Block(int,5)
541: DEF_Block(int,6)
542: DEF_Block(int,7)
543: DEF_Block(int,8)
544: DEF_Block(char,1)
545: DEF_Block(char,2)
546: DEF_Block(char,3)
547: #if PETSC_SIZEOF_INT == 8
548: DEF_Block(char,4)
549: DEF_Block(char,5)
550: DEF_Block(char,6)
551: DEF_Block(char,7)
552: #endif

554: static PetscErrorCode PetscSFSetUp_Basic(PetscSF sf)
555: {
556:   PetscSF_Basic  *bas = (PetscSF_Basic*)sf->data;
558:   PetscInt       *rlengths,*ilengths,i;
559:   PetscMPIInt    rank,niranks,*iranks;
560:   MPI_Comm       comm;
561:   MPI_Group      group;
562:   PetscMPIInt    nreqs = 0;
563:   MPI_Request    *reqs;

566:   MPI_Comm_group(PETSC_COMM_SELF,&group);
567:   PetscSFSetUpRanks(sf,group);
568:   MPI_Group_free(&group);
569:   PetscObjectGetComm((PetscObject)sf,&comm);
570:   PetscObjectGetNewTag((PetscObject)sf,&bas->tag);
571:   MPI_Comm_rank(comm,&rank);
572:   /*
573:    * Inform roots about how many leaves and from which ranks
574:    */
575:   PetscMalloc1(sf->nranks,&rlengths);
576:   /* Determine number, sending ranks, and length of incoming */
577:   for (i=0; i<sf->nranks; i++) {
578:     rlengths[i] = sf->roffset[i+1] - sf->roffset[i]; /* Number of roots referenced by my leaves; for rank sf->ranks[i] */
579:   }
580:   PetscCommBuildTwoSided(comm,1,MPIU_INT,sf->nranks-sf->ndranks,sf->ranks+sf->ndranks,rlengths+sf->ndranks,&niranks,&iranks,&ilengths);

582:   /* Sort iranks. See use of VecScatterGetRemoteOrdered_Private() in MatGetBrowsOfAoCols_MPIAIJ() on why.
583:      We could sort ranks there at the price of allocating extra working arrays. Presumably, niranks is
584:      small and the sorting is cheap.
585:    */
586:   PetscSortMPIIntWithIntArray(niranks,iranks,ilengths);

588:   /* Partition into distinguished and non-distinguished incoming ranks */
589:   bas->ndiranks = sf->ndranks;
590:   bas->niranks = bas->ndiranks + niranks;
591:   PetscMalloc2(bas->niranks,&bas->iranks,bas->niranks+1,&bas->ioffset);
592:   bas->ioffset[0] = 0;
593:   for (i=0; i<bas->ndiranks; i++) {
594:     bas->iranks[i] = sf->ranks[i];
595:     bas->ioffset[i+1] = bas->ioffset[i] + rlengths[i];
596:   }
597:   for (i=bas->ndiranks; i<bas->niranks; i++) {
598:     bas->iranks[i] = iranks[i-bas->ndiranks];
599:     bas->ioffset[i+1] = bas->ioffset[i] + ilengths[i-bas->ndiranks];
600:   }
601:   bas->itotal = bas->ioffset[i];
602:   PetscFree(rlengths);
603:   PetscFree(iranks);
604:   PetscFree(ilengths);

606:   /* Sanity checks for distinguished ranks */
607:   if (sf->ndranks != bas->ndiranks) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Broken setup for shared ranks");
608:   if (sf->ndranks > 1 || (sf->ndranks == 1 && sf->ranks[0] != rank)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Broken setup for shared ranks");
609:   if (bas->ndiranks > 1 || (bas->ndiranks == 1 && bas->iranks[0] != rank)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Broken setup for shared ranks");

611:   /* Send leaf identities to roots */
612:   PetscMalloc1(bas->itotal,&bas->irootloc);
613:   PetscMalloc1(bas->niranks-bas->ndiranks+sf->nranks-sf->ndranks,&reqs);
614:   for (i=sf->ndranks; i<sf->nranks; i++, nreqs++) {
615:     PetscMPIInt npoints;
616:     PetscMPIIntCast(sf->roffset[i+1]-sf->roffset[i],&npoints);
617:     MPI_Isend(sf->rremote+sf->roffset[i],npoints,MPIU_INT,sf->ranks[i],bas->tag,comm,&reqs[nreqs]);
618:   }
619:   for (i=bas->ndiranks; i<bas->niranks; i++, nreqs++) {
620:     PetscMPIInt npoints;
621:     PetscMPIIntCast(bas->ioffset[i+1]-bas->ioffset[i],&npoints);
622:     MPI_Irecv(bas->irootloc+bas->ioffset[i],npoints,MPIU_INT,bas->iranks[i],bas->tag,comm,&reqs[nreqs]);
623:   }
624:   for (i=0; i<sf->ndranks; i++) {
625:     PetscInt npoints = sf->roffset[i+1]-sf->roffset[i];
626:     if (npoints != bas->ioffset[i+1]-bas->ioffset[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Distinguished rank exchange has mismatched lengths");
627:     PetscMemcpy(bas->irootloc+bas->ioffset[i],sf->rremote+sf->roffset[i],npoints*sizeof(bas->irootloc[0]));
628:   }
629:   MPI_Waitall(nreqs,reqs,MPI_STATUSES_IGNORE);
630:   PetscFree(reqs);
631:   return(0);
632: }

634: static PetscErrorCode PetscSFBasicPackTypeSetup(PetscSFBasicPack link,MPI_Datatype unit)
635: {
637:   PetscBool      isInt,isPetscInt,isPetscReal,is2Int,is2PetscInt,isSignedChar,isUnsignedChar;
638:   PetscInt       nPetscIntContig,nPetscRealContig;
639:   PetscMPIInt    ni,na,nd,combiner;
640: #if defined(PETSC_HAVE_COMPLEX)
641:   PetscBool isPetscComplex;
642:   PetscInt nPetscComplexContig;
643: #endif

646:   MPIPetsc_Type_compare(unit,MPI_SIGNED_CHAR,&isSignedChar);
647:   MPIPetsc_Type_compare(unit,MPI_UNSIGNED_CHAR,&isUnsignedChar);
648:   /* MPI_CHAR is treated below as a dumb block type that does not support reduction according to MPI standard */
649:   MPIPetsc_Type_compare(unit,MPI_INT,&isInt);
650:   MPIPetsc_Type_compare(unit,MPIU_INT,&isPetscInt);
651:   MPIPetsc_Type_compare_contig(unit,MPIU_INT,&nPetscIntContig);
652:   MPIPetsc_Type_compare(unit,MPIU_REAL,&isPetscReal);
653:   MPIPetsc_Type_compare_contig(unit,MPIU_REAL,&nPetscRealContig);
654: #if defined(PETSC_HAVE_COMPLEX)
655:   MPIPetsc_Type_compare(unit,MPIU_COMPLEX,&isPetscComplex);
656:   MPIPetsc_Type_compare_contig(unit,MPIU_COMPLEX,&nPetscComplexContig);
657: #endif
658:   MPIPetsc_Type_compare(unit,MPI_2INT,&is2Int);
659:   MPIPetsc_Type_compare(unit,MPIU_2INT,&is2PetscInt);
660:   MPI_Type_get_envelope(unit,&ni,&na,&nd,&combiner);
661:   link->isbuiltin = (combiner == MPI_COMBINER_NAMED) ? PETSC_TRUE : PETSC_FALSE;
662:   link->bs = 1;

664:   if (isSignedChar) {PackInit_SignedChar(link); PackInit_Logical_SignedChar(link); PackInit_Bitwise_SignedChar(link);}
665:   else if (isUnsignedChar) {PackInit_UnsignedChar(link); PackInit_Logical_UnsignedChar(link); PackInit_Bitwise_UnsignedChar(link);}
666:   else if (isInt) {PackInit_int(link); PackInit_Logical_int(link); PackInit_Bitwise_int(link);}
667:   else if (isPetscInt) {PackInit_PetscInt(link); PackInit_Logical_PetscInt(link); PackInit_Bitwise_PetscInt(link);}
668:   else if (isPetscReal) {PackInit_PetscReal(link); PackInit_Logical_PetscReal(link);}
669: #if defined(PETSC_HAVE_COMPLEX)
670:   else if (isPetscComplex) PackInit_PetscComplex_1(link);
671: #endif
672:   else if (is2Int) PackInit_int_int(link);
673:   else if (is2PetscInt) PackInit_PetscInt_PetscInt(link);
674:   else if (nPetscIntContig) {
675:     if (nPetscIntContig%7 == 0) PackInit_PetscInt_7(link);
676:     else if (nPetscIntContig%5 == 0) PackInit_PetscInt_5(link);
677:     else if (nPetscIntContig%4 == 0) PackInit_PetscInt_4(link);
678:     else if (nPetscIntContig%3 == 0) PackInit_PetscInt_3(link);
679:     else if (nPetscIntContig%2 == 0) PackInit_PetscInt_2(link);
680:     else PackInit_PetscInt(link);
681:     link->bs = nPetscIntContig;
682:     link->unitbytes *= nPetscIntContig;
683:   } else if (nPetscRealContig) {
684:     if (nPetscRealContig%7 == 0) PackInit_PetscReal_7(link);
685:     else if (nPetscRealContig%5 == 0) PackInit_PetscReal_5(link);
686:     else if (nPetscRealContig%4 == 0) PackInit_PetscReal_4(link);
687:     else if (nPetscRealContig%3 == 0) PackInit_PetscReal_3(link);
688:     else if (nPetscRealContig%2 == 0) PackInit_PetscReal_2(link);
689:     else PackInit_PetscReal(link);
690:     link->bs = nPetscRealContig;
691:     link->unitbytes *= nPetscRealContig;
692: #if defined(PETSC_HAVE_COMPLEX)
693:   } else if (nPetscComplexContig) {
694:     if (nPetscComplexContig%7 == 0) PackInit_PetscComplex_7(link);
695:     else if (nPetscComplexContig%5 == 0) PackInit_PetscComplex_5(link);
696:     else if (nPetscComplexContig%4 == 0) PackInit_PetscComplex_4(link);
697:     else if (nPetscComplexContig%3 == 0) PackInit_PetscComplex_3(link);
698:     else if (nPetscComplexContig%2 == 0) PackInit_PetscComplex_2(link);
699:     else PackInit_PetscComplex_1(link);
700:     link->bs = nPetscComplexContig;
701:     link->unitbytes *= nPetscComplexContig;
702: #endif
703:   } else {
704:     MPI_Aint lb,bytes;
705:     MPI_Type_get_extent(unit,&lb,&bytes);
706:     if (lb != 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Datatype with nonzero lower bound %ld\n",(long)lb);
707:     if (bytes % sizeof(int)) { /* If the type size is not multiple of int */
708: #if PETSC_SIZEOF_INT == 8
709:       if      (bytes%7 == 0) {PackInit_block_char_7(link); link->bs = bytes/7;} /* Note the basic type is char[7] */
710:       else if (bytes%6 == 0) {PackInit_block_char_6(link); link->bs = bytes/6;}
711:       else if (bytes%5 == 0) {PackInit_block_char_5(link); link->bs = bytes/5;}
712:       else if (bytes%4 == 0) {PackInit_block_char_4(link); link->bs = bytes/4;}
713:       else
714: #endif
715:       if      (bytes%3 == 0) {PackInit_block_char_3(link); link->bs = bytes/3;}
716:       else if (bytes%2 == 0) {PackInit_block_char_2(link); link->bs = bytes/2;}
717:       else                   {PackInit_block_char_1(link); link->bs = bytes/1;}
718:       link->unitbytes = bytes;
719:     } else {
720:       PetscInt nInt = bytes / sizeof(int);
721:       if      (nInt%8 == 0)  {PackInit_block_int_8(link);  link->bs = nInt/8;} /* Note the basic type is int[8] */
722:       else if (nInt%7 == 0)  {PackInit_block_int_7(link);  link->bs = nInt/7;}
723:       else if (nInt%6 == 0)  {PackInit_block_int_6(link);  link->bs = nInt/6;}
724:       else if (nInt%5 == 0)  {PackInit_block_int_5(link);  link->bs = nInt/5;}
725:       else if (nInt%4 == 0)  {PackInit_block_int_4(link);  link->bs = nInt/4;}
726:       else if (nInt%3 == 0)  {PackInit_block_int_3(link);  link->bs = nInt/3;}
727:       else if (nInt%2 == 0)  {PackInit_block_int_2(link);  link->bs = nInt/2;}
728:       else                   {PackInit_block_int_1(link);  link->bs = nInt/1;}
729:       link->unitbytes = bytes;
730:     }
731:   }
732:   if (link->isbuiltin) link->unit = unit; /* builtin datatypes are common. Make it fast */
733:   else {MPI_Type_dup(unit,&link->unit);}
734:   return(0);
735: }

737: static PetscErrorCode PetscSFBasicPackGetUnpackOp(PetscSF sf,PetscSFBasicPack link,MPI_Op op,void (**UnpackOp)(PetscInt,PetscInt,const PetscInt*,void*,const void*))
738: {
740:   *UnpackOp = NULL;
741:   if (op == MPIU_REPLACE) *UnpackOp = link->UnpackInsert;
742:   else if (op == MPI_SUM || op == MPIU_SUM) *UnpackOp = link->UnpackAdd;
743:   else if (op == MPI_PROD) *UnpackOp = link->UnpackMult;
744:   else if (op == MPI_MAX || op == MPIU_MAX) *UnpackOp = link->UnpackMax;
745:   else if (op == MPI_MIN || op == MPIU_MIN) *UnpackOp = link->UnpackMin;
746:   else if (op == MPI_LAND) *UnpackOp = link->UnpackLAND;
747:   else if (op == MPI_BAND) *UnpackOp = link->UnpackBAND;
748:   else if (op == MPI_LOR) *UnpackOp = link->UnpackLOR;
749:   else if (op == MPI_BOR) *UnpackOp = link->UnpackBOR;
750:   else if (op == MPI_LXOR) *UnpackOp = link->UnpackLXOR;
751:   else if (op == MPI_BXOR) *UnpackOp = link->UnpackBXOR;
752:   else if (op == MPI_MAXLOC) *UnpackOp = link->UnpackMaxloc;
753:   else if (op == MPI_MINLOC) *UnpackOp = link->UnpackMinloc;
754:   else *UnpackOp = NULL;
755:   return(0);
756: }
757: static PetscErrorCode PetscSFBasicPackGetFetchAndOp(PetscSF sf,PetscSFBasicPack link,MPI_Op op,void (**FetchAndOp)(PetscInt,PetscInt,const PetscInt*,void*,void*))
758: {
760:   *FetchAndOp = NULL;
761:   if (op == MPIU_REPLACE) *FetchAndOp = link->FetchAndInsert;
762:   else if (op == MPI_SUM || op == MPIU_SUM) *FetchAndOp = link->FetchAndAdd;
763:   else if (op == MPI_MAX || op == MPIU_MAX) *FetchAndOp = link->FetchAndMax;
764:   else if (op == MPI_MIN || op == MPIU_MIN) *FetchAndOp = link->FetchAndMin;
765:   else if (op == MPI_MAXLOC) *FetchAndOp = link->FetchAndMaxloc;
766:   else if (op == MPI_MINLOC) *FetchAndOp = link->FetchAndMinloc;
767:   else if (op == MPI_PROD)   *FetchAndOp = link->FetchAndMult;
768:   else if (op == MPI_LAND)   *FetchAndOp = link->FetchAndLAND;
769:   else if (op == MPI_BAND)   *FetchAndOp = link->FetchAndBAND;
770:   else if (op == MPI_LOR)    *FetchAndOp = link->FetchAndLOR;
771:   else if (op == MPI_BOR)    *FetchAndOp = link->FetchAndBOR;
772:   else if (op == MPI_LXOR)   *FetchAndOp = link->FetchAndLXOR;
773:   else if (op == MPI_BXOR)   *FetchAndOp = link->FetchAndBXOR;
774:   else SETERRQ(PetscObjectComm((PetscObject)sf),PETSC_ERR_SUP,"No support for MPI_Op");
775:   return(0);
776: }

778: typedef enum {PETSC_SF_LEAF2../../../../../.._REDUCE, PETSC_SF_../../../../../..2LEAF_BCAST} PetscSFDirection;

780: static PetscErrorCode PetscSFBasicPackGetReqs(PetscSF sf,PetscSFBasicPack link,PetscSFDirection direction,MPI_Request **rootreqs,MPI_Request **leafreqs)
781: {
782:   PetscSF_Basic *bas   = (PetscSF_Basic*)sf->data;
783:   PetscInt       shift = (direction == PETSC_SF_LEAF2../../../../../.._REDUCE)? 0 : (sf->nranks + bas->niranks); /* reduce reqs are in the front, bcast reqs are at the end */

786:   if (rootreqs) *rootreqs = link->requests + shift;
787:   if (leafreqs) *leafreqs = link->requests + (bas->niranks - bas->ndiranks) + shift;
788:   return(0);
789: }

791: static PetscErrorCode PetscSFBasicPackWaitall(PetscSF sf,PetscSFBasicPack link,PetscSFDirection direction)
792: {
793:   PetscSF_Basic  *bas  = (PetscSF_Basic*)sf->data;
794:   PetscInt       shift = (direction == PETSC_SF_LEAF2../../../../../.._REDUCE)? 0 : (sf->nranks + bas->niranks);

798:   MPI_Waitall(bas->niranks+sf->nranks-(bas->ndiranks+sf->ndranks),link->requests+shift,MPI_STATUSES_IGNORE);
799:   return(0);
800: }

802: static PetscErrorCode PetscSFBasicGetRootInfo(PetscSF sf,PetscInt *nrootranks,PetscInt *ndrootranks,const PetscMPIInt **rootranks,const PetscInt **rootoffset,const PetscInt **rootloc)
803: {
804:   PetscSF_Basic *bas = (PetscSF_Basic*)sf->data;

807:   if (nrootranks)  *nrootranks  = bas->niranks;
808:   if (ndrootranks) *ndrootranks = bas->ndiranks;
809:   if (rootranks)   *rootranks   = bas->iranks;
810:   if (rootoffset)  *rootoffset  = bas->ioffset;
811:   if (rootloc)     *rootloc     = bas->irootloc;
812:   return(0);
813: }

815: static PetscErrorCode PetscSFBasicGetLeafInfo(PetscSF sf,PetscInt *nleafranks,PetscInt *ndleafranks,const PetscMPIInt **leafranks,const PetscInt **leafoffset,const PetscInt **leafloc)
816: {
818:   if (nleafranks)  *nleafranks  = sf->nranks;
819:   if (ndleafranks) *ndleafranks = sf->ndranks;
820:   if (leafranks)   *leafranks   = sf->ranks;
821:   if (leafoffset)  *leafoffset  = sf->roffset;
822:   if (leafloc)     *leafloc     = sf->rmine;
823:   return(0);
824: }

826: static PetscErrorCode PetscSFBasicGetPack(PetscSF sf,MPI_Datatype unit,const void *key,PetscSFBasicPack *mylink)
827: {
828:   PetscSF_Basic    *bas = (PetscSF_Basic*)sf->data;
829:   PetscErrorCode   ierr;
830:   PetscSFBasicPack link,*p;
831:   PetscInt         nrootranks,ndrootranks,nleafranks,ndleafranks,i,half;
832:   const PetscInt   *rootoffset,*leafoffset;
833:   MPI_Comm         comm;
834:   PetscMPIInt      n;
835:   MPI_Request      *rootreqs,*leafreqs;

838:   /* Look for types in cache */
839:   for (p=&bas->avail; (link=*p); p=&link->next) {
840:     PetscBool match;
841:     MPIPetsc_Type_compare(unit,link->unit,&match);
842:     if (match) {
843:       *p = link->next;          /* Remove from available list */
844:       goto found;
845:     }
846:   }

848:   /* Create new composite types for each send rank */
849:   PetscSFBasicGetRootInfo(sf,&nrootranks,&ndrootranks,NULL,&rootoffset,NULL);
850:   PetscSFBasicGetLeafInfo(sf,&nleafranks,&ndleafranks,NULL,&leafoffset,NULL);
851:   PetscNew(&link);
852:   PetscSFBasicPackTypeSetup(link,unit);
853:   PetscMalloc2(nrootranks,&link->root,nleafranks,&link->leaf);
854:   /* Double the requests. First half are used for reduce (leaf to root) communication, second half for bcast (root to leaf) communication */
855:   half     = nrootranks + nleafranks;
856:   PetscCalloc1(half*2,&link->requests);
857:   rootreqs = link->requests;
858:   leafreqs = link->requests + bas->niranks - bas->ndiranks;
859:   comm     = PetscObjectComm((PetscObject)sf);

861:   /* Allocate buffer and then init the persistent communcation */
862:   for (i=0; i<nrootranks; i++) {
863:     PetscMalloc((rootoffset[i+1]-rootoffset[i])*link->unitbytes,&link->root[i]);
864:     if (i >= ndrootranks) {
865:       PetscMPIIntCast(rootoffset[i+1]-rootoffset[i],&n);
866:       MPI_Recv_init(link->root[i],n,unit,bas->iranks[i],bas->tag,comm,&rootreqs[i-ndrootranks]);      /* reduce */
867:       MPI_Send_init(link->root[i],n,unit,bas->iranks[i],bas->tag,comm,&rootreqs[i-ndrootranks+half]); /* bcast  */
868:     }
869:   }
870:   for (i=0; i<nleafranks; i++) {
871:     if (i < ndleafranks) {      /* Leaf buffers for distinguished ranks are pointers directly into root buffers */
872:       if (ndrootranks != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Cannot match distinguished ranks");
873:       link->leaf[i] = link->root[0];
874:       continue;
875:     }
876:     PetscMalloc((leafoffset[i+1]-leafoffset[i])*link->unitbytes,&link->leaf[i]);
877:     PetscMPIIntCast(leafoffset[i+1]-leafoffset[i],&n);
878:     MPI_Send_init(link->leaf[i],n,unit,sf->ranks[i],bas->tag,comm,&leafreqs[i-ndleafranks]);      /* reduce */
879:     MPI_Recv_init(link->leaf[i],n,unit,sf->ranks[i],bas->tag,comm,&leafreqs[i-ndleafranks+half]); /* bcast  */
880:   }

882: found:
883:   link->key  = key;
884:   link->next = bas->inuse;
885:   bas->inuse = link;

887:   *mylink = link;
888:   return(0);
889: }

891: static PetscErrorCode PetscSFBasicGetPackInUse(PetscSF sf,MPI_Datatype unit,const void *key,PetscCopyMode cmode,PetscSFBasicPack *mylink)
892: {
893:   PetscSF_Basic    *bas = (PetscSF_Basic*)sf->data;
894:   PetscErrorCode   ierr;
895:   PetscSFBasicPack link,*p;

898:   /* Look for types in cache */
899:   for (p=&bas->inuse; (link=*p); p=&link->next) {
900:     PetscBool match;
901:     MPIPetsc_Type_compare(unit,link->unit,&match);
902:     if (match && (key == link->key)) {
903:       switch (cmode) {
904:       case PETSC_OWN_POINTER: *p = link->next; break; /* Remove from inuse list */
905:       case PETSC_USE_POINTER: break;
906:       default: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"invalid cmode");
907:       }
908:       *mylink = link;
909:       return(0);
910:     }
911:   }
912:   SETERRQ(PetscObjectComm((PetscObject)sf),PETSC_ERR_ARG_WRONGSTATE,"Could not find pack");
913:   return(0);
914: }

916: static PetscErrorCode PetscSFBasicReclaimPack(PetscSF sf,PetscSFBasicPack *link)
917: {
918:   PetscSF_Basic *bas = (PetscSF_Basic*)sf->data;

921:   (*link)->key  = NULL;
922:   (*link)->next = bas->avail;
923:   bas->avail    = *link;
924:   *link         = NULL;
925:   return(0);
926: }

928: static PetscErrorCode PetscSFSetFromOptions_Basic(PetscOptionItems *PetscOptionsObject,PetscSF sf)
929: {

933:   PetscOptionsHead(PetscOptionsObject,"PetscSF Basic options");
934:   PetscOptionsTail();
935:   return(0);
936: }

938: static PetscErrorCode PetscSFReset_Basic(PetscSF sf)
939: {
940:   PetscSF_Basic    *bas = (PetscSF_Basic*)sf->data;
941:   PetscErrorCode   ierr;
942:   PetscSFBasicPack link,next;

945:   if (bas->inuse) SETERRQ(PetscObjectComm((PetscObject)sf),PETSC_ERR_ARG_WRONGSTATE,"Outstanding operation has not been completed");
946:   PetscFree2(bas->iranks,bas->ioffset);
947:   PetscFree(bas->irootloc);
948:   for (link=bas->avail; link; link=next) {
949:     PetscInt i;
950:     next = link->next;
951:     if (!link->isbuiltin) {MPI_Type_free(&link->unit);}
952:     for (i=0; i<bas->niranks; i++) {PetscFree(link->root[i]);}
953:     for (i=sf->ndranks; i<sf->nranks; i++) {PetscFree(link->leaf[i]);} /* Free only non-distinguished leaf buffers */
954:     PetscFree2(link->root,link->leaf);
955:     /* Free persistent requests using MPI_Request_free */
956:     for (i=0; i<sf->nranks+bas->niranks-(sf->ndranks+bas->ndiranks); i++) {
957:       MPI_Request_free(&link->requests[i]); /* used in reduce */
958:       MPI_Request_free(&link->requests[sf->nranks+bas->niranks+i]); /* used in bcast */
959:     }
960:     PetscFree(link->requests);
961:     PetscFree(link);
962:   }
963:   bas->avail = NULL;
964:   return(0);
965: }

967: static PetscErrorCode PetscSFDestroy_Basic(PetscSF sf)
968: {

972:   PetscSFReset_Basic(sf);
973:   PetscFree(sf->data);
974:   return(0);
975: }

977: static PetscErrorCode PetscSFView_Basic(PetscSF sf,PetscViewer viewer)
978: {
979:   /* PetscSF_Basic *bas = (PetscSF_Basic*)sf->data; */
981:   PetscBool      iascii;

984:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
985:   if (iascii) {
986:     PetscViewerASCIIPrintf(viewer,"  sort=%s\n",sf->rankorder ? "rank-order" : "unordered");
987:   }
988:   return(0);
989: }

991: static PetscErrorCode PetscSFBcastAndOpBegin_Basic(PetscSF sf,MPI_Datatype unit,const void *rootdata,void *leafdata,MPI_Op op)
992: {
993:   PetscErrorCode    ierr;
994:   PetscSFBasicPack  link;
995:   PetscInt          i,nrootranks,ndrootranks,nleafranks,ndleafranks;
996:   const PetscInt    *rootoffset,*leafoffset,*rootloc,*leafloc;
997:   const PetscMPIInt *rootranks,*leafranks;
998:   MPI_Request       *rootreqs,*leafreqs;
999:   PetscMPIInt       n;

1002:   PetscSFBasicGetRootInfo(sf,&nrootranks,&ndrootranks,&rootranks,&rootoffset,&rootloc);
1003:   PetscSFBasicGetLeafInfo(sf,&nleafranks,&ndleafranks,&leafranks,&leafoffset,&leafloc);
1004:   PetscSFBasicGetPack(sf,unit,rootdata,&link);

1006:   PetscSFBasicPackGetReqs(sf,link,PETSC_SF_../../../../../..2LEAF_BCAST,&rootreqs,&leafreqs);
1007:   /* Eagerly post leaf receives, but only from non-distinguished ranks -- distinguished ranks will receive via shared memory */
1008:   PetscMPIIntCast(leafoffset[nleafranks]-leafoffset[ndleafranks],&n);
1009:   MPI_Startall_irecv(n,unit,nleafranks-ndleafranks,leafreqs);

1011:   /* Pack and send root data */
1012:   for (i=0; i<nrootranks; i++) {
1013:     void *packstart = link->root[i];
1014:     PetscMPIIntCast(rootoffset[i+1]-rootoffset[i],&n);
1015:     (*link->Pack)(n,link->bs,rootloc+rootoffset[i],rootdata,packstart);
1016:     if (i < ndrootranks) continue; /* shared memory */
1017:     MPI_Start_isend(n,unit,&rootreqs[i-ndrootranks]);
1018:   }
1019:   return(0);
1020: }

1022: PetscErrorCode PetscSFBcastAndOpEnd_Basic(PetscSF sf,MPI_Datatype unit,const void *rootdata,void *leafdata,MPI_Op op)
1023: {
1024:   PetscErrorCode   ierr;
1025:   PetscSFBasicPack link;
1026:   PetscInt         i,nleafranks,ndleafranks;
1027:   const PetscInt   *leafoffset,*leafloc;
1028:   void             (*UnpackOp)(PetscInt,PetscInt,const PetscInt*,void*,const void*);
1029:   PetscMPIInt      typesize = -1;

1032:   PetscSFBasicGetPackInUse(sf,unit,rootdata,PETSC_OWN_POINTER,&link);
1033:   PetscSFBasicPackWaitall(sf,link,PETSC_SF_../../../../../..2LEAF_BCAST);
1034:   PetscSFBasicGetLeafInfo(sf,&nleafranks,&ndleafranks,NULL,&leafoffset,&leafloc);
1035:   PetscSFBasicPackGetUnpackOp(sf,link,op,&UnpackOp);

1037:   if (UnpackOp) { typesize = link->unitbytes; }
1038:   else { MPI_Type_size(unit,&typesize); }

1040:   for (i=0; i<nleafranks; i++) {
1041:     PetscMPIInt n   = leafoffset[i+1] - leafoffset[i];
1042:     char *packstart = (char *) link->leaf[i];
1043:     if (UnpackOp) { (*UnpackOp)(n,link->bs,leafloc+leafoffset[i],leafdata,(const void *)packstart); }
1044: #if defined(PETSC_HAVE_MPI_REDUCE_LOCAL)
1045:     else if (n) { /* the op should be defined to operate on the whole datatype, so we ignore link->bs */
1046:       PetscInt j;
1047:       for (j=0; j<n; j++) { MPI_Reduce_local(packstart+j*typesize,((char *) leafdata)+(leafloc[leafoffset[i]+j])*typesize,1,unit,op); }
1048:     }
1049: #else
1050:     else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No unpacking reduction operation for this MPI_Op");
1051: #endif
1052:   }

1054:   PetscSFBasicReclaimPack(sf,&link);
1055:   return(0);
1056: }

1058: /* Send from roots to leaves */
1059: static PetscErrorCode PetscSFBcastBegin_Basic(PetscSF sf,MPI_Datatype unit,const void *rootdata,void *leafdata)
1060: {
1061:   PetscErrorCode   ierr;

1064:   PetscSFBcastAndOpBegin_Basic(sf,unit,rootdata,leafdata,MPI_REPLACE);
1065:   return(0);
1066: }

1068: PetscErrorCode PetscSFBcastEnd_Basic(PetscSF sf,MPI_Datatype unit,const void *rootdata,void *leafdata)
1069: {
1070:   PetscErrorCode   ierr;

1073:   PetscSFBcastAndOpEnd_Basic(sf,unit,rootdata,leafdata,MPI_REPLACE);
1074:   return(0);
1075: }

1077: /* leaf -> root with reduction */
1078: PetscErrorCode PetscSFReduceBegin_Basic(PetscSF sf,MPI_Datatype unit,const void *leafdata,void *rootdata,MPI_Op op)
1079: {
1080:   PetscSFBasicPack  link;
1081:   PetscErrorCode    ierr;
1082:   PetscInt          i,nrootranks,ndrootranks,nleafranks,ndleafranks;
1083:   const PetscInt    *rootoffset,*leafoffset,*rootloc,*leafloc;
1084:   const PetscMPIInt *rootranks,*leafranks;
1085:   MPI_Request       *rootreqs,*leafreqs;
1086:   PetscMPIInt       n;

1089:   PetscSFBasicGetRootInfo(sf,&nrootranks,&ndrootranks,&rootranks,&rootoffset,&rootloc);
1090:   PetscSFBasicGetLeafInfo(sf,&nleafranks,&ndleafranks,&leafranks,&leafoffset,&leafloc);
1091:   PetscSFBasicGetPack(sf,unit,leafdata,&link);

1093:   PetscSFBasicPackGetReqs(sf,link,PETSC_SF_LEAF2../../../../../.._REDUCE,&rootreqs,&leafreqs);
1094:   /* Eagerly post root receives for non-distinguished ranks */
1095:   PetscMPIIntCast(rootoffset[nrootranks]-rootoffset[ndrootranks],&n);
1096:   MPI_Startall_irecv(n,unit,nrootranks-ndrootranks,rootreqs);

1098:   /* Pack and send leaf data */
1099:   for (i=0; i<nleafranks; i++) {
1100:     void *packstart = link->leaf[i];
1101:     PetscMPIIntCast(leafoffset[i+1]-leafoffset[i],&n);
1102:     (*link->Pack)(n,link->bs,leafloc+leafoffset[i],leafdata,packstart);
1103:     if (i < ndleafranks) continue; /* shared memory */
1104:     MPI_Start_isend(n,unit,&leafreqs[i-ndleafranks]);
1105:   }
1106:   return(0);
1107: }

1109: static PetscErrorCode PetscSFReduceEnd_Basic(PetscSF sf,MPI_Datatype unit,const void *leafdata,void *rootdata,MPI_Op op)
1110: {
1111:   void             (*UnpackOp)(PetscInt,PetscInt,const PetscInt*,void*,const void*);
1112:   PetscErrorCode   ierr;
1113:   PetscSFBasicPack link;
1114:   PetscInt         i,nrootranks;
1115:   PetscMPIInt      typesize = -1;
1116:   const PetscInt   *rootoffset,*rootloc;

1119:   PetscSFBasicGetPackInUse(sf,unit,leafdata,PETSC_OWN_POINTER,&link);
1120:   /* This implementation could be changed to unpack as receives arrive, at the cost of non-determinism */
1121:   PetscSFBasicPackWaitall(sf,link,PETSC_SF_LEAF2../../../../../.._REDUCE);
1122:   PetscSFBasicGetRootInfo(sf,&nrootranks,NULL,NULL,&rootoffset,&rootloc);
1123:   PetscSFBasicPackGetUnpackOp(sf,link,op,&UnpackOp);
1124:   if (UnpackOp) {
1125:     typesize = link->unitbytes;
1126:   }
1127:   else {
1128:     MPI_Type_size(unit,&typesize);
1129:   }
1130:   for (i=0; i<nrootranks; i++) {
1131:     PetscMPIInt n   = rootoffset[i+1] - rootoffset[i];
1132:     char *packstart = (char *) link->root[i];

1134:     if (UnpackOp) {
1135:       (*UnpackOp)(n,link->bs,rootloc+rootoffset[i],rootdata,(const void *)packstart);
1136:     }
1137: #if defined(PETSC_HAVE_MPI_REDUCE_LOCAL)
1138:     else if (n) { /* the op should be defined to operate on the whole datatype, so we ignore link->bs */
1139:       PetscInt j;

1141:       for (j = 0; j < n; j++) {
1142:         MPI_Reduce_local(packstart+j*typesize,((char *) rootdata)+(rootloc[rootoffset[i]+j])*typesize,1,unit,op);
1143:       }
1144:     }
1145: #else
1146:     else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No unpacking reduction operation for this MPI_Op");
1147: #endif
1148:   }
1149:   PetscSFBasicReclaimPack(sf,&link);
1150:   return(0);
1151: }

1153: static PetscErrorCode PetscSFFetchAndOpBegin_Basic(PetscSF sf,MPI_Datatype unit,void *rootdata,const void *leafdata,void *leafupdate,MPI_Op op)
1154: {

1158:   PetscSFReduceBegin_Basic(sf,unit,leafdata,rootdata,op);
1159:   return(0);
1160: }

1162: static PetscErrorCode PetscSFFetchAndOpEnd_Basic(PetscSF sf,MPI_Datatype unit,void *rootdata,const void *leafdata,void *leafupdate,MPI_Op op)
1163: {
1164:   void              (*FetchAndOp)(PetscInt,PetscInt,const PetscInt*,void*,void*);
1165:   PetscErrorCode    ierr;
1166:   PetscSFBasicPack  link;
1167:   PetscInt          i,nrootranks,ndrootranks,nleafranks,ndleafranks;
1168:   const PetscInt    *rootoffset,*leafoffset,*rootloc,*leafloc;
1169:   const PetscMPIInt *rootranks,*leafranks;
1170:   MPI_Request       *rootreqs,*leafreqs;
1171:   PetscMPIInt       n;

1174:   PetscSFBasicGetPackInUse(sf,unit,leafdata,PETSC_OWN_POINTER,&link);
1175:   /* This implementation could be changed to unpack as receives arrive, at the cost of non-determinism */
1176:   PetscSFBasicPackWaitall(sf,link,PETSC_SF_LEAF2../../../../../.._REDUCE);
1177:   PetscSFBasicGetRootInfo(sf,&nrootranks,&ndrootranks,&rootranks,&rootoffset,&rootloc);
1178:   PetscSFBasicGetLeafInfo(sf,&nleafranks,&ndleafranks,&leafranks,&leafoffset,&leafloc);
1179:   PetscSFBasicPackGetReqs(sf,link,PETSC_SF_../../../../../..2LEAF_BCAST,&rootreqs,&leafreqs);
1180:   /* Post leaf receives */
1181:   PetscMPIIntCast(leafoffset[nleafranks]-leafoffset[ndleafranks],&n);
1182:   MPI_Startall_irecv(n,unit,nleafranks-ndleafranks,leafreqs);

1184:   /* Process local fetch-and-op, post root sends */
1185:   PetscSFBasicPackGetFetchAndOp(sf,link,op,&FetchAndOp);
1186:   for (i=0; i<nrootranks; i++) {
1187:     void *packstart = link->root[i];
1188:     PetscMPIIntCast(rootoffset[i+1]-rootoffset[i],&n);
1189:     (*FetchAndOp)(n,link->bs,rootloc+rootoffset[i],rootdata,packstart);
1190:     if (i < ndrootranks) continue; /* shared memory */
1191:     MPI_Start_isend(n,unit,&rootreqs[i-ndrootranks]);
1192:   }
1193:   PetscSFBasicPackWaitall(sf,link,PETSC_SF_../../../../../..2LEAF_BCAST);
1194:   for (i=0; i<nleafranks; i++) {
1195:     const void  *packstart = link->leaf[i];
1196:     PetscMPIIntCast(leafoffset[i+1]-leafoffset[i],&n);
1197:     (*link->UnpackInsert)(n,link->bs,leafloc+leafoffset[i],leafupdate,packstart);
1198:   }
1199:   PetscSFBasicReclaimPack(sf,&link);
1200:   return(0);
1201: }

1203: static PetscErrorCode PetscSFGetLeafRanks_Basic(PetscSF sf,PetscInt *niranks,const PetscMPIInt **iranks,const PetscInt **ioffset,const PetscInt **irootloc)
1204: {
1205:   PetscSF_Basic *bas = (PetscSF_Basic*)sf->data;

1208:   if (niranks)  *niranks  = bas->niranks;
1209:   if (iranks)   *iranks   = bas->iranks;
1210:   if (ioffset)  *ioffset  = bas->ioffset;
1211:   if (irootloc) *irootloc = bas->irootloc;
1212:   return(0);
1213: }

1215: PETSC_EXTERN PetscErrorCode PetscSFCreate_Basic(PetscSF sf)
1216: {
1217:   PetscSF_Basic  *bas = (PetscSF_Basic*)sf->data;

1221:   sf->ops->SetUp           = PetscSFSetUp_Basic;
1222:   sf->ops->SetFromOptions  = PetscSFSetFromOptions_Basic;
1223:   sf->ops->Reset           = PetscSFReset_Basic;
1224:   sf->ops->Destroy         = PetscSFDestroy_Basic;
1225:   sf->ops->View            = PetscSFView_Basic;
1226:   sf->ops->BcastBegin      = PetscSFBcastBegin_Basic;
1227:   sf->ops->BcastEnd        = PetscSFBcastEnd_Basic;
1228:   sf->ops->BcastAndOpBegin = PetscSFBcastAndOpBegin_Basic;
1229:   sf->ops->BcastAndOpEnd   = PetscSFBcastAndOpEnd_Basic;
1230:   sf->ops->ReduceBegin     = PetscSFReduceBegin_Basic;
1231:   sf->ops->ReduceEnd       = PetscSFReduceEnd_Basic;
1232:   sf->ops->FetchAndOpBegin = PetscSFFetchAndOpBegin_Basic;
1233:   sf->ops->FetchAndOpEnd   = PetscSFFetchAndOpEnd_Basic;
1234:   sf->ops->GetLeafRanks    = PetscSFGetLeafRanks_Basic;

1236:   PetscNewLog(sf,&bas);
1237:   sf->data = (void*)bas;
1238:   return(0);
1239: }