SolverInner.cpp 9.85 KB
Newer Older
1
2
3
4
#include "SolverInner.h"

namespace INMOST {

Dmitry Bagaev's avatar
Dmitry Bagaev committed
5
    SolverInner::SolverInner() {
Dmitry Bagaev's avatar
Dmitry Bagaev committed
6
        maximum_iterations = 2500;
Dmitry Bagaev's avatar
Dmitry Bagaev committed
7
8
9
        atol = 1.0e-5;
        rtol = 1.0e-12;
        dtol = 1.0e+100;
10
11
    }

Dmitry Bagaev's avatar
Dmitry Bagaev committed
12
    SolverInterface *SolverInner::Copy(const SolverInterface *other) {
13
14
15
16
17
18
19
20
21
        //You should not really want to copy solver's information
        throw INMOST::SolverUnsupportedOperation;
    }

    void SolverInner::Assign(const SolverInterface *other) {
        //You should not really want to copy solver's information
        throw INMOST::SolverUnsupportedOperation;
    }

Kirill Terekhov's avatar
Kirill Terekhov committed
22
23
24
25
26
27
28
    void SolverInner::Setup(int *argc, char ***argv, SolverParameters &p)
	{
		char line[4096];
		char parameterName[4096];
		char parameterValue[4096];
        if (!p.internalFile.empty())
		{
Dmitry Bagaev's avatar
Dmitry Bagaev committed
29
            FILE *databaseFile = fopen(p.internalFile.c_str(), "r");
Kirill Terekhov's avatar
Kirill Terekhov committed
30
31
32
            if (!databaseFile) return;
            while (!feof(databaseFile) && fgets(line, 4096, databaseFile))
			{
Dmitry Bagaev's avatar
Dmitry Bagaev committed
33
34
35
36
                if (line[0] == '#') continue;
                sscanf(line, "%s %s", parameterName, parameterValue);
                this->SetParameter(parameterName, parameterValue);
            }
Kirill Terekhov's avatar
Kirill Terekhov committed
37
38
39
40
41
			
        }
		else
		{
            for (parameters_iterator_t parameter = p.parameters.begin(); parameter < p.parameters.end(); parameter++)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
42
43
44
                this->SetParameter((*parameter).first, (*parameter).second);
        }

45
46
47
    }

    bool SolverInner::Solve(Sparse::Vector &RHS, Sparse::Vector &SOL) {
Dmitry Bagaev's avatar
Dmitry Bagaev committed
48
        solver->EnumParameter("maxits") = maximum_iterations;
Dmitry Bagaev's avatar
Dmitry Bagaev committed
49
50
51
        solver->RealParameter("rtol") = rtol;
        solver->RealParameter("atol") = atol;
        solver->RealParameter("divtol") = dtol;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

        return solver->Solve(RHS, SOL);
    }

    bool SolverInner::Clear() {
        info.Clear();
        if (matrix != NULL) {
            delete matrix;
            matrix = NULL;
        }
        if (solver != NULL) {
            delete solver;
            solver = NULL;
        }
        return true;
    }

    bool SolverInner::isMatrixSet() {
        return matrix != NULL;
    }

Dmitry Bagaev's avatar
Dmitry Bagaev committed
73
    std::string SolverInner::GetParameter(std::string name) const {
Dmitry Bagaev's avatar
Dmitry Bagaev committed
74
75
76
77
        if (name == "maximum_iterations") return to_string(maximum_iterations);
        else if (name == "absolute_tolerance") return to_string(atol);
        else if (name == "relative_tolerance") return to_string(rtol);
        else if (name == "divergence_tolerance") return to_string(dtol);
Dmitry Bagaev's avatar
Dmitry Bagaev committed
78
        else {
Kirill Terekhov's avatar
Kirill Terekhov committed
79
#if !defined(SILENCE_SET_PARAMETER)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
80
            std::cout << "Parameter " << name << " is unknown" << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
81
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
82
83
84
85
86
87
            return "";
        }
    }

    void SolverInner::SetParameter(std::string name, std::string value) {
        const char *val = value.c_str();
Dmitry Bagaev's avatar
Dmitry Bagaev committed
88
89
90
91
        if (name == "maximum_iterations") maximum_iterations = static_cast<INMOST_DATA_ENUM_TYPE>(atoi(val));
        else if (name == "absolute_tolerance") atol = atof(val);
        else if (name == "relative_tolerance") rtol = atof(val);
        else if (name == "divergence_tolerance") dtol = atof(val);
Kirill Terekhov's avatar
Kirill Terekhov committed
92
#if !defined(SILENCE_SET_PARAMETER)
Dmitry Bagaev's avatar
Dmitry Bagaev committed
93
        else std::cout << "Parameter " << name << " is unknown" << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
94
#endif
Dmitry Bagaev's avatar
Dmitry Bagaev committed
95
96
    }

97
98
99
100
101
102
103
104
105
106
107
108
    const INMOST_DATA_ENUM_TYPE SolverInner::Iterations() const {
        return solver->GetIterations();
    }

    const INMOST_DATA_REAL_TYPE SolverInner::Residual() const {
        return solver->GetResidual();
    }

    const std::string SolverInner::ReturnReason() const {
        return solver->GetReason();
    }

Dmitry Bagaev's avatar
Dmitry Bagaev committed
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
    const INMOST_DATA_REAL_TYPE SolverInner::Condest(INMOST_DATA_REAL_TYPE tol, INMOST_DATA_ENUM_TYPE maxiter) {
#if defined(ACCELERATED_CONDEST)
        INMOST_DATA_ENUM_TYPE lbeg, lend, l, iter;
        INMOST_DATA_REAL_TYPE norm, sum[2], norm_prev, lambda_min, lambda_max;
        bool diverged_max = false, diverged_min = false;
        info.GetLocalRegion(info.GetRank(), lbeg, lend);
        Sparse::Vector v, Av;
        info.PrepareVector(v);
        info.PrepareVector(Av);
        //Set v to random
        norm = 0;
        for (l = lbeg; l < lend; ++l) {
            v[l] = rand() / static_cast<INMOST_DATA_REAL_TYPE>(RAND_MAX);
            norm += v[l] * v[l];
        }
        info.Integrate(&norm, 1);
        norm_prev = 0.0;
        norm = sqrt(norm);
        for (l = lbeg; l < lend; ++l) v[l] /= norm;
        //Compute maximum eigenvalue
        iter = 0;
        while (fabs((norm - norm_prev) / norm) > tol && iter < maxiter) {
            info.Update(v);
#if defined(USE_OMP)
#pragma omp parallel
#endif
            matrix->MatVec(1.0, v, 0.0, Av);
            norm_prev = norm;
            sum[0] = sum[1] = 0.0;
            for (l = lbeg; l < lend; ++l) {
                sum[0] += v[l] * Av[l];
                sum[1] += v[l] * v[l];
            }
            info.Integrate(sum, 2);
            norm = fabs(sum[0]) / sum[1];
            for (l = lbeg; l < lend; ++l) v[l] = Av[l] / norm;
#if defined(PRINT_CONDEST)
            std::cout << "iteration " << iter << " norm " << norm << std::endl;
#endif
            iter++;
        }
#if defined(PRINT_CONDEST)
        std::cout << "lambda_max " << norm << std::endl;
#endif
        if (iter == maxiter) {
            diverged_max = true;
            std::cout << "Max not converged" << std::endl;
        }
        lambda_max = norm;
        //Set v to random
        norm = 0;
        for (l = lbeg; l < lend; ++l) {
            v[l] = rand() / static_cast<INMOST_DATA_REAL_TYPE>(RAND_MAX);
            norm += v[l] * v[l];
        }
        info.Integrate(&norm, 1);
        norm_prev = 0.0;
        norm = sqrt(norm);
        for (l = lbeg; l < lend; ++l) v[l] /= norm;
        //Compute minimal eigenvalue
        iter = 0;
        while (fabs((norm - norm_prev) / norm) > tol && iter < maxiter) {
            info.Update(v);
            Solve(v, Av);
            norm_prev = norm;
            sum[0] = sum[1] = 0;
            for (l = lbeg; l < lend; ++l) {
                sum[0] += v[l] * Av[l];
                sum[1] += v[l] * v[l];
            }
            info.Integrate(sum, 2);
            norm = fabs(sum[0]) / sum[1];
            for (l = lbeg; l < lend; ++l) v[l] = Av[l] / norm;
#if defined(PRINT_CONDEST)
            std::cout << "iteration " << iter << " norm " << norm << "\t\t" << std::endl;
#endif
            iter++;
        }
#if defined(PRINT_CONDEST)
        std::cout << "lambda_min " << 1.0 / norm << std::endl;
#endif
        if (iter == maxiter) {
            diverged_min = true;
            std::cout << "Min not converged" << std::endl;
        }
        lambda_min = 1.0 / norm;
        if (diverged_max || diverged_min)
            return 1.0e+100;
#if defined(PRINT_CONDEST)
        std::cout << "Condest: " << lambda_max / lambda_min << std::endl;
#endif
        return lambda_max / lambda_min;
#else //!ACCELERATED_CONDEST
        INMOST_DATA_ENUM_TYPE lbeg, lend, l, iter;
        INMOST_DATA_REAL_TYPE norm, norm_prev, lambda_min, lambda_max;
        bool diverged_max = false, diverged_min = false;
        info.GetLocalRegion(info.GetRank(), lbeg, lend);
        Sparse::Vector v, Av;
        info.PrepareVector(v);
        info.PrepareVector(Av);
        //Set v to random
        norm = 0;
        for (l = lbeg; l < lend; ++l) {
            v[l] = rand() / static_cast<INMOST_DATA_REAL_TYPE>(RAND_MAX);
            norm += v[l] * v[l];
        }
        info.Integrate(&norm, 1);
        norm_prev = 0.0;
        norm = sqrt(norm);
        for (l = lbeg; l < lend; ++l) v[l] /= norm;
        //Compute maximum eigenvalue
        iter = 0;
        while (fabs(norm - norm_prev) / norm > tol && iter < maxiter) {
            info.Update(v);
#if defined(USE_OMP)
#pragma omp parallel
#endif
            matrix->MatVec(1.0, v, 0.0, Av);
            v.Swap(Av);
            norm_prev = norm;
            norm = 0.0;
            for (l = lbeg; l < lend; ++l) norm += v[l] * v[l];
            info.Integrate(&norm, 1);
            norm = sqrt(norm);
            for (l = lbeg; l < lend; ++l) v[l] /= norm;
#if defined(PRINT_CONDEST)
            std::cout << "iteration " << iter << " norm " << norm << std::endl;
#endif
            iter++;
        }
#if defined(PRINT_CONDEST)
        std::cout << "lambda_max " << norm << std::endl;
#endif
        if (iter == maxiter) {
            norm = std::max(norm, norm_prev);
            //diverged_max = true;
            //std::cout << "Max not converged" << std::endl;
        }
        lambda_max = norm;
        //Set v to random
        norm = 0;
        for (l = lbeg; l < lend; ++l) {
            v[l] = rand() / static_cast<INMOST_DATA_REAL_TYPE>(RAND_MAX);
            norm += v[l] * v[l];
        }
        info.Integrate(&norm, 1);
        norm_prev = 0.0;
        norm = sqrt(norm);
        for (l = lbeg; l < lend; ++l) v[l] /= norm;
        //Compute minimal eigenvalue
        iter = 0;
        while (fabs(norm - norm_prev) / norm > tol && iter < maxiter) {
            info.Update(v);
            Solve(v, Av);
            v.Swap(Av);
            norm_prev = norm;
            norm = 0.0;
            for (l = lbeg; l < lend; ++l) norm += v[l] * v[l];
            info.Integrate(&norm, 1);
            norm = sqrt(norm);
            for (l = lbeg; l < lend; ++l) v[l] /= norm;
#if defined(PRINT_CONDEST)
            std::cout << "iteration " << iter << " norm " << norm << "\t\t" << std::endl;
#endif
            iter++;
        }
#if defined(PRINT_CONDEST)
        std::cout << "lambda_min " << 1.0 / norm << std::endl;
#endif
        if (iter == maxiter) {
            norm = std::max(norm, norm_prev);
            //diverged_min = true;
            //std::cout << "Min not converged" << std::endl;
        }
        lambda_min = 1.0 / norm;
        //Condition is always false? Maybe check this
        if (diverged_max || diverged_min)
            return 1.0e+100;
#if defined(PRINT_CONDEST)
        std::cout << "Condest: " << lambda_max / lambda_min << std::endl;
#endif
        return lambda_max / lambda_min;
#endif
    }

294
295
296
297
298
299
300
    void SolverInner::Finalize() {

    }

    SolverInner::~SolverInner() {
        this->Clear();
    }
Dmitry Bagaev's avatar
Dmitry Bagaev committed
301

302
}