Note that there are some explanatory texts on larger screens.

plurals
  1. POCuda - printing string from object in __global__ function
    primarykey
    data
    text
    <p>I am new to CUDA and I am getting a strange error. I want to print a string from a passed object and I get the error "calling host function from global function is not allowed" and I don't know why. But if I want to print an integer (changing get method to return sk1), everything works fine. Here is the code:</p> <pre><code>class Duomenys { private: string simb; int sk1; double sk2; public: __device__ __host__ Duomenys(void): simb(""), sk1(0), sk2(0.0) {} __device__ __host__~Duomenys() {} __device__ __host__ Duomenys::Duomenys(string simb1, int sk11, double sk21) : simb(simb1), sk1(sk11), sk2(sk21) {} __device__ __host__ string Duomenys::get(){ return simb; } }; </code></pre> <p>And here I am calling Duomenys::get from __global__ function:</p> <pre><code>__global__ void Vec_add(Duomenys a) { printf(" %s \n",a.get()); } </code></pre> <p><strong>EDIT:</strong> I am trying to read data from a file and print it in a global function. In this code I am trying read all data and print just one object to see if everything works. This is the error I'm getting:</p> <pre><code> calling a __host__ function("std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::~basic_string") from a __global__ function("Vec_add") is not allowed </code></pre> <p><strong>Code:</strong></p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;cuda.h&gt; #include &lt;cuda_runtime.h&gt; #include &lt;vector&gt; #include &lt;string&gt; #include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;iomanip&gt; #include &lt;string&gt; #include &lt;sstream&gt; using namespace std; class Duomenys { private: string simb; int sk1; double sk2; public: __device__ __host__ Duomenys(void): simb(""), sk1(0), sk2(0.0) {} __device__ __host__~Duomenys() {} __device__ __host__ Duomenys::Duomenys(string simb1, int sk11, double sk21) : simb(simb1), sk1(sk11), sk2(sk21) {} __device__ __host__ string Duomenys::print() { stringstream ss; ss &lt;&lt; left &lt;&lt; setw(10) &lt;&lt; simb &lt;&lt; setw(10) &lt;&lt; sk1 &lt;&lt; setw(10) &lt;&lt; sk2; return ss.str(); } }; __global__ void Vec_add(Duomenys a) { printf(" %s \n",a.print()); } /* Host code */ int main(int argc, char* argv[]) { setlocale (LC_ALL,""); vector&lt;Duomenys&gt; vienas; vector&lt;vector&lt;Duomenys&gt;&gt; visi; //data reading to vector "vienas" (it works without any errors) Duomenys *darr; const size_t sz = size_t(2) * sizeof(Duomenys); cudaMalloc((void**)&amp;darr, sz); Vec_add&lt;&lt;&lt;1, 1&gt;&gt;&gt;(visi[0].at(0)); cudaDeviceSynchronize(); cudaMemcpy(darr, &amp;visi[0].at(0), sz, cudaMemcpyHostToDevice); return 0; } </code></pre>
    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.
 

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