370 likes | 604 Views
OpenCL ch. 2~4. Jongeun Lee. Fall 2013. Preparation. download & build instructions https:// code.google.com/p/opencl-book-samples/wiki/Installation source code download svn checkout http ://opencl-book-samples.googlecode.com/svn/trunk/ opencl -book-samples-read-only build instructions.
E N D
OpenCLch.2~4 Jongeun Lee Fall 2013
Preparation • download & build instructions • https://code.google.com/p/opencl-book-samples/wiki/Installation • source code download • svn checkout http://opencl-book-samples.googlecode.com/svn/trunk/ opencl-book-samples-read-only • build instructions
Code overview • note • some shortcuts • using only one platform, one device, etc. • error code • cl… functions returning cl_xxx objects: last arg • others: return value
Platforms, Devices • platform • profile • full profile, embedded profile • capabilities of particular OpenCL version supported • how to query…
Contexts • Context is container for • associated devices • memory objects (buffers, images) • command queues (interface) • Context can be created • with multiple devices • from a single platform • may use multiple contexts… • no auto data sharing • updated as program progresses
OpenCL C • new features • new types • address space qualifiers • built-in functions
New Features • vector data types • easy to use (supports operators similar to scalars’) • increase portability • address space qualifiers • additions for parallelism • work-items, work-groups, sync, etc. • images, samplers • a set of built-in functions
Scalar types • int: 32-bit (fixed) • double: optional • half: 16-bit floating point • requires explicit type casting to/from float • vload_half(float), vstore_half(half) • size_t, ptrdiff_t, intptr_t, uintptr_t • size_t: result of sizeof • ptrdiff_t: result of subtracting two pointers • size_t, ptrdiff_t: 32-bit if CL_DEVICE_ADDRESS_BITS defined in clGetDeviceInfo is 32-bits, and 64-bit if it is 64-bits • intptr_t, uintptr_t: signed/unsigned integer for holding void pointer
Vector types • {scalartype}N, where N = 2, 3, 4, 8, 16 • float8, long4, etc. • aligned to a power of 2 bytes • char3: has 4-byte size, aligned to 4-byte boundary • int3: has 16-byte size, aligned to 16-byte boundary
API types • w/o API type • size_t • ptrdiff_t • intptr_t • uintptr_t
Vector literals • examples • valid forms
more examples • WRONG (compile error) • vector literal cannot be used as l-value
Vector components • using .xy, .xyz, .xyzw
Vector components • using numeric indexes: .[sS][0-9, A-F, a-f] float8 f f.s7 • cannot intermix numeric indexes with .xyzw • using .lo/.hi, .odd/.even
Other data types • image & sampler types have no array or pointer types • no image & sampler in a struct
Derived types • Generally, supported • struct type cannot contain pointer if it (or its pointer) is used as an argument to kernel • okay as a variable inside kernel
Implicit type conversion • no surprise here (same as C99) • not allowed between built-in vector types
Usual arithmetic conversion • C99 defines only scalars int a; short b; int c = a + b; • vectors • compile error if operands are more than one vector type • if there is only a single vector type and all the other operands are scalar, scalars are converted* and widened to vectors • *compile-error if any scalar has greater rank than vector element
Explicit conversion • illegal between vector types • scalar to vector is okay
There is a way… • motivation • solution
in general • example
Reinterpreting data • examples • can do some neat stuffs
Vector operators • note • va < vb • va && vb
Function qualifiers • kernel (or __kernel) void … • kernel’s return type must be void • kernel functions containing local variables cannot be called from another kernel function
Address space qualifiers • global, local, constant, private (or __{}): disjoint • argument, local variable of a function: private • program-scope variable, static variable inside a function: global*/constant • pointerargument to kernel: must point to global, local, or constant • ERROR if NOT specified • no address space for nK function’s returnvalue (ERROR if specified)
Global address space • for memory objects (buffers and images) • buffer can be declared as a pointer to scalar/vector/struct • “global” should not be specified for images • pointer to global addr space: allowed • as argument to K/nK function • as variable declared inside function • variabledeclared inside funct. cannot be allocated in global
Constant address space • for variables allocated in global that are accessed read-only • accessible to all work-items • string literals are in constant • images cannot be allocated in constant • pointer to constant addr space: allowed • as argument to K/nK functions • as variable declared inside functions • variable in the outermost scope of kernel can be declared in constant (with proper initialization)
In OpenCL 1.0: “All program scope variables must be declared in the constant address space.” In OpenCL 2.0: global/constant ; global by default
Local address space • for variables shared (r/w) by all work-items of a work-group • not across work-groups • allocated in local memory • local memory: similar to scratchpad memory • pointer to local addr space: allowed • as argument to K/nK function • as variable declared inside function • variables declared inside a kernel can be allocated in local • must be declared at kernel function scope • cannot be initialized
Private address space • variables declared inside kernel without address space qualifier • all variables declared inside non-kernel • all function arguments
Casting between addr spaces? • it’s not allowed • example
Access & type qualifiers • access qualifiers: read_only, write_only • specified with image arguments • NO read_write • image reads are cached in texture cache • but image writes do not update texture cache • type qualifiers: const, volatile, restrict • cannot be used with image types