aboutsummaryrefslogtreecommitdiff
path: root/init.c
blob: ff995b6e6c3d792151142ac9486774d29e10c9b5 (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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
216
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*
 * Copyright 2017 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 "config.h"

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

#include <cblas.h>

#if HAVE_OPENCL
#include <cl.h>
#include <clBLAS.h>
#endif

#include "basis.h"
#include "common.h"
#include "cpu.h"
#include "log.h"
#include "nlsolve.h"
#include "td_constraints.h"
#include "teukolsky_data.h"
#include "threadpool.h"

#define NB_EQUATIONS 3

typedef struct TDPriv {
    unsigned int basis_order[NB_EQUATIONS][2];
    BasisSetContext *basis[NB_EQUATIONS][2];

    NLSolveContext *solver;

    ThreadPoolContext *tp;
    TDLogger logger;

    double *coeffs;
    double *coeffs_tmp;
    ptrdiff_t coeffs_stride;

    int cpu_flags;

    double (*scalarproduct_metric)(size_t len1, size_t len2, double *mat,
                                   double *vec1, double *vec2);
} TDPriv;

double tdi_scalarproduct_metric_fma3(size_t len1, size_t len2, double *mat,
                                     double *vec1, double *vec2);
double tdi_scalarproduct_metric_avx(size_t len1, size_t len2, double *mat,
                                    double *vec1, double *vec2);
double tdi_scalarproduct_metric_sse3(size_t len1, size_t len2, double *mat,
                                     double *vec1, double *vec2);
double tdi_scalarproduct_metric_c(size_t len1, size_t len2, double *mat,
                                  double *vec1, double *vec2);

static void init_opencl(TDPriv *s)
#if HAVE_OPENCL
{
    int err, count;
    cl_platform_id platform;
    cl_context_properties props[3];
    cl_device_id ocl_device;

    err = clGetPlatformIDs(1, &platform, &count);
    if (err != CL_SUCCESS || count < 1) {
        tdi_log(&s->logger, 0, "Could not get an OpenCL platform ID\n");
        return;
    }

    err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &ocl_device, &count);
    if (err != CL_SUCCESS || count < 1) {
        tdi_log(&s->logger, 0, "Could not get an OpenCL device ID\n");
        return;
    }

    props[0] = CL_CONTEXT_PLATFORM;
    props[1] = (cl_context_properties)platform;
    props[2] = 0;

    s->ocl_ctx = clCreateContext(props, 1, &ocl_device, NULL, NULL, &err);
    if (err != CL_SUCCESS || !s->ocl_ctx) {
        tdi_log(&s->logger, 0, "Could not create an OpenCL context\n");
        return;
    }

    s->ocl_queue = clCreateCommandQueue(s->ocl_ctx, ocl_device, 0, &err);
    if (err != CL_SUCCESS || !s->ocl_queue) {
        tdi_log(&s->logger, 0, "Could not create an OpenCL command queue: %d\n", err);
        goto fail;
    }

    err = clblasSetup();
    if (err != CL_SUCCESS) {
        tdi_log(&s->logger, 0, "Error setting up clBLAS\n");
        goto fail;
    }

    return;
fail:
    if (s->ocl_queue)
        clReleaseCommandQueue(s->ocl_queue);
    s->ocl_queue = 0;

    if (s->ocl_ctx)
        clReleaseContext(s->ocl_ctx);
    s->ocl_ctx = 0;
}
#else
{
}
#endif

static const enum BasisFamily basis_sets[NB_EQUATIONS][2] = {
    { BASIS_FAMILY_SB_EVEN,  BASIS_FAMILY_COS_EVEN },
    { BASIS_FAMILY_SB_EVEN,  BASIS_FAMILY_COS_EVEN },
    { BASIS_FAMILY_SB_EVEN,  BASIS_FAMILY_COS_EVEN },
};

static void log_callback(TDLogger *log, int level, const char *fmt, va_list vl)
{
    TDContext *td = log->opaque;
    td->log_callback(td, level, fmt, vl);
}

static int teukolsky_init_check_options(TDContext *td)
{
    TDPriv *s = td->priv;
    int ret;

    s->cpu_flags = tdi_init_cpu_flags();

    if (!td->nb_threads) {
        td->nb_threads = tdi_cpu_count();
        if (!td->nb_threads)
            td->nb_threads = 1;
    }

    ret = tdi_threadpool_init(&s->tp, td->nb_threads);
    if (ret < 0)
        return ret;

    init_opencl(s);

    s->logger.log    = log_callback;
    s->logger.opaque = td;

    //s->scalarproduct_metric = tdi_scalarproduct_metric_c;
    //if (EXTERNAL_SSE3(s->cpu_flags))
    //    s->scalarproduct_metric = tdi_scalarproduct_metric_sse3;
    //if (EXTERNAL_AVX(s->cpu_flags))
    //    s->scalarproduct_metric = tdi_scalarproduct_metric_avx;
    //if (EXTERNAL_FMA3(s->cpu_flags))
    //    s->scalarproduct_metric = tdi_scalarproduct_metric_fma3;
//    for (int i = 0; i < ctx->nb_equations; i++)
//        for (int j = 0; j < 2; j++) {
//            s->ps_ctx->basis[i][j]       = ctx->basis[i][j];
//            s->ps_ctx->solve_order[i][j] = basis_order[i][j];
//            max_order = MAX(max_order, basis_order[i][j]);
//        }
//
    //for (int i = 0; i < max_order; i++) {
    //    tdi_log(&s->logger, 2, "%d ", i);
    //    for (int j = 0; j < ctx->nb_equations; j++)
    //        for (int k = 0; k < 2; k++) {
    //            if (i < s->ps_ctx->solve_order[j][k])
    //                tdi_log(&s->logger, 2, "%8.8g\t", s->ps_ctx->colloc_grid[j][k][i]);
    //            else
    //                tdi_log(&s->logger, 2, "        ");
    //        }
    //    tdi_log(&s->logger, 2, "\n");
    //}

    s->basis_order[0][0] = td->nb_coeffs[0];
    s->basis_order[0][1] = td->nb_coeffs[1];
    s->basis_order[1][0] = td->nb_coeffs[0];
    s->basis_order[1][1] = td->nb_coeffs[1];
    s->basis_order[2][0] = td->nb_coeffs[0];
    s->basis_order[2][1] = td->nb_coeffs[1];

    ret = posix_memalign((void**)&s->coeffs, 32,
                         sizeof(*s->coeffs) * NB_EQUATIONS * td->nb_coeffs[0] * td->nb_coeffs[1]);
    if (ret)
        return -ENOMEM;
    memset(s->coeffs, 0, sizeof(*s->coeffs) * NB_EQUATIONS * td->nb_coeffs[0] * td->nb_coeffs[1]);

    if (td->solution_branch > 0) {
        ret = posix_memalign((void**)&s->coeffs_tmp, 32,
                             sizeof(*s->coeffs_tmp) * NB_EQUATIONS * td->nb_coeffs[0] * td->nb_coeffs[1]);
        if (ret)
            return -ENOMEM;
        memset(s->coeffs_tmp, 0, sizeof(*s->coeffs_tmp) * NB_EQUATIONS * td->nb_coeffs[0] * td->nb_coeffs[1]);
    }

    for (int i = 0; i < NB_EQUATIONS; i++)
        td->coeffs[i] = s->coeffs + i * td->nb_coeffs[0] * td->nb_coeffs[1];

    for (int i = 0; i < ARRAY_ELEMS(basis_sets); i++)
        for (int j = 0; j < ARRAY_ELEMS(basis_sets[i]); j++) {
            double sf;

            ret = tdi_basis_init(&s->basis[i][j], basis_sets[i][j], td->basis_scale_factor[j]);
            if (ret < 0)
                return ret;
        }


    ret = tdi_nlsolve_context_alloc(&s->solver, ARRAY_ELEMS(basis_sets));
    if (ret < 0) {
        tdi_log(&s->logger, 0, "Error allocating the non-linear solver\n");
        return ret;
    }

    s->solver->logger  = s->logger;
    s->solver->tp      = s->tp;
    s->solver->maxiter = td->max_iter;
    s->solver->atol    = td->atol;

    memcpy(s->solver->basis,       s->basis,       sizeof(s->basis));
    memcpy(s->solver->solve_order, s->basis_order, sizeof(s->basis_order));

#if HAVE_OPENCL
    s->solver->ocl_ctx   = s->ocl_ctx;
    s->solver->ocl_queue = s->ocl_queue;
#endif

    ret = tdi_nlsolve_context_init(s->solver);
    if (ret < 0) {
        tdi_log(&s->logger, 0, "Error initializing the non-linear solver\n");
        return ret;
    }

    return 0;
}

static int constraint_eval_alloc(const TDContext *td, double amplitude,
                                 TDConstraintEvalContext **pce)
{
    TDPriv *priv = td->priv;
    TDConstraintEvalContext *ce;
    int ret;

    ret = tdi_constraint_eval_alloc(&ce, td->family);
    if (ret < 0) {
        tdi_log(&priv->logger, 0, "Error allocating the constraints evaluator\n");
        return ret;
    }

    ce->logger       = priv->logger;
    ce->amplitude    = amplitude;
    ce->nb_coords[0] = td->nb_coeffs[0];
    ce->nb_coords[1] = td->nb_coeffs[1];
    ce->coords[0]    = priv->solver->colloc_grid[0][0];
    ce->coords[1]    = priv->solver->colloc_grid[0][1];

    ret = tdi_constraint_eval_init(ce);
    if (ret < 0) {
        tdi_constraint_eval_free(&ce);
        tdi_log(&priv->logger, 0, "Error initializing the constraints evaluator\n");
        return ret;
    }

    *pce = ce;
    return 0;
}

static int teukolsky_constraint_eval(void *opaque, unsigned int eq_idx,
                                     const unsigned int *colloc_grid_order,
                                     const double * const *colloc_grid,
                                     const double * const (*vars)[PSSOLVE_DIFF_ORDER_NB],
                                     double *dst)
{
    TDConstraintEvalContext *ce = opaque;
    const double *amplitude = opaque;

    return tdi_constraint_eval(ce, eq_idx, vars, dst);
}

int td_solve(TDContext *td, double *coeffs_init[3])
{
    TDPriv *s = td->priv;
    TDConstraintEvalContext *ce;
    double a0;
    int ret;

    ret = teukolsky_init_check_options(td);
    if (ret < 0)
        return ret;

    ret = constraint_eval_alloc(td, 0.0, &ce);
    if (ret < 0)
        return ret;
    if (fabs(td->amplitude) >= ce->a_diverge) {
        tdi_log(&s->logger, 0,
                "Amplitude A=%16.16g is above the point A_{max}=%g, no solutions "
                "are known to exist there. Set solution_branch=1 to get to the "
                "second branch where mass increases with decreasing amplitude\n",
                td->amplitude, ce->a_converge);
    }
    a0 = ce->a_converge;
    tdi_constraint_eval_free(&ce);

    if (coeffs_init) {
        for (int i = 0; i < 3; i++) {
            memcpy(td->coeffs[i], coeffs_init[i], sizeof(*td->coeffs[i]) *
                   td->nb_coeffs[0] * td->nb_coeffs[1]);
        }
    }

    if (td->solution_branch == 0 || coeffs_init) {
        // direct solve with default (flat space) or user-provided initial guess
        ret = constraint_eval_alloc(td, td->amplitude, &ce);
        if (ret < 0)
            return ret;

        ret = tdi_nlsolve_solve(s->solver, teukolsky_constraint_eval, NULL,
                                ce, s->coeffs, 0);
        tdi_constraint_eval_free(&ce);
        if (ret < 0) {
            tdi_log(&s->logger, 0, "tdi_nlsolve_solve() failed: %d", ret);
            return ret;
        }
    } else {
        // second branch requested and no user-provided initial guess
        // execute two lower-branch solutions and extrapolate to get to the upper branch
        const int N = NB_EQUATIONS * td->nb_coeffs[0] * td->nb_coeffs[1];
        const double delta = 1e-5;
        const double a1 = a0 - delta;

        double cur_amplitude, new_amplitude, inverse_step;
        int dir;

        ret = constraint_eval_alloc(td, a0, &ce);
        if (ret < 0)
            return ret;

        ret = tdi_nlsolve_solve(s->solver, teukolsky_constraint_eval, NULL,
                                ce, s->coeffs, 0);
        tdi_constraint_eval_free(&ce);
        if (ret < 0)
            return ret;

        ret = constraint_eval_alloc(td, a1, &ce);
        if (ret < 0)
            return ret;
        ret = tdi_nlsolve_solve(s->solver, teukolsky_constraint_eval, NULL,
                                ce, s->coeffs_tmp, 0);
        tdi_constraint_eval_free(&ce);
        if (ret < 0)
            return ret;

        cblas_daxpy(N, -1.0, s->coeffs,     1, s->coeffs_tmp, 1);
        cblas_daxpy(N, -1.0, s->coeffs_tmp, 1, s->coeffs,     1);

        // obtain solution for a1 in the upper branch
        ret = constraint_eval_alloc(td, a1, &ce);
        if (ret < 0)
            return ret;

        ret = tdi_nlsolve_solve(s->solver, teukolsky_constraint_eval, NULL,
                                ce, s->coeffs, 0);
        tdi_constraint_eval_free(&ce);
        if (ret < 0) {
            tdi_log(&s->logger, 0, "Failed to get into the upper branch\n");
            return ret;
        }

        cur_amplitude = a1;
        dir = SGN(td->amplitude - cur_amplitude);
        inverse_step = 1.0 / td->amplitude - 1.0 / cur_amplitude;
        while (fabs(cur_amplitude - td->amplitude) > DBL_EPSILON) {
            //double scale_factor;
            //scale_factor  = cur_amplitude;
            //cur_amplitude = MIN(td->amplitude, 2 * cur_amplitude);
            //scale_factor  = cur_amplitude / scale_factor;

            new_amplitude = 1.0 / ((1.0 / cur_amplitude) + inverse_step);
            if (dir * (td->amplitude - new_amplitude) < 0)
                new_amplitude = td->amplitude;
            tdi_log(&s->logger, 2, "Trying amplitude %g\n", new_amplitude);

            ret = constraint_eval_alloc(td, new_amplitude, &ce);
            if (ret < 0)
                return ret;

            memcpy(s->coeffs_tmp, s->coeffs, sizeof(*s->coeffs) * N);
            ret = tdi_nlsolve_solve(s->solver, teukolsky_constraint_eval, NULL,
                                    ce, s->coeffs_tmp, 1);
            tdi_constraint_eval_free(&ce);
            if (ret == -EDOM) {
                inverse_step = 0.5 * inverse_step;
                //if (fabs(inverse_step) < 1e-2)
                //    return ret;
                continue;
            } else if (ret < 0)
                return ret;
            cur_amplitude = new_amplitude;
            memcpy(s->coeffs, s->coeffs_tmp, sizeof(*s->coeffs) * N);
        }
        //while (1) {


        //    if (fabs(cur_amplitude - td->amplitude) < DBL_EPSILON)
        //        goto finish;

        //    cblas_dscal(td->nb_coeffs[0] * td->nb_coeffs[1], SQR(scale_factor), td->coeffs[0], 1);
        //    cblas_dscal(td->nb_coeffs[0] * td->nb_coeffs[1], scale_factor, td->coeffs[1], 1);
        //    cblas_dscal(td->nb_coeffs[0] * td->nb_coeffs[1], scale_factor, td->coeffs[2], 1);

        //}

    }
finish:
    tdi_nlsolve_print_stats(s->solver);

    return 0;
}

static void log_default_callback(const TDContext *td, int level, const char *fmt, va_list vl)
{
    vfprintf(stderr, fmt, vl);
}

TDContext *td_context_alloc(void)
{
    TDContext *td = calloc(1, sizeof(*td));

    if (!td)
        return NULL;

    td->nb_threads = 1;

    td->family          = TD_FAMILY_SIMPLE_TIME_ANTISYM;
    td->solution_branch = 0;
    td->amplitude       = 1.0;

    td->nb_coeffs[0] = 32;
    td->nb_coeffs[1] = 16;

    td->basis_scale_factor[0] = 3.0;
    td->basis_scale_factor[1] = 3.0;

    td->max_iter = 16;
    td->atol     = 1e-12;

    td->log_callback = log_default_callback;

    td->priv = calloc(1, sizeof(TDPriv));
    if (!td->priv) {
        free(td);
        return NULL;
    }

    return td;
}

void td_context_free(TDContext **ptd)
{
    TDContext *td = *ptd;
    TDPriv *s;

    if (!td)
        return;

    s = td->priv;

    tdi_nlsolve_context_free(&s->solver);
    tdi_threadpool_free(&s->tp);

#if HAVE_OPENCL
    if (s->ocl_queue)
        clReleaseCommandQueue(s->ocl_queue);
    if (s->ocl_ctx)
        clReleaseContext(s->ocl_ctx);
#endif

    for (int i = 0; i < ARRAY_ELEMS(s->basis); i++)
        for (int j = 0; j < ARRAY_ELEMS(s->basis[i]); j++)
            tdi_basis_free(&s->basis[i][j]);

    free(s->coeffs);
    free(s->coeffs_tmp);

    free(td->priv);
    free(td);
    *ptd = NULL;
}

static double 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;
}

static int eval_var(const TDContext *td, unsigned int var_idx,
                    size_t nb_coords, const double *r, const double *theta,
                    const unsigned int diff_order[2],
                    double *out)
{
    TDPriv *s = td->priv;
    double *basis_val[2] = { NULL };
    int ret = 0;

    if (diff_order[0] > 0 || diff_order[1] > 0) {
        tdi_log(&s->logger, 0, "Derivatives of higher order than 2 are not supported.\n");
        return -ENOSYS;
    }

    posix_memalign((void**)&basis_val[0], 32, sizeof(*basis_val[0]) * td->nb_coeffs[0]);
    posix_memalign((void**)&basis_val[1], 32, sizeof(*basis_val[1]) * td->nb_coeffs[1]);
    if (!basis_val[0] || !basis_val[1]) {
        ret = -ENOMEM;
        goto fail;
    }
    memset(basis_val[0], 0, sizeof(*basis_val[0]) * td->nb_coeffs[0]);
    memset(basis_val[1], 0, sizeof(*basis_val[1]) * td->nb_coeffs[1]);

    for (int i = 0; i < nb_coords; i++) {
        double theta_val = theta[i];
        double r_val     = r[i];

        double val = (var_idx == 0) ? 1.0 : 0.0;

        for (int k = 0; k < td->nb_coeffs[0]; k++)
            basis_val[0][k] = tdi_basis_eval(s->basis[var_idx][0], BS_EVAL_TYPE_VALUE, r_val, k);
        for (int k = 0; k < td->nb_coeffs[1]; k++)
            basis_val[1][k] = tdi_basis_eval(s->basis[var_idx][1], BS_EVAL_TYPE_VALUE, theta_val, k);

        val += scalarproduct_metric_c(td->nb_coeffs[0], td->nb_coeffs[1], td->coeffs[var_idx],
                                      basis_val[0], basis_val[1]);

        out[i] = val;
    }


fail:
    free(basis_val[0]);
    free(basis_val[1]);

    return ret;
}

int td_eval_psi(const TDContext *td,
                size_t nb_coords, const double *r, const double *theta,
                const unsigned int diff_order[2],
                double *out)
{
    return eval_var(td, 0, nb_coords, r, theta, diff_order, out);
}

int td_eval_krr(const TDContext *td,
                size_t nb_coords, const double *r, const double *theta,
                const unsigned int diff_order[2],
                double *out)
{
    return eval_var(td, 1, nb_coords, r, theta, diff_order, out);
}
int td_eval_kpp(const TDContext *td,
                size_t nb_coords, const double *r, const double *theta,
                const unsigned int diff_order[2],
                double *out)
{
    return eval_var(td, 2, nb_coords, r, theta, diff_order, out);
}

int td_eval_krt(const TDContext *td,
                size_t nb_coords, const double *r, const double *theta,
                const unsigned int diff_order[2],
                double *out)
{
    TDConstraintEvalContext *ce;
    int ret;

    if (diff_order[0] || diff_order[1])
        return -ENOSYS;

    ret = constraint_eval_alloc(td, td->amplitude, &ce);
    if (ret < 0)
        return ret;

    for (int i = 0; i < nb_coords; i++) {
        double theta_val = theta[i];
        double r_val     = r[i];

        out[i] = tdi_constraint_eval_k_rtheta(ce, r_val, theta_val);
    }

    tdi_constraint_eval_free(&ce);

    return 0;
}