solver_bcgsl.hpp 64.3 KB
Newer Older
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1

Kirill Terekhov's avatar
Kirill Terekhov committed
2 3 4
#ifndef __SOLVER_BCGS__
#define __SOLVER_BCGS__

Kirill Terekhov's avatar
Kirill Terekhov committed
5 6 7 8
//\todo
// 1. comply solvers with Method prototype, after TODO in solver_prototypes.hpp is done
// 2. Implement tricks from Read/solver/bcgsl/download.pdf with convex update and true residual correction
// 3. Detect numerical accuracy breakdown - when preconditioned residual is too far from true residual (probably 2 will fix).
Kirill Terekhov's avatar
Kirill Terekhov committed
9 10 11

#include "inmost_solver.h"

Kirill Terekhov's avatar
Kirill Terekhov committed
12
#define PERTURBATE_RTILDE
Kirill Terekhov's avatar
Kirill Terekhov committed
13 14
//#define CONVEX_COMBINATION
#define PSEUDOINVERSE  // same trick as in petsc with pseudoinverse
Kirill Terekhov's avatar
Kirill Terekhov committed
15 16
//#define USE_LAPACK_SVD // use lapack's dgesvd routine instead of built-in svdnxn

Kirill Terekhov's avatar
Kirill Terekhov committed
17
//#if !defined(NDEBUG)
Kirill Terekhov's avatar
Kirill Terekhov committed
18
#define REPORT_RESIDUAL
Kirill Terekhov's avatar
Kirill Terekhov committed
19
//#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
20 21
//#define USE_OMP

Kirill Terekhov's avatar
Kirill Terekhov committed
22

Dmitry Bagaev's avatar
Dmitry Bagaev committed
23 24
namespace INMOST {
    //lapack svd
Kirill Terekhov's avatar
Kirill Terekhov committed
25 26
#if defined(PSEUDOINVERSE)
#if defined(USE_LAPACK_SVD)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
27 28 29 30
    extern "C"
    {
        void dgesvd_(char*,char*,int*,int*,double*,int*,double*,double*,int*,double*,int*,double*,int*,int*);
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
31
#else
Dmitry Bagaev's avatar
Dmitry Bagaev committed
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 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 248 249 250 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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

    //SVD adopted from http://www.public.iastate.edu/~dicook/JSS/paper/code/svd.c
    static INMOST_DATA_REAL_TYPE SIGNFUNC(INMOST_DATA_REAL_TYPE a, INMOST_DATA_REAL_TYPE b) {
        return b >= 0.0 ? fabs(a) : -fabs(a);
    }

    static INMOST_DATA_REAL_TYPE MAXFUNC(INMOST_DATA_REAL_TYPE x, INMOST_DATA_REAL_TYPE y) { return x > y ? x : y; }

    static INMOST_DATA_REAL_TYPE PYTHAG(INMOST_DATA_REAL_TYPE a, INMOST_DATA_REAL_TYPE b) {
        INMOST_DATA_REAL_TYPE at = fabs(a), bt = fabs(b), ct, result;
        if (at > bt) {
            ct = bt / at;
            result = sqrt(at) * sqrt(at + ct * bt);
        }
        else if (bt > 0.0) {
            ct = at / bt;
            result = sqrt(bt) * sqrt(bt + ct * at);
        }
        else result = 0.0;
        return (result);
    }

    static int
    svdnxn(INMOST_DATA_REAL_TYPE *pa, INMOST_DATA_REAL_TYPE *pu, INMOST_DATA_REAL_TYPE *pw, INMOST_DATA_REAL_TYPE *pv,
           const int n) {
        shell<INMOST_DATA_REAL_TYPE> a(pa, n * n);
        shell<INMOST_DATA_REAL_TYPE> u(pu, n * n);
        shell<INMOST_DATA_REAL_TYPE> v(pv, n * n);
        shell<INMOST_DATA_REAL_TYPE> w(pw, n);
        //std::copy(a.begin(),a.end(),u.begin());
        //memcpy(u,a,sizeof(INMOST_DATA_REAL_TYPE)*n*n);
        int flag, i, its, j, jj, k, l, nm;
        INMOST_DATA_REAL_TYPE c, f, h, s, x, y, z;
        INMOST_DATA_REAL_TYPE anorm = 0.0, g = 0.0, scale = 0.0;
        dynarray<INMOST_DATA_REAL_TYPE, 64> rv1;
        rv1.resize(n);
        // Householder reduction to bidiagonal form
        for (i = 0; i < n * n; ++i) u[i] = a[i];
        for (i = 0; i < n; i++) {
            // left-hand reduction
            l = i + 1;
            rv1[i] = scale * g;
            g = s = scale = 0.0;
            if (i < n) {
                for (k = i; k < n; k++)
                    scale += fabs(u[k * n + i]);
                if (scale) {
                    for (k = i; k < n; k++) {
                        u[k * n + i] = u[k * n + i] / scale;
                        s += (u[k * n + i] * u[k * n + i]);
                    }
                    f = u[i * n + i];
                    g = -SIGNFUNC(sqrt(s), f);
                    h = f * g - s;
                    u[i * n + i] = (f - g);
                    if (i != n - 1) {
                        for (j = l; j < n; j++) {
                            for (s = 0.0, k = i; k < n; k++)
                                s += (u[k * n + i] * u[k * n + j]);
                            f = s / h;
                            for (k = i; k < n; k++)
                                u[k * n + j] += (f * u[k * n + i]);
                        }
                    }
                    for (k = i; k < n; k++)
                        u[k * n + i] = (u[k * n + i] * scale);
                }
            }
            w[i] = (scale * g);

            // right-hand reduction
            g = s = scale = 0.0;
            if (i < n && i != n - 1) {
                for (k = l; k < n; k++)
                    scale += fabs(u[i * n + k]);
                if (scale) {
                    for (k = l; k < n; k++) {
                        u[i * n + k] = (u[i * n + k] / scale);
                        s += (u[i * n + k] * u[i * n + k]);
                    }
                    f = u[i * n + l];
                    g = -SIGNFUNC(sqrt(s), f);
                    h = f * g - s;
                    u[i * n + l] = (f - g);
                    for (k = l; k < n; k++)
                        rv1[k] = u[i * n + k] / h;
                    if (i != n - 1) {
                        for (j = l; j < n; j++) {
                            for (s = 0.0, k = l; k < n; k++)
                                s += (u[j * n + k] * u[i * n + k]);
                            for (k = l; k < n; k++)
                                u[j * n + k] += (s * rv1[k]);
                        }
                    }
                    for (k = l; k < n; k++)
                        u[i * n + k] = (u[i * n + k] * scale);
                }
            }
            anorm = MAXFUNC(anorm, (fabs(w[i]) + fabs(rv1[i])));
        }

        // accumulate the right-hand transformation
        for (i = n - 1; i >= 0; i--) {
            if (i < n - 1) {
                if (g) {
                    for (j = l; j < n; j++)
                        v[j * n + i] = ((u[i * n + j] / u[i * n + l]) / g);
                    // double division to avoid underflow
                    for (j = l; j < n; j++) {
                        for (s = 0.0, k = l; k < n; k++)
                            s += (u[i * n + k] * v[k * n + j]);
                        for (k = l; k < n; k++)
                            v[k * n + j] += (s * v[k * n + i]);
                    }
                }
                for (j = l; j < n; j++)
                    v[i * n + j] = v[j * n + i] = 0.0;
            }
            v[i * n + i] = 1.0;
            g = rv1[i];
            l = i;
        }

        // accumulate the left-hand transformation
        for (i = n - 1; i >= 0; i--) {
            l = i + 1;
            g = w[i];
            if (i < n - 1)
                for (j = l; j < n; j++)
                    u[i * n + j] = 0.0;
            if (g) {
                g = 1.0 / g;
                if (i != n - 1) {
                    for (j = l; j < n; j++) {
                        for (s = 0.0, k = l; k < n; k++)
                            s += (u[k * n + i] * u[k * n + j]);
                        f = (s / u[i * n + i]) * g;
                        for (k = i; k < n; k++)
                            u[k * n + j] += (f * u[k * n + i]);
                    }
                }
                for (j = i; j < n; j++)
                    u[j * n + i] = (u[j * n + i] * g);
            } else {
                for (j = i; j < n; j++)
                    u[j * n + i] = 0.0;
            }
            ++u[i * n + i];
        }

        // diagonalize the bidiagonal form
        for (k = n - 1; k >= 0; k--) {
            // loop over singular values
            for (its = 0; its < 30; its++) {
                // loop over allowed iterations
                flag = 1;
                for (l = k; l >= 0; l--) {
                    // test for splitting
                    nm = l - 1;
                    if (fabs(rv1[l]) + anorm == anorm) {
                        flag = 0;
                        break;
                    }
                    if (fabs(w[nm]) + anorm == anorm)
                        break;
                }
                if (flag) {
                    c = 0.0;
                    s = 1.0;
                    for (i = l; i <= k; i++) {
                        f = s * rv1[i];
                        if (fabs(f) + anorm != anorm) {
                            g = w[i];
                            h = PYTHAG(f, g);
                            w[i] = h;
                            h = 1.0 / h;
                            c = g * h;
                            s = (-f * h);
                            for (j = nm < 0 ? 1 : 0; j < n; j++) {
                                y = u[j * n + nm];
                                z = u[j * n + i];
                                u[j * n + nm] = (y * c + z * s);
                                u[j * n + i] = (z * c - y * s);
                            }
                        }
                    }
                }
                z = w[k];
                if (l == k) {
                    // convergence
                    if (z < 0.0) {
                        // make singular value nonnegative
                        w[k] = (-z);
                        for (j = 0; j < n; j++)
                            v[j * n + k] = (-v[j * n + k]);
                    }
                    break;
                }
                if (its >= 30) {
                    fprintf(stderr, "No convergence after 30,000! iterations \n");
                    return 1;
                }

                // shift from bottom 2 x 2 minor
                x = w[l];
                nm = k - 1;
                y = w[nm];
                g = rv1[nm];
                h = rv1[k];
                f = ((y - z) * (y + z) + (g - h) * (g + h)) / (2.0 * h * y);
                g = PYTHAG(f, 1.0);
                f = ((x - z) * (x + z) + h * ((y / (f + SIGNFUNC(g, f))) - h)) / x;

                // next QR transformation
                c = s = 1.0;
                for (j = l; j <= nm; j++) {
                    i = j + 1;
                    g = rv1[i];
                    y = w[i];
                    h = s * g;
                    g = c * g;
                    z = PYTHAG(f, h);
                    rv1[j] = z;
                    c = f / z;
                    s = h / z;
                    f = x * c + g * s;
                    g = g * c - x * s;
                    h = y * s;
                    y = y * c;
                    for (jj = 0; jj < n; jj++) {
                        x = v[jj * n + j];
                        z = v[jj * n + i];
                        v[jj * n + j] = (x * c + z * s);
                        v[jj * n + i] = (z * c - x * s);
                    }
                    z = PYTHAG(f, h);
                    w[j] = z;
                    if (z) {
                        z = 1.0 / z;
                        c = f * z;
                        s = h * z;
                    }
                    f = (c * g) + (s * y);
                    x = (c * y) - (s * g);
                    for (jj = 0; jj < n; jj++) {
                        y = u[jj * n + j];
                        z = u[jj * n + i];
                        u[jj * n + j] = (y * c + z * s);
                        u[jj * n + i] = (z * c - y * s);
                    }
                }
                rv1[l] = 0.0;
                rv1[k] = f;
                w[k] = x;
            }
        }

        for (int i = 0; i < n; i++) {
            INMOST_DATA_REAL_TYPE temp;
            for (int j = i + 1; j < n; j++) {
                temp = u[i + n * j];
                u[i + n * j] = u[j + n * i];
                u[j + n * i] = temp;
            }
        }
        for (i = 0; i < n; i++) {
            k = i;
            for (j = i + 1; j < n; ++j)
                if (w[k] < w[j]) k = j;
            INMOST_DATA_REAL_TYPE temp;
            if (w[k] > w[i]) {
                temp = w[k];
                w[k] = w[i];
                w[i] = temp;
                for (int j = 0; j < n; ++j) {
                    temp = u[k * n + j];
                    u[k * n + j] = u[i * n + j];
                    u[i * n + j] = temp;
                    temp = v[j * n + k];
                    v[j * n + k] = v[j * n + i];
                    v[j * n + i] = temp;
                }
            }
        }
        return 0;
    }

Kirill Terekhov's avatar
Kirill Terekhov committed
319
#endif //USE_LAPACK_SVD
Kirill Terekhov's avatar
Kirill Terekhov committed
320
#else //PSEUDOINVERSE
Dmitry Bagaev's avatar
Dmitry Bagaev committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 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 395 396 397 398 399 400 401 402 403 404
    static int solvenxn(INMOST_DATA_REAL_TYPE * A, INMOST_DATA_REAL_TYPE * x, INMOST_DATA_REAL_TYPE * b, int n, int * order)
    {
        INMOST_DATA_REAL_TYPE temp, max;
        int temp2;
        for(int i = 0; i < n; i++) order[i] = i;
        for(int i = 0; i < n; i++)
        {
            int maxk = i, maxq = i;
            max = fabs(A[maxk*n+maxq]);
            //Find best pivot
            for(int q = i; q < n; q++) // over columns
            {
                for(int k = i; k < n; k++) // over rows
                {
                    if( fabs(A[k*n+q]) > max )
                    {
                        max = fabs(A[k*n+q]);
                        maxk = k;
                        maxq = q;
                    }
                }
            }
            //Exchange rows
            if( maxk != i )
            {
                for(int q = 0; q < n; q++)
                {
                    temp = A[maxk*n+q];
                    A[maxk*n+q] = A[i*n+q];
                    A[i*n+q] = temp;
                }
                //exchange rhs
                {
                    temp = b[maxk];
                    b[maxk] = b[i];
                    b[i] = temp;
                }
            }
            //Exchange columns
            if( maxq != i )
            {
                for(int k = 0; k < n; k++)
                {
                    temp = A[k*n+maxq];
                    A[k*n+maxq] = A[k*n+i];
                    A[k*n+i] = temp;
                }
                //remember order in sol
                {
                    temp2 = order[maxq];
                    order[maxq] = order[i];
                    order[i] = temp2;
                }
            }
            if( fabs(b[i]/A[i*n+i]) > 1.0e+100 )
                return i+1;

            for(int k = i+1; k < n; k++)
            {
                A[i*n+k] /= A[i*n+i];
                A[k*n+i] /= A[i*n+i];
            }
            for(int k = i+1; k < n; k++)
            for(int q = i+1; q < n; q++)
            {
                A[k*n+q] -= A[k*n+i] * A[i*n+i] * A[i*n+q];
            }
            for(int j = i+1; j < n; j++) //iterate over columns of L
            {
                b[j] -= b[i] * A[j*n+i];
            }
            b[i] /= A[i*n+i];
        }

        for(int i = n-1; i >= 0; i--) //iterate over rows of U
            for(int j = i+1; j < n; j++)
            {
                b[i] -= b[j] * A[i*n+j];
            }
        for(int i = 0; i < n; i++)
            x[order[i]] = b[i];

        return 0;
    }
Kirill Terekhov's avatar
Kirill Terekhov committed
405
#endif //PSEUDOINVERSE
Dmitry Bagaev's avatar
Dmitry Bagaev committed
406 407 408 409 410 411 412 413 414 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 469 470 471 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 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570

    class BCGSL_solver : public IterativeMethod {
        INMOST_DATA_REAL_TYPE length;
        INMOST_DATA_REAL_TYPE rtol, atol, divtol, last_resid;
        INMOST_DATA_ENUM_TYPE iters, maxits, l, last_it;
        INMOST_DATA_REAL_TYPE resid;
        INMOST_DATA_REAL_TYPE *tau, *sigma, *gamma, *theta1, *theta2, *theta3;
        Sparse::Vector r_tilde, x0, t, *u, *r;
        Sparse::Matrix *Alink;
        Method *prec;
        std::string reason;
        Solver::OrderInfo *info;
        bool init;
    public:
        INMOST_DATA_ENUM_TYPE GetIterations() { return last_it; }

        INMOST_DATA_REAL_TYPE GetResidual() { return last_resid; }

        INMOST_DATA_REAL_TYPE &RealParameter(std::string name) {
            if (name[0] == ':') {
                if (prec != NULL) return prec->RealParameter(name.substr(1, name.size() - 1));
            }
            if (name == "rtol") return rtol;
            else if (name == "atol") return atol;
            else if (name == "divtol") return divtol;
            else if (prec != NULL) return prec->RealParameter(name);
            throw -1;
        }

        INMOST_DATA_ENUM_TYPE &EnumParameter(std::string name) {
            if (name[0] == ':') {
                if (prec != NULL) return prec->EnumParameter(name.substr(1, name.size() - 1));
            }
            if (name == "maxits") return maxits;
            else if (name == "levels") {
                if (init) throw -1; //solver was already initialized, value should not be changed
                return l;
            } else if (prec != NULL) return prec->EnumParameter(name);
            throw -1;
        }

        BCGSL_solver(Method *prec, Solver::OrderInfo &info)
                : rtol(1e-8), atol(1e-9), divtol(1e+40), maxits(1500), l(2), prec(prec), info(&info) {
            Alink = NULL;
            init = false;
        }

        bool Initialize() {
            if (isInitialized()) Finalize();
            if (prec != NULL && !prec->isInitialized()) prec->Initialize();
            info->PrepareVector(r_tilde);
            info->PrepareVector(x0);
            info->PrepareVector(t);
            tau = new INMOST_DATA_REAL_TYPE[l * 3 + (l + 1) * (l + 1) + (l + 1) * 2];
            sigma = tau + (l + 1) * (l + 1);
            gamma = sigma + l + 1;
            theta1 = gamma + l + 1;
            theta2 = theta1 + l;
            theta3 = theta2 + l;
            u = new Sparse::Vector[l * 2 + 2];
            r = u + l + 1;
            for (INMOST_DATA_ENUM_TYPE k = 0; k < l + 1; k++) {
                info->PrepareVector(r[k]);
                info->PrepareVector(u[k]);
            }
            init = true;
            return true;
        }

        bool isInitialized() { return init && (prec == NULL || prec->isInitialized()); }

        bool Finalize() {
            if (isInitialized()) {
                if (!prec->isFinalized()) prec->Finalize();
                delete[] u;
                delete[] tau;
                init = false;
            }
            return true;
        }

        bool isFinalized() { return !init && (prec == NULL || prec->isFinalized()); }

        void Copy(const Method *other) {
            const BCGSL_solver *b = dynamic_cast<const BCGSL_solver *>(other);
            assert(b != NULL);
            rtol = b->rtol;
            atol = b->atol;
            divtol = b->divtol;
            last_resid = b->last_resid;
            iters = b->iters;
            maxits = b->maxits;
            l = b->l;
            last_it = b->last_it;
            resid = b->resid;
            Alink = b->Alink;
            info = b->info;
            if (init) Finalize();
            if (b->prec != NULL) {
                if (prec == NULL) prec = b->prec->Duplicate();
                else prec->Copy(b->prec);
            }
            if (b->init) Initialize();
        }

        BCGSL_solver(const BCGSL_solver &other) : IterativeMethod(other) {
            Copy(&other);
        }

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

        ~BCGSL_solver() {
            if (!isFinalized()) Finalize();
            if (prec != NULL) delete prec;
        }

        void ApplyOperator(Sparse::Vector &Input, Sparse::Vector &Output) {
            if (prec != NULL) //right preconditioning here! for left preconditioner have to reverse order
            {
                prec->Solve(Input, t);
                info->Update(t);
                Alink->MatVec(1.0, t, 0, Output);
                info->Update(Output);
            } else {
                Alink->MatVec(1.0, Input, 0, Output);
                info->Update(Output);
            }
        }

        bool Solve(Sparse::Vector &RHS, Sparse::Vector &SOL) {
            assert(isInitialized());
            INMOST_DATA_ENUM_TYPE vbeg, vend, vlocbeg, vlocend;
            INMOST_DATA_INTEGER_TYPE ivbeg, ivend, ivlocbeg, ivlocend;
            INMOST_DATA_REAL_TYPE rho0 = 1, rho1 = 1, alpha = 0, beta = 0, omega = 1, eta = 0;
            INMOST_DATA_REAL_TYPE resid0, resid, rhs_norm, tau_sum = 0, sigma_sum = 0, r_tilde_norm = 0;//, temp[2];
            iters = 0;
            info->PrepareVector(SOL);
            info->PrepareVector(RHS);
            info->Update(SOL);
            info->Update(RHS);
            if (prec != NULL) prec->ReplaceSOL(SOL);
            if (prec != NULL) prec->ReplaceRHS(RHS);
            info->GetLocalRegion(info->GetRank(), vlocbeg, vlocend);
            info->GetVectorRegion(vbeg, vend);
            ivbeg = vbeg;
            ivend = vend;
            ivlocbeg = vlocbeg;
            ivlocend = vlocend;
            //info->ScalarProd(RHS,RHS,vlocbeg,vlocend,rhs_norm);
            rhs_norm = 1;
            //r[0] = b
            std::copy(RHS.Begin(), RHS.End(), r[0].Begin());
            {
                // r[0] = r[0] - A x
                Alink->MatVec(-1, SOL, 1, r[0]); //global multiplication, r probably needs an update
                info->Update(r[0]); // r is good
                std::copy(SOL.Begin(), SOL.End(), x0.Begin()); //x0 = x
                std::fill(SOL.Begin(), SOL.End(), 0.0); //x = 0
            }
            std::copy(r[0].Begin(), r[0].End(), r_tilde.Begin()); // r_tilde = r[0]
            std::fill(u[0].Begin(), u[0].End(), 0); // u[0] = 0
            resid = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
571 572 573
#if defined(USE_OMP)
#pragma omp parallel for reduction(+:resid)
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
574 575 576 577 578
            for (INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k)
                resid += r[0][k] * r[0][k];
            info->Integrate(&resid, 1);
            //info->ScalarProd(r[0],r[0],vlocbeg,vlocend,resid); //resid = dot(r[0],r[0])
            last_resid = resid = resid0 = sqrt(resid / rhs_norm); //resid = sqrt(dot(r[0],r[0])
Kirill Terekhov's avatar
Kirill Terekhov committed
579 580 581
#if defined(USE_OMP)
#pragma omp parallel for
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
582 583 584
            for (INMOST_DATA_INTEGER_TYPE k = ivbeg; k < ivend; ++k) // r_tilde = r[0] / dot(r[0],r[0])
                r_tilde[k] /= resid;
            last_it = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
585
#if defined(REPORT_RESIDUAL)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
586 587 588 589 590 591 592 593 594 595 596 597 598 599
            if (info->GetRank() == 0) {
                //std::cout << "iter " << last_it << " residual " << resid << std::endl;
                //std::cout << "iter " << last_it << " resid " << resid << "\r";
                //printf("iter %3d resid %12g | %12g relative %12g | %12g\r", last_it, resid, atol, resid / resid0, rtol);
                printf("iter %3d resid %12g | %g\r", last_it, resid, atol);
                fflush(stdout);
            }
#endif

            bool halt = false;
            if (last_resid < atol || last_resid < rtol * resid0) {
                reason = "initial solution satisfy tolerances";
                halt = true;
            }
600

Kirill Terekhov's avatar
Kirill Terekhov committed
601 602 603
#if defined(USE_OMP)
#pragma omp parallel
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
604 605 606 607
            {
                INMOST_DATA_ENUM_TYPE i = 0;
                while (!halt) {

Kirill Terekhov's avatar
Kirill Terekhov committed
608 609 610
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
611 612 613 614 615 616 617 618
                    {
                        rho0 = -omega * rho0;
                    }


                    for (INMOST_DATA_ENUM_TYPE j = 0; j < l; j++) {
                        if (false) {
                            perturbate:
Kirill Terekhov's avatar
Kirill Terekhov committed
619
#if defined(PERTURBATE_RTILDE)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
620 621
                            //std::cout << "Rescue solver by perturbing orthogonal direction!" << std::endl;
                            //Compute length of the vector
Kirill Terekhov's avatar
Kirill Terekhov committed
622 623 624
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
625 626 627 628 629 630
                            {
                                length = static_cast<INMOST_DATA_REAL_TYPE>(ivlocend - ivlocbeg);
                                r_tilde_norm = 0.0;
                            }
                            info->Integrate(&length, 1);
                            //perform perturbation (note that rand() with openmp may give identical sequences of random values
Kirill Terekhov's avatar
Kirill Terekhov committed
631 632 633
#if defined(USE_OMP)
#pragma omp for
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
634 635 636 637 638 639
                            for (INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k) {
                                INMOST_DATA_REAL_TYPE unit = 2 * static_cast<INMOST_DATA_REAL_TYPE>(rand()) /
                                                             static_cast<INMOST_DATA_REAL_TYPE>(RAND_MAX) - 1.0;
                                r_tilde[k] += unit / length;
                            }
                            //compute norm for orthogonal vector
Kirill Terekhov's avatar
Kirill Terekhov committed
640 641 642
#if defined(USE_OMP)
#pragma omp for reduction(+:r_tilde_norm)
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
643 644
                            for (INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k)
                                r_tilde_norm += r_tilde[k] * r_tilde[k];
Kirill Terekhov's avatar
Kirill Terekhov committed
645 646 647
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
648 649 650 651 652
                            {
                                r_tilde_norm = sqrt(r_tilde_norm);
                            }
                            info->Integrate(&r_tilde_norm, 1);
                            //normalize orthogonal vector to unity
Kirill Terekhov's avatar
Kirill Terekhov committed
653 654 655
#if defined(USE_OMP)
#pragma omp for
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
656 657 658
                            for (INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k)
                                r_tilde[k] /= r_tilde_norm;
                            //Recompute rho1
Kirill Terekhov's avatar
Kirill Terekhov committed
659
#else
Dmitry Bagaev's avatar
Dmitry Bagaev committed
660 661 662
                            reason = "r_tilde perturbation algorithm is disabled";
                            halt = true;
                            break;
Kirill Terekhov's avatar
Kirill Terekhov committed
663
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
664
                        }
Kirill Terekhov's avatar
Kirill Terekhov committed
665 666 667
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
668
                        rho1 = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
669 670 671
#if defined(USE_OMP)
#pragma omp for reduction(+:rho1)
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
672 673 674 675 676 677 678 679
                        for (INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k)
                            rho1 += r[j][k] * r_tilde[k];
                        info->Integrate(&rho1, 1);
                        if (fabs(rho1) < 1.0e-50) {
                            //std::cout << "Asking to perturbate r_tilde since rho1 is too small " << rho1 << std::endl;
                            goto perturbate;
                        }
                        //info->ScalarProd(r[j],r_tilde,vlocbeg,vlocend,rho1); // rho1 = dot(r[j],r_tilde)
Kirill Terekhov's avatar
Kirill Terekhov committed
680 681 682
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
683 684 685
                        {
                            beta = alpha * (rho1 / rho0);
                        }
Kirill Terekhov's avatar
Kirill Terekhov committed
686

Dmitry Bagaev's avatar
Dmitry Bagaev committed
687 688
                        if (fabs(beta) > 1.0e+100) {
                            //std::cout << "alpha " << alpha << " rho1 " << rho1 << " rho0 " << rho0 << " beta " << beta << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
689 690 691
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
692 693 694 695 696 697
                            {
                                reason = "multiplier(1) is too large";
                                halt = true;
                            }
                            break;
                        }
Kirill Terekhov's avatar
Kirill Terekhov committed
698

Dmitry Bagaev's avatar
Dmitry Bagaev committed
699 700
                        if (beta != beta) {
                            //std::cout << "alpha " << alpha << " rho1 " << rho1 << " rho0 " << rho0 << " beta " << beta << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
701 702 703
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
704 705 706 707 708 709
                            {
                                reason = "multiplier(1) is NaN";
                                halt = true;
                            }
                            break;
                        }
Kirill Terekhov's avatar
Kirill Terekhov committed
710 711 712
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
713 714 715 716
                        {
                            rho0 = rho1;
                        }
                        for (INMOST_DATA_ENUM_TYPE m = 0; m < j + 1; m++) {
Kirill Terekhov's avatar
Kirill Terekhov committed
717 718 719
#if defined(USE_OMP)
#pragma omp for
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
720 721 722
                            for (INMOST_DATA_INTEGER_TYPE k = ivbeg; k < ivend; ++k)
                                u[m][k] = r[m][k] - beta * u[m][k];
                        }
Kirill Terekhov's avatar
Kirill Terekhov committed
723

Kirill Terekhov's avatar
Kirill Terekhov committed
724

Dmitry Bagaev's avatar
Dmitry Bagaev committed
725
                        ApplyOperator(u[j], u[j + 1]); // u[j+1] = A*R*u[j]
Kirill Terekhov's avatar
Kirill Terekhov committed
726 727 728
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
729
                        eta = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
730 731 732
#if defined(USE_OMP)
#pragma omp for reduction(+:eta)
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
733 734 735 736
                        for (INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k)
                            eta += u[j + 1][k] * r_tilde[k];
                        info->Integrate(&eta, 1);
                        //info->ScalarProd(u[j+1],r_tilde,vlocbeg,vlocend,eta); //eta = dot(u[j+1],r_tilde)
Kirill Terekhov's avatar
Kirill Terekhov committed
737

Dmitry Bagaev's avatar
Dmitry Bagaev committed
738 739 740 741
                        if (fabs(eta) < 1.0e-50) {
                            //std::cout << "Asking to perturbate r_tilde since eta is too small " << eta << std::endl;
                            goto perturbate;
                        }
Kirill Terekhov's avatar
Kirill Terekhov committed
742

Kirill Terekhov's avatar
Kirill Terekhov committed
743 744 745
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
746
                        alpha = rho0 / eta;
Kirill Terekhov's avatar
Kirill Terekhov committed
747

Dmitry Bagaev's avatar
Dmitry Bagaev committed
748
                        if (fabs(alpha) > 1.0e+100) {
Kirill Terekhov's avatar
Kirill Terekhov committed
749 750 751
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
752 753 754 755 756 757 758
                            {
                                reason = "multiplier(2) is too large";
                                halt = true;
                            }
                            break;
                        }
                        if (alpha != alpha) {
Kirill Terekhov's avatar
Kirill Terekhov committed
759 760 761
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
762 763 764 765 766 767
                            {
                                reason = "multiplier(2) is NaN";
                                halt = true;
                            }
                            break;
                        }
Kirill Terekhov's avatar
Kirill Terekhov committed
768 769 770 771

#if defined(USE_OMP)
#pragma omp for
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
772 773
                        for (INMOST_DATA_INTEGER_TYPE k = ivbeg; k < ivend; ++k)
                            SOL[k] += alpha * u[0][k];
Kirill Terekhov's avatar
Kirill Terekhov committed
774

Dmitry Bagaev's avatar
Dmitry Bagaev committed
775
                        for (INMOST_DATA_ENUM_TYPE m = 0; m < j + 1; m++) {
Kirill Terekhov's avatar
Kirill Terekhov committed
776 777 778
#if defined(USE_OMP)
#pragma omp for
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
779 780 781 782
                            for (INMOST_DATA_INTEGER_TYPE k = ivbeg; k < ivend; ++k) //r[i] = r[i] - alpha * u[i+1]
                                r[m][k] -= alpha * u[m + 1][k];
                        }

Kirill Terekhov's avatar
Kirill Terekhov committed
783

Kirill Terekhov's avatar
Kirill Terekhov committed
784 785 786
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
787
                        resid = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
788 789 790
#if defined(USE_OMP)
#pragma omp for reduction(+:resid)
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
791 792 793 794
                        for (INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k)
                            resid += r[0][k] * r[0][k];
                        info->Integrate(&resid, 1);
                        //info->ScalarProd(r[0],r[0],vlocbeg,vlocend,resid); // resid = dot(r[j],r[j])
Kirill Terekhov's avatar
Kirill Terekhov committed
795 796 797
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
798 799 800 801 802 803
                        {
                            resid = sqrt(resid / rhs_norm); // resid = sqrt(dot(r[j],r[j]))
                            last_it++;
                        }

                        if (resid < atol || resid < rtol * resid0) {
Kirill Terekhov's avatar
Kirill Terekhov committed
804 805 806
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
807 808 809 810 811 812 813
                            reason = "early exit in bi-cg block";
                            last_resid = resid;
                            halt = true;
                            break;
                        }
                        ApplyOperator(r[j], r[j + 1]); // r[j+1] = A*R*r[j]
                    }
Kirill Terekhov's avatar
Kirill Terekhov committed
814

Dmitry Bagaev's avatar
Dmitry Bagaev committed
815 816
                    if (halt) break;
                    INMOST_DATA_ENUM_TYPE size = l;
Kirill Terekhov's avatar
Kirill Terekhov committed
817
#if defined(CONVEX_COMBINATION)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
818
                    size = l+1;
Kirill Terekhov's avatar
Kirill Terekhov committed
819
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
820 821 822
                    // Penalization for convex combination for update below
                    for (INMOST_DATA_ENUM_TYPE j = 1; j < l + 1; j++) {
                        for (INMOST_DATA_ENUM_TYPE m = 1; m < j + 1; m++) {
Kirill Terekhov's avatar
Kirill Terekhov committed
823 824 825
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
826
                            tau_sum = 0.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
827 828 829
#if defined(USE_OMP)
#pragma omp for reduction(+:tau_sum)
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
830 831
                            for (INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k)
                                tau_sum += r[j][k] * r[m][k];
Kirill Terekhov's avatar
Kirill Terekhov committed
832

Dmitry Bagaev's avatar
Dmitry Bagaev committed
833
                            if (fabs(tau_sum) > 1.0e+100) {
Kirill Terekhov's avatar
Kirill Terekhov committed
834 835 836
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
837 838 839 840 841 842
                                {
                                    reason = "multiplier(tau) is too large";
                                    halt = true;
                                }
                            }
                            if (tau_sum != tau_sum) {
Kirill Terekhov's avatar
Kirill Terekhov committed
843 844 845
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
846 847 848 849 850
                                {
                                    reason = "multiplier(tau) is NaN";
                                    halt = true;
                                }
                            }
Kirill Terekhov's avatar
Kirill Terekhov committed
851 852 853
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
854 855 856 857
                            {
                                tau[(j - 1) + (m - 1) * size] = tau[(m - 1) + (j - 1) * size] = tau_sum;
                            }
                        }
Kirill Terekhov's avatar
Kirill Terekhov committed
858 859 860
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
861
                        sigma_sum = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
862 863 864
#if defined(USE_OMP)
#pragma omp for reduction(+:sigma_sum)
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
865 866
                        for (INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k)
                            sigma_sum += r[0][k] * r[j][k];
Kirill Terekhov's avatar
Kirill Terekhov committed
867

Dmitry Bagaev's avatar
Dmitry Bagaev committed
868
                        if (fabs(sigma_sum) > 1.0e+100) {
Kirill Terekhov's avatar
Kirill Terekhov committed
869 870 871
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
872 873 874 875 876 877
                            {
                                reason = "multiplier(sigma) is too large";
                                halt = true;
                            }
                        }
                        if (sigma_sum != sigma_sum) {
Kirill Terekhov's avatar
Kirill Terekhov committed
878 879 880
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
881 882 883 884 885
                            {
                                reason = "multiplier(sigma) is NaN";
                                halt = true;
                            }
                        }
Kirill Terekhov's avatar
Kirill Terekhov committed
886 887 888
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
889 890 891 892
                        {
                            sigma[j - 1] = sigma_sum;
                        }
                    }
Kirill Terekhov's avatar
Kirill Terekhov committed
893

Dmitry Bagaev's avatar
Dmitry Bagaev committed
894
                    if (halt) break;
Kirill Terekhov's avatar
Kirill Terekhov committed
895

Kirill Terekhov's avatar
Kirill Terekhov committed
896
#if defined(CONVEX_COMBINATION)
Kirill Terekhov's avatar
Kirill Terekhov committed
897 898 899
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
                    {
                        INMOST_DATA_REAL_TYPE lagrangian = 0.0;
                        for(INMOST_DATA_ENUM_TYPE j = 0; j < l; j++) lagrangian += tau[j+size*j];
                        sigma[l] = lagrangian;
                        tau[(l+1)*(l+1)-1] = 0.0;
                        for(INMOST_DATA_ENUM_TYPE j = 0; j < l; j++)
                        {
                            tau[l + j*(l+1)] = -lagrangian;
                            tau[l*(l+1)+j] = lagrangian;
                        }
                    }
#endif
                    info->Integrate(tau, (l + 2) * (l + 1)); //sigma is updated with tau
                    //info->Integrate(tau,size*size);
                    //info->Integrate(sigma,size);
Kirill Terekhov's avatar
Kirill Terekhov committed
915

Kirill Terekhov's avatar
Kirill Terekhov committed
916 917 918
#if defined(USE_OMP)
#pragma omp single
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
919
#if defined(PSEUDOINVERSE)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
920 921
                    {
                        int dgesvd_info = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
922
#if defined(USE_LAPACK_SVD)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
923 924 925 926 927 928
                        char c = 'A';
                        INMOST_DATA_REAL_TYPE U[128*128], V[128*128], w[128];
                        INMOST_DATA_REAL_TYPE work[5*128];
                        int lwork = 5*128;
                        int n = static_cast<int>(size);
                        dgesvd_(&c,&c,&n,&n,tau,&n,w,U,&n,V,&n,work,&lwork,&dgesvd_info);
Kirill Terekhov's avatar
Kirill Terekhov committed
929
#else
Dmitry Bagaev's avatar
Dmitry Bagaev committed
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
                        /*
                        char c = 'A';
                        INMOST_DATA_REAL_TYPE U2[128*128], V2[128*128], w2[128], tau2[128*128];
                        INMOST_DATA_REAL_TYPE work[5*128];
                        int lwork = 5*128;
                        int n = l;
                        memcpy(tau2,tau,sizeof(INMOST_DATA_REAL_TYPE)*l*l);
                        dgesvd_(&c,&c,&n,&n,tau2,&n,w2,U2,&n,V2,&n,work,&lwork,&dgesvd_info);
                        printf("dgesvd\n");
                        printf("w\n");
                        for(int q = 0; q < l; ++q) printf("%g ",w2[q]);
                        printf("\nU\n");
                        for(int q = 0; q < l*l; ++q)
                        {
                            printf("%g ",U2[q]);
                            if( (q+1)%l == 0 ) printf("\n");
                        }
                        printf("V\n");
                        for(int q = 0; q < l*l; ++q)
                        {
                            printf("%g ",V2[q]);
                            if( (q+1)%l == 0 ) printf("\n");
                        }
                        */
                        INMOST_DATA_REAL_TYPE U[128 * 128], V[128 * 128], w[128];
                        dgesvd_info = svdnxn(tau, U, w, V, size);
                        //for(INMOST_DATA_ENUM_TYPE j = 0; j < l; j++) w[j] = S[j*l+j];
                        /*
                        printf("svdnxn\n");
                        printf("w\n");
                        for(int q = 0; q < l; ++q) printf("%g ",w[q]);
                        printf("\nU\n");
                        for(int q = 0; q < l*l; ++q)
                        {
                            printf("%g ",U[q]);
                            if( (q+1)%l == 0 ) printf("\n");
                        }
                        printf("V\n");
                        for(int q = 0; q < l*l; ++q)
                        {
                            printf("%g ",V[q]);
                            if( (q+1)%l == 0 ) printf("\n");
                        }
                        */
#endif
                        /*
                        printf("w ");
                        for(INMOST_DATA_ENUM_TYPE j = 0; j < l; j++) printf("%20g ",w[j]);
                        printf("\n");

                        printf("U\n");
                        for(INMOST_DATA_ENUM_TYPE j = 0; j < l*l; j++)
                        {
                            printf("%20g ",U[j]);
                            if( (j+1) % l == 0 ) printf("\n");
                        }
                        printf("\n");

                        printf("VT\n");
                        for(INMOST_DATA_ENUM_TYPE j = 0; j < l*l; j++)
                        {
                            printf("%20g ",V[j]);
                            if( (j+1) % l == 0 ) printf("\n");
                        }
                        printf("\n");
                        */
                        if (dgesvd_info != 0) {
                            printf("(%s:%d) dgesvd %d\n", __FILE__, __LINE__, dgesvd_info);
                            exit(-1);
                        }

                        INMOST_DATA_REAL_TYPE maxw = w[0], tol;
                        for (INMOST_DATA_ENUM_TYPE j = 1; j < size; j++) if (w[j] > maxw) maxw = w[j];
                        tol = size * maxw * 1.0e-14;
                        memset(gamma, 0, sizeof(INMOST_DATA_REAL_TYPE) * size);
                        for (INMOST_DATA_ENUM_TYPE j = 0; j < size; j++) {
                            if (w[j] > tol) {
                                INMOST_DATA_REAL_TYPE sum = 0;
                                for (INMOST_DATA_ENUM_TYPE k = 0; k < size; ++k)
                                    sum += sigma[k] * U[j * size + k];
                                for (INMOST_DATA_ENUM_TYPE k = 0; k < size; ++k)
                                    gamma[k] += sum / w[j] * V[k * size + j];
                            }
                        }
                    }

                    //svdnxn(tau,U,S,V,l);
                    //INMOST_DATA_REAL_TYPE inv_tau[64];
                    //pseudoinverse(tau,inv_tau,l);
                    //matmul(inv_tau,sigma,gamma,l,l,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
1020
#else
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
                    {
                        int order[128];
                        int row = solvenxn(tau,gamma,sigma,size,order);
                        /*
                        double sum = 0.0;
                        for(int j = 0; j < l; ++j)
                        {
                            sum += gamma[j];
                            std::cout << gamma[j] << " ";
                        }
                        std::cout << "sum: " << sum;
                        //std::cout << " lagrangian: " << gamma[l];
                        std::cout << std::endl;
                        */
                        if( row != 0 )
                        {
                            std::cout << "breakdown on row " << row << std::endl;
                            reason = "breakdown in matrix inversion in polynomial part";
                            break;
                        }
                    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1042 1043 1044 1045
#endif
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1046 1047
                    omega = gamma[l - 1];
                    if (fabs(omega) > 1.0e+100) {
Kirill Terekhov's avatar
Kirill Terekhov committed
1048 1049 1050
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1051 1052 1053 1054 1055 1056 1057
                        {
                            reason = "multiplier(3) is too large";
                            halt = true;
                        }
                        break;
                    }
                    if (omega != omega) {
Kirill Terekhov's avatar
Kirill Terekhov committed
1058 1059 1060
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1061 1062 1063 1064 1065 1066 1067
                        {
                            reason = "multiplier(3) is NaN";
                            halt = true;
                        }
                        break;
                    }
                    for (INMOST_DATA_ENUM_TYPE j = 1; j < l + 1; ++j) {
Kirill Terekhov's avatar
Kirill Terekhov committed
1068 1069 1070
#if defined(USE_OMP)
#pragma omp for
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
                        for (INMOST_DATA_INTEGER_TYPE k = ivbeg; k < ivend; ++k) {
                            u[0][k] -= gamma[j - 1] * u[j][k];
                            SOL[k] += gamma[j - 1] * r[j - 1][k];
                            r[0][k] -= gamma[j - 1] * r[j][k];
                        }
                    }


                    /*
                    for(INMOST_DATA_ENUM_TYPE j = 1; j < l+1; j++)
                    {
                        for(INMOST_DATA_ENUM_TYPE i = 1; i < j; i++)
                        {
                            tau[i-1 + (j-1)*l] = 0;
                            for(INMOST_DATA_ENUM_TYPE k = vlocbeg; k < vlocend; ++k)
                                tau[i-1 + (j-1)*l] += r[j][k]*r[i][k];
                            info->Integrate(&tau[i-1 + (j-1)*l],1);
                            tau[i-1 + (j-1)*l] /= sigma[i-1];
                            for(INMOST_DATA_ENUM_TYPE k = vbeg; k < vend; ++k)
                                r[j][k] -= tau[i-1 + (j-1)*l]*r[i][k];
                        }
                        INMOST_DATA_REAL_TYPE temp[2] = {0,0};
                        for(INMOST_DATA_ENUM_TYPE k = vlocbeg; k < vlocend; ++k)
                        {
                            temp[0] += r[j][k]*r[j][k];
                            temp[1] += r[0][k]*r[j][k];
                        }
                        info->Integrate(temp,2);
                        sigma[j-1] = temp[0];//+1.0e-35; //REVIEW
                        theta2[j-1] = temp[1]/sigma[j-1];
                    }
                    omega = theta1[l-1] = theta2[l-1];
                    for(INMOST_DATA_ENUM_TYPE j = l-1; j > 0; j--)
                    {
                        eta = 0;
                        for(INMOST_DATA_ENUM_TYPE i = j+1; i < l+1; i++)
                            eta += tau[j-1 + (i-1)*l] * theta1[i-1];
                        theta1[j-1] = theta2[j-1] - eta;
                    }
                    for(INMOST_DATA_ENUM_TYPE j = 1; j < l; j++)
                    {
                        eta = 0;
                        for(INMOST_DATA_ENUM_TYPE i = j+1; i < l; i++)
                            eta += tau[j-1 + (i-1)*l] * theta1[i];
                        theta3[j-1] = theta1[j] + eta;
                    }
                    for(INMOST_DATA_ENUM_TYPE k = vbeg; k < vend; ++k)
                    {
                        SOL[k] += theta1[0]*r[0][k];
                        r[0][k] -= theta2[l-1]*r[l][k];
                        u[0][k] -= theta1[l-1]*u[l][k];
                    }
                    for(INMOST_DATA_ENUM_TYPE j = 1; j < l; j++)
                    {
                        for(INMOST_DATA_ENUM_TYPE k = vbeg; k < vend; ++k)
                        {
                            u[0][k] -= theta1[j-1]*u[j][k];
                            SOL[k] += theta3[j-1]*r[j][k];
                            r[0][k] -= theta2[j-1]*r[j][k];
                        }
                    }
                    */
                    //last_it = l+1;
                    {
Kirill Terekhov's avatar
Kirill Terekhov committed
1135 1136 1137
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1138
                        resid = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1139 1140 1141
#if defined(USE_OMP)
#pragma omp for reduction(+:resid)
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1142 1143 1144 1145
                        for (INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k)
                            resid += r[0][k] * r[0][k];
                        info->Integrate(&resid, 1);
                        //info->ScalarProd(r[0],r[0],vlocbeg,vlocend,resid);
Kirill Terekhov's avatar
Kirill Terekhov committed
1146 1147 1148
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1149 1150 1151
                        resid = sqrt(resid / rhs_norm);
                    }

Kirill Terekhov's avatar
Kirill Terekhov committed
1152
#if defined(REPORT_RESIDUAL)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1153 1154 1155 1156
                    if (info->GetRank() == 0) {
                        //std::cout << "iter " << last_it << " residual " << resid << " time " << tt << " matvec " << ts*0.5/l << " precond " << tp*0.5/l << std::endl;
                        //std::cout << "iter " << last_it << " resid " << resid << "\r";
                        //printf("iter %3d resid %12g | %12g relative %12g | %12g\r", last_it, resid, atol, resid / resid0, rtol);
Kirill Terekhov's avatar
Kirill Terekhov committed
1157 1158 1159
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1160 1161 1162 1163 1164
                        {
                            printf("iter %3d resid %12g | %g\r", last_it, resid, atol);
                            fflush(stdout);
                        }
                    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1165 1166 1167
#endif
#if defined(USE_OMP)
#pragma omp single
Kirill Terekhov's avatar
Kirill Terekhov committed
1168
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1169 1170
                    last_resid = resid;
                    if (resid != resid) {
Kirill Terekhov's avatar
Kirill Terekhov committed
1171 1172 1173
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1174 1175 1176 1177
                        reason = "residual is NAN";
                        break;
                    }
                    if (resid < atol) {
Kirill Terekhov's avatar
Kirill Terekhov committed
1178 1179 1180
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1181 1182 1183 1184
                        reason = "converged due to absolute tolerance";
                        break;
                    }
                    if (resid < rtol * resid0) {
Kirill Terekhov's avatar
Kirill Terekhov committed
1185 1186 1187
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1188 1189 1190 1191
                        reason = "converged due to relative tolerance";
                        break;
                    }
                    if (resid > divtol) {
Kirill Terekhov's avatar
Kirill Terekhov committed
1192 1193 1194
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1195 1196 1197 1198
                        reason = "diverged due to divergence tolerance";
                        break;
                    }
                    if (i == maxits) {
Kirill Terekhov's avatar
Kirill Terekhov committed
1199 1200
#if defined(USE_OMP)
#pragma omp single
Kirill Terekhov's avatar
Kirill Terekhov committed
1201
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
                        reason = "reached maximum iteration number";
                        break;
                    }
                    i++;
                }
            }
            if (prec != NULL) {
                prec->Solve(SOL, r_tilde); //undo right preconditioner
                std::copy(r_tilde.Begin(), r_tilde.End(), SOL.Begin());
            }
Kirill Terekhov's avatar
Kirill Terekhov committed
1212 1213 1214
#if defined(USE_OMP)
#pragma omp parallel for
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
            for (INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k) //undo shift
                SOL[k] += x0[k];
            //info->RestoreMatrix(A);
            info->RestoreVector(SOL);
            info->RestoreVector(RHS);
            if (last_resid < atol || last_resid < rtol * resid0) return true;
            return false;
        }

        bool ReplaceMAT(Sparse::Matrix &A) {
            if (isInitialized()) Finalize();
            if (prec != NULL) prec->ReplaceMAT(A);
            Alink = &A;
            return true;
        }

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

        bool ReplaceSOL(Sparse::Vector &SOL) {
            (void) SOL;
            return true;
        }

        Method *Duplicate() { return new BCGSL_solver(*this); }

        std::string GetReason() { return reason; }
    };


    class BCGS_solver : public IterativeMethod {
        INMOST_DATA_REAL_TYPE rtol, atol, divtol, last_resid;
        INMOST_DATA_ENUM_TYPE iters, maxits, last_it;
        INMOST_DATA_REAL_TYPE resid;
        Sparse::Vector r0, p, y, s, t, z, r, v;
        Sparse::Matrix *Alink;
        Method *prec;
        Solver::OrderInfo *info;
        bool init;
        std::string reason;
    public:
        INMOST_DATA_ENUM_TYPE GetIterations() { return last_it; }

        INMOST_DATA_REAL_TYPE GetResidual() { return last_resid; }

        INMOST_DATA_REAL_TYPE &RealParameter(std::string name) {
            if (name[0] == ':') {
                if (prec != NULL) return prec->RealParameter(name.substr(1, name.size() - 1));
            }
            if (name == "rtol") return rtol;
            else if (name == "atol") return atol;
            else if (name == "divtol") return divtol;
            else if (prec != NULL) return prec->RealParameter(name);
            throw -1;
        }

        INMOST_DATA_ENUM_TYPE &EnumParameter(std::string name) {
            if (name[0] == ':') {
                if (prec != NULL) return prec->EnumParameter(name.substr(1, name.size() - 1));
            }
            if (name == "maxits") return maxits;
            else if (prec != NULL) return prec->EnumParameter(name);
            throw -1;
        }

        BCGS_solver(Method *prec, Solver::OrderInfo &info)
                : rtol(1e-8), atol(1e-11), divtol(1e+40), iters(0), maxits(1500), prec(prec), info(&info) {
            init = false;
        }

        bool Initialize() {
            assert(Alink != NULL);
            if (isInitialized()) Finalize();
            if (prec != NULL && !prec->isInitialized()) prec->Initialize();
            info->PrepareVector(r);
            info->PrepareVector(v);
            info->PrepareVector(p);
            info->PrepareVector(y);
            info->PrepareVector(s);
            info->PrepareVector(t);
            info->PrepareVector(z);
            info->PrepareVector(r0);
            init = true;
            return true;
        }

        bool isInitialized() { return init && (prec == NULL || prec->isInitialized()); }

        bool Finalize() {
            if (prec != NULL && !prec->isFinalized()) prec->Finalize();
            init = false;
            return true;
        }

        bool isFinalized() { return !init && (prec == NULL || prec->isFinalized()); }

        void Copy(const Method *other) {
            const BCGS_solver *b = dynamic_cast<const BCGS_solver *>(other);
            assert(b != NULL);
            info = b->info;
            rtol = b->rtol;
            atol = b->atol;
            divtol = b->divtol;
            maxits = b->maxits;
            last_resid = b->last_resid;
            iters = b->iters;
            last_it = b->last_it;
            resid = b->resid;
            Alink = b->Alink;
            if (b->prec != NULL) {
                if (prec == NULL) prec = b->prec->Duplicate();
                else prec->Copy(b->prec);
            }
            if (b->init) Initialize();
        }

        BCGS_solver(const BCGS_solver &other) : IterativeMethod(other) {
            Copy(&other);
        }

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

        ~BCGS_solver() {
            if (!isFinalized()) Finalize();
            if (prec != NULL) delete prec;
        }

        bool Solve(Sparse::Vector &RHS, Sparse::Vector &SOL) {
            assert(isInitialized());
            INMOST_DATA_REAL_TYPE tempa = 0.0, tempb = 0.0, r0_norm = 0, length;
            INMOST_DATA_ENUM_TYPE vbeg, vend, vlocbeg, vlocend;
            INMOST_DATA_INTEGER_TYPE ivbeg, ivend, ivlocbeg, ivlocend;
            INMOST_DATA_REAL_TYPE rho = 1, rho1 = 0, alpha = 1, beta = 0, omega = 1;
            INMOST_DATA_REAL_TYPE resid0, resid, temp[2] = {0, 0};
            iters = 0;
            info->PrepareVector(SOL);
            info->PrepareVector(RHS);
            info->Update(SOL);
            info->Update(RHS);
            if (prec != NULL)prec->ReplaceSOL(SOL);
            if (prec != NULL)prec->ReplaceRHS(RHS);
            info->GetLocalRegion(info->GetRank(), vlocbeg, vlocend);
            info->GetVectorRegion(vbeg, vend);
            ivbeg = vbeg;
            ivend = vend;
            ivlocbeg = vlocbeg;
            ivlocend = vlocend;

            std::copy(RHS.Begin(), RHS.End(), r.Begin());
            {
                Alink->MatVec(-1, SOL, 1, r); //global multiplication, r probably needs an update
                info->Update(r); // r is good
            }
            std::copy(r.Begin(), r.End(), r0.Begin());
            std::fill(v.Begin(), v.End(), 0.0);
            std::fill(p.Begin(), p.End(), 0.0);
            {
                resid = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1378
#if defined(USE_OMP)
Kirill Terekhov's avatar
Kirill Terekhov committed
1379
#pragma omp parallel for reduction(+:resid)
Kirill Terekhov's avatar
Kirill Terekhov committed
1380
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1381 1382 1383 1384 1385
                for (INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; k++)
                    resid += r0[k] * r0[k];
                info->Integrate(&resid, 1);
            }
            last_resid = resid = resid0 = sqrt(resid);
Kirill Terekhov's avatar
Kirill Terekhov committed
1386 1387 1388
#if defined(USE_OMP)
#pragma omp parallel for
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1389 1390 1391
            for (INMOST_DATA_INTEGER_TYPE k = ivbeg; k < ivend; ++k) // r_tilde = r[0] / dot(r[0],r[0])
                r0[k] /= resid;
            last_it = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1392
#if defined(REPORT_RESIDUAL)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1393 1394 1395 1396 1397 1398 1399
            if (info->GetRank() == 0) {
                //std::cout << "iter " << last_it << " residual " << resid << std::endl;
                //std::cout << "iter " << last_it << " resid " << resid << "\r";
                //printf("iter %3d resid %12g | %12g relative %12g | %12g\r",last_it,resid,atol,resid/resid0,rtol);
                printf("iter %3d resid %12g | %g\r", last_it, resid, atol);
                fflush(stdout);
            }
Kirill Terekhov's avatar
Kirill Terekhov committed
1400
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1401

Dmitry Bagaev's avatar
Dmitry Bagaev committed
1402 1403 1404 1405 1406
            bool halt = false;
            if (last_resid < atol || last_resid < rtol * resid0) {
                reason = "initial solution satisfy tolerances";
                halt = true;
            }
Kirill Terekhov's avatar
Kirill Terekhov committed
1407

Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1408
#if defined(USE_OMP)
Kirill Terekhov's avatar
Kirill Terekhov committed
1409
#pragma omp parallel
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1410
#endif
1411
            {
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1412 1413 1414 1415 1416
                INMOST_DATA_ENUM_TYPE i = 0;
                while (!halt) {

                    if (false) {
                        perturbate:
1417
#if defined(PERTURBATE_RTILDE)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1418 1419
                        //std::cout << "Rescue solver by perturbing orthogonal direction!" << std::endl;
                        //Compute length of the vector
1420 1421 1422
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1423 1424 1425 1426 1427 1428
                        {
                            length = static_cast<INMOST_DATA_REAL_TYPE>(ivlocend - ivlocbeg);
                            r0_norm = 0.0;
                        }
                        info->Integrate(&length, 1);
                        //perform perturbation (note that rand() with openmp may give identical sequences of random values
1429 1430 1431
#if defined(USE_OMP)
#pragma omp for
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1432 1433 1434 1435 1436 1437
                        for (INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k) {
                            INMOST_DATA_REAL_TYPE unit = 2 * static_cast<INMOST_DATA_REAL_TYPE>(rand()) /
                                                         static_cast<INMOST_DATA_REAL_TYPE>(RAND_MAX) - 1.0;
                            r0[k] += unit / length;
                        }
                        //compute norm for orthogonal vector
1438 1439 1440
#if defined(USE_OMP)
#pragma omp for reduction(+:r0_norm)
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1441 1442
                        for (INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k)
                            r0_norm += r0[k] * r0[k];
1443 1444 1445
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1446 1447 1448 1449 1450
                        {
                            r0_norm = sqrt(r0_norm);
                        }
                        info->Integrate(&r0_norm, 1);
                        //normalize orthogonal vector to unity
1451 1452 1453
#if defined(USE_OMP)
#pragma omp for
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1454 1455 1456
                        for (INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k)
                            r0[k] /= r0_norm;
                        //Recompute rho1
1457
#else
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1458 1459 1460
                        reason = "r_tilde perturbation algorithm is disabled";
                        halt = true;
                        break;
1461
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1462
                    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1463

Dmitry Bagaev's avatar
Dmitry Bagaev committed
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
                    /*
                    if( fabs(rho) < 1.0e-31 )
                    {
                        std::cout << "rho " << rho << " alpha " << alpha << " omega " << omega << " beta " << 1.0 /rho * alpha / omega << std::endl;
                        reason = "denominator(1) is zero";
                        break;
                    }
                    if( fabs(omega) < 1.0e-31 )
                    {
                        std::cout << "rho " << rho << " alpha " << alpha << " omega " << omega << " beta " << 1.0 /rho * alpha / omega << std::endl;
                        reason = "denominator(2) is zero";
                        break;
                    }
                    */
Kirill Terekhov's avatar
Kirill Terekhov committed
1478

Kirill Terekhov's avatar
Kirill Terekhov committed
1479 1480 1481
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1482 1483 1484 1485
                    {
                        beta = 1.0 / rho * alpha / omega;
                        rho1 = 0;
                    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1486

Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1487
#if defined(USE_OMP)
Kirill Terekhov's avatar
Kirill Terekhov committed
1488
#pragma omp for reduction(+:rho1)
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1489
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
                    for (INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k)
                        rho1 += r0[k] * r[k];

                    info->Integrate(&rho1, 1);

                    if (fabs(rho1) < 1.0e-50) {
                        //std::cout << "Asking to perturbate r_tilde since rho1 is too small " << rho1 << std::endl;
                        goto perturbate;
                    }

Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1500
#if defined(USE_OMP)
Kirill Terekhov's avatar
Kirill Terekhov committed
1501
#pragma omp single
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1502
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1503 1504 1505 1506
                    {
                        beta *= rho1;
                        rho = rho1;
                    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1507

Dmitry Bagaev's avatar
Dmitry Bagaev committed
1508
                    if (fabs(beta) > 1.0e+100) {
Kirill Terekhov's avatar
Kirill Terekhov committed
1509 1510 1511
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1512 1513 1514 1515 1516 1517
                        {
                            reason = "multiplier(1) is too large";
                            halt = true;
                        }
                        continue;
                    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1518

Dmitry Bagaev's avatar
Dmitry Bagaev committed
1519
                    if (beta != beta) {
Kirill Terekhov's avatar
Kirill Terekhov committed
1520 1521 1522
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1523 1524 1525 1526 1527 1528 1529 1530
                        {
                            reason = "multiplier(1) is NaN";
                            halt = true;
                        }
                        continue;
                    }


Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1531
#if defined(USE_OMP)
Kirill Terekhov's avatar
Kirill Terekhov committed
1532
#pragma omp for
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1533
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
                    for (INMOST_DATA_INTEGER_TYPE k = ivbeg; k < ivend; ++k)
                        p[k] = r[k] + beta * (p[k] - omega * v[k]); //global indexes r, p, v



                    if (prec != NULL) {
                        prec->Solve(p, y);
                        info->Update(y);
                        Alink->MatVec(1, y, 0,
                                      v); // global multiplication, y should be updated, v probably needs an update
                        info->Update(v);
                    } else {
                        Alink->MatVec(1, p, 0,
                                      v); // global multiplication, y should be updated, v probably needs an update
                        info->Update(v);
                    }


Kirill Terekhov's avatar
Kirill Terekhov committed
1552 1553 1554
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1555 1556 1557
                    {
                        alpha = 0;
                    }
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1558
#if defined(USE_OMP)
Kirill Terekhov's avatar
Kirill Terekhov committed
1559
#pragma omp for reduction(+:alpha)
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1560
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1561 1562 1563 1564
                    for (INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k)
                        alpha += r0[k] * v[k];
                    info->Integrate(&alpha, 1);
                    //info->ScalarProd(r0,v,ivlocbeg,ivlocend,alpha);
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1565
#if defined(USE_OMP)
Kirill Terekhov's avatar
Kirill Terekhov committed
1566
#pragma omp single
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1567
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1568 1569 1570 1571 1572 1573
                    {
                        //if( alpha == 0 && rho == 0 )
                        //	alpha = 0;
                        //else
                        alpha = rho / alpha; //local indexes, r0, v
                    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1574

Dmitry Bagaev's avatar
Dmitry Bagaev committed
1575
                    if (fabs(alpha) > 1.0e+100) {
Kirill Terekhov's avatar
Kirill Terekhov committed
1576 1577 1578
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1579 1580 1581 1582 1583 1584
                        {
                            reason = "multiplier(2) is too large";
                            halt = true;
                        }
                        continue;
                    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1585

Dmitry Bagaev's avatar
Dmitry Bagaev committed
1586
                    if (alpha != alpha) {
Kirill Terekhov's avatar
Kirill Terekhov committed
1587 1588 1589
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1590 1591 1592 1593 1594 1595 1596
                        {
                            reason = "multiplier(2) is NaN";
                            halt = true;
                        }
                        continue;
                    }

Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1597
#if defined(USE_OMP)
Kirill Terekhov's avatar
Kirill Terekhov committed
1598
#pragma omp for
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1599
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
                    for (INMOST_DATA_INTEGER_TYPE k = ivbeg; k < ivend; ++k)
                        s[k] = r[k] - alpha * v[k]; //global indexes r, v



                    if (prec != NULL) {
                        prec->Solve(s, z);
                        info->Update(z);
                        Alink->MatVec(1.0, z, 0,
                                      t); // global multiplication, z should be updated, t probably needs an update
                        info->Update(t);
                    } else {
                        Alink->MatVec(1.0, s, 0,
                                      t); // global multiplication, z should be updated, t probably needs an update
                        info->Update(t);
                    }


Kirill Terekhov's avatar
Kirill Terekhov committed
1618 1619 1620
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1621 1622 1623 1624
                    {
                        tempa = 0.0; //can be negative
                        tempb = 1.0e-100; //tempb is positive since squared
                    }
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1625
#if defined(USE_OMP)
Kirill Terekhov's avatar
Kirill Terekhov committed
1626
#pragma omp for reduction(+:tempa,tempb)
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1627
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1628 1629 1630 1631
                    for (INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; k++) {
                        tempa += t[k] * s[k];
                        tempb += t[k] * t[k];
                    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1632 1633 1634
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1635 1636 1637 1638 1639 1640 1641
                    {
                        temp[0] = tempa;
                        temp[1] = tempb;
                    }
                    info->Integrate(temp, 2);

                    //omega = temp[0] / (temp[1] + (temp[1] < 0.0 ? -1.0e-10 : 1.0e-10)); //local indexes t, s
Kirill Terekhov's avatar
Kirill Terekhov committed
1642 1643 1644
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1645 1646 1647 1648 1649 1650
                    {
                        //if( temp[0] == 0 && temp[1] == 0 )
                        //	omega = 0;
                        //else
                        omega = temp[0] / temp[1];
                    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1651

Dmitry Bagaev's avatar
Dmitry Bagaev committed
1652
                    if (fabs(omega) > 1.0e+100) {
Kirill Terekhov's avatar
Kirill Terekhov committed
1653 1654 1655
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1656 1657 1658 1659 1660 1661
                        {
                            reason = "multiplier(3) is too large";
                            halt = true;
                        }
                        continue;
                    }
Kirill Terekhov's avatar
Kirill Terekhov committed
1662

Dmitry Bagaev's avatar
Dmitry Bagaev committed
1663
                    if (omega != omega) {
Kirill Terekhov's avatar
Kirill Terekhov committed
1664 1665 1666
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1667 1668 1669 1670 1671 1672 1673 1674
                        {
                            reason = "multiplier(3) is NaN";
                            halt = true;
                        }
                        continue;
                    }


Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1675
#if defined(USE_OMP)
Kirill Terekhov's avatar
Kirill Terekhov committed
1676
#pragma omp for
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1677
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1678 1679 1680 1681
                    for (INMOST_DATA_INTEGER_TYPE k = ivbeg; k < ivend; ++k)
                        SOL[k] += alpha * y[k] + omega * z[k]; // global indexes SOL, y, z


Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1682
#if defined(USE_OMP)
Kirill Terekhov's avatar
Kirill Terekhov committed
1683
#pragma omp for
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1684
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1685 1686 1687 1688
                    for (INMOST_DATA_INTEGER_TYPE k = ivbeg; k < ivend; ++k)
                        r[k] = s[k] - omega * t[k]; // global indexes r, s, t

                    //info->ScalarProd(r,r,ivlocbeg,ivlocend,resid);
Kirill Terekhov's avatar
Kirill Terekhov committed
1689 1690 1691
#if defined(USE_OMP)
#pragma omp single
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1692 1693 1694 1695
                    {
                        last_it++;
                        resid = 0;
                    }
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1696
#if defined(USE_OMP)
Kirill Terekhov's avatar
Kirill Terekhov committed
1697
#pragma omp for reduction(+:resid)
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1698
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1699 1700 1701
                    for (INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k)
                        resid += r[k] * r[k];
                    info->Integrate(&resid, 1);
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1702
#if defined(USE_OMP)
Kirill Terekhov's avatar