1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: rayfifo.c,v 2.1 2009/12/12 05:20:10 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_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 |
* |
31 |
* Note: The ray passed to ray_fifo_in() may be overwritten |
32 |
* arbitrarily, since it is passed to ray_pqueue(). |
33 |
*/ |
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 |
|
45 |
#define r_fifo(rn) (&r_fifo_buf[(rn)&(r_fifo_len-1)]) |
46 |
|
47 |
|
48 |
static void |
49 |
ray_fifo_growbuf(void) /* double buffer size (or set to minimum if NULL) */ |
50 |
{ |
51 |
RAY *old_buf = r_fifo_buf; |
52 |
int old_len = r_fifo_len; |
53 |
int i; |
54 |
|
55 |
if (r_fifo_buf == NULL) |
56 |
r_fifo_len = 1<<4; |
57 |
else |
58 |
r_fifo_len <<= 1; |
59 |
/* allocate new */ |
60 |
r_fifo_buf = (RAY *)calloc(r_fifo_len, sizeof(RAY)); |
61 |
if (r_fifo_buf == NULL) |
62 |
error(SYSTEM, "out of memory in ray_fifo_growbuf"); |
63 |
if (old_buf == NULL) |
64 |
return; |
65 |
/* copy old & free */ |
66 |
for (i = r_fifo_start; i < r_fifo_end; i++) |
67 |
*r_fifo(i) = old_buf[i&(old_len-1)]; |
68 |
|
69 |
free(old_buf); |
70 |
} |
71 |
|
72 |
|
73 |
static int |
74 |
ray_fifo_push( /* send finished ray to output (or queue it) */ |
75 |
RAY *r |
76 |
) |
77 |
{ |
78 |
int rv, nsent = 0; |
79 |
|
80 |
if (ray_fifo_out == NULL) |
81 |
error(INTERNAL, "ray_fifo_out is NULL"); |
82 |
if ((r->rno < r_fifo_start) | (r->rno >= r_fifo_end)) |
83 |
error(INTERNAL, "unexpected ray number in ray_fifo_push"); |
84 |
|
85 |
if (r->rno - r_fifo_start >= r_fifo_len) |
86 |
ray_fifo_growbuf(); /* need more space */ |
87 |
|
88 |
if (r->rno > r_fifo_start) { /* insert into output queue */ |
89 |
*r_fifo(r->rno) = *r; |
90 |
return(0); |
91 |
} |
92 |
/* r->rno == r_fifo_start, so transfer ray(s) */ |
93 |
do { |
94 |
if ((rv = (*ray_fifo_out)(r)) < 0) |
95 |
return(-1); |
96 |
nsent += rv; |
97 |
if (++r_fifo_start < r_fifo_end) |
98 |
r = r_fifo(r_fifo_start); |
99 |
} while (r->rno == r_fifo_start); |
100 |
|
101 |
return(nsent); |
102 |
} |
103 |
|
104 |
|
105 |
int |
106 |
ray_fifo_in( /* add ray to FIFO */ |
107 |
RAY *r |
108 |
) |
109 |
{ |
110 |
int rv, rval = 0; |
111 |
|
112 |
if (r_fifo_start >= 1L<<30) { /* reset our counter */ |
113 |
if ((rv = ray_fifo_flush()) < 0) |
114 |
return(-1); |
115 |
rval += rv; |
116 |
} |
117 |
/* queue ray */ |
118 |
rayorigin(r, PRIMARY, NULL, NULL); |
119 |
r->rno = r_fifo_end++; |
120 |
if ((rv = ray_pqueue(r)) < 0) |
121 |
return(-1); |
122 |
|
123 |
if (!rv) /* no result this time? */ |
124 |
return(rval); |
125 |
|
126 |
do { /* else send/queue result */ |
127 |
if ((rv = ray_fifo_push(r)) < 0) |
128 |
return(-1); |
129 |
rval += rv; |
130 |
|
131 |
} while (ray_presult(r, -1) > 0); /* empty in-core queue */ |
132 |
|
133 |
return(rval); |
134 |
} |
135 |
|
136 |
|
137 |
int |
138 |
ray_fifo_flush(void) /* flush everything and release buffer */ |
139 |
{ |
140 |
RAY myRay; |
141 |
int rv, rval = 0; |
142 |
/* clear parallel queue */ |
143 |
while ((rv = ray_presult(&myRay, 0)) > 0 && |
144 |
(rv = ray_fifo_push(&myRay)) >= 0) |
145 |
rval += rv; |
146 |
|
147 |
if (rv < 0) /* check for error */ |
148 |
return(-1); |
149 |
|
150 |
if (r_fifo_start != r_fifo_end) |
151 |
error(INTERNAL, "could not empty queue in ray_fifo_flush"); |
152 |
|
153 |
free(r_fifo_buf); |
154 |
r_fifo_buf = NULL; |
155 |
r_fifo_len = 0; |
156 |
r_fifo_end = r_fifo_start = 1; |
157 |
|
158 |
return(rval); |
159 |
} |