aboutsummaryrefslogtreecommitdiff
path: root/transfer.h
blob: 2ed4aea395cd7b3037c0c7abd5dc0bf3bb168cc3 (plain)
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
94
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
/*
 * Grid transfer operators
 * Copyright 2018 Anton Khirnov <anton@khirnov.net>
 *
 * This program 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 3 of the License, or
 * (at your option) any later version.
 *
 * 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 License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef MG2D_TRANSFER_H
#define MG2D_TRANSFER_H

#include <stddef.h>
#include <threadpool.h>

#include "ndarray.h"

enum GridTransferOperator {
    GRID_TRANSFER_LAGRANGE_1,
    GRID_TRANSFER_LAGRANGE_3,
    GRID_TRANSFER_LAGRANGE_5,
    GRID_TRANSFER_LAGRANGE_7,
    GRID_TRANSFER_FW_1,
    GRID_TRANSFER_FW_2,
    GRID_TRANSFER_FW_3,
    GRID_TRANSFER_FW_4,
};

/**
 * A 2-dimensional grid with regular spacing.
 */
typedef struct RegularGrid2D {
    /**
     * Indices of the grid origin, relative to coordinate origin.
     * (o0, o1) = (start[0] * step[0], start[1] * step[1])
     */
    ptrdiff_t           start[2];

    /**
     * Number of points in the domain.
     */
    size_t              size[2];

    /**
     * Size of the step between neighbouring grid points.
     */
    double              step[2];
} RegularGrid2D;

typedef struct GridTransferContext {
    /**
     * Private data, not to be accessed by the caller.
     */
    void *priv;

    /**
     * Thread pool for parallel execution, must be set by the caller.
     */
    TPContext *tp;
    /**
     * CPU feature flags, must be set by the caller.
     */
    int cpuflags;

    /**
     * The operator used, set by mg2di_gt_alloc().
     */
    enum GridTransferOperator op;

    /**
     * Source grid geometry, must be filled by the caller.
     */
    RegularGrid2D src;

    /**
     * Destination grid geometry, must be filled by the caller.
     */
    RegularGrid2D dst;

    /**
     * Allow extrapolating this many points beyond the source rectangle.
     * Defaults to 0: forbid extrapolation.
     */
    int extrapolate_distance;
} GridTransferContext;

/**
 * Allocate the transfer context for the given transfer operator.
 *
 * @return newly allocated transfer context on success, NULL on failure
 */
GridTransferContext *mg2di_gt_alloc(enum GridTransferOperator op);

/**
 * Initialize the tranfer context after all the parameters have been set.
 *
 * @return 0 on success, a negative error code on failure
 */
int mg2di_gt_init(GridTransferContext *ctx);

/**
 * Free the transfer context and everything associated with it.
 * Write NULL in the supplied pointer.
 */
void mg2di_gt_free(GridTransferContext **ctx);

/**
 * Execute the transfer.
 *
 * @param ctx transfer context
 * @param dst destination data, must match the dst grid in the transfer context
 * @param src source data, must match the src grid in the transfer context
 *
 * @return 0 on success, a negative error code on failure.
 */
int mg2di_gt_transfer(GridTransferContext *ctx,
                      NDArray *dst, const NDArray *src);

#endif // MG2D_TRANSFER_H