ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhoptimize.c
(Generate patch)

Comparing ray/src/hd/rhoptimize.c (file contents):
Revision 3.2 by gwlarson, Thu Nov 5 14:43:14 1998 UTC vs.
Revision 3.5 by gwlarson, Thu Nov 12 15:07:49 1998 UTC

# Line 12 | Line 12 | static char SCCSid[] = "$SunId$ SGI";
12  
13   #include "holo.h"
14  
15 + #include <signal.h>
16 +
17   #ifndef BKBSIZE
18   #define BKBSIZE         256             /* beam clump size (kilobytes) */
19   #endif
20  
19 #define flgop(p,i,op)           ((p)[(i)>>5] op (1L<<((i)&0x1f)))
20 #define isset(p,i)              flgop(p,i,&)
21 #define setfl(p,i)              flgop(p,i,|=)
22 #define clrfl(p,i)              flgop(p,i,&=~)
23
21   char    *progname;
22 + char    tempfile[128];
23  
24 + extern char     *rindex();
25 + extern int      quit();
26   extern long     rhinitcopy();
27  
28  
# Line 30 | Line 30 | main(argc, argv)
30   int     argc;
31   char    *argv[];
32   {
33        char    nambuf[128];
33          char    *inpname, *outname;
34          int     hdfd[2];
35          long    nextipos, lastopos, thisopos;
# Line 43 | Line 42 | char   *argv[];
42          inpname = argv[1];
43          if (argc == 3)                  /* use given output file */
44                  outname = argv[2];
45 <        else {                          /* else create temporary file */
46 <                strcpy(nambuf, inpname);
47 <                if ((outname = strrchr(nambuf, '/')) != NULL)
45 >        else {                          /* else use temporary file */
46 >                if (access(inpname, R_OK|W_OK) < 0) {   /* check permissions */
47 >                        sprintf(errmsg, "cannot access \"%s\"", inpname);
48 >                        error(SYSTEM, errmsg);
49 >                }
50 >                strcpy(tempfile, inpname);
51 >                if ((outname = rindex(tempfile, '/')) != NULL)
52                          outname++;
53                  else
54 <                        outname = nambuf;
54 >                        outname = tempfile;
55                  sprintf(outname, "rho%d.hdk", getpid());
56 <                outname = nambuf;
56 >                outname = tempfile;
57          }
58                                          /* copy holodeck file header */
59          nextipos = rhinitcopy(hdfd, inpname, outname);
# Line 75 | Line 78 | char   *argv[];
78                                          /* clean up */
79          close(hdfd[0]);
80          close(hdfd[1]);
81 <        if (argc == 2 && rename(outname, inpname) < 0) {
81 >        if (outname == tempfile && rename(outname, inpname) < 0) {
82                  sprintf(errmsg, "cannot rename \"%s\" to \"%s\"",
83                                  outname, inpname);
84                  error(SYSTEM, errmsg);
# Line 100 | Line 103 | char   *infn, *outfn;
103                  sprintf(errmsg, "cannot open \"%s\" for writing", outfn);
104                  error(SYSTEM, errmsg);
105          }
106 +                                        /* set up signal handling */
107 +        if (signal(SIGINT, quit) == SIG_IGN) signal(SIGINT, SIG_IGN);
108 +        if (signal(SIGHUP, quit) == SIG_IGN) signal(SIGHUP, SIG_IGN);
109 +        if (signal(SIGTERM, quit) == SIG_IGN) signal(SIGTERM, SIG_IGN);
110 + #ifdef SIGXCPU
111 +        if (signal(SIGXCPU, quit) == SIG_IGN) signal(SIGXCPU, SIG_IGN);
112 +        if (signal(SIGXFSZ, quit) == SIG_IGN) signal(SIGXFSZ, SIG_IGN);
113 + #endif
114                                          /* copy and verify header */
115 <        if (checkheader(infp, HOLOFMT, outfp) < 0 ||
105 <                        getw(infp) != HOLOMAGIC)
115 >        if (checkheader(infp, HOLOFMT, outfp) < 0 || getw(infp) != HOLOMAGIC)
116                  error(USER, "input not in holodeck format");
117          fputformat(HOLOFMT, outfp);
118          fputc('\n', outfp);
# Line 115 | Line 125 | char   *infn, *outfn;
125          fclose(infp);
126          if (fclose(outfp) == EOF)
127                  error(SYSTEM, "file flushing error in rhinitcopy");
128 <                                        /* we flush everything manually */
129 <        hdcachesize = 0;
128 >                                        /* check cache size */
129 >        if (BKBSIZE*1024*1.5 > hdcachesize)
130 >                hdcachesize = BKBSIZE*1024*1.5;
131                                          /* return input position */
132          return(ifpos);
133   }
134  
135  
136 < gcshifti(gc, ia, di, hp)        /* shift cell row or column */
126 < register GCOORD *gc;
127 < int     ia, di;
128 < register HOLO   *hp;
129 < {
130 <        int     nw;
136 > static BEAMI    *beamdir;
137  
138 <        if (di > 0) {
139 <                if (++gc->i[ia] >= hp->grid[((gc->w>>1)+1+ia)%3]) {
140 <                        nw = ((gc->w&~1) + (ia<<1) + 3) % 6;
135 <                        gc->i[ia] = gc->i[1-ia];
136 <                        gc->i[1-ia] = gc->w&1 ? hp->grid[((nw>>1)+2-ia)%3]-1 : 0;
137 <                        gc->w = nw;
138 <                }
139 <        } else if (di < 0) {
140 <                if (--gc->i[ia] < 0) {
141 <                        nw = ((gc->w&~1) + (ia<<1) + 2) % 6;
142 <                        gc->i[ia] = gc->i[1-ia];
143 <                        gc->i[1-ia] = gc->w&1 ? hp->grid[((nw>>1)+2-ia)%3]-1 : 0;
144 <                        gc->w = nw;
145 <                }
146 <        }
147 < }
148 <
149 <
150 < mkneighgrid(ng, hp, gc)         /* compute neighborhood for grid cell */
151 < GCOORD  ng[3*3];
152 < HOLO    *hp;
153 < GCOORD  *gc;
138 > static int
139 > bpcmp(b1p, b2p)                 /* compare beam positions on disk */
140 > int     *b1p, *b2p;
141   {
142 <        GCOORD  gci0;
156 <        register int    i, j;
142 >        register long   pdif = beamdir[*b1p].fo - beamdir[*b2p].fo;
143  
144 <        for (i = 3; i--; ) {
145 <                copystruct(&gci0, gc);
146 <                gcshifti(&gci0, 0, i-1, hp);
161 <                for (j = 3; j--; ) {
162 <                        copystruct(ng+(3*i+j), &gci0);
163 <                        gcshifti(ng+(3*i+j), gci0.w==gc->w, j-1, hp);
164 <                }
165 <        }
144 >        if (pdif > 0L) return(1);
145 >        if (pdif < 0L) return(-1);
146 >        return(0);
147   }
148  
149 + static HOLO     *hout;
150  
151 < int     bneighlist[9*9-1];
152 < int     bneighrem;
171 <
172 < #define nextneigh()     (bneighrem<=0 ? 0 : bneighlist[--bneighrem])
173 <
174 < int
175 < firstneigh(hp, b)               /* initialize neighbor list and return first */
151 > static int
152 > xferclump(hp, bq, nb)           /* transfer the given clump to hout and free */
153   HOLO    *hp;
154 < int     b;
154 > int     *bq, nb;
155   {
156 <        GCOORD  wg0[9], wg1[9], bgc[2];
157 <        int     i, j;
156 >        register int    i;
157 >        register BEAM   *bp;
158  
159 <        hdbcoord(bgc, hp, b);
160 <        mkneighgrid(wg0, hp, bgc);
161 <        mkneighgrid(wg1, hp, bgc+1);
162 <        bneighrem = 0;
163 <        for (i = 9; i--; )
164 <                for (j = 9; j--; ) {
165 <                        if (i == 4 & j == 4)
166 <                                continue;
167 <                        if (wg0[i].w == wg1[j].w)
168 <                                continue;
192 <                        copystruct(bgc, wg0+i);
193 <                        copystruct(bgc+1, wg1+j);
194 <                        bneighlist[bneighrem++] = hdbindex(hp, bgc);
195 < #ifdef DEBUG
196 <                        if (bneighlist[bneighrem-1] <= 0)
197 <                                error(CONSISTENCY, "bad beam in firstneigh");
198 < #endif
199 <                }
200 <        return(nextneigh());
201 < }
202 <
203 <
204 < BEAMI   *beamdir;
205 <
206 < int
207 < bpcmp(b1p, b2p)                 /* compare beam positions on disk */
208 < int     *b1p, *b2p;
209 < {
210 <        register long   pdif = beamdir[*b1p].fo - beamdir[*b2p].fo;
211 <
212 <        if (pdif > 0) return(1);
213 <        if (pdif < 0) return(-1);
159 >        beamdir = hp->bi;               /* sort based on file position */
160 >        qsort((char *)bq, nb, sizeof(*bq), bpcmp);
161 >                                        /* transfer and free each beam */
162 >        for (i = 0; i < nb; i++) {
163 >                bp = hdgetbeam(hp, bq[i]);
164 >                bcopy((char *)hdbray(bp), (char *)hdnewrays(hout,bq[i],bp->nrm),
165 >                                bp->nrm*sizeof(RAYVAL));
166 >                hdfreebeam(hp, bq[i]);
167 >        }
168 >        hdflush(hout);                  /* write & free clump */
169          return(0);
170   }
171  
217
172   copysect(ifd, ofd)              /* copy holodeck section from ifd to ofd */
173   int     ifd, ofd;
174   {
175 <        static short    primes[] = {9431,6803,4177,2659,1609,887,587,251,47,1};
222 <        register HOLO   *hinp;
223 <        HOLO    *hout;
224 <        register BEAM   *bp;
225 <        unsigned int4   *bflags;
226 <        int     *bqueue;
227 <        int     bqlen;
228 <        int4    bqtotal;
229 <        int     bc, bci, bqc, myprime;
230 <        register int    i;
175 >        HOLO    *hinp;
176                                          /* load input section directory */
177          hinp = hdinit(ifd, NULL);
178                                          /* create output section directory */
179          hout = hdinit(ofd, (HDGRID *)hinp);
180 <                                        /* allocate beam queue */
181 <        bqueue = (int *)malloc(nbeams(hinp)*sizeof(int));
182 <        bflags = (int4 *)calloc((nbeams(hinp)>>3)+1, sizeof(int4));
238 <        if (bqueue == NULL | bflags == NULL)
239 <                error(SYSTEM, "out of memory in copysect");
240 <                                        /* mark empty beams as done */
241 <        for (i = nbeams(hinp); i-- > 0; )
242 <                if (!hinp->bi[i].nrd)
243 <                        setfl(bflags, i);
244 <                                        /* pick a good prime step size */
245 <        for (i = 0; primes[i]<<5 >= nbeams(hinp); i++)
246 <                ;
247 <        while ((myprime = primes[i++]) > 1)
248 <                if (nbeams(hinp) % myprime)
249 <                        break;
250 <                                        /* add each input beam and neighbors */
251 <        for (bc = bci = nbeams(hinp); bc > 0; bc--,
252 <                        bci += bci>myprime ? -myprime : nbeams(hinp)-myprime) {
253 <                if (isset(bflags, bci))
254 <                        continue;
255 <                bqueue[0] = bci;                /* initialize queue */
256 <                bqlen = 1;
257 <                bqtotal = bnrays(hinp, bci);
258 <                setfl(bflags, bci);
259 <                                                /* run through growing queue */
260 <                for (bqc = 0; bqc < bqlen; bqc++) {
261 <                                                /* add neighbors until full */
262 <                        for (i = firstneigh(hinp,bqueue[bqc]); i > 0;
263 <                                        i = nextneigh()) {
264 <                                if (isset(bflags, i))   /* done already? */
265 <                                        continue;
266 <                                bqueue[bqlen++] = i;    /* add it */
267 <                                bqtotal += bnrays(hinp, i);
268 <                                setfl(bflags, i);
269 <                                if (bqtotal >= BKBSIZE*1024/sizeof(RAYVAL))
270 <                                        break;          /* queue full */
271 <                        }
272 <                        if (i > 0)
273 <                                break;
274 <                }
275 <                beamdir = hinp->bi;             /* sort queue */
276 <                qsort((char *)bqueue, bqlen, sizeof(*bqueue), bpcmp);
277 <                                                /* transfer each beam */
278 <                for (i = 0; i < bqlen; i++) {
279 <                        bp = hdgetbeam(hinp, bqueue[i]);
280 <                        bcopy((char *)hdbray(bp),
281 <                                (char *)hdnewrays(hout,bqueue[i],bp->nrm),
282 <                                        bp->nrm*sizeof(RAYVAL));
283 <                        hdfreebeam(hinp, bqueue[i]);
284 <                }
285 <                hdfreebeam(hout, 0);            /* flush output block */
286 < #ifdef DEBUG
287 <                hdsync(hout, 0);
288 < #endif
289 <        }
290 <                                        /* we're done -- clean up */
291 <        free((char *)bqueue);
292 <        free((char *)bflags);
180 >                                        /* clump the beams */
181 >        clumpbeams(hinp, 0, BKBSIZE*1024, xferclump);
182 >                                        /* clean up */
183          hddone(hinp);
184          hddone(hout);
185   }
# Line 317 | Line 207 | register char  *s;
207   quit(code)                      /* exit the program gracefully */
208   int     code;
209   {
210 <        hdsync(NULL, 1);        /* write out any buffered data */
210 >        if (tempfile[0])
211 >                unlink(tempfile);
212          exit(code);
213   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines