48 positive_definite(
false),
53 VecCreateMPIWithArray(PETSC_COMM_WORLD,1, this->
vec_lsize(), PETSC_DECIDE, v_rhs, &rhs);
56 if (sol_array == NULL) v_solution=(
double *)
xmalloc(
sizeof(
double) * (this->
vec_lsize() + 1));
57 else v_solution=sol_array;
58 VecCreateMPIWithArray(PETSC_COMM_WORLD,1, this->
vec_lsize(), PETSC_DECIDE, v_solution, &solution);
71 finalize(MAT_FLUSH_ASSEMBLY);
77 OLD_ASSERT(0,
"Can not set values. Matrix is not preallocated.\n");
89 finalize(MAT_FLUSH_ASSEMBLY);
95 OLD_ASSERT(0,
"Can not set values. Matrix is not preallocated.\n");
101 void LinSys::finalize(MatAssemblyType assembly_type)
104 xprintf(
Warn,
"Finalizing linear system without setting values.\n");
105 preallocate_matrix();
107 MatAssemblyBegin(matrix, assembly_type);
108 VecAssemblyBegin(rhs);
109 if (assembly_type == MAT_FINAL_ASSEMBLY) status=
DONE;
110 MatAssemblyEnd(matrix, assembly_type);
114 void view(std::ostream output_stream,
int * output_mapping = NULL)
122 if (matrix) MatDestroy(&matrix);
124 VecDestroy(&solution);
127 if (own_solution)
xfree(v_solution);
134 void LSView(LinSystem *ls)
139 const PetscInt *cols;
140 const PetscScalar *vals;
144 MatGetInfo(ls->A,MAT_GLOBAL_SUM,&info);
146 if (ls->ds->myp() == 0) {
147 f=
xfopen(
"matrix.dat",
"w");
148 xfprintf(f,
"%d %d 0.0\n",ls->size, ls->size);
153 for(n=0;n<ls->ds->np();n++) {
155 if (n==ls->ds->myp()) {
156 f=
xfopen(
"matrix.dat",
"a");
157 for(i=ls->ds->begin(); i<ls->ds->end();i++) {
158 MatGetRow(ls->A,i,&ncols,&cols,&vals);
160 xfprintf(f,
"%d %d %f\n",ls->old_4_new[i]+1,ls->old_4_new[cols[j]]+1,vals[j]);
161 MatRestoreRow(ls->A,i,&ncols,&cols,&vals);
167 if (ls->ds->myp() == 0) {
169 xfprintf(f,
"yyy = zeros(%d,2)\n yyy=[\n",ls->size);
173 for(n=0; n<ls->ds->np(); n++) {
175 if (n==ls->ds->myp()) {
177 for(i=ls->ds->begin(); i<ls->ds->end(); i++) {
178 xfprintf(f,
"%d %f\n",ls->old_4_new[i]+1,ls->vb[i-ls->ds->begin()]);
184 if (ls->ds->myp() == 0) {
186 xfprintf(f,
"];\n xxx=sortrows(yyy); Vec=xxx(:,2);\n");
194 void MyVecView(Vec v,
int *order,
const char* fname) {
212 for(n=0; n<ds.
np(); n++) {
215 VecGetArray(v,&array);
217 for(i=ds.
begin(); i<ds.
end(); i++) {
220 VecRestoreArray(v,&array);
227 xfprintf(f,
"];\n xxx=sortrows(yyy); Vec=xxx(:,2);\n");
241 void LSSetCSR( LinSystem *mtx )
245 const PetscInt *cols;
246 const PetscScalar *vals;
249 MatGetInfo(mtx->A,MAT_LOCAL,&info);
250 nnz=(int)(info.nz_used)+1;
252 if (mtx->i == NULL) mtx->i=(
int *)
xmalloc( (mtx->size+1)*
sizeof(int) );
253 if (mtx->j == NULL) mtx->j=(
int *)
xmalloc( nnz*
sizeof(
int) );
254 if (mtx->a == NULL) mtx->a=(
double *)
xmalloc( nnz*
sizeof(
double) );
255 DBGMSG(
"mtx size: %d nnz: %d\n",mtx->size,nnz);
258 for( row=0; row< mtx->size; row++ ) {
260 MatGetRow(mtx->A,row,&nnz_loc,&cols,&vals);
261 DBGMSG(
"CSR row: %d nnz_loc %d\n",row,nnz_loc);
262 for(i=0; i<nnz_loc; i++) {
263 OLD_ASSERT(pos<nnz,
"More nonzeroes then allocated! row: %d entry: %d\n",row,i);
268 MatRestoreRow(mtx->A,row,&nnz_loc,&cols,&vals);
273 void LSFreeCSR( LinSystem *mtx )
284 void LinSys_MPIAIJ::start_allocation()
286 if (status !=
NONE) {
290 VecCreateMPI(PETSC_COMM_WORLD, vec_ds.lsize(), PETSC_DECIDE, &(on_vec));
291 VecDuplicate(on_vec,&(off_vec));
296 void LinSys_MPIAIJ::preallocate_matrix()
300 PetscScalar *on_array, *off_array;
305 VecAssemblyBegin(on_vec);
306 VecAssemblyBegin(off_vec);
308 on_nz=(
int *)
xmalloc( 2 * vec_ds.lsize() *
sizeof(int));
309 off_nz=on_nz + vec_ds.lsize();
311 VecAssemblyEnd(on_vec);
312 VecAssemblyEnd(off_vec);
314 VecGetArray(on_vec,&on_array);
315 VecGetArray(off_vec,&off_array);
317 for(i=0; i<vec_ds.lsize(); i++) {
318 on_nz[i]=min((
unsigned int)(on_array[i]+0.1),vec_ds.lsize());
319 off_nz[i]=(int)(off_array[i]+0.1);
322 VecRestoreArray(on_vec,&on_array);
323 VecRestoreArray(off_vec,&off_array);
325 VecDestroy(&off_vec);
328 MatCreateAIJ(PETSC_COMM_WORLD, vec_ds.lsize(), vec_ds.lsize(), PETSC_DETERMINE, PETSC_DETERMINE,
329 0, on_nz, 0, off_nz, &matrix);
331 if (symmetric) MatSetOption(matrix, MAT_SYMMETRIC, PETSC_TRUE);
332 MatSetOption(matrix, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
338 void LinSys_MPIAIJ::preallocate_values(
int nrow,
int *rows,
int ncol,
int *cols)
342 for (i=0; i<nrow; i++) {
344 for(j=0; j<ncol; j++) {
346 if (vec_ds.get_proc(row) == vec_ds.get_proc(col))
347 VecSetValue(on_vec,row,1.0,ADD_VALUES);
349 VecSetValue(off_vec,row,1.0,ADD_VALUES);
354 void LinSys_MPIAIJ::view_local_matrix()
357 xprintf(
Msg,
"Printing of local matrix is not supported yet for MPIAIJ matrix. \n");
362 LinSys_MPIAIJ:: ~LinSys_MPIAIJ()
366 VecDestroy(&off_vec);
372 LinSys_MATIS::LinSys_MATIS(boost::shared_ptr<LocalToGlobalMap> global_row_4_sub_row,
double *sol_array)
373 :
LinSys(global_row_4_sub_row->get_distr()->lsize(), sol_array), lg_map(global_row_4_sub_row)
381 if (lg_map->get_distr()->size() > numeric_limits<PetscInt>::max())
xprintf(
Err,
"Index range doesn't fit into signed int!");
382 err = ISLocalToGlobalMappingCreate(PETSC_COMM_WORLD,
384 (
const PetscInt*)(&(lg_map->get_map_vector()[0])),
385 PETSC_COPY_VALUES, &map_local_to_global);
387 OLD_ASSERT(err == 0,
"Error in ISLocalToGlobalMappingCreate.");
391 loc_rows =
new int[loc_rows_size];
397 void LinSys_MATIS::start_allocation()
427 void LinSys_MATIS::preallocate_matrix()
436 MatSeqAIJSetPreallocation(local_matrix, 0, subdomain_nz);
438 if (symmetric) MatSetOption(matrix, MAT_SYMMETRIC, PETSC_TRUE);
439 MatSetOption(matrix, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
441 delete[] subdomain_nz;
444 void LinSys_MATIS::preallocate_values(
int nrow,
int *rows,
int ncol,
int *cols)
446 int i,row, n_loc_rows;
449 if (loc_rows_size < nrow) {
452 loc_rows=
new int[loc_rows_size];
457 err = ISGlobalToLocalMappingApply(map_local_to_global, IS_GTOLM_DROP, nrow, rows, &n_loc_rows, loc_rows);
458 OLD_ASSERT(err == 0,
"Error in ISGlobalToLocalMappingApply.");
459 OLD_ASSERT(nrow == n_loc_rows,
"Not all global indices translated to local indices.");
475 for (i=0; i<n_loc_rows; i++) {
477 subdomain_nz[row] = subdomain_nz[row] + ncol;
481 void LinSys_MATIS::view_local_matrix()
493 err = MatView(local_matrix,PETSC_VIEWER_STDOUT_SELF);
515 LinSys_MATIS:: ~LinSys_MATIS()
520 err = ISLocalToGlobalMappingDestroy(&map_local_to_global);
521 OLD_ASSERT(err == 0,
"Error in ISLocalToGlobalMappingDestroy.");
526 delete[] subdomain_indices;
536 #ifdef HAVE_ATLAS_ONLY_LAPACK 543 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()
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
void * xmalloc(size_t size)
Memory allocation with checking.
unsigned int myp() const
get my processor
unsigned int end(int proc) const
get last local index +1
static Input::Type::Abstract & get_input_type()
#define MPI_Barrier(comm)
FILE * xfopen(const std::string &fname, const char *mode)