【问题标题】:Parallelize the generation of combinations并行化组合的生成
【发布时间】:2021-07-13 12:17:44
【问题描述】:

我有一个 n 数组,每个数组包含 600 个元素。我需要通过以下方式生成所有组合:

tab1={0.6, 0.7, 0.8, ...}
tab2={0.5, 0.1, 0.3, ...}
tab3={0.8, 0.6, 0.2, ...} // 600 elements

组合:

combination1={0.6, 0.5, 0.8}
combination2={0.6, 0.5, 0.6}
...

所有可能的组合的生成是一个耗时的过程。实际上我有 6 个数组,这就是我需要并行处理的原因。

【问题讨论】:

    标签: java parallel-processing combinations


    【解决方案1】:

    您可以使用parallel stream 将计算时间减少一半左右。例如,三个double[] 300 个元素的数组的组合:

    /**
     * @param arrays an arbitrary number of arrays.
     * @return the Cartesian product of the passed arrays.
     */
    public static double[][] parallelCombinations(double[]... arrays) {
        return Arrays.stream(arrays)
                // represent each array element as a single element array
                .map(arr -> Arrays.stream(arr).mapToObj(d -> new double[]{d})
                        // Stream<double[][]>
                        .toArray(double[][]::new))
                // summation of pairs of inner arrays in parallel
                .reduce((arr1, arr2) -> Arrays.stream(arr1)
                        // comment this line to check the sequential stream
                        .parallel()
                        // combinations of inner arrays
                        .flatMap(inner1 -> Arrays.stream(arr2)
                                // merge two inner arrays into one
                                .map(inner2 -> Stream.of(inner1, inner2)
                                        .flatMapToDouble(Arrays::stream)
                                        .toArray()))
                        // array of combinations
                        .toArray(double[][]::new))
                // otherwise an empty 2d array
                .orElse(new double[][]{});
    }
    
    // test
    public static void main(String[] args) {
        // size of arrays
        int size = 300;
        // prepare arrays
        double[] arr1 = doubleArray(0, size);
        double[] arr2 = doubleArray(size, size);
        double[] arr3 = doubleArray(size * 2, size);
    
        long time = System.currentTimeMillis();
        // array of combinations
        double[][] combinations = parallelCombinations(arr1, arr2, arr3);
    
        // output
        System.out.println("Combinations: " + combinations.length);
        // Combinations: 27000000
        System.out.println(System.currentTimeMillis() - time);
        // with    .parallel() the time is - 10321
        // without .parallel() the time is - 23468
    }
    
    public static double[] doubleArray(int from, int size) {
        // sequentially filled array of doubles
        return DoubleStream.iterate(from / 10.0, d -> d + 0.1).limit(size).toArray();
    }
    

    另见:Parallelized Matrix Multiplication

    【讨论】:

      猜你喜欢
      • 2018-02-24
      • 2020-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多