In order to get the best performance from your CUDA or OpenCL code, it is helpful to keep in mind some useful tips for optimizing performance.

<em>Note: By "accelerator" we refer to GPUs, APUs, co-processors, FPGAs, and any devices capable of running CUDA or OpenCL.</em>
<ul>
	<li><b>Vectorized Code</b>: Accelerators perform best with <a href="http://en.wikipedia.org/wiki/Vectorization_(parallel_computing)">vectorized code</a> because the computations map naturally onto arithmetic cores of the hardware. ArrayFire functions are inherently vectorized, so if you are using ArrayFire, you are writing vectorized code.</li>
</ul>
<ul>
	<li><b>Memory Transfers</b>: Avoid excessive memory transfers. Each casting operation to and from the accelerator moves data back and forth between CPU memory and accelerator memory. ArrayFire makes many automatic optimizations to minimize these memory transfers by only transferring data when absolutely needed.</li>
</ul>
<ul>
	<li><b>Serial vs Parallel Operations</b>: CPUs are serial computing devices and accelerators are parallel computing devices. For small or serial operations, the best performance is likely achieved on the CPU. For large or parallel operations, the best performance is likely achieved on the accelerator. Often, a good rule of thumb is to use the CPU if your data is only a few hundred elements and to use the accelerator for processing on &gt;10,000 elements. With ArrayFire, you can control the segments of code that are run on each device through <a href="http://www.accelereyes.com/arrayfire/c/page_quickref.htm#quickref_types">creation of array data types</a>.</li>
</ul>
<ul>
	<li><b>Loops</b>: Loops typically imply serial processing. However, with CUDA or OpenCL it is possible to run all the iterations simultaneously, if there are no data dependencies between iterations. ArrayFire makes this very easy with its <a title="GFOR" href="http://www.accelereyes.com/arrayfire/c/page_gfor.htm">GFOR</a> function.</li>
</ul>
<ul>
	<li><b>Lazy Execution</b>: With CUDA and OpenCL, it is important to construct kernels that contain the right amount of computation, not so much that timeouts occur and not so little that throughput drops. ArrayFire employs a lazy execution design which automatically constructs optimally-sized kernels from your algorithms. Lazy execution also means that ArrayFire does not launch GPU kernels until the results are requested, either in a display or subsequent CPU-based computation. If you wish to force a ArrayFire computation, the ArrayFire <a href="http://www.accelereyes.com/arrayfire/c/namespaceaf.htm#a6d1dbf55ce9dbd67b460eac3a0661d58">sync and eval functions</a> are available.</li>
</ul>
<ul>
	<li><b>Write Good Timing Code</b>: Badly written timing code can often cause artificially decreased accelerator performance. ArrayFire comes with a convenient <a href="http://www.accelereyes.com/arrayfire/c/page_timing.htm">timing function</a> to ensure proper benchmarks.</li>
</ul>
<ul>
	<li><b>Regular Access Patterns</b>: When performing subscripting, keep in mind that the accelerator memory controllers are not as versatile as those on the CPU. Best performance is achieved when your subscript access patterns are regular and uniform. For example, <tt>A([1 4 2 3 5 1 2 ...])</tt> would be slow while <tt>A([1 2 3 4 5 6 ...])</tt> would be faster. With ArrayFire, subscripting is very easy. ArrayFire is column-major, so it is faster to access columns (<tt>A(span,i)</tt>) rather than rows (<tt>A(i,span)</tt>).</li>
</ul>
What else? What are good general tips for writing CUDA &amp; OpenCL code?
<h6 class="zemanta-related-title" style="font-size: 1em;">Related articles</h6>
<ul class="zemanta-article-ul zemanta-article-ul-image" style="margin: 0; padding: 0; overflow: hidden;">
	<li class="zemanta-article-ul-li-image zemanta-article-ul-li" style="padding: 0; background: none; list-style: none; display: block; float: left; vertical-align: top; text-align: left; width: 84px; font-size: 11px; margin: 2px 10px 10px 2px;"><a style="box-shadow: 0px 0px 4px #999; padding: 2px; display: block; border-radius: 2px; text-decoration: none;" href="http://blog.accelereyes.com/blog/2013/01/21/how-much-speedup-can-you-get-with-cuda-or-opencl/" target="_blank"><img style="padding: 0; margin: 0; border: 0; display: block; width: 80px; max-width: 100%;" alt="" src="http://i.zemanta.com/139661938_80_80.jpg" /></a><a style="display: block; overflow: hidden; text-decoration: none; line-height: 12pt; height: 80px; padding: 5px 2px 0 2px;" href="http://blog.accelereyes.com/blog/2013/01/21/how-much-speedup-can-you-get-with-cuda-or-opencl/" target="_blank">How much speedup can you get with CUDA or OpenCL?</a></li>
	<li class="zemanta-article-ul-li-image zemanta-article-ul-li" style="padding: 0; background: none; list-style: none; display: block; float: left; vertical-align: top; text-align: left; width: 84px; font-size: 11px; margin: 2px 10px 10px 2px;"><a style="box-shadow: 0px 0px 4px #999; padding: 2px; display: block; border-radius: 2px; text-decoration: none;" href="http://insidehpc.com/2013/01/29/mark-harris-on-using-shared-memory-in-cuda-cc/" target="_blank"><img style="padding: 0; margin: 0; border: 0; display: block; width: 80px; max-width: 100%;" alt="" src="http://i.zemanta.com/141565895_80_80.jpg" /></a><a style="display: block; overflow: hidden; text-decoration: none; line-height: 12pt; height: 80px; padding: 5px 2px 0 2px;" href="http://insidehpc.com/2013/01/29/mark-harris-on-using-shared-memory-in-cuda-cc/" target="_blank">Mark Harris on Using Shared Memory in CUDA C/C++</a></li>
	<li class="zemanta-article-ul-li-image zemanta-article-ul-li" style="padding: 0; background: none; list-style: none; display: block; float: left; vertical-align: top; text-align: left; width: 84px; font-size: 11px; margin: 2px 10px 10px 2px;"><a style="box-shadow: 0px 0px 4px #999; padding: 2px; display: block; border-radius: 2px; text-decoration: none;" href="http://www.hpcwire.com/hpcwire/2013-02-06/nvidia_announces_new_report_outlining_12x_cuda_performance_gains_over_cpu-only_libraries.html" target="_blank"><img style="padding: 0; margin: 0; border: 0; display: block; width: 80px; max-width: 100%;" alt="" src="http://i.zemanta.com/143351767_80_80.jpg" /></a><a style="display: block; overflow: hidden; text-decoration: none; line-height: 12pt; height: 80px; padding: 5px 2px 0 2px;" href="http://www.hpcwire.com/hpcwire/2013-02-06/nvidia_announces_new_report_outlining_12x_cuda_performance_gains_over_cpu-only_libraries.html" target="_blank">NVIDIA Report: 12x CUDA Performance Gains Over CPU-Only</a></li>
	<li class="zemanta-article-ul-li-image zemanta-article-ul-li" style="padding: 0; background: none; list-style: none; display: block; float: left; vertical-align: top; text-align: left; width: 84px; font-size: 11px; margin: 2px 10px 10px 2px;"><a style="box-shadow: 0px 0px 4px #999; padding: 2px; display: block; border-radius: 2px; text-decoration: none;" href="http://openclnews.com/blog/amd_strengthens_opencl_ecosystem_with_new_tools_for_hpc_that_take_advantage" target="_blank"><img style="padding: 0; margin: 0; border: 0; display: block; width: 80px; max-width: 100%;" alt="" src="http://i.zemanta.com/noimg_9_80_80.jpg" /></a><a style="display: block; overflow: hidden; text-decoration: none; line-height: 12pt; height: 80px; padding: 5px 2px 0 2px;" href="http://openclnews.com/blog/amd_strengthens_opencl_ecosystem_with_new_tools_for_hpc_that_take_advantage" target="_blank">AMD strengthens OpenCL ecosystem with new tools for HPC that take advantage of GPU</a></li>
	<li class="zemanta-article-ul-li-image zemanta-article-ul-li" style="padding: 0; background: none; list-style: none; display: block; float: left; vertical-align: top; text-align: left; width: 84px; font-size: 11px; margin: 2px 10px 10px 2px;"><a style="box-shadow: 0px 0px 4px #999; padding: 2px; display: block; border-radius: 2px; text-decoration: none;" href="http://perilsofparallel.blogspot.com/2011/09/conversation-with-intels-james-reinders.html" target="_blank"><img style="padding: 0; margin: 0; border: 0; display: block; width: 80px; max-width: 100%;" alt="" src="http://i.zemanta.com/138442047_80_80.jpg" /></a><a style="display: block; overflow: hidden; text-decoration: none; line-height: 12pt; height: 80px; padding: 5px 2px 0 2px;" href="http://perilsofparallel.blogspot.com/2011/09/conversation-with-intels-james-reinders.html" target="_blank">A Conversation with Intel's James Reinders at IDF 2011</a></li>
	<li class="zemanta-article-ul-li-image zemanta-article-ul-li" style="padding: 0; background: none; list-style: none; display: block; float: left; vertical-align: top; text-align: left; width: 84px; font-size: 11px; margin: 2px 10px 10px 2px;"><a style="box-shadow: 0px 0px 4px #999; padding: 2px; display: block; border-radius: 2px; text-decoration: none;" href="http://devgurus.amd.com/thread/160330" target="_blank"><img style="padding: 0; margin: 0; border: 0; display: block; width: 80px; max-width: 100%;" alt="" src="http://i.zemanta.com/noimg_97_80_80.jpg" /></a><a style="display: block; overflow: hidden; text-decoration: none; line-height: 12pt; height: 80px; padding: 5px 2px 0 2px;" href="http://devgurus.amd.com/thread/160330" target="_blank">A question/appeal to AMD engineers regarding local memory.</a></li>
</ul>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><img class="zemanta-pixie-img" style="border: none; float: right;" alt="" src="http://img.zemanta.com/pixy.gif?x-id=13d60b87-0726-4202-9f19-dc4c28f900a3" /></div>