Skip to content

Commit

Permalink
cuda: modified for kcuda
Browse files Browse the repository at this point in the history
  • Loading branch information
iwata-axe committed Jun 10, 2013
1 parent c67210a commit 14746ee
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 66 deletions.
77 changes: 39 additions & 38 deletions cuda/driver_api/context.c
Expand Up @@ -30,7 +30,7 @@
#include "gdev_list.h"

struct gdev_list gdev_ctx_list;
pthread_mutex_t gdev_ctx_list_mutex;
LOCK_T gdev_ctx_list_lock;

/**
* Creates a new CUDA context and associates it with the calling thread.
Expand Down Expand Up @@ -120,6 +120,30 @@ CUresult cuCtxCreate_v2(CUcontext *pctx, unsigned int flags, CUdevice dev)
/* save the Gdev handle. */
ctx->gdev_handle = handle;

/* save the current context to the stack, if necessary. */
gdev_list_init(&ctx->list_entry, ctx);

/* initialize context synchronization list. */
gdev_list_init(&ctx->sync_list, NULL);
/* initialize context event list. */
gdev_list_init(&ctx->event_list, NULL);

/* we will trace # of kernels. */
ctx->launch_id = 0;
/* save the device ID. */
ctx->minor = minor;

ctx->flags = flags;
ctx->usage = 0;
ctx->destroyed = 0;
ctx->owner = GETTID();
ctx->user = 0;

/* set to the current context. */
res = cuCtxPushCurrent(ctx);
if (res != CUDA_SUCCESS)
goto fail_push_current;

/* get the CUDA-specific device information. */
cuda_info = &ctx->cuda_info;
if (gquery(handle, GDEV_QUERY_CHIPSET, &cuda_info->chipset)) {
Expand Down Expand Up @@ -155,37 +179,14 @@ CUresult cuCtxCreate_v2(CUcontext *pctx, unsigned int flags, CUdevice dev)
cuda_info->warp_size = 32;
}

/* save the current context to the stack, if necessary. */
gdev_list_init(&ctx->list_entry, ctx);

/* initialize context synchronization list. */
gdev_list_init(&ctx->sync_list, NULL);
/* initialize context event list. */
gdev_list_init(&ctx->event_list, NULL);

/* we will trace # of kernels. */
ctx->launch_id = 0;
/* save the device ID. */
ctx->minor = minor;

ctx->flags = flags;
ctx->usage = 0;
ctx->destroyed = 0;
ctx->owner = pthread_self();
ctx->user = (pthread_t)NULL;

/* set to the current context. */
res = cuCtxPushCurrent(ctx);
if (res != CUDA_SUCCESS)
goto fail_push_current;

*pctx = ctx;

return CUDA_SUCCESS;

fail_push_current:
fail_query_mp_count:
fail_query_chipset:
cuCtxPopCurrent(&ctx);
fail_push_current:
gclose(handle);
fail_open_gdev:
FREE(ctx);
Expand Down Expand Up @@ -379,16 +380,16 @@ CUresult cuCtxGetCurrent(CUcontext *pctx)
if (!pctx)
return CUDA_ERROR_INVALID_CONTEXT;

pthread_mutex_lock(&gdev_ctx_list_mutex);
LOCK(&gdev_ctx_list_lock);

gdev_list_for_each(ctx, &gdev_ctx_list, list_entry) {
if (ctx->user == pthread_self())
if (ctx->user == GETTID())
break;
}

pthread_mutex_unlock(&gdev_ctx_list_mutex);
UNLOCK(&gdev_ctx_list_lock);

if (ctx->destroyed) {
if (ctx && ctx->destroyed) {
res = cuCtxPopCurrent(&ctx);
if (res != CUDA_SUCCESS)
return res;
Expand Down Expand Up @@ -514,14 +515,14 @@ CUresult cuCtxPushCurrent(CUcontext ctx)
if (ctx->usage)
return CUDA_ERROR_INVALID_CONTEXT;

pthread_mutex_lock(&gdev_ctx_list_mutex);
LOCK(&gdev_ctx_list_lock);

/* save the current context to the stack. */
ctx->usage++;
ctx->user = pthread_self();
ctx->user = GETTID();
gdev_list_add(&ctx->list_entry, &gdev_ctx_list);

pthread_mutex_unlock(&gdev_ctx_list_mutex);
UNLOCK(&gdev_ctx_list_lock);

return CUDA_SUCCESS;
}
Expand Down Expand Up @@ -570,13 +571,13 @@ CUresult cuCtxPopCurrent(CUcontext *pctx)
if (res != CUDA_SUCCESS)
return res;

pthread_mutex_lock(&gdev_ctx_list_mutex);
LOCK(&gdev_ctx_list_lock);

gdev_list_del(&cur->list_entry);
cur->usage--;
cur->user = (pthread_t)NULL;
cur->user = 0;

pthread_mutex_unlock(&gdev_ctx_list_mutex);
UNLOCK(&gdev_ctx_list_lock);

if (cur->destroyed) {
res = freeDestroyedContext(cur);
Expand Down Expand Up @@ -777,7 +778,7 @@ CUresult cuCtxSynchronize(void)
Ghandle handle;
struct gdev_cuda_fence *f;
struct gdev_list *p;
struct timespec time;
TIME_T time;
struct CUevent_st *e;

if (!gdev_initialized)
Expand All @@ -802,7 +803,7 @@ CUresult cuCtxSynchronize(void)
}

/* complete event */
clock_gettime(CLOCK_MONOTONIC, &time);
GETTIME(&time);
while ((p = gdev_list_head(&cur->event_list))) {
gdev_list_del(p);
e = gdev_list_container(p);
Expand Down
81 changes: 66 additions & 15 deletions cuda/driver_api/device.c
Expand Up @@ -30,6 +30,40 @@

int gdev_device_count = 0;

static Ghandle __gopen(int minor)
{
#if 0
return gopen(minor);
#else
Ghandle handle = NULL;
CUresult res;
CUcontext ctx;
res = cuCtxGetCurrent(&ctx);
if (res == CUDA_SUCCESS) {
if (ctx)
handle = ctx->gdev_handle;
}
if (handle == NULL)
handle = gopen(minor);
return handle;
#endif
}
static int __gclose(Ghandle handle)
{
#if 0
return gclose(handle);
#else
CUresult res;
CUcontext ctx;
res = cuCtxGetCurrent(&ctx);
if (res == CUDA_SUCCESS) {
if (ctx && handle == ctx->gdev_handle)
return 0;
}
return gclose(handle);
#endif
}

/**
* Returns in *major and *minor the major and minor revision numbers that
* define the compute capability of the device dev.
Expand Down Expand Up @@ -176,15 +210,21 @@ CUresult cuDeviceGetAttribute(int *pi, CUdevice_attribute attrib, CUdevice dev)
Ghandle handle;
struct device_properties *props;

handle = gopen(minor);
handle = __gopen(minor);
if (handle == NULL) {
res = CUDA_ERROR_INVALID_CONTEXT;
break;
}
*pi = 0;
#ifndef __KERNEL__
if (gquery(handle, GDEV_QUERY_PCI_VENDOR, &pci_vendor))
res = CUDA_ERROR_INVALID_DEVICE;
else {
if (pci_vendor != 0x10de)
res = CUDA_ERROR_INVALID_DEVICE;
}
if (res != CUDA_SUCCESS) {
gclose(handle);
__gclose(handle);
break;
}
if (gquery(handle, GDEV_QUERY_PCI_DEVICE, &pci_device))
Expand All @@ -197,17 +237,18 @@ CUresult cuDeviceGetAttribute(int *pi, CUdevice_attribute attrib, CUdevice dev)
*pi = props->mp_count;
}
if (res != CUDA_SUCCESS) {
gclose(handle);
__gclose(handle);
break;
}
#endif
if (!*pi) {
if (gquery(handle, GDEV_NVIDIA_QUERY_MP_COUNT,
&mp_count))
res = CUDA_ERROR_INVALID_DEVICE;
else
*pi = mp_count;
}
gclose(handle);
__gclose(handle);
}
break;
case CU_DEVICE_ATTRIBUTE_PCI_DEVICE_ID:
Expand All @@ -216,12 +257,16 @@ CUresult cuDeviceGetAttribute(int *pi, CUdevice_attribute attrib, CUdevice dev)
int minor = (int)dev;
Ghandle handle;

handle = gopen(minor);
handle = __gopen(minor);
if (handle == NULL) {
res = CUDA_ERROR_INVALID_CONTEXT;
break;
}
if (gquery(handle, GDEV_QUERY_PCI_DEVICE, &pci_device))
res = CUDA_ERROR_INVALID_DEVICE;
else
*pi = pci_device;
gclose(handle);
__gclose(handle);
}
break;
default:
Expand Down Expand Up @@ -285,10 +330,12 @@ CUresult cuDeviceGetName(char *name, int len, CUdevice dev)
if (!name)
return CUDA_ERROR_INVALID_VALUE;

handle = gopen(minor);
handle = __gopen(minor);
if (handle == NULL)
return CUDA_ERROR_INVALID_CONTEXT;

if (gquery(handle, GDEV_QUERY_PCI_VENDOR, &pci_vendor)) {
gclose(handle);
__gclose(handle);
return CUDA_ERROR_UNKNOWN;
}
if (pci_vendor != 0x10de) {
Expand All @@ -297,7 +344,7 @@ CUresult cuDeviceGetName(char *name, int len, CUdevice dev)
}

if (gquery(handle, GDEV_QUERY_PCI_DEVICE, &pci_device)) {
gclose(handle);
__gclose(handle);
return CUDA_ERROR_UNKNOWN;
}
props = get_device_properties(pci_device);
Expand All @@ -309,7 +356,7 @@ CUresult cuDeviceGetName(char *name, int len, CUdevice dev)
name[len-1] = '\0';

end:
gclose(handle);
__gclose(handle);

return CUDA_SUCCESS;
}
Expand Down Expand Up @@ -371,7 +418,9 @@ CUresult cuDeviceGetProperties(CUdevprop *prop, CUdevice dev)
if (!prop)
return CUDA_ERROR_INVALID_VALUE;

handle = gopen(minor);
handle = __gopen(minor);
if (handle == NULL)
return CUDA_ERROR_INVALID_CONTEXT;

if (gquery(handle, GDEV_QUERY_PCI_VENDOR, &pci_vendor)) {
return CUDA_ERROR_UNKNOWN;
Expand Down Expand Up @@ -422,7 +471,7 @@ CUresult cuDeviceGetProperties(CUdevprop *prop, CUdevice dev)
prop->textureAlign = 0;

end:
gclose(handle);
__gclose(handle);

return CUDA_SUCCESS;
}
Expand Down Expand Up @@ -453,16 +502,18 @@ CUresult cuDeviceTotalMem_v2(size_t *bytes, CUdevice dev)
if (!bytes)
return CUDA_ERROR_INVALID_VALUE;

handle = gopen(minor);
handle = __gopen(minor);
if (handle == NULL)
return CUDA_ERROR_INVALID_CONTEXT;

if (gquery(handle, GDEV_QUERY_DEVICE_MEM_SIZE, &total_mem)) {
gclose(handle);
__gclose(handle);
return CUDA_ERROR_UNKNOWN;
}

*bytes = total_mem;

gclose(handle);
__gclose(handle);

return CUDA_SUCCESS;
}
Expand Down
11 changes: 8 additions & 3 deletions cuda/driver_api/event.c
Expand Up @@ -186,7 +186,7 @@ CUresult cuEventDestroy(CUevent hEvent)
*/
CUresult cuEventElapsedTime(float *pMilliseconds, CUevent hStart, CUevent hEnd)
{
struct timespec elapsed;
TIME_T elapsed;
long long round;

if (!gdev_initialized)
Expand All @@ -201,12 +201,17 @@ CUresult cuEventElapsedTime(float *pMilliseconds, CUevent hStart, CUevent hEnd)
if (!hEnd->complete)
return CUDA_ERROR_NOT_READY;

#ifdef __KERNEL__
elapsed.tv_sec = hEnd->time.tv_sec - hStart->time.tv_sec;
elapsed.tv_usec = hEnd->time.tv_usec - hStart->time.tv_usec;
round = elapsed.tv_sec * 1000000 + elapsed.tv_usec;
*pMilliseconds = (float)round / 1000.0;
#else
elapsed.tv_sec = hEnd->time.tv_sec - hStart->time.tv_sec;
elapsed.tv_nsec = hEnd->time.tv_nsec - hStart->time.tv_nsec;

round = (elapsed.tv_sec * 1000000000 + elapsed.tv_nsec) / 500;

*pMilliseconds = (float)round / 2000.0;
#endif

return CUDA_SUCCESS;
}
Expand Down

0 comments on commit 14746ee

Please sign in to comment.