main.cpp 8.92 KB
Newer Older
1
#include "inmost.h"
Igor Konshin's avatar
Igor Konshin committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <string>
#include <iostream>

using namespace INMOST;

/***********************************************************************

(*) 5402 3D Poisson equation solver

(*) Test solvers on 3D Poisson equation.

This test is located in Tests/solver_test002

(*) Brief

Test solvers and tune parameters for on-the-fly generated matrices for 3D
Poisson equation.

(*) Description

This test will run solvers in both serial and parallel modes with NP 
processes for a model problem of 3D Poisson equation.
The coefficient matrix and vectors for a parallel run will be generated
directly at the target processor, so no external reordering is required
to be installed.
The artificial right-hand side rhs=(1,1,...,1) is used.
The specific solver is defined by a user.
User may also provide options file to alter default solver options.

Main purpose of this test is to assess robustness of internal or external
solvers during development.
Another purpose is to check the behaviour of the liner solver for large
and extremely large test problems without taking into account the disk
memory requirements.

(*) Arguments

39
Usage: ./solver_test002 <solver_type> N<for NxNxN problem> [solver_options.xml]
Igor Konshin's avatar
Igor Konshin committed
40
41
42


    * First parameter is the Solver type:
43
        inner_ilu2, inner Solver based on BiCGStab(L) solver with second
44
order IIU factorization as preconditioner;
45
46
47
48
        inner_ddpqiluc, inner Solver based on BiCGStab(L) solver with second order Crout-ILU with inversed-based condition estimation and unsymmetric reordering for diagonal dominance as preconditioner;
        inner_mptiluc, inner Solver based on BiCGStab(L) solver with second order Crout-ILU with inversed-based condition estimation and maximum product transversal reordering as preconditioner;
        inner_mptilu2, inner Solver based on BiCGStab(L) solver with second order ILU and maximum product transversal reordering as preconditione;
        trilinos_aztec, external Solver AztecOO from Trilinos package;
49
currentty without preconditioner;
50
51
52
53
54
55
56
        trilinos_belos, external Solver Belos from Trilinos package, currently without preconditioner;
        trilinos_ml, external Solver AztecOO with ML preconditioner;
        trilinos_ifpack, external Solver AztecOO with Ifpack preconditioner;
        petsc, external Solver PETSc;
        ani, external Solver from ANI3D based on ILU2 (sequential Fortran version);
       fcbiilu2, external FCBIILU2 Solver (BIILU2 parallel F2C version);
       k3biilu2, internal K3BIILU2 Solver (BIILU2 parallel version).
Igor Konshin's avatar
Igor Konshin committed
57
58
59
    * Second parameter is the dimension N of the 3D Poisson problem for NxNxN
mesh.
    * Third optional parameter is the file with solver parameters, see
60
examples/MatSolve/database.xml as example.
Igor Konshin's avatar
Igor Konshin committed
61
62
63
64
65
66
67
68

(*) Running test

You can run the test directly from the command line.
For example, you can specify the 100x100x100 test case and solve it by the 
internal ILU2 based solver with the default parameters on 4 processors:

$ cd tests/solver_test002
69
$ mpirun -np 4 ./solver_test002 inner_ilu2 100
Igor Konshin's avatar
Igor Konshin committed
70
71
72
73
74
75
76
77
78
79
80
81
82

(*) Source

    * Source code is adopted from examples/MatSolve

***********************************************************************/

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

Kirill Terekhov's avatar
Kirill Terekhov committed
83
void Poisson3D(int n1, int n2, int n3, Sparse::Matrix & mat); // Generate 3D Poisson matrix
Igor Konshin's avatar
Igor Konshin committed
84
85
86
87
88
89

int main(int argc, char ** argv)
{
	int rank,procs;
	if( argc < 3 )
	{
90
		std::cout << "Usage: " << argv[0] << " S<method name> N<for NxNxN problem> [solver_options.txt]" << std::endl;
Igor Konshin's avatar
Igor Konshin committed
91
92
		return -1;
	}
93
	std::string type = std::string(argv[1]);
Kirill Terekhov's avatar
Kirill Terekhov committed
94
  
Igor Konshin's avatar
Igor Konshin committed
95
96
97
98
99
100
101
102
103
104
	int n = atoi(argv[2]);
	Solver::Initialize(&argc,&argv,argc > 3 ? argv[3] : NULL); // Initialize the linear solver in accordance with args
	{
#if defined(USE_MPI)
		MPI_Comm_rank(MPI_COMM_WORLD,&rank);  // Get the rank of the current process
		MPI_Comm_size(MPI_COMM_WORLD,&procs); // Get the total number of processors used
#else
		rank = 0;
		procs = 1;
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
105
    if( rank == 0 )
106
      std::cout << "Testing " << type << std::endl;
Igor Konshin's avatar
Igor Konshin committed
107
		//std::cout << rank << "/" << procs << " " << argv[0] << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
108
109
110
		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
Igor Konshin's avatar
Igor Konshin committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
		int n1=n, n2=n, n3=n;
		if( !rank ) std::cout << "Poisson equation: " << n1 << " x " << n2 << " x " << n3 << std::endl;
		BARRIER
		double tt = Timer(), t = Timer();
		Poisson3D(n1,n2,n3,mat);
		BARRIER
		if( !rank ) std::cout << "Generate matrix: " << Timer() - t << std::endl;
		//mat.Save("test.mtx");
		// 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;
		bool success = false;
		int iters;
		double resid, realresid = 0;
		std::string reason;
		{
			Solver s(type); // Declare the linear solver by specified type
Dmitry Bagaev's avatar
Dmitry Bagaev committed
130
131
132
133
134
135
136

			s.SetParameter("gmres_substeps", "3");

			s.SetParameter("reorder_nonzeros", "0");
			s.SetParameter("rescale_iterations", "8");
			s.SetParameter("adapt_ddpq_tolerance", "0");

137
138
139
140
			s.SetParameter("verbosity", "0");
			s.SetParameter("drop_tolerance", "0.05");
			s.SetParameter("reuse_tolerance", "0.0025");
			s.SetParameter("pivot_condition", "5");
Dmitry Bagaev's avatar
Dmitry Bagaev committed
141
			s.SetParameter("ddpq_tolerance", "0.7");
142
			
Kirill Terekhov's avatar
merge    
Kirill Terekhov committed
143
144
			//mat.Save("A.mtx");
			//b.Save("b.mtx");
Igor Konshin's avatar
Igor Konshin committed
145
146
147
148
149
150
151

			t = Timer();
			s.SetMatrix(mat); // Compute the preconditioner for the original matrix
			BARRIER
			if( !rank ) std::cout << "preconditioner: " << Timer() - t << std::endl;
			t = Timer();
			success = s.Solve(b,x); // Solve the linear system with the previously computted preconditioner
152
			
Kirill Terekhov's avatar
merge    
Kirill Terekhov committed
153
			//x.Save("x.mtx");
Igor Konshin's avatar
Igor Konshin committed
154
155
156
157
			BARRIER
			if( !rank ) std::cout << "solver: " << Timer() - t << std::endl;
			iters = s.Iterations(); // Get the number of iterations performed
			resid = s.Residual();   // Get the final residual achieved
158
			reason = s.ReturnReason();
Igor Konshin's avatar
Igor Konshin committed
159
160
161
162
163
164
			//x.Save("output.rhs");
		}
		tt = Timer() - tt;

		{ // Compute the true residual
			double aresid = 0, bresid = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
165
			Sparse::Vector test;
Igor Konshin's avatar
Igor Konshin committed
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
			t = Timer();
			Solver::OrderInfo info;
			info.PrepareMatrix(mat,0);
			info.PrepareVector(x);
			info.Update(x);

			mat.MatVec(1.0,x,0.0,test); // Multiply the original matrix by a vector

			{
				INMOST_DATA_ENUM_TYPE mbeg,mend,k;
				info.GetLocalRegion(info.GetRank(),mbeg,mend);
				for( k = mbeg; k < mend; ++k )
				{
					aresid += (test[k]-b[k])*(test[k]-b[k]);
					bresid += b[k]*b[k];
				}
			}
			double temp[2] = {aresid,bresid}, recv[2] = {aresid,bresid};
#if defined(USE_MPI)
			MPI_Reduce(temp,recv,2,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
			if( info.GetRank() == 0 ) std::cout << "||Ax-b|| " << sqrt(recv[0]) << " ||b|| " << sqrt(recv[1]) << " ||Ax-b||/||b|| " << sqrt(recv[0]/recv[1]) << std::endl;
#endif
			realresid = sqrt(recv[0]/recv[1]);
			//realresid = sqrt(realresid);

			info.RestoreVector(x);
			if( !rank ) std::cout << "norms: " << Timer() - t << std::endl;
		}

		{
			if( rank == 0 )
			{
				std::cout << procs << " processors";
				if( success )
				{
					std::cout << " solved in " << tt << " secs";
					std::cout << " with " << iters << " iterations to " << resid << " norm";
				}
				else std::cout << " failed to solve";
				std::cout  << " matrix \"" << argv[2] << "\"";
				if( argc > 3 ) std::cout  << " vector \"" << argv[3] << "\"";
				std::cout << " true residual ||Ax-b||/||b|| " << realresid;
				std::cout << std::endl;
				std::cout << "reason: " << reason << std::endl;
			}
		}
	}

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

// Generate 3D Poisson matrix
Kirill Terekhov's avatar
Kirill Terekhov committed
219
void Poisson3D(int n1, int n2, int n3, Sparse::Matrix & A)
Igor Konshin's avatar
Igor Konshin committed
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
{
    int myid=0, nproc=1;
#if defined(USE_MPI)
    MPI_Comm_size (MPI_COMM_WORLD, &nproc);
    MPI_Comm_rank (MPI_COMM_WORLD, &myid);
#endif

    int n = n1 * n2 * n3;
    unsigned idmax, idmin, block = n / nproc;
    idmin = myid * block;
    idmax = idmin + block;
    if (myid == nproc-1) idmax = n;
    A.SetInterval(idmin,idmax);

    static const unsigned ndiag = 7;
    int id[ndiag] = {  0, 0,-1, 0, 1, 0, 0 }; // !    !    -1    !    !
    int jd[ndiag] = {  0,-1, 0, 0, 0, 1, 0 }; // ! -1 ! -1  6 -1 ! -1 !
    int kd[ndiag] = { -1, 0, 0, 0, 0, 0, 1 }; // !    !    -1    !    !
    int ad[ndiag] = { -1,-1,-1, 6,-1,-1,-1 };

    for (int k=0; k<n3; k++) {
        for (int j=0; j<n2; j++) {
            for (int i=0; i<n1; i++) {
                unsigned iii = i + j*n1 + k*n1*n2;
                if (iii >= idmin && iii < idmax) {
                    for (unsigned d=0; d<ndiag; d++) {
                        int ii = i + id[d];
                        int jj = j + jd[d];
                        int kk = k + kd[d];
                        if (ii >= 0 && ii < n1 &&
                            jj >= 0 && jj < n2 &&
                            kk >= 0 && kk < n3) {
                            int jjj = ii + jj*n1 + kk*n1*n2;
                            A[iii][jjj] = ad[d];
                        }
                    }
                }
            }
        }
    }
}