Examples

The following example demonstrates how to construct the linear spline and perform the interpolation.

 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
#include <cstdint>
#include <iostream>
#include <vector>

#include <CL/sycl.hpp>

#include <oneapi/mkl/experimental/data_fitting.hpp>

constexpr std::int64_t nx = 10'000;
constexpr std::int64_t nsites = 150'000;

int main (int argc, char ** argv) {

    sycl::queue q;
    sycl::usm_allocator<double, sycl::usm::alloc::shared> alloc(q);

    // Allocate memory for spline parameters
    std::vector<double, decltype(alloc)> partitions(nx, alloc);
    std::vector<double, decltype(alloc)> functions(nx, alloc);
    std::vector<double, decltype(alloc)> coeffs(2 * (nx - 1), alloc);
    std::vector<double, decltype(alloc)> sites(nsites, alloc);
    std::vector<double, decltype(alloc)> results(nsites, alloc);

    // Fill parameters with valid data
    for (std::int64_t i = 0; i < nx; ++i) {
        partitions[i] = 0.1 * i;
        functions[i] = i * i;
    }

    for (std::int64_t i = 0; i < nsites; ++i) {
        sites[i] = (0.1 * nx * i) / nsites);
    }

    namespace df = oneapi::mkl::experimental::data_fitting;
    // Set parameters to spline
    df::spline<double, df::linear_spline::default_type> spl(q);
    spl.set_partitions(partitions.data(), nx)
       .set_coefficients(coeffs.data())
       .set_function_values(functions.data());

    // Construct spline
    auto event = spl.construct();
    event = df::interpolate(spl, sites.data(), nsites, results.data(), { event });
    event.wait();

    std::cout << "done" << std::endl;
    return 0;
}