summaryrefslogtreecommitdiffstats
path: root/all_pairs/source/h264_dec/copyright.txt
diff options
context:
space:
mode:
Diffstat (limited to 'all_pairs/source/h264_dec/copyright.txt')
0 files changed, 0 insertions, 0 deletions
9177fce34edb5ad0059a41548888d05588cc1c5'>89177fc
97e97dc



89177fc
97e97dc





89177fc


































97e97dc

89177fc


97e97dc


89177fc

97e97dc


89177fc





























97e97dc
89177fc



97e97dc

89177fc






































97e97dc
89177fc




97e97dc
89177fc




















97e97dc
89177fc

































97e97dc
89177fc






































































































































97e97dc

89177fc
97e97dc
89177fc


















97e97dc
89177fc

97e97dc
89177fc


97e97dc
89177fc







97e97dc
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
                               

                                                          

                                                  
                   

                  

                    



                   
                     





                                                  


































                                                                                

                                   


                                                                          


           

                                                                     


                                                                                         





























                                                                                                                                   
                                      



                                                             

 






































                                                                                                             
                 




                                                      
                 




















                                                                                 
                         

































                                                                     
                 






































































































































                                                                                                                                                                        

                                                         
                                                                                                                                                            
                                 


















                                                                                                                                                                                                                   
                                 

                                                                                         
                         


                                                
         







                                                              
 
// Copyright 2025 Joshua Bakita
// Show or change the GPU core affinity for a CUDA process
// taskset-like utility for NVIDIA GPUs
#define _GNU_SOURCE // For program_invocation_name
#include <argp.h>
#include <dirent.h>
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>

#include <cuda.h> // To help with getting GPC info

#include "libsmctrl.h"

#define LINK_NAME "/memfd:libsmctrl"

// TODO: Write automated tests:
// - Change region of non-existent PID
// - Change region of permission denied PID
// - Change region of non-GPU PID
// - Change GPC list
// - Change TPC list
// - Change TPC mask
// - Start TPC mask
// - Start TPC list
// - Start GPC list
// - Start with subargument containing -
// - Query GPC list
// - Query TPC list
// - Query TPC mask
// - Set GPC list w/ non-existant GPC
// - Set TPC list w/ non-existant TPC

// Private symbols from libsmctrl
extern bool libsmctrl_is_mps_running();
extern uint128_t strtou128(const char *nptr, char **endptr, int base);

const char *argp_program_bug_address = "<jbakita@cs.unc.edu>";
const char *argp_program_version = "nvtaskset 2025.06";
const char desc[] = "Show or change the GPU core affinity for a CUDA process\v"
                    "Warning: When using GPC lists, this tool currently "
                    "derives TPC to GPC mappings from the first NVIDIA GPU in "
                    "the system (by PCI bus ID) device. To use the mappings "
                    "for a different device, use the `libsmctrl_test_get_info` "
                    "tool to get the bitmask of TPCs associated with each GPC, "
                    "OR them, and then set that bitmask via this tool. Better "
                    "multi-GPU support is intended for a future release.\n\n"
                    "Inspired by the Linux taskset utility.";
const char args_doc[] = "[mask | list] [pid | cmd [args...]]";

const struct argp_option opts[] = {
	{"gpc-list", 'g', NULL, 0, "Specify partition as a list of GPCs"},
	{"tpc-list", 't', NULL, 0, "Specify partition as a list of TPCs"},
	{"pid",      'p', NULL, 0, "Operate on an existing PID"},
	{0}
};

// Create a CUDA context and query the associated GPC to TPC mappings
// Based off logic in libsmctrl_test_gpc_info
void libsmctrl_get_gpc_info_ext_easy(uint32_t* num_gpcs, uint128_t** masks, int gpu_id) {
	int res;
	CUcontext ctx;
	char *old_order = NULL;
	// Tell CUDA to use PCI device id ordering (to match nvdebug)
	putenv((char*)"CUDA_DEVICE_ORDER=PCI_BUS_ID");
	// Allow CUDA to see all devices (to better match nvdebug)
	if (getenv("CUDA_VISIBLE_DEVICES")) {
		if (!(old_order = strdup(getenv("CUDA_VISIBLE_DEVICES"))))
			error(1, errno, "Unable to allocate environment string");
		unsetenv("CUDA_VISIBLE_DEVICES");
	}
	// A CUDA context is required before reading the topology information
	if ((res = cuInit(0))) {
		const char* name;
		cuGetErrorName(res, &name);
		error(1, 0, "Unable to create a initialize CUDA, error %s\n", name);
	}
	if ((res = cuCtxCreate(&ctx, 0, gpu_id))) {
		const char* name;
		cuGetErrorName(res, &name);
		error(1, 0, "Unable to create a CUDA context, error %s\n", name);
	}
	// Pull topology information from libsmctrl
	if ((res = libsmctrl_get_gpc_info_ext(num_gpcs, masks, gpu_id)) != 0) {
		error(0, res, "libsmctrl_get_gpc_info() failed");
		if (res == ENOENT)
			fprintf(stderr, "%s: Is the nvdebug kernel module loaded?\n", program_invocation_name);
		if (res == EIO)
			fprintf(stderr, "%s: Is the GPU powered on, i.e., is there an active context?\n", program_invocation_name);
		exit(1);
	}
	// Restore the environment (in case we exec() later)
	unsetenv("CUDA_DEVICE_ORDER");
	if (old_order) {
		setenv("CUDA_VISIBLE_DEVICES", old_order, 1);
		free(old_order);
	}
}

int parse_list(bool use_gpcs, char* list, uint128_t *mask_out) {
	// We support the same ranges as taskset, e.g., X,Y,Z and X,Y-Z
	uint32_t num_xpcs = 0; // Either TPC or GPC count, i.e., "X"PC
	uint128_t* masks = NULL;
	// TODO: Allow specifying GPU ID, rather than assuming 0!
	if (use_gpcs)
		libsmctrl_get_gpc_info_ext_easy(&num_xpcs, &masks, 0);
	else
		libsmctrl_get_tpc_info_cuda(&num_xpcs, 0);
	uint128_t mask = 0;
	int range_start_xpc = -1;
	char* start = list;
	int len = strlen(list);
	// Convert comma-seperated GPC/TPC list into a mask
	for (int i = 0; i < len + 1; i++) {
		if (list[i] == ',' || list[i] == '\0') {
			list[i] = '\0';
			int xpc = atoi(start);
			if (xpc > num_xpcs - 1)
				error(1, EINVAL, "%s is not a valid %s ID", start, use_gpcs ? "GPC" : "TPC");
			// Handle ranges
			if (range_start_xpc != -1) {
				if (range_start_xpc >= xpc)
					error(1, EINVAL, "Malformed %s range", use_gpcs ? "GPC" : "TPC");
				while (range_start_xpc <= xpc) {
					if (use_gpcs)
						mask |= masks[range_start_xpc];
					else
						mask |= (uint128_t)1 << range_start_xpc;
					range_start_xpc++;
				}
				range_start_xpc = -1;
			} else {
				if (use_gpcs)
					mask |= masks[xpc];
				else
					mask |= (uint128_t)1 << xpc;
			}
			start = list + i + 1;
		}
		// Range start
		if (list[i] == '-') {
			list[i] = '\0';
			range_start_xpc = atoi(start);
			start = list + i + 1;
		}
	}
	*mask_out = mask;
	return 0;
}

// Always returns a valid string
char* compose_list(uint128_t mask) {
	// List will always be shorter than every TPC, comma-seperated
	// 128 TPCs, with 10 1-char, 90 2-char, 28 3-char, 127 commas, and 1 null
	static char list[10 + 90*2 + 28*3 + 128];
	char* tail = list;
	int last_enabled = -2;
	bool in_range;
	for (int i = 0; i < 128; i++) {
		bool enabled = (mask >> i) & 1;
		if (in_range) {
			if (enabled) {
				last_enabled = i;
			} else {
				tail += sprintf(tail, "%d,", last_enabled);
				in_range = false;
			}
			continue;
		}
		if (enabled) {
			if (last_enabled == i - 1) {
				in_range = true;
				tail += sprintf(tail, "-");
			} else {
				tail += sprintf(tail, "%d", i);
			}
			last_enabled = i;
		} else {
			if (last_enabled == i - 1) {
				tail += sprintf(tail, ",");
			}
		}
	}
	// Strip trailing comma
	if (*(tail - 1) == ',')
		*(tail - 1) = '\0';
	return list;
}

// Always returns a valid string
// (Terminates the program on error)
char* compose_gpc_list(uint128_t mask) {
	uint32_t num_gpcs = 0;
	uint128_t* masks = NULL;
	libsmctrl_get_gpc_info_ext_easy(&num_gpcs, &masks, 0);
	uint128_t gpc_mask = 0;
	// Try to find correspondence between a list of TPCs and GPCs