Do more. Code less. Free software for GPU computing.
<scroll to top>
Defines | Functions

Access or display array data

Basics

Defines

#define print(exp)
 Show expression and its contents.

Functions

void disp (const array exp, const char *expstr=NULL)
 Display the value of a variable (see print)

template<typename T >
scalar () const
 Returns host-side scalar of first component of data.
template<typename T >
T * device () const
 Device-side pointer to matrix data.
template<typename T >
T * host () const
 Host-side pointer to matrix data.
template<typename T >
static void hostFree (const T *)
 Releases host-side memory allocated for data by host()

Define Documentation

#define print (   exp)

Show expression and its contents.

Example:

       array A = randu(2,2);
       print(A + 1);

produces

     A + 1 =
       1.23423   1.83285
       1.03472   1.64273
Examples:
examples/image/image_demo.cpp, examples/misc/fft.cpp, examples/misc/gfor.cpp, examples/misc/hello_world.cpp, examples/misc/integer.cpp, and examples/misc/lin_algebra.cpp.

Function Documentation

T scalar ( ) const [inherited]

Returns host-side scalar of first component of data.

No need to call unlock() afterward.

void af::disp ( const array  exp,
const char *  expstr = NULL 
)

Display the value of a variable (see print)

T* device ( ) const [inherited]

Device-side pointer to matrix data.

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 inadvertantly.

array A = ones(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 = ones(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
Examples:
examples/misc/any.cpp.
T* host ( ) const [inherited]

Host-side pointer to matrix data.

Call hostFree() when done to allow garbage collection. Modification of this data has no effect on device copy.

array A = randu(5);
float *h_A = A.host<float>();
float firstval = h_A[1]; // grab second value
array::hostFree(h_A); // finished
See also:
scalar(), device()
Examples:
examples/misc/fft.cpp, examples/misc/hello_world.cpp, and examples/multiGPU/multiGPU_Gemv.cpp.
static void hostFree ( const T *  ) [static, inherited]

Releases host-side memory allocated for data by host()

Examples:
examples/misc/hello_world.cpp.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines