Abstract add_cgpu function, to handle device id numbering and devices array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
diff --git a/cgminer.c b/cgminer.c
index 11a7218..84019bb 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -4263,6 +4263,31 @@ void enable_device(struct cgpu_info *cgpu)
#endif
}
+struct _cgpu_devid_counter {
+ char name[4];
+ int lastid;
+ UT_hash_handle hh;
+};
+
+bool add_cgpu(struct cgpu_info*cgpu)
+{
+ static struct _cgpu_devid_counter *devids = NULL;
+ struct _cgpu_devid_counter *d;
+
+ HASH_FIND_STR(devids, cgpu->api->name, d);
+ if (d)
+ cgpu->device_id = ++d->lastid;
+ else
+ {
+ d = malloc(sizeof(*d));
+ memcpy(d->name, cgpu->api->name, sizeof(d->name));
+ cgpu->device_id = d->lastid = 0;
+ HASH_ADD_STR(devids, name, d);
+ }
+ devices[total_devices++] = cgpu;
+ return true;
+}
+
int main (int argc, char *argv[])
{
struct block *block, *tmpblock;
diff --git a/driver-bitforce.c b/driver-bitforce.c
index 3a6deef..3c93c55 100644
--- a/driver-bitforce.c
+++ b/driver-bitforce.c
@@ -92,7 +92,6 @@ static void BFwrite(int fd, const void *buf, ssize_t bufLen)
static bool bitforce_detect_one(const char *devpath)
{
char pdevbuf[0x100];
- static int i = 0;
if (total_devices == MAX_DEVICES)
return false;
@@ -117,14 +116,11 @@ static bool bitforce_detect_one(const char *devpath)
// We have a real BitForce!
struct cgpu_info *bitforce;
bitforce = calloc(1, sizeof(*bitforce));
- devices[total_devices++] = bitforce;
bitforce->api = &bitforce_api;
- bitforce->device_id = i++;
bitforce->device_path = strdup(devpath);
bitforce->deven = DEV_ENABLED;
bitforce->threads = 1;
-
- return true;
+ return add_cgpu(bitforce);
}
static bool bitforce_detect_auto_udev()
diff --git a/driver-cpu.c b/driver-cpu.c
index 04d307c..69c112e 100644
--- a/driver-cpu.c
+++ b/driver-cpu.c
@@ -739,13 +739,12 @@ static void cpu_detect()
for (i = 0; i < opt_n_threads; ++i) {
struct cgpu_info *cgpu;
- cgpu = devices[total_devices + i] = &cpus[i];
+ cgpu = &cpus[i];
cgpu->api = &cpu_api;
cgpu->deven = DEV_ENABLED;
- cgpu->device_id = i;
cgpu->threads = 1;
+ add_cgpu(cgpu);
}
- total_devices += opt_n_threads;
}
static void reinit_cpu_device(struct cgpu_info *cpu)
diff --git a/driver-icarus.c b/driver-icarus.c
index ba96b64..592500e 100644
--- a/driver-icarus.c
+++ b/driver-icarus.c
@@ -191,10 +191,9 @@ static bool icarus_detect_one(const char *devpath)
struct cgpu_info *icarus;
icarus = calloc(1, sizeof(struct cgpu_info));
icarus->api = &icarus_api;
- icarus->device_id = total_devices;
icarus->device_path = strdup(devpath);
icarus->threads = 1;
- devices[total_devices++] = icarus;
+ add_cgpu(icarus);
applog(LOG_INFO, "Found Icarus at %s, mark as %d",
devpath, icarus->device_id);
diff --git a/driver-opencl.c b/driver-opencl.c
index a066611..7d8197a 100644
--- a/driver-opencl.c
+++ b/driver-opencl.c
@@ -1093,12 +1093,13 @@ static void opencl_detect()
for (i = 0; i < nDevs; ++i) {
struct cgpu_info *cgpu;
- cgpu = devices[total_devices++] = &gpus[i];
+ cgpu = &gpus[i];
cgpu->deven = DEV_ENABLED;
cgpu->api = &opencl_api;
cgpu->device_id = i;
cgpu->threads = opt_g_threads;
cgpu->virtual_gpu = i;
+ add_cgpu(cgpu);
}
if (!opt_noadl)
diff --git a/miner.h b/miner.h
index aa46beb..259a85e 100644
--- a/miner.h
+++ b/miner.h
@@ -275,6 +275,8 @@ struct cgpu_info {
time_t last_share_pool_time;
};
+extern bool add_cgpu(struct cgpu_info*);
+
struct thread_q {
struct list_head q;