agency-library / agency

Execution primitives for C++
http://agency-library.github.io
BSD 3-Clause "New" or "Revised" License
154 stars 27 forks source link

Consider syntactic sugar for vector placment #390

Open jaredhoberock opened 7 years ago

jaredhoberock commented 7 years ago

It might be useful if vector's execution policy constructor used the policy's underlying allocator as the vector's allocator when it makes sense to do so.

For example, this syntax:

vector<int, my_allocator> data(par.on(exec), 100);

Could be made equivalent to this syntax:

auto policy = par.on(exec);
vector<int, my_allocator> data(policy, 100, exec.allocator<int>());

In cases where my_allocator is constructible from policy.executor().allocator<int>(). In other cases, the vector's allocator would just be default constructed as usual.

This sort of syntax would make it convenient to construct a vector with affinity to a particular GPU fairly easily:

vector<int, device_allocator<int>> data(par.on(device(1)), 100);
jaredhoberock commented 7 years ago

We might also consider a basic_vector<T, ExecutionPolicy, Allocator = some default> type for embedding a default execution policy to use instead of seq for vector methods overloads without an ExecutionPolicy parameter. The default used for Allocator would probably be whatever allocator is associated with ExecutionPolicy's executor.

With this, we could introduce aliases such as cuda::vector<T> = basic_vector<T,cuda::parallel_policy>.

Something equivalent to thrust::device_vector could be done similarly, and would use cuda::device_allocator for its choice of allocator type.