Browse files

Added initial Cuda Context bindings.

  • Loading branch information...
1 parent 0992bde commit 66d103b03357394016c94179ed47492f4dfc2fe7 @kashif kashif committed Jul 23, 2011
Showing with 73 additions and 20 deletions.
  1. +2 −0 src/bindings.cpp
  2. +38 −0 src/cuda_ctx.cpp
  3. +25 −0 src/cuda_ctx.hpp
  4. +0 −10 src/cuda_device.cpp
  5. +2 −7 src/cuda_device.hpp
  6. +5 −2 test/test.js
  7. +1 −1 wscript
View
2 src/bindings.cpp
@@ -1,8 +1,10 @@
#include "bindings.hpp"
#include "cuda_device.hpp"
+#include "cuda_ctx.hpp"
extern "C"
void init (Handle<Object> target) {
HandleScope scope;
CudaDevice::Initialize(target);
+ CudaCtx::Initialize(target);
}
View
38 src/cuda_ctx.cpp
@@ -0,0 +1,38 @@
+#include "cuda_ctx.hpp"
+#include "cuda_device.hpp"
+
+Persistent<FunctionTemplate> CudaCtx::constructor_template;
+
+void CudaCtx::Initialize(Handle<Object> target) {
+ HandleScope scope;
+
+ Local<FunctionTemplate> t = FunctionTemplate::New(CudaCtx::New);
+ constructor_template = Persistent<FunctionTemplate>::New(t);
+ constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
+ constructor_template->SetClassName(String::NewSymbol("CudaCtx"));
+
+ NODE_SET_PROTOTYPE_METHOD(constructor_template, "Destroy", CudaCtx::destroy);
+
+ target->Set(String::NewSymbol("CudaCtx"), constructor_template->GetFunction());
+}
+
+Handle<Value> CudaCtx::New(const Arguments& args) {
+ HandleScope scope;
+
+ CudaCtx *pctx = new CudaCtx();
+ pctx->Wrap(args.Holder());
+
+ unsigned int flags = args[0]->Uint32Value();
+ CudaDevice *pdev = ObjectWrap::Unwrap<CudaDevice>(args[1]->ToObject());
+
+ CUresult error = cuCtxCreate(&(pctx->m_context), flags, pdev->m_device);
+ return args.This();
+}
+
+Handle<Value> CudaCtx::destroy(const Arguments& args) {
+ HandleScope scope;
+ CudaCtx *pctx = ObjectWrap::Unwrap<CudaCtx>(args.Holder());
+
+ CUresult error = cuCtxDestroy(pctx->m_context);
+ return scope.Close(Number::New(error));
+}
View
25 src/cuda_ctx.hpp
@@ -0,0 +1,25 @@
+#ifndef CUDA_CTX_HPP
+#define CUDA_CTX_HPP
+
+#include <cuda.h>
+#include "bindings.hpp"
+
+class CudaCtx : public EventEmitter
+{
+public:
+ static void Initialize (Handle<Object> target);
+
+protected:
+ static Persistent<FunctionTemplate> constructor_template;
+ static Handle<Value> New(const Arguments& args);
+ static Handle<Value> destroy(const Arguments& args);
+
+ CudaCtx () : EventEmitter () {
+ m_context = NULL;
+ }
+ ~CudaCtx (){}
+
+private:
+ CUcontext m_context;
+};
+#endif
View
10 src/cuda_device.cpp
@@ -18,7 +18,6 @@ void CudaDevice::Initialize (Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD(constructor_template, "GetName", CudaDevice::getName);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "TotalMem", CudaDevice::totalMem);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "ComputeCapability", CudaDevice::computeCapability);
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "CtxCreate", CudaDevice::ctxCreate);
target->Set(String::NewSymbol("CudaDevice"), constructor_template->GetFunction());
}
@@ -80,13 +79,4 @@ Handle<Value> CudaDevice::totalMem(const Arguments& args) {
cuDeviceTotalMem(&totalGlobalMem, cu->m_device);
return scope.Close(Number::New(totalGlobalMem));
-}
-
-Handle<Value> CudaDevice::ctxCreate(const Arguments& args) {
- HandleScope scope;
-
- CudaDevice *cu = ObjectWrap::Unwrap<CudaDevice>(args.Holder());
- CUresult error = cuCtxCreate(&(cu->m_context), 0, cu->m_device);
-
- return scope.Close(Number::New(error));
}
View
9 src/cuda_device.hpp
@@ -7,7 +7,8 @@
class CudaDevice : public EventEmitter {
public:
static void Initialize (Handle<Object> target);
-
+
+ CUdevice m_device;
protected:
static Persistent<FunctionTemplate> constructor_template;
@@ -17,19 +18,13 @@ class CudaDevice : public EventEmitter {
static Handle<Value> getName(const Arguments& args);
static Handle<Value> computeCapability(const Arguments& args);
static Handle<Value> totalMem(const Arguments& args);
- static Handle<Value> ctxCreate(const Arguments& args);
CudaDevice() : EventEmitter () {
m_device = NULL;
- m_context = NULL;
}
~CudaDevice(){
}
-
-private:
- CUdevice m_device;
- CUcontext m_context;
};
#endif
View
7 test/test.js
@@ -20,5 +20,8 @@ var compute = cuDevice.ComputeCapability();
console.log("Device compute capability: major=" + compute[0] + " minor=" + compute[1]);
//cuCtxCreate
-var error = cuDevice.CtxCreate();
-console.log("Ctx created with error code: " + error);
+var cuCtx = new cu.CudaCtx(0, cuDevice);
+
+//cuCtxDestroy
+var error = cuCtx.Destroy();
+console.log("Context destroyed with error code: " + error);
View
2 wscript
@@ -21,5 +21,5 @@ def configure(conf):
def build(bld):
obj = bld.new_task_gen('cxx', 'shlib', 'node_addon')
obj.target = "binding"
- obj.source = "src/bindings.cpp src/cuda_device.cpp"
+ obj.source = "src/bindings.cpp src/cuda_device.cpp src/cuda_ctx.cpp"
obj.uselib = "CUDA"

0 comments on commit 66d103b

Please sign in to comment.