Commit 2f2bbc3e authored by Martin Reinecke's avatar Martin Reinecke

start rewriting

parent 3dd271fc
This diff is collapsed.
/*
* This file is part of the MR utility library.
*
* This code is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This code 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 License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this code; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* Copyright (C) 2020 Max-Planck-Society
Author: Martin Reinecke */
#ifndef DUCC0_GRIDDING_KERNEL_H
#define DUCC0_GRIDDING_KERNEL_H
#include <vector>
#include "ducc0/infra/simd.h"
#include "ducc0/math/gl_integrator.h"
namespace ducc0 {
namespace detail_gridding_kernel {
using namespace std;
constexpr double pi=3.141592653589793238462643383279502884197;
vector<double> getCoeffs(size_t W, size_t D, const function<double(double)> &func)
{
vector<double> coeff(W*(D+1));
vector<double> chebroot(D+1);
for (size_t i=0; i<=D; ++i)
chebroot[i] = cos((2*i+1.)*pi/(2*D+2));
vector<double> y(D+1), lcf(D+1), C((D+1)*(D+1)), lcf2(D+1);
for (size_t i=0; i<W; ++i)
{
double l = -1+2.*i/double(W);
double r = -1+2.*(i+1)/double(W);
// function values at Chebyshev nodes
for (size_t j=0; j<=D; ++j)
y[j] = func(chebroot[j]*(r-l)*0.5 + (r+l)*0.5);
// Chebyshev coefficients
for (size_t j=0; j<=D; ++j)
{
lcf[j] = 0;
for (size_t k=0; k<=D; ++k)
lcf[j] += 2./(D+1)*y[k]*cos(j*(2*k+1)*pi/(2*D+2));
}
lcf[0] *= 0.5;
// Polynomial coefficients
fill(C.begin(), C.end(), 0.);
C[0] = 1.;
C[1*(D+1) + 1] = 1.;
for (size_t j=2; j<=D; ++j)
{
C[j*(D+1) + 0] = -C[(j-2)*(D+1) + 0];
for (size_t k=1; k<=j; ++k)
C[j*(D+1) + k] = 2*C[(j-1)*(D+1) + k-1] - C[(j-2)*(D+1) + k];
}
for (size_t j=0; j<=D; ++j) lcf2[j] = 0;
for (size_t j=0; j<=D; ++j)
for (size_t k=0; k<=D; ++k)
lcf2[k] += C[j*(D+1) + k]*lcf[j];
for (size_t j=0; j<=D; ++j)
coeff[j*W + i] = lcf2[D-j];
}
return coeff;
}
/*! A GriddingKernel is considered to be a symmetric real-valued function
defined on the interval [-1; 1].
This range is subdivided into W equal-sized parts. */
template<typename T> class GriddingKernel
{
public:
virtual ~GriddingKernel() {}
// virtual size_t W() const = 0;
/*! Returns the function approximation at W different locations with the
abscissas x, x+2./W, x+4./W, ..., x+(2.*W-2)/W.
x must lie in [-1; -1+2./W].
NOTE: res must point to memory large enough to hold
((W+vlen-1)/vlen) objects of type native_simd<T>!
*/
virtual void eval(T x, native_simd<T> *res) const = 0;
/*! Returns the function approximation at location x.
x must lie in [-1; 1]. */
virtual T eval_single(T x) const = 0;
/* Computes the correction function at a given coordinate.
Useful coordinates lie in the range [0; 0.5]. */
virtual double corfunc(double x) const = 0;
/* Computes the correction function values at a coordinates
[0, dx, 2*dx, ..., (n-1)*dx] */
virtual vector<double> corfunc(size_t n, double dx, int nthreads=1) const = 0;
};
class KernelCorrection
{
private:
vector<double> x, wgtpsi;
size_t supp;
public:
KernelCorrection(size_t W, const function<double(double)> &func)
: supp(W)
{
size_t p = size_t(1.5*supp+2); // estimate; may need to be higher for arbitrary kernels
GL_Integrator integ(2*p, 1);
x = integ.coordsSymmetric();
wgtpsi = integ.weightsSymmetric();
for (size_t i=0; i<x.size(); ++i)
wgtpsi[i] *= func(x[i]);
}
/* Compute correction factors for gridding kernel
This implementation follows eqs. (3.8) to (3.10) of Barnett et al. 2018 */
double corfunc(double v) const
{
double tmp=0;
for (size_t i=0; i<x.size(); ++i)
tmp += wgtpsi[i]*cos(pi*supp*v*x[i]);
return 2./(supp*tmp);
}
/* Compute correction factors for gridding kernel
This implementation follows eqs. (3.8) to (3.10) of Barnett et al. 2018 */
vector<double> corfunc(size_t n, double dx, int nthreads=1) const
{
vector<double> res(n);
execStatic(n, nthreads, 0, [&](auto &sched)
{
while (auto rng=sched.getNext()) for(auto i=rng.lo; i<rng.hi; ++i)
res[i] = corfunc(i*dx);
});
cout <<"bleep" << n << " " << res.size() << endl;
return res;
}
};
template<typename T> class HornerKernel: public GriddingKernel<T>
{
private:
static constexpr size_t MAXW=16, MINDEG=0, MAXDEG=20;
using Tsimd = native_simd<T>;
static constexpr auto vlen = Tsimd::size();
size_t W, D, nvec;
vector<Tsimd> coeff;
void (HornerKernel<T>::* evalfunc) (T, native_simd<T> *) const;
KernelCorrection corr;
template<size_t NV, size_t DEG> void eval_intern(T x, native_simd<T> *res) const
{
x = (x+1)*W-1;
for (size_t i=0; i<NV; ++i)
{
auto tval = coeff[i];
for (size_t j=1; j<=DEG; ++j)
tval = tval*x + coeff[j*NV+i];
res[i] = tval;
}
}
void eval_intern_general(T x, native_simd<T> *res) const
{
x = (x+1)*W-1;
for (size_t i=0; i<nvec; ++i)
{
auto tval = coeff[i];
for (size_t j=1; j<=D; ++j)
tval = tval*x+coeff[j*nvec+i];
res[i] = tval;
}
}
template<size_t NV, size_t DEG> auto evfhelper2() const
{
if (DEG==D)
return &HornerKernel::eval_intern<NV,DEG>;
if (DEG>MAXDEG)
return &HornerKernel::eval_intern_general;
return evfhelper2<NV, ((DEG>MAXDEG) ? DEG : DEG+1)>();
}
template<size_t NV> auto evfhelper1() const
{
if (nvec==NV) return evfhelper2<NV,0>();
if (nvec*vlen>MAXW) return &HornerKernel::eval_intern_general;
return evfhelper1<((NV*vlen>MAXW) ? NV : NV+1)>();
}
static vector<Tsimd> makeCoeff(size_t W, size_t D,
const function<double(double)> &func)
{
auto nvec = ((W+vlen-1)/vlen);
vector<Tsimd> coeff(nvec*(D+1), 0);
auto coeff_raw = getCoeffs(W,D,func);
for (size_t j=0; j<=D; ++j)
{
for (size_t i=0; i<W; ++i)
coeff[j*nvec + i/vlen][i%vlen] = T(coeff_raw[j*W+i]);
for (size_t i=W; i<vlen*nvec; ++i)
coeff[j*nvec + i/vlen][i%vlen] = T(0);
}
return coeff;
}
public:
using GriddingKernel<T>::eval;
using GriddingKernel<T>::eval_single;
using GriddingKernel<T>::corfunc;
HornerKernel(size_t W_, size_t D_, const function<double(double)> &func)
: W(W_), D(D_), nvec((W+vlen-1)/vlen),
coeff(makeCoeff(W_, D_, func)), evalfunc(evfhelper1<1>()),
corr(W_, [this](T v){return eval_single(v);})
{}
virtual void eval(T x, native_simd<T> *res) const
{ (this->*evalfunc)(x, res); }
/*! Returns the function approximation at location x.
x must lie in [-1; 1]. */
virtual T eval_single(T x) const
{
auto nth = min(W-1, size_t(max(T(0), (x+1)*W*T(0.5))));
x = (x+1)*W-2*nth-1;
auto i = nth/vlen;
auto imod = nth%vlen;
auto tval = coeff[i][imod];
for (size_t j=1; j<=D; ++j)
tval = tval*x + coeff[j*nvec+i][imod];
return tval;
}
virtual double corfunc(double x) const {return corr.corfunc(x); }
/* Computes the correction function values at a coordinates
[0, dx, 2*dx, ..., (n-1)*dx] */
virtual vector<double> corfunc(size_t n, double dx, int nthreads=1) const
{ return corr.corfunc(n, dx, nthreads); }
};
}
using detail_gridding_kernel::GriddingKernel;
using detail_gridding_kernel::HornerKernel;
}
#endif
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