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
CUDApp / evidence / 2026-11-06-costmodel-rest / rx_costmodel.c.gcov
| 1 | -: 0:Source:src/runtime/rx_costmodel.c |
| 2 | -: 0:Graph:/tmp/cm-full-cov-v5qvhlky/oracle.gcno |
| 3 | -: 0:Data:/tmp/cm-full-cov-v5qvhlky/oracle.gcda |
| 4 | -: 0:Runs:2 |
| 5 | -: 1:/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | -: 2: * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 7 | -: 3: * |
| 8 | -: 4: * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | -: 5: * you may not use this file except in compliance with the License. |
| 10 | -: 6: * You may obtain a copy of the License at |
| 11 | -: 7: * |
| 12 | -: 8: * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | -: 9: * |
| 14 | -: 10: * Unless required by applicable law or agreed to in writing, software |
| 15 | -: 11: * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | -: 12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | -: 13: * See the License for the specific language governing permissions and |
| 18 | -: 14: * limitations under the License. |
| 19 | -: 15: */ |
| 20 | -: 16:#include "runtime/rx_cm_sort.h" |
| 21 | -: 17: |
| 22 | -: 18:/* |
| 23 | -: 19: * rx_costmodel.c -- Omega's empirical cost model. See rx_costmodel.h. |
| 24 | -: 20: * |
| 25 | -: 21: * Pure arithmetic over measurements the caller reports. No execution, no |
| 26 | -: 22: * authority, no generation store. |
| 27 | -: 23: */ |
| 28 | -: 24:#include "runtime/rx_costmodel.h" |
| 29 | -: 25:#include "sha256.h" |
| 30 | -: 26: |
| 31 | -: 27:#include <math.h> |
| 32 | -: 28:#include <stdlib.h> |
| 33 | -: 29:#include <string.h> |
| 34 | -: 30:#include "runtime/rx_cm_math.h" |
| 35 | -: 31: |
| 36 | -: 32:_Static_assert(RX_CM_CELL_NONE >= RX_CM_CELLS, |
| 37 | -: 33: "unsupported-op sentinel must not be a cell index"); |
| 38 | -: 34: |
| 39 | -: 35:#define PRIOR_A 1.0 |
| 40 | -: 36:#define PRIOR_B 0.01 /* prior noise variance ~0.01 log2^2 (about 7%) */ |
| 41 | -: 37:#define NOISE_FLOOR 0.0009 /* never claim better than ~2% sd per call */ |
| 42 | -: 38:#define FAIL_PRIOR 0.05 |
| 43 | -: 39:#ifndef CUDAPP_MIGRATION_PENDING_C_CM_FIT |
| 44 | -: 40:#define CUDAPP_MIGRATION_PENDING_C_CM_FIT 0 |
| 45 | -: 41:#endif |
| 46 | -: 42:#ifndef CUDAPP_MIGRATION_PENDING_C_CM_STATS |
| 47 | -: 43:#define CUDAPP_MIGRATION_PENDING_C_CM_STATS 0 |
| 48 | -: 44:#endif |
| 49 | -: 45:#ifndef CUDAPP_MIGRATION_PENDING_C_CM_FEATURES |
| 50 | -: 46:#define CUDAPP_MIGRATION_PENDING_C_CM_FEATURES 0 |
| 51 | -: 47:#endif |
| 52 | -: 48:#ifndef CUDAPP_MIGRATION_PENDING_C_CM_QUANTILE |
| 53 | -: 49:#define CUDAPP_MIGRATION_PENDING_C_CM_QUANTILE 0 |
| 54 | -: 50:#endif |
| 55 | -: 51:#ifndef CUDAPP_MIGRATION_PENDING_C_CM_DECISION |
| 56 | -: 52:#define CUDAPP_MIGRATION_PENDING_C_CM_DECISION 0 |
| 57 | -: 53:#endif |
| 58 | -: 54:#ifndef CUDAPP_MIGRATION_PENDING_C_CM_PREDICTION |
| 59 | -: 55:#define CUDAPP_MIGRATION_PENDING_C_CM_PREDICTION 0 |
| 60 | -: 56:#endif |
| 61 | -: 57:#ifndef CUDAPP_MIGRATION_PENDING_C_CM_CALIBRATION |
| 62 | -: 58:#define CUDAPP_MIGRATION_PENDING_C_CM_CALIBRATION 0 |
| 63 | -: 59:#endif |
| 64 | -: 60:#ifndef CUDAPP_MIGRATION_PENDING_C_CM_BLOB |
| 65 | -: 61:#define CUDAPP_MIGRATION_PENDING_C_CM_BLOB 0 |
| 66 | -: 62:#endif |
| 67 | -: 63:#ifndef CUDAPP_MIGRATION_PENDING_C_CM_DIGEST |
| 68 | -: 64:#define CUDAPP_MIGRATION_PENDING_C_CM_DIGEST 0 |
| 69 | -: 65:#endif |
| 70 | -: 66:#ifndef CUDAPP_MIGRATION_PENDING_C_CM_ROUTE |
| 71 | -: 67:#define CUDAPP_MIGRATION_PENDING_C_CM_ROUTE 0 |
| 72 | -: 68:#endif |
| 73 | -: 69: |
| 74 | -: 70:/* Host pointer validation stays here; scalar admission and wire offsets do not. */ |
| 75 | function cm_route2 called 299452 returned 100% blocks executed 100% |
| 76 | 299452: 71:static uint64_t cm_route2(unsigned mode, uint64_t a, uint64_t b) { |
| 77 | -: 72:#if CUDAPP_MIGRATION_PENDING_C_CM_ROUTE |
| 78 | 299452: 73: if (mode == 2) return a < b; |
| 79 | branch 0 taken 299355 (fallthrough) |
| 80 | branch 1 taken 97 |
| 81 | 97: 74: if (mode == 3) return a + b; |
| 82 | branch 0 taken 54 (fallthrough) |
| 83 | branch 1 taken 43 |
| 84 | 43: 75: return a - b; |
| 85 | -: 76:#else |
| 86 | -: 77: uint64_t data[16] = {a, b}, result; |
| 87 | -: 78: if (omega_cm_route(&result, mode, data, 16) != OMEGA_OK) abort(); |
| 88 | -: 79: return result; |
| 89 | -: 80:#endif |
| 90 | -: 81:} |
| 91 | -: 82: |
| 92 | -: 83: |
| 93 | -: 84:#if CUDAPP_MIGRATION_PENDING_C_CM_FEATURES |
| 94 | function oracle_cm_default_policy called 0 returned 0% blocks executed 0% |
| 95 | #####: 85:void rx_cm_default_policy(RxCmPolicy *p) { |
| 96 | #####: 86: p->ei_min = 0.08; |
| 97 | #####: 87: p->max_fail_p = 0.0; /* any wrong result excludes the arm */ |
| 98 | #####: 88: p->fallback_arm = 0; |
| 99 | #####: 89: p->explore_frac = 0.05; |
| 100 | #####: 90: p->explore_allow_ps = 500000000ull; /* 0.5 ms to get started */ |
| 101 | #####: 91:} |
| 102 | -: 92: |
| 103 | function oracle_cm_init called 32 returned 100% blocks executed 100% |
| 104 | 32: 93:void rx_cm_init(RxCostModel *m, uint32_t n_arms) { |
| 105 | 32: 94: memset(m, 0, sizeof *m); |
| 106 | 32: 95: m->n_arms = n_arms > RX_CM_ARMS ? RX_CM_ARMS : n_arms; |
| 107 | 32: 96: m->lambda = 1e-3; |
| 108 | 32: 97: m->sd_scale = 1.0; |
| 109 | 320: 98: for (uint32_t c = 0; c < RX_CM_CELLS; c++) |
| 110 | branch 0 taken 288 |
| 111 | branch 1 taken 32 (fallthrough) |
| 112 | 2592: 99: for (uint32_t a = 0; a < RX_CM_ARMS; a++) |
| 113 | branch 0 taken 2304 |
| 114 | branch 1 taken 288 (fallthrough) |
| 115 | 20736: 100: for (uint32_t i = 0, k = 0; i < RX_CM_D; i++) |
| 116 | branch 0 taken 18432 |
| 117 | branch 1 taken 2304 (fallthrough) |
| 118 | 101376: 101: for (uint32_t j = i; j < RX_CM_D; j++, k++) |
| 119 | branch 0 taken 82944 |
| 120 | branch 1 taken 18432 (fallthrough) |
| 121 | 82944: 102: m->cell[c][a].tri[k] = i == j ? m->lambda : 0.0; |
| 122 | branch 0 taken 18432 (fallthrough) |
| 123 | branch 1 taken 64512 |
| 124 | 32: 103:} |
| 125 | -: 104: |
| 126 | function oracle_cm_cell called 219324 returned 100% blocks executed 100% |
| 127 | 219324: 105:uint32_t rx_cm_cell(const RxCmFeatures *f) { |
| 128 | -: 106: /* An unknown semantic operation is not the last supported family. |
| 129 | -: 107: * The restore-op-clamp mutant rewrites the next line. */ |
| 130 | 219324: 108: uint32_t op = f && f->op < RX_CM_OPS ? f->op : RX_CM_CELL_NONE; /* CM704:refuse-unsupported-op */ |
| 131 | branch 0 taken 219324 (fallthrough) |
| 132 | branch 1 taken 0 |
| 133 | branch 2 taken 219234 (fallthrough) |
| 134 | branch 3 taken 90 |
| 135 | 219324: 109: if (op >= RX_CM_OPS) return RX_CM_CELL_NONE; |
| 136 | branch 0 taken 90 (fallthrough) |
| 137 | branch 1 taken 219234 |
| 138 | 219234: 110: uint32_t core = f->core < RX_CM_CORES ? f->core : RX_CM_CORE_OTHER; |
| 139 | 219234: 111: uint32_t pr = f->pressure < RX_CM_PRESSURE ? f->pressure : RX_CM_PRESSURE - 1u; |
| 140 | 219234: 112: return (op * RX_CM_CORES + core) * RX_CM_PRESSURE + pr; |
| 141 | -: 113:} |
| 142 | -: 114: |
| 143 | function oracle_cm_basis called 119929 returned 100% blocks executed 89% |
| 144 | 119929: 115:void rx_cm_basis(const RxCmFeatures *f, double phi[RX_CM_D]) { |
| 145 | 119929*: 116: double lm = log2((double)(f->M ? f->M : 1)); |
| 146 | branch 0 taken 119929 (fallthrough) |
| 147 | branch 1 taken 0 |
| 148 | call 2 returned 119929 |
| 149 | 119929*: 117: double ln = log2((double)(f->N ? f->N : 1)); |
| 150 | branch 0 taken 119929 (fallthrough) |
| 151 | branch 1 taken 0 |
| 152 | call 2 returned 119929 |
| 153 | 119929: 118: double ov = 0.0; |
| 154 | 119929: 119: if (f->cache_bytes && f->state_bytes > f->cache_bytes) |
| 155 | branch 0 taken 118145 (fallthrough) |
| 156 | branch 1 taken 1784 |
| 157 | branch 2 taken 88776 (fallthrough) |
| 158 | branch 3 taken 29369 |
| 159 | 88776: 120: ov = log2((double)f->state_bytes / (double)f->cache_bytes); |
| 160 | call 0 returned 88776 |
| 161 | 119929: 121: phi[0] = 1.0; |
| 162 | 119929: 122: phi[1] = lm; |
| 163 | 119929: 123: phi[2] = ln; |
| 164 | 119929: 124: phi[3] = lm * ln; |
| 165 | 119929: 125: phi[4] = ln * ln; |
| 166 | 119929: 126: phi[5] = f->N <= 3 ? 1.0 : 0.0; |
| 167 | branch 0 taken 812 (fallthrough) |
| 168 | branch 1 taken 119117 |
| 169 | 119929: 127: phi[6] = (f->N % 4u) != 0 ? 1.0 : 0.0; |
| 170 | branch 0 taken 89323 (fallthrough) |
| 171 | branch 1 taken 30606 |
| 172 | 119929: 128: phi[7] = ov; |
| 173 | 119929: 129:} |
| 174 | -: 130:#else |
| 175 | -: 131:void rx_cm_default_policy(RxCmPolicy *p) { |
| 176 | -: 132: uint64_t words[64], result; |
| 177 | -: 133: if (omega_cm_features(&result, 4, words, 64) != OMEGA_OK) { |
| 178 | -: 134: memset(p, 0, sizeof *p); |
| 179 | -: 135: return; |
| 180 | -: 136: } |
| 181 | -: 137: memcpy(&p->ei_min, words + 16, sizeof(double)); |
| 182 | -: 138: memcpy(&p->max_fail_p, words + 17, sizeof(double)); |
| 183 | -: 139: p->fallback_arm = (uint32_t)words[18]; |
| 184 | -: 140: memcpy(&p->explore_frac, words + 19, sizeof(double)); |
| 185 | -: 141: p->explore_allow_ps = words[20]; |
| 186 | -: 142:} |
| 187 | -: 143: |
| 188 | -: 144:void rx_cm_init(RxCostModel *m, uint32_t n_arms) { |
| 189 | -: 145: uint64_t words[64], result; |
| 190 | -: 146: memset(m, 0, sizeof *m); |
| 191 | -: 147: words[0] = n_arms; |
| 192 | -: 148: if (omega_cm_features(&result, 3, words, 64) != OMEGA_OK) return; |
| 193 | -: 149: m->n_arms = (uint32_t)result; |
| 194 | -: 150: memcpy(&m->lambda, words + 16, sizeof(double)); |
| 195 | -: 151: memcpy(&m->sd_scale, words, sizeof(double)); |
| 196 | -: 152: for (uint32_t c = 0; c < RX_CM_CELLS; ++c) |
| 197 | -: 153: for (uint32_t a = 0; a < RX_CM_ARMS; ++a) |
| 198 | -: 154: memcpy(&m->cell[c][a], words + 16, sizeof(RxCmArmStats)); |
| 199 | -: 155:} |
| 200 | -: 156: |
| 201 | -: 157:uint32_t rx_cm_cell(const RxCmFeatures *f) { |
| 202 | -: 158: if (!f) return RX_CM_CELL_NONE; |
| 203 | -: 159: uint64_t words[64], result; |
| 204 | -: 160: words[0] = f->op; words[5] = f->core; words[6] = f->pressure; |
| 205 | -: 161: if (omega_cm_features(&result, 0, words, 64) != OMEGA_OK) return RX_CM_CELL_NONE; |
| 206 | -: 162: return (uint32_t)result; |
| 207 | -: 163:} |
| 208 | -: 164: |
| 209 | -: 165:static void cm_host_basis(const RxCmFeatures *f, double phi[RX_CM_D], uint64_t mode) { |
| 210 | -: 166: uint64_t words[64], result; |
| 211 | -: 167: words[1] = f->M; words[2] = f->N; |
| 212 | -: 168: words[3] = f->state_bytes; words[4] = f->cache_bytes; |
| 213 | -: 169: if (omega_cm_features(&result, mode, words, 64) != OMEGA_OK) { |
| 214 | -: 170: for (uint32_t i = 0; i < RX_CM_D; ++i) phi[i] = NAN; |
| 215 | -: 171: return; |
| 216 | -: 172: } |
| 217 | -: 173: memcpy(phi, words + 16, RX_CM_D * sizeof(double)); |
| 218 | -: 174:} |
| 219 | -: 175: |
| 220 | -: 176:void rx_cm_basis(const RxCmFeatures *f, double phi[RX_CM_D]) { cm_host_basis(f, phi, 1); } |
| 221 | -: 177:#endif |
| 222 | -: 178: |
| 223 | -: 179:#if CUDAPP_MIGRATION_PENDING_C_CM_FIT |
| 224 | -: 180:/* Cholesky of the D x D matrix held as an upper triangle. 0 on success. */ |
| 225 | function cholesky called 37360 returned 100% blocks executed 96% |
| 226 | 37360: 181:static int cholesky(const double *tri, double L[RX_CM_D][RX_CM_D]) { |
| 227 | -: 182: double A[RX_CM_D][RX_CM_D]; |
| 228 | 336240: 183: for (uint32_t i = 0, k = 0; i < RX_CM_D; i++) |
| 229 | branch 0 taken 298880 |
| 230 | branch 1 taken 37360 (fallthrough) |
| 231 | 1643840: 184: for (uint32_t j = i; j < RX_CM_D; j++, k++) A[i][j] = A[j][i] = tri[k]; |
| 232 | branch 0 taken 1344960 |
| 233 | branch 1 taken 298880 (fallthrough) |
| 234 | 37360: 185: memset(L, 0, sizeof(double) * RX_CM_D * RX_CM_D); |
| 235 | 336240: 186: for (uint32_t j = 0; j < RX_CM_D; j++) { |
| 236 | branch 0 taken 298880 |
| 237 | branch 1 taken 37360 (fallthrough) |
| 238 | 298880: 187: double s = A[j][j]; |
| 239 | 1344960: 188: for (uint32_t k = 0; k < j; k++) s -= L[j][k] * L[j][k]; |
| 240 | branch 0 taken 1046080 |
| 241 | branch 1 taken 298880 (fallthrough) |
| 242 | 298880*: 189: if (!(s > 0.0)) return -1; |
| 243 | branch 0 taken 0 (fallthrough) |
| 244 | branch 1 taken 298880 |
| 245 | 298880: 190: L[j][j] = sqrt(s); |
| 246 | call 0 returned 298880 |
| 247 | 1344960: 191: for (uint32_t i = j + 1; i < RX_CM_D; i++) { |
| 248 | branch 0 taken 1046080 |
| 249 | branch 1 taken 298880 (fallthrough) |
| 250 | 1046080: 192: double t = A[i][j]; |
| 251 | 3138240: 193: for (uint32_t k = 0; k < j; k++) t -= L[i][k] * L[j][k]; |
| 252 | branch 0 taken 2092160 |
| 253 | branch 1 taken 1046080 (fallthrough) |
| 254 | 1046080: 194: L[i][j] = t / L[j][j]; |
| 255 | -: 195: } |
| 256 | -: 196: } |
| 257 | 37360: 197: return 0; |
| 258 | -: 198:} |
| 259 | -: 199: |
| 260 | function forward called 74720 returned 100% blocks executed 100% |
| 261 | 74720: 200:static void forward(double L[RX_CM_D][RX_CM_D], const double *b, double *v) { |
| 262 | 672480: 201: for (uint32_t i = 0; i < RX_CM_D; i++) { |
| 263 | branch 0 taken 597760 |
| 264 | branch 1 taken 74720 (fallthrough) |
| 265 | 597760: 202: double s = b[i]; |
| 266 | 2689920: 203: for (uint32_t k = 0; k < i; k++) s -= L[i][k] * v[k]; |
| 267 | branch 0 taken 2092160 |
| 268 | branch 1 taken 597760 (fallthrough) |
| 269 | 597760: 204: v[i] = s / L[i][i]; |
| 270 | -: 205: } |
| 271 | 74720: 206:} |
| 272 | -: 207: |
| 273 | function backward called 37360 returned 100% blocks executed 100% |
| 274 | 37360: 208:static void backward(double L[RX_CM_D][RX_CM_D], const double *v, double *x) { |
| 275 | 336240: 209: for (int i = (int)RX_CM_D - 1; i >= 0; i--) { |
| 276 | branch 0 taken 298880 |
| 277 | branch 1 taken 37360 (fallthrough) |
| 278 | 298880: 210: double s = v[i]; |
| 279 | 1344960: 211: for (uint32_t k = (uint32_t)i + 1; k < RX_CM_D; k++) s -= L[k][i] * x[k]; |
| 280 | branch 0 taken 1046080 |
| 281 | branch 1 taken 298880 (fallthrough) |
| 282 | 298880: 212: x[i] = s / L[i][i]; |
| 283 | -: 213: } |
| 284 | 37360: 214:} |
| 285 | -: 215: |
| 286 | -: 216:/* Predictive t for one (cell, arm). Returns 0 if there is no data. */ |
| 287 | function fit_predict called 116424 returned 100% blocks executed 92% |
| 288 | 116424: 217:static int fit_predict(const RxCmArmStats *s, const double phi[RX_CM_D], double *mean, |
| 289 | -: 218: double *sd, double *dof, uint64_t minimum) { |
| 290 | 116424: 219: if (s->n == 0 || s->n < minimum) return 0; |
| 291 | branch 0 taken 37360 (fallthrough) |
| 292 | branch 1 taken 79064 |
| 293 | branch 2 taken 0 (fallthrough) |
| 294 | branch 3 taken 37360 |
| 295 | -: 220: double L[RX_CM_D][RX_CM_D], v[RX_CM_D], mu[RX_CM_D], w[RX_CM_D]; |
| 296 | 37360*: 221: if (cholesky(s->tri, L) != 0) return 0; |
| 297 | call 0 returned 37360 |
| 298 | branch 1 taken 0 (fallthrough) |
| 299 | branch 2 taken 37360 |
| 300 | 37360: 222: forward(L, s->xty, v); |
| 301 | call 0 returned 37360 |
| 302 | 37360: 223: backward(L, v, mu); |
| 303 | call 0 returned 37360 |
| 304 | 37360: 224: double quad = 0.0; /* mu' Lambda mu = |L' mu|^2 = v . v */ |
| 305 | 336240: 225: for (uint32_t i = 0; i < RX_CM_D; i++) quad += v[i] * v[i]; |
| 306 | branch 0 taken 298880 |
| 307 | branch 1 taken 37360 (fallthrough) |
| 308 | 37360: 226: double a = PRIOR_A + 0.5 * (double)s->n; |
| 309 | 37360: 227: double b = PRIOR_B + 0.5 * (s->yty - quad); |
| 310 | 37360*: 228: if (b < PRIOR_B) b = PRIOR_B; |
| 311 | branch 0 taken 0 (fallthrough) |
| 312 | branch 1 taken 37360 |
| 313 | 37360: 229: double sigma2 = b / a; |
| 314 | 37360: 230: if (sigma2 < NOISE_FLOOR) sigma2 = NOISE_FLOOR; |
| 315 | branch 0 taken 17 (fallthrough) |
| 316 | branch 1 taken 37343 |
| 317 | 37360: 231: forward(L, phi, w); |
| 318 | call 0 returned 37360 |
| 319 | 37360: 232: double lev = 0.0; |
| 320 | 336240: 233: for (uint32_t i = 0; i < RX_CM_D; i++) lev += w[i] * w[i]; |
| 321 | branch 0 taken 298880 |
| 322 | branch 1 taken 37360 (fallthrough) |
| 323 | 37360: 234: double m = 0.0; |
| 324 | 336240: 235: for (uint32_t i = 0; i < RX_CM_D; i++) m += mu[i] * phi[i]; |
| 325 | branch 0 taken 298880 |
| 326 | branch 1 taken 37360 (fallthrough) |
| 327 | 37360: 236: *mean = m; |
| 328 | 37360: 237: *sd = sqrt(sigma2 * (1.0 + lev)); |
| 329 | call 0 returned 37360 |
| 330 | 37360: 238: *dof = 2.0 * a; |
| 331 | 37360: 239: return 1; |
| 332 | -: 240:} |
| 333 | -: 241:#else |
| 334 | -: 242:static int fit_predict(const RxCmArmStats *s, const double phi[RX_CM_D], double *mean, |
| 335 | -: 243: double *sd, double *dof, uint64_t minimum) { |
| 336 | -: 244: _Static_assert(sizeof(RxCmArmStats) == 48 * sizeof(uint64_t), "statistics transport layout"); |
| 337 | -: 245: uint64_t input[64], output[16], found; |
| 338 | -: 246: memcpy(input, s, 46 * sizeof(uint64_t)); |
| 339 | -: 247: memcpy(input + 48, phi, RX_CM_D * sizeof(double)); |
| 340 | -: 248: input[56] = minimum; |
| 341 | -: 249: if (omega_cm_fit(&found, input, 64, output, 16) != OMEGA_OK || found != 1) return 0; |
| 342 | -: 250: memcpy(mean, output, sizeof *mean); |
| 343 | -: 251: memcpy(sd, output + 1, sizeof *sd); |
| 344 | -: 252: memcpy(dof, output + 2, sizeof *dof); |
| 345 | -: 253: return 1; |
| 346 | -: 254:} |
| 347 | -: 255:#endif |
| 348 | -: 256: |
| 349 | -: 257:#if CUDAPP_MIGRATION_PENDING_C_CM_FEATURES |
| 350 | function failures_of called 43427 returned 100% blocks executed 100% |
| 351 | 43427: 258:static uint64_t failures_of(const RxCostModel *m, uint32_t arm) { |
| 352 | 43427: 259: uint64_t f = 0; |
| 353 | 434270: 260: for (uint32_t c = 0; c < RX_CM_CELLS; c++) f += m->cell[c][arm].failures; |
| 354 | branch 0 taken 390843 |
| 355 | branch 1 taken 43427 (fallthrough) |
| 356 | 43427: 261: return f; |
| 357 | -: 262:} |
| 358 | -: 263: |
| 359 | function fail_p called 96987 returned 100% blocks executed 100% |
| 360 | 96987: 264:static double fail_p(const RxCostModel *m, uint32_t arm) { |
| 361 | 96987: 265: uint64_t t = 0, f = 0; |
| 362 | 969870: 266: for (uint32_t c = 0; c < RX_CM_CELLS; c++) { |
| 363 | branch 0 taken 872883 |
| 364 | branch 1 taken 96987 (fallthrough) |
| 365 | 872883: 267: t += m->cell[c][arm].trials; |
| 366 | 872883: 268: f += m->cell[c][arm].failures; |
| 367 | -: 269: } |
| 368 | 96987: 270: return ((double)f + FAIL_PRIOR) / ((double)t + 1.0); |
| 369 | -: 271:} |
| 370 | -: 272:#else |
| 371 | -: 273:static uint64_t failures_of(const RxCostModel *m, uint32_t arm) { |
| 372 | -: 274: uint64_t words[64], result; |
| 373 | -: 275: for (uint32_t c = 0; c < RX_CM_CELLS; ++c) words[c] = m->cell[c][arm].failures; |
| 374 | -: 276: if (omega_cm_features(&result, 6, words, 64) != OMEGA_OK) return UINT64_MAX; |
| 375 | -: 277: return result; |
| 376 | -: 278:} |
| 377 | -: 279: |
| 378 | -: 280:static double fail_p(const RxCostModel *m, uint32_t arm) { |
| 379 | -: 281: uint64_t words[64], result; |
| 380 | -: 282: double value; |
| 381 | -: 283: for (uint32_t c = 0; c < RX_CM_CELLS; ++c) { |
| 382 | -: 284: words[c] = m->cell[c][arm].failures; words[9 + c] = m->cell[c][arm].trials; |
| 383 | -: 285: } |
| 384 | -: 286: if (omega_cm_features(&result, 7, words, 64) != OMEGA_OK) return 1.0; |
| 385 | -: 287: memcpy(&value, &result, sizeof value); |
| 386 | -: 288: return value; |
| 387 | -: 289:} |
| 388 | -: 290:#endif |
| 389 | -: 291: |
| 390 | -: 292:/* Pressure cells hold a correction over the same core class at pressure 0: |
| 391 | -: 293: * log2 cost under pressure = quiet prediction + a + b * (doublings past the |
| 392 | -: 294: * private cache). Two numbers per arm, readable as "pressure costs this much, |
| 393 | -: 295: * and this much more per doubling out of cache". */ |
| 394 | function residual_basis called 19270 returned 100% blocks executed 100% |
| 395 | 19270: 296:static void residual_basis(const RxCmFeatures *f, double phi[RX_CM_D]) { |
| 396 | -: 297:#if !CUDAPP_MIGRATION_PENDING_C_CM_FEATURES |
| 397 | -: 298: cm_host_basis(f, phi, 2); |
| 398 | -: 299:#else |
| 399 | -: 300: double full[RX_CM_D]; |
| 400 | 19270: 301: rx_cm_basis(f, full); |
| 401 | call 0 returned 19270 |
| 402 | 19270: 302: memset(phi, 0, sizeof(double) * RX_CM_D); |
| 403 | 19270: 303: phi[0] = 1.0; |
| 404 | 19270: 304: phi[1] = full[7]; |
| 405 | -: 305:#endif |
| 406 | 19270: 306:} |
| 407 | -: 307: |
| 408 | function quiet_predict called 97178 returned 100% blocks executed 91% |
| 409 | 97178: 308:static int quiet_predict(const RxCostModel *m, const RxCmFeatures *f, uint32_t arm, double *mean, |
| 410 | -: 309: double *sd, double *dof, uint64_t *n) { |
| 411 | 97178: 310: RxCmFeatures g = *f; |
| 412 | 97178: 311: g.pressure = 0; |
| 413 | 97178: 312: uint32_t cell = rx_cm_cell(&g); |
| 414 | call 0 returned 97178 |
| 415 | 97178*: 313: if (!cm_route2(2, cell, RX_CM_CELLS)) return 0; |
| 416 | call 0 returned 97178 |
| 417 | branch 1 taken 0 (fallthrough) |
| 418 | branch 2 taken 97178 |
| 419 | 97178: 314: const RxCmArmStats *b = &m->cell[cell][arm]; |
| 420 | -: 315: double phi[RX_CM_D]; |
| 421 | 97178: 316: rx_cm_basis(f, phi); |
| 422 | call 0 returned 97178 |
| 423 | 97178: 317: if (!fit_predict(b, phi, mean, sd, dof, RX_CM_MIN_OBS)) return 0; |
| 424 | call 0 returned 97178 |
| 425 | branch 1 taken 59823 (fallthrough) |
| 426 | branch 2 taken 37355 |
| 427 | 37355: 318: *n = b->n; |
| 428 | 37355: 319: return 1; |
| 429 | -: 320:} |
| 430 | -: 321: |
| 431 | function bucket called 41003 returned 100% blocks executed 100% |
| 432 | 41003: 322:static uint32_t bucket(const RxCmFeatures *f) { |
| 433 | -: 323:#if !CUDAPP_MIGRATION_PENDING_C_CM_FEATURES |
| 434 | -: 324: uint64_t words[64], result; |
| 435 | -: 325: words[6] = f->pressure; |
| 436 | -: 326: if (omega_cm_features(&result, 5, words, 64) != OMEGA_OK) return RX_CM_PRESSURE - 1; |
| 437 | -: 327: return (uint32_t)result; |
| 438 | -: 328:#else |
| 439 | 41003: 329: return f->pressure < RX_CM_PRESSURE ? f->pressure : RX_CM_PRESSURE - 1; |
| 440 | -: 330:#endif |
| 441 | -: 331:} |
| 442 | -: 332: |
| 443 | function oracle_cm_predict called 97009 returned 100% blocks executed 97% |
| 444 | 97009: 333:int rx_cm_predict(const RxCostModel *m, const RxCmFeatures *f, uint32_t arm, |
| 445 | -: 334: RxCmPrediction *out) { |
| 446 | 97009: 335: if (!out) return RX_CM_ERR_ARG; |
| 447 | branch 0 taken 1 (fallthrough) |
| 448 | branch 1 taken 97008 |
| 449 | 97008: 336: memset(out, 0, sizeof *out); |
| 450 | 97008: 337: if (!m || !f) return RX_CM_ERR_ARG; |
| 451 | branch 0 taken 97007 (fallthrough) |
| 452 | branch 1 taken 1 |
| 453 | branch 2 taken 1 (fallthrough) |
| 454 | branch 3 taken 97006 |
| 455 | 97006: 338: if (!cm_route2(2, arm, m->n_arms)) return RX_CM_ERR_ARG; |
| 456 | call 0 returned 97006 |
| 457 | branch 1 taken 1 (fallthrough) |
| 458 | branch 2 taken 97005 |
| 459 | 97005: 339: uint32_t cell = rx_cm_cell(f); |
| 460 | call 0 returned 97005 |
| 461 | 97005: 340: if (!cm_route2(2, cell, RX_CM_CELLS)) { |
| 462 | call 0 returned 97005 |
| 463 | branch 1 taken 18 (fallthrough) |
| 464 | branch 2 taken 96987 |
| 465 | 18: 341: out->refused = 1; |
| 466 | 18: 342: return RX_CM_ERR_UNSUPPORTED; |
| 467 | -: 343: } |
| 468 | 96987: 344: double mean = 0, sd = 0, dof = 0; |
| 469 | 96987: 345: uint64_t n = 0; |
| 470 | 96987: 346: out->fail_p = fail_p(m, arm); |
| 471 | call 0 returned 96987 |
| 472 | 96987: 347: if (!quiet_predict(m, f, arm, &mean, &sd, &dof, &n)) return RX_CM_OK; |
| 473 | call 0 returned 96987 |
| 474 | branch 1 taken 59656 (fallthrough) |
| 475 | branch 2 taken 37331 |
| 476 | -: 348:#if CUDAPP_MIGRATION_PENDING_C_CM_PREDICTION |
| 477 | 37331: 349: if (bucket(f) == 0) { |
| 478 | call 0 returned 37331 |
| 479 | branch 1 taken 18085 (fallthrough) |
| 480 | branch 2 taken 19246 |
| 481 | 18085: 350: out->known = 1; |
| 482 | -: 351: } else { |
| 483 | 19246: 352: const RxCmArmStats *r = &m->cell[cell][arm]; |
| 484 | 19246: 353: double phi[RX_CM_D], rm = 0, rsd = 0, rdof = 0; |
| 485 | 19246: 354: residual_basis(f, phi); |
| 486 | call 0 returned 19246 |
| 487 | 19246: 355: if (fit_predict(r, phi, &rm, &rsd, &rdof, RX_CM_RES_MIN_OBS)) { |
| 488 | call 0 returned 19246 |
| 489 | branch 1 taken 5 (fallthrough) |
| 490 | branch 2 taken 19241 |
| 491 | 5: 356: mean += rm; |
| 492 | 5: 357: sd = sqrt(sd * sd + rsd * rsd); |
| 493 | call 0 returned 5 |
| 494 | 5: 358: if (rdof < dof) dof = rdof; |
| 495 | branch 0 taken 5 (fallthrough) |
| 496 | branch 1 taken 0 |
| 497 | 5: 359: n = r->n; |
| 498 | 5: 360: out->known = 1; |
| 499 | -: 361: } else { |
| 500 | -: 362: /* Never seen under this pressure: the quiet prediction, with the |
| 501 | -: 363: * prior on how far an unseen condition may move cost. */ |
| 502 | 19241: 364: sd = sqrt(sd * sd + RX_CM_UNSEEN_SHIFT * RX_CM_UNSEEN_SHIFT); |
| 503 | call 0 returned 19241 |
| 504 | 19241: 365: out->borrowed = 1; |
| 505 | -: 366: } |
| 506 | -: 367: } |
| 507 | 37331: 368: out->n = (uint32_t)n; |
| 508 | 37331: 369: out->mean_log2_ps = mean; |
| 509 | 37331*: 370: out->sd_log2 = sd * (m->sd_scale >= 1.0 ? m->sd_scale : 1.0); |
| 510 | branch 0 taken 37331 (fallthrough) |
| 511 | branch 1 taken 0 |
| 512 | 37331: 371: out->dof = dof; |
| 513 | 37331: 372: uint32_t core = f->core < RX_CM_CORES ? f->core : RX_CM_CORE_OTHER; |
| 514 | 37331: 373: double mw = m->power_mw[core][arm]; |
| 515 | 37331: 374: if (mw > 0.0) out->energy_pj = exp2(mean) * mw * 1e-3; /* ps * mW = 1e-3 pJ */ |
| 516 | branch 0 taken 4 (fallthrough) |
| 517 | branch 1 taken 37327 |
| 518 | call 2 returned 4 |
| 519 | -: 375:#else |
| 520 | -: 376: uint64_t words[32], result; |
| 521 | -: 377: memcpy(words, &mean, sizeof(double)); memcpy(words + 1, &sd, sizeof(double)); |
| 522 | -: 378: memcpy(words + 2, &dof, sizeof(double)); words[3] = n; |
| 523 | -: 379: words[4] = bucket(f); words[5] = 0; |
| 524 | -: 380: if (words[4] != 0) { |
| 525 | -: 381: const RxCmArmStats *r = &m->cell[cell][arm]; |
| 526 | -: 382: double phi[RX_CM_D], rm = 0, rsd = 0, rdof = 0; |
| 527 | -: 383: residual_basis(f, phi); |
| 528 | -: 384: words[5] = (uint64_t)fit_predict(r, phi, &rm, &rsd, &rdof, RX_CM_RES_MIN_OBS); |
| 529 | -: 385: memcpy(words + 6, &rm, sizeof(double)); memcpy(words + 7, &rsd, sizeof(double)); |
| 530 | -: 386: memcpy(words + 8, &rdof, sizeof(double)); words[9] = r->n; |
| 531 | -: 387: } |
| 532 | -: 388: memcpy(words + 10, &m->sd_scale, sizeof(double)); words[11] = f->core; |
| 533 | -: 389: for (uint32_t c = 0; c < RX_CM_CORES; ++c) |
| 534 | -: 390: memcpy(words + 12 + c, &m->power_mw[c][arm], sizeof(double)); |
| 535 | -: 391: if (omega_cm_prediction(&result, words, 32) != OMEGA_OK) return RX_CM_OK; |
| 536 | -: 392: memcpy(&out->mean_log2_ps, words + 16, sizeof(double)); |
| 537 | -: 393: memcpy(&out->sd_log2, words + 17, sizeof(double)); |
| 538 | -: 394: memcpy(&out->dof, words + 18, sizeof(double)); |
| 539 | -: 395: out->n = (uint32_t)words[19]; out->known = (int)words[20]; out->borrowed = (int)words[21]; |
| 540 | -: 396: memcpy(&out->energy_pj, words + 22, sizeof(double)); |
| 541 | -: 397:#endif |
| 542 | 37331: 398: return RX_CM_OK; |
| 543 | -: 399:} |
| 544 | -: 400: |
| 545 | function oracle_cm_observe called 4083 returned 100% blocks executed 91% |
| 546 | 4083: 401:int rx_cm_observe(RxCostModel *m, const RxCmFeatures *f, uint32_t arm, const RxCmObservation *o) { |
| 547 | 4083*: 402: if (!m || !f || !o) return RX_CM_ERR_ARG; |
| 548 | branch 0 taken 4083 (fallthrough) |
| 549 | branch 1 taken 0 |
| 550 | branch 2 taken 4083 (fallthrough) |
| 551 | branch 3 taken 0 |
| 552 | branch 4 taken 0 (fallthrough) |
| 553 | branch 5 taken 4083 |
| 554 | 4083*: 403: if (!cm_route2(2, arm, m->n_arms)) return RX_CM_ERR_ARG; |
| 555 | call 0 returned 4083 |
| 556 | branch 1 taken 0 (fallthrough) |
| 557 | branch 2 taken 4083 |
| 558 | 4083: 404: uint32_t cell = rx_cm_cell(f); |
| 559 | call 0 returned 4083 |
| 560 | 4083: 405: if (!cm_route2(2, cell, RX_CM_CELLS)) return RX_CM_ERR_UNSUPPORTED; |
| 561 | call 0 returned 4083 |
| 562 | branch 1 taken 7 (fallthrough) |
| 563 | branch 2 taken 4076 |
| 564 | 4076: 406: RxCmArmStats *s = &m->cell[cell][arm]; |
| 565 | -: 407:#if !CUDAPP_MIGRATION_PENDING_C_CM_STATS |
| 566 | -: 408: uint64_t words[64] = {0}, stage; |
| 567 | -: 409: memcpy(words, s, sizeof *s); |
| 568 | -: 410: words[56] = (uint64_t)(uint32_t)o->failed; |
| 569 | -: 411: words[59] = o->ps_per_call; |
| 570 | -: 412: if (omega_cm_stats(&stage, 0, words, 64) != OMEGA_OK) return RX_CM_ERR_ARG; |
| 571 | -: 413: memcpy(s, words, sizeof *s); |
| 572 | -: 414: if (stage == 1) return RX_CM_OK; |
| 573 | -: 415: if (stage != 0) return RX_CM_ERR_ARG; |
| 574 | -: 416: double phi[RX_CM_D]; |
| 575 | -: 417: if (bucket(f) == 0) { |
| 576 | -: 418: rx_cm_basis(f, phi); |
| 577 | -: 419: } else { |
| 578 | -: 420: double qm, qsd, qdof; |
| 579 | -: 421: uint64_t qn; |
| 580 | -: 422: if (!quiet_predict(m, f, arm, &qm, &qsd, &qdof, &qn)) return RX_CM_SKIPPED; |
| 581 | -: 423: residual_basis(f, phi); |
| 582 | -: 424: words[57] = 1; |
| 583 | -: 425: memcpy(words + 58, &qm, sizeof qm); |
| 584 | -: 426: } |
| 585 | -: 427: memcpy(words + 48, phi, sizeof phi); |
| 586 | -: 428: if (omega_cm_stats(&stage, 1, words, 64) != OMEGA_OK || stage != 0) return RX_CM_ERR_ARG; |
| 587 | -: 429: memcpy(s, words, sizeof *s); |
| 588 | -: 430: return RX_CM_OK; |
| 589 | -: 431:#else |
| 590 | 4076: 432: s->trials++; |
| 591 | 4076: 433: if (o->failed) { |
| 592 | branch 0 taken 404 (fallthrough) |
| 593 | branch 1 taken 3672 |
| 594 | 404: 434: s->failures++; |
| 595 | 404: 435: return RX_CM_OK; |
| 596 | -: 436: } |
| 597 | 3672*: 437: if (o->ps_per_call == 0) return RX_CM_ERR_ARG; |
| 598 | branch 0 taken 0 (fallthrough) |
| 599 | branch 1 taken 3672 |
| 600 | -: 438: double phi[RX_CM_D]; |
| 601 | 3672: 439: double y = log2((double)o->ps_per_call); |
| 602 | call 0 returned 3672 |
| 603 | 3672: 440: if (bucket(f) == 0) { |
| 604 | call 0 returned 3672 |
| 605 | branch 1 taken 3481 (fallthrough) |
| 606 | branch 2 taken 191 |
| 607 | 3481: 441: rx_cm_basis(f, phi); |
| 608 | call 0 returned 3481 |
| 609 | -: 442: } else { |
| 610 | -: 443: double qm, qsd, qdof; |
| 611 | -: 444: uint64_t qn; |
| 612 | 191: 445: if (!quiet_predict(m, f, arm, &qm, &qsd, &qdof, &qn)) return RX_CM_SKIPPED; |
| 613 | call 0 returned 191 |
| 614 | branch 1 taken 167 (fallthrough) |
| 615 | branch 2 taken 24 |
| 616 | 24: 446: residual_basis(f, phi); |
| 617 | call 0 returned 24 |
| 618 | 24: 447: y -= qm; |
| 619 | -: 448: } |
| 620 | 31545: 449: for (uint32_t i = 0, k = 0; i < RX_CM_D; i++) { |
| 621 | branch 0 taken 28040 |
| 622 | branch 1 taken 3505 (fallthrough) |
| 623 | 154220: 450: for (uint32_t j = i; j < RX_CM_D; j++, k++) s->tri[k] += phi[i] * phi[j]; |
| 624 | branch 0 taken 126180 |
| 625 | branch 1 taken 28040 (fallthrough) |
| 626 | 28040: 451: s->xty[i] += phi[i] * y; |
| 627 | -: 452: } |
| 628 | 3505: 453: s->yty += y * y; |
| 629 | 3505: 454: s->n++; |
| 630 | 3505: 455: return RX_CM_OK; |
| 631 | -: 456:#endif |
| 632 | -: 457:} |
| 633 | -: 458: |
| 634 | -: 459:#if CUDAPP_MIGRATION_PENDING_C_CM_DECISION |
| 635 | function norm_cdf called 28050 returned 100% blocks executed 100% |
| 636 | 28050: 460:static double norm_cdf(double x) { return 0.5 * erfc(-x / sqrt(2.0)); } |
| 637 | call 0 returned 28050 |
| 638 | call 1 returned 28050 |
| 639 | -: 461:#endif |
| 640 | -: 462: |
| 641 | -: 463:/* Acklam's inverse normal. */ |
| 642 | -: 464:#if CUDAPP_MIGRATION_PENDING_C_CM_QUANTILE |
| 643 | function norm_inv called 300 returned 100% blocks executed 43% |
| 644 | 300: 465:static double norm_inv(double p) { |
| 645 | -: 466: static const double a[] = { -3.969683028665376e+01, 2.209460984245205e+02, |
| 646 | -: 467: -2.759285104469687e+02, 1.383577518672690e+02, |
| 647 | -: 468: -3.066479806614716e+01, 2.506628277459239e+00 }; |
| 648 | -: 469: static const double b[] = { -5.447609879822406e+01, 1.615858368580409e+02, |
| 649 | -: 470: -1.556989798598866e+02, 6.680131188771972e+01, |
| 650 | -: 471: -1.328068155288572e+01 }; |
| 651 | -: 472: static const double c[] = { -7.784894002430293e-03, -3.223964580411365e-01, |
| 652 | -: 473: -2.400758277161838e+00, -2.549732539343734e+00, |
| 653 | -: 474: 4.374664141464968e+00, 2.938163982698783e+00 }; |
| 654 | -: 475: static const double d[] = { 7.784695709041462e-03, 3.224671290700398e-01, |
| 655 | -: 476: 2.445134137142996e+00, 3.754408661907416e+00 }; |
| 656 | 300*: 477: if (p <= 0.0) return -INFINITY; |
| 657 | branch 0 taken 0 (fallthrough) |
| 658 | branch 1 taken 300 |
| 659 | 300*: 478: if (p >= 1.0) return INFINITY; |
| 660 | branch 0 taken 0 (fallthrough) |
| 661 | branch 1 taken 300 |
| 662 | -: 479: double q, r; |
| 663 | 300: 480: if (p < 0.02425) { |
| 664 | branch 0 taken 0 (fallthrough) |
| 665 | branch 1 taken 300 |
| 666 | #####: 481: q = sqrt(-2 * log(p)); |
| 667 | call 0 never executed |
| 668 | call 1 never executed |
| 669 | #####: 482: return (((((c[0] * q + c[1]) * q + c[2]) * q + c[3]) * q + c[4]) * q + c[5]) / |
| 670 | #####: 483: ((((d[0] * q + d[1]) * q + d[2]) * q + d[3]) * q + 1); |
| 671 | -: 484: } |
| 672 | 300: 485: if (p > 1 - 0.02425) { |
| 673 | branch 0 taken 0 (fallthrough) |
| 674 | branch 1 taken 300 |
| 675 | #####: 486: q = sqrt(-2 * log(1 - p)); |
| 676 | call 0 never executed |
| 677 | call 1 never executed |
| 678 | #####: 487: return -(((((c[0] * q + c[1]) * q + c[2]) * q + c[3]) * q + c[4]) * q + c[5]) / |
| 679 | #####: 488: ((((d[0] * q + d[1]) * q + d[2]) * q + d[3]) * q + 1); |
| 680 | -: 489: } |
| 681 | 300: 490: q = p - 0.5; |
| 682 | 300: 491: r = q * q; |
| 683 | 300: 492: return (((((a[0] * r + a[1]) * r + a[2]) * r + a[3]) * r + a[4]) * r + a[5]) * q / |
| 684 | 300: 493: (((((b[0] * r + b[1]) * r + b[2]) * r + b[3]) * r + b[4]) * r + 1); |
| 685 | -: 494:} |
| 686 | -: 495:#endif |
| 687 | -: 496: |
| 688 | function oracle_cm_t_quantile called 300 returned 100% blocks executed 80% |
| 689 | 300: 497:double rx_cm_t_quantile(double q, double dof) { |
| 690 | -: 498:#if CUDAPP_MIGRATION_PENDING_C_CM_QUANTILE |
| 691 | 300: 499: double z = norm_inv(0.5 + 0.5 * q); |
| 692 | call 0 returned 300 |
| 693 | 300*: 500: if (!(dof > 0.0)) return z; |
| 694 | branch 0 taken 0 (fallthrough) |
| 695 | branch 1 taken 300 |
| 696 | 300: 501: double z3 = z * z * z, z5 = z3 * z * z; |
| 697 | 300: 502: return z + (z3 + z) / (4.0 * dof) + (5.0 * z5 + 16.0 * z3 + 3.0 * z) / (96.0 * dof * dof); |
| 698 | -: 503:#else |
| 699 | -: 504: uint64_t qb, db, result; |
| 700 | -: 505: double value; |
| 701 | -: 506: memcpy(&qb, &q, sizeof qb); |
| 702 | -: 507: memcpy(&db, &dof, sizeof db); |
| 703 | -: 508: if (omega_cm_quantile(&result, qb, db) != OMEGA_OK) return NAN; |
| 704 | -: 509: memcpy(&value, &result, sizeof value); |
| 705 | -: 510: return value; |
| 706 | -: 511:#endif |
| 707 | -: 512:} |
| 708 | -: 513: |
| 709 | -: 514:#if CUDAPP_MIGRATION_PENDING_C_CM_DECISION |
| 710 | function explore_allowed called 10474 returned 100% blocks executed 100% |
| 711 | 10474: 515:static int explore_allowed(const RxCostModel *m, const RxCmPolicy *p) { |
| 712 | 10474: 516: double cap = p->explore_frac * (double)m->work_ps + (double)p->explore_allow_ps; |
| 713 | 10474: 517: return (double)m->explore_ps < cap; |
| 714 | -: 518:} |
| 715 | -: 519: |
| 716 | -: 520:/* E[max(0, X)] for X ~ N(d, s^2): the expected gain, in log2 units, of |
| 717 | -: 521: * switching to a competitor whose advantage over the chosen arm is d. */ |
| 718 | function expected_improvement called 14025 returned 100% blocks executed 60% |
| 719 | 14025: 522:static double expected_improvement(double d, double s) { |
| 720 | 14025*: 523: if (!(s > 0.0)) return d > 0.0 ? d : 0.0; |
| 721 | branch 0 taken 0 (fallthrough) |
| 722 | branch 1 taken 14025 |
| 723 | branch 2 never executed (fallthrough) |
| 724 | branch 3 never executed |
| 725 | 14025: 524: double u = d / s; |
| 726 | 14025: 525: return s * exp(-0.5 * u * u) / sqrt(2.0 * M_PI) + d * norm_cdf(u); |
| 727 | call 0 returned 14025 |
| 728 | call 1 returned 14025 |
| 729 | call 2 returned 14025 |
| 730 | -: 526:} |
| 731 | -: 527: |
| 732 | function oracle_cm_decide called 21058 returned 100% blocks executed 99% |
| 733 | 21058: 528:void rx_cm_decide(const RxCostModel *m, const RxCmPolicy *p, const RxCmFeatures *f, |
| 734 | -: 529: int allow_measure, RxCmDecision *out) { |
| 735 | 21058: 530: memset(out, 0, sizeof *out); |
| 736 | 21058: 531: if (m && f && rx_cm_cell(f) >= RX_CM_CELLS) { |
| 737 | branch 0 taken 21058 (fallthrough) |
| 738 | branch 1 taken 0 |
| 739 | branch 2 taken 21058 (fallthrough) |
| 740 | branch 3 taken 0 |
| 741 | call 4 returned 21058 |
| 742 | branch 5 taken 65 (fallthrough) |
| 743 | branch 6 taken 20993 |
| 744 | 65: 532: uint32_t na = m->n_arms; |
| 745 | 65: 533: out->action = RX_CM_REFUSE; |
| 746 | 65: 534: out->why = RX_CM_WHY_UNSUPPORTED_OP; |
| 747 | 65: 535: out->arm = na; |
| 748 | 65: 536: out->measure[0] = na; |
| 749 | 65: 537: out->measure[1] = na; |
| 750 | 585: 538: for (uint32_t a = 0; a < RX_CM_ARMS; a++) out->pred[a].refused = 1; |
| 751 | branch 0 taken 520 |
| 752 | branch 1 taken 65 (fallthrough) |
| 753 | 17344: 539: return; |
| 754 | -: 540: } |
| 755 | 20993: 541: uint32_t fb = p->fallback_arm < m->n_arms ? p->fallback_arm : 0; |
| 756 | branch 0 taken 20729 (fallthrough) |
| 757 | branch 1 taken 264 |
| 758 | 20993: 542: uint32_t elig = (f->eligible | (1u << fb)) & ((1u << m->n_arms) - 1u); |
| 759 | 20993: 543: int have[RX_CM_ARMS] = { 0 }; |
| 760 | 20993: 544: int n_elig = 0; |
| 761 | 108769: 545: for (uint32_t a = 0; a < m->n_arms; a++) { |
| 762 | branch 0 taken 87776 |
| 763 | branch 1 taken 20993 (fallthrough) |
| 764 | 87776: 546: rx_cm_predict(m, f, a, &out->pred[a]); |
| 765 | call 0 returned 87776 |
| 766 | 87776: 547: if (!(elig & (1u << a))) continue; |
| 767 | branch 0 taken 23356 (fallthrough) |
| 768 | branch 1 taken 64420 |
| 769 | 64420: 548: if (a != fb && failures_of(m, a) > 0 && out->pred[a].fail_p > p->max_fail_p) { |
| 770 | branch 0 taken 43427 (fallthrough) |
| 771 | branch 1 taken 20993 |
| 772 | call 2 returned 43427 |
| 773 | branch 3 taken 421 (fallthrough) |
| 774 | branch 4 taken 43006 |
| 775 | branch 5 taken 421 (fallthrough) |
| 776 | branch 6 taken 0 |
| 777 | 421: 549: elig &= ~(1u << a); |
| 778 | 421: 550: continue; |
| 779 | -: 551: } |
| 780 | 63999: 552: n_elig++; |
| 781 | 63999: 553: have[a] = out->pred[a].known || out->pred[a].borrowed; |
| 782 | branch 0 taken 57177 (fallthrough) |
| 783 | branch 1 taken 6822 |
| 784 | branch 2 taken 14108 (fallthrough) |
| 785 | branch 3 taken 43069 |
| 786 | -: 554: } |
| 787 | 20993: 555: out->action = RX_CM_FALLBACK; |
| 788 | 20993: 556: out->arm = fb; |
| 789 | 20993: 557: if (n_elig <= 1) { |
| 790 | branch 0 taken 15 (fallthrough) |
| 791 | branch 1 taken 20978 |
| 792 | 15: 558: out->why = RX_CM_WHY_NO_ELIGIBLE; |
| 793 | 15: 559: return; |
| 794 | -: 560: } |
| 795 | -: 561: |
| 796 | -: 562: /* Best by predicted latency among arms that meet the budgets; if none |
| 797 | -: 563: * meets them, best overall and say so. */ |
| 798 | 20978: 564: int best = -1, best_fit = -1; |
| 799 | 108645: 565: for (uint32_t a = 0; a < m->n_arms; a++) { |
| 800 | branch 0 taken 87667 |
| 801 | branch 1 taken 20978 (fallthrough) |
| 802 | 87667: 566: if (!have[a] || !(elig & (1u << a))) continue; |
| 803 | branch 0 taken 20927 (fallthrough) |
| 804 | branch 1 taken 66740 |
| 805 | branch 2 taken 0 (fallthrough) |
| 806 | branch 3 taken 20927 |
| 807 | 20927: 567: const RxCmPrediction *pr = &out->pred[a]; |
| 808 | 20927: 568: double hi = exp2(pr->mean_log2_ps + 1.2816 * pr->sd_log2); |
| 809 | call 0 returned 20927 |
| 810 | 32060: 569: int fits = (!f->latency_budget_ps || hi <= (double)f->latency_budget_ps) && |
| 811 | branch 0 taken 10172 (fallthrough) |
| 812 | branch 1 taken 10755 |
| 813 | branch 2 taken 378 (fallthrough) |
| 814 | branch 3 taken 9794 |
| 815 | 11133: 570: (!f->energy_budget_pj || pr->energy_pj == 0.0 || |
| 816 | branch 0 taken 71 (fallthrough) |
| 817 | branch 1 taken 11062 |
| 818 | branch 2 taken 2 (fallthrough) |
| 819 | branch 3 taken 69 |
| 820 | 2: 571: pr->energy_pj <= (double)f->energy_budget_pj); |
| 821 | branch 0 taken 1 (fallthrough) |
| 822 | branch 1 taken 1 |
| 823 | 20927: 572: if (best < 0 || pr->mean_log2_ps < out->pred[best].mean_log2_ps) best = (int)a; |
| 824 | branch 0 taken 14025 (fallthrough) |
| 825 | branch 1 taken 6902 |
| 826 | branch 2 taken 1638 (fallthrough) |
| 827 | branch 3 taken 12387 |
| 828 | 20927: 573: if (fits && (best_fit < 0 || pr->mean_log2_ps < out->pred[best_fit].mean_log2_ps)) |
| 829 | branch 0 taken 11132 (fallthrough) |
| 830 | branch 1 taken 9795 |
| 831 | branch 2 taken 7421 (fallthrough) |
| 832 | branch 3 taken 3711 |
| 833 | branch 4 taken 943 (fallthrough) |
| 834 | branch 5 taken 6478 |
| 835 | 4654: 574: best_fit = (int)a; |
| 836 | -: 575: } |
| 837 | 20978: 576: int over = 0; |
| 838 | 20978: 577: if (best_fit >= 0) best = best_fit; |
| 839 | branch 0 taken 3711 (fallthrough) |
| 840 | branch 1 taken 17267 |
| 841 | 17267: 578: else if (best >= 0) over = 1; |
| 842 | branch 0 taken 3191 (fallthrough) |
| 843 | branch 1 taken 14076 |
| 844 | -: 579: |
| 845 | -: 580: /* Competitor: the eligible arm with the largest expected improvement |
| 846 | -: 581: * over `best`. An arm with no prediction at all is always worth a look. */ |
| 847 | 20978: 582: int comp = -1, comp_unknown = 0; |
| 848 | 20978: 583: double ei = 0.0, pc = 0.0; |
| 849 | 108645: 584: for (uint32_t a = 0; a < m->n_arms; a++) { |
| 850 | branch 0 taken 87667 |
| 851 | branch 1 taken 20978 (fallthrough) |
| 852 | 87667: 585: if (!(elig & (1u << a)) || (int)a == best) continue; |
| 853 | branch 0 taken 63984 (fallthrough) |
| 854 | branch 1 taken 23683 |
| 855 | branch 2 taken 6902 (fallthrough) |
| 856 | branch 3 taken 57082 |
| 857 | -: 586: double e, pa; |
| 858 | 57082: 587: int unknown = !have[a] || best < 0; |
| 859 | branch 0 taken 14025 (fallthrough) |
| 860 | branch 1 taken 43057 |
| 861 | branch 2 taken 0 (fallthrough) |
| 862 | branch 3 taken 14025 |
| 863 | 57082: 588: if (unknown) { |
| 864 | branch 0 taken 43057 (fallthrough) |
| 865 | branch 1 taken 14025 |
| 866 | 43057: 589: e = INFINITY; |
| 867 | 43057: 590: pa = 0.5; |
| 868 | -: 591: } else { |
| 869 | 14025: 592: const RxCmPrediction *x = &out->pred[a], *b = &out->pred[best]; |
| 870 | 14025: 593: double s = sqrt(x->sd_log2 * x->sd_log2 + b->sd_log2 * b->sd_log2); |
| 871 | call 0 returned 14025 |
| 872 | 14025: 594: double d = b->mean_log2_ps - x->mean_log2_ps; |
| 873 | 14025: 595: e = expected_improvement(d, s); |
| 874 | call 0 returned 14025 |
| 875 | 14025*: 596: pa = norm_cdf(d / (s > 0 ? s : 1e-9)); |
| 876 | branch 0 taken 14025 (fallthrough) |
| 877 | branch 1 taken 0 |
| 878 | call 2 returned 14025 |
| 879 | -: 597: } |
| 880 | 57082: 598: if (comp < 0 || e > ei) { |
| 881 | branch 0 taken 36104 (fallthrough) |
| 882 | branch 1 taken 20978 |
| 883 | branch 2 taken 5316 (fallthrough) |
| 884 | branch 3 taken 30788 |
| 885 | 26294: 599: comp = (int)a; |
| 886 | 26294: 600: ei = e; |
| 887 | 26294: 601: pc = pa; |
| 888 | 26294: 602: comp_unknown = unknown; |
| 889 | -: 603: } |
| 890 | -: 604: } |
| 891 | 20978: 605: out->p_runner_up_better = pc; |
| 892 | 20978: 606: out->expected_improvement = isinf(ei) ? -1.0 : ei; |
| 893 | branch 0 taken 6902 (fallthrough) |
| 894 | branch 1 taken 14076 |
| 895 | -: 607: |
| 896 | 20978: 608: uint32_t why = RX_CM_WHY_CONFIDENT; |
| 897 | 20978: 609: if (best < 0 || comp_unknown) why = RX_CM_WHY_UNKNOWN_ARM; |
| 898 | branch 0 taken 6902 (fallthrough) |
| 899 | branch 1 taken 14076 |
| 900 | branch 2 taken 0 (fallthrough) |
| 901 | branch 3 taken 6902 |
| 902 | 6902: 610: else if (ei > p->ei_min) why = RX_CM_WHY_WORTH_MEASURING; |
| 903 | branch 0 taken 6543 (fallthrough) |
| 904 | branch 1 taken 359 |
| 905 | -: 611: |
| 906 | 20978: 612: if (why != RX_CM_WHY_CONFIDENT && allow_measure && explore_allowed(m, p) && comp >= 0) { |
| 907 | branch 0 taken 20619 (fallthrough) |
| 908 | branch 1 taken 359 |
| 909 | branch 2 taken 10294 (fallthrough) |
| 910 | branch 3 taken 10325 |
| 911 | call 4 returned 10294 |
| 912 | branch 5 taken 10057 (fallthrough) |
| 913 | branch 6 taken 237 |
| 914 | branch 7 taken 10057 (fallthrough) |
| 915 | branch 8 taken 0 |
| 916 | 10057: 613: out->action = RX_CM_MEASURE; |
| 917 | 10057: 614: out->measure[0] = best >= 0 ? (uint32_t)best : fb; |
| 918 | branch 0 taken 3188 (fallthrough) |
| 919 | branch 1 taken 6869 |
| 920 | 10057: 615: out->measure[1] = (uint32_t)comp; |
| 921 | 10057: 616: if (out->measure[0] == out->measure[1]) out->measure[1] = fb; |
| 922 | branch 0 taken 6771 (fallthrough) |
| 923 | branch 1 taken 3286 |
| 924 | 10057: 617: out->why = why; |
| 925 | 10057: 618: out->arm = out->measure[0]; |
| 926 | 10057: 619: return; |
| 927 | -: 620: } |
| 928 | 10921: 621: if (best < 0) { |
| 929 | branch 0 taken 7207 (fallthrough) |
| 930 | branch 1 taken 3714 |
| 931 | 180: 622: out->why = allow_measure && !explore_allowed(m, p) ? RX_CM_WHY_BUDGET_SPENT |
| 932 | call 0 returned 180 |
| 933 | branch 1 taken 180 (fallthrough) |
| 934 | branch 2 taken 0 |
| 935 | 7387: 623: : RX_CM_WHY_UNKNOWN_ARM; |
| 936 | branch 0 taken 180 (fallthrough) |
| 937 | branch 1 taken 7027 |
| 938 | 7207: 624: return; /* fallback */ |
| 939 | -: 625: } |
| 940 | 3714: 626: out->action = RX_CM_SELECT; |
| 941 | 3714: 627: out->arm = (uint32_t)best; |
| 942 | 3714: 628: if (over) out->why = RX_CM_WHY_OVER_BUDGET; |
| 943 | branch 0 taken 1669 (fallthrough) |
| 944 | branch 1 taken 2045 |
| 945 | 2045: 629: else if (why != RX_CM_WHY_CONFIDENT && allow_measure) out->why = RX_CM_WHY_BUDGET_SPENT; |
| 946 | branch 0 taken 1832 (fallthrough) |
| 947 | branch 1 taken 213 |
| 948 | branch 2 taken 54 (fallthrough) |
| 949 | branch 3 taken 1778 |
| 950 | 1991: 630: else out->why = RX_CM_WHY_CONFIDENT; |
| 951 | -: 631:} |
| 952 | -: 632:#else |
| 953 | -: 633:static void cm_decision_output(const uint64_t words[128], RxCmDecision *out) { |
| 954 | -: 634: out->action = (int)words[100]; out->arm = (uint32_t)words[101]; |
| 955 | -: 635: out->measure[0] = (uint32_t)words[102]; out->measure[1] = (uint32_t)words[103]; |
| 956 | -: 636: out->why = (uint32_t)words[104]; |
| 957 | -: 637: memcpy(&out->p_runner_up_better, words + 105, sizeof(double)); |
| 958 | -: 638: memcpy(&out->expected_improvement, words + 106, sizeof(double)); |
| 959 | -: 639:} |
| 960 | -: 640: |
| 961 | -: 641:void rx_cm_decide(const RxCostModel *m, const RxCmPolicy *p, const RxCmFeatures *f, |
| 962 | -: 642: int allow_measure, RxCmDecision *out) { |
| 963 | -: 643: uint64_t words[128], stage; |
| 964 | -: 644: memset(out, 0, sizeof *out); |
| 965 | -: 645: words[80] = m->n_arms; words[81] = f->op; |
| 966 | -: 646: if (omega_cm_decision(&stage, 0, words, 128) != OMEGA_OK) return; |
| 967 | -: 647: if (stage == 0) { |
| 968 | -: 648: for (uint32_t a = 0; a < RX_CM_ARMS; ++a) out->pred[a].refused = (int)words[9 * a + 8]; |
| 969 | -: 649: cm_decision_output(words, out); |
| 970 | -: 650: return; |
| 971 | -: 651: } |
| 972 | -: 652: for (uint32_t a = 0; a < m->n_arms; ++a) { |
| 973 | -: 653: RxCmPrediction *pr = &out->pred[a]; |
| 974 | -: 654: rx_cm_predict(m, f, a, pr); |
| 975 | -: 655: uint64_t *w = words + 9 * a; |
| 976 | -: 656: memcpy(w, &pr->mean_log2_ps, sizeof(double)); |
| 977 | -: 657: memcpy(w + 1, &pr->sd_log2, sizeof(double)); |
| 978 | -: 658: memcpy(w + 2, &pr->dof, sizeof(double)); |
| 979 | -: 659: w[3] = pr->n; w[4] = (uint64_t)(uint32_t)pr->known; w[5] = (uint64_t)(uint32_t)pr->borrowed; |
| 980 | -: 660: memcpy(w + 6, &pr->energy_pj, sizeof(double)); |
| 981 | -: 661: memcpy(w + 7, &pr->fail_p, sizeof(double)); |
| 982 | -: 662: w[8] = (uint64_t)(uint32_t)pr->refused; |
| 983 | -: 663: words[72 + a] = failures_of(m, a); |
| 984 | -: 664: } |
| 985 | -: 665: words[82] = f->eligible; |
| 986 | -: 666: words[86] = f->latency_budget_ps; words[87] = f->energy_budget_pj; |
| 987 | -: 667: words[88] = (uint64_t)(uint32_t)allow_measure; |
| 988 | -: 668: memcpy(words + 89, &p->ei_min, sizeof(double)); |
| 989 | -: 669: memcpy(words + 90, &p->max_fail_p, sizeof(double)); |
| 990 | -: 670: words[91] = p->fallback_arm; |
| 991 | -: 671: memcpy(words + 92, &p->explore_frac, sizeof(double)); |
| 992 | -: 672: words[93] = p->explore_allow_ps; words[94] = m->explore_ps; words[95] = m->work_ps; |
| 993 | -: 673: if (omega_cm_decision(&stage, 1, words, 128) != OMEGA_OK) return; |
| 994 | -: 674: cm_decision_output(words, out); |
| 995 | -: 675:} |
| 996 | -: 676:#endif |
| 997 | -: 677: |
| 998 | -: 678: |
| 999 | -: 679:#if CUDAPP_MIGRATION_PENDING_C_CM_CALIBRATION |
| 1000 | function oracle_cm_calibrate called 3 returned 100% blocks executed 87% |
| 1001 | 3: 680:double rx_cm_calibrate(RxCostModel *m, const RxCmFeatures *f, const uint32_t *arm, |
| 1002 | -: 681: const uint64_t *ps, uint32_t n, double q) { |
| 1003 | 3*: 682: if (!m || !f || !arm || !ps || n == 0 || !(q > 0.0 && q < 1.0)) return 0.0; |
| 1004 | branch 0 taken 3 (fallthrough) |
| 1005 | branch 1 taken 0 |
| 1006 | branch 2 taken 3 (fallthrough) |
| 1007 | branch 3 taken 0 |
| 1008 | branch 4 taken 3 (fallthrough) |
| 1009 | branch 5 taken 0 |
| 1010 | branch 6 taken 3 (fallthrough) |
| 1011 | branch 7 taken 0 |
| 1012 | branch 8 taken 3 (fallthrough) |
| 1013 | branch 9 taken 0 |
| 1014 | branch 10 taken 3 (fallthrough) |
| 1015 | branch 11 taken 0 |
| 1016 | branch 12 taken 0 (fallthrough) |
| 1017 | branch 13 taken 3 |
| 1018 | 3: 683: double *r = malloc(sizeof(double) * n); |
| 1019 | 3*: 684: if (!r) return 0.0; |
| 1020 | branch 0 taken 0 (fallthrough) |
| 1021 | branch 1 taken 3 |
| 1022 | 3: 685: double saved = m->sd_scale; |
| 1023 | 3: 686: m->sd_scale = 1.0; |
| 1024 | 3: 687: uint32_t k = 0; |
| 1025 | 619: 688: for (uint32_t i = 0; i < n; i++) { |
| 1026 | branch 0 taken 616 |
| 1027 | branch 1 taken 3 (fallthrough) |
| 1028 | -: 689: RxCmPrediction p; |
| 1029 | 616: 690: rx_cm_predict(m, &f[i], arm[i], &p); |
| 1030 | call 0 returned 616 |
| 1031 | 616: 691: if (!(p.known || p.borrowed) || ps[i] == 0 || !(p.sd_log2 > 0.0)) continue; |
| 1032 | branch 0 taken 316 (fallthrough) |
| 1033 | branch 1 taken 300 |
| 1034 | branch 2 taken 0 (fallthrough) |
| 1035 | branch 3 taken 316 |
| 1036 | branch 4 taken 300 (fallthrough) |
| 1037 | branch 5 taken 0 |
| 1038 | branch 6 taken 0 (fallthrough) |
| 1039 | branch 7 taken 300 |
| 1040 | 300: 692: double z = fabs(log2((double)ps[i]) - p.mean_log2_ps) / p.sd_log2; |
| 1041 | call 0 returned 300 |
| 1042 | call 1 returned 300 |
| 1043 | 300: 693: r[k++] = z / rx_cm_t_quantile(q, p.dof); |
| 1044 | call 0 returned 300 |
| 1045 | -: 694: } |
| 1046 | 3: 695: double scale = 0.0; |
| 1047 | 3: 696: if (k >= 10) { |
| 1048 | branch 0 taken 1 (fallthrough) |
| 1049 | branch 1 taken 2 |
| 1050 | 1*: 697: if (rx_cm_sort(r, k) != 0) { m->sd_scale = saved; free(r); return 0.0; } |
| 1051 | call 0 returned 1 |
| 1052 | branch 1 taken 0 (fallthrough) |
| 1053 | branch 2 taken 1 |
| 1054 | 1: 698: uint32_t idx = (uint32_t)ceil(q * (double)k) - 1u; |
| 1055 | call 0 returned 1 |
| 1056 | 1*: 699: scale = r[idx < k ? idx : k - 1]; |
| 1057 | branch 0 taken 1 (fallthrough) |
| 1058 | branch 1 taken 0 |
| 1059 | 1*: 700: if (scale < 1.0) scale = 1.0; |
| 1060 | branch 0 taken 0 (fallthrough) |
| 1061 | branch 1 taken 1 |
| 1062 | 1: 701: m->sd_scale = scale; |
| 1063 | -: 702: } else { |
| 1064 | 2: 703: m->sd_scale = saved; |
| 1065 | -: 704: } |
| 1066 | 3: 705: free(r); |
| 1067 | 3: 706: return scale; |
| 1068 | -: 707:} |
| 1069 | -: 708:#else |
| 1070 | -: 709:double rx_cm_calibrate(RxCostModel *m, const RxCmFeatures *f, const uint32_t *arm, |
| 1071 | -: 710: const uint64_t *ps, uint32_t n, double q) { |
| 1072 | -: 711: if (!m || !f || !arm || !ps) return 0.0; |
| 1073 | -: 712: uint64_t words[32], stage; |
| 1074 | -: 713: memcpy(words, &q, sizeof(double)); words[2] = n; |
| 1075 | -: 714: if (omega_cm_calibration(&stage, 0, words, 32) != OMEGA_OK || stage == 0) return 0.0; |
| 1076 | -: 715: double *r = malloc(sizeof(double) * n); |
| 1077 | -: 716: if (!r) return 0.0; |
| 1078 | -: 717: double saved = m->sd_scale, scale = 0; |
| 1079 | -: 718: memcpy(words + 1, &saved, sizeof(double)); |
| 1080 | -: 719: memcpy(&m->sd_scale, words + 10, sizeof(double)); |
| 1081 | -: 720: uint32_t k = 0; |
| 1082 | -: 721: for (uint32_t i = 0; i < n; ++i) { |
| 1083 | -: 722: RxCmPrediction p; |
| 1084 | -: 723: rx_cm_predict(m, &f[i], arm[i], &p); |
| 1085 | -: 724: words[3] = ps[i]; |
| 1086 | -: 725: memcpy(words + 4, &p.mean_log2_ps, sizeof(double)); |
| 1087 | -: 726: memcpy(words + 5, &p.sd_log2, sizeof(double)); |
| 1088 | -: 727: memcpy(words + 6, &p.dof, sizeof(double)); |
| 1089 | -: 728: words[7] = (uint64_t)(uint32_t)p.known; words[8] = (uint64_t)(uint32_t)p.borrowed; |
| 1090 | -: 729: if (omega_cm_calibration(&stage, 1, words, 32) != OMEGA_OK) { |
| 1091 | -: 730: m->sd_scale = saved; free(r); return 0.0; |
| 1092 | -: 731: } |
| 1093 | -: 732: if (stage != 0) memcpy(r + k++, words + 10, sizeof(double)); |
| 1094 | -: 733: } |
| 1095 | -: 734: words[2] = k; |
| 1096 | -: 735: if (omega_cm_calibration(&stage, 2, words, 32) != OMEGA_OK) { |
| 1097 | -: 736: m->sd_scale = saved; free(r); return 0.0; |
| 1098 | -: 737: } |
| 1099 | -: 738: if (stage != 0) { |
| 1100 | -: 739: uint64_t index = words[10]; |
| 1101 | -: 740: if (rx_cm_sort(r, k) != 0) { m->sd_scale = saved; free(r); return 0.0; } |
| 1102 | -: 741: memcpy(words + 9, r + index, sizeof(double)); |
| 1103 | -: 742: if (omega_cm_calibration(&stage, 3, words, 32) != OMEGA_OK) { |
| 1104 | -: 743: m->sd_scale = saved; free(r); return 0.0; |
| 1105 | -: 744: } |
| 1106 | -: 745: memcpy(&scale, words + 10, sizeof(double)); |
| 1107 | -: 746: } |
| 1108 | -: 747: memcpy(&m->sd_scale, words + 10, sizeof(double)); |
| 1109 | -: 748: free(r); |
| 1110 | -: 749: return scale; |
| 1111 | -: 750:} |
| 1112 | -: 751:#endif |
| 1113 | -: 752: |
| 1114 | function oracle_cm_charge called 4 returned 100% blocks executed 100% |
| 1115 | 4: 753:void rx_cm_charge(RxCostModel *m, uint64_t work_ps, uint64_t explore_ps) { |
| 1116 | -: 754:#if CUDAPP_MIGRATION_PENDING_C_CM_CALIBRATION |
| 1117 | 4: 755: m->work_ps += work_ps; |
| 1118 | 4: 756: m->explore_ps += explore_ps; |
| 1119 | -: 757:#else |
| 1120 | -: 758: uint64_t words[32], result; |
| 1121 | -: 759: words[0] = m->work_ps; words[1] = m->explore_ps; |
| 1122 | -: 760: words[2] = work_ps; words[3] = explore_ps; |
| 1123 | -: 761: if (omega_cm_calibration(&result, 4, words, 32) != OMEGA_OK) return; |
| 1124 | -: 762: m->work_ps = words[0]; m->explore_ps = words[1]; |
| 1125 | -: 763:#endif |
| 1126 | 4: 764:} |
| 1127 | -: 765: |
| 1128 | -: 766:/* ---- canonical bytes ---- */ |
| 1129 | -: 767: |
| 1130 | -: 768:#define MAGIC 0x4d43474fu /* "OGCM" little-endian */ |
| 1131 | -: 769:/* VERSION binds the feature basis and the cell layout. RX_CM_CELLS already |
| 1132 | -: 770: * includes RX_CM_OPS, so a new operation family changes the blob length and |
| 1133 | -: 771: * this header's cells field. Bump VERSION in that same change. Deserialize |
| 1134 | -: 772: * refuses a different version, basis size, cell count, arm cap or core count. */ |
| 1135 | -: 773:#define VERSION 1u |
| 1136 | -: 774: |
| 1137 | -: 775:typedef struct { |
| 1138 | -: 776: uint32_t magic, version, n_arms, d, cells, arms_cap, cores; |
| 1139 | -: 777: uint32_t reserved; |
| 1140 | -: 778: double lambda; |
| 1141 | -: 779:} Header; |
| 1142 | -: 780:_Static_assert(sizeof(Header) == 40, "cost model native blob header layout"); |
| 1143 | -: 781: |
| 1144 | -: 782:#if !CUDAPP_MIGRATION_PENDING_C_CM_BLOB |
| 1145 | -: 783:static uint64_t cm_blob_check(unsigned mode, uint64_t words[64]) { |
| 1146 | -: 784: uint64_t result; |
| 1147 | -: 785:#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 1148 | -: 786: words[63] = 1; |
| 1149 | -: 787:#else |
| 1150 | -: 788: words[63] = 0; |
| 1151 | -: 789:#endif |
| 1152 | -: 790: if (omega_cm_blob(&result, mode, words, 64)) abort(); |
| 1153 | -: 791: return result; |
| 1154 | -: 792:} |
| 1155 | -: 793:#endif |
| 1156 | -: 794: |
| 1157 | -: 795:#if CUDAPP_MIGRATION_PENDING_C_CM_DIGEST |
| 1158 | -: 796:#define cm_sha256 sha256_hash |
| 1159 | function cm_digest_equal called 43 returned 100% blocks executed 100% |
| 1160 | 43: 797:static int cm_digest_equal(const uint8_t a[32], const uint8_t b[32]) { |
| 1161 | 43: 798: return memcmp(a, b, 32) == 0; |
| 1162 | -: 799:} |
| 1163 | -: 800:#else |
| 1164 | -: 801:static uint64_t cm_digest_step(unsigned mode, uint64_t state[16], uint8_t data[128]) { |
| 1165 | -: 802: uint64_t result; |
| 1166 | -: 803: if (omega_cm_digest(&result, mode, state, 16, data, 128)) abort(); |
| 1167 | -: 804: return result; |
| 1168 | -: 805:} |
| 1169 | -: 806:static void cm_sha256(const uint8_t *data, size_t n, uint8_t out[32]) { |
| 1170 | -: 807: uint64_t state[16]; |
| 1171 | -: 808: uint8_t block[128]; |
| 1172 | -: 809: if (cm_digest_step(0, state, block)) abort(); |
| 1173 | -: 810:#if CUDAPP_MIGRATION_PENDING_C_CM_ROUTE |
| 1174 | -: 811: size_t blocks = n / 64, tail = n % 64, offset = blocks * 64; |
| 1175 | -: 812:#else |
| 1176 | -: 813: uint64_t plan[16] = {n}, result; |
| 1177 | -: 814: if (omega_cm_route(&result, 0, plan, 16) != OMEGA_OK || result != 1) abort(); |
| 1178 | -: 815: size_t blocks = (size_t)plan[1], tail = (size_t)plan[2], offset = (size_t)plan[3]; |
| 1179 | -: 816:#endif |
| 1180 | -: 817: const uint8_t *cursor = data; |
| 1181 | -: 818: for (size_t i = 0; i < blocks; ++i, cursor += 64) { |
| 1182 | -: 819: memcpy(block, cursor, 64); |
| 1183 | -: 820: if (cm_digest_step(1, state, block)) abort(); |
| 1184 | -: 821: } |
| 1185 | -: 822: state[9] = tail; |
| 1186 | -: 823: memcpy(block, data + offset, tail); |
| 1187 | -: 824: if (cm_digest_step(2, state, block)) abort(); |
| 1188 | -: 825: memcpy(out, block, 32); |
| 1189 | -: 826:} |
| 1190 | -: 827:static int cm_digest_equal(const uint8_t a[32], const uint8_t b[32]) { |
| 1191 | -: 828: uint64_t state[16]; |
| 1192 | -: 829: uint8_t data[128]; |
| 1193 | -: 830: memcpy(data, a, 32); |
| 1194 | -: 831: memcpy(data + 32, b, 32); |
| 1195 | -: 832: return (int)cm_digest_step(3, state, data); |
| 1196 | -: 833:} |
| 1197 | -: 834:#endif |
| 1198 | -: 835: |
| 1199 | function body_size called 108 returned 100% blocks executed 100% |
| 1200 | 108: 836:static size_t body_size(void) { |
| 1201 | 108: 837: const RxCostModel *m = 0; |
| 1202 | -: 838:#if CUDAPP_MIGRATION_PENDING_C_CM_ROUTE |
| 1203 | 108: 839: return sizeof m->cell + sizeof m->power_mw + sizeof m->verify_ns + sizeof m->synth_ns + |
| 1204 | -: 840: sizeof m->code_bytes + sizeof m->sd_scale; |
| 1205 | -: 841:#else |
| 1206 | -: 842: uint64_t sizes[16] = {sizeof m->cell, sizeof m->power_mw, sizeof m->verify_ns, |
| 1207 | -: 843: sizeof m->synth_ns, sizeof m->code_bytes, sizeof m->sd_scale}, result; |
| 1208 | -: 844: if (omega_cm_route(&result, 1, sizes, 16) != OMEGA_OK) abort(); |
| 1209 | -: 845: return (size_t)result; |
| 1210 | -: 846:#endif |
| 1211 | -: 847:} |
| 1212 | -: 848: |
| 1213 | function oracle_cm_blob_size called 108 returned 100% blocks executed 100% |
| 1214 | 108: 849:size_t rx_cm_blob_size(void) { |
| 1215 | -: 850:#if CUDAPP_MIGRATION_PENDING_C_CM_ROUTE |
| 1216 | 108: 851: return sizeof(Header) + body_size() + 32u; |
| 1217 | call 0 returned 108 |
| 1218 | -: 852:#else |
| 1219 | -: 853: uint64_t sizes[16] = {sizeof(Header), body_size(), 32}, result; |
| 1220 | -: 854: if (omega_cm_route(&result, 1, sizes, 16) != OMEGA_OK) abort(); |
| 1221 | -: 855: return (size_t)result; |
| 1222 | -: 856:#endif |
| 1223 | -: 857:} |
| 1224 | -: 858: |
| 1225 | function pack called 65 returned 100% blocks executed 100% |
| 1226 | 65: 859:static size_t pack(const RxCostModel *m, uint8_t *out) { |
| 1227 | -: 860: Header h; |
| 1228 | -: 861:#if CUDAPP_MIGRATION_PENDING_C_CM_BLOB |
| 1229 | 65: 862: memset(&h, 0, sizeof h); |
| 1230 | 65: 863: h.magic = MAGIC; |
| 1231 | 65: 864: h.version = VERSION; |
| 1232 | 65: 865: h.n_arms = m->n_arms; |
| 1233 | 65: 866: h.d = RX_CM_D; |
| 1234 | 65: 867: h.cells = RX_CM_CELLS; |
| 1235 | 65: 868: h.arms_cap = RX_CM_ARMS; |
| 1236 | 65: 869: h.cores = RX_CM_CORES; |
| 1237 | 65: 870: h.lambda = m->lambda; |
| 1238 | -: 871:#else |
| 1239 | -: 872: uint64_t words[64]; |
| 1240 | -: 873: words[0] = m->n_arms; |
| 1241 | -: 874: memcpy(words + 1, &m->lambda, sizeof m->lambda); |
| 1242 | -: 875: if (!cm_blob_check(0, words)) abort(); |
| 1243 | -: 876: memcpy(&h, words + 16, sizeof h); |
| 1244 | -: 877:#endif |
| 1245 | 65: 878: uint8_t *p = out; |
| 1246 | 65: 879: memcpy(p, &h, sizeof h); |
| 1247 | 65: 880: p += sizeof h; |
| 1248 | 65: 881: memcpy(p, m->cell, sizeof m->cell); |
| 1249 | 65: 882: p += sizeof m->cell; |
| 1250 | 65: 883: memcpy(p, m->power_mw, sizeof m->power_mw); |
| 1251 | 65: 884: p += sizeof m->power_mw; |
| 1252 | 65: 885: memcpy(p, m->verify_ns, sizeof m->verify_ns); |
| 1253 | 65: 886: p += sizeof m->verify_ns; |
| 1254 | 65: 887: memcpy(p, m->synth_ns, sizeof m->synth_ns); |
| 1255 | 65: 888: p += sizeof m->synth_ns; |
| 1256 | 65: 889: memcpy(p, m->code_bytes, sizeof m->code_bytes); |
| 1257 | 65: 890: p += sizeof m->code_bytes; |
| 1258 | 65: 891: memcpy(p, &m->sd_scale, sizeof m->sd_scale); |
| 1259 | 65: 892: p += sizeof m->sd_scale; |
| 1260 | 65: 893: return (size_t)(p - out); |
| 1261 | -: 894:} |
| 1262 | -: 895: |
| 1263 | function oracle_cm_serialize called 65 returned 100% blocks executed 92% |
| 1264 | 65: 896:int rx_cm_serialize(const RxCostModel *m, uint8_t *out, size_t cap, size_t *out_len) { |
| 1265 | 65*: 897: if (!m || !out) return RX_CM_ERR_ARG; |
| 1266 | branch 0 taken 65 (fallthrough) |
| 1267 | branch 1 taken 0 |
| 1268 | branch 2 taken 0 (fallthrough) |
| 1269 | branch 3 taken 65 |
| 1270 | -: 898:#if CUDAPP_MIGRATION_PENDING_C_CM_BLOB |
| 1271 | 65: 899: if (cap < rx_cm_blob_size()) return RX_CM_ERR_ARG; |
| 1272 | call 0 returned 65 |
| 1273 | branch 1 taken 11 (fallthrough) |
| 1274 | branch 2 taken 54 |
| 1275 | -: 900:#else |
| 1276 | -: 901: uint64_t words[64]; |
| 1277 | -: 902: words[0] = cap; |
| 1278 | -: 903: words[1] = rx_cm_blob_size(); |
| 1279 | -: 904: if (!cm_blob_check(3, words)) return RX_CM_ERR_ARG; |
| 1280 | -: 905:#endif |
| 1281 | 54: 906: size_t n = pack(m, out); |
| 1282 | call 0 returned 54 |
| 1283 | 54: 907: cm_sha256(out, n, out + n); |
| 1284 | call 0 returned 54 |
| 1285 | 54: 908: if (out_len) *out_len = (size_t)cm_route2(3, n, 32); |
| 1286 | branch 0 taken 54 (fallthrough) |
| 1287 | branch 1 taken 0 |
| 1288 | call 2 returned 54 |
| 1289 | 54: 909: return RX_CM_OK; |
| 1290 | -: 910:} |
| 1291 | -: 911: |
| 1292 | function oracle_cm_deserialize called 43 returned 100% blocks executed 88% |
| 1293 | 43: 912:int rx_cm_deserialize(RxCostModel *m, const uint8_t *in, size_t len) { |
| 1294 | 43*: 913: if (!m || !in) return RX_CM_ERR_FORMAT; |
| 1295 | branch 0 taken 43 (fallthrough) |
| 1296 | branch 1 taken 0 |
| 1297 | branch 2 taken 0 (fallthrough) |
| 1298 | branch 3 taken 43 |
| 1299 | -: 914:#if CUDAPP_MIGRATION_PENDING_C_CM_BLOB |
| 1300 | 43*: 915: if (len != rx_cm_blob_size()) return RX_CM_ERR_FORMAT; |
| 1301 | call 0 returned 43 |
| 1302 | branch 1 taken 0 (fallthrough) |
| 1303 | branch 2 taken 43 |
| 1304 | -: 916:#else |
| 1305 | -: 917: uint64_t words[64]; |
| 1306 | -: 918: words[0] = len; |
| 1307 | -: 919: words[1] = rx_cm_blob_size(); |
| 1308 | -: 920: if (!cm_blob_check(4, words)) return RX_CM_ERR_FORMAT; |
| 1309 | -: 921:#endif |
| 1310 | -: 922: uint8_t d[32]; |
| 1311 | 43: 923: size_t payload = (size_t)cm_route2(4, len, 32); |
| 1312 | call 0 returned 43 |
| 1313 | 43: 924: cm_sha256(in, payload, d); |
| 1314 | call 0 returned 43 |
| 1315 | 43: 925: if (!cm_digest_equal(d, in + payload)) return RX_CM_ERR_DIGEST; |
| 1316 | call 0 returned 43 |
| 1317 | branch 1 taken 32 (fallthrough) |
| 1318 | branch 2 taken 11 |
| 1319 | -: 926: Header h; |
| 1320 | 11: 927: memcpy(&h, in, sizeof h); |
| 1321 | -: 928:#if CUDAPP_MIGRATION_PENDING_C_CM_BLOB |
| 1322 | 11: 929: if (h.magic != MAGIC || h.version != VERSION || h.d != RX_CM_D || h.cells != RX_CM_CELLS || |
| 1323 | branch 0 taken 11 (fallthrough) |
| 1324 | branch 1 taken 0 |
| 1325 | branch 2 taken 11 (fallthrough) |
| 1326 | branch 3 taken 0 |
| 1327 | branch 4 taken 11 (fallthrough) |
| 1328 | branch 5 taken 0 |
| 1329 | branch 6 taken 11 (fallthrough) |
| 1330 | branch 7 taken 0 |
| 1331 | 11: 930: h.arms_cap != RX_CM_ARMS || h.cores != RX_CM_CORES || h.n_arms == 0 || |
| 1332 | branch 0 taken 11 (fallthrough) |
| 1333 | branch 1 taken 0 |
| 1334 | branch 2 taken 11 (fallthrough) |
| 1335 | branch 3 taken 0 |
| 1336 | branch 4 taken 10 (fallthrough) |
| 1337 | branch 5 taken 1 |
| 1338 | 10: 931: h.n_arms > RX_CM_ARMS) |
| 1339 | branch 0 taken 0 (fallthrough) |
| 1340 | branch 1 taken 10 |
| 1341 | 1: 932: return RX_CM_ERR_FORMAT; |
| 1342 | -: 933:#else |
| 1343 | -: 934: memcpy(words + 16, &h, sizeof h); |
| 1344 | -: 935: uint64_t n_arms = cm_blob_check(1, words); |
| 1345 | -: 936: if (!n_arms) return RX_CM_ERR_FORMAT; |
| 1346 | -: 937:#endif |
| 1347 | -: 938: RxCostModel t; |
| 1348 | 10: 939: memset(&t, 0, sizeof t); |
| 1349 | -: 940:#if CUDAPP_MIGRATION_PENDING_C_CM_BLOB |
| 1350 | 10: 941: t.n_arms = h.n_arms; |
| 1351 | 10: 942: t.lambda = h.lambda; |
| 1352 | -: 943:#else |
| 1353 | -: 944: t.n_arms = (unsigned)n_arms; |
| 1354 | -: 945: memcpy(&t.lambda, words + 20, sizeof t.lambda); |
| 1355 | -: 946:#endif |
| 1356 | 10: 947: const uint8_t *p = in + sizeof h; |
| 1357 | 10: 948: memcpy(t.cell, p, sizeof t.cell); |
| 1358 | 10: 949: p += sizeof t.cell; |
| 1359 | 10: 950: memcpy(t.power_mw, p, sizeof t.power_mw); |
| 1360 | 10: 951: p += sizeof t.power_mw; |
| 1361 | 10: 952: memcpy(t.verify_ns, p, sizeof t.verify_ns); |
| 1362 | 10: 953: p += sizeof t.verify_ns; |
| 1363 | 10: 954: memcpy(t.synth_ns, p, sizeof t.synth_ns); |
| 1364 | 10: 955: p += sizeof t.synth_ns; |
| 1365 | 10: 956: memcpy(t.code_bytes, p, sizeof t.code_bytes); |
| 1366 | 10: 957: p += sizeof t.code_bytes; |
| 1367 | 10: 958: memcpy(&t.sd_scale, p, sizeof t.sd_scale); |
| 1368 | -: 959:#if CUDAPP_MIGRATION_PENDING_C_CM_BLOB |
| 1369 | 10*: 960: if (!(t.sd_scale >= 1.0 && t.sd_scale < 1e6)) return RX_CM_ERR_FORMAT; |
| 1370 | branch 0 taken 10 (fallthrough) |
| 1371 | branch 1 taken 0 |
| 1372 | branch 2 taken 0 (fallthrough) |
| 1373 | branch 3 taken 10 |
| 1374 | -: 961:#else |
| 1375 | -: 962: memcpy(words, &t.sd_scale, sizeof t.sd_scale); |
| 1376 | -: 963: if (!cm_blob_check(2, words)) return RX_CM_ERR_FORMAT; |
| 1377 | -: 964:#endif |
| 1378 | 10: 965: *m = t; |
| 1379 | 10: 966: return RX_CM_OK; |
| 1380 | -: 967:} |
| 1381 | -: 968: |
| 1382 | function oracle_cm_digest called 11 returned 100% blocks executed 100% |
| 1383 | 11: 969:void rx_cm_digest(const RxCostModel *m, uint8_t out[32]) { |
| 1384 | -: 970: uint8_t buf[sizeof(Header) + sizeof(((RxCostModel *)0)->cell) + 4096]; |
| 1385 | 11: 971: size_t n = pack(m, buf); |
| 1386 | call 0 returned 11 |
| 1387 | 11: 972: cm_sha256(buf, n, out); |
| 1388 | call 0 returned 11 |
| 1389 | 11: 973:} |