solver_ilu2.hpp 19.1 KB
Newer Older
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1

Kirill Terekhov's avatar
Kirill Terekhov committed
2 3
#ifndef __SOLVER_ILU2__
#define __SOLVER_ILU2__
Dmitry Bagaev's avatar
Dmitry Bagaev committed
4

Kirill Terekhov's avatar
Kirill Terekhov committed
5 6 7
#include <iomanip>

#include "inmost_solver.h"
Dmitry Bagaev's avatar
Dmitry Bagaev committed
8
#include "../solver_prototypes.hpp"
Kirill Terekhov's avatar
Kirill Terekhov committed
9
//#define REPORT_ILU
Kirill Terekhov's avatar
Kirill Terekhov committed
10
//#define REPORT_ILU_PROGRESS
Kirill Terekhov's avatar
Kirill Terekhov committed
11 12 13 14
using namespace INMOST;

#define DEFAULT_TAU 0.005
#define DEFAULT_TAU2 0.00001
Kirill Terekhov's avatar
Kirill Terekhov committed
15 16
//#define LFILL //control, that factorization is not less then fill for ilu2

Kirill Terekhov's avatar
Kirill Terekhov committed
17 18


Dmitry Bagaev's avatar
Dmitry Bagaev committed
19
class ILU2_preconditioner : public Method {
Kirill Terekhov's avatar
Kirill Terekhov committed
20
private:
Dmitry Bagaev's avatar
Dmitry Bagaev committed
21 22 23 24 25 26 27 28 29 30 31 32 33

    Sparse::Matrix *Alink;
    Solver::OrderInfo *info;
    //Sparse::Matrix L,U;
    //Sparse::Vector div;
    std::vector<INMOST_DATA_REAL_TYPE> luv;
    std::vector<INMOST_DATA_ENUM_TYPE> lui;
    interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> ilu, iu;
    INMOST_DATA_ENUM_TYPE Lfill;
    INMOST_DATA_REAL_TYPE tau, tau2;
    Sparse::Vector DL, DR;
    INMOST_DATA_ENUM_TYPE nnz, sciters;
    bool init;
Kirill Terekhov's avatar
Kirill Terekhov committed
34
public:
Dmitry Bagaev's avatar
Dmitry Bagaev committed
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
    INMOST_DATA_REAL_TYPE &RealParameter(std::string name) {
        if (name == "tau") return tau;
        else if (name == "tau2") return tau2;
        throw -1;
    }

    INMOST_DATA_ENUM_TYPE &EnumParameter(std::string name) {
        if (name == "fill") return Lfill;
        else if (name == "scale_iters") return sciters;
        throw -1;
    }

    ILU2_preconditioner(Solver::OrderInfo &info)
            : info(&info), tau(DEFAULT_TAU), tau2(DEFAULT_TAU2) {
        Alink = NULL;
        init = false;
        sciters = 12;
        Lfill = 1;
    }

    bool Initialize() {
        if (isInitialized()) Finalize();
        assert(Alink != NULL);
        nnz = 0;
        for (Sparse::Matrix::iterator it = (*Alink).Begin(); it != (*Alink).End(); ++it) nnz += it->Size();
Kirill Terekhov's avatar
Kirill Terekhov committed
60
#if defined(LFILL)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
61 62
        std::vector<INMOST_DATA_ENUM_TYPE> lfill;
        lfill.reserve(nnz * 4);
Kirill Terekhov's avatar
Kirill Terekhov committed
63
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
        luv.reserve(nnz * 4);
        lui.reserve(nnz * 4);


        std::vector<INMOST_DATA_REAL_TYPE> rv;
        std::vector<INMOST_DATA_ENUM_TYPE> ri;
        rv.reserve(nnz * 16);
        ri.reserve(nnz * 16);
        INMOST_DATA_ENUM_TYPE mobeg, moend, vlocbeg, vlocend, vbeg, vend, k, r, end, iter, j;
        INMOST_DATA_REAL_TYPE leabs, flin, ldiag, udiag, mva;
        INMOST_DATA_ENUM_TYPE curr, foll;
        INMOST_DATA_ENUM_TYPE ind, jn;
        INMOST_DATA_INTEGER_TYPE prev, ipred;
        const INMOST_DATA_REAL_TYPE tol_modif = 1e-12, eps = 1.0e-54, subst = 1.0;
        const INMOST_DATA_ENUM_TYPE UNDEF = ENUMUNDEF, EOL = ENUMUNDEF - 1;
        //Calculate scaling vectors for matrix (from genebs)
        info->GetOverlapRegion(info->GetRank(), mobeg, moend);
        info->GetLocalRegion(info->GetRank(), vlocbeg, vlocend);
        info->GetVectorRegion(vbeg, vend);
        interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> ir(mobeg, moend + 1);
        interval<INMOST_DATA_INTEGER_TYPE, INMOST_DATA_REAL_TYPE> RowValues(vbeg, vend);
Kirill Terekhov's avatar
Kirill Terekhov committed
85
#if defined(LFILL)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
86 87
        interval<INMOST_DATA_INTEGER_TYPE, INMOST_DATA_ENUM_TYPE> RowFill(vbeg, vend);
        //std::fill(RowFill.begin(),RowFill.end(),ENUMUNDEF);
Kirill Terekhov's avatar
Kirill Terekhov committed
88
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
89 90 91 92 93 94 95 96
        interval<INMOST_DATA_INTEGER_TYPE, INMOST_DATA_ENUM_TYPE> RowIndeces(vbeg - 1, vend);

        ilu.set_interval_beg(mobeg);
        ilu.set_interval_end(moend + 1);
        iu.set_interval_beg(mobeg);
        iu.set_interval_end(moend);
        ilu[mobeg] = 0;
        ir[mobeg] = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
97
#if defined(REPORT_ILU)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
98 99 100
        std::cout << "Matrix overlap    " << mobeg << ".." << moend << std::endl;
        std::cout << "Local vector part " << vlocbeg << ".." << vlocend << std::endl;
        std::cout << "Entire vector     " << vbeg << ".." << vend << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
101
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
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
        //Rescale Matrix
        DL.SetInterval(mobeg, moend);
        info->PrepareVector(DR);
        for (k = mobeg; k < moend; k++) {
            for (Sparse::Row::iterator rit = (*Alink)[k].Begin(); rit != (*Alink)[k].End(); ++rit)
                DL[k] += rit->second * rit->second;
            if (DL[k] < eps) DL[k] = 1.0 / subst; else DL[k] = 1.0 / DL[k];
        }
        for (iter = 0; iter < sciters; iter++) {
            for (Sparse::Vector::iterator rit = DR.Begin(); rit != DR.End(); ++rit) *rit = 0.0;
            for (k = vlocbeg; k < vlocend; k++)
                for (Sparse::Row::iterator rit = (*Alink)[k].Begin(); rit != (*Alink)[k].End(); ++rit)
                    DR[rit->first] += DL[k] * rit->second * rit->second;
            info->Accumulate(DR);
            info->Update(DR);
            for (k = vlocbeg; k < vlocend; k++) if (DR[k] < eps) DR[k] = 1.0 / subst; else DR[k] = 1.0 / DR[k];
            for (Sparse::Vector::iterator rit = DL.Begin(); rit != DL.End(); ++rit) *rit = 0.0;
            for (k = mobeg; k < moend; k++)
                for (Sparse::Row::iterator rit = (*Alink)[k].Begin(); rit != (*Alink)[k].End(); ++rit)
                    DL[k] += DR[rit->first] * rit->second * rit->second;
            for (k = mobeg; k < moend; k++) if (DL[k] < eps) DL[k] = 1.0 / subst; else DL[k] = 1.0 / DL[k];
        }
        for (k = mobeg; k < moend; k++) DL[k] = sqrt(DL[k]);
        for (k = vbeg; k < vend; k++) DR[k] = sqrt(DR[k]);
        for (k = mobeg; k < moend; k++)
            for (Sparse::Row::iterator rit = (*Alink)[k].Begin(); rit != (*Alink)[k].End(); ++rit)
                rit->second = DL[k] * rit->second * DR[rit->first];

        //timer = Timer();
        for (interval<INMOST_DATA_INTEGER_TYPE, INMOST_DATA_ENUM_TYPE>::iterator it = RowIndeces.begin();
             it != RowIndeces.end(); ++it)
            *it = UNDEF;
        std::vector<INMOST_DATA_ENUM_TYPE> sort_indeces;
        //INMOST_DATA_ENUM_TYPE nza = 0, nzl = 0, nzu = 0, nzu2 = 0;
        //for(k = mobeg; k != moend; k++) nza += A[k].Size();
        for (k = mobeg; k != moend; k++) {
Kirill Terekhov's avatar
Kirill Terekhov committed
138
#if defined(REPORT_ILU_PROGRESS)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
139 140 141 142 143 144 145
            if (k % 1000 == 0)
            {
                //std::cout << "precond: " << (double)(k-mobeg)/(double)(moend-mobeg)*100 << "\r";
                //printf("%6.2f nza %12d nzl %12d nzu %12d nzu2 %12d\r", (double)(k-mobeg)/(double)(moend-mobeg)*100,nza,nzl,nzu,nzu2);
                printf("precond: %6.2f\r", (double)(k - mobeg) / (double)(moend - mobeg) * 100);
                fflush(stdout);
            }
Kirill Terekhov's avatar
Kirill Terekhov committed
146
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
147 148 149 150 151 152 153 154
            //Uncompress row
            //row_uncompr
            Sparse::Row &Ak = (*Alink)[k];
            end = Ak.Size();
            sort_indeces.clear();
            for (r = 0; r < end; r++)
                if (fabs(Ak.GetValue(r)) > eps) {
                    RowValues[Ak.GetIndex(r)] = Ak.GetValue(r);
Kirill Terekhov's avatar
Kirill Terekhov committed
155
#if defined(LFILL)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
156
                    RowFill[Ak.GetIndex(r)] = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
157
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
                    ind = Ak.GetIndex(r);
                    sort_indeces.push_back(ind);
                }
            std::sort(sort_indeces.begin(), sort_indeces.end());
            prev = static_cast<INMOST_DATA_INTEGER_TYPE>(vbeg) - 1;
            ipred = static_cast<INMOST_DATA_INTEGER_TYPE>(vbeg) - 1;
            for (r = 0; r < sort_indeces.size(); r++) {
                ind = sort_indeces[r];
                RowIndeces[prev] = ind;
                prev = static_cast<INMOST_DATA_INTEGER_TYPE>(ind);
                if (ind <= k) ipred = ind;
            }
            RowIndeces[prev] = EOL;

            if (ipred != static_cast<INMOST_DATA_INTEGER_TYPE>(k)) {
                RowValues[k] = 0.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
174
#if defined(LFILL)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
175
                RowFill[k] = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
176
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
177 178 179 180
                ind = RowIndeces[ipred];
                RowIndeces[ipred] = k;
                RowIndeces[k] = ind;
            }
Kirill Terekhov's avatar
Kirill Terekhov committed
181
#if defined(DIAGONAL_PERTURBATION)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
182
            RowValues[k] = RowValues[k]*(1.0+DIAGONAL_PERTURBATION_REL) + (RowValues[k] < 0.0? -1.0 : 1.0)*DIAGONAL_PERTURBATION_REL;
Kirill Terekhov's avatar
Kirill Terekhov committed
183
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
184 185 186 187 188 189 190 191
            //Eliminate lower part
            //elim_lpart
            j = RowIndeces[static_cast<INMOST_DATA_INTEGER_TYPE>(vbeg) - 1];
            while (j < k) //until diagonal entry
            {
                assert(lui[iu[j]] == j);
                RowValues[j] *= luv[iu[j]]; //scale by diagonal
                leabs = fabs(RowValues[j]);
Kirill Terekhov's avatar
Kirill Terekhov committed
192
#if defined(LFILL)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
193
                if (leabs > tau2*tau2)// introduce a non-zero, if threshold permits
Kirill Terekhov's avatar
Kirill Terekhov committed
194
#else
Dmitry Bagaev's avatar
Dmitry Bagaev committed
195
                if (leabs > tau2)// introduce a non-zero, if threshold permits
Kirill Terekhov's avatar
Kirill Terekhov committed
196
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
197 198 199 200 201 202 203
                {
                    curr = j;
                    for (r = iu[j] + 1; r < ilu[j + 1]; r++) {
                        ind = lui[r];
                        if (RowIndeces[ind] != UNDEF) //update without pondering on thresholds
                        {
                            RowValues[ind] -= RowValues[j] * luv[r];
Kirill Terekhov's avatar
Kirill Terekhov committed
204
#if defined(LFILL)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
205
                            RowFill[ind] = std::min(lfill[r]+1,RowFill[ind]);
Kirill Terekhov's avatar
Kirill Terekhov committed
206
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
207 208 209 210 211 212 213 214 215 216 217 218 219
                        } else {
                            flin = -RowValues[j] * luv[r];
                            //insert new value
                            foll = curr;
                            while (foll < ind) {
                                curr = foll;
                                foll = RowIndeces[curr];
                            }
                            assert(curr < ind);
                            assert(ind < foll);
                            RowIndeces[curr] = ind;
                            RowIndeces[ind] = foll;
                            RowValues[ind] = flin;
Kirill Terekhov's avatar
Kirill Terekhov committed
220
#if defined(LFILL)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
221
                            RowFill[ind] = lfill[r] + 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
222
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
                        }
                        curr = ind;
                    }

                    if (leabs > tau) {
                        curr = j;
                        for (r = ir[j]; r < ir[j + 1]; r++) {
                            //ind = U2j.GetIndex(r);
                            ind = ri[r];
                            if (RowIndeces[ind] != UNDEF) //update without pondering on thresholds
                                RowValues[ind] -= RowValues[j] * rv[r];
                            else // introduce a non-zero if threshold permits
                            {
                                flin = -RowValues[j] * rv[r];
                                //insert new value
                                foll = curr;
                                while (foll < ind) {
                                    curr = foll;
                                    foll = RowIndeces[curr];
                                }
                                assert(curr < ind);
                                assert(ind < foll);
                                RowIndeces[curr] = ind;
                                RowIndeces[ind] = foll;
                                RowValues[ind] = flin;
Kirill Terekhov's avatar
Kirill Terekhov committed
248
#if defined(LFILL)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
249
                                RowFill[ind] = ENUMUNDEF;
Kirill Terekhov's avatar
Kirill Terekhov committed
250
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
                            }
                            curr = ind;
                        }
                    }
                }
                j = RowIndeces[j];
            }
            // Compress row
            //row_compr
            j = RowIndeces[static_cast<INMOST_DATA_INTEGER_TYPE>(vbeg) - 1];
            //find minimum value in row
            ldiag = 0;
            while (j != EOL) {
                INMOST_DATA_REAL_TYPE temp = fabs(RowValues[j]);
                ldiag = std::max(ldiag, temp);
                j = RowIndeces[j];
            }
            if (ldiag < tau2) {
                ldiag = 1.0 / tau2;
                //std::cout << "ldiag too small " << ldiag << std::endl;
            } else
                ldiag = 1.0 / ldiag;

            //if (ldiag > 1000) std::cout << "ldiag is big " << k << " " << ldiag << std::endl;
            //divide all entries on right from the diagonal
            j = k;
            while (j != EOL) {
                RowValues[j] *= ldiag;
                j = RowIndeces[j];
            }
            j = RowIndeces[static_cast<INMOST_DATA_INTEGER_TYPE>(vbeg) - 1];
            while (j < k) {
                mva = fabs(RowValues[j]);
                if (mva > tau2 * tau2) {
                    if (mva > tau
Kirill Terekhov's avatar
Kirill Terekhov committed
286
#if defined(LFILL)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
287
                        || RowFill[j] <= Lfill
Kirill Terekhov's avatar
Kirill Terekhov committed
288
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
289 290 291 292
                            ) {
                        //L[k][j] = RowValues[j];
                        lui.push_back(j); //lui indicates column index of L matrix
                        luv.push_back(RowValues[j]); //luv indicates corresponding value
Kirill Terekhov's avatar
Kirill Terekhov committed
293
#if defined(LFILL)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
294
                        lfill.push_back(RowFill[j]);
Kirill Terekhov's avatar
Kirill Terekhov committed
295
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
296 297 298 299 300 301 302 303 304 305
                        //nzl++;
                    }
                }
                jn = RowIndeces[j];
                RowIndeces[j] = UNDEF;
                j = jn;
            }
            //add last diagonal entry to L matrix
            lui.push_back(k);
            luv.push_back(ldiag);
Kirill Terekhov's avatar
Kirill Terekhov committed
306
#if defined(LFILL)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
307
            lfill.push_back(0);
Kirill Terekhov's avatar
Kirill Terekhov committed
308
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
            //nzl++;

            iu[k] = static_cast<INMOST_DATA_ENUM_TYPE>(luv.size()); //iu points to the first entry of current line of U matrix
            // END of L-part
            if (fabs(RowValues[j]) > tol_modif)
                udiag = 1.0 / RowValues[j];
            else {
                //std::cout << "udiag too small " << RowValues[j] << std::endl;
                udiag = (RowValues[j] < 0.0 ? -1.0 : 1.0) / tol_modif;
            }

            //if (fabs(udiag) > 1000) std::cout << "udiag is big " << k << " " << udiag << std::endl;

            jn = RowIndeces[j];
            RowIndeces[j] = UNDEF;
            j = jn;
            //start of U matrix entries
            //add diagonal value for U matrix
            lui.push_back(k);
            luv.push_back(udiag);
Kirill Terekhov's avatar
Kirill Terekhov committed
329
#if defined(LFILL)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
330
            lfill.push_back(RowFill[k]);
Kirill Terekhov's avatar
Kirill Terekhov committed
331
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
332 333 334 335 336
            //nzu++;
            while (j != EOL) {
                mva = fabs(RowValues[j]);
                if (mva > tau2 * tau2) {
                    if (mva > tau
Kirill Terekhov's avatar
Kirill Terekhov committed
337
#if defined(LFILL)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
338
                        || RowFill[j] <= Lfill
Kirill Terekhov's avatar
Kirill Terekhov committed
339
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
340 341 342 343
                            ) {
                        //add values to U matrix
                        lui.push_back(j);
                        luv.push_back(RowValues[j]);
Kirill Terekhov's avatar
Kirill Terekhov committed
344
#if defined(LFILL)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
345
                        lfill.push_back(RowFill[j]);
Kirill Terekhov's avatar
Kirill Terekhov committed
346
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
                        //nzu++;
                    } else if (mva > tau2) {
                        //add values to U2 matrix
                        ri.push_back(j);
                        rv.push_back(RowValues[j]);
                        //nzu2++;
                    }
                }
                jn = RowIndeces[j];
                RowIndeces[j] = UNDEF;
                j = jn;
            }
            ilu[k + 1] = static_cast<INMOST_DATA_ENUM_TYPE>(luv.size()); //next first entry for L

            ir[k + 1] = static_cast<INMOST_DATA_ENUM_TYPE>(rv.size()); //next first entry for U2
            //END U-part
        }
        //printf("\n");
        //std::cout << "iluoo_solve: " << Timer() - timer << std::endl;
        //timer = Timer();
        //Rescale LU
        //xxlusc
        for (k = mobeg; k < moend; k++) {
            for (r = iu[k] - 1; r > ilu[k]; r--) {
                luv[r - 1] /= DL[k];
                //LFNORM += luv[r-1]*luv[r-1];
            }
            luv[iu[k] - 1] *= DL[k]; // L diagonal entry
            //LFNORM += luv[iu[k]-1]*luv[iu[k]-1];
        }
        for (k = mobeg; k < moend; k++) {
            for (r = iu[k] + 1; r < ilu[k + 1]; r++) {
                luv[r] /= DR[lui[r]];
                //UFNORM += luv[r]*luv[r];
            }
            luv[iu[k]] *= DR[k]; // U diagonal entry
            //UFNORM += luv[iu[k]]*luv[iu[k]];
        }
        //std::cout << "xxlusc: " << Timer() - timer << " LFNORM " << sqrt(LFNORM) << " UFNORM " << sqrt(UFNORM) << std::endl;
        //timer = Timer();


        //Rescale matrix back
        //matisc
        for (k = mobeg; k < moend; k++)
            for (Sparse::Row::iterator rit = (*Alink)[k].Begin(); rit != (*Alink)[k].End(); ++rit)
                rit->second = rit->second / DL[k] / DR[rit->first];
        //std::cout << "matisc: " << Timer() - timer << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
395 396

#if defined(REPORT_ILU)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
        INMOST_DATA_ENUM_TYPE nzu,nzl, nza;
        nzu = 0;
        nzl = 0;
        nza = 0;
        for(INMOST_DATA_ENUM_TYPE k = mobeg; k < moend; k++)
        {
            nzl += iu[k] - ilu[k];
            nzu += ilu[k+1] - iu[k] - 1;
            nza += (*Alink)[k].Size();
        }
        std::cout << "      nonzeros in A = " << nza << std::endl;
        std::cout << "      nonzeros in L = " << nzl - (moend-mobeg) << std::endl;
        std::cout << "      nonzeros in U = " << nzu << std::endl;
        std::cout << "     nonzeros in LU = " << ilu[moend] - 1 << std::endl;
        std::cout << "     nonzeros in U2 = " << ir[moend] - 1 << std::endl;
        //std::cout << __FUNCTION__ << " done" << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
413 414
#endif

Dmitry Bagaev's avatar
Dmitry Bagaev committed
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
        /*
        info.PrepareVector(div);
        std::fill(div.Begin(),div.End(),0);
        for(k = mobeg; k < moend; k++) div[k] = 1.0;
        info.Accumulate(div);
        for(k = mobeg; k < moend; k++) div[k] = 1.0/div[k];
        */
        init = true;
        return true;
    }

    bool isInitialized() { return init; }

    bool Finalize() {
        if (!isFinalized()) {
            luv.clear();
            lui.clear();
            init = false;
        }
        return true;
    }

    bool isFinalized() { return !init; }

    ~ILU2_preconditioner() {
        if (!isFinalized()) Finalize();
    }

    void Copy(const Method *other) {
        const ILU2_preconditioner *b = dynamic_cast<const ILU2_preconditioner *>(other);
        assert(b != NULL);
        info = b->info;
        Alink = b->Alink;
        DL = b->DL;
        DR = b->DR;
        nnz = b->nnz;
        luv = b->luv;
        lui = b->lui;
        iu = b->iu;
        ilu = b->ilu;
    }

    ILU2_preconditioner(const ILU2_preconditioner &other)
            : Method(other) {
        Copy(&other);
    }

    ILU2_preconditioner &operator=(ILU2_preconditioner const &other) {
        Copy(&other);
        return *this;
    }

    bool Solve(Sparse::Vector &input, Sparse::Vector &output) {
        assert(isInitialized());
Kirill Terekhov's avatar
Kirill Terekhov committed
469 470 471
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
        {
            INMOST_DATA_ENUM_TYPE mobeg, moend, r, k, vbeg, vend; //, end;
            info->GetOverlapRegion(info->GetRank(), mobeg, moend);
            info->GetVectorRegion(vbeg, vend);
            for (k = vbeg; k < mobeg; k++) output[k] = 0; //Restrict additive schwartz (maybe do it outside?)
            for (k = mobeg; k < moend; k++) output[k] = input[k];
            for (k = moend; k < vend; k++) output[k] = 0; //Restrict additive schwartz (maybe do it outside?)
            for (k = mobeg; k < moend; k++) //iterate over L part
            {
                for (r = iu[k] - 1; r > ilu[k]; r--)
                    output[k] -= luv[r - 1] * output[lui[r - 1]];
                output[k] *= luv[iu[k] - 1];
            }
            for (k = moend; k > mobeg; k--) //iterate over U part
            {
                for (r = iu[k - 1] + 1; r < ilu[k]; r++)
                    output[k - 1] -= luv[r] * output[lui[r]];
                output[k - 1] *= luv[iu[k - 1]];
            }
        }
        //May assemble partition of unity instead of restriction before accumulation
        //assembly should be done instead of initialization
        info->Accumulate(output);
        return true;
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
497

Dmitry Bagaev's avatar
Dmitry Bagaev committed
498 499 500 501 502
    bool ReplaceMAT(Sparse::Matrix &A) {
        if (isInitialized()) Finalize();
        Alink = &A;
        return true;
    };
Kirill Terekhov's avatar
Kirill Terekhov committed
503

Dmitry Bagaev's avatar
Dmitry Bagaev committed
504 505 506 507 508 509 510 511 512 513 514 515
    bool ReplaceSOL(Sparse::Vector &x) {
        (void) x;
        return true;
    }

    bool ReplaceRHS(Sparse::Vector &b) {
        (void) b;
        return true;
    }

    Method *Duplicate() { return new ILU2_preconditioner(*this); }
};
Kirill Terekhov's avatar
Kirill Terekhov committed
516 517 518


#endif //__SOLVER_ILU2__