Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when copying vector bool to CUDA memory

I encountered a compilation error where copying a bool vector to cuda memory will fail

bool *gpu; 

cudaMalloc(reinterpret_cast<void **>(&gpu), 100);

std::vector<bool> cpu(100);

for(int i=0;i<100;i++){
  cpu[i]=true;
}

cudaMemcpy(gpu, cpu.data(), 100*sizeof(bool), cudaMemcpyHostToDevice);

It returns

error: invalid use of void expression cudaMemcpyHostToDevice);

but the same code with a float vector will compile.

float *gpu; 

cudaMalloc(reinterpret_cast<void **>(&gpu), 100);

std::vector<float> cpu(100);

for(int i=0;i<100;i++){
  cpu[i]=i;
}

cudaMemcpy(gpu, cpu.data(), 100*sizeof(float), cudaMemcpyHostToDevice);

Why is this happening?

like image 627
user3667089 Avatar asked Mar 17 '26 06:03

user3667089


1 Answers

vector<bool> is a mistake from C++98 that we cannot get rid of (at least in terms of occupying the name). The standard recommends that it keeps the storage as a space-optimized representation of bits and that's what most implementations do.

You can work around this by using vector<uint8_t> instead.

like image 171
krzaq Avatar answered Mar 20 '26 09:03

krzaq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!