From 1322fb34f806624b8858c9ce1c8602e355734653 Mon Sep 17 00:00:00 2001 From: bingyu Date: Thu, 23 Jul 2020 12:13:36 -0700 Subject: [PATCH 01/15] mac local changes --- .gitignore | 2 ++ CMakeLists.txt | 6 ++-- CMakeLists_backup.txt | 70 +++++++++++++++++++++++++++++++++++++++++++ interface.py | 5 +++- 4 files changed, 80 insertions(+), 3 deletions(-) create mode 100755 CMakeLists_backup.txt diff --git a/.gitignore b/.gitignore index c4164b7..b791954 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +hello_build +test_2 # Temporary files *~* *#* diff --git a/CMakeLists.txt b/CMakeLists.txt index 931bc7e..4b0e47f 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,7 @@ set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) include_directories(BEFORE ${sp_SOURCE_DIR}/include/ ${sp_SOURCE_DIR}/external/ + /usr/local/include/ ) # sp executable @@ -54,8 +55,9 @@ add_library(lsp SHARED ${sp_src} ${sp_SOURCE_DIR}/src/py.cc) # Unit test if(SP_BUILD_TESTING) SET(test_src - ${sp_SOURCE_DIR}/tests/test_main.cc - ${sp_SOURCE_DIR}/tests/graph_test.cc + #${sp_SOURCE_DIR}/tests/test_main.cc + #${sp_SOURCE_DIR}/tests/graph_test.cc + ${sp_SOURCE_DIR}/test_2/hello.cpp ) add_executable(sptest ${sp_src} ${test_src}) add_test(NAME sptest COMMAND $) diff --git a/CMakeLists_backup.txt b/CMakeLists_backup.txt new file mode 100755 index 0000000..931bc7e --- /dev/null +++ b/CMakeLists_backup.txt @@ -0,0 +1,70 @@ +project(sp LANGUAGES CXX) + +# Require C++14-compliant compiler; only available for CMake v. 3.1 and up +set(CMAKE_CXX_STANDARD 14) + +cmake_minimum_required(VERSION 3.1) + +SET(CMAKE_COLOR_MAKEFILE ON) +SET(CMAKE_VERBOSE_MAKEFILE OFF) + +# General compile settings +IF (NOT CMAKE_BUILD_TYPE) + SET(CMAKE_BUILD_TYPE "Debug") + #SET(CMAKE_BUILD_TYPE "Release") +ENDIF (NOT CMAKE_BUILD_TYPE) + +# GNU specific settings +if (CMAKE_CXX_COMPILER_ID MATCHES "GNU") + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpermissive") +endif() + +# Intel specific settings +if (CMAKE_CXX_COMPILER_ID MATCHES "Intel") + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") +endif() + +# Clang specific settings +if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-undefined-var-template") +endif() + +# CMake seems to have no way to enable/disable testing per subproject, +# so we provide an option similar to BUILD_TESTING, but just for SP. +option(SP_BUILD_TESTING "enable testing for sp" ON) + +# CMake Modules +set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) + +# Include directories +include_directories(BEFORE + ${sp_SOURCE_DIR}/include/ + ${sp_SOURCE_DIR}/external/ +) + +# sp executable +SET(sp_src + ${sp_SOURCE_DIR}/src/graph.cc +) + +add_executable(sp ${sp_src} ${sp_SOURCE_DIR}/src/main.cc) + +add_library(lsp SHARED ${sp_src} ${sp_SOURCE_DIR}/src/py.cc) + +# Unit test +if(SP_BUILD_TESTING) + SET(test_src + ${sp_SOURCE_DIR}/tests/test_main.cc + ${sp_SOURCE_DIR}/tests/graph_test.cc + ) + add_executable(sptest ${sp_src} ${test_src}) + add_test(NAME sptest COMMAND $) + enable_testing() +endif() + +# Coverage +find_package(codecov) +if(ENABLE_COVERAGE) + #add_executable(mpmtest_coverage ${mpm_src} ${test_src}) + add_coverage(sptest) +endif() diff --git a/interface.py b/interface.py index ce5c4dc..e6cdd1b 100644 --- a/interface.py +++ b/interface.py @@ -3,7 +3,10 @@ from sys import argv from ctypes import * -libsp = cdll.LoadLibrary("./build/liblsp.so") +import os +absolute_path = os.path.dirname(os.path.abspath(__file__)) + +libsp = cdll.LoadLibrary(absolute_path+"/build/liblsp.dylib") libsp.distance.restype = c_double class ShortestPath(Structure): From 564ff4cf0ee509acf326535a0b3e107be032c97a Mon Sep 17 00:00:00 2001 From: bingyu Date: Thu, 6 Aug 2020 15:52:27 -0700 Subject: [PATCH 02/15] create graph from dataframe --- CMakeLists.txt | 6 ++---- include/graph.h | 12 ++++++++++++ interface.py | 28 +++++++++++++++++++++++++++- src/graph.cc | 14 ++++++++++++++ src/py.cc | 16 ++++++++++++++++ 5 files changed, 71 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b0e47f..931bc7e 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,7 +40,6 @@ set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) include_directories(BEFORE ${sp_SOURCE_DIR}/include/ ${sp_SOURCE_DIR}/external/ - /usr/local/include/ ) # sp executable @@ -55,9 +54,8 @@ add_library(lsp SHARED ${sp_src} ${sp_SOURCE_DIR}/src/py.cc) # Unit test if(SP_BUILD_TESTING) SET(test_src - #${sp_SOURCE_DIR}/tests/test_main.cc - #${sp_SOURCE_DIR}/tests/graph_test.cc - ${sp_SOURCE_DIR}/test_2/hello.cpp + ${sp_SOURCE_DIR}/tests/test_main.cc + ${sp_SOURCE_DIR}/tests/graph_test.cc ) add_executable(sptest ${sp_src} ${test_src}) add_test(NAME sptest COMMAND $) diff --git a/include/graph.h b/include/graph.h index 8db4498..42496ce 100644 --- a/include/graph.h +++ b/include/graph.h @@ -89,6 +89,18 @@ class Graph { //! Generate a simple graph void generate_simple_graph(); + //! Create a graph based on edges from Pandas DataFrame + //! \param[in] name of the dataframe column containing edge start nodes + //! \param[in] name of the dataframe column containing edge end nodes + //! \param[in] name of the dataframe column containing edge weights + //! \param[in] numbers of vertices + //! \retval graph Pointer to a graph object + void generate_graph( + int * edge_starts, + int * edge_ends, + double * edge_wghs, + int nvertices_input); + //! Read MatrixMarket graph file format //! \param[in] filename Name of input MatrixMarket file //! \retval status File read status diff --git a/interface.py b/interface.py index b1300a0..767e809 100644 --- a/interface.py +++ b/interface.py @@ -2,6 +2,9 @@ from sys import argv from ctypes import * +from numpy.ctypeslib import ndpointer +import pandas as pd +import numpy as np import os absolute_path = os.path.dirname(os.path.abspath(__file__)) @@ -40,6 +43,12 @@ def writegraph(self, filename): libsp.simplegraph.restype = POINTER(Graph) libsp.readgraph.restype = POINTER(Graph) +libsp.creategraph.restype = POINTER(Graph) +libsp.creategraph.argtypes = [ + ndpointer(c_int32, flags='C_CONTIGUOUS'), + ndpointer(c_int32, flags='C_CONTIGUOUS'), + ndpointer(c_double, flags='C_CONTIGUOUS'), + c_int, c_bool] def simplegraph(directed=True): return libsp.simplegraph(directed).contents @@ -48,6 +57,13 @@ def simplegraph(directed=True): def readgraph(filename, directed=True): return libsp.readgraph(filename, directed).contents +def from_dataframe(edges=None, start_node_col=None, end_node_col=None, weight_col=None, directed=True): + return libsp.creategraph( + edges[start_node_col].values.astype(np.int32), + edges[end_node_col].values.astype(np.int32), + edges[weight_col].values, + edges.shape[0], directed).contents + def test(): g = simplegraph() @@ -61,6 +77,16 @@ def test(): print(destination, sp.distance(destination)) print( " -> ".join("%s"%vertex[1] for vertex in sp.route(destination)) ) + sp.clear() + +def test_df(): + df = pd.DataFrame({'start':[0,1,2,3,4,5,6,7], 'end':[1,2,3,4,5,6,7,0], 'wgh':[0.1,0.5,1.9,1.1,1.2,1.5,1.6,1.9]}) + g = from_dataframe(df, 'start', 'end', 'wgh') + sp = g.dijkstra(1,2) + print(sp.origin) + print( " -> ".join("%s"%vertex[1] for vertex in sp.route(2)) ) + print(sp.distance(2)) + sp.clear() if __name__ == '__main__': - test() + test_df() diff --git a/src/graph.cc b/src/graph.cc index 2e8483a..41cec7b 100644 --- a/src/graph.cc +++ b/src/graph.cc @@ -134,6 +134,20 @@ void Graph::generate_simple_graph() { this->add_edge(5, 6, 9.7); } +// Create graph based on edges from Pandas DataFrame +void Graph::generate_graph(int * edge_starts, int * edge_ends, double * edge_wghs, int nvertices_input) { + // std::cout << nvertices << std::endl; + this->assign_nvertices(nvertices_input + 1); + + for (unsigned int e=0; eadd_edge(v1, v2, weight); + } +} + // Dijktra shortest paths from src to all other vertices ShortestPath Graph::dijkstra_priority_queue(vertex_t source, vertex_t destination) { diff --git a/src/py.cc b/src/py.cc index 1ecf542..541a8bf 100644 --- a/src/py.cc +++ b/src/py.cc @@ -24,6 +24,22 @@ Graph* readgraph(char* filename, bool directed) { return graph; } +//! Create a graph based on edges from Pandas DataFrame +//! \param[in] name of the dataframe column containing edge start nodes +//! \param[in] name of the dataframe column containing edge end nodes +//! \param[in] name of the dataframe column containing edge weights +//! \param[in] numbers of vertices +//! \retval graph Pointer to a graph object +Graph* creategraph(int * edge_starts, + int * edge_ends, + double * edge_wghs, + int nvertices, + bool directed) { + Graph* graph = new Graph(directed); + graph->generate_graph(edge_starts, edge_ends, edge_wghs, nvertices); + return graph; +} + //! Write graph as MatrixMarket file //! \param[in] filename Graph filename //! \param[in] graph Graph object From ca086135b1c06c80385ff30d5d41fdd7b7b75ee7 Mon Sep 17 00:00:00 2001 From: bingyu Date: Sun, 9 Aug 2020 22:51:00 -0700 Subject: [PATCH 03/15] calculating graph edge and node counts from input edge_df --- CMakeLists_backup.txt | 70 ------------------------------------------- include/graph.h | 6 ++-- interface.py | 33 +++++++++++++++----- src/graph.cc | 6 ++-- src/py.cc | 6 ++-- 5 files changed, 37 insertions(+), 84 deletions(-) delete mode 100755 CMakeLists_backup.txt diff --git a/CMakeLists_backup.txt b/CMakeLists_backup.txt deleted file mode 100755 index 931bc7e..0000000 --- a/CMakeLists_backup.txt +++ /dev/null @@ -1,70 +0,0 @@ -project(sp LANGUAGES CXX) - -# Require C++14-compliant compiler; only available for CMake v. 3.1 and up -set(CMAKE_CXX_STANDARD 14) - -cmake_minimum_required(VERSION 3.1) - -SET(CMAKE_COLOR_MAKEFILE ON) -SET(CMAKE_VERBOSE_MAKEFILE OFF) - -# General compile settings -IF (NOT CMAKE_BUILD_TYPE) - SET(CMAKE_BUILD_TYPE "Debug") - #SET(CMAKE_BUILD_TYPE "Release") -ENDIF (NOT CMAKE_BUILD_TYPE) - -# GNU specific settings -if (CMAKE_CXX_COMPILER_ID MATCHES "GNU") - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpermissive") -endif() - -# Intel specific settings -if (CMAKE_CXX_COMPILER_ID MATCHES "Intel") - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") -endif() - -# Clang specific settings -if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-undefined-var-template") -endif() - -# CMake seems to have no way to enable/disable testing per subproject, -# so we provide an option similar to BUILD_TESTING, but just for SP. -option(SP_BUILD_TESTING "enable testing for sp" ON) - -# CMake Modules -set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) - -# Include directories -include_directories(BEFORE - ${sp_SOURCE_DIR}/include/ - ${sp_SOURCE_DIR}/external/ -) - -# sp executable -SET(sp_src - ${sp_SOURCE_DIR}/src/graph.cc -) - -add_executable(sp ${sp_src} ${sp_SOURCE_DIR}/src/main.cc) - -add_library(lsp SHARED ${sp_src} ${sp_SOURCE_DIR}/src/py.cc) - -# Unit test -if(SP_BUILD_TESTING) - SET(test_src - ${sp_SOURCE_DIR}/tests/test_main.cc - ${sp_SOURCE_DIR}/tests/graph_test.cc - ) - add_executable(sptest ${sp_src} ${test_src}) - add_test(NAME sptest COMMAND $) - enable_testing() -endif() - -# Coverage -find_package(codecov) -if(ENABLE_COVERAGE) - #add_executable(mpmtest_coverage ${mpm_src} ${test_src}) - add_coverage(sptest) -endif() diff --git a/include/graph.h b/include/graph.h index 42496ce..883da33 100644 --- a/include/graph.h +++ b/include/graph.h @@ -93,13 +93,15 @@ class Graph { //! \param[in] name of the dataframe column containing edge start nodes //! \param[in] name of the dataframe column containing edge end nodes //! \param[in] name of the dataframe column containing edge weights - //! \param[in] numbers of vertices + //! \param[in] numbers of edges (number of rows in the input edges dataframe) + //! \param[in] numbers of vertices (maximum of start and end node ids in the edges dataframe) //! \retval graph Pointer to a graph object void generate_graph( int * edge_starts, int * edge_ends, double * edge_wghs, - int nvertices_input); + int nedges, + int nvertices); //! Read MatrixMarket graph file format //! \param[in] filename Name of input MatrixMarket file diff --git a/interface.py b/interface.py index 767e809..fe6a4b5 100644 --- a/interface.py +++ b/interface.py @@ -48,21 +48,23 @@ def writegraph(self, filename): ndpointer(c_int32, flags='C_CONTIGUOUS'), ndpointer(c_int32, flags='C_CONTIGUOUS'), ndpointer(c_double, flags='C_CONTIGUOUS'), - c_int, c_bool] + c_int, c_int, c_bool] def simplegraph(directed=True): return libsp.simplegraph(directed).contents - def readgraph(filename, directed=True): return libsp.readgraph(filename, directed).contents def from_dataframe(edges=None, start_node_col=None, end_node_col=None, weight_col=None, directed=True): + # print(np.max(edges[[start_node_col, end_node_col]].values)) return libsp.creategraph( edges[start_node_col].values.astype(np.int32), edges[end_node_col].values.astype(np.int32), edges[weight_col].values, - edges.shape[0], directed).contents + edges.shape[0], ### the number of edges is computed from the edges dataframe, not an input to the python interface + np.max(edges[[start_node_col, end_node_col]].values), ### number of vertices is the maximum of the start or end node id in the edges dataframe + directed).contents def test(): @@ -80,12 +82,29 @@ def test(): sp.clear() def test_df(): + print("test 1") df = pd.DataFrame({'start':[0,1,2,3,4,5,6,7], 'end':[1,2,3,4,5,6,7,0], 'wgh':[0.1,0.5,1.9,1.1,1.2,1.5,1.6,1.9]}) g = from_dataframe(df, 'start', 'end', 'wgh') - sp = g.dijkstra(1,2) - print(sp.origin) - print( " -> ".join("%s"%vertex[1] for vertex in sp.route(2)) ) - print(sp.distance(2)) + + origin, destin = 1, 5 + sp = g.dijkstra(origin, destin) + + print("origin is {}, destination is {}".format(sp.origin, destin)) + print("path is {} --> ".format(sp.origin), " -> ".join("%s"%vertex[1] for vertex in sp.route(destin)) ) + print("distance is ", sp.distance(destin)) + sp.clear() + + print("\ntest 2") + df = pd.DataFrame({'start':[0,2,4,10,12], 'end':[1,3,5,12,0], 'wgh':[0.1,0.5,1.9,1.1,1.2]}) + # df = pd.DataFrame({'start':[0,1,2,3,4], 'end':[1,2,3,4,0], 'wgh':[0.1,0.5,1.9,1.1,1.2]}) + g = from_dataframe(df, 'start', 'end', 'wgh') + + origin, destin = 10,1 #0,4 + sp = g.dijkstra(origin, destin) + + print("origin is {}, destination is {}".format(sp.origin, destin)) + print("path is {} --> ".format(sp.origin), " -> ".join("%s"%vertex[1] for vertex in sp.route(destin)) ) + print("distance is ", sp.distance(destin)) sp.clear() if __name__ == '__main__': diff --git a/src/graph.cc b/src/graph.cc index 41cec7b..6f52ddf 100644 --- a/src/graph.cc +++ b/src/graph.cc @@ -135,11 +135,11 @@ void Graph::generate_simple_graph() { } // Create graph based on edges from Pandas DataFrame -void Graph::generate_graph(int * edge_starts, int * edge_ends, double * edge_wghs, int nvertices_input) { +void Graph::generate_graph(int * edge_starts, int * edge_ends, double * edge_wghs, int nedges, int nvertices) { // std::cout << nvertices << std::endl; - this->assign_nvertices(nvertices_input + 1); + this->assign_nvertices(nvertices + 1); - for (unsigned int e=0; egenerate_graph(edge_starts, edge_ends, edge_wghs, nvertices); + graph->generate_graph(edge_starts, edge_ends, edge_wghs, nedges, nvertices); return graph; } From 958af46b6f872acbc3e0e9667d7804b753da3dc1 Mon Sep 17 00:00:00 2001 From: bingyu Date: Tue, 11 Aug 2020 08:16:16 -0700 Subject: [PATCH 04/15] clang-format -i src/* --- src/graph.cc | 7 ++++--- src/py.cc | 12 ++++-------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/graph.cc b/src/graph.cc index 6f52ddf..46f2714 100644 --- a/src/graph.cc +++ b/src/graph.cc @@ -135,11 +135,12 @@ void Graph::generate_simple_graph() { } // Create graph based on edges from Pandas DataFrame -void Graph::generate_graph(int * edge_starts, int * edge_ends, double * edge_wghs, int nedges, int nvertices) { +void Graph::generate_graph(int* edge_starts, int* edge_ends, double* edge_wghs, + int nedges, int nvertices) { // std::cout << nvertices << std::endl; this->assign_nvertices(nvertices + 1); - - for (unsigned int e=0; egenerate_graph(edge_starts, edge_ends, edge_wghs, nedges, nvertices); return graph; From 5b82ac359776bf1eabc3b877dd8ae36e5f80a65e Mon Sep 17 00:00:00 2001 From: bingyu Date: Tue, 11 Aug 2020 08:21:07 -0700 Subject: [PATCH 05/15] clang-format -i include/* --- include/graph.h | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/include/graph.h b/include/graph.h index 883da33..c8a1f76 100644 --- a/include/graph.h +++ b/include/graph.h @@ -94,14 +94,10 @@ class Graph { //! \param[in] name of the dataframe column containing edge end nodes //! \param[in] name of the dataframe column containing edge weights //! \param[in] numbers of edges (number of rows in the input edges dataframe) - //! \param[in] numbers of vertices (maximum of start and end node ids in the edges dataframe) - //! \retval graph Pointer to a graph object - void generate_graph( - int * edge_starts, - int * edge_ends, - double * edge_wghs, - int nedges, - int nvertices); + //! \param[in] numbers of vertices (maximum of start and end node ids in the + //! edges dataframe) \retval graph Pointer to a graph object + void generate_graph(int* edge_starts, int* edge_ends, double* edge_wghs, + int nedges, int nvertices); //! Read MatrixMarket graph file format //! \param[in] filename Name of input MatrixMarket file From 4b2ccb91a524a9362c605c1e9c36fda5b56062e3 Mon Sep 17 00:00:00 2001 From: bz247 Date: Wed, 12 Aug 2020 15:25:39 -0700 Subject: [PATCH 06/15] Update function input annotation for `creategraph` Co-authored-by: Krishna Kumar <3963513+kks32@users.noreply.github.com> --- src/py.cc | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/py.cc b/src/py.cc index 2db63d9..0ea20a2 100644 --- a/src/py.cc +++ b/src/py.cc @@ -25,12 +25,14 @@ Graph* readgraph(char* filename, bool directed) { } //! Create a graph based on edges from Pandas DataFrame -//! \param[in] name of the dataframe column containing edge start nodes -//! \param[in] name of the dataframe column containing edge end nodes -//! \param[in] name of the dataframe column containing edge weights -//! \param[in] numbers of edges (number of rows in the edges dataframe) -//! \param[in] numbers of vertices (maximum of start and end node ids in the -//! edges dataframe) \retval graph Pointer to a graph object +//! \param[in] edge_starts name of the dataframe column containing edge start nodes +//! \param[in] edge_ends name of the dataframe column containing edge end nodes +//! \param[in] edge_wghs name of the dataframe column containing edge weights +//! \param[in] nedges numbers of edges (number of rows in the edges dataframe) +//! \param[in] nvertices numbers of vertices (maximum of start and end node ids in the +//! edges dataframe) +//! \param[in] directed Boolean to determine if a given graph is directed or not +//! \retval graph Pointer to a graph object Graph* creategraph(int* edge_starts, int* edge_ends, double* edge_wghs, int nedges, int nvertices, bool directed) { Graph* graph = new Graph(directed); From e06c226173030e1645ea1c1d83b645b3753fa444 Mon Sep 17 00:00:00 2001 From: bingyu Date: Wed, 12 Aug 2020 15:30:38 -0700 Subject: [PATCH 07/15] update function description for in --- include/graph.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/graph.h b/include/graph.h index c8a1f76..96cfb08 100644 --- a/include/graph.h +++ b/include/graph.h @@ -90,12 +90,12 @@ class Graph { void generate_simple_graph(); //! Create a graph based on edges from Pandas DataFrame - //! \param[in] name of the dataframe column containing edge start nodes - //! \param[in] name of the dataframe column containing edge end nodes - //! \param[in] name of the dataframe column containing edge weights - //! \param[in] numbers of edges (number of rows in the input edges dataframe) - //! \param[in] numbers of vertices (maximum of start and end node ids in the - //! edges dataframe) \retval graph Pointer to a graph object + //! \param[in] edge_starts name of the dataframe column containing edge start nodes + //! \param[in] edge_ends name of the dataframe column containing edge end nodes + //! \param[in] edge_wghs name of the dataframe column containing edge weights + //! \param[in] nedges numbers of edges (number of rows in the edges dataframe) + //! \param[in] nvertices numbers of vertices (maximum of start and end node ids in the + //! edges dataframe) void generate_graph(int* edge_starts, int* edge_ends, double* edge_wghs, int nedges, int nvertices); From 66c9d2c4a9331d3bd8c1dad3dd2c576672707a19 Mon Sep 17 00:00:00 2001 From: bingyu Date: Wed, 19 Aug 2020 18:32:08 -0700 Subject: [PATCH 08/15] clang-format --- include/.clang-format | 47 ------------------------------------------- 1 file changed, 47 deletions(-) delete mode 100644 include/.clang-format diff --git a/include/.clang-format b/include/.clang-format deleted file mode 100644 index 7605de0..0000000 --- a/include/.clang-format +++ /dev/null @@ -1,47 +0,0 @@ ---- -# BasedOnStyle: CUED GeoMechanics -AccessModifierOffset: -1 -ConstructorInitializerIndentWidth: 4 -AlignEscapedNewlinesLeft: true -AlignTrailingComments: true -AllowAllParametersOfDeclarationOnNextLine: true -AllowShortIfStatementsOnASingleLine: true -AllowShortLoopsOnASingleLine: true -AlwaysBreakTemplateDeclarations: true -AlwaysBreakBeforeMultilineStrings: true -BreakBeforeBinaryOperators: false -BreakBeforeTernaryOperators: true -BreakConstructorInitializersBeforeComma: false -BinPackParameters: true -ColumnLimit: 80 -ConstructorInitializerAllOnOneLineOrOnePerLine: true -DerivePointerBinding: false -ExperimentalAutoDetectBinPacking: false -IndentCaseLabels: true -MaxEmptyLinesToKeep: 1 -NamespaceIndentation: None -ObjCSpaceBeforeProtocolList: false -PenaltyBreakBeforeFirstCallParameter: 1 -PenaltyBreakComment: 60 -PenaltyBreakString: 1000 -PenaltyBreakFirstLessLess: 120 -PenaltyExcessCharacter: 1000000 -PenaltyReturnTypeOnItsOwnLine: 200 -PointerBindsToType: true -SpacesBeforeTrailingComments: 2 -Cpp11BracedListStyle: true -Standard: Auto -IndentWidth: 2 -TabWidth: 8 -UseTab: Never -BreakBeforeBraces: Attach -IndentFunctionDeclarationAfterType: true -SpacesInParentheses: false -SpacesInAngles: false -SpaceInEmptyParentheses: false -SpacesInCStyleCastParentheses: false -SpaceAfterControlStatementKeyword: true -SpaceBeforeAssignmentOperators: true -ContinuationIndentWidth: 4 -... - From 6cda7e758ecb66b96e6723c1da6a089f3eb74077 Mon Sep 17 00:00:00 2001 From: bingyu Date: Wed, 19 Aug 2020 18:37:37 -0700 Subject: [PATCH 09/15] clang-format --- include/graph.h | 27 ++++++++++++++------------- include/unordered_map_tuple_hash.h | 21 +++++++++------------ src/py.cc | 16 ++++++++-------- 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/include/graph.h b/include/graph.h index 96cfb08..a626a44 100644 --- a/include/graph.h +++ b/include/graph.h @@ -34,7 +34,8 @@ struct ShortestPath { // Iterate until source has been reached while (destination != source) { destination = parent.at(destination); - if (destination != source) path.emplace_back(destination); + if (destination != source) + path.emplace_back(destination); } // Reverse to arrange from source to destination std::reverse(path.begin(), path.end()); @@ -54,7 +55,7 @@ struct ShortestPath { //! \brief Graph class to store vertices and edge and compute shortest path //! \details Graph class has Priority Queue Dijkstra algorithm for SSSP class Graph { - public: +public: //! Vertex id type using vertex_t = int; //! Weight type, that can be added with + @@ -90,23 +91,23 @@ class Graph { void generate_simple_graph(); //! Create a graph based on edges from Pandas DataFrame - //! \param[in] edge_starts name of the dataframe column containing edge start nodes - //! \param[in] edge_ends name of the dataframe column containing edge end nodes - //! \param[in] edge_wghs name of the dataframe column containing edge weights - //! \param[in] nedges numbers of edges (number of rows in the edges dataframe) - //! \param[in] nvertices numbers of vertices (maximum of start and end node ids in the - //! edges dataframe) - void generate_graph(int* edge_starts, int* edge_ends, double* edge_wghs, + //! \param[in] edge_starts name of the dataframe column containing edge start + //! nodes \param[in] edge_ends name of the dataframe column containing edge + //! end nodes \param[in] edge_wghs name of the dataframe column containing + //! edge weights \param[in] nedges numbers of edges (number of rows in the + //! edges dataframe) \param[in] nvertices numbers of vertices (maximum of + //! start and end node ids in the edges dataframe) + void generate_graph(int *edge_starts, int *edge_ends, double *edge_wghs, int nedges, int nvertices); //! Read MatrixMarket graph file format //! \param[in] filename Name of input MatrixMarket file //! \retval status File read status - bool read_graph_matrix_market(const std::string& filename); + bool read_graph_matrix_market(const std::string &filename); //! Write MatrixMarket graph file format //! \param[in] filename Name of output MatrixMarket file - bool write_graph_matrix_market(const std::string& filename); + bool write_graph_matrix_market(const std::string &filename); //! Compute the shortest path using priority queue //! \param[in] source ID of source vertex1 @@ -115,7 +116,7 @@ class Graph { ShortestPath dijkstra_priority_queue(vertex_t source, vertex_t destination = -1); - private: +private: //! Assign number of vertices //! \param[in] nvertices Number of vertices in graph void assign_nvertices(unsigned nvertices) { this->nvertices_ = nvertices; } @@ -130,4 +131,4 @@ class Graph { tsl::robin_map>> vertex_edges_; }; -#endif // _GRAPH_H_ +#endif // _GRAPH_H_ diff --git a/include/unordered_map_tuple_hash.h b/include/unordered_map_tuple_hash.h index 080e48e..b4b05cc 100644 --- a/include/unordered_map_tuple_hash.h +++ b/include/unordered_map_tuple_hash.h @@ -14,36 +14,33 @@ namespace { // See Mike Seymour in magic-numbers-in-boosthash-combine: // https://stackoverflow.com/questions/4948780 -template -inline void hash_combine(std::size_t& seed, T const& v) { +template inline void hash_combine(std::size_t &seed, T const &v) { seed ^= hash()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } // Recursive template code derived from Matthieu M. template ::value - 1> struct HashValueImpl { - static void apply(size_t& seed, Tuple const& tuple) { + static void apply(size_t &seed, Tuple const &tuple) { HashValueImpl::apply(seed, tuple); hash_combine(seed, get(tuple)); } }; -template -struct HashValueImpl { - static void apply(size_t& seed, Tuple const& tuple) { +template struct HashValueImpl { + static void apply(size_t &seed, Tuple const &tuple) { hash_combine(seed, get<0>(tuple)); } }; -} // namespace +} // namespace -template -struct hash> { - size_t operator()(std::tuple const& tt) const { +template struct hash> { + size_t operator()(std::tuple const &tt) const { size_t seed = 0; HashValueImpl>::apply(seed, tt); return seed; } }; -} // namespace std +} // namespace std -#endif // _UNORDERED_MAP_TUPLE_HASH_H_ +#endif // _UNORDERED_MAP_TUPLE_HASH_H_ diff --git a/src/py.cc b/src/py.cc index 0ea20a2..623e142 100644 --- a/src/py.cc +++ b/src/py.cc @@ -25,14 +25,14 @@ Graph* readgraph(char* filename, bool directed) { } //! Create a graph based on edges from Pandas DataFrame -//! \param[in] edge_starts name of the dataframe column containing edge start nodes -//! \param[in] edge_ends name of the dataframe column containing edge end nodes -//! \param[in] edge_wghs name of the dataframe column containing edge weights -//! \param[in] nedges numbers of edges (number of rows in the edges dataframe) -//! \param[in] nvertices numbers of vertices (maximum of start and end node ids in the -//! edges dataframe) -//! \param[in] directed Boolean to determine if a given graph is directed or not -//! \retval graph Pointer to a graph object +//! \param[in] edge_starts name of the dataframe column containing edge start +//! nodes \param[in] edge_ends name of the dataframe column containing edge end +//! nodes \param[in] edge_wghs name of the dataframe column containing edge +//! weights \param[in] nedges numbers of edges (number of rows in the edges +//! dataframe) \param[in] nvertices numbers of vertices (maximum of start and +//! end node ids in the edges dataframe) \param[in] directed Boolean to +//! determine if a given graph is directed or not \retval graph Pointer to a +//! graph object Graph* creategraph(int* edge_starts, int* edge_ends, double* edge_wghs, int nedges, int nvertices, bool directed) { Graph* graph = new Graph(directed); From 07351e761ae55f2434dc26095ca7805331bb20c4 Mon Sep 17 00:00:00 2001 From: bingyu Date: Thu, 27 Aug 2020 22:23:11 -0700 Subject: [PATCH 10/15] bring back clang-format --- include/.clang-format | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 include/.clang-format diff --git a/include/.clang-format b/include/.clang-format new file mode 100644 index 0000000..7605de0 --- /dev/null +++ b/include/.clang-format @@ -0,0 +1,47 @@ +--- +# BasedOnStyle: CUED GeoMechanics +AccessModifierOffset: -1 +ConstructorInitializerIndentWidth: 4 +AlignEscapedNewlinesLeft: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortIfStatementsOnASingleLine: true +AllowShortLoopsOnASingleLine: true +AlwaysBreakTemplateDeclarations: true +AlwaysBreakBeforeMultilineStrings: true +BreakBeforeBinaryOperators: false +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BinPackParameters: true +ColumnLimit: 80 +ConstructorInitializerAllOnOneLineOrOnePerLine: true +DerivePointerBinding: false +ExperimentalAutoDetectBinPacking: false +IndentCaseLabels: true +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCSpaceBeforeProtocolList: false +PenaltyBreakBeforeFirstCallParameter: 1 +PenaltyBreakComment: 60 +PenaltyBreakString: 1000 +PenaltyBreakFirstLessLess: 120 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 200 +PointerBindsToType: true +SpacesBeforeTrailingComments: 2 +Cpp11BracedListStyle: true +Standard: Auto +IndentWidth: 2 +TabWidth: 8 +UseTab: Never +BreakBeforeBraces: Attach +IndentFunctionDeclarationAfterType: true +SpacesInParentheses: false +SpacesInAngles: false +SpaceInEmptyParentheses: false +SpacesInCStyleCastParentheses: false +SpaceAfterControlStatementKeyword: true +SpaceBeforeAssignmentOperators: true +ContinuationIndentWidth: 4 +... + From b094499aab2db7cce3fd449ee1dc88536bab47cf Mon Sep 17 00:00:00 2001 From: bz247 Date: Fri, 9 Apr 2021 23:46:58 -0700 Subject: [PATCH 11/15] change shared object extension to so --- interface.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/interface.py b/interface.py index fe6a4b5..7ea5812 100644 --- a/interface.py +++ b/interface.py @@ -1,7 +1,7 @@ from __future__ import print_function from sys import argv -from ctypes import * +from ctypes import c_bool, c_int, c_int32, c_double, cdll, byref, Structure, POINTER from numpy.ctypeslib import ndpointer import pandas as pd import numpy as np @@ -9,7 +9,7 @@ import os absolute_path = os.path.dirname(os.path.abspath(__file__)) -libsp = cdll.LoadLibrary(absolute_path+"/build/liblsp.dylib") +libsp = cdll.LoadLibrary(absolute_path+"/build/liblsp.so") libsp.distance.restype = c_double class ShortestPath(Structure): @@ -70,7 +70,7 @@ def from_dataframe(edges=None, start_node_col=None, end_node_col=None, weight_co def test(): g = simplegraph() #g = readgraph(b"../sf.mtx") - res = g.update_edge(1, 3, c_double(0.5)) + g.update_edge(1, 3, c_double(0.5)) sp = g.dijkstra(1, -1) print("origin:", sp.origin) From 690cded0e8c99aeae8420e22d7f63f93db04b0af Mon Sep 17 00:00:00 2001 From: abhinavDhulipala Date: Mon, 7 Feb 2022 12:26:34 -0800 Subject: [PATCH 12/15] add case for arm64 --- external/catch.hpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/external/catch.hpp b/external/catch.hpp index 6b5129d..af1073f 100644 --- a/external/catch.hpp +++ b/external/catch.hpp @@ -2130,7 +2130,11 @@ namespace Catch{ __asm__("li r0, 20\nsc\nnop\nli r0, 37\nli r4, 2\nsc\nnop\n" \ : : : "memory","r0","r3","r4" ) /* NOLINT */ #else - #define CATCH_TRAP() __asm__("int $3\n" : : /* NOLINT */ ) + #if defined(__i386__) || defined(__x86_64__) + #define CATCH_TRAP() __asm__("int $3\n" : : ) /* NOLINT */ + #elif defined(__aarch64__) + #define CATCH_TRAP() __asm__(".inst 0xd4200000") + #endif #endif #elif defined(CATCH_PLATFORM_LINUX) From 3287ea0e73e3a1dee23fb8bc73d6bb88ab5682f7 Mon Sep 17 00:00:00 2001 From: abhinavDhulipala <46908860+abhinavDhulipala@users.noreply.github.com> Date: Mon, 7 Feb 2022 12:29:40 -0800 Subject: [PATCH 13/15] Delete requirements.txt accidental commit --- requirements.txt | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index fb582bd..0000000 --- a/requirements.txt +++ /dev/null @@ -1,19 +0,0 @@ -backcall==0.1.0 -decorator==4.3.0 -ipython==7.16.3 -ipython-genutils==0.2.0 -jedi==0.12.0 -jgraph==0.2.1 -numpy==1.14.5 -parso==0.2.1 -pexpect==4.6.0 -pickleshare==0.7.4 -prompt-toolkit==1.0.15 -ptyprocess==0.5.2 -Pygments==2.7.4 -python-igraph==0.7.1.post6 -scipy==1.1.0 -simplegeneric==0.8.1 -six==1.11.0 -traitlets==4.3.2 -wcwidth==0.1.7 From 271e2b43bd7846d3042802a6029643a88a262ffe Mon Sep 17 00:00:00 2001 From: abhinavDhulipala Date: Mon, 7 Feb 2022 12:32:57 -0800 Subject: [PATCH 14/15] restore requirements.txt --- requirements.txt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fb582bd --- /dev/null +++ b/requirements.txt @@ -0,0 +1,19 @@ +backcall==0.1.0 +decorator==4.3.0 +ipython==7.16.3 +ipython-genutils==0.2.0 +jedi==0.12.0 +jgraph==0.2.1 +numpy==1.14.5 +parso==0.2.1 +pexpect==4.6.0 +pickleshare==0.7.4 +prompt-toolkit==1.0.15 +ptyprocess==0.5.2 +Pygments==2.7.4 +python-igraph==0.7.1.post6 +scipy==1.1.0 +simplegeneric==0.8.1 +six==1.11.0 +traitlets==4.3.2 +wcwidth==0.1.7 From a6daf291a3f4b6712abbad69e03fc00f5291736d Mon Sep 17 00:00:00 2001 From: Bingyu Zhao Date: Sat, 3 Jan 2026 14:09:25 +0100 Subject: [PATCH 15/15] update cmake_minimum_required --- CMakeLists.txt | 6 ++++-- interface.py | 14 +++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 931bc7e..47c6fc0 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,11 @@ +cmake_minimum_required(VERSION 3.15...3.29) + project(sp LANGUAGES CXX) # Require C++14-compliant compiler; only available for CMake v. 3.1 and up set(CMAKE_CXX_STANDARD 14) - -cmake_minimum_required(VERSION 3.1) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) SET(CMAKE_COLOR_MAKEFILE ON) SET(CMAKE_VERBOSE_MAKEFILE OFF) diff --git a/interface.py b/interface.py index 7ea5812..8bc23f9 100644 --- a/interface.py +++ b/interface.py @@ -1,6 +1,8 @@ from __future__ import print_function +import sys from sys import argv +from pathlib import Path from ctypes import c_bool, c_int, c_int32, c_double, cdll, byref, Structure, POINTER from numpy.ctypeslib import ndpointer import pandas as pd @@ -9,7 +11,17 @@ import os absolute_path = os.path.dirname(os.path.abspath(__file__)) -libsp = cdll.LoadLibrary(absolute_path+"/build/liblsp.so") +root = Path(absolute_path) / "build" + +if sys.platform == "darwin": + ext = ".dylib" +elif sys.platform.startswith("win"): + ext = ".dll" +else: + ext = ".so" + +libsp = cdll.LoadLibrary(str(root / f"liblsp{ext}")) + libsp.distance.restype = c_double class ShortestPath(Structure):