00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <iostream>
00037
00038 #include "PetscVecTools.hpp"
00039 #include "PCBlockDiagonal.hpp"
00040 #include "Exception.hpp"
00041 #include "Warnings.hpp"
00042
00043 PCBlockDiagonal::PCBlockDiagonal(KSP& rKspObject)
00044 {
00045 #ifdef TRACE_KSP
00046 mPCContext.mScatterTime = 0.0;
00047 mPCContext.mA1PreconditionerTime = 0.0;;
00048 mPCContext.mA2PreconditionerTime = 0.0;;
00049 mPCContext.mGatherTime = 0.0;;
00050 #endif
00051
00052 PCBlockDiagonalCreate(rKspObject);
00053 PCBlockDiagonalSetUp();
00054 }
00055
00056 PCBlockDiagonal::~PCBlockDiagonal()
00057 {
00058 #ifdef TRACE_KSP
00059 if (PetscTools::AmMaster())
00060 {
00061 std::cout << " -- Block diagonal preconditioner profile information: " << std::endl;
00062 std::cout << "\t mScatterTime: " << mPCContext.mScatterTime << std::endl;
00063 std::cout << "\t mA1PreconditionerTime: " << mPCContext.mA1PreconditionerTime << std::endl;
00064 std::cout << "\t mA2PreconditionerTime: " << mPCContext.mA2PreconditionerTime << std::endl;
00065 std::cout << "\t mGatherTime: " << mPCContext.mGatherTime << std::endl;
00066 }
00067 #endif
00068
00069 PetscTools::Destroy(mPCContext.A11_matrix_subblock);
00070 PetscTools::Destroy(mPCContext.A22_matrix_subblock);
00071
00072 PCDestroy(PETSC_DESTROY_PARAM(mPCContext.PC_amg_A11));
00073 PCDestroy(PETSC_DESTROY_PARAM(mPCContext.PC_amg_A22));
00074
00075 PetscTools::Destroy(mPCContext.x1_subvector);
00076 PetscTools::Destroy(mPCContext.y1_subvector);
00077
00078 PetscTools::Destroy(mPCContext.x2_subvector);
00079 PetscTools::Destroy(mPCContext.y2_subvector);
00080
00081 VecScatterDestroy(PETSC_DESTROY_PARAM(mPCContext.A11_scatter_ctx));
00082 VecScatterDestroy(PETSC_DESTROY_PARAM(mPCContext.A22_scatter_ctx));
00083 }
00084
00085 void PCBlockDiagonal::PCBlockDiagonalCreate(KSP& rKspObject)
00086 {
00087 KSPGetPC(rKspObject, &mPetscPCObject);
00088
00089 Mat system_matrix, dummy;
00090 #if ( PETSC_VERSION_MAJOR==3 && PETSC_VERSION_MINOR>=5 )
00091 KSPGetOperators(rKspObject, &system_matrix, &dummy);
00092 #else
00093 MatStructure flag;
00094 KSPGetOperators(rKspObject, &system_matrix, &dummy, &flag);
00095 #endif
00096
00097 PetscInt num_rows, num_columns;
00098 MatGetSize(system_matrix, &num_rows, &num_columns);
00099 assert(num_rows==num_columns);
00100
00101 PetscInt num_local_rows, num_local_columns;
00102 MatGetLocalSize(system_matrix, &num_local_rows, &num_local_columns);
00103
00104
00105
00106 if ((num_rows%2 != 0) || (num_local_rows%2 != 0))
00107 {
00108 TERMINATE("Wrong matrix parallel layout detected in PCLDUFactorisation.");
00109 }
00110
00111
00112 unsigned subvector_num_rows = num_rows/2;
00113 unsigned subvector_local_rows = num_local_rows/2;
00114 mPCContext.x1_subvector = PetscTools::CreateVec(subvector_num_rows, subvector_local_rows);
00115 mPCContext.x2_subvector = PetscTools::CreateVec(subvector_num_rows, subvector_local_rows);
00116 mPCContext.y1_subvector = PetscTools::CreateVec(subvector_num_rows, subvector_local_rows);
00117 mPCContext.y2_subvector = PetscTools::CreateVec(subvector_num_rows, subvector_local_rows);
00118
00119
00120 {
00121
00122 Vec dummy_vec = PetscTools::CreateVec(num_rows, num_local_rows);
00123
00124 PetscVecTools::SetupInterleavedVectorScatterGather(dummy_vec, mPCContext.A11_scatter_ctx, mPCContext.A22_scatter_ctx);
00125
00126 PetscTools::Destroy(dummy_vec);
00127 }
00128
00129
00130 {
00131
00132 PetscInt low, high, global_size;
00133 VecGetOwnershipRange(mPCContext.x1_subvector, &low, &high);
00134 VecGetSize(mPCContext.x1_subvector, &global_size);
00135 assert(global_size == num_rows/2);
00136
00137 IS A11_local_rows;
00138 IS A11_columns;
00139 ISCreateStride(PETSC_COMM_WORLD, high-low, 2*low, 2, &A11_local_rows);
00140 ISCreateStride(PETSC_COMM_WORLD, global_size, 0, 2, &A11_columns);
00141
00142 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 1) //PETSc 3.1 or later
00143 MatGetSubMatrix(system_matrix, A11_local_rows, A11_local_rows,
00144 MAT_INITIAL_MATRIX, &mPCContext.A11_matrix_subblock);
00145 #else
00146 MatGetSubMatrix(system_matrix, A11_local_rows, A11_columns, PETSC_DECIDE,
00147 MAT_INITIAL_MATRIX, &mPCContext.A11_matrix_subblock);
00148 #endif
00149
00150
00151 ISDestroy(PETSC_DESTROY_PARAM(A11_local_rows));
00152 ISDestroy(PETSC_DESTROY_PARAM(A11_columns));
00153 }
00154
00155
00156 {
00157
00158 PetscInt low, high, global_size;
00159 VecGetOwnershipRange(mPCContext.x2_subvector, &low, &high);
00160 VecGetSize(mPCContext.x2_subvector, &global_size);
00161 assert(global_size == num_rows/2);
00162
00163 IS A22_local_rows;
00164 IS A22_columns;
00165 ISCreateStride(PETSC_COMM_WORLD, high-low, 2*low+1, 2, &A22_local_rows);
00166 ISCreateStride(PETSC_COMM_WORLD, global_size, 1, 2, &A22_columns);
00167
00168 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 1) //PETSc 3.1 or later
00169 MatGetSubMatrix(system_matrix, A22_local_rows, A22_local_rows,
00170 MAT_INITIAL_MATRIX, &mPCContext.A22_matrix_subblock);
00171 #else
00172 MatGetSubMatrix(system_matrix, A22_local_rows, A22_columns, PETSC_DECIDE,
00173 MAT_INITIAL_MATRIX, &mPCContext.A22_matrix_subblock);
00174 #endif
00175
00176 ISDestroy(PETSC_DESTROY_PARAM(A22_local_rows));
00177 ISDestroy(PETSC_DESTROY_PARAM(A22_columns));
00178 }
00179
00180
00181 PCSetType(mPetscPCObject, PCSHELL);
00182 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
00183 PCShellSetApply(mPetscPCObject, PCBlockDiagonalApply, (void*) &mPCContext);
00184 #else
00185
00186 PCShellSetContext(mPetscPCObject, &mPCContext);
00187
00188
00189 PCShellSetApply(mPetscPCObject, PCBlockDiagonalApply);
00190 #endif
00191
00192 }
00193
00194 void PCBlockDiagonal::PCBlockDiagonalSetUp()
00195 {
00196
00197
00198
00199
00200
00201
00202 PCCreate(PETSC_COMM_WORLD, &(mPCContext.PC_amg_A11));
00203
00204
00205
00206
00207
00208 PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL);
00209 PetscErrorCode pc_set_error = PCSetType(mPCContext.PC_amg_A11, PCHYPRE);
00210 if (pc_set_error != 0)
00211 {
00212 WARNING("PETSc hypre preconditioning library is not installed");
00213 }
00214
00215 PetscPopErrorHandler();
00216
00217
00218
00219 PetscOptionsSetValue("-pc_hypre_type", "euclid");
00220 PetscOptionsSetValue("-pc_hypre_euclid_levels", "0");
00221
00222
00223
00224
00225
00226
00227
00228
00229 #if ( PETSC_VERSION_MAJOR==3 && PETSC_VERSION_MINOR>=5 )
00230
00231 PCSetReusePreconditioner(mPCContext.PC_amg_A11, PETSC_TRUE);
00232 PCSetOperators(mPCContext.PC_amg_A11, mPCContext.A11_matrix_subblock, mPCContext.A11_matrix_subblock);
00233 #else
00234 PCSetOperators(mPCContext.PC_amg_A11, mPCContext.A11_matrix_subblock, mPCContext.A11_matrix_subblock, SAME_PRECONDITIONER);
00235 #endif
00236 PCSetFromOptions(mPCContext.PC_amg_A11);
00237 PCSetUp(mPCContext.PC_amg_A11);
00238
00239
00240 PCCreate(PETSC_COMM_WORLD, &(mPCContext.PC_amg_A22));
00241
00242
00243 PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL);
00244 PCSetType(mPCContext.PC_amg_A22, PCHYPRE);
00245
00246 PetscPopErrorHandler();
00247
00248
00249 PetscOptionsSetValue("-pc_hypre_type", "boomeramg");
00250 PetscOptionsSetValue("-pc_hypre_boomeramg_max_iter", "1");
00251 PetscOptionsSetValue("-pc_hypre_boomeramg_strong_threshold", "0.0");
00252 PetscOptionsSetValue("-pc_hypre_boomeramg_coarsen_type", "HMIS");
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 #if ( PETSC_VERSION_MAJOR==3 && PETSC_VERSION_MINOR>=5 )
00290
00291 PCSetReusePreconditioner(mPCContext.PC_amg_A22, PETSC_TRUE);
00292 PCSetOperators(mPCContext.PC_amg_A22, mPCContext.A22_matrix_subblock, mPCContext.A22_matrix_subblock);
00293 #else
00294 PCSetOperators(mPCContext.PC_amg_A22, mPCContext.A22_matrix_subblock, mPCContext.A22_matrix_subblock, SAME_PRECONDITIONER);
00295 #endif
00296 PCSetFromOptions(mPCContext.PC_amg_A22);
00297 PCSetUp(mPCContext.PC_amg_A22);
00298 }
00299
00300 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 1) //PETSc 3.1 or later
00301 PetscErrorCode PCBlockDiagonalApply(PC pc_object, Vec x, Vec y)
00302 {
00303 void* pc_context;
00304
00305 PCShellGetContext(pc_object, &pc_context);
00306 #else
00307 PetscErrorCode PCBlockDiagonalApply(void* pc_context, Vec x, Vec y)
00308 {
00309 #endif
00310
00311
00312 PCBlockDiagonal::PCBlockDiagonalContext* block_diag_context = (PCBlockDiagonal::PCBlockDiagonalContext*) pc_context;
00313 assert(block_diag_context!=NULL);
00314
00315
00316
00317
00318 #ifdef TRACE_KSP
00319 Timer::Reset();
00320 #endif
00321
00322 PetscVecTools::DoInterleavedVecScatter(x, block_diag_context->A11_scatter_ctx, block_diag_context->x1_subvector, block_diag_context->A22_scatter_ctx, block_diag_context->x2_subvector);
00323
00324 #ifdef TRACE_KSP
00325 block_diag_context->mScatterTime += Timer::GetElapsedTime();
00326 #endif
00327
00328
00329
00330
00331
00332 #ifdef TRACE_KSP
00333 Timer::Reset();
00334 #endif
00335 PCApply(block_diag_context->PC_amg_A11, block_diag_context->x1_subvector, block_diag_context->y1_subvector);
00336 #ifdef TRACE_KSP
00337 block_diag_context->mA1PreconditionerTime += Timer::GetElapsedTime();
00338 #endif
00339
00340 #ifdef TRACE_KSP
00341 Timer::Reset();
00342 #endif
00343 PCApply(block_diag_context->PC_amg_A22, block_diag_context->x2_subvector, block_diag_context->y2_subvector);
00344 #ifdef TRACE_KSP
00345 block_diag_context->mA2PreconditionerTime += Timer::GetElapsedTime();
00346 #endif
00347
00348
00349
00350
00351
00352 #ifdef TRACE_KSP
00353 Timer::Reset();
00354 #endif
00355
00356 PetscVecTools::DoInterleavedVecGather(y, block_diag_context->A11_scatter_ctx, block_diag_context->y1_subvector, block_diag_context->A22_scatter_ctx, block_diag_context->y2_subvector);
00357
00358 #ifdef TRACE_KSP
00359 block_diag_context->mGatherTime += Timer::GetElapsedTime();
00360 #endif
00361 return 0;
00362 }