Preinitialise the devices only once on startup.
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
diff --git a/main.c b/main.c
index 3c28514..07458b8 100644
--- a/main.c
+++ b/main.c
@@ -4023,6 +4023,9 @@ int main (int argc, char *argv[])
#ifdef HAVE_OPENCL
i = 0;
+ if (nDevs > 0)
+ preinit_devices();
+
/* start GPU mining threads */
for (j = 0; j < nDevs * opt_g_threads; j++) {
int gpu = j % nDevs;
diff --git a/ocl.c b/ocl.c
index 5fde373..873bfa4 100644
--- a/ocl.c
+++ b/ocl.c
@@ -52,6 +52,8 @@ char *file_contents(const char *filename, int *length)
return (char*)buffer;
}
+static cl_uint numDevices;
+
int clDevicesNum() {
cl_int status = 0;
@@ -111,6 +113,95 @@ int clDevicesNum() {
return numDevices;
}
+static cl_platform_id platform = NULL;
+static cl_device_id *devices;
+
+int preinit_devices(void)
+{
+ cl_int status;
+ cl_uint numPlatforms;
+ int i;
+
+ status = clGetPlatformIDs(0, NULL, &numPlatforms);
+ if (status != CL_SUCCESS)
+ {
+ applog(LOG_ERR, "Error: Getting Platforms. (clGetPlatformsIDs)");
+ return -1;
+ }
+
+ if (numPlatforms > 0)
+ {
+ cl_platform_id* platforms = (cl_platform_id *)malloc(numPlatforms*sizeof(cl_platform_id));
+ status = clGetPlatformIDs(numPlatforms, platforms, NULL);
+ if (status != CL_SUCCESS)
+ {
+ applog(LOG_ERR, "Error: Getting Platform Ids. (clGetPlatformsIDs)");
+ return -1;
+ }
+
+ for(i = 0; i < numPlatforms; ++i)
+ {
+ char pbuff[100];
+ status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
+ if (status != CL_SUCCESS)
+ {
+ applog(LOG_ERR, "Error: Getting Platform Info. (clGetPlatformInfo)");
+ free(platforms);
+ return -1;
+ }
+ platform = platforms[i];
+ if (!strcmp(pbuff, "Advanced Micro Devices, Inc."))
+ {
+ break;
+ }
+ }
+ free(platforms);
+ }
+
+ if (platform == NULL) {
+ perror("NULL platform found!\n");
+ return -1;
+ }
+
+ status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
+ if (status != CL_SUCCESS)
+ {
+ applog(LOG_ERR, "Error: Getting Device IDs (num)");
+ return -1;
+ }
+
+ if (numDevices > 0 ) {
+ devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
+
+ /* Now, get the device list data */
+
+ status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
+ if (status != CL_SUCCESS)
+ {
+ applog(LOG_ERR, "Error: Getting Device IDs (list)");
+ return -1;
+ }
+
+ applog(LOG_INFO, "List of devices:");
+
+ unsigned int i;
+ for(i=0; i<numDevices; i++) {
+ char pbuff[100];
+ status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
+ if (status != CL_SUCCESS)
+ {
+ applog(LOG_ERR, "Error: Getting Device Info");
+ return -1;
+ }
+
+ applog(LOG_INFO, "\t%i\t%s", i, pbuff);
+ }
+
+ } else return -1;
+
+ return 0;
+}
+
static int advance(char **area, unsigned *remaining, const char *marker)
{
char *find = memmem(*area, *remaining, marker, strlen(marker));
@@ -176,7 +267,6 @@ void patch_opcodes(char *w, unsigned remaining)
_clState *initCQ(_clState *clState, unsigned int gpu)
{
cl_int status = 0;
- cl_device_id *devices = clState->devices;
/* create a cl program executable for all the devices specified */
status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
@@ -226,115 +316,32 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize)
{
int patchbfi = 0;
cl_int status = 0;
- unsigned int i;
+ size_t nDevices;
_clState *clState = calloc(1, sizeof(_clState));
- cl_uint numPlatforms;
- cl_platform_id platform = NULL;
- status = clGetPlatformIDs(0, NULL, &numPlatforms);
- if (status != CL_SUCCESS)
- {
- applog(LOG_ERR, "Error: Getting Platforms. (clGetPlatformsIDs)");
- return NULL;
- }
-
- if (numPlatforms > 0)
- {
- cl_platform_id* platforms = (cl_platform_id *)malloc(numPlatforms*sizeof(cl_platform_id));
- status = clGetPlatformIDs(numPlatforms, platforms, NULL);
- if (status != CL_SUCCESS)
- {
- applog(LOG_ERR, "Error: Getting Platform Ids. (clGetPlatformsIDs)");
- return NULL;
- }
-
- for(i = 0; i < numPlatforms; ++i)
- {
- char pbuff[100];
- status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
- if (status != CL_SUCCESS)
- {
- applog(LOG_ERR, "Error: Getting Platform Info. (clGetPlatformInfo)");
- free(platforms);
- return NULL;
- }
- platform = platforms[i];
- if (!strcmp(pbuff, "Advanced Micro Devices, Inc."))
- {
- break;
- }
- }
- free(platforms);
- }
-
- if (platform == NULL) {
- perror("NULL platform found!\n");
- return NULL;
- }
+ cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
- size_t nDevices;
- cl_uint numDevices;
- status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
+ clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
if (status != CL_SUCCESS)
{
- applog(LOG_ERR, "Error: Getting Device IDs (num)");
+ applog(LOG_ERR, "Error: Creating Context. (clCreateContextFromType)");
return NULL;
}
- cl_device_id *devices;
- if (numDevices > 0 ) {
- devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
- clState->devices = devices;
-
- /* Now, get the device list data */
-
- status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
+ if (gpu < numDevices) {
+ char pbuff[100];
+ status = clGetDeviceInfo(devices[gpu], CL_DEVICE_NAME, sizeof(pbuff), pbuff, &nDevices);
if (status != CL_SUCCESS)
{
- applog(LOG_ERR, "Error: Getting Device IDs (list)");
- return NULL;
- }
-
- applog(LOG_INFO, "List of devices:");
-
- unsigned int i;
- for(i=0; i<numDevices; i++) {
- char pbuff[100];
- status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
- if (status != CL_SUCCESS)
- {
- applog(LOG_ERR, "Error: Getting Device Info");
- return NULL;
- }
-
- applog(LOG_INFO, "\t%i\t%s", i, pbuff);
- }
-
- if (gpu < numDevices) {
- char pbuff[100];
- status = clGetDeviceInfo(devices[gpu], CL_DEVICE_NAME, sizeof(pbuff), pbuff, &nDevices);
- if (status != CL_SUCCESS)
- {
- applog(LOG_ERR, "Error: Getting Device Info");
- return NULL;
- }
-
- applog(LOG_INFO, "Selected %i: %s", gpu, pbuff);
- strncpy(name, pbuff, nameSize);
- } else {
- applog(LOG_ERR, "Invalid GPU %i", gpu);
+ applog(LOG_ERR, "Error: Getting Device Info");
return NULL;
}
- } else return NULL;
-
- cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
-
- clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
- if (status != CL_SUCCESS)
- {
- applog(LOG_ERR, "Error: Creating Context. (clCreateContextFromType)");
+ applog(LOG_INFO, "Selected %i: %s", gpu, pbuff);
+ strncpy(name, pbuff, nameSize);
+ } else {
+ applog(LOG_ERR, "Invalid GPU %i", gpu);
return NULL;
}
diff --git a/ocl.h b/ocl.h
index 0960a4c..a95f972 100644
--- a/ocl.h
+++ b/ocl.h
@@ -18,11 +18,11 @@ typedef struct {
cl_uint preferred_vwidth;
size_t max_work_size;
size_t work_size;
- cl_device_id *devices;
} _clState;
extern char *file_contents(const char *filename, int *length);
extern int clDevicesNum();
+extern int preinit_devices(void);
extern _clState *initCQ(_clState *clState, unsigned int gpu);
extern _clState *initCl(unsigned int gpu, char *name, size_t nameSize);
#endif /* HAVE_OPENCL */