Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This seems to work, others may have better ideas:</p> <pre><code>#include &lt;ostream&gt; #include &lt;thrust/host_vector.h&gt; #include &lt;thrust/device_vector.h&gt; #include &lt;thrust/transform.h&gt; #include &lt;thrust/functional.h&gt; #include &lt;thrust/copy.h&gt; #include &lt;thrust/fill.h&gt; #define DSIZE 10 template &lt;typename T&gt; thrust::device_vector&lt;T&gt; operator+(thrust::device_vector&lt;T&gt; &amp;lhs, const thrust::device_vector&lt;T&gt; &amp;rhs) { thrust::transform(rhs.begin(), rhs.end(), lhs.begin(), lhs.begin(), thrust::plus&lt;T&gt;()); return lhs; } template &lt;typename T&gt; thrust::host_vector&lt;T&gt; operator+(thrust::host_vector&lt;T&gt; &amp;lhs, const thrust::host_vector&lt;T&gt; &amp;rhs) { thrust::transform(rhs.begin(), rhs.end(), lhs.begin(), lhs.begin(), thrust::plus&lt;T&gt;()); return lhs; } int main() { thrust::device_vector&lt;float&gt; dvec(DSIZE); thrust::device_vector&lt;float&gt; otherdvec(DSIZE); thrust::fill(dvec.begin(), dvec.end(), 1.0f); thrust::fill(otherdvec.begin(), otherdvec.end(), 2.0f); thrust::host_vector&lt;float&gt; hresult1 = dvec + otherdvec; std::cout &lt;&lt; "result 1: "; thrust::copy(hresult1.begin(), hresult1.end(), std::ostream_iterator&lt;float&gt;(std::cout, " ")); std::cout &lt;&lt; std::endl; thrust::host_vector&lt;float&gt; hvec(DSIZE); thrust::fill(hvec.begin(), hvec.end(), 5.0f); thrust::host_vector&lt;float&gt; hresult2 = hvec + hresult1; std::cout &lt;&lt; "result 2: "; thrust::copy(hresult2.begin(), hresult2.end(), std::ostream_iterator&lt;float&gt;(std::cout, " ")); std::cout &lt;&lt; std::endl; // this line would produce a compile error: // thrust::host_vector&lt;float&gt; hresult3 = dvec + hvec; return 0; } </code></pre> <p>Note that in either case, I can specify a host or device vector for the result, since thrust will see the difference and automatically generate the necessary copy operation. So the result vector type (host, device) in my templates is not critical.</p> <p>Also note that the <code>thrust::transform</code> function parameters you had in your template definition weren't quite right.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload