NVIDIA / NVIDIA/cccl

Converting constructors and assignment operators on Thrust vector types are inconsistent and dangerous.

Open
#729 3 comments 0 reactions 0 assignees View on GitHub
thrust
Dominant language
C++
Stars
2.5k
Forks
487
Avg merge
2d 7h
Merged PRs (30d)
296

Description

The converting constructors and assignment operators on Thrust vector types are a mess, and inconsistent across different types. We need to figure out what behavior they actually should have; it's likely that many of them are too underconstrained and some of the constructors should be `explicit`. We have to consider conversions of not only vectors with different but convertible element types, but also vectors with the same element type and different allocators.

```
#include
#include
#include
#include

int main() {
{
std::vector f0;

std::vector d0(f0); // Fails as intended.
}
{
std::vector s0;
thrust::host_vector s1;
thrust::device_vector s2;
thrust::cuda::vector s3;

thrust::host_vector da0(s0); // Works as intended.
thrust::host_vector da1(s1); // Works as intended.
thrust::host_vector da2(s2); // Works as intended.
thrust::host_vector da3(s3); // Fails, should work.

da0 = s0; // Works as intended.
da0 = s1; // Works as intended.
da0 = s2; // Works as intended.
da0 = s3; // Fails, should work.

thrust::host_vector db2(s2); // Works, should fail.
thrust::host_vector db3(s3); // Fails as intended.

db0 = s0; // Works, should fail.
db0 = s1; // Works, should fail.
db0 = s2; // Works, should fail.
db0 = s3; // Fails as intended.
}
{
std::vector s0;
thrust::host_vector s1;
thrust::device_vector s2;
thrust::cuda::vector s3;

thrust::device_vector da0(s0); // Works as intended.
thrust::device_vector da1(s1); // Works as intended.
thrust::device_vector da2(s2); // Works as intended.
thrust::device_vector da3(s3); // Fails, should work.

da0 = s0; // Works as intended.
da0 = s1; // Works as intended.
da0 = s2; // Works as intended.
da0 = s3; // Fails, should work.

thrust::device_vector db0(s0); // Works, should fail.
thrust::device_vector db1(s1); // Works, should fail.
thrust::device_vector db2(s2); // Works, should fail.
thrust::device_vector db3(s3); // Fails as intended.

db0 = s0; // Works, should fail.
db0 = s1; // Works, should fail.
db0 = s2; // Works, should fail.
db0 = s3; // Fails as intended.
}
{
std::vector s0;
thrust::host_vector s1;
thrust::device_vector s2;
thrust::cuda::vector s3;

thrust::cuda::vector da0(s0); // Works as intended.
thrust::cuda::vector da1(s1); // Works as intended.
thrust::cuda::vector da2(s2); // Works as intended.
thrust::cuda::vector da3(s3); // Works as intended.

da0 = s0; // Works as intended.
da0 = s1; // Works as intended.
da0 = s2; // Works as intended.
da0 = s3; // Fails, should work.

thrust::cuda::vector db0(s0); // Works, should fail.
thrust::cuda::vector db1(s1); // Works, should fail.
thrust::cuda::vector db2(s2); // Works, should fail.
thrust::cuda::vector db3(s3); // Works, should fail.

db0 = s0; // Works, should fail.
db0 = s1; // Works, should fail.
db0 = s2; // Works, should fail.
db0 = s3; // Works, should fail.
}
}
```

Contributor guide

Open the contributing guide

Research direction

Start by comparing the converting constructors and assignment operators in thrust/system/cuda/vector.h, thrust/host_vector.h, and thrust/device_vector.h, then reproduce the cases in the issue. Done means the intended conversion rules are decided and the vector types behave consistently for convertible element types and differing allocators.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend-api-design, hpc
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.