forked from lenis0012/sgminer-gm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild_kernel.c
190 lines (161 loc) · 5.85 KB
/
build_kernel.c
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
#include <stdio.h>
#include "build_kernel.h"
#include "miner.h"
static char *file_contents(const char *filename, int *length)
{
char *fullpath = (char *)alloca(PATH_MAX);
void *buffer;
FILE *f = NULL;
if (opt_kernel_path && *opt_kernel_path) {
/* Try in the optional kernel path first, defaults to PREFIX */
snprintf(fullpath, PATH_MAX, "%s/%s", opt_kernel_path, filename);
applog(LOG_DEBUG, "Trying to open %s...", fullpath);
f = fopen(fullpath, "rb");
}
if (!f) {
/* Then try from the path sgminer was called */
snprintf(fullpath, PATH_MAX, "%s/%s", sgminer_path, filename);
applog(LOG_DEBUG, "Trying to open %s...", fullpath);
f = fopen(fullpath, "rb");
}
if (!f) {
/* Then from `pwd`/kernel/ */
snprintf(fullpath, PATH_MAX, "%s/kernel/%s", sgminer_path, filename);
applog(LOG_DEBUG, "Trying to open %s...", fullpath);
f = fopen(fullpath, "rb");
}
/* Finally try opening it directly */
if (!f) {
applog(LOG_DEBUG, "Trying to open %s...", fullpath);
f = fopen(filename, "rb");
}
if (!f) {
applog(LOG_ERR, "Unable to open %s for reading!", filename);
return NULL;
}
applog(LOG_DEBUG, "Using %s", fullpath);
fseek(f, 0, SEEK_END);
*length = ftell(f);
fseek(f, 0, SEEK_SET);
buffer = malloc(*length+1);
*length = fread(buffer, 1, *length, f);
fclose(f);
((char*)buffer)[*length] = '\0';
return (char*)buffer;
}
// This should NOT be in here! -- Wolf9466
void set_base_compiler_options(build_kernel_data *data)
{
char buf[255];
sprintf(data->compiler_options, "-I \"%s\" -I \"%s/kernel\" -I \".\" -D WORKSIZE=%d",
data->sgminer_path, data->sgminer_path, (int)data->work_size);
applog(LOG_DEBUG, "Setting worksize to %d", (int)(data->work_size));
sprintf(buf, "w%dl%d", (int)data->work_size, (int)sizeof(long));
strcat(data->binary_filename, buf);
if (data->kernel_path) {
strcat(data->compiler_options, " -I \"");
strcat(data->compiler_options, data->kernel_path);
strcat(data->compiler_options, "\"");
}
if (data->opencl_version < 1.1)
strcat(data->compiler_options, " -D OCL1");
}
cl_program build_opencl_kernel(build_kernel_data *data, const char *filename)
{
int pl;
char *source = file_contents(data->source_filename, &pl);
size_t sourceSize[] = {(size_t)pl};
cl_int status;
cl_program program = NULL;
cl_program ret = NULL;
if (!source)
goto out;
program = clCreateProgramWithSource(data->context, 1, (const char **)&source, sourceSize, &status);
if (status != CL_SUCCESS) {
applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithSource)", status);
goto out;
}
applog(LOG_DEBUG, "CompilerOptions: %s", data->compiler_options);
status = clBuildProgram(program, 1, data->device, data->compiler_options, NULL, NULL);
if (status != CL_SUCCESS) {
size_t log_size;
applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
status = clGetProgramBuildInfo(program, *data->device, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
char *sz_log = (char *)malloc(log_size + 1);
status = clGetProgramBuildInfo(program, *data->device, CL_PROGRAM_BUILD_LOG, log_size, sz_log, NULL);
sz_log[log_size] = '\0';
applogsiz(LOG_ERR, log_size, "%s", sz_log);
free(sz_log);
goto out;
}
ret = program;
out:
if (source) free(source);
return ret;
}
bool save_opencl_kernel(build_kernel_data *data, cl_program program)
{
cl_uint slot, cpnd = 0;
size_t *binary_sizes = (size_t *)calloc(MAX_GPUDEVICES * 4, sizeof(size_t));
char **binaries = NULL;
cl_int status;
FILE *binaryfile;
bool ret = false;
#ifdef __APPLE__
/* OSX OpenCL breaks reading off binaries with >1 GPU so always build
* from source. */
goto out;
#endif
status = clGetProgramInfo(program, CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint), &cpnd, NULL);
if (unlikely(status != CL_SUCCESS)) {
applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_NUM_DEVICES. (clGetProgramInfo)", status);
goto out;
}
status = clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*cpnd, binary_sizes, NULL);
if (unlikely(status != CL_SUCCESS)) {
applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetProgramInfo)", status);
goto out;
}
binaries = (char **)calloc(MAX_GPUDEVICES * 4, sizeof(char *));
for (slot = 0; slot < cpnd; slot++)
if (binary_sizes[slot])
binaries[slot] = (char *)calloc(binary_sizes[slot], 1);
status = clGetProgramInfo(program, CL_PROGRAM_BINARIES, sizeof(char *) * cpnd, binaries, NULL );
if (unlikely(status != CL_SUCCESS)) {
applog(LOG_ERR, "Error %d: Getting program info. CL_PROGRAM_BINARIES (clGetProgramInfo)", status);
goto out;
}
/* The actual compiled binary ends up in a RANDOM slot! Grr, so we have
* to iterate over all the binary slots and find where the real program
* is. What the heck is this!? */
for (slot = 0; slot < cpnd; slot++)
if (binary_sizes[slot])
break;
/* copy over all of the generated binaries. */
applog(LOG_DEBUG, "Binary size found in binary slot %d: %d", slot, (int)(binary_sizes[slot]));
if (!binary_sizes[slot]) {
applog(LOG_ERR, "OpenCL compiler generated a zero sized binary!");
goto out;
}
/* Save the binary to be loaded next time */
binaryfile = fopen(data->binary_filename, "wb");
if (!binaryfile) {
/* Not fatal, just means we build it again next time */
applog(LOG_DEBUG, "Unable to create file %s", data->binary_filename);
goto out;
} else {
if (unlikely(fwrite(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot])) {
applog(LOG_ERR, "Unable to fwrite to binaryfile");
goto out;
}
fclose(binaryfile);
}
ret = true;
out:
for (slot = 0; slot < cpnd; slot++)
if (binary_sizes[slot])
free(binaries[slot]);
if (binaries) free(binaries);
free(binary_sizes);
return ret;
}