<html><head><style type='text/css'>p { margin: 0; }</style></head><body><div style='font-family: arial,helvetica,sans-serif; font-size: 12pt; color: #000000'><font face="arial, helvetica, sans-serif" style="color: rgb(0, 0, 0); font-size: 12pt;">Well, you're in good company! It's not an easy problem. For most languages you have to explicitly write it in a multi-threaded way. As the increase in clock speed has slowed and CPU improvement has turned to increasing the number of cores, a lot of attention has been turned toward this problem. You can, of course write multi-threaded code in C++, Java, Python and lots of languages, but it can be pretty tricky. Most of the language features were designed (IMO) when people mostly used it for big delineations, like creating a thread to handle a whole http connection, or even a little more integrated, like spawning a separate thread in a GUI app to go read the database. The implementations aren't that handy for the problem you described, for example. One of the big problems with multi-threaded programming is having different threads trying to write to the same memory, or having one thread read a variable while another thread is writing it. You can get around this with locks (synchronization), but that tends to slow things down.&nbsp;</font><div style="color: rgb(0, 0, 0); font-size: 12pt; font-family: arial, helvetica, sans-serif;"><br></div><div style="color: rgb(0, 0, 0); font-size: 12pt; font-family: arial, helvetica, sans-serif;">One way to eliminate the problem is with the "functional programming" paradigm. It's not new, but it's gaining a lot of favor lately because of the rapidly increasing numbers of cores. The main core concept is that you don't modify variables once they're assigned. No, seriously! Java is adding language features in Java 8 to make functional programming much easier, along with a lot of additions to the core APIs. For example, to print the sum of double all the numbers in a list in Java 7 you might write:</div><div style="color: rgb(0, 0, 0); font-size: 12pt; font-family: arial, helvetica, sans-serif;"><br></div><div><div style="color: rgb(0, 0, 0); font-size: 12pt;"><font face="courier new, courier, monaco, monospace, sans-serif">&nbsp; &nbsp; List&lt;Integer&gt; numbers = Arrays.asList(1, 2, 3, 4, 5, 6);</font></div><div style="color: rgb(0, 0, 0); font-size: 12pt;"><font face="courier new, courier, monaco, monospace, sans-serif">&nbsp; &nbsp;&nbsp;<span style="font-size: 12pt;">&nbsp;&nbsp;</span></font></div><div style="color: rgb(0, 0, 0); font-size: 12pt;"><font face="courier new, courier, monaco, monospace, sans-serif">&nbsp; &nbsp; int totalOfValuesDoubled = 0;</font></div><div style="color: rgb(0, 0, 0); font-size: 12pt;"><font face="courier new, courier, monaco, monospace, sans-serif">&nbsp; &nbsp; for(int number : numbers) {</font></div><div style="color: rgb(0, 0, 0); font-size: 12pt;"><font face="courier new, courier, monaco, monospace, sans-serif">&nbsp; &nbsp; &nbsp; totalOfValuesDoubled += number * 2;</font></div><div style="color: rgb(0, 0, 0); font-size: 12pt;"><font face="courier new, courier, monaco, monospace, sans-serif">&nbsp; &nbsp; }</font></div><div style="color: rgb(0, 0, 0); font-size: 12pt;"><font face="courier new, courier, monaco, monospace, sans-serif">&nbsp; &nbsp;&nbsp;</font></div><div style="color: rgb(0, 0, 0); font-size: 12pt;"><font face="courier new, courier, monaco, monospace, sans-serif">&nbsp; &nbsp; System.out.println(totalOfValuesDoubled);</font></div><div style="color: rgb(0, 0, 0); font-size: 12pt; font-family: arial, helvetica, sans-serif;"><br></div><div style="color: rgb(0, 0, 0); font-size: 12pt; font-family: arial, helvetica, sans-serif;"><span style="font-size: 12pt;">but, in Java 8, you could just write:</span></div><div style="color: rgb(0, 0, 0); font-size: 12pt;"><div><font face="courier new, courier, monaco, monospace, sans-serif">&nbsp; &nbsp; System.out.println(</font></div><div><font face="courier new, courier, monaco, monospace, sans-serif">&nbsp; &nbsp; &nbsp; numbers.stream()</font></div><div><font face="courier new, courier, monaco, monospace, sans-serif">&nbsp; &nbsp; &nbsp; .map(number -&gt; number * 2)</font></div><div><font face="courier new, courier, monaco, monospace, sans-serif">&nbsp; &nbsp; &nbsp; .sum());</font></div></div><div style="color: rgb(0, 0, 0); font-size: 12pt; font-family: arial, helvetica, sans-serif;"><br></div><div style="color: rgb(0, 0, 0); font-size: 12pt; font-family: arial, helvetica, sans-serif;">Now, if you wanted to make it multi-threaded, you could just change it to:</div><div style="color: rgb(0, 0, 0); font-size: 12pt;"><div><div><font face="courier new, courier, monaco, monospace, sans-serif">&nbsp; &nbsp; System.out.println(</font></div><div><font face="courier new, courier, monaco, monospace, sans-serif">&nbsp; &nbsp; &nbsp; numbers.parallelStream()</font></div><div><font face="courier new, courier, monaco, monospace, sans-serif">&nbsp; &nbsp; &nbsp; .map(number -&gt; number * 2)</font></div><div><font face="courier new, courier, monaco, monospace, sans-serif">&nbsp; &nbsp; &nbsp; .sum());</font></div></div><div style="font-family: arial, helvetica, sans-serif;"><br></div><div style="font-family: arial, helvetica, sans-serif;">These were taken from a presentation I saw recently on this subject. You can download some code here:</div></div><div><font face="arial, helvetica, sans-serif">http://www.agiledeveloper.com/presentations/java_8_language_capabilities.zip</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">And the presentation itself may appear sometime at devnexus.com, but it's not up yet.</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">To run the code, you'd need a special JDK v8 build from this link, as it's not merged into the main branch yet.</font></div><div><font face="arial, helvetica, sans-serif">http://jdk8.java.net/lambda/</font></div><br><hr id="zwchr" style="color: rgb(0, 0, 0); font-size: 12pt; font-family: arial, helvetica, sans-serif;"><div style="color: rgb(0, 0, 0); font-size: 12pt; font-family: Helvetica, Arial, sans-serif; font-weight: normal; font-style: normal; text-decoration: none;"><b>From: </b>"Jeff Hubbs" &lt;jhubbslist@att.net&gt;<br><b>To: </b>"Atlanta Linux Enthusiasts" &lt;ale@ale.org&gt;<br><b>Sent: </b>Friday, March 8, 2013 11:33:04 AM<br><b>Subject: </b>[ale] OT: Multi-core Utilization<br><br>My *practical* experience has a hole in it when it comes to developing <br>software to efficiently use multiple cores in a machine.<br><br>If I'm writing code in the likes of C++, Python, or Fortran <br>(acknowledging that I've got a range of programming paradigms there) and <br>let's say that I'm subtracting two 2-D arrays of floating point numbers <br>from one another element-wise, how is it that the operation gets blown <br>across multiple CPU cores in an efficient way, if at all? &nbsp;Bear in mind <br>that if this is done in Fortran, it's done in a pair of nested do-loops <br>so unless the compiler is really smart, that becomes a serial operation.<br>_______________________________________________<br>Ale mailing list<br>Ale@ale.org<br>http://mail.ale.org/mailman/listinfo/ale<br>See JOBS, ANNOUNCE and SCHOOLS lists at<br>http://mail.ale.org/mailman/listinfo<br></div><br></div></div></body></html>