aboutsummaryrefslogtreecommitdiff
path: root/eval_psi_radial.c
blob: ee913d95245b854bd4c4f1d4323e1c33fe0317ee (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
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright 2014-2016 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/>.
 */

#include <errno.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "internal.h"
#include "threadpool.h"

typedef struct EvalPsiArgs {
    const BDContext *bd;
    double *psi;
    ptrdiff_t psi_stride;
    const double *rho;
    const double *z;
    int nb_coords_rho;
    int nb_coords_z;

    int ret;
} EvalPsiArgs;

#define DIFF 0
#include "eval_psi_radial_template.c"
#undef DIFF

#define DIFF 1
#include "eval_psi_radial_template.c"
#undef DIFF

#define DIFF 2
#include "eval_psi_radial_template.c"
#undef DIFF

#define DIFF 3
#include "eval_psi_radial_template.c"
#undef DIFF

#define DIFF 4
#include "eval_psi_radial_template.c"
#undef DIFF

#define DIFF 6
#include "eval_psi_radial_template.c"
#undef DIFF

typedef void (*EvalPsiRadialFunc)(void *args,
                                  unsigned int job_idx,    unsigned int nb_jobs,
                                  unsigned int thread_idx, unsigned int nb_threads);

static const EvalPsiRadialFunc eval_psi_radial_tab[9] = {
    [0] = eval_psi_radial_0,
    [1] = eval_psi_radial_1,
    [2] = eval_psi_radial_2,
    [3] = eval_psi_radial_3,
    [4] = eval_psi_radial_4,
    [6] = eval_psi_radial_6,
};

double bdi_scalarproduct_metric_c(size_t len1, size_t len2, double *mat,
                                  double *vec1, double *vec2)
{
    double val = 0.0;
    for (int m = 0; m < len2; m++) {
        double tmp = 0.0;
        for (int n = 0; n < len1; n++)
            tmp += mat[m * len1 + n] * vec1[n];
        val += tmp * vec2[m];
    }
    return val;
}

int bdi_eval_psi_radial(const BDContext *bd,
                        const double *rho, int nb_coords_rho,
                        const double *z,   int nb_coords_z,
                        const unsigned int diff_order[2],
                        double *psi, ptrdiff_t psi_stride)
{
    ThreadPoolContext *tctx;
    BDPriv *s = bd->priv;
    int diff_idx = diff_order[1] * 3 + diff_order[0];
    int block_size = (nb_coords_z + bd->nb_threads - 1) / bd->nb_threads;

    EvalPsiArgs *args;
    int ret;

    args = calloc(bd->nb_threads, sizeof(*args));
    if (!args)
        return -ENOMEM;

    ret = bdi_threadpool_init(&tctx, bd->nb_threads);
    if (ret < 0) {
        free(args);
        return ret;
    }

    for (int i = 0; i < bd->nb_threads; i++) {
        EvalPsiArgs *a = &args[i];

        a->bd  = bd;
        a->rho = rho;
        a->nb_coords_rho = nb_coords_rho;

        a->psi_stride  = psi_stride;
        a->psi         = psi + i * block_size * psi_stride;
        a->z           = z   + i * block_size;
        a->nb_coords_z = MIN(block_size, nb_coords_z - i * block_size);
    }

    bdi_threadpool_execute(tctx, bd->nb_threads,
                           eval_psi_radial_tab[diff_idx], args);

    bdi_threadpool_free(&tctx);

    for (int i = 0; i < bd->nb_threads; i++)
        if (args[i].ret < 0) {
            ret = args[i].ret;
            break;
        }

    free(args);

    return ret;
}