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, 3 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

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rayfifo.c,v 2.3 2009/12/12 23:08:13 greg Exp $";
3 #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_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 * 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 *
32 * Note: The ray passed to ray_fifo_in() may be overwritten
33 * arbitrarily, since it is passed to ray_pqueue().
34 */
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 static RNUMBER r_fifo_next = 1; /* next ray assignment */
46
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 r_fifo_len = 1<<5;
59 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 if ((r->rno < r_fifo_start) | (r->rno >= r_fifo_next))
85 error(INTERNAL, "unexpected ray number in ray_fifo_push()");
86
87 if (r->rno > r_fifo_start) { /* insert into output queue */
88 while (r->rno - r_fifo_start >= r_fifo_len)
89 ray_fifo_growbuf(); /* need more space */
90 *r_fifo(r->rno) = *r;
91 if (r->rno >= r_fifo_end)
92 r_fifo_end = r->rno + 1;
93 return(0);
94 }
95 /* r->rno == r_fifo_start, so transfer ray(s) */
96 do {
97 rv = (*ray_fifo_out)(r);
98 r->rno = 0; /* flag this entry complete */
99 if (rv < 0)
100 return(-1);
101 nsent += rv;
102 if (++r_fifo_start < r_fifo_end)
103 r = r_fifo(r_fifo_start);
104 else if (r_fifo_start > r_fifo_end)
105 r_fifo_end = r_fifo_start;
106 } 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 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
123 if (r_fifo_start >= 1L<<30) { /* reset our counters */
124 if ((rv = ray_fifo_flush()) < 0)
125 {--incall; return(-1);}
126 rval += rv;
127 }
128 /* queue ray */
129 r->rno = r_fifo_next++;
130 if ((rv = ray_pqueue(r)) < 0)
131 {--incall; return(-1);}
132
133 if (!rv) /* no result this time? */
134 {--incall; return(rval);}
135
136 do { /* else send/queue result */
137 if ((rv = ray_fifo_push(r)) < 0)
138 {--incall; return(-1);}
139 rval += rv;
140
141 } while (ray_presult(r, -1) > 0); /* empty in-core queue */
142
143 --incall; return(rval);
144 }
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 if (rv < 0) /* check for exception */
158 return(-1);
159
160 if (r_fifo_start != r_fifo_end)
161 error(INTERNAL, "could not empty queue in ray_fifo_flush()");
162
163 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
169 return(rval);
170 }