fix bugs pyrit
authorRadek Brich <radek.brich@devl.cz>
Sat, 29 Mar 2008 12:09:50 +0100
branchpyrit
changeset 55 f6d75ae82c88
parent 54 dbe3c7a4e0f0
child 56 d4481fc43952
fix bugs
src/raytracer.cc
--- a/src/raytracer.cc	Sat Mar 29 10:56:56 2008 +0100
+++ b/src/raytracer.cc	Sat Mar 29 12:09:50 2008 +0100
@@ -395,7 +395,7 @@
 
 void *Raytracer::raytrace_worker(void *d)
 {
-	static const int my_queue_size = 32;
+	static const int my_queue_size = 256;
 	Raytracer *rt = (Raytracer*)d;
 	Sample my_queue[my_queue_size];
 	Colour my_colours[my_queue_size];
@@ -404,8 +404,6 @@
 	for (;;)
 	{
 		pthread_mutex_lock(&rt->sample_queue_mutex);
-		pthread_cond_signal(&rt->worker_ready_cond);
-
 		while (rt->sample_queue_count == 0)
 		{
 			if (rt->end_of_samples)
@@ -424,16 +422,21 @@
 		rt->sample_queue_count -= my_count;
 
 		// copy samples to local queue
-		if (rt->sample_queue_pos+my_count >= rt->sample_queue_size)
+		if (rt->sample_queue_pos + my_count >= rt->sample_queue_size)
 		{
-			memcpy(my_queue, rt->sample_queue+rt->sample_queue_pos,
-				(rt->sample_queue_size - rt->sample_queue_pos)*sizeof(Sample));
-			my_count -= rt->sample_queue_size - rt->sample_queue_pos;
-			rt->sample_queue_pos = 0;
+			register int c = rt->sample_queue_size - rt->sample_queue_pos;
+			memcpy(my_queue, rt->sample_queue + rt->sample_queue_pos, c*sizeof(Sample));
+			memcpy(my_queue + c, rt->sample_queue, (my_count - c)*sizeof(Sample));
+			rt->sample_queue_pos = my_count - c;
 		}
-		memcpy(my_queue, rt->sample_queue+rt->sample_queue_pos,
-			my_count*sizeof(Sample));
-
+		else
+		{
+			memcpy(my_queue, rt->sample_queue + rt->sample_queue_pos,
+				my_count*sizeof(Sample));
+			rt->sample_queue_pos += my_count;
+		}
+		if (rt->sample_queue_count <= my_queue_size*2)
+			pthread_cond_signal(&rt->worker_ready_cond);
 		pthread_mutex_unlock(&rt->sample_queue_mutex);
 
 		// do the work
@@ -456,16 +459,18 @@
 	if (!sampler || !camera || !top)
 		return;
 
-	static const int sample_queue_size = 2000;
-	sample_queue = new Sample [sample_queue_size+1];
+	static const int my_count_max = 256;
+	sample_queue_size = my_count_max*2*num_threads;
+	sample_queue = new Sample [sample_queue_size];
 	sample_queue_pos = 0;
 	sample_queue_count = 0;
 	end_of_samples = false;
-	static const int my_count_max = 512;
-	int  my_count, my_pos = 0;
+	int my_count, my_pos = 0;
 	int sampnum = 0, sampdone;
 	bool more_samples;
 
+	sampler->init();
+
 	// create workers
 	dbgmsg(1, "* running %d threads\n", num_threads);
 	pthread_t threads[num_threads];
@@ -482,7 +487,6 @@
 	dbgmsg(2, "-  0%% done");
 
 	pthread_mutex_lock(&sampler_mutex);
-	sampler->init();
 	while ( (sampnum = sampler->initSampleSet()) > 0 )
 	{
 		sampdone = 0;
@@ -492,12 +496,12 @@
 			while ( more_samples = sampler->nextSample(&sample_queue[my_pos++]) )
 			{
 				my_count++;
+				if (my_pos >= sample_queue_size)
+					my_pos = 0;
 				if (my_count >= my_count_max)
 					break;
-				if (my_pos >= sample_queue_size)
-					my_pos = 0;
 			}
-			if (!more_samples)
+			if (!more_samples && !my_count)
 				break;
 
 			pthread_mutex_unlock(&sampler_mutex);
@@ -505,17 +509,15 @@
 			sample_queue_count += my_count;
 
 			// wait for workers if there is enough samples ready on queue
-			while (sample_queue_count > my_count_max)
-			{
-				pthread_cond_signal(&sample_queue_cond);
+			while (sample_queue_count > (2*num_threads-1)*my_count_max)
 				pthread_cond_wait(&worker_ready_cond, &sample_queue_mutex);
-			}
+
+			pthread_cond_signal(&sample_queue_cond);
+			pthread_mutex_unlock(&sample_queue_mutex);
 
 			sampdone += my_count;
 			dbgmsg(2, "\b\b\b\b\b\b\b\b%2d%% done", (sampdone - sample_queue_count)*100/sampnum);
 
-			pthread_cond_signal(&sample_queue_cond);
-			pthread_mutex_unlock(&sample_queue_mutex);
 			pthread_mutex_lock(&sampler_mutex);
 		}
 		dbgmsg(2, "\b\b\b\b\b\b\b\b100%% done\n");