FEI Version of the Day
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 <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_LSVector.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   std::vector<int> update(localSize);
00075   for(i=0; i<localSize; i++) update[i] = localOffset+i;
00076 
00077   fei::SharedPtr<fei_trilinos::Aztec_Map> map(
00078     new fei_trilinos::Aztec_Map(globalSize, localSize, &update[0], localOffset, comm_));
00079 
00080   fei_trilinos::AztecDMSR_Matrix* matrix = new fei_trilinos::AztecDMSR_Matrix(map);
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 #endif
00197   return(0);
00198 }
00199 
00200 int test_AztecWrappers::test2()
00201 {
00202   return(0);
00203 }
00204 
00205 int test_AztecWrappers::test3()
00206 {
00207   return(0);
00208 }
00209 
00210 int test_AztecWrappers::test4()
00211 {
00212   return(0);
00213 }
00214 
00215 #ifdef HAVE_FEI_AZTECOO
00216 //==============================================================================
00217 int compare_DMSR_contents(fei_trilinos::AztecDMSR_Matrix& matrix, int localOffset,
00218         std::vector<std::vector<int> >& colIndices,
00219         std::vector<std::vector<double> >& values)
00220 {
00221   int localSize = colIndices.size();
00222 
00223   for(int i=0; i<localSize; i++) {
00224     int row = i+localOffset;
00225     int rowLen = matrix.rowLength(row);
00226     if (rowLen == 0) ERReturn(-1);
00227     int* tempInd = new int[rowLen];
00228     double* tempVal = new double[rowLen];
00229     std::vector<int> sortedInd;
00230     std::vector<double> sortedVal;
00231 
00232     int tmpLen = rowLen;
00233     matrix.getRow(row, tmpLen, tempVal, tempInd);
00234     if (tmpLen != rowLen) ERReturn(-1);
00235 
00236     for(int j=0; j<tmpLen; j++) {
00237       int offset = fei::sortedListInsert(tempInd[j], sortedInd);
00238       if (offset < 0) ERReturn(-1);
00239       sortedVal.insert(sortedVal.begin()+offset, tempVal[j]);
00240     }
00241 
00242     delete [] tempInd;
00243     delete [] tempVal;
00244 
00245     std::vector<int>& colInds = colIndices[i];
00246     if (sortedInd != colInds) {
00247       ERReturn(-1);
00248     }
00249     std::vector<double>& vals = values[i];
00250     if (sortedVal != vals) {
00251       ERReturn(-1);
00252     }
00253   }
00254 
00255   return(0);
00256 }
00257 
00258 //==============================================================================
00259 int fill_DMSR(fei_trilinos::AztecDMSR_Matrix& matrix, int localOffset,
00260         std::vector<std::vector<int> >& colIndices,
00261         std::vector<std::vector<double> >& values, bool sumInto)
00262 {
00263   int localSize = colIndices.size();
00264 
00265   for(int i=0; i<localSize; i++) {
00266     int row = localOffset + i;
00267     int rowLen = values[i].size();
00268 
00269     if (sumInto) {
00270       CHK_ERR( matrix.sumIntoRow(row, rowLen,
00271          &(values[i][0]), &(colIndices[i][0])) );
00272     }
00273     else {
00274       CHK_ERR( matrix.putRow(row, rowLen,
00275            &(values[i][0]), &(colIndices[i][0])) );
00276     }
00277   }
00278 
00279   return(0);
00280 }
00281 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends