xref: /petsc/src/vec/is/sf/impls/basic/sfpack.h (revision b23bfdefca792e3d9f47034521b8d4c437693123)
1 #if !defined(__SFPACK_H)
2 #define __SFPACK_H
3 
4 #include <petsc/private/sfimpl.h> /*I "petscsf.h" I*/
5 
6 /* Optimization plans in packing & unpacking for destination ranks.
7 
8   Suppose there are count indices stored in idx[], and two addresses u, p. We want to do packing:
9      p[i] = u[idx[i]], for i in [0,count)
10 
11   Often, the indices are associated with n ranks. Each rank's indices are stored consecutively in idx[].
12   We analyze indices for each rank and see if they are patterns that can be used to optimize the packing.
13   The result is stored in PetscSFPackOpt. Packing for a rank might be not optimizable, or optimized in
14   to a small number of contiguous memory copies or one strided memory copy.
15  */
16 typedef enum {PETSCSF_PACKOPT_NONE=0, PETSCSF_PACKOPT_MULTICOPY, PETSCSF_PACKOPT_STRIDE} PetscSFPackOptType;
17 
18 struct _n_PetscSFPackOpt {
19   PetscInt           n;             /* Number of destination ranks */
20   PetscSFPackOptType *type;         /* [n] Optimization types for the n ranks */
21   PetscInt           *offset;       /* [n+1] Indices for i-th rank are in [offset[i],offset[i+1]) of idx[] */
22   PetscInt           *copy_offset;  /* [n+1] If type[i] = PETSCSF_PACKOPT_MULTICOPY, packing for i-th rank is optimized into copies numbered between [copy_offset[i],copy_offset[i+1]) */
23   PetscInt           *copy_start;   /* [*]     j-th copy starts at copy_start[j] in idx[]. In other words, there are copy_length[j] contiguous indices */
24   PetscInt           *copy_length;  /* [*]     starting from idx[copy_start[j]] */
25   PetscInt           *stride_step;  /* [n]   If type[i] = PETSCSF_PACKOPT_STRIDE, then packing for i-th rank is strided, with first index being idx[offset[i]] and step stride_step[i], */
26   PetscInt           *stride_n;     /* [n]     and total stride_n[i] steps */
27 };
28 
29 typedef struct _n_PetscSFPack* PetscSFPack;
30 
31 #define SFPACKHEADER \
32   PetscErrorCode (*Pack)           (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,const void*,void*);  \
33   PetscErrorCode (*UnpackAndInsert)(PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,const void*);  \
34   PetscErrorCode (*UnpackAndAdd)   (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,const void*);  \
35   PetscErrorCode (*UnpackAndMin)   (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,const void*);  \
36   PetscErrorCode (*UnpackAndMax)   (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,const void*);  \
37   PetscErrorCode (*UnpackAndMinloc)(PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,const void*);  \
38   PetscErrorCode (*UnpackAndMaxloc)(PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,const void*);  \
39   PetscErrorCode (*UnpackAndMult)  (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,const void*);  \
40   PetscErrorCode (*UnpackAndLAND)  (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,const void*);  \
41   PetscErrorCode (*UnpackAndBAND)  (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,const void*);  \
42   PetscErrorCode (*UnpackAndLOR)   (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,const void*);  \
43   PetscErrorCode (*UnpackAndBOR)   (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,const void*);  \
44   PetscErrorCode (*UnpackAndLXOR)  (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,const void*);  \
45   PetscErrorCode (*UnpackAndBXOR)  (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,const void*);  \
46   PetscErrorCode (*FetchAndInsert) (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,void*);        \
47   PetscErrorCode (*FetchAndAdd)    (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,void*);        \
48   PetscErrorCode (*FetchAndMin)    (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,void*);        \
49   PetscErrorCode (*FetchAndMax)    (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,void*);        \
50   PetscErrorCode (*FetchAndMinloc) (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,void*);        \
51   PetscErrorCode (*FetchAndMaxloc) (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,void*);        \
52   PetscErrorCode (*FetchAndMult)   (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,void*);        \
53   PetscErrorCode (*FetchAndLAND)   (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,void*);        \
54   PetscErrorCode (*FetchAndBAND)   (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,void*);        \
55   PetscErrorCode (*FetchAndLOR)    (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,void*);        \
56   PetscErrorCode (*FetchAndBOR)    (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,void*);        \
57   PetscErrorCode (*FetchAndLXOR)   (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,void*);        \
58   PetscErrorCode (*FetchAndBXOR)   (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,void*);        \
59   PetscMPIInt    tag;         /* Each link has a tag so we can perform multiple SF ops at the same time */\
60   MPI_Datatype   unit;                                                                                    \
61   MPI_Datatype   basicunit;   /* unit is made of MPI builtin dataype basicunit */                         \
62   PetscBool      isbuiltin;   /* Is unit an MPI builtin datatype? If it is true, basicunit=unit, bs=1 */  \
63   size_t         unitbytes;   /* Number of bytes in a unit */                                             \
64   PetscInt       bs;          /* Number of basic units in a unit */                                       \
65   const void     *rkey,*lkey; /* rootdata and leafdata used as keys for operation */                      \
66   char           *rootbuf;       /* Buffer for packed roots in send/recv */                                                         \
67   char           *leafbuf;       /* Buffer for packed leaves in send/recv */                                                        \
68   char           *selfbuf;       /* If self communication does not use MPI, this is the shared buffer for packed roots or leaves */ \
69   PetscSFPack    next
70 
71 /* An abstract class that defines a communication link, which includes how to
72    pack/unpack data. Subclasses may further contain fields for send/recv buffers,
73    MPI_Requests etc used in communication.
74  */
75 struct _n_PetscSFPack {
76   SFPACKHEADER;
77 };
78 
79 PETSC_INTERN PetscErrorCode PetscSFPackGetInUse(PetscSF,MPI_Datatype,const void*,const void*,PetscCopyMode,PetscSFPack*);
80 PETSC_INTERN PetscErrorCode PetscSFPackReclaim(PetscSF,PetscSFPack*);
81 PETSC_INTERN PetscErrorCode PetscSFPackSetupType(PetscSFPack,MPI_Datatype);
82 PETSC_INTERN PetscErrorCode PetscSFPackGetUnpackAndOp(PetscSF,PetscSFPack,MPI_Op,PetscErrorCode (**UnpackAndOp)(PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,const void*));
83 PETSC_INTERN PetscErrorCode PetscSFPackGetFetchAndOp (PetscSF,PetscSFPack,MPI_Op,PetscErrorCode (**FetchAndOp) (PetscInt,const PetscInt*,PetscInt,PetscSFPackOpt,void*,      void*));
84 PETSC_INTERN PetscErrorCode PetscSFPackSetupOptimization(PetscInt,const PetscInt*,const PetscInt*,PetscSFPackOpt*);
85 PETSC_INTERN PetscErrorCode PetscSFPackDestoryOptimization(PetscSFPackOpt *out);
86 PETSC_INTERN PetscErrorCode PetscSFPackSetErrorOnUnsupportedOverlap(PetscSF,MPI_Datatype,const void*,const void*);
87 
88 #endif
89