--- jtwin.c 2026-09-21 22:02:57 +++ jtwin_hb2.c 2026-09-23 17:50:27 @@ -68,6 +68,7 @@ } Ctx; static Ctx base; +static volatile int g_stop; /* set the moment a witness is verified: the verdict is decided */ static void init_primes(int n) { @@ -192,6 +193,7 @@ /* primes assigned in increasing order; k = index of the next free prime */ static int dfs_ord(Ctx *c, int k) { + if (g_stop) { c->fails++; return 0; } /* a witness is in hand; this branch cannot matter */ c->nodes++; if (c->uncov == 0) { for (int i = 0; i < NP; i++) c->sol[i] = c->asg[i]; return 1; } if (k >= NP) { c->fails++; return 0; } @@ -233,11 +235,55 @@ static pthread_mutex_t g_mtx = PTHREAD_MUTEX_INITIALIZER; static long long g_nodes, g_fails; -static void *worker(void *arg) +/* ---- heartbeat instrumentation (this file only; jtwin.c is untouched) ---- + * A detached monitor thread reports the engine's OWN node counters, so a long refutation search is + * observable instead of silent. It reads each worker's Ctx->nodes (a benign race, a progress + * figure only) and never touches the search itself. */ +static Ctx *g_ctx[64]; +static volatile int g_ctx_live[64]; +static volatile int g_mon_stop, g_mon_gen; +static int g_hb_threads, g_hb_R; + +static void *hb_monitor(void *arg) { (void)arg; + int myR = g_hb_R, myGen = g_mon_gen, wpref = -1; + double next = 10.0; + struct timespec t0, t1, s = { 1, 0 }; + clock_gettime(CLOCK_MONOTONIC, &t0); + for (;;) { + nanosleep(&s, NULL); + if (g_mon_stop || myGen != g_mon_gen) break; + clock_gettime(CLOCK_MONOTONIC, &t1); + double el = (t1.tv_sec - t0.tv_sec) + (t1.tv_nsec - t0.tv_nsec) / 1e9; + if (el >= next) { + long long total = 0; int live = 0; + for (int i = 0; i < g_hb_threads && i < 64; i++) + if (g_ctx_live[i] && g_ctx[i]) { total += g_ctx[i]->nodes; live++; } + fprintf(stderr, " HEARTBEAT R=%d t=%.0fs nodes=%lld rate=%.3g/s live=%d/%d branches=%d/%d found=%d\n", + myR, el, total, total / el, live, g_hb_threads, g_next, g_np0, g_found); + if (g_found) { + if (wpref < 0 && pthread_mutex_trylock(&g_mtx) == 0) { + wpref = covered_prefix((const int *)g_sol, MAXR - 1); + pthread_mutex_unlock(&g_mtx); + } + if (wpref >= 0) + fprintf(stderr, " WITNESS-STATE R=%d prefix=%d => A144311(%d) >= %d\n", + myR, wpref, 23, 6 * wpref + 5); + } + fflush(stderr); + next = el + 60.0; + } + } + return NULL; +} + +static void *worker(void *arg) +{ + int id = (int)(long)arg; Ctx *c = (Ctx *)malloc(sizeof(Ctx)); long long ln = 0, lf = 0; + if (id >= 0 && id < 64) { g_ctx[id] = c; g_ctx_live[id] = 1; } for (;;) { pthread_mutex_lock(&g_mtx); if (g_found || g_next >= g_np0) { pthread_mutex_unlock(&g_mtx); break; } @@ -250,7 +296,17 @@ if (!verify_solution(c->sol, c->R)) fprintf(stderr, "SELF-CHECK FAILED at R=%d branch %d\n", c->R, b); pthread_mutex_lock(&g_mtx); - if (!g_found) { g_found = 1; memcpy((void *)g_sol, c->sol, sizeof(g_sol)); } + if (!g_found) { + g_found = 1; g_stop = 1; + memcpy((void *)g_sol, c->sol, sizeof(g_sol)); + int pref = covered_prefix(c->sol, MAXR - 1); + fprintf(stderr, " WITNESS R=%d branch=%d prefix=%d => A144311(%d) >= %d , G_2(%d#) >= %d\n", + c->R, b, pref, NP + 2, 6 * pref + 5, pl[NP - 1], 6 * pref + 6); + fprintf(stderr, " WITNESS certificate a_p:"); + for (int i = 0; i < NP; i++) fprintf(stderr, " %d/%d", c->sol[i], pl[i]); + fprintf(stderr, "\n"); + fflush(stderr); + } pthread_mutex_unlock(&g_mtx); } ln += c->nodes; lf += c->fails; @@ -258,6 +314,7 @@ pthread_mutex_lock(&g_mtx); g_nodes += ln; g_fails += lf; pthread_mutex_unlock(&g_mtx); + if (id >= 0 && id < 64) g_ctx_live[id] = 0; free(c); return NULL; } @@ -270,12 +327,17 @@ g_np0 = 0; for (int r0 = 0; r0 < pl[0]; r0++) for (int r1 = 0; r1 < pl[1]; r1++) { g_bP0[g_np0] = r0; g_bP1[g_np0] = r1; g_np0++; } - g_next = 0; g_found = 0; g_nodes = g_fails = 0; + g_next = 0; g_found = 0; g_stop = 0; g_nodes = g_fails = 0; if (nthreads < 1) nthreads = 1; if (nthreads > g_np0) nthreads = g_np0; - pthread_t th[64]; - for (int i = 0; i < nthreads; i++) pthread_create(&th[i], NULL, worker, NULL); + g_hb_R = R; g_hb_threads = nthreads; g_mon_stop = 0; g_mon_gen++; + fprintf(stderr, " RUNG start R=%d threads=%d branches=%d\n", R, nthreads, g_np0); + fflush(stderr); + pthread_t th[64], mon; + if (pthread_create(&mon, NULL, hb_monitor, NULL) == 0) pthread_detach(mon); + for (int i = 0; i < nthreads; i++) pthread_create(&th[i], NULL, worker, (void *)(long)i); for (int i = 0; i < nthreads; i++) pthread_join(th[i], NULL); + g_mon_stop = 1; if (sol_out) memcpy(sol_out, (const void *)g_sol, sizeof(g_sol)); if (nodes_out) *nodes_out = g_nodes; if (fails_out) *fails_out = g_fails;