Skip to content
Browse files

Added asynchronous interface to cuCtxSynchronize

  • Loading branch information...
1 parent 93bfcbc commit a691386b3f833f4d6dcd67e490dc27effe140683 Jitu Das committed Mar 26, 2012
Showing with 90 additions and 13 deletions.
  1. +72 −4 src/ctx.cpp
  2. +8 −1 src/ctx.hpp
  3. +10 −8 test/test.js
View
76 src/ctx.cpp
@@ -31,9 +31,10 @@ Handle<Value> Ctx::New(const Arguments& args) {
pctx->Wrap(args.This());
unsigned int flags = args[0]->Uint32Value();
- Device *pdev = ObjectWrap::Unwrap<Device>(args[1]->ToObject());
+ pctx->m_device = ObjectWrap::Unwrap<Device>(args[1]->ToObject())->m_device;
- CUresult error = cuCtxCreate(&(pctx->m_context), flags, pdev->m_device);
+ cuCtxCreate(&(pctx->m_context), flags, pctx->m_device);
+
return args.This();
}
@@ -77,11 +78,78 @@ Handle<Value> Ctx::GetCurrent(const Arguments& args) {
return scope.Close(Number::New(error));
}
+struct SynchronizeParams {
+ Ctx *ctx;
+ CUresult error;
+ Persistent<Function> cb;
+};
+
Handle<Value> Ctx::Synchronize(const Arguments& args) {
HandleScope scope;
- CUresult error = cuCtxSynchronize();
- return scope.Close(Number::New(error));
+ if (args.Length() >= 1 && args[0]->IsFunction()) {
+ // Asynchronous
+ Local<Function> cb = Local<Function>::Cast(args[0]);
+
+ Ctx *ctx = ObjectWrap::Unwrap<Ctx>(args.This());
+ if (ctx->sync_in_progress)
+ return scope.Close(Number::New(-1));
+
+ SynchronizeParams *params = new SynchronizeParams();
+ params->ctx = ctx;
+ params->cb = Persistent<Function>::New(cb);
+
+ ctx->Ref();
+ ctx->sync_in_progress = true;
+
+ cuCtxPopCurrent(NULL);
+
+ eio_custom(EIO_Synchronize, EIO_PRI_DEFAULT, EIO_AfterSynchronize, params);
+ ev_ref(EV_DEFAULT_UC);
+
+ return Undefined();
+
+ } else {
+ // Synchronous
+ CUresult error = cuCtxSynchronize();
+ return scope.Close(Number::New(error));
+ }
+}
+
+void Ctx::EIO_Synchronize(eio_req *req) {
+ SynchronizeParams *params = static_cast<SynchronizeParams*>(req->data);
+
+ params->error = cuCtxPushCurrent(params->ctx->m_context);
+ if (params->error) return;
+
+ params->error = cuCtxSynchronize();
+ if (params->error) return;
+
+ params->error = cuCtxPopCurrent(NULL);
+}
+
+int Ctx::EIO_AfterSynchronize(eio_req *req) {
+ HandleScope scope;
+ SynchronizeParams *params = static_cast<SynchronizeParams*>(req->data);
+ ev_unref(EV_DEFAULT_UC);
+
+ params->ctx->Unref();
+ params->ctx->sync_in_progress = false;
+
+ cuCtxPushCurrent(params->ctx->m_context);
+
+ Local<Value> argv[1];
+ argv[0] = Number::New(params->error);
+
+ TryCatch try_catch;
+ params->cb->Call(Context::GetCurrent()->Global(), 1, argv);
+ if (try_catch.HasCaught()) FatalException(try_catch);
+
+ params->cb.Dispose();
+
+ delete params;
+
+ return 0;
}
Handle<Value> Ctx::GetApiVersion(Local<String> property, const AccessorInfo &info) {
View
9 src/ctx.hpp
@@ -3,6 +3,7 @@
#include <cuda.h>
#include "bindings.hpp"
+#include "device.hpp"
namespace NodeCuda {
@@ -22,12 +23,18 @@ class Ctx : public ObjectWrap {
static Handle<Value> Synchronize(const Arguments& args);
static Handle<Value> GetApiVersion(Local<String> property, const AccessorInfo &info);
- Ctx() : ObjectWrap(), m_context(NULL) {}
+ Ctx() : ObjectWrap(), m_context(NULL), m_device(0), sync_in_progress(false) {}
~Ctx () {}
private:
CUcontext m_context;
+ CUdevice m_device;
+
+ bool sync_in_progress;
+
+ static void EIO_Synchronize(eio_req *req);
+ static int EIO_AfterSynchronize(eio_req *req);
};
}
View
18 test/test.js
@@ -70,13 +70,15 @@ var error = cuMem.copyDtoH(buf);
console.log("Copied buffer to host:", error);
//cuCtxSynchronize
-var error = cuCtx.synchronize();
-console.log("Context synchronize with error code: " + error);
+var error = cuCtx.synchronize(function (error) {
+ console.log("Context synchronize with error code: " + error);
-//cuMemFree
-var error = cuMem.free();
-console.log("Mem Free with error code: " + error);
+ //cuMemFree
+ var error = cuMem.free();
+ console.log("Mem Free with error code: " + error);
+
+ //cuCtxDestroy
+ error = cuCtx.destroy();
+ console.log("Context destroyed with error code: " + error);
+});
-//cuCtxDestroy
-error = cuCtx.destroy();
-console.log("Context destroyed with error code: " + error);

0 comments on commit a691386

Please sign in to comment.
Something went wrong with that request. Please try again.