/opencl/tools

To get this branch, use:
bzr branch http://darksoft.org/webbzr/opencl/tools
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <CL/cl.h>

#define fail(code, reason) {\
    fprintf(stderr, "%s\n", reason); \
    exit (code); \
}

typedef struct  {
    cl_uint num_devices;
    cl_device_id *devices;
} device_info_t;

static inline cl_program load_app(cl_context ctx, cl_uint num_devices, cl_device_id *devices, const char *name) {
    FILE *f;
    size_t len;
    char *buf;
    device_info_t info = { num_devices, devices }; 
    
    cl_program app;
    
    f = fopen(name, "rb");
    if (!f) return NULL;

    fseek(f, 0, SEEK_END); 
    len = ftell(f);
    fseek(f, 0, SEEK_SET); 
    
    buf = (char*)malloc(len*sizeof(char));
    if (!buf) return NULL;
    
    if (fread(buf, 1, len, f) != len) {
	free(buf);
	fclose(f);
	return NULL;
    }
    
    fclose(f);
    
    int i;
    printf("Compiling for ");
    for (i=0;i<num_devices;i++) {
	char name[256];
	int err = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(name), name, NULL);
	if (err != CL_SUCCESS) puts("Unknown ");
	else printf("%s ", name);
    }
    printf("\n");
    
    app = clCreateProgramWithSource(ctx, 1, (const char**)&buf, &len, NULL);
    
    
    if (app) {
	size_t size;
	char build_log[4096];
	cl_build_status build_status;

	//"-cl-nv-maxrregcount=48"
	//"-cl-nv-opt-level=<N>" (0 - no optimizations, 3 - default)
	//"-cl-nv-arch sm_XX" - selects the target CUDA Compute Level architecture to compile for (sm_10 for 1.0, sm_11 for 1.1, sm_12 for 1.2, sm_13 for 1.3 and sm_20 for 2.0 (Fermi))
	//"--cl-nv-cstd=CLX.X" - selects the target OpenCL C language version (CL1.0 or CL1.1)
	char *build_flags = "-cl-nv-arch sm_20 -cl-nv-verbose -cl-denorms-are-zero -cl-mad-enable -cl-no-signed-zeros -cl-unsafe-math-optimizations -cl-finite-math-only";
	int err = clBuildProgram(app, num_devices, devices, build_flags, NULL, NULL);
	if (err != CL_SUCCESS) printf("Application build failed (%i)\n", err);
	    
	for (i = 0; i < num_devices; i++) {
	    do {
		err = clGetProgramBuildInfo(app, devices[i], CL_PROGRAM_BUILD_STATUS, sizeof(cl_build_status), &build_status, NULL);
	    } while (build_status == CL_BUILD_IN_PROGRESS);
		
	    err = clGetProgramBuildInfo(app, devices[i], CL_PROGRAM_BUILD_LOG, sizeof(build_log) - 1, &build_log, &size);
	    printf("%i\n", err);
	    if (!err) {
		build_log[size] = 0;
	        if (!err) {
		    if (build_status == CL_BUILD_SUCCESS) {
			if (strlen(build_log) > 2) {
		    	    printf("Build successed for device %i:\n===================\n%s\n--------------------\n\n", i, build_log);
		    	}
		    } else if (build_status == CL_BUILD_ERROR) {
		        printf("Build failed for device %i:\n======================\n%s\n--------------------\n\n", i, build_log);
		    } else {
		        printf("Build failed for device %i\n", i);
		    }
		}
	    }
	}
    } else {
	printf("Program creation failed\n");
    }
        
    free(buf);
    return app;
}

int main(int argc, char *argv[]) {
    int devid = -1;
    
    cl_int err;
    cl_device_id devices[16];
    cl_program app;

    char fname[256];    
    unsigned char *binary[16];
    size_t binary_size[16];
    size_t real_size;
    
    FILE *f;
    
    if (argc < 2) {
	fprintf(stderr, "Usage: %s <source> [device] [binary] [kernel]\n", argv[0]);
	exit(0);	
    }
    
    if (argc > 2) {
	devid = atoi(argv[2]);	
    }

    cl_uint max = 0;
    cl_platform_id selected_platform;
    cl_uint i, num_platforms, num_devices;
    cl_platform_id platforms[4];

    clGetPlatformIDs(4, platforms, &num_platforms);

    for (i = 0; i < num_platforms; i++) {
	err = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_GPU, 0, NULL, &num_devices);
	if ((err == CL_SUCCESS)&&(num_devices > max)) {
	    max = num_devices;
	    selected_platform = platforms[i];
	}
    }
    

    clGetDeviceIDs(selected_platform, CL_DEVICE_TYPE_GPU, 16, devices, &num_devices);
    
    if (devid >= (int)num_devices) fail(-1, "Invalid device number is specified");

    cl_context ctx;
    if (devid < 0)
        ctx = clCreateContext(0, num_devices, devices, NULL, NULL, &err);
    else
	ctx = clCreateContext(0, 1, &devices[devid], NULL, NULL, &err);
    if (!ctx) fail(-1, "Failed to create OpenCL context");

//    cl_context ctx = clCreateContextFromType(NULL, CL_DEVICE_TYPE_GPU, NULL, NULL, NULL);
//    if (!ctx) fail(-1, "Failed to create OpenCL context");

  
//    err = clGetContextInfo(ctx, CL_CONTEXT_DEVICES, 16 * sizeof(cl_device_id), &devices, &num_devices);
//    if (err != CL_SUCCESS) fail(-1, "clGetContextInfo call is failed");

//    num_devices /= sizeof(cl_device_id);

    if (devid < 0)
	app = load_app(ctx, num_devices, devices, argv[1]);
    else
	app = load_app(ctx, 1, &devices[devid], argv[1]);
    if (!app) fail(-1, "Compilation failed");

    if (devid >= 0) num_devices = 1;
    err = clGetProgramInfo(app, CL_PROGRAM_BINARY_SIZES, num_devices * sizeof(size_t), &binary_size, &real_size);
    
    printf("Status: %i, Sizes: ", err);
    for (i = 0; i < num_devices; i++) printf (" %li", binary_size[i]);
    printf("\n");

    if ((err != CL_SUCCESS)||(real_size != num_devices*sizeof(size_t)))
	fail(-1, "Failed to get binary size");
    

    for (i = 0; i < num_devices; i++) {
        binary[i] = malloc(binary_size[i] + 1);
	if (!binary[i]) {
	    for (i--; i>=0; i--) free(binary[i]);
	    fail(-1, "allocation failed");
	}
    }
    
    err = clGetProgramInfo(app, CL_PROGRAM_BINARIES, num_devices * sizeof(unsigned char*), &binary, &real_size);
    if ((err == CL_SUCCESS)&&(real_size == num_devices * sizeof(unsigned char*))) {
	for (i = 0; i < num_devices; i++) {
	    sprintf(fname, "%s.%u", ((argc>3)?argv[3]:"opencl.out"), i);
	    f = fopen(fname, "wb");
	    if (f) {
		fwrite(binary[i], 1, binary_size[i], f);
		fclose(f);

		free(binary[i]);
	    } else {
		for (; i < num_devices; i++)
		    free(binary[i]);
		fail(-1, "Failed to create output file");
	    }
	}
    } else {
	for (i = 0; i < num_devices; i++)
	    free(binary[i]);
	fail(-1, "Binary readout failed")
    }

    if (argc > 4) {
	cl_kernel kern;
	kern = clCreateKernel(app, argv[4], &err);
	if (err == CL_SUCCESS) clReleaseKernel(kern);
	else printf("Error creating kernel (%i)\n", err);
    }

    clReleaseProgram(app);
    clReleaseContext(ctx);
    
}