Skip to content

Commit b90d59c

Browse files
committed
make statefiles and change of ncpus robust
1 parent e2ccb20 commit b90d59c

13 files changed

+131
-136
lines changed

Legal.hs

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1+
module Legal where
12
import Data.Array
23
import Data.List
34
import Data.Char
45
import Numeric
6+
import Data.FixedPoint
57

6-
alpha = 0.850639925845714538
8+
abls :: [(Int,FixedPoint10241024,FixedPoint10241024,FixedPoint10241024)]
9+
abls = abl 1 squarish where
10+
abl n (nn:nn1:abl1@(n1n1:_)) = (n,a,b,l) : abl (n+1) abl1 where
11+
l = fromIntegral nn * fromIntegral n1n1 / (fromIntegral nn1)^2
12+
b = fromIntegral nn1 / (fromIntegral nn * l^n)
13+
a = fromIntegral nn / (b^(2*n) * l^(n^2))
14+
abl _ _ = []
15+
16+
main = do
17+
mapM_ print abls
18+
19+
alpha = 0.8506399258457126148173084786343
720
logalpha = logBase 10 alpha
821

9-
beta = 0.96553505933837387
22+
beta = 0.96553505933837393063017090430952
1023
logbeta = logBase 10 beta
1124

12-
lambda = 2.97573419204335724938
25+
lambda = 2.97573419204335724938110448420706
1326
loglambda = logBase 10 lambda
1427

1528
legal m n = alpha * beta**(m+n) * lambda**(m*n)
@@ -32,3 +45,6 @@ legal2 = recurrence [-1,2,20,-13,31,-16,10] [1,5,57,489,4125,35117,299681]
3245
legal3 = recurrence [-5,73,100,-1402,1014,-5352,-2490,6018,-4020,1766,9083,-19993,22072,-16646,9426,-3750,1171,-233,33] [1,15,489,12675,321689,8180343,208144601,5296282323,134764135265,3429075477543,87252874774409,2220150677358587,56491766630430761,1437433832683612783,36575525011037967769,930664771769562054147,23680778803620700205625,602557764897193682879119,15332091188757329557096929]
3346

3447
ternary n = showIntAtBase 3 intToDigit n ""
48+
49+
50+
squarish = [1,5,57,489,12675,321689,24318165,1840058693,414295148741,93332304864173,62567386502084877,41945191530093646965,83677847847984287628595,166931297609667912727898521,990966953618170260281935463385,5882748866432370655674372752123193,103919148791293834318983090438798793469,1835738613899845421140262364853644706891109,96498428501909654589630887978835098088148177857,5072588588647327658457862518216696854885169490987149,793474866816582266820936671790189132321673383112185151899,124118554774307129694783556890846966815009879092863579679259393,57774258489513238998237970307483999327287210756991189655942651331169,26892554058860272116972562366415920138007095980551558908000982332405743333,37249792307686396442294904767024517674249157948208717533254799550970595875237705,51595955665685681166597566866805181435596339502695699293823422273656970477373415200373,212667732900366224249789357650440598098805861083269127196623872213228196352455447575029701325,876571894704740435776253865561651594678577903166188258472955681125289495868953613359454403019145877,10751464308361383118768413754866123809733788820327844402764601662870883601711298309339239868998337801509491,131870512244645759297888472329947870580266256924485681728458086578687538959472921550847035733890182662513180743513,4813066963822755416429056022484299646486874100967249263944719599975607459850502222039591149331431805524655467453067042377,175669398745227675074920433327787366374551886098434473836510647159450821039563378374569811240252763776406712988379278644250456677,19079388919628199204605726181850465220151058338147922243967269231944059187214767997105992341735209230667288462179090073659712583262087437,2072205427619023303039587520236390121754274072718784609433998196933282608067036314403465202963700297341152216286750576593627459392979397487964077,669723114288829212892740188841706543509937780640178732810318337696945624428547218105214326012774371397184848890970111836283470468812827907149926502347633,216450089279078275314395453480468424469694873576469893709517750563261490751122922463339745178577954008324586419548071995019779454584564790800309660950831580481393]

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ GFLAGS = -Wall -g -m64
44

55
all: start legal legalg tar NIC
66

7-
start: start.c states.c modulus.h Makefile
8-
cc $(CFLAGS) -o start start.c states.c
7+
start: start.c outstream.c modadd.h modulus.h partition.c partition.h states.c states.h sortstate.c sortstate.h Makefile
8+
cc $(CFLAGS) -o start start.c states.c sortstate.c outstream.c partition.c modadd.c
99

1010
instream: instream.c instream.h modadd.h modulus.h Makefile
1111
cc -DMAININSTREAM $(CFLAGS) -o instream instream.c modadd.c

counts

+4
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,7 @@ S3 1
244244
N1 2
245245

246246
:'a,'bs/2/O /g
247+
248+
0 0 0 0 2 2 2 0 1 1 0 0 2 0 1 1 1 0 1
249+
2 1 1 2 1 2 0 0 2 1 2 1
250+

gocheck

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ while(<>) {
3939
my $prevnlstates = 1;
4040
my $prevparts = 1;
4141
for my $yx (sort keys %cell) {
42+
last unless $cell{$yx}->{size};
4243
my $nlstates = $cell{$yx}->{needy}+$cell{$yx}->{legal};
4344
my $nstates = $cell{$yx}->{newill} + $nlstates;
4445
my $diff = abs(($nstates - 3*$prevnlstates) % $mod);

instream.c

+43-23
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33
#include <stdlib.h>
44
#include <stdint.h>
55
#include <string.h>
6+
#include <strings.h>
7+
#include <glob.h>
68
#include "instream.h"
79
#include "modadd.h"
810

911
#define MAXSTREAMS 1024
10-
#ifndef FANIN
11-
#define FANIN 1
12-
#endif
1312

1413
struct goin {
1514
uint64_t modulus;
1615
uint64_t totalin;
1716
statebuf stream[MAXSTREAMS];
1817
statebuf *heap[MAXSTREAMS];
1918
int nstreams;
20-
int ncpus;
19+
int incpus;
2120
int cpuid;
2221
};
2322

@@ -126,39 +125,59 @@ void deletemin(goin *gin)
126125
hpreplace(gin, 0);
127126
}
128127

129-
goin *openstreams(char *inbase, int ncpus, int cpuid, uint64_t modulus) {
130-
128+
goin *openstreams(char *inbase, int incpus, int ncpus, int cpuid, uint64_t modulus) {
131129
char inname[FILENAMELEN];
132130
statebuf *sb;
133131
FILE *fp;
134-
int from,to,j;
132+
int from,to,j,nscan;
135133
goin *gin;
134+
glob_t stateglob;
135+
uint64_t prevstate,state;
136136

137137
gin = (goin *)calloc(1,sizeof(goin));
138138
if (gin == NULL) {
139139
printf ("Failed to allocate goin record of size %d\n", (int)sizeof(goin));
140140
exit(1);
141141
}
142142
gin->totalin = 0LL;
143-
gin->ncpus = ncpus;
143+
gin->incpus = incpus;
144144
gin->cpuid = cpuid;
145145
gin->modulus = modulus;
146146
sb = &gin->stream[gin->nstreams = 0];
147-
for (from=0; from<FANIN*ncpus; from++) {
148-
for (to=cpuid; to<FANIN*ncpus; to+=ncpus) {
149-
for (j=0; ; j++) {
150-
sprintf(inname,"%s/fromto.%d.%d/%d",inbase,from,to,j);
151-
if (!(fp = fopen(inname, "r"))) {
152-
// printf("Failed to open %s\n",inname);
153-
break;
147+
int limto = incpus < ncpus ? ncpus : incpus;
148+
for (from=0; from<incpus; from++) {
149+
for (to=cpuid; to<limto; to+=ncpus) {
150+
prevstate = 0L;
151+
for (j=0; prevstate!=FINALSTATE; j++) {
152+
sprintf(inname,"%s/fromto.%d.%d/%d.*",inbase,from,to,j);
153+
stateglob.gl_matchc = 2;
154+
glob(inname, GLOB_LIMIT, NULL, &stateglob);
155+
if (stateglob.gl_matchc != 1) {
156+
printf ("%d files matching %s\n", stateglob.gl_matchc, inname);
157+
exit(1);
158+
}
159+
if (!(fp = fopen(stateglob.gl_pathv[0], "r"))) {
160+
printf("Failed to open %s\n",stateglob.gl_pathv[0]);
161+
exit(1);
154162
}
155163
if (gin->nstreams == MAXSTREAMS) {
156164
printf ("#inputfiles exceeds MAXSTREAMS (%d)\n", MAXSTREAMS);
157165
exit(1);
158166
}
159-
// printf("opened %s\n", inname);
167+
nscan = sscanf(rindex(stateglob.gl_pathv[0],'.')+1, "%lo", &state);
168+
if (nscan != 1) {
169+
printf ("No state suffix in %s\n", stateglob.gl_matchc);
170+
exit(1);
171+
}
172+
if (state <= prevstate) {
173+
printf ("States %lo %lo out of order.\n", prevstate, state);
174+
exit(1);
175+
}
176+
prevstate = state;
177+
// printf("opened %s state %lo\n", stateglob.gl_pathv[0],state);
160178
sb->fp = fp;
161-
strncpy(sb->fname, inname, FILENAMELEN);
179+
strncpy(sb->fname, stateglob.gl_pathv[0], FILENAMELEN);
180+
globfree(&stateglob);
162181
hpinsert(gin, gin->nstreams++, sb++);
163182
}
164183
}
@@ -178,10 +197,10 @@ int main(int argc, char *argv[])
178197
char inbase[64];
179198
statebuf *mb;
180199
goin gin;
181-
int ncpus, cpuid;
200+
int incpus, ncpus, cpuid;
182201

183-
if (argc!=7) {
184-
fprintf(stderr, "usage: %s width imod y x ncpus cpuid\n", argv[0]);
202+
if (argc!=8) {
203+
fprintf(stderr, "usage: %s width imod y x incpus ncpus cpuid\n", argv[0]);
185204
exit(1);
186205
}
187206
wd = atoi(argv[1]);
@@ -193,19 +212,20 @@ int main(int argc, char *argv[])
193212
modulus = -(uint64_t)modulusdeltas[modidx];
194213
y = atoi(argv[3]);
195214
x = atoi(argv[4]);
196-
ncpus = atoi(argv[5]);
215+
incpus = atoi(argv[5]);
216+
ncpus = atoi(argv[6]);
197217
if (ncpus < 1 || ncpus > MAXCPUS) {
198218
fprintf (stderr, "#cpus %d not in range [0,%d]\n", ncpus, MAXCPUS);
199219
exit(1);
200220
}
201-
cpuid = atoi(argv[6]);
221+
cpuid = atoi(argv[7]);
202222
if (cpuid < 0 || cpuid >= ncpus) {
203223
fprintf (stderr, "cpuid %d not in range [0,%d]\n", ncpus, ncpus-1);
204224
exit(1);
205225
}
206226

207227
sprintf(inbase,"%d.%d/yx.%02d.%02d",wd,modidx,y,x);
208-
gin = openstreams(inbase, ncpus, cpuid, modulus))
228+
gin = openstreams(inbase, incpus, ncpus, cpuid, modulus))
209229
if (!nstreams(gin))
210230
fprintf (stderr, "wanring: no input files\n");
211231
for (nin=0LL; (mb = minstream(gin))->state != FINALSTATE; nin++) {

instream.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ uint64_t nstreams(goin *gin);
1818
uint64_t totalread(goin *gin);
1919
void deletemin(goin *gin);
2020
statebuf *minstream(goin *gin);
21-
goin *openstreams(char *inbase, int ncpus, int cpuid, uint64_t modulus);
21+
goin *openstreams(char *inbase, int incpus, int ncpus, int cpuid, uint64_t modulus);

legal.c

+24-26
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
int main(int argc, char *argv[])
1717
{
18-
int ncpus,cpuid,modidx,width,y,x,nextx,tsizelen;
18+
int incpus,ncpus,cpuid,modidx,width,y,x,nextx,tsizelen;
1919
uint64_t msize,modulus,nin;
2020
char c,*tsizearg,inbase[64],outbase[64];
2121
uint64_t nnewillcnt, newstates[3];
@@ -27,8 +27,8 @@ int main(int argc, char *argv[])
2727
goout *go;
2828

2929
assert(sizeof(uint64_t)==8);
30-
if (argc!=8) {
31-
printf("usage: %s width modulo_index y x ncpus cpuid maxtreesize[kKmMgG]\n",argv[0]);
30+
if (argc!=9) {
31+
printf("usage: %s width modulo_index y x incpus ncpus cpuid maxtreesize[kKmMgG]\n",argv[0]);
3232
exit(1);
3333
}
3434
setwidth(width = atoi(argv[1]));
@@ -41,17 +41,18 @@ int main(int argc, char *argv[])
4141
y = atoi(argv[3]);
4242
x = atoi(argv[4]);
4343
nextx = (x+1) % width;
44-
ncpus = atoi(argv[5]);
44+
incpus = atoi(argv[5]);
45+
ncpus = atoi(argv[6]);
4546
if (ncpus < 1 || ncpus > MAXCPUS) {
4647
printf ("#cpus %d not in range [0,%d]\n", ncpus, MAXCPUS);
4748
exit(1);
4849
}
49-
cpuid = atoi(argv[6]);
50+
cpuid = atoi(argv[7]);
5051
if (cpuid < 0 || cpuid >= ncpus) {
5152
printf("cpuid %d not in range [0,%d]\n", ncpus, ncpus-1);
5253
exit(1);
5354
}
54-
tsizelen = strlen(tsizearg = argv[7]);
55+
tsizelen = strlen(tsizearg = argv[8]);
5556
if (!isdigit(c = tsizearg[tsizelen-1]))
5657
tsizearg[tsizelen-1] = '\0';
5758
msize = atol(tsizearg);
@@ -67,35 +68,32 @@ int main(int argc, char *argv[])
6768
}
6869

6970
sprintf(inbase,"%d.%d/yx.%02d.%02d",width,modidx,y,x);
70-
gin = openstreams(inbase, ncpus, cpuid, modulus);
71-
if (!nstreams(gin))
72-
return 0;
71+
gin = openstreams(inbase, incpus, ncpus, cpuid, modulus);
7372
go = goinit(width, modulus, nextx, ncpus, cpuid);
7473
sprintf(outbase,"%d.%d/yx.%02d.%02d",width,modidx,y+(nextx==0),nextx);
7574

7675
nnewillcnt = 0LL;
7776
jts = jtalloc(msize, modulus, LOCBITS);
78-
for (nin=0LL; (mb = minstream(gin))->state != FINALSTATE; nin++) {
79-
sn.cnt = mb->cnt;
80-
// printf("expanding %llo\n", mb->state);
81-
nnew = expandstate(mb->state, x, newstates);
82-
for (i=0; i<nnew; i++) {
83-
sn.state = newstates[i];
84-
//printf("inserting %llo\n", sn.state);
85-
jtinsert(jts, &sn);
77+
if (nstreams(gin)) {
78+
for (nin=0LL; (mb = minstream(gin))->state != FINALSTATE; nin++) {
79+
sn.cnt = mb->cnt;
80+
// printf("expanding %llo\n", mb->state);
81+
nnew = expandstate(mb->state, x, newstates);
82+
for (i=0; i<nnew; i++) {
83+
sn.state = newstates[i];
84+
//printf("inserting %llo\n", sn.state);
85+
jtinsert(jts, &sn);
86+
}
87+
if (nnew < 3) // nnew == 2
88+
modadd(modulus, &nnewillcnt, mb->cnt);
89+
if (jtfull(jts))
90+
dumpstates(go, jts, outbase, noutfiles++, mb->state);
91+
deletemin(gin);
8692
}
87-
if (nnew < 3) // nnew == 2
88-
modadd(modulus, &nnewillcnt, mb->cnt);
89-
if (jtfull(jts))
90-
dumpstates(go, jts, outbase, noutfiles++);
91-
deletemin(gin);
9293
}
93-
if (!jtempty(jts))
94-
dumpstates(go, jts, outbase, noutfiles++);
94+
dumpstates(go, jts, outbase, noutfiles++, FINALSTATE);
9595
jtfree(jts);
9696

97-
hidefiles(go, outbase, noutfiles);
98-
9997
printf("(%d,%d) size %lu xsize %lu mod ",y,x,nin,totalread(gin));
10098
if (modulus)
10199
printf("%lu",modulus);

outstream.c

+7-22
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ void writerecord(goout *go, uint64_t state, uint64_t cnt)
122122
{
123123
uint64_t delta = state - go->bufstate;
124124

125-
assert(delta > 0);
125+
assert(!go->bufstate || delta > 0);
126126
writedelta(go, delta);
127127
if (!fwrite(&cnt, sizeof(uint64_t),1,go->fpdelta)) {
128128
printf("failed to write count\n");
@@ -152,20 +152,20 @@ void closedelta(goout *go)
152152
modadd(go->modulus, &go->cumlegal, go->nlegal);
153153
}
154154

155-
void dumpstates(goout *go, jtset *jts, char *outbase, int extension)
155+
void dumpstates(goout *go, jtset *jts, char *outbase, int iteration, uint64_t state)
156156
{
157157
char formatname[FILENAMELEN];
158158
statecnt *sc;
159159
uint64_t prevstate;
160160
int i;
161161

162-
if (extension==0) {
162+
if (iteration==0) {
163163
for (i=0; i<go->ncpus; i++) {
164164
sprintf(formatname,"%s/fromto.%d.%d", outbase, go->cpuid, i);
165165
mkdir(formatname, 0755);
166166
}
167167
}
168-
sprintf(formatname,"%s/fromto.%d.%%d/%d", outbase, go->cpuid, extension);
168+
sprintf(formatname,"%s/fromto.%d.%%d/%d.%lo", outbase, go->cpuid, iteration, state);
169169
//printf("formatname = %s\n", formatname);
170170
jtstartfor(jts, 3*go->width);
171171
sc = jtnext(jts);
@@ -174,7 +174,7 @@ void dumpstates(goout *go, jtset *jts, char *outbase, int extension)
174174
sprintf(go->outname,formatname, i);
175175
opendelta(go);
176176
while (sc != NULL && sc->state < go->parts[i]) {
177-
assert(sc->state > prevstate);
177+
assert(sc->state==STARTSTATE || sc->state > prevstate);
178178
prevstate = sc->state;
179179
writerecord(go, sc->state, sc->cnt);
180180
sc = jtnext(jts);
@@ -185,19 +185,6 @@ void dumpstates(goout *go, jtset *jts, char *outbase, int extension)
185185
assert(jtempty(jts));
186186
}
187187

188-
void hidefiles(goout *go, char *basename, int extension)
189-
{
190-
char outname[FILENAMELEN],bak[FILENAMELEN];
191-
int i;
192-
193-
for (i=0; i<go->ncpus; i++) {
194-
sprintf(outname,"%s/fromto.%d.%d/%d", basename, go->cpuid, i, extension);
195-
sprintf(bak,"%s/fromto.%d.%d/%d.bak", basename, go->cpuid, i, extension);
196-
if (rename(outname, bak) == 0)
197-
printf("%s renamed to %s\n",outname, bak);
198-
}
199-
}
200-
201188
uint64_t needywritten(goout *go)
202189
{
203190
return go->cumneedy;
@@ -273,13 +260,11 @@ int main(int argc, char *argv[])
273260
while (fread(&sb.state,sizeof(uint64_t),2,stdin)==2) {
274261
jtinsert(jts, &sb);
275262
if (jtfull(jts))
276-
dumpstates(go, jts, outbase, noutfiles++);
263+
dumpstates(go, jts, outbase, noutfiles++, sb.state);
277264
}
278-
if (!jtempty(jts))
279-
dumpstates(go, jts, outbase, noutfiles++);
265+
dumpstates(go, jts, outbase, noutfiles++, FINALSTATE);
280266
jtfree(jts);
281267

282-
hidefiles(go, outbase, noutfiles);
283268
if (nextx==0)
284269
printf("legal(%dx%d) %% %llu = %llu\n",y+1,width,modulus,legalwritten(go));
285270
return 0;

outstream.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
typedef struct goout goout;
44

55
goout *goinit(int width, uint64_t modulus, int bump, int ncpus,int cpuid);
6-
void dumpstates(goout *go, jtset *jts, char *outbase, int extension);
6+
void dumpstates(goout *go, jtset *jts, char *outbase, int iteration, uint64_t state);
77
void hidefiles(goout *go, char *basename, int extension);
88
uint64_t needywritten(goout *go);
99
uint64_t legalwritten(goout *go);

0 commit comments

Comments
 (0)