From 1a96b71935b2ee38d4f84be352d3d6df8e89d551 Mon Sep 17 00:00:00 2001 From: Tom Deakin Date: Fri, 29 Apr 2016 13:59:31 +0100 Subject: [PATCH] First attempt at parse args --- src/main.cpp | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 008a6a2..cabac5f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "common.h" #include "Stream.h" @@ -20,6 +21,7 @@ const unsigned int ARRAY_SIZE = 52428800; const unsigned int ntimes = 10; +unsigned int deviceIndex = 0; template @@ -28,6 +30,8 @@ void check_solution(const unsigned int ntimes, std::vector& a, std::vector template void run(); +void parseArguments(int argc, char *argv[]); + int main(int argc, char *argv[]) { std::cout @@ -35,7 +39,9 @@ int main(int argc, char *argv[]) << "Version: " << VERSION_STRING << std::endl << "Implementation: " << IMPLEMENTATION_STRING << std::endl; - run(); + parseArguments(argc, argv); + + run(); } @@ -186,3 +192,34 @@ void check_solution(const unsigned int ntimes, std::vector& a, std::vector } +int parseUInt(const char *str, unsigned int *output) +{ + std::size_t next; + *output = std::stoul(str, &next); + return !next; +} + +void parseArguments(int argc, char *argv[]) +{ + for (int i = 1; i < argc; i++) + { + if (!std::string("--list").compare(argv[i])) + { + #if defined(CUDA) + CUDAStream::listDevices(); + #elif defined(OCL) + OCLStream::listDevices(); + #endif + exit(EXIT_SUCCESS); + } + else if (!std::string("--device").compare(argv[i])) + { + if (++i >= argc || !parseUInt(argv[i], &deviceIndex)) + { + std::cerr << "Invalid device index." << std::endl; + exit(EXIT_FAILURE); + } + } + } +} +