Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added initial Cuda Mem bindings.

  • Loading branch information...
commit c58e180a84a8fad7c0858f1d7c64158d965cdcc0 1 parent b97ecf0
@kashif authored
View
2  src/bindings.cpp
@@ -1,10 +1,12 @@
#include "bindings.hpp"
#include "cuda_device.hpp"
#include "cuda_ctx.hpp"
+#include "cuda_mem.hpp"
extern "C"
void init (Handle<Object> target) {
HandleScope scope;
CudaDevice::Initialize(target);
CudaCtx::Initialize(target);
+ CudaMem::Initialize(target);
}
View
60 src/cuda_mem.cpp
@@ -0,0 +1,60 @@
+#include "cuda_mem.hpp"
+
+Persistent<FunctionTemplate> CudaMem::constructor_template;
+
+void CudaMem::Initialize(Handle<Object> target) {
+ HandleScope scope;
+
+ Local<FunctionTemplate> t = FunctionTemplate::New(CudaMem::New);
+ constructor_template = Persistent<FunctionTemplate>::New(t);
+ constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
+ constructor_template->SetClassName(String::NewSymbol("CudaMem"));
+
+ NODE_SET_PROTOTYPE_METHOD(constructor_template, "Alloc", CudaMem::alloc);
+ NODE_SET_PROTOTYPE_METHOD(constructor_template, "AllocPitch", CudaMem::allocPitch);
+ NODE_SET_PROTOTYPE_METHOD(constructor_template, "Free", CudaMem::free);
+
+ target->Set(String::NewSymbol("CudaMem"), constructor_template->GetFunction());
+}
+
+Handle<Value> CudaMem::New(const Arguments& args) {
+ HandleScope scope;
+
+ CudaMem *cuptr = new CudaMem();
+ cuptr->Wrap(args.Holder());
+
+ return args.This();
+}
+
+Handle<Value> CudaMem::alloc(const Arguments& args) {
+ HandleScope scope;
+ CudaMem *cuptr = ObjectWrap::Unwrap<CudaMem>(args.Holder());
+
+ size_t bytesize = args[0]->Uint32Value();
+ CUresult error = cuMemAlloc(&(cuptr->m_devicePtr), bytesize);
+
+ return scope.Close(Number::New(error));
+}
+
+Handle<Value> CudaMem::allocPitch(const Arguments& args) {
+ HandleScope scope;
+ CudaMem *cuptr = ObjectWrap::Unwrap<CudaMem>(args.Holder());
+
+ 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);
+
+ return scope.Close(Number::New(pPitch));
+}
+
+Handle<Value> CudaMem::free(const Arguments& args) {
+ HandleScope scope;
+ CudaMem *cuptr = ObjectWrap::Unwrap<CudaMem>(args.Holder());
+
+ CUresult error = cuMemFree(cuptr->m_devicePtr);
+
+ return scope.Close(Number::New(error));
+}
View
24 src/cuda_mem.hpp
@@ -0,0 +1,24 @@
+#ifndef CUDA_MEM_HPP
+#define CUDA_MEM_HPP
+
+#include <cuda.h>
+#include "bindings.hpp"
+
+class CudaMem : public EventEmitter {
+public:
+ static Persistent<FunctionTemplate> constructor_template;
+ static void Initialize (Handle<Object> target);
+ 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);
+
+ CUdeviceptr m_devicePtr;
+protected:
+ CudaMem () : EventEmitter () {
+ m_devicePtr = 0;
+ }
+ ~CudaMem (){}
+};
+
+#endif
View
17 test/test.js
@@ -29,6 +29,21 @@ console.log("Context synchronize with error code: " + error);
//cuCtxGetApiVersion
console.log("Context API version: " + cuCtx.GetApiVersion());
+
+//cuMemAlloc
+var cuMem = new cu.CudaMem();
+error = cuMem.Alloc(100);
+console.log("Cuda Mem Alloc with error code: " + error);
+
+//cuMemFree
+error = cuMem.Free();
+console.log("Cuda Mem Free with error code: " + error);
+
+//cuMemAllocPitch
+var pitch = cuMem.AllocPitch(100, 100, 8);
+console.log("Cuda Mem pitch of allocation in bytes: " + pitch);
+cuMem.Free();
+
//cuCtxDestroy
error = cuCtx.Destroy();
-console.log("Context destroyed with error code: " + error);
+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 src/cuda_ctx.cpp"
+ obj.source = "src/bindings.cpp src/cuda_device.cpp src/cuda_ctx.cpp src/cuda_mem.cpp"
obj.uselib = "CUDA"
Please sign in to comment.
Something went wrong with that request. Please try again.