Skip to content

Commit 5e951d5

Browse files
authored
allow creating cuda IPC handles for all Memory types except PinnedMemory and ManagedMemory (#442)
1 parent 2829f0a commit 5e951d5

4 files changed

Lines changed: 40 additions & 10 deletions

File tree

modules/cuda/src/buffer.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2021, NVIDIA CORPORATION.
1+
// Copyright (c) 2020-2022, NVIDIA CORPORATION.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
1414

1515
import {Math, runtime} from './addon';
1616
import {BigIntArray, MemoryData, TypedArray, TypedArrayConstructor} from './interfaces';
17-
import {DeviceMemory, IpcHandle, Memory} from './memory';
17+
import {DeviceMemory, IpcHandle, ManagedMemory, Memory, PinnedMemory} from './memory';
1818
import {
1919
clampRange,
2020
isArrayBufferLike,
@@ -319,10 +319,15 @@ export abstract class MemoryView<T extends TypedArray|BigIntArray = any> impleme
319319
* @summary Create an IpcHandle for the underlying CUDA device memory.
320320
*/
321321
public getIpcHandle() {
322-
if (!(this.buffer instanceof DeviceMemory)) {
323-
throw new Error(`${this[Symbol.toStringTag]}'s buffer must be an instance of DeviceMemory`);
322+
if (this.buffer instanceof PinnedMemory) {
323+
throw new Error(
324+
`${this[Symbol.toStringTag]}'s buffer must not be an instance of PinnedMemory`);
324325
}
325-
return new IpcHandle(this.buffer, this.byteOffset);
326+
if (this.buffer instanceof ManagedMemory) {
327+
throw new Error(
328+
`${this[Symbol.toStringTag]}'s buffer must not be an instance of ManagedMemory`);
329+
}
330+
return new IpcHandle(<any>this.buffer, this.byteOffset);
326331
}
327332
}
328333

modules/cuda/src/memory.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2021, NVIDIA CORPORATION.
1+
// Copyright (c) 2020-2022, NVIDIA CORPORATION.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -54,14 +54,16 @@ export class IpcHandle extends CUDAIpcHandle {
5454

5555
/**
5656
* @summary An object describing the exported {@link DeviceMemory} and CUDA IPC handle.
57-
* @returns An object with the device ordinal, byte offset (if applicable) into the
58-
* exported {@link DeviceMemory}, and the 64-bit IPC handle (as a JavaScript Array of octets).
57+
* @returns An object with the device ordinal, the 64-bit IPC handle (as a JavaScript Array of
58+
* octets), byte offset (if applicable) into the exported {@link DeviceMemory}, byte length of
59+
* the IPC segment.
5960
*/
6061
public toJSON() {
6162
return {
6263
device: this.device,
63-
byteOffset: this.byteOffset,
6464
handle: [...this.handle],
65+
byteOffset: this.byteOffset,
66+
byteLength: this.buffer.byteLength - this.byteOffset,
6567
};
6668
}
6769
}

modules/cuda/src/memory/memory.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2021, NVIDIA CORPORATION.
1+
// Copyright (c) 2020-2022, NVIDIA CORPORATION.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -69,6 +69,19 @@ Napi::Value cudaMemGetInfoNapi(CallbackArgs const& args) {
6969
std::vector<std::string>{"free", "total"});
7070
}
7171

72+
Napi::Value cudaPointerGetAttributesNapi(CallbackArgs const& args) {
73+
auto env = args.Env();
74+
void* dptr = args[0];
75+
CUDARTAPI::cudaPointerAttributes attrs{};
76+
NODE_CUDA_TRY(CUDARTAPI::cudaPointerGetAttributes(&attrs, dptr), env);
77+
auto obj = Napi::Object::New(env);
78+
obj.Set("type", attrs.type);
79+
obj.Set("device", attrs.device);
80+
obj.Set("hptr", reinterpret_cast<uint64_t>(attrs.hostPointer));
81+
obj.Set("dptr", reinterpret_cast<uint64_t>(attrs.devicePointer));
82+
return obj;
83+
}
84+
7285
// CUresult cuPointerGetAttribute(void *data, CUpointer_attribute attribute,
7386
// CUdeviceptr ptr);
7487
Napi::Value cuPointerGetAttributeNapi(CallbackArgs const& args) {
@@ -140,6 +153,7 @@ Napi::Object initModule(Napi::Env const& env,
140153
EXPORT_FUNC(env, runtime, "cudaMemset", cudaMemsetNapi);
141154
EXPORT_FUNC(env, runtime, "cudaMemcpy", cudaMemcpyNapi);
142155
EXPORT_FUNC(env, runtime, "cudaMemGetInfo", cudaMemGetInfoNapi);
156+
EXPORT_FUNC(env, runtime, "cudaPointerGetAttributes", cudaPointerGetAttributesNapi);
143157
EXPORT_FUNC(env, driver, "cuPointerGetAttribute", cuPointerGetAttributeNapi);
144158

145159
auto PointerAttributes = Napi::Object::New(env);

modules/cuda/src/node_cuda/utilities/cpp_to_napi.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,12 @@ inline Napi::Value CPPToNapi::operator()(cudaDeviceProp const& props) const {
9999
}
100100

101101
} // namespace nv
102+
103+
namespace Napi {
104+
105+
template <>
106+
inline Value Value::From(napi_env env, CUDARTAPI::cudaMemoryType const& type) {
107+
return Value::From(env, static_cast<uint8_t>(type));
108+
}
109+
110+
} // namespace Napi

0 commit comments

Comments
 (0)