Permalink
Browse files

Complete revamp of Mem

  • Loading branch information...
1 parent 411fc3f commit dd3633a0b31f4742be01e988f0803e3be3d8bed4 Jitu Das committed Mar 24, 2012
Showing with 115 additions and 103 deletions.
  1. +4 −4 src/bindings.cpp
  2. +2 −2 src/bindings.hpp
  3. +16 −16 src/ctx.cpp
  4. +7 −7 src/ctx.hpp
  5. +23 −26 src/device.cpp
  6. +6 −3 src/device.hpp
  7. +32 −22 src/mem.cpp
  8. +3 −3 src/mem.hpp
  9. +22 −20 test/test.js
View
@@ -10,23 +10,23 @@ void init (Handle<Object> target) {
cuInit(0);
// These methods don't need instances
- NODE_SET_METHOD(target, "DriverGetVersion", driverGetVersion);
- NODE_SET_METHOD(target, "DeviceGetCount", deviceGetCount);
+ target->SetAccessor(String::New("driverVersion"), GetDriverVersion);
+ target->SetAccessor(String::New("deviceCount"), GetDeviceCount);
// Initialize driver api bindings
Device::Initialize(target);
Ctx::Initialize(target);
Mem::Initialize(target);
}
-Handle<Value> driverGetVersion(const Arguments& args) {
+Handle<Value> GetDriverVersion(Local<String> property, const AccessorInfo &info) {
HandleScope scope;
int driverVersion = 0;
cuDriverGetVersion(&driverVersion);
return scope.Close(Integer::New(driverVersion));
}
-Handle<Value> deviceGetCount(const Arguments& args) {
+Handle<Value> GetDeviceCount(Local<String> property, const AccessorInfo &info) {
HandleScope scope;
int count = 0;
cuDeviceGetCount(&count);
View
@@ -7,7 +7,7 @@
using namespace v8;
using namespace node;
-static Handle<Value> driverGetVersion(const Arguments& args);
-static Handle<Value> deviceGetCount(const Arguments& args);
+static Handle<Value> GetDriverVersion(Local<String> property, const AccessorInfo &info);
+static Handle<Value> GetDeviceCount(Local<String> property, const AccessorInfo &info);
#endif
View
@@ -9,15 +9,15 @@ void Ctx::Initialize(Handle<Object> target) {
Local<FunctionTemplate> t = FunctionTemplate::New(Ctx::New);
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
- constructor_template->SetClassName(String::NewSymbol("Ctx"));
+ constructor_template->SetClassName(String::NewSymbol("CudaCtx"));
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "Destroy", Ctx::destroy);
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "PushCurrent", Ctx::pushCurrent);
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "PopCurrent", Ctx::popCurrent);
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "SetCurrent", Ctx::setCurrent);
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "GetCurrent", Ctx::getCurrent);
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "Synchronize", Ctx::synchronize);
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "GetApiVersion", Ctx::getApiVersion);
+ NODE_SET_PROTOTYPE_METHOD(constructor_template, "destroy", Ctx::Destroy);
+ NODE_SET_PROTOTYPE_METHOD(constructor_template, "pushCurrent", Ctx::PushCurrent);
+ NODE_SET_PROTOTYPE_METHOD(constructor_template, "popCurrent", Ctx::PopCurrent);
+ NODE_SET_PROTOTYPE_METHOD(constructor_template, "setCurrent", Ctx::SetCurrent);
+ NODE_SET_PROTOTYPE_METHOD(constructor_template, "getCurrent", Ctx::GetCurrent);
+ NODE_SET_PROTOTYPE_METHOD(constructor_template, "synchronize", Ctx::Synchronize);
+ constructor_template->InstanceTemplate()->SetAccessor(String::New("apiVersion"), Ctx::GetApiVersion);
target->Set(String::NewSymbol("Ctx"), constructor_template->GetFunction());
}
@@ -35,56 +35,56 @@ Handle<Value> Ctx::New(const Arguments& args) {
return args.This();
}
-Handle<Value> Ctx::destroy(const Arguments& args) {
+Handle<Value> Ctx::Destroy(const Arguments& args) {
HandleScope scope;
Ctx *pctx = ObjectWrap::Unwrap<Ctx>(args.This());
CUresult error = cuCtxDestroy(pctx->m_context);
return scope.Close(Number::New(error));
}
-Handle<Value> Ctx::pushCurrent(const Arguments& args) {
+Handle<Value> Ctx::PushCurrent(const Arguments& args) {
HandleScope scope;
Ctx *pctx = ObjectWrap::Unwrap<Ctx>(args.This());
CUresult error = cuCtxPushCurrent(pctx->m_context);
return scope.Close(Number::New(error));
}
-Handle<Value> Ctx::popCurrent(const Arguments& args) {
+Handle<Value> Ctx::PopCurrent(const Arguments& args) {
HandleScope scope;
Ctx *pctx = ObjectWrap::Unwrap<Ctx>(args.This());
CUresult error = cuCtxPopCurrent(&(pctx->m_context));
return scope.Close(Number::New(error));
}
-Handle<Value> Ctx::setCurrent(const Arguments& args) {
+Handle<Value> Ctx::SetCurrent(const Arguments& args) {
HandleScope scope;
Ctx *pctx = ObjectWrap::Unwrap<Ctx>(args.This());
CUresult error = cuCtxSetCurrent(pctx->m_context);
return scope.Close(Number::New(error));
}
-Handle<Value> Ctx::getCurrent(const Arguments& args) {
+Handle<Value> Ctx::GetCurrent(const Arguments& args) {
HandleScope scope;
Ctx *pctx = ObjectWrap::Unwrap<Ctx>(args.This());
CUresult error = cuCtxGetCurrent(&(pctx->m_context));
return scope.Close(Number::New(error));
}
-Handle<Value> Ctx::synchronize(const Arguments& args) {
+Handle<Value> Ctx::Synchronize(const Arguments& args) {
HandleScope scope;
CUresult error = cuCtxSynchronize();
return scope.Close(Number::New(error));
}
-Handle<Value> Ctx::getApiVersion(const Arguments& args) {
+Handle<Value> Ctx::GetApiVersion(Local<String> property, const AccessorInfo &info) {
HandleScope scope;
- Ctx *pctx = ObjectWrap::Unwrap<Ctx>(args.This());
+ Ctx *pctx = ObjectWrap::Unwrap<Ctx>(info.Holder());
unsigned int version;
CUresult error = cuCtxGetApiVersion(pctx->m_context, &version);
View
@@ -12,13 +12,13 @@ class Ctx : public ObjectWrap {
static Persistent<FunctionTemplate> constructor_template;
static Handle<Value> New(const Arguments& args);
- static Handle<Value> destroy(const Arguments& args);
- static Handle<Value> pushCurrent(const Arguments& args);
- static Handle<Value> popCurrent(const Arguments& args);
- static Handle<Value> setCurrent(const Arguments& args);
- static Handle<Value> getCurrent(const Arguments& args);
- static Handle<Value> synchronize(const Arguments& args);
- static Handle<Value> getApiVersion(const Arguments& args);
+ static Handle<Value> Destroy(const Arguments& args);
+ static Handle<Value> PushCurrent(const Arguments& args);
+ static Handle<Value> PopCurrent(const Arguments& args);
+ static Handle<Value> SetCurrent(const Arguments& args);
+ static Handle<Value> GetCurrent(const Arguments& args);
+ static Handle<Value> Synchronize(const Arguments& args);
+ static Handle<Value> GetApiVersion(Local<String> property, const AccessorInfo &info);
Ctx() : ObjectWrap(), m_context(NULL) {}
View
@@ -8,16 +8,16 @@ void Device::Initialize(Handle<Object> target) {
Local<FunctionTemplate> t = FunctionTemplate::New(Device::New);
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
- constructor_template->SetClassName(String::NewSymbol("Device"));
+ constructor_template->SetClassName(String::NewSymbol("CudaDevice"));
- constructor_template->InstanceTemplate()->SetAccessor(String::New("name"), Device::Name);
- constructor_template->InstanceTemplate()->SetAccessor(String::New("totalMem"), Device::TotalMem);
- constructor_template->InstanceTemplate()->SetAccessor(String::New("computeCapability"), Device::ComputeCapability);
+ constructor_template->InstanceTemplate()->SetAccessor(String::New("name"), Device::GetName);
+ constructor_template->InstanceTemplate()->SetAccessor(String::New("totalMem"), Device::GetTotalMem);
+ constructor_template->InstanceTemplate()->SetAccessor(String::New("computeCapability"), Device::GetComputeCapability);
target->Set(String::NewSymbol("Device"), constructor_template->GetFunction());
}
-static Handle<Value> GetName(CUdevice device) {
+static Handle<Value> GetName_(CUdevice device) {
HandleScope scope;
char deviceName[256];
@@ -28,49 +28,46 @@ static Handle<Value> GetName(CUdevice device) {
Handle<Value> Device::New(const Arguments& args) {
HandleScope scope;
+ Local<Object> result = args.This();
int ordinal = args[0]->IntegerValue();
- if (constructor_template->HasInstance(args.This())) {
- Device *cu = new Device();
- cuDeviceGet(&(cu->m_device), ordinal);
-
- cu->Wrap(args.This());
- return args.This();
-
- } else {
- CUdevice device;
-
- cuDeviceGet(&device, ordinal);
- return GetName(device);
+ if (!constructor_template->HasInstance(result)) {
+ result = constructor_template->InstanceTemplate()->NewInstance();
}
+
+ Device *pdevice = new Device();
+ cuDeviceGet(&(pdevice->m_device), ordinal);
+
+ pdevice->Wrap(result);
+ return scope.Close(result);
}
-Handle<Value> Device::ComputeCapability(Local<String> property, const AccessorInfo &info) {
+Handle<Value> Device::GetComputeCapability(Local<String> property, const AccessorInfo &info) {
HandleScope scope;
- Device *cu = ObjectWrap::Unwrap<Device>(info.Holder());
+ Device *pdevice = ObjectWrap::Unwrap<Device>(info.Holder());
int major = 0, minor = 0;
- cuDeviceComputeCapability(&major, &minor, cu->m_device);
+ cuDeviceComputeCapability(&major, &minor, pdevice->m_device);
Local<Object> result = Object::New();
result->Set(String::New("major"), Integer::New(major));
result->Set(String::New("minor"), Integer::New(minor));
return scope.Close(result);
}
-Handle<Value> Device::Name(Local<String> property, const AccessorInfo &info) {
+Handle<Value> Device::GetName(Local<String> property, const AccessorInfo &info) {
HandleScope scope;
- Device *cu = ObjectWrap::Unwrap<Device>(info.Holder());
- return GetName(cu->m_device);
+ Device *pdevice = ObjectWrap::Unwrap<Device>(info.Holder());
+ return GetName_(pdevice->m_device);
}
-Handle<Value> Device::TotalMem(Local<String> property, const AccessorInfo &info) {
+Handle<Value> Device::GetTotalMem(Local<String> property, const AccessorInfo &info) {
HandleScope scope;
- Device *cu = ObjectWrap::Unwrap<Device>(info.Holder());
+ Device *pdevice = ObjectWrap::Unwrap<Device>(info.Holder());
size_t totalGlobalMem;
- cuDeviceTotalMem(&totalGlobalMem, cu->m_device);
+ cuDeviceTotalMem(&totalGlobalMem, pdevice->m_device);
return scope.Close(Number::New(totalGlobalMem));
}
View
@@ -12,9 +12,12 @@ class Device : public ObjectWrap {
static Persistent<FunctionTemplate> constructor_template;
static Handle<Value> New(const Arguments& args);
- static Handle<Value> ComputeCapability(Local<String> property, const AccessorInfo &info);
- static Handle<Value> Name(Local<String> property, const AccessorInfo &info);
- static Handle<Value> TotalMem(Local<String> property, const AccessorInfo &info);
+ static Handle<Value> GetComputeCapability(Local<String> property, const AccessorInfo &info);
+ static Handle<Value> GetName(Local<String> property, const AccessorInfo &info);
+ static Handle<Value> GetTotalMem(Local<String> property, const AccessorInfo &info);
+
+ // TODO: cuDeviceGetAttribute
+ // TODO: cuDeviceGetProperties
Device() : ObjectWrap(), m_device(NULL) {}
View
@@ -8,53 +8,63 @@ void Mem::Initialize(Handle<Object> target) {
Local<FunctionTemplate> t = FunctionTemplate::New(Mem::New);
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
- constructor_template->SetClassName(String::NewSymbol("Mem"));
+ constructor_template->SetClassName(String::NewSymbol("CudaMem"));
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "Alloc", Mem::alloc);
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "AllocPitch", Mem::allocPitch);
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "Free", Mem::free);
-
- target->Set(String::NewSymbol("Mem"), constructor_template->GetFunction());
+ // Mem objects can only be created by allocation functions
+ NODE_SET_METHOD(target, "memAlloc", Mem::Alloc);
+ NODE_SET_METHOD(target, "memAllocPitch", Mem::AllocPitch);
+
+ NODE_SET_PROTOTYPE_METHOD(constructor_template, "free", Mem::Free);
}
Handle<Value> Mem::New(const Arguments& args) {
HandleScope scope;
- Mem *cuptr = new Mem();
- cuptr->Wrap(args.This());
+ Mem *pmem = new Mem();
+ pmem->Wrap(args.This());
return args.This();
}
-Handle<Value> Mem::alloc(const Arguments& args) {
+Handle<Value> Mem::Alloc(const Arguments& args) {
HandleScope scope;
- Mem *cuptr = ObjectWrap::Unwrap<Mem>(args.This());
+ Local<Object> result = constructor_template->InstanceTemplate()->NewInstance();
+ Mem *pmem = ObjectWrap::Unwrap<Mem>(result);
size_t bytesize = args[0]->Uint32Value();
- CUresult error = cuMemAlloc(&(cuptr->m_devicePtr), bytesize);
+ CUresult error = cuMemAlloc(&(pmem->m_devicePtr), bytesize);
+
+ // TODO: Throw error if any
+
+ result->Set(String::New("size"), Integer::NewFromUnsigned(bytesize));
- return scope.Close(Number::New(error));
+ return scope.Close(result);
}
-Handle<Value> Mem::allocPitch(const Arguments& args) {
+Handle<Value> Mem::AllocPitch(const Arguments& args) {
HandleScope scope;
- Mem *cuptr = ObjectWrap::Unwrap<Mem>(args.This());
+ Local<Object> result = constructor_template->InstanceTemplate()->NewInstance();
+ Mem *pmem = ObjectWrap::Unwrap<Mem>(result);
- size_t bytesize = args[0]->Uint32Value();
size_t pPitch;
- size_t WidthInBytes = args[1]->Uint32Value();
- size_t Height = args[2]->Uint32Value();
unsigned int ElementSizeBytes = args[2]->Uint32Value();
- CUresult error = cuMemAllocPitch(&(cuptr->m_devicePtr), &pPitch, WidthInBytes, Height, ElementSizeBytes);
+ size_t WidthInBytes = ElementSizeBytes * args[0]->Uint32Value();
+ size_t Height = args[1]->Uint32Value();
+ CUresult error = cuMemAllocPitch(&(pmem->m_devicePtr), &pPitch, WidthInBytes, Height, ElementSizeBytes);
+
+ // TODO: Throw error if any
+
+ result->Set(String::New("size"), Integer::NewFromUnsigned(pPitch * Height));
+ result->Set(String::New("pitch"), Integer::NewFromUnsigned(pPitch));
- return scope.Close(Number::New(pPitch));
+ return scope.Close(result);
}
-Handle<Value> Mem::free(const Arguments& args) {
+Handle<Value> Mem::Free(const Arguments& args) {
HandleScope scope;
- Mem *cuptr = ObjectWrap::Unwrap<Mem>(args.This());
+ Mem *pmem = ObjectWrap::Unwrap<Mem>(args.This());
- CUresult error = cuMemFree(cuptr->m_devicePtr);
+ CUresult error = cuMemFree(pmem->m_devicePtr);
return scope.Close(Number::New(error));
}
View
@@ -12,9 +12,9 @@ class Mem : public ObjectWrap {
static Persistent<FunctionTemplate> constructor_template;
static Handle<Value> New(const Arguments& args);
- static Handle<Value> alloc(const Arguments& args);
- static Handle<Value> allocPitch(const Arguments& args);
- static Handle<Value> free(const Arguments& args);
+ static Handle<Value> Alloc(const Arguments& args);
+ static Handle<Value> AllocPitch(const Arguments& args);
+ static Handle<Value> Free(const Arguments& args);
Mem() : ObjectWrap(), m_devicePtr(0) {}
Oops, something went wrong.

0 comments on commit dd3633a

Please sign in to comment.