gridding_kernel.h 12.4 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
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
/*
 *  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"
Martin Reinecke's avatar
Martin Reinecke committed
28
#include "ducc0/math/least_misfit.h"
Martin Reinecke's avatar
Martin Reinecke committed
29
#include "ducc0/math/es_kernel.h"
Martin Reinecke's avatar
Martin Reinecke committed
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

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() {}
Martin Reinecke's avatar
Martin Reinecke committed
89
90
91

    virtual size_t support() const = 0;

Martin Reinecke's avatar
Martin Reinecke committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    /*! 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;
  };

Martin Reinecke's avatar
Martin Reinecke committed
113
class PiecewiseKernelCorrection
Martin Reinecke's avatar
Martin Reinecke committed
114
115
  {
  private:
Martin Reinecke's avatar
Martin Reinecke committed
116
117
    static constexpr size_t ideg=10; // integration points per interval
    static_assert((ideg&1)==0, "ideg must be even");
Martin Reinecke's avatar
Martin Reinecke committed
118
119
120
121
    vector<double> x, wgtpsi;
    size_t supp;

  public:
Martin Reinecke's avatar
Martin Reinecke committed
122
    PiecewiseKernelCorrection(size_t W, const function<double(double)> &func)
Martin Reinecke's avatar
Martin Reinecke committed
123
124
      : supp(W)
      {
Martin Reinecke's avatar
Martin Reinecke committed
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
      // we know that the kernel is piecewise smooth in all W sections but not
      // necessarily at borders between sections. Therefore we integrate piece
      // by piece.
      GL_Integrator integ(ideg, 1);
      auto xgl = integ.coords();
      auto wgl = integ.weights();
      x.resize((supp*ideg)/2);
      wgtpsi.resize((supp*ideg)/2);
      for (size_t i=0; i<x.size(); ++i)
        {
        size_t iiv = i/ideg;
        x[i] = -1. + iiv*2./supp + (1.+xgl[i%ideg])/supp;
        wgtpsi[i] = wgl[i%ideg]*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 1./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);
        });
      return res;
      }
  };

class GLFullCorrection
  {
  private:
    vector<double> x, wgtpsi;
    size_t supp;

  public:
    GLFullCorrection(size_t W, const function<double(double)> &func)
      : supp(W)
      {
      size_t p = size_t(1.5*W)+2;
      GL_Integrator integ(2*p,1);
Martin Reinecke's avatar
Martin Reinecke committed
176
177
178
      x = integ.coordsSymmetric();
      wgtpsi = integ.weightsSymmetric();
      for (size_t i=0; i<x.size(); ++i)
Martin Reinecke's avatar
Martin Reinecke committed
179
        wgtpsi[i] *= func(x[i])*supp*0.5;
Martin Reinecke's avatar
Martin Reinecke committed
180
181
182
183
184
185
186
187
188
      }

    /* 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]);
Martin Reinecke's avatar
Martin Reinecke committed
189
      return 1./tmp;
Martin Reinecke's avatar
Martin Reinecke committed
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
      }
    /* 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);
        });
      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;

Martin Reinecke's avatar
Martin Reinecke committed
216
    PiecewiseKernelCorrection corr;
Martin Reinecke's avatar
Martin Reinecke committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272

    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;
      }
Martin Reinecke's avatar
Martin Reinecke committed
273
274
275
    static vector<Tsimd> coeffFromPolyKernel(const PolyKernel &krn)
      {
      auto W = krn.W;
Martin Reinecke's avatar
Martin Reinecke committed
276
      auto D = krn.D;
Martin Reinecke's avatar
Martin Reinecke committed
277
      auto nvec = ((W+vlen-1)/vlen);
Martin Reinecke's avatar
Martin Reinecke committed
278
279
      vector<Tsimd> coeff(nvec*(D+1), 42.);
      vector<double> coeff_raw(W*(D+1), 42.);
Martin Reinecke's avatar
Martin Reinecke committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
      size_t Whalf = W/2;
      for (size_t j=0; j<=D; ++j)
        {
        double flip = (j&1) ? -1:1;
        for (size_t i=Whalf; i<W; ++i)
          {
          double val = krn.coeff[(i-Whalf)*(D+1)+j];
          coeff_raw[j*W+i] = val;
          coeff_raw[j*W+W-1-i] = flip*val;
          }
        }
      for (size_t j=0; j<=D; ++j)
        {
        for (size_t i=0; i<W; ++i)
          coeff[(D-j)*nvec + i/vlen][i%vlen] = T(coeff_raw[j*W+i]);
        for (size_t i=W; i<vlen*nvec; ++i)
          coeff[(D-j)*nvec + i/vlen][i%vlen] = T(0);
        }
      return coeff;
      }
Martin Reinecke's avatar
Martin Reinecke committed
300
301
302
303
304
305
306
307
308
309
310
311

  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);})
      {}

Martin Reinecke's avatar
Martin Reinecke committed
312
313
314
315
316
317
318
319
    HornerKernel(const PolyKernel &krn)
      : W(krn.W), D(krn.coeff.size()/((W+1)/2)-1), nvec((W+vlen-1)/vlen),
        coeff(coeffFromPolyKernel(krn)), evalfunc(evfhelper1<1>()),
        corr(W, [this](T v){return eval_single(v);})
      {}

    virtual size_t support() const { return W; }

Martin Reinecke's avatar
Martin Reinecke committed
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
    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); }
  };

Martin Reinecke's avatar
Martin Reinecke committed
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
template<typename T> class ES_Kernel: public GriddingKernel<T>
  {
  private:
    using Tsimd = native_simd<T>;
    static constexpr auto vlen = Tsimd::size();
    size_t W, nvec;
    double beta;

    GLFullCorrection corr;

  public:
    using GriddingKernel<T>::eval;
    using GriddingKernel<T>::eval_single;
    using GriddingKernel<T>::corfunc;

    ES_Kernel(size_t W_, double beta_)
      : W(W_), nvec((W+vlen-1)/vlen), beta(beta_),
        corr(W, [this](T v){return eval_single(v);}) {}

    virtual size_t support() const { return W; }

    virtual void eval(T x, native_simd<T> *res) const
      {
      T dist = T(2./W);
      for (size_t i=0; i<W; ++i)
        res[i/vlen][i%vlen] = x+i*dist;
      for (size_t i=W; i<nvec*vlen; ++i)
        res[i/vlen][i%vlen] = 0;
      for (size_t i=0; i<nvec; ++i)
        res[i] = esk(res[i], T(beta));
      }

    /*! Returns the function approximation at location x.
        x must lie in [-1; 1].  */
    virtual T eval_single(T x) const
      { return esk(x, T(beta)); }

    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); }
  };

template<typename T> auto selectESKernel(double ofactor, double epsilon)
  {
  auto supp = esk_support(epsilon, ofactor);
  auto beta = esk_beta(supp, ofactor)*supp;
//  return make_shared<HornerKernel<T>>(supp, supp+3, [beta](double v){return esk(v, beta);});
  return make_shared<ES_Kernel<T>>(supp, beta);
  }

Martin Reinecke's avatar
Martin Reinecke committed
398
399
400
401
}

using detail_gridding_kernel::GriddingKernel;
using detail_gridding_kernel::HornerKernel;
Martin Reinecke's avatar
Martin Reinecke committed
402
403
using detail_gridding_kernel::ES_Kernel;
using detail_gridding_kernel::selectESKernel;
Martin Reinecke's avatar
Martin Reinecke committed
404
405
406
407

}

#endif