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