Commit d1431d95 authored by BRAMAS Berenger's avatar BRAMAS Berenger
Browse files

make hmtrix work and add a test based on a file

parent 33aad35f
......@@ -105,6 +105,33 @@ public:
}
template<class FReal>
static bool read(const std::string filename, FReal** matrix, int* nbRows, int* nbCols){
std::ifstream stream(filename.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
if (!stream.good()) {
return false;
}
const std::ifstream::pos_type size = stream.tellg();
if (size<=0) {
std::cout << "Info: The requested binary file " << filename
<< " does not yet exist. Compute it now ... " << std::endl;
return false;
}
stream.seekg(0);
stream.read(reinterpret_cast<char*>(nbRows), sizeof(int));
stream.read(reinterpret_cast<char*>(nbCols), sizeof(int));
delete[] (*matrix);
(*matrix) = new FReal[(*nbRows)*(*nbCols)];
stream.read(reinterpret_cast<char*>((*matrix)), sizeof(FReal)*(*nbRows)*(*nbCols));
stream.close();
return true;
}
};
......
......@@ -8,6 +8,7 @@
#include "FDenseBlockWrapper.hpp"
#include "../Utils/FHUtils.hpp"
#include "../Utils/FMatrixIO.hpp"
template <class FReal>
class FMatDense {
......@@ -33,10 +34,23 @@ public:
}
}
explicit FMatDense(const char inFilename[])
: matDim(0), values(nullptr){
int readNbRows = 0;
int readNbCols = 0;
FAssertLF(FMatrixIO::read(inFilename, &values, &readNbRows, &readNbCols));
FAssertLF(readNbRows == readNbCols);
matDim = readNbRows;
}
~FMatDense(){
delete[] values;
}
int getDim() const{
return matDim;
}
const FReal& getVal(const int idxRow , const int idxCol) const{
return values[idxCol*matDim+idxRow];
}
......
......@@ -20,17 +20,11 @@ int main(int argc, char** argv){
{"-N", "-nb", "-dim"} ,
"Dim of the matrix."
};
static const FParameterNames HeightParam = {
{"-h", "-height"} ,
"Number of dissection (+1)."
};
FHelpDescribeAndExit(argc, argv,
"Test the bisection.",SvgOutParam,
DimParam,HeightParam);
FHelpDescribeAndExit(argc, argv,"Test the bisection.",SvgOutParam,DimParam,FParameterDefinitions::OctreeHeight);
const int dim = FParameters::getValue(argc, argv, DimParam.options, 100);
const int height = FParameters::getValue(argc, argv, HeightParam.options, 4);
const int height = FParameters::getValue(argc, argv, FParameterDefinitions::OctreeHeight.options, 4);
const char* outputdir = FParameters::getStr(argc, argv, SvgOutParam.options, "/tmp/");
std::cout << "Config : dim = " << dim << "\n";
......
......@@ -15,15 +15,11 @@ int main(int argc, char** argv){
{"-N", "-nb", "-dim"} ,
"Dim of the matrix."
};
static const FParameterNames HeightParam = {
{"-h", "-height"} ,
"Number of dissection (+1)."
};
FHelpDescribeAndExit(argc, argv, "Test the bisection.", DimParam,HeightParam);
FHelpDescribeAndExit(argc, argv, "Test the bisection.", DimParam, FParameterDefinitions::OctreeHeight);
const int dim = FParameters::getValue(argc, argv, DimParam.options, 100);
const int height = FParameters::getValue(argc, argv, HeightParam.options, 4);
const int height = FParameters::getValue(argc, argv, FParameterDefinitions::OctreeHeight.options, 4);
std::cout << "Config : dim = " << dim << "\n";
std::cout << "Config : height = " << height << "\n";
......
// @SCALFMM_PRIVATE
#include "../Src/Containers/FDiv2Bissection.hpp"
#include "../Src/Viewers/FMatDense.hpp"
#include "../Src/Blocks/FDenseMatrix.hpp"
#include "Utils/FParameters.hpp"
#include "Utils/FParameterNames.hpp"
#include <memory>
int main(int argc, char** argv){
FHelpDescribeAndExit(argc, argv, "Test the bisection.", FParameterDefinitions::InputFile, FParameterDefinitions::OctreeHeight);
const char* filename = FParameters::getStr(argc, argv, FParameterDefinitions::InputFile.options, "../Addons/HMat/Data/unitCube1000_ONE_OVER_R.bin");
const int height = FParameters::getValue(argc, argv, FParameterDefinitions::OctreeHeight.options, 4);
typedef double FReal;
typedef FMatDense<FReal> MatrixClass;
MatrixClass matrix(filename);
const int dim = matrix.getDim();
std::cout << "Config : dim = " << dim << "\n";
std::cout << "Config : height = " << height << "\n";
std::unique_ptr<FReal[]> vec(new FReal[dim]);
for(int idxVal = 0 ; idxVal < dim ; ++idxVal){
vec[idxVal] = 1.0;
}
std::unique_ptr<FReal[]> resTest(new FReal[dim]);
FSetToZeros(resTest.get(), dim);
{
for(int idxRow = 0; idxRow < dim ; ++idxRow){
for(int idxCol = 0; idxCol < dim ; ++idxCol){
resTest[idxRow] += vec[idxCol] * matrix.getVal(idxRow, idxCol);
}
}
}
{
typedef FDenseMatrix<FReal> LeafClass;
typedef FDenseMatrix<FReal> CellClass;
typedef FDiv2Bissection<FReal, LeafClass, CellClass> GridClass;
GridClass grid(dim, height);
grid.fillBlocks(matrix);
std::unique_ptr<FReal[]> resDense(new FReal[dim]);
FSetToZeros(resDense.get(), dim);
grid.gemv(resDense.get(), vec.get());
FMath::FAccurater<FReal> testDense(resTest.get(), resDense.get(), dim);
std::cout << "Test Dense, Error = " << testDense << "\n";
}
{
typedef FDenseMatrix<FReal> LeafClass;
typedef FDenseMatrix<FReal> CellClass;
typedef FDiv2Bissection<FReal, LeafClass, CellClass> GridClass;
const int nbPartitions = FMath::pow2(height-1);
std::unique_ptr<int[]> partitions(new int[nbPartitions]);
{
int nbValuesLeft = dim;
for(int idxPartition = 0 ; idxPartition < nbPartitions-1 ; ++idxPartition){
partitions[idxPartition] = FMath::Max(1, int(drand48()*(nbValuesLeft-(nbPartitions-idxPartition))));
nbValuesLeft -= partitions[idxPartition];
}
partitions[nbPartitions-1] = nbValuesLeft;
}
GridClass grid(dim, height, partitions.get(), nbPartitions);
grid.fillBlocks(matrix);
std::unique_ptr<FReal[]> resDense(new FReal[dim]);
FSetToZeros(resDense.get(), dim);
grid.gemv(resDense.get(), vec.get());
FMath::FAccurater<FReal> testDense(resTest.get(), resDense.get(), dim);
std::cout << "Test Dense with partitions, Error = " << testDense << "\n";
}
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment