paralution_pcg.cpp 4.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
// **************************************************************************
//
//    PARALUTION   www.paralution.com
//
//    Copyright (C) 2015  PARALUTION Labs UG (haftungsbeschränkt) & Co. KG
//                        Am Hasensprung 6, 76571 Gaggenau
//                        Handelsregister: Amtsgericht Mannheim, HRA 706051
//                        Vertreten durch:
//                        PARALUTION Labs Verwaltungs UG (haftungsbeschränkt)
//                        Am Hasensprung 6, 76571 Gaggenau
//                        Handelsregister: Amtsgericht Mannheim, HRB 721277
//                        Geschäftsführer: Dimitar Lukarski, Nico Trost
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// **************************************************************************



// PARALUTION version 1.1.0 


#include <paralution.hpp>
#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {

  // Check output argument
  if (nlhs != 1) {
    mexErrMsgTxt("Expecting one output argument.");
    return;
  }

  // Check input arguments
  if (nrhs < 2) {
    mexErrMsgTxt("Expecting at least two input arguments.");
    return;
  }

  // Check if input matrix and rhs are double precision
  if (!mxIsDouble(prhs[0]) || !mxIsSparse(prhs[0])) {
    mexErrMsgTxt("Expecting sparse matrix in double precision.");
    return;
  }

  if (!mxIsDouble(prhs[1])) {
    mexErrMsgTxt("Expecting rhs in double precision.");
    return;
  }

  double tol     = 1e-6;
  mwSize maxIter = 10000;

  if (nrhs > 2) tol     = mxGetScalar(prhs[2]);
  if (nrhs > 3) maxIter = mxGetScalar(prhs[3]);

  // Get matrix arrays
  mwIndex *mw_row     = mxGetIr(prhs[0]);
  mwIndex *mw_col_ptr = mxGetJc(prhs[0]);
  double  *mw_val     = mxGetPr(prhs[0]);

  // Get matrix sizes
  int nrow = int(mxGetN(prhs[0]));
  int ncol = int(mxGetM(prhs[0]));
  int nnz  = int(mw_col_ptr[nrow]);

  int *row = NULL;
  int *col_ptr = NULL;
  double *val = NULL;
  paralution::allocate_host(nnz, &val);
  paralution::allocate_host(nnz, &row);
  paralution::allocate_host(nrow+1, &col_ptr);

  // copy needed due to diff types
  for (int i=0; i<nnz; ++i) {
    row[i] = mw_row[i];
    val[i] = mw_val[i];
  }

  for (int i=0; i<nrow+1; ++i)
    col_ptr[i] = mw_col_ptr[i];

  // Get rhs
  double  *mw_rhs = mxGetPr(prhs[1]);
  double  *rhs = NULL;

  paralution::allocate_host(nrow, &rhs);

  for (int i=0; i<nrow; ++i)
    rhs[i] = mw_rhs[i];

  // Allocate output vector
  plhs[0] = mxCreateDoubleMatrix(nrow, 1, mxREAL);
  double  *sol = mxGetPr(plhs[0]);

  /*
  // Time measurement
  double tick, tack;
  tick = paralution::paralution_time();
  */

  // Initialize PARALUTION
  paralution::init_paralution();

  // Create PARALUTION data structures
  paralution::LocalMatrix<double> A;
  paralution::LocalVector<double> b;
  paralution::LocalVector<double> x;

  // Fill PARALUTION data
  // For symmetric matrices CSC == CSR
  A.SetDataPtrCSR(&col_ptr, &row, &val, "A", nnz, ncol, nrow);
  b.SetDataPtr(&rhs, "b", ncol);
  x.Allocate("x", nrow);

  // Solver
  paralution::CG<paralution::LocalMatrix<double>, paralution::LocalVector<double>, double> ls;
  // Preconditioner
  //  paralution::MultiColoredILU<paralution::LocalMatrix<double>, paralution::LocalVector<double>, double> p;
  paralution::Jacobi<paralution::LocalMatrix<double>, paralution::LocalVector<double>, double> p;

  ls.SetOperator(A);
  ls.SetPreconditioner(p);
  ls.Init(0.0, tol, 1e8, maxIter);

  // Build solver and preconditioner
  ls.Build();

  ls.MoveToAccelerator();
  A.MoveToAccelerator();
  b.MoveToAccelerator();
  x.MoveToAccelerator();

  x.Zeros();

  // Disable PARALUTION printing
  //ls.Verbose(0);

  // Verbose printing
  ls.Verbose(1);
  A.info();

  /*
  tack = paralution::paralution_time();
  std::cout << "PARALUTION::Building time: " << (tack-tick)/1000000. << "sec.\n";
  tick = paralution::paralution_time();
  */

  // Solve Ax=b
  ls.Solve(b, &x);

  /*
  tack = paralution::paralution_time();
  std::cout << "PARALUTION::Solving time: " << (tack-tick)/1000000. << "sec.\n";
  */

  ls.Clear();
  A.Clear();
  b.Clear();

  double *ptr_sol = NULL;
  x.MoveToHost();
  x.LeaveDataPtr(&ptr_sol);

  for (int i=0; i<nrow; ++i)
    sol[i] = ptr_sol[i];

  paralution::free_host(&ptr_sol);

  paralution::stop_paralution();

  return;

}