I have been working on a childish little program: there are a bunch of little circles on the screen, of different colors and sizes. When a larger circle encounters a smaller circle it eats the smaller circle, and when a circle has eaten enough other circles it reproduces. It's kind of neat!
我一直在做一个幼稚的小程序:屏幕上有一堆不同颜色和大小的小圆圈。当较大的圆圈遇到较小的圆圈时,它会吃较小的圆圈,当圆圈吃掉足够的其他圆圈时,它会再现。它有点整洁!
However, the way I have it implemented, the process of detecting nearby circles and checking them for edibility is done with a for loop that cycles through the entire living population of circles... which takes longer and longer as the population tends to spike into the 3000 before it starts to drop. The process doesn't slow my computer down, I can go off and play Dawn of War or whatever and there isn't any slow down: it's just the process of checking every circle to see if it has collided with every other circle...
然而,我实现它的方式,检测附近的圆圈并检查它们的可食性的过程是通过循环遍历整个活动圈的圈子来完成的......随着人口趋于飙升,这需要更长更长的时间。在它开始下降之前的3000。这个过程不会让我的电脑慢下来,我可以去玩Dawn of War或其他任何减速器:它只是检查每个圆圈以查看它是否与其他所有圆圈相撞的过程。 。
So what occurred to me, is that I could try to separate the application window into four quadrants, and have the circles in the quadrants do their checks simultaneously, since they would have almost no chance of interfering with each other: or something to that effect!
所以我想到的是,我可以尝试将应用程序窗口分成四个象限,并让象限中的圆圈同时进行检查,因为它们几乎没有机会相互干扰:或者是那种效果!
My question, then, is: how does one make for loops that run side by side? In Java, say.
那么我的问题是:如何制作并排运行的循环?在Java中,比方说。
7 个解决方案
#1
8
Computers are usually single tasked, this means they can usually execute one instruction at a time per CPU or core.
计算机通常是单任务的,这意味着它们通常可以按CPU或核心一次执行一条指令。
However, as you have noticed, your operation system (and other programs) appear to run many tasks at the same time.
但是,正如您所注意到的,您的操作系统(和其他程序)似乎同时运行许多任务。
This is accomplished by splitting the work into processes, and each process can further implement concurrency by spawning threads. The operation system then switches between each process and thread very quickly to give the illusion of multitasking.
这是通过将工作分成进程来完成的,每个进程可以通过生成线程来进一步实现并发。然后,操作系统非常快速地在每个进程和线程之间切换,以产生多任务处理的错觉。
In your situation,your java program is a single process, and you would need to create 4 threads each running their own loop. It can get tricky, because threads need to synchronize their access to local variables, to prevent one thread editing a variable while another thread is trying to access it.
在您的情况下,您的Java程序是一个单独的进程,您需要创建4个线程,每个线程运行自己的循环。它可能变得棘手,因为线程需要同步它们对局部变量的访问,以防止一个线程在另一个线程试图访问它时编辑变量。
Because threading is a complex subject it would take far more explaining than I can do here.
因为线程是一个复杂的主题,它需要比我在这里做的更多的解释。
However, you can read Suns excellent tutorial on Concurrency, which covers everything you need to know:
但是,您可以阅读Suns关于并发的优秀教程,其中涵盖了您需要了解的所有内容:
http://java.sun.com/docs/books/tutorial/essential/concurrency/
http://java.sun.com/docs/books/tutorial/essential/concurrency/
#2
9
the problem you have here can actually be solved without threads.
这里的问题实际上可以在没有线程的情况下解决。
What you need is a spatial data structure. a quad tree would be best, or if the field in which the spheres move is fixed (i assume it is) you could use a simple grid. Heres the idea.
您需要的是空间数据结构。一个四叉树是最好的,或者如果球体移动的场是固定的(我假设它是)你可以使用一个简单的网格。继承人的想法。
Divide the display area into a square grid where each cell is at least as big as your biggest circle. for each cell keep a list (linked list is best) of all the circles whose center is in that cell. Then during the collision detection step go through each cell and check each circle in that cell against all the other circles in that cell and the surrounding cells.
将显示区域划分为正方形网格,其中每个单元格至少与最大圆圈一样大。对于每个单元格,保留其中心位于该单元格中的所有圆的列表(链表最佳)。然后在碰撞检测步骤中,遍历每个单元格并检查该单元格中的每个圆圈与该单元格中的所有其他圆圈以及周围的单元格。
technically you don't have to check all the cells around each one as some of them might have already been checked.
从技术上讲,您不必检查每个单元格周围的所有单元格,因为其中一些单元格可能已经被检查过。
you can combine this technique with multithreading techniques to get even better performance.
您可以将此技术与多线程技术相结合,以获得更好的性能。
#3
5
What you're looking for is not a way to have these run simultaneously (as people have noted, this depends on how many cores you have, and can only offer a 2x or maybe 4x speedup), but instead to somehow cut down on the number of collisions you have to detect.
您正在寻找的并不是让这些同时运行的方法(正如人们已经注意到的,这取决于您拥有多少核心,并且只能提供2倍或4倍的加速),而是以某种方式减少了你必须检测的碰撞次数。
You should look into using a quadtree. In brief, you recursively break down your 2D region into four quadrants (as needed), and then only need to detect collisions between objects in nearby components. In good cases, it can effectively reduce your collision detection time from N^2 to N * log N.
您应该考虑使用四叉树。简而言之,您递归地将2D区域分解为四个象限(根据需要),然后只需要检测附近组件中对象之间的碰撞。在良好的情况下,它可以有效地将您的碰撞检测时间从N ^ 2减少到N * log N.
#4
1
Instead of trying to do parallel-processing, you may want to look for collision detection optimization. Because in many situations, perforiming less calculations in one thread is better than distributing the calculations among multiple threads, plus it's easy to shoot yourself on the foot in this multi-threading business. Try googling "collision detection algorithm" and see where it gets you ;)
您可能希望寻找碰撞检测优化,而不是尝试进行并行处理。因为在许多情况下,在一个线程中执行较少的计算比在多个线程中分配计算要好,而且在这个多线程业务中很容易让自己陷入困境。尝试谷歌搜索“碰撞检测算法”,看看它在哪里;)
#5
0
IF your computer has multiple processors or multiple cores, then you could easily run multiple threads and run smaller parts of the loops in each thread. Many PCs these days do have multiple cores -- so have it so that each thread gets 1/nth of the loop count and then create n threads.
如果您的计算机有多个处理器或多个核心,那么您可以轻松地运行多个线程并在每个线程中运行较小的循环部分。如今许多PC都有多个核心 - 所以要让每个线程获得循环计数的1 / n,然后创建n个线程。
#6
0
If you really want to get into concurrent programming, you need to learn how to use threads.
如果你真的想进入并发编程,你需要学习如何使用线程。
Sun has a tutorial for programming Java threads here: http://java.sun.com/docs/books/tutorial/essential/concurrency/
Sun提供了一个编写Java线程的教程:http://java.sun.com/docs/books/tutorial/essential/concurrency/
#7
0
This sounds quite similar to an experiment of mine - check it out...
这听起来与我的实验非常相似 - 检查出来......
http://tinyurl.com/3fn8w8
I'm also interested in quadtrees (which is why I'm here)... hope you figured it all out.
我也对四叉树感兴趣(这就是为什么我在这里)...希望你能想到这一切。
#1
8
Computers are usually single tasked, this means they can usually execute one instruction at a time per CPU or core.
计算机通常是单任务的,这意味着它们通常可以按CPU或核心一次执行一条指令。
However, as you have noticed, your operation system (and other programs) appear to run many tasks at the same time.
但是,正如您所注意到的,您的操作系统(和其他程序)似乎同时运行许多任务。
This is accomplished by splitting the work into processes, and each process can further implement concurrency by spawning threads. The operation system then switches between each process and thread very quickly to give the illusion of multitasking.
这是通过将工作分成进程来完成的,每个进程可以通过生成线程来进一步实现并发。然后,操作系统非常快速地在每个进程和线程之间切换,以产生多任务处理的错觉。
In your situation,your java program is a single process, and you would need to create 4 threads each running their own loop. It can get tricky, because threads need to synchronize their access to local variables, to prevent one thread editing a variable while another thread is trying to access it.
在您的情况下,您的Java程序是一个单独的进程,您需要创建4个线程,每个线程运行自己的循环。它可能变得棘手,因为线程需要同步它们对局部变量的访问,以防止一个线程在另一个线程试图访问它时编辑变量。
Because threading is a complex subject it would take far more explaining than I can do here.
因为线程是一个复杂的主题,它需要比我在这里做的更多的解释。
However, you can read Suns excellent tutorial on Concurrency, which covers everything you need to know:
但是,您可以阅读Suns关于并发的优秀教程,其中涵盖了您需要了解的所有内容:
http://java.sun.com/docs/books/tutorial/essential/concurrency/
http://java.sun.com/docs/books/tutorial/essential/concurrency/
#2
9
the problem you have here can actually be solved without threads.
这里的问题实际上可以在没有线程的情况下解决。
What you need is a spatial data structure. a quad tree would be best, or if the field in which the spheres move is fixed (i assume it is) you could use a simple grid. Heres the idea.
您需要的是空间数据结构。一个四叉树是最好的,或者如果球体移动的场是固定的(我假设它是)你可以使用一个简单的网格。继承人的想法。
Divide the display area into a square grid where each cell is at least as big as your biggest circle. for each cell keep a list (linked list is best) of all the circles whose center is in that cell. Then during the collision detection step go through each cell and check each circle in that cell against all the other circles in that cell and the surrounding cells.
将显示区域划分为正方形网格,其中每个单元格至少与最大圆圈一样大。对于每个单元格,保留其中心位于该单元格中的所有圆的列表(链表最佳)。然后在碰撞检测步骤中,遍历每个单元格并检查该单元格中的每个圆圈与该单元格中的所有其他圆圈以及周围的单元格。
technically you don't have to check all the cells around each one as some of them might have already been checked.
从技术上讲,您不必检查每个单元格周围的所有单元格,因为其中一些单元格可能已经被检查过。
you can combine this technique with multithreading techniques to get even better performance.
您可以将此技术与多线程技术相结合,以获得更好的性能。
#3
5
What you're looking for is not a way to have these run simultaneously (as people have noted, this depends on how many cores you have, and can only offer a 2x or maybe 4x speedup), but instead to somehow cut down on the number of collisions you have to detect.
您正在寻找的并不是让这些同时运行的方法(正如人们已经注意到的,这取决于您拥有多少核心,并且只能提供2倍或4倍的加速),而是以某种方式减少了你必须检测的碰撞次数。
You should look into using a quadtree. In brief, you recursively break down your 2D region into four quadrants (as needed), and then only need to detect collisions between objects in nearby components. In good cases, it can effectively reduce your collision detection time from N^2 to N * log N.
您应该考虑使用四叉树。简而言之,您递归地将2D区域分解为四个象限(根据需要),然后只需要检测附近组件中对象之间的碰撞。在良好的情况下,它可以有效地将您的碰撞检测时间从N ^ 2减少到N * log N.
#4
1
Instead of trying to do parallel-processing, you may want to look for collision detection optimization. Because in many situations, perforiming less calculations in one thread is better than distributing the calculations among multiple threads, plus it's easy to shoot yourself on the foot in this multi-threading business. Try googling "collision detection algorithm" and see where it gets you ;)
您可能希望寻找碰撞检测优化,而不是尝试进行并行处理。因为在许多情况下,在一个线程中执行较少的计算比在多个线程中分配计算要好,而且在这个多线程业务中很容易让自己陷入困境。尝试谷歌搜索“碰撞检测算法”,看看它在哪里;)
#5
0
IF your computer has multiple processors or multiple cores, then you could easily run multiple threads and run smaller parts of the loops in each thread. Many PCs these days do have multiple cores -- so have it so that each thread gets 1/nth of the loop count and then create n threads.
如果您的计算机有多个处理器或多个核心,那么您可以轻松地运行多个线程并在每个线程中运行较小的循环部分。如今许多PC都有多个核心 - 所以要让每个线程获得循环计数的1 / n,然后创建n个线程。
#6
0
If you really want to get into concurrent programming, you need to learn how to use threads.
如果你真的想进入并发编程,你需要学习如何使用线程。
Sun has a tutorial for programming Java threads here: http://java.sun.com/docs/books/tutorial/essential/concurrency/
Sun提供了一个编写Java线程的教程:http://java.sun.com/docs/books/tutorial/essential/concurrency/
#7
0
This sounds quite similar to an experiment of mine - check it out...
这听起来与我的实验非常相似 - 检查出来......
http://tinyurl.com/3fn8w8
I'm also interested in quadtrees (which is why I'm here)... hope you figured it all out.
我也对四叉树感兴趣(这就是为什么我在这里)...希望你能想到这一切。