有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java并行计算开销

我使用以下代码作为CompilerPhase类的一部分。该方法由编译器的主方法调用(并进行基准测试)

并行编译阶段:

private Consumer<ICompilationUnit> apply;
// ...

@Override
public void apply(Collection<ICompilationUnit> units)
{
    this.count = units.size();
    for (ICompilationUnit unit : units)
    {
        new Thread()
        {
            @Override
            public void run()
            {
                ParallelCompilerPhase.this.apply.accept(unit);
                ParallelCompilerPhase.this.count--;
            }
        }.start();
    }

    long now = System.currentTimeMillis();
    while (this.count > 0)
    {
        long l = System.currentTimeMillis() - now;
        if (l >= 1000L)
        {
            DyvilCompiler.logger.warning(this.name + " is taking too long! " + l + " ms");
            try
            {
                Thread.sleep(1000L);
            }
            catch (InterruptedException ex)
            {
                ex.printStackTrace();
            }
        }
    }
}

编译阶段:

private Consumer<Collection<ICompilationUnit>> apply;
//...

@Override
public void apply(Collection<ICompilationUnit> units)
{
    this.apply.accept(units);
}

对于旧的实现(CompilerPhase),编译一个编译单元的整个过程(11个不同阶段)需要40-60毫秒。然而,新的实现(ParallelCompilerPhase)增加了2000毫秒的开销。阶段TOKENIZEPARSERESOLVE_TYPESRESOLVECHECKPRINTCOMPILE使用ParallelCompilerPhase

以下是编译器的输出:

[2015-03-04 23:16:49] [INFO]: Loaded 2 Libraries (235.7 ms, 117.9 ms/L, 8.48 L/s)
[2015-03-04 23:16:49] [INFO]: Compiling 'src/test' to 'dbin'
[2015-03-04 23:16:49] [INFO]: Applying 8 States: [TOKENIZE, PARSE, RESOLVE_TYPES, RESOLVE, CHECK, PRINT, COMPILE, TEST]
[2015-03-04 23:16:49] [INFO]: Compiling 2 Packages, 2 Files (1 Compilation Unit)

[2015-03-04 23:16:49] [INFO]: Applying State TOKENIZE
[2015-03-04 23:16:49] [INFO]: Finished State TOKENIZE (2.4 ms, 2.4 ms/CU, 423.19 CU/s)
[2015-03-04 23:16:49] [INFO]: Applying State PARSE
[2015-03-04 23:16:50] [WARNING]: PARSE is taking too long! 1000 ms
[2015-03-04 23:16:51] [INFO]: Finished State PARSE (2005.1 ms, 2005.1 ms/CU, 0.50 CU/s)
[2015-03-04 23:16:51] [INFO]: Applying State RESOLVE_TYPES
[2015-03-04 23:16:51] [INFO]: Finished State RESOLVE_TYPES (17.1 ms, 17.1 ms/CU, 58.35 CU/s)
[2015-03-04 23:16:51] [INFO]: Applying State RESOLVE
[2015-03-04 23:16:51] [INFO]: Finished State RESOLVE (24.0 ms, 24.0 ms/CU, 41.70 CU/s)
[2015-03-04 23:16:51] [INFO]: Applying State CHECK
[2015-03-04 23:16:51] [INFO]: Finished State CHECK (0.5 ms, 0.5 ms/CU, 1838.24 CU/s)
[2015-03-04 23:16:51] [INFO]: Applying State PRINT
[2015-03-04 23:16:51] [INFO]: src/test/dyvil/test/Main.dyvil:
// ...
[2015-03-04 23:16:51] [INFO]: Finished State PRINT (42.3 ms, 42.3 ms/CU, 23.61 CU/s)
[2015-03-04 23:16:51] [INFO]: Applying State COMPILE
[2015-03-04 23:16:51] [INFO]: Finished State COMPILE (5.2 ms, 5.2 ms/CU, 192.64 CU/s)
[2015-03-04 23:16:51] [INFO]: Applying State TEST
[2015-03-04 23:16:51] [INFO]: Finished State TEST (46.0 ms, 46.0 ms/CU, 21.72 CU/s)

[2015-03-04 23:16:51] [INFO]: Compilation finished (2148.6 ms, 2148.6 ms/CU, 0.47 CU/s)
// ...
[2015-03-04 23:16:51] [INFO]: Test completed without Errors (1 ms)

但是,如果我将ParallelCompilerPhase的实现更改为:

@Override
public void apply(Collection<ICompilationUnit> units)
{
    for (ICompilationUnit unit : units)
    {
        this.apply.accept(unit);
    }
}

编译器的输出如下所示:

[2015-03-04 23:21:36] [INFO]: Dyvil Compiler 1.0.0 for Dyvil 1.0.0

[2015-03-04 23:21:36] [INFO]: Loaded 2 Libraries (245.6 ms, 122.8 ms/L, 8.14 L/s)
[2015-03-04 23:21:36] [INFO]: Compiling 'src/test' to 'dbin'
[2015-03-04 23:21:36] [INFO]: Applying 8 States: [TOKENIZE, PARSE, RESOLVE_TYPES, RESOLVE, CHECK, PRINT, COMPILE, TEST]
[2015-03-04 23:21:36] [INFO]: Compiling 2 Packages, 2 Files (1 Compilation Unit)

[2015-03-04 23:21:36] [INFO]: Applying State TOKENIZE
[2015-03-04 23:21:36] [INFO]: Finished State TOKENIZE (0.6 ms, 0.6 ms/CU, 1721.17 CU/s)
[2015-03-04 23:21:36] [INFO]: Applying State PARSE
[2015-03-04 23:21:36] [INFO]: Finished State PARSE (20.6 ms, 20.6 ms/CU, 48.59 CU/s)
[2015-03-04 23:21:36] [INFO]: Applying State RESOLVE_TYPES
[2015-03-04 23:21:36] [INFO]: Finished State RESOLVE_TYPES (8.5 ms, 8.5 ms/CU, 117.34 CU/s)
[2015-03-04 23:21:36] [INFO]: Applying State RESOLVE
[2015-03-04 23:21:36] [INFO]: Finished State RESOLVE (15.9 ms, 15.9 ms/CU, 63.07 CU/s)
[2015-03-04 23:21:36] [INFO]: Applying State CHECK
[2015-03-04 23:21:36] [INFO]: Finished State CHECK (0.2 ms, 0.2 ms/CU, 4587.16 CU/s)
[2015-03-04 23:21:36] [INFO]: Applying State PRINT
[2015-03-04 23:21:36] [INFO]: src/test/dyvil/test/Main.dyvil:
// ...
[2015-03-04 23:21:36] [INFO]: Finished State PRINT (2.1 ms, 2.1 ms/CU, 479.39 CU/s)
[2015-03-04 23:21:36] [INFO]: Applying State COMPILE
[2015-03-04 23:21:36] [INFO]: Finished State COMPILE (4.0 ms, 4.0 ms/CU, 251.76 CU/s)
[2015-03-04 23:21:36] [INFO]: Applying State TEST
[2015-03-04 23:21:36] [INFO]: Finished State TEST (0.6 ms, 0.6 ms/CU, 1686.34 CU/s)

[2015-03-04 23:21:36] [INFO]: Compilation finished (57.5 ms, 57.5 ms/CU, 17.40 CU/s)
// ...
[2015-03-04 23:21:36] [INFO]: Test completed without Errors (2 ms)

是什么导致2000毫秒的开销


作为一种可能的修复方法,将用

units.parallelStream().forEach(this.apply);

用线程方法做我最初想做的事情


共 (2) 个答案

  1. # 1 楼答案

    您正在为每个工作单元启动一个新线程。这是一个糟糕的想法。对于任何受计算限制的任务(一个不会花费大部分时间阻塞等待IO的任务),没有任何理由拥有比CPU核更多的线程。超过这个阈值只会浪费时间在线程之间切换上下文(并浪费大量时间旋转和拆卸线程,这并不便宜)。无论有多少线程在运行,处理器实际上不能同时完成比它拥有的处理器资源更多的事情

    相反,您应该考虑使用Executor为您管理thread pool,只需让工作线程从队列中弹出工作单元并执行它们

  2. # 2 楼答案

    在ParallelCompilerPhase中,会发生以下情况

    • 将创建新线程
    • 主线程一直忙于检查this.count和时间,因此新线程不会运行
    • 1000毫秒后,打印消息,主线休眠1000毫秒
    • 其他线程执行并完成
    • 主线醒了,2005.1ms过去了

    问题在于忙循环
    尝试:

    @Override
    public void apply(Collection<ICompilationUnit> units)
    {
        this.count = units.size();
        for (ICompilationUnit unit : units)
        {
            new Thread()
            {
                @Override
                public void run()
                {
                    ParallelCompilerPhase.this.apply.accept(unit);
                    ParallelCompilerPhase.this.count ;
                }
            }.start();
        }
    
        long now = System.currentTimeMillis();
        while (this.count > 0)
        {
            long l = System.currentTimeMillis() - now;
            if (l >= 1000L)
            {
                DyvilCompiler.logger.warning(this.name + " is taking too long! " + l + " ms");
                try
                {
                    Thread.sleep(1000L);
                }
                catch (InterruptedException ex)
                {
                    ex.printStackTrace();
                }
            }
            try
            {
                Thread.sleep(10L);
            }
            catch (InterruptedException ex)
            {
                ex.printStackTrace();
            }
        }
    }
    

    然而,等待线程的最佳方式是使用Thread.join(),正如@rici所建议的那样,因为这是实现这一点的“官方方式”,不会造成处理器时间的浪费。在上面的解决方案中,主线程在辅助线程完成后再等待10毫秒,在join()辅助线程完成后,主线程将立即唤醒