/* fprofile.c -- job #4128 (route 2): min over all starts of the first-order bound F, for * EVERY length L at once. * * F(a,L) = N(a,L) - sum_i max_t |A_i(t;L)| (the served lossscan762.c convention), where * N is the support size and |A_i(t)| = #{support s : (s+t) mod q_i in {0, q_i-2}}. * * The support grows with L, so one pass per start yields F(a,L) for all L: maintain the * residue-class counts c_i[.] of the support (mod q_i) and the pair sums * |A_i(t)| = c_i[(-t) mod q_i] + c_i[(-t-2) mod q_i]; adding a support point s only * changes the two t whose pair contains s mod q_i. Accumulate the running min over * starts into minF[L]. * * Usage: fprofile W Lmax q0 q1 q2 q3 [AEND] * JSON on stdout (minF array over L = 0..Lmax), timing on stderr. */ #include #include #include #include #include #ifdef _OPENMP #include #endif #define MAXQ 64 #define MAXL 20000 static unsigned char *slot2; static int Qg[4]; static int gcd_int(int a, int b) { while (b) { int r = a % b; a = b; b = r; } return a; } int main(int argc, char **argv) { if (argc < 7) { fprintf(stderr, "usage: fprofile W Lmax q0 q1 q2 q3 [AEND]\n"); return 2; } int W = atoi(argv[1]), Lmax = atoi(argv[2]), aend = W; for (int i = 0; i < 4; i++) Qg[i] = atoi(argv[3 + i]); if (argc > 7) aend = atoi(argv[7]); if (aend > W) aend = W; if (Lmax > MAXL || Lmax > W) { fprintf(stderr, "Lmax out of range\n"); return 2; } clock_t c0 = clock(); /* per-s update tables: residue and the two affected phase pairs, per prime */ static int C[MAXL + 1][4], T1[MAXL + 1][4], T2[MAXL + 1][4]; for (int s = 1; s <= Lmax; s++) for (int i = 0; i < 4; i++) { int q = Qg[i], c = s % q; C[s][i] = c; T1[s][i] = (q - c) % q; T2[s][i] = (q - 2 - c + q) % q; } slot2 = malloc((size_t)2 * W); if (!slot2) { fprintf(stderr, "alloc slot2 failed\n"); return 4; } for (int r = 0; r < 2 * W; r++) { int r2 = r + 2; if (r2 >= W) r2 %= W; slot2[r] = (unsigned char)(gcd_int(r, W) == 1 && gcd_int(r2, W) == 1); } int nthreads = 1; #ifdef _OPENMP nthreads = omp_get_max_threads(); #endif int *minF = malloc(sizeof(int) * (size_t)(Lmax + 1) * (size_t)nthreads); for (size_t i = 0; i < (size_t)(Lmax + 1) * nthreads; i++) minF[i] = 1 << 29; fprintf(stderr, "[info] W=%d Lmax=%d threads=%d\n", W, Lmax, nthreads); #pragma omp parallel { int tid = 0; #ifdef _OPENMP tid = omp_get_thread_num(); #endif int *my = minF + (size_t)tid * (Lmax + 1); int cnt[4][MAXQ], val[4][MAXQ], mx[4]; int a; #pragma omp for schedule(dynamic, 1024) for (a = 0; a < aend; a++) { memset(cnt, 0, sizeof cnt); memset(val, 0, sizeof val); mx[0] = mx[1] = mx[2] = mx[3] = 0; int N = 0, F = 0; if (0 < my[0]) my[0] = 0; const unsigned char *sl = slot2 + a; for (int s = 1; s <= Lmax; s++) { if (sl[s]) { N++; for (int i = 0; i < 4; i++) { int c = C[s][i], v1, v2; cnt[i][c]++; v1 = ++val[i][T1[s][i]]; v2 = ++val[i][T2[s][i]]; if (v1 > mx[i]) mx[i] = v1; if (v2 > mx[i]) mx[i] = v2; } F = N - (mx[0] + mx[1] + mx[2] + mx[3]); } if (F < my[s]) my[s] = F; } } } /* merge threads */ int firstpos = -1; printf("{\"W\":%d,\"Lmax\":%d,\"AEND\":%d,\"q\":[%d,%d,%d,%d],\"minF\":[", W, Lmax, aend, Qg[0], Qg[1], Qg[2], Qg[3]); for (int L = 0; L <= Lmax; L++) { int m = 1 << 29; for (int t = 0; t < nthreads; t++) { int v = minF[(size_t)t * (Lmax + 1) + L]; if (v < m) m = v; } printf("%s%d", L ? "," : "", m); if (firstpos < 0 && m >= 1) firstpos = L; } printf("],\"first_L_minF_ge_1\":%d,\"minF_last\":%d}\n", firstpos, minF[(size_t)(Lmax)]); fprintf(stderr, "[timing] W=%d Lmax=%d cpu=%.2fs\n", W, Lmax, (double)(clock() - c0) / CLOCKS_PER_SEC); return 0; }