Skip to content

Commit 2bcb605

Browse files
committed
Code added to get PCI Bus ID from a CUDA device.
Some workaround has been done to get the same thing from OpenCL, but both OpenCL and CUDA don't print the same info. So for the moment, getting PCI Bus ID is available from CUDA and "not supported" for OpenCL (see commented code (will be removed after reviewing!)). Recall: PCI Bus ID is printed as: domain:bus:device.function where domain, bus, device and function are hexadecimal strings: domain: 16 bits info (4 characters) bus: 8 bits info (2 characters) device: 5 bits info (2 characters) function: 3 bits info (1 character) Reference: http://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__DEVICE.html#group__CUDART__DEVICE_1gea264dad3d8c4898e0b82213c0253def http://www.makelinux.net/ldd3/chp-12-sect-1
1 parent a0491d1 commit 2bcb605

7 files changed

Lines changed: 98 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Build
12
build
23
Debug
34
Release

pygpu/gpuarray.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ cdef extern from "gpuarray/buffer.h":
8787
int GA_CTX_DISABLE_ALLOCATION_CACHE
8888

8989
int GA_CTX_PROP_DEVNAME
90+
int GA_CTX_PROP_PCIBUSID
9091
int GA_CTX_PROP_MAXLSIZE
9192
int GA_CTX_PROP_LMEMSIZE
9293
int GA_CTX_PROP_NUMPROCS

pygpu/gpuarray.pyx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,19 @@ cdef class GpuContext:
10161016
free(tmp)
10171017
return res
10181018

1019+
property pcibusid:
1020+
"Device PCI Bus ID for this context"
1021+
def __get__(self):
1022+
cdef char *tmp
1023+
cdef unicode res
1024+
1025+
ctx_property(self, GA_CTX_PROP_PCIBUSID, &tmp)
1026+
try:
1027+
res = tmp.decode('ascii')
1028+
finally:
1029+
free(tmp)
1030+
return res
1031+
10191032
property maxlsize:
10201033
"Maximum size of thread block (local size) for this context"
10211034
def __get__(self):

src/gpuarray/buffer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,15 @@ GPUARRAY_PUBLIC gpucontext *gpukernel_context(gpukernel *k);
680680
*/
681681
#define GA_CTX_PROP_COMM_OPS 18
682682

683+
/**
684+
* Get the device PCI Bus ID for the context.
685+
*
686+
* \note The returned string is allocated and must be freed by the caller.
687+
*
688+
* Type: `char *`
689+
*/
690+
#define GA_CTX_PROP_PCIBUSID 19
691+
683692
/* Start at 512 for GA_BUFFER_PROP_ */
684693
#define GA_BUFFER_PROP_START 512
685694

src/gpuarray_buffer_cuda.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,30 @@ static int cuda_property(gpucontext *c, gpudata *buf, gpukernel *k, int prop_id,
15021502
*((char **)res) = s;
15031503
cuda_exit(ctx);
15041504
return GA_NO_ERROR;
1505+
1506+
case GA_CTX_PROP_PCIBUSID:
1507+
cuda_enter(ctx);
1508+
ctx->err = cuCtxGetDevice(&id);
1509+
if (ctx->err != CUDA_SUCCESS) {
1510+
cuda_exit(ctx);
1511+
return GA_IMPL_ERROR;
1512+
}
1513+
s = malloc(13);
1514+
if (s == NULL) {
1515+
cuda_exit(ctx);
1516+
return GA_MEMORY_ERROR;
1517+
}
1518+
ctx->err = cudaDeviceGetPCIBusId(s, 13, id);
1519+
if (ctx->err != CUDA_SUCCESS) {
1520+
/* PS: in GA_CTX_PROP_DEVNAME above, s is not freed here.
1521+
* I think it should be freed, isn't it ? */
1522+
free(s);
1523+
cuda_exit(ctx);
1524+
return GA_IMPL_ERROR;
1525+
}
1526+
*((char **)res) = s;
1527+
cuda_exit(ctx);
1528+
return GA_NO_ERROR;
15051529

15061530
case GA_CTX_PROP_MAXLSIZE:
15071531
cuda_enter(ctx);

src/gpuarray_buffer_opencl.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,14 @@ static int cl_property(gpucontext *c, gpudata *buf, gpukernel *k, int prop_id,
11521152
size_t *psz;
11531153
cl_device_id id;
11541154
cl_uint ui;
1155+
/* For GA_CTX_PROP_PCIBUSID (currently desactivated).
1156+
* According to http://www.makelinux.net/ldd3/chp-12-sect-1
1157+
* (accessed on 2016/11/09:15h41 EST):
1158+
* domain: 16 bits, bus: 8 bits, device: 5 bits, function: 3 bits. */
1159+
/*
1160+
uint32_t* busid;
1161+
uint32_t domain = 0, bus = 0, device = 0, function = 0;
1162+
*/
11551163

11561164
case GA_CTX_PROP_DEVNAME:
11571165
ctx->err = clGetContextInfo(ctx->ctx, CL_CONTEXT_DEVICES, sizeof(id),
@@ -1172,6 +1180,46 @@ static int cl_property(gpucontext *c, gpudata *buf, gpukernel *k, int prop_id,
11721180
*((char **)res) = s;
11731181
return GA_NO_ERROR;
11741182

1183+
case GA_CTX_PROP_PCIBUSID:
1184+
/* PS: Currently desactivated. This does not print the same
1185+
* Bus ID as cuda and nvidia-smi. For the moment, I don't find
1186+
* which info will display the correct PCI Bus ID with OpenCL. */
1187+
/*
1188+
ctx->err = clGetContextInfo(ctx->ctx, CL_CONTEXT_DEVICES, sizeof(id),
1189+
&id, NULL);
1190+
if (ctx->err != CL_SUCCESS)
1191+
return GA_IMPL_ERROR;
1192+
ctx->err = clGetDeviceInfo(id, CL_DEVICE_VENDOR_ID, 0, NULL, &sz);
1193+
if (ctx->err != CL_SUCCESS)
1194+
return GA_IMPL_ERROR;
1195+
if(sz != 4)
1196+
return GA_IMPL_ERROR;
1197+
busid = malloc(sz);
1198+
if (busid == NULL)
1199+
return GA_MEMORY_ERROR;
1200+
ctx->err = clGetDeviceInfo(id, CL_DEVICE_VENDOR_ID, sz, busid, NULL);
1201+
if (ctx->err != CL_SUCCESS) {
1202+
free(busid);
1203+
return GA_IMPL_ERROR;
1204+
}
1205+
domain = *busid >> (32-16);
1206+
bus = *busid << 16 >> (32-8);
1207+
device = *busid << 24 >> (32-5);
1208+
function = *busid << 29 >> (32-3);
1209+
free(busid);
1210+
s = malloc(13);
1211+
sprintf(s, "%04x", domain);
1212+
sprintf(s + 5, "%02x", bus);
1213+
sprintf(s + 8, "%02x", device);
1214+
sprintf(s + 11, "%01x", function);
1215+
s[4] = s[7] = ':';
1216+
s[10] = '.';
1217+
*((char **)res) = s;
1218+
return GA_NO_ERROR;
1219+
*/
1220+
*((void **)res) = NULL;
1221+
return GA_DEVSUP_ERROR;
1222+
11751223
case GA_CTX_PROP_MAXLSIZE:
11761224
ctx->err = clGetContextInfo(ctx->ctx, CL_CONTEXT_DEVICES, sizeof(id),
11771225
&id, NULL);

src/private_cuda.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
#ifdef __APPLE__
55
#include <CUDA/cuda.h>
6+
#include <CUDA/cuda_runtime_api.h>
67
#else
78
#include <cuda.h>
9+
#include <cuda_runtime_api.h>
810
#endif
911

1012
#include <cache.h>

0 commit comments

Comments
 (0)