ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rayfifo.c
Revision: 2.4
Committed: Sun Dec 13 19:13:04 2009 UTC (14 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad5R0, rad5R1, rad4R2, rad4R1, rad4R0, rad4R2P1
Changes since 2.3: +7 -7 lines
Log Message:
Eliminated restriction with -n and -I in rtrace

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.4 static const char RCSid[] = "$Id: rayfifo.c,v 2.3 2009/12/12 23:08:13 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 greg 2.4 * the same manner as for the ray_pqueue() call, i.e., rayorigin()
24     * has been called and the origin, direction and maximum distance
25     * have all been assigned. However, the ray number will be reset
26     * by ray_fifo_in() according to the number of rays traced since the
27 greg 2.1 * last call to ray_fifo_flush(). This final call completes all
28     * pending ray calculations and frees the FIFO buffer. If any of
29     * the automatic calls to the ray_fifo_out callback return a
30     * negative value, processing stops and -1 is returned.
31 greg 2.2 *
32     * Note: The ray passed to ray_fifo_in() may be overwritten
33     * arbitrarily, since it is passed to ray_pqueue().
34 greg 2.1 */
35    
36     #include "ray.h"
37     #include <string.h>
38    
39     int (*ray_fifo_out)(RAY *r) = NULL; /* ray output callback */
40    
41     static RAY *r_fifo_buf = NULL; /* circular FIFO out buffer */
42     static int r_fifo_len = 0; /* allocated FIFO length */
43     static RNUMBER r_fifo_start = 1; /* first awaited ray */
44     static RNUMBER r_fifo_end = 1; /* one past FIFO last */
45 greg 2.3 static RNUMBER r_fifo_next = 1; /* next ray assignment */
46 greg 2.1
47     #define r_fifo(rn) (&r_fifo_buf[(rn)&(r_fifo_len-1)])
48    
49    
50     static void
51     ray_fifo_growbuf(void) /* double buffer size (or set to minimum if NULL) */
52     {
53     RAY *old_buf = r_fifo_buf;
54     int old_len = r_fifo_len;
55     int i;
56    
57     if (r_fifo_buf == NULL)
58 greg 2.4 r_fifo_len = 1<<5;
59 greg 2.1 else
60     r_fifo_len <<= 1;
61     /* allocate new */
62     r_fifo_buf = (RAY *)calloc(r_fifo_len, sizeof(RAY));
63     if (r_fifo_buf == NULL)
64     error(SYSTEM, "out of memory in ray_fifo_growbuf");
65     if (old_buf == NULL)
66     return;
67     /* copy old & free */
68     for (i = r_fifo_start; i < r_fifo_end; i++)
69     *r_fifo(i) = old_buf[i&(old_len-1)];
70    
71     free(old_buf);
72     }
73    
74    
75     static int
76     ray_fifo_push( /* send finished ray to output (or queue it) */
77     RAY *r
78     )
79     {
80     int rv, nsent = 0;
81    
82     if (ray_fifo_out == NULL)
83     error(INTERNAL, "ray_fifo_out is NULL");
84 greg 2.3 if ((r->rno < r_fifo_start) | (r->rno >= r_fifo_next))
85     error(INTERNAL, "unexpected ray number in ray_fifo_push()");
86 greg 2.2
87 greg 2.1 if (r->rno > r_fifo_start) { /* insert into output queue */
88 greg 2.4 while (r->rno - r_fifo_start >= r_fifo_len)
89 greg 2.3 ray_fifo_growbuf(); /* need more space */
90 greg 2.1 *r_fifo(r->rno) = *r;
91 greg 2.3 if (r->rno >= r_fifo_end)
92     r_fifo_end = r->rno + 1;
93 greg 2.1 return(0);
94     }
95     /* r->rno == r_fifo_start, so transfer ray(s) */
96     do {
97 greg 2.3 rv = (*ray_fifo_out)(r);
98     r->rno = 0; /* flag this entry complete */
99     if (rv < 0)
100 greg 2.1 return(-1);
101     nsent += rv;
102     if (++r_fifo_start < r_fifo_end)
103     r = r_fifo(r_fifo_start);
104 greg 2.3 else if (r_fifo_start > r_fifo_end)
105     r_fifo_end = r_fifo_start;
106 greg 2.1 } while (r->rno == r_fifo_start);
107    
108     return(nsent);
109     }
110    
111    
112     int
113     ray_fifo_in( /* add ray to FIFO */
114     RAY *r
115     )
116     {
117 greg 2.3 static int incall = 0; /* prevent recursion */
118     int rv, rval = 0;
119    
120     if (incall++)
121     error(INTERNAL, "recursive call to ray_fifo_in()");
122 greg 2.1
123 greg 2.3 if (r_fifo_start >= 1L<<30) { /* reset our counters */
124 greg 2.1 if ((rv = ray_fifo_flush()) < 0)
125 greg 2.3 {--incall; return(-1);}
126 greg 2.1 rval += rv;
127     }
128     /* queue ray */
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     }