Our goal at ArrayFire is to provide a simple-to-use library that can utilize the high-performance of a GPU. For the case that the user wants to extend ArrayFire capabilities, the ArrayFire library provides methods that allow programmers to look under the hood of an <code>af::array</code> and access the memory directly. While this approach is very powerful, it can be tedious to worry about the details of the layout and memory management. So today, we are eager to introduce new interoperability functions that will interface ArrayFire with several common, popular, and some might argue essential libraries. These are the Thrust, STL, and OpenCV libraries.

[custom_headline type="left" level="h3" looks_like="h3"]Getting Started[/custom_headline]

The new functions are part of a new standalone repository that can be found here: https://github.com/arrayfire/arrayfire-extras

This new repository will have functionality that will extend the ArrayFire library. The library can be cloned separately, however it can be automatically downloaded during ArrayFire installation if the <code>BUILD_ARRAYFIRE_EXTRAS</code> option is set within the CMake configuration. This is the case by default, so using the new functionality is as simple as including the relevant headers.
<pre class="lang:c++ decode:true" title="#include headers for interop functionalilty">#include &lt;af/extras/thrust.h&gt;
#include &lt;af/extras/stl.h&gt;
#include &lt;af/extras/opencv.h&gt;
#include &lt;af/extras/opencv3.h&gt;</pre>
[custom_headline type="left" level="h3" looks_like="h3"]STL Interoperability[/custom_headline]

The STL is ubiquitous, and rightfully so. Data structures like the <code>std::vector</code> are interwoven within the source of most C++ programs. Furthermore, copying to and from the host in a GPU program often happens with the help of an STL data structure. For efficiency's sake, we further abstracted the back and forth process and came up with the following interface functions.
<pre class="lang:c++ decode:true" title="STL interop interface">//create af::array from STL data structures
af::array array(const std::vector&lt;T&gt; &amp;vec)
af::array array(std::valarray&lt;T&gt; &amp;varr)
//create STL data structures from af::array
std::vector&lt;T&gt;toStdVector(af::array &amp;arr)
std::valarray&lt;T&gt;toStdValarray(af::array &amp;arr)

</pre>
[custom_headline type="left" level="h3" looks_like="h3"]Thrust Interoperability[/custom_headline]

Thrust is based on the STL, so the interoperability interface is almost identical to the previously-covered STL interface.
<pre class="lang:default decode:true" title="Thrust interop interface">//create af::array from thrust data structures
af::array array(thrust::device_vector&lt;T&gt; &amp;tvec)
af::array array(thrust::host_vector&lt;T&gt; &amp;tvec)
//create thrust data structures from af::array
thrust::device_vector&lt;T&gt; toDeviceVector(af::array &amp;arr)
thrust::host_vector&lt;T&gt; toHostVector(af::array &amp;arr)</pre>
[custom_headline type="left" level="h3" looks_like="h3"]OpenCV Interoperability[/custom_headline]

The OpenCV library provides a large number of computer vision related functions. While ArrayFire supports some gpu-enabled computer vision functionality such as <a href="http://sandbox.arrayfire.com/computer-vision-arrayfire-part-1/">feature detectors</a>, the OpenCV library has other functions and even GPU-implementations that are very useful. To ease the communication with the ArrayFire library, we provide the following methods to interface with OpenCV's main data structure, the <code>cv::Mat</code> .
<pre class="lang:default decode:true " title="OpenCV interop interface">//create af::array from OpenCV data structures
af::array array(const cv::Mat&amp; input, bool transpose = true);
af::array array(const std::vector&lt;cv::Mat&gt;&amp; input, bool transpose = true);
//create cv::Mat from an af::array
cv::Mat toMat(const af::array&amp; input, int type = CV_32F, bool transpose = true);</pre>
[custom_headline type="left" level="h3" looks_like="h3"]Conclusion[/custom_headline]

Integrating ArrayFire with your code is easier than ever!With these new interoperability functions you can focus on using the tools you need instead of the implementation details.