FChebSymM2LHandler.hpp 22.2 KB
Newer Older
BRAMAS Berenger's avatar
BRAMAS Berenger committed
1
// ===================================================================================
2 3 4 5
// Copyright ScalFmm 2016 INRIA, Olivier Coulaud, Bérenger Bramas,
// Matthias Messner olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// This software is a computer program whose purpose is to compute the
// FMM.
6
//
7
// This software is governed by the CeCILL-C and LGPL licenses and
8
// abiding by the rules of distribution of free software.
9 10 11
// An extension to the license is given to allow static linking of scalfmm
// inside a proprietary application (no matter its license).
// See the main license file for more details.
12
//
13 14 15
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 17 18
// GNU General Public and CeCILL-C Licenses for more details.
// "http://www.cecill.info".
// "http://www.gnu.org/licenses".
BRAMAS Berenger's avatar
BRAMAS Berenger committed
19
// ===================================================================================
20 21 22 23
#ifndef FCHEBSYMM2LHANDLER_HPP
#define FCHEBSYMM2LHANDLER_HPP

#include <climits>
COULAUD Olivier's avatar
COULAUD Olivier committed
24
#include <sstream>
25

COULAUD Olivier's avatar
COULAUD Olivier committed
26
#include "Utils/FBlas.hpp"
27

28

COULAUD Olivier's avatar
COULAUD Olivier committed
29
#include "FChebTensor.hpp"
30
#include "../Interpolation/FInterpSymmetries.hpp"
COULAUD Olivier's avatar
COULAUD Olivier committed
31
#include "FChebM2LHandler.hpp"
32

BLANCHARD Pierre's avatar
BLANCHARD Pierre committed
33 34 35
#include "Utils/FAca.hpp"


36 37 38 39 40 41
/**
 * @author Matthias Messner (matthias.matthias@inria.fr)
 * Please read the license
 */


Matthias Messner's avatar
Matthias Messner committed
42
/*!  Choose either \a FULLY_PIVOTED_ACASVD or \a PARTIALLY_PIVOTED_ACASVD or
43
    \a ONLY_SVD.
COULAUD Olivier's avatar
COULAUD Olivier committed
44
 */
45
//#define ONLY_SVD
Matthias Messner's avatar
Matthias Messner committed
46
//#define FULLY_PIVOTED_ACASVD
47
#define PARTIALLY_PIVOTED_ACASVD
Matthias Messner's avatar
Matthias Messner committed
48 49


Matthias Messner's avatar
Matthias Messner committed
50

Matthias Messner's avatar
Matthias Messner committed
51 52 53 54 55
/*!  Precomputes the 16 far-field interactions (due to symmetries in their
  arrangement all 316 far-field interactions can be represented by
  permutations of the 16 we compute in this function). Depending on whether
  FACASVD is defined or not, either ACA+SVD or only SVD is used to compress
  them. */
56
template <class FReal, int ORDER, typename MatrixKernelClass>
57
static void precompute(const MatrixKernelClass *const MatrixKernel, const FReal CellWidth,
58
        const FReal Epsilon, FReal* K[343], int LowRank[343])
59
{
60 61
    //  std::cout << "\nComputing 16 far-field interactions (l=" << ORDER << ", eps=" << Epsilon
    //                      << ") for cells of width w = " << CellWidth << std::endl;
62

63
    static const unsigned int nnodes = ORDER*ORDER*ORDER;
64

65
    // interpolation points of source (Y) and target (X) cell
66
    FPoint<FReal> X[nnodes], Y[nnodes];
67
    // set roots of target cell (X)
68
    FChebTensor<FReal, ORDER>::setRoots(FPoint<FReal>(0.,0.,0.), CellWidth, X);
69 70
    // temporary matrix
    FReal* U = new FReal [nnodes*nnodes];
71

72 73 74 75 76 77
    // needed for the SVD
     int INFO;
    const unsigned int LWORK = 2 * (3*nnodes + nnodes);
    FReal *const WORK = new FReal [LWORK];
    FReal *const VT = new FReal [nnodes*nnodes];
    FReal *const S = new FReal [nnodes];
Matthias Messner's avatar
Matthias Messner committed
78 79


80 81 82 83
    // initialize timer
    FTic time;
    double overall_time(0.);
    double elapsed_time(0.);
84

85 86
    // initialize rank counter
    unsigned int overall_rank = 0;
Matthias Messner's avatar
Matthias Messner committed
87

88 89 90 91
    unsigned int counter = 0;
    for (int i=2; i<=3; ++i) {
        for (int j=0; j<=i; ++j) {
            for (int k=0; k<=j; ++k) {
92

93
                // assemble matrix and apply weighting matrices
94 95
                const FPoint<FReal> cy(CellWidth*FReal(i), CellWidth*FReal(j), CellWidth*FReal(k));
                FChebTensor<FReal, ORDER>::setRoots(cy, CellWidth, Y);
96
                FReal weights[nnodes];
97
                FChebTensor<FReal, ORDER>::setRootOfWeights(weights);
98

99
                // now the entry-computer is responsible for weighting the matrix entries
100
                EntryComputer<FReal, MatrixKernelClass> Computer(MatrixKernel, nnodes, X, nnodes, Y, weights);
Matthias Messner's avatar
Matthias Messner committed
101

102 103
                // start timer
                time.tic();
Matthias Messner's avatar
Matthias Messner committed
104 105

#if (defined ONLY_SVD || defined FULLY_PIVOTED_ACASVD)
106
                Computer(0, nnodes, 0, nnodes, U);
Matthias Messner's avatar
Matthias Messner committed
107
#endif
108 109 110
                /*
                // applying weights ////////////////////////////////////////
                FReal weights[nnodes];
111
                FChebTensor<FReal,ORDER>::setRootOfWeights(weights);
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
                for (unsigned int n=0; n<nnodes; ++n) {
                    FBlas::scal(nnodes, weights[n], U + n,  nnodes); // scale rows
                    FBlas::scal(nnodes, weights[n], U + n * nnodes); // scale cols
                }
                 */

                //////////////////////////////////////////////////////////////
                //////////////////////////////////////////////////////////////
                //////////////////////////////////////////////////////////////
                // ALL PREPROC FLAGS ARE SET ON TOP OF THIS FILE !!! /////////
                //////////////////////////////////////////////////////////////
                //////////////////////////////////////////////////////////////
                //////////////////////////////////////////////////////////////



                //////////////////////////////////////////////////////////////
Matthias Messner's avatar
Matthias Messner committed
129
#if (defined FULLY_PIVOTED_ACASVD || defined PARTIALLY_PIVOTED_ACASVD) ////////////
130 131
                FReal *UU, *VV;
                unsigned int rank;
Matthias Messner's avatar
Matthias Messner committed
132 133

#ifdef FULLY_PIVOTED_ACASVD
BLANCHARD Pierre's avatar
BLANCHARD Pierre committed
134
                FAca::fACA(U,        nnodes, nnodes, Epsilon, UU, VV, rank);
Matthias Messner's avatar
Matthias Messner committed
135
#else
BLANCHARD Pierre's avatar
BLANCHARD Pierre committed
136
                FAca::pACA(Computer, nnodes, nnodes, Epsilon, UU, VV, rank);
Matthias Messner's avatar
Matthias Messner committed
137
#endif 
Matthias Messner's avatar
Matthias Messner committed
138

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
                // QR decomposition
                FReal* phi = new FReal [rank*rank];
                {
                    // QR of U and V
                    FReal* tauU = new FReal [rank];
                    INFO = FBlas::geqrf(nnodes, rank, UU, tauU, LWORK, WORK);
                    assert(INFO==0);
                    FReal* tauV = new FReal [rank];
                    INFO = FBlas::geqrf(nnodes, rank, VV, tauV, LWORK, WORK);
                    assert(INFO==0);
                    // phi = Ru Rv'
                    FReal* rU = new FReal [2 * rank*rank];
                    FReal* rV = rU + rank*rank;
                    FBlas::setzero(2 * rank*rank, rU);
                    for (unsigned int l=0; l<rank; ++l) {
                        FBlas::copy(l+1, UU + l*nnodes, rU + l*rank);
                        FBlas::copy(l+1, VV + l*nnodes, rV + l*rank);
                    }
                    FBlas::gemmt(rank, rank, rank, FReal(1.), rU, rank, rV, rank, phi, rank);
                    delete [] rU;
                    // get Qu and Qv
                    INFO = FBlas::orgqr(nnodes, rank, UU, tauU, LWORK, WORK);
                    assert(INFO==0);
                    INFO = FBlas::orgqr(nnodes, rank, VV, tauV, LWORK, WORK);
                    assert(INFO==0);
                    delete [] tauU;
                    delete [] tauV;
                }

                const unsigned int aca_rank = rank;

                // SVD
                {
                    INFO = FBlas::gesvd(aca_rank, aca_rank, phi, S, VT, aca_rank, LWORK, WORK);
                    if (INFO!=0){
                        std::stringstream stream;
                        stream << INFO;
                        throw std::runtime_error("SVD did not converge with " + stream.str());
                    }
                    rank = getRank(S, aca_rank, Epsilon);
                }                   

                const unsigned int idx = (i+3)*7*7 + (j+3)*7 + (k+3);

                // store
                {
                    // allocate
                    assert(K[idx]==nullptr);
                    K[idx] = new FReal [2*rank*nnodes];

                    // set low rank
                    LowRank[idx] = static_cast<int>(rank);

                    // (U Sigma)
                    for (unsigned int r=0; r<rank; ++r)
                        FBlas::scal(aca_rank, S[r], phi + r*aca_rank);

                    // Qu (U Sigma) 
                    FBlas::gemm(nnodes, aca_rank, rank, FReal(1.), UU, nnodes, phi, aca_rank, K[idx], nnodes);
                    delete [] phi;

                    // Vt -> V and then Qu V
                    FReal *const V = new FReal [aca_rank * rank];
                    for (unsigned int r=0; r<rank; ++r)
                        FBlas::copy(aca_rank, VT + r, aca_rank, V + r*aca_rank, 1);
                    FBlas::gemm(nnodes, aca_rank, rank, FReal(1.), VV, nnodes, V, aca_rank, K[idx] + rank*nnodes, nnodes);
                    delete [] V;
                }

                //// store recompressed UV
                //const unsigned int idx = (i+3)*7*7 + (j+3)*7 + (k+3);
                //assert(K[idx]==NULL);
                //K[idx] = new FReal [2*rank*nnodes];
                //LowRank[idx] = rank;
                //FBlas::copy(rank*nnodes, UU,  K[idx]);
                //FBlas::copy(rank*nnodes, VV,  K[idx] + rank*nnodes);

                delete [] UU;
                delete [] VV;

                elapsed_time = time.tacAndElapsed(); 
                overall_time += elapsed_time;
                overall_rank += rank;
                // std::cout << "(" << i << "," << j << "," << k << ") " << idx <<
                //  ", low rank = " << rank << " (" << aca_rank << ") in " << elapsed_time << "s" << std::endl;

                //////////////////////////////////////////////////////////////
                //////////////////////////////////////////////////////////////
                //////////////////////////////////////////////////////////////
                // ALL PREPROC FLAGS ARE SET ON TOP OF THIS FILE !!! /////////
                //////////////////////////////////////////////////////////////
                //////////////////////////////////////////////////////////////
                //////////////////////////////////////////////////////////////
Matthias Messner's avatar
Matthias Messner committed
232 233

#elif defined ONLY_SVD
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
                // truncated singular value decomposition of matrix
                INFO = FBlas::gesvd(nnodes, nnodes, U, S, VT, nnodes, LWORK, WORK);
                if (INFO!=0){
                    std::stringstream stream;
                    stream << INFO;
                    throw std::runtime_error("SVD did not converge with " + stream.str());
                }
                const unsigned int rank = getRank<ORDER>(S, Epsilon);

                // store 
                const unsigned int idx = (i+3)*7*7 + (j+3)*7 + (k+3);
                assert(K[idx]==nullptr);
                K[idx] = new FReal [2*rank*nnodes];
                LowRank[idx] = rank;
                for (unsigned int r=0; r<rank; ++r)
                    FBlas::scal(nnodes, S[r], U + r*nnodes);
                FBlas::copy(rank*nnodes, U,  K[idx]);
                for (unsigned int r=0; r<rank; ++r)
                    FBlas::copy(nnodes, VT + r, nnodes, K[idx] + rank*nnodes + r*nnodes, 1);

                elapsed_time = time.tacAndElapsed(); 
                overall_time += elapsed_time;
                overall_rank += rank;
                //              std::cout << "(" << i << "," << j << "," << k << ") " << idx <<
                //  ", low rank = " << rank << " in " << elapsed_time << "s" << std::endl;
Matthias Messner's avatar
Matthias Messner committed
259 260
#else
#error Either fully-, partially pivoted ACA or only SVD must be defined!
Matthias Messner's avatar
Matthias Messner committed
261
#endif ///////////////////////////////////////////////////////////////
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
                //////////////////////////////////////////////////////////////


                //////////////////////////////////////////////////////////////
                //////////////////////////////////////////////////////////////
                //////////////////////////////////////////////////////////////
                // ALL PREPROC FLAGS ARE SET ON TOP OF THIS FILE !!! /////////
                //////////////////////////////////////////////////////////////
                //////////////////////////////////////////////////////////////
                //////////////////////////////////////////////////////////////


                // un-weighting ////////////////////////////////////////////
                for (unsigned int n=0; n<nnodes; ++n) {
                    FBlas::scal(rank, FReal(1.) / weights[n], K[idx] + n,               nnodes); // scale rows
                    FBlas::scal(rank, FReal(1.) / weights[n], K[idx] + rank*nnodes + n, nnodes); // scale rows
                }
                //////////////////////////////////////////////////////////      

                ++counter;
            }
        }
    }
    //std::cout << "The approximation of the " << counter
    //      << " far-field interactions (overall rank " << overall_rank
    //      << " / " << 16*nnodes
    //      << " , sizeM2L= " << 2*overall_rank*nnodes*sizeof(FReal) << ""
    //      << " / " << 16*nnodes*nnodes*sizeof(FReal) << " B"
    //      << ") took " << overall_time << "s\n" << std::endl;

    std::cout << "Compressed and set M2L operators (" << 2*overall_rank*nnodes*sizeof(FReal) << " B) in " << overall_time << "sec." << std::endl;

    delete [] U;
    delete [] WORK;
    delete [] VT;
    delete [] S;
298 299 300 301 302 303 304 305 306 307
}









Matthias Messner's avatar
Matthias Messner committed
308
/*!  \class SymmetryHandler 
309

310
    \brief Deals with all the symmetries in the arrangement of the far-field interactions
Matthias Messner's avatar
Matthias Messner committed
311

312
    Stores permutation indices and permutation vectors to reduce 316 (7^3-3^3)
Matthias Messner's avatar
Matthias Messner committed
313 314 315 316
  different far-field interactions to 16 only. We use the number 343 (7^3)
  because it allows us to use to associate the far-field interactions based on
  the index \f$t = 7^2(i+3) + 7(j+3) + (k+3)\f$ where \f$(i,j,k)\f$ denotes
  the relative position of the source cell to the target cell. */
317
template <class FReal, int ORDER, KERNEL_FUNCTION_TYPE TYPE> class SymmetryHandler;
318 319

/*! Specialization for homogeneous kernel functions */
320 321
template <class FReal, int ORDER>
class SymmetryHandler<FReal, ORDER, HOMOGENEOUS>
322
{
323
    static const unsigned int nnodes = ORDER*ORDER*ORDER;
324

325 326 327
    // M2L operators
    FReal*    K[343];
    int LowRank[343];
328 329

public:
COULAUD Olivier's avatar
COULAUD Olivier committed
330

331 332 333
    // permutation vectors and permutated indices
    unsigned int pvectors[343][nnodes];
    unsigned int pindices[343];
334 335


336 337 338 339 340 341 342 343 344 345
    /** Constructor: with 16 small SVDs */
    template <typename MatrixKernelClass>
    SymmetryHandler(const MatrixKernelClass *const MatrixKernel, const FReal Epsilon,
                    const FReal, const unsigned int)
    {
        // init all 343 item to zero, because effectively only 16 exist
        for (unsigned int t=0; t<343; ++t) {
            K[t]            = nullptr;
            LowRank[t] = 0;
        }
COULAUD Olivier's avatar
COULAUD Olivier committed
346

347 348 349 350 351 352 353 354 355 356
        // set permutation vector and indices
        const FInterpSymmetries<ORDER> Symmetries;
        for (int i=-3; i<=3; ++i)
            for (int j=-3; j<=3; ++j)
                for (int k=-3; k<=3; ++k) {
                    const unsigned int idx = ((i+3) * 7 + (j+3)) * 7 + (k+3);
                    pindices[idx] = 0;
                    if (abs(i)>1 || abs(j)>1 || abs(k)>1)
                        pindices[idx] = Symmetries.getPermutationArrayAndIndex(i,j,k, pvectors[idx]);
                }
357

358 359
        // precompute 16 M2L operators
        const FReal ReferenceCellWidth = FReal(2.0);
360
        precompute<FReal, ORDER>(MatrixKernel, ReferenceCellWidth, Epsilon, K, LowRank);
361
    }
362 363 364



365 366 367 368 369
    /** Destructor */
    ~SymmetryHandler()
    {
        for (unsigned int t=0; t<343; ++t) if (K[t]!=nullptr) delete [] K[t];
    }
370

371

372
    /*! return the t-th approximated far-field interactions*/
373
    const FReal * getK(const  int, const unsigned int t) const
374
    {   return K[t]; }
375

376
    /*! return the t-th approximated far-field interactions*/
377
    int getLowRank(const int, const unsigned int t) const
378
    {   return LowRank[t]; }
379 380 381 382 383 384 385 386 387

};






/*! Specialization for non-homogeneous kernel functions */
388 389
template <class FReal, int ORDER>
class SymmetryHandler<FReal, ORDER, NON_HOMOGENEOUS>
390
{
391
    static const unsigned int nnodes = ORDER*ORDER*ORDER;
392

393 394
    // Height of octree; needed only in the case of non-homogeneous kernel functions
    const unsigned int TreeHeight;
395

396 397 398
    // M2L operators for all levels in the octree
    FReal***    K;
    int** LowRank;
399 400

public:
COULAUD Olivier's avatar
COULAUD Olivier committed
401

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
    // permutation vectors and permutated indices
    unsigned int pvectors[343][nnodes];
    unsigned int pindices[343];


    /** Constructor: with 16 small SVDs */
    template <typename MatrixKernelClass>
    SymmetryHandler(const MatrixKernelClass *const MatrixKernel, const double Epsilon,
                    const FReal RootCellWidth, const unsigned int inTreeHeight)
    : TreeHeight(inTreeHeight)
    {
        // init all 343 item to zero, because effectively only 16 exist
        K       = new FReal** [TreeHeight];
        LowRank = new int*    [TreeHeight];
        K[0]       = nullptr; K[1]       = nullptr;
        LowRank[0] = nullptr; LowRank[1] = nullptr;
        for (unsigned int l=2; l<TreeHeight; ++l) {
            K[l]       = new FReal* [343];
            LowRank[l] = new int    [343];
            for (unsigned int t=0; t<343; ++t) {
                K[l][t]       = nullptr;
                LowRank[l][t] = 0;
            }
        }


        // set permutation vector and indices
        const FInterpSymmetries<ORDER> Symmetries;
        for (int i=-3; i<=3; ++i)
            for (int j=-3; j<=3; ++j)
                for (int k=-3; k<=3; ++k) {
                    const unsigned int idx = ((i+3) * 7 + (j+3)) * 7 + (k+3);
                    pindices[idx] = 0;
                    if (abs(i)>1 || abs(j)>1 || abs(k)>1)
                        pindices[idx] = Symmetries.getPermutationArrayAndIndex(i,j,k, pvectors[idx]);
                }

        // precompute 16 M2L operators at all levels having far-field interactions
        FReal CellWidth = RootCellWidth / FReal(2.); // at level 1
        CellWidth /= FReal(2.);                      // at level 2
        for (unsigned int l=2; l<TreeHeight; ++l) {
443
            precompute<FReal,ORDER>(MatrixKernel, CellWidth, Epsilon, K[l], LowRank[l]);
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
            CellWidth /= FReal(2.);                    // at level l+1 
        }
    }



    /** Destructor */
    ~SymmetryHandler()
    {
        for (unsigned int l=0; l<TreeHeight; ++l) {
            if (K[l]!=nullptr) {
                for (unsigned int t=0; t<343; ++t) if (K[l][t]!=nullptr) delete [] K[l][t];
                delete [] K[l];
            }
            if (LowRank[l]!=nullptr)    delete [] LowRank[l];
        }
        delete [] K;
        delete [] LowRank;
    }

    /*! return the t-th approximated far-field interactions*/
465
    const FReal * getK(const  int l, const unsigned int t) const
466 467 468
    {   return K[l][t]; }

    /*! return the t-th approximated far-field interactions*/
469
    int getLowRank(const  int l, const unsigned int t) const
470
    {   return LowRank[l][t]; }
471

472 473 474 475 476 477 478 479 480 481 482 483 484
};








#include <fstream>
#include <sstream>


Matthias Messner's avatar
Matthias Messner committed
485 486 487
/**
 * Computes, compresses and stores the 16 M2L kernels in a binary file.
 */
488
template <class FReal, int ORDER, typename MatrixKernelClass>
489 490
static void ComputeAndCompressAndStoreInBinaryFile(const MatrixKernelClass *const MatrixKernel, const FReal Epsilon)
{
491 492 493 494 495 496
    static const unsigned int nnodes = ORDER*ORDER*ORDER;

    // compute and compress ////////////
    FReal* K[343];
    int LowRank[343];
    for (unsigned int idx=0; idx<343; ++idx) { K[idx] = nullptr; LowRank[idx] = 0;  }
497
    precompute<FReal,ORDER>(MatrixKernel, FReal(2.), Epsilon, K, LowRank);
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530

    // write to binary file ////////////
    FTic time; time.tic();
    // start computing process
    const char precision = (typeid(FReal)==typeid(double) ? 'd' : 'f');
    std::stringstream sstream;
    sstream << "sym2l_" << precision << "_o" << ORDER << "_e" << Epsilon << ".bin";
    const std::string filename(sstream.str());
    std::ofstream stream(filename.c_str(),
            std::ios::out | std::ios::binary | std::ios::trunc);
    if (stream.good()) {
        stream.seekp(0);
        for (unsigned int idx=0; idx<343; ++idx)
            if (K[idx]!=nullptr) {
                // 1) write index
                stream.write(reinterpret_cast<char*>(&idx), sizeof(int));
                // 2) write low rank (int)
                int rank = LowRank[idx];
                stream.write(reinterpret_cast<char*>(&rank), sizeof(int));
                // 3) write U and V (both: rank*nnodes * FReal)
                FReal *const U = K[idx];
                FReal *const V = K[idx] + rank*nnodes;
                stream.write(reinterpret_cast<char*>(U), sizeof(FReal)*rank*nnodes);
                stream.write(reinterpret_cast<char*>(V), sizeof(FReal)*rank*nnodes);
            }
    } else throw std::runtime_error("File could not be opened to write");
    stream.close();
    // write info
    //  std::cout << "Compressed M2L operators stored in binary file " << filename
    //                  << " in " << time.tacAndElapsed() << "sec." << std::endl;

    // free memory /////////////////////
    for (unsigned int t=0; t<343; ++t) if (K[t]!=nullptr) delete [] K[t];
531 532 533
}


Matthias Messner's avatar
Matthias Messner committed
534 535 536 537
/**
 * Reads the 16 compressed M2L kernels from the binary files and writes them
 * in K and the respective low-rank in LowRank.
 */
538
template <class FReal, int ORDER>
539 540
void ReadFromBinaryFile(const FReal Epsilon, FReal* K[343], int LowRank[343])
{
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
    // compile time constants
    const unsigned int nnodes = ORDER*ORDER*ORDER;

    // find filename
    const char precision = (typeid(FReal)==typeid(double) ? 'd' : 'f');
    std::stringstream sstream;
    sstream << "sym2l_" << precision << "_o" << ORDER << "_e" << Epsilon << ".bin";
    const std::string filename(sstream.str());

    // read binary file
    std::ifstream istream(filename.c_str(),
            std::ios::in | std::ios::binary | std::ios::ate);
    const std::ifstream::pos_type size = istream.tellg();
    if (size<=0) throw std::runtime_error("The requested binary file does not yet exist. Exit.");

    if (istream.good()) {
        istream.seekg(0);
        // 1) read index (int)
        int _idx;
        istream.read(reinterpret_cast<char*>(&_idx), sizeof(int));
        // loop to find 16 compressed m2l operators
        for (int idx=0; idx<343; ++idx) {
            K[idx] = nullptr;
            LowRank[idx] = 0;
            // if it exists
            if (idx == _idx) {
                // 2) read low rank (int)
                int rank;
                istream.read(reinterpret_cast<char*>(&rank), sizeof(int));
                LowRank[idx] = rank;
                // 3) read U and V (both: rank*nnodes * FReal)
                K[idx] = new FReal [2*rank*nnodes];
                FReal *const U = K[idx];
                FReal *const V = K[idx] + rank*nnodes;
                istream.read(reinterpret_cast<char*>(U), sizeof(FReal)*rank*nnodes);
                istream.read(reinterpret_cast<char*>(V), sizeof(FReal)*rank*nnodes);

                // 1) read next index
                istream.read(reinterpret_cast<char*>(&_idx), sizeof(int));
            }
        }
    }   else throw std::runtime_error("File could not be opened to read");
    istream.close();
584 585 586 587 588 589 590
}





#endif