In the code below, I have use the thrust library, what I don't understand is in the device
code I can access v
, which I think is in host memory, because I direct give it the value
4
in the constructor.
struct op{
float v;
op(float iv):v(iv){}
__device__
void operator()(float v2)
{
printf("%f\n", v+v2);
}
};
int main()
{
device_vector<float> data(4,1);
for_each(data.begin(), data.end(), op(4));
return 0;
}
The output of the code above is 5.0 5.0 5.0 5.0
.
So, can you give some hints about this behavior? I have searched through the official
manual, but didn't get any information about this behavior.
Sorry for my express, just like Ben Voigt said, my problem is " what I don't understand is in the device code I can access v, which I think is in host memory
"
v
is not in host memory, at least not if the functor object is instantiated in device code. (And your struct, whose ()
operator only has a __device__
decoration, would only "work" in device code.)
iv
starts out in host memory, but the process of copying from iv
(host) to v
(device, at least if the op
object is used in a thrust device operation) is handled by thrust prior to computation of the for_each
function.
The copying of the op()
initialization parameter (4, in your case) is really no different than what needs to happen with the 1
parameter here:
device_vector<float> data(4,1);
^
That 1
parameter exists in host code . However the thrust constructor for device_vector
initiates the necessary host->device memory copies (and/or kernel calls, depending on the specific thrust function) to initialize the device_vector
storage, which exists on the device .
This is handled at runtime, not compile-time. You can replace your 4
constant with a variable, and still get valid results, just as you could replace your 1 initializer for device_vector
with a variable.
That is how I would explain it. Probably talonmies or JaredHoberock (or others) can correct my terminology, but I think the gist is correct.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.