Trait rustacuda::memory::DeviceCopy[][src]

pub unsafe trait DeviceCopy { }
Expand description

Marker trait for types which can safely be copied to or from a CUDA device.

A type can be safely copied if its value can be duplicated simply by copying bits and if it does not contain a reference to memory which is not accessible to the device. Additionally, the DeviceCopy trait does not imply copy semantics as the Copy trait does.

How can I implement DeviceCopy?

There are two ways to implement DeviceCopy on your type. The simplest is to use derive:

extern crate rustacuda;

#[derive(Clone, DeviceCopy)]
struct MyStruct(u64);

This is safe because the DeviceCopy derive macro will check that all fields of the struct, enum or union implement DeviceCopy. For example, this fails to compile, because Vec cannot be copied to the device:

#[derive(Clone, DeviceCopy)]
struct MyStruct(Vec<u64>);

You can also implement DeviceCopy unsafely:

use rustacuda::memory::DeviceCopy;

struct MyStruct(u64);

unsafe impl DeviceCopy for MyStruct { }

What is the difference between DeviceCopy and Copy?

DeviceCopy is stricter than Copy. DeviceCopy must only be implemented for types which do not contain references or raw pointers to non-device-accessible memory. DeviceCopy also does not imply copy semantics - that is, DeviceCopy values are not implicitly copied on assignment the way that Copy values are. This is helpful, as it may be desirable to implement DeviceCopy for large structures that would be inefficient to copy for every assignment.

When can’t my type be DeviceCopy?

Some types cannot be safely copied to the device. For example, copying &T would create an invalid reference on the device which would segfault if dereferenced. Generalizing this, any type implementing Drop cannot be DeviceCopy since it is responsible for some resource that would not be available on the device.

Implementations on Foreign Types