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);
69 if (sol_array == NULL) v_solution=(
double *)
xmalloc(
sizeof(
double) * (this->
vec_lsize() + 1));
70 else v_solution=sol_array;
71 VecCreateMPIWithArray(PETSC_COMM_WORLD,1, this->
vec_lsize(), PETSC_DECIDE, v_solution, &solution);
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");
118 preallocate_matrix();
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)
135 if (matrix) MatDestroy(&matrix);
137 VecDestroy(&solution);
140 if (own_solution)
xfree(v_solution);
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 )
297 void LinSys_MPIAIJ::start_allocation()
299 if (status != NONE) {
303 VecCreateMPI(PETSC_COMM_WORLD, vec_ds.lsize(), PETSC_DECIDE, &(on_vec));
304 VecDuplicate(on_vec,&(off_vec));
309 void LinSys_MPIAIJ::preallocate_matrix()
311 ASSERT(status == ALLOCATE,
"Linear system has to be in ALLOCATE status.");
313 PetscScalar *on_array, *off_array;
318 VecAssemblyBegin(on_vec);
319 VecAssemblyBegin(off_vec);
321 on_nz=(
int *)
xmalloc( 2 * vec_ds.lsize() *
sizeof(int));
322 off_nz=on_nz + vec_ds.lsize();
324 VecAssemblyEnd(on_vec);
325 VecAssemblyEnd(off_vec);
327 VecGetArray(on_vec,&on_array);
328 VecGetArray(off_vec,&off_array);
330 for(i=0; i<vec_ds.lsize(); i++) {
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);
338 VecDestroy(&off_vec);
341 MatCreateAIJ(PETSC_COMM_WORLD, vec_ds.lsize(), vec_ds.lsize(), PETSC_DETERMINE, PETSC_DETERMINE,
342 0, on_nz, 0, off_nz, &matrix);
344 if (symmetric) MatSetOption(matrix, MAT_SYMMETRIC, PETSC_TRUE);
345 MatSetOption(matrix, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
351 void LinSys_MPIAIJ::preallocate_values(
int nrow,
int *rows,
int ncol,
int *cols)
355 for (i=0; i<nrow; i++) {
357 for(j=0; j<ncol; j++) {
359 if (vec_ds.get_proc(row) == vec_ds.get_proc(col))
360 VecSetValue(on_vec,row,1.0,ADD_VALUES);
362 VecSetValue(off_vec,row,1.0,ADD_VALUES);
367 void LinSys_MPIAIJ::view_local_matrix()
370 xprintf(
Msg,
"Printing of local matrix is not supported yet for MPIAIJ matrix. \n");
375 LinSys_MPIAIJ:: ~LinSys_MPIAIJ()
377 if (status == ALLOCATE) {
379 VecDestroy(&off_vec);
385 LinSys_MATIS::LinSys_MATIS(boost::shared_ptr<LocalToGlobalMap> global_row_4_sub_row,
double *sol_array)
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];
410 void LinSys_MATIS::start_allocation()
412 ASSERT(0,
"Not implemented");
440 void LinSys_MATIS::preallocate_matrix()
442 ASSERT(status == ALLOCATE,
"Linear system has to be in ALLOCATE status.");
449 MatSeqAIJSetPreallocation(local_matrix, 0, subdomain_nz);
451 if (symmetric) MatSetOption(matrix, MAT_SYMMETRIC, PETSC_TRUE);
452 MatSetOption(matrix, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
454 delete[] subdomain_nz;
457 void LinSys_MATIS::preallocate_values(
int nrow,
int *rows,
int ncol,
int *cols)
459 int i,row, n_loc_rows;
462 if (loc_rows_size < nrow) {
465 loc_rows=
new int[loc_rows_size];
470 err = ISGlobalToLocalMappingApply(map_local_to_global, IS_GTOLM_DROP, nrow, rows, &n_loc_rows, 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++) {
490 subdomain_nz[row] = subdomain_nz[row] + ncol;
494 void LinSys_MATIS::view_local_matrix()
506 err = MatView(local_matrix,PETSC_VIEWER_STDOUT_SELF);
507 ASSERT(err == 0,
"Error in MatView.");
528 LinSys_MATIS:: ~LinSys_MATIS()
533 err = ISLocalToGlobalMappingDestroy(&map_local_to_global);
534 ASSERT(err == 0,
"Error in ISLocalToGlobalMappingDestroy.");
539 delete[] subdomain_indices;
540 if (status == ALLOCATE) {
549 #ifdef HAVE_ATLAS_ONLY_LAPACK
556 void dgeqrf_(
int m,
int n,
double **A,
double *TAU,
double *work,
int lwork,
int info)
virtual void start_insert_assembly()
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)
static Input::Type::AbstractRecord input_type
void * xmalloc(size_t size)
Memory allocation with checking.
Abstract linear system class.
#define MPI_Barrier(comm)
FILE * xfopen(const std::string &fname, const char *mode)