Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 799 lines (695 sloc) 26.352 kb
13b43cf @ckolivas Update copyright and authors.
ckolivas authored
1 /*
be9db9c @ckolivas Copyright updates.
ckolivas authored
2 * Copyright 2011-2012 Con Kolivas
810ad04 @ckolivas More copyright updates.
ckolivas authored
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
d1cddf8 @ckolivas Update licensing to GPL V3.
ckolivas authored
6 * Software Foundation; either version 3 of the License, or (at your option)
810ad04 @ckolivas More copyright updates.
ckolivas authored
7 * any later version. See COPYING for more details.
13b43cf @ckolivas Update copyright and authors.
ckolivas authored
8 */
be9db9c @ckolivas Copyright updates.
ckolivas authored
9
594b38b @ckolivas Fix redefinition of gnu source.
ckolivas authored
10 #include "config.h"
a93b22c @ckolivas Make it possible to build without GPU mining by picking up HAVE_OPENC…
ckolivas authored
11 #ifdef HAVE_OPENCL
594b38b @ckolivas Fix redefinition of gnu source.
ckolivas authored
12
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
13 #include <signal.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <stdio.h>
17 #include <sys/types.h>
ec83191 @ycros Build on windows using mingw32.
ycros authored
18
19 #ifdef WIN32
20 #include <winsock2.h>
21 #else
22 #include <sys/socket.h>
23 #include <netinet/in.h>
52d6e7c @ycros Fixed fread issues under Windows.
ycros authored
24 #include <netdb.h>
ec83191 @ycros Build on windows using mingw32.
ycros authored
25 #endif
26
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
27 #include <time.h>
28 #include <sys/time.h>
29 #include <pthread.h>
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
30 #include <sys/stat.h>
31 #include <unistd.h>
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
32
33 #include "findnonce.h"
34 #include "ocl.h"
35
53e9c61 @ckolivas Find the gpu platform with the most devices and use that if no platfo…
ckolivas authored
36 int opt_platform_id = -1;
656b485 @ckolivas Make the worksize and vector width configurable.
ckolivas authored
37
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
38 char *file_contents(const char *filename, int *length)
39 {
3d5f555 @ckolivas Allow a custom kernel path to be entered on the command line.
ckolivas authored
40 char *fullpath = alloca(PATH_MAX);
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
41 void *buffer;
413d970 @ckolivas Make cgminer look in the install directory for the .cl files making m…
ckolivas authored
42 FILE *f;
43
3d5f555 @ckolivas Allow a custom kernel path to be entered on the command line.
ckolivas authored
44 strcpy(fullpath, opt_kernel_path);
413d970 @ckolivas Make cgminer look in the install directory for the .cl files making m…
ckolivas authored
45 strcat(fullpath, filename);
46
2053de6 @ckolivas Add the directory name from the arguments cgminer was called from as …
ckolivas authored
47 /* Try in the optional kernel path or installed prefix first */
48 f = fopen(fullpath, "rb");
49 if (!f) {
50 /* Then try from the path cgminer was called */
51 strcpy(fullpath, cgminer_path);
52 strcat(fullpath, filename);
413d970 @ckolivas Make cgminer look in the install directory for the .cl files making m…
ckolivas authored
53 f = fopen(fullpath, "rb");
2053de6 @ckolivas Add the directory name from the arguments cgminer was called from as …
ckolivas authored
54 }
55 /* Finally try opening it directly */
56 if (!f)
57 f = fopen(filename, "rb");
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
58
59 if (!f) {
413d970 @ckolivas Make cgminer look in the install directory for the .cl files making m…
ckolivas authored
60 applog(LOG_ERR, "Unable to open %s or %s for reading", filename, fullpath);
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
61 return NULL;
62 }
63
64 fseek(f, 0, SEEK_END);
65 *length = ftell(f);
66 fseek(f, 0, SEEK_SET);
67
68 buffer = malloc(*length+1);
69 *length = fread(buffer, 1, *length, f);
70 fclose(f);
71 ((char*)buffer)[*length] = '\0';
72
73 return (char*)buffer;
74 }
75
a4f4781 @ckolivas Iterate over all platforms displaying their information and number of…
ckolivas authored
76 int clDevicesNum(void) {
5d5584f @ckolivas Explicitly check for nvidia in opencl platform strings as well.
ckolivas authored
77 cl_int status;
b8f845b @ckolivas Display information about the opencl platform with verbose enabled.
ckolivas authored
78 char pbuff[256];
5d5584f @ckolivas Explicitly check for nvidia in opencl platform strings as well.
ckolivas authored
79 cl_uint numDevices;
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
80 cl_uint numPlatforms;
a4f4781 @ckolivas Iterate over all platforms displaying their information and number of…
ckolivas authored
81 cl_platform_id *platforms;
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
82 cl_platform_id platform = NULL;
471daec @ckolivas Initialise mdplatform.
ckolivas authored
83 unsigned int most_devices = 0, i, mdplatform = 0;
5d5584f @ckolivas Explicitly check for nvidia in opencl platform strings as well.
ckolivas authored
84
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
85 status = clGetPlatformIDs(0, NULL, &numPlatforms);
efebee5 @rustyrussell Fix the case where there are no GPUs, and exit if they give errors.
rustyrussell authored
86 /* If this fails, assume no GPUs. */
6442c1a @ckolivas Style police.
ckolivas authored
87 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
88 applog(LOG_ERR, "Error %d: clGetPlatformsIDs failed (no OpenCL SDK installed?)", status);
a4f4781 @ckolivas Iterate over all platforms displaying their information and number of…
ckolivas authored
89 return -1;
c566605 @ckolivas Tab dainbramage.
ckolivas authored
90 }
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
91
a4f4781 @ckolivas Iterate over all platforms displaying their information and number of…
ckolivas authored
92 if (numPlatforms == 0) {
93 applog(LOG_ERR, "clGetPlatformsIDs returned no platforms (no OpenCL SDK installed?)");
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
94 return -1;
95 }
96
a4f4781 @ckolivas Iterate over all platforms displaying their information and number of…
ckolivas authored
97 platforms = (cl_platform_id *)alloca(numPlatforms*sizeof(cl_platform_id));
98 status = clGetPlatformIDs(numPlatforms, platforms, NULL);
6442c1a @ckolivas Style police.
ckolivas authored
99 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
100 applog(LOG_ERR, "Error %d: Getting Platform Ids. (clGetPlatformsIDs)", status);
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
101 return -1;
102 }
103
a4f4781 @ckolivas Iterate over all platforms displaying their information and number of…
ckolivas authored
104 for (i = 0; i < numPlatforms; i++) {
105 status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
106 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
107 applog(LOG_ERR, "Error %d: Getting Platform Info. (clGetPlatformInfo)", status);
a4f4781 @ckolivas Iterate over all platforms displaying their information and number of…
ckolivas authored
108 return -1;
109 }
110 platform = platforms[i];
53c1e9a @ckolivas Allow the OpenCL platform ID to be chosen with --gpu-platform.
ckolivas authored
111 applog(LOG_INFO, "CL Platform %d vendor: %s", i, pbuff);
a4f4781 @ckolivas Iterate over all platforms displaying their information and number of…
ckolivas authored
112 status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(pbuff), pbuff, NULL);
113 if (status == CL_SUCCESS)
53c1e9a @ckolivas Allow the OpenCL platform ID to be chosen with --gpu-platform.
ckolivas authored
114 applog(LOG_INFO, "CL Platform %d name: %s", i, pbuff);
a4f4781 @ckolivas Iterate over all platforms displaying their information and number of…
ckolivas authored
115 status = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(pbuff), pbuff, NULL);
116 if (status == CL_SUCCESS)
53c1e9a @ckolivas Allow the OpenCL platform ID to be chosen with --gpu-platform.
ckolivas authored
117 applog(LOG_INFO, "CL Platform %d version: %s", i, pbuff);
7d53fba @ckolivas Reinstate GPU only opencl device detection.
ckolivas authored
118 status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
a4f4781 @ckolivas Iterate over all platforms displaying their information and number of…
ckolivas authored
119 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
120 applog(LOG_ERR, "Error %d: Getting Device IDs (num)", status);
884f83f @ckolivas Allow more platforms to be probed if first does not return GPUs.
ckolivas authored
121 if (i < numPlatforms - 1)
122 continue;
a4f4781 @ckolivas Iterate over all platforms displaying their information and number of…
ckolivas authored
123 return -1;
124 }
53c1e9a @ckolivas Allow the OpenCL platform ID to be chosen with --gpu-platform.
ckolivas authored
125 applog(LOG_INFO, "Platform %d devices: %d", i, numDevices);
53e9c61 @ckolivas Find the gpu platform with the most devices and use that if no platfo…
ckolivas authored
126 if (numDevices > most_devices) {
a4f4781 @ckolivas Iterate over all platforms displaying their information and number of…
ckolivas authored
127 most_devices = numDevices;
53e9c61 @ckolivas Find the gpu platform with the most devices and use that if no platfo…
ckolivas authored
128 mdplatform = i;
129 }
9175e4f @ckolivas Display all OpenCL devices when -n is called as well to allow debuggi…
ckolivas authored
130 if (numDevices) {
131 unsigned int j;
132 char pbuff[256];
133 cl_device_id *devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
134
7d53fba @ckolivas Reinstate GPU only opencl device detection.
ckolivas authored
135 clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
9175e4f @ckolivas Display all OpenCL devices when -n is called as well to allow debuggi…
ckolivas authored
136 for (j = 0; j < numDevices; j++) {
137 clGetDeviceInfo(devices[j], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
138 applog(LOG_INFO, "\t%i\t%s", j, pbuff);
139 }
140 free(devices);
141 }
a4f4781 @ckolivas Iterate over all platforms displaying their information and number of…
ckolivas authored
142 }
143
53e9c61 @ckolivas Find the gpu platform with the most devices and use that if no platfo…
ckolivas authored
144 if (opt_platform_id < 0)
145 opt_platform_id = mdplatform;;
146
a4f4781 @ckolivas Iterate over all platforms displaying their information and number of…
ckolivas authored
147 return most_devices;
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
148 }
149
623b9b9 @ckolivas Patch bitalign separately from bfi_int.
ckolivas authored
150 static int advance(char **area, unsigned *remaining, const char *marker)
c548dea @ckolivas First BFI_INT patch changes.
ckolivas authored
151 {
d28f526 @ckolivas Need memmem in advance() to work properly for patching.
ckolivas authored
152 char *find = memmem(*area, *remaining, marker, strlen(marker));
153
623b9b9 @ckolivas Patch bitalign separately from bfi_int.
ckolivas authored
154 if (!find) {
405a212 @ckolivas Remove unnecessary check for opt_debug on every invocation of applog …
ckolivas authored
155 applog(LOG_DEBUG, "Marker \"%s\" not found", marker);
623b9b9 @ckolivas Patch bitalign separately from bfi_int.
ckolivas authored
156 return 0;
157 }
c548dea @ckolivas First BFI_INT patch changes.
ckolivas authored
158 *remaining -= find - *area;
159 *area = find;
623b9b9 @ckolivas Patch bitalign separately from bfi_int.
ckolivas authored
160 return 1;
c548dea @ckolivas First BFI_INT patch changes.
ckolivas authored
161 }
162
fdb46f2 @ckolivas 32bit fixes.
ckolivas authored
163 #define OP3_INST_BFE_UINT 4ULL
164 #define OP3_INST_BFE_INT 5ULL
165 #define OP3_INST_BFI_INT 6ULL
166 #define OP3_INST_BIT_ALIGN_INT 12ULL
167 #define OP3_INST_BYTE_ALIGN_INT 13ULL
c548dea @ckolivas First BFI_INT patch changes.
ckolivas authored
168
169 void patch_opcodes(char *w, unsigned remaining)
170 {
171 uint64_t *opcode = (uint64_t *)w;
172 int patched = 0;
173 int count_bfe_int = 0;
174 int count_bfe_uint = 0;
175 int count_byte_align = 0;
6442c1a @ckolivas Style police.
ckolivas authored
176 while (42) {
c548dea @ckolivas First BFI_INT patch changes.
ckolivas authored
177 int clamp = (*opcode >> (32 + 31)) & 0x1;
178 int dest_rel = (*opcode >> (32 + 28)) & 0x1;
179 int alu_inst = (*opcode >> (32 + 13)) & 0x1f;
180 int s2_neg = (*opcode >> (32 + 12)) & 0x1;
181 int s2_rel = (*opcode >> (32 + 9)) & 0x1;
182 int pred_sel = (*opcode >> 29) & 0x3;
183 if (!clamp && !dest_rel && !s2_neg && !s2_rel && !pred_sel) {
91e5cef @ckolivas Actually get first BFI_INT patch working.
ckolivas authored
184 if (alu_inst == OP3_INST_BFE_INT) {
185 count_bfe_int++;
186 } else if (alu_inst == OP3_INST_BFE_UINT) {
187 count_bfe_uint++;
188 } else if (alu_inst == OP3_INST_BYTE_ALIGN_INT) {
189 count_byte_align++;
190 // patch this instruction to BFI_INT
826cc48 @ckolivas Opcode should be ULL.
ckolivas authored
191 *opcode &= 0xfffc1fffffffffffULL;
91e5cef @ckolivas Actually get first BFI_INT patch working.
ckolivas authored
192 *opcode |= OP3_INST_BFI_INT << (32 + 13);
193 patched++;
194 }
c548dea @ckolivas First BFI_INT patch changes.
ckolivas authored
195 }
6442c1a @ckolivas Style police.
ckolivas authored
196 if (remaining <= 8)
91e5cef @ckolivas Actually get first BFI_INT patch working.
ckolivas authored
197 break;
c548dea @ckolivas First BFI_INT patch changes.
ckolivas authored
198 opcode++;
199 remaining -= 8;
200 }
405a212 @ckolivas Remove unnecessary check for opt_debug on every invocation of applog …
ckolivas authored
201 applog(LOG_DEBUG, "Potential OP3 instructions identified: "
202 "%i BFE_INT, %i BFE_UINT, %i BYTE_ALIGN",
203 count_bfe_int, count_bfe_uint, count_byte_align);
204 applog(LOG_DEBUG, "Patched a total of %i BFI_INT instructions", patched);
c548dea @ckolivas First BFI_INT patch changes.
ckolivas authored
205 }
206
b1289a0 @ckolivas Revert "Move the non cl_ variables into the cgpu info struct to allow…
ckolivas authored
207 _clState *initCl(unsigned int gpu, char *name, size_t nameSize)
c2e4165 @ckolivas Detect platforms that support cl_amd_media_ops and decide whether to …
ckolivas authored
208 {
5d5584f @ckolivas Explicitly check for nvidia in opencl platform strings as well.
ckolivas authored
209 _clState *clState = calloc(1, sizeof(_clState));
d18d556 @ckolivas Do not attempt to build the program that becomes the kernel twice. Th…
ckolivas authored
210 bool patchbfi = false, prog_built = false;
5d5584f @ckolivas Explicitly check for nvidia in opencl platform strings as well.
ckolivas authored
211 cl_platform_id platform = NULL;
2c33f12 @ckolivas Whitelist ATI SDK 2.6 to use the poclbm kernel by default.
ckolivas authored
212 char pbuff[256], vbuff[255];
53c1e9a @ckolivas Allow the OpenCL platform ID to be chosen with --gpu-platform.
ckolivas authored
213 cl_platform_id* platforms;
deff55c @ckolivas Allow different vectors to be set per device.
ckolivas authored
214 cl_uint preferred_vwidth;
5d5584f @ckolivas Explicitly check for nvidia in opencl platform strings as well.
ckolivas authored
215 cl_device_id *devices;
216 cl_uint numPlatforms;
217 cl_uint numDevices;
218 cl_int status;
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
219
cf54350 @ckolivas Revert "Preinitialise the devices only once on startup."
ckolivas authored
220 status = clGetPlatformIDs(0, NULL, &numPlatforms);
6442c1a @ckolivas Style police.
ckolivas authored
221 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
222 applog(LOG_ERR, "Error %d: Getting Platforms. (clGetPlatformsIDs)", status);
cf54350 @ckolivas Revert "Preinitialise the devices only once on startup."
ckolivas authored
223 return NULL;
224 }
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
225
53c1e9a @ckolivas Allow the OpenCL platform ID to be chosen with --gpu-platform.
ckolivas authored
226 platforms = (cl_platform_id *)alloca(numPlatforms*sizeof(cl_platform_id));
227 status = clGetPlatformIDs(numPlatforms, platforms, NULL);
228 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
229 applog(LOG_ERR, "Error %d: Getting Platform Ids. (clGetPlatformsIDs)", status);
53c1e9a @ckolivas Allow the OpenCL platform ID to be chosen with --gpu-platform.
ckolivas authored
230 return NULL;
231 }
5d5584f @ckolivas Explicitly check for nvidia in opencl platform strings as well.
ckolivas authored
232
56907db @ckolivas Fix harmless warnings with -Wsign-compare to allow cgminer to build w…
ckolivas authored
233 if (opt_platform_id >= (int)numPlatforms) {
53c1e9a @ckolivas Allow the OpenCL platform ID to be chosen with --gpu-platform.
ckolivas authored
234 applog(LOG_ERR, "Specified platform that does not exist");
235 return NULL;
236 }
cf54350 @ckolivas Revert "Preinitialise the devices only once on startup."
ckolivas authored
237
53c1e9a @ckolivas Allow the OpenCL platform ID to be chosen with --gpu-platform.
ckolivas authored
238 status = clGetPlatformInfo(platforms[opt_platform_id], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
239 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
240 applog(LOG_ERR, "Error %d: Getting Platform Info. (clGetPlatformInfo)", status);
53c1e9a @ckolivas Allow the OpenCL platform ID to be chosen with --gpu-platform.
ckolivas authored
241 return NULL;
cf54350 @ckolivas Revert "Preinitialise the devices only once on startup."
ckolivas authored
242 }
53c1e9a @ckolivas Allow the OpenCL platform ID to be chosen with --gpu-platform.
ckolivas authored
243 platform = platforms[opt_platform_id];
cf54350 @ckolivas Revert "Preinitialise the devices only once on startup."
ckolivas authored
244
245 if (platform == NULL) {
246 perror("NULL platform found!\n");
247 return NULL;
248 }
249
b8f845b @ckolivas Display information about the opencl platform with verbose enabled.
ckolivas authored
250 applog(LOG_INFO, "CL Platform vendor: %s", pbuff);
251 status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(pbuff), pbuff, NULL);
252 if (status == CL_SUCCESS)
253 applog(LOG_INFO, "CL Platform name: %s", pbuff);
2c33f12 @ckolivas Whitelist ATI SDK 2.6 to use the poclbm kernel by default.
ckolivas authored
254 status = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(vbuff), vbuff, NULL);
b8f845b @ckolivas Display information about the opencl platform with verbose enabled.
ckolivas authored
255 if (status == CL_SUCCESS)
2c33f12 @ckolivas Whitelist ATI SDK 2.6 to use the poclbm kernel by default.
ckolivas authored
256 applog(LOG_INFO, "CL Platform version: %s", vbuff);
b8f845b @ckolivas Display information about the opencl platform with verbose enabled.
ckolivas authored
257
7d53fba @ckolivas Reinstate GPU only opencl device detection.
ckolivas authored
258 status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
6442c1a @ckolivas Style police.
ckolivas authored
259 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
260 applog(LOG_ERR, "Error %d: Getting Device IDs (num)", status);
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
261 return NULL;
262 }
263
cf54350 @ckolivas Revert "Preinitialise the devices only once on startup."
ckolivas authored
264 if (numDevices > 0 ) {
265 devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
266
267 /* Now, get the device list data */
268
7d53fba @ckolivas Reinstate GPU only opencl device detection.
ckolivas authored
269 status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
6442c1a @ckolivas Style police.
ckolivas authored
270 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
271 applog(LOG_ERR, "Error %d: Getting Device IDs (list)", status);
cf54350 @ckolivas Revert "Preinitialise the devices only once on startup."
ckolivas authored
272 return NULL;
273 }
274
275 applog(LOG_INFO, "List of devices:");
276
277 unsigned int i;
3d4cfce @ckolivas Instead of using the BFI_INT patching hack on any device reporting cl…
ckolivas authored
278 for (i = 0; i < numDevices; i++) {
cf54350 @ckolivas Revert "Preinitialise the devices only once on startup."
ckolivas authored
279 status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
6442c1a @ckolivas Style police.
ckolivas authored
280 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
281 applog(LOG_ERR, "Error %d: Getting Device Info", status);
cf54350 @ckolivas Revert "Preinitialise the devices only once on startup."
ckolivas authored
282 return NULL;
283 }
284
285 applog(LOG_INFO, "\t%i\t%s", i, pbuff);
286 }
287
288 if (gpu < numDevices) {
5848c11 @ckolivas Confusion over the variable name for number of devices was passing a …
ckolivas authored
289 status = clGetDeviceInfo(devices[gpu], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
6442c1a @ckolivas Style police.
ckolivas authored
290 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
291 applog(LOG_ERR, "Error %d: Getting Device Info", status);
cf54350 @ckolivas Revert "Preinitialise the devices only once on startup."
ckolivas authored
292 return NULL;
293 }
294
295 applog(LOG_INFO, "Selected %i: %s", gpu, pbuff);
296 strncpy(name, pbuff, nameSize);
297 } else {
298 applog(LOG_ERR, "Invalid GPU %i", gpu);
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
299 return NULL;
300 }
301
cf54350 @ckolivas Revert "Preinitialise the devices only once on startup."
ckolivas authored
302 } else return NULL;
303
304 cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
305
7d53fba @ckolivas Reinstate GPU only opencl device detection.
ckolivas authored
306 clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
6442c1a @ckolivas Style police.
ckolivas authored
307 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
308 applog(LOG_ERR, "Error %d: Creating Context. (clCreateContextFromType)", status);
407df0f @ckolivas Use correct boolean testing and long enough string for cl_amd_media o…
ckolivas authored
309 return NULL;
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
310 }
311
3e61db1 @ckolivas Create command queue before compiling program in opencl.
ckolivas authored
312 /////////////////////////////////////////////////////////////////
313 // Create an OpenCL command queue
314 /////////////////////////////////////////////////////////////////
315 clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu],
316 CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &status);
317 if (status != CL_SUCCESS) /* Try again without OOE enable */
318 clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu], 0 , &status);
319 if (status != CL_SUCCESS) {
320 applog(LOG_ERR, "Error %d: Creating Command Queue. (clCreateCommandQueue)", status);
321 return NULL;
322 }
323
c2e4165 @ckolivas Detect platforms that support cl_amd_media_ops and decide whether to …
ckolivas authored
324 /* Check for BFI INT support. Hopefully people don't mix devices with
325 * and without it! */
326 char * extensions = malloc(1024);
26546ad @ckolivas Make the optimisations per-gpu card and update code to work properly …
ckolivas authored
327 const char * camo = "cl_amd_media_ops";
328 char *find;
c2e4165 @ckolivas Detect platforms that support cl_amd_media_ops and decide whether to …
ckolivas authored
329
26546ad @ckolivas Make the optimisations per-gpu card and update code to work properly …
ckolivas authored
330 status = clGetDeviceInfo(devices[gpu], CL_DEVICE_EXTENSIONS, 1024, (void *)extensions, NULL);
331 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
332 applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_EXTENSIONS", status);
26546ad @ckolivas Make the optimisations per-gpu card and update code to work properly …
ckolivas authored
333 return NULL;
334 }
335 find = strstr(extensions, camo);
336 if (find)
3d4cfce @ckolivas Instead of using the BFI_INT patching hack on any device reporting cl…
ckolivas authored
337 clState->hasBitAlign = true;
47a09ce @Diapolo added OpenCL >= 1.1 detection code, in preparation of OpenCL 1.1 glob…
Diapolo authored
338
339 /* Check for OpenCL >= 1.0 support, needed for global offset parameter usage. */
340 char * devoclver = malloc(1024);
341 const char * ocl10 = "OpenCL 1.0";
342
343 status = clGetDeviceInfo(devices[gpu], CL_DEVICE_VERSION, 1024, (void *)devoclver, NULL);
344 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
345 applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_VERSION", status);
47a09ce @Diapolo added OpenCL >= 1.1 detection code, in preparation of OpenCL 1.1 glob…
Diapolo authored
346 return NULL;
347 }
348 find = strstr(devoclver, ocl10);
196e8a0 Fix kernel selection process and build error.
Con Kolivas authored
349 if (!find)
47a09ce @Diapolo added OpenCL >= 1.1 detection code, in preparation of OpenCL 1.1 glob…
Diapolo authored
350 clState->hasOpenCL11plus = true;
3335221 @ckolivas Report preferred vector width.
ckolivas authored
351
deff55c @ckolivas Allow different vectors to be set per device.
ckolivas authored
352 status = clGetDeviceInfo(devices[gpu], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint), (void *)&preferred_vwidth, NULL);
26546ad @ckolivas Make the optimisations per-gpu card and update code to work properly …
ckolivas authored
353 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
354 applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT", status);
26546ad @ckolivas Make the optimisations per-gpu card and update code to work properly …
ckolivas authored
355 return NULL;
356 }
deff55c @ckolivas Allow different vectors to be set per device.
ckolivas authored
357 applog(LOG_DEBUG, "Preferred vector width reported %d", preferred_vwidth);
407df0f @ckolivas Use correct boolean testing and long enough string for cl_amd_media o…
ckolivas authored
358
b1289a0 @ckolivas Revert "Move the non cl_ variables into the cgpu info struct to allow…
ckolivas authored
359 status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), (void *)&clState->max_work_size, NULL);
26546ad @ckolivas Make the optimisations per-gpu card and update code to work properly …
ckolivas authored
360 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
361 applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_WORK_GROUP_SIZE", status);
26546ad @ckolivas Make the optimisations per-gpu card and update code to work properly …
ckolivas authored
362 return NULL;
19eea90 @ckolivas Implement code detecting max work size and optimal vector width.
ckolivas authored
363 }
405a212 @ckolivas Remove unnecessary check for opt_debug on every invocation of applog …
ckolivas authored
364 applog(LOG_DEBUG, "Max work group size reported %d", clState->max_work_size);
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
365
89eb1fa @ckolivas Check the maximum allocable memory size per opencl device.
ckolivas authored
366 status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_MEM_ALLOC_SIZE , sizeof(cl_ulong), (void *)&gpus[gpu].max_alloc, NULL);
367 if (status != CL_SUCCESS) {
368 applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_MEM_ALLOC_SIZE", status);
369 return NULL;
370 }
371 applog(LOG_DEBUG, "Max mem alloc size is %u", gpus[gpu].max_alloc);
372
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
373 /* Create binary filename based on parameters passed to opencl
374 * compiler to ensure we only load a binary that matches what would
375 * have otherwise created. The filename is:
621bcca @ckolivas Use global offset parameter to diablo and poclbm kernel ONLY for 1 ve…
ckolivas authored
376 * name + kernelname +/- g(offset) + v + vectors + w + work_size + l + sizeof(long) + .bin
39f7d2f @ckolivas Allow lookup gap and thread concurrency to be passed per device and s…
ckolivas authored
377 * For scrypt the filename is:
378 * name + kernelname + g + lg + lookup_gap + tc + thread_concurrency + w + work_size + l + sizeof(long) + .bin
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
379 */
380 char binaryfilename[255];
e6cf96a @ckolivas ALlow much longer filenames for kernels to load properly.
ckolivas authored
381 char filename[255];
4822cca @ckolivas Allow different kernels to be used by different devices and fix the l…
ckolivas authored
382 char numbuf[10];
a9e1a25 @ckolivas Make it possible to select the choice of kernel on the command line.
ckolivas authored
383
93efb72 @ckolivas Allow multiple different kernels to be chosen per device.
ckolivas authored
384 if (gpus[gpu].kernel == KL_NONE) {
dd740ca @ckolivas Provide initial support for the scrypt kernel to compile with and min…
ckolivas authored
385 if (opt_scrypt) {
386 applog(LOG_INFO, "Selecting scrypt kernel");
387 clState->chosen_kernel = KL_SCRYPT;
388 } else if (!strstr(name, "Tahiti") &&
389 /* Detect all 2.6 SDKs not with Tahiti and use diablo kernel */
39395eb @ckolivas Use poclbm preferentially on Tahiti now regardless of SDK.
ckolivas authored
390 (strstr(vbuff, "844.4") || // Linux 64 bit ATI 2.6 SDK
391 strstr(vbuff, "851.4") || // Windows 64 bit ""
392 strstr(vbuff, "831.4") ||
9a3ae26 @ckolivas Add support for latest ATI SDK on windows.
ckolivas authored
393 strstr(vbuff, "898.1") || // 12.2 driver SDK
394 strstr(vbuff, "923.1"))) { // 12.4 driver SDK
dfcb98d @ckolivas Use the SDK and hardware information to choose good performing defaul…
ckolivas authored
395 applog(LOG_INFO, "Selecting diablo kernel");
396 clState->chosen_kernel = KL_DIABLO;
39395eb @ckolivas Use poclbm preferentially on Tahiti now regardless of SDK.
ckolivas authored
397 /* Detect all 7970s, older ATI and NVIDIA and use poclbm */
398 } else if (strstr(name, "Tahiti") || !clState->hasBitAlign) {
dfcb98d @ckolivas Use the SDK and hardware information to choose good performing defaul…
ckolivas authored
399 applog(LOG_INFO, "Selecting poclbm kernel");
400 clState->chosen_kernel = KL_POCLBM;
39395eb @ckolivas Use poclbm preferentially on Tahiti now regardless of SDK.
ckolivas authored
401 /* Use phatk for the rest R5xxx R6xxx */
402 } else {
403 applog(LOG_INFO, "Selecting phatk kernel");
404 clState->chosen_kernel = KL_PHATK;
67c4ada @ckolivas Provide warning on each startup about sdk 2.6 and decrease poclbm ker…
ckolivas authored
405 }
994cd77 @ckolivas Allow writing of multiple kernels to the configuration file.
ckolivas authored
406 gpus[gpu].kernel = clState->chosen_kernel;
bb31988 @ckolivas Detect poorly performing combination of SDK and phatk kernel and add …
ckolivas authored
407 } else {
93efb72 @ckolivas Allow multiple different kernels to be chosen per device.
ckolivas authored
408 clState->chosen_kernel = gpus[gpu].kernel;
bb31988 @ckolivas Detect poorly performing combination of SDK and phatk kernel and add …
ckolivas authored
409 if (clState->chosen_kernel == KL_PHATK &&
410 (strstr(vbuff, "844.4") || strstr(vbuff, "851.4") ||
9a3ae26 @ckolivas Add support for latest ATI SDK on windows.
ckolivas authored
411 strstr(vbuff, "831.4") || strstr(vbuff, "898.1") ||
412 strstr(vbuff, "923.1"))) {
bb31988 @ckolivas Detect poorly performing combination of SDK and phatk kernel and add …
ckolivas authored
413 applog(LOG_WARNING, "WARNING: You have selected the phatk kernel.");
414 applog(LOG_WARNING, "You are running SDK 2.6 which performs poorly with this kernel.");
415 applog(LOG_WARNING, "Downgrade your SDK and delete any .bin files before starting again.");
416 applog(LOG_WARNING, "Or allow cgminer to automatically choose a more suitable kernel.");
417 }
418 }
a9e1a25 @ckolivas Make it possible to select the choice of kernel on the command line.
ckolivas authored
419
709c4cd @ckolivas Use diablo kernel on all future SDKs for Tahiti and set preferred vec…
ckolivas authored
420 /* For some reason 2 vectors is still better even if the card says
421 * otherwise, and many cards lie about their max so use 256 as max
6274fbe @ckolivas Change the preferred vector width to 1 for Tahiti only, not all poclb…
ckolivas authored
422 * unless explicitly set on the command line. Tahiti prefers 1 */
423 if (strstr(name, "Tahiti"))
424 preferred_vwidth = 1;
425 else if (preferred_vwidth > 2)
709c4cd @ckolivas Use diablo kernel on all future SDKs for Tahiti and set preferred vec…
ckolivas authored
426 preferred_vwidth = 2;
427
95a989d @ckolivas Conflicting entries of cl_kernel may have been causing problems, and …
ckolivas authored
428 switch (clState->chosen_kernel) {
a9e1a25 @ckolivas Make it possible to select the choice of kernel on the command line.
ckolivas authored
429 case KL_POCLBM:
cb7145b @ckolivas Add basic build ability with diakgcn and put all kernel names in conf…
ckolivas authored
430 strcpy(filename, POCLBM_KERNNAME".cl");
431 strcpy(binaryfilename, POCLBM_KERNNAME);
a9e1a25 @ckolivas Make it possible to select the choice of kernel on the command line.
ckolivas authored
432 break;
433 case KL_PHATK:
cb7145b @ckolivas Add basic build ability with diakgcn and put all kernel names in conf…
ckolivas authored
434 strcpy(filename, PHATK_KERNNAME".cl");
435 strcpy(binaryfilename, PHATK_KERNNAME);
a9e1a25 @ckolivas Make it possible to select the choice of kernel on the command line.
ckolivas authored
436 break;
f2d5db0 @ckolivas Use only working kernels by default.
ckolivas authored
437 case KL_DIAKGCN:
438 strcpy(filename, DIAKGCN_KERNNAME".cl");
439 strcpy(binaryfilename, DIAKGCN_KERNNAME);
440 break;
dd740ca @ckolivas Provide initial support for the scrypt kernel to compile with and min…
ckolivas authored
441 case KL_SCRYPT:
442 strcpy(filename, SCRYPT_KERNNAME".cl");
443 strcpy(binaryfilename, SCRYPT_KERNNAME);
428d5e5 @ckolivas Limit scrypt to 1 vector.
ckolivas authored
444 /* Scrypt only supports vector 1 */
445 gpus[gpu].vwidth = 1;
dd740ca @ckolivas Provide initial support for the scrypt kernel to compile with and min…
ckolivas authored
446 break;
00290a3 @ckolivas Select diablo kernel on all but GCN+SDK 2.6.
ckolivas authored
447 case KL_NONE: /* Shouldn't happen */
f2d5db0 @ckolivas Use only working kernels by default.
ckolivas authored
448 case KL_DIABLO:
449 strcpy(filename, DIABLO_KERNNAME".cl");
450 strcpy(binaryfilename, DIABLO_KERNNAME);
451 break;
a9e1a25 @ckolivas Make it possible to select the choice of kernel on the command line.
ckolivas authored
452 }
453
709c4cd @ckolivas Use diablo kernel on all future SDKs for Tahiti and set preferred vec…
ckolivas authored
454 if (gpus[gpu].vwidth)
455 clState->vwidth = gpus[gpu].vwidth;
456 else {
457 clState->vwidth = preferred_vwidth;
458 gpus[gpu].vwidth = preferred_vwidth;
459 }
460
aabc723 @ckolivas Make sure goffset is set for scrypt and drop padbuffer8 to something …
ckolivas authored
461 if (((clState->chosen_kernel == KL_POCLBM || clState->chosen_kernel == KL_DIABLO || clState->chosen_kernel == KL_DIAKGCN) &&
462 clState->vwidth == 1 && clState->hasOpenCL11plus) || opt_scrypt)
621bcca @ckolivas Use global offset parameter to diablo and poclbm kernel ONLY for 1 ve…
ckolivas authored
463 clState->goffset = true;
464
709c4cd @ckolivas Use diablo kernel on all future SDKs for Tahiti and set preferred vec…
ckolivas authored
465 if (gpus[gpu].work_size && gpus[gpu].work_size <= clState->max_work_size)
466 clState->wsize = gpus[gpu].work_size;
467 else if (strstr(name, "Tahiti"))
468 clState->wsize = 64;
469 else
470 clState->wsize = (clState->max_work_size <= 256 ? clState->max_work_size : 256) / clState->vwidth;
471 gpus[gpu].work_size = clState->wsize;
472
39f7d2f @ckolivas Allow lookup gap and thread concurrency to be passed per device and s…
ckolivas authored
473 #ifdef USE_SCRYPT
474 if (opt_scrypt) {
475 if (!gpus[gpu].lookup_gap)
476 gpus[gpu].lookup_gap = 2;
477 if (!gpus[gpu].thread_concurrency)
478 gpus[gpu].thread_concurrency = 2048;
479 }
480 #endif
481
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
482 FILE *binaryfile;
483 size_t *binary_sizes;
484 char **binaries;
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
485 int pl;
3567b69 @ckolivas Remove fragile source patching for bitalign, vectors et. al and simpl…
ckolivas authored
486 char *source = file_contents(filename, &pl);
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
487 size_t sourceSize[] = {(size_t)pl};
fb99c8d @ckolivas The longstanding generation of a zero sized binary appears to be due …
ckolivas authored
488 cl_uint slot, cpnd;
489
490 slot = cpnd = 0;
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
491
3567b69 @ckolivas Remove fragile source patching for bitalign, vectors et. al and simpl…
ckolivas authored
492 if (!source)
48180b6 @ckolivas Fail gracefully if unable to open the opencl files.
ckolivas authored
493 return NULL;
494
fb99c8d @ckolivas The longstanding generation of a zero sized binary appears to be due …
ckolivas authored
495 binary_sizes = calloc(sizeof(size_t) * MAX_GPUDEVICES * 4, 1);
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
496 if (unlikely(!binary_sizes)) {
fb99c8d @ckolivas The longstanding generation of a zero sized binary appears to be due …
ckolivas authored
497 applog(LOG_ERR, "Unable to calloc binary_sizes");
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
498 return NULL;
499 }
fb99c8d @ckolivas The longstanding generation of a zero sized binary appears to be due …
ckolivas authored
500 binaries = calloc(sizeof(char *) * MAX_GPUDEVICES * 4, 1);
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
501 if (unlikely(!binaries)) {
fb99c8d @ckolivas The longstanding generation of a zero sized binary appears to be due …
ckolivas authored
502 applog(LOG_ERR, "Unable to calloc binaries");
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
503 return NULL;
504 }
505
bf3a9f9 @ckolivas Unintentionally dropped the device name from the binary filenames. Re…
ckolivas authored
506 strcat(binaryfilename, name);
621bcca @ckolivas Use global offset parameter to diablo and poclbm kernel ONLY for 1 ve…
ckolivas authored
507 if (clState->goffset)
508 strcat(binaryfilename, "g");
39f7d2f @ckolivas Allow lookup gap and thread concurrency to be passed per device and s…
ckolivas authored
509 if (opt_scrypt) {
510 sprintf(numbuf, "lg%dtc%d", gpus[gpu].lookup_gap, gpus[gpu].thread_concurrency);
511 strcat(binaryfilename, numbuf);
512 } else {
513 sprintf(numbuf, "v%d", clState->vwidth);
514 strcat(binaryfilename, numbuf);
515 }
516 sprintf(numbuf, "w%d", (int)clState->wsize);
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
517 strcat(binaryfilename, numbuf);
39f7d2f @ckolivas Allow lookup gap and thread concurrency to be passed per device and s…
ckolivas authored
518 sprintf(numbuf, "l%d", (int)sizeof(long));
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
519 strcat(binaryfilename, numbuf);
520 strcat(binaryfilename, ".bin");
521
52d6e7c @ycros Fixed fread issues under Windows.
ycros authored
522 binaryfile = fopen(binaryfilename, "rb");
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
523 if (!binaryfile) {
405a212 @ckolivas Remove unnecessary check for opt_debug on every invocation of applog …
ckolivas authored
524 applog(LOG_DEBUG, "No binary found, generating from source");
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
525 } else {
526 struct stat binary_stat;
527
528 if (unlikely(stat(binaryfilename, &binary_stat))) {
405a212 @ckolivas Remove unnecessary check for opt_debug on every invocation of applog …
ckolivas authored
529 applog(LOG_DEBUG, "Unable to stat binary, generating from source");
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
530 fclose(binaryfile);
531 goto build;
532 }
0719d40 @ckolivas Clean up on failure to load a binary kernel.
ckolivas authored
533 if (!binary_stat.st_size)
534 goto build;
535
fb99c8d @ckolivas The longstanding generation of a zero sized binary appears to be due …
ckolivas authored
536 binary_sizes[slot] = binary_stat.st_size;
537 binaries[slot] = (char *)calloc(binary_sizes[slot], 1);
538 if (unlikely(!binaries[slot])) {
539 applog(LOG_ERR, "Unable to calloc binaries");
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
540 fclose(binaryfile);
541 return NULL;
542 }
543
fb99c8d @ckolivas The longstanding generation of a zero sized binary appears to be due …
ckolivas authored
544 if (fread(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot]) {
545 applog(LOG_ERR, "Unable to fread binaries");
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
546 fclose(binaryfile);
fb99c8d @ckolivas The longstanding generation of a zero sized binary appears to be due …
ckolivas authored
547 free(binaries[slot]);
5d301c8 @ycros Make a binary load failure build from source.
ycros authored
548 goto build;
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
549 }
550
fb99c8d @ckolivas The longstanding generation of a zero sized binary appears to be due …
ckolivas authored
551 clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)binaries, &status, NULL);
fb0c580 @ckolivas Go to kernel build should we fail to clCreateProgramWithBinary instea…
ckolivas authored
552 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
553 applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
fb0c580 @ckolivas Go to kernel build should we fail to clCreateProgramWithBinary instea…
ckolivas authored
554 fclose(binaryfile);
fb99c8d @ckolivas The longstanding generation of a zero sized binary appears to be due …
ckolivas authored
555 free(binaries[slot]);
fb0c580 @ckolivas Go to kernel build should we fail to clCreateProgramWithBinary instea…
ckolivas authored
556 goto build;
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
557 }
60c7014 @ckolivas Retain cl program after successfully loading a binary image.
ckolivas authored
558
fb0c580 @ckolivas Go to kernel build should we fail to clCreateProgramWithBinary instea…
ckolivas authored
559 fclose(binaryfile);
405a212 @ckolivas Remove unnecessary check for opt_debug on every invocation of applog …
ckolivas authored
560 applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
52d6e7c @ycros Fixed fread issues under Windows.
ycros authored
561
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
562 goto built;
563 }
564
565 /////////////////////////////////////////////////////////////////
566 // Load CL file, build CL program object, create CL kernel object
567 /////////////////////////////////////////////////////////////////
568
569 build:
3567b69 @ckolivas Remove fragile source patching for bitalign, vectors et. al and simpl…
ckolivas authored
570 clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
571 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
572 applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithSource)", status);
3567b69 @ckolivas Remove fragile source patching for bitalign, vectors et. al and simpl…
ckolivas authored
573 return NULL;
574 }
19eea90 @ckolivas Implement code detecting max work size and optimal vector width.
ckolivas authored
575
3567b69 @ckolivas Remove fragile source patching for bitalign, vectors et. al and simpl…
ckolivas authored
576 /* create a cl program executable for all the devices specified */
77e9b1c @ckolivas Use calloced stack memory for CompilerOptions to ensure sprintf write…
ckolivas authored
577 char *CompilerOptions = calloc(1, 256);
623b9b9 @ckolivas Patch bitalign separately from bfi_int.
ckolivas authored
578
243d005 @ckolivas Set scrypt settings and buffer size in ocl.c code to be future modifi…
ckolivas authored
579 #ifdef USE_SCRYPT
580 if (opt_scrypt)
581 sprintf(CompilerOptions, "-D LOOKUP_GAP=%d -D CONCURRENT_THREADS=%d -D WORKSIZE=%d",
39f7d2f @ckolivas Allow lookup gap and thread concurrency to be passed per device and s…
ckolivas authored
582 gpus[gpu].lookup_gap, gpus[gpu].thread_concurrency, (int)clState->wsize);
243d005 @ckolivas Set scrypt settings and buffer size in ocl.c code to be future modifi…
ckolivas authored
583 else
584 #endif
585 {
dd740ca @ckolivas Provide initial support for the scrypt kernel to compile with and min…
ckolivas authored
586 sprintf(CompilerOptions, "-D WORKSIZE=%d -D VECTORS%d -D WORKVEC=%d",
587 (int)clState->wsize, clState->vwidth, (int)clState->wsize * clState->vwidth);
588 }
a54f760 @ckolivas Fix multiple work size entry.
ckolivas authored
589 applog(LOG_DEBUG, "Setting worksize to %d", clState->wsize);
deff55c @ckolivas Allow different vectors to be set per device.
ckolivas authored
590 if (clState->vwidth > 1)
591 applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->vwidth);
3567b69 @ckolivas Remove fragile source patching for bitalign, vectors et. al and simpl…
ckolivas authored
592
593 if (clState->hasBitAlign) {
2ecabd8 @ckolivas Some opencl compilers have issues with no spaces after -D in the comp…
ckolivas authored
594 strcat(CompilerOptions, " -D BITALIGN");
405a212 @ckolivas Remove unnecessary check for opt_debug on every invocation of applog …
ckolivas authored
595 applog(LOG_DEBUG, "cl_amd_media_ops found, setting BITALIGN");
3d4cfce @ckolivas Instead of using the BFI_INT patching hack on any device reporting cl…
ckolivas authored
596 if (strstr(name, "Cedar") ||
597 strstr(name, "Redwood") ||
598 strstr(name, "Juniper") ||
599 strstr(name, "Cypress" ) ||
600 strstr(name, "Hemlock" ) ||
601 strstr(name, "Caicos" ) ||
602 strstr(name, "Turks" ) ||
603 strstr(name, "Barts" ) ||
604 strstr(name, "Cayman" ) ||
605 strstr(name, "Antilles" ) ||
606 strstr(name, "Wrestler" ) ||
607 strstr(name, "Zacate" ) ||
608 strstr(name, "WinterPark" ) ||
609 strstr(name, "BeaverCreek" ))
610 patchbfi = true;
405a212 @ckolivas Remove unnecessary check for opt_debug on every invocation of applog …
ckolivas authored
611 } else
3d4cfce @ckolivas Instead of using the BFI_INT patching hack on any device reporting cl…
ckolivas authored
612 applog(LOG_DEBUG, "cl_amd_media_ops not found, will not set BITALIGN");
623b9b9 @ckolivas Patch bitalign separately from bfi_int.
ckolivas authored
613
6374e0f @ckolivas Import the phatk kernel. Enable it only for hardware with amd media o…
ckolivas authored
614 if (patchbfi) {
2ecabd8 @ckolivas Some opencl compilers have issues with no spaces after -D in the comp…
ckolivas authored
615 strcat(CompilerOptions, " -D BFI_INT");
405a212 @ckolivas Remove unnecessary check for opt_debug on every invocation of applog …
ckolivas authored
616 applog(LOG_DEBUG, "BFI_INT patch requiring device found, patched source with BFI_INT");
617 } else
3d4cfce @ckolivas Instead of using the BFI_INT patching hack on any device reporting cl…
ckolivas authored
618 applog(LOG_DEBUG, "BFI_INT patch requiring device not found, will not BFI_INT patch");
26546ad @ckolivas Make the optimisations per-gpu card and update code to work properly …
ckolivas authored
619
621bcca @ckolivas Use global offset parameter to diablo and poclbm kernel ONLY for 1 ve…
ckolivas authored
620 if (clState->goffset)
621 strcat(CompilerOptions, " -D GOFFSET");
622
405a212 @ckolivas Remove unnecessary check for opt_debug on every invocation of applog …
ckolivas authored
623 applog(LOG_DEBUG, "CompilerOptions: %s", CompilerOptions);
d15d225 @Phateus Changed phatk version to 2.2
Phateus authored
624 status = clBuildProgram(clState->program, 1, &devices[gpu], CompilerOptions , NULL, NULL);
77e9b1c @ckolivas Use calloced stack memory for CompilerOptions to ensure sprintf write…
ckolivas authored
625 free(CompilerOptions);
d15d225 @Phateus Changed phatk version to 2.2
Phateus authored
626
082e20d @ckolivas Explicitly tell the compiler to retain the program to minimise the ch…
ckolivas authored
627 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
628 applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
629 size_t logSize;
630 status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
631
632 char *log = malloc(logSize);
633 status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
634 applog(LOG_ERR, "%s", log);
52d6e7c @ycros Fixed fread issues under Windows.
ycros authored
635 return NULL;
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
636 }
637
d18d556 @ckolivas Do not attempt to build the program that becomes the kernel twice. Th…
ckolivas authored
638 prog_built = true;
639
fb99c8d @ckolivas The longstanding generation of a zero sized binary appears to be due …
ckolivas authored
640 status = clGetProgramInfo(clState->program, CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint), &cpnd, NULL);
082e20d @ckolivas Explicitly tell the compiler to retain the program to minimise the ch…
ckolivas authored
641 if (unlikely(status != CL_SUCCESS)) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
642 applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_NUM_DEVICES. (clGetProgramInfo)", status);
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
643 return NULL;
644 }
645
fb99c8d @ckolivas The longstanding generation of a zero sized binary appears to be due …
ckolivas authored
646 status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*cpnd, binary_sizes, NULL);
647 if (unlikely(status != CL_SUCCESS)) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
648 applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetProgramInfo)", status);
fb99c8d @ckolivas The longstanding generation of a zero sized binary appears to be due …
ckolivas authored
649 return NULL;
650 }
651
652 /* The actual compiled binary ends up in a RANDOM slot! Grr, so we have
653 * to iterate over all the binary slots and find where the real program
654 * is. What the heck is this!? */
655 for (slot = 0; slot < cpnd; slot++)
656 if (binary_sizes[slot])
657 break;
658
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
659 /* copy over all of the generated binaries. */
fb99c8d @ckolivas The longstanding generation of a zero sized binary appears to be due …
ckolivas authored
660 applog(LOG_DEBUG, "Binary size for gpu %d found in binary slot %d: %d", gpu, slot, binary_sizes[slot]);
661 if (!binary_sizes[slot]) {
b2b5083 @ckolivas Microoptimise phatk kernel on return code.
ckolivas authored
662 applog(LOG_ERR, "OpenCL compiler generated a zero sized binary, FAIL!");
1c67f60 @ckolivas Sometimes the cl compiler generates zero sized binaries and only a re…
ckolivas authored
663 return NULL;
664 }
fb99c8d @ckolivas The longstanding generation of a zero sized binary appears to be due …
ckolivas authored
665 binaries[slot] = calloc(sizeof(char) * binary_sizes[slot], 1);
666 status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARIES, sizeof(char *) * cpnd, binaries, NULL );
082e20d @ckolivas Explicitly tell the compiler to retain the program to minimise the ch…
ckolivas authored
667 if (unlikely(status != CL_SUCCESS)) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
668 applog(LOG_ERR, "Error %d: Getting program info. CL_PROGRAM_BINARIES (clGetProgramInfo)", status);
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
669 return NULL;
670 }
671
3d4cfce @ckolivas Instead of using the BFI_INT patching hack on any device reporting cl…
ckolivas authored
672 /* Patch the kernel if the hardware supports BFI_INT but it needs to
673 * be hacked in */
6374e0f @ckolivas Import the phatk kernel. Enable it only for hardware with amd media o…
ckolivas authored
674 if (patchbfi) {
fb99c8d @ckolivas The longstanding generation of a zero sized binary appears to be due …
ckolivas authored
675 unsigned remaining = binary_sizes[slot];
676 char *w = binaries[slot];
401586f @ckolivas Only try to patch GPU referenced.
ckolivas authored
677 unsigned int start, length;
678
679 /* Find 2nd incidence of .text, and copy the program's
680 * position and length at a fixed offset from that. Then go
681 * back and find the 2nd incidence of \x7ELF (rewind by one
682 * from ELF) and then patch the opcocdes */
623b9b9 @ckolivas Patch bitalign separately from bfi_int.
ckolivas authored
683 if (!advance(&w, &remaining, ".text"))
55bd031 @ckolivas Variable unused after this so remove setting it.
ckolivas authored
684 goto build;
401586f @ckolivas Only try to patch GPU referenced.
ckolivas authored
685 w++; remaining--;
11c8818 @ckolivas 32 bit only builds one elf, not an elf in an elf, so account for it t…
ckolivas authored
686 if (!advance(&w, &remaining, ".text")) {
687 /* 32 bit builds only one ELF */
688 w--; remaining++;
689 }
401586f @ckolivas Only try to patch GPU referenced.
ckolivas authored
690 memcpy(&start, w + 285, 4);
691 memcpy(&length, w + 289, 4);
fb99c8d @ckolivas The longstanding generation of a zero sized binary appears to be due …
ckolivas authored
692 w = binaries[slot]; remaining = binary_sizes[slot];
623b9b9 @ckolivas Patch bitalign separately from bfi_int.
ckolivas authored
693 if (!advance(&w, &remaining, "ELF"))
55bd031 @ckolivas Variable unused after this so remove setting it.
ckolivas authored
694 goto build;
401586f @ckolivas Only try to patch GPU referenced.
ckolivas authored
695 w++; remaining--;
11c8818 @ckolivas 32 bit only builds one elf, not an elf in an elf, so account for it t…
ckolivas authored
696 if (!advance(&w, &remaining, "ELF")) {
697 /* 32 bit builds only one ELF */
698 w--; remaining++;
699 }
401586f @ckolivas Only try to patch GPU referenced.
ckolivas authored
700 w--; remaining++;
701 w += start; remaining -= start;
405a212 @ckolivas Remove unnecessary check for opt_debug on every invocation of applog …
ckolivas authored
702 applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
703 w, remaining);
401586f @ckolivas Only try to patch GPU referenced.
ckolivas authored
704 patch_opcodes(w, length);
705
c2e4165 @ckolivas Detect platforms that support cl_amd_media_ops and decide whether to …
ckolivas authored
706 status = clReleaseProgram(clState->program);
082e20d @ckolivas Explicitly tell the compiler to retain the program to minimise the ch…
ckolivas authored
707 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
708 applog(LOG_ERR, "Error %d: Releasing program. (clReleaseProgram)", status);
c2e4165 @ckolivas Detect platforms that support cl_amd_media_ops and decide whether to …
ckolivas authored
709 return NULL;
710 }
c548dea @ckolivas First BFI_INT patch changes.
ckolivas authored
711
fb99c8d @ckolivas The longstanding generation of a zero sized binary appears to be due …
ckolivas authored
712 clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)&binaries[slot], &status, NULL);
082e20d @ckolivas Explicitly tell the compiler to retain the program to minimise the ch…
ckolivas authored
713 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
714 applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
c2e4165 @ckolivas Detect platforms that support cl_amd_media_ops and decide whether to …
ckolivas authored
715 return NULL;
716 }
25caca9 @ckolivas Revert "Don't explicitly retain the cl program as it is of no benefit…
ckolivas authored
717
31f6e8c @ckolivas Unset prog_built after it is patched because it needs rebuilding.
ckolivas authored
718 /* Program needs to be rebuilt */
719 prog_built = false;
c548dea @ckolivas First BFI_INT patch changes.
ckolivas authored
720 }
721
623b9b9 @ckolivas Patch bitalign separately from bfi_int.
ckolivas authored
722 free(source);
723
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
724 /* Save the binary to be loaded next time */
52d6e7c @ycros Fixed fread issues under Windows.
ycros authored
725 binaryfile = fopen(binaryfilename, "wb");
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
726 if (!binaryfile) {
727 /* Not a fatal problem, just means we build it again next time */
405a212 @ckolivas Remove unnecessary check for opt_debug on every invocation of applog …
ckolivas authored
728 applog(LOG_DEBUG, "Unable to create file %s", binaryfilename);
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
729 } else {
fb99c8d @ckolivas The longstanding generation of a zero sized binary appears to be due …
ckolivas authored
730 if (unlikely(fwrite(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot])) {
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
731 applog(LOG_ERR, "Unable to fwrite to binaryfile");
732 return NULL;
733 }
734 fclose(binaryfile);
735 }
60c7014 @ckolivas Retain cl program after successfully loading a binary image.
ckolivas authored
736 built:
fb99c8d @ckolivas The longstanding generation of a zero sized binary appears to be due …
ckolivas authored
737 if (binaries[slot])
738 free(binaries[slot]);
3aa5be4 @ckolivas Reinstate binary kernel loading with fixes.
ckolivas authored
739 free(binaries);
740 free(binary_sizes);
741
1c1b8be @ckolivas BFI INT patching is not necessarily true on binary loading of files a…
ckolivas authored
742 applog(LOG_INFO, "Initialising kernel %s with%s bitalign, %d vectors and worksize %d",
a54f760 @ckolivas Fix multiple work size entry.
ckolivas authored
743 filename, clState->hasBitAlign ? "" : "out", clState->vwidth, clState->wsize);
623b9b9 @ckolivas Patch bitalign separately from bfi_int.
ckolivas authored
744
d18d556 @ckolivas Do not attempt to build the program that becomes the kernel twice. Th…
ckolivas authored
745 if (!prog_built) {
746 /* create a cl program executable for all the devices specified */
747 status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
748 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
749 applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
d18d556 @ckolivas Do not attempt to build the program that becomes the kernel twice. Th…
ckolivas authored
750 size_t logSize;
751 status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
42d49ff @ckolivas Revert "Restart threads by abstracting out the clcontext initialisati…
ckolivas authored
752
d18d556 @ckolivas Do not attempt to build the program that becomes the kernel twice. Th…
ckolivas authored
753 char *log = malloc(logSize);
754 status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
755 applog(LOG_ERR, "%s", log);
d18d556 @ckolivas Do not attempt to build the program that becomes the kernel twice. Th…
ckolivas authored
756 return NULL;
757 }
42d49ff @ckolivas Revert "Restart threads by abstracting out the clcontext initialisati…
ckolivas authored
758 }
759
760 /* get a kernel object handle for a kernel with the given name */
761 clState->kernel = clCreateKernel(clState->program, "search", &status);
082e20d @ckolivas Explicitly tell the compiler to retain the program to minimise the ch…
ckolivas authored
762 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
763 applog(LOG_ERR, "Error %d: Creating Kernel from program. (clCreateKernel)", status);
42d49ff @ckolivas Revert "Restart threads by abstracting out the clcontext initialisati…
ckolivas authored
764 return NULL;
765 }
766
b085c33 @ckolivas Make scrypt buffers and midstate compatible with cgminer.
ckolivas authored
767 #ifdef USE_SCRYPT
e0296c4 @ckolivas Set up buffer8 for scrypt.
ckolivas authored
768 if (opt_scrypt) {
39f7d2f @ckolivas Allow lookup gap and thread concurrency to be passed per device and s…
ckolivas authored
769 size_t ipt = (1024 / gpus[gpu].lookup_gap + (1024 % gpus[gpu].lookup_gap > 0));
770 size_t bufsize = 128 * ipt * gpus[gpu].thread_concurrency;
243d005 @ckolivas Set scrypt settings and buffer size in ocl.c code to be future modifi…
ckolivas authored
771
5087ff9 @ckolivas Add debugging output if buffer allocation fails for scrypt and round …
ckolivas authored
772 if (bufsize % 256)
773 bufsize += (256 - bufsize % 256);
774 applog(LOG_DEBUG, "Creating scrypt buffer sized %d", bufsize);
a9a0bba @ckolivas Set the correct data for cldata and prepare for pad8 fixes.
ckolivas authored
775 clState->padbufsize = bufsize;
5087ff9 @ckolivas Add debugging output if buffer allocation fails for scrypt and round …
ckolivas authored
776 clState->padbuffer8 = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, bufsize, NULL, &status);
777 if (status != CL_SUCCESS) {
778 applog(LOG_ERR, "Error %d: clCreateBuffer (padbuffer8), decrease CT or increase LG", status);
779 return NULL;
780 }
781
782 clState->CLbuffer0 = clCreateBuffer(clState->context, CL_MEM_READ_ONLY, 128, NULL, &status);
783 if (status != CL_SUCCESS) {
784 applog(LOG_ERR, "Error %d: clCreateBuffer (CLbuffer0)", status);
785 return NULL;
786 }
e0296c4 @ckolivas Set up buffer8 for scrypt.
ckolivas authored
787 }
b085c33 @ckolivas Make scrypt buffers and midstate compatible with cgminer.
ckolivas authored
788 #endif
23c01bc @ckolivas Make output buffer write only as per Diapolo's suggestion.
ckolivas authored
789 clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, BUFFERSIZE, NULL, &status);
42d49ff @ckolivas Revert "Restart threads by abstracting out the clcontext initialisati…
ckolivas authored
790 if (status != CL_SUCCESS) {
22d3034 @ckolivas Show error code on any opencl failure status.
ckolivas authored
791 applog(LOG_ERR, "Error %d: clCreateBuffer (outputBuffer)", status);
42d49ff @ckolivas Revert "Restart threads by abstracting out the clcontext initialisati…
ckolivas authored
792 return NULL;
793 }
794
795 return clState;
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
796 }
a93b22c @ckolivas Make it possible to build without GPU mining by picking up HAVE_OPENC…
ckolivas authored
797 #endif /* HAVE_OPENCL */
dde7039 @ckolivas Merge gpumining from oclmine. Unstable.
ckolivas authored
798
Something went wrong with that request. Please try again.