2年ほど前に作成したマージ・ソートの分割処理及び併合処理を並列化したソート・プログラムをご紹介します。
キモは、CPUコアをフル稼働させるため連の組を分割し並列併合処理している点とその連の組の分割を効率よく行うためにバイナリサーチ・アルゴリズムを応用している点ですが、自作PC(Xeon 14コアx2=28コア56スレッド)でのランダムな1000万~5000万個の整数ソート性能はCPUコア数倍にまで達し理想的な結果であり、最初から最後まで完全に並列処理することによりCPUコアをフル稼働できていることによる結果と言えます。
基本ソートとして安定だけど遅い挿入ソートを利用していますが、後日、JavaのArrays.parallelSort()と比較してみたところほぼ同性能でした。が、JavaのArrays.sort()はシングルスレッドでもかなり早い...こんだけCPU使ってと考えるとなんとなく萎えます。(笑)
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
/* * Copyright (c) 2017 Sasapea * * /2017/02/07/parallesort/ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.Arrays; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; import java.util.concurrent.RecursiveAction; public class ParallelMergeSort { private static final int SIMPLE_SORT_THRESHOLD = 50; private static final int SEQUENTIAL_SORT_THRESHOLD = 50000; private ParallelMergeSort() { } public static void sort(int[] a) { sort(a, Runtime.getRuntime().availableProcessors()); } public static void sort(final int[] a, final int threads) { new ForkJoinPool(threads).invoke(new RecursiveAction() { protected void compute() { psort(Arrays.copyOf(a, a.length), a, 0, a.length, threads); } }); } private static void ssort(int[] src, int[] dest, int low, int high) { if (high - low < SIMPLE_SORT_THRESHOLD) { // // insertion sort // for (int i = low + 1; i < high; i++) { for (int j = i; (j > low) && (dest[j - 1] > dest[j]); j--) { int x = dest[j]; dest[j] = dest[j - 1]; dest[j - 1] = x; } } } else { // // merge sort // int mid = (low + high) >> 1; ssort(dest, src, low, mid); ssort(dest, src, mid, high); mergeL(src, low, mid, mid, high, dest, low, high - low); } } private static void psort(final int[] src, final int[] dest, final int low, final int high, final int threads) { if ((threads < 2) || (high - low < SEQUENTIAL_SORT_THRESHOLD)) { // // sequential sort // ssort(src, dest, low, high); } else { // // parallel sort // final int th2 = threads >> 1; final int mid = (high - low) * th2 / threads + low; ForkJoinTask<?> ft = new RecursiveAction() { protected void compute() { psort(dest, src, low, mid, th2); } }.fork(); psort(dest, src, mid, high, threads - th2); ft.join(); // // parallel merge // pmerge(src, low, mid, mid, high, dest, low, threads); } } private static void pmerge(final int[] src, final int low0, final int high0, final int low1, final int high1, final int[] dest, final int low, final int threads) { final int ln0 = high0 - low0; final int ln1 = high1 - low1; if (threads > 2) { final int th2 = threads >> 1; final int[] mid = split(src, low0, high0, low1, high1, new int[] {ln0 * th2 / threads + low0, ln1 * th2 / threads + low1}); ForkJoinTask<?> ft = new RecursiveAction() { protected void compute() { pmerge(src, low0, mid[0], low1, mid[1], dest, low, th2); } }.fork(); pmerge(src, mid[0], high0, mid[1], high1, dest, low + (mid[0] - low0) + (mid[1] - low1), threads - th2); ft.join(); } else if (threads == 2) { final int len = ln0 + ln1; final int ln2 = len >> 1; ForkJoinTask<?> ft = new RecursiveAction() { protected void compute() { mergeH(src, low0, high0 - 1, low1, high1 - 1, dest, low + len - 1, ln2 - 1); } }.fork(); mergeL(src, low0, high0, low1, high1, dest, low, len - ln2); ft.join(); } else { mergeL(src, low0, high0, low1, high1, dest, low, ln0 + ln1); } } private static int[] split(int[] src, int low0, int high0, int low1, int high1, int[] mid) { int m0 = mid[0]; int m1 = mid[1]; int l = Math.max(low0 - m0, m1 - high1); int h = Math.min(m1 - low1, high0 - m0); while (l <= h) { int m = (l + h) >> 1; if ((m0 + m >= high0) || (m1 - m >= high1)) break; if (src[m0 + m] <= src[m1 - m]) l = m + 1; else h = m - 1; } m0 += l; m1 -= l; if (m1 < high1) { while ((--m0 >= low0) && (src[m0] > src[m1])) ; ++m0; } if (m0 < high0) { while ((++m1 < high1) && (src[m0] > src[m1])) ; --m1; } mid[0] = m0; mid[1] = m1; return mid; } private static void mergeL(int[] src, int low0, int high0, int low1, int high1, int[] dest, int low, int len) { if ((low1 >= high1) || ((low0 < high0) && (src[high0 - 1] <= src[low1]))) { int ln2 = Math.min(len, high0 - low0); System.arraycopy(src, low0, dest, low, ln2); if (ln2 < len) System.arraycopy(src, low1, dest, low + ln2, len - ln2); } else if ((low0 >= high0) || ((low1 < high1) && (src[high1 - 1] < src[low0]))) { int ln2 = Math.min(len, high1 - low1); System.arraycopy(src, low1, dest, low, ln2); if (ln2 < len) System.arraycopy(src, low0, dest, low + ln2, len - ln2); } else { for (; len > 0; --len) { if (low0 >= high0) { System.arraycopy(src, low1, dest, low, len); break; } else if (low1 >= high1) { System.arraycopy(src, low0, dest, low, len); break; } else { dest[low++] = src[low0] <= src[low1] ? src[low0++] : src[low1++]; } } } } private static void mergeH(int[] src, int low0, int high0, int low1, int high1, int[] dest, int low, int len) { /*************************** * 下記引数は、-1してから呼び出すこと。* *************************** * int high0 * int high1 * int low * int len */ if ((low1 > high1) || ((low0 <= high0) && (src[high1] < src[low0]))) { int ln2 = Math.min(len, high0 - low0); System.arraycopy(src, high0 - ln2, dest, low - ln2, ln2 + 1); if (ln2 < len) System.arraycopy(src, high1 - (len - ln2), dest, low - len, len - ln2); } else if ((low0 > high0) || ((low1 <= high1) && (src[high0] <= src[low1]))) { int ln2 = Math.min(len, high1 - low1); System.arraycopy(src, high1 - ln2, dest, low - ln2, ln2 + 1); if (ln2 < len) System.arraycopy(src, high0 - (len - ln2), dest, low - len, len - ln2); } else { for (; len >= 0; --len) { if (low0 > high0) { System.arraycopy(src, high1 - len, dest, low - len, len + 1); break; } else if (low1 > high1) { System.arraycopy(src, high0 - len, dest, low - len, len + 1); break; } else { dest[low--] = src[high0] > src[high1] ? src[high0--] : src[high1--]; } } } } } |