Permalink
Browse files

Made cleaner method for setting up kernel arguments.

  • Loading branch information...
1 parent 457c4b1 commit 8b35ebaf6354e35fa1d16f28d7053a41bf76f67b Jitu Das committed Mar 28, 2012
Showing with 83 additions and 38 deletions.
  1. +77 −0 index.js
  2. +1 −29 src/function.cpp
  3. +0 −1 src/function.hpp
  4. +5 −8 test/test.js
View
@@ -0,0 +1,77 @@
+module.exports = require('./build/Release/cuda');
+
+// Type names follow the W3C typed array specs, not NodeJS's Buffer library
+
+var typeByteSize = {
+ "Uint8": 1,
+ "Uint16": 2,
+ "Uint32": 4,
+ "Int8": 1,
+ "Int16": 2,
+ "Int32": 4,
+ "Float32": 4,
+ "Float64": 8,
+ "DevicePtr": 8
+};
+
+var typeAlignment = {
+ "Uint8": 1,
+ "Uint16": 2,
+ "Uint32": 4,
+ "Int8": 1,
+ "Int16": 2,
+ "Int32": 4,
+ "Float32": 4,
+ "Float64": 8,
+ "DevicePtr": 8
+};
+
+var typeBufferFunc = {
+ "Uint8": "UInt8",
+ "Uint16": "UInt16",
+ "Uint32": "UInt32",
+ "Int8": "Int8",
+ "Int16": "Int16",
+ "Int32": "Int32",
+ "Float32": "Float",
+ "Float64": "Double"
+}
+
+function alignUp(offset, alignment) {
+ return (((offset) + (alignment) - 1) & ~((alignment) - 1));
+}
+
+module.exports.prepareArguments = function (args) {
+ var paramBufferSize = 0;
+
+ for (var i in args) {
+ var type = args[i].type;
+
+ paramBufferSize = alignUp(paramBufferSize, typeAlignment[type]);
+
+ if (typeof(typeByteSize[type]) != "number")
+ throw "Invalid type given";
+
+ paramBufferSize += typeByteSize[type];
+ }
+
+ var paramBuffer = new Buffer(paramBufferSize);
+
+ var offset = 0;
+ for (var i in args) {
+ var type = args[i].type;
+
+ offset = alignUp(offset, typeAlignment[type]);
+
+ if (type == "DevicePtr") {
+ args[i].value.copy(paramBuffer, offset);
+ } else {
+ paramBuffer["write" + typeBufferFunc[type] + "LE"](args[i].value, offset);
+ }
+
+ offset += typeByteSize[type];
+ }
+
+ return paramBuffer;
+}
+
View
@@ -16,8 +16,6 @@ void NodeCuda::Function::Initialize(Handle<Object> target) {
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
constructor_template->SetClassName(String::NewSymbol("CudaFunction"));
- NODE_SET_METHOD(target, "addToParamBuffer", NodeCuda::Function::AddToParamBuffer);
-
NODE_SET_PROTOTYPE_METHOD(constructor_template, "launch", NodeCuda::Function::LaunchKernel);
// Function objects can only be created by cuModuleGetFunction
@@ -48,7 +46,7 @@ Handle<Value> NodeCuda::Function::LaunchKernel(const Arguments& args) {
Local<Object> buf = args[2]->ToObject();
char *pbuffer = Buffer::Data(buf);
- size_t bufferSize = args[3]->IntegerValue();
+ size_t bufferSize = Buffer::Length(buf);
void *cuExtra[] = {
CU_LAUNCH_PARAM_BUFFER_POINTER, pbuffer,
@@ -64,29 +62,3 @@ Handle<Value> NodeCuda::Function::LaunchKernel(const Arguments& args) {
return scope.Close(Number::New(error));
}
-// From from NVIDIA C Programming Guide
-#define ALIGN_UP(offset, alignment) \
- (((offset) + (alignment) - 1) & ~((alignment) - 1))
-
-Handle<Value> NodeCuda::Function::AddToParamBuffer(const Arguments& args) {
- HandleScope scope;
-
- Local<Object> dstbuf = args[0]->ToObject();
- char *dst = Buffer::Data(dstbuf);
-
- size_t bufferSize = args[1]->IntegerValue();
-
- Local<Object> srcbuf = args[2]->ToObject();
- char *src = Buffer::Data(srcbuf);
- size_t srclen = Buffer::Length(srcbuf);
-
- size_t alignment = args[3]->IntegerValue();
-
- bufferSize = ALIGN_UP(bufferSize, alignment);
- for (int i=0; i<srclen; i++)
- dst[i+bufferSize] = src[i];
- bufferSize += srclen;
-
- return scope.Close(Number::New(bufferSize));
-}
-
View
@@ -15,7 +15,6 @@ class Function : public ObjectWrap {
static Persistent<FunctionTemplate> constructor_template;
static Handle<Value> LaunchKernel(const Arguments& args);
- static Handle<Value> AddToParamBuffer(const Arguments& args);
Function() : ObjectWrap(), m_function(0) {}
View
@@ -1,5 +1,5 @@
var Buffer = require('buffer').Buffer;
-var cu = require(__dirname + '/../build/Release/cuda');
+var cu = require(__dirname + '/../index');
//cuDriverGetVersion
//cuDeviceGetCount
@@ -54,14 +54,11 @@ console.log("Loaded module:", cuModule);
var cuFunction = cuModule.getFunction("helloWorld");
console.log("Got function:", cuFunction);
-var paramBuffer = new Buffer(256);
-var paramBufferSize = 0;
-var argBuffer = new Buffer(8);
-paramBufferSize = cu.addToParamBuffer(paramBuffer, paramBufferSize, cuMem.devicePtr, 8);
-
//cuLaunchKernel
-//var error = cuFunction.launch([3,1,1], [2,2,2], cuMem);
-var error = cuFunction.launch([3,1,1], [2,2,2], paramBuffer, paramBufferSize);
+var args = cu.prepareArguments([
+ { type: "DevicePtr", value: cuMem.devicePtr },
+]);
+var error = cuFunction.launch([3,1,1], [2,2,2], args);
console.log("Launched kernel:", error);

0 comments on commit 8b35eba

Please sign in to comment.