|
| 1 | +--- |
| 2 | +weight: 999 |
| 3 | +title: "Parallel Merge Sort" |
| 4 | +description: "" |
| 5 | +icon: "article" |
| 6 | +date: "2025-05-04T22:58:38+08:00" |
| 7 | +lastmod: "2025-05-04T22:58:38+08:00" |
| 8 | +draft: false |
| 9 | +toc: true |
| 10 | +--- |
| 11 | + |
| 12 | +## Example Description |
| 13 | + |
| 14 | +A primary use case for OGraph is accelerating parallel computations. Taking merge sort as an example, conventional merge sort algorithms do not leverage multi-core CPU parallelism. |
| 15 | + |
| 16 | +During the merge sort process, each subsequence needs to be sorted. If these sorts can be performed concurrently, it significantly reduces computation time. |
| 17 | + |
| 18 | +In this scenario, OGraph enables task parallelism without complicating the code structure. |
| 19 | + |
| 20 | +## Example Code |
| 21 | + |
| 22 | +The following code implements a parallelized merge sort. It first defines a merge function to combine two already sorted sequences. |
| 23 | + |
| 24 | +Then, it defines sorting tasks for subsequences, merges them into the final result using the merge function, and defines a validation task to check the final result. |
| 25 | + |
| 26 | +Finally, a pipeline is created, these tasks are registered, and the pipeline is executed. |
| 27 | + |
| 28 | +(For code brevity, standard library sorting is used for subsequences rather than recursively applying the merge function as in conventional merge sort.) |
| 29 | + |
| 30 | +```go |
| 31 | +package main |
| 32 | + |
| 33 | +import ( |
| 34 | + "context" |
| 35 | + "fmt" |
| 36 | + "log" |
| 37 | + "math/rand" |
| 38 | + "slices" |
| 39 | + "sync" |
| 40 | + |
| 41 | + "github.com/symphony09/ograph" |
| 42 | +) |
| 43 | + |
| 44 | +func merge(left, right []int) []int { |
| 45 | + result := make([]int, 0, len(left)+len(right)) |
| 46 | + for len(left) > 0 || len(right) > 0 { |
| 47 | + if len(left) == 0 { |
| 48 | + return append(result, right...) |
| 49 | + } |
| 50 | + if len(right) == 0 { |
| 51 | + return append(result, left...) |
| 52 | + } |
| 53 | + if left[0] < right[0] { |
| 54 | + result = append(result, left[0]) |
| 55 | + left = left[1:] |
| 56 | + } else { |
| 57 | + result = append(result, right[0]) |
| 58 | + right = right[1:] |
| 59 | + } |
| 60 | + } |
| 61 | + return result |
| 62 | +} |
| 63 | + |
| 64 | +func main() { |
| 65 | + size := 1000 |
| 66 | + randomInts := make([]int, 0, size) |
| 67 | + sortedInts := make([]int, 0, size) |
| 68 | + mux := &sync.Mutex{} |
| 69 | + |
| 70 | + for i := 0; i < size; i++ { |
| 71 | + randomInts = append(randomInts, rand.Intn(10000)) |
| 72 | + } |
| 73 | + |
| 74 | + sortTasks := make([]*ograph.Element, 0, 10) |
| 75 | + |
| 76 | + for i := 0; i < 10; i++ { |
| 77 | + part := randomInts[i*100 : (i+1)*100] |
| 78 | + |
| 79 | + t := ograph.NewElement(fmt.Sprintf("t%d", i)).UseFn(func() error { |
| 80 | + slices.Sort(part) |
| 81 | + mux.Lock() |
| 82 | + sortedInts = merge(sortedInts, part) |
| 83 | + mux.Unlock() |
| 84 | + return nil |
| 85 | + }) |
| 86 | + |
| 87 | + sortTasks = append(sortTasks, t) |
| 88 | + } |
| 89 | + |
| 90 | + checkTask := ograph.NewElement("check").UseFn(func() error { |
| 91 | + if len(sortedInts) != 1000 || !slices.IsSorted(sortedInts) { |
| 92 | + return fmt.Errorf("got wrong sort result") |
| 93 | + } |
| 94 | + return nil |
| 95 | + }) |
| 96 | + |
| 97 | + p := ograph.NewPipeline() |
| 98 | + err := p.Register(checkTask, ograph.Rely(sortTasks...)).Run(context.Background(), nil) |
| 99 | + if err != nil { |
| 100 | + log.Fatalf("run merge sort pipeline failed, err: %v\n", err) |
| 101 | + } |
| 102 | + |
| 103 | + fmt.Println("result:", sortedInts) |
| 104 | +} |
0 commit comments