Templated cuda version, ready for more function types
Further templatization and some cleaning of CUDA version