FACABlock.hpp 5.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
// ===================================================================================
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Berenger Bramas, Matthias Messner
// olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// This software is a computer program whose purpose is to compute the FMM.
//
// This software is governed by the CeCILL-C and LGPL licenses and
// abiding by the rules of distribution of free software.
//
// 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
// GNU General Public and CeCILL-C Licenses for more details.
// "http://www.cecill.info".
// "http://www.gnu.org/licenses".
// ===================================================================================
// 
// @SCALFMM_PRIVATE
// 
#ifndef FACABLOCK_HPP
#define FACABLOCK_HPP

#include "Utils/FBlas.hpp"
#include "Utils/FAca.hpp"


/*!  Choose either \a FULLY_PIVOTED_ACA or \a PARTIALLY_PIVOTED_ACA */
#define FULLY_PIVOTED_ACA
//#define PARTIALLY_PIVOTED_ACA

/*!  Choose \a RECOMPRESSED_ACA */
//#define RECOMPRESSED_ACA


template <class FReal, int ORDER = 14>
class FACABlock{
protected:
    // members
    FReal* block;
    FReal* U;
    FReal* VT;
    int nbRows;
    int nbCols;
    int level;
    unsigned int rank;
    FReal accuracy;
    FACABlock(const FACABlock&) = delete;
    FACABlock& operator=(const FACABlock&) = delete;

public:
    FACABlock()
        : block(nullptr), U(nullptr), VT(nullptr), nbRows(0), nbCols(0),  level(0), rank(0), accuracy(FMath::pow(10.0,static_cast<FReal>(-ORDER))) {
    }

    // ctor
    template <class ViewerClass>
    void fill(const ViewerClass& viewer, const int inLevel){
        clear();
        // Allocate memory
        level  = inLevel;
        nbRows = viewer.getNbRows();
        nbCols = viewer.getNbCols();


#if (defined FULLY_PIVOTED_ACA)
        block  = new FReal[nbRows*nbCols];

        for(int idxRow = 0 ; idxRow < nbRows ; ++idxRow){
            for(int idxCol = 0 ; idxCol < nbCols ; ++idxCol){
                block[idxCol*nbRows+idxRow] = viewer.getValue(idxRow,idxCol);
            }
        }
#endif


        // SVD specific (col major)
        rank = std::min(nbRows,nbCols);
        //S  = new FReal[rank];
        FReal* _U ;// = new FReal[nbRows*nbCols]; // Call to computeSVD() copies block into U
        FReal* _V;// = new FReal[rank*nbCols];

#if (defined FULLY_PIVOTED_ACA)

        // Perform fully pivoted ACA
        FAca::fACA<FReal>(block, nbRows, nbCols, accuracy, _U, _V, rank);

#elif (defined PARTIAL_PIVOTED_ACA)

        // TODO
        // Perform partially pivoted ACA
        //FAca::fACA<FReal>(viewer, nbRows, nbCols, accuracy, _U, _V, rank);

#endif
        // display rank
94
        std::cout << "rank after ACA=" << rank << " (" << nbRows << "," << nbCols << ")" << std::endl;
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 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

#if (defined RECOMPRESSED_ACA)

        // Recompression by QR+SVD
        FAca::recompress(_U,_V,nbRows,nbCols,accuracy,U,VT,rank);
        // display rank after recompression
        std::cout << "  rank after QR+SVD=" << rank << std::endl;

#else

        // Resize U and VT
        U  = new FReal[nbRows*rank]; 
        for(int idxRow = 0 ; idxRow < nbRows ; ++idxRow)
            for(int idxCol = 0 ; idxCol < rank ; ++idxCol)
                U[idxCol*nbRows+idxRow] = _U[idxCol*nbRows+idxRow];
  
        VT = new FReal[rank*nbCols];
        for(int idxRow = 0 ; idxRow < rank ; ++idxRow)
            for(int idxCol = 0 ; idxCol < nbCols ; ++idxCol)
                VT[idxCol*rank+idxRow] = _V[idxRow*nbCols+idxCol]; // transposition

#endif

        // Free memory
        delete [] _U;
        delete [] _V;

    };

    // dtor
    ~FACABlock(){
        // Free memory
        clear();
    };

    void clear(){
        nbRows = 0;
        nbCols = 0;
        level = 0;
        rank = 0;
        delete[] block;
        block = 0;
        delete[] U;
        U = 0;
        delete[] VT;
        VT = 0;
    }

    void gemv(FReal res[], const FReal vec[], const FReal scale = FReal(1.)) const {

        //// Apply (dense) block
        //FReal* res_dense = new FReal[nbRows];
        //FBlas::copy(nbRows,res,res_dense);
        //FBlas::gemva(nbRows, nbCols, scale, const_cast<FReal*>(block), const_cast<FReal*>(vec), res_dense);
        
        // Apply low-rank block
        FReal* VTvec = new FReal[rank];
        FBlas::setzero(rank,VTvec);

        // Apply VT
        FBlas::gemv(rank, nbCols, scale, const_cast<FReal*>(VT), const_cast<FReal*>(vec), VTvec);
        
        // Apply U
        FBlas::gemva(nbRows, rank, scale, const_cast<FReal*>(U), const_cast<FReal*>(VTvec), res);

    }

    void gemm(FReal res[], const FReal mat[], const int nbRhs, const FReal scale = FReal(1.)) const {
        
        //// Apply (dense) block
        //FBlas::gemma(nbRows, nbCols, nbRhs, scale, const_cast<FReal*>(block), nbRows, const_cast<FReal*>(mat), nbCols, res, nbRows);

        // Apply low-rank block
        FReal* VTmat = new FReal[nbCols*nbRhs];
        // Apply VT
        FBlas::gemm(rank, nbCols, nbRhs, scale, const_cast<FReal*>(VT), rank, const_cast<FReal*>(mat), nbCols, VTmat, rank);
        // Apply U
        FBlas::gemma(nbRows, rank, nbRhs, scale, const_cast<FReal*>(U), nbRows, const_cast<FReal*>(VTmat), rank, res, nbRows);

    }

176 177 178
    int getRank() const{
        return rank;
    }
179 180 181 182 183 184

};

#endif // FACABLOCK_HPP

// [--END--]