changeset 2478:930ca9c4e2a7 octree-diff

Fixed concurrent octree builder. The iso line cuts are now in the same quantity order of line segments but still differ. Need to have a closer look once we serve the data.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 28 Feb 2019 12:34:47 +0100
parents 9b1f0edf5fdc
children c85b16db8a02
files pkg/octree/builder.go
diffstat 1 files changed, 34 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/octree/builder.go	Thu Feb 28 12:03:02 2019 +0100
+++ b/pkg/octree/builder.go	Thu Feb 28 12:34:47 2019 +0100
@@ -18,6 +18,7 @@
 	"encoding/binary"
 	"io"
 	"log"
+	"runtime"
 	"sync"
 	"sync/atomic"
 
@@ -77,38 +78,38 @@
 		triangles[i] = int32(i)
 	}
 
-	/*
-		n := runtime.NumCPU()
+	n := runtime.NumCPU()
 
-		steps := make(chan buildStep, n)
+	steps := make(chan buildStep)
 
-		var wg sync.WaitGroup
-		for i := 0; i < n; i++ {
-			wg.Add(1)
-			go func() {
-				defer wg.Done()
-				for step := range steps {
-					step(steps)
-				}
-			}()
-		}
+	var wg sync.WaitGroup
+	for i := 0; i < n; i++ {
+		wg.Add(1)
+		go func() {
+			defer wg.Done()
+			for step := range steps {
+				step(steps)
+			}
+		}()
+	}
 
-		tb.index = append(tb.index, 0)
+	tb.index = append(tb.index, 0)
 
-		root := func(int32) {
-			close(steps)
-		}
+	root := func(int32) {
+		close(steps)
+	}
 
-		steps <- tb.buildConcurrent(
-			triangles,
-			tb.t.Min, tb.t.Max,
-			0,
-			root)
+	steps <- tb.buildConcurrent(
+		triangles,
+		tb.t.Min, tb.t.Max,
+		0,
+		root)
 
-		wg.Wait()
+	wg.Wait()
+
+	/*
+		tb.buildRecursive(triangles, tb.t.Min, tb.t.Max, 0)
 	*/
-
-	tb.buildRecursive(triangles, tb.t.Min, tb.t.Max, 0)
 	tb.index[0] = int32(len(tb.index))
 	log.Printf("info: num nodes: %d\n", tb.index[0])
 	log.Printf("info: nodes: %d leaves: %d index %d\n",
@@ -125,7 +126,7 @@
 	return func(steps chan buildStep) {
 
 		// none concurrent for small parts.
-		if len(triangles) < 1024 || depth > 8 {
+		if len(triangles) <= 1024 || depth > 8 {
 			parent(tb.buildRecursive(triangles, min, max, depth))
 			return
 		}
@@ -164,10 +165,10 @@
 			}
 		}
 
-		var used int32
+		used := new(int32)
 		for i := range quandrants {
 			if len(quandrants[i]) > 0 {
-				used++
+				*used++
 			}
 		}
 
@@ -175,16 +176,16 @@
 
 		for i := range quandrants {
 			if len(quandrants[i]) > 0 {
-				j := i
+				j := int32(i)
 				parent := func(v int32) {
-					tb.index[j] = v
-					if atomic.AddInt32(&used, -1) == 0 {
+					tb.index[pos+j] = v
+					if atomic.AddInt32(used, -1) == 0 {
 						parent(pos)
 					}
 				}
 				step := tb.buildConcurrent(
-					quandrants[j],
-					bboxes[j][0], bboxes[j][1],
+					quandrants[i],
+					bboxes[i][0], bboxes[i][1],
 					depth+1,
 					parent)
 				select {