Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/tools/dagsim/dagsim.c # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / tools / dagsim / dagsim.c` 1108 lines · 54117 bytes Raw Blame History 1 /* 2 * dagsim.c -- offline resource-DAG cost evaluator (omega-mv#583). 3 * 4 * See dagsim.h and spec/offline-evidence-v1.md. The scheduler is a 5 * deterministic fluid simulation: compute engines have slots, bandwidth 6 * resources are shared equally among the ops streaming on them at any 7 * instant, launches are serialized per launch resource, and an op finishes 8 * when both its own busy time and its traffic are done (perfect overlap 9 * inside one op, which is the issue's stated floor max(F/P, Q/B)). Ops are 10 * dispatched in file order, so the same file always gives the same bytes. 11 */ 12 #include "dagsim.h" 13 #include "runtime/rx_costmodel.h" 14 15 #include 16 #include 17 #include 18 #include 19 #include 20 #include 21 #if !CUDAPP_MIGRATION_PENDING_C_DAGSIM_PARSE 22 #include "dagsim_format.h" 23 #endif 24 25 /* ---------------------------------------------------------------- names */ 26 27 #if CUDAPP_MIGRATION_PENDING_C_DAGSIM_PARSE 28 const char *ds_status_name(int s) { 29 switch (s) { 30 case DS_OK: return "OK"; 31 case DS_REFUSED_UNKNOWN: return "REFUSED_UNKNOWN"; 32 case DS_REFUSED_STALE: return "REFUSED_STALE"; 33 case DS_REFUSED_NO_CALIBRATION: return "REFUSED_NO_CALIBRATION"; 34 case DS_REFUSED_CAPACITY: return "REFUSED_CAPACITY"; 35 case DS_REFUSED_CYCLE: return "REFUSED_CYCLE"; 36 case DS_REFUSED_UNSUPPORTED: return "REFUSED_UNSUPPORTED"; 37 case DS_REFUSED_REMOVED_REQUIRED: return "REFUSED_REMOVED_REQUIRED"; 38 case DS_REFUSED_LIFETIME: return "REFUSED_LIFETIME"; 39 case DS_PARSE_ERROR: return "PARSE_ERROR"; 40 default: return "?"; 41 } 42 } 43 44 const char *ds_prov_name(int p) { 45 switch (p) { 46 case DS_PROV_UNKNOWN: return "unknown"; 47 case DS_PROV_SYNTHETIC: return "synthetic"; 48 case DS_PROV_FITTED: return "fitted"; 49 case DS_PROV_MEASURED: return "measured"; 50 case DS_PROV_SPECIFIED: return "specified"; 51 default: return "?"; 52 } 53 } 54 55 static int prov_from_name(const char *s) { 56 if (!strcmp(s, "unknown")) return DS_PROV_UNKNOWN; 57 if (!strcmp(s, "synthetic")) return DS_PROV_SYNTHETIC; 58 if (!strcmp(s, "fitted")) return DS_PROV_FITTED; 59 if (!strcmp(s, "measured")) return DS_PROV_MEASURED; 60 if (!strcmp(s, "specified")) return DS_PROV_SPECIFIED; 61 return -1; 62 } 63 64 /* days since 1970-01-01 for a civil date (Howard Hinnant's days_from_civil) */ 65 int ds_day_from_date(const char *s) { 66 int y, m, d; 67 if (!s || sscanf(s, "%d-%d-%d", &y, &m, &d) != 3) return -1; 68 y -= m <= 2; 69 int era = (y >= 0 ? y : y - 399) / 400; 70 unsigned yoe = (unsigned)(y - era * 400); 71 unsigned doy = (153u * (unsigned)(m + (m > 2 ? -3 : 9)) + 2u) / 5u + (unsigned)d - 1u; 72 unsigned doe = yoe * 365u + yoe / 4u - yoe / 100u + doy; 73 return era * 146097 + (int)doe - 719468; 74 } 75 #else 76 const char *ds_status_name(int s) { 77 static _Thread_local char out[256]; 78 uint64_t a[16] = {(uint32_t)s}; ds_fmt(0, a, out); return out; 79 } 80 const char *ds_prov_name(int p) { 81 static _Thread_local char out[256]; 82 uint64_t a[16] = {(uint32_t)p}; ds_fmt(1, a, out); return out; 83 } 84 int ds_day_from_date(const char *s) { 85 if (!s) return -1; 86 char in[256]; 87 uint64_t a[16] = {0}; 88 for (;;) { 89 size_t n = strnlen(s, sizeof in); 90 memcpy(in, s, n); 91 a[0] = n; a[13] = n < sizeof in; 92 uint64_t rc = ds_fmt(21, a, in); 93 if (rc == 1) return (int32_t)a[12]; 94 if (rc == 2) return -1; 95 s += n; 96 } 97 } 98 #endif 99 100 #if !CUDAPP_MIGRATION_PENDING_C_DAGSIM_RESOLVE 101 static int dr_lookup(const ds_model *, const char *, int); 102 #endif 103 int ds_find_op(const ds_model *m, const char *id) { 104 #if CUDAPP_MIGRATION_PENDING_C_DAGSIM_RESOLVE 105 for (int i = 0; i < m->n_ops; i++) if (!strcmp(m->ops[i].id, id)) return i; 106 return -1; 107 #else 108 return dr_lookup(m,id,0); 109 #endif 110 } 111 int ds_find_res(const ds_model *m, const char *id) { 112 #if CUDAPP_MIGRATION_PENDING_C_DAGSIM_RESOLVE 113 for (int i = 0; i < m->n_res; i++) if (!strcmp(m->res[i].id, id)) return i; 114 return -1; 115 #else 116 return dr_lookup(m,id,1); 117 #endif 118 } 119 120 /* ---------------------------------------------------------------- parse */ 121 #if CUDAPP_MIGRATION_PENDING_C_DAGSIM_PARSE 122 /* bounded string copy; ids and sources are truncated, never overflowed */ 123 static void sset(char *dst, size_t cap, const char *src) { 124 size_t n = strlen(src); 125 if (n >= cap) n = cap - 1; 126 memcpy(dst, src, n); dst[n] = 0; 127 } 128 129 130 typedef struct { char key[32]; char val[DS_SRC]; int used; } kv; 131 132 static int split_kv(char *line, char *keyword, size_t kwcap, kv *kvs, int maxkv, char *positional[], int *npos) { 133 int n = 0; *npos = 0; keyword[0] = 0; 134 char *p = strchr(line, '#'); if (p) *p = 0; 135 char *save = NULL; 136 char *tok = strtok_r(line, " \t\r\n", &save); 137 if (!tok) return -1; /* empty */ 138 snprintf(keyword, kwcap, "%s", tok); 139 while ((tok = strtok_r(NULL, " \t\r\n", &save))) { 140 char *eq = strchr(tok, '='); 141 if (!eq) { if (*npos < 8) positional[(*npos)++] = tok; continue; } 142 if (n >= maxkv) return -2; 143 *eq = 0; 144 sset(kvs[n].key, sizeof kvs[n].key, tok); 145 sset(kvs[n].val, sizeof kvs[n].val, eq + 1); 146 kvs[n].used = 0; 147 n++; 148 } 149 return n; 150 } 151 152 static const char *get(kv *kvs, int n, const char *key) { 153 for (int i = 0; i < n; i++) if (!strcmp(kvs[i].key, key)) { kvs[i].used = 1; return kvs[i].val; } 154 return NULL; 155 } 156 157 static int unused_key(kv *kvs, int n, char *out, size_t cap) { 158 for (int i = 0; i < n; i++) if (!kvs[i].used) { snprintf(out, cap, "%s", kvs[i].key); return 1; } 159 return 0; 160 } 161 162 static int parse_real(const char *s, double *out) { 163 if (!s) return 0; 164 char *end; double v = strtod(s, &end); 165 if (end == s || *end) return 0; 166 if (!(v == v) || v < 0) return 0; 167 *out = v; return 1; 168 } 169 170 #define PERR(...) do { snprintf(err, errcap, "line %d: ", lineno); size_t _l = strlen(err); snprintf(err + _l, errcap - _l, __VA_ARGS__); return DS_PARSE_ERROR; } while (0) 171 172 static int parse_prov_src(kv *kvs, int n, int *prov, char *src, size_t srccap, int required, char *err, size_t errcap, int lineno) { 173 const char *p = get(kvs, n, "prov"); 174 const char *s = get(kvs, n, "src"); 175 if (!p) { if (required) PERR("prov= is required wherever a number appears"); *prov = -1; return DS_OK; } 176 int v = prov_from_name(p); 177 if (v < 0) PERR("prov=%s is not one of specified|measured|fitted|synthetic|unknown", p); 178 *prov = v; 179 if ((v == DS_PROV_MEASURED || v == DS_PROV_FITTED) && !s) PERR("prov=%s needs src=", p); 180 snprintf(src, srccap, "%s", s ? s : ""); 181 return DS_OK; 182 } 183 184 int ds_parse_text(const char *text, ds_model *m, char *err, size_t errcap) { 185 memset(m, 0, sizeof *m); 186 err[0] = 0; 187 int lineno = 0; 188 const char *cur = text; 189 int seen_oev = 0, seen_machine = 0, seen_build = 0; 190 /* two-pass is not needed: ops before deps/buffers is the convention; we 191 * resolve references at use and require definition before use. */ 192 while (*cur) { 193 const char *nl = strchr(cur, '\n'); 194 size_t len = nl ? (size_t)(nl - cur) : strlen(cur); 195 char line[1024]; 196 lineno++; 197 if (len >= sizeof line) PERR("line too long"); 198 memcpy(line, cur, len); line[len] = 0; 199 cur = nl ? nl + 1 : cur + len; 200 char keyword[32]; kv kvs[24]; char *pos[8]; int npos; 201 int n = split_kv(line, keyword, sizeof keyword, kvs, 24, pos, &npos); 202 if (n == -1) continue; 203 if (n == -2) PERR("too many fields"); 204 if (!strcmp(keyword, "oev")) { 205 const char *v = get(kvs, n, "version"); 206 if (!v || strcmp(v, "1")) PERR("oev version=1 required"); 207 m->version = 1; seen_oev = 1; 208 } else if (!strcmp(keyword, "machine")) { 209 const char *v; 210 if (!(v = get(kvs, n, "id"))) PERR("machine needs id="); 211 sset(m->machine_id, sizeof m->machine_id, v); 212 if ((v = get(kvs, n, "soc"))) sset(m->soc, sizeof m->soc, v); 213 if ((v = get(kvs, n, "cores"))) m->cores = atoi(v); 214 if ((v = get(kvs, n, "mem_bytes")) && !parse_real(v, &m->mem_bytes)) PERR("bad mem_bytes"); 215 int prov; char src[DS_SRC]; 216 int rc = parse_prov_src(kvs, n, &prov, src, sizeof src, 1, err, errcap, lineno); if (rc) return rc; 217 seen_machine = 1; 218 } else if (!strcmp(keyword, "build")) { 219 const char *v; 220 if ((v = get(kvs, n, "repo"))) sset(m->repo, sizeof m->repo, v); 221 if (!(v = get(kvs, n, "sha")) || strlen(v) != 40) PERR("build needs sha=<40 hex>"); 222 sset(m->build_sha, sizeof m->build_sha, v); 223 if ((v = get(kvs, n, "physics"))) sset(m->physics, sizeof m->physics, v); 224 seen_build = 1; 225 } else if (!strcmp(keyword, "calibration")) { 226 const char *v; 227 if (m->has_calibration) PERR("one calibration record only"); 228 if (!(v = get(kvs, n, "id"))) PERR("calibration needs id="); 229 sset(m->cal_id, sizeof m->cal_id, v); 230 if (!(v = get(kvs, n, "taken")) || (m->cal_taken_day = ds_day_from_date(v)) < 0) PERR("calibration needs taken=YYYY-MM-DD"); 231 if (!(v = get(kvs, n, "valid_days"))) PERR("calibration needs valid_days="); 232 m->cal_valid_days = atoi(v); 233 if (!(v = get(kvs, n, "load1")) || !parse_real(v, &m->cal_load1)) PERR("calibration needs load1="); 234 if (!(v = get(kvs, n, "src"))) PERR("calibration needs src="); 235 sset(m->cal_src, sizeof m->cal_src, v); 236 if ((v = get(kvs, n, "model"))) sset(m->cal_model, sizeof m->cal_model, v); 237 m->has_calibration = 1; 238 } else if (!strcmp(keyword, "resource")) { 239 if (m->n_res >= DS_MAX_RES) PERR("too many resources (max %d)", DS_MAX_RES); 240 ds_resource *r = &m->res[m->n_res]; 241 memset(r, 0, sizeof *r); 242 const char *v; 243 if (!(v = get(kvs, n, "id"))) PERR("resource needs id="); 244 if (ds_find_res(m, v) >= 0) PERR("duplicate resource %s", v); 245 sset(r->id, sizeof r->id, v); 246 if (!(v = get(kvs, n, "kind"))) PERR("resource needs kind="); 247 if (!strcmp(v, "compute")) { 248 r->kind = DS_RES_COMPUTE; 249 if (!(v = get(kvs, n, "slots")) || (r->slots = atoi(v)) < 1) PERR("compute resource needs slots>=1"); 250 } else if (!strcmp(v, "bandwidth")) { 251 r->kind = DS_RES_BANDWIDTH; 252 if (!parse_real(get(kvs, n, "cap"), &r->cap) || r->cap <= 0) PERR("bandwidth resource needs cap>0"); 253 r->lo = r->hi = r->cap; 254 if ((v = get(kvs, n, "lo")) && !parse_real(v, &r->lo)) PERR("bad lo"); 255 if ((v = get(kvs, n, "hi")) && !parse_real(v, &r->hi)) PERR("bad hi"); 256 if (r->lo > r->cap || r->hi < r->cap) PERR("need lo <= cap <= hi"); 257 } else if (!strcmp(v, "capacity")) { 258 r->kind = DS_RES_CAPACITY; 259 if (!parse_real(get(kvs, n, "cap"), &r->cap) || r->cap <= 0) PERR("capacity resource needs cap>0"); 260 } else if (!strcmp(v, "launch")) { 261 r->kind = DS_RES_LAUNCH; 262 } else PERR("resource kind=%s unknown", v); 263 int rc = parse_prov_src(kvs, n, &r->prov, r->src, sizeof r->src, 1, err, errcap, lineno); if (rc) return rc; 264 m->n_res++; 265 } else if (!strcmp(keyword, "op")) { 266 if (m->n_ops >= DS_MAX_OPS) PERR("too many ops (max %d)", DS_MAX_OPS); 267 ds_op *o = &m->ops[m->n_ops]; 268 memset(o, 0, sizeof *o); 269 o->engine = o->bw = o->launcher = -1; 270 const char *v; 271 if (!(v = get(kvs, n, "id"))) PERR("op needs id="); 272 if (ds_find_op(m, v) >= 0) PERR("duplicate op %s", v); 273 sset(o->id, sizeof o->id, v); 274 if (!(v = get(kvs, n, "kind"))) PERR("op needs kind="); 275 if (!strcmp(v, "compute")) o->kind = DS_OP_COMPUTE; 276 else if (!strcmp(v, "copy")) o->kind = DS_OP_COPY; 277 else if (!strcmp(v, "matvec")) o->kind = DS_OP_MATVEC; 278 else if (!strcmp(v, "barrier")) o->kind = DS_OP_BARRIER; 279 else if (!strcmp(v, "storage")) o->kind = DS_OP_STORAGE; 280 else if (!strcmp(v, "persist")) o->kind = DS_OP_PERSIST; 281 else PERR("op kind=%s unknown", v); 282 if ((v = get(kvs, n, "engine"))) { 283 o->engine = ds_find_res(m, v); 284 if (o->engine < 0 || m->res[o->engine].kind != DS_RES_COMPUTE) PERR("engine=%s is not a compute resource defined above", v); 285 } 286 if ((v = get(kvs, n, "t_ns"))) { if (!parse_real(v, &o->t_ns)) PERR("bad t_ns"); o->has_t = 1; o->lo = o->hi = o->t_ns; } 287 if ((v = get(kvs, n, "lo"))) { if (!o->has_t || !parse_real(v, &o->lo) || o->lo > o->t_ns) PERR("lo needs t_ns and lo <= t_ns"); } 288 if ((v = get(kvs, n, "hi"))) { if (!o->has_t || !parse_real(v, &o->hi) || o->hi < o->t_ns) PERR("hi needs t_ns and hi >= t_ns"); } 289 if ((v = get(kvs, n, "bytes"))) { 290 if (!parse_real(v, &o->bytes)) PERR("bad bytes"); 291 const char *b = get(kvs, n, "bw"); 292 if (!b || (o->bw = ds_find_res(m, b)) < 0 || m->res[o->bw].kind != DS_RES_BANDWIDTH) PERR("bytes= needs bw="); 293 } 294 if ((v = get(kvs, n, "launch_ns"))) { 295 if (!parse_real(v, &o->launch_ns)) PERR("bad launch_ns"); 296 const char *l = get(kvs, n, "launcher"); 297 if (!l || (o->launcher = ds_find_res(m, l)) < 0 || m->res[o->launcher].kind != DS_RES_LAUNCH) PERR("launch_ns= needs launcher="); 298 } 299 if ((v = get(kvs, n, "M"))) o->M = (uint32_t)strtoul(v, NULL, 10); 300 if ((v = get(kvs, n, "N"))) o->N = (uint32_t)strtoul(v, NULL, 10); 301 if ((v = get(kvs, n, "core"))) o->core = (uint32_t)strtoul(v, NULL, 10); 302 if ((v = get(kvs, n, "arm"))) o->arm = (uint32_t)strtoul(v, NULL, 10); 303 if (o->kind == DS_OP_MATVEC && !o->has_t && (o->M == 0 || o->N == 0)) PERR("matvec needs M= and N= (or t_ns=)"); 304 if (o->kind != DS_OP_BARRIER && o->engine < 0) PERR("op %s needs engine=", o->id); 305 int required = o->has_t || o->bytes > 0 || o->launch_ns > 0 || o->kind == DS_OP_MATVEC; 306 int rc = parse_prov_src(kvs, n, &o->t_prov, o->src, sizeof o->src, required, err, errcap, lineno); if (rc) return rc; 307 if (o->t_prov < 0) o->t_prov = DS_PROV_SPECIFIED; /* a costless barrier: ordering only */ 308 if (!o->has_t && o->kind != DS_OP_MATVEC && o->t_prov == DS_PROV_UNKNOWN) { /* fine: explicitly unknown */ } 309 m->n_ops++; 310 } else if (!strcmp(keyword, "dep")) { 311 if (npos != 2 || n != 0) PERR("dep "); 312 if (m->n_deps >= DS_MAX_DEPS) PERR("too many deps"); 313 int a = ds_find_op(m, pos[0]), b = ds_find_op(m, pos[1]); 314 if (a < 0 || b < 0) PERR("dep names an op not defined above: %s %s", pos[0], pos[1]); 315 m->deps[m->n_deps].from = a; m->deps[m->n_deps].to = b; m->n_deps++; 316 } else if (!strcmp(keyword, "buffer")) { 317 if (m->n_buf >= DS_MAX_BUF) PERR("too many buffers"); 318 ds_buffer *b = &m->buf[m->n_buf]; 319 memset(b, 0, sizeof *b); b->first = b->last = -1; 320 const char *v; 321 if (!(v = get(kvs, n, "id"))) PERR("buffer needs id="); 322 sset(b->id, sizeof b->id, v); 323 if (!parse_real(get(kvs, n, "bytes"), &b->bytes) || !isfinite(b->bytes)) 324 PERR("buffer bytes must be a finite number >= 0"); 325 if (!(v = get(kvs, n, "cap")) || (b->cap = ds_find_res(m, v)) < 0 || m->res[b->cap].kind != DS_RES_CAPACITY) PERR("buffer needs cap="); 326 if ((v = get(kvs, n, "first")) && (b->first = ds_find_op(m, v)) < 0) PERR("first=%s not an op", v); 327 if ((v = get(kvs, n, "last")) && (b->last = ds_find_op(m, v)) < 0) PERR("last=%s not an op", v); 328 m->n_buf++; 329 } else if (!strcmp(keyword, "sync")) { 330 /* a barrier op that depends on every listed op */ 331 if (m->n_ops >= DS_MAX_OPS) PERR("too many ops"); 332 const char *v, *list; 333 if (!(v = get(kvs, n, "id"))) PERR("sync needs id="); 334 if (ds_find_op(m, v) >= 0) PERR("duplicate op %s", v); 335 if (!(list = get(kvs, n, "ops"))) PERR("sync needs ops=a,b,..."); 336 ds_op *o = &m->ops[m->n_ops]; 337 memset(o, 0, sizeof *o); 338 o->engine = o->bw = o->launcher = -1; o->kind = DS_OP_BARRIER; o->t_prov = DS_PROV_SPECIFIED; 339 sset(o->id, sizeof o->id, v); 340 int self = m->n_ops++; 341 char tmp[DS_SRC]; sset(tmp, sizeof tmp, list); 342 char *save = NULL; 343 for (char *t = strtok_r(tmp, ",", &save); t; t = strtok_r(NULL, ",", &save)) { 344 int a = ds_find_op(m, t); 345 if (a < 0 || a == self) PERR("sync lists unknown op %s", t); 346 if (m->n_deps >= DS_MAX_DEPS) PERR("too many deps"); 347 m->deps[m->n_deps].from = a; m->deps[m->n_deps].to = self; m->n_deps++; 348 } 349 } else if (!strcmp(keyword, "energy")) { 350 const char *v; 351 if (m->has_energy) PERR("one energy record only"); 352 if (!(v = get(kvs, n, "instrument"))) PERR("energy needs instrument="); 353 sset(m->instrument, sizeof m->instrument, v); 354 if ((v = get(kvs, n, "boundary"))) sset(m->boundary, sizeof m->boundary, v); 355 const char *j = get(kvs, n, "joules"), *pw = get(kvs, n, "power_w"); 356 int prov = -1; char src[DS_SRC]; 357 int rc = parse_prov_src(kvs, n, &prov, src, sizeof src, j || pw ? 1 : 0, err, errcap, lineno); if (rc) return rc; 358 m->energy_prov = prov; 359 if (j) { 360 if (!strcmp(m->instrument, "none") || !m->boundary[0]) PERR("joules= needs a named instrument and boundary="); 361 if (prov != DS_PROV_MEASURED) PERR("joules= must be prov=measured with src="); 362 if (!parse_real(j, &m->joules)) PERR("bad joules"); 363 } 364 if (pw) { 365 if (prov != DS_PROV_SYNTHETIC) PERR("power_w= is allowed only as prov=synthetic (a sensitivity figure); measured power must be integrated into joules= by the instrument"); 366 if (!parse_real(pw, &m->power_w)) PERR("bad power_w"); 367 } 368 m->has_energy = 1; 369 } else if (!strcmp(keyword, "removed")) { 370 if (m->n_removed >= DS_MAX_REMOVED) PERR("too many removed records"); 371 ds_removed *r = &m->removed[m->n_removed]; 372 memset(r, 0, sizeof *r); 373 const char *v; 374 if (!(v = get(kvs, n, "op"))) PERR("removed needs op="); 375 sset(r->op, sizeof r->op, v); 376 if (ds_find_op(m, v) >= 0) PERR("removed op %s is still present in this DAG", v); 377 if (!(v = get(kvs, n, "reason"))) PERR("removed needs reason="); 378 if (!strcmp(v, "eliminated")) r->reason = DS_RM_ELIMINATED; 379 else if (!strcmp(v, "reused")) r->reason = DS_RM_REUSED; 380 else if (!strcmp(v, "unchanged_dep")) r->reason = DS_RM_UNCHANGED_DEP; 381 else if (!strcmp(v, "deferred")) r->reason = DS_RM_DEFERRED; 382 else if (!strcmp(v, "moved")) r->reason = DS_RM_MOVED; 383 else if (!strcmp(v, "shared")) r->reason = DS_RM_SHARED; 384 else if (!strcmp(v, "lookup")) r->reason = DS_RM_LOOKUP; 385 else PERR("removed reason=%s unknown", v); 386 if ((v = get(kvs, n, "ref"))) sset(r->ref, sizeof r->ref, v); 387 if ((v = get(kvs, n, "proof"))) sset(r->proof, sizeof r->proof, v); 388 m->n_removed++; 389 } else PERR("unknown record '%s'", keyword); 390 char bad[32]; 391 if (unused_key(kvs, n, bad, sizeof bad)) PERR("unknown field %s= on '%s'", bad, keyword); 392 } 393 lineno = 0; 394 if (!seen_oev) PERR("missing 'oev version=1'"); 395 if (!seen_machine) PERR("missing machine record"); 396 if (!seen_build) PERR("missing build record"); 397 if (!m->has_energy) PERR("missing energy record (use instrument=none for UNMEASURED)"); 398 return DS_OK; 399 } 400 #else 401 /* Copy only the already-bounded source spans selected by the Omega parser. */ 402 static void dp_copy(char *dst, const unsigned char *text, const uint64_t *w, size_t i) { 403 memcpy(dst, text + w[i], (size_t)w[i + 1]); 404 dst[w[i + 1]] = 0; 405 } 406 static double dp_double(const uint64_t *w, size_t i) { 407 return ds_unbits(w[i]); 408 } 409 int ds_parse_text(const char *text, ds_model *m, char *err, size_t errcap) { 410 memset(m, 0, sizeof *m); 411 if (errcap) err[0] = 0; 412 size_t n = strnlen(text, `1048576`); 413 if (n == `1048576`) { 414 if (errcap) snprintf(err, errcap, "file size out of range"); 415 return DS_PARSE_ERROR; 416 } 417 unsigned char *t = malloc(`1048576`); 418 uint64_t *w = malloc(16384 * sizeof *w); 419 if (!t || !w) { 420 free(t); free(w); 421 if (errcap) snprintf(err, errcap, "out of memory"); 422 return DS_PARSE_ERROR; 423 } 424 memcpy(t, text, n + 1); 425 unsigned char error[256] = {0}; 426 uint64_t rc = 0; 427 uint32_t trap = omega_dag_parse(&rc, t, `1048576`, n, w, 16384, error, 256); 428 if (trap || rc) { 429 if (errcap) snprintf(err, errcap, "%s", trap ? "Omega parser trap" : (char *)error); 430 free(t); free(w); return DS_PARSE_ERROR; 431 } 432 m->version = (int32_t)w[0]; m->cores = (int32_t)w[1]; 433 m->mem_bytes = dp_double(w, 2); m->has_calibration = (int32_t)w[3]; 434 m->cal_taken_day = (int32_t)w[4]; m->cal_valid_days = (int32_t)w[5]; 435 m->cal_load1 = dp_double(w, 6); m->has_energy = (int32_t)w[7]; 436 m->joules = dp_double(w, 8); m->power_w = dp_double(w, 9); 437 m->energy_prov = (int32_t)w[10]; 438 m->n_res = (int32_t)w[11]; m->n_ops = (int32_t)w[12]; 439 m->n_buf = (int32_t)w[13]; m->n_deps = (int32_t)w[14]; m->n_removed = (int32_t)w[15]; 440 dp_copy(m->machine_id, t, w, 17); dp_copy(m->soc, t, w, 19); 441 dp_copy(m->repo, t, w, 21); dp_copy(m->build_sha, t, w, 23); 442 dp_copy(m->physics, t, w, 25); dp_copy(m->cal_id, t, w, 27); 443 dp_copy(m->cal_src, t, w, 29); dp_copy(m->cal_model, t, w, 31); 444 dp_copy(m->instrument, t, w, 33); dp_copy(m->boundary, t, w, 35); 445 for (int i = 0; i < m->n_res; i++) { 446 ds_resource *r = &m->res[i]; const uint64_t *p = w + 64 + i * 12; 447 dp_copy(r->id, t, p, 0); r->kind = (int32_t)p[2]; r->slots = (int32_t)p[3]; 448 r->cap = dp_double(p, 4); r->lo = dp_double(p, 5); r->hi = dp_double(p, 6); 449 r->prov = (int32_t)p[7]; dp_copy(r->src, t, p, 8); 450 } 451 for (int i = 0; i < m->n_ops; i++) { 452 ds_op *o = &m->ops[i]; const uint64_t *p = w + 448 + i * 26; 453 dp_copy(o->id, t, p, 0); o->kind = (int32_t)p[2]; o->engine = (int32_t)p[3]; 454 o->t_ns = dp_double(p, 4); o->lo = dp_double(p, 5); o->hi = dp_double(p, 6); 455 o->t_prov = (int32_t)p[7]; o->has_t = (int32_t)p[8]; o->bytes = dp_double(p, 9); 456 o->bw = (int32_t)p[10]; o->launch_ns = dp_double(p, 11); o->launcher = (int32_t)p[12]; 457 o->M = (uint32_t)p[13]; o->N = (uint32_t)p[14]; o->core = (uint32_t)p[15]; o->arm = (uint32_t)p[16]; 458 dp_copy(o->src, t, p, 17); 459 } 460 for (int i = 0; i < m->n_deps; i++) { 461 m->deps[i].from = (int32_t)w[7104 + i * 2]; 462 m->deps[i].to = (int32_t)w[7105 + i * 2]; 463 } 464 for (int i = 0; i < m->n_buf; i++) { 465 ds_buffer *b = &m->buf[i]; const uint64_t *p = w + 9152 + i * 8; 466 dp_copy(b->id, t, p, 0); b->bytes = dp_double(p, 2); b->cap = (int32_t)p[3]; 467 b->first = (int32_t)p[4]; b->last = (int32_t)p[5]; 468 } 469 for (int i = 0; i < m->n_removed; i++) { 470 ds_removed *r = &m->removed[i]; const uint64_t *p = w + 11200 + i * 8; 471 dp_copy(r->op, t, p, 0); r->reason = (int32_t)p[2]; 472 dp_copy(r->ref, t, p, 3); dp_copy(r->proof, t, p, 5); 473 } 474 free(t); free(w); 475 return DS_OK; 476 } 477 #endif 478 479 int ds_parse_file(const char *path, ds_model *m, char *err, size_t errcap) { 480 FILE *fp = fopen(path, "r"); 481 if (!fp) { snprintf(err, errcap, "cannot open %s", path); return DS_PARSE_ERROR; } 482 fseek(fp, 0, SEEK_END); long n = ftell(fp); fseek(fp, 0, SEEK_SET); 483 if (n < 0 || n > (1 << 20)) { fclose(fp); snprintf(err, errcap, "file size out of range"); return DS_PARSE_ERROR; } 484 char *buf = malloc((size_t)n + 1); 485 if (!buf) { fclose(fp); snprintf(err, errcap, "out of memory"); return DS_PARSE_ERROR; } 486 size_t got = fread(buf, 1, (size_t)n, fp); buf[got] = 0; fclose(fp); 487 int rc = ds_parse_text(buf, m, err, errcap); 488 free(buf); 489 return rc; 490 } 491 492 /* ---------------------------------------------------------------- evaluate */ 493 494 /* resolved per-op numbers for one evaluation mode */ 495 typedef struct { double t, launch, bytes, cap; int prov; } ds_num; /* cap in B/ns */ 496 497 #if CUDAPP_MIGRATION_PENDING_C_DAGSIM_RESOLVE 498 static double res_cap(const ds_resource *r, int mode) { 499 if (mode < 0) return r->lo; 500 if (mode > 0) return r->hi; 501 return r->cap; 502 } 503 #endif 504 505 static int refuse(ds_result *r, int status, const char *fmt, const char *a, const char *b) { 506 memset(r, 0, sizeof *r); 507 r->status = status; 508 snprintf(r->reason, sizeof r->reason, fmt, a ? a : "", b ? b : ""); 509 return status; 510 } 511 512 static int refuse_msg(ds_result *r, int status, const char *msg) { 513 memset(r, 0, sizeof *r); 514 r->status = status; 515 snprintf(r->reason, sizeof r->reason, "%s", msg ? msg : ""); 516 return status; 517 } 518 519 #if CUDAPP_MIGRATION_PENDING_C_DAGSIM_RESOLVE 520 /* An unsupported operation and a refused prediction are the same as an 521 * unknown cell: no certificate. known alone is not enough. */ 522 static int predict_unusable(int st, const RxCmPrediction *p) { 523 return st == RX_CM_ERR_UNSUPPORTED || p->refused || !p->known || p->n == 0; /* PREDICT_REFUSAL_CHECK */ 524 } 525 526 static int resolve(const ds_model *m, const ds_opts *o, ds_num *num, ds_result *r) { 527 int any_cal = 0; 528 for (int i = 0; i < m->n_res; i++) { 529 if (m->res[i].prov == DS_PROV_UNKNOWN) return refuse(r, DS_REFUSED_UNKNOWN, "resource %s has prov=unknown; unknown stays unknown%s", m->res[i].id, ""); 530 if (m->res[i].prov == DS_PROV_MEASURED || m->res[i].prov == DS_PROV_FITTED) any_cal = 1; 531 } 532 for (int i = 0; i < m->n_ops; i++) { 533 const ds_op *op = &m->ops[i]; 534 ds_num *x = &num[i]; 535 memset(x, 0, sizeof *x); 536 x->prov = op->t_prov; 537 x->launch = op->launch_ns; 538 x->bytes = op->bytes; 539 x->cap = op->bw >= 0 ? res_cap(&m->res[op->bw], o->mode) * 1e-9 : 0; /* B/s -> B/ns */ 540 if (op->kind == DS_OP_MATVEC && !op->has_t) { 541 const RxCostModel *cm = (const RxCostModel *)o->costmodel; 542 if (!cm) return refuse(r, DS_REFUSED_UNKNOWN, "op %s is a matvec with no rx_costmodel blob supplied (calibration model=)%s", op->id, ""); 543 RxCmFeatures f; memset(&f, 0, sizeof f); 544 f.op = RX_CM_OP_MATVEC; f.M = op->M; f.N = op->N; 545 f.state_bytes = ((uint64_t)op->M * op->N + op->M + op->N) * 8u; 546 f.cache_bytes = 1u << 20; f.core = op->core; f.eligible = 1u << op->arm; f.current_arm = op->arm; 547 RxCmPrediction p; 548 int st = rx_cm_predict(cm, &f, op->arm, &p); 549 if (predict_unusable(st, &p)) return refuse(r, DS_REFUSED_UNKNOWN, "op %s: rx_costmodel has no known cell for this (op, core, pressure) and arm; measure it first%s", op->id, ""); 550 double q = o->interval_q > 0 && o->interval_q < 1 ? o->interval_q : 0.9; 551 double k = rx_cm_t_quantile(q, p.dof); 552 double l2 = p.mean_log2_ps + (o->mode < 0 ? k * p.sd_log2 : o->mode > 0 ? -k * p.sd_log2 : 0); 553 x->t = pow(2.0, l2) / 1000.0; /* ps -> ns */ 554 x->prov = DS_PROV_FITTED; 555 any_cal = 1; 556 } else { 557 if (op->t_prov == DS_PROV_UNKNOWN) return refuse(r, DS_REFUSED_UNKNOWN, "op %s has prov=unknown (%s); unknown stays unknown", op->id, op->src[0] ? op->src : "no source"); 558 x->t = o->mode < 0 ? op->hi : o->mode > 0 ? op->lo : op->t_ns; 559 if (op->t_prov == DS_PROV_MEASURED || op->t_prov == DS_PROV_FITTED) any_cal = 1; 560 } 561 if (op->engine < 0 && op->kind != DS_OP_BARRIER) return refuse(r, DS_REFUSED_UNSUPPORTED, "op %s has no engine%s", op->id, ""); 562 if (op->launcher >= 0 && op->engine < 0) return refuse(r, DS_REFUSED_UNSUPPORTED, "op %s: launch without an engine%s", op->id, ""); 563 } 564 if (any_cal) { 565 if (!m->has_calibration) return refuse(r, DS_REFUSED_NO_CALIBRATION, "measured or fitted values need a calibration record naming when and under what load they were taken%s%s", "", ""); 566 int asof = o->asof_day; 567 if (asof <= 0) asof = (int)(time(NULL) / 86400); 568 if (asof > m->cal_taken_day + m->cal_valid_days) { 569 char d[64]; snprintf(d, sizeof d, "asof is %d day(s) past the validity envelope", asof - m->cal_taken_day - m->cal_valid_days); 570 return refuse(r, DS_REFUSED_STALE, "calibration %s is stale: %s", m->cal_id, d); 571 } 572 } 573 return DS_OK; 574 } 575 #else 576 #include "dagsim_resolve.h" 577 #endif 578 579 static void pk_d(uint64_t *dst, double x) { 580 uint64_t b; 581 memcpy(&b, &x, 8); 582 *dst = b; 583 } 584 585 /* Pack post-resolve numbers for omega_dag_eval. Refusal strings stay in C. */ 586 int ds_resolved_pack(const ds_model *m, const ds_opts *o, uint64_t *model, ds_result *r) { 587 #if CUDAPP_MIGRATION_PENDING_C_DAGSIM_RESOLVE 588 if (m->n_ops == 0) return refuse(r, DS_REFUSED_UNSUPPORTED, "empty DAG%s%s", "", ""); 589 ds_num num[DS_MAX_OPS]; 590 int rc = resolve(m, o, num, r); 591 if (rc) return rc; 592 memset(model, 0, 8192 * sizeof *model); 593 model[0] = (uint64_t)m->n_ops; 594 model[1] = (uint64_t)m->n_res; 595 model[2] = (uint64_t)m->n_deps; 596 model[3] = (uint64_t)m->n_buf; 597 model[4] = m->has_energy ? 1u : 0u; 598 pk_d(&model[5], m->joules); 599 pk_d(&model[6], m->power_w); 600 model[7] = (uint64_t)m->energy_prov; 601 model[8] = strcmp(m->instrument, "none") == 0 ? 1u : 0u; 602 model[9] = 1u; 603 for (int i = 0; i < m->n_ops; i++) { 604 uint64_t *p = &model[32 + i * 10]; 605 pk_d(&p[0], num[i].t); 606 pk_d(&p[1], num[i].bytes); 607 pk_d(&p[2], num[i].launch); 608 pk_d(&p[3], num[i].cap); 609 p[4] = (uint64_t)(int64_t)m->ops[i].engine; 610 p[5] = (uint64_t)(int64_t)m->ops[i].bw; 611 p[6] = (uint64_t)(int64_t)m->ops[i].launcher; 612 p[7] = (uint64_t)m->ops[i].kind; 613 p[8] = (uint64_t)num[i].prov; 614 } 615 for (int i = 0; i < m->n_res; i++) { 616 uint64_t *p = &model[2592 + i * 4]; 617 p[0] = (uint64_t)m->res[i].kind; 618 p[1] = (uint64_t)m->res[i].slots; 619 pk_d(&p[2], m->res[i].cap); 620 p[3] = (uint64_t)m->res[i].prov; 621 } 622 #else 623 int rc=dr_resolved(m,o,model,r); 624 if(rc)return rc; 625 #endif 626 for (int i = 0; i < m->n_deps; i++) { 627 uint64_t *p = &model[2720 + i * 2]; 628 p[0] = (uint64_t)m->deps[i].from; 629 p[1] = (uint64_t)m->deps[i].to; 630 } 631 for (int i = 0; i < m->n_buf; i++) { 632 uint64_t *p = &model[4768 + i * 4]; 633 pk_d(&p[0], m->buf[i].bytes); 634 p[1] = (uint64_t)m->buf[i].cap; 635 p[2] = (uint64_t)(int64_t)m->buf[i].first; 636 p[3] = (uint64_t)(int64_t)m->buf[i].last; 637 } 638 return DS_OK; 639 } 640 static const char *endpoint_name(const ds_model *m, int op) { 641 return op >= 0 ? m->ops[op].id : "omitted"; 642 } 643 644 #ifdef CUDAPP_MIGRATION_PENDING_C_TOOLS 645 /* Kahn topological order; returns 0 on a cycle */ 646 static int topo(const ds_model *m, int *order) { 647 int indeg[DS_MAX_OPS] = {0}; 648 for (int i = 0; i < m->n_deps; i++) indeg[m->deps[i].to]++; 649 int n = 0, head = 0; 650 for (int i = 0; i < m->n_ops; i++) if (indeg[i] == 0) order[n++] = i; 651 while (head < n) { 652 int u = order[head++]; 653 for (int i = 0; i < m->n_deps; i++) if (m->deps[i].from == u && --indeg[m->deps[i].to] == 0) order[n++] = m->deps[i].to; 654 } 655 return n == m->n_ops; 656 } 657 658 enum { ST_WAIT = 0, ST_READY, ST_LAUNCH, ST_RUN, ST_DONE }; 659 enum { BLK_NONE = 0, BLK_SLOT, BLK_LAUNCHER }; 660 661 /* Half-open [start, end). Missing first -> 0; missing last -> makespan. */ 662 static void buffer_window(const ds_model *m, const ds_result *r, int b, double *s, double *e) { 663 *s = m->buf[b].first >= 0 ? r->start_ns[m->buf[b].first] : 0.0; 664 *e = m->buf[b].last >= 0 ? r->end_ns[m->buf[b].last] : r->makespan_ns; 665 } 666 667 /* Resolved end earlier than start is not an interval. Refuse before the ledger. */ 668 static int reject_inverted_lifetimes(const ds_model *m, ds_result *r) { 669 for (int b = 0; b < m->n_buf; b++) { 670 double s, e; 671 buffer_window(m, r, b, &s, &e); 672 if (e + 1e-9 < s) { 673 char msg[256]; 674 snprintf(msg, sizeof msg, 675 "inverted lifetime for buffer %s: first=%s start_ns=%.9g last=%s end_ns=%.9g", 676 m->buf[b].id, endpoint_name(m, m->buf[b].first), s, 677 endpoint_name(m, m->buf[b].last), e); 678 return refuse_msg(r, DS_REFUSED_LIFETIME, msg); 679 } 680 } 681 return DS_OK; 682 } 683 684 int ds_eval_c(const ds_model *m, const ds_opts *o, ds_result *r) { 685 memset(r, 0, sizeof *r); 686 if (m->n_ops == 0) return refuse(r, DS_REFUSED_UNSUPPORTED, "empty DAG%s%s", "", ""); 687 ds_num num[DS_MAX_OPS]; 688 int rc = resolve(m, o, num, r); 689 if (rc) return rc; 690 int order[DS_MAX_OPS]; 691 if (!topo(m, order)) return refuse(r, DS_REFUSED_CYCLE, "dependency cycle%s%s", "", ""); 692 /* a buffer that never fits */ 693 for (int i = 0; i < m->n_buf; i++) 694 if (m->buf[i].bytes > m->res[m->buf[i].cap].cap) 695 return refuse(r, DS_REFUSED_CAPACITY, "buffer %s alone exceeds capacity %s", m->buf[i].id, m->res[m->buf[i].cap].id); 696 697 /* provenance mask */ 698 for (int i = 0; i < m->n_ops; i++) r->prov_mask |= 1u << num[i].prov; 699 for (int i = 0; i < m->n_res; i++) { 700 int used = 0; 701 for (int j = 0; j < m->n_ops; j++) if (m->ops[j].engine == i || m->ops[j].bw == i || m->ops[j].launcher == i) used = 1; 702 for (int j = 0; j < m->n_buf; j++) if (m->buf[j].cap == i) used = 1; 703 if (used) r->prov_mask |= 1u << m->res[i].prov; 704 } 705 706 /* ---- fluid simulation ---- */ 707 int state[DS_MAX_OPS] = {0}, blocked[DS_MAX_OPS] = {0}, npred[DS_MAX_OPS] = {0}, done_pred[DS_MAX_OPS] = {0}; 708 double rem_t[DS_MAX_OPS], rem_b[DS_MAX_OPS], rem_l[DS_MAX_OPS], run_start[DS_MAX_OPS], bytes_end[DS_MAX_OPS]; 709 int free_slots[DS_MAX_RES], launcher_busy[DS_MAX_RES] = {0}; 710 for (int i = 0; i < m->n_res; i++) free_slots[i] = m->res[i].slots; 711 for (int i = 0; i < m->n_deps; i++) npred[m->deps[i].to]++; 712 for (int i = 0; i < m->n_ops; i++) { 713 rem_t[i] = num[i].t; rem_b[i] = num[i].bytes; rem_l[i] = num[i].launch; 714 run_start[i] = bytes_end[i] = -1; 715 r->n_ops++; 716 r->total_bytes += num[i].bytes; r->total_busy_ns += num[i].t; r->total_launch_ns += num[i].launch; 717 } 718 double t = 0; 719 int remaining = m->n_ops, guard = 0; 720 while (remaining > 0) { 721 if (++guard > 8 * DS_MAX_OPS + 16) return refuse(r, DS_REFUSED_UNSUPPORTED, "scheduler did not converge%s%s", "", ""); 722 /* deps satisfied -> ready */ 723 for (int i = 0; i < m->n_ops; i++) if (state[i] == ST_WAIT && done_pred[i] == npred[i]) { state[i] = ST_READY; r->ready_ns[i] = t; } 724 /* dispatch in file order */ 725 for (int i = 0; i < m->n_ops; i++) { 726 if (state[i] != ST_READY) continue; 727 const ds_op *op = &m->ops[i]; 728 if (op->engine >= 0 && free_slots[op->engine] <= 0) { blocked[i] = BLK_SLOT; continue; } 729 if (op->launcher >= 0 && rem_l[i] > 0 && launcher_busy[op->launcher]) { blocked[i] = BLK_LAUNCHER; continue; } 730 if (op->engine >= 0) free_slots[op->engine]--; 731 r->start_ns[i] = t; 732 if (op->launcher >= 0 && rem_l[i] > 0) { launcher_busy[op->launcher] = 1; state[i] = ST_LAUNCH; } 733 else { state[i] = ST_RUN; run_start[i] = t; } 734 } 735 /* next event */ 736 int active[DS_MAX_RES] = {0}; 737 for (int i = 0; i < m->n_ops; i++) if (state[i] == ST_RUN && rem_b[i] > 0) active[m->ops[i].bw]++; 738 double dt = -1; int any = 0; 739 for (int i = 0; i < m->n_ops; i++) { 740 if (state[i] == ST_LAUNCH) { any = 1; if (dt < 0 || rem_l[i] < dt) dt = rem_l[i]; } 741 else if (state[i] == ST_RUN) { 742 any = 1; 743 /* next event for this op: the earlier of its two progress bars */ 744 double d = -1; 745 if (rem_t[i] > 0) d = rem_t[i]; 746 if (rem_b[i] > 0) { 747 double share = num[i].cap / active[m->ops[i].bw]; 748 double db = rem_b[i] / share; 749 if (d < 0 || db < d) d = db; 750 } 751 if (d < 0) d = 0; 752 if (dt < 0 || d < dt) dt = d; 753 } 754 } 755 if (!any) return refuse(r, DS_REFUSED_UNSUPPORTED, "no op can make progress (slots=0 or all blocked)%s%s", "", ""); 756 if (dt < 0) dt = 0; 757 t += dt; 758 const double eps = 1e-9; 759 for (int i = 0; i < m->n_ops; i++) { 760 if (state[i] == ST_LAUNCH) { 761 rem_l[i] -= dt; 762 if (rem_l[i] <= eps) { rem_l[i] = 0; launcher_busy[m->ops[i].launcher] = 0; state[i] = ST_RUN; run_start[i] = t; } 763 } else if (state[i] == ST_RUN) { 764 if (rem_t[i] > 0) { rem_t[i] -= dt; if (rem_t[i] <= eps) rem_t[i] = 0; } 765 if (rem_b[i] > 0) { 766 double share = num[i].cap / active[m->ops[i].bw]; 767 rem_b[i] -= share * dt; 768 if (rem_b[i] <= eps * (num[i].bytes > 1 ? num[i].bytes : 1)) { rem_b[i] = 0; bytes_end[i] = t; } 769 } 770 if (rem_t[i] <= 0 && rem_b[i] <= 0) { 771 state[i] = ST_DONE; r->end_ns[i] = t; remaining--; 772 if (m->ops[i].engine >= 0) free_slots[m->ops[i].engine]++; 773 for (int d = 0; d < m->n_deps; d++) if (m->deps[d].from == i) done_pred[m->deps[d].to]++; 774 if (num[i].bytes > 0) { 775 double ideal = num[i].bytes / num[i].cap; 776 double actual = (bytes_end[i] >= 0 ? bytes_end[i] : t) - run_start[i]; 777 r->op_contention_ns[i] = actual - ideal > 0 ? actual - ideal : 0; 778 } 779 } 780 } 781 } 782 if (t > r->makespan_ns) r->makespan_ns = t; 783 } 784 785 /* ---- bounds ---- */ 786 double cp[DS_MAX_OPS]; 787 for (int k = 0; k < m->n_ops; k++) { 788 int i = order[k]; 789 double floor_ = num[i].t; 790 if (num[i].bytes > 0 && num[i].bytes / num[i].cap > floor_) floor_ = num[i].bytes / num[i].cap; 791 floor_ += num[i].launch; 792 r->op_floor_ns[i] = floor_; 793 double best = 0; 794 for (int d = 0; d < m->n_deps; d++) if (m->deps[d].to == i && cp[m->deps[d].from] > best) best = cp[m->deps[d].from]; 795 cp[i] = best + floor_; 796 if (cp[i] > r->bound_critical_path_ns) r->bound_critical_path_ns = cp[i]; 797 } 798 r->bound_ns = r->bound_critical_path_ns; 799 for (int i = 0; i < m->n_res; i++) { 800 double w = 0; 801 for (int j = 0; j < m->n_ops; j++) { 802 if (m->res[i].kind == DS_RES_BANDWIDTH && m->ops[j].bw == i) w += num[j].bytes / num[j].cap; 803 if (m->res[i].kind == DS_RES_COMPUTE && m->ops[j].engine == i) w += (num[j].t + num[j].launch) / m->res[i].slots; 804 if (m->res[i].kind == DS_RES_LAUNCH && m->ops[j].launcher == i) w += num[j].launch; 805 } 806 r->bound_resource_ns[i] = w; 807 if (w > r->bound_ns) r->bound_ns = w; 808 } 809 810 /* Inverted lifetimes never enter the ledger (and never rank). */ 811 if ((rc = reject_inverted_lifetimes(m, r)) != DS_OK) return rc; /* LIFETIME_ORDER_CHECK */ 812 813 /* ---- capacity ledger: frees before allocations at the same instant ---- 814 * Positive intervals only. A zero-duration [s, e) is empty and emits no 815 * event, so a same-time free cannot pull the ledger negative or offset 816 * another buffer. Frees of earlier intervals still precede allocs, which 817 * is the completion/allocation tie. */ 818 for (int c = 0; c < m->n_res; c++) { 819 if (m->res[c].kind != DS_RES_CAPACITY) continue; 820 double live = 0, peak = 0, peak_at = 0; 821 /* event list */ 822 double et[2 * DS_MAX_BUF]; double ev[2 * DS_MAX_BUF]; int en = 0; 823 for (int b = 0; b < m->n_buf; b++) { 824 if (m->buf[b].cap != c) continue; 825 double s, e; 826 buffer_window(m, r, b, &s, &e); 827 if (!(s + 1e-9 < e)) continue; 828 et[en] = s; ev[en] = m->buf[b].bytes; en++; 829 et[en] = e; ev[en] = -m->buf[b].bytes; en++; 830 } 831 /* insertion sort by time, frees (negative) first on ties; stable */ 832 for (int i = 1; i < en; i++) { 833 double kt = et[i], kv_ = ev[i]; int j = i - 1; 834 while (j >= 0 && (et[j] > kt || (et[j] == kt && ev[j] > 0 && kv_ < 0))) { et[j + 1] = et[j]; ev[j + 1] = ev[j]; j--; } 835 et[j + 1] = kt; ev[j + 1] = kv_; 836 } 837 for (int i = 0; i < en; i++) { 838 live += ev[i]; 839 /* Reachable by rounding: 1e20 + 1 sums to 1e20, so the later 840 * frees overshoot (dagsim_unit rounding_drives_live_negative). */ 841 if (live < -1e-6) { 842 char msg[256]; 843 snprintf(msg, sizeof msg, 844 "capacity %s live bytes went negative (%.9g at t=%.9g); refusing the certificate", 845 m->res[c].id, live, et[i]); 846 return refuse_msg(r, DS_REFUSED_LIFETIME, msg); 847 } 848 if (live > peak) { peak = live; peak_at = et[i]; } 849 } 850 r->peak_live_bytes[c] = peak; r->peak_live_at_ns[c] = peak_at; 851 if (peak > m->res[c].cap + 1e-9) { 852 char names[160] = ""; size_t L = 0; 853 for (int b = 0; b < m->n_buf && L < sizeof names - 8; b++) { 854 if (m->buf[b].cap != c) continue; 855 double s, e; 856 buffer_window(m, r, b, &s, &e); 857 if (s <= peak_at && e > peak_at) { snprintf(names + L, sizeof names - L, "%s%s", L ? "," : "", m->buf[b].id); L = strlen(names); } 858 } 859 char what[200]; snprintf(what, sizeof what, "%.0f B live at t=%.0f ns (%s) exceeds", peak, peak_at, names); 860 int st = refuse(r, DS_REFUSED_CAPACITY, "%s %s", what, m->res[c].id); 861 return st; 862 } 863 } 864 865 /* ---- attribution along the schedule's critical chain ---- */ 866 int cur = 0; 867 for (int i = 1; i < m->n_ops; i++) if (r->end_ns[i] > r->end_ns[cur]) cur = i; 868 double accounted = 0; 869 int chain[DS_MAX_OPS]; int nc = 0; 870 while (cur >= 0 && nc < DS_MAX_OPS) { 871 chain[nc++] = cur; 872 const ds_op *op = &m->ops[cur]; 873 double runtime = r->end_ns[cur] - (run_start[cur] >= 0 ? run_start[cur] : r->start_ns[cur]); 874 r->attr_ns[DS_ATTR_LAUNCH] += num[cur].launch; 875 accounted += num[cur].launch; 876 if (op->kind == DS_OP_BARRIER) r->attr_ns[DS_ATTR_SYNC] += runtime; 877 else if (num[cur].bytes > 0) { 878 double ideal = num[cur].bytes / num[cur].cap; 879 double actual = ideal + r->op_contention_ns[cur]; 880 if (num[cur].t >= actual) r->attr_ns[DS_ATTR_COMPUTE] += runtime; 881 else { r->attr_ns[DS_ATTR_BANDWIDTH] += ideal; r->attr_ns[DS_ATTR_CONTENTION] += r->op_contention_ns[cur]; if (runtime > actual) r->attr_ns[DS_ATTR_RESIDUAL] += runtime - actual; } 882 } else r->attr_ns[DS_ATTR_COMPUTE] += runtime; 883 accounted += runtime; 884 /* predecessor: the dep that finished last */ 885 int pred = -1; 886 for (int d = 0; d < m->n_deps; d++) if (m->deps[d].to == cur && (pred < 0 || r->end_ns[m->deps[d].from] > r->end_ns[pred])) pred = m->deps[d].from; 887 double gap = r->start_ns[cur] - (pred >= 0 ? r->end_ns[pred] : 0); 888 if (gap > 0) { 889 if (blocked[cur] == BLK_SLOT) r->attr_ns[DS_ATTR_SERIALIZATION] += gap; 890 else if (blocked[cur] == BLK_LAUNCHER) r->attr_ns[DS_ATTR_LAUNCH] += gap; 891 else r->attr_ns[DS_ATTR_RESIDUAL] += gap; 892 accounted += gap; 893 } 894 cur = pred; 895 } 896 for (int i = 0; i < nc; i++) r->critical_path[i] = chain[nc - 1 - i]; 897 r->n_critical = nc; 898 double leftover = r->makespan_ns - accounted; 899 if (fabs(leftover) > 1e-6) r->attr_ns[DS_ATTR_RESIDUAL] += leftover; 900 901 /* ---- energy ---- */ 902 if (m->has_energy && m->joules > 0 && strcmp(m->instrument, "none")) { r->energy_measured = 1; r->energy_j = m->joules; } 903 else if (m->has_energy && m->power_w > 0 && m->energy_prov == DS_PROV_SYNTHETIC) { r->energy_synthetic = 1; r->energy_j = m->power_w * r->makespan_ns * 1e-9; r->prov_mask |= 1u << DS_PROV_SYNTHETIC; } 904 r->synthetic = (r->prov_mask & (1u << DS_PROV_SYNTHETIC)) ? 1 : 0; 905 r->status = DS_OK; 906 return DS_OK; 907 } 908 #endif 909 910 #ifndef CUDAPP_MIGRATION_PENDING_C_TOOLS 911 extern uint32_t omega_dag_eval(uint64_t *result, const void *p0, uint64_t p0_n, void *p1, uint64_t p1_n); 912 913 static double om_f(const uint64_t *s, int i) { 914 double x; 915 memcpy(&x, &s[i], 8); 916 return x; 917 } 918 919 static void om_names(const ds_model *m, const uint64_t *s, char *names, size_t cap) { 920 names[0] = 0; 921 size_t L = 0; 922 for (int b = 0; b < m->n_buf && L < cap - 8; b++) { 923 if (!(s[6336 + b / 64] & (1ull << (b % 64)))) continue; 924 snprintf(names + L, cap - L, "%s%s", L ? "," : "", m->buf[b].id); 925 L = strlen(names); 926 } 927 } 928 929 static int om_refuse(const ds_model *m, const uint64_t *s, ds_result *r) { 930 int why = (int)s[5], a = (int)s[6], b = (int)s[7]; 931 double fa = om_f(s, 8), fb = om_f(s, 9); 932 char msg[256]; 933 if (why == 2) return refuse(r, DS_REFUSED_CYCLE, "dependency cycle%s%s", "", ""); 934 if (why == 3 && a >= 0 && a < m->n_buf && b >= 0 && b < m->n_res) 935 return refuse(r, DS_REFUSED_CAPACITY, "buffer %s alone exceeds capacity %s", m->buf[a].id, m->res[b].id); 936 if (why == 4) return refuse(r, DS_REFUSED_UNSUPPORTED, "no op can make progress (slots=0 or all blocked)%s%s", "", ""); 937 if (why == 5) return refuse(r, DS_REFUSED_UNSUPPORTED, "scheduler did not converge%s%s", "", ""); 938 if (why == 6 && a >= 0 && a < m->n_buf) { 939 snprintf(msg, sizeof msg, "inverted lifetime for buffer %s: first=%s start_ns=%.9g last=%s end_ns=%.9g", 940 m->buf[a].id, endpoint_name(m, m->buf[a].first), fa, endpoint_name(m, m->buf[a].last), fb); 941 return refuse_msg(r, DS_REFUSED_LIFETIME, msg); 942 } 943 if (why == 7 && a >= 0 && a < m->n_res) { 944 snprintf(msg, sizeof msg, "capacity %s live bytes went negative (%.9g at t=%.9g); refusing the certificate", 945 m->res[a].id, fa, fb); 946 return refuse_msg(r, DS_REFUSED_LIFETIME, msg); 947 } 948 if (why == 8 && a >= 0 && a < m->n_res) { 949 char names[160], what[200]; 950 om_names(m, s, names, sizeof names); 951 snprintf(what, sizeof what, "%.0f B live at t=%.0f ns (%s) exceeds", fa, fb, names); 952 return refuse(r, DS_REFUSED_CAPACITY, "%s %s", what, m->res[a].id); 953 } 954 return refuse(r, DS_REFUSED_UNSUPPORTED, "omega refusal %s%s", "", ""); 955 } 956 957 static int om_fill(const ds_model *m, const uint64_t *s, ds_result *r) { 958 if ((int)s[4] != DS_OK) return om_refuse(m, s, r); 959 memset(r, 0, sizeof *r); 960 r->status = DS_OK; 961 r->makespan_ns = om_f(s, 3); 962 r->total_bytes = om_f(s, 15); 963 r->total_busy_ns = om_f(s, 16); 964 r->total_launch_ns = om_f(s, 17); 965 r->bound_ns = om_f(s, 19); 966 r->bound_critical_path_ns = om_f(s, 20); 967 r->n_critical = (int)s[21]; 968 r->prov_mask = (uint32_t)s[11]; 969 r->energy_measured = (int)s[22]; 970 r->energy_synthetic = (int)s[23]; 971 r->energy_j = om_f(s, 24); 972 r->synthetic = (int)s[26]; 973 r->n_ops = m->n_ops; 974 for (int i = 0; i < m->n_ops; i++) { 975 r->start_ns[i] = om_f(s, 4512 + i); 976 r->end_ns[i] = om_f(s, 4768 + i); 977 r->ready_ns[i] = om_f(s, 5024 + i); 978 r->op_contention_ns[i] = om_f(s, 5536 + i); 979 r->op_floor_ns[i] = om_f(s, 5280 + i); 980 if (i < r->n_critical) r->critical_path[i] = (int)s[6080 + i]; 981 } 982 for (int i = 0; i < DS_ATTR_N; i++) r->attr_ns[i] = om_f(s, 5824 + i); 983 for (int i = 0; i < m->n_res; i++) { 984 r->bound_resource_ns[i] = om_f(s, 5792 + i); 985 r->peak_live_bytes[i] = om_f(s, 6344 + i); 986 r->peak_live_at_ns[i] = om_f(s, 6376 + i); 987 } 988 return DS_OK; 989 } 990 991 #endif 992 int ds_eval(const ds_model *m, const ds_opts *o, ds_result *r) { 993 #if defined(CUDAPP_MIGRATION_PENDING_C_TOOLS) 994 return ds_eval_c(m, o, r); 995 #else 996 memset(r, 0, sizeof *r); 997 uint64_t model[8192], scratch[8192], result = 0; 998 int rc = ds_resolved_pack(m, o, model, r); 999 if (rc) return rc; 1000 memset(scratch, 0, sizeof scratch); 1001 if (omega_dag_eval(&result, model, 8192, scratch, 8192)) 1002 return refuse(r, DS_REFUSED_UNSUPPORTED, "omega trap%s%s", "", ""); 1003 return om_fill(m, scratch, r); 1004 #endif 1005 } 1006 1007 int ds_eval_interval(const ds_model *m, const ds_opts *o, ds_result *nominal, double *lo_ns, double *hi_ns) { 1008 ds_opts x = *o; x.mode = 0; 1009 int rc = ds_eval(m, &x, nominal); 1010 *lo_ns = *hi_ns = 0; 1011 if (rc) return rc; 1012 ds_result tmp; 1013 x.mode = +1; if (ds_eval(m, &x, &tmp) == DS_OK) *lo_ns = tmp.makespan_ns; 1014 x.mode = -1; if (ds_eval(m, &x, &tmp) == DS_OK) *hi_ns = tmp.makespan_ns; 1015 return rc; 1016 } 1017 1018 double ds_counterfactual(const ds_model *m, const ds_opts *o, int res_index) { 1019 #if CUDAPP_MIGRATION_PENDING_C_DAGSIM_RESOLVE 1020 if (res_index < 0 || res_index >= m->n_res) return -DS_REFUSED_UNSUPPORTED; 1021 ds_model *c = malloc(sizeof *c); 1022 if (!c) return -DS_REFUSED_UNSUPPORTED; 1023 *c = *m; 1024 ds_resource *r = &c->res[res_index]; 1025 switch (r->kind) { 1026 case DS_RES_BANDWIDTH: r->cap *= 2; r->lo *= 2; r->hi *= 2; break; 1027 case DS_RES_COMPUTE: r->slots *= 2; break; 1028 case DS_RES_LAUNCH: for (int i = 0; i < c->n_ops; i++) if (c->ops[i].launcher == res_index) c->ops[i].launch_ns = 0; break; 1029 case DS_RES_CAPACITY: r->cap *= 2; break; 1030 } 1031 ds_result res; ds_opts x = *o; x.mode = 0; 1032 int rc = ds_eval(c, &x, &res); 1033 free(c); 1034 return rc == DS_OK ? res.makespan_ns : -(double)rc; 1035 #else 1036 return dr_counter(m,o,res_index); 1037 #endif 1038 } 1039 1040 /* ---------------------------------------------------------------- diff */ 1041 1042 #if CUDAPP_MIGRATION_PENDING_C_DAGSIM_RESOLVE 1043 static int in_changed(const char *list, const char *id) { 1044 if (!list) return 0; 1045 const char *p = list; size_t L = strlen(id); 1046 while (*p) { 1047 const char *q = strchr(p, ','); size_t n = q ? (size_t)(q - p) : strlen(p); 1048 if (n == L && !strncmp(p, id, L)) return 1; 1049 if (!q) break; 1050 p = q + 1; 1051 } 1052 return 0; 1053 } 1054 #endif 1055 1056 int ds_diff_models(const ds_model *b, const ds_model *t, const ds_opts *o, ds_diff *d) { 1057 #if CUDAPP_MIGRATION_PENDING_C_DAGSIM_RESOLVE 1058 memset(d, 0, sizeof *d); 1059 for (int i = 0; i < t->n_ops; i++) if (ds_find_op(b, t->ops[i].id) < 0) d->n_added++; 1060 for (int i = 0; i < b->n_ops; i++) { 1061 int j = ds_find_op(t, b->ops[i].id); 1062 if (j >= 0) { 1063 d->n_kept++; 1064 if (b->ops[i].t_ns != t->ops[j].t_ns || b->ops[i].bytes != t->ops[j].bytes) 1065 snprintf(d->reason, sizeof d->reason, "op %s kept but its demand changed; trace conservation broken", b->ops[i].id); 1066 continue; 1067 } 1068 d->n_removed++; 1069 /* find its removed record(s) */ 1070 int found = 0; const ds_removed *rec = NULL; 1071 for (int k = 0; k < t->n_removed; k++) if (!strcmp(t->removed[k].op, b->ops[i].id)) { found++; rec = &t->removed[k]; } 1072 if (found == 0) { d->required_removed = 1; snprintf(d->reason, sizeof d->reason, "op %s vanished without a removed record", b->ops[i].id); return DS_REFUSED_REMOVED_REQUIRED; } 1073 if (found > 1) { d->double_counted = 1; snprintf(d->reason, sizeof d->reason, "op %s removed %d times (double counted)", b->ops[i].id, found); return DS_REFUSED_REMOVED_REQUIRED; } 1074 /* consumers of the removed op in the baseline that still exist */ 1075 int has_consumer = 0; 1076 for (int k = 0; k < b->n_deps; k++) if (b->deps[k].from == i && ds_find_op(t, b->ops[b->deps[k].to].id) >= 0) has_consumer = 1; 1077 int ok = 1; 1078 switch (rec->reason) { 1079 case DS_RM_ELIMINATED: if (has_consumer && !rec->proof[0]) ok = 0; break; 1080 case DS_RM_REUSED: case DS_RM_LOOKUP: case DS_RM_MOVED: if (!rec->ref[0] || ds_find_op(t, rec->ref) < 0) ok = 0; break; 1081 case DS_RM_UNCHANGED_DEP: if (!rec->ref[0] || ds_find_op(b, rec->ref) < 0 || in_changed(o ? o->changed : NULL, rec->ref)) ok = 0; break; 1082 case DS_RM_DEFERRED: if (has_consumer) ok = 0; break; 1083 case DS_RM_SHARED: 1084 if (!rec->ref[0] || ds_find_op(t, rec->ref) < 0) ok = 0; 1085 for (int k = 0; k < t->n_removed; k++) if (!strcmp(t->removed[k].op, rec->ref)) { d->double_counted = 1; ok = 0; } 1086 break; 1087 } 1088 if (!ok) { 1089 d->required_removed = 1; 1090 snprintf(d->reason, sizeof d->reason, "op %s: removal reason %d not supported by its evidence (ref=%s proof=%s changed=%s)", 1091 b->ops[i].id, rec->reason, rec->ref, rec->proof, o && o->changed ? o->changed : ""); 1092 return DS_REFUSED_REMOVED_REQUIRED; 1093 } 1094 double w = b->ops[i].t_ns + b->ops[i].launch_ns; 1095 if (b->ops[i].bytes > 0 && b->ops[i].bw >= 0) { double tb = b->ops[i].bytes / (b->res[b->ops[i].bw].cap * 1e-9); if (tb > b->ops[i].t_ns) w = tb + b->ops[i].launch_ns; } 1096 d->removed_ns[rec->reason] += w; 1097 } 1098 /* removed records that name ops never in the baseline are fabricated savings */ 1099 for (int k = 0; k < t->n_removed; k++) if (ds_find_op(b, t->removed[k].op) < 0) { 1100 d->double_counted = 1; 1101 snprintf(d->reason, sizeof d->reason, "removed record for %s, which was never in the baseline", t->removed[k].op); 1102 return DS_REFUSED_REMOVED_REQUIRED; 1103 } 1104 return DS_OK; 1105 #else 1106 return dr_diff(b,t,o,d); 1107 #endif 1108 }