Skip to content

Commit

Permalink
[FIX] Fix ssr demos after most recent dependency updates (#464)
Browse files Browse the repository at this point in the history
* update forceAtlas2 to use any memory type instead of always copying to an rmm::device_buffer

* updates for deck.gl 8.9

* make worker importScripts() synchronously fetch and evaluate URLs

* synchronously initialize jsdom again

* fix luma ssr demo

* fix point-cloud ssr demo

* fix graph ssr demo

* Update yarn.lock
  • Loading branch information
trxcllnt authored Aug 10, 2023
1 parent d28a85e commit c9fddaf
Show file tree
Hide file tree
Showing 19 changed files with 1,305 additions and 1,225 deletions.
23 changes: 12 additions & 11 deletions modules/cugraph/src/graph.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022, NVIDIA CORPORATION.
// Copyright (c) 2022-2023, NVIDIA CORPORATION.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -114,18 +114,18 @@ export class Graph<T extends DataType = any> {
*
* @returns {Float32Buffer} The new positions.
*/
public forceAtlas2(options: ForceAtlas2Options = {}) {
public forceAtlas2(options: ForceAtlas2Options<any> = {positions: undefined}) {
const {numNodes} = this;
let positions: Float32Buffer|undefined;

let positions: Float32Buffer|void = undefined;

if (options.positions && typeof options.positions === 'object') {
positions = options.positions ? new Float32Buffer(options.positions instanceof MemoryView
? options.positions?.buffer
: options.positions)
: undefined;
if (positions && positions.length !== numNodes * 2) {
positions = new Float32Buffer(
options.positions instanceof MemoryView && options.positions.buffer || options.positions);
if (positions.length < numNodes * 2) {
// reallocate new positions and copy over old X/Y positions
const p =
new Float32Buffer(new DeviceBuffer(numNodes * 2 * Float32Buffer.BYTES_PER_ELEMENT));
const p = new Float32Buffer(
new DeviceBuffer(numNodes * 2 * Float32Buffer.BYTES_PER_ELEMENT, options.memoryResource));
if (positions.length > 0) {
const pn = positions.length / 2;
const sx = positions.subarray(0, Math.min(numNodes, pn));
Expand All @@ -135,7 +135,8 @@ export class Graph<T extends DataType = any> {
positions = p;
}
}
return new Float32Buffer(this.graph.forceAtlas2({...options, positions}));

return new Float32Buffer(this.graph.forceAtlas2({...options, positions: positions?.buffer}));
}
}

Expand Down
53 changes: 45 additions & 8 deletions modules/cugraph/src/layout/force_atlas2.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2022, NVIDIA CORPORATION.
// Copyright (c) 2021-2023, NVIDIA CORPORATION.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -58,15 +58,51 @@ Napi::Value Graph::force_atlas2(Napi::CallbackInfo const &info) {
auto gravity = get_float(options.Get("gravity"), 1.0);
auto verbose = get_bool(options.Get("verbose"), false);

size_t positions_offset{0};
float *x_positions{nullptr};
float *y_positions{nullptr};
DeviceBuffer::wrapper_t positions;

Napi::Object positions;
bool positions_is_device_memory_wrapper{false};

auto is_device_memory = [&](Napi::Value const &data) -> bool {
return data.IsObject() and //
data.As<Napi::Object>().Has("ptr") and //
data.As<Napi::Object>().Get("ptr").IsNumber();
};
auto is_device_memory_wrapper = [&](Napi::Value const &data) -> bool {
return data.IsObject() and //
data.As<Napi::Object>().Has("buffer") and //
data.As<Napi::Object>().Get("buffer").IsObject() and //
is_device_memory(data.As<Napi::Object>().Get("buffer"));
};
auto get_device_memory_ptr = [&](Napi::Object const &buffer) -> float * {
return reinterpret_cast<float *>(buffer.Get("ptr").ToNumber().Int64Value()) + positions_offset;
};

if (options.Has("positions") && options.Get("positions").IsObject()) {
positions = data_to_devicebuffer(
env, options.Get("positions"), cudf::data_type{cudf::type_id::FLOAT32}, mr);
x_positions = static_cast<float *>(positions->data());
y_positions = static_cast<float *>(positions->data()) + num_nodes();
positions = options.Get("positions");

if (is_device_memory_wrapper(positions)) {
positions_is_device_memory_wrapper = true;

if (positions.Has("byteOffset")) {
auto val = positions.Get("byteOffset");
if (val.IsNumber()) {
positions_offset = val.ToNumber().Int64Value();
} else if (val.IsBigInt()) {
bool lossless{false};
positions_offset = val.As<Napi::BigInt>().Uint64Value(&lossless);
}
}

positions = positions.Get("buffer").ToObject();
} else if (!is_device_memory(positions)) {
positions = data_to_devicebuffer(env, positions, cudf::data_type{cudf::type_id::FLOAT32}, mr);
}

x_positions = get_device_memory_ptr(positions);
y_positions = x_positions + num_nodes();
} else {
positions =
DeviceBuffer::New(env,
Expand All @@ -79,7 +115,7 @@ Napi::Value Graph::force_atlas2(Napi::CallbackInfo const &info) {
try {
cugraph::force_atlas2({rmm::cuda_stream_default},
graph,
static_cast<float *>(positions->data()),
get_device_memory_ptr(positions),
max_iter,
x_positions,
y_positions,
Expand All @@ -96,7 +132,8 @@ Napi::Value Graph::force_atlas2(Napi::CallbackInfo const &info) {
verbose);
} catch (std::exception const &e) { throw Napi::Error::New(info.Env(), e.what()); }

return positions;
return positions_is_device_memory_wrapper ? options.Get("positions").As<Napi::Object>()
: positions;
}

} // namespace nv
12 changes: 7 additions & 5 deletions modules/cugraph/src/node_cugraph.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2022, NVIDIA CORPORATION.
// Copyright (c) 2021-2023, NVIDIA CORPORATION.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {MemoryData, MemoryView} from '@rapidsai/cuda';
import {Memory, MemoryData, MemoryView} from '@rapidsai/cuda';
import {Column, Float32, Int32} from '@rapidsai/cudf';
import {DeviceBuffer, MemoryResource} from '@rapidsai/rmm';

Expand Down Expand Up @@ -46,7 +46,9 @@ export declare class Graph {
*
* @returns {DeviceBuffer} The new positions.
*/
forceAtlas2(options: ForceAtlas2Options): DeviceBuffer;
forceAtlas2<T extends ForceAtlas2Options<Memory>>(options: T): T['positions'];
forceAtlas2<T extends ForceAtlas2Options<MemoryView>>(options: T): T['positions'];
forceAtlas2<T extends ForceAtlas2Options<MemoryData|DeviceBuffer|void>>(options: T): DeviceBuffer;

/**
* @summary Compute the total number of edges incident to a vertex (both in and out edges).
Expand Down Expand Up @@ -104,11 +106,11 @@ export declare class Graph {
analyzeRatioCutClustering(num_clusters: number, clusters: Column<Int32>): number;
}

export interface ForceAtlas2Options {
export interface ForceAtlas2Options<TPositions = void> {
/**
* Optional buffer of initial vertex positions.
*/
positions?: MemoryData|MemoryView|DeviceBuffer;
positions: TPositions;
/**
* The maximum number of levels/iterations of the Force Atlas algorithm. When specified the
* algorithm will terminate after no more than the specified number of iterations. No error occurs
Expand Down
7 changes: 2 additions & 5 deletions modules/deck.gl/src/deck.gl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021, NVIDIA CORPORATION.
// Copyright (c) 2021-2023, NVIDIA CORPORATION.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -51,13 +51,10 @@ export interface UpdateStateProps {
}

export declare class Deck {
constructor(props: any);
[key: string]: any;
}

export declare class DeckController {
constructor(ControllerState: any, options?: any);
}

export declare class DeckLayer {
constructor(props?: any);
props: any;
Expand Down
3 changes: 1 addition & 2 deletions modules/deck.gl/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2021, NVIDIA CORPORATION.
// Copyright (c) 2020-2023, NVIDIA CORPORATION.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -19,4 +19,3 @@ export * from './utils/series-color-utils';

export {Deck as DeckSSR} from './ssr/deck';
export {AnimationLoop as AnimationLoopSSR} from './ssr/animation-loop';
export {default as OrthographicController} from './ssr/orthographic-controller';
31 changes: 26 additions & 5 deletions modules/deck.gl/src/ssr/deck.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021, NVIDIA CORPORATION.
// Copyright (c) 2021-2023, NVIDIA CORPORATION.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -21,8 +21,27 @@ import {createGLContext} from '@luma.gl/gltools';
import {AnimationLoop} from './animation-loop';

export class Deck extends (BaseDeck as typeof DeckGL) {
constructor(props: any) {
super(props);
const {
Controller,
MapController,
_GlobeController,
FirstPersonController,
OrbitController,
OrthographicController,
} = require('@deck.gl/core');

Controller.prototype._getTransitionProps = () => ({transitionDuration: 0});
MapController.prototype._getTransitionProps = () => ({transitionDuration: 0});
_GlobeController.prototype._getTransitionProps = () => ({transitionDuration: 0});
FirstPersonController.prototype._getTransitionProps = () => ({transitionDuration: 0});
OrbitController.prototype._getTransitionProps = () => ({transitionDuration: 0});
OrthographicController.prototype._getTransitionProps = () => ({transitionDuration: 0});
}
_createAnimationLoop(props: any) {
const {
_sync,
width,
height,
gl,
Expand All @@ -40,6 +59,7 @@ export class Deck extends (BaseDeck as typeof DeckGL) {
const getFramebufferFromLoop = createFramebuffer && !props._framebuffer;

const loop = new AnimationLoop({
_sync,
width,
height,
animationProps,
Expand Down Expand Up @@ -79,7 +99,8 @@ export class Deck extends (BaseDeck as typeof DeckGL) {
}

_onInteractionStateChange(interactionState: any) {
return super._onInteractionStateChange(Object.assign(this.interactiveState, interactionState));
return super._onInteractionStateChange(
Object.assign(this._interactionState ||= {}, interactionState));
}

restore(state: any) {
Expand All @@ -88,8 +109,8 @@ export class Deck extends (BaseDeck as typeof DeckGL) {
if (state.metrics) { this.metrics = {...this.metrics, ...state.metrics}; }
if ('_metricsCounter' in state) { this._metricsCounter = state._metricsCounter; }
if (state._pickRequest) { this._pickRequest = {...this._pickRequest, ...state._pickRequest}; }
if (state.interactiveState) {
this.interactiveState = {...this.interactiveState, ...state.interactiveState};
if (state.interactionState) {
this._interactionState = {...this._interactionState, ...state.interactionState};
}
if (state._lastPointerDownInfo) {
this._lastPointerDownInfo = {...this._lastPointerDownInfo, ...state._lastPointerDownInfo};
Expand Down Expand Up @@ -138,7 +159,7 @@ export class Deck extends (BaseDeck as typeof DeckGL) {
metrics: this.metrics ? {...this.metrics} : undefined,
_pickRequest: this._pickRequest ? {...this._pickRequest} : undefined,
animationProps: this.animationLoop ? this.animationLoop.serialize() : undefined,
interactiveState: this.interactiveState ? {...this.interactiveState} : undefined,
interactionState: this._interactionState ? {...this._interactionState} : undefined,
_lastPointerDownInfo: serializePickingInfo(this._lastPointerDownInfo),
deckPicker: this.deckPicker.lastPickedInfo && {
lastPickedInfo: serializeLastPickedInfo(this),
Expand Down
91 changes: 0 additions & 91 deletions modules/deck.gl/src/ssr/orthographic-controller.ts

This file was deleted.

Loading

0 comments on commit c9fddaf

Please sign in to comment.