forked from kashif/node-cuda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cuda.js
87 lines (68 loc) · 1.72 KB
/
cuda.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
module.exports = require('./build/Release/cuda.node');
// 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": "UInt16LE",
"Uint32": "UInt32LE",
"Int8": "Int8",
"Int16": "Int16LE",
"Int32": "Int32LE",
"Float32": "FloatLE",
"Float64": "DoubleLE"
}
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]](args[i].value, offset);
}
offset += typeByteSize[type];
}
return paramBuffer;
}
module.exports.launch = function () {
var func = arguments[0];
var gridDim = arguments[1];
var blockDim = arguments[2];
var args = arguments[3];
args = module.exports.prepareArguments(args);
return func.launchKernel(gridDim, blockDim, args);
}