Skip to content

Commit

Permalink
Multithreading version tests successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
legendPerceptor committed Mar 19, 2021
1 parent 15361e3 commit d78b408
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
File renamed without changes
4 changes: 4 additions & 0 deletions rendr/ctx.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package rendr

import "sync"

var blendmap = map[string] rndBlend {
"over": rndBlendOver,
"max": rndBlendMax,
Expand Down Expand Up @@ -58,6 +60,8 @@ func rndCtxNew(vol *rndVolume, kern *rndKernel,
},
WtoI: [16]float64{},
MT: [9]float64{},
rWlock: sync.Mutex{},
nextTask: 0,
}
ctx.Cam.rndCameraSet(fr, at, up, nc, fc, fov, uint(size[0]), uint(size[1]), orth)
ctx.Cam.rndCameraUpdate()
Expand Down
49 changes: 48 additions & 1 deletion rendr/go.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
package rendr

import "fmt"
import (
"fmt"
"sync"
)

type threadArg struct {
ray *rndRay
cnv *rndConvo
ctx *rndCtx
odata []float64
plen uint
}

func Worker(wg*sync.WaitGroup, arg *threadArg) {
//defer wg.Done()
size0:= arg.ctx.Cam.Size[0]
for {
arg.ctx.rWlock.Lock()
jj:= arg.ctx.nextTask
arg.ctx.nextTask+=1
arg.ctx.rWlock.Unlock()
if jj>=arg.ctx.Cam.Size[1] {
// all tasks are finished
break
}
plen:=arg.plen
index:=jj*plen*size0
for ii:=uint(0); ii<size0; ii++ {
odata:= arg.odata[index:index+plen]
rndRayGo(odata, ii, jj, arg.ray, arg.cnv, arg.ctx)
index += plen
}
}
wg.Done()
}

func rndRender(img *rndImage, ctx *rndCtx, _dhi, _dvi int) {
plen:= rndProbeLen(ctx.Probe)
Expand All @@ -21,5 +55,18 @@ func rndRender(img *rndImage, ctx *rndCtx, _dhi, _dvi int) {
}
} else {
fmt.Println("Multithreading version of Gorendr")
args := make([]threadArg,ctx.ThreadNum)
var wg sync.WaitGroup

for i:=uint(0); i<ctx.ThreadNum; i++ {
args[i].plen=plen
args[i].ctx=ctx
args[i].cnv=rndConvoNew(ctx)
args[i].odata = img.Data
args[i].ray=rndNewRay()
wg.Add(1)
go Worker(&wg,&args[i])
}
wg.Wait()
}
}
6 changes: 5 additions & 1 deletion rendr/rnd.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package rendr

import "sync"

/* The rndKernel stores everything about a reconstruction kernel. The kernel
is non-zero only within [-support/2,support/2], for integer "support"
which may be odd or even (but always positive). The kernels are set up at
Expand Down Expand Up @@ -304,7 +307,8 @@ type rndCtx struct {
// student definition
WtoI [16]float64
MT [9]float64

rWlock sync.Mutex
nextTask uint
}

/*
Expand Down

0 comments on commit d78b408

Please sign in to comment.