-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
169 lines (137 loc) · 3.58 KB
/
Copy pathmain.go
File metadata and controls
169 lines (137 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// The source code based on the Computer Language Benchmarks Game
// http://benchmarksgame.alioth.debian.org/
//
// Go adaptation of binary-trees Rust #4 program - https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/binarytrees-go-8.html
// This release uses semaphores to match the number of workers with the CPU count
//
// contributed by Marcel Ibes
//
// This version extending the base code to use several memory allocators strategy.
//
// Copyright (C) 2019 Aleksei Piianin <piyanin@gmail.com>
//
package main
import (
"context"
"flag"
"fmt"
"math"
"runtime"
"sort"
"strconv"
"golang.org/x/sync/semaphore"
)
type Tree struct {
Left *Tree
Right *Tree
}
func NewTree(depth uint32, allocator Allocator) (tree *Tree) {
tree = allocator.NewTree()
if depth > uint32(0) {
tree.Right = NewTree(depth-1, allocator)
tree.Left = NewTree(depth-1, allocator)
}
return
}
func (o *Tree) ItemCheck() uint32 {
if o.Left != nil && o.Right != nil {
return uint32(1) + o.Right.ItemCheck() + o.Left.ItemCheck()
}
return 1
}
type Message struct {
Pos uint32
Text string
}
func inner(depth, iterations uint32, allocatorFabric AllocatorFactory) string {
chk := uint32(0)
for i := uint32(0); i < iterations; i++ {
chk += NewTree(
depth,
allocatorFabric(depth),
).ItemCheck()
}
return fmt.Sprintf("%d\t trees of depth %d\t check: %d",
iterations, depth, chk)
}
const minDepth = uint32(4)
func main() {
n := 0
flag.IntVar((*int)(&AllocatorSelected), "allocator", 0, "allocator type: 0 - naive, 1 - buffered")
flag.Parse()
if flag.NArg() > 0 {
n, _ = strconv.Atoi(flag.Arg(0))
}
run(uint32(n), GetAllocator())
}
func run(n uint32, allocatorFabric AllocatorFactory) {
cpuCount := runtime.NumCPU()
sem := semaphore.NewWeighted(int64(cpuCount))
maxDepth := n
if minDepth+2 > n {
maxDepth = minDepth + 2
}
depth := maxDepth + 1
messages := make(chan *Message, cpuCount)
expected := uint32(2) // initialize with the 2 summary messages we're always outputting
go func() {
for halfDepth := minDepth / 2; halfDepth < maxDepth/2+1; halfDepth++ {
depth := halfDepth * 2
iterations := uint32(1 << (maxDepth - depth + minDepth))
expected++
func(d, i, pos uint32) {
if err := sem.Acquire(context.TODO(), 1); err == nil {
go func() {
defer sem.Release(1)
messages <- &Message{pos, inner(d, i, allocatorFabric)}
}()
} else {
panic(err)
}
}(depth, iterations, expected)
}
if err := sem.Acquire(context.TODO(), 1); err == nil {
go func() {
defer sem.Release(1)
messages <- &Message{0,
fmt.Sprintf("stretch tree of depth %d\t check: %d",
depth, NewTree(
depth,
allocatorFabric(depth),
).ItemCheck())}
}()
} else {
panic(err)
}
if err := sem.Acquire(context.TODO(), 1); err == nil {
go func() {
defer sem.Release(1)
messages <- &Message{math.MaxUint32,
fmt.Sprintf("long lived tree of depth %d\t check: %d",
maxDepth, NewTree(
maxDepth,
allocatorFabric(maxDepth),
).ItemCheck())}
}()
} else {
panic(err)
}
}()
sortedMsg := make([]*Message, 0, len(messages))
for m := range messages {
sortedMsg = append(sortedMsg, m)
expected--
if expected == 0 {
close(messages)
}
}
sort.Slice(sortedMsg, func(i, j int) bool { return sortedMsg[i].Pos < sortedMsg[j].Pos })
for _, m := range sortedMsg {
fmt.Println(m.Text)
}
// MEM STATISTICS
var memStat runtime.MemStats
runtime.ReadMemStats(&memStat)
fmt.Printf("Alloc: %.3f/%.3f\n", float64(memStat.Alloc)/(1024*1024), float64(memStat.TotalAlloc)/(1024*1024))
}