is it possible to Memset float values?


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)?



It looks like this has been attempted before:

It doesn’t look like the original poster was very successful but you could give it a try if there’s no other suggestions

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);

Hey that looks nice! Thanks for pointing that out.