<p>I am currently designing an application that has one module which will load large amounts of data from a database and reduce it to a much smaller set by various calculations depending on the circumstances.</p>

<p>Many of the more intensive operations behave deterministically and would lend themselves to parallel processing.</p>

<p>Provided I have a loop that iterates over a large number of data chunks arriving from the db and for each one call a deterministic function without side effects, how would I make it so that the program does not wait for the function to return but rather sets the next calls going, so they could be processed in parallel? A naive approach to demonstrate the principle would do me for now.</p>

<p>I have read Google's MapReduce paper and while I could use the overall principle in a number of places, I won't, for now, target large clusters, rather it's going to be a single multi-core or multi-CPU machine for version 1.0. So currently, I'm not sure if I can actually use the library or would have to roll a dumbed-down basic version myself.</p>

<p>I am at an early stage of the design process and so far I am targeting C-something (for the speed critical bits) and Python (for the productivity critical bits) as my languages. If there are compelling reasons, I might switch, but so far I am contented with my choice.</p>

<p>Please note that I'm aware of the fact that it might take longer to retrieve the next chunk from the database than to process the current one and the whole process would then be I/O-bound. I would, however, assume for now that it isn't and in practice use a db cluster or memory caching or something else to be not I/O-bound at this point.</p>

[{'answer_id': 56782, 'author': 'EBGreen', 'author_id': 1358, 'author_profile': 'https://Stackoverflow.com/users/1358', 'pm_score': 2, 'selected': False, 'text': '<p>Well, if .net is an option, they have put a lot of effort into <a href="http://msdn.microsoft.com/en-us/concurrency/default.aspx" rel="nofollow noreferrer">Parallel Computing</a>.</p>\n'}, {'answer_id': 56785, 'author': 'Jason Cohen', 'author_id': 4926, 'author_profile': 'https://Stackoverflow.com/users/4926', 'pm_score': 2, 'selected': False, 'text': '<p>You can implement the algorithm from Google\'s <a href="http://labs.google.com/papers/mapreduce.html" rel="nofollow noreferrer">MapReduce</a> without having physically separate machines.  Just consider each of those "machines" to be "threads."  Threads are automatically distributed on multi-core machines.</p>\n'}, {'answer_id': 57151, 'author': 'Thomas Kammeyer', 'author_id': 4410, 'author_profile': 'https://Stackoverflow.com/users/4410', 'pm_score': 2, 'selected': False, 'text': '<p>If you\'re working with a compiler that will support it, I would suggest taking a look at <a href="http://www.openmp.org" rel="nofollow noreferrer">http://www.openmp.org</a> for a way of annotating your code in such a way that\ncertain loops will be parallelized.</p>\n\n<p>It does a lot more as well, and you might find it very helpful.</p>\n\n<p>Their web page reports that gcc4.2 will support openmp, for example.</p>\n'}, {'answer_id': 58007, 'author': 'Christian', 'author_id': 4074, 'author_profile': 'https://Stackoverflow.com/users/4074', 'pm_score': 3, 'selected': True, 'text': '<p>I might be missing something here, but this this seems fairly straight forward using pthreads.</p>\n\n<p>Set up a small threadpool with N threads in it and have one thread to control them all.</p>\n\n<p>The master thread simply sits in a loop doing something like:</p>\n\n<ol>\n<li>Get data chunk from DB </li>\n<li>Find next free thread If no thread is free then wait </li>\n<li>Hand over chunk to worker thread</li>\n<li>Go back and get next chunk from DB</li>\n</ol>\n\n<p>In the meantime the worker threads they sit and do:</p>\n\n<ol>\n<li>Mark myself as free</li>\n<li>Wait for the mast thread to give me a chunk of data</li>\n<li>Process the chunk of data</li>\n<li>Mark myself as free again</li>\n</ol>\n\n<p>The method by which you implement this can be as simple as two mutex controlled arrays. One has the worked threads in it (the threadpool) and the other indicated if each corresponding thread is free or busy.</p>\n\n<p>Tweak N to your liking ...</p>\n'}, {'answer_id': 66302, 'author': 'Torsten Marek', 'author_id': 9567, 'author_profile': 'https://Stackoverflow.com/users/9567', 'pm_score': 2, 'selected': False, 'text': '<p>If you still plan on using Python, you might want to have a look at <a href="http://pypi.python.org/pypi/processing" rel="nofollow noreferrer">Processing</a>. It uses processes rather than threads for parallel computing (due to the Python GIL) and provides classes for distributing "work items" onto several processes. Using the pool class, you can write code like the following:</p>\n\n<pre><code>import processing\n\ndef worker(i):\n    return i*i\nnum_workers = 2\npool = processing.Pool(num_workers)\nresult = pool.imap(worker, range(100000))\n</code></pre>\n\n<p>This is a parallel version of itertools.imap, which distributes calls over to processes. You can also use the apply_async methods of the pool and store lazy result objects in a list:</p>\n\n<pre><code>results = []\nfor i in range(10000):\n    results.append(pool.apply_async(worker, i))\n</code></pre>\n\n<p>For further reference, see <a href="http://pyprocessing.berlios.de/doc/pool-objects.html" rel="nofollow noreferrer">the documentation of the Pool class</a>.  </p>\n\n<p>Gotchas:</p>\n\n<ul>\n<li>processing uses fork(), so you have to be careful on Win32</li>\n<li>objects transferred between processes need to be pickleable</li>\n<li>if the workers are relatively fast, you can tweak chunksize, i.e.\nthe number of work items send to a worker process in one batch</li>\n<li>processing.Pool uses a background thread</li>\n</ul>\n'}, {'answer_id': 629836, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>The same thread pool is used in java. But the threads in threadpools are serialisable and sent to other computers and  deserialised to run.</p>\n'}, {'answer_id': 1637123, 'author': 'cdmh', 'author_id': 198083, 'author_profile': 'https://Stackoverflow.com/users/198083', 'pm_score': 0, 'selected': False, 'text': '<p>I have developed a MapReduce library for multi-threaded/multi-core use on a single server. Everything is taken care of by the library, and the user just has to implement Map and Reduce. It is positioned as a Boost library, but not yet accepted as a formal lib. Check out <a href="http://www.craighenderson.co.uk/mapreduce" rel="nofollow noreferrer">http://www.craighenderson.co.uk/mapreduce</a></p>\n'}, {'answer_id': 1637149, 'author': 'mouviciel', 'author_id': 45249, 'author_profile': 'https://Stackoverflow.com/users/45249', 'pm_score': 0, 'selected': False, 'text': '<p>You may be interested in examining the code of <a href="http://libdispatch.macosforge.org/" rel="nofollow noreferrer">libdispatch</a>, which is the open source implementation of Apple\'s Grand Central Dispatch.</p>\n'}, {'answer_id': 1637228, 'author': 'piotr', 'author_id': 72784, 'author_profile': 'https://Stackoverflow.com/users/72784', 'pm_score': 0, 'selected': False, 'text': "<p>Intel's TBB or boost::mpi might be of interest to you also.</p>\n"}]
['https://Stackoverflow.com/questions/56769', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2077/']