UNPKG

cuda.js

Version:

CUDA bindings for Node.js

72 lines 1.91 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Memory = exports.GpuBuffer = void 0; const native_1 = __importDefault(require("./native")); class GpuBuffer { constructor(arg, isHandle = false) { if (isHandle) { this._handle = arg; } else { this._handle = new native_1.default.GpuBuffer(arg); } this.elementType = undefined; } get ptr() { return this._handle.ptr; } get size() { return Number(this._handle.size); } free() { this._handle.free(); } upload(data) { this._handle.upload(data); } download() { return this._handle.download(); } memset(value, size) { this._handle.memset(value, size ?? 0); } copy(src, size) { this._handle.copyFrom(src._handle, size ?? 0); } // Internal marker so native can detect this is our buffer get _isGpuBuffer() { return this._handle._isGpuBuffer; } static fromHandle(handle) { return new GpuBuffer(handle, true); } } exports.GpuBuffer = GpuBuffer; class Memory { static malloc(size) { return new GpuBuffer(size); } static free(buffer) { buffer.free(); } static toDevice(data) { const buf = new GpuBuffer(data.byteLength); buf.upload(data); return buf; } static fromDevice(buffer, dtype = 'uint8') { // MVP: return Uint8Array and let higher-level API re-interpret return buffer.download(); } static memset(buffer, value) { buffer.memset(value); } static copy(dst, src, size) { dst.copy(src, size); } } exports.Memory = Memory; //# sourceMappingURL=memory.js.map