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