两个双核处理器系统上的并行性
我编写了一个 Java 程序来绘制 Mandelbrot 图像。为了让它变得有趣,我将计算每个像素颜色的 for 循环分成两半;每一半将作为一个线程执行,从而并行化任务。在双核单 CPU 系统上,使用双线程方法与仅使用一个主线程方法的性能几乎是其两倍。我的问题是在两个双核处理器系统上,并行任务是否会在不同处理器之间分配,而不是仅利用一个处理器上的两个核心?我认为前一种情况会比后一种情况慢,这仅仅是因为 2 个 CPU 通过主板线进行通信的延迟。
有什么想法吗?
谢谢
I wrote a Java program that draw the Mandelbrot image. To make it interesting, I divided the for loop that calculates the color of each pixel into 2 halves; each half will be executed as a thread thus parallelizing the task. On a two core one cpu system, the performance of using two thread approach vs just one main thread is nearly two fold. My question is on a two dual-core processor system, will the parallelized task be split among different processor instead of just utilize the two core on one processor? I suppose the former scenario will be slower than the latter one simply because the latency of communicating between 2 CPU over the motherboard wires.
Any ideas?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
线程在哪个处理器(或核心)上运行取决于操作系统。我认为操作系统通常不会在多 CPU 或多核系统之间做出任何显着区别,因此具有 4 核的单 proc 系统上的程序将以与具有 2 个双核处理器的系统相同的方式进行调度。
一般来说,我的经验是线程或多或少均匀地分布在所有可用的处理器上。因此,如果您要查看在 4 核系统上运行的程序的 CPU 图表,您会看到每个核的利用率约为 25%。在大多数操作系统上,您可以将线程关联设置为特定的 CPU/核心,但我不确定该功能在 Java 中是否可用。
Which processor (or core) a thread is run on is something that is dependent on the operating system. I don't think the OS generally makes any significant distinction between multi-CPU or multi-core systems, so programs on a single proc system with 4 cores would be scheduled the same way as a system with 2 dual core processors.
Generally my experience has been that the threads will be more or less evenly distributed over all the available processors. So if you were to watch a CPU graph of your program running on a system with 4 cores, you would see roughly 25% utilization on each core. You can set thread affinity to a specific CPU/core on most operating systems, but I'm not sure if that functionality is available in Java.
如果我正确理解你的描述,你只有 2 个线程。不可能同时使用 4 个核心和 2 个线程。理想情况下,您至少需要与系统中的核心数量一样多的线程。由于 Mandelbrot 集的成本分布不均匀(集合中的点的计算比集合外的点的计算成本更高),最佳线程数可能会更高(我会尝试 4 倍的核心数。)
我不确定您在这里的意思,但您可能应该在线程之间划分最外层循环(通过 Y 坐标进行迭代)。这将减少两个或多个 CPU 争用同一缓存行的可能性(假设图像按行优先顺序渲染。)
注意:
Runtime.getRuntime().availableProcessors
会告诉您有多少个系统拥有的核心。If I understand your description correctly, you have only 2 threads. It is not possible to utilize 4 cores simultaneously with 2 threads. Ideally you want at least as many threads as there are cores in the system. With the non-uniform cost distribution of the Mandelbrot Set (computation is more expensive for points in the set than those outside the set) the optimal number of threads may be higher (I would try 4× the number of cores.)
I am not sure what you mean here, but you should probably divide the outermost loop (that iterates through Y coordinates) between threads. That will reduce the likelihood of two or more CPUs contending for the same cache line (assuming the image is rendered in row-major order.)
Note:
Runtime.getRuntime().availableProcessors
will tell you how many cores the system has.