[Algorithms] General purpose task parallel threading approach
Brought to you by:
vexxed72
|
From: <asy...@gm...> - 2009-04-03 18:13:15
|
Hello, A while ago I started wondering about how to apply task parallel approach for a complete application, not just a simple "let's parallelize this loop" approach. What I wanted to achieve is an approach where you can have all sort of different tasks running in parallel which is what you usually have in real life applications. I think all games now have some sort of background processing where they do some cpu calculations, some wait on disk io, some wait on other tasks to complete, while there are heavy computations going on in the game logic for examle, and all require CPU. All avialable solutions have either too small scale and algorithm centric approach (like openmp or tbb) while more generic ones suffer from blocking conditions you have all over (including synchronization primitives, all sort of io, and ability to wait on other tasks to finish). I've tried to develop my own alternative and would like to have your oppinion on my approach. Main goals I tried to achieve are speed, low memory overhead and ability to handle blocking conditions somehow, (which is why I started to reinvent the wheel). To fight blocking conditions I've made a task suspension mechanics, which allows a worker thread to suspend a task when it meets something it should wait for (whether it is a mutex or an on going disk read). After task is suspended, worker thread picks up another task from the task pool, and keeps on executing it. When blocking condition ends, task is resumed, and put back into a task pool. This approach maximizes task throughput, and worker threads go to sleep only when there is nothing else to do (or all tasks are suspended). To achieve speed I avoid using any kernel structures (except one place in task scheduler which I haven't optimized yet, so expect performance to be about a million task switches per second or so). That also helped me to reduce the size of synchronization primitives. Other useful features include: - a unified way to wait on primitives (you can wait on all sort of primitives including tasks together) - task canceling - timeout on wait operations I'd like to hear your feedback on all this stuff you can down load at http://groups.google.com/group/asynclib. I've also composed few samples demonstrating some aspects of programming with the library, but in general it is no more complex than using windows threading api. I'd also ask you to comment on library specific questions in the group forum. Thanks. |