PetscVecTools.cpp

00001 /*
00002 
00003 Copyright (c) 2005-2015, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 // Implementation
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     //Give full precision, scientific notation
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 //    DistributedVectorFactory factory(vec_size/2);
00202 //
00203 //    DistributedVector dist_inter_vec = factory.CreateDistributedVector(interleavedVec);
00204 //    DistributedVector::Stripe dist_inter_vec_1st_var(dist_inter_vec, 0);
00205 //    DistributedVector::Stripe dist_inter_vec_2nd_var(dist_inter_vec, 1);
00206 //
00207 //    DistributedVector dist_1st_var_vec = factory.CreateDistributedVector(firstVariableVec);
00208 //    DistributedVector dist_2nd_var_vec = factory.CreateDistributedVector(secondVariableVec);
00209 //
00210 //    for (DistributedVector::Iterator index = dist_1st_var_vec.Begin();
00211 //         index!= dist_1st_var_vec.End();
00212 //         ++index)
00213 //    {
00214 //        dist_1st_var_vec[index] = dist_inter_vec_1st_var[index];
00215 //        dist_2nd_var_vec[index] = dist_inter_vec_2nd_var[index];
00216 //    }
00217 
00218 
00219 //    //PETSc-3.x.x or PETSc-2.3.3
00220 //#if ( (PETSC_VERSION_MAJOR == 3) || (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 3)) //2.3.3 or 3.x.x
00221 //    VecScatterBegin(firstVariableScatterContext, interleavedVec, firstVariableVec, INSERT_VALUES, SCATTER_FORWARD);
00222 //    VecScatterEnd(firstVariableScatterContext, interleavedVec, firstVariableVec, INSERT_VALUES, SCATTER_FORWARD);
00223 //#else
00224 //    VecScatterBegin(interleavedVec, firstVariableVec, INSERT_VALUES, SCATTER_FORWARD, firstVariableScatterContext);
00225 //    VecScatterEnd(interleavedVec, firstVariableVec, INSERT_VALUES, SCATTER_FORWARD, firstVariableScatterContext);
00226 //#endif
00227 //
00228 //    //PETSc-3.x.x or PETSc-2.3.3
00229 //#if ( (PETSC_VERSION_MAJOR == 3) || (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 3)) //2.3.3 or 3.x.x
00230 //    VecScatterBegin(secondVariableScatterContefirstVariableScatterContextt, interleavedVec, secondVariableVec, INSERT_VALUES, SCATTER_FORWARD);
00231 //    VecScatterEnd(secondVariableScatterContefirstVariableScatterContextt, interleavedVec, secondVariableVec, INSERT_VALUES, SCATTER_FORWARD);
00232 //#else
00233 //    VecScatterBegin(interleavedVec, secondVariableVec, INSERT_VALUES, SCATTER_FORWARD, secondVariableScatterContefirstVariableScatterContextt);
00234 //    VecScatterEnd(interleavedVec, secondVariableVec, INSERT_VALUES, SCATTER_FORWARD, secondVariableScatterContefirstVariableScatterContextt);
00235 //#endif
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 //#if ( (PETSC_VERSION_MAJOR == 3) || (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 3)) //2.3.3 or 3.x.x
00263 //    VecScatterBegin(firstVariableScatterContext, firstVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE);
00264 //    VecScatterEnd(firstVariableScatterContext, firstVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE);
00265 //#else
00266 //    VecScatterBegin(firstVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE, firstVariableScatterContext);
00267 //    VecScatterEnd(firstVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE, firstVariableScatterContext);
00268 //#endif
00269 //
00271 //#if ( (PETSC_VERSION_MAJOR == 3) || (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 3)) //2.3.3 or 3.x.x
00272 //    VecScatterBegin(secondVariableScatterContext, secondVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE);
00273 //    VecScatterEnd(secondVariableScatterContext, secondVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE);
00274 //#else
00275 //    VecScatterBegin(secondVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE, secondVariableScatterContext);
00276 //    VecScatterEnd(secondVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE, secondVariableScatterContext);
00277 //#endif
00278 }

Generated by  doxygen 1.6.2