main.cpp 9.51 KB
Newer Older
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1
2
3
4
5
6
7
#include "inmost.h"
#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>

Dmitry Bagaev's avatar
Dmitry Bagaev committed
8
#include "Source/Solver/ttsp/ttsp.h"
Dmitry Bagaev's avatar
Dmitry Bagaev committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "Source/Solver/ttsp/optimizers/bruteforce/ttsp_bruteforce.h"

using namespace INMOST;

#if defined(USE_MPI)
#define BARRIER MPI_Barrier(MPI_COMM_WORLD)
#else
#define BARRIER
#endif


int main(int argc, char **argv) {
    int rank = 0, size = 1;

#if defined(USE_MPI)
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);  // Get the rank of the current process
    MPI_Comm_size(MPI_COMM_WORLD, &size); // Get the total number of processors used
#endif


    {
        std::string matrixFileName = "";
        std::string vectorBFileName = "";
        std::string parametersFileName = "";
        std::string solverName = "fcbiilu2";

        bool matrixFound = false;
        bool vectorBFound = false;
        bool parametersFound = false;
        bool typeFound = false;

        //Parse argv parameters
        if (argc == 1) goto helpMessage;
        int i;
        for (i = 1; i < argc; i++) {
            //Print help message and exit
            if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
                helpMessage:
                if (rank == 0) {
                    std::cout << "Help message: " << std::endl;
                    std::cout << "Command line options: " << std::endl;
                    std::cout << "Required: " << std::endl;
                    std::cout << "-m, --matrix <Matrix file name>" << std::endl;
                    std::cout << "Optional: " << std::endl;
                    std::cout << "-b, --bvector <RHS vector file name>" << std::endl;
                    std::cout << "-d, --database <Solver parameters file name>" << std::endl;
                    std::cout << "-t, --type <Solver type name>" << std::endl;
                    std::cout << "  Available solvers:" << std::endl;
                    Solver::Initialize(NULL, NULL, NULL);
                    std::vector<std::string> availableSolvers = Solver::getAvailableSolvers();
                    for (auto it = availableSolvers.begin(); it != availableSolvers.end(); it++) {
                        std::cout << "      " << *it << std::endl;
                    }
                    Solver::Finalize();
                }
                return 0;
            }
            //Matrix file name found with -m or --matrix options
            if (strcmp(argv[i], "-m") == 0 || strcmp(argv[i], "--matrix") == 0) {
                matrixFound = true;
                matrixFileName = std::string(argv[i + 1]);
                FILE *matrixFile = fopen(matrixFileName.c_str(), "r");
                if (matrixFile == NULL) {
                    if (rank == 0) {
                        std::cout << "Matrix file not found: " << argv[i + 1] << std::endl;
                        exit(1);
                    }
                } else {
                    if (rank == 0) {
                        std::cout << "Matrix file found: " << argv[i + 1] << std::endl;
                    }
                }
                fclose(matrixFile);
                i++;
                continue;
            }
            //B vector file name found with -b or --bvector options
            if (strcmp(argv[i], "-b") == 0 || strcmp(argv[i], "--bvector") == 0) {
                if (rank == 0) {
                    std::cout << "B vector file found: " << argv[i + 1] << std::endl;
                }
                vectorBFound = true;
                vectorBFileName = std::string(argv[i + 1]);
                i++;
                continue;
            }
            //Parameters file name found with -d or --database options
            if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--database") == 0) {
                if (rank == 0) {
                    std::cout << "Solver parameters file found: " << argv[i + 1] << std::endl;
                }
                parametersFound = true;
                parametersFileName = std::string(argv[i + 1]);
                i++;
                continue;
            }
            //Solver type found with -t ot --type options
            if (strcmp(argv[i], "-t") == 0 || strcmp(argv[i], "--type") == 0) {
                if (rank == 0) {
                    std::cout << "Solver type index found: " << argv[i + 1] << std::endl;
                }
                typeFound = true;
                solverName = std::string(argv[i + 1]);
                i++;
                continue;
            }
        }

        if (!matrixFound) {
            if (rank == 0) {
                std::cout <<
                          "Matrix not found, you can specify matrix file name using -m or --matrix options, otherwise specify -h option to see all options, exiting...";
            }
            return -1;
        }

        if (!typeFound) {
            if (rank == 0) {
                std::cout <<
                          "Solver type not found in command line, you can specify solver type with -t or --type option, using INNER_ILU2 solver by default."
                          <<
                          std::endl;
            }
        }

        if (!vectorBFound) {
            if (rank == 0) {
                std::cout <<
                          "B vector not found, you can specify b vector file name with -b or --bvector option, using identity vector by default."
                          <<
                          std::endl;
            }
        }

        // Initialize the linear solver in accordance with args
        Solver::Initialize(&argc, &argv, parametersFound ? parametersFileName.c_str() : NULL);

        if (!Solver::isSolverAvailable(solverName)) {
            if (rank == 0) std::cout << "Solver " << solverName << " is not available" << std::endl;
            Solver::Finalize();
            exit(1);
        }

        Solver solver = Solver(solverName, "test");

        if (rank == 0) std::cout << "Solving with " << solverName << std::endl;

        Sparse::Matrix mat("A"); // Declare the matrix of the linear system to be solved
        Sparse::Vector b("rhs"); // Declare the right-hand side vector
        Sparse::Vector x("sol"); // Declare the solution vector

        double timer = Timer();
        mat.Load(matrixFileName); //ifinterval parameters not set, matrix will be divided automatically

        if (rank == 0) std::cout << "Load matrix time:    " << Timer() - timer << std::endl;

        timer = Timer();
        if (vectorBFound) {
            b.Load(vectorBFileName); //if interval parameters not set, matrix will be divided automatically
        } else { // Set local RHS to 1 if it was not specified
            INMOST_DATA_ENUM_TYPE mbeg, mend, k;
            mat.GetInterval(mbeg, mend);
            b.SetInterval(mbeg, mend);
            for (k = mbeg; k < mend; ++k) b[k] = 1.0;
        }

        if (rank == 0) std::cout << "Load vector time:    " << Timer() - timer << std::endl;

Dmitry Bagaev's avatar
Dmitry Bagaev committed
178
        TTSP::OptimizationParameter tau("tau", {1e-3, 3e-3, 5e-3, 7e-3, 1e-2, 3e-2, 5e-2, 7e-2}, 1e-3);
Dmitry Bagaev's avatar
Dmitry Bagaev committed
179
180
181
182
183
184
185
186


        TTSP::OptimizationParameters parameters;
        parameters.push_back(std::make_pair(tau, 1e-3));

        TTSP::OptimizationParametersSpace space(solverName, "test", parameters);
        TTSP::BruteforceOptimizer optimizer(space);

Dmitry Bagaev's avatar
Dmitry Bagaev committed
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
        //        BARRIER;
        //        timer = Timer();
        //        solver.SetMatrix(mat);       // Compute the preconditioner for the original matrix
        //        BARRIER;
        //
        //        if (rank == 0) std::cout << "Preconditioner time: " << Timer() - timer << std::endl;
        //
        //        BARRIER;
        //        timer = Timer();
        //        bool isSuccess = solver.Solve(b, x); // Solve the linear system with the previously computted preconditioner
        //        BARRIER;

        int test = 0;

        while (test < 15) {

            optimizer.Solve(solver, mat, b, x);

            std::cout << std::endl << "Best optimization parameters found for current iteration:" << std::endl;
            const TTSP::OptimizationParameterPoints &best = optimizer.GetSpace().GetPoints();
            std::for_each(best.begin(), best.end(), [](const TTSP::OptimizationParameterPoint &p) {
                std::cout << "\t" << p.first << " = " << p.second << std::endl;
            });

            std::cout << std::endl << "Optimization results buffer output:" << std::endl;
            const TTSP::OptimizationParameterResultsBuffer &results = optimizer.GetResults();

            int index = 1;
            std::for_each(results.begin(), results.end(), [&index](const TTSP::OptimizationParameterResult &result) {
                std::cout << "\t" << index++ << "\t" << " [";

                const TTSP::OptimizationParameterPoints &points = result.GetPoints();
                std::for_each(points.begin(), points.end(), [](const TTSP::OptimizationParameterPoint &point) {
                    std::cout << " " << point.first << "=" << point.second << " ";
                });

                std::cout << "] " << result.GetPreconditionerTime() << "\t" << result.GetSolveTime() << "\t" << result.GetTime() << std::endl;
            });

            if (rank == 0) {
                std::cout << std::endl
                          << "Solved with " << solver.SolverName()
                          << " on " << solver.Iterations()
                          << " iterations and " << solver.Residual()
                          << " residual. Reason: " << solver.ReturnReason()
                          << std::endl;
            }

            test += 1;
Dmitry Bagaev's avatar
Dmitry Bagaev committed
236
237
238
239
240
241
        }
    }

    Solver::Finalize(); // Finalize solver and close MPI activity
    return 0;
}