Skip to content

Commit

Permalink
Added initial Cuda Context bindings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kashif Rasul committed Jul 23, 2011
1 parent 0992bde commit 66d103b
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/bindings.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
38 changes: 38 additions & 0 deletions src/cuda_ctx.cpp
Original file line number Diff line number Diff line change
@@ -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));
}
25 changes: 25 additions & 0 deletions src/cuda_ctx.hpp
Original file line number Diff line number Diff line change
@@ -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
10 changes: 0 additions & 10 deletions src/cuda_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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));
}
9 changes: 2 additions & 7 deletions src/cuda_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
class CudaDevice : public EventEmitter {
public:
static void Initialize (Handle<Object> target);


CUdevice m_device;
protected:
static Persistent<FunctionTemplate> constructor_template;

Expand All @@ -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
7 changes: 5 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
2 changes: 1 addition & 1 deletion wscript
Original file line number Diff line number Diff line change
Expand Up @@ -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.