test_AztecWrappers.cpp

00001 /*--------------------------------------------------------------------*/
00002 /*    Copyright 2005 Sandia Corporation.                              */
00003 /*    Under the terms of Contract DE-AC04-94AL85000, there is a       */
00004 /*    non-exclusive license for use of this work by or on behalf      */
00005 /*    of the U.S. Government.  Export of this program may require     */
00006 /*    a license from the United States Government.                    */
00007 /*--------------------------------------------------------------------*/
00008 
00009 
00010 #include <fei_macros.hpp>
00011 
00012 #include <fei_mpi.h>
00013 
00014 #include <test_utils/test_AztecWrappers.hpp>
00015 
00016 #include <snl_fei_ArrayUtils.hpp>
00017 #include <test_utils/LibraryFactory.hpp>
00018 
00019 #ifdef HAVE_FEI_AZTECOO
00020 #include <fei_Aztec_Map.hpp>
00021 #include <fei_Aztec_Vector.hpp>
00022 #include <fei_AztecDMSR_Matrix.hpp>
00023 #endif
00024 
00025 #undef fei_file
00026 #define fei_file "test_AztecWrappers.cpp"
00027 
00028 #include <fei_ErrMacros.hpp>
00029 
00030 #ifdef HAVE_FEI_AZTECOO
00031 int compare_DMSR_contents(fei_trilinos::AztecDMSR_Matrix& matrix, int localOffset,
00032         std::vector<std::vector<int> >& colIndices,
00033         std::vector<std::vector<double> >& values);
00034 
00035 int fill_DMSR(fei_trilinos::AztecDMSR_Matrix& matrix, int localOffset,
00036         std::vector<std::vector<int> >& colIndices,
00037         std::vector<std::vector<double> >& values, bool sumInto);
00038 #endif
00039 
00040 test_AztecWrappers::test_AztecWrappers(MPI_Comm comm)
00041  : tester(comm)
00042 {
00043 }
00044 
00045 test_AztecWrappers::~test_AztecWrappers()
00046 {
00047 }
00048 
00049 int test_AztecWrappers::runtests()
00050 {
00051   if (numProcs_ < 2) {
00052     CHK_ERR( serialtest1() );
00053   }
00054 
00055   CHK_ERR( test1() );
00056   CHK_ERR( test2() );
00057   CHK_ERR( test3() );
00058   CHK_ERR( test4() );
00059   return(0);
00060 }
00061 
00062 int test_AztecWrappers::serialtest1()
00063 {
00064   return(0);
00065 }
00066 
00067 int test_AztecWrappers::test1()
00068 {
00069 #ifdef HAVE_FEI_AZTECOO
00070   int localSize = 3, globalSize = localSize*numProcs_;
00071   int localOffset = localSize*localProc_;
00072   int i;
00073 
00074   fei_trilinos::Aztec_Map* map =
00075     new fei_trilinos::Aztec_Map(globalSize, localSize, localOffset, comm_);
00076 
00077   std::vector<int> update(localSize);
00078   for(i=0; i<localSize; i++) update[i] = localOffset+i;
00079 
00080   fei_trilinos::AztecDMSR_Matrix* matrix = new fei_trilinos::AztecDMSR_Matrix(*map, &update[0], true);
00081 
00082   std::vector<int> elemrows(localSize);
00083   std::vector<int> elemcols(globalSize);
00084   double** elemcoefs = new double*[localSize];
00085   for(int j=0; j<globalSize; ++j) elemcols[j] = j;
00086   for(i=0; i<localSize; ++i) {
00087     elemrows[i] = localOffset+i;
00088     elemcoefs[i] = new double[globalSize];
00089     for(int j=0; j<globalSize; ++j) {
00090       elemcoefs[i][j] = (double)(localOffset+i+j);
00091     }
00092   }
00093   
00094   std::vector<std::vector<int> > colIndices(localSize);
00095   std::vector<std::vector<double> > values(localSize);
00096   std::vector<int> rowLengths(localSize);
00097   std::vector<int*> colPtrs(localSize);
00098   int nnzeros = 0;
00099 
00100   for(i=0; i<localSize; i++) {
00101     int diagEntry = 0;
00102     int row = i+localOffset;
00103     for(int j=0; j<globalSize; j++) {
00104       int col = j;
00105       if (col == row) diagEntry = 1;
00106       colIndices[i].push_back(col);
00107       values[i].push_back((double)(row+col));
00108     }
00109     rowLengths[i] = colIndices[i].size() - diagEntry;
00110     nnzeros += rowLengths[i] + 1;
00111     colPtrs[i] = &(colIndices[i][0]);
00112   }
00113 
00114   matrix->allocate( &rowLengths[0] );
00115 
00116   if (!(matrix->isAllocated())) {
00117     ERReturn(-1);
00118   }
00119 
00120   if (matrix->getNumNonZeros() != nnzeros) {
00121     ERReturn(-1);
00122   }
00123 
00124   CHK_ERR( fill_DMSR(*matrix, localOffset, colIndices, values, true) );
00125 
00126   int* rowinds = &elemrows[0];
00127   int* colinds = &elemcols[0];
00128 
00129   CHK_ERR( matrix->sumIntoRow(localSize, rowinds, globalSize, colinds, elemcoefs) );
00130 
00131   for(i=0; i<localSize; ++i) {
00132     for(int j=0; j<globalSize; ++j) values[i][j] *= 2.0;
00133   }
00134 
00135   CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
00136 
00137   for(i=0; i<localSize; ++i) {
00138     for(int j=0; j<globalSize; ++j) values[i][j] /= 2.0;
00139   }
00140 
00141   CHK_ERR( fill_DMSR(*matrix, localOffset, colIndices, values, false) );
00142 
00143   CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
00144 
00145   if (matrix->writeToFile("A_Az_notFilled.mtx") != true) {
00146     ERReturn(-1);
00147   }
00148 
00149   if (matrix->readFromFile("A_Az_notFilled.mtx") != true) {
00150     ERReturn(-1);
00151   }
00152 
00153   CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
00154 
00155   matrix->fillComplete();
00156 
00157   if (!(matrix->isFilled())) {
00158     ERReturn(-1);
00159   }
00160 
00161   if (matrix->writeToFile("A_Az_filled.mtx") != true) {
00162     ERReturn(-1);
00163   }
00164 
00165   CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
00166 
00167   CHK_ERR( fill_DMSR(*matrix, localOffset, colIndices, values, false) );
00168 
00169   CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
00170 
00171   matrix->put(0.0);
00172 
00173   CHK_ERR( fill_DMSR(*matrix, localOffset, colIndices, values, true) );
00174 
00175   CHK_ERR( matrix->sumIntoRow(localSize, rowinds, globalSize, colinds, elemcoefs) );
00176 
00177   for(i=0; i<localSize; ++i) {
00178     for(int j=0; j<globalSize; ++j) values[i][j] *= 2.0;
00179     delete [] elemcoefs[i];
00180   }
00181   delete [] elemcoefs;
00182 
00183   CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
00184 
00185   if (matrix->writeToFile("A_Az_filled2.mtx") != true) {
00186     ERReturn(-1);
00187   }
00188 
00189   if (matrix->readFromFile("A_Az_filled2.mtx") != true) {
00190     ERReturn(-1);
00191   }
00192 
00193   CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
00194 
00195   delete matrix;
00196   delete map;
00197 #endif
00198   return(0);
00199 }
00200 
00201 int test_AztecWrappers::test2()
00202 {
00203   return(0);
00204 }
00205 
00206 int test_AztecWrappers::test3()
00207 {
00208   return(0);
00209 }
00210 
00211 int test_AztecWrappers::test4()
00212 {
00213   return(0);
00214 }
00215 
00216 #ifdef HAVE_FEI_AZTECOO
00217 //==============================================================================
00218 int compare_DMSR_contents(fei_trilinos::AztecDMSR_Matrix& matrix, int localOffset,
00219         std::vector<std::vector<int> >& colIndices,
00220         std::vector<std::vector<double> >& values)
00221 {
00222   int localSize = colIndices.size();
00223 
00224   for(int i=0; i<localSize; i++) {
00225     int row = i+localOffset;
00226     int rowLen = matrix.rowLength(row);
00227     if (rowLen == 0) ERReturn(-1);
00228     int* tempInd = new int[rowLen];
00229     double* tempVal = new double[rowLen];
00230     std::vector<int> sortedInd;
00231     std::vector<double> sortedVal;
00232 
00233     int tmpLen = rowLen;
00234     matrix.getRow(row, tmpLen, tempVal, tempInd);
00235     if (tmpLen != rowLen) ERReturn(-1);
00236 
00237     for(int j=0; j<tmpLen; j++) {
00238       int offset = snl_fei::sortedListInsert(tempInd[j], sortedInd);
00239       if (offset < 0) ERReturn(-1);
00240       sortedVal.insert(sortedVal.begin()+offset, tempVal[j]);
00241     }
00242 
00243     delete [] tempInd;
00244     delete [] tempVal;
00245 
00246     std::vector<int>& colInds = colIndices[i];
00247     if (sortedInd != colInds) {
00248       ERReturn(-1);
00249     }
00250     std::vector<double>& vals = values[i];
00251     if (sortedVal != vals) {
00252       ERReturn(-1);
00253     }
00254   }
00255 
00256   return(0);
00257 }
00258 
00259 //==============================================================================
00260 int fill_DMSR(fei_trilinos::AztecDMSR_Matrix& matrix, int localOffset,
00261         std::vector<std::vector<int> >& colIndices,
00262         std::vector<std::vector<double> >& values, bool sumInto)
00263 {
00264   int localSize = colIndices.size();
00265 
00266   for(int i=0; i<localSize; i++) {
00267     int row = localOffset + i;
00268     int rowLen = values[i].size();
00269 
00270     if (sumInto) {
00271       CHK_ERR( matrix.sumIntoRow(row, rowLen,
00272          &(values[i][0]), &(colIndices[i][0])) );
00273     }
00274     else {
00275       CHK_ERR( matrix.putRow(row, rowLen,
00276            &(values[i][0]), &(colIndices[i][0])) );
00277     }
00278   }
00279 
00280   return(0);
00281 }
00282 #endif

Generated on Wed May 12 21:30:42 2010 for FEI by  doxygen 1.4.7