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::Assemble(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
00121
00122
00123 void PetscVecTools::Scale(Vec vector, double scaleFactor)
00124 {
00125 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
00126 PETSCEXCEPT( VecScale(&scaleFactor, vector) );
00127 #else
00128 PETSCEXCEPT( VecScale(vector, scaleFactor) );
00129 #endif
00130 }
00131
00132 void PetscVecTools::WAXPY(Vec w, double a, Vec x, Vec y)
00133 {
00134 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
00135 PETSCEXCEPT( VecWAXPY(&a, x, y, w) );
00136 #else
00137 PETSCEXCEPT( VecWAXPY(w, a, x, y) );
00138 #endif
00139 }
00140
00141
00142 void PetscVecTools::SetupInterleavedVectorScatterGather(Vec interleavedVec, VecScatter& rFirstVariableScatterContext, VecScatter& rSecondVariableScatterContext)
00143 {
00144 PetscInt num_rows, num_local_rows;
00145
00146 VecGetSize(interleavedVec, &num_rows);
00147 VecGetLocalSize(interleavedVec, &num_local_rows);
00148
00149 IS A11_rows, A22_rows;
00150 ISCreateStride(PETSC_COMM_WORLD, num_rows/2, 0, 2, &A11_rows);
00151 ISCreateStride(PETSC_COMM_WORLD, num_rows/2, 1, 2, &A22_rows);
00152
00153 IS all_vector;
00154 ISCreateStride(PETSC_COMM_WORLD, num_rows/2, 0, 1, &all_vector);
00155
00156 unsigned subvector_num_rows = num_rows/2;
00157 unsigned subvector_local_rows = num_local_rows/2;
00158 Vec x1_subvector = PetscTools::CreateVec(subvector_num_rows, subvector_local_rows);
00159 Vec x2_subvector = PetscTools::CreateVec(subvector_num_rows, subvector_local_rows);
00160
00161 VecScatterCreate(interleavedVec, A11_rows, x1_subvector, all_vector, &rFirstVariableScatterContext);
00162 VecScatterCreate(interleavedVec, A22_rows, x2_subvector, all_vector, &rSecondVariableScatterContext);
00163
00164 VecDestroy(x1_subvector);
00165 VecDestroy(x2_subvector);
00166
00167 ISDestroy(A11_rows);
00168 ISDestroy(A22_rows);
00169 ISDestroy(all_vector);
00170 }
00171
00172 void PetscVecTools::DoInterleavedVecScatter(Vec interleavedVec, VecScatter firstVariableScatterContext, Vec firstVariableVec, VecScatter secondVariableScatterContefirstVariableScatterContextt, Vec secondVariableVec)
00173 {
00174 PetscScalar *p_interleaved_vec;
00175 PetscScalar *p_1st_variable_vec;
00176 PetscScalar *p_2nd_variable_vec;
00177
00178 VecGetArray(interleavedVec, &p_interleaved_vec);
00179 VecGetArray(firstVariableVec, &p_1st_variable_vec);
00180 VecGetArray(secondVariableVec, &p_2nd_variable_vec);
00181
00182 PetscInt vec_local_size;
00183 VecGetLocalSize(interleavedVec, &vec_local_size);
00184 assert(vec_local_size%2 == 0);
00185
00186 for (PetscInt local_index=0; local_index<vec_local_size/2; local_index++)
00187 {
00188 p_1st_variable_vec[local_index] = p_interleaved_vec[2*local_index];
00189 p_2nd_variable_vec[local_index] = p_interleaved_vec[2*local_index+1];
00190 }
00191
00192 VecRestoreArray(interleavedVec, &p_interleaved_vec);
00193 VecRestoreArray(firstVariableVec, &p_1st_variable_vec);
00194 VecRestoreArray(secondVariableVec, &p_2nd_variable_vec);
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
00230
00231
00232 }
00233
00234 void PetscVecTools::DoInterleavedVecGather(Vec interleavedVec, VecScatter firstVariableScatterContext, Vec firstVariableVec, VecScatter secondVariableScatterContext, Vec secondVariableVec)
00235 {
00236 PetscScalar *p_interleaved_vec;
00237 PetscScalar *p_1st_variable_vec;
00238 PetscScalar *p_2nd_variable_vec;
00239
00240 VecGetArray(interleavedVec, &p_interleaved_vec);
00241 VecGetArray(firstVariableVec, &p_1st_variable_vec);
00242 VecGetArray(secondVariableVec, &p_2nd_variable_vec);
00243
00244 PetscInt vec_local_size;
00245 VecGetLocalSize(interleavedVec, &vec_local_size);
00246 assert(vec_local_size%2 == 0);
00247
00248 for (PetscInt local_index=0; local_index<vec_local_size/2; local_index++)
00249 {
00250 p_interleaved_vec[2*local_index] = p_1st_variable_vec[local_index];
00251 p_interleaved_vec[2*local_index+1] = p_2nd_variable_vec[local_index];
00252 }
00253
00254 VecRestoreArray(interleavedVec, &p_interleaved_vec);
00255 VecRestoreArray(firstVariableVec, &p_1st_variable_vec);
00256 VecRestoreArray(secondVariableVec, &p_2nd_variable_vec);
00257
00258
00259
00260
00261
00262
00263
00264
00265
00267
00268
00269
00270
00271
00272
00273
00274 }