File: piglit-framework-cl-program.c

package info (click to toggle)
piglit 0~git20200212-f4710c51b-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 106,972 kB
  • sloc: ansic: 263,763; xml: 48,941; python: 29,918; lisp: 19,789; cpp: 12,142; sh: 22; makefile: 20; pascal: 5
file content (360 lines) | stat: -rw-r--r-- 12,403 bytes parent folder | download | duplicates (3)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * Copyright © 2012 Blaž Tomažič <blaz.tomazic@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "piglit-framework-cl-program.h"


/* Default test configuration values */
const struct piglit_cl_program_test_config
             PIGLIT_CL_DEFAULT_PROGRAM_TEST_CONFIG = {
	.clc_version_min = 0,
	.clc_version_max = 0,

	.program_source = NULL,
	.program_source_file = NULL,
	.program_binary = NULL,
	.program_binary_file = NULL,

	.build_options = NULL,
	.expect_build_fail = false,

	.kernel_name = NULL,
};

/* Return default values for test configuration */
const void*
piglit_cl_get_empty_program_test_config()
{
	return &PIGLIT_CL_DEFAULT_PROGRAM_TEST_CONFIG;
}

/* Check configuration */
void piglit_cl_program_test_init(const int argc,
                                 const char** argv,
                                 void* void_config)
{
	struct piglit_cl_program_test_config* config = void_config;

	/* Run test's init */
	if(config->_init_test != NULL) {
		config->_init_test(argc, argv, void_config);
	}

	/* Check that config is valid */
	// run_per_device, run_per_platform
	if(!(config->run_per_device || config->run_per_platform)) {
		fprintf(stderr, "Invalid configuration, neither run_per_device nor run_per_platform is set to true.\n");
		piglit_report_result(PIGLIT_WARN);
	}
	// clc_version_min
	if(config->clc_version_min == 0) {
		config->clc_version_min = 10;
	}
	if(config->clc_version_min <= 0) {
		fprintf(stderr, "Invalid configuration, clc_version_min is %d.\n",
		        config->clc_version_min);
		piglit_report_result(PIGLIT_WARN);
	}
	// clc_version_max
	if(config->clc_version_max < 0) {
		fprintf(stderr, "Invalid configuration, clc_version_max is %d.\n",
		        config->clc_version_max);
		piglit_report_result(PIGLIT_WARN);
	}
	if(   config->clc_version_max > 0
	   && config->clc_version_max < config->clc_version_min) {
		fprintf(stderr, "Invalid configuration, clc_version_max (%d) is lower than clc_version_min (%d).\n",
		        config->clc_version_max, config->clc_version_min);
		piglit_report_result(PIGLIT_WARN);
	}
	// program_*
	if(!( /* one must be different than NULL */
	     (   config->program_source        != NULL
	      || config->program_source_file   != NULL
	      || config->program_binary        != NULL
	      || config->program_binary_file   != NULL
	     )
	     && /* the other three must be NULL */
	     (   (   config->program_source_file == NULL
	          && config->program_binary      == NULL
	          && config->program_binary_file == NULL)
	      || (   config->program_source      == NULL
	          && config->program_binary      == NULL
	          && config->program_binary_file == NULL)
	      || (   config->program_source      == NULL
	          && config->program_source_file == NULL
	          && config->program_binary_file == NULL)
	      || (   config->program_source      == NULL
	          && config->program_source_file == NULL
	          && config->program_binary      == NULL)
	     )
	    )) {
		fprintf(stderr, "Invalid configuration, one and only one of program_* must be defined.\n");
		piglit_report_result(PIGLIT_WARN);
	}
	// build_fail and kernel_name
	if(config->expect_build_fail && config->kernel_name != NULL) {
		fprintf(stderr, "Invalid configuration, kernel_name cannot be defined when build_fail is true.\n");
		piglit_report_result(PIGLIT_WARN);
	}
}

/* Run by piglit_cl_framework_run() */
enum piglit_result
piglit_cl_program_test_run(const int argc,
                           const char** argv,
                           void* void_config,
                           int version,
                           cl_platform_id platform_id,
                           cl_device_id device_id)
{
	enum piglit_result result;

	struct piglit_cl_program_test_config* config = void_config;
	struct piglit_cl_program_test_env env = {
		.version = version,
		.clc_version = 0,

		.platform_id = NULL,
		.device_id = NULL,

		.context = NULL,

		.program = NULL,

		.kernel = NULL,
	};

	int i;
	char* build_options = malloc(1 * sizeof(char));
	unsigned int num_devices;
	cl_device_id* device_ids;

	build_options[0] = '\0';

	/* Set environment */
	env.platform_id = platform_id;
	env.device_id = device_id;
	env.version = version;

	/* Get device ids */
	if(config->run_per_platform) {
		num_devices = piglit_cl_get_device_ids(platform_id,
		                                       CL_DEVICE_TYPE_ALL,
		                                       &device_ids);
	}

	/* Check OpenCL C version to test against */
	if(config->run_per_platform) {
		for(i = 0; i < num_devices; i++) {
			int device_clc_version =
				piglit_cl_get_device_cl_c_version(device_ids[i]);

			if(device_clc_version < env.clc_version || env.clc_version == 0) {
				env.clc_version = device_clc_version;
			}
		}
	} else { // config->run_per_device
		env.clc_version = piglit_cl_get_device_cl_c_version(device_id);
	}
	if(env.clc_version > version) {
		printf("#   Lowering OpenCL C version to %d.%d because of OpenCL version.\n",
		       version/10, version%10);
		env.clc_version = version;
	}
	if(   config->clc_version_max > 0
	   && env.clc_version > config->clc_version_max) {
		printf("#   Lowering OpenCL C version to %d.%d because of clc_version_max.\n",
		       config->clc_version_max/10, config->clc_version_max%10);
		env.clc_version = config->clc_version_max;
	}
	if(env.clc_version < config->clc_version_min) {
		printf("Trying to run test with OpenCL C version (%d.%d) ""lower than clc_version_min: %d\n",
		       env.clc_version/10, env.clc_version%10,
		       config->clc_version_min);
		return PIGLIT_SKIP;
	}

	printf("#   OpenCL C version: %d.%d\n",
	       env.clc_version/10, env.clc_version%10);

	/* Create context */
	if(config->run_per_platform) {
		env.context = piglit_cl_create_context(platform_id, device_ids,
		                                       num_devices);
	} else { // config->run_per_device
		env.context = piglit_cl_create_context(platform_id, &device_id, 1);
	}

	if(env.context == NULL) {
		return PIGLIT_FAIL;
	}

	/* Set build options */
	if(config->build_options != NULL) {
		char* old = build_options;
		build_options = malloc((strlen(old) + strlen(config->build_options) + 1) * sizeof(char));
		strcpy(build_options, old);
		sprintf(build_options+strlen(old), "%s", config->build_options);
		free(old);
	}

	if(env.clc_version > 10) {
		//If -cl-std was already in config->build_options, use what the test requested
		if (!strstr(build_options, "-cl-std")){
			char* template = " -cl-std=CL%d.%d";
			char* old = build_options;
			build_options = malloc((strlen(old) + strlen(template) + 1) * sizeof(char));
			strcpy(build_options, old);
			sprintf(build_options+strlen(old), template, env.clc_version/10,
								     env.clc_version%10);
			free(old);
		}
	}

	printf("#   Build options: %s\n", build_options);

	/* Create and build program */
	if(config->program_source != NULL) {
		if(!config->expect_build_fail) {
			env.program = piglit_cl_build_program_with_source(env.context,
			                                                  1,
			                                                  &config->program_source,
			                                                  build_options);
		} else {
			env.program = piglit_cl_fail_build_program_with_source(env.context,
			                                                       1,
			                                                       &config->program_source,
			                                                       build_options);
		}
	} else if(config->program_source_file != NULL) {
		unsigned int size;
		char* program_source;

		program_source = piglit_load_text_file(config->program_source_file, &size);
		if(program_source != NULL && size > 0) {
			if(!config->expect_build_fail) {
				env.program = piglit_cl_build_program_with_source(env.context,
				                                                  1,
				                                                  &program_source,
				                                                  build_options);
			} else {
				env.program = piglit_cl_fail_build_program_with_source(env.context,
				                                                       1,
				                                                       &program_source,
				                                                       build_options);
			}
		} else {
			fprintf(stderr, "Program source file %s does not exists or is empty\n",
			        config->program_source_file);
			return PIGLIT_WARN;
		}
		free(program_source);
	} else if(config->program_binary != NULL) {
		size_t length = strlen((char*)config->program_binary);

		if(!config->expect_build_fail) {
			env.program = piglit_cl_build_program_with_binary(env.context,
			                                                  &length,
			                                                  &config->program_binary,
			                                                  build_options);
		} else {
			env.program = piglit_cl_fail_build_program_with_binary(env.context,
			                                                       &length,
			                                                       &config->program_binary,
			                                                       build_options);
		}
	} else if(config->program_binary_file != NULL) {
		unsigned int length;
		size_t* lengths = malloc(sizeof(size_t) * env.context->num_devices);
		unsigned char** program_binaries = malloc(sizeof(unsigned char*) * env.context->num_devices);

		((char**)program_binaries)[0] =
			piglit_load_text_file(config->program_binary_file, &length);
		lengths[0] = length;
		for(i = 1; i < env.context->num_devices; i++) {
			lengths[i] = lengths[0];
			program_binaries[i] = program_binaries[0];
		}

		if(((char**)program_binaries)[0] != NULL && length > 0) {
			if(!config->expect_build_fail) {
				env.program = piglit_cl_build_program_with_binary(env.context,
				                                                  lengths,
				                                                  program_binaries,
				                                                  build_options);
			} else {
				env.program = piglit_cl_fail_build_program_with_binary(env.context,
				                                                       lengths,
				                                                       program_binaries,
				                                                       build_options);
			}
		} else {
			fprintf(stderr, "Program binary file %s does not exists or is empty\n",
			        config->program_source_file);
			return PIGLIT_WARN;
		}

		free(program_binaries[0]);
		free(program_binaries);
		free(lengths);
	}

	free(build_options);

	if(env.program == NULL) {
		return PIGLIT_FAIL;
	}

	/* Create kernel(s) */
	if(config->kernel_name != NULL) {
		env.kernel = piglit_cl_create_kernel(env.program, config->kernel_name);

		if(env.kernel == NULL) {
			return PIGLIT_FAIL;
		}
	}

	/* Release retrieved device IDs */
	if(config->run_per_platform) {
		free(device_ids);
	}


	/* Run the actual test */
	result = config->_program_test(argc, argv, config, &env);


	/* Release kernel(s) */
	if(env.kernel != NULL) {
		clReleaseKernel(env.kernel);
	}

	/* Release program */
	clReleaseProgram(env.program);

	/* Release context */
	piglit_cl_release_context(env.context);

	return result;
}