32 inline double mumps_to_Scalar(
double x)
37 inline std::complex<double> mumps_to_Scalar(ZMUMPS_COMPLEX x)
39 return std::complex<double>(x.r, x.i);
42 double inline real(
double x)
47 double inline imag(
double x)
52 double inline real(ZMUMPS_COMPLEX x)
54 return mumps_to_Scalar(x).real();
57 double inline imag(ZMUMPS_COMPLEX x)
59 return mumps_to_Scalar(x).imag();
65 extern void dmumps_c(DMUMPS_STRUC_C *mumps_param_ptr);
66 extern void zmumps_c(ZMUMPS_STRUC_C *mumps_param_ptr);
70 #define USE_COMM_WORLD -987654
72 inline ZMUMPS_COMPLEX& operator +=(ZMUMPS_COMPLEX &a,
double b)
78 inline ZMUMPS_COMPLEX& operator +=(ZMUMPS_COMPLEX &a, std::complex<double> b)
85 inline ZMUMPS_COMPLEX& operator +=(ZMUMPS_COMPLEX &a, ZMUMPS_COMPLEX b)
92 inline ZMUMPS_COMPLEX& operator *=(ZMUMPS_COMPLEX &a, std::complex<double> b)
94 std::complex<double> a_c = std::complex<double>(a.r, a.i);
95 std::complex<double> result = a_c * b;
101 inline void mumps_assign_Scalar(ZMUMPS_COMPLEX & a, std::complex<double> b)
107 inline void mumps_assign_Scalar(
double & a,
double b)
112 template<
typename Scalar>
117 template<
typename Scalar>
123 template<
typename Scalar>
128 irn = malloc_with_check<MumpsMatrix<Scalar>,
int>(this->nnz,
this);
129 jcn = malloc_with_check<MumpsMatrix<Scalar>,
int>(this->nnz,
this);
130 for (
unsigned int i = 0; i < this->nnz; i++)
137 template<
typename Scalar>
142 free_with_check(irn);
143 free_with_check(jcn);
146 template<
typename Scalar>
157 return mumps_to_Scalar(Ax[mid]);
160 template<
typename Scalar>
193 #pragma omp critical (MumpsMatrix_add)
200 template<
typename Scalar>
203 bool invert_storage =
false;
208 FILE* file = fopen(filename,
"w");
212 fprintf(file,
"%%%%MatrixMarket matrix coordinate real general\n");
214 fprintf(file,
"%%%%MatrixMarket matrix coordinate complex general\n");
216 fprintf(file,
"%d %d %d\n", this->size, this->size, this->nnz);
219 this->switch_orientation();
220 for (
unsigned int j = 0; j < this->size; j++)
222 for (
int i = this->Ap[j]; i < this->Ap[j + 1]; i++)
224 Hermes::Helpers::fprint_coordinate_num(file, this->Ai[i] + 1, j + 1, mumps_to_Scalar(this->Ax[i]), number_format);
229 this->switch_orientation();
239 sparse.nzmax = this->nnz;
241 this->switch_orientation();
243 sparse.nir = this->nnz;
244 sparse.ir = this->Ai;
245 sparse.njc = this->size + 1;
246 sparse.jc = (
int *)this->Ap;
247 sparse.ndata = this->nnz;
250 dims[0] = this->size;
251 dims[1] = this->size;
253 mat_t *mat = Mat_CreateVer(filename,
"", MAT_FT_MAT5);
258 double* Ax_re =
nullptr;
259 double* Ax_im =
nullptr;
264 sparse.data = this->Ax;
265 matvar = Mat_VarCreate(var_name, MAT_C_SPARSE, MAT_T_DOUBLE, 2, dims, &sparse, MAT_F_DONT_COPY_DATA);
270 Ax_re = malloc_with_check<CSMatrix<Scalar>,
double>(this->nnz,
this);
271 Ax_im = malloc_with_check<CSMatrix<Scalar>,
double>(this->nnz,
this);
272 struct mat_complex_split_t z = { Ax_re, Ax_im };
274 for (
int i = 0; i < this->nnz; i++)
276 Ax_re[i] = ((std::complex<double>)(mumps_to_Scalar(this->Ax[i]))).real();
277 Ax_im[i] = ((std::complex<double>)(mumps_to_Scalar(this->Ax[i]))).imag();
280 matvar = Mat_VarCreate(var_name, MAT_C_SPARSE, MAT_T_DOUBLE, 2, dims, &sparse, MAT_F_DONT_COPY_DATA | MAT_F_COMPLEX);
285 Mat_VarWrite(mat, matvar, MAT_COMPRESSION_ZLIB);
289 this->switch_orientation();
290 free_with_check(Ax_re);
291 free_with_check(Ax_im);
302 FILE* file = fopen(filename,
"w");
307 this->switch_orientation();
308 for (
unsigned int j = 0; j < this->size; j++)
310 for (
int i = this->Ap[j]; i < this->Ap[j + 1]; i++)
312 Helpers::fprint_coordinate_num(file, this->Ai[i], j, mumps_to_Scalar(this->Ax[i]), number_format);
317 this->switch_orientation();
324 case EXPORT_FORMAT_BSON:
331 bson_append_int(&bw,
"size", this->size);
333 bson_append_int(&bw,
"nnz", this->nnz);
336 this->switch_orientation();
338 bson_append_start_array(&bw,
"Ap");
339 for (
unsigned int i = 0; i < this->size; i++)
340 bson_append_int(&bw,
"p", this->Ap[i]);
341 bson_append_finish_array(&bw);
343 bson_append_start_array(&bw,
"Ai");
344 for (
unsigned int i = 0; i < this->nnz; i++)
345 bson_append_int(&bw,
"i", this->Ai[i]);
346 bson_append_finish_array(&bw);
348 bson_append_start_array(&bw,
"Ax");
349 for (
unsigned int i = 0; i < this->nnz; i++)
350 bson_append_double(&bw,
"x", real(this->Ax[i]));
351 bson_append_finish_array(&bw);
355 bson_append_start_array(&bw,
"Ax-imag");
356 for (
unsigned int i = 0; i < this->nnz; i++)
357 bson_append_double(&bw,
"x-i", imag(this->Ax[i]));
358 bson_append_finish_array(&bw);
360 bson_append_finish_array(&bw);
363 this->switch_orientation();
370 fpw = fopen(filename,
"wb");
371 const char *dataw = (
const char *)bson_data(&bw);
372 fwrite(dataw, bson_size(&bw), 1, fpw);
382 template<
typename Scalar>
385 bool invert_storage =
false;
397 matfp = Mat_Open(filename, MAT_ACC_RDONLY);
402 matvar = Mat_VarRead(matfp, var_name);
406 mat_sparse_t *sparse = (mat_sparse_t *)matvar->data;
408 this->nnz = sparse->nir;
409 this->size = sparse->njc;
412 this->Ai = malloc_with_check<MumpsMatrix<Scalar>,
int>(this->nnz,
this);
414 this->irn = malloc_with_check<MumpsMatrix<Scalar>,
int>(this->nnz,
this);
415 this->jcn = malloc_with_check<MumpsMatrix<Scalar>,
int>(this->nnz,
this);
417 void* data =
nullptr;
422 std::complex<double>* complex_data = malloc_with_check<CSMatrix<Scalar>, std::complex<double> >(this->nnz,
this);
423 double* real_array = (
double*)((mat_complex_split_t*)sparse->data)->Re;
424 double* imag_array = (
double*)((mat_complex_split_t*)sparse->data)->Im;
425 for (
int i = 0; i < this->nnz; i++)
426 complex_data[i] = std::complex<double>(real_array[i], imag_array[i]);
427 data = (
void*)complex_data;
429 memcpy(this->Ax, data, this->nnz *
sizeof(Scalar));
431 free_with_check(data);
432 memcpy(this->Ap, sparse->jc, this->size *
sizeof(
int));
433 this->Ap[this->size] = this->nnz;
434 memcpy(this->Ai, sparse->ir, this->nnz *
sizeof(
int));
436 for (
unsigned int i = 0; i < this->nnz; i++)
437 this->irn[i] = this->Ai[i] + 1;
438 for (
unsigned int i = 0; i < this->size; i++)
440 for (
int j = this->Ap[i]; j < this->Ap[i + 1]; j++)
445 this->switch_orientation();
462 case EXPORT_FORMAT_BSON:
465 fpr = fopen(filename,
"rb");
468 fseek(fpr, 0, SEEK_END);
469 int size = ftell(fpr);
473 char *datar = malloc_with_check<char>(size);
474 fread(datar, size, 1, fpr);
478 bson_init_finished_data(&br, datar, 0);
482 bson_find(&it, &br,
"size");
483 this->size = bson_iterator_int(&it);
484 bson_find(&it, &br,
"nnz");
485 this->nnz = bson_iterator_int(&it);
487 this->Ap = malloc_with_check<MumpsMatrix<Scalar>,
int>(this->size + 1,
this);
488 this->Ai = malloc_with_check<MumpsMatrix<Scalar>,
int>(this->nnz,
this);
490 this->irn = malloc_with_check<MumpsMatrix<Scalar>,
int>(this->nnz,
this);
491 this->jcn = malloc_with_check<MumpsMatrix<Scalar>,
int>(this->nnz,
this);
494 bson_iterator it_coeffs;
495 bson_find(&it_coeffs, &br,
"Ap");
496 bson_iterator_subobject_init(&it_coeffs, &sub, 0);
497 bson_iterator_init(&it, &sub);
499 while (bson_iterator_next(&it))
500 this->Ap[index_coeff++] = bson_iterator_int(&it);
501 this->Ap[this->size] = this->nnz;
503 bson_find(&it_coeffs, &br,
"Ai");
504 bson_iterator_subobject_init(&it_coeffs, &sub, 0);
505 bson_iterator_init(&it, &sub);
507 while (bson_iterator_next(&it))
508 this->Ai[index_coeff++] = bson_iterator_int(&it);
510 bson_find(&it_coeffs, &br,
"Ax");
511 bson_iterator_subobject_init(&it_coeffs, &sub, 0);
512 bson_iterator_init(&it, &sub);
514 while (bson_iterator_next(&it))
515 mumps_assign_Scalar(this->Ax[index_coeff++], bson_iterator_double(&it));
519 bson_find(&it_coeffs, &br,
"Ax-imag");
520 bson_iterator_subobject_init(&it_coeffs, &sub, 0);
521 bson_iterator_init(&it, &sub);
523 while (bson_iterator_next(&it))
524 this->Ax[index_coeff++] += bson_iterator_double(&it);
527 for (
unsigned int i = 0; i < this->nnz; i++)
528 this->irn[i] = this->Ai[i] + 1;
529 for (
unsigned int i = 0; i < this->size; i++)
531 for (
int j = this->Ap[i]; j < this->Ap[i + 1]; j++)
536 this->switch_orientation();
539 free_with_check(datar);
545 template<
typename Scalar>
548 if (!vector_out_initialized)
549 vector_out = malloc_with_check<Scalar>(this->size);
551 memset(vector_out, 0,
sizeof(Scalar)* this->size);
554 for (
unsigned int i = 0; i < this->nnz; i++)
556 a = mumps_to_Scalar(Ax[i]);
557 vector_out[jcn[i] - 1] += vector_in[irn[i] - 1] * a;
561 template<
typename Scalar>
564 for (
int i = 0; i < this->nnz; i++)
570 template<
typename Scalar>
575 this->Ap = malloc_with_check<MumpsMatrix<Scalar>,
int>(this->size + 1,
this);
576 this->Ai = malloc_with_check<MumpsMatrix<Scalar>,
int>(this->nnz,
this);
578 irn = malloc_with_check<MumpsMatrix<Scalar>,
int>(this->nnz,
this);
579 jcn = malloc_with_check<MumpsMatrix<Scalar>,
int>(this->nnz,
this);
581 for (
unsigned int i = 0; i < this->size; i++)
584 for (
int j = ap[i]; j < ap[i + 1]; j++)
587 this->Ap[this->size] = ap[this->size];
588 for (
unsigned int i = 0; i < this->nnz; i++)
590 mumps_assign_Scalar(this->Ax[i], ax[i]);
596 template<
typename Scalar>
601 nmat->
nnz = this->nnz;
602 nmat->
size = this->size;
603 nmat->
Ap = malloc_with_check<MumpsMatrix<Scalar>,
int>(this->size + 1, nmat);
604 nmat->
Ai = malloc_with_check<MumpsMatrix<Scalar>,
int>(this->nnz, nmat);
606 nmat->
irn = malloc_with_check<MumpsMatrix<Scalar>,
int>(this->nnz, nmat);
607 nmat->
jcn = malloc_with_check<MumpsMatrix<Scalar>,
int>(this->nnz, nmat);
608 for (
unsigned int i = 0; i < this->nnz; i++)
610 nmat->
Ai[i] = this->Ai[i];
612 nmat->
irn[i] = irn[i];
613 nmat->
jcn[i] = jcn[i];
615 for (
unsigned int i = 0; i < this->size + 1; i++)
617 nmat->
Ap[i] = this->Ap[i];
629 #define ICNTL(I) icntl[(I)-1]
630 #define MUMPS_INFO(param, I) (param).infog[(I)-1]
631 #define INFOG(I) infog[(I)-1]
636 #define JOB_ANALYZE_FACTORIZE_SOLVE 6
637 #define JOB_FACTORIZE_SOLVE 5
640 template<
typename Scalar>
642 DirectSolver<Scalar>(m, rhs), m(m), rhs(rhs), icntl_14(init_icntl_14)
650 param.INFOG(33) = -999;
654 template<
typename Scalar>
660 template<
typename Scalar>
670 if (param.rhs !=
nullptr)
671 free_with_check(param.rhs);
686 template<
typename Scalar>
689 switch (param.INFOG(1))
700 case -9:
return false;
707 template<
typename Scalar>
723 param.comm_fortran = USE_COMM_WORLD;
726 inited = check_status();
752 param.ICNTL(14) = 100 * this->icntl_14;
765 template<
typename Scalar>
771 template<
typename Scalar>
774 assert(m !=
nullptr);
775 assert(rhs !=
nullptr);
782 if (!setup_factorization())
795 free_with_check(this->sln);
796 this->sln = malloc_with_check<MumpsSolver<Scalar>, Scalar>(m->size,
this);
797 for (
unsigned int i = 0; i < rhs->get_size(); i++)
798 this->sln[i] = mumps_to_Scalar(param.rhs[i]);
802 free_with_check(param.rhs);
805 if (icntl_14 > max_icntl_14)
826 this->time = this->accumulated();
828 free_with_check(param.rhs);
832 template<
typename Scalar>
837 int eff_fact_scheme = this->reuse_scheme;
842 switch (eff_fact_scheme)
851 param.job = JOB_ANALYZE_FACTORIZE_SOLVE;
860 param.job = JOB_FACTORIZE_SOLVE;
867 if (param.INFOG(33) != -2)
871 param.job = JOB_ANALYZE_FACTORIZE_SOLVE;
876 param.job = JOB_FACTORIZE_SOLVE;
880 param.job = JOB_SOLVE;
unsigned int nnz
Number of non-zero entries ( = Ap[size]).
General namespace for the Hermes library.
Exception interface Basically a std::exception, but with a constructor with string and with print_msg...
unsigned int size
matrix size
int * Ap
Index to Ax/Ai, where each column / row starts.
File containing common definitions, and basic global enums etc. for HermesCommon. ...
MatrixExportFormat
Format of file matrix and vector output.
bool setup_factorization()
Plain ascii file lines contains row column and value.
Vector used with MUMPS solver.
General CSC Matrix class. (can be used in umfpack, in that case use the CSCMatrix subclass...
IO exception. Internal. Exception occurs when something fails to be written to / read from the disk...
mumps_type< Scalar >::mumps_struct param
MUMPS specific structure with solver parameters.
Matrix used with MUMPS solver.
#define JOB_INIT
Job definitions according to MUMPS documentation.
existing factorization data.
File containing functionality for investigating call stack.
factorization / operator.
int * Ai
Row / Column indices of values in Ax.
pattern as the one already factorized.
Method is not overriden and should be.
General CS Matrix class. Either row- or column- specific (see subclassses).
mumps_type< Scalar >::mumps_Scalar * Ax
Matrix entries (column-wise).
virtual int get_matrix_size()
Get size of matrix.
Matrix Market which can be read by pysparse library.