From 6ee28dd9beb72c66726baed424afa1ac30f1e63a Mon Sep 17 00:00:00 2001
From: Cyrille Piacibello <cyrille.piacibello@inria.fr>
Date: Wed, 5 Apr 2017 16:23:28 +0200
Subject: [PATCH] Api updated : Error spotted by D Barbier fixed alongside
 another one, and add a definition of scalfmm_call_delete()

---
 Addons/CKernelApi/Src/FScalFMMEngine.hpp      | 18 ++++---
 .../CKernelApi/Src/FUserKernelDistrEngine.hpp | 54 +++++++++++--------
 Addons/CKernelApi/Tests/testMpiUserKernel.c   | 12 ++---
 3 files changed, 48 insertions(+), 36 deletions(-)

diff --git a/Addons/CKernelApi/Src/FScalFMMEngine.hpp b/Addons/CKernelApi/Src/FScalFMMEngine.hpp
index 546335721..d9af3a5ef 100644
--- a/Addons/CKernelApi/Src/FScalFMMEngine.hpp
+++ b/Addons/CKernelApi/Src/FScalFMMEngine.hpp
@@ -428,7 +428,7 @@ public:
 
     template<class ContainerClass,class LeafClass,class CellClass>
     void generic_set_positions(FOctree<FReal,CellClass,ContainerClass,LeafClass> * octree,
-                       int NbPositions, FReal * X, FReal * Y, FReal * Z, PartType type){
+                               int NbPositions, FReal * X, FReal * Y, FReal * Z, PartType type){
         int checkCount = 0;
         if(type == SOURCE || type==BOTH){
             octree->forEachLeaf([&](LeafClass* leaf){
@@ -564,7 +564,7 @@ public:
 
     template<class ContainerClass,class LeafClass,class CellClass>
     void generic_get_positions_xyz(FOctree<FReal,CellClass,ContainerClass,LeafClass> * octree,
-                           int NbPositions, FReal * positionsToFill, PartType type){
+                                   int NbPositions, FReal * positionsToFill, PartType type){
         int checkCount = 0;
         if(type == SOURCE || type==BOTH){
             octree->forEachLeaf([&](LeafClass* leaf){
@@ -599,7 +599,7 @@ public:
 
     template<class ContainerClass,class LeafClass,class CellClass>
     void generic_get_positions_xyz_npart(FOctree<FReal,CellClass,ContainerClass,LeafClass> * octree,
-                                 int NbPositions, int * idxOfParticles, FReal * positionsToFill, PartType type){
+                                         int NbPositions, int * idxOfParticles, FReal * positionsToFill, PartType type){
         int checkCount = 0;
         if(type == SOURCE || type==BOTH){
             octree->forEachLeaf([&](LeafClass* leaf){
@@ -654,7 +654,7 @@ public:
 
     template<class ContainerClass,class LeafClass,class CellClass>
     void generic_get_positions(FOctree<FReal,CellClass,ContainerClass,LeafClass> * octree,
-                       int NbPositions, FReal * X, FReal * Y , FReal * Z, PartType type){
+                               int NbPositions, FReal * X, FReal * Y , FReal * Z, PartType type){
         int checkCount = 0;
         if(type == SOURCE || type==BOTH){
             octree->forEachLeaf([&](LeafClass* leaf){
@@ -689,7 +689,7 @@ public:
 
     template<class ContainerClass,class LeafClass,class CellClass>
     void generic_get_positions_npart(FOctree<FReal,CellClass,ContainerClass,LeafClass> * octree,
-                             int NbPositions, int * idxOfParticles,FReal * X, FReal * Y , FReal * Z,PartType type){
+                                     int NbPositions, int * idxOfParticles,FReal * X, FReal * Y , FReal * Z,PartType type){
         int checkCount = 0;
         if(type == SOURCE || type==BOTH){
             octree->forEachLeaf([&](LeafClass* leaf){
@@ -865,12 +865,12 @@ extern "C" void scalfmm_get_physical_values(scalfmm_handle Handle, int nbPhysica
 extern "C" void scalfmm_set_physical_values_npart(scalfmm_handle Handle, int nbPhysicalValues,
                                                   int* idxOfParticles, double * physicalValues, PartType type){
     ((ScalFmmCoreHandle<double> * ) Handle)->engine->set_physical_values_npart(nbPhysicalValues,
-                                                                       idxOfParticles, physicalValues, type);
+                                                                               idxOfParticles, physicalValues, type);
 }
 extern "C" void scalfmm_get_physical_values_npart(scalfmm_handle Handle, int nbPhysicalValues,
                                                   int* idxOfParticles, double * physicalValues, PartType type){
     ((ScalFmmCoreHandle<double> * ) Handle)->engine->get_physical_values_npart(nbPhysicalValues,
-                                                                       idxOfParticles, physicalValues, type);
+                                                                               idxOfParticles, physicalValues, type);
 }
 
 //To get the result
@@ -1073,6 +1073,10 @@ extern "C" void scalfmm_generic_partition(scalfmm_handle handle, FSize nbThings,
     ((ScalFmmCoreHandle<double> * ) handle)->engine->generic_partition(nbThings,sizeofthing,arrayOfThing,newArray);
 }
 
+extern "C" void scalfmm_call_delete(void * inPtr){
+    delete [] static_cast<double*>(inPtr);
+}
+
 #endif
 
 #endif
diff --git a/Addons/CKernelApi/Src/FUserKernelDistrEngine.hpp b/Addons/CKernelApi/Src/FUserKernelDistrEngine.hpp
index c1f663634..060cd563a 100644
--- a/Addons/CKernelApi/Src/FUserKernelDistrEngine.hpp
+++ b/Addons/CKernelApi/Src/FUserKernelDistrEngine.hpp
@@ -431,6 +431,8 @@ public:
     ~FUserKernelDistrEngine(){
         delete comm;
         comm = nullptr;
+        delete kernel;
+        delete octreeDist;
     }
 
     //Qu'est-ce qu'il faut que je surcharge ?
@@ -486,15 +488,20 @@ public:
 
         for(int id = 0 ; id<nbPoints ; ++id){
             FTreeCoordinate host;
-            arrayToBeSorted[id].position = FPoint<FReal>(particleXYZ[id+0],particleXYZ[id+1],particleXYZ[id+2]);
+            arrayToBeSorted[id].position = FPoint<FReal>(particleXYZ[3*id+0],
+                                                         particleXYZ[3*id+1],
+                                                         particleXYZ[3*id+2]);
             arrayToBeSorted[id].orIndex = id + Ind.getoriIntervals(myRank).first;
             arrayToBeSorted[id].orOwner = myRank;
             //Evaluate Morton Index
-            host.setX(FCoordinateComputer::GetTreeCoordinate<FReal>(particleXYZ[id*3+0] - this->getBoxCorner().getX(),this->getBoxWidth(),this->getBoxWidthAtLeafLevel(),
+            host.setX(FCoordinateComputer::GetTreeCoordinate<FReal>(particleXYZ[id*3+0] - this->getBoxCorner().getX(),
+                                                                    this->getBoxWidth(),this->getBoxWidthAtLeafLevel(),
                                                                     this->getTreeHeight()));
-            host.setX(FCoordinateComputer::GetTreeCoordinate<FReal>(particleXYZ[id*3+1] - this->getBoxCorner().getY(),this->getBoxWidth(),this->getBoxWidthAtLeafLevel(),
+            host.setX(FCoordinateComputer::GetTreeCoordinate<FReal>(particleXYZ[id*3+1] - this->getBoxCorner().getY(),
+                                                                    this->getBoxWidth(),this->getBoxWidthAtLeafLevel(),
                                                                     this->getTreeHeight()));
-            host.setX(FCoordinateComputer::GetTreeCoordinate<FReal>(particleXYZ[id*3+2] - this->getBoxCorner().getZ(),this->getBoxWidth(),this->getBoxWidthAtLeafLevel(),
+            host.setX(FCoordinateComputer::GetTreeCoordinate<FReal>(particleXYZ[id*3+2] - this->getBoxCorner().getZ(),
+                                                                    this->getBoxWidth(),this->getBoxWidthAtLeafLevel(),
                                                                     this->getTreeHeight()));
             arrayToBeSorted[id].index = host.getMortonIndex();
         }
@@ -578,11 +585,11 @@ public:
                            return A*stride;
                        });
         std::transform(Ind.getDisplSendVector().begin(),Ind.getDisplSendVector().end(),displSendByte.begin(),
-                        [&](const FSize & A){
+                       [&](const FSize & A){
                            return A*stride;
                        });
         std::transform(Ind.getDisplRecvVector().begin(),Ind.getDisplRecvVector().end(),displRecvByte.begin(),
-                        [&](const FSize & A){
+                       [&](const FSize & A){
                            return A*stride;
                        });
         std::vector<char > recvBuffer;
@@ -603,12 +610,13 @@ public:
             //Then, i need to find the index where current index
             //is sorted inside currentArray;
             FSize idSearch = 0;
-            for(idSearch=0 ; idSearch < Ind.getCount(myRank) && (Ind.getCurrentArrayValue(idSearch) != currentIndex) ; ++idSearch);
-            memcpy(&output[idSearch*stride],&recvBuffer[idRead*stride],stride);
+            for(idSearch=0 ; idSearch < Ind.getCount(myRank) && (Ind.getCurrentArrayValue(idSearch) != currentIndex) ; ++idSearch){
+                memcpy(&output[idSearch*stride],&recvBuffer[idRead*stride],stride);
+            }
         }
 
         //C Part
-        *newArray = malloc(output.size());
+        *newArray = new char[output.size()];
         memcpy(*newArray,output.data(),output.size());
     }
 
@@ -696,13 +704,13 @@ public:
         //Only one config shall work , so let's use it
         switch(FScalFMMEngine<FReal>::Algorithm){
         case 5:
-            {
-                typedef FFmmAlgorithmThreadProc<OctreeClass,CoreCellDist,ContainerClass,CoreKernelClass,LeafClass> AlgoProcClass;
-                AlgoProcClass * algoProc = new AlgoProcClass(*comm,octreeDist,kernel);
-                FScalFMMEngine<FReal>::algoTimer = algoProc;
-                algoProc->execute(FFmmP2M | FFmmM2M | FFmmM2L | FFmmL2L | FFmmL2P | FFmmP2P);
-                break;
-            }
+        {
+            typedef FFmmAlgorithmThreadProc<OctreeClass,CoreCellDist,ContainerClass,CoreKernelClass,LeafClass> AlgoProcClass;
+            AlgoProcClass * algoProc = new AlgoProcClass(*comm,octreeDist,kernel);
+            FScalFMMEngine<FReal>::algoTimer = algoProc;
+            algoProc->execute(FFmmP2M | FFmmM2M | FFmmM2L | FFmmL2L | FFmmL2P | FFmmP2P);
+            break;
+        }
         default:
             break;
         }
@@ -714,13 +722,13 @@ public:
         //Only one config shall work , so let's use it
         switch(FScalFMMEngine<FReal>::Algorithm){
         case 5:
-            {
-                typedef FFmmAlgorithmThreadProc<OctreeClass,CoreCellDist,ContainerClass,CoreKernelClass,LeafClass> AlgoProcClass;
-                AlgoProcClass * algoProc = new AlgoProcClass(*comm,octreeDist,kernel);
-                FScalFMMEngine<FReal>::algoTimer = algoProc;
-                algoProc->execute(FFmmP2M | FFmmM2M | FFmmM2L | FFmmL2L | FFmmL2P);
-                break;
-            }
+        {
+            typedef FFmmAlgorithmThreadProc<OctreeClass,CoreCellDist,ContainerClass,CoreKernelClass,LeafClass> AlgoProcClass;
+            AlgoProcClass * algoProc = new AlgoProcClass(*comm,octreeDist,kernel);
+            FScalFMMEngine<FReal>::algoTimer = algoProc;
+            algoProc->execute(FFmmP2M | FFmmM2M | FFmmM2L | FFmmL2L | FFmmL2P);
+            break;
+        }
         default:
             break;
         }
diff --git a/Addons/CKernelApi/Tests/testMpiUserKernel.c b/Addons/CKernelApi/Tests/testMpiUserKernel.c
index 8896a4219..9cf74ed5d 100644
--- a/Addons/CKernelApi/Tests/testMpiUserKernel.c
+++ b/Addons/CKernelApi/Tests/testMpiUserKernel.c
@@ -175,9 +175,9 @@ void on_leaf(int level, FSize nbParts, const FSize * idxParts, long long morton_
         double pot = 0;
 
         for(int i=0 ; i<nbParts ; ++i){
-            pot += potentials[i];
+            ptrToUserData->totalEnergy += potentials[i]*(ptrToUserData->myPhyValues[idxParts[i]]);
         }
-        ptrToUserData->totalEnergy += pot*(ptrToUserData->myPhyValues[idxParts[i]]);
+
     }
     /* printf("I'm leaf at %lld pos, of center [%e %e %e], containing %lld parts\n", */
     /*        morton_index,center[0],center[1],center[2],nbParts); */
@@ -319,8 +319,6 @@ int main(int argc, char ** argv){
     //Only read, so no split needed
     userDatas.insertedPositions = outputArray;                     // Set the position
 
-    //Need to get the good ones...
-    double * newPhyValues = malloc(sizeof(double) * outputNbPoint);
     //In this part, we store the physicalvalues
 
     //Create as many array of forces as there are threads in order to
@@ -364,6 +362,7 @@ int main(int argc, char ** argv){
     //Dealloc scalfmm handle
     scalfmm_dealloc_handle(Handle,cheb_free_cell);
 
+
     {//This part will write generated particles to a file at ScalFMM
      //format in order to verify numercal results
         /* if(my_rank==0){ */
@@ -395,8 +394,9 @@ int main(int argc, char ** argv){
         /* } */
     }
 
-    free(outputIndexes);
-    free(outputArray);
+    scalfmm_call_delete(outputIndexes);
+    scalfmm_call_delete(outputArray);
+    scalfmm_call_delete(*outputPhyValPtr);
 
 
     MPI_Finalize();
-- 
GitLab