Files
2026-07-16 11:57:36 +07:00

140 lines
8.4 KiB
Python

#!/usr/bin/env python3
"""Add perturb_stress command handler + main loop dispatch to observer .cu"""
path = '/mnt/d/resonance-engine-active/cuda/khra_gixx_1024_v5_observer.cu'
with open(path, 'r', encoding='utf-8') as f:
lines = f.readlines()
# 1. Add perturb_stress state after inject_density state
# Find: "static unsigned long long total_injections = 0;"
for i, line in enumerate(lines):
if 'static unsigned long long total_injections = 0;' in line:
lines.insert(i + 1, '// perturb_stress state (same targeting, separate dispatch)\n')
lines.insert(i + 2, 'static float pstress_sigma = 16.0f, pstress_strength = 0.1f;\n')
lines.insert(i + 3, 'static float pstress_cx = 0.0f, pstress_cy = 0.0f;\n')
lines.insert(i + 4, 'static unsigned long long total_stress_perts = 0;\n')
lines.insert(i + 5, '\n')
print("Added perturb_stress state variables")
break
# 2. Update handle_command comment to include perturb_stress return code
for i, line in enumerate(lines):
if '// 6=inject_density, 7=stress_snapshot_now, 0=handled/noop' in line:
lines[i] = '// 6=inject_density, 7=stress_snapshot_now, 8=health_check,\n'
lines.insert(i + 1, '// 9=perturb_stress, 0=handled/noop\n')
print("Updated return code comment")
break
# 3. Add perturb_stress command handler after the stress_snapshot_now handler
# Find: } else if (strncmp(cmd_start, "stress_snapshot_now", 19) == 0) {
for i, line in enumerate(lines):
if 'else if (strncmp(cmd_start, "stress_snapshot_now", 19) == 0)' in line:
# Find the closing of this block and the next else if
# This block ends with return 7; then next is } else if for health_check
# We'll insert AFTER the stress_snapshot_now block and BEFORE health_check
# Find the "return 7;" line
for j in range(i, len(lines)):
if 'return 7;' in lines[j]:
# Insert after this block (after " return 7;\n }")
# Need to find the closing brace
for k in range(j, len(lines)):
if lines[k].strip() == '} else if (strncmp(cmd_start, "health_check", 12) == 0) {':
# Insert before this line
perturb_handler = [
' // === perturb_stress: write into non-equilibrium stress moment ===\n',
' } else if (strncmp(cmd_start, "perturb_stress", 14) == 0) {\n',
' strncpy(last_cmd_name, "perturb_stress", sizeof(last_cmd_name) - 1);\n',
' const char* vx = strstr(msg, "\\"x\\":");\n',
' const char* vy = strstr(msg, "\\"y\\":");\n',
' if (vx && vy) {\n',
' pstress_cx = strtof(vx + 4, NULL);\n',
' pstress_cy = strtof(vy + 4, NULL);\n',
' if (pstress_cx < 0.0f) pstress_cx = 0.0f;\n',
' if (pstress_cx >= (float)NX) pstress_cx = (float)(NX - 1);\n',
' if (pstress_cy < 0.0f) pstress_cy = 0.0f;\n',
' if (pstress_cy >= (float)NY) pstress_cy = (float)(NY - 1);\n',
' const char* vs = strstr(msg, "\\"sigma\\":");\n',
' if (vs) {\n',
' float s = strtof(vs + 8, NULL);\n',
' if (s >= 1.0f && s <= 256.0f) pstress_sigma = s;\n',
' }\n',
' const char* vst = strstr(msg, "\\"strength\\":");\n',
' if (vst) {\n',
' float st = strtof(vst + 11, NULL);\n',
' if (st >= -1.0f && st <= 1.0f) pstress_strength = st;\n',
' }\n',
' printf("[CMD] perturb_stress at (%.1f, %.1f) sigma=%.1f strength=%.4f\\n",\n',
' pstress_cx, pstress_cy, pstress_sigma, pstress_strength);\n',
' fflush(stdout);\n',
' return 9;\n',
' } else {\n',
' fprintf(stderr, "[CMD] perturb_stress requires \\"x\\" and \\"y\\" fields\\n");\n',
' fflush(stderr);\n',
' }\n',
]
for idx, pl in enumerate(perturb_handler):
lines.insert(k + idx, pl)
print(f"Added perturb_stress handler at line {k+1}")
break
break
break
# 4. Add main loop dispatch for return code 9 (perturb_stress)
# Find: } else if (cmd_result == 8) {
for i, line in enumerate(lines):
if '} else if (cmd_result == 8) {' in line:
# Find the closing of this block
for j in range(i, len(lines)):
if lines[j].strip() == ' }' and 'cmd_result' in lines[j-3] if j >= 3 else False:
pass
# Better: find the next "} else if (cmd_result ==" or the ack section
if '// v4: Publish ACK on port 5559' in lines[j]:
# Insert before this
dispatch = [
' } else if (cmd_result == 9) {\n',
' // perturb_stress -- launch kernel on current distribution\n',
' total_stress_perts++;\n',
' perturb_stress_kernel<<<grid, block>>>(d_f[current],\n',
' pstress_cx, pstress_cy, pstress_sigma, pstress_strength);\n',
' CUDA_CHECK(cudaDeviceSynchronize());\n',
' printf("[v5] perturb_stress #%llu applied at (%.1f,%.1f) sigma=%.1f str=%.4f\\n",\n',
' total_stress_perts, pstress_cx, pstress_cy, pstress_sigma, pstress_strength);\n',
' fflush(stdout);\n',
' char pert_msg[512];\n',
' snprintf(pert_msg, sizeof(pert_msg),\n',
' "{\\"perturb_id\\":%llu,\\"cycle\\":%d,\\"x\\":%.1f,\\"y\\":%.1f,\\"sigma\\":%.1f,\\"strength\\":%.4f}",\n',
' total_stress_perts, cycle, pstress_cx, pstress_cy, pstress_sigma, pstress_strength);\n',
' zmq_send_resilient(&ack_pub, zmq_ctx, "tcp://127.0.0.1:5559",\n',
' ZMQ_PUB, 1, pert_msg, strlen(pert_msg), 0, &ack_fail_count);\n',
]
for idx, dl in enumerate(dispatch):
lines.insert(j + idx, dl)
print(f"Added main loop dispatch at line {j+1}")
# Also update the ack exclude list to include cmd_result 9
for k in range(j + len(dispatch), len(lines)):
if 'cmd_result != 6 && cmd_result != 8' in lines[k]:
lines[k] = lines[k].replace('cmd_result != 6 && cmd_result != 8',
'cmd_result != 6 && cmd_result != 8 && cmd_result != 9')
print("Updated ACK exclusion list")
break
break
break
# 5. Update startup banner to mention perturb_stress
for i, line in enumerate(lines):
if 'stress_snapshot_now, health_check' in line:
lines[i] = lines[i].replace('stress_snapshot_now, health_check',
'stress_snapshot_now, health_check, perturb_stress')
print("Updated startup banner")
break
with open(path, 'w', encoding='utf-8') as f:
f.writelines(lines)
print("All edits complete. Verifying...")
with open(path, 'r', encoding='utf-8') as f:
content = f.read()
for keyword in ['perturb_stress', 'pstress_cx', 'cmd_result == 9', 'total_stress_perts']:
if keyword in content:
print(f" [OK] '{keyword}' found")
else:
print(f" [FAIL] '{keyword}' NOT found!")