Skip to content

[FIX] make GraphCOO numNodes and numEdges functions instead of getters #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions modules/cugraph/src/addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

/* eslint-disable @typescript-eslint/no-redeclare */

import {addon as CUDA} from '@nvidia/cuda';
import {loadNativeModule} from '@rapidsai/core';
import {addon as CUDF} from '@rapidsai/cudf';
import {addon as RMM} from '@rapidsai/rmm';
import {GraphCOOConstructor} from './graph_coo';

export const {GraphCOO} = loadNativeModule<{
GraphCOO: GraphCOOConstructor, //
}>(module, 'node_cugraph', init => init(CUDA, CUDF, RMM));
export const {GraphCOO, _cpp_exports} = loadNativeModule<typeof import('./node_cugraph')>(
module, 'node_cugraph', init => init(CUDA, CUDF, RMM));

export type GraphCOO = import('./node_cugraph').GraphCOO;
30 changes: 19 additions & 11 deletions modules/cugraph/src/graph_coo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@

#include <node_cugraph/graph_coo.hpp>

#include <node_cudf/utilities/dtypes.hpp>

#include <node_cuda/utilities/error.hpp>
#include <node_cuda/utilities/napi_to_cpp.hpp>

#include <cudf/reduction.hpp>
#include <cudf/types.hpp>

#include <napi.h>
Expand All @@ -27,8 +30,8 @@ Napi::Function GraphCOO::Init(Napi::Env const& env, Napi::Object exports) {
return DefineClass(env,
"GraphCOO",
{
InstanceAccessor<&GraphCOO::num_edges>("numEdges"),
InstanceAccessor<&GraphCOO::num_nodes>("numNodes"),
InstanceMethod<&GraphCOO::num_edges>("numEdges"),
InstanceMethod<&GraphCOO::num_nodes>("numNodes"),
InstanceMethod<&GraphCOO::force_atlas2>("forceAtlas2"),
});
}
Expand All @@ -45,8 +48,16 @@ GraphCOO::GraphCOO(CallbackArgs const& args) : EnvLocalObjectWrap<GraphCOO>(args
NODE_CUDA_EXPECT(
Column::IsInstance(args[1]), "GraphCOO requires dst argument to a Column", args.Env());

Column::wrapper_t const src = args[0];
Column::wrapper_t const dst = args[1];
Column::wrapper_t src = args[0];
if (src->type().id() != cudf::type_id::INT32) {
src = src->cast(cudf::data_type{cudf::type_id::INT32});
}

Column::wrapper_t dst = args[1];
if (dst->type().id() != cudf::type_id::INT32) {
dst = dst->cast(cudf::data_type{cudf::type_id::INT32});
}

NapiToCPP::Object const options = args[2];

src_ = Napi::Persistent(src);
Expand All @@ -56,13 +67,10 @@ GraphCOO::GraphCOO(CallbackArgs const& args) : EnvLocalObjectWrap<GraphCOO>(args

size_t GraphCOO::num_nodes() {
if (!node_count_computed_) {
auto const& src = *src_.Value();
auto const& dst = *dst_.Value();
auto const src_max = src.minmax().second;
auto const dst_max = dst.minmax().second;
node_count_ = 1 + std::max<int32_t>( //
src_max->get_value().ToNumber(),
dst_max->get_value().ToNumber());
auto max_id = [&](Napi::Reference<Column::wrapper_t> const& col) -> int32_t {
return col.Value()->minmax().second->get_value().ToNumber();
};
node_count_ = 1 + std::max(max_id(src_), max_id(dst_));
node_count_computed_ = true;
}
return node_count_;
Expand Down
6 changes: 4 additions & 2 deletions modules/cugraph/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020, NVIDIA CORPORATION.
// Copyright (c) 2020-2021, 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,4 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

export * from './graph_coo';
export * as addon from './addon';

export {GraphCOO} from './addon';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020, NVIDIA CORPORATION.
// Copyright (c) 2021, 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,18 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {Column, Int32} from '@rapidsai/cudf';
import {Column, FloatingPoint, Integral} from '@rapidsai/cudf';
import {DeviceBuffer, MemoryResource} from '@rapidsai/rmm';

export interface GraphCOOConstructor {
readonly prototype: GraphCOO;
new(src: Column<Int32>, dst: Column<Int32>, options?: {directedEdges?: boolean}): GraphCOO;
}
/** @ignore */
export declare const _cpp_exports: any;

export declare class GraphCOO {
constructor(src: Column<Integral|FloatingPoint>,
dst: Column<Integral|FloatingPoint>,
options?: {directedEdges?: boolean});

// eslint-disable-next-line @typescript-eslint/no-redeclare
interface GraphCOO {
readonly numEdges: number;
readonly numNodes: number;
numEdges(): number;
numNodes(): number;

forceAtlas2(options: {
memoryResource?: MemoryResource,
Expand All @@ -41,5 +42,3 @@ interface GraphCOO {
verbose?: boolean,
}): DeviceBuffer;
}

export {GraphCOO} from './addon';
4 changes: 2 additions & 2 deletions modules/cugraph/src/node_cugraph/graph_coo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ struct GraphCOO : public EnvLocalObjectWrap<GraphCOO> {
cudf::size_type node_count_{};
bool node_count_computed_{false};

Napi::Reference<Column::wrapper_t> src_{};
Napi::Reference<Column::wrapper_t> dst_{};
Napi::Reference<Column::wrapper_t> src_;
Napi::Reference<Column::wrapper_t> dst_;
};

} // namespace nv
6 changes: 3 additions & 3 deletions modules/demo/graph/src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export default async function* loadGraphData(props = {}) {
}
}

const n = graph.numNodes;
const n = graph.numNodes();

// If new nodes, update existing positions
if (positions && positions.length < (n * 2)) {
Expand Down Expand Up @@ -267,8 +267,8 @@ function promiseSubject() {
* @param {*} graph
*/
function createGraphRenderProps(nodes, edges, graph) {
const numNodes = graph.numNodes;
const numEdges = graph.numEdges;
const numNodes = graph.numNodes();
const numEdges = graph.numEdges();
return {
numNodes, numEdges, nodeRadiusScale: 1 / 75,
// nodeRadiusScale: 1/255,
Expand Down