ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rayfifo.c
Revision: 2.3
Committed: Sat Dec 12 23:08:13 2009 UTC (14 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +32 -21 lines
Log Message:
Bug fixes and performance improvements to rtrace -n option

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.3 static const char RCSid[] = "$Id: rayfifo.c,v 2.2 2009/12/12 19:01:00 greg Exp $";
3 greg 2.1 #endif
4     /*
5     * rayfifo.c - parallelize ray queue that respects order
6     *
7     * External symbols declared in ray.h
8     */
9    
10     #include "copyright.h"
11    
12     /*
13     * These routines are essentially an adjunct to raypcalls.c, providing
14     * a convenient means to get first-in/first-out behavior from multiple
15     * processor cores. The interface is quite simple, with two functions
16     * and a callback, which must be defined by the calling program. The
17     * hand-off for finished rays is assigned to ray_fifo_out, which takes
18     * a single pointer to the finished ray and returns a non-negative
19     * integer. If there is an exceptional condition where termination
20     * is desired, a negative value may be returned.
21     *
22     * The ray_fifo_in() call takes a ray that has been initialized in
23     * the same manner as for the ray_trace() call, i.e., the origin,
24     * direction, and maximum length have been assigned. The ray number
25     * will be set according to the number of rays traced since the
26     * last call to ray_fifo_flush(). This final call completes all
27     * pending ray calculations and frees the FIFO buffer. If any of
28     * the automatic calls to the ray_fifo_out callback return a
29     * negative value, processing stops and -1 is returned.
30 greg 2.2 *
31     * Note: The ray passed to ray_fifo_in() may be overwritten
32     * arbitrarily, since it is passed to ray_pqueue().
33 greg 2.1 */
34    
35     #include "ray.h"
36     #include <string.h>
37    
38     int (*ray_fifo_out)(RAY *r) = NULL; /* ray output callback */
39    
40     static RAY *r_fifo_buf = NULL; /* circular FIFO out buffer */
41     static int r_fifo_len = 0; /* allocated FIFO length */
42     static RNUMBER r_fifo_start = 1; /* first awaited ray */
43     static RNUMBER r_fifo_end = 1; /* one past FIFO last */
44 greg 2.3 static RNUMBER r_fifo_next = 1; /* next ray assignment */
45 greg 2.1
46     #define r_fifo(rn) (&r_fifo_buf[(rn)&(r_fifo_len-1)])
47    
48    
49     static void
50     ray_fifo_growbuf(void) /* double buffer size (or set to minimum if NULL) */
51     {
52     RAY *old_buf = r_fifo_buf;
53     int old_len = r_fifo_len;
54     int i;
55    
56     if (r_fifo_buf == NULL)
57     r_fifo_len = 1<<4;
58     else
59     r_fifo_len <<= 1;
60     /* allocate new */
61     r_fifo_buf = (RAY *)calloc(r_fifo_len, sizeof(RAY));
62     if (r_fifo_buf == NULL)
63     error(SYSTEM, "out of memory in ray_fifo_growbuf");
64     if (old_buf == NULL)
65     return;
66     /* copy old & free */
67     for (i = r_fifo_start; i < r_fifo_end; i++)
68     *r_fifo(i) = old_buf[i&(old_len-1)];
69    
70     free(old_buf);
71     }
72    
73    
74     static int
75     ray_fifo_push( /* send finished ray to output (or queue it) */
76     RAY *r
77     )
78     {
79     int rv, nsent = 0;
80    
81     if (ray_fifo_out == NULL)
82     error(INTERNAL, "ray_fifo_out is NULL");
83 greg 2.3 if ((r->rno < r_fifo_start) | (r->rno >= r_fifo_next))
84     error(INTERNAL, "unexpected ray number in ray_fifo_push()");
85 greg 2.2
86 greg 2.1 if (r->rno > r_fifo_start) { /* insert into output queue */
87 greg 2.3 if (r->rno - r_fifo_start >= r_fifo_len)
88     ray_fifo_growbuf(); /* need more space */
89 greg 2.1 *r_fifo(r->rno) = *r;
90 greg 2.3 if (r->rno >= r_fifo_end)
91     r_fifo_end = r->rno + 1;
92 greg 2.1 return(0);
93     }
94     /* r->rno == r_fifo_start, so transfer ray(s) */
95     do {
96 greg 2.3 rv = (*ray_fifo_out)(r);
97     r->rno = 0; /* flag this entry complete */
98     if (rv < 0)
99 greg 2.1 return(-1);
100     nsent += rv;
101     if (++r_fifo_start < r_fifo_end)
102     r = r_fifo(r_fifo_start);
103 greg 2.3 else if (r_fifo_start > r_fifo_end)
104     r_fifo_end = r_fifo_start;
105 greg 2.1 } while (r->rno == r_fifo_start);
106    
107     return(nsent);
108     }
109    
110    
111     int
112     ray_fifo_in( /* add ray to FIFO */
113     RAY *r
114     )
115     {
116 greg 2.3 static int incall = 0; /* prevent recursion */
117     int rv, rval = 0;
118    
119     if (incall++)
120     error(INTERNAL, "recursive call to ray_fifo_in()");
121 greg 2.1
122 greg 2.3 if (r_fifo_start >= 1L<<30) { /* reset our counters */
123 greg 2.1 if ((rv = ray_fifo_flush()) < 0)
124 greg 2.3 {--incall; return(-1);}
125 greg 2.1 rval += rv;
126     }
127     /* queue ray */
128     rayorigin(r, PRIMARY, NULL, NULL);
129 greg 2.3 r->rno = r_fifo_next++;
130 greg 2.1 if ((rv = ray_pqueue(r)) < 0)
131 greg 2.3 {--incall; return(-1);}
132 greg 2.1
133     if (!rv) /* no result this time? */
134 greg 2.3 {--incall; return(rval);}
135 greg 2.2
136     do { /* else send/queue result */
137     if ((rv = ray_fifo_push(r)) < 0)
138 greg 2.3 {--incall; return(-1);}
139 greg 2.2 rval += rv;
140    
141     } while (ray_presult(r, -1) > 0); /* empty in-core queue */
142    
143 greg 2.3 --incall; return(rval);
144 greg 2.1 }
145    
146    
147     int
148     ray_fifo_flush(void) /* flush everything and release buffer */
149     {
150     RAY myRay;
151     int rv, rval = 0;
152     /* clear parallel queue */
153     while ((rv = ray_presult(&myRay, 0)) > 0 &&
154     (rv = ray_fifo_push(&myRay)) >= 0)
155     rval += rv;
156    
157 greg 2.3 if (rv < 0) /* check for exception */
158 greg 2.1 return(-1);
159    
160     if (r_fifo_start != r_fifo_end)
161 greg 2.3 error(INTERNAL, "could not empty queue in ray_fifo_flush()");
162 greg 2.1
163 greg 2.3 if (r_fifo_buf != NULL) {
164     free(r_fifo_buf);
165     r_fifo_buf = NULL; r_fifo_len = 0;
166     }
167     r_fifo_next = r_fifo_end = r_fifo_start = 1;
168 greg 2.1
169     return(rval);
170     }