/[escript]/trunk/ripley/test/SystemMatrixTestCase.cpp
ViewVC logotype

Diff of /trunk/ripley/test/SystemMatrixTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 5219 by caltinay, Wed Oct 22 05:06:48 2014 UTC revision 5220 by caltinay, Thu Oct 23 03:50:37 2014 UTC
# Line 130  const double ref_symm_bs4[] = Line 130  const double ref_symm_bs4[] =
130    
131  const double* ref_symm[] = {ref_symm_bs1, ref_symm_bs2, ref_symm_bs3, ref_symm_bs4};  const double* ref_symm[] = {ref_symm_bs1, ref_symm_bs2, ref_symm_bs3, ref_symm_bs4};
132    
133    /// helper
134    double lsup(const double* d0, const double* d1, int length)
135    {
136        double result = 0.;
137        for (int i=0; i<length; i++) {
138            result = std::max(result, std::abs(d0[i] - d1[i]));
139            //std::cerr << d0[i] << " " << d1[i] << std::endl;
140        }
141        return result;
142    }
143    
144  TestSuite* SystemMatrixTestCase::suite()  TestSuite* SystemMatrixTestCase::suite()
145  {  {
146      TestSuite *testSuite = new TestSuite("SystemMatrixTestCase");      TestSuite *testSuite = new TestSuite("SystemMatrixTestCase");
147      testSuite->addTest(new TestCaller<SystemMatrixTestCase>(      testSuite->addTest(new TestCaller<SystemMatrixTestCase>(
148                  "testSpMV",&SystemMatrixTestCase::testSpMV));                  "testSpMV_CPU_blocksize1_nonsymmetric",
149                    &SystemMatrixTestCase::testSpMV_CPU_blocksize1_nonsymmetric));
150        testSuite->addTest(new TestCaller<SystemMatrixTestCase>(
151                    "testSpMV_CPU_blocksize2_nonsymmetric",
152                    &SystemMatrixTestCase::testSpMV_CPU_blocksize2_nonsymmetric));
153        testSuite->addTest(new TestCaller<SystemMatrixTestCase>(
154                    "testSpMV_CPU_blocksize3_nonsymmetric",
155                    &SystemMatrixTestCase::testSpMV_CPU_blocksize3_nonsymmetric));
156        testSuite->addTest(new TestCaller<SystemMatrixTestCase>(
157                    "testSpMV_CPU_blocksize4_nonsymmetric",
158                    &SystemMatrixTestCase::testSpMV_CPU_blocksize4_nonsymmetric));
159        testSuite->addTest(new TestCaller<SystemMatrixTestCase>(
160                    "testSpMV_CPU_blocksize1_symmetric",
161                    &SystemMatrixTestCase::testSpMV_CPU_blocksize1_symmetric));
162        testSuite->addTest(new TestCaller<SystemMatrixTestCase>(
163                    "testSpMV_CPU_blocksize2_symmetric",
164                    &SystemMatrixTestCase::testSpMV_CPU_blocksize2_symmetric));
165        testSuite->addTest(new TestCaller<SystemMatrixTestCase>(
166                    "testSpMV_CPU_blocksize3_symmetric",
167                    &SystemMatrixTestCase::testSpMV_CPU_blocksize3_symmetric));
168        testSuite->addTest(new TestCaller<SystemMatrixTestCase>(
169                    "testSpMV_CPU_blocksize4_symmetric",
170                    &SystemMatrixTestCase::testSpMV_CPU_blocksize4_symmetric));
171      return testSuite;      return testSuite;
172  }  }
173    
174  void SystemMatrixTestCase::testSpMV()  void SystemMatrixTestCase::setUp()
175  {  {
176      esysUtils::JMPI info(esysUtils::makeInfo(MPI_COMM_WORLD));      mpiInfo = esysUtils::makeInfo(MPI_COMM_WORLD);
177      escript::Domain_ptr dom(new ripley::Rectangle(4, 3, 0., 0., 1., 1.));      domain.reset(new ripley::Rectangle(4, 3, 0., 0., 1., 1.));
178      escript::FunctionSpace fs(escript::solution(*dom));  }
   
     int blocksize = 1;  
     bool symmetric = false;  
     int firstdiag = (symmetric ? 4 : 0);  
     ripley::IndexVector offsets(diag_off+firstdiag, diag_off+9);  
179    
180      // create a 20x20 matrix with 9 diagonals, blocksize 1, non-symmetric  escript::ASM_ptr SystemMatrixTestCase::createMatrix(int blocksize,
181      escript::ASM_ptr matptr(new ripley::SystemMatrix(info, blocksize,                                                      bool symmetric)
182                 fs, rows, offsets, symmetric));  {
183        escript::FunctionSpace fs(escript::solution(*domain));
184        const int firstdiag = (symmetric ? 4 : 0);
185        const ripley::IndexVector offsets(diag_off+firstdiag, diag_off+9);
186    
187        // create a matrix with 9 diagonals, given blocksize and symmetric flag
188        escript::ASM_ptr matptr(new ripley::SystemMatrix(mpiInfo, blocksize, fs,
189                                 rows, offsets, symmetric));
190      ripley::SystemMatrix* mat(dynamic_cast<ripley::SystemMatrix*>(matptr.get()));      ripley::SystemMatrix* mat(dynamic_cast<ripley::SystemMatrix*>(matptr.get()));
191    
192      ripley::IndexVector rowIdx(4);      ripley::IndexVector rowIdx(4);
# Line 176  void SystemMatrixTestCase::testSpMV() Line 211  void SystemMatrixTestCase::testSpMV()
211          }          }
212          mat->add(rowIdx, array);          mat->add(rowIdx, array);
213      }      }
214        //mat->saveMM("/tmp/test.mtx");
215        return matptr;
216    }
217    
218    escript::Data SystemMatrixTestCase::createInputVector(int blocksize)
219    {
220        escript::FunctionSpace fs(escript::solution(*domain));
221      escript::DataTypes::ShapeType shape;      escript::DataTypes::ShapeType shape;
222      if (blocksize > 1)      if (blocksize > 1)
223          shape.push_back(blocksize);          shape.push_back(blocksize);
# Line 186  void SystemMatrixTestCase::testSpMV() Line 227  void SystemMatrixTestCase::testSpMV()
227          for (int j=0; j<blocksize; j++)          for (int j=0; j<blocksize; j++)
228              xx[j] = (double)i*blocksize + j;              xx[j] = (double)i*blocksize + j;
229      }      }
230        return x;
231    }
232    
233      //mat->saveMM("/tmp/test.mtx");  void SystemMatrixTestCase::testSpMV_CPU_blocksize1_nonsymmetric()
234      escript::Data y = mat->vectorMultiply(x);  {
235      double lsup = 0.;      int blocksize = 1;
236      for (int i=0; i<rows; i++) {      bool symmetric = false;
237          const double* yy = y.getSampleDataRO(i);      escript::ASM_ptr mat(createMatrix(blocksize, symmetric));
238          const double* yref = (symmetric ? &ref_symm[blocksize-1][i*blocksize] : &ref[blocksize-1][i*blocksize]);      const escript::Data x(createInputVector(blocksize));
239          for (int j=0; j<blocksize; j++) {      const escript::Data y(mat->vectorMultiply(x));
240              lsup = std::max(lsup, std::abs(yref[j] - yy[j]));      const double* yref = ref[blocksize-1];
241              //std::cerr << yref[j] << " " << yy[j] << std::endl;      const double* yy = y.getSampleDataRO(0);
242          }      double error = lsup(yref, yy, blocksize*rows);
243      }      CPPUNIT_ASSERT(error < 1e-12);
244    }
245    
246      CPPUNIT_ASSERT(lsup < 1e-12);  void SystemMatrixTestCase::testSpMV_CPU_blocksize2_nonsymmetric()
247    {
248        int blocksize = 2;
249        bool symmetric = false;
250        escript::ASM_ptr mat(createMatrix(blocksize, symmetric));
251        const escript::Data x(createInputVector(blocksize));
252        const escript::Data y(mat->vectorMultiply(x));
253        const double* yref = ref[blocksize-1];
254        const double* yy = y.getSampleDataRO(0);
255        double error = lsup(yref, yy, blocksize*rows);
256        CPPUNIT_ASSERT(error < 1e-12);
257    }
258    
259    void SystemMatrixTestCase::testSpMV_CPU_blocksize3_nonsymmetric()
260    {
261        int blocksize = 3;
262        bool symmetric = false;
263        escript::ASM_ptr mat(createMatrix(blocksize, symmetric));
264        const escript::Data x(createInputVector(blocksize));
265        const escript::Data y(mat->vectorMultiply(x));
266        const double* yref = ref[blocksize-1];
267        const double* yy = y.getSampleDataRO(0);
268        double error = lsup(yref, yy, blocksize*rows);
269        CPPUNIT_ASSERT(error < 1e-12);
270    }
271    
272    void SystemMatrixTestCase::testSpMV_CPU_blocksize4_nonsymmetric()
273    {
274        int blocksize = 4;
275        bool symmetric = false;
276        escript::ASM_ptr mat(createMatrix(blocksize, symmetric));
277        const escript::Data x(createInputVector(blocksize));
278        const escript::Data y(mat->vectorMultiply(x));
279        const double* yref = ref[blocksize-1];
280        const double* yy = y.getSampleDataRO(0);
281        double error = lsup(yref, yy, blocksize*rows);
282        CPPUNIT_ASSERT(error < 1e-12);
283    }
284    
285    void SystemMatrixTestCase::testSpMV_CPU_blocksize1_symmetric()
286    {
287        int blocksize = 1;
288        bool symmetric = true;
289        escript::ASM_ptr mat(createMatrix(blocksize, symmetric));
290        const escript::Data x(createInputVector(blocksize));
291        const escript::Data y(mat->vectorMultiply(x));
292        const double* yref = ref_symm[blocksize-1];
293        const double* yy = y.getSampleDataRO(0);
294        double error = lsup(yref, yy, blocksize*rows);
295        CPPUNIT_ASSERT(error < 1e-12);
296    }
297    
298    void SystemMatrixTestCase::testSpMV_CPU_blocksize2_symmetric()
299    {
300        int blocksize = 2;
301        bool symmetric = true;
302        escript::ASM_ptr mat(createMatrix(blocksize, symmetric));
303        const escript::Data x(createInputVector(blocksize));
304        const escript::Data y(mat->vectorMultiply(x));
305        const double* yref = ref_symm[blocksize-1];
306        const double* yy = y.getSampleDataRO(0);
307        double error = lsup(yref, yy, blocksize*rows);
308        CPPUNIT_ASSERT(error < 1e-12);
309    }
310    
311    void SystemMatrixTestCase::testSpMV_CPU_blocksize3_symmetric()
312    {
313        int blocksize = 3;
314        bool symmetric = true;
315        escript::ASM_ptr mat(createMatrix(blocksize, symmetric));
316        const escript::Data x(createInputVector(blocksize));
317        const escript::Data y(mat->vectorMultiply(x));
318        const double* yref = ref_symm[blocksize-1];
319        const double* yy = y.getSampleDataRO(0);
320        double error = lsup(yref, yy, blocksize*rows);
321        CPPUNIT_ASSERT(error < 1e-12);
322    }
323    
324    void SystemMatrixTestCase::testSpMV_CPU_blocksize4_symmetric()
325    {
326        int blocksize = 4;
327        bool symmetric = true;
328        escript::ASM_ptr mat(createMatrix(blocksize, symmetric));
329        const escript::Data x(createInputVector(blocksize));
330        const escript::Data y(mat->vectorMultiply(x));
331        const double* yref = ref_symm[blocksize-1];
332        const double* yy = y.getSampleDataRO(0);
333        double error = lsup(yref, yy, blocksize*rows);
334        CPPUNIT_ASSERT(error < 1e-12);
335  }  }
336    

Legend:
Removed from v.5219  
changed lines
  Added in v.5220

  ViewVC Help
Powered by ViewVC 1.1.26