static void *do_init(CUdevice dev, int flags, int *ret) { cuda_context *res; CUcontext ctx; unsigned int fl = CU_CTX_SCHED_AUTO; int i; CHKFAIL(NULL); if (flags & GA_CTX_SINGLE_THREAD) fl = CU_CTX_SCHED_SPIN; if (flags & GA_CTX_MULTI_THREAD) fl = CU_CTX_SCHED_YIELD; err = cuDeviceGetAttribute(&i, CU_DEVICE_ATTRIBUTE_UNIFIED_ADDRESSING, dev); CHKFAIL(NULL); if (i != 1) FAIL(NULL, GA_UNSUPPORTED_ERROR); err = cuCtxCreate(&ctx, fl, dev); CHKFAIL(NULL); res = cuda_make_ctx(ctx, 0); if (res == NULL) { cuCtxDestroy(ctx); FAIL(NULL, GA_IMPL_ERROR); } res->flags |= flags; /* Don't leave the context on the thread stack */ cuCtxPopCurrent(NULL); return res; }
static void *cuda_init(int ord, int flags, int *ret) { CUdevice dev; CUcontext ctx; cuda_context *res; static int init_done = 0; unsigned int fl = CU_CTX_SCHED_AUTO; if (ord == -1) { /* Grab the ambient context */ err = cuCtxGetCurrent(&ctx); CHKFAIL(NULL); res = cuda_make_ctx(ctx, DONTFREE); if (res == NULL) { FAIL(NULL, GA_IMPL_ERROR); } res->flags |= flags; return res; } if (!init_done) { err = cuInit(0); CHKFAIL(NULL); init_done = 1; } err = cuDeviceGet(&dev, ord); CHKFAIL(NULL); if (flags & GA_CTX_SINGLE_THREAD) fl = CU_CTX_SCHED_SPIN; if (flags & GA_CTX_MULTI_THREAD) fl = CU_CTX_SCHED_YIELD; err = cuCtxCreate(&ctx, fl, dev); CHKFAIL(NULL); res = cuda_make_ctx(ctx, 0); res->flags |= flags; if (res == NULL) { cuCtxDestroy(ctx); FAIL(NULL, GA_IMPL_ERROR); } /* Don't leave the context on the thread stack */ cuCtxPopCurrent(NULL); return res; }
static void *cuda_init(int ord, int flags, int *ret) { CUdevice dev; cuda_context *res; static int init_done = 0; if (ord == -2) { CUcontext ctx; /* Grab the ambient context */ err = cuCtxGetCurrent(&ctx); CHKFAIL(NULL); /* If somebody made a context, then the api is initialized */ init_done = 1; res = cuda_make_ctx(ctx, DONTFREE); if (res == NULL) { FAIL(NULL, GA_IMPL_ERROR); } res->flags |= flags; return res; } if (!init_done) { err = cuInit(0); CHKFAIL(NULL); init_done = 1; } if (ord == -1) { int i, c; err = cuDeviceGetCount(&c); CHKFAIL(NULL); for (i = 0; i < c; i++) { err = cuDeviceGet(&dev, i); CHKFAIL(NULL); res = do_init(dev, flags, NULL); if (res != NULL) return res; } FAIL(NULL, GA_NODEV_ERROR); } else { err = cuDeviceGet(&dev, ord); CHKFAIL(NULL); return do_init(dev, flags, ret); } }