Skip to content

Commit

Permalink
doc(example): add parallel merge sort example
Browse files Browse the repository at this point in the history
  • Loading branch information
noneback committed Nov 16, 2024
1 parent 539267f commit 62a68d7
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions examples/parallel_merge_sort/parallel_merge_sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"fmt"
"log"
"math/rand"
"os"
"slices"
"strconv"
"sync"

gtf "github.com/noneback/go-taskflow"
)

// meger sorted src to sorted dest
func mergeInto(dest, src []int) []int {
size := len(dest) + len(src)
tmp := make([]int, 0, size)
i, j := 0, 0
for i < len(dest) && j < len(src) {
if dest[i] < src[j] {
tmp = append(tmp, dest[i])
i++
} else {
tmp = append(tmp, src[j])
j++
}
}

if i < len(dest) {
tmp = append(tmp, dest[i:]...)
} else {
tmp = append(tmp, src[j:]...)
}

return tmp
}
func main() {
size := 100
radomArr := make([][]int, 10)
sortedArr := make([]int, 0, 10*size)
mutex := &sync.Mutex{}

for i := 0; i < 10; i++ {
for j := 0; j < size; j++ {
radomArr[i] = append(radomArr[i], rand.Int())
}
}

sortTasks := make([]*gtf.Task, 10)
tf := gtf.NewTaskFlow("merge sort")
done := gtf.NewTask("Done", func() {
if !slices.IsSorted(sortedArr) {
log.Fatal("Failed")
}
fmt.Println("Sorted")
fmt.Println(sortedArr[:1000])
})

for i := 0; i < 10; i++ {
sortTasks[i] = gtf.NewTask("sort_"+strconv.Itoa(i), func() {
arr := radomArr[i]
slices.Sort(arr)
mutex.Lock()
defer mutex.Unlock()
sortedArr = mergeInto(sortedArr, arr)
})

}
done.Succeed(sortTasks...)
tf.Push(sortTasks...)
tf.Push(done)

executor := gtf.NewExecutor(1000)

executor.Run(tf).Wait()

if err := gtf.Visualize(tf, os.Stdout); err != nil {
log.Fatal("V->", err)
}

if err := executor.Profile(os.Stdout); err != nil {
log.Fatal("P->", err)
}

}

0 comments on commit 62a68d7

Please sign in to comment.