libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
xtc_dio_sched.h
1/*-
2 * Copyright (c) 2026, The XTC Project
3 * Use of this source code is governed by the ISC License.
4 *
5 * src/inc/xtc_dio_sched.h
6 * A small genetic-algorithm tuner for runtime self-tuning of integer
7 * "genes" (tunables) against an observed fitness, after Jake
8 * Moilanen's genetic scheduler (OLS 2005): a population of candidate
9 * gene-sets is evaluated, the best are bred (elitism + per-gene
10 * crossover + mutation), and the mutation rate ADAPTS -- it falls
11 * while fitness improves (hone in) and rises when fitness drops (a
12 * workload shift), so the tuner re-converges quickly. Capped at 45%
13 * to keep it from thrashing.
14 *
15 * It is a pure, deterministic (seeded) optimiser with no I/O of its
16 * own: the caller reads the current candidate's genes, uses them for
17 * one evaluation window, and reports the fitness it observed. The
18 * xtc_iosched write scheduler drives it with throughput/latency
19 * fitness; the unit tests drive it with a synthetic function.
20 */
21
22#ifndef XTC_DIO_SCHED_H
23#define XTC_DIO_SCHED_H
24
25#include <stdint.h>
26
27#include "xtc.h"
28
29#define XTC_DIO_SCHED_MAX_GENES 8
30
31typedef struct xtc_dio_sched xtc_dio_sched_t;
32
33typedef struct xtc_dio_sched_spec {
34 int n_genes; /* 1 .. XTC_DIO_SCHED_MAX_GENES */
35 int min[XTC_DIO_SCHED_MAX_GENES]; /* per-gene inclusive bounds */
36 int max[XTC_DIO_SCHED_MAX_GENES];
37 int init[XTC_DIO_SCHED_MAX_GENES]; /* seed candidate */
38 int population; /* candidates/generation (>=2) */
39 uint64_t seed; /* PRNG seed (0 = default) */
40
41 /* Phenotypes (Moilanen): partition the genes into groups, each
42 * tuned by its OWN fitness measure with its own selection and
43 * adaptive-mutation/freeze state. n_phenos <= 1 (the default)
44 * means one phenotype over all genes (the single-fitness case).
45 * Otherwise gene_pheno[i] is gene i's phenotype index
46 * (0 .. n_phenos-1), and the caller reports one fitness per
47 * phenotype via xtc_dio_sched_report_multi. */
48 int n_phenos;
49 int gene_pheno[XTC_DIO_SCHED_MAX_GENES];
51
52/*
53 * PUBLIC: int xtc_dio_sched_create __P((const xtc_dio_sched_spec_t *, xtc_dio_sched_t **));
54 * PUBLIC: void xtc_dio_sched_destroy __P((xtc_dio_sched_t *));
55 * PUBLIC: void xtc_dio_sched_current __P((const xtc_dio_sched_t *, int *));
56 * PUBLIC: void xtc_dio_sched_report __P((xtc_dio_sched_t *, double));
57 * PUBLIC: void xtc_dio_sched_best __P((const xtc_dio_sched_t *, int *, double *));
58 * PUBLIC: double xtc_dio_sched_mutation_rate __P((const xtc_dio_sched_t *));
59 * PUBLIC: uint64_t xtc_dio_sched_generation __P((const xtc_dio_sched_t *));
60 */
61int xtc_dio_sched_create(const xtc_dio_sched_spec_t *spec, xtc_dio_sched_t **out);
62void xtc_dio_sched_destroy(xtc_dio_sched_t *g);
63
64/* Copy the genes of the candidate currently under evaluation into
65 * out_genes (n_genes ints). Use these until the next report. */
66void xtc_dio_sched_current(const xtc_dio_sched_t *g, int *out_genes);
67
68/* Report the fitness observed while using the current candidate (higher
69 * is better). Advances to the next candidate; when the whole
70 * population has been evaluated, breeds the next generation and adapts
71 * the mutation rate. */
72void xtc_dio_sched_report(xtc_dio_sched_t *g, double fitness);
73
74/* Report one fitness per phenotype (n must equal the configured
75 * n_phenos; for a single phenotype this is equivalent to
76 * xtc_dio_sched_report). Each phenotype advances and breeds its own
77 * genes independently against its own fitness. */
78void xtc_dio_sched_report_multi(xtc_dio_sched_t *g, const double *fitness,
79 int n);
80
81/* Best gene-set found so far and its fitness. Either pointer may be
82 * NULL. This is the set to use once tuning has converged / frozen. */
83void xtc_dio_sched_best(const xtc_dio_sched_t *g, int *out_genes, double *out_fitness);
84
85double xtc_dio_sched_mutation_rate(const xtc_dio_sched_t *g);
86uint64_t xtc_dio_sched_generation(const xtc_dio_sched_t *g);
87
88#endif /* XTC_DIO_SCHED_H */