IFPACK  Development
 All Classes Namespaces Files Functions Variables Enumerations Friends Pages
getRow_dh.c
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack: Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2002) 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 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 */
42 
43 #include "getRow_dh.h"
44 #include "Mat_dh.h"
45 #include "Euclid_dh.h"
46 #include "Mem_dh.h"
47 
48 
49 /*-------------------------------------------------------------------
50  * EPETRA
51  *-------------------------------------------------------------------*/
52 
53 #undef __FUNC__
54 #define __FUNC__ "EuclidGetRow"
55 void
56 EuclidGetRow (void *A, int row, int *len, int **ind, double **val)
57 {
58  START_FUNC_DH int ierr = 0;
59  if (ind != NULL)
60  ierr += ExtractIndicesView (A, row, len, ind);
61  if (ierr != 0)
62  {
63  sprintf (msgBuf_dh, "ExtractIndicesView(row= %i) returned %i", row + 1,
64  ierr);
65  SET_V_ERROR (msgBuf_dh);
66  }
67  if (val != NULL)
68  ierr += ExtractValuesView (A, row, len, val);
69  if (ierr != 0)
70  {
71  sprintf (msgBuf_dh, " ExtractValuesView(row= %i) returned %i", row + 1,
72  ierr);
73  SET_V_ERROR (msgBuf_dh);
74  }
75 END_FUNC_DH}
76 
77 #undef __FUNC__
78 #define __FUNC__ "EuclidRestoreRow"
79 void
80 EuclidRestoreRow (void *A, int row, int *len, int **ind, double **val)
81 {
82  START_FUNC_DH
83  /* This is needed in Hypre and Petsc, but not Trilinos. */
84 END_FUNC_DH}
85 
86 #undef __FUNC__
87 #define __FUNC__ "EuclidGetDimensions"
88 void
89 EuclidGetDimensions (void *A, int *beg_row, int *rowsLocal, int *rowsGlobal)
90 {
91  START_FUNC_DH int m, n;
92  int row_start, row_end, col_start, col_end;
93 
94  row_start = MinMaxMyGID (A, true, true);
95  row_end = MinMaxMyGID (A, true, false);
96  col_start = MinMaxMyGID (A, false, true);
97  col_end = MinMaxMyGID (A, false, false);
98 
99  m = NumGlobalRowCol (A, false);
100  n = NumGlobalRowCol (A, true);
101  *beg_row = row_start;
102  *rowsLocal = (row_end - row_start + 1);
103  *rowsGlobal = n;
104 END_FUNC_DH}
105 
106 /*
107 #undef __FUNC__
108 #define __FUNC__ "EuclidReadLocalNz"
109 int EuclidReadLocalNz(void *A)
110 {
111  START_FUNC_DH
112  if (ignoreMe) SET_V_ERROR("not implemented");
113  return(0);
114  END_FUNC_DH
115 }
116 */
117 
118 #undef __FUNC__
119 #define __FUNC__ "PrintMatUsingGetRow"
120 void
121 PrintMatUsingGetRow (void *A, int beg_row, int m,
122  int *n2o_row, int *n2o_col, char *filename)
123 {
124  START_FUNC_DH FILE *fp;
125  int *o2n_col = NULL, pe, i, j, *cval, len;
126  int newCol, newRow;
127  double *aval;
128 
129  /* form inverse column permutation */
130  if (n2o_col != NULL)
131  {
132  o2n_col = (int *) MALLOC_DH (m * sizeof (int));
133  CHECK_V_ERROR;
134  for (i = 0; i < m; ++i)
135  o2n_col[n2o_col[i]] = i;
136  }
137 
138  for (pe = 0; pe < np_dh; ++pe)
139  {
140 
141  MPI_Barrier (comm_dh);
142 
143  if (myid_dh == pe)
144  {
145  if (pe == 0)
146  {
147  fp = fopen (filename, "w");
148  }
149  else
150  {
151  fp = fopen (filename, "a");
152  }
153  if (fp == NULL)
154  {
155  sprintf (msgBuf_dh, "can't open %s for writing\n", filename);
156  SET_V_ERROR (msgBuf_dh);
157  }
158 
159  for (i = 0; i < m; ++i)
160  {
161 
162  if (n2o_row == NULL)
163  {
164  EuclidGetRow (A, i + beg_row, &len, &cval, &aval);
165  CHECK_V_ERROR;
166  for (j = 0; j < len; ++j)
167  {
168  fprintf (fp, "%i %i %g\n", i + 1, cval[j], aval[j]);
169  }
170  EuclidRestoreRow (A, i, &len, &cval, &aval);
171  CHECK_V_ERROR;
172  }
173  else
174  {
175  newRow = n2o_row[i] + beg_row;
176  EuclidGetRow (A, newRow, &len, &cval, &aval);
177  CHECK_V_ERROR;
178  for (j = 0; j < len; ++j)
179  {
180  newCol = o2n_col[cval[j] - beg_row] + beg_row;
181  fprintf (fp, "%i %i %g\n", i + 1, newCol, aval[j]);
182  }
183  EuclidRestoreRow (A, i, &len, &cval, &aval);
184  CHECK_V_ERROR;
185  }
186  }
187  fclose (fp);
188  }
189  }
190 
191  if (n2o_col != NULL)
192  {
193  FREE_DH (o2n_col);
194  CHECK_V_ERROR;
195  }
196 END_FUNC_DH}