39 namespace it = Input::Type;
43 "Relative residual tolerance (to initial error).")
45 "Maximum number of outer iterations of the linear solver.");
59 vec_ds(vec_lsize, PETSC_COMM_WORLD),
61 positive_definite(false),
66 VecCreateMPIWithArray(PETSC_COMM_WORLD,1, this->
vec_lsize(), PETSC_DECIDE,
v_rhs, &
rhs);
84 finalize(MAT_FLUSH_ASSEMBLY);
90 ASSERT(0,
"Can not set values. Matrix is not preallocated.\n");
102 finalize(MAT_FLUSH_ASSEMBLY);
108 ASSERT(0,
"Can not set values. Matrix is not preallocated.\n");
114 void LinSys::finalize(MatAssemblyType assembly_type)
117 xprintf(
Warn,
"Finalizing linear system without setting values.\n");
120 MatAssemblyBegin(
matrix, assembly_type);
121 VecAssemblyBegin(
rhs);
122 if (assembly_type == MAT_FINAL_ASSEMBLY)
status=
DONE;
123 MatAssemblyEnd(
matrix, assembly_type);
127 void view(std::ostream output_stream,
int * output_mapping = NULL)
147 void LSView(LinSystem *ls)
152 const PetscInt *cols;
153 const PetscScalar *vals;
157 MatGetInfo(ls->A,MAT_GLOBAL_SUM,&info);
159 if (ls->ds->myp() == 0) {
160 f=
xfopen(
"matrix.dat",
"w");
161 xfprintf(f,
"%d %d 0.0\n",ls->size, ls->size);
166 for(n=0;n<ls->ds->np();n++) {
168 if (n==ls->ds->myp()) {
169 f=
xfopen(
"matrix.dat",
"a");
170 for(i=ls->ds->begin(); i<ls->ds->end();i++) {
171 MatGetRow(ls->A,i,&ncols,&cols,&vals);
173 xfprintf(f,
"%d %d %f\n",ls->old_4_new[i]+1,ls->old_4_new[cols[j]]+1,vals[j]);
174 MatRestoreRow(ls->A,i,&ncols,&cols,&vals);
180 if (ls->ds->myp() == 0) {
182 xfprintf(f,
"yyy = zeros(%d,2)\n yyy=[\n",ls->size);
186 for(n=0; n<ls->ds->np(); n++) {
188 if (n==ls->ds->myp()) {
190 for(i=ls->ds->begin(); i<ls->ds->end(); i++) {
191 xfprintf(f,
"%d %f\n",ls->old_4_new[i]+1,ls->vb[i-ls->ds->begin()]);
197 if (ls->ds->myp() == 0) {
199 xfprintf(f,
"];\n xxx=sortrows(yyy); Vec=xxx(:,2);\n");
207 void MyVecView(Vec v,
int *order,
const char* fname) {
221 xfprintf(f,
"yyy = zeros(%d,2)\n yyy=[\n",ds.size());
225 for(n=0; n<ds.np(); n++) {
228 VecGetArray(v,&array);
230 for(i=ds.begin(); i<ds.end(); i++) {
231 xfprintf(f,
"%d %f\n",order[i]+1,array[i-ds.begin()]);
233 VecRestoreArray(v,&array);
240 xfprintf(f,
"];\n xxx=sortrows(yyy); Vec=xxx(:,2);\n");
254 void LSSetCSR( LinSystem *mtx )
258 const PetscInt *cols;
259 const PetscScalar *vals;
262 MatGetInfo(mtx->A,MAT_LOCAL,&info);
263 nnz=(int)(info.nz_used)+1;
265 if (mtx->i == NULL) mtx->i=(
int *)
xmalloc( (mtx->size+1)*
sizeof(int) );
266 if (mtx->j == NULL) mtx->j=(
int *)
xmalloc( nnz*
sizeof(
int) );
267 if (mtx->a == NULL) mtx->a=(
double *)
xmalloc( nnz*
sizeof(
double) );
268 DBGMSG(
"mtx size: %d nnz: %d\n",mtx->size,nnz);
271 for( row=0; row< mtx->size; row++ ) {
273 MatGetRow(mtx->A,row,&nnz_loc,&cols,&vals);
274 DBGMSG(
"CSR row: %d nnz_loc %d\n",row,nnz_loc);
275 for(i=0; i<nnz_loc; i++) {
276 ASSERT(pos<nnz,
"More nonzeroes then allocated! row: %d entry: %d\n",row,i);
281 MatRestoreRow(mtx->A,row,&nnz_loc,&cols,&vals);
286 void LSFreeCSR( LinSystem *mtx )
313 PetscScalar *on_array, *off_array;
327 VecGetArray(
on_vec,&on_array);
328 VecGetArray(
off_vec,&off_array);
331 on_nz[i]=min((
unsigned int)(on_array[i]+0.1),
vec_ds.
lsize());
332 off_nz[i]=(int)(off_array[i]+0.1);
335 VecRestoreArray(
on_vec,&on_array);
336 VecRestoreArray(
off_vec,&off_array);
342 0, on_nz, 0, off_nz, &
matrix);
345 MatSetOption(
matrix, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
355 for (i=0; i<nrow; i++) {
357 for(j=0; j<ncol; j++) {
360 VecSetValue(
on_vec,row,1.0,ADD_VALUES);
362 VecSetValue(
off_vec,row,1.0,ADD_VALUES);
370 xprintf(
Msg,
"Printing of local matrix is not supported yet for MPIAIJ matrix. \n");
386 :
LinSys(global_row_4_sub_row->get_distr()->lsize(), sol_array), lg_map(global_row_4_sub_row)
394 if (lg_map->get_distr()->size() > numeric_limits<PetscInt>::max())
xprintf(
Err,
"Index range doesn't fit into signed int!");
395 err = ISLocalToGlobalMappingCreate(PETSC_COMM_WORLD,
397 (
const PetscInt*)(&(lg_map->get_map_vector()[0])),
398 PETSC_COPY_VALUES, &map_local_to_global);
400 ASSERT(err == 0,
"Error in ISLocalToGlobalMappingCreate.");
404 loc_rows =
new int[loc_rows_size];
412 ASSERT(0,
"Not implemented");
452 MatSetOption(
matrix, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
459 int i,row, n_loc_rows;
471 ASSERT(err == 0,
"Error in ISGlobalToLocalMappingApply.");
472 ASSERT(nrow == n_loc_rows,
"Not all global indices translated to local indices.");
488 for (i=0; i<n_loc_rows; i++) {
507 ASSERT(err == 0,
"Error in MatView.");
534 ASSERT(err == 0,
"Error in ISLocalToGlobalMappingDestroy.");
549 #ifdef HAVE_ATLAS_ONLY_LAPACK
556 void dgeqrf_(
int m,
int n,
double **A,
double *TAU,
double *work,
int lwork,
int info)
bool own_solution
Indicates if the solution array has been allocated by this class.
Vec solution
PETSc vector constructed with vb array.
int * subdomain_nz
For counting non-zero enteries of local subdomain.
unsigned int get_proc(unsigned int idx) const
get processor of the given index
virtual void view_local_matrix()
virtual void start_insert_assembly()
Vec rhs
PETSc vector constructed with vx array.
SetValuesMode status
Set value status of the linear system.
virtual void start_add_assembly()
int xfclose(FILE *stream)
FCLOSE WITH ERROR HANDLING.
Wrappers for linear systems based on MPIAIJ and MATIS format.
Mat local_matrix
local matrix of subdomain (used in LinSys_MATIS)
int xfprintf(FILE *out, const char *fmt,...)
FPRINTF WITH ERROR HANDLING.
LinSys(const Distribution *rows_ds)
Distribution vec_ds
Distribution of continuous blocks of system rows among the processors.
double * v_rhs
RHS vector.
virtual void start_allocation()
ISLocalToGlobalMapping map_local_to_global
PETSC mapping form local indexes of subdomain to global indexes.
static Input::Type::AbstractRecord input_type
Vec off_vec
Vectors for counting non-zero entries.
virtual void preallocate_matrix()
bool symmetric
Flag for the symmetric system.
void * xmalloc(size_t size)
Memory allocation with checking.
virtual void preallocate_values(int nrow, int *rows, int ncol, int *cols)
LinSysType type
Particular type of the linear system.
virtual void start_allocation()
virtual void preallocate_matrix()=0
Abstract linear system class.
virtual void preallocate_matrix()
int * subdomain_indices
Remember indices which created mapping.
double * v_solution
Vector of solution.
#define MPI_Barrier(comm)
virtual void preallocate_values(int nrow, int *rows, int ncol, int *cols)
virtual void view_local_matrix()
FILE * xfopen(const std::string &fname, const char *mode)
Mat matrix
Petsc matrix of the problem.
LinSys_MATIS(unsigned int lsize, int sz, int *global_row_4_sub_row, double *sol_array=NULL)
unsigned int lsize(int proc) const
get local size