tag:blogger.com,1999:blog-30437988764613993502024-03-19T05:26:16.102+01:00The dress of thoughtPatrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.comBlogger34125tag:blogger.com,1999:blog-3043798876461399350.post-34174333905946145432011-03-18T16:15:00.003+01:002011-03-18T16:28:52.527+01:00This blog has moved<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOlks3z0MrkNOk9qthtBeqhXvG27ggHkgEhzwRZfB8eBlIa_Q0_i5QtARfA3FdifD-NZm7FDoV0pEYKr2bweoc-5skTg7jDDn_K70Qe8QhgE6cg9rH4Su-k-0C9G57-YQef5znqYqoWQ/s1600/Ateji_103x75.png"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 103px; height: 75px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOlks3z0MrkNOk9qthtBeqhXvG27ggHkgEhzwRZfB8eBlIa_Q0_i5QtARfA3FdifD-NZm7FDoV0pEYKr2bweoc-5skTg7jDDn_K70Qe8QhgE6cg9rH4Su-k-0C9G57-YQef5znqYqoWQ/s200/Ateji_103x75.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5585441909133773970" /></a><br />This blog is now hosted on the Ateji web site at <a href="http://www.ateji.com/blog/">http://www.ateji.com/blog/</a>. <br /><br />Please update your RSS feeds.<br /><br />Using this new platform will make it much easier to format and post source code. Expect a wealth of technical articles about Java, multithreading, parallel programming and Ateji PX in the coming weeks. <br /><br />See you there.Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com0tag:blogger.com,1999:blog-3043798876461399350.post-44177160063210771462011-03-02T20:01:00.006+01:002011-03-02T20:24:58.566+01:00Ateji PX Free Edition availableHere at Ateji, we aim at offering innovative programming languages and tools. Running such a business is like performing a delicate balancing act: developing a good product takes time, and when the product is finally out you need to implement the right business model. An important choice is pricing.<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://upload.wikimedia.org/wikipedia/commons/thumb/f/f9/Balance_icon.svg/200px-Balance_icon.svg.png"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 200px; height: 163px;" src="http://upload.wikimedia.org/wikipedia/commons/thumb/f/f9/Balance_icon.svg/200px-Balance_icon.svg.png" border="0" alt="" /></a>Offering free software helps building a community behind the product, but does not help financing the company. This is why innovative solutions are typically launched in two steps. We have now reached the first step, where an initial stream of corporate customers guarantees a recurrent revenue. Now that we've made sure that we can feed our families, we're ready to offer a free personal edition of the software.<br /><br />I am proud to announce the availability of the <a href="http://www.ateji.com/px/free-ateji-px-download.html">Free Edition</a> of Ateji PX, the easiest Java-based parallel programming tool on the market. The Free Edition handles up to 4 cores, which is typically the upper count found in personal computers. Use it for your existing applications and future developments, and enjoy multicore programming!<br /><br />Corporate servers now commonly have 16 cores or more for computational intensive applications. Corporate users can try the core-unlimited evaluation version and purchase a license when the tests are promising. Don't leave your powerful servers idle! Depending on the application, <a href="http://www.ateji.com/px/whitepapers/Ateji%20PX%20MatMult%20Whitepaper%20v1.2.pdf?phpMyAdmin=95wsvAC1wsqrAq3j%2CM3duZU3UJ7">12x speedups on 16-core servers</a> are not uncommon.Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com0tag:blogger.com,1999:blog-3043798876461399350.post-66867967726989213322011-02-07T15:24:00.021+01:002011-02-15T13:14:14.527+01:00Java on GPU with Ateji PX<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://www.itechnews.net/wp-content/uploads/2010/10/NVIDIA-Quadro-600-Fermi-Graphics-Card.jpg"><img style="cursor:pointer; cursor:hand;width: 450px; height: 283px;" src="http://www.itechnews.net/wp-content/uploads/2010/10/NVIDIA-Quadro-600-Fermi-Graphics-Card.jpg" border="0" alt="" /></a><br /><br />An explicit parallel composition operator (<a href="http://www.ateji.com/px">Ateji PX</a>'s parallel bar) is pretty useful for hybrid CPU-GPU programming. <br /><br />All frameworks I know embed GPU code into some sort of method call, sometimes indirectly. In any case, you end up writing something like<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '><br />blablabla(); <span style="color: #004680;">// runs on CPU</span><br/><br /><span style="color: #004680;">// this code must run on the GPU</span><br />output = doSomethingOnGPU(input);<br/><br />blablabla(); <span style="color: #004680;">// runs on CPU</span><br /><br /></div><br /><br />The bottom line is that input and output data, either implicit or explicit, are passed from CPU to GPU before running the code, and then from GPU to CPU when computation terminates. In other words, the GPU spends a lot of time idle waiting for I/O.<br /><br />The key to GPU performance is to overlap data transfer and computation, so that input data is already present when a new computation starts. When coding in OpenCL or CUDA, this is done via asynchronous calls :<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '><br /><span style="color: #004680;">// start asynchronous transfer</span><br /><span style="color: #004680;">// the method call returns immediately</span><br />cudaMemcpyAsync(handle, ...);<br /><br /><span style="color: #004680;">// perform computations that do not depend</span><br /><span style="color: #004680;">// on the result of the transfer</span><br /><br /><span style="color: #004680;">// wait until the transfer is finished,</span><br /><span style="color: #004680;">// using 'handle' as a reference to the async transfer</span><br />kernel<<<grid, block>>>(handle);<br /><br /><span style="color: #004680;">// now we're sure the transfer is complete,</span><br /><span style="color: #004680;">// we can perform computations that do depend</span><br /><span style="color: #004680;">// on its result</span><br /><br /></div><br />In this example, the <span style="font-style:italic;">intent</span> is really to perform computation and communication in parallel. But since there's no notion of <span style="font-style:italic;">parallel composition</span> in OpenCL (or C, or any mainstream language for that matter), it is not possible to express directly the intent. Instead, you have to resort to this complex and not very intuitive mechanism of asynchronous calls. This is pretty low-level and you'd be happy that the compiler transform your intent into these low-level calls. That's precisely what Ateji PX does.<br /><br />So what's the <span style="font-style:italic;">intent</span>? Expressing that CPU and GPU must execute concurrently. In Ateji PX, this is done with two parallel branches, one of them bearing the <code>#GPU</code> annotation. A channel is visible from both branches and will be used for communication.<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '><br />Chan input = <span style="font-weight:bold; color: #b00000;">new</span> AsyncChan();<br />Chan output = <span style="font-weight:bold; color: #b00000;">new</span> AsyncChan()<br />[<br /> || <span style="color: #004680;">// code to be run on CPU</span><br /> ...<br /> || (#OpenCL) <span style="color: #004680;">// code to be run on GPU</span><br /> ...<br />]<br /><br /></div><br /><br />Note that running code on the CPU or on the GPU is only a matter of modifying the annotations (can also be determined at run-time). No other change is the source code is required. <br /><br />The GPU repeatedly waits for input data on channel c, performs computation and sends a result:<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '><br /> || <span style="color: #004680;">// code to be run on GPU</span><br /> <span style="font-weight:bold; color: #b00000;">for</span>(;;) <span style="font-weight:bold;">{</span><br /> input ? data; <span style="color: #004680;">// receive data from CPU on the input channel</span><br /> result = computation(data);<br /> output ! data; <span style="color: #004680;">// send result on the output channel </span><br /> <span style="font-weight:bold;">}</span><br /><br /></div><br /><br /><br />The CPU repeatedly sends input data and waits for results:<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '><br /> || <span style="color: #004680;">// code to be run on CPU</span><br /> <span style="font-weight:bold; color: #b00000;">for</span>(;;) <span style="font-weight:bold;">{</span><br /> input ! data; <br /> output ? data;<br /> ... <span style="font-weight:bold; color: #b00000;">do</span> something with the result ...<br /> <span style="font-weight:bold;">}</span><br /><br /></div><br /><br /><br />Computation and communication overlap because the communication channels have been declared as asynchronous:<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '><br />Chan input = <span style="font-weight:bold; color: #b00000;">new</span> AsyncChan();<br />Chan output = <span style="font-weight:bold; color: #b00000;">new</span> AsyncChan();<br /><br /></div><br /><br />That's all! <br /><br />The intent is clearly expressed in the source code : we have two parallel branches that communicate using channels, add a single annotation to state that a branch should run on GPU. No need to manage asynchronous calls, no need to use two different languages for coding an application, the Ateji PX compiler does all this for you. <br /><br />Code is understandable and maintainable, and can work on multicore CPUs by simply changing the #GPU annotation. You can for instance debug the application on CPU before deploying it on GPU.<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="data:image/jpg;base64,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"><img style="cursor:pointer; cursor:hand;width: 132px; height: 59px;" src="data:image/jpg;base64,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" border="0" alt="" /></a><br /><br />We prototyped a parallel version of the Mandelbrot set demo based on this idea, and achieved a speedup of 60x on a standard desktop PC. A pretty impressive speedup for just adding a #GPU annotation in the source code, isn't it ?Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com2tag:blogger.com,1999:blog-3043798876461399350.post-42700070729730567722010-12-04T14:57:00.027+01:002010-12-04T20:46:56.047+01:00Why multithreading is difficultIt is common wisdom that programming with Java threads is difficult (and even more so in other languages). But have you ever tried to figure precisely <span style="font-weight:bold;">why</span> this is so ?<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQHt-lGZJTGbDLQXdSSdgRLtBzbS_jn4M9Snigw80jOiFiO_zWrOBpggjUgNiKaPfS2a7nn2AhsBSdaI_3YavmZe7h6WAyyw7dbMukNrNLF6lDrMTkS2GzTspEuuoQdVi30sEZbJrO_w/s1600/09-AMD.jpg"><img style="border:0; float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 200px; height: 165px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQHt-lGZJTGbDLQXdSSdgRLtBzbS_jn4M9Snigw80jOiFiO_zWrOBpggjUgNiKaPfS2a7nn2AhsBSdaI_3YavmZe7h6WAyyw7dbMukNrNLF6lDrMTkS2GzTspEuuoQdVi30sEZbJrO_w/s200/09-AMD.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5546829269683678754" /></a><a href="http://www.eecs.berkeley.edu/Pubs/TechRpts/2006/EECS-2006-1.pdf">The problem with threads</a> (PDF) is a nicely written research report that tries to answer this question. In short, a thread is a notion that makes sense at the <span style="text-decoration: underline;">hardware level</span>, when you think in terms of registers and instruction pointers. But this hardware level notion should never have made its way up to the <span style="text-decoration: underline;">source code level</span>. Just like you wouldn't mix Java source code and assembly language instructions.<br /><br /><br /><br /><br /><br />Let us try to go deeper: why exactly is the notion of thread ill-suited as a programming abstraction ? The core of the problem can be expressed in one single word: <span style="font-weight:bold;">composability</span>.<br /><br />Consider for example arithmetic expressions. You can take two numbers and compose them to form an addition:<br /><blockquote><span style="font-family:monospace;">1+2</span></blockquote><br />The result of the addition is itself a number, so you can compose it again with yet another number:<br /><blockquote><span style="font-family:monospace;">(1+2)+3</span> or <span style="font-family:monospace;">3+(1+2)</span></blockquote><br />When there's no ambiguity, you can remove the parentheses altogether:<br /><blockquote><span style="font-family:monospace;">1+2+3</span></blockquote><br />In other words, arithmetic expressions can be composed using + as a composition operator (there are many other).<br /><br />The exact same process works for Java statements using sequential composition:<br /><blockquote><br /><span style="font-family:monospace;">{ a; b; }</span><br /><span style="font-family:monospace;">{ a; b; }; c;</span><br /><span style="font-family:monospace;">c; { a; b; }</span><br /><span style="font-family:monospace;">a; b; c;</span><br /></blockquote><br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpwkiDK8W7MmcK18BwPh3Ozke9dpiiZR_3AU2eRRyBt2LYdEeXr0EmI1JtToYzKS6UzQGl4nFSQclMoWhbroTxRopmuif_oIdGqw4KJqhyru2q7IbSYjisLKq44wevR_gtpkQWew9NZA/s1600/threads.jpg"><img style="border:0; float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 124px; height: 87px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpwkiDK8W7MmcK18BwPh3Ozke9dpiiZR_3AU2eRRyBt2LYdEeXr0EmI1JtToYzKS6UzQGl4nFSQclMoWhbroTxRopmuif_oIdGqw4KJqhyru2q7IbSYjisLKq44wevR_gtpkQWew9NZA/s200/threads.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5546840944827263650" /></a><br />What about threads ? Can you take two threads and combine them in order to obtain a thread ? So that it can itself be combined with yet another thread ? No, <span style="font-weight:bold;">threads do not compose</span>.<br /><br /><br /><br /><span style="font-weight:bold;">So why is composability important ?</span><br /><br />Composability provides a method for building larger pieces out of smaller ones: just compose. This is how you can build large programs by building up on smaller ones. <br /><br />You want to make sure that the program behaves as expected ? First start by making sure that each of its pieces behaves as expected. This is what unit testing is about.<br /><br />Composability also provides a method for understanding what is going on: just decompose. If a divide-by-zero exception is thrown by <span style="font-family:monospace;">{ a; b; }</span>, then it must have been thrown either from <span style="font-family:monospace;">a</span> or from <span style="font-family:monospace;">b</span>. Decomposition is our standard and intuitive way to debug a program. <br /><br />Now what about a multithreaded program ? Imagine a program with two threads that throws an exception. Can you split the program in two pieces such that the exception comes from either one or the other ? No way !<br /><br />This is precisely why multithreading is difficult. Multithreaded programs are impossible to test and debug, and making sure that they work properly requires a thorough analysis of the whole code, including pieces you didn't even know they existed. <br /><br />Parallel programming itself does not need to be difficult, the problem is that mainstream programming languages do not provide a parallel composition operator as they do for sequential composition. Ateji's technological offering consists precisely in retrofitting languages with such an operator.<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2EP4csJ4aflKKy6qqhkG0J-YhPBjCmpVECe3T17JP4lRRHEx3jle4S7grpmO-uWcne_9z1jfwSdCfJB7FU1M3LvupRVMA9KgiWOVXa3aJmruSgtp5E9MnBVdn0zPB2TA6iuopibIqHg/s1600/chou.jpg"><img style="border:0; cursor:pointer; cursor:hand;width: 116px; height: 112px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2EP4csJ4aflKKy6qqhkG0J-YhPBjCmpVECe3T17JP4lRRHEx3jle4S7grpmO-uWcne_9z1jfwSdCfJB7FU1M3LvupRVMA9KgiWOVXa3aJmruSgtp5E9MnBVdn0zPB2TA6iuopibIqHg/s200/chou.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5546847902499205954" /></a><br /><br /><span style="font-weight:bold;">Let me stress this once again:</span> multithreaded programming is difficult because of the lack of <span style="font-weight:bold;">composability</span>. Not because because of parallelism, non-determinism, or the other usual suspects. <br /><br />This is true of Java threads, but also of all structures built upon threads without providing their own composition mechanism, such as tasks (the Java Executor framework) or concurrent collections (the Java concurrency framework).Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com0tag:blogger.com,1999:blog-3043798876461399350.post-13466720527749874852010-11-27T17:09:00.028+01:002010-11-28T19:54:46.227+01:00Back from SuperComputing 2010<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLTifo2l2XQIg65GC_6h03GvcTvc7wUek7VcocoGWKVEHGk9I1seF_I8QTyGvBKeE1oY7-Zb148gA9JBRqQeJjVrLqLJRcY6SurKd_X5y6bN2EgaXfvrbMMEk1l-biBN7cdU9ypGNNwA/s1600/nola+skyline++%255B1600%255D.jpg"><img style="cursor:pointer; cursor:hand;width: 640px; height: 180px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLTifo2l2XQIg65GC_6h03GvcTvc7wUek7VcocoGWKVEHGk9I1seF_I8QTyGvBKeE1oY7-Zb148gA9JBRqQeJjVrLqLJRcY6SurKd_X5y6bN2EgaXfvrbMMEk1l-biBN7cdU9ypGNNwA/s320/nola+skyline++%255B1600%255D.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5544268092739534130" /></a><br /><span style="font-style:italic;">The New Orleans skyline from Garden District</span><br /><br />This view from the hotel at 6am was about the only time we had a chance to see the sun. SC10 has been a very busy week for the Ateji team, with a lot of business meetings (all the big guys where there) and a wealth of visitors to our booth.<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHNJVsgG387ekn-ZSdWrCmy5w-MwlNbg6Axjs0M32LHVJQhQpEyD2o_ICvMYumZv2DausdVvVBLWDMOHvyUoqVOAmOPvKEPvLru8EcUacyrpPZgMUp_pgOF2hADlcKY2S791XeyNAMBQ/s1600/2010-11-16+13.52.53.jpg"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 320px; height: 240px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHNJVsgG387ekn-ZSdWrCmy5w-MwlNbg6Axjs0M32LHVJQhQpEyD2o_ICvMYumZv2DausdVvVBLWDMOHvyUoqVOAmOPvKEPvLru8EcUacyrpPZgMUp_pgOF2hADlcKY2S791XeyNAMBQ/s320/2010-11-16+13.52.53.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5544658024276998290" /></a>From left to right: Claude, Maxence and Patrick on the Ateji booth. Most visitors were curious about this new approach to do parallel programming and spent a long time chatting and asking questions. We even had a handful of teachers interested in using Ateji PX as a tool for teaching parallel programming, as it provides a general and intuitive model of parallelism on top of Java.<br /><br /><table><br /><tr><br /><td><br />Ateji was part of the <a href="http://sc10.supercomputing.org/?pg=disrupttech.html">Disruptive Technologies</a> exhibit. You get there after submitting your technology and vision and being selected by a panel of experts of the program committee. And yes, we got a free booth! Many thanks to John and Elizabeth for making this exhibit a success.<br /></td><br /><td><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1Sk0quccfy6A32xHj8Lv4DLxyS6UMijWNaRsiqs-3sxBo7r_dAtmk_XsaYPTxMWVpJAuf3hlmWtVRNN1BrdZWteCVAfQuzpX2BFacgGBeJegSPf9q9rse_5g4dicL_ecCGuQLyylYoA/s1600/2010-11-17+09.28.34-a.jpg"><img style="cursor:pointer; cursor:hand;width: 320px; height: 274px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1Sk0quccfy6A32xHj8Lv4DLxyS6UMijWNaRsiqs-3sxBo7r_dAtmk_XsaYPTxMWVpJAuf3hlmWtVRNN1BrdZWteCVAfQuzpX2BFacgGBeJegSPf9q9rse_5g4dicL_ecCGuQLyylYoA/s320/2010-11-17+09.28.34-a.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5544661113577965698" /></a><br /><span style="font-style:italic;">Parallel lines named Desire</span><br /></td><br /></table><br /><br />Being recognized as a disruptive technology means that Ateji PX has the potential to deeply change the landscape of HPC and parallel programming in the coming years. We all hope for its success as an industry standard. The focus on disruptive technologies was emphasized by having Clayton Christensen, the author of "The Innovator's Dilemma" and "The Innovator's Solution", as <a href="http://sc10.supercomputing.org/?pg=keynote.html">keynote speaker</a>. Clayton, if you happen to read this, I'd be happy to have a chat with you.<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4VIZ-AGPT-PZYG8JLOjP25cmskamdbyYUMdA9o_FRNO-5ohZ4sdeVY4ckG7RCSalwdOrDL9b5GQT184TiZlwxM9T7rTt70pwnKNM5-moloIGKZsO9N8eN6nmsPYSAdfYzxhbTUP9_Sg/s1600/keys.jpg"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 320px; height: 288px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4VIZ-AGPT-PZYG8JLOjP25cmskamdbyYUMdA9o_FRNO-5ohZ4sdeVY4ckG7RCSalwdOrDL9b5GQT184TiZlwxM9T7rTt70pwnKNM5-moloIGKZsO9N8eN6nmsPYSAdfYzxhbTUP9_Sg/s320/keys.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5544266907170768498" /></a><br /><br />We handed out these cute USB keys labeled Ateji - unlock Java performance, that contain whitepapers, documentation and an evaluation version of the software. They had a lot of success on the SC10 booth, but also with the TSA: I carried a few hundreds of them in my suitcase, it was opened and inspected <span style="font-style:italic;">every single time</span> we boarded a plane! I now have a collection of these <a href="http://upload.wikimedia.org/wikipedia/commons/a/ad/Tsa_notice_of_baggage_inspection.jpg">flyers</a>.Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com0tag:blogger.com,1999:blog-3043798876461399350.post-56275065912771342502010-09-03T18:19:00.030+02:002010-11-27T15:45:40.749+01:00Java for HPC (High-Performance Computing)When presenting <a href="http://www.ateji.com/px">Ateji PX</a> to an audience with HPC or simulation background, I often hear definitive opinions of the kind "We'll never consider Java, it's too slow". <br /><br />This appears to be more a cultural bias than an objective statement referring to actual benchmarks. To better understand the state of Java for high-performance computing, let us browse through three recent publications:<br /><ul><br /><li><a href="http://www.spscicomp.org/ScicomP15/slides/astro/torra.pdf">High-performance computing in Java: the data processing of Gaia</a>, by Luri & Torra, presented at SciComp XXL, May 2009 [1]</li><br /><li><a href="http://www.des.udc.es/~gltaboada/papers/taboada_pppj09_slides.pdf">Java for High Performance Computing: Assessment of Current Research and Practice</a>, by Taboada et.al., presented at PPPJ’09 [2]</li><br /><li><a href="http://proactive.inria.fr/userfiles/file/papers/ProActiveJavaStatusforHPC.pdf">Current State of Java for HPC</a>, by Amedro et.al., Inria Research Report N° 0353, August 2008 [3]</li><br /></ul><br /><br /><span style="font-weight:bold;">Java is used happily for huge HPC projects</span><br /><br />At the European Space Agency, Java has been chosen for large CPU and data handling needs in the order of 10^21 flops and 10^15 bytes. That's a lot of zeroes, there is no doubt here we're talking here about high-performance computing. <br /><blockquote>"We are happy with the decision made and haven’t (yet) faced any major drawback due to the choice of language" [1].</blockquote><br /><br />"HPC developers and users usually want to use Java in their projects" [2]. Indeed, Java has many advantages over traditional HPC languages:<br /><ul><br /><li>faster development</li><br /><li>higher code reliability</li><br /><li>portability</li><br /><li>adaptative run-time optimization</li><br /></ul><br />There is also a lesser known but very interesting side-effect. Since the language is cleaner, it is easier for developers to concentrate on performance optimization (rather than, say, chasing memory-allocation bugs):<br /><br /><blockquote>"In Mare Nostrum the Java version runs about four times <span style="font-weight:bold;">faster</span> than the C version [...]. Obviously, an optimisation of the C code should make it much more efficient, to at least the level of the Java code. However, this shows how the same developer did a quicker and better job in Java (a language that, unlike C, he was unfamiliar with)" [1].</blockquote><br /><br /><span style="font-weight:bold;">Java code is no longer slow [2].</span><br /><br />All benchmarks show that Java is not any slower than C++. This was the case ten years ago, and the deceptive results of the JavaGrande initiative for promoting Java for HPC gave it a bad reputation. But recent JVMs (Java Virtual Machines) do an impressive job of aggressive runtime optimization, adapting to the specific hardware it is running on and dynamically optimizing critical code fragments.<br /><br />However, performance varies greatly, depending on the JVM used (version and vendor) and the kind of computation performed [1]. One lesson to be remembered is that you should always test and benchmark your application with several recent JVMs from different vendors, as well as different command line arguments (compare -client and -server).<br /><br /><span style="font-weight:bold;">Obviously, there are some caveats.</span><br /><br />There are still performance penalties in Java communications: pure Java libraries are not well suited for HPC ("the speed and scalability of the Java ProActive implementation are, as of today still lower than MPI" [3]), and wrapping native libraries with JNI is slow.<br /><br />The situation is improving recently with projects such as <a href="http://www.des.udc.es/~juan/papers/comcom.pdf">Java Fast Sockets</a>, <a href="http://www.springerlink.com/index/502370v652212273.pdf">Fast-MPJ</a>, <a href="http://acet.rdg.ac.uk/projects/mpj/">MPJ Express</a>, that aim at providing fast message-passing without JNI overhead.<br /><br />HPC users also lack the quality and sophistication of those available in C or Fortran (but this seems to be improving). Obviously, this is a matter of having a large enough community, and the critical mass seems to have been reached.<br /><br /><span style="font-weight:bold;">The decision process</span><br /><br />I cannot resist citing the whole part about the decision process that took place at ESA:<br /><br /><blockquote><br />FORTRAN was somewhat favoured by the scientific community but was quickly discarded; the type of system to develop would have been unmaintainable, and even not feasible in some cases. For this purpose the choice of an object-oriented approach was deemed advisable. The choice was narrowed to C++ and Java.<br /><br />The C++ versus Java debate lasted longer. “Orthodox” thinking stated that C++ should be used for High Performance Computing for performance reasons. “Heterodox” thinking suggested that the disadvantage of Java in performance was outweighted by faster development and higher code reliability.<br /><br />However, when JIT Java VMs were released we did some benchmarks to compare C++ vs Java performances (linear algebra, FFTs, etc.) . The results showed that the Java performance had become quite reasonable, even comparable to C++ code (and likely to improve!). Additionally, Java offered 100% portability and I/O was likely to be the main limiting factor rather than raw computation performance.<br /><br />Java was finally chosen as the development language for DPAC. Since then hundreds of thousands of code lines have been written for the reduction system. We are happy with the decision made and haven’t (yet) faced any major drawback due to the choice of language.<br /></blockquote><br /><br /><span style="font-weight:bold;">And now comes Ateji PX</span><br /><br />So there's a growing consensus about using Java for HPC applications, in industries such as space, particle physics, bioinformatics, finance. <br /><br />However, Java does not make parallel programming easy. It relies on libraries providing explicit threads (a concepts very ill-suited as a programming abstraction, see <a href="http://www.eecs.berkeley.edu/Pubs/TechRpts/2006/EECS-2006-1.pdf">"The Problem with Threads"</a>) or relying on Runnable-like interfaces for distributed programming, leaving all the splitting and interfacing work to the programmer.<br /><br />What we've tried to provide with Ateji PX is a simple and easy way to do parallel high-performance computing in Java. Basically, the only thing you need to learn is the parallel bar '||'. Here is the parallel 'Hello World' in Ateji PX:<br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '><br />[<br/><br /> || System.out.println("Hello");<br/><br /> || System.out.println("World");<br/><br />]<br/><br /></div><br /><br />This code contains two parallel branches, introduced by the '||' symbol. It will print either <br />Hello<br />World<br />or<br />World<br />Hello<br />depending on which branch gets scheduled first.<br /><br />Data parallelism is obtained by quantifiying parallel branches. For instance, the code below increments all N elements of array in parallel:<br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '><br />[<br/><br /> || (<span style="font-weight:bold; color: #b00000;">int</span> i: n) array[i]++;<br/><br />]<br /></div><br /><br />Communication is also part of language, and is mapped by the compiler to any available communication library, such as sockets or MPI, or via shared memory when possible. Here are two communicating branches, the first one sends a value on a channel, the second one reads the value and prints it:<br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '><br />Chan<Integer> c = <span style="font-weight:bold; color: #b00000;">new</span> Chan<Integer>();<br/><br />[<br/><br /> || c ! 123;<br/><br /> || c ? <span style="font-weight:bold; color: #b00000;">int</span> value; System.out.println(value);<br/><br />]<br/><br /></div><br /><br />There much more, have a look at the <a href="http://www.ateji.com/px/whitepapers/Ateji%20PX%20for%20Java%20v1.0.pdf">Ateji PX whitepaper</a> for a detailed survey.<br /><br />Ateji PX has proven to be efficient, demonstrating a <a href="http://www.ateji.com/px/whitepapers/Ateji%20PX%20MatMult%20Whitepaper%20v1.2.pdf">12.5x speedup on a 16-core server with a single '||'</a>. <br /><br />It has also proven to be easy to learn and use. We ran a 12-month beta testing program that demonstrated that most Java developers, given the Ateji PX documentation can write, compile and run their first parallel program within a couple of hours.<br /><br />An interesting aspect is that the source code does not depend on the actual hardware architecture being used, be it shared-memory, cluster, grid, cloud or GPU accelerator. It is the compiler that performs the boring task of mapping syntactic constructs to specific architectures and libraries.<br /><br />A free evaluation version is available for download <a href="http://www.ateji.com/px/30-day-free-trial.html">here</a>. Have fun!Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com1tag:blogger.com,1999:blog-3043798876461399350.post-88675446440038424262010-08-30T17:01:00.004+02:002010-08-30T17:15:44.876+02:00Disruptive Technology at SC'10Ateji PX has been selected for presentation at the <a href="http://sc10.supercomputing.org/?pg=disrupttech.html">Disrupted Technologies</a> exhibit part of the SuperComputing 2010 conference. <br /><br /><blockquote><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjL0rRJ_KqRNISI4cLfDUAM-ImLQZjV3vwO9uYVzNTVCJUL163uY0Ta6rjnZ6p9ZXV2FCx7uj17z8g2eiM_deHM8LUCry3RytOtfQUsK-pqC0Km0-acSY_MVI91s3xw_HZHIVpyakAMQw/s1600/Sans+titre.jpg"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 104px; height: 130px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjL0rRJ_KqRNISI4cLfDUAM-ImLQZjV3vwO9uYVzNTVCJUL163uY0Ta6rjnZ6p9ZXV2FCx7uj17z8g2eiM_deHM8LUCry3RytOtfQUsK-pqC0Km0-acSY_MVI91s3xw_HZHIVpyakAMQw/s320/Sans+titre.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5511219925451445762" /></a><br /><span style="font-style:italic;">"Each year, the SC Conference seeks out new technologies with the potential to disrupt the HPC landscape as we know it. Generally speaking, “disruptive technology” refers to drastic innovations in current practices such that they have the potential to completely transform the high-performance computing field as it currently exists — ultimately overtaking the incumbent technologies or software tools in the marketplace. For SC10, Disruptive Technologies examines new computing architectures and interfaces that will significantly impact the high-performance computing field throughout the next five to 15 years, but have not yet emerged in current systems. The Disruptive Technologies exhibits, located in the SC10 exhibit hall, will showcase technologies ranging from storage, programming, cooling and productivity software through presentations, demonstrations and an exhibit showcase.<br /><br />Selected technologies for SC10 will be on display during regular exhibit hall hours. Please stop by the booth for more information on the presentations and demonstrations schedule."</span></blockquote><br />See you in New Orleans, November 13-19.Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com0tag:blogger.com,1999:blog-3043798876461399350.post-64336064643870002142010-08-15T17:22:00.016+02:002010-08-15T18:08:07.278+02:00Explaining parallelism to my mother with the Mandelbrot demoWe have put online an Ateji PX version of the <a href="http://www.ateji.com/px/demo.html">Mandelbrot set demo</a>.<br /><br />You specify the number of processor cores to be used for the computation using a slider (lower right), ranging from 1 to the number of available cores on your computer. My mom's PC has two cores:<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhj1ex7GMmVt3TrEscKMuqrWWqTjtsY6DLxP6tj1g-cag7ZxwyaP8iHsXBHUZhzBkuetRU1r4Mq8QkGgoZwe2qyMGIpWw9wkApdDZGPPS9UbggeZqJoKIzQR6GPd4H_-gQteo78mPXa4g/s1600/md3.png"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 200px; height: 71px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhj1ex7GMmVt3TrEscKMuqrWWqTjtsY6DLxP6tj1g-cag7ZxwyaP8iHsXBHUZhzBkuetRU1r4Mq8QkGgoZwe2qyMGIpWw9wkApdDZGPPS9UbggeZqJoKIzQR6GPd4H_-gQteo78mPXa4g/s200/md3.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5505660964810414770" /></a><br />Look 'ma, when the slider is on 1, you see only one guy painting. When the slider is on 2, you see two guys painting at the same time:<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQ3jONiPc8862m4mxvwLkTpQSXTbng1f3dAUWqbLz8D6YhhNfANubq-w3y22uWbaUlPeW3Bb9m9D0Q_6idXD5iRoqlpi6CxQL_i1sMctS23o7VLMwKZjJgR4kYGWDUxNzhMvktC9DQ7w/s1600/md4.png"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 200px; height: 89px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQ3jONiPc8862m4mxvwLkTpQSXTbng1f3dAUWqbLz8D6YhhNfANubq-w3y22uWbaUlPeW3Bb9m9D0Q_6idXD5iRoqlpi6CxQL_i1sMctS23o7VLMwKZjJgR4kYGWDUxNzhMvktC9DQ7w/s200/md4.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5505662233146546498" /></a><br />Now 'ma is finally able to proudly explain what her son is busy at. We're actually using the same demo with CTO's and high-ranking managers in large corporations.<br /><br />For us developers, here's the code. Since dots are independent of each other, we use a simple for loop:<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '><br />for (<span style="font-weight:bold; color: #b00000;">int</span> x:nx, <span style="font-weight:bold; color: #b00000;">int</span> y:ny)<span style="font-weight:bold;">{</span><br /> compute( x , y );<br /><span style="font-weight:bold;">}</span><br /><br /></div><br />You can see the code used in the demo under the "Source Code" tab. Parallelizing the for loop is simply a matter of inserting parallel bars right after the for keyword:<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '><br />for ||(<span style="font-weight:bold; color: #b00000;">int</span> x:nx, <span style="font-weight:bold; color: #b00000;">int</span> y:ny)<span style="font-weight:bold;">{</span><br /> compute( x , y );<br /><span style="font-weight:bold;">}</span><br /><br /></div><br />The demo also shows some advanced features of loop splitting. By default, the work is split in blocks in order to get one block per processors. However, you can see on the Mandelbrot demo that some blocks take more time to compute than others (blacks dots take more time). The result is that although the work has been split in parallel across a number of workers, we end up waiting for the one worker who has the more black dots. This is not the most efficient way of leveraging parallel hardware.<br /><br />In such cases, the solution consists in splitting work in smaller blocks. You can play with block splitting in the "Advanced Settings" area (lower left). The effect on the source code is to insert the corresponding #BlockSize annotation:<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '><br />for ||(#BlockSize(30), <span style="font-weight:bold; color: #b00000;">int</span> x:nx, <span style="font-weight:bold; color: #b00000;">int</span> y:ny)<span style="font-weight:bold;">{</span><br /> compute( x , y );<br /><span style="font-weight:bold;">}</span><br /></div><br /><br />You can learn more about Ateji PX loop splitting in the language manual, downloadable from <a href="http://www.ateji.com/px/index.html">Ateji's web site</a>.Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com0tag:blogger.com,1999:blog-3043798876461399350.post-69440283044431930462010-07-30T11:29:00.023+02:002010-08-01T17:32:38.845+02:00Integrating π (pi) in parallel<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1bldMLRYiAppDst7CtPG0BwbAb4l4vItMI53VV1BUsHp10G0EdIyKkfooDy_W8cKg36V9Zf_25UELhrD0piTp7s1CCJYJK6CJdOrVzpJGmPcl6ATq4_baQEhzSa6uZaZWSonn0yoTOQ/s1600/200px-Circle_Area.svg.png"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 200px; height: 200px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1bldMLRYiAppDst7CtPG0BwbAb4l4vItMI53VV1BUsHp10G0EdIyKkfooDy_W8cKg36V9Zf_25UELhrD0piTp7s1CCJYJK6CJdOrVzpJGmPcl6ATq4_baQEhzSa6uZaZWSonn0yoTOQ/s200/200px-Circle_Area.svg.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5499631681441669586" /></a>A simple way of computing the constant <a href="http://en.wikipedia.org/wiki/Pi">π (pi)</a> consists in measuring the surface under a curve. In more algebraic terms, this amounts to integrating y = 4/(1+x*x) between 0 and 1, and in programming terms this means incrementing x in small steps and summing the corresponding y (the smaller the steps, the more accurate the result).<br /><br />Tim Mattson in his blog entry <a href="http://software.intel.com/en-us/articles/writing-parallel-programs-a-multi-language-tutorial-introduction/">"Writing Parallel Programs: a multi-language tutorial introduction"</a> explores available tools for coding this algorithm in parallel, namely <a href="http://en.wikipedia.org/wiki/Openmp">OpenMP</a>, <a href="http://en.wikipedia.org/wiki/Message_Passing_Interface">MPI</a> and <a href="http://download.oracle.com/javase/tutorial/essential/concurrency/threads.html">Java threads</a>.<br /><br />Here we will stick to the Java universe, and compare Java sequential and multi-threaded code with their <a href="http://www.ateji.com/px">Ateji PX</a> equivalent. Impatient readers may readily jump to the Ateji PX version at the end of the article.<br /><br />The sequential Java code, inspired from Tim's sequential C version, is as follows:<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #ffffff; '><br /> <span style="font-weight:bold; color: #b00000;">static</span> <span style="font-weight:bold; color: #b00000;">final</span> <span style="font-weight:bold; color: #b00000;">int</span> numSteps = 100000; <br /> <span style="font-weight:bold; color: #b00000;">static</span> <span style="font-weight:bold; color: #b00000;">final</span> <span style="font-weight:bold; color: #b00000;">double</span> step = 1.0/numSteps; ; <br /><br /> <span style="font-weight:bold; color: #b00000;">public</span> <span style="font-weight:bold; color: #b00000;">static</span> <span style="font-weight:bold; color: #b00000;">void</span> main(String[] args) <span style="font-weight:bold;">{</span><br /> <span style="font-weight:bold; color: #b00000;">double</span> sum = 0.0; <br /> <span style="font-weight:bold; color: #b00000;">for</span>(int i=0; i<= numSteps; i++) <span style="font-weight:bold;">{</span> <br /> <span style="font-weight:bold; color: #b00000;">double</span> x = (i+0.5)*step; <br /> sum = sum + 4.0/(1.0+x*x); <br /> <span style="font-weight:bold;">}</span> <br /> <span style="font-weight:bold; color: #b00000;">double</span> pi = step * sum;<br /> System.out.println(pi);<br /> System.out.println(Math.PI); <br /> <span style="font-weight:bold;">}</span><br /><br /></div><br /><br />Try to play with the value of numSteps and see the effect on precision.<br /><br />Tim parallelizes this code using threads as follows (slightly edited to make it look more Java-ish) :<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #ffffff; '><br /> <span style="font-weight:bold; color: #b00000;">static</span> <span style="font-weight:bold; color: #b00000;">int</span> nProcs = Runtime.getRuntime().availableProcessors(); <br /><br /> <span style="font-weight:bold; color: #b00000;">static</span> <span style="font-weight:bold; color: #b00000;">class</span> PIThread <span style="font-weight:bold; color: #b00000;">extends</span> Thread <br /> <span style="font-weight:bold;">{</span> <br /> <span style="font-weight:bold; color: #b00000;">final</span> <span style="font-weight:bold; color: #b00000;">int</span> partNumber; <br /> <span style="font-weight:bold; color: #b00000;">double</span> sum = 0.0; <br /><br /> <span style="font-weight:bold; color: #b00000;">public</span> PIThread(<span style="font-weight:bold; color: #b00000;">int</span> partNumber) <span style="font-weight:bold;">{</span> <br /> <span style="font-weight:bold; color: #b00000;">this</span>.partNumber = partNumber; <br /> <span style="font-weight:bold;">}</span> <br /><br /> <span style="font-weight:bold; color: #b00000;">public</span> <span style="font-weight:bold; color: #b00000;">void</span> run() <span style="font-weight:bold;">{</span> <br /> <span style="font-weight:bold; color: #b00000;">for</span> (<span style="font-weight:bold; color: #b00000;">int</span> i = partNumber; i < numSteps; i += nProcs) <span style="font-weight:bold;">{</span> <br /> <span style="font-weight:bold; color: #b00000;">double</span> x = (i + 0.5) * step; <br /> sum += 4.0 / (1.0 + x * x); <br /> <span style="font-weight:bold;">}</span> <br /> <span style="font-weight:bold;">}</span> <br /> <span style="font-weight:bold;">}</span> <br /><br /> <span style="font-weight:bold; color: #b00000;">public</span> <span style="font-weight:bold; color: #b00000;">static</span> <span style="font-weight:bold; color: #b00000;">void</span> main(String[] args) <span style="font-weight:bold;">{</span> <br /> PIThread[] part_sums = <span style="font-weight:bold; color: #b00000;">new</span> PIThread[nProcs]; <br /> <span style="font-weight:bold; color: #b00000;">for</span>(int i = 0; i < nProcs; i++) <span style="font-weight:bold;">{</span> <br /> (part_sums[i] = <span style="font-weight:bold; color: #b00000;">new</span> PIThread(i)).start(); <br /> <span style="font-weight:bold;">}</span> <br /> <span style="font-weight:bold; color: #b00000;">double</span> sum = 0.0; <br /> <span style="font-weight:bold; color: #b00000;">for</span>(int i = 0; i < nProcs; i++) <span style="font-weight:bold;">{</span> <br /> <span style="font-weight:bold; color: #b00000;">try</span> <span style="font-weight:bold;">{</span> <br /> part_sums[i].join(); <br /> <span style="font-weight:bold;">}</span> <span style="font-weight:bold; color: #b00000;">catch</span> (InterruptedException e) <span style="font-weight:bold;">{</span><br /> <span style="font-weight:bold;">}</span> <br /> sum += part_sums[i].sum; <br /> <span style="font-weight:bold;">}</span> <br /> <span style="font-weight:bold; color: #b00000;">double</span> pi = step * sum; <br /> System.out.println(pi); <br /> <span style="font-weight:bold;">}</span> <br /><br /></div><br /><br />Pretty verbose, isn't it ? The core of the algorithm becomes hidden behind a lot of irrelevant details. <br /><br />Being verbose also means that it becomes just too easy to overlook potential problems. In this code, the handling of InterruptedException is wrong and may lead to very nasty bugs when put in the context of a larger application. Not to blame Tim: honestly, who understands the precise meaning and usage rules of InterruptedException ?<br /><br />In contrast, let us code the integration of π using Ateji PX, an extension of Java. First of all, the mathematical expression used in the integration is a typical example of a <a href="http://en.wikipedia.org/wiki/List_comprehension">comprehension</a>, for which Ateji PX provides an intuitive syntax. Here is the sequential code:<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '><br /> <span style="font-weight:bold; color: #b00000;">public</span> <span style="font-weight:bold; color: #b00000;">static</span> <span style="font-weight:bold; color: #b00000;">void</span> main(String[] args) <span style="font-weight:bold;">{</span><br /> <span style="font-weight:bold; color: #b00000;">double</span> sum = `+ <span style="font-weight:bold; color: #b00000;">for</span><span style="font-weight:bold;">{</span> 4.0/(1.0+x*x) | <span style="font-weight:bold; color: #b00000;">int</span> i : numSteps, <span style="font-weight:bold; color: #b00000;">double</span> x = (i+0.5)*step <span style="font-weight:bold;">}</span>; <br /> <span style="font-weight:bold; color: #b00000;">double</span> pi = step * sum;<br /> System.out.println(pi);<br /> <span style="font-weight:bold;">}</span><br /><br /></div><br /><br />The second line, computing sum, is very close to the standard big-sigma notation in mathematics. Having this notation available as an extension of Java makes the expression of many mathematical formulas concise and intuitive, almost like what you've learned in high school. <br /><br />It also makes the code closer to the programmer's intent. In the first sequential version, using a for loop, it takes some thinking before realizing that the code is actually computing a sum. This has a strong impact on code readability and maintenance.<br /><br />But what's really interesting is how this code can be parallelized. Simply add a parallel bar ("||") right after the for keyword, and Ateji PX will perform the computation in parallel using all available cores.<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '><br /> <span style="font-weight:bold; color: #b00000;">public</span> <span style="font-weight:bold; color: #b00000;">static</span> <span style="font-weight:bold; color: #b00000;">void</span> main(String[] args) <span style="font-weight:bold;">{</span><br /> <span style="font-weight:bold; color: #b00000;">double</span> sum = `+ <span style="font-weight:bold; color: #b00000;">for</span>||<span style="font-weight:bold;">{</span> 4.0/(1.0+x*x) | <span style="font-weight:bold; color: #b00000;">int</span> i : numSteps, <span style="font-weight:bold; color: #b00000;">double</span> x = (i+0.5)*step <span style="font-weight:bold;">}</span>; <br /> <span style="font-weight:bold; color: #b00000;">double</span> pi = step * sum;<br /> System.out.println(pi);<br /> <span style="font-weight:bold;">}</span><br /><br /></div><br /><br />In the OpenMP community, this is called a parallel reduction. Compare this code to the OpenMP version and the multi-threaded version.<br /><br />Comprehension expressions in Ateji PX are not limited to summation. They can express aggregate operations such as product, logical or, count and average, but also bulk data manipulation such as SQL-like queries and list or set comprehensions (the set of all ... such that ...), and even operate on user-defined operations.Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com0tag:blogger.com,1999:blog-3043798876461399350.post-46826324633781734902010-07-19T17:24:00.008+02:002010-08-01T17:24:45.084+02:00Ateji PX gems : Non-local exits in parallel branchesThis article is the first of a series that will explore all the lesser known gems of Ateji PX.<br /><br />Non-local exits are all the statements that take the flow of control "away" from the current local scope. In Java, they are <br /><ul><br /><li>return, <li>throw, <li>break, <li>continue<br /></ul><br />Other than Ateji PX, I do not know of any parallel programming framework that properly handles the combination of parallelism and non-local exits.<br /><br />Which is a pity, because this combination proves very useful. For one, it makes it possible to parallelize existing code without having to rewrite all the control flow, a long and error-prone operation making code difficult to read.<br /><br />It also makes it possible to design interesting algorithms specifically making use of this combination.<br /><br />A good example is speculative parallelism. "speculative" in this context means starting work before being totally sure that it is needed. This is a way to make the best use of idle cores.<br /><br />You can use speculative parallelism to put different algorithms in competition, and take the result from the first one that terminates. Here is a try at speculatively sorting an array in Ateji PX:<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '><br /> [<br /> || <span style="font-weight:bold; color: #b00000;">return</span> bubbleSortAlgorithm(array);<br /> || <span style="font-weight:bold; color: #b00000;">return</span> insertionSortAlgorithm(array); <br /> ]<br /></div><br />This code runs the two algorithms in parallel, take the result from the first algorithm that terminates and return it as the global result, stopping all remaining branches. <br /><br />Here the interesting work is done by the return statements enclosed within a parallel block. As expected, a return statement returns from the enclosing method, <i>stopping</i> all existing branches as necessary. Without the return statements, the program would wait until both branches have terminated.<br /><br />Properly stopping other threads/tasks/processes is one of the trickiest part of parallel programming. If you've ever tried it, you know what I'm talking about. With Ateji PX, you simply add a return (or break, or continue) statement inside a branch.Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com0tag:blogger.com,1999:blog-3043798876461399350.post-87443634170528900122010-07-15T14:02:00.004+02:002010-07-15T23:52:27.652+02:00Dr DobbsI have been for years an enthousiastic reader of <a href="http://www.drdobbs.com">Dr. Dobbs Journal</a>, a software magazine where programmers talk to programmers. DDJ has always been for me a reference, providing accurate and timely information with a practitioner's point of view. <br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7MT_l32qkbOlLhBRcXf1FeZGm9umrgg4ap0Z3ivSqmoiI5oypOp0d6RZDScwpiexfMPrUH6JJB7GYNq-N34TRPLXWSjDF1OqLtoYxxpH011pF00G_5-P1gf_DroirEo26FotRXqbD3g/s1600/drdobbsupdate.png"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 200px; height: 171px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7MT_l32qkbOlLhBRcXf1FeZGm9umrgg4ap0Z3ivSqmoiI5oypOp0d6RZDScwpiexfMPrUH6JJB7GYNq-N34TRPLXWSjDF1OqLtoYxxpH011pF00G_5-P1gf_DroirEo26FotRXqbD3g/s200/drdobbsupdate.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5494118818842767682" /></a>I learned a lot from DDJ about technology itself and the way to apply it. I used to subscribe to the paper edition (probably sent via sail-mail, since it always took about 3 months to reach Paris...), before it became web-only.<br /><br />This is why I take special pride in having made the headline of Dr. Dobbs Update with Ateji PX in an article by Jon Erickson, DDJ's editor in chief, titled <a href="http://www.drdobbs.com/blog/archives/2010/07/think_parallel_2.html">"Think Parallel, Think Java"</a>.Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com1tag:blogger.com,1999:blog-3043798876461399350.post-71985422512767788642010-07-05T14:53:00.010+02:002010-08-01T17:25:47.151+02:00Easy multi-core programming for all: Ateji PX is launchedI am proud to announce the first public release of <a href="http://www.ateji.com/px">Ateji PX</a>, our new language extension and programming environment for multi-core and parallel programming in Java.<br /><br />Ateji PX went through a one year technology preview program with selected customers, that confirmed our initial claims about ease of use, ease of learning, and compatibility wih existing code and tools.<br /><br />You can now <a href="http://www.ateji.com/px">download the public release</a> and discover the powerful language constructs offered by Ateji PX. Read the documentation and play with the samples for a quick introduction.<br /><br />Here is the official <a href="http://ateji.com/press-releases/2010-07-06.htm">press release</a>.<br /><br />Enjoy!Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com1tag:blogger.com,1999:blog-3043798876461399350.post-46092365988743721452010-06-01T18:12:00.004+02:002010-08-01T17:26:05.822+02:00Everything you always wanted to know about Ateji PXA gentle introduction to Ateji PX is available as a white-paper. It covers all new language constructs, and will definitely give you food for thought if you've ever had a look at the existing multi-core programming techniques.<br /><br />Download it <a href="http://www.ateji.com/multicore/whitepapers/Ateji%20PX%20for%20Java%20v1.0.pdf">here </a> (pdf, 408kb).Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com0tag:blogger.com,1999:blog-3043798876461399350.post-34707756945381062912010-04-27T17:03:00.003+02:002010-04-27T17:23:29.428+02:00A *language* in the top-10 technologiesIn its estimation of the <a href="http://www.technologyreview.com/computing/25089/">10 most important emerging technologies</a> for the year 2010, Technology Review mentions a programming language.<br /><br />Yes, a <span style="font-style:italic;">language</span>.<br /><br />When we started Ateji, the common wisdom was that language was an irrelevant artifact of the programming process. I'm glad to see that we were on the right track, and that the importance of language is now acknowledged even in the most prestigious magazines.<br /><br />We're preparing an offering for cloud programming that will let the same source code run indifferently on desktop PCs, multi-core servers, clusters, or in the cloud, while remaining compatible with existing tools and practice. This takes the form of a language extension, and wouldn't have been possible without language technology and clever language design.Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com0tag:blogger.com,1999:blog-3043798876461399350.post-73488227652500663082010-03-31T18:16:00.003+02:002010-03-31T18:29:59.727+02:00Robin MilnerProfessor Robin Milner, one of the pioneers of computer science, passed away on March 20: <a href="http://www.timesonline.co.uk/tol/comment/obituaries/article7081867.ece">http://www.timesonline.co.uk/tol/comment/obituaries/article7081867.ece</a>.<br /><br />He will be remembered, among many other achievements, as the inventor of <a href="http://en.wikipedia.org/wiki/Pi_calculus">pi-calculus</a>, the theoretical foundation behind Ateji Parallel Extensions.<br /><br />Though I never had a chance to actually meet him, he inspired my career all along, as a researcher and as an entrepreneur.Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com0tag:blogger.com,1999:blog-3043798876461399350.post-8811888838990122752010-03-27T18:22:00.010+01:002010-07-20T09:57:43.259+02:00Ateji PX vs Java's ParallelArrayThe <code>ParallelArray</code> API is a proposal for integrating data parallelism into the Java language. Here is an overview by Brian Goetz: <a href="http://www.ibm.com/developerworks/java/library/j-jtp11137.html">http://www.ibm.com/developerworks/java/library/j-jtp11137.html</a><br /><br />The canonical example for <code>ParallelArray</code> shows how to compute an average of student grades in parallel. Note the declaration of <code>students</code> as a <code>ParallelArray</code> rather than an array, the introduction of a filter 'isSenior' and a selector 'selectGpa', and the use of an <code>ExecutorService</code> 'fjPool'.<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; '><br />ParallelArray<Student> students = <span style="font-weight:bold; color: #b00000;">new</span> ParallelArray<Student>(fjPool, data);<br /><span style="font-weight:bold; color: #b00000;">double</span> bestGpa = students.withFilter(isSenior)<br /> .withMapping(selectGpa)<br /> .max();<br /><br/><br /><span style="font-weight:bold; color: #b00000;">static</span> <span style="font-weight:bold; color: #b00000;">final</span> Ops.Predicate<Student> isSenior = <span style="font-weight:bold; color: #b00000;">new</span> Ops.Predicate<Student>() <span style="font-weight:bold;">{</span><br /> <span style="font-weight:bold; color: #b00000;">public</span> <span style="font-weight:bold; color: #b00000;">boolean</span> op(Student s) <span style="font-weight:bold;">{</span><br /> <span style="font-weight:bold; color: #b00000;">return</span> s.graduationYear == Student.THIS_YEAR;<br /> <span style="font-weight:bold;">}</span><br /><span style="font-weight:bold;">}</span>;<br /><br/><br /><span style="font-weight:bold; color: #b00000;">static</span> <span style="font-weight:bold; color: #b00000;">final</span> Ops.ObjectToDouble<Student> selectGpa = <span style="font-weight:bold; color: #b00000;">new</span> Ops.ObjectToDouble<Student>() <span style="font-weight:bold;">{</span><br /> <span style="font-weight:bold; color: #b00000;">public</span> <span style="font-weight:bold; color: #b00000;">double</span> op(Student student) <span style="font-weight:bold;">{</span><br /> <span style="font-weight:bold; color: #b00000;">return</span> student.gpa;<br /> <span style="font-weight:bold;">}</span><br /><span style="font-weight:bold;">}</span>;<br /><br /></div><br /><br />Closures may help in making this code less verbose. Here is an example based on the BGGA proposal:<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; '><br />ParallelArray students = <span style="font-weight:bold; color: #b00000;">new</span> ParallelArray(fjPool, data);<br /><span style="font-weight:bold; color: #b00000;">double</span> bestGpa = students.withFilter(<span style="font-weight:bold;">{</span>Student s => (s.graduationYear == THIS_YEAR) <span style="font-weight:bold;">}</span>)<br /> .withMapping(<span style="font-weight:bold;">{</span> Student s => s.gpa <span style="font-weight:bold;">}</span>)<br /> .max();<br /><br /></div><br /><br />However, what we're interested in with this use case is expressing so-called "reduction operations" over arrays using generators, filters and selectors, and run them in parallel. Making Java a functional language is an interesting but different topic.<br /><br />Here is the syntax used by Ateji Parallel Extensions for expressing reduction operations (we actually call them "comprehensions", as in "list comprehension"). This syntax should look familiar, as it inspired by the set notation used in high school mathematics.<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '><br /> Student[] students = ...;<br /> <span style="font-weight:bold; color: #b00000;">double</span> bestGpa = `+ <span style="font-weight:bold; color: #b00000;">for</span><span style="font-weight:bold;">{</span> s.gpa | Student s : students, <span style="font-weight:bold; color: #b00000;">if</span> s.graduationYear == THIS_YEAR <span style="font-weight:bold;">}</span>;<br /><br /></div><br /><br />Note that <code>students</code> remains a classical Java array. This code is sequential, it becomes parallel when a parallel operator is inserted right after the for keyword:<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '><br /> Student[] students = ...;<br /> <span style="font-weight:bold; color: #b00000;">double</span> bestGpa = `+ <span style="font-weight:bold; color: #b00000;">for</span>||<span style="font-weight:bold;">{</span> s.gpa | Student s : students, <span style="font-weight:bold; color: #b00000;">if</span> s.graduationYear == THIS_YEAR <span style="font-weight:bold;">}</span>;<br /><br /></div><br /><br />The parallel operator on reduction expressions provides almost linear speedup on multi-core processors, as soon as the amount of computation for each array element is more than a couple of additions.<br /><br />Quiz: <br />Using the comprehension notation, how would you express the number of students graduating this year ? The average of their grades ?Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com2tag:blogger.com,1999:blog-3043798876461399350.post-21110143885651530512010-03-23T15:37:00.018+01:002010-04-03T00:44:25.710+02:00Matrix multiplication - 12.5x speedup with a single "||"Agreed, matrix multiplication is not a very sexy piece of code, but it serves as a standard example and benchmark for data-parallel computations (code that applies the same operation to many elements in parallel).<br /><br />The matrix multiplication white-paper is available from <a href="http://www.ateji.com/multicore/whitepapers.html">http://www.ateji.com/multicore/whitepapers.html</a>. It shows how we achieved a 12.5x speedup on a 16-core server, simply adding one single "||" operator to an existing sequential Java code. Raw performance is pretty good as well, on par with linear algebra libraries.<br /><br />Here is the parallel code. Note the "||" operator right after the first 'for' keyword, this is the only difference between sequential and parallel version of the code.<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '><br />for||(<span style="font-weight:bold; color: #b00000;">int</span> i : I) <span style="font-weight:bold;">{</span><br /> <span style="font-weight:bold; color: #b00000;">for</span>(int j : J) <span style="font-weight:bold;">{</span><br /> <span style="font-weight:bold; color: #b00000;">for</span>(int k : K) <span style="font-weight:bold;">{</span><br /> C[i][j] += A[i][k] * B[k][j];<br /> <span style="font-weight:bold;">}</span><br /> <span style="font-weight:bold;">}</span><br /><span style="font-weight:bold;">}</span><br /><br /></div><br /><br />Performance is pretty impressive, on par with dedicated linear algebra librairies:<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLNQ2XF_sQsluoYxxDuSjsrp_CnVuYJusMkWY5861oGRKZEcksM-c3dalGxAuy4lw2BeVlXagwHYUP64TGmOlSLl5KylTFKEBVHM1nouDlo-bQH4W5K_dwLl5n0GQIBslmJnjJpmCJBQ/s1600-h/matmult1.PNG"><img style="cursor:pointer; cursor:hand;width: 320px; height: 166px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLNQ2XF_sQsluoYxxDuSjsrp_CnVuYJusMkWY5861oGRKZEcksM-c3dalGxAuy4lw2BeVlXagwHYUP64TGmOlSLl5KylTFKEBVHM1nouDlo-bQH4W5K_dwLl5n0GQIBslmJnjJpmCJBQ/s320/matmult1.PNG" border="0" alt=""id="BLOGGER_PHOTO_ID_5451849861718134834" /></a><br /><br />The part that I find really interesting is the comparison with the same algorithm using plain Java threads. Even if you have a general knowledge about threads, you need to see actual code before you can imagine the amount of small details that need to be taken into account. <br /><br />They include adding many final keywords, copying local variables, computing indices, managing InterruptedException. 27 lines vs. 7 lines. And we haven't even returned values or thrown exception from within threads! The problem is not so much verbosity itself, but the fact that programmer's intent gets hidden behind a lot of irrelevant details.<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9f9f9; '><br /><b>Enjoyed the article?</b><br />Share your interest by voting up this article on social sites!<br /><script type="text/javascript">var dzone_style = '2';</script><br /><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script><br /></div>Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com4tag:blogger.com,1999:blog-3043798876461399350.post-76277662741977449332009-11-24T08:31:00.004+01:002010-08-01T17:26:25.503+02:00100-cores by next yearOnce again, hardware is far ahead of software. Tilera has <a href="http://tilera.com/news_&_events/press_release_091026.php">announced </a>a 100-cores processor for 2010.<br /><br />Unlike standard multi-core processors, Tilera's TILE-Gx is architectured around a 2D grid network rather than a single shared bus. This is a way to jump over the "memory wall", and feed enough data to keep all cores busy.<br /><br />This design provides a lot of raw computing power, but also better efficiency (more computing power per watt).<br /><br />But this beast is supposed to be coded in standard C/C++. It already requires black magic in order to write a 2-threads program that behaves as expected, what about 100's of threads ?<br /><br />The TILE-Gx is a perfect match for Ateji Parallel Extensions : data parallelism handles large scientific computations task parallelism handles server-like applications, and message-passing leverages the hardware's packet network interconnection mechanism. High-performance code can be arranged in a data-flow or streaming architecture, reducing accesses to shared memory.Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com1tag:blogger.com,1999:blog-3043798876461399350.post-35163890917151502232009-11-18T20:41:00.002+01:002009-11-18T20:45:35.273+01:00Session EvaluationI just received my session evaluation from the TSS Java Symposium Europe : an impressive 4.58/5.0 with the comment "Great Session !". <br /><br />I am less proud of the speaker evaluation, at 4.21/5.0. If you attended the session, I'd be happy to hear from you about what could be improved.Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com0tag:blogger.com,1999:blog-3043798876461399350.post-81816784620888292832009-11-01T16:01:00.001+01:002009-11-24T08:30:41.771+01:00Parallelism at the language level - Part 1: Hello WorldThe major contribution of <a href="http://www.ateji.com/multicore/multicore.html">Ateji Parallel Extensions</a> is to add parallelism at the language level.<br /><br />What does this change? Today's mainstream programming languages have been designed with sequential processing in mind, they simply have no idea about what is parallelism. Consider how you'd run two tasks in parallel in Java:<br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '> Thread otherThread = new Thread() <span style="font-weight:bold;">{</span><br /> void run() <span style="font-weight:bold;">{</span><br /> println("Hello"); <span style="color: #004680;">// print Hello in the other thread</span><br /> <span style="font-weight:bold;">}</span><br /> <span style="font-weight:bold;">}</span>.start();<br /> println("World"); <span style="color: #004680;">// print World in this thread</span><br /> otherThread.join(); <span style="color: #004680;">// wait until code1 has terminated</span><br /></div><br />Not to mention how unreadable and unmaintainable this code is, you'll notice that there is a fair amount of black magic involved here: just because you called a method whose name happens to be <code>start()</code>, the <i>whole</i> behaviour of your program has changed. But the compiler is not aware of this change, it thinks it is just calling an ordinary library method.<br /><br />With Ateji Parallel Extensions, two tasks are run in parallel by composing them using the || operator :<br /><br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '> println("Hello"); || println("World");<br /></div><br />How could it be simpler?<br /><br />Not only is this much more concise and understandable, it also makes it easier for the developer to "think" parallel and to catch potential errors early. <br /><br />And since the very idea of parallelism is present in the language, the compiler is able to understand the actual meaning of the code and to perform tricks such as high-level code optimization or better verification.<br /><br />Read more on <a href="http://www.ateji.com/multicore/multicore.html">parallelism at the language level</a>.Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com0tag:blogger.com,1999:blog-3043798876461399350.post-48091794536630645932009-10-30T00:42:00.007+01:002009-10-30T17:38:10.113+01:00I have been assaulted......by a fan !<br /><br />Just back from TSS Java Symposium Europe, while waiting for my bags at CDG airport, a total stranger pounced on me: <br /><br />"I saw your presentation in Prague, it was great, wonderful, so exciting, definitely the best thing that happened to Java since its creation more than a decade ago. Oh, and did I mention your were awesome?!"<br /><br />Whoaa!!! This was great for my self-esteem. My cardiologist, my wife and myself owe you a big thank you.<br /><br />My presentation about Ateji Parallel Extensions was met with two extremely opposite reactions: either totally intrigued, all ears for the whole duration. Or totally lost, pulling "what an earth is this all about and what am I doing here?" type faces.<br /><br />I'll need to improve on the tutorial aspect of this talk. There was definitely strong interest in the audience, as I could see from the large number of questions.<br /><br />The Server Side Java Symposium Europe 2009 in Prague was overall a great experience. It was rather orientated, well, "server-side" a world that I'm not that much familiar with. Lots of boring guys who try to make software actually work rather than just hacking the JVM for the fun of it. I loved the bits about practical static analysis and performance tuning. <br /><br />Other highlights were the "dinner with strangers" (not by the end), and hands-on pair programming. It was my first time, we had 30 minutes to write a method that sums factors and we hopelessly failed -- let's blame it on the Czech brandy. But I'll definitely try pair programming again with my team.<br /><br />But the best bit was Neal Ford introducing my presentation in his opening keynote speech!<br /><br />The topic was "Predicting the future". Well, first try learning lessons from the past: he began with a picture of a blacksmith in 1890. The best trade at the time, the career that you would recommend to your own children. But within 20 years there were no more blacksmiths. <br /><br />The biggest change happening now in software is the requirement to write parallel code, and he drew an analogy between the 1890 blacksmith and today's Java developer. In short, better become fluent in parallel programming right now or find another career. <br /><br />He hinted that we will all need to battle with learning new languages (probably of the functional kind: Haskell, F#, etc.) if we want to survive the transition to parallel programming, whether we like it or not. <br /><br />The good news is that he was wrong: Ateji Parallel Extensions adds parallelism to your favourite language and doesn't require you to go back to school.<br /><br />Neal also insisted on the importance of language and notation, and even dared to mention the Sapir-Whorf hypothesis. I understand his talk as proof that common wisdom about the importance of language is gradually changing. That I was on the right track four years ago when I founded Ateji in order to promote and develop language design and language technology.<br /><br />Neal concluded "The best way to predict the future is to create it". This is precisely what we're doing at Ateji. Stay tuned!Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com1tag:blogger.com,1999:blog-3043798876461399350.post-33541478357597260662009-10-24T22:42:00.006+02:002009-10-24T22:58:36.346+02:00Heading for the Java Symposium EuropeDear All,<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4-X2e8trnFlHfYGaJFqWKqHPLt_GhvGbQXMrmEO4MiZC2Q7zMxUxBgeNOsegJFO569kbsjJJxXsL5kv4SJ6-FHOa0nwNbsoUu2a2MhHczN36-WBiN9QfGorHMdFL4c7O0x-2iVaN7rA/s1600-h/TSSJS09-speaking-125x125.gif"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 125px; height: 125px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4-X2e8trnFlHfYGaJFqWKqHPLt_GhvGbQXMrmEO4MiZC2Q7zMxUxBgeNOsegJFO569kbsjJJxXsL5kv4SJ6-FHOa0nwNbsoUu2a2MhHczN36-WBiN9QfGorHMdFL4c7O0x-2iVaN7rA/s320/TSSJS09-speaking-125x125.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5396273099071494386" /></a><br />It's been a long time! We've been busy preparing Ateji's new product for parallel programming. If you thought parallel programming was difficult, we'll make you change your mind.<br /><br />The product is now available under a limited preview program, and is already a success with its early users.<br /><br />I'll be next week at the <a href="http://javasymposium.techtarget.com/">Java Symposium Europe</a> in Prague for its first public presentation. Watch out this blog for a smooth introduction to this new language and programming paradigm.Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com0tag:blogger.com,1999:blog-3043798876461399350.post-10685396039748433712008-10-23T16:50:00.005+02:002009-01-19T23:55:10.967+01:00Carpet-Bombing MarketingUnless you spent the last 6 months deep in a cave, you must have heard buzz about the <a href="http://code.msdn.microsoft.com/solverfoundation">Microsoft Solver Foundation</a>. It is much less likely however that you've heard about <a href="http://www.ateji.com/optimj.html">OptimJ</a>. That's a major difference between being Microsoft or being Ateji.<br /><br />In the pure Microsoft tradition, this vaporware has already generated far more downloads and excitation than OptimJ, an existing and proven product acclaimed by our users. Everybody in the OR community is pretending that they're not concerned or worried by the MSF announcement, but have anyway downloaded a large .msi archive "just in case". And the blogosphere is buzzing with wild guesses about what's really inside (do you know how to open an .msi archive ? most of them don't know either, but who cares).<br /><br />Effects of carpet-bombing marketing are also visible in everything related to the forthcoming <a href="http://ics09.meetings.informs.org/">ICS'09</a> conference. I have yet to see any conference-related announcement without the "M" word. And almost all sponsorship opportunities are already bought and reserved by this single company. Maybe the Ateji team will end up on the side-walk distributing flyers from a makeshift cardboard tent (it may be cold in January, we'll exchange marketing material for warm soup).<br /><br />So what's Ateji to do in this context ? We cannot afford a carpet-bombing marketing strategy, and vaporware announcements are not an option. Clients expect us to deliver first.<br /><br />What we can do, however, is to leverage all this buzz-making to spread the word about what we've been promoting for long. The vaporware strategy is typically an attempt at delaying competitor's sales. Hairy-pointed bosses will prefer to wait until Microsoft actually delivers anything usable, and we'll lose those sales. But many others will have a chance to look at our product and download right now a fully functional version, so all in all we expect a big boost for Ateji. Time will tell.<br /><br />But the best bit is about self-satisfaction. The fact that Microsoft is following a similar embedded-language approach applied to optimization reassures me that we choose the right approach when we founded Ateji three years ago.Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com0tag:blogger.com,1999:blog-3043798876461399350.post-37323639821407194322008-06-15T16:53:00.019+02:002009-01-19T23:52:22.534+01:00Common Idioms 2Associative arrays are one of the driving force behind the adoption of so-called scripting languages such as JavaScript or Ruby. Strangely enough, they're absent from the mainstream generalist languages of the C++/C#/Java family.<br /><br />Associative arrays are like standard arrays but can be indexed with any given collection of values (whereas Java arrays can only be indexed with 0-based integers). Alternatively, associative arrays can be thought of as maps with built-in language support and a fixed an immutable set of keys (refering to a non-existent key raises an ArrayIndexOutOfBoundsException). Here is the <a href="http://www.ateji.com/optimj">OptimJ</a> version of the <a href="http://en.wikipedia.org/wiki/Associative_arrays">canonical example from Wikipedia</a> :<br /><div style="border: 2px solid rgb(229, 229, 229); padding: 5px; text-align: left; color: black; background-color: rgb(249, 255, 229);font-family:monospace;"> String[String] phoneBook = <span style="font-weight: bold;">{</span><br /> "Sally Smart" -> "555-9999",<br /> "John Doe" -> "555-1212",<br /> "J. Random Hacker" -> "553-1337"<br /> <span style="font-weight: bold;">}</span>;<br /><br /> <span style="color: rgb(0, 70, 128);">// iterate over the values</span><br /> <span style="font-weight: bold; color: rgb(176, 0, 0);">for</span>(String number : phoneBook) <span style="font-weight: bold;">{</span><br /> System.out.println(number);<br /> <span style="font-weight: bold;">}</span><br /><br /> <span style="color: rgb(0, 70, 128);">// iterate over the keys</span><br /> <span style="font-weight: bold; color: rgb(176, 0, 0);">for</span>(String name : phoneBook.keys) <span style="font-weight: bold;">{</span><br /> System.out.println(name + " -> " + phoneBook[name]);<br /> <span style="font-weight: bold;">}</span><br /></div><br />Notice the type String[String] that denotes an array of strings indexed by strings.<br /><br />To see how associative arrays differ from Java arrays, consider the two definitions :<br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '> String[] a1 = <span style="font-weight:bold;">{</span> "a", "b", "c" <span style="font-weight:bold;">}</span>;<br /> String[<span style="font-weight:bold; color: #b00000;">int</span>] a2 = <span style="font-weight:bold;">{</span> 12 -> "a", 3 -> "b", 7 -> "c" <span style="font-weight:bold;">}</span>;<br /></div><br />Here a1 is indexed by 0, 1, 2 and a2 is indexed by 3, 7, 12. Note how their types are different.<br /><br />Java and associative array dimensions can be mixed freely:<br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '> int[String][][<span style="font-weight:bold; color: #b00000;">double</span>] a;<br /></div><br />a is a 3-dimensional array. The first dimension is associative, indexed by doubles. The second dimension is a Java array dimension, indexed by 0-based integers. The third dimension is associative, indexed by strings.<br /><br />As usual in Java, an array index expression is written with indices in the opposite order of the types:<br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '> a[3.1416][10]["abc"]<br /></div><br />Associative arrays are common in algebraic modeling languages such as AMPL, GAMS, OPL, etc., where they allow for a concise and mathematical-like expression of optimization problems.Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com0tag:blogger.com,1999:blog-3043798876461399350.post-58097902800153101982008-06-08T23:40:00.005+02:002009-01-19T23:52:37.673+01:00Common Idioms 1Most mainstream programming languages are pretty bad at initializating and populating structured data. Let's say you want a Java list containing "a" and "b".<br /><br />First attempt :<br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; '>List<String> a = <span style="font-weight:bold; color: #b00000;">new</span> ArrayList<String>();<br />a.add("1");<br />a.add("2");<br /></div><br /><br />Pretty bad signal-to-noise ratio.<br /><br />Second attempt :<br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; '>List<String> a = Arrays.asList(<span style="font-weight:bold; color: #b00000;">new</span> String[]<span style="font-weight:bold;">{</span> "a", "b" <span style="font-weight:bold;">}</span>);<br /></div><br /><br /><br />Slightly more readable, but allocates a useless intermediate array.<br /><br /><br />Third attempt :<br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black;'>List<String> a = Arrays.asList("a", "b");<br /></div><br /><br /><br />Much more readable, but doesn't work if you want a set rather than a list : there's no <code>Arrays.asSet()</code>.<br /><br />Here's the OptimJ version :<br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '>ArrayList<String> a = <span style="font-weight:bold;">{</span> "a", "b" <span style="font-weight:bold;">}</span>;<br />HashSet<String> s = <span style="font-weight:bold;">{</span> "a", "b" <span style="font-weight:bold;">}</span>;<br /></div><br /><br />But nausea quickly kicks in when you try to instanciate imbricated collection. A cooking recipe may be seen as a list (ordered) of sets of tasks (unordered because they can be done in parallel).<br /><br />First attempt :<br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; '> List<Hashset<String>> irishStew = <span style="font-weight:bold; color: #b00000;">new</span> ArrayList<Hashset <String>>();<br /> HashSet<String> set1 = <span style="font-weight:bold; color: #b00000;">new</span> HashSet<String>();<br /> set1.add("Mince the meet");<br /> set1.add("Mash the potatoes");<br /> irishStew.add(set1);<br /> HashSet<String> set2 = <span style="font-weight:bold; color: #b00000;">new</span> HashSet<String>();<br /> set2.add("Cook in oven");<br /> irishStew.add(set2);<br /></div><br /><br /><br />Wow! This is called <i>write-only</i> code. Think about having to maintain pages of similar code written by an summer intern two years ago. <br /><br />Second attempt :<br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black;'>... ?<br /></div><br /><br /><br />The <code>asList</code> doesn't work because it requires expressions, and the code that builds our sets is a sequence of statements, not an expression.<br /><br />Here's the OptimJ version :<br /><div style='font-family:monospace; border: 2px solid #e5e5e5; padding: 5px 5px 5px 5px; text-align: left; color:black; background-color: #f9ffe5; '> ArrayList<HashSet<String>> irishStew = <span style="font-weight:bold;">{</span><br /> <span style="font-weight:bold;">{</span> "Mince the meet", "Mash the potatoes" <span style="font-weight:bold;">}</span>,<br /> <span style="font-weight:bold;">{</span> "Cook in oven"<span style="font-weight:bold;">}</span><br /> <span style="font-weight:bold;">}</span>;<br /></div><br /><br /><br />Do you feel more confident now taking this code maintenance assignment ?Patrick Viryhttp://www.blogger.com/profile/10442033581047493529noreply@blogger.com0