Debian Codegraph @

packages/isorropia/src/epetra/zoltanLib/QueryObject.cpp

raw
1 //@HEADER
2 //************************************************************************
3 //
4 // Isorropia: Partitioning and Load Balancing Package
5 // Copyright (2006) Sandia Corporation
6 //
7 //Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 //license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 //************************************************************************
38 //@HEADER
39
40 #include <QueryObject.hpp>
41 #include <Isorropia_EpetraCostDescriber.hpp>
42
43 #include <Epetra_CrsGraph.h>
44 #include <Epetra_Comm.h>
45 #include <Epetra_BlockMap.h>
46 #include <Epetra_Map.h>
47 #include <Epetra_Vector.h>
48 #include <Epetra_RowMatrix.h>
49
50 #include <map>
51
52 // if non-zero, print out query function responses
53 //#define DEBUG_QUERIES 1
54 // process to print out responses, or -1 for all
55 //#define DEBUG_PROC -1
56
57 namespace Isorropia{
58
59 namespace Epetra {
60
61 namespace ZoltanLib{
62
63
64
65 QueryObject::QueryObject( Teuchos::RCP<const Epetra_CrsGraph> graph,
66 Teuchos::RCP<const Isorropia::Epetra::CostDescriber> costs,
67 int inputType)
68 : haveGraph_(true) ,
69 graph_(graph),
70 matrix_(0),
71 coords_(0),
72 rowMap_(&(graph->RowMap())),
73 colMap_(&(graph->ColMap())),
74 costs_(costs),
75 weights_(0),
76 input_type_(inputType)
77 {
78 myProc_ = graph->Comm().MyPID();
79 base_ = rowMap_->IndexBase();
80
81 // If graph
82 if (input_type_ == graph_input_)
83 {
84
85 // graph queries need to know processes owning my column entries
86 fill_procmap();
87
88 if (graph->NumMyDiagonals() > 0){
89 // In graph partitioning, we need to omit graph diagonal entries
90 // (self edges) from the query functions.
91
92 int nRows = rowMap_->NumMyElements();
93 int *rowGIDs = rowMap_->MyGlobalElements();
94
95 for (int i=0; i < nRows; i++){
96
97 int numEntries;
98 int *idx;
99
100 graph->ExtractMyRowView(i, numEntries, idx);
101
102 for (int j=0; j<numEntries; j++){
103 if (rowGIDs[i] == colMap_->GID(idx[j])){
104 graph_self_edges_.insert(rowGIDs[i]);
105 }
106 }
107 }
108 }
109 }
110 }
111
112 QueryObject::QueryObject( Teuchos::RCP<const Epetra_RowMatrix> matrix,
113 Teuchos::RCP<const Isorropia::Epetra::CostDescriber> costs,
114 int inputType)
115 : haveGraph_(false),
116 graph_(0),
117 matrix_(matrix),
118 coords_(0),
119 rowMap_((const Epetra_BlockMap*)&(matrix->RowMatrixRowMap())),
120 colMap_((const Epetra_BlockMap*)&(matrix->RowMatrixColMap())),
121 costs_(costs),
122 weights_(0),
123 input_type_(inputType)
124 {
125 myProc_ = matrix->Comm().MyPID();
126 base_ = rowMap_->IndexBase();
127
128 // If graph or hierarchical with graph and hypergraph
129 if (input_type_ == graph_input_ || input_type_ == hgraph_graph_input_)
130 {
131 // graph queries need to know processes owning my column entries
132 fill_procmap();
133
134 if (matrix->NumMyDiagonals() > 0){
135 // In graph partitioning, we need to omit graph diagonal entries
136 // (self edges) from the query functions.
137
138 int *rowGIDs;
139 int nRows;
140
141 Epetra_Vector diagonal(matrix->RowMatrixRowMap());
142 nRows = rowMap_->NumMyElements();
143 rowGIDs = rowMap_->MyGlobalElements();
144 matrix->ExtractDiagonalCopy(diagonal);
145
146 for (int i=0; i < nRows; i++){
147 if (diagonal[i] != 0){
148 graph_self_edges_.insert(rowGIDs[i]);
149 }
150 }
151 }
152 }
153 else if(input_type_ != hgraph2d_finegrain_input_) // otherwise must be hypergraph
154 {
155 input_type_ = hgraph_input_;
156 }
157 }
158
159 // For geometric partitioning
160 QueryObject::QueryObject( Teuchos::RCP<const Epetra_MultiVector> coords,
161 Teuchos::RCP<const Epetra_MultiVector> weights)
162 : haveGraph_(false),
163 graph_(0),
164 matrix_(0),
165 coords_(coords),
166 rowMap_(&(coords->Map())),
167 costs_(0),
168 weights_(weights)
169 {
170 myProc_ = rowMap_->Comm().MyPID();
171 base_ = rowMap_->IndexBase();
172 input_type_ = geometric_input_;
173 }
174
175
176 ////////////////////////////////////////////////////////////////////////////////
177 ////////////////////////////////////////////////////////////////////////////////
178 QueryObject::QueryObject( Teuchos::RCP<const Epetra_BlockMap> input_map,
179 int inputType)
180 : haveGraph_(false) ,
181 graph_(0),
182 matrix_(0),
183 coords_(0),
184 rowMap_(input_map.getRawPtr()), // MMW: this is dangerous but so are all of the above rowMap_ assignments
185 costs_(0),
186 weights_(0),
187 input_type_(inputType)
188 {
189 myProc_ = rowMap_->Comm().MyPID();
190 base_ = rowMap_->IndexBase();
191 }
192 ////////////////////////////////////////////////////////////////////////////////
193
194 ////////////////////////////////////////////////////////////////////////////////
195 // For hierarchical with graph or hgraph and geometric
196 ////////////////////////////////////////////////////////////////////////////////
197 QueryObject::QueryObject(Teuchos::RCP<const Epetra_CrsGraph> graph,
198 Teuchos::RCP<const Isorropia::Epetra::CostDescriber> costs,
199 Teuchos::RCP<const Epetra_MultiVector> coords,
200 Teuchos::RCP<const Epetra_MultiVector> weights, int inputType)
201 : haveGraph_(true) ,
202 graph_(graph),
203 matrix_(0),
204 coords_(coords),
205 rowMap_(&(graph->RowMap())),
206 colMap_(&(graph->ColMap())),
207 costs_(costs),
208 weights_(weights),
209 input_type_(inputType)
210 {
211 myProc_ = graph->Comm().MyPID();
212 base_ = rowMap_->IndexBase();
213
214 // if graph
215 if (input_type_ == graph_geometric_input_ ||
216 input_type_ == hgraph_graph_geometric_input_)
217 {
218
219 // graph queries need to know processes owning my column entries
220 fill_procmap();
221
222 if (graph->NumMyDiagonals() > 0){
223 // In graph partitioning, we need to omit graph diagonal entries
224 // (self edges) from the query functions.
225
226 int nRows = rowMap_->NumMyElements();
227 int *rowGIDs = rowMap_->MyGlobalElements();
228
229 for (int i=0; i < nRows; i++){
230
231 int numEntries;
232 int *idx;
233
234 graph->ExtractMyRowView(i, numEntries, idx);
235
236 for (int j=0; j<numEntries; j++){
237 if (rowGIDs[i] == colMap_->GID(idx[j])){
238 graph_self_edges_.insert(rowGIDs[i]);
239 }
240 }
241 }
242 }
243 }
244 }
245 ////////////////////////////////////////////////////////////////////////////////
246
247 ////////////////////////////////////////////////////////////////////////////////
248 // For hierarchical with graph or hgraph and geometric
249 ////////////////////////////////////////////////////////////////////////////////
250 QueryObject::QueryObject(Teuchos::RCP<const Epetra_RowMatrix> matrix,
251 Teuchos::RCP<const Isorropia::Epetra::CostDescriber> costs,
252 Teuchos::RCP<const Epetra_MultiVector> coords,
253 Teuchos::RCP<const Epetra_MultiVector> weights,
254 int inputType)
255 : haveGraph_(false),graph_(0), matrix_(matrix),coords_(coords),
256 rowMap_((const Epetra_BlockMap*)&(matrix->RowMatrixRowMap())),
257 colMap_((const Epetra_BlockMap*)&(matrix->RowMatrixColMap())),
258 costs_(costs), weights_(weights), input_type_(inputType)
259 {
260 myProc_ = matrix->Comm().MyPID();
261 base_ = rowMap_->IndexBase();
262
263 // If graph or hierarchical with graph and hypergraph
264 if (input_type_ == graph_geometric_input_ || input_type_ == hgraph_graph_geometric_input_)
265 {
266 // graph queries need to know processes owning my column entries
267 fill_procmap();
268
269 if (matrix->NumMyDiagonals() > 0){
270 // In graph partitioning, we need to omit graph diagonal entries
271 // (self edges) from the query functions.
272
273 int *rowGIDs;
274 int nRows;
275
276 Epetra_Vector diagonal(matrix->RowMatrixRowMap());
277 nRows = rowMap_->NumMyElements();
278 rowGIDs = rowMap_->MyGlobalElements();
279 matrix->ExtractDiagonalCopy(diagonal);
280
281 for (int i=0; i < nRows; i++){
282 if (diagonal[i] != 0){
283 graph_self_edges_.insert(rowGIDs[i]);
284 }
285 }
286 }
287 }
288 }
289 ////////////////////////////////////////////////////////////////////////////////
290
291
292 QueryObject::~QueryObject()
293 {
294 }
295
296 // Create a map required by graph queries
297
298 void QueryObject::fill_procmap()
299 {
300 int num_colmap_elems = colMap_->NumMyElements();
301
302 int *colIDs = new int [num_colmap_elems];
303 int *procIDs = new int [num_colmap_elems];
304 int *tmp = new int [num_colmap_elems];
305
306 colMap_->MyGlobalElements(colIDs);
307
308 rowMap_->RemoteIDList(num_colmap_elems, colIDs, procIDs, tmp);
309
310 delete [] tmp;
311
312 int i;
313 for(i=0; i<num_colmap_elems; ++i) {
314
315 // map from global column ID to process owning the row
316 // with that global ID (this is a square matrix)
317
318 procmap_[colIDs[i]] = procIDs[i];
319 }
320
321 delete [] colIDs;
322 delete [] procIDs;
323 }
324
325 bool QueryObject::haveVertexWeights()
326 {
327 return costs_->haveGlobalVertexWeights();
328 }
329 bool QueryObject::haveGraphEdgeWeights()
330 {
331 return costs_->haveGlobalGraphEdgeWeights();
332 }
333 bool QueryObject::haveHypergraphEdgeWeights()
334 {
335 return costs_->haveGlobalHypergraphEdgeWeights();
336 }
337
338 // Static query functions. These will call the query function of the
339 // appropriate QueryObject object.
340
341 int QueryObject::Number_Objects(void *data, int *ierr)
342 {
343 int numObj = 0;
344
345 QueryObject *zq = (QueryObject *)data;
346
347 if (zq){
348 numObj = zq->My_Number_Objects(ierr);
349 }
350 else{
351 *ierr = ZOLTAN_FATAL;
352 }
353
354 return numObj;
355 }
356 void QueryObject::Object_List ( void * data,
357 int num_gid_entries, int num_lid_entries,
358 ZOLTAN_ID_PTR global_ids, ZOLTAN_ID_PTR local_ids,
359 int weight_dim, float * object_weights, int * ierr )
360 {
361 QueryObject *zq = (QueryObject *)data;
362
363 if (zq)
364 {
365 zq->My_Object_List(num_gid_entries, num_lid_entries,
366 global_ids, local_ids, weight_dim, object_weights, ierr);
367 }
368 else
369 {
370 *ierr = ZOLTAN_FATAL;
371 }
372
373 return;
374 }
375 void QueryObject::Number_Edges_Multi ( void * data,
376 int num_gid_entries, int num_lid_entries,
377 int num_obj,
378 ZOLTAN_ID_PTR global_ids, ZOLTAN_ID_PTR local_ids,
379 int *num_edges, int * ierr )
380 {
381 QueryObject *zq = (QueryObject *)data;
382
383 if (zq){
384 zq->My_Number_Edges_Multi(num_gid_entries, num_lid_entries, num_obj,
385 global_ids, local_ids, num_edges, ierr );
386 }
387 else{
388 *ierr = ZOLTAN_FATAL;
389 }
390
391 return;
392 }
393 void QueryObject::Edge_List_Multi( void * data,
394 int num_gid_entries, int num_lid_entries, int num_obj,
395 ZOLTAN_ID_PTR global_ids, ZOLTAN_ID_PTR local_ids, int *num_edges,
396 ZOLTAN_ID_PTR neighbor_global_ids, int * neighbor_procs,
397 int weight_dim, float * edge_weights, int * ierr )
398 {
399 QueryObject *zq = (QueryObject *)data;
400
401 if (zq){
402 zq->My_Edge_List_Multi(num_gid_entries, num_lid_entries, num_obj,
403 global_ids, local_ids, num_edges,
404 neighbor_global_ids, neighbor_procs,
405 weight_dim, edge_weights, ierr );
406 }
407 else{
408 *ierr = ZOLTAN_FATAL;
409 }
410
411 return;
412 }
413 void QueryObject::HG_Size_CS ( void * data,
414 int* num_lists, int* num_pins, int* format, int * ierr )
415 {
416 QueryObject *zq = (QueryObject *)data;
417
418 if (zq)
419 {
420 zq->My_HG_Size_CS(num_lists, num_pins, format, ierr );
421 }
422 else
423 {
424 *ierr = ZOLTAN_FATAL;
425 }
426
427 }
428 void QueryObject::HG_CS ( void * data,
429 int num_gid_entries, int num_row_or_col, int num_pins, int format,
430 ZOLTAN_ID_PTR vtxedge_GID, int* vtxedge_ptr, ZOLTAN_ID_PTR pin_GID,
431 int * ierr )
432 {
433 QueryObject *zq = (QueryObject *)data;
434
435 if (zq){
436 zq->My_HG_CS(num_gid_entries, num_row_or_col, num_pins, format,
437 vtxedge_GID, vtxedge_ptr, pin_GID, ierr );
438 }
439 else{
440 *ierr = ZOLTAN_FATAL;
441 }
442 }
443 void QueryObject::HG_Size_Edge_Weights(void * data,
444 int* num_edges, int* ierr)
445 {
446 QueryObject *zq = (QueryObject *)data;
447
448 if (zq){
449 zq->My_HG_Size_Edge_Weights(num_edges, ierr);
450 }
451 else{
452 *ierr = ZOLTAN_FATAL;
453 }
454 }
455 void QueryObject::HG_Edge_Weights(void * data,
456 int num_gid_entries, int num_lid_entries, int num_edges, int edge_weight_dim,
457 ZOLTAN_ID_PTR edge_GID, ZOLTAN_ID_PTR edge_LID, float* edge_weights, int* ierr)
458 {
459 QueryObject *zq = (QueryObject *)data;
460
461 if (zq){
462 zq->My_HG_Edge_Weights(num_gid_entries, num_lid_entries, num_edges,
463 edge_weight_dim, edge_GID, edge_LID, edge_weights, ierr);
464 }
465 else{
466 *ierr = ZOLTAN_FATAL;
467 }
468 }
469
470 int QueryObject::Number_Geom(void *data, int *ierr)
471 {
472 int dim=0;
473 QueryObject *zq = (QueryObject *)data;
474
475 if (zq){
476 dim = zq->My_Number_Geom(ierr);
477 }
478 else{
479 *ierr = ZOLTAN_FATAL;
480 }
481 return dim;
482 }
483
484 void QueryObject::Geom_Multi(void *data, int num_gid_entries, int num_lid_entries,
485 int num_obj, ZOLTAN_ID_PTR gids, ZOLTAN_ID_PTR lids, int num_dim,
486 double *geom_vec, int *ierr)
487 {
488 QueryObject *zq = (QueryObject *)data;
489
490 if (zq){
491 zq->My_Geom_Multi(num_gid_entries, num_lid_entries,
492 num_obj, gids, lids, num_dim, geom_vec, ierr);
493 }
494 else{
495 *ierr = ZOLTAN_FATAL;
496 }
497 }
498
499 // Member general query functions.
500
501 int QueryObject::My_Number_Objects(int * ierr )
502 {
503 *ierr = ZOLTAN_OK;
504
505 if (input_type_ == geometric_input_)
506 {
507 return coords_->MyLength(); // probably can use rowMap_ instead
508 }
509 else if (input_type_ == hgraph2d_finegrain_input_)
510 {
511 if (!haveGraph_)
512 {
513 return matrix_->NumMyNonzeros();
514 }
515 else
516 {
517 return graph_->NumMyNonzeros();
518 }
519 }
520 else
521 {
522 return rowMap_->NumMyElements(); // graph or hypergraph or simple partitioning methods
523 }
524 }
525
526 void QueryObject::My_Object_List(int num_gid_entries, int num_lid_entries,
527 ZOLTAN_ID_PTR global_ids, ZOLTAN_ID_PTR local_ids,
528 int weight_dim, float * object_weights, int * ierr )
529 {
530 *ierr = ZOLTAN_OK;
531 int ngids = 0;
532 int *ibuf;
533
534 //M.M.W. need to add hierarchical support
535
536 if ((input_type_ == geometric_input_) || (input_type_ == hgraph_input_) || (input_type_ == graph_input_) || (input_type_==simple_input_))
537 {
538 ngids = rowMap_->NumMyElements();
539
540 if (sizeof(ZOLTAN_ID_TYPE) != sizeof(int)){
541 ibuf = rowMap_->MyGlobalElements();
542
543 for (int i=0; i < ngids; i++){
544 global_ids[i] = (ZOLTAN_ID_TYPE)ibuf[i];
545 }
546 }
547 else{
548 rowMap_->MyGlobalElements( ((int *) global_ids) );
549 }
550 }
551 else if(input_type_ == hgraph2d_finegrain_input_)
552 {
553 // if statement for now, maybe set function pointer for different function instead
554
555 int numRows = rowMap_->NumMyElements();
556
557 object_weights = 0;
558
559 if (!haveGraph_) //matrix
560 {
561 ngids = 0;
562 for (int rowNum=0; rowNum<numRows; rowNum++)
563 {
564 int rowSize;
565 matrix_->NumMyRowEntries(rowNum,rowSize);
566
567 //MMW need to make this memory allocation more efficient
568 int *tmprowCols = new int[rowSize];
569 double *tmprowVals = new double[rowSize];
570 int numEntries;
571
572 matrix_->ExtractMyRowCopy (rowNum, rowSize, numEntries, tmprowVals, tmprowCols);
573
574 for(int colIndx=0; colIndx<rowSize; colIndx++)
575 {
576 global_ids[ngids] = (ZOLTAN_ID_TYPE)rowMap_->GID(rowNum);
577 ngids++;
578 global_ids[ngids]= (ZOLTAN_ID_TYPE)colMap_->GID(tmprowCols[colIndx]);
579 ngids++;
580 }
581
582 delete [] tmprowCols;
583 delete [] tmprowVals;
584 }
585 }
586 else // graph
587 {
588 ngids = 0;
589 for (int rowNum=0; rowNum<numRows; rowNum++)
590 {
591 int rowSize;
592 rowSize = graph_->NumMyIndices(rowNum);
593
594 int *tmprowCols = new int[rowSize];
595 int numEntries;
596
597 graph_->ExtractMyRowCopy (rowNum, rowSize, numEntries, tmprowCols);
598
599 for(int colIndx=0;colIndx<rowSize;colIndx++)
600 {
601 global_ids[ngids] = (ZOLTAN_ID_TYPE)rowMap_->GID(rowNum);
602 ngids++;
603 global_ids[ngids]= (ZOLTAN_ID_TYPE)colMap_->GID(tmprowCols[colIndx]);
604 ngids++;
605 }
606
607 delete [] tmprowCols;
608 }
609 }
610
611 for (int indx=0; indx<ngids/2; indx++)
612 {
613 local_ids[indx] = indx;
614 }
615
616 return;
617 } // fine-grain hypergraph case
618
619
620 if (ngids < 1)
621 {
622 return;
623 }
624
625 for (int i=0; i<ngids; i++){
626 local_ids[i] = (ZOLTAN_ID_TYPE)i;
627 }
628
629 if (weight_dim >= 1) // Note we only supply 1-D weights
630 {
631 float *to_wgts = object_weights;
632 if (input_type_ == geometric_input_){
633 double *wgts=NULL;
634 int ld=0;
635 if (weights_.get()){
636 weights_->ExtractView(&wgts, &ld);
637 if (ld < ngids){
638 *ierr = ZOLTAN_FATAL;
639 std::cerr << "Proc:" << myProc_ << " Error: ";
640 std::cerr << "My_Object_List: not enough object weights" << std::endl;
641 return;
642 }
643 for (int i=0; i < ngids; i++){
644 *to_wgts= static_cast<float>(wgts[i]);
645 to_wgts += weight_dim;
646 }
647 }
648 }
649 else if (costs_->haveVertexWeights())
650 {
651 std::map<int, float> weightMap;
652 std::map<int, float>::iterator curr;
653 std::map<int, float>::iterator end = weightMap.end();
654
655 int mapSize = costs_->getVertexWeights(weightMap);
656 if (mapSize != ngids)
657 {
658 *ierr = ZOLTAN_FATAL;
659 std::cout << "Proc:" << myProc_ << " Error: ";
660 std::cout << "QueryObject::My_Object_List, number of vertex weights" << std::endl;
661 return;
662 }
663 for (int i=0; i < ngids; i++){
664 curr = weightMap.find(global_ids[i]);
665 if (curr == end){
666 *ierr = ZOLTAN_FATAL;
667 std::cout << "Proc:" << myProc_ << " Error: ";
668 std::cout << "QueryObject::My_Object_List, missing vertex weight" << std::endl;
669 return;
670 }
671 *to_wgts = curr->second;
672 to_wgts += weight_dim;
673 }
674 }
675 }
676
677 return;
678 }
679
680 // member graph query functions
681
682 void QueryObject::My_Number_Edges_Multi(int num_gid_entries, int num_lid_entries,
683 int num_obj,
684 ZOLTAN_ID_PTR global_ids, ZOLTAN_ID_PTR local_ids,
685 int *num_edges, int * ierr )
686 {
687 *ierr = ZOLTAN_OK;
688
689 int num_indices, rc;
690
691 if (!haveGraph_){
692 for (int i=0; i<num_obj; i++){
693 rc = matrix_->NumMyRowEntries((int)local_ids[i], num_indices);
694 if (rc != 0){
695 *ierr = ZOLTAN_FATAL;
696 std::cout << "Proc:" << myProc_ << " Error: ";
697 std::cout << "QueryObject::My_Number_Edges_Multi, local id not in matrix" << std::endl;
698 return;
699 }
700 num_edges[i] = num_indices;
701 if (graph_self_edges_.size() > 0){
702 std::set<int>::iterator it = graph_self_edges_.find((int)global_ids[i]);
703 if (it != graph_self_edges_.end()){
704 num_edges[i]--;
705 }
706 }
707 }
708 }
709 else{
710 for (int i=0; i<num_obj; i++){
711 num_edges[i] = graph_->NumMyIndices((int)local_ids[i]);
712 if (num_edges[i] < 0){
713 *ierr = ZOLTAN_FATAL;
714 std::cout << "Proc:" << myProc_ << " Error: ";
715 std::cout << "QueryObject::My_Number_Edges_Multi, local id not in graph" << std::endl;
716 return;
717 }
718 if (graph_self_edges_.size() > 0){
719 std::set<int>::iterator it = graph_self_edges_.find((int)global_ids[i]);
720 if (it != graph_self_edges_.end()){
721 num_edges[i]--;
722 }
723 }
724 }
725 }
726 return;
727 }
728
729 void QueryObject::My_Edge_List_Multi(int num_gid_entries, int num_lid_entries, int num_obj,
730 ZOLTAN_ID_PTR global_ids, ZOLTAN_ID_PTR local_ids, int *num_edges,
731 ZOLTAN_ID_PTR neighbor_global_ids, int * neighbor_procs,
732 int weight_dim, float * edge_weights, int * ierr )
733 {
734 int *gids=NULL;
735 int num_indices, rc, tmpSize;
736 double *tmp = NULL;
737
738 *ierr = ZOLTAN_OK;
739
740 if (num_obj < 1) {
741 return;
742 }
743
744 int *nborProc = neighbor_procs;
745 float *wgt = edge_weights;
746 ZOLTAN_ID_PTR nborID = neighbor_global_ids;
747 bool use_weights = false;
748 std::map<int, float> wgtMap;
749 std::map<int, float>::iterator wgtIter;
750 std::map<int, int>::iterator procIter;
751
752 if ((weight_dim >= 1) && costs_->haveGraphEdgeWeights()){
753 use_weights = true;
754 }
755
756 if (!haveGraph_)
757 tmpSize = matrix_->MaxNumEntries();
758 else
759 tmpSize = graph_->MaxNumIndices();
760
761 if (tmpSize > 0){
762 if (!haveGraph_)
763 tmp = new double [tmpSize];
764
765 gids = new int [tmpSize];
766
767 if ((!haveGraph_ && !tmp) || !gids){
768 *ierr = ZOLTAN_MEMERR;
769 return;
770 }
771 }
772
773 for (int i=0; (i<num_obj) && (*ierr == ZOLTAN_OK); i++){
774
775 int self_edge = 0;
776 if (graph_self_edges_.size() > 0){
777 std::set<int>::iterator it = graph_self_edges_.find((int)global_ids[i]);
778 if (it != graph_self_edges_.end()){
779 self_edge = 1;
780 }
781 }
782
783 if (!haveGraph_){
784 rc = matrix_->ExtractMyRowCopy((int)local_ids[i], tmpSize, num_indices, tmp, gids);
785 }
786 else{
787 rc = graph_->ExtractMyRowCopy((int)local_ids[i], tmpSize, num_indices, gids);
788 }
789
790 if ((rc < 0) || (num_indices != (num_edges[i] + self_edge))){
791 *ierr = ZOLTAN_FATAL;
792 std::cout << "Proc:" << myProc_ << " Error: ";
793 std::cout << "QueryObject::My_Edge_List_Multi, extracting row" << std::endl;
794 break;
795 }
796
797 for (int j=0; j<num_indices; j++){
798 gids[j] = colMap_->GID(gids[j]); // convert to global IDs
799 }
800
801 if (use_weights){
802 rc = costs_->getGraphEdgeWeights((int)global_ids[i], wgtMap);
803 if (rc != num_edges[i]){
804 *ierr = ZOLTAN_FATAL;
805 std::cout << "Proc:" << myProc_ << " Error: ";
806 std::cout << "QueryObject::My_Edge_List_Multi, getting weights" << std::endl;
807 break;
808 }
809 }
810 for (int j=0; j < num_indices; j++){
811
812 if (self_edge && (gids[j] == (int)global_ids[i])) continue; // skip self edge
813
814 *nborID++ = (ZOLTAN_ID_TYPE)gids[j]; // Global ID of neighbor
815
816 procIter = procmap_.find(gids[j]);
817 if (procIter == procmap_.end()){
818 *ierr = ZOLTAN_FATAL;
819 std::cout << "Proc:" << myProc_ << " Error: ";
820 std::cout << "QueryObject::My_Edge_List_Multi, process ID map" << std::endl;
821 break;
822 }
823
824 *nborProc++ = procIter->second; // Process owning neighbor
825
826 if (use_weights){
827 wgtIter = wgtMap.find(gids[j]);
828 if (wgtIter == wgtMap.end()){
829 *ierr = ZOLTAN_FATAL;
830 std::cout << "Proc:" << myProc_ << " Error: ";
831 std::cout << "QueryObject::My_Edge_List_Multi, weight map" << std::endl;
832 break;
833 }
834 *wgt++ = (float)wgtIter->second; // edge weight
835 }
836 }
837 }
838 if (gids) delete [] gids;
839 if (tmp) delete [] tmp;
840
841 return;
842 }
843
844 // member hypergraph query functions
845
846 void QueryObject::My_HG_Size_CS(int* num_lists, int* num_pins, int* format, int * ierr )
847 {
848 *ierr = ZOLTAN_OK;
849
850 *format = ZOLTAN_COMPRESSED_VERTEX; // We will return row (vertex) lists
851
852
853 if(input_type_ == hgraph2d_finegrain_input_) // 2D fine-grain hypergraph
854 {
855 if (haveGraph_) // graph
856 {
857 *num_lists = graph_->NumMyNonzeros();
858 }
859 else // matrix
860 {
861 *num_lists = matrix_->NumMyNonzeros();
862 }
863 *num_pins = 2 * (*num_lists);
864 }
865 else // 1D hypergraph
866 {
867 if (haveGraph_)
868 {
869 *num_lists = graph_->NumMyRows(); // Number of rows
870 *num_pins = graph_->NumMyNonzeros(); // Total nonzeros in these rows
871 }
872 else
873 {
874 *num_lists = matrix_->NumMyRows(); // Number of rows
875 *num_pins = matrix_->NumMyNonzeros(); // Total nonzeros in these rows
876 }
877 }
878
879
880
881 return;
882 }
883
884 void QueryObject::My_HG_CS (int num_gid_entries, int num_row_or_col, int num_pins, int format,
885 ZOLTAN_ID_PTR vtxedge_GID, int* vtxedge_ptr, ZOLTAN_ID_PTR pin_GID,
886 int * ierr )
887 {
888 int num_indices, rc, npins;
889 double *tmp=NULL;
890
891
892 *ierr = ZOLTAN_OK;
893
894 /////////////////////////////////////////////////////
895 // if fine-grain hypergraph, call this other function
896 /////////////////////////////////////////////////////
897 if(input_type_ == hgraph2d_finegrain_input_)
898 {
899 My_FGHG_CS(num_gid_entries, num_row_or_col, num_pins, format,
900 vtxedge_GID, vtxedge_ptr, pin_GID, ierr);
901 return;
902 }
903 /////////////////////////////////////////////////////
904
905 int maxrow = 0;
906
907 if (haveGraph_)
908 {
909 npins = graph_->NumMyNonzeros();
910 maxrow = graph_->MaxNumIndices();
911 }
912 else
913 {
914 npins = matrix_->NumMyNonzeros();
915 maxrow = matrix_->MaxNumEntries();
916 if (maxrow > 0){
917 tmp = new double [maxrow];
918 if (!tmp){
919 *ierr = ZOLTAN_MEMERR;
920 return;
921 }
922 }
923 }
924
925 if ((format != ZOLTAN_COMPRESSED_VERTEX) ||
926 (num_row_or_col != rowMap_->NumMyElements()) ||
927 (num_pins != npins)){
928 *ierr = ZOLTAN_FATAL;
929 std::cout << "Proc:" << myProc_ << " Error: ";
930 std::cout << "QueryObject::My_HG_CS, bad arguments" << std::endl;
931 if (tmp) delete [] tmp;
932 return;
933 }
934
935 int pin_start_pos = 0;
936 unsigned int *gids = NULL;
937
938 if (sizeof(ZOLTAN_ID_TYPE) != sizeof(unsigned int)){
939 gids = new unsigned int [maxrow];
940 if (maxrow && !gids){
941 *ierr = ZOLTAN_MEMERR;
942 return;
943 }
944 }
945 else{
946 gids = (unsigned int *)pin_GID;
947 }
948
949 for (int i=0; i<num_row_or_col; i++){
950 vtxedge_GID[i] = (ZOLTAN_ID_TYPE)rowMap_->GID(i);
951 vtxedge_ptr[i] = pin_start_pos;
952
953 if (sizeof(ZOLTAN_ID_TYPE) != sizeof(int)){
954 if (haveGraph_){
955 rc = graph_->ExtractMyRowCopy(i, maxrow, num_indices, (int *)gids);
956 }
957 else{
958 rc = matrix_->ExtractMyRowCopy(i, maxrow, num_indices, tmp, (int *)gids);
959 }
960 if (rc == 0){
961 for (int j=pin_start_pos, count=0; count < num_indices; j++, count++){
962 pin_GID[j] = (ZOLTAN_ID_TYPE)colMap_->GID(gids[count]);
963 if (pin_GID[i] < base_){
964 *ierr = ZOLTAN_FATAL;
965 std::cout << "Proc:" << myProc_ << " Error: ";
966 std::cout << "QueryObject::My_HG_CS, local ID not in column map" << std::endl;
967 return;
968 }
969 }
970 }
971 else{
972 *ierr = ZOLTAN_FATAL;
973 std::cout << "Proc:" << myProc_ << " Error: ";
974 std::cout << "QueryObject::My_HG_CS, extracting row" << std::endl;
975 break;
976 }
977 }
978 else{
979 if (haveGraph_){
980 rc = graph_->ExtractMyRowCopy(i, npins, num_indices,(int *)gids + pin_start_pos);
981 }
982 else{
983 rc = matrix_->ExtractMyRowCopy(i, npins, num_indices, tmp, (int *)gids + pin_start_pos);
984 }
985
986 if (rc == 0){
987 for (int i=pin_start_pos; i<pin_start_pos+num_indices; i++){
988 gids[i] = (unsigned int)colMap_->GID(gids[i]); // convert to global IDs
989 if (gids[i] < base_){
990 *ierr = ZOLTAN_FATAL;
991 std::cout << "Proc:" << myProc_ << " Error: ";
992 std::cout << "QueryObject::My_HG_CS, local ID not in column map" << std::endl;
993 return;
994 }
995 }
996 }
997 else{
998 *ierr = ZOLTAN_FATAL;
999 std::cout << "Proc:" << myProc_ << " Error: ";
1000 std::cout << "QueryObject::My_HG_CS, extracting row" << std::endl;
1001 break;
1002 }
1003 }
1004
1005 pin_start_pos += num_indices;
1006 npins -= num_indices;
1007 }
1008
1009 if (gids && (gids != (unsigned int *)pin_GID)){
1010 delete [] gids;
1011 }
1012
1013 if (tmp)
1014 {
1015 delete [] tmp;
1016 }
1017 }
1018
1019 ////////////////////////////////////////////////////////////////////////////////
1020 ////////////////////////////////////////////////////////////////////////////////
1021 void QueryObject::My_FGHG_CS (int num_gid_entries, int num_nonzeros, int num_pins, int format,
1022 ZOLTAN_ID_PTR vtx_GID, int* vtx_ptr, ZOLTAN_ID_PTR pin_GID,
1023 int * ierr )
1024 {
1025 int nverts, npins;
1026
1027 *ierr = ZOLTAN_OK;
1028
1029 //////////////////////////////////////////
1030 // Sanity checks of function input
1031 //////////////////////////////////////////
1032 if (haveGraph_) // graph
1033 {
1034 nverts = graph_->NumMyNonzeros();
1035 }
1036 else //matrix
1037 {
1038 nverts = matrix_->NumMyNonzeros();
1039 }
1040 npins = 2*nverts;
1041
1042 if ((format != ZOLTAN_COMPRESSED_VERTEX) ||
1043 (num_nonzeros != nverts) || (num_pins != npins))
1044 {
1045 *ierr = ZOLTAN_FATAL;
1046 std::cout << "Proc:" << myProc_ << " Error: ";
1047 std::cout << "QueryObject::My_HG_CS, bad arguments" << std::endl;
1048 return;
1049 }
1050 //////////////////////////////////////////
1051
1052 //////////////////////////////////////////
1053 // Allocate temporary memory needed for row extraction
1054 //////////////////////////////////////////
1055 int *tmprowCols=0;
1056 double *tmprowVals=0;
1057
1058 int maxrow=0;
1059
1060 if (haveGraph_) // graph
1061 {
1062 maxrow = graph_->MaxNumIndices();
1063
1064 if (maxrow > 0)
1065 {
1066 tmprowCols = new int [maxrow];
1067 if (!tmprowCols)
1068 {
1069 *ierr = ZOLTAN_MEMERR;
1070 return;
1071 }
1072 }
1073 }
1074 else //matrix
1075 {
1076 maxrow = matrix_->MaxNumEntries();
1077 if (maxrow > 0)
1078 {
1079 tmprowCols = new int [maxrow];
1080 if (!tmprowCols)
1081 {
1082 *ierr = ZOLTAN_MEMERR;
1083 return;
1084 }
1085 tmprowVals = new double [maxrow];
1086 if (!tmprowVals)
1087 {
1088 *ierr = ZOLTAN_MEMERR;
1089 return;
1090 }
1091 }
1092 }
1093 //////////////////////////////////////////
1094
1095 //////////////////////////////////////////
1096 int numRows = rowMap_->NumMyElements();
1097 int num_indices = 0;
1098
1099 vtx_ptr[0] = 0;
1100 int nzindx=0;
1101 int rc=0;
1102 for (int rowi=0; rowi<numRows; rowi++)
1103 {
1104
1105 if (haveGraph_)
1106 {
1107 rc = graph_->ExtractMyRowCopy(rowi, maxrow, num_indices, tmprowCols);
1108 }
1109 else
1110 {
1111 rc = matrix_->ExtractMyRowCopy(rowi, maxrow, num_indices, tmprowVals, tmprowCols);
1112 }
1113
1114 if (rc != 0)
1115 {
1116 *ierr = ZOLTAN_FATAL;
1117 std::cout << "Proc:" << myProc_ << " Error: ";
1118 std::cout << "QueryObject::My_FGHG_CS, extracting row" << std::endl;
1119 return;
1120 }
1121
1122 for(int indx=0; indx<num_indices; indx++)
1123 {
1124 vtx_GID[2*nzindx] = (ZOLTAN_ID_TYPE)rowMap_->GID(rowi);
1125 vtx_GID[2*nzindx+1] = (ZOLTAN_ID_TYPE)colMap_->GID(tmprowCols[indx]);
1126
1127 // First pin for vertex (row pin)
1128 pin_GID[4*nzindx] = 0;
1129 pin_GID[4*nzindx+1] = (ZOLTAN_ID_TYPE)rowMap_->GID(rowi);
1130
1131 // Second pin for vertex (column pin)
1132 pin_GID[4*nzindx+2] = 1;
1133 pin_GID[4*nzindx+3] = (ZOLTAN_ID_TYPE)colMap_->GID(tmprowCols[indx]);
1134
1135 nzindx++;
1136 vtx_ptr[nzindx]= 2*nzindx;
1137 }
1138
1139 }
1140 //////////////////////////////////////////
1141
1142
1143 if (tmprowCols)
1144 {
1145 delete [] tmprowCols;
1146 }
1147 if (tmprowVals)
1148 {
1149 delete [] tmprowVals;
1150 }
1151 }
1152 ////////////////////////////////////////////////////////////////////////////////
1153
1154
1155 void QueryObject::My_HG_Size_Edge_Weights( int* num_edges, int* ierr)
1156 {
1157 *num_edges = costs_->getNumHypergraphEdgeWeights();
1158
1159 *ierr = ZOLTAN_OK;
1160 }
1161
1162 void QueryObject::My_HG_Edge_Weights(
1163 int num_gid_entries, int num_lid_entries, int num_edges, int edge_weight_dim,
1164 ZOLTAN_ID_PTR edge_GID, ZOLTAN_ID_PTR edge_LID, float* edge_weights, int* ierr)
1165 {
1166 *ierr = ZOLTAN_OK;
1167
1168 if ((num_edges == 0) || (edge_weight_dim == 0)) return;
1169
1170 if (num_edges != costs_->getNumHypergraphEdgeWeights()){
1171 *ierr = ZOLTAN_FATAL;
1172 std::cerr << "Proc:" << myProc_ << " Error: ";
1173 std::cerr << "QueryObject::My_HG_Edge_Weights, bad arguments" << std::endl;
1174 return;
1175 }
1176
1177 if (num_lid_entries > 0){
1178 for (int i=0; i<num_edges; i++){
1179 edge_LID[i] = (ZOLTAN_ID_TYPE)i;
1180 }
1181 }
1182
1183 if (sizeof(ZOLTAN_ID_TYPE) != sizeof(int)){
1184 int *ibuf = new int [num_edges];
1185 if (num_edges && !ibuf){
1186 *ierr = ZOLTAN_MEMERR;
1187 return;
1188 }
1189 costs_->getHypergraphEdgeWeights(num_edges, ibuf, edge_weights);
1190 for (int i=0; i < num_edges; i++){
1191 edge_GID[i] = (ZOLTAN_ID_TYPE)ibuf[i];
1192 }
1193 delete [] ibuf;
1194 ibuf = NULL;
1195 }
1196 else{
1197 costs_->getHypergraphEdgeWeights(num_edges, (int *)edge_GID, edge_weights);
1198 }
1199 }
1200
1201 // member geometric query functions
1202
1203 int QueryObject::My_Number_Geom(int *ierr)
1204 {
1205 int dim = 0;
1206 *ierr = ZOLTAN_FATAL;
1207
1208 if (coords_.get()){
1209 dim = coords_->NumVectors();
1210 *ierr = ZOLTAN_OK;
1211 }
1212 else{
1213 std::cerr << "in My_Number_Geom, no MultiVector present" << std::endl;
1214 }
1215
1216 return dim;
1217 }
1218
1219 void QueryObject::My_Geom_Multi(int num_gid_entries, int num_lid_entries,
1220 int num_obj, ZOLTAN_ID_PTR gids, ZOLTAN_ID_PTR lids, int num_dim,
1221 double *geom_vec, int *ierr)
1222 {
1223 if ((num_obj < 1) || (num_dim < 1)){
1224 *ierr = ZOLTAN_OK;
1225 return;
1226 }
1227
1228 if (coords_.get() &&
1229 (coords_->MyLength() >= num_obj) &&
1230 (coords_->NumVectors() >= num_dim)){
1231
1232 double *coords;
1233 int stride;
1234
1235 coords_->ExtractView(&coords, &stride);
1236
1237 double **v = new double * [num_dim];
1238 for (int j=0; j < num_dim; j++){
1239 v[j] = coords + (stride * j);
1240 }
1241
1242 for (int i=0; i<num_obj; i++){
1243 for (int j=0; j<num_dim; j++){
1244 *geom_vec++ = v[j][i];
1245 }
1246 }
1247
1248 delete [] v;
1249 *ierr = ZOLTAN_OK;
1250 }
1251 else{
1252 std::cerr << "Error in My_Geom_Multi" << std::endl;
1253 *ierr = ZOLTAN_FATAL;
1254 }
1255 }
1256
1257 //M.M.W. need to add some query functions
1258
1259 } // end namespace ZoltanLib
1260 } // end namespace Epetra
1261 } // end namespace Isorropia