[RFC] Block captured variables in OpenCL 2.X dynamic parallelism

Hi,
OpenCL 2.X dynamic parallelism in clang 3.9 leverages most of the Objective-C’s block structure to save captured variables from parent kernel. However, there is a disadvantage on this design: The block literal structure, which needs to be passed to child kernel by address later, is allocated in parent kernel context by default. But parent kernels and child kernels only shares the global address space, so we either need to put block literal structures in global address space, which brings other problems like memory management due to the large amount of block literals potentially, or find another way to figure this out.

We propose a way that passes all the captured variables as function arguments to the child kernel invoke function, which is the “main body” function of child kernel. OpenCL 2.X spec doesn’t allowed __local variables to be captured into child kernel block, so we can almost assert that there are only two kinds of captured variables: constants(i.e by-value variables) and __global pointers. Also, OpenCL 2.X allows child kernels shared chunks of memory which are passed as arguments in(and only in) __local pointer type to child kernel block. Thus, if we insert all the captured variables to the head of the child block invoke function argument list, which originally contains only __local pointers if there are shared memory chunks, we can not only conveniently distinguish captured variables from shared memory pointers based on their types in the argument list, but also remove the need of maintaining memory for block literal structures since we don’t pass it to child kernel anymore.

Nevertheless, captured variables still needed to be extracted inside most of the __enqueue_kernel_XXX implementations. We came up with a design that adds two new fields into the block literal struct: cap_num, which tells the amount of captured variables, and cap_copy_helper, which is a function with prototype:
size_t cap_copy_helper(void* block_literal, unsigned int arg_index, void* dest_memory)
The second parameter arg_index requires index of the desired captured variables in block invoke function argument list mentioned above. This helper would copy your desired variable value from block_literal to dest_memory and return its size. Actually we had previously built a version of helper function that still took block_literal and arg_index but return the captured variables’s pointer(in void* type) directly. In that approach neither can we know the size of variable nor could we retrieve value from the returning pointer due to the lack of type information and one can not directly de-reference a void pointer either. The new cap_copy_helper approach can solve both of the problems, although it seems to be more suitable for copying all the captured variables adjacently into a chunk of memory, I think it still can be used to fetch individual captured variable.

We have implemented a prototype for all of the design above except the cap_copy_helper, but it could be finished soon. Also, discussions for captured variables in advanced type like image_t are welcomed - since we haven’t covered those types yet.

Yours sincerely,

Hi Bekket,

Could you please elaborate what benefits passing captured variables as separate argument gives in comparison to passing a pointer to a chunk of captures located somewhere in contiguous memory. Perhaps examples could be helpful or if you already have a patch feel free to setup the review.

Current design passes the whole block literal structure into __enqueue_kernel_XXX and relies on it to make capturing data available before enqueueing the block. Using memcpy should be sufficient here to copy thread local (private address space) data into some global memory. I am guessing the same should be done with your approach but in the format of parameter passing if blocks are to be enqueued anywhere globally on the device?

In your proposal does frontend generate definitions of cap_copy_helper functions then?

Cheers,
Anastasia

Hi Bekket,

Could you please elaborate what benefits passing captured variables as
separate argument gives in comparison to passing a pointer to a chunk of
captures located somewhere in contiguous memory. Perhaps examples could be
helpful or if you already have a patch feel free to setup the review.

Sure, we would set up a code review later.

Current design passes the whole block literal structure into
__enqueue_kernel_XXX and relies on it to make capturing data available
before enqueueing the block. Using memcpy should be sufficient here to copy
thread local (private address space) data into some global memory.

Of course one can copy captured variables to some global memory, but global
memory management is non trivial. Which either we need some special help
from target specific APIs for better management, or we need to allocate a
large chunk, static size, of global memory in our OCL runtime
implementation. We think both of them are not neat and less flexible(due to
the static size of global memory) in comparison to passing function
arguments(to block invoke function) "locally".

I am guessing the same should be done with your approach but in the format
of parameter passing if blocks are to be enqueued anywhere globally on the
device?

Yes, one key feature of current(ObjC) block literal structure is that it
arrange captures variables in a pretty "strange" way:

struct block_literal {
int isa;
(...other block header fields...)

int capture_1; //<-- How do we know the offset of this field within our
OCL runtime implementation?
float capture_2;
}

As mentioned in the code comment above, although we can get the offset of
each fields in block_literal during codegen, it's nearly impossible to do
so in a OCL runtime implementation code, i.e "outside" the compiler. In
another word: The layout of block_literal is dynamic, which bothers
developers.
cap_copy_helper, which is the helper "memcpy" generated(function definition
included) during codegen, comes to this aid.

In your proposal does frontend generate definitions of cap_copy_helper

functions then?

Cheers,
Anastasia

Cheers,
McClane