FEI  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
test_AztecWrappers.cpp
1 /*--------------------------------------------------------------------*/
2 /* Copyright 2005 Sandia Corporation. */
3 /* Under the terms of Contract DE-AC04-94AL85000, there is a */
4 /* non-exclusive license for use of this work by or on behalf */
5 /* of the U.S. Government. Export of this program may require */
6 /* a license from the United States Government. */
7 /*--------------------------------------------------------------------*/
8 
9 
10 #include <fei_macros.hpp>
11 
12 #include <fei_mpi.h>
13 
14 #include <test_utils/test_AztecWrappers.hpp>
15 
16 #include <fei_ArrayUtils.hpp>
17 #include <test_utils/LibraryFactory.hpp>
18 
19 #ifdef HAVE_FEI_AZTECOO
20 #include <fei_Aztec_Map.hpp>
21 #include <fei_Aztec_LSVector.hpp>
22 #include <fei_AztecDMSR_Matrix.hpp>
23 #endif
24 
25 #undef fei_file
26 #define fei_file "test_AztecWrappers.cpp"
27 
28 #include <fei_ErrMacros.hpp>
29 
30 #ifdef HAVE_FEI_AZTECOO
31 int compare_DMSR_contents(fei_trilinos::AztecDMSR_Matrix& matrix, int localOffset,
32  std::vector<std::vector<int> >& colIndices,
33  std::vector<std::vector<double> >& values);
34 
35 int fill_DMSR(fei_trilinos::AztecDMSR_Matrix& matrix, int localOffset,
36  std::vector<std::vector<int> >& colIndices,
37  std::vector<std::vector<double> >& values, bool sumInto);
38 #endif
39 
40 test_AztecWrappers::test_AztecWrappers(MPI_Comm comm)
41  : tester(comm)
42 {
43 }
44 
45 test_AztecWrappers::~test_AztecWrappers()
46 {
47 }
48 
49 int test_AztecWrappers::runtests()
50 {
51  if (numProcs_ < 2) {
52  CHK_ERR( serialtest1() );
53  }
54 
55  CHK_ERR( test1() );
56  CHK_ERR( test2() );
57  CHK_ERR( test3() );
58  CHK_ERR( test4() );
59  return(0);
60 }
61 
62 int test_AztecWrappers::serialtest1()
63 {
64  return(0);
65 }
66 
67 int test_AztecWrappers::test1()
68 {
69 #ifdef HAVE_FEI_AZTECOO
70  int localSize = 3, globalSize = localSize*numProcs_;
71  int localOffset = localSize*localProc_;
72  int i;
73 
74  std::vector<int> update(localSize);
75  for(i=0; i<localSize; i++) update[i] = localOffset+i;
76 
78  new fei_trilinos::Aztec_Map(globalSize, localSize, &update[0], localOffset, comm_));
79 
80  fei_trilinos::AztecDMSR_Matrix* matrix = new fei_trilinos::AztecDMSR_Matrix(map);
81 
82  std::vector<int> elemrows(localSize);
83  std::vector<int> elemcols(globalSize);
84  double** elemcoefs = new double*[localSize];
85  for(int j=0; j<globalSize; ++j) elemcols[j] = j;
86  for(i=0; i<localSize; ++i) {
87  elemrows[i] = localOffset+i;
88  elemcoefs[i] = new double[globalSize];
89  for(int j=0; j<globalSize; ++j) {
90  elemcoefs[i][j] = (double)(localOffset+i+j);
91  }
92  }
93 
94  std::vector<std::vector<int> > colIndices(localSize);
95  std::vector<std::vector<double> > values(localSize);
96  std::vector<int> rowLengths(localSize);
97  std::vector<int*> colPtrs(localSize);
98  int nnzeros = 0;
99 
100  for(i=0; i<localSize; i++) {
101  int diagEntry = 0;
102  int row = i+localOffset;
103  for(int j=0; j<globalSize; j++) {
104  int col = j;
105  if (col == row) diagEntry = 1;
106  colIndices[i].push_back(col);
107  values[i].push_back((double)(row+col));
108  }
109  rowLengths[i] = colIndices[i].size() - diagEntry;
110  nnzeros += rowLengths[i] + 1;
111  colPtrs[i] = &(colIndices[i][0]);
112  }
113 
114  matrix->allocate( &rowLengths[0] );
115 
116  if (!(matrix->isAllocated())) {
117  ERReturn(-1);
118  }
119 
120  if (matrix->getNumNonZeros() != nnzeros) {
121  ERReturn(-1);
122  }
123 
124  CHK_ERR( fill_DMSR(*matrix, localOffset, colIndices, values, true) );
125 
126  int* rowinds = &elemrows[0];
127  int* colinds = &elemcols[0];
128 
129  CHK_ERR( matrix->sumIntoRow(localSize, rowinds, globalSize, colinds, elemcoefs) );
130 
131  for(i=0; i<localSize; ++i) {
132  for(int j=0; j<globalSize; ++j) values[i][j] *= 2.0;
133  }
134 
135  CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
136 
137  for(i=0; i<localSize; ++i) {
138  for(int j=0; j<globalSize; ++j) values[i][j] /= 2.0;
139  }
140 
141  CHK_ERR( fill_DMSR(*matrix, localOffset, colIndices, values, false) );
142 
143  CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
144 
145  if (matrix->writeToFile("A_Az_notFilled.mtx") != true) {
146  ERReturn(-1);
147  }
148 
149  if (matrix->readFromFile("A_Az_notFilled.mtx") != true) {
150  ERReturn(-1);
151  }
152 
153  CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
154 
155  matrix->fillComplete();
156 
157  if (!(matrix->isFilled())) {
158  ERReturn(-1);
159  }
160 
161  if (matrix->writeToFile("A_Az_filled.mtx") != true) {
162  ERReturn(-1);
163  }
164 
165  CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
166 
167  CHK_ERR( fill_DMSR(*matrix, localOffset, colIndices, values, false) );
168 
169  CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
170 
171  matrix->put(0.0);
172 
173  CHK_ERR( fill_DMSR(*matrix, localOffset, colIndices, values, true) );
174 
175  CHK_ERR( matrix->sumIntoRow(localSize, rowinds, globalSize, colinds, elemcoefs) );
176 
177  for(i=0; i<localSize; ++i) {
178  for(int j=0; j<globalSize; ++j) values[i][j] *= 2.0;
179  delete [] elemcoefs[i];
180  }
181  delete [] elemcoefs;
182 
183  CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
184 
185  if (matrix->writeToFile("A_Az_filled2.mtx") != true) {
186  ERReturn(-1);
187  }
188 
189  if (matrix->readFromFile("A_Az_filled2.mtx") != true) {
190  ERReturn(-1);
191  }
192 
193  CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
194 
195  delete matrix;
196 #endif
197  return(0);
198 }
199 
200 int test_AztecWrappers::test2()
201 {
202  return(0);
203 }
204 
205 int test_AztecWrappers::test3()
206 {
207  return(0);
208 }
209 
210 int test_AztecWrappers::test4()
211 {
212  return(0);
213 }
214 
215 #ifdef HAVE_FEI_AZTECOO
216 //==============================================================================
217 int compare_DMSR_contents(fei_trilinos::AztecDMSR_Matrix& matrix, int localOffset,
218  std::vector<std::vector<int> >& colIndices,
219  std::vector<std::vector<double> >& values)
220 {
221  int localSize = colIndices.size();
222 
223  for(int i=0; i<localSize; i++) {
224  int row = i+localOffset;
225  int rowLen = matrix.rowLength(row);
226  if (rowLen == 0) ERReturn(-1);
227  int* tempInd = new int[rowLen];
228  double* tempVal = new double[rowLen];
229  std::vector<int> sortedInd;
230  std::vector<double> sortedVal;
231 
232  int tmpLen = rowLen;
233  matrix.getRow(row, tmpLen, tempVal, tempInd);
234  if (tmpLen != rowLen) ERReturn(-1);
235 
236  for(int j=0; j<tmpLen; j++) {
237  int offset = fei::sortedListInsert(tempInd[j], sortedInd);
238  if (offset < 0) ERReturn(-1);
239  sortedVal.insert(sortedVal.begin()+offset, tempVal[j]);
240  }
241 
242  delete [] tempInd;
243  delete [] tempVal;
244 
245  std::vector<int>& colInds = colIndices[i];
246  if (sortedInd != colInds) {
247  ERReturn(-1);
248  }
249  std::vector<double>& vals = values[i];
250  if (sortedVal != vals) {
251  ERReturn(-1);
252  }
253  }
254 
255  return(0);
256 }
257 
258 //==============================================================================
259 int fill_DMSR(fei_trilinos::AztecDMSR_Matrix& matrix, int localOffset,
260  std::vector<std::vector<int> >& colIndices,
261  std::vector<std::vector<double> >& values, bool sumInto)
262 {
263  int localSize = colIndices.size();
264 
265  for(int i=0; i<localSize; i++) {
266  int row = localOffset + i;
267  int rowLen = values[i].size();
268 
269  if (sumInto) {
270  CHK_ERR( matrix.sumIntoRow(row, rowLen,
271  &(values[i][0]), &(colIndices[i][0])) );
272  }
273  else {
274  CHK_ERR( matrix.putRow(row, rowLen,
275  &(values[i][0]), &(colIndices[i][0])) );
276  }
277  }
278 
279  return(0);
280 }
281 #endif
int sortedListInsert(const T &item, std::vector< T > &list)