Make grainsize() a __host__ __device__ function.