]> https://gitweb.dealii.org/ - dealii.git/commitdiff
PETScWrappers::VectorBase: avoid hard-coding unsigned int indices. 17468/head
authorDavid Wells <drwells@email.unc.edu>
Sun, 11 Aug 2024 17:50:05 +0000 (11:50 -0600)
committerDavid Wells <drwells@email.unc.edu>
Sun, 11 Aug 2024 17:50:05 +0000 (11:50 -0600)
Part of #16528.

include/deal.II/lac/petsc_vector_base.h

index acd5901fd106eecc62ec1af6f027342ef7bfb45e..37e70f7cf83a241dc0f0ddb427def65b184c87aa 100644 (file)
@@ -1205,8 +1205,7 @@ namespace PETScWrappers
                                    const ForwardIterator indices_end,
                                    OutputIterator        values_begin) const
   {
-    const PetscInt n_idx = static_cast<PetscInt>(indices_end - indices_begin);
-    if (n_idx == 0)
+    if (indices_begin == indices_end)
       return;
 
     // if we are dealing
@@ -1246,24 +1245,28 @@ namespace PETScWrappers
         ierr = VecGetArrayRead(locally_stored_elements, &ptr);
         AssertThrow(ierr == 0, ExcPETScError(ierr));
 
-        for (PetscInt i = 0; i < n_idx; ++i)
+        auto input  = indices_begin;
+        auto output = values_begin;
+        while (input != indices_end)
           {
-            const unsigned int index = *(indices_begin + i);
-            if (index >= static_cast<unsigned int>(begin) &&
-                index < static_cast<unsigned int>(end))
+            const auto index = static_cast<PetscInt>(*input);
+            AssertDimension(index, *input);
+            if (index >= begin && index < end)
               {
                 // local entry
-                *(values_begin + i) = *(ptr + index - begin);
+                *output = *(ptr + index - begin);
               }
             else
               {
                 // ghost entry
-                const unsigned int ghostidx =
-                  ghost_indices.index_within_set(index);
+                const auto ghost_index = ghost_indices.index_within_set(*input);
 
-                AssertIndexRange(ghostidx + end - begin, lsize);
-                *(values_begin + i) = *(ptr + ghostidx + end - begin);
+                AssertIndexRange(ghost_index + end - begin, lsize);
+                *output = *(ptr + ghost_index + end - begin);
               }
+
+            ++input;
+            ++output;
           }
 
         ierr = VecRestoreArrayRead(locally_stored_elements, &ptr);
@@ -1285,12 +1288,14 @@ namespace PETScWrappers
         ierr = VecGetArrayRead(vector, &ptr);
         AssertThrow(ierr == 0, ExcPETScError(ierr));
 
-        for (PetscInt i = 0; i < n_idx; ++i)
+        auto input  = indices_begin;
+        auto output = values_begin;
+        while (input != indices_end)
           {
-            const unsigned int index = *(indices_begin + i);
+            const auto index = static_cast<PetscInt>(*input);
+            AssertDimension(index, *input);
 
-            Assert(index >= static_cast<unsigned int>(begin) &&
-                     index < static_cast<unsigned int>(end),
+            Assert(index >= begin && index < end,
                    ExcMessage("You are accessing elements of a vector without "
                               "ghost elements that are not actually owned by "
                               "this vector. A typical case where this may "
@@ -1300,7 +1305,10 @@ namespace PETScWrappers
                               "elements for all locally relevant or locally "
                               "active vector entries."));
 
-            *(values_begin + i) = *(ptr + index - begin);
+            *output = *(ptr + index - begin);
+
+            ++input;
+            ++output;
           }
 
         ierr = VecRestoreArrayRead(vector, &ptr);

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.