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 #include "PetscVecTools.hpp"
00030 #include "PetscTools.hpp"
00031 #include <petscviewer.h>
00032 #include <cassert>
00033 #include "DistributedVectorFactory.hpp"
00034 #include "DistributedVector.hpp"
00035 #include "PetscException.hpp"
00036
00038
00040
00041 void PetscVecTools::Finalise(Vec vector)
00042 {
00043 VecAssemblyBegin(vector);
00044 VecAssemblyEnd(vector);
00045 }
00046
00047 void PetscVecTools::SetElement(Vec vector, PetscInt row, double value)
00048 {
00049 PetscInt lo, hi;
00050 GetOwnershipRange(vector, lo, hi);
00051
00052 if (row >= lo && row < hi)
00053 {
00054 VecSetValues(vector, 1, &row, &value, INSERT_VALUES);
00055 }
00056 }
00057
00058 void PetscVecTools::AddToElement(Vec vector, PetscInt row, double value)
00059 {
00060 PetscInt lo, hi;
00061 GetOwnershipRange(vector, lo, hi);
00062
00063 if (row >= lo && row < hi)
00064 {
00065 VecSetValues(vector, 1, &row, &value, ADD_VALUES);
00066 }
00067 }
00068
00069 void PetscVecTools::Display(Vec vector)
00070 {
00071 VecView(vector, PETSC_VIEWER_STDOUT_WORLD);
00072 }
00073
00074 void PetscVecTools::Zero(Vec vector)
00075 {
00076 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
00077 PetscScalar zero = 0.0;
00078 VecSet(&zero, vector);
00079 #else
00080 VecZeroEntries(vector);
00081 #endif
00082 }
00083
00084 unsigned PetscVecTools::GetSize(Vec vector)
00085 {
00086 PetscInt size;
00087 VecGetSize(vector, &size);
00088 return (unsigned) size;
00089 }
00090
00091 void PetscVecTools::GetOwnershipRange(Vec vector, PetscInt& lo, PetscInt& hi)
00092 {
00093 VecGetOwnershipRange(vector, &lo, &hi);
00094 }
00095
00096 double PetscVecTools::GetElement(Vec vector, PetscInt row)
00097 {
00098 PetscInt lo, hi;
00099 GetOwnershipRange(vector, lo, hi);
00100 assert(lo <= row && row < hi);
00101
00102 double* p_vector;
00103 PetscInt local_index = row-lo;
00104 VecGetArray(vector, &p_vector);
00105 double answer = p_vector[local_index];
00106 VecRestoreArray(vector, &p_vector);
00107
00108 return answer;
00109 }
00110
00111 void PetscVecTools::AddScaledVector(Vec y, Vec x, double scaleFactor)
00112 {
00113 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
00114 VecAXPY(&scaleFactor, x, y);
00115 #else
00116 VecAXPY(y, scaleFactor, x);
00117 #endif
00118 }
00119
00120 void PetscVecTools::Scale(Vec vector, double scaleFactor)
00121 {
00122 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
00123 PETSCEXCEPT( VecScale(&scaleFactor, vector) );
00124 #else
00125 PETSCEXCEPT( VecScale(vector, scaleFactor) );
00126 #endif
00127 }
00128
00129 void PetscVecTools::WAXPY(Vec w, double a, Vec x, Vec y)
00130 {
00131 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
00132 PETSCEXCEPT( VecWAXPY(&a, x, y, w) );
00133 #else
00134 PETSCEXCEPT( VecWAXPY(w, a, x, y) );
00135 #endif
00136 }
00137
00138 void PetscVecTools::SetupInterleavedVectorScatterGather(Vec interleavedVec, VecScatter& rFirstVariableScatterContext, VecScatter& rSecondVariableScatterContext)
00139 {
00140 PetscInt num_rows, num_local_rows;
00141
00142 VecGetSize(interleavedVec, &num_rows);
00143 VecGetLocalSize(interleavedVec, &num_local_rows);
00144
00145 IS A11_rows, A22_rows;
00146 ISCreateStride(PETSC_COMM_WORLD, num_rows/2, 0, 2, &A11_rows);
00147 ISCreateStride(PETSC_COMM_WORLD, num_rows/2, 1, 2, &A22_rows);
00148
00149 IS all_vector;
00150 ISCreateStride(PETSC_COMM_WORLD, num_rows/2, 0, 1, &all_vector);
00151
00152 unsigned subvector_num_rows = num_rows/2;
00153 unsigned subvector_local_rows = num_local_rows/2;
00154 Vec x1_subvector = PetscTools::CreateVec(subvector_num_rows, subvector_local_rows);
00155 Vec x2_subvector = PetscTools::CreateVec(subvector_num_rows, subvector_local_rows);
00156
00157 VecScatterCreate(interleavedVec, A11_rows, x1_subvector, all_vector, &rFirstVariableScatterContext);
00158 VecScatterCreate(interleavedVec, A22_rows, x2_subvector, all_vector, &rSecondVariableScatterContext);
00159
00160 VecDestroy(x1_subvector);
00161 VecDestroy(x2_subvector);
00162
00163 ISDestroy(A11_rows);
00164 ISDestroy(A22_rows);
00165 ISDestroy(all_vector);
00166 }
00167
00168 void PetscVecTools::DoInterleavedVecScatter(Vec interleavedVec, VecScatter firstVariableScatterContext, Vec firstVariableVec, VecScatter secondVariableScatterContefirstVariableScatterContextt, Vec secondVariableVec)
00169 {
00170 PetscScalar *p_interleaved_vec;
00171 PetscScalar *p_1st_variable_vec;
00172 PetscScalar *p_2nd_variable_vec;
00173
00174 VecGetArray(interleavedVec, &p_interleaved_vec);
00175 VecGetArray(firstVariableVec, &p_1st_variable_vec);
00176 VecGetArray(secondVariableVec, &p_2nd_variable_vec);
00177
00178 PetscInt vec_local_size;
00179 VecGetLocalSize(interleavedVec, &vec_local_size);
00180 assert(vec_local_size%2 == 0);
00181
00182 for (PetscInt local_index=0; local_index<vec_local_size/2; local_index++)
00183 {
00184 p_1st_variable_vec[local_index] = p_interleaved_vec[2*local_index];
00185 p_2nd_variable_vec[local_index] = p_interleaved_vec[2*local_index+1];
00186 }
00187
00188 VecRestoreArray(interleavedVec, &p_interleaved_vec);
00189 VecRestoreArray(firstVariableVec, &p_1st_variable_vec);
00190 VecRestoreArray(secondVariableVec, &p_2nd_variable_vec);
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 }
00228
00229 void PetscVecTools::DoInterleavedVecGather(Vec interleavedVec, VecScatter firstVariableScatterContext, Vec firstVariableVec, VecScatter secondVariableScatterContext, Vec secondVariableVec)
00230 {
00231 PetscScalar *p_interleaved_vec;
00232 PetscScalar *p_1st_variable_vec;
00233 PetscScalar *p_2nd_variable_vec;
00234
00235 VecGetArray(interleavedVec, &p_interleaved_vec);
00236 VecGetArray(firstVariableVec, &p_1st_variable_vec);
00237 VecGetArray(secondVariableVec, &p_2nd_variable_vec);
00238
00239 PetscInt vec_local_size;
00240 VecGetLocalSize(interleavedVec, &vec_local_size);
00241 assert(vec_local_size%2 == 0);
00242
00243 for (PetscInt local_index=0; local_index<vec_local_size/2; local_index++)
00244 {
00245 p_interleaved_vec[2*local_index] = p_1st_variable_vec[local_index];
00246 p_interleaved_vec[2*local_index+1] = p_2nd_variable_vec[local_index];
00247 }
00248
00249 VecRestoreArray(interleavedVec, &p_interleaved_vec);
00250 VecRestoreArray(firstVariableVec, &p_1st_variable_vec);
00251 VecRestoreArray(secondVariableVec, &p_2nd_variable_vec);
00252
00253
00254
00255
00256
00257
00258
00259
00260
00262
00263
00264
00265
00266
00267
00268
00269 }