I want to fill a device array with a constant float value. It seems cudaMemset only allows copying byte values. Is there an easy way to fill a device array with constant values of >1 byte (short of making a temporary copy in host memory or using a kernel to write the values)?
You’re right, memset only works for bytes. If you’re using the runtime API, you can use thrust::fill() instead.
Here’s some example code:
#include <thrust/device_ptr.h>
#include <thrust/fill.h>
...
float *d_array = ...; // pointer to device memory
float fill_value = ...; // the value to use for the fill
unsigned int N = ...; // the number of elements in the array
// wrap up the pointer in a device_ptr
thrust::device_ptr<float> dev_ptr(d_array);
// fill the array
thrust::fill(dev_ptr, dev_ptr + N, fill_value);