views:

43

answers:

4

Is there any thread library which can parse through code and find blocks of code which can be threaded and accordingly add the required threading instructions.

Also I want to check performance of a multithreaded program as compared to its single thread version. For this I would need to monitor the CPU usage(how much each processor is getting used). Is there any tool available to do this?

+3  A: 

I'd say the decision whether or not a given block of code can be rewritten to be multi-threaded is way too hard for an automated process to make. To make matters worse, multi-threaded code typically accesses resources outside its own scope, such as pulling data over the network, loading large files, waiting for events, executing database queries, etc.; without detailed information about all these external factors, it is impossible to decide where to go multithreaded, simply because not all the required information is in the code.

Also, a lot of code that is multi-threadable in theory will not run faster if multi-threaded, but in fact slow down.

tdammers
Its not like we can leave everything on the automation, but fragments of code which are clearly visible that they can be multithreaded( ex. : loops calling same func. with different values large number of time).
Manish
I have lots of loops calling functions with different numbers, it would be a disaster to multithread them though.
nos
A: 

Consider multi-threading as an approach to make full utilization of available resources. This is when it works the best. Consider an application which has multiple modules/areas which are multi-threadable. If all of them are made multi-threaded, the available resources might go down substantially. This could at times be detrimental to the application itself. Thus, multi-threading has to be used very carefully.

As Chris mentioned, there are a lot of profilers which do profiling for given combination of OS/language.

Tushar Tarkas
+1  A: 

Some compilers (such as recent versions of the Intel compiler and gcc) can automatically parallelize simple loops, but anything beyond that is too complex. On the other hand, there are task libraries that use thread pools, and will automatically scale the number of threads to the available processors, and divide the work between them. Of course, using such a library will require rewriting your code to do so.

Structuring your application to make best use of multithreading is not a simple matter, and requires careful thought about which parts of your application can best make use of it. This is not something that can be automated.

Anthony Williams
Yes, full automation is not possible. But it would be nice to have something which can do multithreading to fragment of codes which are present in almost all the programs. Can you point me to specific intel and gcc compiler version which can do something like this? Thanks
Manish
See the -ftree-parallelize-loops option for gcc 4.5: http://gcc.gnu.org/onlinedocs/gcc-4.5.0/gcc/Optimize-Options.html#index-ftree_002dparallelize_002dloops-764 and the -parallel option for the Intel compiler v11.1 http://software.intel.com/sites/products/documentation/hpc/compilerpro/en-us/cpp/win/compiler_c/index.htm
Anthony Williams
@Anthony: Thanks for the links.
Manish
A: 

The first thing you need to do is profile your code in a single thread and see if the areas you think are good candidates for multithreading are actually a problem. It's easy to waste a lot of time multithreading working code only to end up with a buggy mess that's slower than the original implementation if you don't carefully consider the problem first.

Wade Williams