boostorg / boostorg/compute

possible kernel proliferation with scalar parameters

Open
#663 10 comments 0 reactions 0 assignees View on GitHub
feature
Dominant language
C++
Stars
1.7k
Forks
340
PR merge metrics
No merged PRs in 30d

Description

Consider the following function

```
namespace bc=boost::compute;
void add_scalar(bc::vector& v, float scalar){
auto unary = bc::bind(bc::plus(),bc::placeholders::_1, scalar);//_1+scalar
bc::transform(v().begin(),v().end(), v().begin(), unary);
}
...
bc::vector v(10000,1.0);
for(int i = 0; i != 1000; ++i)
add_scalar(v,0.5*i);
```

This will in every iteration generate a kernel and compile it on the device, leading to way more kernel compilations than required(essentially making kernel caching useless). The issue, as far as i understood the meta_kernel code is, that a function can only register buffers, but not constants. Thus the only way to generate the kernel is to stringify the scalar value. A solution would be to add the ability to register constants as additional kernel arguments the same way as it is done with buffers.

An alternative solution that does not require deep changes to the internals would be to use an bc::array for everything that is not a constant. But this is very tedious and error prone as the user must be very careful with scalar arguments, especially as for example bc::accumulate returns a float and not an array (while the internal implementation actually uses an array...).

A way to make the latter solution more viable would be to implement a small wrapper class scalar which is a bit of syntactic sugar around bc::array
##

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.