Debian Codegraph @

3rdparty/dissection/src/Interfaces/fortranAPI.cpp

raw
1 /*! \file fortranAPI.cpp
2 \brief Fortran style interface named as Dissecotion-fortran interface
3 \author Atsushi Suzuki, Laboratoire Jacques-Louis Lions
4 \date Mar. 30th 2012
5 \date Jul. 12th 2015
6 \date Nov. 30th 2016
7 */
8
9 // This file is part of Dissection
10 //
11 // Dissection is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 3 of the License, or
14 // (at your option) any later version.
15 //
16 // Linking Dissection statically or dynamically with other modules is making
17 // a combined work based on Disssection. Thus, the terms and conditions of
18 // the GNU General Public License cover the whole combination.
19 //
20 // As a special exception, the copyright holders of Dissection give you
21 // permission to combine Dissection program with free software programs or
22 // libraries that are released under the GNU LGPL and with independent modules
23 // that communicate with Dissection solely through the Dissection-fortran
24 // interface. You may copy and distribute such a system following the terms of
25 // the GNU GPL for Dissection and the licenses of the other code concerned,
26 // provided that you include the source code of that other code when and as
27 // the GNU GPL requires distribution of source code and provided that you do
28 // not modify the Dissection-fortran interface.
29 //
30 // Note that people who make modified versions of Dissection are not obligated
31 // to grant this special exception for their modified versions; it is their
32 // choice whether to do so. The GNU General Public License gives permission to
33 // release a modified version without this exception; this exception also makes
34 // it possible to release a modified version which carries forward this
35 // exception. If you modify the Dissection-fortran interface, this exception
36 // does not apply to your modified version of Dissection, and you must remove
37 // this exception when you distribute your modified version.
38 //
39 // This exception is an additional permission under section 7 of the GNU
40 // General Public License, version 3 ("GPLv3")
41 //
42 // Dissection is distributed in the hope that it will be useful,
43 // but WITHOUT ANY WARRANTY; without even the implied warranty of
44 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 // GNU General Public License for more details.
46 //
47 // You should have received a copy of the GNU General Public License
48 // along with Dissection. If not, see <http://www.gnu.org/licenses/>.
49 //
50
51 #include <sys/types.h>
52 #include <stdint.h>
53 #include <cstdio>
54 #include <cstdlib>
55
56 #ifdef DISSECTION_FORTRAN
57 #include "Interfaces/fortranAPI.h"
58 #else
59 #include "Interfaces/Dissection.hpp"
60 #endif
61 #include "Driver/DissectionSolver.hpp"
62
63 struct dissection_solver_ptr
64 {
65 int real_or_complex;
66 DissectionSolver<double> *rptr;
67 DissectionSolver<complex<double>, double> *cptr;
68 DissectionSolver<quadruple, quadruple, double, double> *rqtr;
69 DissectionSolver<double, double, quadruple, quadruple> *rqtr_fwbw;
70 DissectionSolver<complex<quadruple>, quadruple> *cqtr;
71 DissectionSolver<complex<double>, double,
72 complex<quadruple>, quadruple> *cqtr_fwbw;
73 FILE *fp;
74 bool verbose;
75 int called;
76 #ifdef BLAS_MKL
77 int mkl_num_threads;
78 #endif
79 int symbolic;
80 int numeric;
81 };
82
83 DISSECTION_API void DISS_VERSION(int *versn,
84 int *reles,
85 int *patch)
86 {
87 *versn = DISSECTION_VERSION;
88 *reles = DISSECTION_RELEASE;
89 *patch = DISSECTION_PATCHLEVEL;
90 }
91
92 DISSECTION_API void DISS_INIT(uint64_t &dslv_,
93 const int &called,
94 const int &real_or_complex,
95 const int &nthreads,
96 const int &verbose)
97 {
98 int num_threads;
99 dissection_solver_ptr *dslv;
100 dslv_ = (uint64_t)new dissection_solver_ptr;
101 dslv = (dissection_solver_ptr *)dslv_;
102 dslv->real_or_complex = real_or_complex;
103 dslv->called = called;
104 dslv->symbolic = 0;
105 dslv->numeric = 0;
106 {
107 int pid = (int)getpid();
108 char fname[256];
109 if (verbose > 0) {
110 dslv->verbose = true;
111 }
112 else {
113 dslv->verbose = false;
114 }
115 #if 1
116 if (dslv->verbose) {
117 fprintf(stderr, "pid = %d\n", pid);
118 sprintf(fname, "dissection.%04d.%04d.log", pid, called);
119 dslv->fp = fopen(fname, "a");
120 }
121 else {
122 dslv->fp = stderr;
123 }
124 #else
125 dslv->fp = stderr;
126 #endif
127 }
128 if (dslv->verbose) {
129 fprintf(dslv->fp, "%s %d : diss_init : called = %d\n",
130 __FILE__, __LINE__, dslv->called);
131 }
132
133 // _called++; // counter for dumping matrix data to debug
134 #ifdef BLAS_MKL
135 if (getenv("MKL_NUM_THREADS")) {
136 sscanf(getenv("MKL_NUM_THREADS"), "%d", &dslv->mkl_num_threads);
137 if (dslv->verbose > 0) {
138 fprintf(dslv->fp,
139 "environmental variable MKL_NUM_THREADS = %d\n",
140 dslv->mkl_num_threads);
141 }
142 }
143 else {
144 dslv->mkl_num_threads = mkl_get_max_threads();
145 }
146 if (dslv->verbose) {
147 fprintf(dslv->fp,
148 "MKL_NUM_THREADS = %d\n", dslv->mkl_num_threads);
149 }
150 #endif
151 if (nthreads == (-1)) {
152 if (getenv("NTHREADS")) {
153 sscanf(getenv("NTHREADS"), "%d", &num_threads);
154 }
155 else {
156 num_threads = 1;
157 }
158 }
159 if (nthreads > 0) {
160 num_threads = nthreads;
161 }
162 {
163 switch(real_or_complex) {
164 case DISSECTION_REAL_MATRIX:
165 dslv->rptr = new DissectionSolver<double>(num_threads,
166 (verbose != 0 ? true : false),
167 dslv->called, dslv->fp);
168 break;
169 case DISSECTION_COMPLEX_MATRIX:
170 dslv->cptr = new DissectionSolver<complex<double>,
171 double>(num_threads,
172 (verbose != 0 ? true : false),
173 dslv->called, dslv->fp);
174 break;
175 default:
176 if (dslv->verbose) {
177 fprintf(dslv->fp, "%s %d : unknown matrix data type : %d\n",
178 __FILE__, __LINE__, dslv->real_or_complex);
179 }
180 }
181 }
182 }
183
184 DISSECTION_API void DISS_FREE(uint64_t &dslv_)
185 {
186 dissection_solver_ptr *dslv = (dissection_solver_ptr *)dslv_;
187
188 switch(dslv->real_or_complex) {
189 case DISSECTION_REAL_MATRIX:
190 delete dslv->rptr;
191 break;
192 case DISSECTION_COMPLEX_MATRIX:
193 delete dslv->cptr;
194 break;
195 default:
196 if (dslv->verbose) {
197 fprintf(dslv->fp, "%s %d : unknown matrix data type : %d\n",
198 __FILE__, __LINE__, dslv->real_or_complex);
199 }
200 }
201
202 delete dslv;
203 dslv_ = (uint64_t)NULL;
204 // _called--;
205 // if ((_called == 0) && (dslv->fp != stderr)) {
206 if (dslv->fp != stderr) {
207 fclose(dslv->fp);
208 }
209 #ifdef VECLIB
210 unsetenv("VECLIB_MAXIMUM_THREADS");
211 #endif
212
213 }
214
215 DISSECTION_API void DISS_NUMERIC_FREE(uint64_t &dslv_)
216 {
217 dissection_solver_ptr *dslv = (dissection_solver_ptr *)dslv_;
218 switch(dslv->real_or_complex) {
219 case DISSECTION_REAL_MATRIX:
220 dslv->rptr->NumericFree();
221 break;
222 case DISSECTION_COMPLEX_MATRIX:
223 dslv->cptr->NumericFree();
224 break;
225 default:
226 if (dslv->verbose) {
227 fprintf(dslv->fp, "%s %d unknown matrix data type : %d\n",
228 __FILE__, __LINE__, dslv->real_or_complex);
229 }
230 }
231 }
232
233 DISSECTION_API void DISS_S_FACT(uint64_t &dslv_,
234 const int &dim,
235 const int *ptRows,
236 const int *indCols,
237 const int &sym,
238 const int &decomposer)
239 {
240 // sym = 1 : symmetric with upper
241 // = 0 : unsymmetric,
242 // = 3 : symmetric with lower
243 // decomposer = 0 : SCOTCH
244 // = 1 : METIS
245 // = 2 : TRIDAIG without nested bisection
246 dissection_solver_ptr *dslv = (dissection_solver_ptr *)dslv_;
247 // int num_levels;
248
249 switch(dslv->real_or_complex) {
250 case DISSECTION_REAL_MATRIX:
251 dslv->rptr->SymbolicFact(dim, (int *)ptRows, (int *)indCols,
252 (bool)(sym % 2 == 1), (bool)((sym / 2) == 0),
253 (bool)((sym / 4) == 1),
254 decomposer); // using default parameter
255 break;
256 case DISSECTION_COMPLEX_MATRIX:
257 dslv->cptr->SymbolicFact(dim, (int *)ptRows, (int *)indCols,
258 (bool)(sym % 2 == 1), (bool)((sym / 2) == 0),
259 (bool)((sym / 4) == 1),
260 decomposer); // using default parameter
261 break;
262 default:
263 if (dslv->verbose) {
264 fprintf(dslv->fp, "%s %d unknown matrix data type : %d\n",
265 __FILE__, __LINE__, dslv->real_or_complex);
266 }
267 }
268 if (dslv->verbose) {
269 fprintf(dslv->fp, "%s:%d Dissection::SymbolicFact done\n",
270 __FILE__, __LINE__);
271 }
272 dslv->symbolic++;
273 }
274
275 DISSECTION_API void DISS_N_FACT(uint64_t &dslv_,
276 const double *coefs,
277 const int &scaling,
278 const double &eps_pivot,
279 const int &indefinite_flag)
280 {
281 // scaling = 0 : without scaling
282 // 1 : 1/sqrt(a_ii) or 1/sqrt(max|a_ij|)
283 // 2 : 1/sqrt(a_ii) or Schur complement corresponding to diagonal
284 // kernel_detection_all (for KKT type)
285 // eps_pivot = 1.0e-2 : threshold of pivot, ratio of contiguous diagonal
286 // entries with absolute value
287 // indefinite_flag = 1 : indefinite -> kernel_detection_all = false
288 // indefinite_flag = 0 : semi-definite -> kernel_detection_all = true
289
290 bool kernel_detection_all = indefinite_flag == 0 ? true : false;
291
292 // FILE *fout;
293 dissection_solver_ptr *dslv = (dissection_solver_ptr *)dslv_;
294
295 #ifdef BLAS_MKL
296 mkl_set_num_threads(1);
297 #endif
298 #ifdef VECLIB
299 setenv("VECLIB_MAXIMUM_THREADS", "1", true);
300 #endif
301 // dslv->NumericFree(); // for debugging : 20 Nov.2013
302 switch(dslv->real_or_complex) {
303 case DISSECTION_REAL_MATRIX:
304 dslv->rptr->NumericFact(dslv->numeric,
305 (double *)coefs, scaling,
306 eps_pivot,
307 kernel_detection_all);
308 if (dslv->rptr->getFactorized() == false) {
309 dslv->rptr->SaveMMMatrix(dslv->called, coefs);
310 }
311 break;
312 case DISSECTION_COMPLEX_MATRIX:
313 dslv->cptr->NumericFact(dslv->numeric,
314 (complex<double> *)coefs, scaling,
315 eps_pivot,
316 kernel_detection_all);
317 break;
318 }
319 #ifdef BLAS_MKL
320 mkl_set_num_threads(dslv->mkl_num_threads);
321 #endif
322 if (dslv->verbose) {
323 fprintf(dslv->fp,
324 "%s %d : Dissection::NumericFact done : %d\n",
325 __FILE__, __LINE__, dslv->numeric);
326 }
327 dslv->numeric++;
328 }
329
330 DISSECTION_API void DISS_GET_COLORS(uint64_t &dslv_,
331 int *n)
332 {
333 dissection_solver_ptr *dslv = (dissection_solver_ptr *)dslv_;
334 switch(dslv->real_or_complex) {
335 case DISSECTION_REAL_MATRIX:
336 *n = dslv->rptr->GetMaxColors();
337 break;
338 case DISSECTION_COMPLEX_MATRIX:
339 *n = dslv->cptr->GetMaxColors();
340 break;
341 default:
342 if (dslv->verbose) {
343 fprintf(dslv->fp, "%s %d unknown matrix data type : %d\n",
344 __FILE__, __LINE__, dslv->real_or_complex);
345 }
346 }
347 }
348
349 DISSECTION_API void DISS_GET_KERN_DIM(uint64_t &dslv_,
350 int *n0)
351 {
352 dissection_solver_ptr *dslv = (dissection_solver_ptr *)dslv_;
353
354 switch(dslv->real_or_complex) {
355 case DISSECTION_REAL_MATRIX:
356 *n0 = dslv->rptr->kern_dimension();
357 break;
358 case DISSECTION_COMPLEX_MATRIX:
359 if (dslv->verbose) {
360 fprintf(dslv->fp,
361 "%s %d diss_get_kern_dim() for complex is not yet implemented\n",
362 __FILE__, __LINE__);
363 }
364 *n0 = 0;
365 break;
366 default:
367 if (dslv->verbose) {
368 fprintf(dslv->fp, "%s %d unknown matrix data type : %d\n",
369 __FILE__, __LINE__, dslv->real_or_complex);
370 }
371 }
372 }
373
374 DISSECTION_API void DISS_GET_NULLPIVOTS(uint64_t &dslv_,
375 int *pivots)
376 {
377 dissection_solver_ptr *dslv = (dissection_solver_ptr *)dslv_;
378 switch(dslv->real_or_complex) {
379 case DISSECTION_REAL_MATRIX:
380 dslv->rptr->GetNullPivotIndices(pivots);
381 break;
382 case DISSECTION_COMPLEX_MATRIX:
383 dslv->cptr->GetNullPivotIndices(pivots);
384 break;
385 default:
386 if (dslv->verbose) {
387 fprintf(dslv->fp, "%s %d unknown matrix data type : %d\n",
388 __FILE__, __LINE__, dslv->real_or_complex);
389 }
390 }
391 }
392
393 DISSECTION_API void DISS_GET_SMALLPIVOTS(uint64_t &dslv_,
394 const int &n,
395 int *pivots)
396 {
397 dissection_solver_ptr *dslv = (dissection_solver_ptr *)dslv_;
398 switch(dslv->real_or_complex) {
399 case DISSECTION_REAL_MATRIX:
400 dslv->rptr->GetSmallestPivotIndices(n, pivots);
401 break;
402 case DISSECTION_COMPLEX_MATRIX:
403 dslv->cptr->GetSmallestPivotIndices(n, pivots);
404 break;
405 default:
406 if (dslv->verbose) {
407 fprintf(dslv->fp, "%s %d unknown matrix data type : %d\n",
408 __FILE__, __LINE__, dslv->real_or_complex);
409 }
410 }
411 }
412
413 DISSECTION_API void DISS_GET_KERN_VECS(uint64_t &dslv_,
414 double *vec)
415 {
416 dissection_solver_ptr *dslv = (dissection_solver_ptr *)dslv_;
417 switch(dslv->real_or_complex) {
418 case DISSECTION_REAL_MATRIX:
419 dslv->rptr->GetKernelVectors(vec);
420 break;
421 case DISSECTION_COMPLEX_MATRIX:
422 if (dslv->verbose) {
423 fprintf(dslv->fp,
424 "%s %d diss_get_kern_vecs() for complex is not yet implemented\n",
425 __FILE__, __LINE__);
426 }
427 break;
428 default:
429 if (dslv->verbose) {
430 fprintf(dslv->fp, "%s %d unknown matrix data type : %d\n",
431 __FILE__, __LINE__, dslv->real_or_complex);
432 }
433 }
434 }
435
436 DISSECTION_API void DISS_GET_KERNT_VECS(uint64_t &dslv_,
437 double *vec)
438 {
439 dissection_solver_ptr *dslv = (dissection_solver_ptr *)dslv_;
440 switch(dslv->real_or_complex) {
441 case DISSECTION_REAL_MATRIX:
442 dslv->rptr->GetTransKernelVectors(vec);
443 break;
444 case DISSECTION_COMPLEX_MATRIX:
445 if (dslv->verbose) {
446 fprintf(dslv->fp,
447 "%s %d diss_get_kern_vecs() for complex is not yet implemented\n",
448 __FILE__, __LINE__);
449 }
450 break;
451 default:
452 if (dslv->verbose) {
453 fprintf(dslv->fp, "%s %d unknown matrix data type : %d\n",
454 __FILE__, __LINE__, dslv->real_or_complex);
455 }
456 }
457 }
458
459 DISSECTION_API void DISS_PROJECT(uint64_t &dslv_,
460 double *x)
461 {
462 dissection_solver_ptr *dslv = (dissection_solver_ptr *)dslv_;
463 int n0;
464 switch(dslv->real_or_complex) {
465 case DISSECTION_REAL_MATRIX:
466 n0 = dslv->rptr->kern_dimension();
467 if (n0 > 0) {
468 dslv->rptr->ProjectionImageSingle(x);
469 }
470 break;
471 case DISSECTION_COMPLEX_MATRIX:
472 if (dslv->verbose) {
473 fprintf(dslv->fp,
474 "%s %d diss_project() for complex is not yet implemented\n",
475 __FILE__, __LINE__);
476 }
477 break;
478 default:
479 if (dslv->verbose) {
480 fprintf(dslv->fp, "%s %d unknown matrix data type : %d\n",
481 __FILE__, __LINE__, dslv->real_or_complex);
482 }
483 }
484 }
485
486 DISSECTION_API void DISS_SOLVE_1(uint64_t &dslv_,
487 double *x,
488 const int &projection,
489 const int &trans)
490 {
491 const bool isProj = (bool)(projection == 1);
492 const bool isTrans = (bool)(trans == 1);
493
494 dissection_solver_ptr *dslv = (dissection_solver_ptr *)dslv_;
495 switch(dslv->real_or_complex) {
496 case DISSECTION_REAL_MATRIX:
497 dslv->rptr->SolveSingle(x, isProj, isTrans, true);
498 break;
499 case DISSECTION_COMPLEX_MATRIX:
500 fprintf(dslv->cptr->get_filedescriptor(),
501 "Dissection::SolveSingle : %p\n", dslv->cptr);
502 dslv->cptr->SolveSingle((complex<double> *)x, isProj, isTrans, true);
503 // isTrans
504 break;
505 default:
506 if (dslv->verbose) {
507 fprintf(dslv->fp, "%s %d unknown matrix data type : %d\n",
508 __FILE__, __LINE__, dslv->real_or_complex);
509 }
510 }
511 }
512
513 DISSECTION_API void DISS_SOLVE_N(uint64_t &dslv_,
514 double *x,
515 const int &nrhs, const int &projection,
516 const int &trans)
517 {
518 const bool isProj = (bool)(projection == 1);
519 const bool isTrans = (bool)(trans == 1);
520
521 dissection_solver_ptr *dslv = (dissection_solver_ptr *)dslv_;
522 switch(dslv->real_or_complex) {
523 case DISSECTION_REAL_MATRIX:
524 dslv->rptr->SolveMulti(x, nrhs, isProj, isTrans, true);
525 break;
526 case DISSECTION_COMPLEX_MATRIX:
527 dslv->cptr->SolveMulti((complex<double> *)x, nrhs, isProj, isTrans, true);
528 break;
529 default:
530 if (dslv->verbose) {
531 fprintf(dslv->fp, "%s %d unknown matrix data type : %d\n",
532 __FILE__, __LINE__, dslv->real_or_complex);
533 }
534 }
535 }
536
537 DISSECTION_API void DISS_MATRIX_PRODUCT(uint64_t &dslv_,
538 const double* x, double* y)
539 {
540 dissection_solver_ptr *dslv = (dissection_solver_ptr *)dslv_;
541 switch(dslv->real_or_complex) {
542 case DISSECTION_REAL_MATRIX:
543 dslv->rptr->SpMV(x, y);
544 break;
545 case DISSECTION_COMPLEX_MATRIX:
546 dslv->cptr->SpMV((complex<double> *)x, (complex<double> *)y);
547 break;
548 default:
549 if (dslv->verbose) {
550 fprintf(dslv->fp, "%s %d unknown matrix data type : %d\n",
551 __FILE__, __LINE__, dslv->real_or_complex);
552 }
553 }
554 }