Zoltan2
Zoltan2_AlgZoltanCallbacks.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 #ifndef _ZOLTAN2_ALGZOLTANCALLBACKS_HPP_
46 #define _ZOLTAN2_ALGZOLTANCALLBACKS_HPP_
47 
48 #include <Zoltan2_MeshAdapter.hpp>
50 #include <Zoltan2_GraphAdapter.hpp>
53 
55 
56 #include <Zoltan2_Util.hpp>
57 #include <Zoltan2_TPLTraits.hpp>
58 #include <zoltan_cpp.h>
59 
63 // Callbacks based on Adapter; specializations provided where needed
64 
65 namespace Zoltan2 {
66 
68 // CALLBACKS SHARED BY MANY ADAPTERS
70 
72 // ZOLTAN_NUM_OBJ_FN
73 template <typename Adapter>
74 static int zoltanNumObj(void *data, int *ierr) {
75  const Adapter *adp = static_cast<Adapter *>(data);
76  *ierr = ZOLTAN_OK;
77  return int(adp->getLocalNumIDs());
78 }
79 
81 // ZOLTAN_OBJ_LIST_FN
82 template <typename Adapter>
83 static void zoltanObjList(void *data, int nGidEnt, int nLidEnt,
84  ZOLTAN_ID_PTR gids, ZOLTAN_ID_PTR lids,
85  int wdim, float *wgts, int *ierr)
86 {
87  const Adapter *adp = static_cast<Adapter *>(data);
88  *ierr = ZOLTAN_OK;
89 
90  size_t mynObj = adp->getLocalNumIDs();
91 
92  const typename Adapter::gno_t *myids = NULL;
93  adp->getIDsView(myids);
94  for (size_t i = 0; i < mynObj; i++) {
95  gids[i] = ZOLTAN_ID_TYPE(myids[i]); // TODO TRAITS CONVERSION MAY BE NEEDED
96  lids[i] = ZOLTAN_ID_TYPE(i); // TODO TRAITS CONVERSION MAY BE NEEDED
97  }
98 
99  if (wdim) {
100  int mywdim = adp->getNumWeightsPerID();
101  for (int w = 0; w < wdim; w++) {
102  if (w < mywdim) {
103  // copy weights from adapter
104  const typename Adapter::scalar_t *mywgts;
105  int mystride;
106  adp->getWeightsView(mywgts, mystride, w);
107  for (size_t i = 0; i < mynObj; i++)
108  wgts[i*wdim+w] = float(mywgts[i*mystride]);
109  }
110  else {
111  // provide uniform weights
112  for (size_t i = 0; i < mynObj; i++)
113  wgts[i*wdim+w] = 1.;
114  }
115  }
116  }
117 }
118 
120 // ZOLTAN_PART_MULTI_FN
121 template <typename Adapter>
122 static void zoltanParts(void *data, int nGidEnt, int nLidEnt, int nObj,
123  ZOLTAN_ID_PTR gids, ZOLTAN_ID_PTR lids,
124  int *parts, int *ierr)
125 {
126  const Adapter *adp = static_cast<Adapter *>(data);
127  *ierr = ZOLTAN_OK;
128  const typename Adapter::part_t *myparts;
129  adp->getPartsView(myparts);
130  // User parts from input adapter
131  for (int i = 0; i < nObj; i++)
132  parts[i] = int(myparts[lids[i]]);
133 }
134 
136 // ZOLTAN_NUM_GEOM_FN
137 template <typename Adapter>
138 static int zoltanNumGeom(void *data, int *ierr)
139 {
140  const Adapter *adp = static_cast<Adapter *>(data);
141  *ierr = ZOLTAN_OK;
142  return adp->getDimension();
143 }
144 
146 // ZOLTAN_GEOM_MULTI_FN
147 template <typename Adapter>
148 static void zoltanGeom(void *data, int nGidEnt, int nLidEnt, int nObj,
149  ZOLTAN_ID_PTR gids, ZOLTAN_ID_PTR lids,
150  int nDim, double *coords, int *ierr)
151 {
152  const Adapter *adp = static_cast<Adapter *>(data);
153  *ierr = ZOLTAN_OK;
154 
155  for (int d = 0; d < nDim; d++) {
156  const typename Adapter::scalar_t *mycoords;
157  int mystride;
158  adp->getCoordinatesView(mycoords, mystride, d);
159  for (int i = 0; i < nObj; i++)
160  coords[i*nDim+d] = double(mycoords[lids[i]*mystride]);
161  }
162 }
163 
165 // MATRIX ADAPTER CALLBACKS
167 
169 // ZOLTAN_HG_SIZE_CS_FN
170 template <typename Adapter>
172  void *data, int *nEdges, int *nPins,
173  int *format, int *ierr
174 )
175 {
176  std::cout << "HELLO FROM HGSizeCS with MATRIX ADAPTER" << std::endl;
177  *ierr = ZOLTAN_FATAL;
178 }
179 
181 // ZOLTAN_HG_CS_FN
182 template <typename Adapter>
184  void *data, int nGidEnt, int nEdges, int nPins,
185  int format, ZOLTAN_ID_PTR edgeIds,
186  int *edgeIdx, ZOLTAN_ID_PTR pinIds, int *ierr
187 )
188 {
189  std::cout << "HELLO FROM HGCS with MATRIX ADAPTER" << std::endl;
190  *ierr = ZOLTAN_FATAL;
191 }
192 
194 // TODO: GRAPH ADAPTER CALLBACKS
195 
196 
198 // HYPERGRAPH MODEL CALLBACKS
200 
202 // ZOLTAN_NUM_OBJ_FN
203 template <typename Adapter>
204 static int zoltanHGModelNumObj(void *data, int *ierr) {
205  const HyperGraphModel<Adapter>* mdl = static_cast<HyperGraphModel<Adapter>* >(data);
206  *ierr = ZOLTAN_OK;
207  return int(mdl->getLocalNumOwnedVertices());
208 }
209 
211 // ZOLTAN_OBJ_LIST_FN
212 template <typename Adapter>
213 static void zoltanHGModelObjList(void *data, int nGidEnt, int nLidEnt,
214  ZOLTAN_ID_PTR gids, ZOLTAN_ID_PTR lids,
215  int wdim, float *wgts, int *ierr)
216 {
217  const HyperGraphModel<Adapter>* mdl = static_cast<HyperGraphModel<Adapter>* >(data);
218  typedef typename Adapter::gno_t gno_t;
219  typedef typename Adapter::lno_t lno_t;
220  typedef typename Adapter::scalar_t scalar_t;
221  typedef StridedData<lno_t, scalar_t> input_t;
222 
223  *ierr = ZOLTAN_OK;
224  ArrayView<const gno_t> Ids;
225  ArrayView<input_t> model_wgts;
226  size_t num_verts = mdl->getVertexList(Ids,model_wgts);
227  ArrayView<bool> isOwner;
228  mdl->getOwnedList(isOwner);
229  int j=0;
230  for (size_t i=0;i<num_verts;i++) {
231  if (isOwner[i]) {
232  lids[j] = i;
233  gids[j] = Ids[i];
234  j++;
235  }
236  }
237  if (wdim) {
238  int mywdim = mdl->getNumWeightsPerVertex();
239  for (int w = 0; w < wdim; w++) {
240  j=0;
241  if (w < mywdim) {
242  for (size_t i = 0; i < num_verts; i++) {
243  if (isOwner[i]) {
244  wgts[j*wdim+w] = float(model_wgts[w][i]);
245  j++;
246  }
247  }
248  }
249  else {
250  // provide uniform weights
251  for (size_t i = 0; i < num_verts; i++) {
252  if (isOwner[i]) {
253  wgts[j*wdim+w] = 1.;
254  j++;
255  }
256  }
257  }
258  }
259  }
260 }
261 
263 // ZOLTAN_HG_SIZE_CS_FN
264 template <typename Adapter>
266  void *data, int *nEdges, int *nPins,
267  int *format, int *ierr
268 )
269 {
270  *ierr = ZOLTAN_OK;
271  const HyperGraphModel<Adapter>* mdl = static_cast<HyperGraphModel<Adapter>* >(data);
272  *nEdges = mdl->getLocalNumHyperEdges();
273  *nPins = mdl->getLocalNumPins();
274  if (mdl->getCentricView()==HYPEREDGE_CENTRIC)
275  *format = ZOLTAN_COMPRESSED_EDGE;
276  else
277  *format = ZOLTAN_COMPRESSED_VERTEX;
278 }
279 
281 // ZOLTAN_HG_CS_FN
282 template <typename Adapter>
284  void *data, int nGidEnt, int nEdges, int nPins,
285  int format, ZOLTAN_ID_PTR edgeIds,
286  int *edgeIdx, ZOLTAN_ID_PTR pinIds, int *ierr
287 )
288 {
289  *ierr = ZOLTAN_OK;
290  const HyperGraphModel<Adapter>* mdl = static_cast<HyperGraphModel<Adapter>* >(data);
291  typedef typename Adapter::gno_t gno_t;
292  typedef typename Adapter::lno_t lno_t;
293  typedef typename Adapter::scalar_t scalar_t;
294  typedef StridedData<lno_t, scalar_t> input_t;
295 
296  ArrayView<const gno_t> Ids;
297  ArrayView<input_t> wgts;
298  mdl->getEdgeList(Ids,wgts);
299  ArrayView<const gno_t> pinIds_;
300  ArrayView<const lno_t> offsets;
301  ArrayView<input_t> pin_wgts;
302  mdl->getPinList(pinIds_,offsets,pin_wgts);
303  for (int i=0;i<nEdges;i++) {
304  edgeIds[i]=Ids[i];
305  edgeIdx[i]=offsets[i];
306  }
307 
308  for (int i=0;i<nPins;i++)
309  pinIds[i] = pinIds_[i];
310 }
311 
313 // MESH ADAPTER CALLBACKS
315 
317 // ZOLTAN_HG_SIZE_CS_FN
318 template <typename Adapter>
320  void *data, int *nEdges, int *nPins,
321  int *format, int *ierr
322 )
323 {
324  *ierr = ZOLTAN_OK;
325  typedef typename Adapter::user_t user_t;
326  const MeshAdapter<user_t>* madp = static_cast<MeshAdapter<user_t>* >(data);
327  *nEdges = madp->getLocalNumOf(madp->getAdjacencyEntityType());
328  *nPins = madp->getLocalNumAdjs(madp->getAdjacencyEntityType(),madp->getPrimaryEntityType());
329  *format = ZOLTAN_COMPRESSED_EDGE;
330 }
331 
333 // ZOLTAN_HG_CS_FN
334 template <typename Adapter>
336  void *data, int nGidEnt, int nEdges, int nPins,
337  int format, ZOLTAN_ID_PTR edgeIds,
338  int *edgeIdx, ZOLTAN_ID_PTR pinIds, int *ierr
339 )
340 {
341  *ierr = ZOLTAN_OK;
342  typedef typename Adapter::gno_t gno_t;
343  typedef typename Adapter::lno_t lno_t;
344  typedef typename Adapter::user_t user_t;
345  const MeshAdapter<user_t>* madp = static_cast<MeshAdapter<user_t>*>(data);
346  const gno_t *Ids;
347  madp->getIDsViewOf(madp->getAdjacencyEntityType(),Ids);
348  const lno_t* offsets;
349  const gno_t* adjIds;
351  offsets, adjIds);
352  for (int i=0;i<nEdges;i++) {
353  edgeIds[i]=Ids[i];
354  edgeIdx[i]=offsets[i];
355  }
356  for (int i=0;i<nPins;i++)
357  pinIds[i] = adjIds[i];
358 }
359 
360 }
361 
362 
363 #endif
static int zoltanNumGeom(void *data, int *ierr)
static void zoltanHGCSForMeshAdapter(void *data, int nGidEnt, int nEdges, int nPins, int format, ZOLTAN_ID_PTR edgeIds, int *edgeIdx, ZOLTAN_ID_PTR pinIds, int *ierr)
static void zoltanHGModelCSForMeshAdapter(void *data, int nGidEnt, int nEdges, int nPins, int format, ZOLTAN_ID_PTR edgeIds, int *edgeIdx, ZOLTAN_ID_PTR pinIds, int *ierr)
Defines the MeshAdapter interface.
CentricView getCentricView() const
Returns the centric view of the hypergraph.
MeshAdapter defines the interface for mesh input.
virtual size_t getLocalNumOf(MeshEntityType etype) const =0
Returns the global number of mesh entities of MeshEntityType.
size_t getPinList(ArrayView< const gno_t > &pinIds, ArrayView< const lno_t > &offsets, ArrayView< input_t > &wgts) const
Sets pointers to this process&#39; pins global Ids based on the centric view given by getCentricView() ...
Defines the IdentifierAdapter interface.
Defines the VectorAdapter interface.
enum MeshEntityType getAdjacencyEntityType() const
Returns the entity that describes adjacencies between the entities to be partitioned, ordered, colored, etc. That is, a primaryEntityType that contains an adjacencyEntityType are adjacent.
static void zoltanObjList(void *data, int nGidEnt, int nLidEnt, ZOLTAN_ID_PTR gids, ZOLTAN_ID_PTR lids, int wdim, float *wgts, int *ierr)
static int zoltanNumObj(void *data, int *ierr)
size_t getOwnedList(ArrayView< bool > &isOwner) const
Sets pointer to the ownership of this processes vertices.
static void zoltanHGModelSizeCSForMeshAdapter(void *data, int *nEdges, int *nPins, int *format, int *ierr)
virtual void getIDsViewOf(MeshEntityType etype, gno_t const *&Ids) const =0
Provide a pointer to this process&#39; identifiers.
size_t getEdgeList(ArrayView< const gno_t > &Ids, ArrayView< input_t > &wgts) const
Sets pointers to this process&#39; hyperedge Ids and their weights.
static void zoltanGeom(void *data, int nGidEnt, int nLidEnt, int nObj, ZOLTAN_ID_PTR gids, ZOLTAN_ID_PTR lids, int nDim, double *coords, int *ierr)
size_t getLocalNumHyperEdges() const
Returns the number of hyper edges on this process. These are all hyper edges that have an adjacency t...
virtual size_t getLocalNumAdjs(MeshEntityType source, MeshEntityType target) const
Returns the number of first adjacencies on this process.
The StridedData class manages lists of weights or coordinates.
virtual void getAdjsView(MeshEntityType source, MeshEntityType target, const lno_t *&offsets, const gno_t *&adjacencyIds) const
Sets pointers to this process&#39; mesh first adjacencies.
size_t getLocalNumPins() const
Returns the local number of pins.
size_t getVertexList(ArrayView< const gno_t > &Ids, ArrayView< input_t > &wgts) const
Sets pointers to this process&#39; vertex Ids and their weights.
Traits class to handle conversions between gno_t/lno_t and TPL data types (e.g., ParMETIS&#39;s idx_t...
static int zoltanHGModelNumObj(void *data, int *ierr)
static void zoltanParts(void *data, int nGidEnt, int nLidEnt, int nObj, ZOLTAN_ID_PTR gids, ZOLTAN_ID_PTR lids, int *parts, int *ierr)
static void zoltanHGSizeCSForMeshAdapter(void *data, int *nEdges, int *nPins, int *format, int *ierr)
static void zoltanHGCSForMatrixAdapter(void *data, int nGidEnt, int nEdges, int nPins, int format, ZOLTAN_ID_PTR edgeIds, int *edgeIdx, ZOLTAN_ID_PTR pinIds, int *ierr)
static void zoltanHGModelObjList(void *data, int nGidEnt, int nLidEnt, ZOLTAN_ID_PTR gids, ZOLTAN_ID_PTR lids, int wdim, float *wgts, int *ierr)
Defines the HyperGraphModel interface.
Defines the MatrixAdapter interface.
Defines the GraphAdapter interface.
A gathering of useful namespace methods.
HyperGraphModel defines the interface required for hyper graph models.
enum MeshEntityType getPrimaryEntityType() const
Returns the entity to be partitioned, ordered, colored, etc.
static void zoltanHGSizeCSForMatrixAdapter(void *data, int *nEdges, int *nPins, int *format, int *ierr)
int getNumWeightsPerVertex() const
Returns the number (0 or greater) of weights per vertex.
size_t getLocalNumOwnedVertices() const
Returns the number vertices on this process that are owned.