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.1 by gwlarson, Wed Nov 4 16:37:19 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         64              /* beam clump size (kilobytes) */
18 > #define BKBSIZE         256             /* beam clump size (kilobytes) */
19   #endif
20  
19
21   char    *progname;
22 + char    tempfile[128];
23  
24 + extern char     *rindex();
25 + extern int      quit();
26   extern long     rhinitcopy();
27  
28  
# Line 26 | Line 30 | main(argc, argv)
30   int     argc;
31   char    *argv[];
32   {
29        char    nambuf[128];
33          char    *inpname, *outname;
34          int     hdfd[2];
35          long    nextipos, lastopos, thisopos;
# Line 39 | 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 71 | 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 96 | 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 ||
101 <                        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 111 | 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 */
137 < register GCOORD *gc;
138 < int     ia, di;
139 < register HOLO   *hp;
136 > static BEAMI    *beamdir;
137 >
138 > static int
139 > bpcmp(b1p, b2p)                 /* compare beam positions on disk */
140 > int     *b1p, *b2p;
141   {
142 <        int     nw;
142 >        register long   pdif = beamdir[*b1p].fo - beamdir[*b2p].fo;
143  
144 <        if (di > 0) {
145 <                if (++gc->i[ia] >= hp->grid[((gc->w>>1)+1+ia)%3]) {
146 <                        nw = ((gc->w&~1) + (ia<<1) + 3) % 6;
131 <                        gc->i[ia] = gc->i[1-ia];
132 <                        gc->i[1-ia] = gc->w&1 ? hp->grid[((nw>>1)+2-ia)%3]-1 : 0;
133 <                        gc->w = nw;
134 <                }
135 <        } else if (di < 0) {
136 <                if (--gc->i[ia] < 0) {
137 <                        nw = ((gc->w&~1) + (ia<<1) + 2) % 6;
138 <                        gc->i[ia] = gc->i[1-ia];
139 <                        gc->i[1-ia] = gc->w&1 ? hp->grid[((nw>>1)+2-ia)%3]-1 : 0;
140 <                        gc->w = nw;
141 <                }
142 <        }
144 >        if (pdif > 0L) return(1);
145 >        if (pdif < 0L) return(-1);
146 >        return(0);
147   }
148  
149 + static HOLO     *hout;
150  
151 < mkneighgrid(ng, hp, gc)         /* compute neighborhood for grid cell */
152 < GCOORD  ng[3*3];
151 > static int
152 > xferclump(hp, bq, nb)           /* transfer the given clump to hout and free */
153   HOLO    *hp;
154 < GCOORD  *gc;
154 > int     *bq, nb;
155   {
156 <        GCOORD  gci0;
157 <        int     i, j;
156 >        register int    i;
157 >        register BEAM   *bp;
158  
159 <        for (i = 3; i--; ) {
160 <                copystruct(&gci0, gc);
161 <                gcshifti(&gci0, 0, i-1, hp);
162 <                for (j = 3; j--; ) {
163 <                        copystruct(ng+(3*i+j), &gci0);
164 <                        gcshifti(ng+(3*i+j), 1, j-1, hp);
165 <                }
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  
164
165 int     bneighlist[9*9-1];
166 int     bneighrem;
167
168 #define nextneigh()     (bneighrem<=0 ? 0 : bneighlist[--bneighrem])
169
170 int
171 firstneigh(hp, b)               /* initialize neighbor list and return first */
172 HOLO    *hp;
173 int     b;
174 {
175        GCOORD  wg0[9], wg1[9], bgc[2];
176        int     i, j;
177
178        hdbcoord(bgc, hp, b);
179        mkneighgrid(wg0, hp, bgc);
180        mkneighgrid(wg1, hp, bgc+1);
181        bneighrem = 0;
182        for (i = 9; i--; )
183                for (j = 9; j--; ) {
184                        if (i == 4 & j == 4)
185                                continue;
186                        copystruct(bgc, wg0+i);
187                        copystruct(bgc+1, wg1+j);
188                        bneighlist[bneighrem++] = hdbindex(hp, bgc);
189                }
190        return(nextneigh());
191 }
192
193
172   copysect(ifd, ofd)              /* copy holodeck section from ifd to ofd */
173   int     ifd, ofd;
174   {
175 < #define beamdone(b)     (!hinp->bi[b].nrd || bnrays(hout,b))
198 <        static short    primes[] = {9431,6803,4177,2659,1609,887,587,251,47,1};
199 <        register HOLO   *hinp, *hout;
200 <        register BEAM   *bp;
201 <        int     *bqueue;
202 <        int     bqlen;
203 <        int4    bqtotal;
204 <        int     bc, bci, bqc, bnc, myprime;
205 <        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 <        if ((bqueue = (int *)malloc(nbeams(hinp)*sizeof(int))) == NULL)
182 <                error(SYSTEM, "out of memory in copysect");
213 <                                        /* pick a good prime step size */
214 <        for (i = 0; primes[i]<<5 >= nbeams(hinp); i++)
215 <                ;
216 <        while ((myprime = primes[i++]) > 1)
217 <                if (nbeams(hinp) % myprime)
218 <                        break;
219 <                                        /* add each input beam and neighbors */
220 <        for (bc = bci = nbeams(hinp); bc > 0; bc--,
221 <                        bci += bci>myprime ? -myprime : nbeams(hinp)-myprime) {
222 <                if (beamdone(bci))
223 <                        continue;
224 <                bqueue[0] = bci;                /* initialize queue */
225 <                bqlen = 1;
226 <                bqtotal = bnrays(hinp, bci);
227 <                                                /* run through growing queue */
228 <                for (bqc = 0; bqc < bqlen; bqc++) {
229 <                                                /* transfer the beam */
230 <                        bp = hdgetbeam(hinp, bqueue[bqc]);
231 <                        bcopy((char *)hdbray(bp),
232 <                                (char *)hdnewrays(hout,bqueue[bqc],bp->nrm),
233 <                                        bp->nrm*sizeof(RAYVAL));
234 <                        hdfreebeam(hinp, bqueue[bqc]);
235 <                                                /* check queue size */
236 <                        if (bqtotal >= BKBSIZE*1024/sizeof(RAYVAL))
237 <                                continue;
238 <                                                /* add neighbors to queue */
239 <                        for (bnc = firstneigh(hinp,bqueue[bqc]); bnc > 0;
240 <                                        bnc = nextneigh()) {
241 <                                if (beamdone(bnc))      /* see if valid */
242 <                                        continue;
243 <                                for (i = bqlen; i--; )
244 <                                        if (bqueue[i] == bnc) break;
245 <                                if (i >= 0)
246 <                                        continue;
247 <                                bqueue[bqlen++] = bnc;  /* add it */
248 <                                bqtotal += bnrays(hinp, bnc);
249 <                                if (bqtotal >= BKBSIZE*1024/sizeof(RAYVAL))
250 <                                        break;          /* queue full */
251 <                        }
252 <                }
253 <                hdfreebeam(hout, 0);            /* flush output block */
254 <        }
255 <                                        /* we're done -- clean up */
256 <        free((char *)bqueue);
180 >                                        /* clump the beams */
181 >        clumpbeams(hinp, 0, BKBSIZE*1024, xferclump);
182 >                                        /* clean up */
183          hddone(hinp);
184          hddone(hout);
259 #undef beamdone
185   }
186  
187  
# Line 282 | 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