From 8fc0a54553be6b3accd2cf8419ebc69d5116255c Mon Sep 17 00:00:00 2001
From: bramas <berenger.bramas@inria.fr>
Date: Thu, 7 Nov 2013 15:32:11 +0100
Subject: [PATCH] remove MPI loading file bug

---
 Src/Files/FMpiFmaLoader.hpp          | 21 +++++++++++----------
 Tests/Utils/testFmmAlgorithmProc.cpp | 16 ++++++++++++++--
 2 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/Src/Files/FMpiFmaLoader.hpp b/Src/Files/FMpiFmaLoader.hpp
index 50878a435..ec1184e26 100755
--- a/Src/Files/FMpiFmaLoader.hpp
+++ b/Src/Files/FMpiFmaLoader.hpp
@@ -81,7 +81,7 @@ public:
                 MPI_Status status;
                 if( MPI_File_read(file, &sizeOfElement, 1, MPI_INT, &status) == MPI_SUCCESS
                     && MPI_File_read(file, &this->totalNbParticles, 1, MPI_LONG_LONG, &status) == MPI_SUCCESS
-                    && MPI_File_read(file, xyzBoxWidth, 4, MPI_FLOAT, &status) == MPI_SUCCESS ){
+                        && MPI_File_read(file, xyzBoxWidth, 4, FMpi::GetType(xyzBoxWidth[0]), &status) == MPI_SUCCESS ){
 
                     FLOG(if(sizeOfElement != sizeof(FReal)){)
                         FLOG( FLog::Controller.writeFromLine("Warning type size between file and FReal are differents\n", __LINE__, __FILE__); )
@@ -110,13 +110,7 @@ public:
                     // local number to read
                     particles = new FReal[bufsize];
 
-                    if( sizeof(FReal) == sizeof(float) ){
-                        MPI_File_read_at(file, headDataOffSet + startPart * 4 * sizeof(FReal), particles, int(bufsize), MPI_FLOAT, &status);
-                    }
-                    else{
-                        MPI_File_read_at(file, headDataOffSet + startPart * 4 * sizeof(FReal), particles, int(bufsize), MPI_DOUBLE, &status);
-                    }
-
+                    MPI_File_read_at(file, headDataOffSet + startPart * 4 * sizeof(FReal), particles, int(bufsize), FMpi::GetType(xyzBoxWidth[0]), &status);
 
                     // check if needed
                     int count(0);
@@ -142,7 +136,6 @@ public:
                     FLOG( FLog::Controller.writeFromLine("Warning type size between file and FReal are differents\n", __LINE__, __FILE__); )
                 FLOG(})
                 removeWarning += fread(&this->totalNbParticles, sizeof(FSize), 1, file);
-
                 removeWarning += fread(&this->boxWidth, sizeof(FReal), 1, file);
                 this->boxWidth *= 2;
 
@@ -160,6 +153,7 @@ public:
 
                 if(filesize/4 != this->totalNbParticles){
                     printf("Error fileSize %ld, nbPart %lld\n", filesize/4, this->totalNbParticles);
+                    exit(0);
                 }
 
                 // in number of floats
@@ -172,7 +166,10 @@ public:
 
                 fseek(file, long(headDataOffSet + startPart * 4 * sizeof(FReal)), SEEK_SET);
 
-                removeWarning += fread(particles, sizeof(FReal), int(bufsize), file);
+                if( fread(particles, sizeof(FReal), int(bufsize), file) != unsigned(bufsize)){
+                    printf("Error when reading file.\n");
+                    exit(0);
+                }
 
                 fclose(file);
             }
@@ -226,6 +223,10 @@ public:
       * @param the particle to fill
       */
     void fillParticle(FPoint*const inParticlePositions, FReal*const inPhysicalValue){
+        if(nbParticles*4 <= idxParticles){
+            printf("Error you're loading too much particles.\n");
+            exit(0);
+        }
         inParticlePositions->setPosition(particles[idxParticles],particles[idxParticles+1],particles[idxParticles+2]);
         (*inPhysicalValue) = (particles[idxParticles+3]);
         idxParticles += 4;
diff --git a/Tests/Utils/testFmmAlgorithmProc.cpp b/Tests/Utils/testFmmAlgorithmProc.cpp
index 7d03b1fa9..e8cdaef7b 100755
--- a/Tests/Utils/testFmmAlgorithmProc.cpp
+++ b/Tests/Utils/testFmmAlgorithmProc.cpp
@@ -327,6 +327,11 @@ int main(int argc, char ** argv){
         return 1;
     }
 
+    std::cout << "Simulation properties :\n";
+    std::cout << "Nb Particles " << loader.getNumberOfParticles() << "\n";
+    std::cout << "Box Width : " << loader.getBoxWidth() << "\n";
+    std::cout << "Box Center : " << loader.getCenterOfBox() << "\n";
+
     // The real tree to work on
     OctreeClass realTree(NbLevels, SizeSubLevels,loader.getBoxWidth(),loader.getCenterOfBox());
 
@@ -371,7 +376,8 @@ int main(int argc, char ** argv){
     else{
         FPoint position;
         FReal physicalValue;
-        for(FSize idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
+        const FSize nbParticles = loader.getNumberOfParticles();
+        for(FSize idxPart = 0 ; idxPart < nbParticles ; ++idxPart){
             loader.fillParticle(&position,&physicalValue);
             realTree.insert(position);
         }
@@ -384,10 +390,16 @@ int main(int argc, char ** argv){
     OctreeClass treeValide(NbLevels, SizeSubLevels,loader.getBoxWidth(),loader.getCenterOfBox());
     {
         FFmaBinLoader loaderSeq(filename);
+
+        std::cout << "Simulation properties :\n";
+        std::cout << "Nb Particles " << loaderSeq.getNumberOfParticles() << "\n";
+        std::cout << "Box Width : " << loaderSeq.getBoxWidth() << "\n";
+        std::cout << "Box Center : " << loaderSeq.getCenterOfBox() << "\n";
+
         FPoint position;
         FReal physicalValue;
         for(FSize idxPart = 0 ; idxPart < loaderSeq.getNumberOfParticles() ; ++idxPart){
-            loader.fillParticle(&position,&physicalValue);
+            loaderSeq.fillParticle(&position,&physicalValue);
             treeValide.insert(position);
         }
     }
-- 
GitLab