MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ML_Linker.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
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
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 // Unmaintained code snippet to use Muelu's aggregation algorithms in ML
47 
48 #include "ml_aggregate.h"
49 #include "ml_epetra_utils.h"
50 
51 extern MueLu_Graph *MueLu_BuildGraph(ML_Matrix *Amatrix, char *name);
52 extern int MueLu_DestroyGraph(MueLu_Graph *graph);
53 
54 /**********************************************************************************/
55 /* Function to execute new MueLu aggregation via old ML */
56 /* This function should go away soon as we should start executing new MueLu */
57 /* aggregation inside MueLu.
58 /**********************************************************************************/
59 int ML_Aggregate_CoarsenUncoupled(ML_Aggregate *mlAggregates,
60  ML_Matrix *Amatrix, ML_Matrix **Pmatrix, ML_Comm *comm) {
61  MueLu_Graph *graph;
62  graph = MueLu_BuildGraph(Amatrix, "ML_Uncoupled");
63 
64  if (graph->eGraph->Comm().MyPID() == 0 && mlAggregates->printFlag < MueLu_PrintLevel())
65  printf("ML_Aggregate_CoarsenUncoupled : \n");
66 
67  MueLu_AggOptions aggregateOptions;
68 
69  aggregateOptions.printFlag = mlAggregates->print_flag;
70  aggregateOptions.minNodesPerAggregate = mlAggregates->min_nodes_per_aggregate;
71  aggregateOptions.maxNeighAlreadySelected = mlAggregates->max_neigh_already_selected;
72  aggregateOptions.ordering = mlAggregates->ordering;
73  aggregateOptions.phase3AggCreation = mlAggregates->phase3_agg_creation;
74 
75  Aggregates *aggregates = NULL;
76 
77  aggregates = MueLu_Aggregate_CoarsenUncoupled(&aggregateOptions, graph);
78 
79  MueLu_AggregateLeftOvers(&aggregateOptions, aggregates, "UC_CleanUp", graph);
80 
81  //#ifdef out
82  Epetra_IntVector Final(aggregates->vertex2AggId->Map());
83  for (int i = 0; i < aggregates->vertex2AggId->Map().NumMyElements(); i++)
84  Final[i] = (*(aggregates->vertex2AggId))[i] + (*(aggregates->procWinner))[i] * 1000;
85  printf("finals\n");
86  cout << Final << endl;
87  sleep(2);
88  //#endif
89 
90  MueLu_AggregateDestroy(aggregates);
91  MueLu_DestroyGraph(graph);
92  return 0;
93 }
94 
95 /**********************************************************************************/
96 /* Function to take an ML_Matrix (which actually wraps an Epetra_CrsMatrix) and */
97 /* extract out the Epetra_CrsGraph. My guess is that this should be changed soon */
98 /* so that the first argument is some MueLu API Matrix. */
99 /**********************************************************************************/
100 MueLu_Graph *MueLu_BuildGraph(ML_Matrix *Amatrix, char *name) {
101  MueLu_Graph *graph;
102  double *dtmp = NULL;
103  Epetra_CrsMatrix *A;
104 
105  graph = (MueLu_Graph *)malloc(sizeof(MueLu_Graph));
106  graph->eGraph = NULL;
107  graph->name = NULL;
108  graph->name = (char *)malloc(sizeof(char) * 80);
109  strcpy(Graph->name, name);
110  graph->nVertices = Amatrix->invec_leng;
111 
112  if (Amatrix->getrow->nrows == 0) {
113  graph->vertexNeighbors = NULL;
114  graph->vertexNeighborsPtr = NULL;
115  graph->nEdges = 0;
116  } else {
117  Epetra_ML_GetCrsDataptrs(Amatrix, &dtmp, &(graph->vertexNeighbors), &(graph->vertexNeighborsPtr));
118  if (graph->vertexNeighborsPtr == NULL) {
119  printf("MueLu_BuildGraph: Only functions for an Epetra_CrsMatrix.\n");
120  exit(1);
121  }
122  graph->nEdges = (graph->vertexNeighborsPtr)[Amatrix->getrow->Nrows];
123  Epetra_ML_GetCrsMatrix(Amatrix, (void **)&A);
124  graph->eGraph = &(A->graph());
125  }
126  if (graph->eGraph == NULL)
127  graph->nGhost = 0;
128  else {
129  graph->nGhost = A->RowMatrixColMap().NumMyElements() - A->MatrixDomainMap().NumMyElements();
130  if (graph->nGhost < 0) graph->nGhost = 0;
131  }
132  return graph;
133 }
134 
135 int MueLu_DestroyGraph(MueLu_Graph *graph) {
136  if (graph != NULL) {
137  if (graph->name != NULL) free(graph->name);
138  free(graph);
139  }
140  return 0;
141 }
int MueLu_DestroyGraph(MueLu_Graph *graph)
Definition: ML_Linker.hpp:135
MueLu_Graph * MueLu_BuildGraph(ML_Matrix *Amatrix, char *name)
Definition: ML_Linker.hpp:100
int ML_Aggregate_CoarsenUncoupled(ML_Aggregate *mlAggregates, ML_Matrix *Amatrix, ML_Matrix **Pmatrix, ML_Comm *comm)
Definition: ML_Linker.hpp:59
int NumMyElements() const
Keep data only for this run. Used to keep data useful for Hierarchy::Iterate(). Data will be deleted ...
const Epetra_Map & RowMatrixColMap() const