Skip to content

Commit eaa0ac2

Browse files
author
George Burgess IV
committed
Initial commit
0 parents  commit eaa0ac2

13 files changed

+533
-0
lines changed

Presentation.pdf

36 MB
Binary file not shown.

benchmark.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
"time"
7+
)
8+
9+
func main() {
10+
const NUM = 50000
11+
ch := make(chan int)
12+
runtime.GOMAXPROCS(1) // Force 1 process, tops.
13+
fmt.Println("Brb, spawning", NUM, "goroutines")
14+
15+
memStatsNow := runtime.MemStats{}
16+
memStatsAfter := runtime.MemStats{}
17+
runtime.ReadMemStats(&memStatsNow)
18+
startTime := time.Now()
19+
for i := 0; i < NUM; i++ {
20+
go func(num int) { ch <- num }(i)
21+
}
22+
duration := time.Since(startTime)
23+
24+
fmt.Println("Done")
25+
fmt.Println("To prove it, we're running", runtime.NumGoroutine(), "goroutines")
26+
runtime.ReadMemStats(&memStatsAfter)
27+
diff := memStatsAfter.Alloc - memStatsNow.Alloc
28+
fmt.Println("That took", duration)
29+
fmt.Println("...Which means an average of", duration/(NUM), "per goroutine")
30+
fmt.Println("Also,", NUM, "goroutines takes", diff, "bytes")
31+
fmt.Println("...Which leaves approx", int(diff/NUM), "bytes allocated for each gorotuine. Tiny, eh?")
32+
33+
fmt.Println("\nThat's cool. Let's see how long it takes to swap the currently running goroutine", NUM*2, "times.")
34+
fmt.Println("In the interest of transparency, GOMAXPROCS =", runtime.GOMAXPROCS(0))
35+
36+
startTime2 := time.Now()
37+
for i := 0; i < NUM; i++ {
38+
<-ch
39+
}
40+
duration2 := time.Since(startTime2)
41+
42+
fmt.Println("Took", duration2)
43+
fmt.Println("...Which means", duration2/(2*NUM), "per goroutine swap")
44+
fmt.Println("Pretty good, right?")
45+
}

bitonic.go

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
"sync"
7+
"time"
8+
)
9+
10+
func bitonicComp(a, b *int) {
11+
if *a >= *b {
12+
*b, *a = *a, *b
13+
}
14+
}
15+
16+
func findPowerOfTwo(n int) int {
17+
var i int
18+
for i = 0; n != 0; i++ {
19+
n >>= 1
20+
}
21+
return i
22+
}
23+
24+
func bitonicSort(array []int) {
25+
lock := &sync.Mutex{}
26+
cond := sync.NewCond(lock)
27+
numToWait := 0
28+
29+
incSig := func(howMany int) {
30+
lock.Lock()
31+
numToWait += howMany
32+
lock.Unlock()
33+
}
34+
35+
decSig := func(howMany int) {
36+
incSig(-howMany)
37+
if numToWait <= 0 {
38+
lock.Lock()
39+
numToWait = 0
40+
cond.Broadcast()
41+
lock.Unlock()
42+
}
43+
}
44+
45+
blockOrSig := func() {
46+
lock.Lock()
47+
numToWait -= 1
48+
if numToWait == 0 {
49+
cond.Broadcast()
50+
} else {
51+
cond.Wait()
52+
}
53+
lock.Unlock()
54+
}
55+
56+
var bitonicSliceSecond func([]int, bool)
57+
bitonicSliceSecond = func(array []int, wait bool) {
58+
jmp := len(array) / 2
59+
start := 0
60+
end := start + jmp
61+
for end < len(array) {
62+
bitonicComp(&array[start], &array[end])
63+
start++
64+
end++
65+
}
66+
67+
if len(array) > 10000 {
68+
incBy := 1
69+
if wait {
70+
incBy++
71+
}
72+
incSig(1)
73+
go bitonicSliceSecond(array[:len(array)/2], true)
74+
go bitonicSliceSecond(array[len(array)/2:], true)
75+
} else {
76+
if len(array) > 2 {
77+
bitonicSliceSecond(array[:len(array)/2], false)
78+
bitonicSliceSecond(array[len(array)/2:], false)
79+
}
80+
81+
if wait {
82+
decSig(1)
83+
}
84+
}
85+
}
86+
87+
bitonicSliceFirst := func(array []int) {
88+
start := 0
89+
end := len(array) - 1
90+
for end > start {
91+
bitonicComp(&array[start], &array[end])
92+
start++
93+
end--
94+
}
95+
96+
if len(array) > 10000 {
97+
incSig(1)
98+
go bitonicSliceSecond(array[:len(array)/2], true)
99+
go bitonicSliceSecond(array[len(array)/2:], true)
100+
} else {
101+
if len(array) > 2 {
102+
bitonicSliceSecond(array[:len(array)/2], false)
103+
bitonicSliceSecond(array[len(array)/2:], false)
104+
}
105+
decSig(1)
106+
}
107+
}
108+
109+
for i := 2; i <= len(array); i <<= 1 {
110+
incSig(len(array)/i + 1)
111+
for s := 0; s < len(array); s += i {
112+
go bitonicSliceFirst(array[s : s+i])
113+
}
114+
blockOrSig()
115+
}
116+
}
117+
118+
func isSorted(array []int) bool {
119+
if len(array) < 2 {
120+
return true
121+
}
122+
123+
old := array[0]
124+
for _, n := range array[1:] {
125+
if n < old {
126+
return false
127+
}
128+
old = n
129+
}
130+
return true
131+
}
132+
133+
func parallelIsSorted(array []int, numProcs int) bool {
134+
step := len(array) / numProcs
135+
chans := []chan bool{}
136+
for i := 0; i < numProcs; i++ {
137+
c := make(chan bool, 1)
138+
chans = append(chans, c)
139+
go func(n int) {
140+
start := n * step
141+
end := (n + 1) * step
142+
if n > 0 {
143+
start--
144+
}
145+
if n+1 < numProcs {
146+
end++
147+
}
148+
c <- isSorted(array[start:end])
149+
close(c)
150+
}(i)
151+
}
152+
153+
for _, c := range chans {
154+
if !<-c {
155+
return false
156+
}
157+
}
158+
return true
159+
}
160+
161+
func main() {
162+
const PROCS = 4
163+
const NUM_ELEMENTS = 1 << 20
164+
runtime.GOMAXPROCS(4)
165+
array := make([]int, NUM_ELEMENTS)
166+
for i := 0; i < NUM_ELEMENTS; i++ {
167+
array[i] = NUM_ELEMENTS - i
168+
}
169+
170+
start := time.Now()
171+
bitonicSort(array)
172+
diff := time.Since(start)
173+
174+
if parallelIsSorted(array, PROCS) {
175+
fmt.Println("Array is sorted in", diff)
176+
} else {
177+
fmt.Println("NOOOOOOOOOOOOOOOO")
178+
for _, n := range array {
179+
fmt.Printf("%d ", n)
180+
}
181+
fmt.Println()
182+
}
183+
}

bufprintf.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
)
7+
8+
func main() {
9+
buf := bytes.NewBuffer(nil)
10+
fmt.Fprintf(buf, "Stop! %s time!", "hammer")
11+
fmt.Println("Buffer says:", string(buf.Bytes()))
12+
}

copy.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
)
8+
9+
func main() {
10+
fmt.Println("Start typing, please.")
11+
io.Copy(os.Stdout, os.Stdin)
12+
}

copy2.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io"
7+
"os"
8+
)
9+
10+
func main() {
11+
buf := bytes.NewBuffer(nil)
12+
speshulReader := io.TeeReader(os.Stdin, buf)
13+
fmt.Println("Type anything!")
14+
io.Copy(os.Stdout, speshulReader)
15+
16+
fmt.Println("Thanks for visiting! Here's a record of everything you typed, courtesy of the NSA:")
17+
buf.WriteTo(os.Stdout)
18+
}

fibs.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func fibs(c chan int64) {
6+
var a, b int64 = 1, 0
7+
8+
for i := 0; i < 10; i++ {
9+
c <- a
10+
a, b = a+b, a
11+
}
12+
13+
close(c)
14+
}
15+
16+
func main() {
17+
ch := make(chan int64)
18+
go fibs(ch)
19+
for i := 0; i < 50; i++ {
20+
num, ok := <-ch
21+
if !ok {
22+
break
23+
}
24+
fmt.Println("Got fib", num) // ok glass
25+
}
26+
}

heapsoffun.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
var nextInt = func() func() *int {
8+
i := 0
9+
return func() *int {
10+
i++
11+
next := i
12+
return &next
13+
}
14+
}()
15+
16+
func main() {
17+
ints := []*int{}
18+
for i := 0; i < 100; i++ {
19+
ints = append(ints, nextInt())
20+
}
21+
22+
for _, n := range ints {
23+
fmt.Println(*n)
24+
}
25+
}

0 commit comments

Comments
 (0)