Commit 727184d6ef70933f6d94243ab70175f8ec57edff

Con Kolivas 2013-06-03T11:09:36

Change the --device parameter parsing and configuration to use ranges and comma separated values.

diff --git a/cgminer.c b/cgminer.c
index 3bf959f..1e6eef3 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -110,7 +110,9 @@ bool opt_restart = true;
 static bool opt_nogpu;
 
 struct list_head scan_devices;
-static signed int devices_enabled;
+static bool devices_enabled[MAX_DEVICES];
+static int opt_devs_enabled;
+static bool opt_display_devs;
 static bool opt_removedisabled;
 int total_devices;
 struct cgpu_info **devices;
@@ -576,21 +578,52 @@ static char *add_serial(char *arg)
 }
 #endif
 
+void get_intrange(char *arg, int *val1, int *val2)
+{
+	if (sscanf(arg, "%d-%d", val1, val2) == 1)
+		*val2 = *val1;
+}
+
 static char *set_devices(char *arg)
 {
-	int i = strtol(arg, &arg, 0);
+	int i, val1 = 0, val2 = 0;
+	char *nextptr;
 
 	if (*arg) {
 		if (*arg == '?') {
-			devices_enabled = -1;
+			opt_display_devs = true;
 			return NULL;
 		}
-		return "Invalid device number";
+	} else
+		return "Invalid device parameters";
+
+	nextptr = strtok(arg, ",");
+	if (nextptr == NULL)
+		return "Invalid parameters for set devices";
+	get_intrange(nextptr, &val1, &val2);
+	if (val1 < 0 || val1 > MAX_DEVICES || val2 < 0 || val2 > MAX_DEVICES ||
+	    val1 > val2) {
+		return "Invalid value passed to set devices";
+	}
+
+	for (i = val1; i <= val2; i++) {
+		devices_enabled[i] = true;
+		opt_devs_enabled++;
+	}
+
+	while ((nextptr = strtok(NULL, ",")) != NULL) {
+		get_intrange(nextptr, &val1, &val2);
+		if (val1 < 0 || val1 > MAX_DEVICES || val2 < 0 || val2 > MAX_DEVICES ||
+		val1 > val2) {
+			return "Invalid value passed to set devices";
+		}
+
+		for (i = val1; i <= val2; i++) {
+			devices_enabled[i] = true;
+			opt_devs_enabled++;
+		}
 	}
 
-	if (i < 0 || i >= (int)(sizeof(devices_enabled) * 8) - 1)
-		return "Invalid device number";
-	devices_enabled |= 1 << i;
 	return NULL;
 }
 
@@ -921,7 +954,7 @@ static struct opt_table opt_config_table[] = {
 		     "Enable debug output"),
 	OPT_WITH_ARG("--device|-d",
 		     set_devices, NULL, NULL,
-	             "Select device to use, (Use repeat -d for multiple devices, default: all)"),
+	             "Select device to use, one value, range and/or comma separated (e.g. 0-2,4) default: all"),
 	OPT_WITHOUT_ARG("--disable-gpu|-G",
 			opt_set_bool, &opt_nogpu,
 #ifdef HAVE_OPENCL
@@ -4007,11 +4040,24 @@ void write_config(FILE *fcfg)
 		fprintf(fcfg, ",\n\"stop-time\" : \"%d:%d\"", schedstop.tm.tm_hour, schedstop.tm.tm_min);
 	if (opt_socks_proxy && *opt_socks_proxy)
 		fprintf(fcfg, ",\n\"socks-proxy\" : \"%s\"", json_escape(opt_socks_proxy));
-	if (devices_enabled) {
-		for (i = 0; i < (int)(sizeof(devices_enabled) * 8) - 1; ++i) {
-			if (devices_enabled & (1 << i))
-				fprintf(fcfg, ",\n\"device\" : \"%d\"", i);
+	if (opt_devs_enabled) {
+		fprintf(fcfg, ",\n\"device\" : \"");
+		bool extra_devs = false;
+
+		for (i = 0; i < MAX_DEVICES; i++) {
+			if (devices_enabled[i]) {
+				int startd = i;
+
+				if (extra_devs)
+					fprintf(fcfg, ",");
+				while (i < MAX_DEVICES && devices_enabled[i + 1])
+					++i;
+				fprintf(fcfg, "%d", startd);
+				if (i > startd)
+					fprintf(fcfg, "-%d", i);
+			}
 		}
+		fprintf(fcfg, "\"");
 	}
 	if (opt_removedisabled)
 		fprintf(fcfg, ",\n\"remove-disabled\" : true");
@@ -7321,7 +7367,7 @@ int main(int argc, char *argv[])
 	}
 
 #ifdef HAVE_CURSES
-	if (opt_realquiet || devices_enabled == -1)
+	if (opt_realquiet || opt_display_devs)
 		use_curses = false;
 
 	if (use_curses)
@@ -7417,7 +7463,7 @@ int main(int argc, char *argv[])
 		avalon_drv.drv_detect();
 #endif
 
-	if (devices_enabled == -1) {
+	if (opt_display_devs) {
 		applog(LOG_ERR, "Devices detected:");
 		for (i = 0; i < total_devices; ++i) {
 			struct cgpu_info *cgpu = devices[i];
@@ -7430,9 +7476,9 @@ int main(int argc, char *argv[])
 	}
 
 	mining_threads = 0;
-	if (devices_enabled) {
-		for (i = 0; i < (int)(sizeof(devices_enabled) * 8) - 1; ++i) {
-			if (devices_enabled & (1 << i)) {
+	if (opt_devs_enabled) {
+		for (i = 0; i < MAX_DEVICES; i++) {
+			if (devices_enabled[i]) {
 				if (i >= total_devices)
 					quit (1, "Command line options set a device that doesn't exist");
 				enable_device(devices[i]);
diff --git a/driver-opencl.c b/driver-opencl.c
index a1fef8b..501b411 100644
--- a/driver-opencl.c
+++ b/driver-opencl.c
@@ -285,14 +285,6 @@ char *set_gpu_map(char *arg)
 	return NULL;
 }
 
-void get_intrange(char *arg, int *val1, int *val2)
-{
-	if (sscanf(arg, "%d-%d", val1, val2) == 1) {
-		*val2 = *val1;
-		*val1 = 0;
-	}
-}
-
 char *set_gpu_engine(char *arg)
 {
 	int i, val1 = 0, val2 = 0, device = 0;
diff --git a/miner.h b/miner.h
index 410df53..63b387f 100644
--- a/miner.h
+++ b/miner.h
@@ -919,12 +919,14 @@ extern void api(int thr_id);
 
 extern struct pool *current_pool(void);
 extern int enabled_pools;
+extern void get_intrange(char *arg, int *val1, int *val2);
 extern bool detect_stratum(struct pool *pool, char *url);
 extern void print_summary(void);
 extern struct pool *add_pool(void);
 extern bool add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
 
 #define MAX_GPUDEVICES 16
+#define MAX_DEVICES 4096
 
 #define MIN_INTENSITY -10
 #define _MIN_INTENSITY_STR "-10"