From 51fc0f1f17c93c5ea3d2805774b79a726e354e23 Mon Sep 17 00:00:00 2001
From: Boris Martin <boris.martin@uliege.be>
Date: Fri, 18 Apr 2025 13:52:59 +0200
Subject: [PATCH] moved type declaration

---
 src/field/DistributedField.cpp | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/src/field/DistributedField.cpp b/src/field/DistributedField.cpp
index 15b8c381..dfdffe7b 100644
--- a/src/field/DistributedField.cpp
+++ b/src/field/DistributedField.cpp
@@ -413,6 +413,16 @@ namespace gmshfem::field
       unsigned long long global;
     };
 
+    MPI_Datatype types[3] = {MPI_UNSIGNED_LONG, MPI_UNSIGNED_LONG, MPI_UNSIGNED_LONG};
+    MPI_Datatype mpi_struct_type;
+    MPI_Aint offsets[3];
+    int blocklengths[3] = {1, 1, 1};
+    offsets[0] = offsetof(LocalGlobalPair, type);
+    offsets[1] = offsetof(LocalGlobalPair, entity);
+    offsets[2] = offsetof(LocalGlobalPair, global);
+    MPI_Type_create_struct(3, blocklengths, offsets, types, &mpi_struct_type);
+    MPI_Type_commit(&mpi_struct_type);
+
     std::vector< LocalGlobalPair > myOwnedDofs, allSharedDofsArray;
 
     for(auto dof : this->_toSend) {
@@ -431,15 +441,7 @@ namespace gmshfem::field
     }
 
 
-    MPI_Datatype types[3] = {MPI_UNSIGNED_LONG, MPI_UNSIGNED_LONG, MPI_UNSIGNED_LONG};
-    MPI_Datatype mpi_struct_type;
-    MPI_Aint offsets[3];
-    int blocklengths[3] = {1, 1, 1};
-    offsets[0] = offsetof(LocalGlobalPair, type);
-    offsets[1] = offsetof(LocalGlobalPair, entity);
-    offsets[2] = offsetof(LocalGlobalPair, global);
-    MPI_Type_create_struct(3, blocklengths, offsets, types, &mpi_struct_type);
-    MPI_Type_commit(&mpi_struct_type);
+    
     // Synchronize the full array
     allSharedDofsArray.resize(total_size);
 
@@ -451,8 +453,7 @@ namespace gmshfem::field
       msg::info << "Gathered allSharedDofsArray. Total size is " << allSharedDofsArray.size() << msg::endl;
     }
     MPI_Barrier(MPI_COMM_WORLD);
-    // Free the type
-    MPI_Type_free(&mpi_struct_type);
+    
 
     std::unordered_map< DofIndex, unsigned long long, HashBySecond, std::equal_to<DofIndex> > allSharedDofs;
     // Put in hashtable
@@ -488,6 +489,8 @@ namespace gmshfem::field
       msg::debug << msg::endl
                  << "Leaving syncGlobalDofs" << msg::endl;
     MPI_Barrier(MPI_COMM_WORLD);
+    // Free the type
+    MPI_Type_free(&mpi_struct_type);
 
 #else
     throw common::Exception("MPI is not available");
-- 
GitLab