Returns a device pointer to the data. More...

template<typename T >
T * device () const
 

Detailed Description

Returns a device pointer to the data.

Device-side pointer to matrix data. Data stored in column-major (Fortran) order. No need to free this, but unlock() when finished to allow garbage collection.

array A = randu(n);
float *d_A = A.device<float>();
custom_kernel<<<blocks,threads>>>(A.elements(), d_A);
A.unlock(); // finished

This function does not implement copy-on-write semantics when another array points to same data. Since memory returned by this call may be modified at any point, other arrays sharing this device memory are in danger of modification without copy-on-write semantics. The following example demonstrates how A is modified inadvertently.

array A = constant(1,n);
array B = A; // B and A point to same memory
float *d_A = A.device<float>();
cudaMemset(d_A, 0, A.bytes());
print(B); // all zeros since pointed to same memory

Use copy() to manually create copies

array A = constant(1,n);
array B = copy(A); // B,A point to different areas but same contents
float *d_A = A.device<float>();
cudaMemset(d_A, 0, A.bytes());
print(B); // all ones

Function Documentation

T* device ( ) const
Returns
A device pointer to the data.
See Also
scalar(), host()