Advertisement
antt

solveLinmex.cpp

Feb 6th, 2013
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. /*
  2. A very simple mex-implementation for testing the Fliege's algorithm.
  3. Copyright (C) 2013 Antti Lipponen ([email protected])
  4.  
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  
  18. -------------------------------------------------------------------------
  19.  
  20. The algorithm implemented here is based on the algorithm published in
  21. "A Randomized Parallel Algorithm with Run Time $O(n^2)$ for Solving an
  22.  $n \times n$ System of Linear Equations",  http://arxiv.org/abs/1209.3995v1
  23.  
  24. I also took some ideas from Python code by Riccardo Murri and Benjamin Jonen (thanks!),
  25. https://code.google.com/p/gc3pie/source/browse/#svn%2Ftrunk%2Fgc3pie%2Fexamples%2Foptimizer%2Flinear-systems-solver
  26.  
  27. TO RUN THE TEST:
  28. ****************
  29. Compile to mex-file in Matlab for example with:
  30. mex solveLinmex.cpp CXXFLAGS="\$CFLAGS -fopenmp" LDFLAGS="\$LDFLAGS -fopenmp" LDOPTIMFLAGS="-O3" CXXOPTIMFLAGS="-O3 -DNDEBUG"
  31.  
  32. Test script:
  33. N = 1000;
  34. A = randn(N,N);
  35. b = randn(N,1);
  36.  
  37. tic
  38. x_backslash = A\b;
  39. toc
  40.  
  41. tic
  42. x_fliege = solveLinmex(A',b, randn(N,N+1));
  43. toc
  44.  
  45. Result:
  46. Elapsed time for x_backslash is 0.057052 seconds.
  47. Elapsed time for x_fliege is 0.655128 seconds.
  48.  
  49.  
  50. */
  51.  
  52. #include "mex.h"
  53.  
  54. void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
  55. {
  56.     const unsigned int n = (int) mxGetM(prhs[0]);
  57.     const unsigned int m = (int) mxGetN(prhs[0]);
  58.     const double * const A = mxGetPr(prhs[0]);
  59.     const double * const b = mxGetPr(prhs[1]);
  60.     double * const v1 = mxGetPr(prhs[2]);
  61.     double * const v2 = new double[(n+1)*n];
  62.     double * const av = new double[n+1];
  63.        
  64.     for(unsigned int k=0; k<m; ++k) {
  65.        
  66.         const double * const AA = A + k*n;
  67.         const double * const bk = b + k;
  68.         double * const vcurrent = (k%2) ? v2 : v1;
  69.         double * const xcurrent = (k%2) ? v1 : v2;
  70.                
  71.        
  72.         #pragma omp parallel for
  73.         for(unsigned int i=0; i<n+1; ++i) {
  74.             const double * const vv = vcurrent + i*n;
  75.            
  76.             av[i] = 0.0;
  77.             for(unsigned int j=0; j<n; ++j) {
  78.                 av[i] += AA[j]*vv[j];
  79.             }
  80.         }        
  81.        
  82.        
  83.         #pragma omp parallel for
  84.         for(unsigned int i=0; i<n+1; ++i) {
  85.             const double * const vv = vcurrent + ((i<n) ? (i+1)*n : 0);
  86.             const double * const uu = vcurrent + i*n;
  87.             double * const xx = xcurrent + i*n;
  88.            
  89.             const double t = ((*bk)-av[i])/(av[(i<n) ? (i+1) : 0]-av[i]);
  90.            
  91.             for(unsigned int j=0; j<n; ++j) {
  92.                 xx[j] = t*(vv[j]-uu[j]) + uu[j];
  93.             }
  94.         }
  95.        
  96.     }
  97.        
  98.     plhs[0] = mxCreateDoubleMatrix(n, 1, mxREAL);
  99.     double *xvec = mxGetPr(plhs[0]);
  100.        
  101.     double * const vfinal = (m%2) ? v2 : v1;
  102.     for(unsigned int i=0; i<m; ++i) {
  103.         xvec[i] = vfinal[i];
  104.     }    
  105.    
  106.     delete [] av;
  107.     delete [] v2;
  108.    
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement