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 "xtc_export.h"
26
27#include <stdint.h>
28
29#include "xtc.h"
30
31#define XTC_DIO_SCHED_MAX_GENES 8
32
33typedef struct xtc_dio_sched xtc_dio_sched_t;
34
35typedef struct xtc_dio_sched_spec {
36 int n_genes; /* 1 .. XTC_DIO_SCHED_MAX_GENES */
37 int min[XTC_DIO_SCHED_MAX_GENES]; /* per-gene inclusive bounds */
38 int max[XTC_DIO_SCHED_MAX_GENES];
39 int init[XTC_DIO_SCHED_MAX_GENES]; /* seed candidate */
40 int population; /* candidates/generation (>=2) */
41 uint64_t seed; /* PRNG seed (0 = default) */
42
43 /* Phenotypes (Moilanen): partition the genes into groups, each
44 * tuned by its OWN fitness measure with its own selection and
45 * adaptive-mutation/freeze state. n_phenos <= 1 (the default)
46 * means one phenotype over all genes (the single-fitness case).
47 * Otherwise gene_pheno[i] is gene i's phenotype index
48 * (0 .. n_phenos-1), and the caller reports one fitness per
49 * phenotype via xtc_dio_sched_report_multi. */
50 int n_phenos;
51 int gene_pheno[XTC_DIO_SCHED_MAX_GENES];
53
54/*
55 * PUBLIC: int xtc_dio_sched_create __P((const xtc_dio_sched_spec_t *, xtc_dio_sched_t **));
56 * PUBLIC: void xtc_dio_sched_destroy __P((xtc_dio_sched_t *));
57 * PUBLIC: void xtc_dio_sched_current __P((const xtc_dio_sched_t *, int *));
58 * PUBLIC: void xtc_dio_sched_report __P((xtc_dio_sched_t *, double));
59 * PUBLIC: void xtc_dio_sched_best __P((const xtc_dio_sched_t *, int *, double *));
60 * PUBLIC: double xtc_dio_sched_mutation_rate __P((const xtc_dio_sched_t *));
61 * PUBLIC: uint64_t xtc_dio_sched_generation __P((const xtc_dio_sched_t *));
62 */
63XTC_API int xtc_dio_sched_create(const xtc_dio_sched_spec_t *spec, xtc_dio_sched_t **out);
64XTC_API void xtc_dio_sched_destroy(xtc_dio_sched_t *g);
65
66/* Copy the genes of the candidate currently under evaluation into
67 * out_genes (n_genes ints). Use these until the next report. */
68XTC_API void xtc_dio_sched_current(const xtc_dio_sched_t *g, int *out_genes);
69
70/* Report the fitness observed while using the current candidate (higher
71 * is better). Advances to the next candidate; when the whole
72 * population has been evaluated, breeds the next generation and adapts
73 * the mutation rate. */
74XTC_API void xtc_dio_sched_report(xtc_dio_sched_t *g, double fitness);
75
76/* Report one fitness per phenotype (n must equal the configured
77 * n_phenos; for a single phenotype this is equivalent to
78 * xtc_dio_sched_report). Each phenotype advances and breeds its own
79 * genes independently against its own fitness. */
80void xtc_dio_sched_report_multi(xtc_dio_sched_t *g, const double *fitness,
81 int n);
82
83/* Best gene-set found so far and its fitness. Either pointer may be
84 * NULL. This is the set to use once tuning has converged / frozen. */
85XTC_API void xtc_dio_sched_best(const xtc_dio_sched_t *g, int *out_genes, double *out_fitness);
86
87XTC_API double xtc_dio_sched_mutation_rate(const xtc_dio_sched_t *g);
88XTC_API uint64_t xtc_dio_sched_generation(const xtc_dio_sched_t *g);
89
90#endif /* XTC_DIO_SCHED_H */