49 positive_definite(
false),
53 v_rhs=(
double *) xmalloc(
sizeof(
double) * (this->
vec_lsize() + 1) );
54 VecCreateMPIWithArray(PETSC_COMM_WORLD,1, this->
vec_lsize(), PETSC_DECIDE, v_rhs, &rhs);
57 if (sol_array == NULL) v_solution=(
double *)xmalloc(
sizeof(
double) * (this->
vec_lsize() + 1));
58 else v_solution=sol_array;
59 VecCreateMPIWithArray(PETSC_COMM_WORLD,1, this->
vec_lsize(), PETSC_DECIDE, v_solution, &solution);
72 finalize(MAT_FLUSH_ASSEMBLY);
78 OLD_ASSERT(0,
"Can not set values. Matrix is not preallocated.\n");
90 finalize(MAT_FLUSH_ASSEMBLY);
96 OLD_ASSERT(0,
"Can not set values. Matrix is not preallocated.\n");
102 void LinSys::finalize(MatAssemblyType assembly_type)
105 WarningOut() <<
"Finalizing linear system without setting values.\n";
106 preallocate_matrix();
108 MatAssemblyBegin(matrix, assembly_type);
109 VecAssemblyBegin(rhs);
110 if (assembly_type == MAT_FINAL_ASSEMBLY) status=
DONE;
111 MatAssemblyEnd(matrix, assembly_type);
115 void view(std::ostream output_stream,
int * output_mapping = NULL)
123 if (matrix) MatDestroy(&matrix);
125 VecDestroy(&solution);
128 if (own_solution) xfree(v_solution);
135 void LSView(LinSystem *ls)
140 const PetscInt *cols;
141 const PetscScalar *vals;
145 MatGetInfo(ls->A,MAT_GLOBAL_SUM,&info);
147 if (ls->ds->myp() == 0) {
148 f=
xfopen(
"matrix.dat",
"w");
149 xfprintf(f,
"%d %d 0.0\n",ls->size, ls->size);
154 for(n=0;n<ls->ds->np();n++) {
156 if (n==ls->ds->myp()) {
157 f=
xfopen(
"matrix.dat",
"a");
158 for(i=ls->ds->begin(); i<ls->ds->end();i++) {
159 MatGetRow(ls->A,i,&ncols,&cols,&vals);
161 xfprintf(f,
"%d %d %f\n",ls->old_4_new[i]+1,ls->old_4_new[cols[j]]+1,vals[j]);
162 MatRestoreRow(ls->A,i,&ncols,&cols,&vals);
168 if (ls->ds->myp() == 0) {
170 xfprintf(f,
"yyy = zeros(%d,2)\n yyy=[\n",ls->size);
174 for(n=0; n<ls->ds->np(); n++) {
176 if (n==ls->ds->myp()) {
178 for(i=ls->ds->begin(); i<ls->ds->end(); i++) {
179 xfprintf(f,
"%d %f\n",ls->old_4_new[i]+1,ls->vb[i-ls->ds->begin()]);
185 if (ls->ds->myp() == 0) {
187 xfprintf(f,
"];\n xxx=sortrows(yyy); Vec=xxx(:,2);\n");
195 void MyVecView(Vec v,
int *order,
const char* fname) {
213 for(n=0; n<ds.
np(); n++) {
216 VecGetArray(v,&array);
218 for(i=ds.
begin(); i<ds.
end(); i++) {
221 VecRestoreArray(v,&array);
228 xfprintf(f,
"];\n xxx=sortrows(yyy); Vec=xxx(:,2);\n");
242 void LSSetCSR( LinSystem *mtx )
246 const PetscInt *cols;
247 const PetscScalar *vals;
250 MatGetInfo(mtx->A,MAT_LOCAL,&info);
251 nnz=(int)(info.nz_used)+1;
253 if (mtx->i == NULL) mtx->i=(
int *)xmalloc( (mtx->size+1)*
sizeof(int) );
254 if (mtx->j == NULL) mtx->j=(
int *)xmalloc( nnz*
sizeof(
int) );
255 if (mtx->a == NULL) mtx->a=(
double *)xmalloc( nnz*
sizeof(
double) );
256 DebugOut().fmt(
"mtx size: {} nnz: {}\n", mtx->size, nnz);
259 for( row=0; row< mtx->size; row++ ) {
261 MatGetRow(mtx->A,row,&nnz_loc,&cols,&vals);
262 DebugOut().fmt(
"CSR row: {} nnz_loc {}\n", row, nnz_loc);
263 for(i=0; i<nnz_loc; i++) {
264 OLD_ASSERT(pos<nnz,
"More nonzeroes then allocated! row: %d entry: %d\n",row,i);
269 MatRestoreRow(mtx->A,row,&nnz_loc,&cols,&vals);
274 void LSFreeCSR( LinSystem *mtx )
285 void LinSys_MPIAIJ::start_allocation()
287 if (status !=
NONE) {
291 VecCreateMPI(PETSC_COMM_WORLD, vec_ds.lsize(), PETSC_DECIDE, &(on_vec));
292 VecDuplicate(on_vec,&(off_vec));
297 void LinSys_MPIAIJ::preallocate_matrix()
301 PetscScalar *on_array, *off_array;
306 VecAssemblyBegin(on_vec);
307 VecAssemblyBegin(off_vec);
309 on_nz=(
int *)xmalloc( 2 * vec_ds.lsize() *
sizeof(int));
310 off_nz=on_nz + vec_ds.lsize();
312 VecAssemblyEnd(on_vec);
313 VecAssemblyEnd(off_vec);
315 VecGetArray(on_vec,&on_array);
316 VecGetArray(off_vec,&off_array);
318 for(i=0; i<vec_ds.lsize(); i++) {
319 on_nz[i]=min((
unsigned int)(on_array[i]+0.1),vec_ds.lsize());
320 off_nz[i]=(int)(off_array[i]+0.1);
323 VecRestoreArray(on_vec,&on_array);
324 VecRestoreArray(off_vec,&off_array);
326 VecDestroy(&off_vec);
329 MatCreateAIJ(PETSC_COMM_WORLD, vec_ds.lsize(), vec_ds.lsize(), PETSC_DETERMINE, PETSC_DETERMINE,
330 0, on_nz, 0, off_nz, &matrix);
332 if (symmetric) MatSetOption(matrix, MAT_SYMMETRIC, PETSC_TRUE);
333 MatSetOption(matrix, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
339 void LinSys_MPIAIJ::preallocate_values(
int nrow,
int *rows,
int ncol,
int *cols)
343 for (i=0; i<nrow; i++) {
345 for(j=0; j<ncol; j++) {
347 if (vec_ds.get_proc(row) == vec_ds.get_proc(col))
348 VecSetValue(on_vec,row,1.0,ADD_VALUES);
350 VecSetValue(off_vec,row,1.0,ADD_VALUES);
355 void LinSys_MPIAIJ::view_local_matrix()
358 MessageOut() <<
"Printing of local matrix is not supported yet for MPIAIJ matrix. \n";
363 LinSys_MPIAIJ:: ~LinSys_MPIAIJ()
367 VecDestroy(&off_vec);
373 LinSys_MATIS::LinSys_MATIS(std::shared_ptr<LocalToGlobalMap> global_row_4_sub_row,
double *sol_array)
374 :
LinSys(global_row_4_sub_row->get_distr()->lsize(), sol_array), lg_map(global_row_4_sub_row)
382 if (lg_map->get_distr()->size() > numeric_limits<PetscInt>::max())
xprintf(
Err,
"Index range doesn't fit into signed int!");
383 err = ISLocalToGlobalMappingCreate(PETSC_COMM_WORLD,
385 (
const PetscInt*)(&(lg_map->get_map_vector()[0])),
386 PETSC_COPY_VALUES, &map_local_to_global);
388 OLD_ASSERT(err == 0,
"Error in ISLocalToGlobalMappingCreate.");
392 loc_rows =
new int[loc_rows_size];
398 void LinSys_MATIS::start_allocation()
428 void LinSys_MATIS::preallocate_matrix()
437 MatSeqAIJSetPreallocation(local_matrix, 0, subdomain_nz);
439 if (symmetric) MatSetOption(matrix, MAT_SYMMETRIC, PETSC_TRUE);
440 MatSetOption(matrix, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
442 delete[] subdomain_nz;
445 void LinSys_MATIS::preallocate_values(
int nrow,
int *rows,
int ncol,
int *cols)
447 int i,row, n_loc_rows;
450 if (loc_rows_size < nrow) {
453 loc_rows=
new int[loc_rows_size];
458 err = ISGlobalToLocalMappingApply(map_local_to_global, IS_GTOLM_DROP, nrow, rows, &n_loc_rows, loc_rows);
459 OLD_ASSERT(err == 0,
"Error in ISGlobalToLocalMappingApply.");
460 OLD_ASSERT(nrow == n_loc_rows,
"Not all global indices translated to local indices.");
476 for (i=0; i<n_loc_rows; i++) {
478 subdomain_nz[row] = subdomain_nz[row] + ncol;
482 void LinSys_MATIS::view_local_matrix()
494 err = MatView(local_matrix,PETSC_VIEWER_STDOUT_SELF);
516 LinSys_MATIS:: ~LinSys_MATIS()
521 err = ISLocalToGlobalMappingDestroy(&map_local_to_global);
522 OLD_ASSERT(err == 0,
"Error in ISLocalToGlobalMappingDestroy.");
527 delete[] subdomain_indices;
537 #ifdef HAVE_ATLAS_ONLY_LAPACK 544 void dgeqrf_(
int m,
int n,
double **A,
double *TAU,
double *work,
int lwork,
int info)
unsigned int size() const
get global size
virtual void start_insert_assembly()
#define MessageOut()
Macro defining 'message' record of log.
virtual void start_add_assembly()
int xfclose(FILE *stream)
FCLOSE WITH ERROR HANDLING.
Wrappers for linear systems based on MPIAIJ and MATIS format.
int xfprintf(FILE *out, const char *fmt,...)
FPRINTF WITH ERROR HANDLING.
LinSys(const Distribution *rows_ds)
unsigned int begin(int proc) const
get starting local index
unsigned int np() const
get num of processors
unsigned int myp() const
get my processor
unsigned int end(int proc) const
get last local index +1
#define WarningOut()
Macro defining 'warning' record of log.
static Input::Type::Abstract & get_input_type()
#define MPI_Barrier(comm)
#define DebugOut()
Macro defining 'debug' record of log.
FILE * xfopen(const std::string &fname, const char *mode)