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