[ale] OT: Multi-core Utilization

Scott Plante splante at insightsys.com
Fri Mar 8 13:08:33 EST 2013


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. 


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: 



List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6); 

int totalOfValuesDoubled = 0; 
for(int number : numbers) { 
totalOfValuesDoubled += number * 2; 
} 

System.out.println(totalOfValuesDoubled); 


but, in Java 8, you could just write: 

System.out.println( 
numbers.stream() 
.map(number -> number * 2) 
.sum()); 


Now, if you wanted to make it multi-threaded, you could just change it to: 


System.out.println( 
numbers.parallelStream() 
.map(number -> number * 2) 
.sum()); 


These were taken from a presentation I saw recently on this subject. You can download some code here: 
http://www.agiledeveloper.com/presentations/java_8_language_capabilities.zip 


And the presentation itself may appear sometime at devnexus.com, but it's not up yet. 


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. 
http://jdk8.java.net/lambda/ 
----- Original Message -----

From: "Jeff Hubbs" <jhubbslist at att.net> 
To: "Atlanta Linux Enthusiasts" <ale at ale.org> 
Sent: Friday, March 8, 2013 11:33:04 AM 
Subject: [ale] OT: Multi-core Utilization 

My *practical* experience has a hole in it when it comes to developing 
software to efficiently use multiple cores in a machine. 

If I'm writing code in the likes of C++, Python, or Fortran 
(acknowledging that I've got a range of programming paradigms there) and 
let's say that I'm subtracting two 2-D arrays of floating point numbers 
from one another element-wise, how is it that the operation gets blown 
across multiple CPU cores in an efficient way, if at all? Bear in mind 
that if this is done in Fortran, it's done in a pair of nested do-loops 
so unless the compiler is really smart, that becomes a serial operation. 
_______________________________________________ 
Ale mailing list 
Ale at ale.org 
http://mail.ale.org/mailman/listinfo/ale 
See JOBS, ANNOUNCE and SCHOOLS lists at 
http://mail.ale.org/mailman/listinfo 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.ale.org/pipermail/ale/attachments/20130308/1d0f50d7/attachment.html>


More information about the Ale mailing list