diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index d35c5c02218..ebffd18ca5a 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1,3 +1,17 @@ +# https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/about-code-owners +# Order matters - match of highest importance goes last (last match wins) + +#doc code owners +datasets/ @rapidsai/cugraph-doc-codeowners +notebooks/ @rapidsai/cugraph-doc-codeowners +docs/ @rapidsai/cugraph-doc-codeowners +**/*.txt @rapidsai/cugraph-doc-codeowners +**/*.md @rapidsai/cugraph-doc-codeowners +**/*.rst @rapidsai/cugraph-doc-codeowners +**/*.ipynb @rapidsai/cugraph-doc-codeowners +**/*.pdf @rapidsai/cugraph-doc-codeowners +**/*.png @rapidsai/cugraph-doc-codeowners + #cpp code owners cpp/ @rapidsai/cugraph-cpp-codeowners @@ -9,7 +23,7 @@ python/ @rapidsai/cugraph-python-codeowners **/cmake/ @rapidsai/cugraph-cmake-codeowners #build/ops code owners -.github/ @rapidsai/ops-codeowners +.github/ @rapidsai/ops-codeowners ci/ @rapidsai/ops-codeowners conda/ @rapidsai/ops-codeowners **/Dockerfile @rapidsai/ops-codeowners diff --git a/.github/labeler.yml b/.github/labeler.yml new file mode 100644 index 00000000000..9c3af6de64b --- /dev/null +++ b/.github/labeler.yml @@ -0,0 +1,37 @@ +# https://github.com/actions/labeler#common-examples +# Adapted from https://github.com/rapidsai/cugraph/blob/main/.github/CODEOWNERS +# Labels culled from https://github.com/rapidsai/cugraph/labels + +python: + - 'python/**' + - 'notebooks/**' + +benchmarks: + - 'benchmarks/**' + +doc: + - 'docs/**' + - '**/*.md' + - 'datasets/**' + - 'notebooks/**' + - '**/*.txt' + - '**/*.rst' + - '**/*.ipynb' + - '**/*.pdf' + - '**/*.png' + +datasets: + - 'datasets/**' + +cuGraph: + - 'cpp/**' + +CMake: + - '**/CMakeLists.txt' + - '**/cmake/**' + +gpuCI: + - 'ci/**' + +conda: + - 'conda/**' diff --git a/.github/workflows/stale.yaml b/.github/workflows/stale.yaml new file mode 100644 index 00000000000..8b65da69aa2 --- /dev/null +++ b/.github/workflows/stale.yaml @@ -0,0 +1,57 @@ +name: Mark inactive issues and pull requests + +on: + schedule: + - cron: "0 * * * *" + +jobs: + mark-inactive-30d: + runs-on: ubuntu-latest + steps: + - name: Mark 30 day inactive issues and pull requests + uses: actions/stale@v3 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + stale-issue-message: > + This issue has been labeled `inactive-30d` due to no recent activity in the past 30 days. + Please close this issue if no further response or action is needed. + Otherwise, please respond with a comment indicating any updates or changes to the original issue and/or confirm this issue still needs to be addressed. + This issue will be labeled `inactive-90d` if there is no activity in the next 60 days. + stale-issue-label: "inactive-30d" + exempt-issue-labels: "0 - Blocked,0 - Backlog,good first issue" + days-before-issue-stale: 30 + days-before-issue-close: -1 + stale-pr-message: > + This PR has been labeled `inactive-30d` due to no recent activity in the past 30 days. + Please close this PR if it is no longer required. + Otherwise, please respond with a comment indicating any updates. + This PR will be labeled `inactive-90d` if there is no activity in the next 60 days. + stale-pr-label: "inactive-30d" + exempt-pr-labels: "0 - Blocked,0 - Backlog,good first issue" + days-before-pr-stale: 30 + days-before-pr-close: -1 + operations-per-run: 50 + mark-inactive-90d: + runs-on: ubuntu-latest + steps: + - name: Mark 90 day inactive issues and pull requests + uses: actions/stale@v3 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + stale-issue-message: > + This issue has been labeled `inactive-90d` due to no recent activity in the past 90 days. + Please close this issue if no further response or action is needed. + Otherwise, please respond with a comment indicating any updates or changes to the original issue and/or confirm this issue still needs to be addressed. + stale-issue-label: "inactive-90d" + exempt-issue-labels: "0 - Blocked,0 - Backlog,good first issue" + days-before-issue-stale: 90 + days-before-issue-close: -1 + stale-pr-message: > + This PR has been labeled `inactive-90d` due to no recent activity in the past 90 days. + Please close this PR if it is no longer required. + Otherwise, please respond with a comment indicating any updates. + stale-pr-label: "inactive-90d" + exempt-pr-labels: "0 - Blocked,0 - Backlog,good first issue" + days-before-pr-stale: 90 + days-before-pr-close: -1 + operations-per-run: 50 diff --git a/CHANGELOG.md b/CHANGELOG.md index 815ffa86e35..601ac2fb4f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,228 @@ -# cuGraph 0.17.0 (10 Dec 2020) +# cuGraph 21.08.00 (4 Aug 2021) + +## 🚨 Breaking Changes + +- Removed depricated code ([#1705](https://github.com/rapidsai/cugraph/pull/1705)) [@BradReesWork](https://github.com/BradReesWork) +- Delete legacy renumbering implementation ([#1681](https://github.com/rapidsai/cugraph/pull/1681)) [@ChuckHastings](https://github.com/ChuckHastings) +- Migrate old graph to legacy directory/namespace ([#1675](https://github.com/rapidsai/cugraph/pull/1675)) [@ChuckHastings](https://github.com/ChuckHastings) + +## πŸ› Bug Fixes + +- Changed cuco cmake function to return early if cuco has already been added as a target ([#1746](https://github.com/rapidsai/cugraph/pull/1746)) [@rlratzel](https://github.com/rlratzel) +- revert cuco to latest dev branch, issues should be fixed ([#1721](https://github.com/rapidsai/cugraph/pull/1721)) [@ChuckHastings](https://github.com/ChuckHastings) +- Fix `conda` uploads ([#1712](https://github.com/rapidsai/cugraph/pull/1712)) [@ajschmidt8](https://github.com/ajschmidt8) +- Updated for CUDA-specific py packages ([#1709](https://github.com/rapidsai/cugraph/pull/1709)) [@rlratzel](https://github.com/rlratzel) +- Use `library_dirs` for cython linking, link cudatoolkit libs, allow setting UCX install location ([#1698](https://github.com/rapidsai/cugraph/pull/1698)) [@trxcllnt](https://github.com/trxcllnt) +- Fix the Louvain failure with 64 bit vertex IDs ([#1696](https://github.com/rapidsai/cugraph/pull/1696)) [@seunghwak](https://github.com/seunghwak) +- Use nested include in destination of install headers to avoid docker permission issues ([#1656](https://github.com/rapidsai/cugraph/pull/1656)) [@dantegd](https://github.com/dantegd) +- Added accidentally-removed cpp-mgtests target back to the valid args list ([#1652](https://github.com/rapidsai/cugraph/pull/1652)) [@rlratzel](https://github.com/rlratzel) +- Update UCX-Py version to 0.21 ([#1650](https://github.com/rapidsai/cugraph/pull/1650)) [@pentschev](https://github.com/pentschev) + +## πŸ“– Documentation + +- Docs for RMAT ([#1735](https://github.com/rapidsai/cugraph/pull/1735)) [@BradReesWork](https://github.com/BradReesWork) +- Doc updates ([#1719](https://github.com/rapidsai/cugraph/pull/1719)) [@BradReesWork](https://github.com/BradReesWork) + +## πŸš€ New Features + +- Fea cleanup stream part1 ([#1653](https://github.com/rapidsai/cugraph/pull/1653)) [@ChuckHastings](https://github.com/ChuckHastings) + +## πŸ› οΈ Improvements + +- Pinning cuco to a specific commit hash for release ([#1741](https://github.com/rapidsai/cugraph/pull/1741)) [@rlratzel](https://github.com/rlratzel) +- Pin max version for `dask` & `distributed` ([#1736](https://github.com/rapidsai/cugraph/pull/1736)) [@galipremsagar](https://github.com/galipremsagar) +- Fix libfaiss dependency to not expressly depend on conda-forge ([#1728](https://github.com/rapidsai/cugraph/pull/1728)) [@Ethyling](https://github.com/Ethyling) +- Fix MG_test bug ([#1718](https://github.com/rapidsai/cugraph/pull/1718)) [@jnke2016](https://github.com/jnke2016) +- Cascaded dispatch for type-erased API ([#1711](https://github.com/rapidsai/cugraph/pull/1711)) [@aschaffer](https://github.com/aschaffer) +- ReduceV test ([#1710](https://github.com/rapidsai/cugraph/pull/1710)) [@kaatish](https://github.com/kaatish) +- Removed depricated code ([#1705](https://github.com/rapidsai/cugraph/pull/1705)) [@BradReesWork](https://github.com/BradReesWork) +- Delete unused/out-dated primitives ([#1704](https://github.com/rapidsai/cugraph/pull/1704)) [@seunghwak](https://github.com/seunghwak) +- Update primitives to support DCSR (DCSC) segments (Part 2/2) ([#1703](https://github.com/rapidsai/cugraph/pull/1703)) [@seunghwak](https://github.com/seunghwak) +- Fea speedup compile ([#1702](https://github.com/rapidsai/cugraph/pull/1702)) [@ChuckHastings](https://github.com/ChuckHastings) +- Update `conda` environment name for CI ([#1699](https://github.com/rapidsai/cugraph/pull/1699)) [@ajschmidt8](https://github.com/ajschmidt8) +- Count if test ([#1697](https://github.com/rapidsai/cugraph/pull/1697)) [@kaatish](https://github.com/kaatish) +- replace cudf assert_eq ([#1693](https://github.com/rapidsai/cugraph/pull/1693)) [@jnke2016](https://github.com/jnke2016) +- Fix int64 vertex_t ([#1691](https://github.com/rapidsai/cugraph/pull/1691)) [@Iroy30](https://github.com/Iroy30) +- Update primitives to support DCSR (DCSC) segments (Part 1) ([#1690](https://github.com/rapidsai/cugraph/pull/1690)) [@seunghwak](https://github.com/seunghwak) +- remove hardcoded dtype ([#1689](https://github.com/rapidsai/cugraph/pull/1689)) [@Iroy30](https://github.com/Iroy30) +- Updating Clang Version to 11.0.0 ([#1688](https://github.com/rapidsai/cugraph/pull/1688)) [@codereport](https://github.com/codereport) +- `CHECK_CUDA` macros in debug builds ([#1687](https://github.com/rapidsai/cugraph/pull/1687)) [@trxcllnt](https://github.com/trxcllnt) +- fixing symmetrize_ddf ([#1686](https://github.com/rapidsai/cugraph/pull/1686)) [@jnke2016](https://github.com/jnke2016) +- Improve Random Walks performance ([#1685](https://github.com/rapidsai/cugraph/pull/1685)) [@aschaffer](https://github.com/aschaffer) +- Use the 21.08 branch of rapids-cmake as rmm requires it ([#1683](https://github.com/rapidsai/cugraph/pull/1683)) [@robertmaynard](https://github.com/robertmaynard) +- Delete legacy renumbering implementation ([#1681](https://github.com/rapidsai/cugraph/pull/1681)) [@ChuckHastings](https://github.com/ChuckHastings) +- Fix vertex partition offsets ([#1680](https://github.com/rapidsai/cugraph/pull/1680)) [@Iroy30](https://github.com/Iroy30) +- Ues std::optional (or thrust::optional) for optional parameters & first part of DCSR (DCSC) implementation. ([#1676](https://github.com/rapidsai/cugraph/pull/1676)) [@seunghwak](https://github.com/seunghwak) +- Migrate old graph to legacy directory/namespace ([#1675](https://github.com/rapidsai/cugraph/pull/1675)) [@ChuckHastings](https://github.com/ChuckHastings) +- Expose epsilon parameter (precision) through python layer ([#1674](https://github.com/rapidsai/cugraph/pull/1674)) [@ChuckHastings](https://github.com/ChuckHastings) +- Fea hungarian expose precision ([#1673](https://github.com/rapidsai/cugraph/pull/1673)) [@ChuckHastings](https://github.com/ChuckHastings) +- Branch 21.08 merge 21.06 ([#1672](https://github.com/rapidsai/cugraph/pull/1672)) [@BradReesWork](https://github.com/BradReesWork) +- Update pins to Dask/Distributed >= 2021.6.0 ([#1666](https://github.com/rapidsai/cugraph/pull/1666)) [@pentschev](https://github.com/pentschev) +- Fix conflicts in `1643` ([#1651](https://github.com/rapidsai/cugraph/pull/1651)) [@ajschmidt8](https://github.com/ajschmidt8) +- Rename include/cugraph/patterns to include/cugraph/prims ([#1644](https://github.com/rapidsai/cugraph/pull/1644)) [@seunghwak](https://github.com/seunghwak) +- Fix merge conflicts in 1631 ([#1639](https://github.com/rapidsai/cugraph/pull/1639)) [@ajschmidt8](https://github.com/ajschmidt8) +- Update to changed `rmm::device_scalar` API ([#1637](https://github.com/rapidsai/cugraph/pull/1637)) [@harrism](https://github.com/harrism) +- Fix merge conflicts ([#1614](https://github.com/rapidsai/cugraph/pull/1614)) [@ajschmidt8](https://github.com/ajschmidt8) + +# cuGraph 21.06.00 (9 Jun 2021) + +## πŸ› Bug Fixes + +- Delete CUDA_ARCHITECTURES=OFF ([#1638](https://github.com/rapidsai/cugraph/pull/1638)) [@seunghwak](https://github.com/seunghwak) +- transform_reduce_e bug fixes ([#1633](https://github.com/rapidsai/cugraph/pull/1633)) [@ChuckHastings](https://github.com/ChuckHastings) +- Correct install path for include folder to avoid double nesting ([#1630](https://github.com/rapidsai/cugraph/pull/1630)) [@dantegd](https://github.com/dantegd) +- Remove thread local thrust::sort (thrust::sort with the execution policy thrust::seq) from copy_v_transform_reduce_key_aggregated_out_nbr ([#1627](https://github.com/rapidsai/cugraph/pull/1627)) [@seunghwak](https://github.com/seunghwak) + +## πŸš€ New Features + +- SG & MG Weakly Connected Components ([#1604](https://github.com/rapidsai/cugraph/pull/1604)) [@seunghwak](https://github.com/seunghwak) + +## πŸ› οΈ Improvements + +- Remove Pascal guard and test cuGraph use of cuco::static_map on Pascal ([#1640](https://github.com/rapidsai/cugraph/pull/1640)) [@seunghwak](https://github.com/seunghwak) +- Upgraded recipe and dev envs to NCCL 2.9.9 ([#1636](https://github.com/rapidsai/cugraph/pull/1636)) [@rlratzel](https://github.com/rlratzel) +- Use UCX-Py 0.20 ([#1634](https://github.com/rapidsai/cugraph/pull/1634)) [@jakirkham](https://github.com/jakirkham) +- Updated dependencies for CalVer ([#1629](https://github.com/rapidsai/cugraph/pull/1629)) [@rlratzel](https://github.com/rlratzel) +- MG WCC improvements ([#1628](https://github.com/rapidsai/cugraph/pull/1628)) [@seunghwak](https://github.com/seunghwak) +- Initialize force_atlas2 `old_forces` device_uvector, use new `rmm::exec_policy` ([#1625](https://github.com/rapidsai/cugraph/pull/1625)) [@trxcllnt](https://github.com/trxcllnt) +- Fix developer guide examples for device_buffer ([#1619](https://github.com/rapidsai/cugraph/pull/1619)) [@harrism](https://github.com/harrism) +- Pass rmm memory allocator to cuco::static_map ([#1617](https://github.com/rapidsai/cugraph/pull/1617)) [@seunghwak](https://github.com/seunghwak) +- Undo disabling MG C++ testing outputs for non-root processes ([#1615](https://github.com/rapidsai/cugraph/pull/1615)) [@seunghwak](https://github.com/seunghwak) +- WCC bindings ([#1612](https://github.com/rapidsai/cugraph/pull/1612)) [@Iroy30](https://github.com/Iroy30) +- address 'ValueError: Series contains NULL values' from from_cudf_edge… ([#1610](https://github.com/rapidsai/cugraph/pull/1610)) [@mattf](https://github.com/mattf) +- Fea rmm device buffer change ([#1609](https://github.com/rapidsai/cugraph/pull/1609)) [@ChuckHastings](https://github.com/ChuckHastings) +- Update `CHANGELOG.md` links for calver ([#1608](https://github.com/rapidsai/cugraph/pull/1608)) [@ajschmidt8](https://github.com/ajschmidt8) +- Handle int64 in force atlas wrapper and update to uvector ([#1607](https://github.com/rapidsai/cugraph/pull/1607)) [@hlinsen](https://github.com/hlinsen) +- Update docs build script ([#1606](https://github.com/rapidsai/cugraph/pull/1606)) [@ajschmidt8](https://github.com/ajschmidt8) +- WCC performance/memory footprint optimization ([#1605](https://github.com/rapidsai/cugraph/pull/1605)) [@seunghwak](https://github.com/seunghwak) +- adding test graphs - part 2 ([#1603](https://github.com/rapidsai/cugraph/pull/1603)) [@ChuckHastings](https://github.com/ChuckHastings) +- Update the Random Walk binding ([#1599](https://github.com/rapidsai/cugraph/pull/1599)) [@Iroy30](https://github.com/Iroy30) +- Add mnmg out degree ([#1592](https://github.com/rapidsai/cugraph/pull/1592)) [@Iroy30](https://github.com/Iroy30) +- Update `cugraph` to with newest CMake features, including CPM for dependencies ([#1585](https://github.com/rapidsai/cugraph/pull/1585)) [@robertmaynard](https://github.com/robertmaynard) +- Implement Graph Batching functionality ([#1580](https://github.com/rapidsai/cugraph/pull/1580)) [@aschaffer](https://github.com/aschaffer) +- add multi-column support in algorithms - part 2 ([#1571](https://github.com/rapidsai/cugraph/pull/1571)) [@Iroy30](https://github.com/Iroy30) + +# cuGraph 0.19.0 (21 Apr 2021) + +## πŸ› Bug Fixes + +- Fixed copyright date and format ([#1526](https://github.com//rapidsai/cugraph/pull/1526)) [@rlratzel](https://github.com/rlratzel) +- fix mg_renumber non-deterministic errors ([#1523](https://github.com//rapidsai/cugraph/pull/1523)) [@Iroy30](https://github.com/Iroy30) +- Updated NetworkX version to 2.5.1 ([#1510](https://github.com//rapidsai/cugraph/pull/1510)) [@rlratzel](https://github.com/rlratzel) +- pascal renumbering fix ([#1505](https://github.com//rapidsai/cugraph/pull/1505)) [@Iroy30](https://github.com/Iroy30) +- Fix MNMG test failures and skip tests that are not supported on Pascal ([#1498](https://github.com//rapidsai/cugraph/pull/1498)) [@jnke2016](https://github.com/jnke2016) +- Revert "Update conda recipes pinning of repo dependencies" ([#1493](https://github.com//rapidsai/cugraph/pull/1493)) [@raydouglass](https://github.com/raydouglass) +- Update conda recipes pinning of repo dependencies ([#1485](https://github.com//rapidsai/cugraph/pull/1485)) [@mike-wendt](https://github.com/mike-wendt) +- Update to make notebook_list.py compatible with numba 0.53 ([#1455](https://github.com//rapidsai/cugraph/pull/1455)) [@rlratzel](https://github.com/rlratzel) +- Fix bugs in copy_v_transform_reduce_key_aggregated_out_nbr & groupby_gpuid_and_shuffle ([#1434](https://github.com//rapidsai/cugraph/pull/1434)) [@seunghwak](https://github.com/seunghwak) +- update default path of setup to use the new directory paths in build … ([#1425](https://github.com//rapidsai/cugraph/pull/1425)) [@ChuckHastings](https://github.com/ChuckHastings) + +## πŸ“– Documentation + +- Create C++ documentation ([#1489](https://github.com//rapidsai/cugraph/pull/1489)) [@ChuckHastings](https://github.com/ChuckHastings) +- Create cuGraph developers guide ([#1431](https://github.com//rapidsai/cugraph/pull/1431)) [@ChuckHastings](https://github.com/ChuckHastings) +- Add boost 1.0 license file. ([#1401](https://github.com//rapidsai/cugraph/pull/1401)) [@seunghwak](https://github.com/seunghwak) + +## πŸš€ New Features + +- Implement C/CUDA RandomWalks functionality ([#1439](https://github.com//rapidsai/cugraph/pull/1439)) [@aschaffer](https://github.com/aschaffer) +- Add R-mat generator ([#1411](https://github.com//rapidsai/cugraph/pull/1411)) [@seunghwak](https://github.com/seunghwak) + +## πŸ› οΈ Improvements + +- Random Walks - Python Bindings ([#1516](https://github.com//rapidsai/cugraph/pull/1516)) [@jnke2016](https://github.com/jnke2016) +- Updating RAFT tag ([#1509](https://github.com//rapidsai/cugraph/pull/1509)) [@afender](https://github.com/afender) +- Clean up nullptr cuda_stream_view arguments ([#1504](https://github.com//rapidsai/cugraph/pull/1504)) [@hlinsen](https://github.com/hlinsen) +- Reduce the size of the cugraph libraries ([#1503](https://github.com//rapidsai/cugraph/pull/1503)) [@robertmaynard](https://github.com/robertmaynard) +- Add indirection and replace algorithms with new renumbering ([#1484](https://github.com//rapidsai/cugraph/pull/1484)) [@Iroy30](https://github.com/Iroy30) +- Multiple graph generator with power law distribution on sizes ([#1483](https://github.com//rapidsai/cugraph/pull/1483)) [@afender](https://github.com/afender) +- TSP solver bug fix ([#1480](https://github.com//rapidsai/cugraph/pull/1480)) [@hlinsen](https://github.com/hlinsen) +- Added cmake function and .hpp template for generating version_config.hpp file. ([#1476](https://github.com//rapidsai/cugraph/pull/1476)) [@rlratzel](https://github.com/rlratzel) +- Fix for bug in SCC on self-loops ([#1475](https://github.com//rapidsai/cugraph/pull/1475)) [@aschaffer](https://github.com/aschaffer) +- MS BFS python APIs + EgoNet updates ([#1469](https://github.com//rapidsai/cugraph/pull/1469)) [@afender](https://github.com/afender) +- Removed unused dependencies from libcugraph recipe, moved non-test script code from test script to gpu build script ([#1468](https://github.com//rapidsai/cugraph/pull/1468)) [@rlratzel](https://github.com/rlratzel) +- Remove literals passed to `device_uvector::set_element_async` ([#1453](https://github.com//rapidsai/cugraph/pull/1453)) [@harrism](https://github.com/harrism) +- ENH Change conda build directories to work with ccache ([#1452](https://github.com//rapidsai/cugraph/pull/1452)) [@dillon-cullinan](https://github.com/dillon-cullinan) +- Updating docs ([#1448](https://github.com//rapidsai/cugraph/pull/1448)) [@BradReesWork](https://github.com/BradReesWork) +- Improve graph primitives performance on graphs with widely varying vertex degrees ([#1447](https://github.com//rapidsai/cugraph/pull/1447)) [@seunghwak](https://github.com/seunghwak) +- Update Changelog Link ([#1446](https://github.com//rapidsai/cugraph/pull/1446)) [@ajschmidt8](https://github.com/ajschmidt8) +- Updated NCCL to version 2.8.4 ([#1445](https://github.com//rapidsai/cugraph/pull/1445)) [@BradReesWork](https://github.com/BradReesWork) +- Update FAISS to 1.7.0 ([#1444](https://github.com//rapidsai/cugraph/pull/1444)) [@BradReesWork](https://github.com/BradReesWork) +- Update graph partitioning scheme ([#1443](https://github.com//rapidsai/cugraph/pull/1443)) [@seunghwak](https://github.com/seunghwak) +- Add additional datasets to improve coverage ([#1441](https://github.com//rapidsai/cugraph/pull/1441)) [@jnke2016](https://github.com/jnke2016) +- Update C++ MG PageRank and SG PageRank, Katz Centrality, BFS, and SSSP to use the new R-mat graph generator ([#1438](https://github.com//rapidsai/cugraph/pull/1438)) [@seunghwak](https://github.com/seunghwak) +- Remove raft handle duplication ([#1436](https://github.com//rapidsai/cugraph/pull/1436)) [@Iroy30](https://github.com/Iroy30) +- Streams infra + support in egonet ([#1435](https://github.com//rapidsai/cugraph/pull/1435)) [@afender](https://github.com/afender) +- Prepare Changelog for Automation ([#1433](https://github.com//rapidsai/cugraph/pull/1433)) [@ajschmidt8](https://github.com/ajschmidt8) +- Update 0.18 changelog entry ([#1429](https://github.com//rapidsai/cugraph/pull/1429)) [@ajschmidt8](https://github.com/ajschmidt8) +- Update and Test Renumber bindings ([#1427](https://github.com//rapidsai/cugraph/pull/1427)) [@Iroy30](https://github.com/Iroy30) +- Update Louvain to use new graph primitives and pattern accelerators ([#1423](https://github.com//rapidsai/cugraph/pull/1423)) [@ChuckHastings](https://github.com/ChuckHastings) +- Replace rmm::device_vector & thrust::host_vector with rmm::device_uvector & std::vector, respectively. ([#1421](https://github.com//rapidsai/cugraph/pull/1421)) [@seunghwak](https://github.com/seunghwak) +- Update C++ MG PageRank test ([#1419](https://github.com//rapidsai/cugraph/pull/1419)) [@seunghwak](https://github.com/seunghwak) +- ENH Build with `cmake --build` & Pass ccache variables to conda recipe & use Ninja in CI ([#1415](https://github.com//rapidsai/cugraph/pull/1415)) [@Ethyling](https://github.com/Ethyling) +- Adding new primitives: copy_v_transform_reduce_key_aggregated_out_nbr & transform_reduce_by_adj_matrix_row|col_key_e bug fixes ([#1399](https://github.com//rapidsai/cugraph/pull/1399)) [@seunghwak](https://github.com/seunghwak) +- Add new primitives: compute_in|out_degrees, compute_in|out_weight_sums to graph_view_t ([#1394](https://github.com//rapidsai/cugraph/pull/1394)) [@seunghwak](https://github.com/seunghwak) +- Rename sort_and_shuffle to groupby_gpuid_and_shuffle ([#1392](https://github.com//rapidsai/cugraph/pull/1392)) [@seunghwak](https://github.com/seunghwak) +- Matching updates for RAFT comms updates (device_sendrecv, device_multicast_sendrecv, gather, gatherv) ([#1391](https://github.com//rapidsai/cugraph/pull/1391)) [@seunghwak](https://github.com/seunghwak) +- Fix forward-merge conflicts for #1370 ([#1377](https://github.com//rapidsai/cugraph/pull/1377)) [@ajschmidt8](https://github.com/ajschmidt8) +- Add utility function for computing a secondary cost for BFS and SSSP output ([#1376](https://github.com//rapidsai/cugraph/pull/1376)) [@hlinsen](https://github.com/hlinsen) + +# cuGraph 0.18.0 (24 Feb 2021) + +## Bug Fixes πŸ› + +- Fixed TSP returned routes (#1412) @hlinsen +- Updated CI scripts to use a different error handling convention, updated LD_LIBRARY_PATH for project flash runs (#1386) @rlratzel +- Bug fixes for MNMG coarsen_graph, renumber_edgelist, relabel (#1364) @seunghwak +- Set a specific known working commit hash for gunrock instead of "dev" (#1336) @rlratzel +- Updated git utils used by copyright.py for compatibility with current CI env (#1325) @rlratzel +- Fix MNMG Louvain tests on Pascal architecture (#1322) @ChuckHastings +- FIX Set bash trap after PATH is updated (#1321) @dillon-cullinan +- Fix graph nodes function and renumbering from series (#1319) @Iroy30 +- Fix Branch 0.18 merge 0.17 (#1314) @BradReesWork +- Fix EXPERIMENTAL_LOUVAIN_TEST on Pascal (#1312) @ChuckHastings +- Updated cuxfilter to 0.18, removed datashader indirect dependency in conda dev .yml files (#1311) @rlratzel +- Update SG PageRank C++ tests (#1307) @seunghwak + +## Documentation πŸ“– + +- Enabled MultiGraph class and tests, updated SOURCEBUILD.md to include the latest build.sh options (#1351) @rlratzel + +## New Features πŸš€ + +- New EgoNet extractor (#1365) @afender +- Implement induced subgraph extraction primitive (SG C++) (#1354) @seunghwak + +## Improvements πŸ› οΈ + +- Update stale GHA with exemptions & new labels (#1413) @mike-wendt +- Add GHA to mark issues/prs as stale/rotten (#1408) @Ethyling +- update subgraph tests and remove legacy pagerank (#1378) @Iroy30 +- Update the conda environments and README file (#1369) @BradReesWork +- Prepare Changelog for Automation (#1368) @ajschmidt8 +- Update CMakeLists.txt files for consistency with RAPIDS and to support cugraph as an external project and other tech debt removal (#1367) @rlratzel +- Use new coarsen_graph primitive in Louvain (#1362) @ChuckHastings +- Added initial infrastructure for MG C++ testing and a Pagerank MG test using it (#1361) @rlratzel +- Add SG TSP (#1360) @hlinsen +- Build a Dendrogram class, adapt Louvain/Leiden/ECG to use it (#1359) @ChuckHastings +- Auto-label PRs based on their content (#1358) @jolorunyomi +- Implement MNMG Renumber (#1355) @aschaffer +- Enabling pytest code coverage output by default (#1352) @jnke2016 +- Added configuration for new cugraph-doc-codeowners review group (#1344) @rlratzel +- API update to match RAFT PR #120 (#1343) @drobison00 +- Pin gunrock to v1.2 for version 0.18 (#1342) @ChuckHastings +- Fix #1340 - Use generic from_edgelist() methods (#1341) @miguelusque +- Using RAPIDS_DATASET_ROOT_DIR env var in place of absolute path to datasets in tests (#1337) @jnke2016 +- Expose dense implementation of Hungarian algorithm (#1333) @ChuckHastings +- SG Pagerank transition (#1332) @Iroy30 +- improving error checking and docs (#1327) @BradReesWork +- Fix MNMG cleanup exceptions (#1326) @Iroy30 +- Create labeler.yml (#1318) @jolorunyomi +- Updates to support nightly MG test automation (#1308) @rlratzel +- Add C++ graph functions (coarsen_grpah, renumber_edgelist, relabel) and primitvies (transform_reduce_by_adj_matrix_row_key, transform_reduce_by_adj_matrix_col_key, copy_v_transform_reduce_key_aggregated_out_nbr) (#1257) @seunghwak +>>>>>>> upstream/branch-0.18 +# cuGraph 0.17.0 (10 Dec 2020) ## New Features - PR #1276 MST - PR #1245 Add functions to add pandas and numpy compatibility @@ -10,6 +233,7 @@ - PR #1279 Add self loop check variable in graph - PR #1277 SciPy sparse matrix input support for WCC, SCC, SSSP, and BFS - PR #1278 Add support for shortest_path_length and fix graph vertex checks +- PR #1280 Add Multi(Di)Graph support ## Improvements - PR #1227 Pin cmake policies to cmake 3.17 version diff --git a/README.md b/README.md index 8fee5451ac3..76015f528f6 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ The [RAPIDS](https://rapids.ai) cuGraph library is a collection of GPU accelerat **NOTE:** For the latest stable [README.md](https://github.com/rapidsai/cugraph/blob/main/README.md) ensure you are on the latest branch. - +As an example, the following Python snippet loads graph data and computes PageRank: ```python import cugraph @@ -30,18 +30,30 @@ for i in range(len(df_page)): " PageRank is " + str(df_page['pagerank'].iloc[i])) ``` +## Getting cuGraph +There are 3 ways to get cuGraph : +1. [Quick start with Docker Repo](#quick) +2. [Conda Installation](#conda) +3. [Build from Source](#source) +

+ +--- +# Currently Supported Features +As of Release 21.08 - including 21.08 nightly + ## Supported Algorithms -| Category | Algorithm | Scale | Notes +| Category | Algorithm | Scale | Notes | | ------------ | -------------------------------------- | ------------ | ------------------- | | Centrality | | | | | | Katz | Multi-GPU | | | | Betweenness Centrality | Single-GPU | | | | Edge Betweenness Centrality | Single-GPU | | | Community | | | | +| | EgoNet | Single-GPU | | | | Leiden | Single-GPU | | -| | Louvain | Multi-GPU | | +| | Louvain | Multi-GPU | [C++ README](cpp/src/community/README.md#Louvain) | | | Ensemble Clustering for Graphs | Single-GPU | | | | Spectral-Clustering - Balanced Cut | Single-GPU | | | | Spectral-Clustering - Modularity | Single-GPU | | @@ -49,32 +61,35 @@ for i in range(len(df_page)): | | Triangle Counting | Single-GPU | | | | K-Truss | Single-GPU | | | Components | | | | -| | Weakly Connected Components | Single-GPU | | +| | Weakly Connected Components | Multi-GPU | | | | Strongly Connected Components | Single-GPU | | | Core | | | | | | K-Core | Single-GPU | | | | Core Number | Single-GPU | | | Layout | | | | | | Force Atlas 2 | Single-GPU | | +| Linear Assignment| | | | +| | Hungarian | Single-GPU | [README](cpp/src/linear_assignment/README-hungarian.md) | | Link Analysis| | | | -| | Pagerank | Multi-GPU | | -| | Personal Pagerank | Multi-GPU | | -| | HITS | Single-GPU | leverages Gunrock | +| | Pagerank | Multi-GPU | [C++ README](cpp/src/centrality/README.md#Pagerank) | +| | Personal Pagerank | Multi-GPU | [C++ README](cpp/src/centrality/README.md#Personalized-Pagerank) | +| | HITS | Single-GPU | leverages Gunrock | | Link Prediction | | | | | | Jaccard Similarity | Single-GPU | | | | Weighted Jaccard Similarity | Single-GPU | | | | Overlap Similarity | Single-GPU | | +| Sampling | | | | +| | Random Walks (RW) | Single-GPU | | | Traversal | | | | -| | Breadth First Search (BFS) | Multi-GPU | | -| | Single Source Shortest Path (SSSP) | Multi-GPU | | -| Structure | | | | -| | Renumbering | Single-GPU | multiple columns, any data type | -| | Symmetrize | Multi-GPU | | -| Other | | | | -| | Hungarian Algorithm | Single-GPU | | +| | Breadth First Search (BFS) | Multi-GPU | with cutoff support
[C++ README](cpp/src/traversal/README.md#BFS) | +| | Single Source Shortest Path (SSSP) | Multi-GPU | [C++ README](cpp/src/traversal/README.md#SSSP) | +| | Traveling Salesperson Problem (TSP) | Single-GPU | | +| Tree | | | | | | Minimum Spanning Tree | Single-GPU | | | | Maximum Spanning Tree | Single-GPU | | - +| Other | | | | +| | Renumbering | Multi-GPU | multiple columns, any data type | +| | Symmetrize | Multi-GPU | | | | |

@@ -83,13 +98,16 @@ for i in range(len(df_page)): | --------------- | --------------------------------------------------- | | Graph | An undirected Graph | | DiGraph | A Directed Graph | -| _Multigraph_ | _coming in 0.18_ | -| _MultiDigraph_ | _coming in 0.18_ | +| Multigraph | A Graph with multiple edges between a vertex pair | +| MultiDigraph | A Directed Graph with multiple edges between a vertex pair | | | | +ALL Algorithms support Graphs and MultiGraph (directed and undirected) + +

## Supported Data Types -cuGraph supports the creation of a graph several data types: +cuGraph supports graph creation with Source and Destination being expressed as: * cuDF DataFrame * Pandas DataFrame @@ -103,11 +121,8 @@ cuGraph tries to match the return type based on the input type. So a NetworkX i ## cuGraph Notice -The current version of cuGraph has some limitations: -- Vertex IDs are expected to be contiguous integers starting from 0. - -cuGraph provides the renumber function to mitigate this problem, which is by default automatically called when data is addted to a graph. Input vertex IDs for the renumber function can be any type, can be non-contiguous, can be multiple columns, and can start from an arbitrary number. The renumber function maps the provided input vertex IDs to 32-bit contiguous integers starting from 0. cuGraph still requires the renumbered vertex IDs to be representable in 32-bit integers. These limitations are being addressed and will be fixed soon. +Vertex IDs are expected to be contiguous integers starting from 0. If your data doesn't match that restriction, we have a solution. cuGraph provides the renumber function, which is by default automatically called when data is addted to a graph. Input vertex IDs for the renumber function can be any type, can be non-contiguous, can be multiple columns, and can start from an arbitrary number. The renumber function maps the provided input vertex IDs to either 32- or 64-bit contiguous integers starting from 0. Additionally, when using the auto-renumbering feature, vertices are automatically un-renumbered in results. @@ -123,42 +138,31 @@ The amount of memory required is dependent on the graph structure and the analyt The use of managed memory for oversubscription can also be used to exceed the above memory limitations. See the recent blog on _Tackling Large Graphs with RAPIDS cuGraph and CUDA Unified Memory on GPUs_: https://medium.com/rapids-ai/tackling-large-graphs-with-rapids-cugraph-and-unified-virtual-memory-b5b69a065d4 +

-## Getting cuGraph -### Intro -There are 3 ways to get cuGraph : -1. [Quick start with Docker Demo Repo](#quick) -2. [Conda Installation](#conda) -3. [Build from Source](#source) - - - - +--- ## Quick Start -Please see the [Demo Docker Repository](https://hub.docker.com/r/rapidsai/rapidsai/), choosing a tag based on the NVIDIA CUDA version you’re running. This provides a ready to run Docker container with example notebooks and data, showcasing how you can utilize all of the RAPIDS libraries: cuDF, cuML, and cuGraph. +Please see the [Docker Repository](https://hub.docker.com/r/rapidsai/rapidsai/), choosing a tag based on the NVIDIA CUDA version you’re running. This provides a ready to run Docker container with example notebooks and data, showcasing how you can utilize all of the RAPIDS libraries: cuDF, cuML, and cuGraph. -### Conda +## Conda It is easy to install cuGraph using conda. You can get a minimal conda installation with [Miniconda](https://conda.io/miniconda.html) or get the full installation with [Anaconda](https://www.anaconda.com/download). Install and update cuGraph using the conda command: ```bash -# CUDA 10.1 -conda install -c nvidia -c rapidsai -c numba -c conda-forge -c defaults cugraph cudatoolkit=10.1 - -# CUDA 10.2 -conda install -c nvidia -c rapidsai -c numba -c conda-forge -c defaults cugraph cudatoolkit=10.2 - # CUDA 11.0 -conda install -c nvidia -c rapidsai -c numba -c conda-forge -c defaults cugraph cudatoolkit=11.0 +conda install -c nvidia -c rapidsai -c numba -c conda-forge cugraph cudatoolkit=11.0 + +# CUDA 11.2 +conda install -c nvidia -c rapidsai -c numba -c conda-forge cugraph cudatoolkit=11.2 ``` Note: This conda installation only applies to Linux and Python versions 3.7/3.8. -### Build from Source and Contributing +## Build from Source and Contributing Please see our [guide for building cuGraph from source](SOURCEBUILD.md) diff --git a/SOURCEBUILD.md b/SOURCEBUILD.md index 8acd90c4f7f..47b842a0ce6 100644 --- a/SOURCEBUILD.md +++ b/SOURCEBUILD.md @@ -1,25 +1,21 @@ # Building from Source -The following instructions are for users wishing to build cuGraph from source code. These instructions are tested on supported distributions of Linux, CUDA, and Python - See [RAPIDS Getting Started](https://rapids.ai/start.html) for list of supported environments. Other operating systems _might be_ compatible, but are not currently tested. +The following instructions are for users wishing to build cuGraph from source code. These instructions are tested on supported distributions of Linux, CUDA, and Python - See [RAPIDS Getting Started](https://rapids.ai/start.html) for list of supported environments. Other operating systems _might be_ compatible, but are not currently tested. The cuGraph package include both a C/C++ CUDA portion and a python portion. Both libraries need to be installed in order for cuGraph to operate correctly. ## Prerequisites __Compiler__: -* `gcc` version 5.4+ -* `nvcc` version 10.0+ -* `cmake` version 3.12+ +* `gcc` version 9.3+ +* `nvcc` version 11.0+ +* `cmake` version 3.20.1+ __CUDA:__ -* CUDA 10.1+ -* NVIDIA driver 396.44+ +* CUDA 11.0+ +* NVIDIA driver 450.80.02+ * Pascal architecture or better -__Other__ -* `git` - - You can obtain CUDA from [https://developer.nvidia.com/cuda-downloads](https://developer.nvidia.com/cuda-downloads). @@ -47,17 +43,12 @@ __Create the conda development environment__ ```bash # create the conda environment (assuming in base `cugraph` directory) - - -# for CUDA 10.1 -conda env create --name cugraph_dev --file conda/environments/cugraph_dev_cuda10.1.yml - -# for CUDA 10.2 -conda env create --name cugraph_dev --file conda/environments/cugraph_dev_cuda10.2.yml - -# for CUDA 11 +# for CUDA 11.0 conda env create --name cugraph_dev --file conda/environments/cugraph_dev_cuda11.0.yml +# for CUDA 11.2 +conda env create --name cugraph_dev --file conda/environments/cugraph_dev_cuda11.2.yml + # activate the environment conda activate cugraph_dev @@ -70,15 +61,12 @@ conda deactivate ```bash -# for CUDA 10.1 -conda env update --name cugraph_dev --file conda/environments/cugraph_dev_cuda10.1.yml - -# for CUDA 10.2 -conda env update --name cugraph_dev --file conda/environments/cugraph_dev_cuda10.2.yml - -# for CUDA 11 +# for CUDA 11.0 conda env update --name cugraph_dev --file conda/environments/cugraph_dev_cuda11.0.yml +# for CUDA 11.2 +conda env update --name cugraph_dev --file conda/environments/cugraph_dev_cuda11.2.yml + conda activate cugraph_dev ``` @@ -97,17 +85,21 @@ There are several other options available on the build script for advanced users `build.sh` options: ```bash build.sh [ ...] [ ...] - clean - remove all existing build artifacts and configuration (start over) - libcugraph - build the cugraph C++ code - cugraph - build the cugraph Python package - + where is: + clean - remove all existing build artifacts and configuration (start over) + libcugraph - build the cugraph C++ code + cugraph - build the cugraph Python package + docs - build the docs and is: -v - verbose build mode -g - build for debug -n - no install step + --allgpuarch - build for all supported GPU architectures --show_depr_warn - show cmake deprecation warnings -h - print this text + default action (no args) is to build and install 'libcugraph' then 'cugraph' then 'docs' targets + examples: $ ./build.sh clean # remove prior build artifacts (start over) $ ./build.sh libcugraph -v # compile and install libcugraph with verbose output @@ -189,7 +181,7 @@ Run either the C++ or the Python tests with datasets ```bash cd $CUGRAPH_HOME/datasets - source get_test_data.sh #This takes about 10 minutes and download 1GB data (>5 GB uncompressed) + source get_test_data.sh #This takes about 10 minutes and downloads 1GB data (>5 GB uncompressed) ``` Run the C++ tests on large input: @@ -228,8 +220,8 @@ Next the env_vars.sh file needs to be edited vi ./etc/conda/activate.d/env_vars.sh #!/bin/bash -export PATH=/usr/local/cuda-10.1/bin:$PATH # or cuda-10.2 if using CUDA 10.2 -export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib64:$LD_LIBRARY_PATH # or cuda-10.2 if using CUDA 10.2 +export PATH=/usr/local/cuda-11.0/bin:$PATH # or cuda-11.1 if using CUDA 11.1 and cuda-11.2 if using CUDA 11.2, respectively +export LD_LIBRARY_PATH=/usr/local/cuda-11.0/lib64:$LD_LIBRARY_PATH # or cuda-11.1 if using CUDA 11.1 and cuda-11.2 if using CUDA 11.2, respectively ``` ``` diff --git a/benchmarks/bench_algos.py b/benchmarks/bench_algos.py index 9be636ca480..5284ffbd37b 100644 --- a/benchmarks/bench_algos.py +++ b/benchmarks/bench_algos.py @@ -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. # You may obtain a copy of the License at @@ -32,6 +32,7 @@ def setFixtureParamNames(*args, **kwargs): import cugraph from cugraph.structure.number_map import NumberMap from cugraph.tests import utils +from cugraph.utilities.utils import is_device_version_less_than import rmm from .params import FIXTURE_PARAMS @@ -50,9 +51,9 @@ def createGraph(csvFileName, graphType=None): # complexity lower, and assume tests have coverage to verify # correctness for those combinations. if "/directed/" in csvFileName: - graphType = cugraph.structure.graph.DiGraph + graphType = cugraph.structure.graph_classes.DiGraph else: - graphType = cugraph.structure.graph.Graph + graphType = cugraph.structure.graph_classes.Graph return cugraph.from_cudf_edgelist( utils.read_csv_file(csvFileName), @@ -121,7 +122,7 @@ def graphWithAdjListComputed(request): csvFileName = request.param[0] reinitRMM(request.param[1], request.param[2]) - G = createGraph(csvFileName, cugraph.structure.graph.Graph) + G = createGraph(csvFileName, cugraph.structure.graph_classes.Graph) G.view_adj_list() return G @@ -165,7 +166,7 @@ def bench_create_graph(gpubenchmark, edgelistCreated): gpubenchmark(cugraph.from_cudf_edgelist, edgelistCreated, source="0", destination="1", - create_using=cugraph.structure.graph.Graph, + create_using=cugraph.structure.graph_classes.Graph, renumber=False) @@ -182,7 +183,7 @@ def bench_create_digraph(gpubenchmark, edgelistCreated): gpubenchmark(cugraph.from_cudf_edgelist, edgelistCreated, source="0", destination="1", - create_using=cugraph.structure.graph.DiGraph, + create_using=cugraph.structure.graph_classes.DiGraph, renumber=False) @@ -212,6 +213,8 @@ def bench_jaccard(gpubenchmark, graphWithAdjListComputed): gpubenchmark(cugraph.jaccard, graphWithAdjListComputed) +@pytest.mark.skipif( + is_device_version_less_than((7, 0)), reason="Not supported on Pascal") def bench_louvain(gpubenchmark, graphWithAdjListComputed): gpubenchmark(cugraph.louvain, graphWithAdjListComputed) diff --git a/build.sh b/build.sh index b3d3463ed4e..506cc482c59 100755 --- a/build.sh +++ b/build.sh @@ -1,6 +1,6 @@ #!/bin/bash -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-2021, NVIDIA CORPORATION. # cugraph build script @@ -19,18 +19,21 @@ ARGS=$* REPODIR=$(cd $(dirname $0); pwd) LIBCUGRAPH_BUILD_DIR=${LIBCUGRAPH_BUILD_DIR:=${REPODIR}/cpp/build} -VALIDARGS="clean libcugraph cugraph docs -v -g -n --allgpuarch --show_depr_warn -h --help" +VALIDARGS="clean uninstall libcugraph cugraph cpp-mgtests docs -v -g -n --allgpuarch --buildfaiss --show_depr_warn -h --help" HELP="$0 [ ...] [ ...] where is: clean - remove all existing build artifacts and configuration (start over) + uninstall - uninstall libcugraph and cugraph from a prior build/install (see also -n) libcugraph - build the cugraph C++ code cugraph - build the cugraph Python package + cpp-mgtests - build libcugraph mnmg tests. Builds MPI communicator, adding MPI as a dependency. docs - build the docs and is: -v - verbose build mode -g - build for debug - -n - no install step + -n - do not install after a successful build --allgpuarch - build for all supported GPU architectures + --buildfaiss - build faiss statically into cugraph --show_depr_warn - show cmake deprecation warnings -h - print this text @@ -44,11 +47,13 @@ CUGRAPH_BUILD_DIR=${REPODIR}/python/build BUILD_DIRS="${LIBCUGRAPH_BUILD_DIR} ${CUGRAPH_BUILD_DIR}" # Set defaults for vars modified by flags to this script -VERBOSE="" +VERBOSE_FLAG="" BUILD_TYPE=Release INSTALL_TARGET=install BUILD_DISABLE_DEPRECATION_WARNING=ON -GPU_ARCH="" +BUILD_CPP_MG_TESTS=OFF +BUILD_STATIC_FAISS=OFF +BUILD_ALL_GPU_ARCH=0 # Set defaults for vars that may not have been defined externally # FIXME: if PREFIX is not set, check CONDA_PREFIX, but there is no fallback @@ -82,7 +87,7 @@ fi # Process flags if hasArg -v; then - VERBOSE=1 + VERBOSE_FLAG="-v" fi if hasArg -g; then BUILD_TYPE=Debug @@ -91,18 +96,48 @@ if hasArg -n; then INSTALL_TARGET="" fi if hasArg --allgpuarch; then - GPU_ARCH="-DGPU_ARCHS=ALL" + BUILD_ALL_GPU_ARCH=1 +fi +if hasArg --buildfaiss; then + BUILD_STATIC_FAISS=ON fi if hasArg --show_depr_warn; then BUILD_DISABLE_DEPRECATION_WARNING=OFF fi +if hasArg cpp-mgtests; then + BUILD_CPP_MG_TESTS=ON +fi + +# If clean or uninstall given, run them prior to any other steps +if hasArg uninstall; then + # uninstall libcugraph + if [[ "$INSTALL_PREFIX" != "" ]]; then + rm -rf ${INSTALL_PREFIX}/include/cugraph + rm -f ${INSTALL_PREFIX}/lib/libcugraph.so + fi + # This may be redundant given the above, but can also be used in case + # there are other installed files outside of the locations above. + if [ -e ${LIBCUGRAPH_BUILD_DIR}/install_manifest.txt ]; then + xargs rm -f < ${LIBCUGRAPH_BUILD_DIR}/install_manifest.txt > /dev/null 2>&1 + fi + # uninstall cugraph installed from a prior "setup.py install" + pip uninstall -y cugraph +fi -# If clean given, run it prior to any other steps if hasArg clean; then - # FIXME: ideally the "setup.py clean" command below would also be run to - # remove all the "inplace" python build artifacts, but currently, running - # any setup.py command has side effects (eg. cloning repos). - #(cd ${REPODIR}/python && python setup.py clean) + # remove artifacts generated inplace + # FIXME: ideally the "setup.py clean" command would be used for this, but + # currently running any setup.py command has side effects (eg. cloning + # repos). + # (cd ${REPODIR}/python && python setup.py clean) + if [[ -d ${REPODIR}/python ]]; then + pushd ${REPODIR}/python > /dev/null + rm -rf dist dask-worker-space cugraph/raft *.egg-info + find . -name "__pycache__" -type d -exec rm -rf {} \; > /dev/null 2>&1 + find . -name "*.cpp" -type f -delete + find . -name "*.cpython*.so" -type f -delete + popd > /dev/null + fi # If the dirs to clean are mounted dirs in a container, the contents should # be removed but the mounted dirs will remain. The find removes all @@ -119,18 +154,23 @@ fi ################################################################################ # Configure, build, and install libcugraph if buildAll || hasArg libcugraph; then - if [[ ${GPU_ARCH} == "" ]]; then + if (( ${BUILD_ALL_GPU_ARCH} == 0 )); then + CUGRAPH_CMAKE_CUDA_ARCHITECTURES="NATIVE" echo "Building for the architecture of the GPU in the system..." else + CUGRAPH_CMAKE_CUDA_ARCHITECTURES="ALL" echo "Building for *ALL* supported GPU architectures..." fi mkdir -p ${LIBCUGRAPH_BUILD_DIR} cd ${LIBCUGRAPH_BUILD_DIR} cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} \ - ${GPU_ARCH} \ - -DDISABLE_DEPRECATION_WARNING=${BUILD_DISABLE_DEPRECATION_WARNING} \ - -DCMAKE_BUILD_TYPE=${BUILD_TYPE} ${REPODIR}/cpp - make -j${PARALLEL_LEVEL} VERBOSE=${VERBOSE} ${INSTALL_TARGET} + -DCMAKE_CUDA_ARCHITECTURES=${CUGRAPH_CMAKE_CUDA_ARCHITECTURES} \ + -DDISABLE_DEPRECATION_WARNING=${BUILD_DISABLE_DEPRECATION_WARNING} \ + -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \ + -DBUILD_STATIC_FAISS=${BUILD_STATIC_FAISS} \ + -DBUILD_CUGRAPH_MG_TESTS=${BUILD_CPP_MG_TESTS} \ + ${REPODIR}/cpp + cmake --build "${LIBCUGRAPH_BUILD_DIR}" -j${PARALLEL_LEVEL} --target ${INSTALL_TARGET} ${VERBOSE_FLAG} fi # Build and install the cugraph Python package @@ -152,10 +192,11 @@ if buildAll || hasArg docs; then cd ${LIBCUGRAPH_BUILD_DIR} cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} \ -DDISABLE_DEPRECATION_WARNING=${BUILD_DISABLE_DEPRECATION_WARNING} \ - -DCMAKE_BUILD_TYPE=${BUILD_TYPE} ${REPODIR}/cpp + -DCMAKE_BUILD_TYPE=${BUILD_TYPE} ${REPODIR}/cpp \ + -DBUILD_STATIC_FAISS=${BUILD_STATIC_FAISS} fi cd ${LIBCUGRAPH_BUILD_DIR} - make -j${PARALLEL_LEVEL} VERBOSE=${VERBOSE} docs_cugraph - cd ${REPODIR}/docs + cmake --build "${LIBCUGRAPH_BUILD_DIR}" -j${PARALLEL_LEVEL} --target docs_cugraph ${VERBOSE_FLAG} + cd ${REPODIR}/docs/cugraph make html fi diff --git a/ci/benchmark/build.sh b/ci/benchmark/build.sh index 5f74dca4044..f8100222c12 100644 --- a/ci/benchmark/build.sh +++ b/ci/benchmark/build.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# Copyright (c) 2018-2020, NVIDIA CORPORATION. +# Copyright (c) 2018-2021, NVIDIA CORPORATION. ########################################## # cuGraph Benchmark test script for CI # ########################################## @@ -20,18 +20,18 @@ function cleanup { rm -f testoutput.txt } -# Set cleanup trap for Jenkins -if [ ! -z "$JENKINS_HOME" ] ; then - gpuci_logger "Jenkins environment detected, setting cleanup trap" - trap cleanup EXIT -fi - # Set path, build parallel level, and CUDA version cd $WORKSPACE export PATH=/opt/conda/bin:/usr/local/cuda/bin:$PATH export PARALLEL_LEVEL=${PARALLEL_LEVEL:-4} export CUDA_REL=${CUDA_VERSION%.*} +# Set cleanup trap for Jenkins +if [ ! -z "$JENKINS_HOME" ] ; then + gpuci_logger "Jenkins environment detected, setting cleanup trap" + trap cleanup EXIT +fi + # Set home export HOME=$WORKSPACE @@ -68,13 +68,13 @@ CUGRAPH_DEPS=(cudf rmm) LIBCUGRAPH_DEPS=(cudf rmm) gpuci_logger "Install required packages" -gpuci_conda_retry install -c nvidia -c rapidsai -c rapidsai-nightly -c conda-forge -c defaults \ +gpuci_conda_retry install -c nvidia -c rapidsai -c rapidsai-nightly -c conda-forge \ "cudf=${MINOR_VERSION}" \ "rmm=${MINOR_VERSION}" \ "cudatoolkit=$CUDA_REL" \ "dask-cudf=${MINOR_VERSION}" \ "dask-cuda=${MINOR_VERSION}" \ - "ucx-py=${MINOR_VERSION}" \ + "ucx-py=0.21.*" \ "ucx-proc=*=gpu" \ "rapids-build-env=${MINOR_VERSION}" \ rapids-pytest-benchmark diff --git a/ci/checks/style.sh b/ci/checks/style.sh index 978ac03d85b..81388fa7b20 100755 --- a/ci/checks/style.sh +++ b/ci/checks/style.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Copyright (c) 2018-2020, NVIDIA CORPORATION. +# Copyright (c) 2018-2021, NVIDIA CORPORATION. ######################## # cuGraph Style Tester # ######################## @@ -18,7 +18,8 @@ ERRORCODE=0 PATH=/conda/bin:$PATH # Activate common conda env -source activate gdf +. /opt/conda/etc/profile.d/conda.sh +conda activate rapids # Run flake8 and get results/return code FLAKE=`flake8 --config=python/.flake8 python` @@ -52,13 +53,14 @@ COPYRIGHT=`env PYTHONPATH=ci/utils python ci/checks/copyright.py --git-modified- CR_RETVAL=$? ERRORCODE=$((ERRORCODE | ${CR_RETVAL})) -# Output results if failure otherwise show pass if [ "$CR_RETVAL" != "0" ]; then echo -e "\n\n>>>> FAILED: copyright check; begin output\n\n" echo -e "$COPYRIGHT" echo -e "\n\n>>>> FAILED: copyright check; end output\n\n" else - echo -e "\n\n>>>> PASSED: copyright check\n\n" + echo -e "\n\n>>>> PASSED: copyright check; begin debug output\n\n" + echo -e "$COPYRIGHT" + echo -e "\n\n>>>> PASSED: copyright check; end debug output\n\n" fi exit ${ERRORCODE} diff --git a/ci/cpu/build.sh b/ci/cpu/build.sh index 2c6dc899be2..4f46938ee49 100755 --- a/ci/cpu/build.sh +++ b/ci/cpu/build.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Copyright (c) 2018-2020, NVIDIA CORPORATION. +# Copyright (c) 2018-2021, NVIDIA CORPORATION. ######################################### # cuGraph CPU conda build script for CI # ######################################### @@ -24,6 +24,10 @@ fi export GPUCI_CONDA_RETRY_MAX=1 export GPUCI_CONDA_RETRY_SLEEP=30 +# Use Ninja to build +export CMAKE_GENERATOR="Ninja" +export CONDA_BLD_DIR="${WORKSPACE}/.conda-bld" + ################################################################################ # SETUP - Check environment ################################################################################ @@ -35,6 +39,11 @@ gpuci_logger "Activate conda env" . /opt/conda/etc/profile.d/conda.sh conda activate rapids +# Remove rapidsai-nightly channel if we are building main branch +if [ "$SOURCE_BRANCH" = "main" ]; then + conda config --system --remove channels rapidsai-nightly +fi + gpuci_logger "Check versions" python --version $CC --version @@ -55,18 +64,20 @@ conda config --set ssl_verify False gpuci_logger "Build conda pkg for libcugraph" if [ "$BUILD_LIBCUGRAPH" == '1' ]; then if [[ -z "$PROJECT_FLASH" || "$PROJECT_FLASH" == "0" ]]; then - conda build conda/recipes/libcugraph + gpuci_conda_retry build --no-build-id --croot ${CONDA_BLD_DIR} conda/recipes/libcugraph else - conda build --dirty --no-remove-work-dir conda/recipes/libcugraph + gpuci_conda_retry build --no-build-id --croot ${CONDA_BLD_DIR} --dirty --no-remove-work-dir conda/recipes/libcugraph + mkdir -p ${CONDA_BLD_DIR}/libcugraph/work + cp -r ${CONDA_BLD_DIR}/work/* ${CONDA_BLD_DIR}/libcugraph/work fi fi gpuci_logger "Build conda pkg for cugraph" if [ "$BUILD_CUGRAPH" == "1" ]; then if [[ -z "$PROJECT_FLASH" || "$PROJECT_FLASH" == "0" ]]; then - conda build conda/recipes/cugraph --python=$PYTHON + gpuci_conda_retry build --croot ${CONDA_BLD_DIR} conda/recipes/cugraph --python=$PYTHON else - conda build conda/recipes/cugraph -c ci/artifacts/cugraph/cpu/conda-bld/ --dirty --no-remove-work-dir --python=$PYTHON + gpuci_conda_retry build --croot ${CONDA_BLD_DIR} conda/recipes/cugraph -c ci/artifacts/cugraph/cpu/.conda-bld/ --dirty --no-remove-work-dir --python=$PYTHON fi fi diff --git a/ci/cpu/prebuild.sh b/ci/cpu/prebuild.sh index ee471329b35..9f2629d153c 100644 --- a/ci/cpu/prebuild.sh +++ b/ci/cpu/prebuild.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# Copyright (c) 2018-2020, NVIDIA CORPORATION. +# Copyright (c) 2018-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. # You may obtain a copy of the License at @@ -18,12 +18,6 @@ if [[ -z "$PROJECT_FLASH" || "$PROJECT_FLASH" == "0" ]]; then export BUILD_LIBCUGRAPH=1 fi -if [[ "$CUDA" == "10.1" ]]; then - export UPLOAD_CUGRAPH=1 -else - export UPLOAD_CUGRAPH=0 -fi - if [[ "$PYTHON" == "3.7" ]]; then export UPLOAD_LIBCUGRAPH=1 else diff --git a/ci/cpu/upload.sh b/ci/cpu/upload.sh index 0fca82216c3..11f28366c5f 100644 --- a/ci/cpu/upload.sh +++ b/ci/cpu/upload.sh @@ -1,4 +1,5 @@ #!/bin/bash +# Copyright (c) 2018-2021, NVIDIA CORPORATION. # # Adopted from https://github.com/tmcdonell/travis-scripts/blob/dfaac280ac2082cd6bcaba3217428347899f2975/update-accelerate-buildbot.sh @@ -29,8 +30,8 @@ fi gpuci_logger "Get conda file output locations" -export LIBCUGRAPH_FILE=`conda build conda/recipes/libcugraph --output` -export CUGRAPH_FILE=`conda build conda/recipes/cugraph --python=$PYTHON --output` +export LIBCUGRAPH_FILE=`conda build --no-build-id --croot ${CONDA_BLD_DIR} conda/recipes/libcugraph --output` +export CUGRAPH_FILE=`conda build --croot ${CONDA_BLD_DIR} conda/recipes/cugraph --python=$PYTHON --output` ################################################################################ # UPLOAD - Conda packages @@ -42,13 +43,13 @@ if [[ "$BUILD_LIBCUGRAPH" == "1" && "$UPLOAD_LIBCUGRAPH" == "1" ]]; then test -e ${LIBCUGRAPH_FILE} echo "Upload libcugraph" echo ${LIBCUGRAPH_FILE} - gpuci_retry anaconda -t ${MY_UPLOAD_KEY} upload -u ${CONDA_USERNAME:-rapidsai} ${LABEL_OPTION} --skip-existing ${LIBCUGRAPH_FILE} + gpuci_retry anaconda -t ${MY_UPLOAD_KEY} upload -u ${CONDA_USERNAME:-rapidsai} ${LABEL_OPTION} --skip-existing ${LIBCUGRAPH_FILE} --no-progress fi -if [[ "$BUILD_CUGRAPH" == "1" && "$UPLOAD_CUGRAPH" == "1" ]]; then +if [[ "$BUILD_CUGRAPH" == "1" ]]; then test -e ${CUGRAPH_FILE} echo "Upload cugraph" echo ${CUGRAPH_FILE} - gpuci_retry anaconda -t ${MY_UPLOAD_KEY} upload -u ${CONDA_USERNAME:-rapidsai} ${LABEL_OPTION} --skip-existing ${CUGRAPH_FILE} + gpuci_retry anaconda -t ${MY_UPLOAD_KEY} upload -u ${CONDA_USERNAME:-rapidsai} ${LABEL_OPTION} --skip-existing ${CUGRAPH_FILE} --no-progress fi diff --git a/ci/docs/build.sh b/ci/docs/build.sh index 6ce223d8b2b..2135ff04b45 100644 --- a/ci/docs/build.sh +++ b/ci/docs/build.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Copyright (c) 2020, NVIDIA CORPORATION. +# Copyright (c) 2021, NVIDIA CORPORATION. ################################# # cuGraph Docs build script for CI # ################################# @@ -15,7 +15,6 @@ export PATH=/conda/bin:/usr/local/cuda/bin:$PATH export HOME=$WORKSPACE export PROJECT_WORKSPACE=/rapids/cugraph export LIBCUDF_KERNEL_CACHE_PATH="$HOME/.jitify-cache" -export NIGHTLY_VERSION=$(echo $BRANCH_VERSION | awk -F. '{print $2}') export PROJECTS=(cugraph libcugraph) gpuci_logger "Check environment" @@ -28,11 +27,6 @@ gpuci_logger "Activate conda env" . /opt/conda/etc/profile.d/conda.sh conda activate rapids -# TODO: Move installs to docs-build-env meta package -gpuci_conda_retry install -c anaconda markdown beautifulsoup4 jq -pip install sphinx-markdown-tables - - gpuci_logger "Check versions" python --version $CC --version @@ -47,10 +41,10 @@ conda list --show-channel-urls gpuci_logger "Build Doxygen docs" cd $PROJECT_WORKSPACE/cpp/build make docs_cugraph - + # Build Python docs gpuci_logger "Build Sphinx docs" -cd $PROJECT_WORKSPACE/docs +cd $PROJECT_WORKSPACE/docs/cugraph make html #Commit to Website @@ -60,10 +54,10 @@ for PROJECT in ${PROJECTS[@]}; do if [ ! -d "api/$PROJECT/$BRANCH_VERSION" ]; then mkdir -p api/$PROJECT/$BRANCH_VERSION fi - rm -rf $DOCS_WORKSPACE/api/$PROJECT/$BRANCH_VERSION/* + rm -rf $DOCS_WORKSPACE/api/$PROJECT/$BRANCH_VERSION/* done mv $PROJECT_WORKSPACE/cpp/doxygen/html/* $DOCS_WORKSPACE/api/libcugraph/$BRANCH_VERSION -mv $PROJECT_WORKSPACE/docs/build/html/* $DOCS_WORKSPACE/api/cugraph/$BRANCH_VERSION +mv $PROJECT_WORKSPACE/docs/cugraph/build/html/* $DOCS_WORKSPACE/api/cugraph/$BRANCH_VERSION diff --git a/ci/getGTestTimes.sh b/ci/getGTestTimes.sh deleted file mode 100755 index 8a3752d76e2..00000000000 --- a/ci/getGTestTimes.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/bash -# Copyright (c) 2019-2020, NVIDIA CORPORATION. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# This script will print the gtest results sorted by runtime. This will print -# the results two ways: first by printing all tests sorted by runtime, then by -# printing all tests grouped by test binary with tests sorted by runtime within -# the group. -# -# To use this script, capture the test run output to a file then run this script -# with the file as the first arg, or just redirect test output to this script. - -awk '/^Running GoogleTest .+$/ { - testbinary = $3 - } - /^\[ OK \].+$/ { - testtime = substr($(NF-1),2) - newtestdata = testbinary ":" substr($0,14) - alltestdata = alltestdata newtestdata "\n" - testdata[testbinary] = testdata[testbinary] newtestdata "\n" - totaltime = totaltime + testtime - } - END { - # Print all tests sorted by time - system("echo \"" alltestdata "\" | sort -r -t\\( -nk2") - print "\n================================================================================" - # Print test binaries with tests sorted by time - print "Tests grouped by test binary:" - for (testbinary in testdata) { - print testbinary - system("echo \"" testdata[testbinary] "\" | sort -r -t\\( -nk2") - } - print "\n================================================================================" - print totaltime " milliseconds = " totaltime/60000 " minutes" - } -' $1 diff --git a/ci/gpu/build.sh b/ci/gpu/build.sh index 9dd6e14181e..02e139fc05e 100755 --- a/ci/gpu/build.sh +++ b/ci/gpu/build.sh @@ -1,10 +1,10 @@ #!/usr/bin/env bash -# Copyright (c) 2018-2020, NVIDIA CORPORATION. +# Copyright (c) 2018-2021, NVIDIA CORPORATION. ########################################## # cuGraph GPU build & testscript for CI # ########################################## -set -e -set -o pipefail +set -e # abort the script on error, this will change for running tests (see below) +set -o pipefail # piped commands propagate their error NUMARGS=$# ARGS=$* @@ -16,6 +16,7 @@ function hasArg { export PATH=/opt/conda/bin:/usr/local/cuda/bin:$PATH export PARALLEL_LEVEL=${PARALLEL_LEVEL:-4} export CUDA_REL=${CUDA_VERSION%.*} +export CONDA_ARTIFACT_PATH=${WORKSPACE}/ci/artifacts/cugraph/cpu/.conda-bld/ function cleanup { gpuci_logger "Removing datasets and temp files" @@ -61,7 +62,7 @@ gpuci_conda_retry install -y \ "cudatoolkit=$CUDA_REL" \ "dask-cudf=${MINOR_VERSION}" \ "dask-cuda=${MINOR_VERSION}" \ - "ucx-py=${MINOR_VERSION}" \ + "ucx-py=0.21.*" \ "ucx-proc=*=gpu" \ "rapids-build-env=$MINOR_VERSION.*" \ "rapids-notebook-env=$MINOR_VERSION.*" \ @@ -71,10 +72,6 @@ gpuci_conda_retry install -y \ # gpuci_conda_retry remove --force rapids-build-env rapids-notebook-env # gpuci_conda_retry install -y "your-pkg=1.0.0" -gpuci_logger "Install the master version of dask and distributed" -pip install "git+https://github.com/dask/distributed.git" --upgrade --no-deps -pip install "git+https://github.com/dask/dask.git" --upgrade --no-deps - gpuci_logger "Check versions" python --version $CC --version @@ -90,18 +87,44 @@ conda list --show-channel-urls ################################################################################ if [[ -z "$PROJECT_FLASH" || "$PROJECT_FLASH" == "0" ]]; then - gpuci_logger "Build from source" - $WORKSPACE/build.sh -v clean libcugraph cugraph --allgpuarch + gpuci_logger "Build from source" + $WORKSPACE/build.sh -v clean libcugraph cugraph +else + export LIBCUGRAPH_BUILD_DIR="$WORKSPACE/ci/artifacts/cugraph/cpu/conda_work/cpp/build" + + # Faiss patch + echo "Update libcugraph.so" + cd $LIBCUGRAPH_BUILD_DIR + chrpath -d libcugraph.so + patchelf --replace-needed `patchelf --print-needed libcugraph.so | grep faiss` libfaiss.so libcugraph.so + + CONDA_FILE=`find ${CONDA_ARTIFACT_PATH} -name "libcugraph*.tar.bz2"` + CONDA_FILE=`basename "$CONDA_FILE" .tar.bz2` #get filename without extension + CONDA_FILE=${CONDA_FILE//-/=} #convert to conda install + echo "Installing $CONDA_FILE" + conda install -c ${CONDA_ARTIFACT_PATH} "$CONDA_FILE" + + gpuci_logger "Install the master version of dask and distributed" + pip install "git+https://github.com/dask/distributed.git" --upgrade --no-deps + pip install "git+https://github.com/dask/dask.git" --upgrade --no-deps + + echo "Build cugraph..." + $WORKSPACE/build.sh cugraph fi ################################################################################ # TEST - Run GoogleTest and py.tests for libcugraph and cuGraph ################################################################################ -set +e -Eo pipefail -EXITCODE=0 +# Switch to +e to allow failing commands to continue the script, which is needed +# so all testing commands run regardless of pass/fail. This requires the desired +# exit code to be managed using the ERR trap. +set +e # allow script to continue on error +set -E # ERR traps are inherited by subcommands trap "EXITCODE=1" ERR +EXITCODE=0 + if hasArg --skip-tests; then gpuci_logger "Skipping Tests" else @@ -117,18 +140,19 @@ else TEST_MODE_FLAG="" fi + gpuci_logger "Running cuGraph test.sh..." ${WORKSPACE}/ci/test.sh ${TEST_MODE_FLAG} | tee testoutput.txt + gpuci_logger "Ran cuGraph test.sh : return code was: $?, gpu/build.sh exit code is now: $EXITCODE" - echo -e "\nTOP 20 SLOWEST TESTS:\n" - # Wrap in echo to prevent non-zero exit since this command is non-essential - echo "$(${WORKSPACE}/ci/getGTestTimes.sh testoutput.txt | head -20)" - + gpuci_logger "Running cuGraph notebook test script..." ${WORKSPACE}/ci/gpu/test-notebooks.sh 2>&1 | tee nbtest.log + gpuci_logger "Ran cuGraph notebook test script : return code was: $?, gpu/build.sh exit code is now: $EXITCODE" python ${WORKSPACE}/ci/utils/nbtestlog2junitxml.py nbtest.log fi -if [ -n "\${CODECOV_TOKEN}" ]; then - codecov -t \$CODECOV_TOKEN +if [ -n "${CODECOV_TOKEN}" ]; then + codecov -t $CODECOV_TOKEN fi +gpuci_logger "gpu/build.sh returning value: $EXITCODE" return ${EXITCODE} diff --git a/ci/gpu/notebook_list.py b/ci/gpu/notebook_list.py new file mode 100644 index 00000000000..23a198830a8 --- /dev/null +++ b/ci/gpu/notebook_list.py @@ -0,0 +1,56 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import re +import sys +import glob + +from numba import cuda + +cuda_version_string = ".".join([str(n) for n in cuda.runtime.get_version()]) +# +# Not strictly true... however what we mean is +# Pascal or earlier +# +pascal = False +device = cuda.get_current_device() +# check for the attribute using both pre and post numba 0.53 names +cc = getattr(device, 'COMPUTE_CAPABILITY', None) or \ + getattr(device, 'compute_capability') +if (cc[0] < 7): + pascal = True + +for filename in glob.iglob('**/*.ipynb', recursive=True): + skip = False + for line in open(filename, 'r'): + if re.search('# Skip notebook test', line): + skip = True + print(f'SKIPPING {filename} (marked as skip)', file=sys.stderr) + break; + elif re.search('dask', line): + print(f'SKIPPING {filename} (suspected Dask usage, not currently automatable)', file=sys.stderr) + skip = True + break; + elif pascal and re.search('# Does not run on Pascal', line): + print(f'SKIPPING {filename} (does not run on Pascal)', file=sys.stderr) + skip = True + break; + elif re.search('# Does not run on CUDA ', line) and \ + (cuda_version_string in line): + print(f'SKIPPING {filename} (does not run on CUDA {cuda_version_string})', + file=sys.stderr) + skip = True + break; + + if not skip: + print(filename) diff --git a/ci/gpu/test-notebooks.sh b/ci/gpu/test-notebooks.sh index 389d3be0bfd..650132f116d 100755 --- a/ci/gpu/test-notebooks.sh +++ b/ci/gpu/test-notebooks.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -12,23 +12,27 @@ # See the License for the specific language governing permissions and # limitations under the License. -#RAPIDS_DIR=/rapids +# Any failing command will set EXITCODE to non-zero +set -e # abort the script on error, this will change for running tests (see below) +set -o pipefail # piped commands propagate their error +set -E # ERR traps are inherited by subcommands +trap "EXITCODE=1" ERR + NOTEBOOKS_DIR=${WORKSPACE}/notebooks NBTEST=${WORKSPACE}/ci/utils/nbtest.sh LIBCUDF_KERNEL_CACHE_PATH=${WORKSPACE}/.jitcache +EXITCODE=0 cd ${NOTEBOOKS_DIR} TOPLEVEL_NB_FOLDERS=$(find . -name *.ipynb |cut -d'/' -f2|sort -u) -# Add notebooks that should be skipped here -# (space-separated list of filenames without paths) - -SKIPNBS="uvm.ipynb bfs_benchmark.ipynb louvain_benchmark.ipynb pagerank_benchmark.ipynb sssp_benchmark.ipynb release.ipynb nx_cugraph_bc_benchmarking.ipynb" - ## Check env env -EXITCODE=0 +# Do not abort the script on error. This allows all tests to run regardless of +# pass/fail but relies on the ERR trap above to manage the EXITCODE for the +# script. +set +e # Always run nbtest in all TOPLEVEL_NB_FOLDERS, set EXITCODE to failure # if any run fails @@ -37,29 +41,20 @@ for folder in ${TOPLEVEL_NB_FOLDERS}; do echo "FOLDER: ${folder}" echo "========================================" cd ${NOTEBOOKS_DIR}/${folder} - for nb in $(find . -name "*.ipynb"); do + NBLIST=$(python ${WORKSPACE}/ci/gpu/notebook_list.py) + for nb in ${NBLIST}; do nbBasename=$(basename ${nb}) - # Skip all NBs that use dask (in the code or even in their name) - if ((echo ${nb}|grep -qi dask) || \ - (grep -q dask ${nb})); then - echo "--------------------------------------------------------------------------------" - echo "SKIPPING: ${nb} (suspected Dask usage, not currently automatable)" - echo "--------------------------------------------------------------------------------" - elif (echo " ${SKIPNBS} " | grep -q " ${nbBasename} "); then - echo "--------------------------------------------------------------------------------" - echo "SKIPPING: ${nb} (listed in skip list)" - echo "--------------------------------------------------------------------------------" - else - cd $(dirname ${nb}) - nvidia-smi - ${NBTEST} ${nbBasename} - EXITCODE=$((EXITCODE | $?)) - rm -rf ${LIBCUDF_KERNEL_CACHE_PATH}/* - cd ${NOTEBOOKS_DIR}/${folder} - fi + cd $(dirname ${nb}) + nvidia-smi + ${NBTEST} ${nbBasename} + echo "Ran nbtest for $nb : return code was: $?, test script exit code is now: $EXITCODE" + echo + rm -rf ${LIBCUDF_KERNEL_CACHE_PATH}/* + cd ${NOTEBOOKS_DIR}/${folder} done done nvidia-smi +echo "Notebook test script exiting with value: $EXITCODE" exit ${EXITCODE} diff --git a/ci/release/update-version.sh b/ci/release/update-version.sh index d853c3693c6..2ff14c6c6e9 100755 --- a/ci/release/update-version.sh +++ b/ci/release/update-version.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Copyright (c) 2018-2020, NVIDIA CORPORATION. +# Copyright (c) 2018-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. # You may obtain a copy of the License at @@ -13,42 +13,25 @@ # limitations under the License. ## Usage -# bash update-version.sh -# where is either `major`, `minor`, `patch` +# bash update-version.sh -set -e -# Grab argument for release type -RELEASE_TYPE=$1 +# Format is YY.MM.PP - no leading 'v' or trailing 'a' +NEXT_FULL_TAG=$1 -# Get current version and calculate next versions -CURRENT_TAG=`git tag | grep -xE 'v[0-9\.]+' | sort --version-sort | tail -n 1 | tr -d 'v'` -CURRENT_MAJOR=`echo $CURRENT_TAG | awk '{split($0, a, "."); print a[1]}'` -CURRENT_MINOR=`echo $CURRENT_TAG | awk '{split($0, a, "."); print a[2]}'` -CURRENT_PATCH=`echo $CURRENT_TAG | awk '{split($0, a, "."); print a[3]}'` +# Get current version +CURRENT_TAG=$(git tag --merged HEAD | grep -xE '^v.*' | sort --version-sort | tail -n 1 | tr -d 'v') +CURRENT_MAJOR=$(echo $CURRENT_TAG | awk '{split($0, a, "."); print a[1]}') +CURRENT_MINOR=$(echo $CURRENT_TAG | awk '{split($0, a, "."); print a[2]}') +CURRENT_PATCH=$(echo $CURRENT_TAG | awk '{split($0, a, "."); print a[3]}') CURRENT_SHORT_TAG=${CURRENT_MAJOR}.${CURRENT_MINOR} -NEXT_MAJOR=$((CURRENT_MAJOR + 1)) -NEXT_MINOR=$((CURRENT_MINOR + 1)) -NEXT_PATCH=$((CURRENT_PATCH + 1)) -NEXT_FULL_TAG="" -NEXT_SHORT_TAG="" -# Determine release type -if [ "$RELEASE_TYPE" == "major" ]; then - NEXT_FULL_TAG="${NEXT_MAJOR}.0.0" - NEXT_SHORT_TAG="${NEXT_MAJOR}.0" -elif [ "$RELEASE_TYPE" == "minor" ]; then - NEXT_FULL_TAG="${CURRENT_MAJOR}.${NEXT_MINOR}.0" - NEXT_SHORT_TAG="${CURRENT_MAJOR}.${NEXT_MINOR}" -elif [ "$RELEASE_TYPE" == "patch" ]; then - NEXT_FULL_TAG="${CURRENT_MAJOR}.${CURRENT_MINOR}.${NEXT_PATCH}" - NEXT_SHORT_TAG="${CURRENT_MAJOR}.${CURRENT_MINOR}" -else - echo "Incorrect release type; use 'major', 'minor', or 'patch' as an argument" - exit 1 -fi +#Get . for next version +NEXT_MAJOR=$(echo $NEXT_FULL_TAG | awk '{split($0, a, "."); print a[1]}') +NEXT_MINOR=$(echo $NEXT_FULL_TAG | awk '{split($0, a, "."); print a[2]}') +NEXT_SHORT_TAG=${NEXT_MAJOR}.${NEXT_MINOR} -echo "Preparing '$RELEASE_TYPE' release [$CURRENT_TAG -> $NEXT_FULL_TAG]" +echo "Preparing release $CURRENT_TAG => $NEXT_FULL_TAG" # Inplace sed replace; workaround for Linux and Mac function sed_runner() { @@ -59,13 +42,13 @@ function sed_runner() { sed_runner 's/'"CUGRAPH VERSION .* LANGUAGES C CXX CUDA)"'/'"CUGRAPH VERSION ${NEXT_FULL_TAG} LANGUAGES C CXX CUDA)"'/g' cpp/CMakeLists.txt # RTD update -sed_runner 's/version = .*/version = '"'${NEXT_SHORT_TAG}'"'/g' docs/source/conf.py -sed_runner 's/release = .*/release = '"'${NEXT_FULL_TAG}'"'/g' docs/source/conf.py +sed_runner 's/version = .*/version = '"'${NEXT_SHORT_TAG}'"'/g' docs/cugraph/source/conf.py +sed_runner 's/release = .*/release = '"'${NEXT_FULL_TAG}'"'/g' docs/cugraph/source/conf.py for FILE in conda/environments/*.yml; do sed_runner "s/cudf=${CURRENT_SHORT_TAG}/cudf=${NEXT_SHORT_TAG}/g" ${FILE}; sed_runner "s/rmm=${CURRENT_SHORT_TAG}/rmm=${NEXT_SHORT_TAG}/g" ${FILE}; sed_runner "s/dask-cuda=${CURRENT_SHORT_TAG}/dask-cuda=${NEXT_SHORT_TAG}/g" ${FILE}; sed_runner "s/dask-cudf=${CURRENT_SHORT_TAG}/dask-cudf=${NEXT_SHORT_TAG}/g" ${FILE}; - sed_runner "s/ucx-py=${CURRENT_SHORT_TAG}/ucx-py=${NEXT_SHORT_TAG}/g" ${FILE}; + sed_runner "s/cuxfilter=${CURRENT_SHORT_TAG}/cuxfilter=${NEXT_SHORT_TAG}/g" ${FILE}; done diff --git a/ci/test.sh b/ci/test.sh index db9390461c0..31660cd15ec 100755 --- a/ci/test.sh +++ b/ci/test.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -12,9 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -# note: do not use set -e in order to allow all gtest invocations to take place, -# and instead keep track of exit status and exit with an overall exit status -set -o pipefail +# Any failing command will set EXITCODE to non-zero +set -e # abort the script on error, this will change for running tests (see below) +set -o pipefail # piped commands propagate their error +set -E # ERR traps are inherited by subcommands +trap "EXITCODE=1" ERR NUMARGS=$# ARGS=$* @@ -22,7 +24,7 @@ THISDIR=$(cd $(dirname $0);pwd) CUGRAPH_ROOT=$(cd ${THISDIR}/..;pwd) GTEST_ARGS="--gtest_output=xml:${CUGRAPH_ROOT}/test-results/" DOWNLOAD_MODE="" -ERRORCODE=0 +EXITCODE=0 export RAPIDS_DATASET_ROOT_DIR=${CUGRAPH_ROOT}/datasets @@ -50,47 +52,41 @@ else echo "Download datasets..." cd ${RAPIDS_DATASET_ROOT_DIR} bash ./get_test_data.sh ${DOWNLOAD_MODE} - ERRORCODE=$((ERRORCODE | $?)) - # no need to run tests if dataset download fails - if (( ${ERRORCODE} != 0 )); then - exit ${ERRORCODE} - fi fi if [[ -z "$PROJECT_FLASH" || "$PROJECT_FLASH" == "0" ]]; then cd ${CUGRAPH_ROOT}/cpp/build else - export LD_LIBRARY_PATH="$WORKSPACE/ci/artifacts/cugraph/cpu/conda_work/cpp/build:$LD_LIBRARY_PATH" + export LD_LIBRARY_PATH="$WORKSPACE/ci/artifacts/cugraph/cpu/conda_work/cpp/build:$CONDA_PREFIX/lib:$LD_LIBRARY_PATH" cd $WORKSPACE/ci/artifacts/cugraph/cpu/conda_work/cpp/build fi -for gt in gtests/*; do - test_name=$(basename $gt) - echo "Running GoogleTest $test_name" - ${gt} ${GTEST_FILTER} ${GTEST_ARGS} - ERRORCODE=$((ERRORCODE | $?)) -done +# Do not abort the script on error from this point on. This allows all tests to +# run regardless of pass/fail, but relies on the ERR trap above to manage the +# EXITCODE for the script. +set +e -if [[ "$PROJECT_FLASH" == "1" ]]; then - CONDA_FILE=`find $WORKSPACE/ci/artifacts/cugraph/cpu/conda-bld/ -name "libcugraph*.tar.bz2"` - CONDA_FILE=`basename "$CONDA_FILE" .tar.bz2` #get filename without extension - CONDA_FILE=${CONDA_FILE//-/=} #convert to conda install - echo "Installing $CONDA_FILE" - conda install -c $WORKSPACE/ci/artifacts/cugraph/cpu/conda-bld/ "$CONDA_FILE" - - export LIBCUGRAPH_BUILD_DIR="$WORKSPACE/ci/artifacts/cugraph/cpu/conda_work/cpp/build" - echo "Build cugraph..." - $WORKSPACE/build.sh cugraph +if (python ${CUGRAPH_ROOT}/ci/utils/is_pascal.py); then + echo "WARNING: skipping C++ tests on Pascal GPU arch." +else + echo "C++ gtests for cuGraph..." + for gt in tests/*_TEST; do + test_name=$(basename $gt) + echo "Running gtest $test_name" + ${gt} ${GTEST_FILTER} ${GTEST_ARGS} + echo "Ran gtest $test_name : return code was: $?, test script exit code is now: $EXITCODE" + done fi echo "Python pytest for cuGraph..." cd ${CUGRAPH_ROOT}/python pytest --cache-clear --junitxml=${CUGRAPH_ROOT}/junit-cugraph.xml -v --cov-config=.coveragerc --cov=cugraph --cov-report=xml:${WORKSPACE}/python/cugraph/cugraph-coverage.xml --cov-report term --ignore=cugraph/raft --benchmark-disable -ERRORCODE=$((ERRORCODE | $?)) +echo "Ran Python pytest for cugraph : return code was: $?, test script exit code is now: $EXITCODE" echo "Python benchmarks for cuGraph (running as tests)..." cd ${CUGRAPH_ROOT}/benchmarks pytest -v -m "managedmem_on and poolallocator_on and tiny" --benchmark-disable -ERRORCODE=$((ERRORCODE | $?)) +echo "Ran Python benchmarks for cuGraph (running as tests) : return code was: $?, test script exit code is now: $EXITCODE" -exit ${ERRORCODE} +echo "Test script exiting with value: $EXITCODE" +exit ${EXITCODE} diff --git a/ci/utils/git_helpers.py b/ci/utils/git_helpers.py index 83ad73fe283..a0c413b75f4 100644 --- a/ci/utils/git_helpers.py +++ b/ci/utils/git_helpers.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. @@ -59,14 +59,24 @@ def uncommittedFiles(): return ret -def changedFilesBetween(b1, b2): - """Returns a list of files changed between branches b1 and b2""" +def changedFilesBetween(baseName, branchName, commitHash): + """ + Returns a list of files changed between branches baseName and latest commit + of branchName. + """ current = branch() - __git("checkout", "--quiet", b1) - __git("checkout", "--quiet", b2) - files = __gitdiff("--name-only", "--ignore-submodules", "%s...%s" % - (b1, b2)) - __git("checkout", "--quiet", current) + # checkout "base" branch + __git("checkout", "--force", baseName) + # checkout branch for comparing + __git("checkout", "--force", branchName) + # checkout latest commit from branch + __git("checkout", "-fq", commitHash) + + files = __gitdiff("--name-only", "--ignore-submodules", + f"{baseName}..{branchName}") + + # restore the original branch + __git("checkout", "--force", current) return files.splitlines() @@ -87,10 +97,10 @@ def changesInFileBetween(file, b1, b2, pathFilter=None): def modifiedFiles(pathFilter=None): """ - If inside a CI-env (ie. currentBranch=current-pr-branch and the env-var - PR_TARGET_BRANCH is defined), then lists out all files modified between - these 2 branches. Else, lists out all the uncommitted files in the current - branch. + If inside a CI-env (ie. TARGET_BRANCH and COMMIT_HASH are defined, and + current branch is "current-pr-branch"), then lists out all files modified + between these 2 branches. Else, lists out all the uncommitted files in the + current branch. Such utility function is helpful while putting checker scripts as part of cmake, as well as CI process. This way, during development, only the files @@ -98,15 +108,26 @@ def modifiedFiles(pathFilter=None): process ALL files modified by the dev, as submiited in the PR, will be checked. This happens, all the while using the same script. """ - if "PR_TARGET_BRANCH" in os.environ and branch() == "current-pr-branch": - allFiles = changedFilesBetween(os.environ["PR_TARGET_BRANCH"], - branch()) + targetBranch = os.environ.get("TARGET_BRANCH") + commitHash = os.environ.get("COMMIT_HASH") + currentBranch = branch() + print(f" [DEBUG] TARGET_BRANCH={targetBranch}, COMMIT_HASH={commitHash}, " + f"currentBranch={currentBranch}") + + if targetBranch and commitHash and (currentBranch == "current-pr-branch"): + print(" [DEBUG] Assuming a CI environment.") + allFiles = changedFilesBetween(targetBranch, currentBranch, commitHash) else: + print(" [DEBUG] Did not detect CI environment.") allFiles = uncommittedFiles() + files = [] for f in allFiles: if pathFilter is None or pathFilter(f): files.append(f) + + filesToCheckString = "\n\t".join(files) if files else "" + print(f" [DEBUG] Found files to check:\n\t{filesToCheckString}\n") return files diff --git a/ci/utils/is_pascal.py b/ci/utils/is_pascal.py new file mode 100644 index 00000000000..e55a3153a12 --- /dev/null +++ b/ci/utils/is_pascal.py @@ -0,0 +1,39 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import re +import sys +import glob + +from numba import cuda + +# FIXME: consolidate this code with ci/gpu/notebook_list.py + +# +# Not strictly true... however what we mean is +# Pascal or earlier +# +pascal = False + +device = cuda.get_current_device() +# check for the attribute using both pre and post numba 0.53 names +cc = getattr(device, 'COMPUTE_CAPABILITY', None) or \ + getattr(device, 'compute_capability') +if (cc[0] < 7): + pascal = True + +# Return zero (success) if pascal is True +if pascal: + sys.exit(0) +else: + sys.exit(1) diff --git a/ci/utils/nbtest.sh b/ci/utils/nbtest.sh index 8c86baeaa09..ae8b52df106 100755 --- a/ci/utils/nbtest.sh +++ b/ci/utils/nbtest.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -12,6 +12,19 @@ # See the License for the specific language governing permissions and # limitations under the License. +# Any failing command will set EXITCODE to non-zero +set +e # do not abort the script on error +set -o pipefail # piped commands propagate their error +set -E # ERR traps are inherited by subcommands +trap "EXITCODE=1" ERR + +# Prepend the following code to all scripts generated from nbconvert. This +# allows all cell and line magic code to run and update the namespace as if +# running in jupyter, but will also tolerate failures due to running in a +# non-jupyter env. +# Note: depending on the assumptions of the notebook script, ignoring failures +# may not be acceptable (meaning the converted notebook simply cannot run +# outside of jupyter as-is), hence the warning. MAGIC_OVERRIDE_CODE=" def my_run_line_magic(*args, **kwargs): g=globals() @@ -58,7 +71,6 @@ for nb in $*; do NBEXITCODE=$? echo EXIT CODE: ${NBEXITCODE} echo - EXITCODE=$((EXITCODE | ${NBEXITCODE})) done exit ${EXITCODE} diff --git a/conda/environments/builddocs.yml b/conda/environments/builddocs.yml deleted file mode 100644 index 89bd44a5542..00000000000 --- a/conda/environments/builddocs.yml +++ /dev/null @@ -1,19 +0,0 @@ -name: builddocs -channels: -- rapidsai -- pytorch -- conda-forge -- numba -- defaults -dependencies: -- python=3.6* -- cugraph=0.8* -- cudatoolkit=9.2 -- cudf=0.8* -- pyarrow=0.12.1.* -- cython=0.29* -- pip: - - numpydoc - - sphinx - - sphinx-rtd-theme - - sphinxcontrib-websupport diff --git a/conda/environments/cugraph_dev_cuda10.2.yml b/conda/environments/cugraph_dev_cuda10.2.yml deleted file mode 100644 index 6526dd73f98..00000000000 --- a/conda/environments/cugraph_dev_cuda10.2.yml +++ /dev/null @@ -1,47 +0,0 @@ -name: cugraph_dev -channels: -- rapidsai -- nvidia -- rapidsai-nightly -- conda-forge -dependencies: -- cudf=0.17.* -- libcudf=0.17.* -- rmm=0.17.* -- cuxfilter=0.17.* -- librmm=0.17.* -- dask>=2.12.0 -- distributed>=2.12.0 -- dask-cuda=0.17* -- dask-cudf=0.17* -- nccl>=2.7 -- ucx-py=0.17* -- ucx-proc=*=gpu -- scipy -- networkx -- python-louvain -- cudatoolkit=10.2 -- clang=8.0.1 -- clang-tools=8.0.1 -- cmake>=3.12 -- python>=3.6,<3.9 -- notebook>=0.5.0 -- boost -- cython>=0.29,<0.30 -- pytest -- scikit-learn>=0.23.1 -- colorcet -- holoviews -- datashader -- sphinx -- sphinx_rtd_theme -- sphinxcontrib-websupport -- sphinx-markdown-tables -- nbsphinx -- numpydoc -- ipython -- recommonmark -- pip -- libcypher-parser -- rapids-pytest-benchmark -- doxygen diff --git a/conda/environments/cugraph_dev_cuda11.0.yml b/conda/environments/cugraph_dev_cuda11.0.yml index 5016eb9405c..d19ac1bd00e 100644 --- a/conda/environments/cugraph_dev_cuda11.0.yml +++ b/conda/environments/cugraph_dev_cuda11.0.yml @@ -5,43 +5,43 @@ channels: - rapidsai-nightly - conda-forge dependencies: -- cudf=0.17.* -- libcudf=0.17.* -- rmm=0.17.* -- cuxfilter=0.17.* -- librmm=0.17.* -- dask>=2.12.0 -- distributed>=2.12.0 -- dask-cuda=0.17* -- dask-cudf=0.17* -- nccl>=2.7 -- ucx-py=0.17* +- cudatoolkit=11.0 +- cudf=21.08.* +- libcudf=21.08.* +- rmm=21.08.* +- librmm=21.08.* +- dask>=2021.6.0,<=2021.07.1 +- distributed>=2021.6.0,<=2021.07.1 +- dask-cuda=21.08.* +- dask-cudf=21.08.* +- nccl>=2.9.9 +- ucx-py=0.21.* - ucx-proc=*=gpu - scipy -- networkx -- python-louvain -- cudatoolkit=11.0 -- clang=8.0.1 -- clang-tools=8.0.1 -- cmake>=3.12 +- networkx>=2.5.1 +- clang=11.0.0 +- clang-tools=11.0.0 +- cmake>=3.20.1 - python>=3.6,<3.9 - notebook>=0.5.0 - boost - cython>=0.29,<0.30 - pytest +- libfaiss=1.7.0 +- faiss-proc=*=cuda - scikit-learn>=0.23.1 -- colorcet -- datashader -- holoviews - sphinx - sphinx_rtd_theme - sphinxcontrib-websupport - sphinx-markdown-tables +- sphinx-copybutton - nbsphinx - numpydoc - ipython - recommonmark - pip -- libcypher-parser - rapids-pytest-benchmark - doxygen +- pytest-cov +- gtest +- gmock diff --git a/conda/environments/cugraph_dev_cuda10.1.yml b/conda/environments/cugraph_dev_cuda11.2.yml similarity index 50% rename from conda/environments/cugraph_dev_cuda10.1.yml rename to conda/environments/cugraph_dev_cuda11.2.yml index 9b4274abef5..7d2f3d26ef5 100644 --- a/conda/environments/cugraph_dev_cuda10.1.yml +++ b/conda/environments/cugraph_dev_cuda11.2.yml @@ -5,43 +5,43 @@ channels: - rapidsai-nightly - conda-forge dependencies: -- cudf=0.17.* -- libcudf=0.17.* -- rmm=0.17.* -- cuxfilter=0.17.* -- librmm=0.17.* -- dask>=2.12.0 -- distributed>=2.12.0 -- dask-cuda=0.17* -- dask-cudf=0.17* -- nccl>=2.7 -- ucx-py=0.17* +- cudatoolkit=11.2 +- cudf=21.08.* +- libcudf=21.08.* +- rmm=21.08.* +- librmm=21.08.* +- dask>=2021.6.0,<=2021.07.1 +- distributed>=2021.6.0,<=2021.07.1 +- dask-cuda=21.08.* +- dask-cudf=21.08.* +- nccl>=2.9.9 +- ucx-py=0.21.* - ucx-proc=*=gpu - scipy -- networkx -- python-louvain -- cudatoolkit=10.1 -- clang=8.0.1 -- clang-tools=8.0.1 -- cmake>=3.12 +- networkx>=2.5.1 +- clang=11.0.0 +- clang-tools=11.0.0 +- cmake>=3.20.1 - python>=3.6,<3.9 - notebook>=0.5.0 - boost - cython>=0.29,<0.30 - pytest +- libfaiss=1.7.0 +- faiss-proc=*=cuda - scikit-learn>=0.23.1 -- colorcet -- holoviews -- datashader - sphinx - sphinx_rtd_theme - sphinxcontrib-websupport - sphinx-markdown-tables +- sphinx-copybutton - nbsphinx - numpydoc - ipython - recommonmark - pip -- libcypher-parser - rapids-pytest-benchmark - doxygen +- pytest-cov +- gtest +- gmock diff --git a/conda/recipes/cugraph/meta.yaml b/conda/recipes/cugraph/meta.yaml index 90f5bed942a..b335ec7753e 100644 --- a/conda/recipes/cugraph/meta.yaml +++ b/conda/recipes/cugraph/meta.yaml @@ -1,20 +1,21 @@ -# Copyright (c) 2018, NVIDIA CORPORATION. +# Copyright (c) 2018-2021, NVIDIA CORPORATION. # Usage: -# conda build -c nvidia -c rapidsai -c conda-forge -c defaults . +# conda build -c nvidia -c rapidsai -c conda-forge . {% set version = environ.get('GIT_DESCRIBE_TAG', '0.0.0.dev').lstrip('v') + environ.get('VERSION_SUFFIX', '') %} {% set minor_version = version.split('.')[0] + '.' + version.split('.')[1] %} +{% set cuda_version='.'.join(environ.get('CUDA', 'unknown').split('.')[:2]) %} {% set py_version=environ.get('CONDA_PY', 36) %} package: name: cugraph version: {{ version }} source: - path: ../../.. + git_url: ../../.. build: number: {{ GIT_DESCRIBE_NUMBER }} - string: py{{ py_version }}_{{ GIT_DESCRIBE_HASH }}_{{ GIT_DESCRIBE_NUMBER }} + string: cuda{{ cuda_version }}_py{{ py_version }}_{{ GIT_DESCRIBE_HASH }}_{{ GIT_DESCRIBE_NUMBER }} script_env: - CC - CXX @@ -27,27 +28,23 @@ requirements: - cython>=0.29,<0.30 - libcugraph={{ version }} - cudf={{ minor_version }} - - ucx-py {{ minor_version }} + - ucx-py 0.21 - ucx-proc=*=gpu + - cudatoolkit {{ cuda_version }}.* run: - python x.x - libcugraph={{ version }} - cudf={{ minor_version }} - dask-cudf {{ minor_version }} - dask-cuda {{ minor_version }} - - dask>=2.12.0 - - distributed>=2.12.0 - - nccl>=2.7 - - ucx-py {{ minor_version }} + - dask>=2021.6.0,<=2021.07.1 + - distributed>=2021.6.0,<=2021.07.1 + - ucx-py 0.21 - ucx-proc=*=gpu - -#test: -# commands: -# - test -f $PREFIX/include/cugraph.h - + - {{ pin_compatible('cudatoolkit', max_pin='x.x') }} about: home: http://rapids.ai/ license: Apache-2.0 license_file: ../../../LICENSE - summary: libcugraph library + summary: cuGraph library diff --git a/conda/recipes/libcugraph/meta.yaml b/conda/recipes/libcugraph/meta.yaml index 211ec920d27..570a0ec09b2 100644 --- a/conda/recipes/libcugraph/meta.yaml +++ b/conda/recipes/libcugraph/meta.yaml @@ -1,7 +1,7 @@ -# Copyright (c) 2018, NVIDIA CORPORATION. +# Copyright (c) 2018-2021, NVIDIA CORPORATION. # Usage: -# conda build -c nvidia -c rapidsai -c conda-forge -c defaults . +# conda build -c nvidia -c rapidsai -c conda-forge . {% set version = environ.get('GIT_DESCRIBE_TAG', '0.0.0.dev').lstrip('v') + environ.get('VERSION_SUFFIX', '') %} {% set minor_version = version.split('.')[0] + '.' + version.split('.')[1] %} {% set cuda_version='.'.join(environ.get('CUDA', '9.2').split('.')[:2]) %} @@ -21,28 +21,32 @@ build: - CUDAHOSTCXX - PARALLEL_LEVEL - VERSION_SUFFIX + - CCACHE_DIR + - CCACHE_NOHASHDIR + - CCACHE_COMPILERCHECK + - CMAKE_GENERATOR + - CMAKE_C_COMPILER_LAUNCHER + - CMAKE_CXX_COMPILER_LAUNCHER + - CMAKE_CUDA_COMPILER_LAUNCHER requirements: build: - - cmake>=3.12.4 - - libcudf={{ minor_version }} + - cmake>=3.20.1 - cudatoolkit {{ cuda_version }}.* + - librmm {{ minor_version }}.* - boost-cpp>=1.66 - - libcypher-parser - - nccl>=2.7 - - ucx-py {{ minor_version }} + - nccl>=2.9.9 - ucx-proc=*=gpu + - gtest + - gmock + - faiss-proc=*=cuda + - libfaiss 1.7.0 *_cuda run: - - libcudf={{ minor_version }} - {{ pin_compatible('cudatoolkit', max_pin='x.x') }} - - nccl>=2.7 - - ucx-py {{ minor_version }} + - nccl>=2.9.9 - ucx-proc=*=gpu - -#test: -# commands: -# - test -f $PREFIX/include/cugraph.h - + - faiss-proc=*=cuda + - libfaiss 1.7.0 *_cuda about: home: http://rapids.ai/ diff --git a/conda_build.sh b/conda_build.sh index 4643e302f5c..1254b7d8d5a 100755 --- a/conda_build.sh +++ b/conda_build.sh @@ -1,13 +1,15 @@ #!/usr/bin/env bash +# Copyright (c) 2021, NVIDIA CORPORATION + set -xe CUDA_REL=${CUDA_VERSION%.*} conda install conda-build anaconda-client conda-verify -y -conda build -c nvidia -c rapidsai -c rapidsai-nightly/label/cuda${CUDA_REL} -c conda-forge -c defaults --python=${PYTHON} conda/recipes/cugraph +conda build -c nvidia -c rapidsai -c rapidsai-nightly/label/cuda${CUDA_REL} -c conda-forge --python=${PYTHON} conda/recipes/cugraph if [ "$UPLOAD_PACKAGE" == '1' ]; then - export UPLOADFILE=`conda build -c nvidia -c rapidsai -c conda-forge -c defaults --python=${PYTHON} conda/recipes/cugraph --output` + export UPLOADFILE=`conda build -c nvidia -c rapidsai -c conda-forge --python=${PYTHON} conda/recipes/cugraph --output` SOURCE_BRANCH=main test -e ${UPLOADFILE} @@ -26,7 +28,7 @@ if [ "$UPLOAD_PACKAGE" == '1' ]; then echo "Upload" echo ${UPLOADFILE} - anaconda -t ${MY_UPLOAD_KEY} upload -u ${CONDA_USERNAME:-rapidsai} ${LABEL_OPTION} --force ${UPLOADFILE} + anaconda -t ${MY_UPLOAD_KEY} upload -u ${CONDA_USERNAME:-rapidsai} ${LABEL_OPTION} --force ${UPLOADFILE} --no-progress else echo "Skipping upload" fi diff --git a/cpp/.clang-format b/cpp/.clang-format index 6f48df58b74..0c05436e922 100644 --- a/cpp/.clang-format +++ b/cpp/.clang-format @@ -6,16 +6,22 @@ Language: Cpp AccessModifierOffset: -1 AlignAfterOpenBracket: Align AlignConsecutiveAssignments: true +AlignConsecutiveBitFields: true AlignConsecutiveDeclarations: false +AlignConsecutiveMacros: true AlignEscapedNewlines: Left AlignOperands: true AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true AllowAllParametersOfDeclarationOnNextLine: true AllowShortBlocksOnASingleLine: true AllowShortCaseLabelsOnASingleLine: true +AllowShortEnumsOnASingleLine: true AllowShortFunctionsOnASingleLine: All AllowShortIfStatementsOnASingleLine: true -AllowShortLoopsOnASingleLine: true +AllowShortLambdasOnASingleLine: true +AllowShortLoopsOnASingleLine: false # This is deprecated AlwaysBreakAfterDefinitionReturnType: None AlwaysBreakAfterReturnType: None @@ -40,14 +46,14 @@ BraceWrapping: SplitEmptyFunction: false SplitEmptyRecord: false SplitEmptyNamespace: false +BreakAfterJavaFieldAnnotations: false BreakBeforeBinaryOperators: None BreakBeforeBraces: WebKit BreakBeforeInheritanceComma: false -BreakInheritanceList: BeforeColon BreakBeforeTernaryOperators: true BreakConstructorInitializersBeforeComma: false BreakConstructorInitializers: BeforeColon -BreakAfterJavaFieldAnnotations: false +BreakInheritanceList: BeforeColon BreakStringLiterals: true ColumnLimit: 100 CommentPragmas: '^ IWYU pragma:' @@ -57,7 +63,7 @@ ConstructorInitializerAllOnOneLineOrOnePerLine: true ConstructorInitializerIndentWidth: 2 ContinuationIndentWidth: 2 Cpp11BracedListStyle: true -DerivePointerAlignment: true +DerivePointerAlignment: false DisableFormat: false ExperimentalAutoDetectBinPacking: false FixNamespaceComments: true @@ -139,18 +145,20 @@ SpaceBeforeCtorInitializerColon: true SpaceBeforeInheritanceColon: true SpaceBeforeParens: ControlStatements SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false SpaceInEmptyParentheses: false SpacesBeforeTrailingComments: 2 SpacesInAngles: false +SpacesInConditionalStatement: false SpacesInContainerLiterals: true SpacesInCStyleCastParentheses: false SpacesInParentheses: false SpacesInSquareBrackets: false -Standard: Cpp11 +Standard: c++17 StatementMacros: - Q_UNUSED - QT_REQUIRE_VERSION # Be consistent with indent-width, even for people who use tab for indentation! TabWidth: 2 UseTab: Never - diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 8504f0c7b87..9fcb40b1eba 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -1,5 +1,5 @@ #============================================================================= -# Copyright (c) 2018-2020, NVIDIA CORPORATION. +# Copyright (c) 2018-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. @@ -14,401 +14,265 @@ # limitations under the License. #============================================================================= -cmake_minimum_required(VERSION 3.12..3.17 FATAL_ERROR) +cmake_minimum_required(VERSION 3.20.1 FATAL_ERROR) +include(FetchContent) +FetchContent_Declare( + rapids-cmake + GIT_REPOSITORY https://github.com/rapidsai/rapids-cmake.git + GIT_TAG origin/branch-21.08 + ) +FetchContent_MakeAvailable(rapids-cmake) -project(CUGRAPH VERSION 0.17.0 LANGUAGES C CXX CUDA) +include(rapids-cmake) +include(rapids-cpm) +include(rapids-cuda) +include(rapids-export) +include(rapids-find) -################################################################################################### -# - build type ------------------------------------------------------------------------------------ +rapids_cuda_init_architectures(CUGRAPH) -# Set a default build type if none was specified -set(DEFAULT_BUILD_TYPE "Release") - -if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) - message(STATUS "Setting build type to '${DEFAULT_BUILD_TYPE}' since none specified.") - set(CMAKE_BUILD_TYPE "${DEFAULT_BUILD_TYPE}" CACHE - STRING "Choose the type of build." FORCE) - # Set the possible values of build type for cmake-gui - set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS - "Debug" "Release" "MinSizeRel" "RelWithDebInfo") -endif() +project(CUGRAPH VERSION 21.08.00 LANGUAGES C CXX CUDA) -################################################################################################### -# - compiler options ------------------------------------------------------------------------------ +# Remove the following archs from CMAKE_CUDA_ARCHITECTURES that +# cuhornet currently doesn't support +# +# < 60 +# >= 86 +set(supported_archs "60" "62" "70" "72" "75" "80") +foreach( arch IN LISTS CMAKE_CUDA_ARCHITECTURES) + string(REPLACE "-real" "" arch ${arch}) + if( arch IN_LIST supported_archs ) + list(APPEND usable_arch_values ${arch}) + endif() +endforeach() +# Make sure everything but the 'newest' arch +# is marked as `-real` so we only generate PTX for +# arch > 80 +list(POP_BACK usable_arch_values latest_arch) +list(TRANSFORM usable_arch_values APPEND "-real") +list(APPEND usable_arch_values ${latest_arch}) -set(CMAKE_CXX_STANDARD 14) -set(CMAKE_C_COMPILER $ENV{CC}) -set(CMAKE_CXX_COMPILER $ENV{CXX}) -set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CUDA_ARCHITECTURES ${usable_arch_values}) -set(CMAKE_CUDA_STANDARD 14) -set(CMAKE_CUDA_STANDARD_REQUIRED ON) -if(CMAKE_COMPILER_IS_GNUCXX) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wno-error=deprecated-declarations") -endif(CMAKE_COMPILER_IS_GNUCXX) +# Write the version header +include(cmake/Modules/Version.cmake) +write_version() -find_package(CUDA) +################################################################################################### +# - build type ------------------------------------------------------------------------------------ -# Configure GPU arch to build -set(GUNROCK_GENCODE_SM60 "OFF") -set(GUNROCK_GENCODE_SM61 "OFF") -set(GUNROCK_GENCODE_SM70 "OFF") -set(GUNROCK_GENCODE_SM72 "OFF") -set(GUNROCK_GENCODE_SM75 "OFF") -set(GUNROCK_GENCODE_SM80 "OFF") +# Set a default build type if none was specified +rapids_cmake_build_type(Release) -# ARCHS handling: -# -if("${GPU_ARCHS}" STREQUAL "") - include(cmake/EvalGpuArchs.cmake) - evaluate_gpu_archs(GPU_ARCHS) -endif() +############################################################################## +# - User Options ------------------------------------------------------------ +option(BUILD_CUGRAPH_MG_TESTS "Build cuGraph multigpu algorithm tests" OFF) +set(BLAS_LIBRARIES "" CACHE STRING "Location of BLAS library for FAISS build.") +option(BUILD_STATIC_FAISS "Build the FAISS library for nearest neighbors search on GPU" OFF) +option(CMAKE_CUDA_LINEINFO "Enable the -lineinfo option for nvcc (useful for cuda-memcheck / profiler" OFF) +option(BUILD_TESTS "Configure CMake to build tests" ON) -# CUDA 11 onwards cub ships with CTK -if((CUDA_VERSION_MAJOR EQUAL 11) OR (CUDA_VERSION_MAJOR GREATER 11)) - set(CUB_IS_PART_OF_CTK ON) -else() - set(CUB_IS_PART_OF_CTK OFF) -endif() +################################################################################################### +# - compiler options ------------------------------------------------------------------------------ -if("${GPU_ARCHS}" STREQUAL "ALL") - set(GPU_ARCHS "60") - if((CUDA_VERSION_MAJOR EQUAL 9) OR (CUDA_VERSION_MAJOR GREATER 9)) - set(GPU_ARCHS "${GPU_ARCHS};70") - endif() - if((CUDA_VERSION_MAJOR EQUAL 10) OR (CUDA_VERSION_MAJOR GREATER 10)) - set(GPU_ARCHS "${GPU_ARCHS};75") - endif() - if((CUDA_VERSION_MAJOR EQUAL 11) OR (CUDA_VERSION_MAJOR GREATER 11)) - set(GPU_ARCHS "${GPU_ARCHS};80") - endif() -endif() +rapids_find_package(CUDAToolkit REQUIRED + BUILD_EXPORT_SET cugraph-exports + INSTALL_EXPORT_SET cugraph-exports + ) -message("-- Building for GPU_ARCHS = ${GPU_ARCHS}") -foreach(arch ${GPU_ARCHS}) - set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -gencode arch=compute_${arch},code=sm_${arch}") - set(GUNROCK_GENCODE_SM${arch} "ON") -endforeach() +set(CUGRAPH_CXX_FLAGS "") +set(CUGRAPH_CUDA_FLAGS "") + +if(CMAKE_COMPILER_IS_GNUCXX) + list(APPEND CUGRAPH_CXX_FLAGS -Werror -Wno-error=deprecated-declarations) +endif(CMAKE_COMPILER_IS_GNUCXX) -list(GET GPU_ARCHS -1 ptx) -set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -gencode arch=compute_${ptx},code=compute_${ptx}") -set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --expt-extended-lambda --expt-relaxed-constexpr") -set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Werror=cross-execution-space-call -Wno-deprecated-declarations -Xptxas --disable-warnings") -set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcompiler -Wall,-Wno-error=sign-compare,-Wno-error=unused-but-set-variable") +message("-- Building for GPU_ARCHS = ${CMAKE_CUDA_ARCHITECTURES}") +list(APPEND CUGRAPH_CUDA_FLAGS --expt-extended-lambda --expt-relaxed-constexpr) +list(APPEND CUGRAPH_CUDA_FLAGS -Werror=cross-execution-space-call -Wno-deprecated-declarations -Xptxas=--disable-warnings) +list(APPEND CUGRAPH_CUDA_FLAGS -Xcompiler=-Wall,-Wno-error=sign-compare,-Wno-error=unused-but-set-variable) +list(APPEND CUGRAPH_CUDA_FLAGS -Xfatbin=-compress-all) # Option to enable line info in CUDA device compilation to allow introspection when profiling / # memchecking -option(CMAKE_CUDA_LINEINFO "Enable the -lineinfo option for nvcc (useful for cuda-memcheck / profiler" OFF) if (CMAKE_CUDA_LINEINFO) - set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -lineinfo") + list(APPEND CUGRAPH_CUDA_FLAGS -lineinfo) endif(CMAKE_CUDA_LINEINFO) # Debug options if(CMAKE_BUILD_TYPE MATCHES Debug) message(STATUS "Building with debugging flags") - set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -G -Xcompiler -rdynamic") + list(APPEND CUGRAPH_CUDA_FLAGS -G -Xcompiler=-rdynamic) endif(CMAKE_BUILD_TYPE MATCHES Debug) -# To apply RUNPATH to transitive dependencies (this is a temporary solution) -set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--disable-new-dtags") -set(CMAKE_EXE_LINKER_FLAGS "-Wl,--disable-new-dtags") - -option(BUILD_TESTS "Configure CMake to build tests" - ON) - -################################################################################################### -# - cmake modules --------------------------------------------------------------------------------- - -set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/" ${CMAKE_MODULE_PATH}) - -include(FeatureSummary) -include(CheckIncludeFiles) -include(CheckLibraryExists) -if(BUILD_TESTS) - include(CTest) -endif(BUILD_TESTS) - -################################################################################################### -# - find boost ------------------------------------------------------------------------------------ - -find_package(Boost REQUIRED) -if(Boost_FOUND) - message(STATUS "Boost found in ${Boost_INCLUDE_DIRS}") -else() - message(FATAL_ERROR "Boost not found, please check your settings.") -endif(Boost_FOUND) - ################################################################################################### # - find openmp ----------------------------------------------------------------------------------- find_package(OpenMP) if(OpenMP_FOUND) # find_package(OPenMP) does not automatically add OpenMP flags to CUDA - set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcompiler=${OpenMP_CXX_FLAGS}") + list(APPEND CUGRAPH_CUDA_FLAGS -Xcompiler=${OpenMP_CXX_FLAGS}) endif(OpenMP_FOUND) ################################################################################################### -# - find gtest ------------------------------------------------------------------------------------ +# - find blas ------------------------------------------------------------------------------------- -if(BUILD_TESTS) - include(ConfigureGoogleTest) - - if(GTEST_FOUND) - message(STATUS - "Google C++ Testing Framework (Google Test) found in ${GTEST_ROOT}") - else() - message(AUTHOR_WARNING - "Google C++ Testing Framework (Google Test) not found: automated tests are disabled.") - endif(GTEST_FOUND) -endif(BUILD_TESTS) - -################################################################################################### -# - RMM ------------------------------------------------------------------------------------------- - -find_path(RMM_INCLUDE "rmm" - HINTS - "$ENV{RMM_ROOT}/include" - "$ENV{CONDA_PREFIX}/include/rmm" - "$ENV{CONDA_PREFIX}/include") - -message(STATUS "RMM: RMM_INCLUDE set to ${RMM_INCLUDE}") - -################################################################################################### -# - Fetch Content --------------------------------------------------------------------------------- -include(FetchContent) - -# - THRUST/CUB -message("Fetching Thrust") - -FetchContent_Declare( - thrust - GIT_REPOSITORY https://github.com/thrust/thrust.git - # August 28, 2020 - GIT_TAG 52a8bda46c5c2128414d1d47f546b486ff0be2f0 -) - -FetchContent_GetProperties(thrust) -if(NOT thrust_POPULATED) - FetchContent_Populate(thrust) - # We are not using the thrust CMake targets, so no need to call `add_subdirectory()`. -endif() -set(THRUST_INCLUDE_DIR "${thrust_SOURCE_DIR}") - -# - cuco -message("Fetching cuco") - -FetchContent_Declare( - cuco - GIT_REPOSITORY https://github.com/NVIDIA/cuCollections.git - GIT_TAG d965ed8dea8f56da8e260a6130dddf3ca351c45f -) - -FetchContent_GetProperties(cuco) -if(NOT cuco_POPULATED) - FetchContent_Populate(cuco) -endif() -set(CUCO_INCLUDE_DIR "${cuco_SOURCE_DIR}/include") - -# - libcudacxx -# NOTE: This is necessary because libcudacxx is not supported in -# debian cuda 10.2 packages. Once 10.2 is deprecated -# we should not need this any longer. -message("Fetching libcudacxx") - -FetchContent_Declare( - libcudacxx - GIT_REPOSITORY https://github.com/NVIDIA/libcudacxx.git - GIT_TAG 1.3.0 - GIT_SHALLOW true -) - -FetchContent_GetProperties(libcudacxx) -if(NOT libcudacxx_POPULATED) - message("populating libcudacxx") - FetchContent_Populate(libcudacxx) +if(NOT DEFINED BLAS_LIBRARIES) + find_package( BLAS REQUIRED ) +else() + message(STATUS "Manually setting BLAS to ${BLAS_LIBRARIES}") endif() -set(LIBCUDACXX_INCLUDE_DIR "${libcudacxx_SOURCE_DIR}/include") -message("set LIBCUDACXX_INCLUDE_DIR to: ${LIBCUDACXX_INCLUDE_DIR}") - ################################################################################################### -# - External Projects ----------------------------------------------------------------------------- - -# https://cmake.org/cmake/help/v3.0/module/ExternalProject.html -include(ExternalProject) - -# - CUHORNET -set(CUHORNET_DIR ${CMAKE_CURRENT_BINARY_DIR}/cuhornet CACHE STRING "Path to cuhornet repo") -set(CUHORNET_INCLUDE_DIR ${CUHORNET_DIR}/src/cuhornet CACHE STRING "Path to cuhornet includes") - - -ExternalProject_Add(cuhornet - GIT_REPOSITORY https://github.com/rapidsai/cuhornet.git - GIT_TAG 9cb8e8803852bd895a9c95c0fe778ad6eeefa7ad - PREFIX ${CUHORNET_DIR} - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" -) +# - find CPM based dependencies ------------------------------------------------------------------ -# - GUNROCK -set(GUNROCK_DIR ${CMAKE_CURRENT_BINARY_DIR}/gunrock CACHE STRING "Path to gunrock repo") -set(GUNROCK_INCLUDE_DIR ${GUNROCK_DIR}/src/gunrock_ext CACHE STRING "Path to gunrock includes") - -ExternalProject_Add(gunrock_ext - GIT_REPOSITORY https://github.com/gunrock/gunrock.git - GIT_TAG dev - PREFIX ${GUNROCK_DIR} - CMAKE_ARGS -DCMAKE_INSTALL_PREFIX= - -DGUNROCK_BUILD_SHARED_LIBS=OFF - -DGUNROCK_BUILD_TESTS=OFF - -DCUDA_AUTODETECT_GENCODE=OFF - -DGUNROCK_GENCODE_SM60=${GUNROCK_GENCODE_SM60} - -DGUNROCK_GENCODE_SM61=${GUNROCK_GENCODE_SM61} - -DGUNROCK_GENCODE_SM70=${GUNROCK_GENCODE_SM70} - -DGUNROCK_GENCODE_SM72=${GUNROCK_GENCODE_SM72} - -DGUNROCK_GENCODE_SM75=${GUNROCK_GENCODE_SM75} - -DGUNROCK_GENCODE_SM80=${GUNROCK_GENCODE_SM80} - ${GUNROCK_GENCODE} - BUILD_BYPRODUCTS ${GUNROCK_DIR}/src/gunrock_ext-build/lib/libgunrock.a - INSTALL_COMMAND "" -) - -add_library(gunrock STATIC IMPORTED) - -add_dependencies(gunrock gunrock_ext) - -set_property(TARGET gunrock PROPERTY IMPORTED_LOCATION ${GUNROCK_DIR}/src/gunrock_ext-build/lib/libgunrock.a) - -# - NCCL -if(NOT NCCL_PATH) - find_package(NCCL REQUIRED) -else() - message("-- Manually set NCCL PATH to ${NCCL_PATH}") - set(NCCL_INCLUDE_DIRS ${NCCL_PATH}/include) - set(NCCL_LIBRARIES ${NCCL_PATH}/lib/libnccl.so) -endif(NOT NCCL_PATH) -# - raft - (header only) ----------------------------------------------------- +rapids_cpm_init() -# Only cloned if RAFT_PATH env variable is not defined -if(DEFINED ENV{RAFT_PATH}) - message(STATUS "RAFT_PATH environment variable detected.") - message(STATUS "RAFT_DIR set to $ENV{RAFT_PATH}") - set(RAFT_DIR "$ENV{RAFT_PATH}") +include(cmake/thirdparty/get_thrust.cmake) +include(cmake/thirdparty/get_faiss.cmake) +include(cmake/thirdparty/get_nccl.cmake) +include(cmake/thirdparty/get_rmm.cmake) - ExternalProject_Add(raft - DOWNLOAD_COMMAND "" - SOURCE_DIR ${RAFT_DIR} - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "") +include(cmake/thirdparty/get_raft.cmake) -else(DEFINED ENV{RAFT_PATH}) - message(STATUS "RAFT_PATH environment variable NOT detected, cloning RAFT") - set(RAFT_DIR ${CMAKE_CURRENT_BINARY_DIR}/raft CACHE STRING "Path to RAFT repo") +include(cmake/thirdparty/get_cuco.cmake) +include(cmake/thirdparty/get_cuhornet.cmake) - ExternalProject_Add(raft - GIT_REPOSITORY https://github.com/rapidsai/raft.git - GIT_TAG f75d7b437bf1da3df749108161b8a0505fb6b7b3 - PREFIX ${RAFT_DIR} - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "") +include(cmake/thirdparty/get_gunrock.cmake) - # Redefining RAFT_DIR so it coincides with the one inferred by env variable. - set(RAFT_DIR "${RAFT_DIR}/src/raft/") -endif(DEFINED ENV{RAFT_PATH}) +if(BUILD_TESTS) + include(cmake/thirdparty/get_gtest.cmake) +endif() ################################################################################################### # - library targets ------------------------------------------------------------------------------- -# target_link_directories is added in cmake 3.13, and cmake advises to use this instead of -# link_directoires (we should switch to target_link_directories once 3.13 becomes the minimum -# required version). -link_directories( - # CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES is an undocumented/unsupported variable containing the - # link directories for nvcc. - "${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES}") - add_library(cugraph SHARED + src/detail/utility_wrappers.cu + src/detail/shuffle_wrappers.cu src/utilities/spmv_1D.cu src/utilities/cython.cu + src/utilities/path_retrieval.cu + src/utilities/graph_bcast.cu src/structure/graph.cu src/linear_assignment/hungarian.cu - src/link_analysis/pagerank.cu - src/link_analysis/pagerank_1D.cu src/link_analysis/gunrock_hits.cpp src/traversal/bfs.cu src/traversal/sssp.cu + src/traversal/tsp.cu src/link_prediction/jaccard.cu src/link_prediction/overlap.cu src/layout/force_atlas2.cu - src/converters/renumber.cu src/converters/COOtoCSR.cu src/community/spectral_clustering.cu src/community/louvain.cu src/community/leiden.cu src/community/ktruss.cu - src/community/ECG.cu + src/community/ecg.cu src/community/triangles_counting.cu src/community/extract_subgraph_by_vertex.cu + src/community/egonet.cu + src/sampling/random_walks.cu src/cores/core_number.cu src/traversal/two_hop_neighbors.cu src/components/connectivity.cu src/centrality/katz_centrality.cu src/centrality/betweenness_centrality.cu + src/generators/generate_rmat_edgelist.cu + src/generators/generator_tools.cu + src/generators/simple_generators.cu + src/generators/erdos_renyi_generator.cu src/experimental/graph.cu src/experimental/graph_view.cu + src/experimental/coarsen_graph.cu + src/experimental/renumber_edgelist.cu + src/experimental/renumber_utils.cu + src/experimental/relabel.cu + src/experimental/induced_subgraph.cu src/experimental/bfs.cu src/experimental/sssp.cu src/experimental/pagerank.cu src/experimental/katz_centrality.cu + src/serialization/serializer.cu src/tree/mst.cu + src/components/weakly_connected_components.cu + src/structure/create_graph_from_edgelist.cpp + src/utilities/host_barrier.cpp + src/visitors/graph_envelope.cpp + src/visitors/visitors_factory.cpp + src/visitors/bfs_visitor.cpp ) -# -# NOTE: This dependency will force the building of cugraph to -# wait until after cugunrock is constructed. -# -add_dependencies(cugraph gunrock_ext) -add_dependencies(cugraph raft) +set_target_properties(cugraph + PROPERTIES BUILD_RPATH "\$ORIGIN" + INSTALL_RPATH "\$ORIGIN" + # set target compile options + CXX_STANDARD 17 + CXX_STANDARD_REQUIRED ON + CUDA_STANDARD 17 + CUDA_STANDARD_REQUIRED ON + INTERFACE_POSITION_INDEPENDENT_CODE ON +) + +target_compile_options(cugraph + PRIVATE "$<$:${CUGRAPH_CXX_FLAGS}>" + "$<$:${CUGRAPH_CUDA_FLAGS}>" +) + +# Per-thread default stream option see https://docs.nvidia.com/cuda/cuda-runtime-api/stream-sync-behavior.html +# The per-thread default stream does not synchronize with other streams +target_compile_definitions(cugraph PUBLIC CUDA_API_PER_THREAD_DEFAULT_STREAM) + +file(WRITE "${CUGRAPH_BINARY_DIR}/fatbin.ld" +[=[ +SECTIONS +{ + .nvFatBinSegment : { *(.nvFatBinSegment) } + .nv_fatbin : { *(.nv_fatbin) } +} +]=]) +target_link_options(cugraph PRIVATE "${CUGRAPH_BINARY_DIR}/fatbin.ld") + +add_library(cugraph::cugraph ALIAS cugraph) ################################################################################################### # - include paths --------------------------------------------------------------------------------- target_include_directories(cugraph PRIVATE - "${THRUST_INCLUDE_DIR}" - "${CUCO_INCLUDE_DIR}" - "${LIBCUDACXX_INCLUDE_DIR}" - "${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}" - "${Boost_INCLUDE_DIRS}" - "${RMM_INCLUDE}" - "${CMAKE_CURRENT_SOURCE_DIR}/../thirdparty" - "${CUHORNET_INCLUDE_DIR}/hornet/include" - "${CUHORNET_INCLUDE_DIR}/hornetsnest/include" - "${CUHORNET_INCLUDE_DIR}/xlib/include" - "${CUHORNET_INCLUDE_DIR}/primitives" - "${CMAKE_CURRENT_SOURCE_DIR}/src" - "${GUNROCK_INCLUDE_DIR}" - "${NCCL_INCLUDE_DIRS}" - "${RAFT_DIR}/cpp/include" + "${CMAKE_CURRENT_SOURCE_DIR}/../thirdparty" + "${CMAKE_CURRENT_SOURCE_DIR}/src" + "${NCCL_INCLUDE_DIRS}" PUBLIC - "${CMAKE_CURRENT_SOURCE_DIR}/include" + "$" + "$" ) ################################################################################################### # - link libraries -------------------------------------------------------------------------------- - -target_link_libraries(cugraph PRIVATE - gunrock cublas cusparse curand cusolver cudart cuda ${NCCL_LIBRARIES}) +target_link_libraries(cugraph + PUBLIC + rmm::rmm + cugraph::Thrust + raft::raft + PRIVATE + cuco::cuco + CUDA::cublas + CUDA::curand + CUDA::cusolver + CUDA::cusparse + cugraph::cuHornet + FAISS::FAISS + gunrock + NCCL::NCCL +) if(OpenMP_CXX_FOUND) target_link_libraries(cugraph PRIVATE @@ -461,39 +325,76 @@ target_link_libraries(cugraph PRIVATE ${OpenMP_CXX_LIB_NAMES}) endif(OpenMP_CXX_FOUND) + ################################################################################################### # - generate tests -------------------------------------------------------------------------------- if(BUILD_TESTS) - if(GTEST_FOUND) - # target_link_directories is added in cmake 3.13, and cmake advises to use this instead of - # link_directoires (we should switch to target_link_directories once 3.13 becomes the - # minimum required version). - link_directories(${GTEST_LIBRARY_DIR}) - add_subdirectory(${CMAKE_SOURCE_DIR}/tests) - endif(GTEST_FOUND) + include(CTest) + add_subdirectory(tests) endif(BUILD_TESTS) ################################################################################################### # - install targets ------------------------------------------------------------------------------- -install(TARGETS cugraph LIBRARY - DESTINATION lib) +include(CPack) + +install(TARGETS cugraph + DESTINATION lib + EXPORT cugraph-exports) + +install(DIRECTORY include/cugraph/ + DESTINATION include/cugraph) + +install(FILES ${CMAKE_CURRENT_BINARY_DIR}/include/cugraph/version_config.hpp + DESTINATION include/cugraph) -install(DIRECTORY include/ - DESTINATION include/cugraph) +################################################################################################ +# - install export ------------------------------------------------------------------------------- +set(doc_string +[=[ +Provide targets for cuGraph. + +cuGraph library is a collection of GPU accelerated graph algorithms that process data found in +[GPU DataFrames](https://github.com/rapidsai/cudf). + +]=]) + +set(code_string +[=[ +thrust_create_target(cugraph::Thrust FROM_OPTIONS) +]=]) + + rapids_export(INSTALL cugraph + EXPORT_SET cugraph-exports + GLOBAL_TARGETS cugraph + NAMESPACE cugraph:: + DOCUMENTATION doc_string + FINAL_CODE_BLOCK code_string + ) + +################################################################################################ +# - build export ------------------------------------------------------------------------------- +rapids_export(BUILD cugraph + EXPORT_SET cugraph-exports + GLOBAL_TARGETS cugraph + NAMESPACE cugraph:: + DOCUMENTATION doc_string + FINAL_CODE_BLOCK code_string + ) -install(DIRECTORY ${RAFT_DIR}/cpp/include/raft/ - DESTINATION include/cugraph/raft) ################################################################################################### # - make documentation ---------------------------------------------------------------------------- # requires doxygen and graphviz to be installed # from build directory, run make docs_cugraph -# doc targets for cuGraph -add_custom_command(OUTPUT CUGRAPH_DOXYGEN - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/doxygen - COMMAND doxygen Doxyfile - VERBATIM) +# doc targets for cugraph +find_package(Doxygen 1.8.11) +if(Doxygen_FOUND) + add_custom_command(OUTPUT CUGRAPH_DOXYGEN + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/doxygen + COMMAND doxygen Doxyfile + VERBATIM) -add_custom_target(docs_cugraph DEPENDS CUGRAPH_DOXYGEN) + add_custom_target(docs_cugraph DEPENDS CUGRAPH_DOXYGEN) +endif() diff --git a/cpp/cmake/EvalGpuArchs.cmake b/cpp/cmake/EvalGpuArchs.cmake deleted file mode 100644 index f3918542db9..00000000000 --- a/cpp/cmake/EvalGpuArchs.cmake +++ /dev/null @@ -1,68 +0,0 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -function(evaluate_gpu_archs gpu_archs) - set(eval_file ${PROJECT_BINARY_DIR}/eval_gpu_archs.cu) - set(eval_exe ${PROJECT_BINARY_DIR}/eval_gpu_archs) - file(WRITE ${eval_file} - " -#include -#include -#include -using namespace std; -int main(int argc, char** argv) { - set archs; - int nDevices; - if((cudaGetDeviceCount(&nDevices) == cudaSuccess) && (nDevices > 0)) { - for(int dev=0;dev::const_iterator itr=archs.begin();itr!=archs.end();++itr) { - printf(first? \"%s\" : \";%s\", itr->c_str()); - first = false; - } - } - printf(\"\\n\"); - return 0; -} -") - execute_process( - COMMAND ${CUDA_NVCC_EXECUTABLE} - -o ${eval_exe} - --run - ${eval_file} - OUTPUT_VARIABLE __gpu_archs - OUTPUT_STRIP_TRAILING_WHITESPACE) - set(__gpu_archs_filtered "${__gpu_archs}") - foreach(arch ${__gpu_archs}) - if (arch VERSION_LESS 60) - list(REMOVE_ITEM __gpu_archs_filtered ${arch}) - endif() - endforeach() - if (NOT __gpu_archs_filtered) - message(FATAL_ERROR "No supported GPU arch found on this system") - endif() - message("Auto detection of gpu-archs: ${__gpu_archs_filtered}") - set(${gpu_archs} ${__gpu_archs_filtered} PARENT_SCOPE) -endfunction(evaluate_gpu_archs) diff --git a/cpp/cmake/Modules/ConfigureArrow.cmake b/cpp/cmake/Modules/ConfigureArrow.cmake deleted file mode 100644 index b27e53dd415..00000000000 --- a/cpp/cmake/Modules/ConfigureArrow.cmake +++ /dev/null @@ -1,98 +0,0 @@ -set(ARROW_ROOT ${CMAKE_BINARY_DIR}/arrow) - -set(ARROW_CMAKE_ARGS " -DARROW_WITH_LZ4=OFF" - " -DARROW_WITH_ZSTD=OFF" - " -DARROW_WITH_BROTLI=OFF" - " -DARROW_WITH_SNAPPY=OFF" - " -DARROW_WITH_ZLIB=OFF" - " -DARROW_BUILD_STATIC=ON" - " -DARROW_BUILD_SHARED=OFF" - " -DARROW_BOOST_USE_SHARED=ON" - " -DARROW_BUILD_TESTS=OFF" - " -DARROW_TEST_LINKAGE=OFF" - " -DARROW_TEST_MEMCHECK=OFF" - " -DARROW_BUILD_BENCHMARKS=OFF" - " -DARROW_IPC=ON" - " -DARROW_COMPUTE=OFF" - " -DARROW_CUDA=OFF" - " -DARROW_JEMALLOC=OFF" - " -DARROW_BOOST_VENDORED=OFF" - " -DARROW_PYTHON=OFF" - " -DARROW_USE_GLOG=OFF" - " -DCMAKE_VERBOSE_MAKEFILE=ON") - -configure_file("${CMAKE_SOURCE_DIR}/cmake/Templates/Arrow.CMakeLists.txt.cmake" - "${ARROW_ROOT}/CMakeLists.txt") - -file(MAKE_DIRECTORY "${ARROW_ROOT}/build") -file(MAKE_DIRECTORY "${ARROW_ROOT}/install") - -execute_process( - COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . - RESULT_VARIABLE ARROW_CONFIG - WORKING_DIRECTORY ${ARROW_ROOT}) - -if(ARROW_CONFIG) - message(FATAL_ERROR "Configuring Arrow failed: " ${ARROW_CONFIG}) -endif(ARROW_CONFIG) - -set(PARALLEL_BUILD -j) -if($ENV{PARALLEL_LEVEL}) - set(NUM_JOBS $ENV{PARALLEL_LEVEL}) - set(PARALLEL_BUILD "${PARALLEL_BUILD}${NUM_JOBS}") -endif($ENV{PARALLEL_LEVEL}) - -if(${NUM_JOBS}) - if(${NUM_JOBS} EQUAL 1) - message(STATUS "ARROW BUILD: Enabling Sequential CMake build") - elseif(${NUM_JOBS} GREATER 1) - message(STATUS "ARROW BUILD: Enabling Parallel CMake build with ${NUM_JOBS} jobs") - endif(${NUM_JOBS} EQUAL 1) -else() - message(STATUS "ARROW BUILD: Enabling Parallel CMake build with all threads") -endif(${NUM_JOBS}) - -execute_process( - COMMAND ${CMAKE_COMMAND} --build .. -- ${PARALLEL_BUILD} - RESULT_VARIABLE ARROW_BUILD - WORKING_DIRECTORY ${ARROW_ROOT}/build) - -if(ARROW_BUILD) - message(FATAL_ERROR "Building Arrow failed: " ${ARROW_BUILD}) -endif(ARROW_BUILD) - -set(ARROW_GENERATED_IPC_DIR - "${ARROW_ROOT}/build/src/arrow/ipc") - -configure_file(${ARROW_GENERATED_IPC_DIR}/File_generated.h ${CMAKE_SOURCE_DIR}/include/cudf/ipc_generated/File_generated.h COPYONLY) -configure_file(${ARROW_GENERATED_IPC_DIR}/Message_generated.h ${CMAKE_SOURCE_DIR}/include/cudf/ipc_generated/Message_generated.h COPYONLY) -configure_file(${ARROW_GENERATED_IPC_DIR}/Schema_generated.h ${CMAKE_SOURCE_DIR}/include/cudf/ipc_generated/Schema_generated.h COPYONLY) -configure_file(${ARROW_GENERATED_IPC_DIR}/Tensor_generated.h ${CMAKE_SOURCE_DIR}/include/cudf/ipc_generated/Tensor_generated.h COPYONLY) - -message(STATUS "Arrow installed here: " ${ARROW_ROOT}/install) -set(ARROW_LIBRARY_DIR "${ARROW_ROOT}/install/lib") -set(ARROW_INCLUDE_DIR "${ARROW_ROOT}/install/include") - -find_library(ARROW_LIB arrow - NO_DEFAULT_PATH - HINTS "${ARROW_LIBRARY_DIR}") - -if(ARROW_LIB) - message(STATUS "Arrow library: " ${ARROW_LIB}) - set(ARROW_FOUND TRUE) -endif(ARROW_LIB) - -set(FLATBUFFERS_ROOT "${ARROW_ROOT}/build/flatbuffers_ep-prefix/src/flatbuffers_ep-install") - -message(STATUS "FlatBuffers installed here: " ${FLATBUFFERS_ROOT}) -set(FLATBUFFERS_INCLUDE_DIR "${FLATBUFFERS_ROOT}/include") -set(FLATBUFFERS_LIBRARY_DIR "${FLATBUFFERS_ROOT}/lib") - -add_definitions(-DARROW_METADATA_V4) -add_definitions(-DARROW_VERSION=1210) - - - - - - diff --git a/cpp/cmake/Modules/ConfigureGoogleTest.cmake b/cpp/cmake/Modules/ConfigureGoogleTest.cmake deleted file mode 100644 index 9fac40f4649..00000000000 --- a/cpp/cmake/Modules/ConfigureGoogleTest.cmake +++ /dev/null @@ -1,49 +0,0 @@ -set(GTEST_ROOT "${CMAKE_BINARY_DIR}/googletest") - -set(GTEST_CMAKE_ARGS "") - #" -Dgtest_build_samples=ON" - #" -DCMAKE_VERBOSE_MAKEFILE=ON") - -configure_file("${CMAKE_SOURCE_DIR}/cmake/Templates/GoogleTest.CMakeLists.txt.cmake" - "${GTEST_ROOT}/CMakeLists.txt") - -file(MAKE_DIRECTORY "${GTEST_ROOT}/build") -file(MAKE_DIRECTORY "${GTEST_ROOT}/install") - -execute_process(COMMAND ${CMAKE_COMMAND} -G ${CMAKE_GENERATOR} . - RESULT_VARIABLE GTEST_CONFIG - WORKING_DIRECTORY ${GTEST_ROOT}) - -if(GTEST_CONFIG) - message(FATAL_ERROR "Configuring GoogleTest failed: " ${GTEST_CONFIG}) -endif(GTEST_CONFIG) - -set(PARALLEL_BUILD -j) -if($ENV{PARALLEL_LEVEL}) - set(NUM_JOBS $ENV{PARALLEL_LEVEL}) - set(PARALLEL_BUILD "${PARALLEL_BUILD}${NUM_JOBS}") -endif($ENV{PARALLEL_LEVEL}) - -if(${NUM_JOBS}) - if(${NUM_JOBS} EQUAL 1) - message(STATUS "GTEST BUILD: Enabling Sequential CMake build") - elseif(${NUM_JOBS} GREATER 1) - message(STATUS "GTEST BUILD: Enabling Parallel CMake build with ${NUM_JOBS} jobs") - endif(${NUM_JOBS} EQUAL 1) -else() - message(STATUS "GTEST BUILD: Enabling Parallel CMake build with all threads") -endif(${NUM_JOBS}) - -execute_process(COMMAND ${CMAKE_COMMAND} --build .. -- ${PARALLEL_BUILD} - RESULT_VARIABLE GTEST_BUILD - WORKING_DIRECTORY ${GTEST_ROOT}/build) - -if(GTEST_BUILD) - message(FATAL_ERROR "Building GoogleTest failed: " ${GTEST_BUILD}) -endif(GTEST_BUILD) - -message(STATUS "GoogleTest installed here: " ${GTEST_ROOT}/install) -set(GTEST_INCLUDE_DIR "${GTEST_ROOT}/install/include") -set(GTEST_LIBRARY_DIR "${GTEST_ROOT}/install/lib") -set(GTEST_FOUND TRUE) - diff --git a/cpp/cmake/Modules/FindNCCL.cmake b/cpp/cmake/Modules/FindNCCL.cmake deleted file mode 100644 index 0f673707444..00000000000 --- a/cpp/cmake/Modules/FindNCCL.cmake +++ /dev/null @@ -1,116 +0,0 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -# Based on FindPNG.cmake from cmake 3.14.3 - -#[=======================================================================[.rst: -FindNCCL --------- - -Find libnccl, the NVIDIA Collective Communication Library. A hint to find NCCL -can be provided by setting NCCL_INSTALL_DIR. - -Imported targets -^^^^^^^^^^^^^^^^ - -This module defines the following :prop_tgt:`IMPORTED` target: - -``NCCL::NCCL`` - The libnccl library, if found. - -Result variables -^^^^^^^^^^^^^^^^ - -This module will set the following variables in your project: - -``NCCL_INCLUDE_DIRS`` - where to find nccl.h , etc. -``NCCL_LIBRARIES`` - the libraries to link against to use NCCL. -``NCCL_FOUND`` - If false, do not try to use NCCL. -``NCCL_VERSION_STRING`` - the version of the NCCL library found - -#]=======================================================================] - -find_path(NCCL_NCCL_INCLUDE_DIR nccl.h HINTS ${NCCL_INSTALL_DIR} PATH_SUFFIXES include) - -#TODO: Does this need to support finding the static library? - -list(APPEND NCCL_NAMES nccl libnccl) -set(_NCCL_VERSION_SUFFIXES 2) - -foreach(v IN LISTS _NCCL_VERSION_SUFFIXES) - list(APPEND NCCL_NAMES nccl${v} libnccl${v}) -endforeach() -unset(_NCCL_VERSION_SUFFIXES) -# For compatibility with versions prior to this multi-config search, honor -# any NCCL_LIBRARY that is already specified and skip the search. -if(NOT NCCL_LIBRARY) - find_library(NCCL_LIBRARY_RELEASE NAMES ${NCCL_NAMES} HINTS ${NCCL_INSTALL_DIR} PATH_SUFFIXES lib) - include(${CMAKE_ROOT}/Modules/SelectLibraryConfigurations.cmake) - select_library_configurations(NCCL) - mark_as_advanced(NCCL_LIBRARY_RELEASE) -endif() -unset(NCCL_NAMES) - -# Set by select_library_configurations(), but we want the one from -# find_package_handle_standard_args() below. -unset(NCCL_FOUND) - -if (NCCL_LIBRARY AND NCCL_NCCL_INCLUDE_DIR) - set(NCCL_INCLUDE_DIRS ${NCCL_NCCL_INCLUDE_DIR} ) - set(NCCL_LIBRARY ${NCCL_LIBRARY}) - - if(NOT TARGET NCCL::NCCL) - add_library(NCCL::NCCL UNKNOWN IMPORTED) - set_target_properties(NCCL::NCCL PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES "${NCCL_INCLUDE_DIRS}") - if(EXISTS "${NCCL_LIBRARY}") - set_target_properties(NCCL::NCCL PROPERTIES - IMPORTED_LINK_INTERFACE_LANGUAGES "C" - IMPORTED_LOCATION "${NCCL_LIBRARY}") - endif() - endif() -endif () - -if (NCCL_NCCL_INCLUDE_DIR AND EXISTS "${NCCL_NCCL_INCLUDE_DIR}/nccl.h") - file(STRINGS "${NCCL_NCCL_INCLUDE_DIR}/nccl.h" nccl_major_version_str REGEX "^#define[ \t]+NCCL_MAJOR[ \t]+[0-9]+") - string(REGEX REPLACE "^#define[ \t]+NCCL_MAJOR[ \t]+([0-9]+)" "\\1" nccl_major_version_str "${nccl_major_version_str}") - - file(STRINGS "${NCCL_NCCL_INCLUDE_DIR}/nccl.h" nccl_minor_version_str REGEX "^#define[ \t]+NCCL_MINOR[ \t]+[0-9]+") - string(REGEX REPLACE "^#define[ \t]+NCCL_MINOR[ \t]+([0-9]+)" "\\1" nccl_minor_version_str "${nccl_minor_version_str}") - - file(STRINGS "${NCCL_NCCL_INCLUDE_DIR}/nccl.h" nccl_patch_version_str REGEX "^#define[ \t]+NCCL_PATCH[ \t]+[0-9]+") - string(REGEX REPLACE "^#define[ \t]+NCCL_PATCH[ \t]+([0-9]+)" "\\1" nccl_patch_version_str "${nccl_patch_version_str}") - - file(STRINGS "${NCCL_NCCL_INCLUDE_DIR}/nccl.h" nccl_suffix_version_str REGEX "^#define[ \t]+NCCL_SUFFIX[ \t]+\".*\"") - string(REGEX REPLACE "^#define[ \t]+NCCL_SUFFIX[ \t]+\"(.*)\"" "\\1" nccl_suffix_version_str "${nccl_suffix_version_str}") - - set(NCCL_VERSION_STRING "${nccl_major_version_str}.${nccl_minor_version_str}.${nccl_patch_version_str}${nccl_suffix_version_str}") - - unset(nccl_major_version_str) - unset(nccl_minor_version_str) - unset(nccl_patch_version_str) - unset(nccl_suffix_version_str) -endif () - -include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake) -find_package_handle_standard_args(NCCL - REQUIRED_VARS NCCL_LIBRARY NCCL_NCCL_INCLUDE_DIR - VERSION_VAR NCCL_VERSION_STRING) - -mark_as_advanced(NCCL_NCCL_INCLUDE_DIR NCCL_LIBRARY) diff --git a/cpp/cmake/Modules/Version.cmake b/cpp/cmake/Modules/Version.cmake new file mode 100644 index 00000000000..15046784175 --- /dev/null +++ b/cpp/cmake/Modules/Version.cmake @@ -0,0 +1,18 @@ +# 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. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software distributed under the License +# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express +# or implied. See the License for the specific language governing permissions and limitations under +# the License. + +# Generate version_config.hpp from the version found in CMakeLists.txt +function(write_version) + message(STATUS "CUGRAPH VERSION: ${CUGRAPH_VERSION}") + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/version_config.hpp.in + ${CMAKE_CURRENT_BINARY_DIR}/include/cugraph/version_config.hpp @ONLY) +endfunction(write_version) diff --git a/cpp/cmake/Templates/Arrow.CMakeLists.txt.cmake b/cpp/cmake/Templates/Arrow.CMakeLists.txt.cmake deleted file mode 100644 index b1eaf3f0efa..00000000000 --- a/cpp/cmake/Templates/Arrow.CMakeLists.txt.cmake +++ /dev/null @@ -1,19 +0,0 @@ -cmake_minimum_required(VERSION 3.12) - -include(ExternalProject) - -ExternalProject_Add(Arrow - GIT_REPOSITORY https://github.com/apache/arrow.git - GIT_TAG apache-arrow-0.12.1 - SOURCE_DIR "${ARROW_ROOT}/arrow" - SOURCE_SUBDIR "cpp" - BINARY_DIR "${ARROW_ROOT}/build" - INSTALL_DIR "${ARROW_ROOT}/install" - CMAKE_ARGS ${ARROW_CMAKE_ARGS} -DCMAKE_INSTALL_PREFIX=${ARROW_ROOT}/install) - - - - - - - diff --git a/cpp/cmake/Templates/GoogleTest.CMakeLists.txt.cmake b/cpp/cmake/Templates/GoogleTest.CMakeLists.txt.cmake deleted file mode 100644 index 66e1dc85a50..00000000000 --- a/cpp/cmake/Templates/GoogleTest.CMakeLists.txt.cmake +++ /dev/null @@ -1,19 +0,0 @@ -cmake_minimum_required(VERSION 3.12) - -include(ExternalProject) - -ExternalProject_Add(GoogleTest - GIT_REPOSITORY https://github.com/google/googletest.git - GIT_TAG release-1.8.0 - SOURCE_DIR "${GTEST_ROOT}/googletest" - BINARY_DIR "${GTEST_ROOT}/build" - INSTALL_DIR "${GTEST_ROOT}/install" - CMAKE_ARGS ${GTEST_CMAKE_ARGS} -DCMAKE_INSTALL_PREFIX=${GTEST_ROOT}/install) - - - - - - - - diff --git a/cpp/cmake/thirdparty/get_cuco.cmake b/cpp/cmake/thirdparty/get_cuco.cmake new file mode 100644 index 00000000000..7b9ab17bef6 --- /dev/null +++ b/cpp/cmake/thirdparty/get_cuco.cmake @@ -0,0 +1,37 @@ +#============================================================================= +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#============================================================================= + +function(find_and_configure_cuco VERSION) + + if(TARGET cuco::cuco) + return() + endif() + + rapids_cpm_find(cuco ${VERSION} + GLOBAL_TARGETS cuco cuco::cuco + CPM_ARGS + GIT_REPOSITORY https://github.com/NVIDIA/cuCollections.git + GIT_TAG b1fea0cbe4c384160740af00f7c8760846539abb + OPTIONS "BUILD_TESTS OFF" + "BUILD_BENCHMARKS OFF" + "BUILD_EXAMPLES OFF" + ) + + add_library(cuco::cuco ALIAS cuco) + +endfunction() + +find_and_configure_cuco(0.0.1) diff --git a/cpp/cmake/thirdparty/get_cuhornet.cmake b/cpp/cmake/thirdparty/get_cuhornet.cmake new file mode 100644 index 00000000000..28c83161ff4 --- /dev/null +++ b/cpp/cmake/thirdparty/get_cuhornet.cmake @@ -0,0 +1,45 @@ +#============================================================================= +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#============================================================================= + +function(find_and_configure_cuhornet) + + # We are not using the cuhornet CMake targets, so no need to call `add_subdirectory()`, + # or to use CPM + FetchContent_Declare( + cuhornet + GIT_REPOSITORY https://github.com/rapidsai/cuhornet.git + GIT_TAG 261399356e62bd76fa7628880f1a847aee713eed + SOURCE_SUBDIR hornet + ) + FetchContent_GetProperties(cuhornet) + + if(NOT cuhornet_POPULATED) + FetchContent_Populate(cuhornet) + endif() + + if(NOT TARGET cugraph::cuHornet) + add_library(cugraph::cuHornet IMPORTED INTERFACE GLOBAL) + target_include_directories(cugraph::cuHornet INTERFACE + "${cuhornet_SOURCE_DIR}/hornet/include" + "${cuhornet_SOURCE_DIR}/hornetsnest/include" + "${cuhornet_SOURCE_DIR}/xlib/include" + "${cuhornet_SOURCE_DIR}/primitives" + ) + endif() +endfunction() + + +find_and_configure_cuhornet() diff --git a/cpp/cmake/thirdparty/get_faiss.cmake b/cpp/cmake/thirdparty/get_faiss.cmake new file mode 100644 index 00000000000..a65401579cb --- /dev/null +++ b/cpp/cmake/thirdparty/get_faiss.cmake @@ -0,0 +1,53 @@ +#============================================================================= +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#============================================================================= + +function(find_and_configure_faiss) + set(oneValueArgs VERSION PINNED_TAG) + cmake_parse_arguments(PKG "${options}" "${oneValueArgs}" + "${multiValueArgs}" ${ARGN} ) + + rapids_find_generate_module(FAISS + HEADER_NAMES faiss/IndexFlat.h + LIBRARY_NAMES faiss + ) + + rapids_cpm_find(FAISS ${PKG_VERSION} + GLOBAL_TARGETS faiss + CPM_ARGS + GIT_REPOSITORY https://github.com/facebookresearch/faiss.git + GIT_TAG ${PKG_PINNED_TAG} + OPTIONS + "FAISS_ENABLE_PYTHON OFF" + "BUILD_SHARED_LIBS OFF" + "CUDAToolkit_ROOT ${CUDAToolkit_LIBRARY_DIR}" + "FAISS_ENABLE_GPU ON" + "BUILD_TESTING OFF" + "CMAKE_MESSAGE_LOG_LEVEL VERBOSE" + ) + + if(FAISS_ADDED) + set(FAISS_GPU_HEADERS ${FAISS_SOURCE_DIR} PARENT_SCOPE) + endif() + + if(TARGET faiss AND NOT TARGET FAISS::FAISS) + add_library(FAISS::FAISS ALIAS faiss) + endif() + +endfunction() + +find_and_configure_faiss(VERSION 1.7.0 + PINNED_TAG bde7c0027191f29c9dadafe4f6e68ca0ee31fb30 + ) diff --git a/cpp/cmake/thirdparty/get_gtest.cmake b/cpp/cmake/thirdparty/get_gtest.cmake new file mode 100644 index 00000000000..e413cad7601 --- /dev/null +++ b/cpp/cmake/thirdparty/get_gtest.cmake @@ -0,0 +1,43 @@ +#============================================================================= +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#============================================================================= + +function(find_and_configure_gtest VERSION) + + if(TARGET GTest::gtest) + return() + endif() + + rapids_cpm_find(GTest ${VERSION} + GLOBAL_TARGETS gmock gmock_main gtest gtest_main GTest::gmock GTest::gtest GTest::gtest_main + CPM_ARGS + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG release-${VERSION} + GIT_SHALLOW TRUE + OPTIONS "INSTALL_GTEST ON" + # googletest >= 1.10.0 provides a cmake config file -- use it if it exists + FIND_PACKAGE_ARGUMENTS "CONFIG" + ) + + if(NOT TARGET GTest::gtest) + add_library(GTest::gmock ALIAS gmock) + add_library(GTest::gmock_main ALIAS gmock_main) + add_library(GTest::gtest ALIAS gtest) + add_library(GTest::gtest_main ALIAS gtest_main) + endif() + +endfunction() + +find_and_configure_gtest(1.10.0) diff --git a/cpp/cmake/thirdparty/get_gunrock.cmake b/cpp/cmake/thirdparty/get_gunrock.cmake new file mode 100644 index 00000000000..056cd4bd5ea --- /dev/null +++ b/cpp/cmake/thirdparty/get_gunrock.cmake @@ -0,0 +1,64 @@ +#============================================================================= +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#============================================================================= + +function(find_and_configure_gunrock VERSION) + + if(NOT TARGET gunrock) + set(GUNROCK_GENCODE_SM60 OFF) + set(GUNROCK_GENCODE_SM61 OFF) + set(GUNROCK_GENCODE_SM70 OFF) + set(GUNROCK_GENCODE_SM72 OFF) + set(GUNROCK_GENCODE_SM75 OFF) + set(GUNROCK_GENCODE_SM80 OFF) + + foreach(arch IN LISTS CMAKE_CUDA_ARCHITECTURES) + string(REPLACE "-real" "" arch ${arch}) + set(GUNROCK_GENCODE_SM${arch} "ON") + endforeach() + + # FIXME: gunrock is still using ExternalProject instead of CPM, as version 1.2 + # doesn't work with CPM + + include(ExternalProject) + + set(GUNROCK_DIR ${CMAKE_CURRENT_BINARY_DIR}/gunrock) + ExternalProject_Add(gunrock_ext + GIT_REPOSITORY https://github.com/gunrock/gunrock.git + GIT_TAG v${VERSION} + PREFIX ${GUNROCK_DIR} + CMAKE_ARGS -DCMAKE_INSTALL_PREFIX= + -DGUNROCK_BUILD_SHARED_LIBS=OFF + -DGUNROCK_BUILD_TESTS=OFF + -DCUDA_AUTODETECT_GENCODE=OFF + -DGUNROCK_GENCODE_SM60=${GUNROCK_GENCODE_SM60} + -DGUNROCK_GENCODE_SM61=${GUNROCK_GENCODE_SM61} + -DGUNROCK_GENCODE_SM70=${GUNROCK_GENCODE_SM70} + -DGUNROCK_GENCODE_SM72=${GUNROCK_GENCODE_SM72} + -DGUNROCK_GENCODE_SM75=${GUNROCK_GENCODE_SM75} + -DGUNROCK_GENCODE_SM80=${GUNROCK_GENCODE_SM80} + BUILD_BYPRODUCTS ${GUNROCK_DIR}/src/gunrock_ext-build/lib/libgunrock.a + INSTALL_COMMAND "" + ) + + add_library(gunrock STATIC IMPORTED) + add_dependencies(gunrock gunrock_ext) + set_property(TARGET gunrock PROPERTY IMPORTED_LOCATION "${GUNROCK_DIR}/src/gunrock_ext-build/lib/libgunrock.a") + target_include_directories(gunrock INTERFACE "${GUNROCK_DIR}/src/gunrock_ext") + endif() +endfunction() + + +find_and_configure_gunrock(1.2) diff --git a/cpp/cmake/thirdparty/get_nccl.cmake b/cpp/cmake/thirdparty/get_nccl.cmake new file mode 100644 index 00000000000..30ec976f27c --- /dev/null +++ b/cpp/cmake/thirdparty/get_nccl.cmake @@ -0,0 +1,42 @@ +#============================================================================= +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#============================================================================= + +function(find_and_configure_nccl) + + if(TARGET NCCL::NCCL) + return() + endif() + + set(oneValueArgs VERSION PINNED_TAG) + cmake_parse_arguments(PKG "${options}" "${oneValueArgs}" + "${multiValueArgs}" ${ARGN} ) + + rapids_find_generate_module(NCCL + HEADER_NAMES nccl.h + LIBRARY_NAMES nccl + ) + + # Currently NCCL has no CMake build-system so we require + # it built and installed on the machine already + rapids_find_package(NCCL REQUIRED) + +endfunction() + +find_and_configure_nccl() + + + + diff --git a/cpp/cmake/thirdparty/get_raft.cmake b/cpp/cmake/thirdparty/get_raft.cmake new file mode 100644 index 00000000000..a819d7158e1 --- /dev/null +++ b/cpp/cmake/thirdparty/get_raft.cmake @@ -0,0 +1,47 @@ +#============================================================================= +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#============================================================================= + +function(find_and_configure_raft) + + set(oneValueArgs VERSION FORK PINNED_TAG) + cmake_parse_arguments(PKG "" "${oneValueArgs}" "" ${ARGN} ) + + rapids_cpm_find(raft ${PKG_VERSION} + GLOBAL_TARGETS raft::raft + BUILD_EXPORT_SET cugraph-exports + INSTALL_EXPORT_SET cugraph-exports + CPM_ARGS + GIT_REPOSITORY https://github.com/${PKG_FORK}/raft.git + GIT_TAG ${PKG_PINNED_TAG} + SOURCE_SUBDIR cpp + OPTIONS "BUILD_TESTS OFF" + ) + + message(VERBOSE "CUGRAPH: Using RAFT located in ${raft_SOURCE_DIR}") + +endfunction() + +set(CUGRAPH_MIN_VERSION_raft "${CUGRAPH_VERSION_MAJOR}.${CUGRAPH_VERSION_MINOR}.00") +set(CUGRAPH_BRANCH_VERSION_raft "${CUGRAPH_VERSION_MAJOR}.${CUGRAPH_VERSION_MINOR}") + + +# Change pinned tag and fork here to test a commit in CI +# To use a different RAFT locally, set the CMake variable +# RPM_raft_SOURCE=/path/to/local/raft +find_and_configure_raft(VERSION ${CUGRAPH_MIN_VERSION_raft} + FORK rapidsai + PINNED_TAG branch-${CUGRAPH_BRANCH_VERSION_raft} + ) diff --git a/cpp/cmake/thirdparty/get_rmm.cmake b/cpp/cmake/thirdparty/get_rmm.cmake new file mode 100644 index 00000000000..aecb6489f92 --- /dev/null +++ b/cpp/cmake/thirdparty/get_rmm.cmake @@ -0,0 +1,47 @@ +#============================================================================= +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#============================================================================= + +function(find_and_configure_rmm VERSION) + + if(${VERSION} MATCHES [=[([0-9]+)\.([0-9]+)\.([0-9]+)]=]) + set(MAJOR_AND_MINOR "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}") + else() + set(MAJOR_AND_MINOR "${VERSION}") + endif() + + if(TARGET rmm::rmm) + return() + endif() + + rapids_cpm_find(rmm ${VERSION} + GLOBAL_TARGETS rmm::rmm + BUILD_EXPORT_SET cugraph-exports + INSTALL_EXPORT_SET cugraph-exports + CPM_ARGS + GIT_REPOSITORY https://github.com/rapidsai/rmm.git + GIT_TAG branch-${MAJOR_AND_MINOR} + GIT_SHALLOW TRUE + OPTIONS "BUILD_TESTS OFF" + "BUILD_BENCHMARKS OFF" + "CUDA_STATIC_RUNTIME ${CUDA_STATIC_RUNTIME}" + "DISABLE_DEPRECATION_WARNING ${DISABLE_DEPRECATION_WARNING}" + ) + +endfunction() + +set(CUGRAPH_MIN_VERSION_rmm "${CUGRAPH_VERSION_MAJOR}.${CUGRAPH_VERSION_MINOR}.00") + +find_and_configure_rmm(${CUGRAPH_MIN_VERSION_rmm}) diff --git a/cpp/cmake/thirdparty/get_thrust.cmake b/cpp/cmake/thirdparty/get_thrust.cmake new file mode 100644 index 00000000000..86fcffed5d2 --- /dev/null +++ b/cpp/cmake/thirdparty/get_thrust.cmake @@ -0,0 +1,29 @@ +#============================================================================= +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#============================================================================= + +function(find_and_configure_thrust VERSION) + + rapids_cpm_find(Thrust ${VERSION} + CPM_ARGS + GIT_REPOSITORY https://github.com/thrust/thrust.git + GIT_TAG ${VERSION} + ) + + thrust_create_target(cugraph::Thrust FROM_OPTIONS) + +endfunction() + +find_and_configure_thrust(1.12.0) diff --git a/cpp/cmake/version_config.hpp.in b/cpp/cmake/version_config.hpp.in new file mode 100644 index 00000000000..c669d1b97f3 --- /dev/null +++ b/cpp/cmake/version_config.hpp.in @@ -0,0 +1,20 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#define CUGRAPH_VERSION_MAJOR @CUGRAPH_VERSION_MAJOR@ +#define CUGRAPH_VERSION_MINOR @CUGRAPH_VERSION_MINOR@ +#define CUGRAPH_VERSION_PATCH @CUGRAPH_VERSION_PATCH@ diff --git a/cpp/docs/DEVELOPER_GUIDE.md b/cpp/docs/DEVELOPER_GUIDE.md new file mode 100644 index 00000000000..b369183a262 --- /dev/null +++ b/cpp/docs/DEVELOPER_GUIDE.md @@ -0,0 +1,281 @@ +# cuGraph C++ Developer Guide + +This document serves as a guide for contributors to cuGraph C++ code. Developers should also refer +to these additional files for further documentation of cuGraph best practices. + +* [Documentation Guide](TODO) for guidelines on documenting cuGraph code. +* [Testing Guide](TODO) for guidelines on writing unit tests. +* [Benchmarking Guide](TODO) for guidelines on writing unit benchmarks. + +# Overview + +cuGraph includes a C++ library that provides GPU-accelerated graph algorithms for processing +sparse graphs. + +## Lexicon + +This section defines terminology used within cuGraph + +### COO + +COOrdinate format is one of the standard formats for representing graph data. In COO format the +graph is represented as an array of source vertex ids, an array of destination vertex ids, and an +optional array of edge weights. Edge i is identified by source_vertex_id[i], destination_vertex_id[i] +and weight[i]. + +### MORE + +# Directory Structure and File Naming + +External/public cuGraph APIs are grouped based on functionality into an appropriately titled +header file in `cugraph/cpp/include/`. For example, `cugraph/cpp/include/graph.hpp` +contains the definition of the (legacy) graph objects. Note the `.hpp` +file extension used to indicate a C++ header file. + +Header files should use the `#pragma once` include guard. + +## File extensions + +- `.hpp` : C++ header files +- `.cpp` : C++ source files +- `.cu` : CUDA C++ source files +- `.cuh` : Headers containing CUDA device code + +Header files and source files should use `.hpp` and `.cpp` extensions unless they must +be compiled by nvcc. `.cu` and `.cuh` files are more expensive to compile, so we want +to minimize the use of these files to only when necessary. A good indicator of the need +to use a `.cu` or `.cuh` file is the inclusion of `__device__` and other +symbols that are only recognized by `nvcc`. Another indicator is Thrust +algorithm APIs with a device execution policy (always `rmm::exec_policy` in cuGraph). + +## Code and Documentation Style and Formatting + +cuGraph code uses [snake_case](https://en.wikipedia.org/wiki/Snake_case) for all names except in a +few cases: unit tests and test case names may use Pascal case, aka +[UpperCamelCase](https://en.wikipedia.org/wiki/Camel_case). We do not use +[Hungarian notation](https://en.wikipedia.org/wiki/Hungarian_notation), except for the following examples: + * device data variables should be prefaced by d_ if it makes the intent clearer + * host data variables should be prefaced by h_ if it makes the intent clearer + * template parameters defining a type should be suffixed with _t + * private member variables are typically suffixed with an underscore + +```c++ +template +void algorithm_function(graph_t const &g) +{ + ... +} + +template +class utility_class +{ + ... + private: + vertex_t num_vertices_{}; +} +``` + +C++ formatting is enforced using `clang-format`. You should configure `clang-format` on your +machine to use the `cugraph/cpp/.clang-format` configuration file, and run `clang-format` on all +changed code before committing it. The easiest way to do this is to configure your editor to +"format on save". + +Aspects of code style not discussed in this document and not automatically enforceable are typically +caught during code review, or not enforced. + +### C++ Guidelines + +In general, we recommend following +[C++ Core Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines). We also +recommend watching Sean Parent's [C++ Seasoning talk](https://www.youtube.com/watch?v=W2tWOdzgXHA), +and we try to follow his rules: "No raw loops. No raw pointers. No raw synchronization primitives." + + * Prefer algorithms from STL and Thrust to raw loops. + * Prefer cugraph and RMM to raw pointers and raw memory allocation. + +Documentation is discussed in the [Documentation Guide](TODO). + +### Includes + +The following guidelines apply to organizing `#include` lines. + + * Group includes by library (e.g. cuGraph, RMM, Thrust, STL). `clang-format` will respect the + groupings and sort the individual includes within a group lexicographically. + * Separate groups by a blank line. + * Order the groups from "nearest" to "farthest". In other words, local includes, then includes + from other RAPIDS libraries, then includes from related libraries, like ``, then + includes from dependencies installed with cuGraph, and then standard headers (for example ``, + ``). + * Use <> instead of "" unless the header is in the same directory as the source file. + * Tools like `clangd` often auto-insert includes when they can, but they usually get the grouping + and brackets wrong. + * Always check that includes are only necessary for the file in which they are included. + Try to avoid excessive including especially in header files. Double check this when you remove + code. + +# cuGraph Data Structures + +Application data in cuGraph is contained in graph objects, but there are a variety of other +data structures you will use when developing cuGraph code. + +## Views and Ownership + +Resource ownership is an essential concept in cuGraph. In short, an "owning" object owns a +resource (such as device memory). It acquires that resource during construction and releases the +resource in destruction ([RAII](https://en.cppreference.com/w/cpp/language/raii)). A "non-owning" +object does not own resources. Any class in cuGraph with the `*_view` suffix is non-owning. + +## `rmm::device_memory_resource` + +cuGraph allocates all device memory via RMM memory resources (MR). See the +[RMM documentation](https://github.com/rapidsai/rmm/blob/main/README.md) for details. + +## Streams + +CUDA streams are not yet exposed in external cuGraph APIs. + +We are currently investigating the best technique for exposing this. + +### Memory Management + +cuGraph code generally eschews raw pointers and direct memory allocation. Use RMM classes built to +use `device_memory_resource`(*)s for device memory allocation with automated lifetime management. + +#### `rmm::device_buffer` +Allocates a specified number of bytes of untyped, uninitialized device memory using a +`device_memory_resource`. If no resource is explicitly provided, uses +`rmm::mr::get_current_device_resource()`. + +`rmm::device_buffer` is movable and copyable on a stream. A copy performs a deep copy of the +`device_buffer`'s device memory on the specified stream, whereas a move moves ownership of the +device memory from one `device_buffer` to another. + +```c++ +// Allocates at least 100 bytes of uninitialized device memory +// using the specified resource and stream +rmm::device_buffer buff(100, stream, mr); +void * raw_data = buff.data(); // Raw pointer to underlying device memory + +// Deep copies `buff` into `copy` on `stream` +rmm::device_buffer copy(buff, stream); + +// Moves contents of `buff` into `moved_to` +rmm::device_buffer moved_to(std::move(buff)); + +custom_memory_resource *mr...; +// Allocates 100 bytes from the custom_memory_resource +rmm::device_buffer custom_buff(100, mr, stream); +``` + +#### `rmm::device_uvector` + +Similar to a `rmm::device_vector`, allocates a contiguous set of elements in device memory but with +key differences: +- As an optimization, elements are uninitialized and no synchronization occurs at construction. +This limits the types `T` to trivially copyable types. +- All operations are stream ordered (i.e., they accept a `cuda_stream_view` specifying the stream +on which the operation is performed). + +## Namespaces + +### External +All public cuGraph APIs should be placed in the `cugraph` namespace. Example: +```c++ +namespace cugraph{ + void public_function(...); +} // namespace cugraph +``` + +### Internal + +Many functions are not meant for public use, so place them in either the `detail` or an *anonymous* +namespace, depending on the situation. + +#### `detail` namespace + +Functions or objects that will be used across *multiple* translation units (i.e., source files), +should be exposed in an internal header file and placed in the `detail` namespace. Example: + +```c++ +// some_utilities.hpp +namespace cugraph{ +namespace detail{ +void reusable_helper_function(...); +} // namespace detail +} // namespace cugraph +``` + +#### Anonymous namespace + +Functions or objects that will only be used in a *single* translation unit should be defined in an +*anonymous* namespace in the source file where it is used. Example: + +```c++ +// some_file.cpp +namespace{ +void isolated_helper_function(...); +} // anonymous namespace +``` + +[**Anonymous namespaces should *never* be used in a header file.**](https://wiki.sei.cmu.edu/confluence/display/cplusplus/DCL59-CPP.+Do+not+define+an+unnamed+namespace+in+a+header+file) + +# Error Handling + +cuGraph follows conventions (and provides utilities) enforcing compile-time and run-time +conditions and detecting and handling CUDA errors. Communication of errors is always via C++ +exceptions. + +## Runtime Conditions + +Use the `CUGRAPH_EXPECTS` macro to enforce runtime conditions necessary for correct execution. + +Example usage: +```c++ +CUGRAPH_EXPECTS(lhs.type() == rhs.type(), "Column type mismatch"); +``` + +The first argument is the conditional expression expected to resolve to `true` under normal +conditions. If the conditional evaluates to `false`, then an error has occurred and an instance of `cugraph::logic_error` is thrown. The second argument to `CUGRAPH_EXPECTS` is a short description of the +error that has occurred and is used for the exception's `what()` message. + +There are times where a particular code path, if reached, should indicate an error no matter what. +For example, often the `default` case of a `switch` statement represents an invalid alternative. +Use the `CUGRAPH_FAIL` macro for such errors. This is effectively the same as calling +`CUGRAPH_EXPECTS(false, reason)`. + +Example: +```c++ +CUGRAPH_FAIL("This code path should not be reached."); +``` + +### CUDA Error Checking + +Use the `CUDA_TRY` macro to check for the successful completion of CUDA runtime API functions. This +macro throws a `cugraph::cuda_error` exception if the CUDA API return value is not `cudaSuccess`. The +thrown exception includes a description of the CUDA error code in it's `what()` message. + +Example: + +```c++ +CUDA_TRY( cudaMemcpy(&dst, &src, num_bytes) ); +``` + +## Compile-Time Conditions + +Use `static_assert` to enforce compile-time conditions. For example, + +```c++ +template +void trivial_types_only(T t){ + static_assert(std::is_trivial::value, "This function requires a trivial type."); +... +} +``` + +# Data Types + +TBD + +# Type Dispatcher + +TBD diff --git a/cpp/include/algorithms.hpp b/cpp/include/cugraph/algorithms.hpp similarity index 69% rename from cpp/include/algorithms.hpp rename to cpp/include/cugraph/algorithms.hpp index a57e550521e..2e0d0f055c8 100644 --- a/cpp/include/algorithms.hpp +++ b/cpp/include/cugraph/algorithms.hpp @@ -1,5 +1,5 @@ /* - * 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. @@ -15,84 +15,16 @@ */ #pragma once -#include -#include -#include -#include +#include +#include +#include -namespace cugraph { +#include +#include -/** - * @brief Find the PageRank vertex values for a graph. - * - * cuGraph computes an approximation of the Pagerank eigenvector using the power method. - * The number of iterations depends on the properties of the network itself; it increases - * when the tolerance descreases and/or alpha increases toward the limiting value of 1. - * The user is free to use default values or to provide inputs for the initial guess, - * tolerance and maximum number of iterations. +#include - * - * @throws cugraph::logic_error with a custom message when an error - occurs. - * - * @tparam VT Type of vertex identifiers. Supported value : int (signed, - 32-bit) - * @tparam ET Type of edge identifiers. Supported value : int (signed, - 32-bit) - * @tparam WT Type of edge weights. Supported value : float or double. - * - * @param[in] handle Library handle (RAFT). If a communicator is set in the handle, - the multi GPU version will be selected. - * @param[in] graph cuGraph graph descriptor, should contain the connectivity - information as a transposed adjacency list (CSC). Edge weights are not used for this algorithm. - * @param[in] alpha The damping factor alpha represents the probability to follow - an outgoing edge, standard value is 0.85. Thus, 1.0-alpha is the probability to β€œteleport” to a - random vertex. Alpha should be greater than 0.0 and strictly lower than 1.0. - * The initial guess must not be the vector of 0s. Any value other - than 1 or 0 is treated as an invalid value. - * @param[in] pagerank Array of size V. Should contain the initial guess if - has_guess=true. In this case the initial guess cannot be the vector of 0s. Memory is provided and - owned by the caller. - * @param[in] personalization_subset_size (optional) Supported on single-GPU, on the roadmap for - Multi-GPU. The number of vertices for to personalize. Initialized to 0 by default. - * @param[in] personalization_subset (optional) Supported on single-GPU, on the roadmap for - Multi-GPU..= Array of size personalization_subset_size containing vertices for running personalized - pagerank. Initialized to nullptr by default. Memory is provided and owned by the caller. - * @param[in] personalization_values (optional) Supported on single-GPU, on the roadmap for - Multi-GPU. Array of size personalization_subset_size containing values associated with - personalization_subset vertices. Initialized to nullptr by default. Memory is provided and owned by - the caller. - * @param[in] tolerance Supported on single-GPU. Set the tolerance the approximation, - this parameter should be a small magnitude value. - * The lower the tolerance the better the approximation. If this - value is 0.0f, cuGraph will use the default value which is 1.0E-5. - * Setting too small a tolerance can lead to non-convergence due - to numerical roundoff. Usually values between 0.01 and 0.00001 are acceptable. - * @param[in] max_iter (optional) The maximum number of iterations before an answer is - returned. This can be used to limit the execution time and do an early exit before the solver - reaches the convergence tolerance. - * If this value is lower or equal to 0 cuGraph will use the - default value, which is 500. - * @param[in] has_guess (optional) Supported on single-GPU. This parameter is used to - notify cuGraph if it should use a user-provided initial guess. False means the user does not have a - guess, in this case cuGraph will use a uniform vector set to 1/V. - * If the value is True, cuGraph will read the pagerank parameter - and use this as an initial guess. - * @param[out] *pagerank The PageRank : pagerank[i] is the PageRank of vertex i. Memory - remains provided and owned by the caller. - * - */ -template -void pagerank(raft::handle_t const &handle, - GraphCSCView const &graph, - WT *pagerank, - VT personalization_subset_size = 0, - VT *personalization_subset = nullptr, - WT *personalization_values = nullptr, - double alpha = 0.85, - double tolerance = 1e-5, - int64_t max_iter = 500, - bool has_guess = false); +namespace cugraph { /** * @brief Compute jaccard similarity coefficient for all vertices @@ -113,7 +45,7 @@ void pagerank(raft::handle_t const &handle, * caller */ template -void jaccard(GraphCSRView const &graph, WT const *weights, WT *result); +void jaccard(legacy::GraphCSRView const& graph, WT const* weights, WT* result); /** * @brief Compute jaccard similarity coefficient for selected vertex pairs @@ -137,12 +69,12 @@ void jaccard(GraphCSRView const &graph, WT const *weights, WT *resul * caller */ template -void jaccard_list(GraphCSRView const &graph, - WT const *weights, +void jaccard_list(legacy::GraphCSRView const& graph, + WT const* weights, ET num_pairs, - VT const *first, - VT const *second, - WT *result); + VT const* first, + VT const* second, + WT* result); /** * @brief Compute overlap coefficient for all vertices in the graph @@ -163,7 +95,7 @@ void jaccard_list(GraphCSRView const &graph, * caller */ template -void overlap(GraphCSRView const &graph, WT const *weights, WT *result); +void overlap(legacy::GraphCSRView const& graph, WT const* weights, WT* result); /** * @brief Compute overlap coefficient for select pairs of vertices @@ -187,12 +119,12 @@ void overlap(GraphCSRView const &graph, WT const *weights, WT *resul * caller */ template -void overlap_list(GraphCSRView const &graph, - WT const *weights, +void overlap_list(legacy::GraphCSRView const& graph, + WT const* weights, ET num_pairs, - VT const *first, - VT const *second, - WT *result); + VT const* first, + VT const* second, + WT* result); /** * @@ -210,6 +142,8 @@ void overlap_list(GraphCSRView const &graph, * @tparam weight_t Type of edge weights. Supported values : float * or double. * + * @param[in] handle Library handle (RAFT). If a communicator is set in the + * handle, the multi GPU version will be selected. * @param[in] graph cuGraph graph descriptor, should contain the * connectivity information as a COO. Graph is considered undirected. Edge weights are used for this * algorithm and set to 1 by default. @@ -246,11 +180,12 @@ void overlap_list(GraphCSRView const &graph, * */ template -void force_atlas2(GraphCOOView &graph, - float *pos, +void force_atlas2(raft::handle_t const& handle, + legacy::GraphCOOView& graph, + float* pos, const int max_iter = 500, - float *x_start = nullptr, - float *y_start = nullptr, + float* x_start = nullptr, + float* y_start = nullptr, bool outbound_attraction_distribution = true, bool lin_log_mode = false, bool prevent_overlapping = false, @@ -262,7 +197,45 @@ void force_atlas2(GraphCOOView &graph, bool strong_gravity_mode = false, const float gravity = 1.0, bool verbose = false, - internals::GraphBasedDimRedCallback *callback = nullptr); + internals::GraphBasedDimRedCallback* callback = nullptr); + +/** + * @brief Finds an approximate solution to the traveling salesperson problem (TSP). + * cuGraph computes an approximation of the TSP problem using hill climbing + * optimization. + * + * The current implementation does not support a weighted graph. + * + * @throws cugraph::logic_error when an error occurs. + * @param[in] handle Library handle (RAFT). If a communicator is set in the + * handle, the multi GPU version will be selected. + * @param[in] vtx_ptr Device array containing the vertex identifiers used + * to initialize the route. + * @param[in] x_pos Device array containing starting x-axis positions. + * @param[in] y_pos Device array containing starting y-axis positions. + * @param[in] nodes Number of cities. + * @param[in] restarts Number of starts to try. The more restarts, + * the better the solution will be approximated. The number of restarts depends on the problem + * size and should be kept low for instances above 2k cities. + * @param[in] beam_search Specify if the initial solution should use KNN + * for an approximation solution. + * @param[in] k Beam width to use in the search. + * @param[in] nstart Start from a specific position. + * @param[in] verbose Logs configuration and iterative improvement. + * @param[out] route Device array containing the returned route. + * + */ +float traveling_salesperson(raft::handle_t const& handle, + int const* vtx_ptr, + float const* x_pos, + float const* y_pos, + int nodes, + int restarts, + bool beam_search, + int k, + int nstart, + bool verbose, + int* route); /** * @brief Compute betweenness centrality for a graph @@ -302,14 +275,14 @@ void force_atlas2(GraphCOOView &graph, * */ template -void betweenness_centrality(const raft::handle_t &handle, - GraphCSRView const &graph, - result_t *result, +void betweenness_centrality(const raft::handle_t& handle, + legacy::GraphCSRView const& graph, + result_t* result, bool normalized = true, bool endpoints = false, - weight_t const *weight = nullptr, + weight_t const* weight = nullptr, vertex_t k = 0, - vertex_t const *vertices = nullptr); + vertex_t const* vertices = nullptr); /** * @brief Compute edge betweenness centrality for a graph @@ -346,13 +319,13 @@ void betweenness_centrality(const raft::handle_t &handle, * */ template -void edge_betweenness_centrality(const raft::handle_t &handle, - GraphCSRView const &graph, - result_t *result, +void edge_betweenness_centrality(const raft::handle_t& handle, + legacy::GraphCSRView const& graph, + result_t* result, bool normalized = true, - weight_t const *weight = nullptr, + weight_t const* weight = nullptr, vertex_t k = 0, - vertex_t const *vertices = nullptr); + vertex_t const* vertices = nullptr); enum class cugraph_cc_t { CUGRAPH_WEAK = 0, ///> Weakly Connected Components @@ -390,9 +363,9 @@ enum class cugraph_cc_t { * associated with vertex id i. */ template -void connected_components(GraphCSRView const &graph, +void connected_components(legacy::GraphCSRView const& graph, cugraph_cc_t connectivity_type, - VT *labels); + VT* labels); /** * @brief Compute k truss for a graph @@ -419,10 +392,10 @@ void connected_components(GraphCSRView const &graph, * */ template -std::unique_ptr> k_truss_subgraph( - GraphCOOView const &graph, +std::unique_ptr> k_truss_subgraph( + legacy::GraphCOOView const& graph, int k, - rmm::mr::device_memory_resource *mr = rmm::mr::get_current_device_resource()); + rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource()); /** * @brief Compute the Katz centrality for the nodes of the graph G @@ -457,8 +430,8 @@ std::unique_ptr> k_truss_subgraph( * @param[in] normalized If True normalize the resulting katz centrality values */ template -void katz_centrality(GraphCSRView const &graph, - result_t *result, +void katz_centrality(legacy::GraphCSRView const& graph, + result_t* result, double alpha, int max_iter, double tol, @@ -475,7 +448,7 @@ void katz_centrality(GraphCSRView const &graph, */ /* ----------------------------------------------------------------------------*/ template -void core_number(GraphCSRView const &graph, VT *core_number); +void core_number(legacy::GraphCSRView const& graph, VT* core_number); /** * @brief Compute K Core of the graph G @@ -499,13 +472,13 @@ void core_number(GraphCSRView const &graph, VT *core_number); * @param[out] out_graph Unique pointer to K Core subgraph in COO format */ template -std::unique_ptr> k_core( - GraphCOOView const &graph, +std::unique_ptr> k_core( + legacy::GraphCOOView const& graph, int k, - VT const *vertex_id, - VT const *core_number, + VT const* vertex_id, + VT const* core_number, VT num_vertex_ids, - rmm::mr::device_memory_resource *mr = rmm::mr::get_current_device_resource()); + rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource()); /** * @brief Find all 2-hop neighbors in the graph @@ -525,7 +498,8 @@ std::unique_ptr> k_core( * @return Graph in COO format */ template -std::unique_ptr> get_two_hop_neighbors(GraphCSRView const &graph); +std::unique_ptr> get_two_hop_neighbors( + legacy::GraphCSRView const& graph); /** * @Synopsis Performs a single source shortest path traversal of a graph starting from a vertex. @@ -552,13 +526,13 @@ std::unique_ptr> get_two_hop_neighbors(GraphCSRView -void sssp(GraphCSRView const &graph, - WT *distances, - VT *predecessors, +void sssp(legacy::GraphCSRView const& graph, + WT* distances, + VT* predecessors, const VT source_vertex); // FIXME: Internally distances is of int (signed 32-bit) data type, but current -// template uses data from VT, ET, WT from he GraphCSR View even if weights +// template uses data from VT, ET, WT from the legacy::GraphCSR View even if weights // are not considered /** * @Synopsis Performs a breadth first search traversal of a graph starting from a vertex. @@ -593,11 +567,11 @@ void sssp(GraphCSRView const &graph, * */ template -void bfs(raft::handle_t const &handle, - GraphCSRView const &graph, - VT *distances, - VT *predecessors, - double *sp_counters, +void bfs(raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + VT* distances, + VT* predecessors, + double* sp_counters, const VT start_vertex, bool directed = true, bool mg_batch = false); @@ -622,16 +596,50 @@ void bfs(raft::handle_t const &handle, * @param[in] graph cuGRAPH COO graph * @param[in] num_workers number of vertices in the worker set * @param[in] workers device pointer to an array of worker vertex ids - * @param[out] assignment device pointer to an array to which the assignment will be + * @param[out] assignments device pointer to an array to which the assignment will be * written. The array should be num_workers long, and will identify which vertex id (job) is * assigned to that worker */ template -weight_t hungarian(raft::handle_t const &handle, - GraphCOOView const &graph, +weight_t hungarian(raft::handle_t const& handle, + legacy::GraphCOOView const& graph, vertex_t num_workers, - vertex_t const *workers, - vertex_t *assignment); + vertex_t const* workers, + vertex_t* assignments); + +/** + * @brief Compute Hungarian algorithm on a weighted bipartite graph + * + * The Hungarian algorithm computes an assigment of "jobs" to "workers". This function accepts + * a weighted graph and a vertex list identifying the "workers". The weights in the weighted + * graph identify the cost of assigning a particular job to a worker. The algorithm computes + * a minimum cost assignment and returns the cost as well as a vector identifying the assignment. + * + * @throws cugraph::logic_error when an error occurs. + * + * @tparam vertex_t Type of vertex identifiers. Supported value : int (signed, + * 32-bit) + * @tparam edge_t Type of edge identifiers. Supported value : int (signed, + * 32-bit) + * @tparam weight_t Type of edge weights. Supported values : float or double. + * + * @param[in] handle Library handle (RAFT). If a communicator is set in the handle, + * @param[in] graph cuGRAPH COO graph + * @param[in] num_workers number of vertices in the worker set + * @param[in] workers device pointer to an array of worker vertex ids + * @param[out] assignments device pointer to an array to which the assignment will be + * written. The array should be num_workers long, and will identify which vertex id (job) is + * assigned to that worker + * @param[in] epsilon parameter to define precision of comparisons + * in reducing weights to zero. + */ +template +weight_t hungarian(raft::handle_t const& handle, + legacy::GraphCOOView const& graph, + vertex_t num_workers, + vertex_t const* workers, + vertex_t* assignments, + weight_t epsilon); /** * @brief Louvain implementation @@ -646,7 +654,7 @@ weight_t hungarian(raft::handle_t const &handle, * * @throws cugraph::logic_error when an error occurs. * - * @tparam graph_t Type of graph + * @tparam graph_view_t Type of graph * * @param[in] handle Library handle (RAFT). If a communicator is set in the handle, * @param[in] graph input graph object (CSR) @@ -663,13 +671,74 @@ weight_t hungarian(raft::handle_t const &handle, * 2) modularity of the returned clustering * */ -template -std::pair louvain( - raft::handle_t const &handle, - graph_t const &graph, - typename graph_t::vertex_type *clustering, - size_t max_level = 100, - typename graph_t::weight_type resolution = typename graph_t::weight_type{1}); +template +std::pair louvain( + raft::handle_t const& handle, + graph_view_t const& graph_view, + typename graph_view_t::vertex_type* clustering, + size_t max_level = 100, + typename graph_view_t::weight_type resolution = typename graph_view_t::weight_type{1}); + +/** + * @brief Louvain implementation, returning dendrogram + * + * Compute a clustering of the graph by maximizing modularity + * + * Computed using the Louvain method described in: + * + * VD Blondel, J-L Guillaume, R Lambiotte and E Lefebvre: Fast unfolding of + * community hierarchies in large networks, J Stat Mech P10008 (2008), + * http://arxiv.org/abs/0803.0476 + * + * @throws cugraph::logic_error when an error occurs. + * + * @tparam graph_view_t Type of graph + * + * @param[in] handle Library handle (RAFT) + * @param[in] graph_view Input graph view object (CSR) + * @param[in] max_level (optional) maximum number of levels to run (default 100) + * @param[in] resolution (optional) The value of the resolution parameter to use. + * Called gamma in the modularity formula, this changes the size + * of the communities. Higher resolutions lead to more smaller + * communities, lower resolutions lead to fewer larger + * communities. (default 1) + * + * @return a pair containing: + * 1) unique pointer to dendrogram + * 2) modularity of the returned clustering + * + */ +template +std::pair>, + typename graph_view_t::weight_type> +louvain(raft::handle_t const& handle, + graph_view_t const& graph_view, + size_t max_level = 100, + typename graph_view_t::weight_type resolution = typename graph_view_t::weight_type{1}); + +/** + * @brief Flatten a Dendrogram at a particular level + * + * A Dendrogram represents a hierarchical clustering/partitioning of + * a graph. This function will flatten the hierarchical clustering into + * a label for each vertex representing the final cluster/partition to + * which it is assigned + * + * @throws cugraph::logic_error when an error occurs. + * + * @tparam graph_view_t Type of graph + * + * @param[in] handle Library handle (RAFT). If a communicator is set in the handle, + * @param[in] graph input graph object + * @param[in] dendrogram input dendrogram object + * @param[out] clustering Pointer to device array where the clustering should be stored + * + */ +template +void flatten_dendrogram(raft::handle_t const& handle, + graph_view_t const& graph_view, + Dendrogram const& dendrogram, + typename graph_view_t::vertex_type* clustering); /** * @brief Leiden implementation @@ -706,9 +775,9 @@ std::pair louvain( * 2) modularity of the returned clustering */ template -std::pair leiden(raft::handle_t const &handle, - GraphCSRView const &graph, - vertex_t *clustering, +std::pair leiden(raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + vertex_t* clustering, size_t max_iter = 100, weight_t resolution = weight_t{1}); @@ -738,11 +807,11 @@ std::pair leiden(raft::handle_t const &handle, * written */ template -void ecg(raft::handle_t const &handle, - GraphCSRView const &graph, +void ecg(raft::handle_t const& handle, + legacy::GraphCSRView const& graph, weight_t min_weight, vertex_t ensemble_size, - vertex_t *clustering); + vertex_t* clustering); /** * @brief Generate edges in a minimum spanning forest of an undirected weighted graph. @@ -765,10 +834,10 @@ void ecg(raft::handle_t const &handle, * @return out_graph Unique pointer to MSF subgraph in COO format */ template -std::unique_ptr> minimum_spanning_tree( - raft::handle_t const &handle, - GraphCSRView const &graph, - rmm::mr::device_memory_resource *mr = rmm::mr::get_current_device_resource()); +std::unique_ptr> minimum_spanning_tree( + raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource()); namespace triangle { /** @@ -787,7 +856,7 @@ namespace triangle { * @return The number of triangles */ template -uint64_t triangle_count(GraphCSRView const &graph); +uint64_t triangle_count(legacy::GraphCSRView const& graph); } // namespace triangle namespace subgraph { @@ -812,9 +881,9 @@ namespace subgraph { * @param[out] result a graph in COO format containing the edges in the subgraph */ template -std::unique_ptr> extract_subgraph_vertex(GraphCOOView const &graph, - VT const *vertices, - VT num_vertices); +std::unique_ptr> extract_subgraph_vertex( + legacy::GraphCOOView const& graph, VT const* vertices, VT num_vertices); +} // namespace subgraph /** * @brief Wrapper function for Nvgraph balanced cut clustering @@ -837,18 +906,17 @@ std::unique_ptr> extract_subgraph_vertex(GraphCOOView -void balancedCutClustering(GraphCSRView const &graph, +void balancedCutClustering(legacy::GraphCSRView const& graph, VT num_clusters, VT num_eigen_vects, WT evs_tolerance, int evs_max_iter, WT kmean_tolerance, int kmean_max_iter, - VT *clustering); + VT* clustering); /** * @brief Wrapper function for Nvgraph spectral modularity maximization algorithm @@ -872,14 +940,14 @@ void balancedCutClustering(GraphCSRView const &graph, * be stored */ template -void spectralModularityMaximization(GraphCSRView const &graph, +void spectralModularityMaximization(legacy::GraphCSRView const& graph, VT n_clusters, VT n_eig_vects, WT evs_tolerance, int evs_max_iter, WT kmean_tolerance, int kmean_max_iter, - VT *clustering); + VT* clustering); /** * @brief Wrapper function for Nvgraph clustering modularity metric @@ -898,10 +966,10 @@ void spectralModularityMaximization(GraphCSRView const &graph, * @param[out] score Pointer to a float in which the result will be written */ template -void analyzeClustering_modularity(GraphCSRView const &graph, +void analyzeClustering_modularity(legacy::GraphCSRView const& graph, int n_clusters, - VT const *clustering, - WT *score); + VT const* clustering, + WT* score); /** * @brief Wrapper function for Nvgraph clustering edge cut metric @@ -920,10 +988,10 @@ void analyzeClustering_modularity(GraphCSRView const &graph, * @param[out] score Pointer to a float in which the result will be written */ template -void analyzeClustering_edge_cut(GraphCSRView const &graph, +void analyzeClustering_edge_cut(legacy::GraphCSRView const& graph, int n_clusters, - VT const *clustering, - WT *score); + VT const* clustering, + WT* score); /** * @brief Wrapper function for Nvgraph clustering ratio cut metric @@ -942,10 +1010,10 @@ void analyzeClustering_edge_cut(GraphCSRView const &graph, * @param[out] score Pointer to a float in which the result will be written */ template -void analyzeClustering_ratio_cut(GraphCSRView const &graph, +void analyzeClustering_ratio_cut(legacy::GraphCSRView const& graph, int n_clusters, - VT const *clustering, - WT *score); + VT const* clustering, + WT* score); } // namespace ext_raft @@ -978,13 +1046,13 @@ namespace gunrock { * */ template -void hits(GraphCSRView const &graph, +void hits(legacy::GraphCSRView const& graph, int max_iter, WT tolerance, - WT const *starting_value, + WT const* starting_value, bool normalized, - WT *hubs, - WT *authorities); + WT* hubs, + WT* authorities); } // namespace gunrock @@ -1007,16 +1075,48 @@ namespace dense { * @param[in] costs pointer to array of costs, stored in row major order * @param[in] num_rows number of rows in dense matrix * @param[in] num_cols number of cols in dense matrix - * @param[out] assignment device pointer to an array to which the assignment will be + * @param[out] assignments device pointer to an array to which the assignment will be + * written. The array should be num_cols long, and will identify + * which vertex id (job) is assigned to that worker + */ +template +weight_t hungarian(raft::handle_t const& handle, + weight_t const* costs, + vertex_t num_rows, + vertex_t num_columns, + vertex_t* assignments); + +/** + * @brief Compute Hungarian algorithm on a weighted bipartite graph + * + * The Hungarian algorithm computes an assigment of "jobs" to "workers". This function accepts + * a weighted graph and a vertex list identifying the "workers". The weights in the weighted + * graph identify the cost of assigning a particular job to a worker. The algorithm computes + * a minimum cost assignment and returns the cost as well as a vector identifying the assignment. + * + * @throws cugraph::logic_error when an error occurs. + * + * @tparam vertex_t Type of vertex identifiers. Supported value : int (signed, + * 32-bit) + * @tparam weight_t Type of edge weights. Supported values : float or double. + * + * @param[in] handle Library handle (RAFT). If a communicator is set in the handle, + * @param[in] costs pointer to array of costs, stored in row major order + * @param[in] num_rows number of rows in dense matrix + * @param[in] num_cols number of cols in dense matrix + * @param[out] assignments device pointer to an array to which the assignment will be * written. The array should be num_cols long, and will identify * which vertex id (job) is assigned to that worker + * @param[in] epsilon parameter to define precision of comparisons + * in reducing weights to zero. */ template -weight_t hungarian(raft::handle_t const &handle, - weight_t const *costs, +weight_t hungarian(raft::handle_t const& handle, + weight_t const* costs, vertex_t num_rows, vertex_t num_columns, - vertex_t *assignment); + vertex_t* assignments, + weight_t epsilon); } // namespace dense @@ -1035,6 +1135,8 @@ namespace experimental { * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. * @tparam edge_t Type of edge identifiers. Needs to be an integral type. * @tparam weight_t Type of edge weights. Needs to be a floating point type. + * @tparam multi_gpu Flag indicating whether template instantiation should target single-GPU (false) + * or multi-GPU (true). * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and * handles to various CUDA libraries) to run graph algorithms. * @param graph_view Graph view object. @@ -1051,10 +1153,10 @@ namespace experimental { * @param do_expensive_check A flag to run expensive checks for input arguments (if set to `true`). */ template -void bfs(raft::handle_t const &handle, - graph_view_t const &graph_view, - vertex_t *distances, - vertex_t *predecessors, +void bfs(raft::handle_t const& handle, + graph_view_t const& graph_view, + vertex_t* distances, + vertex_t* predecessors, vertex_t source_vertex, bool direction_optimizing = false, vertex_t depth_limit = std::numeric_limits::max(), @@ -1073,6 +1175,8 @@ void bfs(raft::handle_t const &handle, * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. * @tparam edge_t Type of edge identifiers. Needs to be an integral type. * @tparam weight_t Type of edge weights. Needs to be a floating point type. + * @tparam multi_gpu Flag indicating whether template instantiation should target single-GPU (false) + * or multi-GPU (true). * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and * handles to various CUDA libraries) to run graph algorithms. * @param graph_view Graph view object. @@ -1084,10 +1188,10 @@ void bfs(raft::handle_t const &handle, * @param do_expensive_check A flag to run expensive checks for input arguments (if set to `true`). */ template -void sssp(raft::handle_t const &handle, - graph_view_t const &graph_view, - weight_t *distances, - vertex_t *predecessors, +void sssp(raft::handle_t const& handle, + graph_view_t const& graph_view, + weight_t* distances, + vertex_t* predecessors, vertex_t source_vertex, weight_t cutoff = std::numeric_limits::max(), bool do_expensive_check = false); @@ -1105,20 +1209,22 @@ void sssp(raft::handle_t const &handle, * @tparam edge_t Type of edge identifiers. Needs to be an integral type. * @tparam weight_t Type of edge weights. Needs to be a floating point type. * @tparam result_t Type of PageRank scores. + * @tparam multi_gpu Flag indicating whether template instantiation should target single-GPU (false) + * or multi-GPU (true). * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and * handles to various CUDA libraries) to run graph algorithms. * @param graph_view Graph view object. - * @param adj_matrix_row_out_weight_sums Pointer to an array storing sums of out-going edge weights - * for the vertices in the rows of the graph adjacency matrix (for re-use) or `nullptr`. If - * `nullptr`, these values are freshly computed. Computing these values outsid this function reduces - * the number of memoray allocations/deallocations and computing if a user repeatedly computes - * PageRank scores using the same graph with different personalization vectors. + * @param precomputed_vertex_out_weight_sums Pointer to an array storing sums of out-going edge + * weights for the vertices (for re-use) or `std::nullopt`. If `std::nullopt`, these values are + * freshly computed. Computing these values outside this function reduces the number of memory + * allocations/deallocations and computing if a user repeatedly computes PageRank scores using the + * same graph with different personalization vectors. * @param personalization_vertices Pointer to an array storing personalization vertex identifiers - * (compute personalized PageRank) or `nullptr` (compute general PageRank). + * (compute personalized PageRank) or `std::nullopt` (compute general PageRank). * @param personalization_values Pointer to an array storing personalization values for the vertices - * in the personalization set. Relevant only if @p personalization_vertices is not `nullptr`. + * in the personalization set. Relevant only if @p personalization_vertices is not `std::nullopt`. * @param personalization_vector_size Size of the personalization set. If @personalization_vertices - * is not `nullptr`, the sizes of the arrays pointed by @p personalization_vertices and @p + * is not `std::nullopt`, the sizes of the arrays pointed by @p personalization_vertices and @p * personalization_values should be @p personalization_vector_size. * @param pageranks Pointer to the output PageRank score array. * @param alpha PageRank damping factor. @@ -1132,13 +1238,13 @@ void sssp(raft::handle_t const &handle, * @param do_expensive_check A flag to run expensive checks for input arguments (if set to `true`). */ template -void pagerank(raft::handle_t const &handle, - graph_view_t const &graph_view, - weight_t *adj_matrix_row_out_weight_sums, - vertex_t *personalization_vertices, - result_t *personalization_values, - vertex_t personalization_vector_size, - result_t *pageranks, +void pagerank(raft::handle_t const& handle, + graph_view_t const& graph_view, + std::optional precomputed_vertex_out_weight_sums, + std::optional personalization_vertices, + std::optional personalization_values, + std::optional personalization_vector_size, + result_t* pageranks, result_t alpha, result_t epsilon, size_t max_iterations = 500, @@ -1157,6 +1263,8 @@ void pagerank(raft::handle_t const &handle, * @tparam edge_t Type of edge identifiers. Needs to be an integral type. * @tparam weight_t Type of edge weights. Needs to be a floating point type. * @tparam result_t Type of Katz Centrality scores. + * @tparam multi_gpu Flag indicating whether template instantiation should target single-GPU (false) + * or multi-GPU (true). * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and * handles to various CUDA libraries) to run graph algorithms. * @param graph_view Graph view object. @@ -1180,10 +1288,10 @@ void pagerank(raft::handle_t const &handle, * @param do_expensive_check A flag to run expensive checks for input arguments (if set to `true`). */ template -void katz_centrality(raft::handle_t const &handle, - graph_view_t const &graph_view, - result_t *betas, - result_t *katz_centralities, +void katz_centrality(raft::handle_t const& handle, + graph_view_t const& graph_view, + result_t const* betas, + result_t* katz_centralities, result_t alpha, result_t beta, result_t epsilon, @@ -1191,6 +1299,96 @@ void katz_centrality(raft::handle_t const &handle, bool has_initial_guess = false, bool normalize = false, bool do_expensive_check = false); +/** + * @brief returns induced EgoNet subgraph(s) of neighbors centered at nodes in source_vertex within + * a given radius. + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @tparam edge_t Type of edge identifiers. Needs to be an integral type. + * @tparam weight_t Type of edge weights. Needs to be a floating point type. + * @tparam multi_gpu Flag indicating whether template instantiation should target single-GPU (false) + * or multi-GPU (true). + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. Must have at least one worker stream. + * @param graph_view Graph view object of, we extract induced egonet subgraphs from @p graph_view. + * @param source_vertex Pointer to egonet center vertices (size == @p n_subgraphs). + * @param n_subgraphs Number of induced EgoNet subgraphs to extract (ie. number of elements in @p + * source_vertex). + * @param radius Include all neighbors of distance <= radius from @p source_vertex. + * @return std::tuple, rmm::device_uvector, + * rmm::device_uvector, rmm::device_uvector> Quadraplet of edge source vertices, + * edge destination vertices, edge weights, and edge offsets for each induced EgoNet subgraph. + */ +template +std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_ego(raft::handle_t const& handle, + graph_view_t const& graph_view, + vertex_t* source_vertex, + vertex_t n_subgraphs, + vertex_t radius); + +/** + * @brief returns random walks (RW) from starting sources, where each path is of given maximum + * length. Uniform distribution is assumed for the random engine. + * + * @tparam graph_t Type of graph/view (typically, graph_view_t). + * @tparam index_t Type used to store indexing and sizes. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param graph Graph (view )object to generate RW on. + * @param ptr_d_start Device pointer to set of starting vertex indices for the RW. + * @param num_paths = number(paths). + * @param max_depth maximum length of RWs. + * @param use_padding (optional) specifies if return uses padded format (true), or coalesced + * (compressed) format; when padding is used the output is a matrix of vertex paths and a matrix of + * edges paths (weights); in this case the matrices are stored in row major order; the vertex path + * matrix is padded with `num_vertices` values and the weight matrix is padded with `0` values; + * @return std::tuple, rmm::device_uvector, + * rmm::device_uvector> Triplet of either padded or coalesced RW paths; in the coalesced + * case (default), the return consists of corresponding vertex and edge weights for each, and + * corresponding path sizes. This is meant to minimize the number of DF's to be passed to the Python + * layer. The meaning of "coalesced" here is that a 2D array of paths of different sizes is + * represented as a 1D contiguous array. In the padded case the return is a matrix of num_paths x + * max_depth vertex paths; and num_paths x (max_depth-1) edge (weight) paths, with an empty array of + * sizes. Note: if the graph is un-weighted the edge (weight) paths consists of `weight_t{1}` + * entries; + */ +template +std::tuple, + rmm::device_uvector, + rmm::device_uvector> +random_walks(raft::handle_t const& handle, + graph_t const& graph, + typename graph_t::vertex_type const* ptr_d_start, + index_t num_paths, + index_t max_depth, + bool use_padding = false); + +/** + * @brief Finds (weakly-connected-)component IDs of each vertices in the input graph. + * + * The input graph must be symmetric. Component IDs can be arbitrary integers (they can be + * non-consecutive and are not ordered by component size or any other criterion). + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @tparam edge_t Type of edge identifiers. Needs to be an integral type. + * @tparam weight_t Type of edge weights. Needs to be a floating point type. + * @tparam multi_gpu Flag indicating whether template instantiation should target single-GPU (false) + * or multi-GPU (true). + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * @param graph_view Graph view object. + * @param components Pointer to the output component ID array. + * @param do_expensive_check A flag to run expensive checks for input arguments (if set to `true`). + */ +template +void weakly_connected_components( + raft::handle_t const& handle, + graph_view_t const& graph_view, + vertex_t* components, + bool do_expensive_check = false); } // namespace experimental } // namespace cugraph diff --git a/cpp/include/compute_partition.cuh b/cpp/include/cugraph/compute_partition.cuh similarity index 79% rename from cpp/include/compute_partition.cuh rename to cpp/include/cugraph/compute_partition.cuh index c81a6237b31..b8ad0fc19ab 100644 --- a/cpp/include/compute_partition.cuh +++ b/cpp/include/cugraph/compute_partition.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -17,7 +17,7 @@ #include -#include +#include #include @@ -39,27 +39,32 @@ class compute_partition_t { using graph_view_t = graph_view_type; using vertex_t = typename graph_view_type::vertex_type; - compute_partition_t(graph_view_t const &graph_view) + compute_partition_t(raft::handle_t const& handle, graph_view_t const& graph_view) + : vertex_partition_offsets_v_(0, handle.get_stream()) { - init(graph_view); + init(handle, graph_view); } private: - template * = nullptr> - void init(graph_view_t const &graph_view) + template * = nullptr> + void init(raft::handle_t const& handle, graph_view_t const& graph_view) { } - template * = nullptr> - void init(graph_view_t const &graph_view) + template * = nullptr> + void init(raft::handle_t const& handle, graph_view_t const& graph_view) { auto partition = graph_view.get_partition(); row_size_ = partition.get_row_size(); col_size_ = partition.get_col_size(); size_ = row_size_ * col_size_; - vertex_partition_offsets_v_.resize(size_ + 1); - vertex_partition_offsets_v_ = partition.get_vertex_partition_offsets(); + vertex_partition_offsets_v_.resize(size_ + 1, handle.get_stream()); + auto vertex_partition_offsets = partition.get_vertex_partition_offsets(); + raft::update_device(vertex_partition_offsets_v_.data(), + vertex_partition_offsets.data(), + vertex_partition_offsets.size(), + handle.get_stream()); } public: @@ -76,7 +81,7 @@ class compute_partition_t { */ class vertex_device_view_t { public: - vertex_device_view_t(vertex_t const *d_vertex_partition_offsets, int size) + vertex_device_view_t(vertex_t const* d_vertex_partition_offsets, int size) : d_vertex_partition_offsets_(d_vertex_partition_offsets), size_(size) { } @@ -101,13 +106,13 @@ class compute_partition_t { } private: - vertex_t const *d_vertex_partition_offsets_; + vertex_t const* d_vertex_partition_offsets_; int size_; }; class edge_device_view_t { public: - edge_device_view_t(vertex_t const *d_vertex_partition_offsets, + edge_device_view_t(vertex_t const* d_vertex_partition_offsets, int row_size, int col_size, int size) @@ -153,7 +158,7 @@ class compute_partition_t { } private: - vertex_t const *d_vertex_partition_offsets_; + vertex_t const* d_vertex_partition_offsets_; int row_size_; int col_size_; int size_; @@ -166,7 +171,7 @@ class compute_partition_t { */ vertex_device_view_t vertex_device_view() const { - return vertex_device_view_t(vertex_partition_offsets_v_.data().get(), size_); + return vertex_device_view_t(vertex_partition_offsets_v_.data(), size_); } /** @@ -176,12 +181,11 @@ class compute_partition_t { */ edge_device_view_t edge_device_view() const { - return edge_device_view_t( - vertex_partition_offsets_v_.data().get(), row_size_, col_size_, size_); + return edge_device_view_t(vertex_partition_offsets_v_.data(), row_size_, col_size_, size_); } private: - rmm::device_vector vertex_partition_offsets_v_{}; + rmm::device_uvector vertex_partition_offsets_v_; int row_size_{1}; int col_size_{1}; int size_{1}; diff --git a/cpp/include/cugraph/dendrogram.hpp b/cpp/include/cugraph/dendrogram.hpp new file mode 100644 index 00000000000..beebec4fd3f --- /dev/null +++ b/cpp/include/cugraph/dendrogram.hpp @@ -0,0 +1,70 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include + +#include +#include + +namespace cugraph { + +template +class Dendrogram { + public: + void add_level(vertex_t first_index, + vertex_t num_verts, + rmm::cuda_stream_view stream_view, + rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource()) + { + level_ptr_.push_back( + std::make_unique>(num_verts, stream_view, mr)); + level_first_index_.push_back(first_index); + } + + size_t current_level() const { return level_ptr_.size() - 1; } + + size_t num_levels() const { return level_ptr_.size(); } + + vertex_t const* get_level_ptr_nocheck(size_t level) const { return level_ptr_[level]->data(); } + + vertex_t* get_level_ptr_nocheck(size_t level) { return level_ptr_[level]->data(); } + + size_t get_level_size_nocheck(size_t level) const { return level_ptr_[level]->size(); } + + vertex_t get_level_first_index_nocheck(size_t level) const { return level_first_index_[level]; } + + vertex_t const* current_level_begin() const { return get_level_ptr_nocheck(current_level()); } + + vertex_t const* current_level_end() const { return current_level_begin() + current_level_size(); } + + vertex_t* current_level_begin() { return get_level_ptr_nocheck(current_level()); } + + vertex_t* current_level_end() { return current_level_begin() + current_level_size(); } + + size_t current_level_size() const { return get_level_size_nocheck(current_level()); } + + vertex_t current_level_first_index() const + { + return get_level_first_index_nocheck(current_level()); + } + + private: + std::vector level_first_index_; + std::vector>> level_ptr_; +}; + +} // namespace cugraph diff --git a/cpp/include/cugraph/detail/shuffle_wrappers.hpp b/cpp/include/cugraph/detail/shuffle_wrappers.hpp new file mode 100644 index 00000000000..fcfd98db447 --- /dev/null +++ b/cpp/include/cugraph/detail/shuffle_wrappers.hpp @@ -0,0 +1,102 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include + +namespace cugraph { +namespace detail { + +/** + * @brief Shuffle edgelist using the edge key function + * + * NOTE: d_edgelist_rows, d_edgelist_cols and d_edgelist_weights + * are modified within this function (data is sorted) + * But the actual output is returned. The exact contents + * of d_edgelist_rows, d_edgelist_cols and d_edgelist_weights + * after the function is undefined. + * + * @tparam vertex_t vertex type + * @tparam weight_t weight type + * + * @param[in] handle raft handle + * @param[in/out] d_edgelist_rows vertex ids for row + * @param[in/out] d_edgelist_cols vertex ids for column + * @param[in/out] d_edgelist_weights optional edge weights + * @param[in] store_transposed true if operating on + * transposed matrix + * + * @return tuple of shuffled rows, columns and optional weights + */ +template +std::tuple, + rmm::device_uvector, + std::optional>> +shuffle_edgelist_by_edge(raft::handle_t const& handle, + rmm::device_uvector& d_edgelist_rows, + rmm::device_uvector& d_edgelist_cols, + std::optional>& d_edgelist_weights, + bool store_transposed); + +/** + * @brief Shuffle vertices using the vertex key function + * + * NOTE: d_value is modified within this function + * (data is sorted). But the actual output is returned. + * The exact contents of d_value after the function is + * undefined. + * + * @tparam vertex_t vertex type + * + * @param[in] handle raft handle + * @param[in/out] d_vertices vertex ids to shuffle + * + * @return device vector of shuffled vertices + */ +template +rmm::device_uvector shuffle_vertices(raft::handle_t const& handle, + rmm::device_uvector& d_vertices); + +/** + * @brief Groupby and count edgelist using the edge key function + * + * NOTE: d_edgelist_rows, d_edgelist_cols and d_edgelist_weights + * are modified within this function (data is sorted) + * But the actual output is returned. The exact contents + * of d_edgelist_rows, d_edgelist_cols and d_edgelist_weights + * after the function is undefined. + * + * @tparam vertex_t vertex type + * @tparam weight_t weight type + * + * @param[in] handle raft handle + * @param[in/out] d_edgelist_rows vertex ids for row + * @param[in/out] d_edgelist_cols vertex ids for column + * @param[in/out] d_edgelist_weights optional edge weights + * + * @return tuple of shuffled rows, columns and optional weights + */ +template +rmm::device_uvector groupby_and_count_by_edge( + raft::handle_t const& handle, + rmm::device_uvector& d_edgelist_rows, + rmm::device_uvector& d_edgelist_cols, + std::optional>& d_edgelist_weights, + size_t number_of_local_adj_matrix_partitions); + +} // namespace detail +} // namespace cugraph diff --git a/cpp/include/cugraph/detail/utility_wrappers.hpp b/cpp/include/cugraph/detail/utility_wrappers.hpp new file mode 100644 index 00000000000..580ca00250a --- /dev/null +++ b/cpp/include/cugraph/detail/utility_wrappers.hpp @@ -0,0 +1,89 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include + +namespace cugraph { +namespace detail { + +/** + * @brief Fill a buffer with uniformly distributed random values + * + * Fills a buffer with uniformly distributed random values between + * the specified minimum and maximum values. + * + * @tparam value_t type of the value to operate on + * + * @param[in] stream_view stream view + * @param[out] d_value device array to fill + * @param[in] size number of elements in array + * @param[in] min_value minimum value + * @param[in] max_value maximum value + * @param[in] seed seed for initializing random number generator + * + */ +template +void uniform_random_fill(rmm::cuda_stream_view const& stream_view, + value_t* d_value, + size_t size, + value_t min_value, + value_t max_value, + uint64_t seed); + +/** + * @brief Fill a buffer with a sequence of values + * + * Fills the buffer with the sequence: + * {start_value, start_value+1, start_value+2, ..., start_value+size-1} + * + * Similar to the function std::iota, wraps the function thrust::sequence + * + * @tparam value_t type of the value to operate on + * + * @param[in] stream_view stream view + * @param[out] d_value device array to fill + * @param[in] size number of elements in array + * @param[in] start_value starting value for sequence + * + */ +template +void sequence_fill(rmm::cuda_stream_view const& stream_view, + value_t* d_value, + size_t size, + value_t start_value); + +/** + * @brief Compute the maximum vertex id of an edge list + * + * max(d_edgelist_rows.max(), d_edgelist_cols.max()) + * + * @tparam vertex_t vertex type + * + * @param[in] stream_view stream view + * @param[in] d_edgelist_rows device array to fill + * @param[in] d_edgelist_cols number of elements in array + * + * @param the maximum value occurring in the edge list + */ +template +vertex_t compute_maximum_vertex_id(rmm::cuda_stream_view const& stream_view, + rmm::device_uvector const& d_edgelist_rows, + rmm::device_uvector const& d_edgelist_cols); + +} // namespace detail +} // namespace cugraph diff --git a/cpp/include/cugraph/experimental/detail/graph_utils.cuh b/cpp/include/cugraph/experimental/detail/graph_utils.cuh new file mode 100644 index 00000000000..02da9a80854 --- /dev/null +++ b/cpp/include/cugraph/experimental/detail/graph_utils.cuh @@ -0,0 +1,177 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include + +namespace cugraph { +namespace experimental { +namespace detail { + +// compute the numbers of nonzeros in rows (of the graph adjacency matrix, if store_transposed = +// false) or columns (of the graph adjacency matrix, if store_transposed = true) +template +rmm::device_uvector compute_major_degrees( + raft::handle_t const& handle, + std::vector const& adj_matrix_partition_offsets, + std::optional> const& adj_matrix_partition_dcs_nzd_vertices, + std::optional> const& adj_matrix_partition_dcs_nzd_vertex_counts, + partition_t const& partition, + std::optional> const& adj_matrix_partition_segment_offsets) +{ + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_rank = row_comm.get_rank(); + auto const row_comm_size = row_comm.get_size(); + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_rank = col_comm.get_rank(); + auto const col_comm_size = col_comm.get_size(); + + auto use_dcs = adj_matrix_partition_dcs_nzd_vertices.has_value(); + + rmm::device_uvector local_degrees(0, handle.get_stream()); + rmm::device_uvector degrees(0, handle.get_stream()); + + vertex_t max_num_local_degrees{0}; + for (int i = 0; i < col_comm_size; ++i) { + auto vertex_partition_idx = static_cast(i * row_comm_size + row_comm_rank); + auto vertex_partition_size = partition.get_vertex_partition_size(vertex_partition_idx); + max_num_local_degrees = std::max(max_num_local_degrees, vertex_partition_size); + if (i == col_comm_rank) { degrees.resize(vertex_partition_size, handle.get_stream()); } + } + local_degrees.resize(max_num_local_degrees, handle.get_stream()); + for (int i = 0; i < col_comm_size; ++i) { + auto vertex_partition_idx = static_cast(i * row_comm_size + row_comm_rank); + vertex_t major_first{}; + vertex_t major_last{}; + std::tie(major_first, major_last) = partition.get_vertex_partition_range(vertex_partition_idx); + auto p_offsets = adj_matrix_partition_offsets[i]; + auto major_hypersparse_first = + use_dcs ? major_first + (*adj_matrix_partition_segment_offsets) + [(detail::num_sparse_segments_per_vertex_partition + 2) * i + + detail::num_sparse_segments_per_vertex_partition] + : major_last; + thrust::transform(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + thrust::make_counting_iterator(vertex_t{0}), + thrust::make_counting_iterator(major_hypersparse_first - major_first), + local_degrees.begin(), + [p_offsets] __device__(auto i) { return p_offsets[i + 1] - p_offsets[i]; }); + if (use_dcs) { + auto p_dcs_nzd_vertices = (*adj_matrix_partition_dcs_nzd_vertices)[i]; + auto dcs_nzd_vertex_count = (*adj_matrix_partition_dcs_nzd_vertex_counts)[i]; + thrust::fill(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + local_degrees.begin() + (major_hypersparse_first - major_first), + local_degrees.begin() + (major_last - major_first), + edge_t{0}); + thrust::for_each(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + thrust::make_counting_iterator(vertex_t{0}), + thrust::make_counting_iterator(dcs_nzd_vertex_count), + [p_offsets, + p_dcs_nzd_vertices, + major_first, + major_hypersparse_first, + local_degrees = local_degrees.data()] __device__(auto i) { + auto d = p_offsets[(major_hypersparse_first - major_first) + i + 1] - + p_offsets[(major_hypersparse_first - major_first) + i]; + auto v = p_dcs_nzd_vertices[i]; + local_degrees[v - major_first] = d; + }); + } + col_comm.reduce(local_degrees.data(), + i == col_comm_rank ? degrees.data() : static_cast(nullptr), + static_cast(major_last - major_first), + raft::comms::op_t::SUM, + i, + handle.get_stream()); + } + + return degrees; +} + +// compute the numbers of nonzeros in rows (of the graph adjacency matrix, if store_transposed = +// false) or columns (of the graph adjacency matrix, if store_transposed = true) +template +rmm::device_uvector compute_major_degrees(raft::handle_t const& handle, + edge_t const* offsets, + vertex_t number_of_vertices) +{ + rmm::device_uvector degrees(number_of_vertices, handle.get_stream()); + thrust::tabulate(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + degrees.begin(), + degrees.end(), + [offsets] __device__(auto i) { return offsets[i + 1] - offsets[i]; }); + return degrees; +} + +template +struct compute_gpu_id_from_vertex_t { + int comm_size{0}; + + __device__ int operator()(vertex_t v) const + { + cuco::detail::MurmurHash3_32 hash_func{}; + return hash_func(v) % comm_size; + } +}; + +template +struct compute_gpu_id_from_edge_t { + int comm_size{0}; + int row_comm_size{0}; + int col_comm_size{0}; + + __device__ int operator()(vertex_t major, vertex_t minor) const + { + cuco::detail::MurmurHash3_32 hash_func{}; + auto major_comm_rank = static_cast(hash_func(major) % comm_size); + auto minor_comm_rank = static_cast(hash_func(minor) % comm_size); + return (minor_comm_rank / row_comm_size) * row_comm_size + (major_comm_rank % row_comm_size); + } +}; + +template +struct compute_partition_id_from_edge_t { + int comm_size{0}; + int row_comm_size{0}; + int col_comm_size{0}; + + __device__ int operator()(vertex_t major, vertex_t minor) const + { + cuco::detail::MurmurHash3_32 hash_func{}; + auto major_comm_rank = static_cast(hash_func(major) % comm_size); + auto minor_comm_rank = static_cast(hash_func(minor) % comm_size); + return major_comm_rank * col_comm_size + minor_comm_rank / row_comm_size; + } +}; + +} // namespace detail +} // namespace experimental +} // namespace cugraph diff --git a/cpp/include/experimental/eidecl_graph.hpp b/cpp/include/cugraph/experimental/eidecl_graph.hpp similarity index 99% rename from cpp/include/experimental/eidecl_graph.hpp rename to cpp/include/cugraph/experimental/eidecl_graph.hpp index b8ac201008a..18e617c0993 100644 --- a/cpp/include/experimental/eidecl_graph.hpp +++ b/cpp/include/cugraph/experimental/eidecl_graph.hpp @@ -1,5 +1,5 @@ /* - * 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. diff --git a/cpp/include/experimental/eidir_graph.hpp b/cpp/include/cugraph/experimental/eidir_graph.hpp similarity index 98% rename from cpp/include/experimental/eidir_graph.hpp rename to cpp/include/cugraph/experimental/eidir_graph.hpp index 8998943ec16..93aa333dc5b 100644 --- a/cpp/include/experimental/eidir_graph.hpp +++ b/cpp/include/cugraph/experimental/eidir_graph.hpp @@ -1,5 +1,5 @@ /* - * 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. diff --git a/cpp/include/experimental/graph.hpp b/cpp/include/cugraph/experimental/graph.hpp similarity index 50% rename from cpp/include/experimental/graph.hpp rename to cpp/include/cugraph/experimental/graph.hpp index 592294c8967..9b0849c704f 100644 --- a/cpp/include/experimental/graph.hpp +++ b/cpp/include/cugraph/experimental/graph.hpp @@ -1,5 +1,5 @@ /* - * 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. @@ -15,13 +15,14 @@ */ #pragma once -#include -#include +#include +#include #include #include #include +#include #include #include #include @@ -31,9 +32,9 @@ namespace experimental { template struct edgelist_t { - vertex_t const *p_src_vertices{nullptr}; - vertex_t const *p_dst_vertices{nullptr}; - weight_t const *p_edge_weights{nullptr}; + vertex_t const* p_src_vertices{nullptr}; + vertex_t const* p_dst_vertices{nullptr}; + std::optional p_edge_weights{std::nullopt}; edge_t number_of_edges{0}; }; @@ -61,24 +62,44 @@ class graph_t> const &edge_lists, - partition_t const &partition, + graph_t(raft::handle_t const& handle) : detail::graph_base_t() {} + + graph_t(raft::handle_t const& handle, + std::vector> const& edgelists, + partition_t const& partition, vertex_t number_of_vertices, edge_t number_of_edges, graph_properties_t properties, - bool sorted_by_global_degree_within_vertex_partition, + std::optional> const& segment_offsets, bool do_expensive_check = false); - graph_view_t view() + bool is_weighted() const { return adj_matrix_partition_weights_.has_value(); } + + graph_view_t view() const { - std::vector offsets(adj_matrix_partition_offsets_.size(), nullptr); - std::vector indices(adj_matrix_partition_indices_.size(), nullptr); - std::vector weights(adj_matrix_partition_weights_.size(), nullptr); + std::vector offsets(adj_matrix_partition_offsets_.size(), nullptr); + std::vector indices(adj_matrix_partition_indices_.size(), nullptr); + auto weights = adj_matrix_partition_weights_ + ? std::make_optional>( + (*adj_matrix_partition_weights_).size(), nullptr) + : std::nullopt; + auto dcs_nzd_vertices = adj_matrix_partition_dcs_nzd_vertices_ + ? std::make_optional>( + (*adj_matrix_partition_dcs_nzd_vertices_).size(), nullptr) + : std::nullopt; + auto dcs_nzd_vertex_counts = + adj_matrix_partition_dcs_nzd_vertex_counts_ + ? std::make_optional>( + (*adj_matrix_partition_dcs_nzd_vertex_counts_).size(), vertex_t{0}) + : std::nullopt; for (size_t i = 0; i < offsets.size(); ++i) { offsets[i] = adj_matrix_partition_offsets_[i].data(); indices[i] = adj_matrix_partition_indices_[i].data(); - if (weights.size() > 0) { weights[i] = adj_matrix_partition_weights_[i].data(); } + if (weights) { (*weights)[i] = (*adj_matrix_partition_weights_)[i].data(); } + if (dcs_nzd_vertices) { + (*dcs_nzd_vertices)[i] = (*adj_matrix_partition_dcs_nzd_vertices_)[i].data(); + (*dcs_nzd_vertex_counts)[i] = (*adj_matrix_partition_dcs_nzd_vertex_counts_)[i]; + } } return graph_view_t( @@ -86,26 +107,31 @@ class graph_tget_number_of_vertices(), this->get_number_of_edges(), this->get_graph_properties(), - vertex_partition_segment_offsets_.size() > 0, + adj_matrix_partition_segment_offsets_, false); } private: std::vector> adj_matrix_partition_offsets_{}; std::vector> adj_matrix_partition_indices_{}; - std::vector> adj_matrix_partition_weights_{}; + std::optional>> adj_matrix_partition_weights_{ + std::nullopt}; + // nzd: nonzero (local) degree, relevant only if segment_offsets.size() > 0 + std::optional>> adj_matrix_partition_dcs_nzd_vertices_{ + std::nullopt}; + std::optional> adj_matrix_partition_dcs_nzd_vertex_counts_{std::nullopt}; partition_t partition_{}; - std::vector - vertex_partition_segment_offsets_{}; // segment offsets within the vertex partition based on - // vertex degree, relevant only if - // sorted_by_global_degree_within_vertex_partition is true + // segment offsets within the vertex partition based on vertex degree, relevant only if + // segment_offsets.size() > 0 + std::optional> adj_matrix_partition_segment_offsets_{std::nullopt}; }; // single-GPU version @@ -123,36 +149,62 @@ class graph_t const &edge_list, + graph_t(raft::handle_t const& handle) + : detail::graph_base_t(), + offsets_(0, handle.get_stream()), + indices_(0, handle.get_stream()){}; + + graph_t(raft::handle_t const& handle, + edgelist_t const& edgelist, vertex_t number_of_vertices, graph_properties_t properties, - bool sorted_by_degree, + std::optional> const& segment_offsets, bool do_expensive_check = false); - vertex_t get_number_of_local_vertices() const { return this->get_number_of_vertices(); } + bool is_weighted() const { return weights_.has_value(); } - graph_view_t view() + graph_view_t view() const { return graph_view_t( *(this->get_handle_ptr()), offsets_.data(), indices_.data(), - weights_.data(), - segment_offsets_, + weights_ ? std::optional{(*weights_).data()} : std::nullopt, this->get_number_of_vertices(), this->get_number_of_edges(), this->get_graph_properties(), - segment_offsets_.size() > 0, + segment_offsets_, false); } private: + friend class cugraph::serializer::serializer_t; + + // cnstr. to be used _only_ for un/serialization purposes: + // + graph_t(raft::handle_t const& handle, + vertex_t number_of_vertices, + edge_t number_of_edges, + graph_properties_t properties, + rmm::device_uvector&& offsets, + rmm::device_uvector&& indices, + std::optional>&& weights, + std::optional>&& segment_offsets) + : detail::graph_base_t( + handle, number_of_vertices, number_of_edges, properties), + offsets_(std::move(offsets)), + indices_(std::move(indices)), + weights_(std::move(weights)), + segment_offsets_(std::move(segment_offsets)) + { + } + rmm::device_uvector offsets_; rmm::device_uvector indices_; - rmm::device_uvector weights_; - std::vector segment_offsets_{}; // segment offsets based on vertex degree, relevant - // only if sorted_by_global_degree is true + std::optional> weights_{std::nullopt}; + + // segment offsets based on vertex degree, relevant only if sorted_by_global_degree is true + std::optional> segment_offsets_{}; }; template @@ -180,6 +232,24 @@ template struct invalid_edge_id : invalid_idx { }; +template +struct invalid_component_id : invalid_idx { +}; + +template +__host__ __device__ std::enable_if_t::value, bool> is_valid_vertex( + vertex_t num_vertices, vertex_t v) +{ + return (v >= 0) && (v < num_vertices); +} + +template +__host__ __device__ std::enable_if_t::value, bool> is_valid_vertex( + vertex_t num_vertices, vertex_t v) +{ + return v < num_vertices; +} + } // namespace experimental } // namespace cugraph diff --git a/cpp/include/cugraph/experimental/graph_functions.hpp b/cpp/include/cugraph/experimental/graph_functions.hpp new file mode 100644 index 00000000000..10d3f6d2216 --- /dev/null +++ b/cpp/include/cugraph/experimental/graph_functions.hpp @@ -0,0 +1,376 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include + +#include +#include + +#include +#include +#include +#include + +namespace cugraph { +namespace experimental { + +/** + * @brief renumber edgelist (multi-GPU) + * + * This function assumes that vertices and edges are pre-shuffled to their target processes using + * the compute_gpu_id_from_vertex_t & compute_gpu_id_from_edge_t functors, respectively. + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @tparam edge_t Type of edge identifiers. Needs to be an integral type. + * @tparam multi_gpu Flag indicating whether template instantiation should target single-GPU (false) + * or multi-GPU (true). + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param optional_local_vertex_span If valid, part of the entire set of vertices in the graph to be + * renumbered. The first tuple element is the pointer to the array and the second tuple element is + * the size of the array. This parameter can be used to include isolated vertices. Applying the + * compute_gpu_id_from_vertex_t to every vertex should return the local GPU ID for this function to + * work (vertices should be pre-shuffled). + * @param edgelist_major_vertices Pointers (one pointer per local graph adjacency matrix partition + * assigned to this process) to edge source vertex IDs (if the graph adjacency matrix is stored as + * is) or edge destination vertex IDs (if the transposed graph adjacency matrix is stored). Vertex + * IDs are updated in-place ([INOUT] parameter). Edges should be pre-shuffled to their final target + * process & matrix partition; i.e. applying the compute_gpu_id_from_edge_t functor to every (major, + * minor) pair should return the GPU ID of this process and applying the + * compute_partition_id_from_edge_t fuctor to every (major, minor) pair for a local matrix partition + * should return the partition ID of the corresponding matrix partition. + * @param edgelist_minor_vertices Pointers (one pointer per local graph adjacency matrix partition + * assigned to this process) to edge destination vertex IDs (if the graph adjacency matrix is stored + * as is) or edge source vertex IDs (if the transposed graph adjacency matrix is stored). Vertex IDs + * are updated in-place ([INOUT] parameter). Edges should be pre-shuffled to their final target + * process & matrix partition; i.e. applying the compute_gpu_id_from_edge_t functor to every (major, + * minor) pair should return the GPU ID of this process and applying the + * compute_partition_id_from_edge_t fuctor to every (major, minor) pair for a local matrix partition + * should return the partition ID of the corresponding matrix partition. + * @param edgelist_edge_counts Edge counts (one count per local graph adjacency matrix partition + * assigned to this process). + * @param do_expensive_check A flag to run expensive checks for input arguments (if set to `true`). + * @return std::tuple, partition_t, vertex_t, edge_t, + * std::vector> Tuple of labels (vertex IDs before renumbering) for the entire set of + * vertices (assigned to this process in multi-GPU), partition_t object storing graph partitioning + * information, total number of vertices, total number of edges, and vertex partition segment + * offsets (a vertex partition is partitioned to multiple segments based on vertex degrees). + */ +template +std::enable_if_t, + partition_t, + vertex_t, + edge_t, + std::vector>> +renumber_edgelist(raft::handle_t const& handle, + std::optional> optional_local_vertex_span, + std::vector const& edgelist_major_vertices /* [INOUT] */, + std::vector const& edgelist_minor_vertices /* [INOUT] */, + std::vector const& edgelist_edge_counts, + bool do_expensive_check = false); + +/** + * @brief renumber edgelist (single-GPU) + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @tparam edge_t Type of edge identifiers. Needs to be an integral type. + * @tparam multi_gpu Flag indicating whether template instantiation should target single-GPU (false) + * or multi-GPU (true). + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param optional_local_vertex_span If valid, vertices in the graph to be renumbered. The first + * tuple element is the pointer to the array and the second tuple element is the size of the array. + * This parameter can be used to include isolated vertices. + * @param vertices The entire set of vertices in the graph to be renumbered. + * @param num_vertices Number of vertices. + * @param edgelist_major_vertices Edge source vertex IDs (if the graph adjacency matrix is stored as + * is) or edge destination vertex IDs (if the transposed graph adjacency matrix is stored). Vertex + * IDs are updated in-place ([INOUT] parameter). + * @param edgelist_minor_vertices Edge destination vertex IDs (if the graph adjacency matrix is + * stored as is) or edge source vertex IDs (if the transposed graph adjacency matrix is stored). + * Vertex IDs are updated in-place ([INOUT] parameter). + * @param num_edgelist_edges Number of edges in the edgelist. + * @param do_expensive_check A flag to run expensive checks for input arguments (if set to `true`). + * @return std::tuple, std::vector> Tuple of abels (vertex + * IDs before renumbering) for the entire set of vertices and vertex partition segment offsets (a + * vertex partition is partitioned to multiple segments based on vertex degrees). + */ +template +std::enable_if_t, std::vector>> +renumber_edgelist(raft::handle_t const& handle, + std::optional> optional_vertex_span, + vertex_t* edgelist_major_vertices /* [INOUT] */, + vertex_t* edgelist_minor_vertices /* [INOUT] */, + edge_t num_edgelist_edges, + bool do_expensive_check = false); + +/** + * @brief Renumber external vertices to internal vertices based on the provoided @p + * renumber_map_labels. + * + * Note cugraph::experimental::invalid_id::value remains unchanged. + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @tparam multi_gpu Flag indicating whether template instantiation should target single-GPU (false) + * or multi-GPU (true). + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param vertices Pointer to the vertices to be renumbered. The input external vertices are + * renumbered to internal vertices in-place. + * @param num_vertices Number of vertices to be renumbered. + * @param renumber_map_labels Pointer to the external vertices corresponding to the internal + * vertices in the range [@p local_int_vertex_first, @p local_int_vertex_last). + * @param local_int_vertex_first The first local internal vertex (inclusive, assigned to this + * process in multi-GPU). + * @param local_int_vertex_last The last local internal vertex (exclusive, assigned to this process + * in multi-GPU). + * @param do_expensive_check A flag to run expensive checks for input arguments (if set to `true`). + */ +template +void renumber_ext_vertices(raft::handle_t const& handle, + vertex_t* vertices /* [INOUT] */, + size_t num_vertices, + vertex_t const* renumber_map_labels, + vertex_t local_int_vertex_first, + vertex_t local_int_vertex_last, + bool do_expensive_check = false); + +/** + * @brief Unrenumber local internal vertices to external vertices based on the providied @p + * renumber_map_labels. + * + * Note cugraph::experimental::invalid_id::value remains unchanged. + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param vertices Pointer to the local internal vertices to be unrenumbered. Each input element + * should be in [@p local_int_vertex_first, @p local_int_vertex_last). The input internal vertices + * are renumbered to external vertices in-place. + * @param num_vertices Number of vertices to be unrenumbered. + * @param renumber_map_labels Pointer to the external vertices corresponding to the internal + * vertices in the range [@p local_int_vertex_first, @p local_int_vertex_last). + * @param local_int_vertex_first The first local internal vertex (inclusive, assigned to this + * process in multi-GPU). + * @param local_int_vertex_last The last local internal vertex (exclusive, assigned to this process + * in multi-GPU). + * @param do_expensive_check A flag to run expensive checks for input arguments (if set to `true`). + */ +template +void unrenumber_local_int_vertices( + raft::handle_t const& handle, + vertex_t* vertices /* [INOUT] */, + size_t num_vertices, + vertex_t const* renumber_map_labels /* size = local_int_vertex_last - local_int_vertex_first */, + vertex_t local_int_vertex_first, + vertex_t local_int_vertex_last, + bool do_expensive_check = false); + +// FIXME: We may add unrenumber_int_rows(or cols) as this will require communication only within a +// sub-communicator and potentially be more efficient. +/** + * @brief Unrenumber (possibly non-local) internal vertices to external vertices based on the + * providied @p renumber_map_labels. + * + * Note cugraph::experimental::invalid_id::value remains unchanged. + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @tparam multi_gpu Flag indicating whether template instantiation should target single-GPU (false) + * or multi-GPU (true). + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param vertices Pointer to the internal vertices to be unrenumbered. The input internal vertices + * are renumbered to external vertices in-place. + * @param num_vertices Number of vertices to be unrenumbered. + * @param renumber_map_labels Pointer to the external vertices corresponding to the internal + * vertices in the range [@p local_int_vertex_first, @p local_int_vertex_last). + * @param local_int_vertex_first The first local internal vertex (inclusive, assigned to this + * process in multi-GPU). + * @param local_int_vertex_last The last local internal vertex (exclusive, assigned to this process + * in multi-GPU). + * @param vertex_partition_lasts Last local internal vertices (exclusive, assigned to each process + * in multi-GPU). + * @param do_expensive_check A flag to run expensive checks for input arguments (if set to `true`). + */ +template +void unrenumber_int_vertices(raft::handle_t const& handle, + vertex_t* vertices /* [INOUT] */, + size_t num_vertices, + vertex_t const* renumber_map_labels, + vertex_t local_int_vertex_first, + vertex_t local_int_vertex_last, + std::vector const& vertex_partition_lasts, + bool do_expensive_check = false); + +/** + * @brief Compute the coarsened graph. + * + * Aggregates the vertices with the same label to a new vertex in the output coarsened graph. + * Multi-edges in the coarsened graph are collapsed to a single edge with its weight equal to the + * sum of multi-edge weights. + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @tparam edge_t Type of edge identifiers. Needs to be an integral type. + * @tparam weight_t Type of edge weights. Needs to be a floating point type. + * @tparam store_transposed Flag indicating whether to store the graph adjacency matrix as is or as + * transposed. + * @tparam multi_gpu Flag indicating whether template instantiation should target single-GPU (false) + * or multi-GPU (true). + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param graph_view Graph view object of the input graph to be coarsened. + * @param labels Vertex labels (assigned to this process in multi-GPU) to be used in coarsening. + * @param do_expensive_check A flag to run expensive checks for input arguments (if set to `true`). + * @return std::tuple>, rmm::device_uvector> Tuple of the coarsened graph and labels mapped to the + * vertices (assigned to this process in multi-GPU) in the coarsened graph. + */ +template +std::tuple>, + rmm::device_uvector> +coarsen_graph( + raft::handle_t const& handle, + graph_view_t const& graph_view, + vertex_t const* labels, + bool do_expensive_check = false); + +/** + * @brief Relabel old labels to new labels. + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @tparam multi_gpu Flag indicating whether template instantiation should target single-GPU (false) + * or multi-GPU (true). + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param old_new_label_pairs Pairs of an old label and the corresponding new label (each process + * holds only part of the entire old labels and the corresponding new labels; partitioning can be + * arbitrary). + * @param num_label_pairs Number of (old, new) label pairs. + * @param labels Labels to be relabeled. This initially holds old labels. Old labels are updated to + * new labels in-place ([INOUT] parameter). + * @param num_labels Number of labels to be relabeled. + * @param skip_missing_labels Flag dictating the behavior on missing labels (@p labels contains old + * labels missing in @p old_new_label_pairs). If set to true, missing elements are skipped (not + * relabeled). If set to false, undefined behavior (if @p do_expensive_check is set to true, this + * function will throw an exception). + * @param do_expensive_check A flag to run expensive checks for input arguments (if set to `true`). + * @return rmm::device_uvector New labels corresponding to the @p old_labels. + */ +template +void relabel(raft::handle_t const& handle, + std::tuple old_new_label_pairs, + vertex_t num_label_pairs, + vertex_t* labels /* [INOUT] */, + vertex_t num_labels, + bool skip_missing_labels, + bool do_expensive_check = false); + +/** + * @brief extract induced subgraph(s). + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @tparam edge_t Type of edge identifiers. Needs to be an integral type. + * @tparam weight_t Type of edge weights. + * @tparam store_transposed Flag indicating whether to store the graph adjacency matrix as is or as + * transposed. + * @tparam multi_gpu Flag indicating whether template instantiation should target single-GPU (false) + * or multi-GPU (true). + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param graph_view Graph view object, we extract induced subgraphs from @p graph_view. + * @param subgraph_offsets Pointer to subgraph vertex offsets (size == @p num_subgraphs + 1). + * @param subgraph_vertices Pointer to subgraph vertices (size == @p subgraph_offsets[@p + * num_subgraphs]). The elements of @p subgraph_vertices for each subgraph should be sorted in + * ascending order and unique. + * @param num_subgraphs Number of induced subgraphs to extract. + * @param do_expensive_check A flag to run expensive checks for input arguments (if set to `true`). + * @return std::tuple, rmm::device_uvector, + * rmm::device_uvector, rmm::device_uvector> Quadraplet of edge major (destination + * if @p store_transposed is true, source otherwise) vertices, edge minor (source if @p + * store_transposed is true, destination otherwise) vertices, edge weights, and edge offsets for + * each induced subgraphs (size == num_subgraphs + 1). The sizes of the edge major & minor vertices + * are edge_offsets[num_subgraphs]. The size of the edge weights is either + * edge_offsets[num_subgraphs] (if @p graph_view is weighted) or 0 (if @p graph_view is unweighted). + */ +template +std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_induced_subgraphs( + raft::handle_t const& handle, + graph_view_t const& graph_view, + size_t const* subgraph_offsets /* size == num_subgraphs + 1 */, + vertex_t const* subgraph_vertices /* size == subgraph_offsets[num_subgraphs] */, + size_t num_subgraphs, + bool do_expensive_check = false); + +/** + * @brief create a graph from (the optional vertex list and) the given edge list. + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @tparam edge_t Type of edge identifiers. Needs to be an integral type. + * @tparam weight_t Type of edge weights. Needs to be a floating point type. + * @tparam store_transposed Flag indicating whether to store the graph adjacency matrix as is or as + * transposed. + * @tparam multi_gpu Flag indicating whether template instantiation should target single-GPU (false) + * or multi-GPU (true). + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param optional_vertex_span If valid, part of the entire set of vertices in the graph to be + * renumbered. The first tuple element is the pointer to the array and the second tuple element is + * the size of the array. This parameter can be used to include isolated vertices. If multi-GPU, + * applying the compute_gpu_id_from_vertex_t to every vertex should return the local GPU ID for this + * function to work (vertices should be pre-shuffled). + * @param edgelist_rows Vector of edge row (source) vertex IDs. + * @param edgelist_cols Vector of edge column (destination) vertex IDs. + * @param edgelist_weights Vector of edge weights. + * @param graph_properties Properties of the graph represented by the input (optional vertex list + * and) edge list. + * @param renumber Flag indicating whether to renumber vertices or not. + * @return std::tuple, rmm::device_uvector> Pair of the generated graph and the renumber map (if + * @p renumber is true) or std::nullopt (if @p renumber is false). + */ +template +std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/include/experimental/graph_view.hpp b/cpp/include/cugraph/experimental/graph_view.hpp similarity index 58% rename from cpp/include/experimental/graph_view.hpp rename to cpp/include/cugraph/experimental/graph_view.hpp index ba327047b1d..272f02259e3 100644 --- a/cpp/include/experimental/graph_view.hpp +++ b/cpp/include/cugraph/experimental/graph_view.hpp @@ -1,5 +1,5 @@ /* - * 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. @@ -15,7 +15,13 @@ */ #pragma once -#include +#include +#include +#include + +// visitor logic: +// +#include #include #include @@ -29,6 +35,9 @@ #include namespace cugraph { +namespace serializer { +class serializer_t; // forward... +} namespace experimental { /** @@ -40,32 +49,11 @@ namespace experimental { * * We need to partition 1D vertex arrays (storing per vertex values) and the 2D graph adjacency * matrix (or transposed 2D graph adjacency matrix) of G. An 1D vertex array of size V is divided to - * P linear partitions; each partition has the size close to V / P. We consider two different - * strategies to partition the 2D matrix: the default strategy and the hypergraph partitioning based - * strategy (the latter is for future extension). - * FIXME: in the future we may use the latter for both as this leads to simpler communication - * patterns and better control over parallelism vs memory footprint trade-off. - * - * In the default case, one GPU will be responsible for 1 rectangular partition. The matrix will be - * horizontally partitioned first to P_row slabs. Each slab will be further vertically partitioned - * to P_col rectangles. Each rectangular partition will have the size close to V / P_row by V / - * P_col. - * - * To be more specific, a GPU with (col_comm_rank, row_comm_rank) will be responsible for one - * rectangular partition [a,b) by [c,d) where a = vertex_partition_offsets[row_comm_size * - * col_comm_rank], b = vertex_partition_offsets[row_comm_size * (col_comm_rank + 1)], c = - * vertex_partition_offsets[col_comm_size * row_comm_rank], and d = - * vertex_partition_offsets[col_comm_size * (row_comm_rank + 1)]. + * P linear partitions; each partition has the size close to V / P. * - * In the future, we may apply hyper-graph partitioning to divide V vertices to P groups minimizing - * edge cuts across groups while balancing the number of vertices in each group. We will also - * renumber vertices so the vertices in each group are mapped to consecutive integers. Then, there - * will be more non-zeros in the diagonal partitions of the 2D graph adjacency matrix (or the - * transposed 2D graph adjacency matrix) than the off-diagonal partitions. The default strategy does - * not balance the number of nonzeros if hyper-graph partitioning is applied. To solve this problem, - * the matrix is first horizontally partitioned to P slabs, then each slab will be further - * vertically partitioned to P_row (instead of P_col in the default case) rectangles. One GPU will - * be responsible col_comm_size rectangular partitions in this case. + * The 2D graph adjacency matrix is first horizontally partitioned to P slabs, then each slab will + * be further vertically partitioned to P_row (instead of P_col in the default case) rectangles. One + * GPU will be responsible col_comm_size rectangular partitions. * * To be more specific, a GPU with (col_comm_rank, row_comm_rank) will be responsible for * col_comm_size rectangular partitions [a_i,b_i) by [c,d) where a_i = @@ -82,14 +70,14 @@ namespace experimental { template class partition_t { public: + partition_t() = default; + partition_t(std::vector const& vertex_partition_offsets, - bool hypergraph_partitioned, int row_comm_size, int col_comm_size, int row_comm_rank, int col_comm_rank) : vertex_partition_offsets_(vertex_partition_offsets), - hypergraph_partitioned_(hypergraph_partitioned), comm_rank_(col_comm_rank * row_comm_size + row_comm_rank), row_comm_size_(row_comm_size), col_comm_size_(col_comm_size), @@ -157,10 +145,7 @@ class partition_t { get_vertex_partition_first(vertex_partition_idx); } - size_t get_number_of_matrix_partitions() const - { - return hypergraph_partitioned_ ? col_comm_size_ : 1; - } + size_t get_number_of_matrix_partitions() const { return col_comm_size_; } // major: row of the graph adjacency matrix (if the graph adjacency matrix is stored as is) or // column of the graph adjacency matrix (if the transposed graph adjacency matrix is stored). @@ -173,16 +158,18 @@ class partition_t { vertex_t get_matrix_partition_major_first(size_t partition_idx) const { - return hypergraph_partitioned_ - ? vertex_partition_offsets_[row_comm_size_ * partition_idx + row_comm_rank_] - : vertex_partition_offsets_[col_comm_rank_ * row_comm_size_]; + return vertex_partition_offsets_[row_comm_size_ * partition_idx + row_comm_rank_]; } vertex_t get_matrix_partition_major_last(size_t partition_idx) const { - return hypergraph_partitioned_ - ? vertex_partition_offsets_[row_comm_size_ * partition_idx + row_comm_rank_ + 1] - : vertex_partition_offsets_[(col_comm_rank_ + 1) * row_comm_size_]; + return vertex_partition_offsets_[row_comm_size_ * partition_idx + row_comm_rank_ + 1]; + } + + vertex_t get_matrix_partition_major_size(size_t partition_idx) const + { + return get_matrix_partition_major_last(partition_idx) - + get_matrix_partition_major_first(partition_idx); } vertex_t get_matrix_partition_major_value_start_offset(size_t partition_idx) const @@ -202,24 +189,21 @@ class partition_t { vertex_t get_matrix_partition_minor_first() const { - return hypergraph_partitioned_ ? vertex_partition_offsets_[col_comm_rank_ * row_comm_size_] - : vertex_partition_offsets_[row_comm_rank_ * col_comm_size_]; + return vertex_partition_offsets_[col_comm_rank_ * row_comm_size_]; } vertex_t get_matrix_partition_minor_last() const { - return hypergraph_partitioned_ - ? vertex_partition_offsets_[(col_comm_rank_ + 1) * row_comm_size_] - : vertex_partition_offsets_[(row_comm_rank_ + 1) * col_comm_size_]; + return vertex_partition_offsets_[(col_comm_rank_ + 1) * row_comm_size_]; } - // FIXME: this function may be removed if we use the same partitioning strategy whether hypergraph - // partitioning is applied or not - bool is_hypergraph_partitioned() const { return hypergraph_partitioned_; } + vertex_t get_matrix_partition_minor_size() const + { + return get_matrix_partition_minor_last() - get_matrix_partition_minor_first(); + } private: std::vector vertex_partition_offsets_{}; // size = P + 1 - bool hypergraph_partitioned_{false}; int comm_rank_{0}; int row_comm_size_{0}; @@ -238,15 +222,22 @@ struct graph_properties_t { namespace detail { +using namespace cugraph::visitors; + // FIXME: threshold values require tuning +// use the hypersparse format (currently, DCSR or DCSC) for the vertices with their degrees smaller +// than col_comm_size * hypersparse_threshold_ratio, should be less than 1.0 +double constexpr hypersparse_threshold_ratio = 0.5; size_t constexpr low_degree_threshold{raft::warp_size()}; size_t constexpr mid_degree_threshold{1024}; -size_t constexpr num_segments_per_vertex_partition{3}; +size_t constexpr num_sparse_segments_per_vertex_partition{3}; // Common for both graph_view_t & graph_t and both single-GPU & multi-GPU versions template -class graph_base_t { +class graph_base_t : public graph_envelope_t::base_graph_t /*<- visitor logic*/ { public: + graph_base_t() = default; // Note: required by visitor logic + graph_base_t(raft::handle_t const& handle, vertex_t number_of_vertices, edge_t number_of_edges, @@ -274,7 +265,14 @@ class graph_base_t { bool is_symmetric() const { return properties_.is_symmetric; } bool is_multigraph() const { return properties_.is_multigraph; } + void apply(visitor_t& v) const override // <- visitor logic + { + v.visit_graph(*this); + } + protected: + friend class cugraph::serializer::serializer_t; + raft::handle_t const* get_handle_ptr() const { return handle_ptr_; }; graph_properties_t get_graph_properties() const { return properties_; } @@ -318,21 +316,21 @@ class graph_view_t const& adj_matrix_partition_offsets, - std::vector const& adj_matrix_partition_indices, - std::vector const& adj_matrix_partition_weights, - std::vector const& vertex_partition_segment_offsets, - partition_t const& partition, - vertex_t number_of_vertices, - edge_t number_of_edges, - graph_properties_t properties, - bool sorted_by_global_degree_within_vertex_partition, - bool do_expensive_check = false); - - bool is_weighted() const { return adj_matrix_partition_weights_.size() > 0; } - - partition_t get_partition() const { return partition_; } + graph_view_t( + raft::handle_t const& handle, + std::vector const& adj_matrix_partition_offsets, + std::vector const& adj_matrix_partition_indices, + std::optional> const& adj_matrix_partition_weights, + std::optional> const& adj_matrix_partition_dcs_nzd_vertices, + std::optional> const& adj_matrix_partition_dcs_nzd_vertex_counts, + partition_t const& partition, + vertex_t number_of_vertices, + edge_t number_of_edges, + graph_properties_t properties, + std::optional> const& adj_matrix_partition_segment_offsets, + bool do_expensive_check = false); + + bool is_weighted() const { return adj_matrix_partition_weights_.has_value(); } vertex_t get_number_of_local_vertices() const { @@ -399,6 +397,29 @@ class graph_view_t + std::enable_if_t get_local_adj_matrix_partition_row_first() const + { + return partition_.get_matrix_partition_minor_first(); + } + + template + std::enable_if_t get_local_adj_matrix_partition_row_last() const + { + return partition_.get_matrix_partition_minor_last(); + } + + template + std::enable_if_t get_number_of_local_adj_matrix_partition_rows() const + { + return get_local_adj_matrix_partition_row_last() - get_local_adj_matrix_partition_row_first(); + } + vertex_t get_local_adj_matrix_partition_row_first(size_t adj_matrix_partition_idx) const { return store_transposed ? partition_.get_matrix_partition_minor_first() @@ -411,6 +432,12 @@ class graph_view_t + std::enable_if_t get_local_adj_matrix_partition_col_first() const + { + return partition_.get_matrix_partition_minor_first(); + } + + template + std::enable_if_t get_local_adj_matrix_partition_col_last() const + { + return partition_.get_matrix_partition_minor_last(); + } + + template + std::enable_if_t get_number_of_local_adj_matrix_partition_cols() const + { + return get_local_adj_matrix_partition_col_last() - get_local_adj_matrix_partition_col_first(); + } + vertex_t get_local_adj_matrix_partition_col_first(size_t adj_matrix_partition_idx) const { return store_transposed ? partition_.get_matrix_partition_major_first(adj_matrix_partition_idx) @@ -431,6 +476,12 @@ class graph_view_t> get_local_adj_matrix_partition_segment_offsets( + size_t partition_idx) const { - return adj_matrix_partition_offsets_[adj_matrix_partition_idx]; + if (adj_matrix_partition_segment_offsets_) { + auto size_per_partition = + (*adj_matrix_partition_segment_offsets_).size() / partition_.get_col_size(); + return std::vector( + (*adj_matrix_partition_segment_offsets_).begin() + partition_idx * size_per_partition, + (*adj_matrix_partition_segment_offsets_).begin() + + (partition_idx + 1) * size_per_partition); + } else { + return std::nullopt; + } } - // FIXME: this function is not part of the public stable API.This function is mainly for pattern - // accelerator implementation. This function is currently public to support the legacy - // implementations directly accessing CSR/CSC data, but this function will eventually become - // private or even disappear if we switch to CSR + DCSR (or CSC + DCSC). - vertex_t const* indices(size_t adj_matrix_partition_idx) const + vertex_partition_view_t get_vertex_partition_view() const { - return adj_matrix_partition_indices_[adj_matrix_partition_idx]; + return vertex_partition_view_t(this->get_number_of_vertices(), + this->get_local_vertex_first(), + this->get_local_vertex_last()); } - // FIXME: this function is not part of the public stable API.This function is mainly for pattern - // accelerator implementation. This function is currently public to support the legacy - // implementations directly accessing CSR/CSC data, but this function will eventually become - // private or even disappear if we switch to CSR + DCSR (or CSC + DCSC). - weight_t const* weights(size_t adj_matrix_partition_idx) const + matrix_partition_view_t get_matrix_partition_view( + size_t adj_matrix_partition_idx) const { - return adj_matrix_partition_weights_.size() > 0 - ? adj_matrix_partition_weights_[adj_matrix_partition_idx] - : static_cast(nullptr); - } + return matrix_partition_view_t( + adj_matrix_partition_offsets_[adj_matrix_partition_idx], + adj_matrix_partition_indices_[adj_matrix_partition_idx], + adj_matrix_partition_weights_ + ? std::optional{(*adj_matrix_partition_weights_)[adj_matrix_partition_idx]} + : std::nullopt, + adj_matrix_partition_dcs_nzd_vertices_ + ? std::optional{( + *adj_matrix_partition_dcs_nzd_vertices_)[adj_matrix_partition_idx]} + : std::nullopt, + adj_matrix_partition_dcs_nzd_vertex_counts_ + ? std::optional{( + *adj_matrix_partition_dcs_nzd_vertex_counts_)[adj_matrix_partition_idx]} + : std::nullopt, + this->get_number_of_local_adj_matrix_partition_edges(adj_matrix_partition_idx), + store_transposed ? this->get_local_adj_matrix_partition_col_first(adj_matrix_partition_idx) + : this->get_local_adj_matrix_partition_row_first(adj_matrix_partition_idx), + store_transposed ? this->get_local_adj_matrix_partition_col_last(adj_matrix_partition_idx) + : this->get_local_adj_matrix_partition_row_last(adj_matrix_partition_idx), + store_transposed ? this->get_local_adj_matrix_partition_row_first(adj_matrix_partition_idx) + : this->get_local_adj_matrix_partition_col_first(adj_matrix_partition_idx), + store_transposed ? this->get_local_adj_matrix_partition_row_last(adj_matrix_partition_idx) + : this->get_local_adj_matrix_partition_col_last(adj_matrix_partition_idx), + store_transposed + ? this->get_local_adj_matrix_partition_col_value_start_offset(adj_matrix_partition_idx) + : this->get_local_adj_matrix_partition_row_value_start_offset(adj_matrix_partition_idx)); + } + + rmm::device_uvector compute_in_degrees(raft::handle_t const& handle) const; + rmm::device_uvector compute_out_degrees(raft::handle_t const& handle) const; + + rmm::device_uvector compute_in_weight_sums(raft::handle_t const& handle) const; + rmm::device_uvector compute_out_weight_sums(raft::handle_t const& handle) const; + + edge_t compute_max_in_degree(raft::handle_t const& handle) const; + edge_t compute_max_out_degree(raft::handle_t const& handle) const; + + weight_t compute_max_in_weight_sum(raft::handle_t const& handle) const; + weight_t compute_max_out_weight_sum(raft::handle_t const& handle) const; private: std::vector adj_matrix_partition_offsets_{}; std::vector adj_matrix_partition_indices_{}; - std::vector adj_matrix_partition_weights_{}; + std::optional> adj_matrix_partition_weights_{}; + + // relevant only if we use the CSR + DCSR (or CSC + DCSC) hybrid format + std::optional> adj_matrix_partition_dcs_nzd_vertices_{}; + std::optional> adj_matrix_partition_dcs_nzd_vertex_counts_{}; + + std::vector adj_matrix_partition_number_of_edges_{}; partition_t partition_{}; - std::vector - vertex_partition_segment_offsets_{}; // segment offsets within the vertex partition based on - // vertex degree, relevant only if - // sorted_by_global_degree_within_vertex_partition is true + // segment offsets based on vertex degree, relevant only if vertex IDs are renumbered + std::optional> adj_matrix_partition_segment_offsets_{}; }; // single-GPU version @@ -524,15 +595,14 @@ class graph_view_t const& segment_offsets, + std::optional weights, vertex_t number_of_vertices, edge_t number_of_edges, graph_properties_t properties, - bool sorted_by_degree, + std::optional> const& segment_offsets, bool do_expensive_check = false); - bool is_weighted() const { return weights_ != nullptr; } + bool is_weighted() const { return weights_.has_value(); } vertex_t get_number_of_local_vertices() const { return this->get_number_of_vertices(); } @@ -567,6 +637,30 @@ class graph_view_tget_number_of_vertices(); } + edge_t get_number_of_local_adj_matrix_partition_edges(size_t adj_matrix_partition_idx) const + { + assert(adj_matrix_partition_idx == 0); + return this->get_number_of_edges(); + } + + template + std::enable_if_t get_local_adj_matrix_partition_row_first() const + { + return get_local_adj_matrix_partition_row_first(0); + } + + template + std::enable_if_t get_local_adj_matrix_partition_row_last() const + { + return get_local_adj_matrix_partition_row_last(0); + } + + template + std::enable_if_t get_number_of_local_adj_matrix_partition_rows() const + { + return get_number_of_local_adj_matrix_partition_rows(0); + } + vertex_t get_local_adj_matrix_partition_row_first(size_t adj_matrix_partition_idx) const { assert(adj_matrix_partition_idx == 0); @@ -586,6 +680,24 @@ class graph_view_t + std::enable_if_t get_local_adj_matrix_partition_col_first() const + { + return get_local_adj_matrix_partition_col_first(0); + } + + template + std::enable_if_t get_local_adj_matrix_partition_col_last() const + { + return get_local_adj_matrix_partition_col_last(0); + } + + template + std::enable_if_t get_number_of_local_adj_matrix_partition_cols() const + { + return get_number_of_local_adj_matrix_partition_cols(0); + } + vertex_t get_local_adj_matrix_partition_col_first(size_t adj_matrix_partition_idx) const { assert(adj_matrix_partition_idx == 0); @@ -605,32 +717,45 @@ class graph_view_t> get_local_adj_matrix_partition_segment_offsets( + size_t adj_matrix_partition_idx) const + { + assert(adj_matrix_partition_idx == 0); + return segment_offsets_; + } + + vertex_partition_view_t get_vertex_partition_view() const + { + return vertex_partition_view_t(this->get_number_of_vertices()); + } + + matrix_partition_view_t get_matrix_partition_view( + size_t adj_matrix_partition_idx = 0) const + { + assert(adj_matrix_partition_idx == 0); // there is only one matrix partition in single-GPU + return matrix_partition_view_t( + offsets_, indices_, weights_, this->get_number_of_vertices(), this->get_number_of_edges()); + } + + rmm::device_uvector compute_in_degrees(raft::handle_t const& handle) const; + rmm::device_uvector compute_out_degrees(raft::handle_t const& handle) const; - // FIXME: this function is not part of the public stable API.This function is mainly for pattern - // accelerator implementation. This function is currently public to support the legacy - // implementations directly accessing CSR/CSC data, but this function will eventually become - // private. - edge_t const* offsets() const { return offsets_; } + rmm::device_uvector compute_in_weight_sums(raft::handle_t const& handle) const; + rmm::device_uvector compute_out_weight_sums(raft::handle_t const& handle) const; - // FIXME: this function is not part of the public stable API.This function is mainly for pattern - // accelerator implementation. This function is currently public to support the legacy - // implementations directly accessing CSR/CSC data, but this function will eventually become - // private. - vertex_t const* indices() const { return indices_; } + edge_t compute_max_in_degree(raft::handle_t const& handle) const; + edge_t compute_max_out_degree(raft::handle_t const& handle) const; - // FIXME: this function is not part of the public stable API.This function is mainly for pattern - // accelerator implementation. This function is currently public to support the legacy - // implementations directly accessing CSR/CSC data, but this function will eventually become - // private. - weight_t const* weights() const { return weights_; } + weight_t compute_max_in_weight_sum(raft::handle_t const& handle) const; + weight_t compute_max_out_weight_sum(raft::handle_t const& handle) const; private: edge_t const* offsets_{nullptr}; vertex_t const* indices_{nullptr}; - weight_t const* weights_{nullptr}; - std::vector segment_offsets_{}; // segment offsets based on vertex degree, relevant - // only if sorted_by_global_degree is true + std::optional weights_{std::nullopt}; + + // segment offsets based on vertex degree, relevant only if vertex IDs are renumbered + std::optional> segment_offsets_{std::nullopt}; }; } // namespace experimental diff --git a/cpp/include/functions.hpp b/cpp/include/cugraph/functions.hpp similarity index 51% rename from cpp/include/functions.hpp rename to cpp/include/cugraph/functions.hpp index ede1be3767f..23edd204c3b 100644 --- a/cpp/include/functions.hpp +++ b/cpp/include/cugraph/functions.hpp @@ -1,5 +1,5 @@ /* - * 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. @@ -18,7 +18,7 @@ #include #include -#include +#include namespace cugraph { @@ -40,43 +40,9 @@ namespace cugraph { * */ template -std::unique_ptr> coo_to_csr( - GraphCOOView const &graph, - rmm::mr::device_memory_resource *mr = rmm::mr::get_current_device_resource()); - -/** - * @brief Renumber source and destination indices - * - * Renumber source and destination indexes to be a dense numbering, - * using contiguous values between 0 and number of vertices minus 1. - * - * @throws cugraph::logic_error when an error occurs. - * - * @tparam VT_IN type of vertex index input - * @tparam VT_OUT type of vertex index output - * @tparam ET type of edge index - * - * @param[in] number_of_edges number of edges in the graph - * @param[in] src Pointer to device memory containing source vertex ids - * @param[in] dst Pointer to device memory containing destination vertex ids - * @param[out] src_renumbered Pointer to device memory containing the output source vertices. - * @param[out] dst_renumbered Pointer to device memory containing the output destination vertices. - * @param[out] map_size Pointer to local memory containing the number of elements in the - * renumbering map - * @param[in] mr Memory resource used to allocate the returned graph - * - * @return Unique pointer to renumbering map - * - */ -template -std::unique_ptr renumber_vertices( - ET number_of_edges, - VT_IN const *src, - VT_IN const *dst, - VT_OUT *src_renumbered, - VT_OUT *dst_renumbered, - ET *map_size, - rmm::mr::device_memory_resource *mr = rmm::mr::get_current_device_resource()); +std::unique_ptr> coo_to_csr( + legacy::GraphCOOView const& graph, + rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource()); /** * @brief Broadcast using handle communicator @@ -94,7 +60,7 @@ std::unique_ptr renumber_vertices( // FIXME: It would be better to expose it in RAFT template -void comms_bcast(const raft::handle_t &handle, value_t *value, size_t count) +void comms_bcast(const raft::handle_t& handle, value_t* value, size_t count) { handle.get_comms().bcast(value, count, 0, handle.get_stream()); } diff --git a/cpp/include/cugraph/graph_generators.hpp b/cpp/include/cugraph/graph_generators.hpp new file mode 100644 index 00000000000..94ae5d2cf81 --- /dev/null +++ b/cpp/include/cugraph/graph_generators.hpp @@ -0,0 +1,364 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include + +#include +#include +#include + +namespace cugraph { + +/** + * @brief generate an edge list for an R-mat graph. + * + * This function allows multi-edges and self-loops similar to the Graph 500 reference + * implementation. + * + * NOTE: The scramble_vertex_ids function needs to be called in order to generate a + * graph conforming to the Graph 500 specification (note that scrambling does not + * affect cuGraph's graph construction performance, so this is generally unnecessary). + * If `edge_factor` is given (e.g. Graph 500), set @p num_edges to + * (size_t{1} << @p scale) * `edge_factor`. To generate an undirected graph, set @p b == @p c and @p + * clip_and_flip = true. All the resulting edges will be placed in the lower triangular part + * (including the diagonal) of the graph adjacency matrix. + * + * For multi-GPU generation with `P` GPUs, @p seed should be set to different values in different + * GPUs to avoid every GPU generating the same set of edges. @p num_edges should be adjusted as + * well; e.g. assuming `edge_factor` is given, set @p num_edges = (size_t{1} << @p scale) * + * `edge_factor` / `P` + (rank < (((size_t{1} << @p scale) * `edge_factor`) % P) ? 1 : 0). + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param scale Scale factor to set the number of verties in the graph. Vertex IDs have values in + * [0, V), where V = 1 << @p scale. + * @param num_edges Number of edges to generate. + * @param a a, b, c, d (= 1.0 - (a + b + c)) in the R-mat graph generator (vist https://graph500.org + * for additional details). a, b, c, d should be non-negative and a + b + c should be no larger + * than 1.0. + * @param b a, b, c, d (= 1.0 - (a + b + c)) in the R-mat graph generator (vist https://graph500.org + * for additional details). a, b, c, d should be non-negative and a + b + c should be no larger + * than 1.0. + * @param c a, b, c, d (= 1.0 - (a + b + c)) in the R-mat graph generator (vist https://graph500.org + * for additional details). a, b, c, d should be non-negative and a + b + c should be no larger + * than 1.0. + * @param seed Seed value for the random number generator. + * @param clip_and_flip Flag controlling whether to generate edges only in the lower triangular part + * (including the diagonal) of the graph adjacency matrix (if set to `true`) or not (if set to + * `false`). + * @return std::tuple, rmm::device_uvector> A tuple of + * rmm::device_uvector objects for edge source vertex IDs and edge destination vertex IDs. + */ +template +std::tuple, rmm::device_uvector> generate_rmat_edgelist( + raft::handle_t const& handle, + size_t scale, + size_t num_edges, + double a = 0.57, + double b = 0.19, + double c = 0.19, + uint64_t seed = 0, + bool clip_and_flip = false); + +enum class generator_distribution_t { POWER_LAW = 0, UNIFORM }; + +/** + * @brief generate multiple edge lists using the R-mat graph generator. + * + * This function allows multi-edges and self-loops similar to the Graph 500 reference + * implementation. + * + * NOTE: The scramble_vertex_ids function needs to be called in order to generate a + * graph conforming to the Graph 500 specification (note that scrambling does not + * affect cuGraph's graph construction performance, so this is generally unnecessary). + * If `edge_factor` is given (e.g. Graph 500), set @p num_edges to + * (size_t{1} << @p scale) * `edge_factor`. To generate an undirected graph, set @p b == @p c and @p + * clip_and_flip = true. All the resulting edges will be placed in the lower triangular part + * (including the diagonal) of the graph adjacency matrix. + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param n_edgelists Number of edge lists (graphs) to generate + * @param min_scale Scale factor to set the minimum number of verties in the graph. + * @param max_scale Scale factor to set the maximum number of verties in the graph. + * @param edge_factor Average number of edges per vertex to generate. + * @param size_distribution Distribution of the graph sizes, impacts the scale parameter of the + * R-MAT generator + * @param edge_distribution Edges distribution for each graph, impacts how R-MAT parameters a,b,c,d, + * are set. + * @param seed Seed value for the random number generator. + * @param clip_and_flip Flag controlling whether to generate edges only in the lower triangular part + * (including the diagonal) of the graph adjacency matrix (if set to `true`) or not (if set to + * `false`). + * @return A vector of std::tuple, rmm::device_uvector> of + *size @p n_edgelists, each vector element being a tuple of rmm::device_uvector objects for edge + *source vertex IDs and edge destination vertex IDs. + */ +template +std::vector, rmm::device_uvector>> +generate_rmat_edgelists( + raft::handle_t const& handle, + size_t n_edgelists, + size_t min_scale, + size_t max_scale, + size_t edge_factor = 16, + generator_distribution_t size_distribution = generator_distribution_t::POWER_LAW, + generator_distribution_t edge_distribution = generator_distribution_t::POWER_LAW, + uint64_t seed = 0, + bool clip_and_flip = false); + +/** + * @brief generate an edge list for path graph + * + * A path graph of size n connects the vertices from 0 to (n - 1) + * in a single long path: ((0,1), (1,2), ..., (n - 2, n - 1) + * + * If executed in a multi-gpu context (handle comms has been initialized) + * the path will span all GPUs including an edge from the last vertex on + * GPU i to the first vertex on GPU (i+1) + * + * This function will generate a collection of path graphs. @p component_parameters_v + * defines the parameters for generating each component. Each element of + * @p component_parameters_v defines a tuple consisting of the number of vertices + * and the base vertex id for the component. + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param component_parameters_v A vector containing tuples consisting of the number of vertices and + * base vertex id for each component to generate. + * @return std::tuple, rmm::device_uvector> A tuple of + * rmm::device_uvector objects for edge source vertex IDs and edge destination vertex IDs. + */ +template +std::tuple, rmm::device_uvector> +generate_path_graph_edgelist( + raft::handle_t const& handle, + std::vector> const& component_parameters_v); + +/** + * @brief generate an edge list for a 2D Mesh Graph + * + * A sequence of 2D mesh graphs will be constructed according to the + * component specifications. Each 2D mesh graph is configured with a tuple + * containing (x, y, base_vertex_id). @p component_parameters_v will contain + * a tuple for each component. + * + * If executed in a multi-gpu context (handle comms has been initialized) + * each GPU will generate disjoint 2D mesh constructs of equal size. + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param component_parameters_v Vector containing tuple defining the configuration of each + * component + * @return std::tuple, rmm::device_uvector> A tuple of + * rmm::device_uvector objects for edge source vertex IDs and edge destination vertex IDs. + */ +template +std::tuple, rmm::device_uvector> +generate_2d_mesh_graph_edgelist( + raft::handle_t const& handle, + std::vector> const& component_parameters_v); + +/** + * @brief generate an edge list for a 3D Mesh Graph + * + * A sequence of 3D mesh graphs will be constructed according to the + * component specifications. Each 3D mesh graph is configured with a tuple + * containing (x, y, z, base_vertex_id). @p component_parameters_v will contain + * a tuple for each component. + * + * If executed in a multi-gpu context (handle comms has been initialized) + * each GPU will generate disjoint 3D mesh constructs of equal size. + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param component_parameters_v Vector containing tuple defining the configuration of each + * component + * @return std::tuple, rmm::device_uvector> A tuple of + * rmm::device_uvector objects for edge source vertex IDs and edge destination vertex IDs. + */ +template +std::tuple, rmm::device_uvector> +generate_3d_mesh_graph_edgelist( + raft::handle_t const& handle, + std::vector> const& component_parameters_v); + +/** + * @brief generate an edge lists for some complete graphs + * + * A sequence of complete graphs will be constructed according to the + * component specifications. Each complete graph is configured with a tuple + * containing (n, base_vertex_id). @p component_parameters_v will contain + * a tuple for each component. + * + * If executed in a multi-gpu context (handle comms has been initialized) + * each GPU will generate disjoint complete graph constructs of equal size. + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param component_parameters_v Vector containing tuple defining the configuration of each + * component + * @return std::tuple, rmm::device_uvector> A tuple of + * rmm::device_uvector objects for edge source vertex IDs and edge destination vertex IDs. + */ +template +std::tuple, rmm::device_uvector> +generate_complete_graph_edgelist( + raft::handle_t const& handle, + std::vector> const& component_parameters_v); + +/** + * @brief generate an edge lists for an Erdos-Renyi graph + * + * This API supports the G(n,p) model which requires O(n^2) work. + * + * If executed in a multi-gpu context (handle comms has been initialized) + * each GPU will generate Erdos-Renyi edges for its portion of the 2D + * partitioning of the adjacency matrix. + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param num_vertices Number of vertices to use in the generated graph + * @param p Probability for edge creation + * @param base_vertex_id Starting vertex id for the generated graph + * @param seed Seed value for the random number generator. + * @return std::tuple, rmm::device_uvector> A tuple of + * rmm::device_uvector objects for edge source vertex IDs and edge destination vertex IDs. + */ +template +std::tuple, rmm::device_uvector> +generate_erdos_renyi_graph_edgelist_gnp(raft::handle_t const& handle, + vertex_t num_vertices, + float p, + vertex_t base_vertex_id, + uint64_t seed = 0); + +/** + * @brief generate an edge lists for an Erdos-Renyi graph + * + * This API supports the G(n,m) model + * + * If executed in a multi-gpu context (handle comms has been initialized) + * each GPU will generate Erdos-Renyi edges for its portion of the 2D + * partitioning of the adjacency matrix. + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param num_vertices Number of vertices to use in each complete graph + * @param m Number of edges to generate + * @param base_vertex_id Starting vertex id for the generated graph + * @param seed Seed value for the random number generator. + * @return std::tuple, rmm::device_uvector> A tuple of + * rmm::device_uvector objects for edge source vertex IDs and edge destination vertex IDs. + */ +template +std::tuple, rmm::device_uvector> +generate_erdos_renyi_graph_edgelist_gnm(raft::handle_t const& handle, + vertex_t num_vertices, + size_t m, + vertex_t base_vertex_id, + uint64_t seed = 0); + +/** + * @brief symmetrize an edgelist + * + * Given an edgelist for a graph, symmetrize and deduplicate edges. + * + * If a duplicate edge exists in a weighted graph, one of the weights is arbitrarily + * returned. + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @tparam weight_t Type of weights. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param d_src_v Vector of source vertices + * @param d_dst_v Vector of destination vertices + * @param d_weights_v Optional vector of edge weights + * @return std::tuple, rmm::device_uvector> A tuple of + * rmm::device_uvector objects for edge source vertex IDs and edge destination vertex IDs. + */ +template +std::tuple, + rmm::device_uvector, + std::optional>> +symmetrize_edgelist(raft::handle_t const& handle, + rmm::device_uvector&& d_src_v, + rmm::device_uvector&& d_dst_v, + std::optional>&& optional_d_weights_v); + +/** + * @brief scramble vertex ids in a graph + * + * Given an edgelist for a graph, scramble all vertex ids by the given offset. + * This translation is done in place. + * + * The scramble code here follows the algorithm in the Graph 500 reference + * implementation version 3.0.0. + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param d_src_v Vector of source vertices + * @param d_dst_v Vector of destination vertices + * @param vertex_id_offset Offset to add to each vertex id + * @param seed Used to initialize random number generator + */ +template +void scramble_vertex_ids(raft::handle_t const& handle, + rmm::device_uvector& d_src_v, + rmm::device_uvector& d_dst_v, + vertex_t vertex_id_offset, + uint64_t seed = 0); + +/** + * @brief Combine edgelists from multiple sources into a single edgelist + * + * If executed in a multi-gpu context (handle comms has been initialized) + * each GPU will operate only on its subset of data. Any shuffling to get + * edges onto the same GPU should be done prior to calling this function. + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param sources The source vertex ids to combine + * @param dests The destination vertex ids to combine + * @param weights Optional vector of weights to combine + * @param remove_multi_edges If true (the default) then remove multi edges, if false leave them in + * @return std::tuple, rmm::device_uvector, + * rmm::device_uvector> A tuple of rmm::device_uvector objects for edge source vertex IDs + * and edge destination vertex IDs and edge weights. + */ +template +std::tuple, + rmm::device_uvector, + std::optional>> +combine_edgelists(raft::handle_t const& handle, + std::vector>&& d_sources, + std::vector>&& d_dests, + std::optional>>&& optional_d_weights, + bool remove_multi_edges = true); + +} // namespace cugraph diff --git a/cpp/include/internals.hpp b/cpp/include/cugraph/internals.hpp similarity index 83% rename from cpp/include/internals.hpp rename to cpp/include/cugraph/internals.hpp index f71426491e3..1c311304ae4 100644 --- a/cpp/include/internals.hpp +++ b/cpp/include/cugraph/internals.hpp @@ -1,5 +1,5 @@ /* - * 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. @@ -35,9 +35,9 @@ class GraphBasedDimRedCallback : public Callback { this->n_components = n_components; this->isFloat = std::is_same::value; } - virtual void on_preprocess_end(void *positions) = 0; - virtual void on_epoch_end(void *positions) = 0; - virtual void on_train_end(void *positions) = 0; + virtual void on_preprocess_end(void* positions) = 0; + virtual void on_epoch_end(void* positions) = 0; + virtual void on_train_end(void* positions) = 0; protected: int n; diff --git a/cpp/include/eidecl_graph.hpp b/cpp/include/cugraph/legacy/eidecl_graph.hpp similarity index 98% rename from cpp/include/eidecl_graph.hpp rename to cpp/include/cugraph/legacy/eidecl_graph.hpp index 03f6a675597..d636b7fba5b 100644 --- a/cpp/include/eidecl_graph.hpp +++ b/cpp/include/cugraph/legacy/eidecl_graph.hpp @@ -1,5 +1,5 @@ /* - * 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. @@ -16,6 +16,7 @@ #pragma once namespace cugraph { +namespace legacy { extern template class GraphViewBase; extern template class GraphViewBase; extern template class GraphViewBase; @@ -88,4 +89,5 @@ extern template class GraphCSC; extern template class GraphCSC; extern template class GraphCSC; extern template class GraphCSC; +} // namespace legacy } // namespace cugraph diff --git a/cpp/include/eidir_graph.hpp b/cpp/include/cugraph/legacy/eidir_graph.hpp similarity index 97% rename from cpp/include/eidir_graph.hpp rename to cpp/include/cugraph/legacy/eidir_graph.hpp index d7273b9ea37..df9f6eb8f71 100644 --- a/cpp/include/eidir_graph.hpp +++ b/cpp/include/cugraph/legacy/eidir_graph.hpp @@ -1,5 +1,5 @@ /* - * 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. @@ -16,6 +16,7 @@ #pragma once namespace cugraph { +namespace legacy { template class GraphViewBase; template class GraphViewBase; template class GraphViewBase; @@ -70,4 +71,5 @@ template class GraphCSC; template class GraphCSC; template class GraphCSC; template class GraphCSC; +} // namespace legacy } // namespace cugraph diff --git a/cpp/include/graph.hpp b/cpp/include/cugraph/legacy/graph.hpp similarity index 89% rename from cpp/include/graph.hpp rename to cpp/include/cugraph/legacy/graph.hpp index b30159566b5..4345fd225c1 100644 --- a/cpp/include/graph.hpp +++ b/cpp/include/cugraph/legacy/graph.hpp @@ -1,5 +1,5 @@ /* - * 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. @@ -23,6 +23,7 @@ #include namespace cugraph { +namespace legacy { enum class PropType { PROP_UNDEF, PROP_FALSE, PROP_TRUE }; @@ -57,17 +58,21 @@ class GraphViewBase { using edge_type = edge_t; using weight_type = weight_t; - raft::handle_t *handle; - weight_t *edge_data; ///< edge weight + raft::handle_t* handle; + weight_t* edge_data; ///< edge weight GraphProperties prop; vertex_t number_of_vertices; edge_t number_of_edges; - vertex_t *local_vertices; - edge_t *local_edges; - vertex_t *local_offsets; + vertex_t* local_vertices; + edge_t* local_edges; + vertex_t* local_offsets; + + vertex_t get_number_of_vertices() const { return number_of_vertices; } + + vertex_t get_local_vertex_first() const { return vertex_t{0}; } /** * @brief Fill the identifiers array with the vertex identifiers. @@ -75,18 +80,18 @@ class GraphViewBase { * @param[out] identifiers Pointer to device memory to store the vertex * identifiers */ - void get_vertex_identifiers(vertex_t *identifiers) const; + void get_vertex_identifiers(vertex_t* identifiers) const; - void set_local_data(vertex_t *vertices, edge_t *edges, vertex_t *offsets) + void set_local_data(vertex_t* vertices, edge_t* edges, vertex_t* offsets) { local_vertices = vertices; local_edges = edges; local_offsets = offsets; } - void set_handle(raft::handle_t *handle_in) { handle = handle_in; } + void set_handle(raft::handle_t* handle_in) { handle = handle_in; } - GraphViewBase(weight_t *edge_data, vertex_t number_of_vertices, edge_t number_of_edges) + GraphViewBase(weight_t* edge_data, vertex_t number_of_vertices, edge_t number_of_edges) : handle(nullptr), edge_data(edge_data), prop(), @@ -111,8 +116,8 @@ class GraphViewBase { template class GraphCOOView : public GraphViewBase { public: - vertex_t *src_indices{nullptr}; ///< rowInd - vertex_t *dst_indices{nullptr}; ///< colInd + vertex_t* src_indices{nullptr}; ///< rowInd + vertex_t* dst_indices{nullptr}; ///< colInd /** * @brief Computes degree(in, out, in+out) of all the nodes of a Graph @@ -124,7 +129,7 @@ class GraphCOOView : public GraphViewBase { * to zeros. Will contain the computed degree of every vertex. * @param[in] direction IN_PLUS_OUT, IN or OUT */ - void degree(edge_t *degree, DegreeDirection direction) const; + void degree(edge_t* degree, DegreeDirection direction) const; /** * @brief Default constructor @@ -151,9 +156,9 @@ class GraphCOOView : public GraphViewBase { * @param number_of_vertices The number of vertices in the graph * @param number_of_edges The number of edges in the graph */ - GraphCOOView(vertex_t *src_indices, - vertex_t *dst_indices, - weight_t *edge_data, + GraphCOOView(vertex_t* src_indices, + vertex_t* dst_indices, + weight_t* edge_data, vertex_t number_of_vertices, edge_t number_of_edges) : GraphViewBase(edge_data, number_of_vertices, number_of_edges), @@ -175,8 +180,8 @@ class GraphCOOView : public GraphViewBase { template class GraphCompressedSparseBaseView : public GraphViewBase { public: - edge_t *offsets{nullptr}; ///< CSR offsets - vertex_t *indices{nullptr}; ///< CSR indices + edge_t* offsets{nullptr}; ///< CSR offsets + vertex_t* indices{nullptr}; ///< CSR indices /** * @brief Fill the identifiers in the array with the source vertex @@ -185,7 +190,7 @@ class GraphCompressedSparseBaseView : public GraphViewBase(edge_data, number_of_vertices, number_of_edges), @@ -275,9 +280,9 @@ class GraphCSRView : public GraphCompressedSparseBaseView( @@ -326,9 +331,9 @@ class GraphCSCView : public GraphCompressedSparseBaseView( @@ -394,7 +399,7 @@ class GraphCOO { edge_t number_of_edges, bool has_data = false, cudaStream_t stream = nullptr, - rmm::mr::device_memory_resource *mr = rmm::mr::get_current_device_resource()) + rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource()) : number_of_vertices_p(number_of_vertices), number_of_edges_p(number_of_edges), src_indices_p(sizeof(vertex_t) * number_of_edges, stream, mr), @@ -403,9 +408,9 @@ class GraphCOO { { } - GraphCOO(GraphCOOView const &graph, + GraphCOO(GraphCOOView const& graph, cudaStream_t stream = nullptr, - rmm::mr::device_memory_resource *mr = rmm::mr::get_current_device_resource()) + rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource()) : number_of_vertices_p(graph.number_of_vertices), number_of_edges_p(graph.number_of_edges), src_indices_p(graph.src_indices, graph.number_of_edges * sizeof(vertex_t), stream, mr), @@ -416,7 +421,7 @@ class GraphCOO { rmm::device_buffer{graph.edge_data, graph.number_of_edges * sizeof(weight_t), stream, mr}; } } - GraphCOO(GraphCOOContents &&contents) + GraphCOO(GraphCOOContents&& contents) : number_of_vertices_p(contents.number_of_vertices), number_of_edges_p(contents.number_of_edges), src_indices_p(std::move(*(contents.src_indices.release()))), @@ -427,9 +432,9 @@ class GraphCOO { vertex_t number_of_vertices(void) { return number_of_vertices_p; } edge_t number_of_edges(void) { return number_of_edges_p; } - vertex_t *src_indices(void) { return static_cast(src_indices_p.data()); } - vertex_t *dst_indices(void) { return static_cast(dst_indices_p.data()); } - weight_t *edge_data(void) { return static_cast(edge_data_p.data()); } + vertex_t* src_indices(void) { return static_cast(src_indices_p.data()); } + vertex_t* dst_indices(void) { return static_cast(dst_indices_p.data()); } + weight_t* edge_data(void) { return static_cast(edge_data_p.data()); } GraphCOOContents release() noexcept { @@ -500,7 +505,7 @@ class GraphCompressedSparseBase { edge_t number_of_edges, bool has_data, cudaStream_t stream, - rmm::mr::device_memory_resource *mr) + rmm::mr::device_memory_resource* mr) : number_of_vertices_p(number_of_vertices), number_of_edges_p(number_of_edges), offsets_p(sizeof(edge_t) * (number_of_vertices + 1), stream, mr), @@ -509,7 +514,7 @@ class GraphCompressedSparseBase { { } - GraphCompressedSparseBase(GraphSparseContents &&contents) + GraphCompressedSparseBase(GraphSparseContents&& contents) : number_of_vertices_p(contents.number_of_vertices), number_of_edges_p(contents.number_of_edges), offsets_p(std::move(*contents.offsets.release())), @@ -520,9 +525,9 @@ class GraphCompressedSparseBase { vertex_t number_of_vertices(void) { return number_of_vertices_p; } edge_t number_of_edges(void) { return number_of_edges_p; } - edge_t *offsets(void) { return static_cast(offsets_p.data()); } - vertex_t *indices(void) { return static_cast(indices_p.data()); } - weight_t *edge_data(void) { return static_cast(edge_data_p.data()); } + edge_t* offsets(void) { return static_cast(offsets_p.data()); } + vertex_t* indices(void) { return static_cast(indices_p.data()); } + weight_t* edge_data(void) { return static_cast(edge_data_p.data()); } GraphSparseContents release() noexcept { @@ -570,13 +575,13 @@ class GraphCSR : public GraphCompressedSparseBase { edge_t number_of_edges_, bool has_data_ = false, cudaStream_t stream = nullptr, - rmm::mr::device_memory_resource *mr = rmm::mr::get_current_device_resource()) + rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource()) : GraphCompressedSparseBase( number_of_vertices_, number_of_edges_, has_data_, stream, mr) { } - GraphCSR(GraphSparseContents &&contents) + GraphCSR(GraphSparseContents&& contents) : GraphCompressedSparseBase(std::move(contents)) { } @@ -621,13 +626,13 @@ class GraphCSC : public GraphCompressedSparseBase { edge_t number_of_edges_in, bool has_data_in = false, cudaStream_t stream = nullptr, - rmm::mr::device_memory_resource *mr = rmm::mr::get_current_device_resource()) + rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource()) : GraphCompressedSparseBase( number_of_vertices_in, number_of_edges_in, has_data_in, stream, mr) { } - GraphCSC(GraphSparseContents &&contents) + GraphCSC(GraphSparseContents&& contents) : GraphCompressedSparseBase( std::forward>(contents)) { @@ -668,6 +673,8 @@ struct invalid_vertex_id : invalid_idx { template struct invalid_edge_id : invalid_idx { }; + +} // namespace legacy } // namespace cugraph #include "eidecl_graph.hpp" diff --git a/cpp/include/cugraph/matrix_partition_device_view.cuh b/cpp/include/cugraph/matrix_partition_device_view.cuh new file mode 100644 index 00000000000..f96419f9600 --- /dev/null +++ b/cpp/include/cugraph/matrix_partition_device_view.cuh @@ -0,0 +1,305 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include + +namespace cugraph { +namespace experimental { + +namespace detail { + +template +class matrix_partition_device_view_base_t { + public: + matrix_partition_device_view_base_t(edge_t const* offsets, + vertex_t const* indices, + std::optional weights, + edge_t number_of_edges) + : offsets_(offsets), + indices_(indices), + weights_(weights ? thrust::optional(*weights) : thrust::nullopt), + number_of_edges_(number_of_edges) + { + } + + __host__ __device__ edge_t get_number_of_edges() const { return number_of_edges_; } + + __host__ __device__ edge_t const* get_offsets() const { return offsets_; } + __host__ __device__ vertex_t const* get_indices() const { return indices_; } + __host__ __device__ thrust::optional get_weights() const { return weights_; } + + // major_idx == major offset if CSR/CSC, major_offset != major_idx if DCSR/DCSC + __device__ thrust::tuple, edge_t> + get_local_edges(vertex_t major_idx) const noexcept + { + auto edge_offset = *(offsets_ + major_idx); + auto local_degree = *(offsets_ + (major_idx + 1)) - edge_offset; + auto indices = indices_ + edge_offset; + auto weights = + weights_ ? thrust::optional{*weights_ + edge_offset} : thrust::nullopt; + return thrust::make_tuple(indices, weights, local_degree); + } + + // major_idx == major offset if CSR/CSC, major_offset != major_idx if DCSR/DCSC + __device__ edge_t get_local_degree(vertex_t major_idx) const noexcept + { + return *(offsets_ + (major_idx + 1)) - *(offsets_ + major_idx); + } + + // major_idx == major offset if CSR/CSC, major_offset != major_idx if DCSR/DCSC + __device__ edge_t get_local_offset(vertex_t major_idx) const noexcept + { + return *(offsets_ + major_idx); + } + + private: + // should be trivially copyable to device + edge_t const* offsets_{nullptr}; + vertex_t const* indices_{nullptr}; + thrust::optional weights_{thrust::nullopt}; + edge_t number_of_edges_{0}; +}; + +} // namespace detail + +template +class matrix_partition_device_view_t; + +// multi-GPU version +template +class matrix_partition_device_view_t> + : public detail::matrix_partition_device_view_base_t { + public: + matrix_partition_device_view_t( + matrix_partition_view_t view) + : detail::matrix_partition_device_view_base_t( + view.get_offsets(), view.get_indices(), view.get_weights(), view.get_number_of_edges()), + dcs_nzd_vertices_(view.get_dcs_nzd_vertices() + ? thrust::optional{*(view.get_dcs_nzd_vertices())} + : thrust::nullopt), + dcs_nzd_vertex_count_(view.get_dcs_nzd_vertex_count() + ? thrust::optional{*(view.get_dcs_nzd_vertex_count())} + : thrust::nullopt), + major_first_(view.get_major_first()), + major_last_(view.get_major_last()), + minor_first_(view.get_minor_first()), + minor_last_(view.get_minor_last()), + major_value_start_offset_(view.get_major_value_start_offset()) + { + } + + __host__ __device__ vertex_t get_major_first() const noexcept { return major_first_; } + + __host__ __device__ vertex_t get_major_last() const noexcept { return major_last_; } + + __host__ __device__ vertex_t get_major_size() const noexcept + { + return major_last_ - major_first_; + } + + __host__ __device__ vertex_t get_minor_first() const noexcept { return minor_first_; } + + __host__ __device__ vertex_t get_minor_last() const noexcept { return minor_last_; } + + __host__ __device__ vertex_t get_minor_size() const noexcept + { + return minor_last_ - minor_first_; + } + + __host__ __device__ vertex_t get_major_offset_from_major_nocheck(vertex_t major) const noexcept + { + return major - major_first_; + } + + __host__ __device__ vertex_t get_minor_offset_from_minor_nocheck(vertex_t minor) const noexcept + { + return minor - minor_first_; + } + + __host__ __device__ vertex_t + get_major_from_major_offset_nocheck(vertex_t major_offset) const noexcept + { + return major_first_ + major_offset; + } + + // major_hypersparse_idx: index within the hypersparse segment + __host__ __device__ thrust::optional get_major_hypersparse_idx_from_major_nocheck( + vertex_t major) const noexcept + { + if (dcs_nzd_vertices_) { + // we can avoid binary search (and potentially improve performance) if we add an auxiliary + // array or cuco::static_map (at the expense of additional memory) + auto it = thrust::lower_bound( + thrust::seq, *dcs_nzd_vertices_, *dcs_nzd_vertices_ + *dcs_nzd_vertex_count_, major); + return it != *dcs_nzd_vertices_ + *dcs_nzd_vertex_count_ + ? (*it == major ? thrust::optional{static_cast( + thrust::distance(*dcs_nzd_vertices_, it))} + : thrust::nullopt) + : thrust::nullopt; + } else { + return thrust::nullopt; + } + } + + // major_hypersparse_idx: index within the hypersparse segment + __host__ __device__ thrust::optional get_major_from_major_hypersparse_idx_nocheck( + vertex_t major_hypersparse_idx) const noexcept + { + return dcs_nzd_vertices_ + ? thrust::optional{(*dcs_nzd_vertices_)[major_hypersparse_idx]} + : thrust::nullopt; + } + + __host__ __device__ vertex_t + get_minor_from_minor_offset_nocheck(vertex_t minor_offset) const noexcept + { + return minor_first_ + minor_offset; + } + + __host__ __device__ vertex_t get_major_value_start_offset() const + { + return major_value_start_offset_; + } + + __host__ __device__ thrust::optional get_dcs_nzd_vertices() const + { + return dcs_nzd_vertices_; + } + __host__ __device__ thrust::optional get_dcs_nzd_vertex_count() const + { + return dcs_nzd_vertex_count_; + } + + private: + // should be trivially copyable to device + + thrust::optional dcs_nzd_vertices_{nullptr}; + thrust::optional dcs_nzd_vertex_count_{0}; + + vertex_t major_first_{0}; + vertex_t major_last_{0}; + vertex_t minor_first_{0}; + vertex_t minor_last_{0}; + + vertex_t major_value_start_offset_{0}; +}; + +// single-GPU version +template +class matrix_partition_device_view_t> + : public detail::matrix_partition_device_view_base_t { + public: + matrix_partition_device_view_t( + matrix_partition_view_t view) + : detail::matrix_partition_device_view_base_t( + view.get_offsets(), view.get_indices(), view.get_weights(), view.get_number_of_edges()), + number_of_vertices_(view.get_major_last()) + { + } + + __host__ __device__ vertex_t get_major_value_start_offset() const { return vertex_t{0}; } + + __host__ __device__ constexpr vertex_t get_major_first() const noexcept { return vertex_t{0}; } + + __host__ __device__ vertex_t get_major_last() const noexcept { return number_of_vertices_; } + + __host__ __device__ vertex_t get_major_size() const noexcept { return number_of_vertices_; } + + __host__ __device__ constexpr vertex_t get_minor_first() const noexcept { return vertex_t{0}; } + + __host__ __device__ vertex_t get_minor_last() const noexcept { return number_of_vertices_; } + + __host__ __device__ vertex_t get_minor_size() const noexcept { return number_of_vertices_; } + + __host__ __device__ vertex_t get_major_offset_from_major_nocheck(vertex_t major) const noexcept + { + return major; + } + + __host__ __device__ vertex_t get_minor_offset_from_minor_nocheck(vertex_t minor) const noexcept + { + return minor; + } + + __host__ __device__ vertex_t + get_major_from_major_offset_nocheck(vertex_t major_offset) const noexcept + { + return major_offset; + } + + // major_hypersparse_idx: index within the hypersparse segment + __host__ __device__ thrust::optional get_major_hypersparse_idx_from_major_nocheck( + vertex_t major) const noexcept + { + assert(false); + return thrust::nullopt; + } + + // major_hypersparse_idx: index within the hypersparse segment + __host__ __device__ thrust::optional get_major_from_major_hypersparse_idx_nocheck( + vertex_t major_hypersparse_idx) const noexcept + { + assert(false); + return thrust::nullopt; + } + + __host__ __device__ vertex_t + get_minor_from_minor_offset_nocheck(vertex_t minor_offset) const noexcept + { + return minor_offset; + } + + __host__ __device__ thrust::optional get_dcs_nzd_vertices() const + { + assert(false); + return thrust::nullopt; + } + __host__ __device__ thrust::optional get_dcs_nzd_vertex_count() const + { + assert(false); + return thrust::nullopt; + } + + private: + vertex_t number_of_vertices_; +}; + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/include/cugraph/matrix_partition_view.hpp b/cpp/include/cugraph/matrix_partition_view.hpp new file mode 100644 index 00000000000..6036bd2af48 --- /dev/null +++ b/cpp/include/cugraph/matrix_partition_view.hpp @@ -0,0 +1,139 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include + +namespace cugraph { +namespace experimental { + +namespace detail { + +template +class matrix_partition_view_base_t { + public: + matrix_partition_view_base_t(edge_t const* offsets, + vertex_t const* indices, + std::optional weights, + edge_t number_of_edges) + : offsets_(offsets), indices_(indices), weights_(weights), number_of_edges_(number_of_edges) + { + } + + edge_t get_number_of_edges() const { return number_of_edges_; } + + edge_t const* get_offsets() const { return offsets_; } + vertex_t const* get_indices() const { return indices_; } + std::optional get_weights() const { return weights_; } + + private: + edge_t const* offsets_{nullptr}; + vertex_t const* indices_{nullptr}; + std::optional weights_{std::nullopt}; + edge_t number_of_edges_{0}; +}; + +} // namespace detail + +template +class matrix_partition_view_t; + +// multi-GPU version +template +class matrix_partition_view_t> + : public detail::matrix_partition_view_base_t { + public: + matrix_partition_view_t(edge_t const* offsets, + vertex_t const* indices, + std::optional weights, + std::optional dcs_nzd_vertices, + std::optional dcs_nzd_vertex_count, + edge_t number_of_matrix_partition_edges, + vertex_t major_first, + vertex_t major_last, + vertex_t minor_first, + vertex_t minor_last, + vertex_t major_value_start_offset) + : detail::matrix_partition_view_base_t( + offsets, indices, weights, number_of_matrix_partition_edges), + dcs_nzd_vertices_(dcs_nzd_vertices), + dcs_nzd_vertex_count_(dcs_nzd_vertex_count), + major_first_(major_first), + major_last_(major_last), + minor_first_(minor_first), + minor_last_(minor_last), + major_value_start_offset_(major_value_start_offset) + { + } + + std::optional get_dcs_nzd_vertices() const { return dcs_nzd_vertices_; } + std::optional get_dcs_nzd_vertex_count() const { return dcs_nzd_vertex_count_; } + + vertex_t get_major_first() const { return major_first_; } + vertex_t get_major_last() const { return major_last_; } + vertex_t get_minor_first() const { return minor_first_; } + vertex_t get_minor_last() const { return minor_last_; } + + vertex_t get_major_value_start_offset() const { return major_value_start_offset_; } + + private: + // relevant only if we use the CSR + DCSR (or CSC + DCSC) hybrid format + std::optional dcs_nzd_vertices_{}; + std::optional dcs_nzd_vertex_count_{}; + + vertex_t major_first_{0}; + vertex_t major_last_{0}; + vertex_t minor_first_{0}; + vertex_t minor_last_{0}; + + vertex_t major_value_start_offset_{0}; +}; + +// single-GPU version +template +class matrix_partition_view_t> + : public detail::matrix_partition_view_base_t { + public: + matrix_partition_view_t(edge_t const* offsets, + vertex_t const* indices, + std::optional weights, + vertex_t number_of_vertices, + edge_t number_of_edges) + : detail::matrix_partition_view_base_t( + offsets, indices, weights, number_of_edges), + number_of_vertices_(number_of_vertices) + { + } + + std::optional get_dcs_nzd_vertices() const { return std::nullopt; } + std::optional get_dcs_nzd_vertex_count() const { return std::nullopt; } + + vertex_t get_major_first() const { return vertex_t{0}; } + vertex_t get_major_last() const { return number_of_vertices_; } + vertex_t get_minor_first() const { return vertex_t{0}; } + vertex_t get_minor_last() const { return number_of_vertices_; } + + private: + vertex_t number_of_vertices_{0}; +}; + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/include/partition_manager.hpp b/cpp/include/cugraph/partition_manager.hpp similarity index 98% rename from cpp/include/partition_manager.hpp rename to cpp/include/cugraph/partition_manager.hpp index 431655e5642..c7657d459b2 100644 --- a/cpp/include/partition_manager.hpp +++ b/cpp/include/cugraph/partition_manager.hpp @@ -1,5 +1,5 @@ /* - * 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. diff --git a/cpp/include/patterns/copy_to_adj_matrix_row_col.cuh b/cpp/include/cugraph/prims/copy_to_adj_matrix_row_col.cuh similarity index 52% rename from cpp/include/patterns/copy_to_adj_matrix_row_col.cuh rename to cpp/include/cugraph/prims/copy_to_adj_matrix_row_col.cuh index 760775c03d4..1aedd952cf6 100644 --- a/cpp/include/patterns/copy_to_adj_matrix_row_col.cuh +++ b/cpp/include/cugraph/prims/copy_to_adj_matrix_row_col.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -15,13 +15,16 @@ */ #pragma once -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include @@ -49,31 +52,49 @@ void copy_to_matrix_major(raft::handle_t const& handle, MatrixMajorValueOutputIterator matrix_major_value_output_first) { if (GraphViewType::is_multi_gpu) { - if (graph_view.is_hypergraph_partitioned()) { - CUGRAPH_FAIL("unimplemented."); - } else { - auto& comm = handle.get_comms(); - auto const comm_rank = comm.get_rank(); - auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); - auto const row_comm_rank = row_comm.get_rank(); - auto const row_comm_size = row_comm.get_size(); - auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); - auto const col_comm_rank = col_comm.get_rank(); - auto const col_comm_size = col_comm.get_size(); - - std::vector rx_counts(row_comm_size, size_t{0}); - std::vector displacements(row_comm_size, size_t{0}); - for (int i = 0; i < row_comm_size; ++i) { - rx_counts[i] = graph_view.get_vertex_partition_size(col_comm_rank * row_comm_size + i); - displacements[i] = (i == 0) ? 0 : displacements[i - 1] + rx_counts[i - 1]; - } - device_allgatherv(row_comm, - vertex_value_input_first, - matrix_major_value_output_first, - rx_counts, - displacements, - handle.get_stream()); + auto& comm = handle.get_comms(); + auto const comm_rank = comm.get_rank(); + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_rank = row_comm.get_rank(); + auto const row_comm_size = row_comm.get_size(); + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_rank = col_comm.get_rank(); + auto const col_comm_size = col_comm.get_size(); + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (beginning of col_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + + std::vector rx_counts(col_comm_size, size_t{0}); + std::vector displacements(col_comm_size, size_t{0}); + for (int i = 0; i < col_comm_size; ++i) { + rx_counts[i] = graph_view.get_vertex_partition_size(i * row_comm_size + row_comm_rank); + displacements[i] = (i == 0) ? 0 : displacements[i - 1] + rx_counts[i - 1]; } + device_allgatherv(col_comm, + vertex_value_input_first, + matrix_major_value_output_first, + rx_counts, + displacements, + handle.get_stream()); + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (end of col_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif } else { assert(graph_view.get_number_of_local_vertices() == GraphViewType::is_adj_matrix_transposed ? graph_view.get_number_of_local_adj_matrix_partition_cols() @@ -97,90 +118,110 @@ void copy_to_matrix_major(raft::handle_t const& handle, MatrixMajorValueOutputIterator matrix_major_value_output_first) { using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; if (GraphViewType::is_multi_gpu) { - if (graph_view.is_hypergraph_partitioned()) { - CUGRAPH_FAIL("unimplemented."); - } else { - auto& comm = handle.get_comms(); - auto const comm_rank = comm.get_rank(); - auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); - auto const row_comm_rank = row_comm.get_rank(); - auto const row_comm_size = row_comm.get_size(); - auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); - auto const col_comm_rank = col_comm.get_rank(); - auto const col_comm_size = col_comm.get_size(); - - auto rx_counts = - host_scalar_allgather(row_comm, - static_cast(thrust::distance(vertex_first, vertex_last)), - handle.get_stream()); - - matrix_partition_device_t matrix_partition(graph_view, 0); - for (int i = 0; i < row_comm_size; ++i) { - rmm::device_uvector rx_vertices(row_comm_rank == i ? size_t{0} : rx_counts[i], - handle.get_stream()); - auto rx_tmp_buffer = - allocate_comm_buffer::value_type>( - rx_counts[i], handle.get_stream()); - auto rx_value_first = get_comm_buffer_begin< - typename std::iterator_traits::value_type>(rx_tmp_buffer); - - if (row_comm_rank == i) { - vertex_partition_device_t vertex_partition(graph_view); - auto map_first = - thrust::make_transform_iterator(vertex_first, [vertex_partition] __device__(auto v) { - return vertex_partition.get_local_vertex_offset_from_vertex_nocheck(v); - }); - // FIXME: this gather (and temporary buffer) is unnecessary if NCCL directly takes a - // permutation iterator (and directly gathers to the internal buffer) - thrust::gather(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - map_first, - map_first + thrust::distance(vertex_first, vertex_last), - vertex_value_input_first, - rx_value_first); - } - - // FIXME: these broadcast operations can be placed between ncclGroupStart() and - // ncclGroupEnd() - device_bcast( - row_comm, vertex_first, rx_vertices.begin(), rx_counts[i], i, handle.get_stream()); - device_bcast( - row_comm, rx_value_first, rx_value_first, rx_counts[i], i, handle.get_stream()); - - if (row_comm_rank == i) { - auto map_first = - thrust::make_transform_iterator(vertex_first, [matrix_partition] __device__(auto v) { - return matrix_partition.get_major_offset_from_major_nocheck(v); - }); - // FIXME: this scatter is unnecessary if NCCL directly takes a permutation iterator (and - // directly scatters from the internal buffer) - thrust::scatter(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - rx_value_first, - rx_value_first + rx_counts[i], - map_first, - matrix_major_value_output_first); - } else { - auto map_first = thrust::make_transform_iterator( - rx_vertices.begin(), [matrix_partition] __device__(auto v) { - return matrix_partition.get_major_offset_from_major_nocheck(v); - }); - // FIXME: this scatter is unnecessary if NCCL directly takes a permutation iterator (and - // directly scatters from the internal buffer) - thrust::scatter(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - rx_value_first, - rx_value_first + rx_counts[i], - map_first, - matrix_major_value_output_first); - } - - CUDA_TRY(cudaStreamSynchronize( - handle.get_stream())); // this is as necessary rx_tmp_buffer will become out-of-scope - // once control flow exits this block (FIXME: we can reduce stream - // synchronization if we compute the maximum rx_counts and - // allocate rx_tmp_buffer outside the loop) + auto& comm = handle.get_comms(); + auto const comm_rank = comm.get_rank(); + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_rank = row_comm.get_rank(); + auto const row_comm_size = row_comm.get_size(); + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_rank = col_comm.get_rank(); + auto const col_comm_size = col_comm.get_size(); + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (beginning of col_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + + auto rx_counts = + host_scalar_allgather(col_comm, + static_cast(thrust::distance(vertex_first, vertex_last)), + handle.get_stream()); + + for (int i = 0; i < col_comm_size; ++i) { + auto matrix_partition = + matrix_partition_device_view_t( + graph_view.get_matrix_partition_view(i)); + + rmm::device_uvector rx_vertices(col_comm_rank == i ? size_t{0} : rx_counts[i], + handle.get_stream()); + auto rx_tmp_buffer = allocate_dataframe_buffer< + typename std::iterator_traits::value_type>(rx_counts[i], + handle.get_stream()); + auto rx_value_first = get_dataframe_buffer_begin< + typename std::iterator_traits::value_type>(rx_tmp_buffer); + + if (col_comm_rank == i) { + auto vertex_partition = + vertex_partition_device_view_t( + graph_view.get_vertex_partition_view()); + auto map_first = + thrust::make_transform_iterator(vertex_first, [vertex_partition] __device__(auto v) { + return vertex_partition.get_local_vertex_offset_from_vertex_nocheck(v); + }); + // FIXME: this gather (and temporary buffer) is unnecessary if NCCL directly takes a + // permutation iterator (and directly gathers to the internal buffer) + thrust::gather(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + map_first, + map_first + thrust::distance(vertex_first, vertex_last), + vertex_value_input_first, + rx_value_first); + } + + // FIXME: these broadcast operations can be placed between ncclGroupStart() and + // ncclGroupEnd() + device_bcast( + col_comm, vertex_first, rx_vertices.begin(), rx_counts[i], i, handle.get_stream()); + device_bcast(col_comm, rx_value_first, rx_value_first, rx_counts[i], i, handle.get_stream()); + + if (col_comm_rank == i) { + auto map_first = + thrust::make_transform_iterator(vertex_first, [matrix_partition] __device__(auto v) { + return matrix_partition.get_major_offset_from_major_nocheck(v); + }); + // FIXME: this scatter is unnecessary if NCCL directly takes a permutation iterator (and + // directly scatters from the internal buffer) + thrust::scatter( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + rx_value_first, + rx_value_first + rx_counts[i], + map_first, + matrix_major_value_output_first + matrix_partition.get_major_value_start_offset()); + } else { + auto map_first = thrust::make_transform_iterator( + rx_vertices.begin(), [matrix_partition] __device__(auto v) { + return matrix_partition.get_major_offset_from_major_nocheck(v); + }); + // FIXME: this scatter is unnecessary if NCCL directly takes a permutation iterator (and + // directly scatters from the internal buffer) + thrust::scatter( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + rx_value_first, + rx_value_first + rx_counts[i], + map_first, + matrix_major_value_output_first + matrix_partition.get_major_value_start_offset()); } } + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (end of col_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif } else { assert(graph_view.get_number_of_local_vertices() == GraphViewType::is_adj_matrix_transposed ? graph_view.get_number_of_local_adj_matrix_partition_cols() @@ -203,59 +244,49 @@ void copy_to_matrix_minor(raft::handle_t const& handle, MatrixMinorValueOutputIterator matrix_minor_value_output_first) { if (GraphViewType::is_multi_gpu) { - if (graph_view.is_hypergraph_partitioned()) { - CUGRAPH_FAIL("unimplemented."); - } else { - auto& comm = handle.get_comms(); - auto const comm_rank = comm.get_rank(); - auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); - auto const row_comm_rank = row_comm.get_rank(); - auto const row_comm_size = row_comm.get_size(); - auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); - auto const col_comm_rank = col_comm.get_rank(); - auto const col_comm_size = col_comm.get_size(); - - // FIXME: this P2P is unnecessary if we apply the partitioning scheme used with hypergraph - // partitioning - auto comm_src_rank = row_comm_rank * col_comm_size + col_comm_rank; - auto comm_dst_rank = (comm_rank % col_comm_size) * row_comm_size + comm_rank / col_comm_size; - // FIXME: this branch may no longer necessary with NCCL backend - if (comm_src_rank == comm_rank) { - assert(comm_dst_rank == comm_rank); - thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - vertex_value_input_first, - vertex_value_input_first + graph_view.get_number_of_local_vertices(), - matrix_minor_value_output_first + - (graph_view.get_vertex_partition_first(comm_src_rank) - - graph_view.get_vertex_partition_first(row_comm_rank * col_comm_size))); - } else { - device_sendrecv( - comm, - vertex_value_input_first, - static_cast(graph_view.get_number_of_local_vertices()), - comm_dst_rank, - matrix_minor_value_output_first + - (graph_view.get_vertex_partition_first(comm_src_rank) - - graph_view.get_vertex_partition_first(row_comm_rank * col_comm_size)), - static_cast(graph_view.get_vertex_partition_size(comm_src_rank)), - comm_src_rank, - handle.get_stream()); - } - - // FIXME: these broadcast operations can be placed between ncclGroupStart() and - // ncclGroupEnd() - for (int i = 0; i < col_comm_size; ++i) { - auto offset = graph_view.get_vertex_partition_first(row_comm_rank * col_comm_size + i) - - graph_view.get_vertex_partition_first(row_comm_rank * col_comm_size); - auto count = graph_view.get_vertex_partition_size(row_comm_rank * col_comm_size + i); - device_bcast(col_comm, - matrix_minor_value_output_first + offset, - matrix_minor_value_output_first + offset, - count, - i, - handle.get_stream()); - } + auto& comm = handle.get_comms(); + auto const comm_rank = comm.get_rank(); + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_rank = row_comm.get_rank(); + auto const row_comm_size = row_comm.get_size(); + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_rank = col_comm.get_rank(); + auto const col_comm_size = col_comm.get_size(); + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (beginning of row_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + + std::vector rx_counts(row_comm_size, size_t{0}); + std::vector displacements(row_comm_size, size_t{0}); + for (int i = 0; i < row_comm_size; ++i) { + rx_counts[i] = graph_view.get_vertex_partition_size(col_comm_rank * row_comm_size + i); + displacements[i] = (i == 0) ? 0 : displacements[i - 1] + rx_counts[i - 1]; } + device_allgatherv(row_comm, + vertex_value_input_first, + matrix_minor_value_output_first, + rx_counts, + displacements, + handle.get_stream()); + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (end of row_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif } else { assert(graph_view.get_number_of_local_vertices() == GraphViewType::is_adj_matrix_transposed ? graph_view.get_number_of_local_adj_matrix_partition_rows() @@ -279,162 +310,107 @@ void copy_to_matrix_minor(raft::handle_t const& handle, MatrixMinorValueOutputIterator matrix_minor_value_output_first) { using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; if (GraphViewType::is_multi_gpu) { - if (graph_view.is_hypergraph_partitioned()) { - CUGRAPH_FAIL("unimplemented."); - } else { - auto& comm = handle.get_comms(); - auto const comm_rank = comm.get_rank(); - auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); - auto const row_comm_rank = row_comm.get_rank(); - auto const row_comm_size = row_comm.get_size(); - auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); - auto const col_comm_rank = col_comm.get_rank(); - auto const col_comm_size = col_comm.get_size(); - - // FIXME: this P2P is unnecessary if apply the same partitioning scheme regardless of - // hypergraph partitioning is applied or not - auto comm_src_rank = row_comm_rank * col_comm_size + col_comm_rank; - auto comm_dst_rank = (comm_rank % col_comm_size) * row_comm_size + comm_rank / col_comm_size; - size_t tx_count = thrust::distance(vertex_first, vertex_last); - size_t rx_count{}; - // FIXME: it seems like raft::isend and raft::irecv do not properly handle the destination (or - // source) == self case. Need to double check and fix this if this is indeed the case (or RAFT - // may use ncclSend/ncclRecv instead of UCX for device data). - if (comm_src_rank == comm_rank) { - assert(comm_dst_rank == comm_rank); - rx_count = tx_count; - } else { - std::vector count_requests(2); - comm.isend(&tx_count, 1, comm_dst_rank, 0 /* tag */, count_requests.data()); - comm.irecv(&rx_count, 1, comm_src_rank, 0 /* tag */, count_requests.data() + 1); - comm.waitall(count_requests.size(), count_requests.data()); - } - - vertex_partition_device_t vertex_partition(graph_view); - rmm::device_uvector dst_vertices(rx_count, handle.get_stream()); - auto dst_tmp_buffer = - allocate_comm_buffer::value_type>( - rx_count, handle.get_stream()); - auto dst_value_first = - get_comm_buffer_begin::value_type>( - dst_tmp_buffer); - if (comm_src_rank == comm_rank) { - thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - vertex_first, - vertex_last, - dst_vertices.begin()); + auto& comm = handle.get_comms(); + auto const comm_rank = comm.get_rank(); + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_rank = row_comm.get_rank(); + auto const row_comm_size = row_comm.get_size(); + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_rank = col_comm.get_rank(); + auto const col_comm_size = col_comm.get_size(); + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (beginning of row_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + + auto rx_counts = + host_scalar_allgather(row_comm, + static_cast(thrust::distance(vertex_first, vertex_last)), + handle.get_stream()); + + auto matrix_partition = + matrix_partition_device_view_t( + graph_view.get_matrix_partition_view(size_t{0})); + for (int i = 0; i < row_comm_size; ++i) { + rmm::device_uvector rx_vertices(row_comm_rank == i ? size_t{0} : rx_counts[i], + handle.get_stream()); + auto rx_tmp_buffer = allocate_dataframe_buffer< + typename std::iterator_traits::value_type>(rx_counts[i], + handle.get_stream()); + auto rx_value_first = get_dataframe_buffer_begin< + typename std::iterator_traits::value_type>(rx_tmp_buffer); + + if (row_comm_rank == i) { + auto vertex_partition = + vertex_partition_device_view_t( + graph_view.get_vertex_partition_view()); auto map_first = thrust::make_transform_iterator(vertex_first, [vertex_partition] __device__(auto v) { return vertex_partition.get_local_vertex_offset_from_vertex_nocheck(v); }); + // FIXME: this gather (and temporary buffer) is unnecessary if NCCL directly takes a + // permutation iterator (and directly gathers to the internal buffer) thrust::gather(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), map_first, map_first + thrust::distance(vertex_first, vertex_last), vertex_value_input_first, - dst_value_first); - } else { - auto src_tmp_buffer = - allocate_comm_buffer::value_type>( - tx_count, handle.get_stream()); - auto src_value_first = get_comm_buffer_begin< - typename std::iterator_traits::value_type>(src_tmp_buffer); + rx_value_first); + } + + // FIXME: these broadcast operations can be placed between ncclGroupStart() and + // ncclGroupEnd() + device_bcast( + row_comm, vertex_first, rx_vertices.begin(), rx_counts[i], i, handle.get_stream()); + device_bcast(row_comm, rx_value_first, rx_value_first, rx_counts[i], i, handle.get_stream()); + if (row_comm_rank == i) { auto map_first = - thrust::make_transform_iterator(vertex_first, [vertex_partition] __device__(auto v) { - return vertex_partition.get_local_vertex_offset_from_vertex_nocheck(v); + thrust::make_transform_iterator(vertex_first, [matrix_partition] __device__(auto v) { + return matrix_partition.get_minor_offset_from_minor_nocheck(v); }); - thrust::gather(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - map_first, - map_first + thrust::distance(vertex_first, vertex_last), - vertex_value_input_first, - src_value_first); - - device_sendrecv( - comm, - vertex_first, - tx_count, - comm_dst_rank, - dst_vertices.begin(), - rx_count, - comm_src_rank, - handle.get_stream()); - - device_sendrecv(comm, - src_value_first, - tx_count, - comm_dst_rank, - dst_value_first, - rx_count, - comm_src_rank, - handle.get_stream()); - - CUDA_TRY(cudaStreamSynchronize( - handle.get_stream())); // this is as necessary src_tmp_buffer will become out-of-scope - // once control flow exits this block - } - - // FIXME: now we can clear tx_tmp_buffer - - auto rx_counts = host_scalar_allgather(col_comm, rx_count, handle.get_stream()); - - matrix_partition_device_t matrix_partition(graph_view, 0); - for (int i = 0; i < col_comm_size; ++i) { - rmm::device_uvector rx_vertices(col_comm_rank == i ? size_t{0} : rx_counts[i], - handle.get_stream()); - auto rx_tmp_buffer = - allocate_comm_buffer::value_type>( - rx_counts[i], handle.get_stream()); - auto rx_value_first = get_comm_buffer_begin< - typename std::iterator_traits::value_type>(rx_tmp_buffer); - - // FIXME: these broadcast operations can be placed between ncclGroupStart() and - // ncclGroupEnd() - device_bcast(col_comm, - dst_vertices.begin(), - rx_vertices.begin(), - rx_counts[i], - i, - handle.get_stream()); - device_bcast( - col_comm, dst_value_first, rx_value_first, rx_counts[i], i, handle.get_stream()); - - if (col_comm_rank == i) { - auto map_first = thrust::make_transform_iterator( - dst_vertices.begin(), [matrix_partition] __device__(auto v) { - return matrix_partition.get_minor_offset_from_minor_nocheck(v); - }); - - thrust::scatter(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - dst_value_first, - dst_value_first + rx_counts[i], - map_first, - matrix_minor_value_output_first); - } else { - auto map_first = thrust::make_transform_iterator( - rx_vertices.begin(), [matrix_partition] __device__(auto v) { - return matrix_partition.get_minor_offset_from_minor_nocheck(v); - }); - - thrust::scatter(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - rx_value_first, - rx_value_first + rx_counts[i], - map_first, - matrix_minor_value_output_first); - } - - CUDA_TRY(cudaStreamSynchronize( - handle.get_stream())); // this is as necessary rx_tmp_buffer will become out-of-scope - // once control flow exits this block (FIXME: we can reduce stream - // synchronization if we compute the maximum rx_counts and - // allocate rx_tmp_buffer outside the loop) + // FIXME: this scatter is unnecessary if NCCL directly takes a permutation iterator (and + // directly scatters from the internal buffer) + thrust::scatter(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + rx_value_first, + rx_value_first + rx_counts[i], + map_first, + matrix_minor_value_output_first); + } else { + auto map_first = thrust::make_transform_iterator( + rx_vertices.begin(), [matrix_partition] __device__(auto v) { + return matrix_partition.get_minor_offset_from_minor_nocheck(v); + }); + // FIXME: this scatter is unnecessary if NCCL directly takes a permutation iterator (and + // directly scatters from the internal buffer) + thrust::scatter(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + rx_value_first, + rx_value_first + rx_counts[i], + map_first, + matrix_minor_value_output_first); } - - CUDA_TRY(cudaStreamSynchronize( - handle.get_stream())); // this is as necessary dst_tmp_buffer will become out-of-scope once - // control flow exits this block } + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (end of row_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif } else { assert(graph_view.get_number_of_local_vertices() == graph_view.get_number_of_local_adj_matrix_partition_rows()); diff --git a/cpp/include/patterns/copy_v_transform_reduce_in_out_nbr.cuh b/cpp/include/cugraph/prims/copy_v_transform_reduce_in_out_nbr.cuh similarity index 52% rename from cpp/include/patterns/copy_v_transform_reduce_in_out_nbr.cuh rename to cpp/include/cugraph/prims/copy_v_transform_reduce_in_out_nbr.cuh index f3c36897dd6..148549fa99a 100644 --- a/cpp/include/patterns/copy_v_transform_reduce_in_out_nbr.cuh +++ b/cpp/include/cugraph/prims/copy_v_transform_reduce_in_out_nbr.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -15,12 +15,14 @@ */ #pragma once -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include @@ -41,23 +43,104 @@ namespace experimental { namespace detail { -// FIXME: block size requires tuning -int32_t constexpr copy_v_transform_reduce_nbr_for_all_block_size = 128; +int32_t constexpr copy_v_transform_reduce_nbr_for_all_block_size = 512; -#if 0 -// FIXME: delete this once we verify that the thrust replace in for_all_major_for_all_nbr_low_degree is no slower than the original for loop based imoplementation -template -__device__ std::enable_if_t accumulate_edge_op_result(T& lhs, T const& rhs) +template +__global__ void for_all_major_for_all_nbr_hypersparse( + matrix_partition_device_view_t matrix_partition, + typename GraphViewType::vertex_type major_hypersparse_first, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + ResultValueOutputIterator result_value_output_first, + EdgeOp e_op, + T init /* relevent only if update_major == true */) { - lhs = plus_edge_op_result(lhs, rhs); -} + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; -template -__device__ std::enable_if_t accumulate_edge_op_result(T& lhs, T const& rhs) -{ - atomic_add(&lhs, rhs); + auto const tid = threadIdx.x + blockIdx.x * blockDim.x; + auto major_start_offset = + static_cast(major_hypersparse_first - matrix_partition.get_major_first()); + auto idx = static_cast(tid); + + auto dcs_nzd_vertex_count = *(matrix_partition.get_dcs_nzd_vertex_count()); + + property_add edge_property_add{}; + while (idx < static_cast(dcs_nzd_vertex_count)) { + auto major = + *(matrix_partition.get_major_from_major_hypersparse_idx_nocheck(static_cast(idx))); + auto major_idx = + major_start_offset + idx; // major_offset != major_idx in the hypersparse region + vertex_t const* indices{nullptr}; + thrust::optional weights{nullptr}; + edge_t local_degree{}; + thrust::tie(indices, weights, local_degree) = + matrix_partition.get_local_edges(static_cast(major_idx)); + auto transform_op = [&matrix_partition, + &adj_matrix_row_value_input_first, + &adj_matrix_col_value_input_first, + &e_op, + major, + indices, + weights] __device__(auto i) { + auto major_offset = matrix_partition.get_major_offset_from_major_nocheck(major); + auto minor = indices[i]; + auto weight = weights ? (*weights)[i] : weight_t{1.0}; + auto minor_offset = matrix_partition.get_minor_offset_from_minor_nocheck(minor); + auto row = GraphViewType::is_adj_matrix_transposed ? minor : major; + auto col = GraphViewType::is_adj_matrix_transposed ? major : minor; + auto row_offset = GraphViewType::is_adj_matrix_transposed + ? minor_offset + : static_cast(major_offset); + auto col_offset = GraphViewType::is_adj_matrix_transposed + ? static_cast(major_offset) + : minor_offset; + return evaluate_edge_op() + .compute(row, + col, + weight, + *(adj_matrix_row_value_input_first + row_offset), + *(adj_matrix_col_value_input_first + col_offset), + e_op); + }; + + if (update_major) { + *(result_value_output_first + (major - major_hypersparse_first)) = + thrust::transform_reduce(thrust::seq, + thrust::make_counting_iterator(edge_t{0}), + thrust::make_counting_iterator(local_degree), + transform_op, + init, + edge_property_add); + } else { + thrust::for_each( + thrust::seq, + thrust::make_counting_iterator(edge_t{0}), + thrust::make_counting_iterator(local_degree), + [&matrix_partition, indices, &result_value_output_first, &transform_op] __device__(auto i) { + auto e_op_result = transform_op(i); + auto minor = indices[i]; + auto minor_offset = matrix_partition.get_minor_offset_from_minor_nocheck(minor); + atomic_accumulate_edge_op_result(result_value_output_first + minor_offset, e_op_result); + }); + } + idx += gridDim.x * blockDim.x; + } } -#endif template __global__ void for_all_major_for_all_nbr_low_degree( - matrix_partition_device_t matrix_partition, + matrix_partition_device_view_t matrix_partition, typename GraphViewType::vertex_type major_first, typename GraphViewType::vertex_type major_last, AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, @@ -76,23 +162,22 @@ __global__ void for_all_major_for_all_nbr_low_degree( EdgeOp e_op, T init /* relevent only if update_major == true */) { - using vertex_t = typename GraphViewType::vertex_type; - using edge_t = typename GraphViewType::edge_type; - using weight_t = typename GraphViewType::weight_type; - using e_op_result_t = T; + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; auto const tid = threadIdx.x + blockIdx.x * blockDim.x; auto major_start_offset = static_cast(major_first - matrix_partition.get_major_first()); auto idx = static_cast(tid); + property_add edge_property_add{}; while (idx < static_cast(major_last - major_first)) { + auto major_offset = major_start_offset + idx; vertex_t const* indices{nullptr}; - weight_t const* weights{nullptr}; + thrust::optional weights{nullptr}; edge_t local_degree{}; - auto major_offset = major_start_offset + idx; thrust::tie(indices, weights, local_degree) = matrix_partition.get_local_edges(static_cast(major_offset)); -#if 1 auto transform_op = [&matrix_partition, &adj_matrix_row_value_input_first, &adj_matrix_col_value_input_first, @@ -101,21 +186,22 @@ __global__ void for_all_major_for_all_nbr_low_degree( indices, weights] __device__(auto i) { auto minor = indices[i]; - auto weight = weights != nullptr ? weights[i] : weight_t{1.0}; + auto weight = weights ? (*weights)[i] : weight_t{1.0}; auto minor_offset = matrix_partition.get_minor_offset_from_minor_nocheck(minor); auto row = GraphViewType::is_adj_matrix_transposed - ? minor - : matrix_partition.get_major_from_major_offset_nocheck(major_offset); - auto col = GraphViewType::is_adj_matrix_transposed - ? matrix_partition.get_major_from_major_offset_nocheck(major_offset) - : minor; - auto row_offset = GraphViewType::is_adj_matrix_transposed - ? minor_offset - : static_cast(major_offset); - auto col_offset = GraphViewType::is_adj_matrix_transposed - ? static_cast(major_offset) - : minor_offset; + ? minor + : matrix_partition.get_major_from_major_offset_nocheck(major_offset); + auto col = GraphViewType::is_adj_matrix_transposed + ? matrix_partition.get_major_from_major_offset_nocheck(major_offset) + : minor; + auto row_offset = GraphViewType::is_adj_matrix_transposed + ? minor_offset + : static_cast(major_offset); + auto col_offset = GraphViewType::is_adj_matrix_transposed + ? static_cast(major_offset) + : minor_offset; return evaluate_edge_op() @@ -128,13 +214,13 @@ __global__ void for_all_major_for_all_nbr_low_degree( }; if (update_major) { - *(result_value_output_first + idx) = thrust::transform_reduce( - thrust::seq, - thrust::make_counting_iterator(edge_t{0}), - thrust::make_counting_iterator(local_degree), - transform_op, - init, - [] __device__(auto lhs, auto rhs) { return plus_edge_op_result(lhs, rhs); }); + *(result_value_output_first + idx) = + thrust::transform_reduce(thrust::seq, + thrust::make_counting_iterator(edge_t{0}), + thrust::make_counting_iterator(local_degree), + transform_op, + init, + edge_property_add); } else { thrust::for_each( thrust::seq, @@ -147,44 +233,6 @@ __global__ void for_all_major_for_all_nbr_low_degree( atomic_accumulate_edge_op_result(result_value_output_first + minor_offset, e_op_result); }); } -#else - // FIXME: delete this once we verify that the code above is not slower than this. - e_op_result_t e_op_result_sum{init}; // relevent only if update_major == true - for (edge_t i = 0; i < local_degree; ++i) { - auto minor = indices[i]; - auto weight = weights != nullptr ? weights[i] : weight_t{1.0}; - auto minor_offset = matrix_partition.get_minor_offset_from_minor_nocheck(minor); - auto row = GraphViewType::is_adj_matrix_transposed - ? minor - : matrix_partition.get_major_from_major_offset_nocheck(major_offset); - auto col = GraphViewType::is_adj_matrix_transposed - ? matrix_partition.get_major_from_major_offset_nocheck(major_offset) - : minor; - auto row_offset = GraphViewType::is_adj_matrix_transposed - ? minor_offset - : static_cast(major_offset); - auto col_offset = GraphViewType::is_adj_matrix_transposed - ? static_cast(major_offset) - : minor_offset; - auto e_op_result = evaluate_edge_op() - .compute(row, - col, - weight, - *(adj_matrix_row_value_input_first + row_offset), - *(adj_matrix_col_value_input_first + col_offset), - e_op); - if (update_major) { - accumulate_edge_op_result(e_op_result_sum, e_op_result); - } else { - accumulate_edge_op_result(*(result_value_output_first + minor_offset), - e_op_result); - } - } - if (update_major) { *(result_value_output_first + idx) = e_op_result_sum; } -#endif idx += gridDim.x * blockDim.x; } } @@ -197,7 +245,10 @@ template __global__ void for_all_major_for_all_nbr_mid_degree( - matrix_partition_device_t matrix_partition, + matrix_partition_device_view_t matrix_partition, typename GraphViewType::vertex_type major_first, typename GraphViewType::vertex_type major_last, AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, @@ -217,31 +268,33 @@ __global__ void for_all_major_for_all_nbr_mid_degree( auto major_start_offset = static_cast(major_first - matrix_partition.get_major_first()); auto idx = static_cast(tid / raft::warp_size()); + property_add edge_property_add{}; while (idx < static_cast(major_last - major_first)) { + auto major_offset = major_start_offset + idx; vertex_t const* indices{nullptr}; - weight_t const* weights{nullptr}; + thrust::optional weights{nullptr}; edge_t local_degree{}; - auto major_offset = major_start_offset + idx; thrust::tie(indices, weights, local_degree) = matrix_partition.get_local_edges(major_offset); auto e_op_result_sum = lane_id == 0 ? init : e_op_result_t{}; // relevent only if update_major == true - for (edge_t i = lane_id; i < local_degree; i += raft::warp_size) { + for (edge_t i = lane_id; i < local_degree; i += raft::warp_size()) { auto minor = indices[i]; - auto weight = weights != nullptr ? weights[i] : weight_t{1.0}; + auto weight = weights ? (*weights)[i] : weight_t{1.0}; auto minor_offset = matrix_partition.get_minor_offset_from_minor_nocheck(minor); auto row = GraphViewType::is_adj_matrix_transposed - ? minor - : matrix_partition.get_major_from_major_offset_nocheck(major_offset); - auto col = GraphViewType::is_adj_matrix_transposed - ? matrix_partition.get_major_from_major_offset_nocheck(major_offset) - : minor; - auto row_offset = GraphViewType::is_adj_matrix_transposed - ? minor_offset - : static_cast(major_offset); - auto col_offset = GraphViewType::is_adj_matrix_transposed - ? static_cast(major_offset) - : minor_offset; - auto e_op_result = evaluate_edge_op(major_offset); + auto col_offset = GraphViewType::is_adj_matrix_transposed + ? static_cast(major_offset) + : minor_offset; + auto e_op_result = evaluate_edge_op() @@ -252,7 +305,7 @@ __global__ void for_all_major_for_all_nbr_mid_degree( *(adj_matrix_col_value_input_first + col_offset), e_op); if (update_major) { - e_op_result_sum = plus_edge_op_result(e_op_result_sum, e_op_result); + e_op_result_sum = edge_property_add(e_op_result_sum, e_op_result); } else { atomic_accumulate_edge_op_result(result_value_output_first + minor_offset, e_op_result); } @@ -274,7 +327,10 @@ template __global__ void for_all_major_for_all_nbr_high_degree( - matrix_partition_device_t matrix_partition, + matrix_partition_device_view_t matrix_partition, typename GraphViewType::vertex_type major_first, typename GraphViewType::vertex_type major_last, AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, @@ -291,31 +347,33 @@ __global__ void for_all_major_for_all_nbr_high_degree( auto major_start_offset = static_cast(major_first - matrix_partition.get_major_first()); auto idx = static_cast(blockIdx.x); + property_add edge_property_add{}; while (idx < static_cast(major_last - major_first)) { + auto major_offset = major_start_offset + idx; vertex_t const* indices{nullptr}; - weight_t const* weights{nullptr}; + thrust::optional weights{nullptr}; edge_t local_degree{}; - auto major_offset = major_start_offset + idx; thrust::tie(indices, weights, local_degree) = matrix_partition.get_local_edges(major_offset); auto e_op_result_sum = threadIdx.x == 0 ? init : e_op_result_t{}; // relevent only if update_major == true for (edge_t i = threadIdx.x; i < local_degree; i += blockDim.x) { auto minor = indices[i]; - auto weight = weights != nullptr ? weights[i] : weight_t{1.0}; + auto weight = weights ? (*weights)[i] : weight_t{1.0}; auto minor_offset = matrix_partition.get_minor_offset_from_minor_nocheck(minor); auto row = GraphViewType::is_adj_matrix_transposed - ? minor - : matrix_partition.get_major_from_major_offset_nocheck(major_offset); - auto col = GraphViewType::is_adj_matrix_transposed - ? matrix_partition.get_major_from_major_offset_nocheck(major_offset) - : minor; - auto row_offset = GraphViewType::is_adj_matrix_transposed - ? minor_offset - : static_cast(major_offset); - auto col_offset = GraphViewType::is_adj_matrix_transposed - ? static_cast(major_offset) - : minor_offset; - auto e_op_result = evaluate_edge_op(major_offset); + auto col_offset = GraphViewType::is_adj_matrix_transposed + ? static_cast(major_offset) + : minor_offset; + auto e_op_result = evaluate_edge_op() @@ -326,7 +384,7 @@ __global__ void for_all_major_for_all_nbr_high_degree( *(adj_matrix_col_value_input_first + col_offset), e_op); if (update_major) { - e_op_result_sum = plus_edge_op_result(e_op_result_sum, e_op_result); + e_op_result_sum = edge_property_add(e_op_result_sum, e_op_result); } else { atomic_accumulate_edge_op_result(result_value_output_first + minor_offset, e_op_result); } @@ -357,38 +415,28 @@ void copy_v_transform_reduce_nbr(raft::handle_t const& handle, T init, VertexValueOutputIterator vertex_value_output_first) { - using vertex_t = typename GraphViewType::vertex_type; + constexpr auto update_major = (in == GraphViewType::is_adj_matrix_transposed); + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; static_assert(is_arithmetic_or_thrust_tuple_of_arithmetic::value); - auto loop_count = size_t{1}; - if (GraphViewType::is_multi_gpu) { - auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); - auto const row_comm_size = row_comm.get_size(); - loop_count = graph_view.is_hypergraph_partitioned() - ? graph_view.get_number_of_local_adj_matrix_partitions() - : static_cast(row_comm_size); - } - auto comm_rank = handle.comms_initialized() ? handle.get_comms().get_rank() : int{0}; - auto minor_tmp_buffer_size = (GraphViewType::is_multi_gpu && (in != GraphViewType::is_adj_matrix_transposed)) ? GraphViewType::is_adj_matrix_transposed ? graph_view.get_number_of_local_adj_matrix_partition_rows() : graph_view.get_number_of_local_adj_matrix_partition_cols() : vertex_t{0}; - auto minor_tmp_buffer = allocate_comm_buffer(minor_tmp_buffer_size, handle.get_stream()); - auto minor_buffer_first = get_comm_buffer_begin(minor_tmp_buffer); + auto minor_tmp_buffer = allocate_dataframe_buffer(minor_tmp_buffer_size, handle.get_stream()); + auto minor_buffer_first = get_dataframe_buffer_begin(minor_tmp_buffer); if (in != GraphViewType::is_adj_matrix_transposed) { auto minor_init = init; if (GraphViewType::is_multi_gpu) { auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); auto const row_comm_rank = row_comm.get_rank(); - auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); - auto const col_comm_rank = col_comm.get_rank(); - minor_init = graph_view.is_hypergraph_partitioned() ? (row_comm_rank == 0) ? init : T{} - : (col_comm_rank == 0) ? init : T{}; + minor_init = (row_comm_rank == 0) ? init : T{}; } if (GraphViewType::is_multi_gpu) { @@ -406,96 +454,139 @@ void copy_v_transform_reduce_nbr(raft::handle_t const& handle, assert(minor_tmp_buffer_size == 0); } - for (size_t i = 0; i < loop_count; ++i) { - matrix_partition_device_t matrix_partition( - graph_view, (GraphViewType::is_multi_gpu && !graph_view.is_hypergraph_partitioned()) ? 0 : i); - - auto major_tmp_buffer_size = vertex_t{0}; - if (GraphViewType::is_multi_gpu) { - auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); - auto const row_comm_size = row_comm.get_size(); - auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); - auto const col_comm_rank = col_comm.get_rank(); + for (size_t i = 0; i < graph_view.get_number_of_local_adj_matrix_partitions(); ++i) { + auto matrix_partition = + matrix_partition_device_view_t( + graph_view.get_matrix_partition_view(i)); - major_tmp_buffer_size = - (in == GraphViewType::is_adj_matrix_transposed) - ? graph_view.is_hypergraph_partitioned() - ? matrix_partition.get_major_size() - : graph_view.get_vertex_partition_size(col_comm_rank * row_comm_size + i) - : vertex_t{0}; - } - auto major_tmp_buffer = allocate_comm_buffer(major_tmp_buffer_size, handle.get_stream()); - auto major_buffer_first = get_comm_buffer_begin(major_tmp_buffer); + auto major_tmp_buffer_size = + GraphViewType::is_multi_gpu && update_major ? matrix_partition.get_major_size() : vertex_t{0}; + auto major_tmp_buffer = + allocate_dataframe_buffer(major_tmp_buffer_size, handle.get_stream()); + auto major_buffer_first = get_dataframe_buffer_begin(major_tmp_buffer); auto major_init = T{}; - if (in == GraphViewType::is_adj_matrix_transposed) { + if (update_major) { if (GraphViewType::is_multi_gpu) { - auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); - auto const row_comm_rank = row_comm.get_rank(); auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); auto const col_comm_rank = col_comm.get_rank(); - major_init = graph_view.is_hypergraph_partitioned() ? (col_comm_rank == 0) ? init : T{} - : (row_comm_rank == 0) ? init : T{}; + major_init = (col_comm_rank == 0) ? init : T{}; } else { major_init = init; } } - int comm_root_rank = 0; - if (GraphViewType::is_multi_gpu) { - auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); - auto const row_comm_rank = row_comm.get_rank(); - auto const row_comm_size = row_comm.get_size(); - auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); - auto const col_comm_rank = col_comm.get_rank(); - comm_root_rank = graph_view.is_hypergraph_partitioned() ? i * row_comm_size + row_comm_rank - : col_comm_rank * row_comm_size + i; + auto row_value_input_offset = GraphViewType::is_adj_matrix_transposed + ? vertex_t{0} + : matrix_partition.get_major_value_start_offset(); + auto col_value_input_offset = GraphViewType::is_adj_matrix_transposed + ? matrix_partition.get_major_value_start_offset() + : vertex_t{0}; + std::conditional_t< + GraphViewType::is_multi_gpu, + std::conditional_t, + VertexValueOutputIterator> + output_buffer_first{}; + if constexpr (GraphViewType::is_multi_gpu) { + output_buffer_first = update_major ? major_buffer_first : minor_buffer_first; + } else { + output_buffer_first = vertex_value_output_first; } - - if (graph_view.get_vertex_partition_size(comm_root_rank) > 0) { - raft::grid_1d_thread_t update_grid(graph_view.get_vertex_partition_size(comm_root_rank), + auto segment_offsets = graph_view.get_local_adj_matrix_partition_segment_offsets(i); + if (segment_offsets) { + // FIXME: we may further improve performance by 1) concurrently running kernels on different + // segments; 2) individually tuning block sizes for different segments; and 3) adding one more + // segment for very high degree vertices and running segmented reduction + static_assert(detail::num_sparse_segments_per_vertex_partition == 3); + if ((*segment_offsets)[1] > 0) { + raft::grid_1d_block_t update_grid((*segment_offsets)[1], + detail::copy_v_transform_reduce_nbr_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + detail::for_all_major_for_all_nbr_high_degree + <<>>( + matrix_partition, + matrix_partition.get_major_first(), + matrix_partition.get_major_first() + (*segment_offsets)[1], + adj_matrix_row_value_input_first + row_value_input_offset, + adj_matrix_col_value_input_first + col_value_input_offset, + output_buffer_first, + e_op, + major_init); + } + if ((*segment_offsets)[2] - (*segment_offsets)[1] > 0) { + raft::grid_1d_warp_t update_grid((*segment_offsets)[2] - (*segment_offsets)[1], detail::copy_v_transform_reduce_nbr_for_all_block_size, handle.get_device_properties().maxGridSize[0]); - - if (GraphViewType::is_multi_gpu) { - auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); - auto const row_comm_size = row_comm.get_size(); - auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); - auto const col_comm_rank = col_comm.get_rank(); - - auto row_value_input_offset = GraphViewType::is_adj_matrix_transposed - ? vertex_t{0} - : matrix_partition.get_major_value_start_offset(); - auto col_value_input_offset = GraphViewType::is_adj_matrix_transposed - ? matrix_partition.get_major_value_start_offset() - : vertex_t{0}; - - detail::for_all_major_for_all_nbr_low_degree + detail::for_all_major_for_all_nbr_mid_degree <<>>( matrix_partition, - graph_view.get_vertex_partition_first(comm_root_rank), - graph_view.get_vertex_partition_last(comm_root_rank), + matrix_partition.get_major_first() + (*segment_offsets)[1], + matrix_partition.get_major_first() + (*segment_offsets)[2], adj_matrix_row_value_input_first + row_value_input_offset, adj_matrix_col_value_input_first + col_value_input_offset, - (in == GraphViewType::is_adj_matrix_transposed) ? major_buffer_first - : minor_buffer_first, + output_buffer_first + (update_major ? (*segment_offsets)[1] : vertex_t{0}), e_op, major_init); - } else { - detail::for_all_major_for_all_nbr_low_degree + } + if ((*segment_offsets)[3] - (*segment_offsets)[2] > 0) { + raft::grid_1d_thread_t update_grid((*segment_offsets)[3] - (*segment_offsets)[2], + detail::copy_v_transform_reduce_nbr_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + detail::for_all_major_for_all_nbr_low_degree + <<>>( + matrix_partition, + matrix_partition.get_major_first() + (*segment_offsets)[2], + matrix_partition.get_major_first() + (*segment_offsets)[3], + adj_matrix_row_value_input_first + row_value_input_offset, + adj_matrix_col_value_input_first + col_value_input_offset, + output_buffer_first + (update_major ? (*segment_offsets)[2] : vertex_t{0}), + e_op, + major_init); + } + if (matrix_partition.get_dcs_nzd_vertex_count()) { + if constexpr (update_major) { // this is necessary as we don't visit every vertex in the + // hypersparse segment in + // for_all_major_for_all_nbr_hypersparse + thrust::fill(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + output_buffer_first + (*segment_offsets)[3], + output_buffer_first + (*segment_offsets)[4], + major_init); + } + if (*(matrix_partition.get_dcs_nzd_vertex_count()) > 0) { + raft::grid_1d_thread_t update_grid(*(matrix_partition.get_dcs_nzd_vertex_count()), + detail::copy_v_transform_reduce_nbr_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + detail::for_all_major_for_all_nbr_hypersparse + <<>>( + matrix_partition, + matrix_partition.get_major_first() + (*segment_offsets)[3], + adj_matrix_row_value_input_first + row_value_input_offset, + adj_matrix_col_value_input_first + col_value_input_offset, + output_buffer_first + (update_major ? (*segment_offsets)[3] : vertex_t{0}), + e_op, + major_init); + } + } + } else { + if (matrix_partition.get_major_size() > 0) { + raft::grid_1d_thread_t update_grid(matrix_partition.get_major_size(), + detail::copy_v_transform_reduce_nbr_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + detail::for_all_major_for_all_nbr_low_degree <<>>( matrix_partition, - graph_view.get_vertex_partition_first(comm_root_rank), - graph_view.get_vertex_partition_last(comm_root_rank), - adj_matrix_row_value_input_first, - adj_matrix_col_value_input_first, - vertex_value_output_first, + matrix_partition.get_major_first(), + matrix_partition.get_major_last(), + adj_matrix_row_value_input_first + row_value_input_offset, + adj_matrix_col_value_input_first + col_value_input_offset, + output_buffer_first, e_op, major_init); } } - if (GraphViewType::is_multi_gpu && (in == GraphViewType::is_adj_matrix_transposed)) { + if (GraphViewType::is_multi_gpu && update_major) { + auto& comm = handle.get_comms(); auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); auto const row_comm_rank = row_comm.get_rank(); auto const row_comm_size = row_comm.get_size(); @@ -503,35 +594,39 @@ void copy_v_transform_reduce_nbr(raft::handle_t const& handle, auto const col_comm_rank = col_comm.get_rank(); auto const col_comm_size = col_comm.get_size(); - if (graph_view.is_hypergraph_partitioned()) { - device_reduce( - col_comm, - major_buffer_first, - vertex_value_output_first, - static_cast(graph_view.get_vertex_partition_size(i * row_comm_size + i)), - raft::comms::op_t::SUM, - i, - handle.get_stream()); - } else { - device_reduce(row_comm, - major_buffer_first, - vertex_value_output_first, - static_cast( - graph_view.get_vertex_partition_size(col_comm_rank * row_comm_size + i)), - raft::comms::op_t::SUM, - i, - handle.get_stream()); - } - } + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (beginning of col_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + + device_reduce(col_comm, + major_buffer_first, + vertex_value_output_first, + matrix_partition.get_major_size(), + raft::comms::op_t::SUM, + i, + handle.get_stream()); - CUDA_TRY(cudaStreamSynchronize( - handle.get_stream())); // this is as necessary major_tmp_buffer will become out-of-scope once - // control flow exits this block (FIXME: we can reduce stream - // synchronization if we compute the maximum major_tmp_buffer_size and - // allocate major_tmp_buffer outside the loop) + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (end of col_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + } } - if (GraphViewType::is_multi_gpu && (in != GraphViewType::is_adj_matrix_transposed)) { + if (GraphViewType::is_multi_gpu && !update_major) { auto& comm = handle.get_comms(); auto const comm_rank = comm.get_rank(); auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); @@ -541,59 +636,41 @@ void copy_v_transform_reduce_nbr(raft::handle_t const& handle, auto const col_comm_rank = col_comm.get_rank(); auto const col_comm_size = col_comm.get_size(); - if (graph_view.is_hypergraph_partitioned()) { - CUGRAPH_FAIL("unimplemented."); - } else { - for (int i = 0; i < col_comm_size; ++i) { - auto offset = (graph_view.get_vertex_partition_first(row_comm_rank * col_comm_size + i) - - graph_view.get_vertex_partition_first(row_comm_rank * col_comm_size)); - auto size = static_cast( - graph_view.get_vertex_partition_size(row_comm_rank * col_comm_size + i)); - device_reduce(col_comm, - minor_buffer_first + offset, - minor_buffer_first + offset, - size, - raft::comms::op_t::SUM, - i, - handle.get_stream()); - } + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (beginning of row_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif - // FIXME: this P2P is unnecessary if we apply the partitioning scheme used with hypergraph - // partitioning - auto comm_src_rank = (comm_rank % col_comm_size) * row_comm_size + comm_rank / col_comm_size; - auto comm_dst_rank = row_comm_rank * col_comm_size + col_comm_rank; - // FIXME: this branch may no longer necessary with NCCL backend - if (comm_src_rank == comm_rank) { - assert(comm_dst_rank == comm_rank); - auto offset = - graph_view.get_vertex_partition_first(row_comm_rank * col_comm_size + col_comm_rank) - - graph_view.get_vertex_partition_first(row_comm_rank * col_comm_size); - auto size = static_cast( - graph_view.get_vertex_partition_size(row_comm_rank * col_comm_size + col_comm_rank)); - thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - minor_buffer_first + offset, - minor_buffer_first + offset + size, - vertex_value_output_first); - } else { - device_sendrecv( - comm, - minor_buffer_first + - (graph_view.get_vertex_partition_first(row_comm_rank * col_comm_size + col_comm_rank) - - graph_view.get_vertex_partition_first(row_comm_rank * col_comm_size)), - static_cast( - graph_view.get_vertex_partition_size(row_comm_rank * col_comm_size + col_comm_rank)), - comm_dst_rank, - vertex_value_output_first, - static_cast(graph_view.get_vertex_partition_size(comm_rank)), - comm_src_rank, - handle.get_stream()); - } + for (int i = 0; i < row_comm_size; ++i) { + auto offset = (graph_view.get_vertex_partition_first(col_comm_rank * row_comm_size + i) - + graph_view.get_vertex_partition_first(col_comm_rank * row_comm_size)); + device_reduce(row_comm, + minor_buffer_first + offset, + vertex_value_output_first, + static_cast( + graph_view.get_vertex_partition_size(col_comm_rank * row_comm_size + i)), + raft::comms::op_t::SUM, + i, + handle.get_stream()); } - } - CUDA_TRY(cudaStreamSynchronize( - handle.get_stream())); // this is as necessary minor_tmp_buffer will become out-of-scope once - // control flow exits this block + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (end of row_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + } } } // namespace detail @@ -627,7 +704,7 @@ void copy_v_transform_reduce_nbr(raft::handle_t const& handle, * weight), *(@p adj_matrix_row_value_input_first + i), and *(@p adj_matrix_col_value_input_first + * j) (where i is in [0, graph_view.get_number_of_local_adj_matrix_partition_rows()) and j is in [0, * get_number_of_local_adj_matrix_partition_cols())) and returns a value to be reduced. - * @param init Initial value to be added to the reduced @e_op return values for each vertex. + * @param init Initial value to be added to the reduced @p e_op return values for each vertex. * @param vertex_value_output_first Iterator pointing to the vertex property variables for the first * (inclusive) vertex (assigned to tihs process in multi-GPU). `vertex_value_output_last` * (exclusive) is deduced as @p vertex_value_output_first + @p @@ -689,7 +766,7 @@ void copy_v_transform_reduce_in_nbr(raft::handle_t const& handle, * adj_matrix_col_value_input_first + j) (where i is in [0, * graph_view.get_number_of_local_adj_matrix_partition_rows()) and j is in [0, * get_number_of_local_adj_matrix_partition_cols())) and returns a value to be reduced. - * @param init Initial value to be added to the reduced @e_op return values for each vertex. + * @param init Initial value to be added to the reduced @p e_op return values for each vertex. * @param vertex_value_output_first Iterator pointing to the vertex property variables for the * first (inclusive) vertex (assigned to tihs process in multi-GPU). `vertex_value_output_last` * (exclusive) is deduced as @p vertex_value_output_first + @p diff --git a/cpp/include/cugraph/prims/copy_v_transform_reduce_key_aggregated_out_nbr.cuh b/cpp/include/cugraph/prims/copy_v_transform_reduce_key_aggregated_out_nbr.cuh new file mode 100644 index 00000000000..c504fa49526 --- /dev/null +++ b/cpp/include/cugraph/prims/copy_v_transform_reduce_key_aggregated_out_nbr.cuh @@ -0,0 +1,695 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +namespace cugraph { +namespace experimental { + +namespace detail { + +// FIXME: block size requires tuning +int32_t constexpr copy_v_transform_reduce_key_aggregated_out_nbr_for_all_block_size = 1024; + +// a workaround for cudaErrorInvalidDeviceFunction error when device lambda is used +template +struct minor_to_key_t { + using vertex_t = typename std::iterator_traits::value_type; + VertexIterator adj_matrix_col_key_first{}; + vertex_t minor_first{}; + __device__ vertex_t operator()(vertex_t minor) + { + return *(adj_matrix_col_key_first + (minor - minor_first)); + } +}; + +template +__global__ void for_all_major_for_all_nbr_mid_degree( + matrix_partition_device_view_t matrix_partition, + vertex_t major_first, + vertex_t major_last, + vertex_t* majors) +{ + auto const tid = threadIdx.x + blockIdx.x * blockDim.x; + static_assert( + copy_v_transform_reduce_key_aggregated_out_nbr_for_all_block_size % raft::warp_size() == 0); + auto const lane_id = tid % raft::warp_size(); + auto major_start_offset = static_cast(major_first - matrix_partition.get_major_first()); + size_t idx = static_cast(tid / raft::warp_size()); + + while (idx < static_cast(major_last - major_first)) { + auto major_offset = major_start_offset + idx; + auto major = + matrix_partition.get_major_from_major_offset_nocheck(static_cast(major_offset)); + vertex_t const* indices{nullptr}; + thrust::optional weights{nullptr}; + edge_t local_degree{}; + thrust::tie(indices, weights, local_degree) = matrix_partition.get_local_edges(major_offset); + auto local_offset = matrix_partition.get_local_offset(major_offset); + for (edge_t i = lane_id; i < local_degree; i += raft::warp_size()) { + majors[local_offset + i] = major; + } + idx += gridDim.x * (blockDim.x / raft::warp_size()); + } +} + +template +__global__ void for_all_major_for_all_nbr_high_degree( + matrix_partition_device_view_t matrix_partition, + vertex_t major_first, + vertex_t major_last, + vertex_t* majors) +{ + auto major_start_offset = static_cast(major_first - matrix_partition.get_major_first()); + size_t idx = static_cast(blockIdx.x); + + while (idx < static_cast(major_last - major_first)) { + auto major_offset = major_start_offset + idx; + auto major = + matrix_partition.get_major_from_major_offset_nocheck(static_cast(major_offset)); + vertex_t const* indices{nullptr}; + thrust::optional weights{nullptr}; + edge_t local_degree{}; + thrust::tie(indices, weights, local_degree) = + matrix_partition.get_local_edges(static_cast(major_offset)); + auto local_offset = matrix_partition.get_local_offset(major_offset); + for (edge_t i = threadIdx.x; i < local_degree; i += blockDim.x) { + majors[local_offset + i] = major; + } + idx += gridDim.x; + } +} + +template +void decompress_matrix_partition_to_fill_edgelist_majors( + raft::handle_t const& handle, + matrix_partition_device_view_t matrix_partition, + vertex_t* majors, + std::optional> const& segment_offsets) +{ + if (segment_offsets) { + // FIXME: we may further improve performance by 1) concurrently running kernels on different + // segments; 2) individually tuning block sizes for different segments; and 3) adding one more + // segment for very high degree vertices and running segmented reduction + static_assert(detail::num_sparse_segments_per_vertex_partition == 3); + if ((*segment_offsets)[1] > 0) { + raft::grid_1d_block_t update_grid( + (*segment_offsets)[1], + detail::copy_v_transform_reduce_key_aggregated_out_nbr_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + + detail::for_all_major_for_all_nbr_high_degree<<>>( + matrix_partition, + matrix_partition.get_major_first(), + matrix_partition.get_major_first() + (*segment_offsets)[1], + majors); + } + if ((*segment_offsets)[2] - (*segment_offsets)[1] > 0) { + raft::grid_1d_warp_t update_grid( + (*segment_offsets)[2] - (*segment_offsets)[1], + detail::copy_v_transform_reduce_key_aggregated_out_nbr_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + + detail::for_all_major_for_all_nbr_mid_degree<<>>( + matrix_partition, + matrix_partition.get_major_first() + (*segment_offsets)[1], + matrix_partition.get_major_first() + (*segment_offsets)[2], + majors); + } + if ((*segment_offsets)[3] - (*segment_offsets)[2] > 0) { + thrust::for_each( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + thrust::make_counting_iterator(matrix_partition.get_major_first()) + (*segment_offsets)[2], + thrust::make_counting_iterator(matrix_partition.get_major_first()) + (*segment_offsets)[3], + [matrix_partition, majors] __device__(auto major) { + auto major_offset = matrix_partition.get_major_offset_from_major_nocheck(major); + auto local_degree = matrix_partition.get_local_degree(major_offset); + auto local_offset = matrix_partition.get_local_offset(major_offset); + thrust::fill( + thrust::seq, majors + local_offset, majors + local_offset + local_degree, major); + }); + } + if (matrix_partition.get_dcs_nzd_vertex_count() && + (*(matrix_partition.get_dcs_nzd_vertex_count()) > 0)) { + thrust::for_each( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + thrust::make_counting_iterator(vertex_t{0}), + thrust::make_counting_iterator(*(matrix_partition.get_dcs_nzd_vertex_count())), + [matrix_partition, major_start_offset = (*segment_offsets)[3], majors] __device__( + auto idx) { + auto major = *(matrix_partition.get_major_from_major_hypersparse_idx_nocheck(idx)); + auto major_idx = + major_start_offset + idx; // major_offset != major_idx in the hypersparse region + auto local_degree = matrix_partition.get_local_degree(major_idx); + auto local_offset = matrix_partition.get_local_offset(major_idx); + thrust::fill( + thrust::seq, majors + local_offset, majors + local_offset + local_degree, major); + }); + } + } else { + thrust::for_each( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + thrust::make_counting_iterator(matrix_partition.get_major_first()), + thrust::make_counting_iterator(matrix_partition.get_major_first()) + + matrix_partition.get_major_size(), + [matrix_partition, majors] __device__(auto major) { + auto major_offset = matrix_partition.get_major_offset_from_major_nocheck(major); + auto local_degree = matrix_partition.get_local_degree(major_offset); + auto local_offset = matrix_partition.get_local_offset(major_offset); + thrust::fill( + thrust::seq, majors + local_offset, majors + local_offset + local_degree, major); + }); + } +} + +} // namespace detail + +/** + * @brief Iterate over every vertex's key-aggregated outgoing edges to update vertex properties. + * + * This function is inspired by thrust::transfrom_reduce() (iteration over the outgoing edges + * part) and thrust::copy() (update vertex properties part, take transform_reduce output as copy + * input). + * Unlike copy_v_transform_reduce_out_nbr, this function first aggregates outgoing edges by key to + * support two level reduction for every vertex. + * + * @tparam GraphViewType Type of the passed non-owning graph object. + * @tparam AdjMatrixRowValueInputIterator Type of the iterator for graph adjacency matrix row + * input properties. + * @tparam VertexIterator Type of the iterator for graph adjacency matrix column key values for + * aggregation (key type should coincide with vertex type). + * @tparam ValueIterator Type of the iterator for values in (key, value) pairs. + * @tparam KeyAggregatedEdgeOp Type of the quinary key-aggregated edge operator. + * @tparam ReduceOp Type of the binary reduction operator. + * @tparam T Type of the initial value for reduction over the key-aggregated outgoing edges. + * @tparam VertexValueOutputIterator Type of the iterator for vertex output property variables. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param graph_view Non-owning graph object. + * @param adj_matrix_row_value_input_first Iterator pointing to the adjacency matrix row input + * properties for the first (inclusive) row (assigned to this process in multi-GPU). + * `adj_matrix_row_value_input_last` (exclusive) is deduced as @p adj_matrix_row_value_input_first + * + @p graph_view.get_number_of_local_adj_matrix_partition_rows(). + * @param adj_matrix_col_key_first Iterator pointing to the adjacency matrix column key (for + * aggregation) for the first (inclusive) column (assigned to this process in multi-GPU). + * `adj_matrix_col_key_last` (exclusive) is deduced as @p adj_matrix_col_key_first + @p + * graph_view.get_number_of_local_adj_matrix_partition_cols(). + * @param map_key_first Iterator pointing to the first (inclusive) key in (key, value) pairs + * (assigned to this process in multi-GPU, + * `cugraph::experimental::detail::compute_gpu_id_from_vertex_t` is used to map keys to processes). + * (Key, value) pairs may be provided by transform_reduce_by_adj_matrix_row_key_e() or + * transform_reduce_by_adj_matrix_col_key_e(). + * @param map_key_last Iterator pointing to the last (exclusive) key in (key, value) pairs (assigned + * to this process in multi-GPU). + * @param map_value_first Iterator pointing to the first (inclusive) value in (key, value) pairs + * (assigned to this process in multi-GPU). `map_value_last` (exclusive) is deduced as @p + * map_value_first + thrust::distance(@p map_key_first, @p map_key_last). + * @param key_aggregated_e_op Quinary operator takes edge source, key, aggregated edge weight, *(@p + * adj_matrix_row_value_input_first + i), and value for the key stored in the input (key, value) + * pairs provided by @p map_key_first, @p map_key_last, and @p map_value_first (aggregated over the + * entire set of processes in multi-GPU). + * @param reduce_op Binary operator takes two input arguments and reduce the two variables to one. + * @param init Initial value to be added to the reduced @p reduce_op return values for each vertex. + * @param vertex_value_output_first Iterator pointing to the vertex property variables for the + * first (inclusive) vertex (assigned to tihs process in multi-GPU). `vertex_value_output_last` + * (exclusive) is deduced as @p vertex_value_output_first + @p + * graph_view.get_number_of_local_vertices(). + */ +template +void copy_v_transform_reduce_key_aggregated_out_nbr( + raft::handle_t const& handle, + GraphViewType const& graph_view, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + VertexIterator0 adj_matrix_col_key_first, + VertexIterator1 map_key_first, + VertexIterator1 map_key_last, + ValueIterator map_value_first, + KeyAggregatedEdgeOp key_aggregated_e_op, + ReduceOp reduce_op, + T init, + VertexValueOutputIterator vertex_value_output_first) +{ + static_assert(!GraphViewType::is_adj_matrix_transposed, + "GraphViewType should support the push model."); + static_assert(std::is_same::value_type, + typename GraphViewType::vertex_type>::value); + static_assert(std::is_same::value_type, + typename std::iterator_traits::value_type>::value); + static_assert(is_arithmetic_or_thrust_tuple_of_arithmetic::value); + + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; + using value_t = typename std::iterator_traits::value_type; + + double constexpr load_factor = 0.7; + + // 1. build a cuco::static_map object for the k, v pairs. + + auto poly_alloc = rmm::mr::polymorphic_allocator(rmm::mr::get_current_device_resource()); + auto stream_adapter = rmm::mr::make_stream_allocator_adaptor(poly_alloc, cudaStream_t{nullptr}); + auto kv_map_ptr = std::make_unique< + cuco::static_map>( + size_t{0}, + invalid_vertex_id::value, + invalid_vertex_id::value, + stream_adapter); + if (GraphViewType::is_multi_gpu) { + auto& comm = handle.get_comms(); + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_rank = row_comm.get_rank(); + auto const row_comm_size = row_comm.get_size(); + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (beginning of row_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + + auto map_counts = + host_scalar_allgather(row_comm, + static_cast(thrust::distance(map_key_first, map_key_last)), + handle.get_stream()); + std::vector map_displacements(row_comm_size, size_t{0}); + std::partial_sum(map_counts.begin(), map_counts.end() - 1, map_displacements.begin() + 1); + rmm::device_uvector map_keys(map_displacements.back() + map_counts.back(), + handle.get_stream()); + auto map_value_buffer = + allocate_dataframe_buffer(map_keys.size(), handle.get_stream()); + for (int i = 0; i < row_comm_size; ++i) { + device_bcast(row_comm, + map_key_first, + map_keys.begin() + map_displacements[i], + map_counts[i], + i, + handle.get_stream()); + device_bcast(row_comm, + map_value_first, + get_dataframe_buffer_begin(map_value_buffer) + map_displacements[i], + map_counts[i], + i, + handle.get_stream()); + } + // FIXME: these copies are unnecessary, better fix RAFT comm's bcast to take separate input & + // output pointers + thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + map_key_first, + map_key_last, + map_keys.begin() + map_displacements[row_comm_rank]); + thrust::copy( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + map_value_first, + map_value_first + thrust::distance(map_key_first, map_key_last), + get_dataframe_buffer_begin(map_value_buffer) + map_displacements[row_comm_rank]); + + handle.get_stream_view().synchronize(); // cuco::static_map currently does not take stream + + kv_map_ptr.reset(); + + kv_map_ptr = std::make_unique< + cuco::static_map>( + // cuco::static_map requires at least one empty slot + std::max(static_cast(static_cast(map_keys.size()) / load_factor), + static_cast(thrust::distance(map_key_first, map_key_last)) + 1), + invalid_vertex_id::value, + invalid_vertex_id::value, + stream_adapter); + + auto pair_first = thrust::make_zip_iterator( + thrust::make_tuple(map_keys.begin(), get_dataframe_buffer_begin(map_value_buffer))); + kv_map_ptr->insert(pair_first, pair_first + map_keys.size()); + } else { + handle.get_stream_view().synchronize(); // cuco::static_map currently does not take stream + + kv_map_ptr.reset(); + + kv_map_ptr = std::make_unique< + cuco::static_map>( + // cuco::static_map requires at least one empty slot + std::max(static_cast( + static_cast(thrust::distance(map_key_first, map_key_last)) / load_factor), + static_cast(thrust::distance(map_key_first, map_key_last)) + 1), + invalid_vertex_id::value, + invalid_vertex_id::value, + stream_adapter); + + auto pair_first = thrust::make_zip_iterator(thrust::make_tuple(map_key_first, map_value_first)); + kv_map_ptr->insert(pair_first, pair_first + thrust::distance(map_key_first, map_key_last)); + } + + // 2. aggregate each vertex out-going edges based on keys and transform-reduce. + + if (GraphViewType::is_multi_gpu) { + auto& comm = handle.get_comms(); + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (beginning of col_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + } + + rmm::device_uvector major_vertices(0, handle.get_stream()); + auto e_op_result_buffer = allocate_dataframe_buffer(0, handle.get_stream()); + for (size_t i = 0; i < graph_view.get_number_of_local_adj_matrix_partitions(); ++i) { + auto matrix_partition = + matrix_partition_device_view_t( + graph_view.get_matrix_partition_view(i)); + + rmm::device_uvector tmp_major_vertices(matrix_partition.get_number_of_edges(), + handle.get_stream()); + rmm::device_uvector tmp_minor_keys(tmp_major_vertices.size(), handle.get_stream()); + rmm::device_uvector tmp_key_aggregated_edge_weights( + graph_view.is_weighted() ? tmp_major_vertices.size() : size_t{0}, handle.get_stream()); + + if (matrix_partition.get_major_size() > 0) { + auto minor_key_first = thrust::make_transform_iterator( + matrix_partition.get_indices(), + detail::minor_to_key_t{adj_matrix_col_key_first, + matrix_partition.get_minor_first()}); + thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + minor_key_first, + minor_key_first + matrix_partition.get_number_of_edges(), + tmp_minor_keys.begin()); + if (graph_view.is_weighted()) { + thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + *(matrix_partition.get_weights()), + *(matrix_partition.get_weights()) + matrix_partition.get_number_of_edges(), + tmp_key_aggregated_edge_weights.begin()); + } + detail::decompress_matrix_partition_to_fill_edgelist_majors( + handle, + matrix_partition, + tmp_major_vertices.data(), + graph_view.get_local_adj_matrix_partition_segment_offsets(i)); + rmm::device_uvector reduced_major_vertices(tmp_major_vertices.size(), + handle.get_stream()); + rmm::device_uvector reduced_minor_keys(reduced_major_vertices.size(), + handle.get_stream()); + rmm::device_uvector reduced_key_aggregated_edge_weights( + reduced_major_vertices.size(), handle.get_stream()); + size_t reduced_size{}; + // FIXME: cub segmented sort may be more efficient as this is already sorted by major + auto input_key_first = thrust::make_zip_iterator( + thrust::make_tuple(tmp_major_vertices.begin(), tmp_minor_keys.begin())); + auto output_key_first = thrust::make_zip_iterator( + thrust::make_tuple(reduced_major_vertices.begin(), reduced_minor_keys.begin())); + if (graph_view.is_weighted()) { + thrust::sort_by_key(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + input_key_first, + input_key_first + tmp_major_vertices.size(), + tmp_key_aggregated_edge_weights.begin()); + reduced_size = + thrust::distance(output_key_first, + thrust::get<0>(thrust::reduce_by_key( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + input_key_first, + input_key_first + tmp_major_vertices.size(), + tmp_key_aggregated_edge_weights.begin(), + output_key_first, + reduced_key_aggregated_edge_weights.begin()))); + } else { + thrust::sort(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + input_key_first, + input_key_first + tmp_major_vertices.size()); + reduced_size = + thrust::distance(output_key_first, + thrust::get<0>(thrust::reduce_by_key( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + input_key_first, + input_key_first + tmp_major_vertices.size(), + thrust::make_constant_iterator(weight_t{1.0}), + output_key_first, + reduced_key_aggregated_edge_weights.begin()))); + } + tmp_major_vertices = std::move(reduced_major_vertices); + tmp_minor_keys = std::move(reduced_minor_keys); + tmp_key_aggregated_edge_weights = std::move(reduced_key_aggregated_edge_weights); + tmp_major_vertices.resize(reduced_size, handle.get_stream()); + tmp_minor_keys.resize(tmp_major_vertices.size(), handle.get_stream()); + tmp_key_aggregated_edge_weights.resize(tmp_major_vertices.size(), handle.get_stream()); + tmp_major_vertices.shrink_to_fit(handle.get_stream()); + tmp_minor_keys.shrink_to_fit(handle.get_stream()); + tmp_key_aggregated_edge_weights.shrink_to_fit(handle.get_stream()); + } + + if (GraphViewType::is_multi_gpu) { + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_size = row_comm.get_size(); + + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_size = col_comm.get_size(); + + auto triplet_first = + thrust::make_zip_iterator(thrust::make_tuple(tmp_major_vertices.begin(), + tmp_minor_keys.begin(), + tmp_key_aggregated_edge_weights.begin())); + rmm::device_uvector rx_major_vertices(0, handle.get_stream()); + rmm::device_uvector rx_minor_keys(0, handle.get_stream()); + rmm::device_uvector rx_key_aggregated_edge_weights(0, handle.get_stream()); + std::forward_as_tuple( + std::tie(rx_major_vertices, rx_minor_keys, rx_key_aggregated_edge_weights), std::ignore) = + groupby_gpuid_and_shuffle_values( + col_comm, + triplet_first, + triplet_first + tmp_major_vertices.size(), + [key_func = detail::compute_gpu_id_from_vertex_t{comm_size}, + row_comm_size] __device__(auto val) { + return key_func(thrust::get<1>(val)) / row_comm_size; + }, + handle.get_stream()); + + auto pair_first = thrust::make_zip_iterator( + thrust::make_tuple(rx_major_vertices.begin(), rx_minor_keys.begin())); + thrust::sort_by_key(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + pair_first, + pair_first + rx_major_vertices.size(), + rx_key_aggregated_edge_weights.begin()); + tmp_major_vertices.resize(rx_major_vertices.size(), handle.get_stream()); + tmp_minor_keys.resize(tmp_major_vertices.size(), handle.get_stream()); + tmp_key_aggregated_edge_weights.resize(tmp_major_vertices.size(), handle.get_stream()); + auto pair_it = + thrust::reduce_by_key(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + pair_first, + pair_first + rx_major_vertices.size(), + rx_key_aggregated_edge_weights.begin(), + thrust::make_zip_iterator(thrust::make_tuple( + tmp_major_vertices.begin(), tmp_minor_keys.begin())), + tmp_key_aggregated_edge_weights.begin()); + tmp_major_vertices.resize( + thrust::distance(tmp_key_aggregated_edge_weights.begin(), thrust::get<1>(pair_it)), + handle.get_stream()); + tmp_minor_keys.resize(tmp_major_vertices.size(), handle.get_stream()); + tmp_key_aggregated_edge_weights.resize(tmp_major_vertices.size(), handle.get_stream()); + tmp_major_vertices.shrink_to_fit(handle.get_stream()); + tmp_minor_keys.shrink_to_fit(handle.get_stream()); + tmp_key_aggregated_edge_weights.shrink_to_fit(handle.get_stream()); + } + + auto tmp_e_op_result_buffer = + allocate_dataframe_buffer(tmp_major_vertices.size(), handle.get_stream()); + auto tmp_e_op_result_buffer_first = get_dataframe_buffer_begin(tmp_e_op_result_buffer); + + auto triplet_first = thrust::make_zip_iterator(thrust::make_tuple( + tmp_major_vertices.begin(), tmp_minor_keys.begin(), tmp_key_aggregated_edge_weights.begin())); + thrust::transform( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + triplet_first, + triplet_first + tmp_major_vertices.size(), + tmp_e_op_result_buffer_first, + [adj_matrix_row_value_input_first = + adj_matrix_row_value_input_first + matrix_partition.get_major_value_start_offset(), + key_aggregated_e_op, + matrix_partition, + kv_map = kv_map_ptr->get_device_view()] __device__(auto val) { + auto major = thrust::get<0>(val); + auto key = thrust::get<1>(val); + auto w = thrust::get<2>(val); + return key_aggregated_e_op(major, + key, + w, + *(adj_matrix_row_value_input_first + + matrix_partition.get_major_offset_from_major_nocheck(major)), + kv_map.find(key)->second.load(cuda::std::memory_order_relaxed)); + }); + tmp_minor_keys.resize(0, handle.get_stream()); + tmp_key_aggregated_edge_weights.resize(0, handle.get_stream()); + tmp_minor_keys.shrink_to_fit(handle.get_stream()); + tmp_key_aggregated_edge_weights.shrink_to_fit(handle.get_stream()); + + if (GraphViewType::is_multi_gpu) { + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_rank = col_comm.get_rank(); + auto const col_comm_size = col_comm.get_size(); + + // FIXME: additional optimization is possible if reduce_op is a pure function (and reduce_op + // can be mapped to ncclRedOp_t). + + auto rx_sizes = + host_scalar_gather(col_comm, tmp_major_vertices.size(), i, handle.get_stream()); + std::vector rx_displs{}; + rmm::device_uvector rx_major_vertices(0, handle.get_stream()); + if (static_cast(col_comm_rank) == i) { + rx_displs.assign(col_comm_size, size_t{0}); + std::partial_sum(rx_sizes.begin(), rx_sizes.end() - 1, rx_displs.begin() + 1); + rx_major_vertices.resize(rx_displs.back() + rx_sizes.back(), handle.get_stream()); + } + auto rx_tmp_e_op_result_buffer = + allocate_dataframe_buffer(rx_major_vertices.size(), handle.get_stream()); + + device_gatherv(col_comm, + tmp_major_vertices.data(), + rx_major_vertices.data(), + tmp_major_vertices.size(), + rx_sizes, + rx_displs, + i, + handle.get_stream()); + device_gatherv(col_comm, + tmp_e_op_result_buffer_first, + get_dataframe_buffer_begin(rx_tmp_e_op_result_buffer), + tmp_major_vertices.size(), + rx_sizes, + rx_displs, + i, + handle.get_stream()); + + if (static_cast(col_comm_rank) == i) { + major_vertices = std::move(rx_major_vertices); + e_op_result_buffer = std::move(rx_tmp_e_op_result_buffer); + } + } else { + major_vertices = std::move(tmp_major_vertices); + e_op_result_buffer = std::move(tmp_e_op_result_buffer); + } + } + + if (GraphViewType::is_multi_gpu) { + auto& comm = handle.get_comms(); + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (beginning of col_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + } + + thrust::fill(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + vertex_value_output_first, + vertex_value_output_first + graph_view.get_number_of_local_vertices(), + T{}); + thrust::sort_by_key(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + major_vertices.begin(), + major_vertices.end(), + get_dataframe_buffer_begin(e_op_result_buffer)); + + auto num_uniques = thrust::count_if( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + thrust::make_counting_iterator(size_t{0}), + thrust::make_counting_iterator(major_vertices.size()), + [major_vertices = major_vertices.data()] __device__(auto i) { + return ((i == 0) || (major_vertices[i] != major_vertices[i - 1])) ? true : false; + }); + rmm::device_uvector unique_major_vertices(num_uniques, handle.get_stream()); + + auto major_vertex_first = thrust::make_transform_iterator( + thrust::make_counting_iterator(size_t{0}), + [major_vertices = major_vertices.data()] __device__(auto i) { + return ((i == 0) || (major_vertices[i] != major_vertices[i - 1])) + ? major_vertices[i] + : invalid_vertex_id::value; + }); + thrust::copy_if( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + major_vertex_first, + major_vertex_first + major_vertices.size(), + unique_major_vertices.begin(), + [] __device__(auto major) { return major != invalid_vertex_id::value; }); + thrust::reduce_by_key( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + major_vertices.begin(), + major_vertices.end(), + get_dataframe_buffer_begin(e_op_result_buffer), + thrust::make_discard_iterator(), + thrust::make_permutation_iterator( + vertex_value_output_first, + thrust::make_transform_iterator( + unique_major_vertices.begin(), + [vertex_partition = vertex_partition_device_view_t( + graph_view.get_vertex_partition_view())] __device__(auto v) { + return vertex_partition.get_local_vertex_offset_from_vertex_nocheck(v); + })), + thrust::equal_to{}, + reduce_op); + + thrust::transform(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + vertex_value_output_first, + vertex_value_output_first + graph_view.get_number_of_local_vertices(), + vertex_value_output_first, + [reduce_op, init] __device__(auto val) { return reduce_op(val, init); }); +} + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/include/cugraph/prims/count_if_e.cuh b/cpp/include/cugraph/prims/count_if_e.cuh new file mode 100644 index 00000000000..cfbb81d9bc9 --- /dev/null +++ b/cpp/include/cugraph/prims/count_if_e.cuh @@ -0,0 +1,86 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include +#include + +#include + +#include + +namespace cugraph { +namespace experimental { + +/** + * @brief Count the number of edges that satisfies the given predicate. + * + * This function is inspired by thrust::count_if(). + * + * @tparam GraphViewType Type of the passed non-owning graph object. + * @tparam AdjMatrixRowValueInputIterator Type of the iterator for graph adjacency matrix row + * input properties. + * @tparam AdjMatrixColValueInputIterator Type of the iterator for graph adjacency matrix column + * input properties. + * @tparam EdgeOp Type of the quaternary (or quinary) edge operator. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param graph_view Non-owning graph object. + * @param adj_matrix_row_value_input_first Iterator pointing to the adjacency matrix row input + * properties for the first (inclusive) row (assigned to this process in multi-GPU). + * `adj_matrix_row_value_input_last` (exclusive) is deduced as @p adj_matrix_row_value_input_first + + * @p graph_view.get_number_of_local_adj_matrix_partition_rows(). + * @param adj_matrix_col_value_input_first Iterator pointing to the adjacency matrix column input + * properties for the first (inclusive) column (assigned to this process in multi-GPU). + * `adj_matrix_col_value_output_last` (exclusive) is deduced as @p adj_matrix_col_value_output_first + * + @p graph_view.get_number_of_local_adj_matrix_partition_cols(). + * @param e_op Quaternary (or quinary) operator takes edge source, edge destination, (optional edge + * weight), *(@p adj_matrix_row_value_input_first + i), and *(@p adj_matrix_col_value_input_first + + * j) (where i is in [0, graph_view.get_number_of_local_adj_matrix_partition_rows()) and j is in [0, + * get_number_of_local_adj_matrix_partition_cols())) and returns true if this edge should be + * included in the returned count. + * @return GraphViewType::edge_type Number of times @p e_op returned true. + */ +template +typename GraphViewType::edge_type count_if_e( + raft::handle_t const& handle, + GraphViewType const& graph_view, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + EdgeOp e_op) +{ + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + + return transform_reduce_e(handle, + graph_view, + adj_matrix_row_value_input_first, + adj_matrix_col_value_input_first, + cast_edge_op_bool_to_integer{e_op}, + edge_t{0}); +} + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/include/patterns/count_if_v.cuh b/cpp/include/cugraph/prims/count_if_v.cuh similarity index 96% rename from cpp/include/patterns/count_if_v.cuh rename to cpp/include/cugraph/prims/count_if_v.cuh index c90b259cdde..ef49a3e463b 100644 --- a/cpp/include/patterns/count_if_v.cuh +++ b/cpp/include/cugraph/prims/count_if_v.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -15,9 +15,9 @@ */ #pragma once -#include -#include -#include +#include +#include +#include #include #include diff --git a/cpp/include/patterns/edge_op_utils.cuh b/cpp/include/cugraph/prims/property_op_utils.cuh similarity index 58% rename from cpp/include/patterns/edge_op_utils.cuh rename to cpp/include/cugraph/prims/property_op_utils.cuh index 58fb31c7605..ec3ed788cc1 100644 --- a/cpp/include/patterns/edge_op_utils.cuh +++ b/cpp/include/cugraph/prims/property_op_utils.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -15,7 +15,7 @@ */ #pragma once -#include +#include #include @@ -30,19 +30,20 @@ namespace cugraph { namespace experimental { -template +template struct is_valid_edge_op { static constexpr bool value = false; }; -template +template struct is_valid_edge_op< - ResultOfEdgeOp, - typename std::conditional::type> { + InvokeResultEdgeOp, + typename std::conditional_t> { static constexpr bool valid = true; }; template @@ -52,44 +53,98 @@ struct evaluate_edge_op { using row_value_type = typename std::iterator_traits::value_type; using col_value_type = typename std::iterator_traits::value_type; - template - __device__ std::enable_if_t>::valid, - typename std::result_of::type> - compute(V r, V c, W w, R rv, C cv, E e) + __device__ + std::enable_if_t>::valid, + typename std::invoke_result::type> + compute(K r, V c, W w, R rv, C cv, E e) { return e(r, c, w, rv, cv); } - template - __device__ std::enable_if_t>::valid, - typename std::result_of::type> - compute(V r, V c, W w, R rv, C cv, E e) + __device__ std::enable_if_t>::valid, + typename std::invoke_result::type> + compute(K r, V c, W w, R rv, C cv, E e) { return e(r, c, rv, cv); } }; -template -__host__ __device__ std::enable_if_t::value, T> plus_edge_op_result( - T const& lhs, T const& rhs) -{ - return lhs + rhs; -} +template +struct cast_edge_op_bool_to_integer { + static_assert(std::is_integral::value); + using vertex_type = typename GraphViewType::vertex_type; + using weight_type = typename GraphViewType::weight_type; + using row_value_type = typename std::iterator_traits::value_type; + using col_value_type = typename std::iterator_traits::value_type; + + EdgeOp e_op{}; + + template + __device__ + std::enable_if_t>::valid, T> + operator()(K r, V c, W w, R rv, C cv) + { + return e_op(r, c, w, rv, cv) ? T{1} : T{0}; + } + + template + __device__ + std::enable_if_t>::valid, T> + operator()(K r, V c, R rv, C cv) + { + return e_op(r, c, rv, cv) ? T{1} : T{0}; + } +}; template -__host__ __device__ std::enable_if_t::value, T> plus_edge_op_result(T const& lhs, - T const& rhs) -{ - return plus_thrust_tuple()(lhs, rhs); -} +struct property_add : public thrust::plus { +}; + +template +struct property_add> + : public thrust:: + binary_function, thrust::tuple, thrust::tuple> { + using Type = thrust::tuple; + + private: + template + __device__ constexpr auto sum_impl(T& t1, T& t2, std::index_sequence) + { + return thrust::make_tuple((thrust::get(t1) + thrust::get(t2))...); + } + + public: + __device__ constexpr auto operator()(const Type& t1, const Type& t2) + { + return sum_impl(t1, t2, std::make_index_sequence::value>()); + } +}; template __device__ std::enable_if_t::value, void> diff --git a/cpp/include/patterns/reduce_op.cuh b/cpp/include/cugraph/prims/reduce_op.cuh similarity index 61% rename from cpp/include/patterns/reduce_op.cuh rename to cpp/include/cugraph/prims/reduce_op.cuh index e9011914292..e73a2861cb0 100644 --- a/cpp/include/patterns/reduce_op.cuh +++ b/cpp/include/cugraph/prims/reduce_op.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -20,20 +20,34 @@ namespace cugraph { namespace experimental { namespace reduce_op { +// in case there is no payload to reduce +struct null { + using type = void; +}; + // reducing N elements, any element can be a valid output. template struct any { - using type = T; + using type = T; + // FIXME: actually every reduction operation should be side-effect free if reduction is performed + // by thrust; thrust reduction call rounds up the number of invocations based on the block size + // and discards the values outside the valid range; this does not work if the reduction operation + // has side-effects. static constexpr bool pure_function = true; // this can be called in any process __host__ __device__ T operator()(T const& lhs, T const& rhs) const { return lhs; } }; +// FIXME: thrust::minimum can replace this. // reducing N elements (operator < should be defined between any two elements), the minimum element // should be selected. template struct min { - using type = T; + using type = T; + // FIXME: actually every reduction operation should be side-effect free if reduction is performed + // by thrust; thrust reduction call rounds up the number of invocations based on the block size + // and discards the values outside the valid range; this does not work if the reduction operation + // has side-effects. static constexpr bool pure_function = true; // this can be called in any process __host__ __device__ T operator()(T const& lhs, T const& rhs) const diff --git a/cpp/include/patterns/reduce_v.cuh b/cpp/include/cugraph/prims/reduce_v.cuh similarity index 83% rename from cpp/include/patterns/reduce_v.cuh rename to cpp/include/cugraph/prims/reduce_v.cuh index 12224dc55f4..c7c504942d1 100644 --- a/cpp/include/patterns/reduce_v.cuh +++ b/cpp/include/cugraph/prims/reduce_v.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -15,9 +15,10 @@ */ #pragma once -#include -#include -#include +#include +#include +#include +#include #include @@ -51,10 +52,12 @@ T reduce_v(raft::handle_t const& handle, VertexValueInputIterator vertex_value_input_first, T init) { - auto ret = thrust::reduce(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - vertex_value_input_first, - vertex_value_input_first + graph_view.get_number_of_local_vertices(), - init); + auto ret = thrust::reduce( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + vertex_value_input_first, + vertex_value_input_first + graph_view.get_number_of_local_vertices(), + ((GraphViewType::is_multi_gpu) && (handle.get_comms().get_rank() == 0)) ? init : T{}, + property_add()); if (GraphViewType::is_multi_gpu) { ret = host_scalar_allreduce(handle.get_comms(), ret, handle.get_stream()); } @@ -87,7 +90,11 @@ T reduce_v(raft::handle_t const& handle, T init) { auto ret = thrust::reduce( - rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), input_first, input_last, init); + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + input_first, + input_last, + ((GraphViewType::is_multi_gpu) && (handle.get_comms().get_rank() == 0)) ? init : T{}, + property_add()); if (GraphViewType::is_multi_gpu) { ret = host_scalar_allreduce(handle.get_comms(), ret, handle.get_stream()); } diff --git a/cpp/include/cugraph/prims/transform_reduce_by_adj_matrix_row_col_key_e.cuh b/cpp/include/cugraph/prims/transform_reduce_by_adj_matrix_row_col_key_e.cuh new file mode 100644 index 00000000000..c1887433fd1 --- /dev/null +++ b/cpp/include/cugraph/prims/transform_reduce_by_adj_matrix_row_col_key_e.cuh @@ -0,0 +1,699 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +namespace cugraph { +namespace experimental { + +namespace detail { + +// FIXME: block size requires tuning +int32_t constexpr transform_reduce_by_adj_matrix_row_col_key_e_for_all_block_size = 128; + +template +__device__ void update_buffer_element( + matrix_partition_device_view_t& matrix_partition, + typename GraphViewType::vertex_type major, + typename GraphViewType::vertex_type minor, + typename GraphViewType::weight_type weight, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + VertexIterator adj_matrix_row_col_key_first, + EdgeOp e_op, + typename GraphViewType::vertex_type* key, + T* value) +{ + using vertex_t = typename GraphViewType::vertex_type; + + auto major_offset = matrix_partition.get_major_offset_from_major_nocheck(major); + auto minor_offset = matrix_partition.get_minor_offset_from_minor_nocheck(minor); + auto row = GraphViewType::is_adj_matrix_transposed ? minor : major; + auto col = GraphViewType::is_adj_matrix_transposed ? major : minor; + auto row_offset = GraphViewType::is_adj_matrix_transposed ? minor_offset : major_offset; + auto col_offset = GraphViewType::is_adj_matrix_transposed ? major_offset : minor_offset; + + *key = *(adj_matrix_row_col_key_first + + ((GraphViewType::is_adj_matrix_transposed != adj_matrix_row_key) ? major_offset + : minor_offset)); + *value = evaluate_edge_op() + .compute(row, + col, + weight, + *(adj_matrix_row_value_input_first + row_offset), + *(adj_matrix_col_value_input_first + col_offset), + e_op); +} + +template +__global__ void for_all_major_for_all_nbr_hypersparse( + matrix_partition_device_view_t matrix_partition, + typename GraphViewType::vertex_type major_hypersparse_first, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + VertexIterator adj_matrix_row_col_key_first, + EdgeOp e_op, + typename GraphViewType::vertex_type* keys, + T* values) +{ + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; + + auto const tid = threadIdx.x + blockIdx.x * blockDim.x; + auto major_start_offset = + static_cast(major_hypersparse_first - matrix_partition.get_major_first()); + auto idx = static_cast(tid); + + auto dcs_nzd_vertex_count = *(matrix_partition.get_dcs_nzd_vertex_count()); + + while (idx < static_cast(dcs_nzd_vertex_count)) { + auto major = + *(matrix_partition.get_major_from_major_hypersparse_idx_nocheck(static_cast(idx))); + auto major_idx = + major_start_offset + idx; // major_offset != major_idx in the hypersparse region + vertex_t const* indices{nullptr}; + thrust::optional weights{nullptr}; + edge_t local_degree{}; + thrust::tie(indices, weights, local_degree) = + matrix_partition.get_local_edges(static_cast(major_idx)); + auto local_offset = matrix_partition.get_local_offset(major_idx); + for (edge_t i = 0; i < local_degree; ++i) { + update_buffer_element( + matrix_partition, + major, + indices[i], + weights ? (*weights)[i] : weight_t{1.0}, + adj_matrix_row_value_input_first, + adj_matrix_col_value_input_first, + adj_matrix_row_col_key_first, + e_op, + keys + local_offset + i, + values + local_offset + i); + } + + idx += gridDim.x * blockDim.x; + } +} + +template +__global__ void for_all_major_for_all_nbr_low_degree( + matrix_partition_device_view_t matrix_partition, + typename GraphViewType::vertex_type major_first, + typename GraphViewType::vertex_type major_last, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + VertexIterator adj_matrix_row_col_key_first, + EdgeOp e_op, + typename GraphViewType::vertex_type* keys, + T* values) +{ + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; + + auto const tid = threadIdx.x + blockIdx.x * blockDim.x; + auto major_start_offset = static_cast(major_first - matrix_partition.get_major_first()); + auto idx = static_cast(tid); + + while (idx < static_cast(major_last - major_first)) { + auto major_offset = major_start_offset + idx; + auto major = + matrix_partition.get_major_from_major_offset_nocheck(static_cast(major_offset)); + vertex_t const* indices{nullptr}; + thrust::optional weights{nullptr}; + edge_t local_degree{}; + thrust::tie(indices, weights, local_degree) = + matrix_partition.get_local_edges(static_cast(major_offset)); + auto local_offset = matrix_partition.get_local_offset(major_offset); + for (edge_t i = 0; i < local_degree; ++i) { + update_buffer_element( + matrix_partition, + major, + indices[i], + weights ? (*weights)[i] : weight_t{1.0}, + adj_matrix_row_value_input_first, + adj_matrix_col_value_input_first, + adj_matrix_row_col_key_first, + e_op, + keys + local_offset + i, + values + local_offset + i); + } + + idx += gridDim.x * blockDim.x; + } +} + +template +__global__ void for_all_major_for_all_nbr_mid_degree( + matrix_partition_device_view_t matrix_partition, + typename GraphViewType::vertex_type major_first, + typename GraphViewType::vertex_type major_last, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + VertexIterator adj_matrix_row_col_key_first, + EdgeOp e_op, + typename GraphViewType::vertex_type* keys, + T* values) +{ + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; + + auto const tid = threadIdx.x + blockIdx.x * blockDim.x; + static_assert( + transform_reduce_by_adj_matrix_row_col_key_e_for_all_block_size % raft::warp_size() == 0); + auto const lane_id = tid % raft::warp_size(); + auto major_start_offset = static_cast(major_first - matrix_partition.get_major_first()); + size_t idx = static_cast(tid / raft::warp_size()); + + while (idx < static_cast(major_last - major_first)) { + auto major_offset = major_start_offset + idx; + auto major = + matrix_partition.get_major_from_major_offset_nocheck(static_cast(major_offset)); + vertex_t const* indices{nullptr}; + thrust::optional weights{nullptr}; + edge_t local_degree{}; + thrust::tie(indices, weights, local_degree) = + matrix_partition.get_local_edges(static_cast(major_offset)); + auto local_offset = matrix_partition.get_local_offset(major_offset); + for (edge_t i = lane_id; i < local_degree; i += raft::warp_size()) { + update_buffer_element( + matrix_partition, + major, + indices[i], + weights ? (*weights)[i] : weight_t{1.0}, + adj_matrix_row_value_input_first, + adj_matrix_col_value_input_first, + adj_matrix_row_col_key_first, + e_op, + keys + local_offset + i, + values + local_offset + i); + } + + idx += gridDim.x * (blockDim.x / raft::warp_size()); + } +} + +template +__global__ void for_all_major_for_all_nbr_high_degree( + matrix_partition_device_view_t matrix_partition, + typename GraphViewType::vertex_type major_first, + typename GraphViewType::vertex_type major_last, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + VertexIterator adj_matrix_row_col_key_first, + EdgeOp e_op, + typename GraphViewType::vertex_type* keys, + T* values) +{ + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; + + auto major_start_offset = static_cast(major_first - matrix_partition.get_major_first()); + auto idx = static_cast(blockIdx.x); + + while (idx < static_cast(major_last - major_first)) { + auto major_offset = major_start_offset + idx; + auto major = + matrix_partition.get_major_from_major_offset_nocheck(static_cast(major_offset)); + vertex_t const* indices{nullptr}; + thrust::optional weights{nullptr}; + edge_t local_degree{}; + thrust::tie(indices, weights, local_degree) = + matrix_partition.get_local_edges(static_cast(major_offset)); + auto local_offset = matrix_partition.get_local_offset(major_offset); + for (edge_t i = threadIdx.x; i < local_degree; i += blockDim.x) { + update_buffer_element( + matrix_partition, + major, + indices[i], + weights ? (*weights)[i] : weight_t{1.0}, + adj_matrix_row_value_input_first, + adj_matrix_col_value_input_first, + adj_matrix_row_col_key_first, + e_op, + keys + local_offset + i, + values + local_offset + i); + } + + idx += gridDim.x; + } +} + +// FIXME: better derive value_t from BufferType +template +std::tuple, BufferType> reduce_to_unique_kv_pairs( + rmm::device_uvector&& keys, BufferType&& value_buffer, cudaStream_t stream) +{ + thrust::sort_by_key(rmm::exec_policy(stream)->on(stream), + keys.begin(), + keys.end(), + get_dataframe_buffer_begin(value_buffer)); + auto num_uniques = + thrust::count_if(rmm::exec_policy(stream)->on(stream), + thrust::make_counting_iterator(size_t{0}), + thrust::make_counting_iterator(keys.size()), + [keys = keys.data()] __device__(auto i) { + return ((i == 0) || (keys[i] != keys[i - 1])) ? true : false; + }); + + rmm::device_uvector unique_keys(num_uniques, stream); + auto value_for_unique_key_buffer = allocate_dataframe_buffer(unique_keys.size(), stream); + thrust::reduce_by_key(rmm::exec_policy(stream)->on(stream), + keys.begin(), + keys.end(), + get_dataframe_buffer_begin(value_buffer), + unique_keys.begin(), + get_dataframe_buffer_begin(value_for_unique_key_buffer)); + + return std::make_tuple(std::move(unique_keys), std::move(value_for_unique_key_buffer)); +} + +template +std::tuple, + decltype(allocate_dataframe_buffer(0, cudaStream_t{nullptr}))> +transform_reduce_by_adj_matrix_row_col_key_e( + raft::handle_t const& handle, + GraphViewType const& graph_view, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + VertexIterator adj_matrix_row_col_key_first, + EdgeOp e_op, + T init) +{ + static_assert(is_arithmetic_or_thrust_tuple_of_arithmetic::value); + static_assert(std::is_same::value_type, + typename GraphViewType::vertex_type>::value); + + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; + + rmm::device_uvector keys(0, handle.get_stream()); + auto value_buffer = allocate_dataframe_buffer(0, handle.get_stream()); + for (size_t i = 0; i < graph_view.get_number_of_local_adj_matrix_partitions(); ++i) { + auto matrix_partition = + matrix_partition_device_view_t( + graph_view.get_matrix_partition_view(i)); + + int comm_root_rank = 0; + if (GraphViewType::is_multi_gpu) { + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_rank = row_comm.get_rank(); + auto const row_comm_size = row_comm.get_size(); + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_rank = col_comm.get_rank(); + comm_root_rank = i * row_comm_size + row_comm_rank; + } + + auto num_edges = matrix_partition.get_number_of_edges(); + + rmm::device_uvector tmp_keys(num_edges, handle.get_stream()); + auto tmp_value_buffer = allocate_dataframe_buffer(tmp_keys.size(), handle.get_stream()); + + if (graph_view.get_vertex_partition_size(comm_root_rank) > 0) { + auto row_value_input_offset = GraphViewType::is_adj_matrix_transposed + ? vertex_t{0} + : matrix_partition.get_major_value_start_offset(); + auto col_value_input_offset = GraphViewType::is_adj_matrix_transposed + ? matrix_partition.get_major_value_start_offset() + : vertex_t{0}; + auto segment_offsets = graph_view.get_local_adj_matrix_partition_segment_offsets(i); + if (segment_offsets) { + // FIXME: we may further improve performance by 1) concurrently running kernels on different + // segments; 2) individually tuning block sizes for different segments; and 3) adding one + // more segment for very high degree vertices and running segmented reduction + static_assert(detail::num_sparse_segments_per_vertex_partition == 3); + if ((*segment_offsets)[1] > 0) { + raft::grid_1d_block_t update_grid( + (*segment_offsets)[1], + detail::transform_reduce_by_adj_matrix_row_col_key_e_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + detail::for_all_major_for_all_nbr_high_degree + <<>>( + matrix_partition, + matrix_partition.get_major_first(), + matrix_partition.get_major_first() + (*segment_offsets)[1], + adj_matrix_row_value_input_first + row_value_input_offset, + adj_matrix_col_value_input_first + col_value_input_offset, + adj_matrix_row_col_key_first + + (adj_matrix_row_key ? row_value_input_offset : col_value_input_offset), + e_op, + tmp_keys.data(), + get_dataframe_buffer_begin(tmp_value_buffer)); + } + if ((*segment_offsets)[2] - (*segment_offsets)[1] > 0) { + raft::grid_1d_warp_t update_grid( + (*segment_offsets)[2] - (*segment_offsets)[1], + detail::transform_reduce_by_adj_matrix_row_col_key_e_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + detail::for_all_major_for_all_nbr_mid_degree + <<>>( + matrix_partition, + matrix_partition.get_major_first() + (*segment_offsets)[1], + matrix_partition.get_major_first() + (*segment_offsets)[2], + adj_matrix_row_value_input_first + row_value_input_offset, + adj_matrix_col_value_input_first + col_value_input_offset, + adj_matrix_row_col_key_first + + (adj_matrix_row_key ? row_value_input_offset : col_value_input_offset), + e_op, + tmp_keys.data(), + get_dataframe_buffer_begin(tmp_value_buffer)); + } + if ((*segment_offsets)[3] - (*segment_offsets)[2] > 0) { + raft::grid_1d_thread_t update_grid( + (*segment_offsets)[3] - (*segment_offsets)[2], + detail::transform_reduce_by_adj_matrix_row_col_key_e_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + detail::for_all_major_for_all_nbr_low_degree + <<>>( + matrix_partition, + matrix_partition.get_major_first() + (*segment_offsets)[2], + matrix_partition.get_major_first() + (*segment_offsets)[3], + adj_matrix_row_value_input_first + row_value_input_offset, + adj_matrix_col_value_input_first + col_value_input_offset, + adj_matrix_row_col_key_first + + (adj_matrix_row_key ? row_value_input_offset : col_value_input_offset), + e_op, + tmp_keys.data(), + get_dataframe_buffer_begin(tmp_value_buffer)); + } + if (matrix_partition.get_dcs_nzd_vertex_count() && + (*(matrix_partition.get_dcs_nzd_vertex_count()) > 0)) { + raft::grid_1d_thread_t update_grid( + *(matrix_partition.get_dcs_nzd_vertex_count()), + detail::transform_reduce_by_adj_matrix_row_col_key_e_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + detail::for_all_major_for_all_nbr_hypersparse + <<>>( + matrix_partition, + matrix_partition.get_major_first() + (*segment_offsets)[3], + adj_matrix_row_value_input_first + row_value_input_offset, + adj_matrix_col_value_input_first + col_value_input_offset, + adj_matrix_row_col_key_first + + (adj_matrix_row_key ? row_value_input_offset : col_value_input_offset), + e_op, + tmp_keys.data(), + get_dataframe_buffer_begin(tmp_value_buffer)); + } + } else { + raft::grid_1d_thread_t update_grid( + matrix_partition.get_major_size(), + detail::transform_reduce_by_adj_matrix_row_col_key_e_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + + detail::for_all_major_for_all_nbr_low_degree + <<>>( + matrix_partition, + matrix_partition.get_major_first(), + matrix_partition.get_major_last(), + adj_matrix_row_value_input_first + row_value_input_offset, + adj_matrix_col_value_input_first + col_value_input_offset, + adj_matrix_row_col_key_first + + (adj_matrix_row_key ? row_value_input_offset : col_value_input_offset), + e_op, + tmp_keys.data(), + get_dataframe_buffer_begin(tmp_value_buffer)); + } + } + std::tie(tmp_keys, tmp_value_buffer) = reduce_to_unique_kv_pairs( + std::move(tmp_keys), std::move(tmp_value_buffer), handle.get_stream()); + + if (GraphViewType::is_multi_gpu) { + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + + rmm::device_uvector rx_unique_keys(0, handle.get_stream()); + auto rx_value_for_unique_key_buffer = allocate_dataframe_buffer(0, handle.get_stream()); + std::tie(rx_unique_keys, rx_value_for_unique_key_buffer, std::ignore) = + groupby_gpuid_and_shuffle_kv_pairs( + comm, + tmp_keys.begin(), + tmp_keys.end(), + get_dataframe_buffer_begin(tmp_value_buffer), + [key_func = detail::compute_gpu_id_from_vertex_t{comm_size}] __device__( + auto val) { return key_func(val); }, + handle.get_stream()); + + std::tie(tmp_keys, tmp_value_buffer) = reduce_to_unique_kv_pairs( + std::move(rx_unique_keys), std::move(rx_value_for_unique_key_buffer), handle.get_stream()); + } + + auto cur_size = keys.size(); + if (cur_size == 0) { + keys = std::move(tmp_keys); + value_buffer = std::move(tmp_value_buffer); + } else { + // FIXME: this can lead to frequent costly reallocation; we may be able to avoid this if we + // can reserve address space to avoid expensive reallocation. + // https://devblogs.nvidia.com/introducing-low-level-gpu-virtual-memory-management + keys.resize(cur_size + tmp_keys.size(), handle.get_stream()); + resize_dataframe_buffer(value_buffer, keys.size(), handle.get_stream()); + + thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + tmp_keys.begin(), + tmp_keys.end(), + keys.begin() + cur_size); + thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + get_dataframe_buffer_begin(tmp_value_buffer), + get_dataframe_buffer_begin(tmp_value_buffer) + tmp_keys.size(), + get_dataframe_buffer_begin(value_buffer) + cur_size); + } + } + + if (GraphViewType::is_multi_gpu) { + std::tie(keys, value_buffer) = reduce_to_unique_kv_pairs( + std::move(keys), std::move(value_buffer), handle.get_stream()); + } + + // FIXME: add init + + return std::make_tuple(std::move(keys), std::move(value_buffer)); +} + +} // namespace detail + +// FIXME: EdgeOp & VertexOp in update_frontier_v_push_if_out_nbr concatenates push inidicator or +// bucket idx with the value while EdgeOp here does not. This is inconsistent. Better be fixed. +/** + * @brief Iterate over the entire set of edges and reduce @p edge_op outputs to (key, value) pairs. + * + * This function is inspired by thrust::transform_reduce() and thrust::reduce_by_key(). Keys for + * edges are determined by the graph adjacency matrix rows. + * + * @tparam GraphViewType Type of the passed non-owning graph object. + * @tparam AdjMatrixRowValueInputIterator Type of the iterator for graph adjacency matrix row + * input properties. + * @tparam AdjMatrixColValueInputIterator Type of the iterator for graph adjacency matrix column + * input properties. + * @tparam VertexIterator Type of the iterator for keys in (key, value) pairs (key type should + * coincide with vertex type). + * @tparam EdgeOp Type of the quaternary (or quinary) edge operator. + * @tparam T Type of the values in (key, value) pairs. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param graph_view Non-owning graph object. + * @param adj_matrix_row_value_input_first Iterator pointing to the adjacency matrix row input + * properties for the first (inclusive) row (assigned to this process in multi-GPU). + * `adj_matrix_row_value_input_last` (exclusive) is deduced as @p adj_matrix_row_value_input_first + + * @p graph_view.get_number_of_local_adj_matrix_partition_rows(). + * @param adj_matrix_col_value_input_first Iterator pointing to the adjacency matrix column input + * properties for the first (inclusive) column (assigned to this process in multi-GPU). + * `adj_matrix_col_value_output_last` (exclusive) is deduced as @p adj_matrix_col_value_output_first + * + @p graph_view.get_number_of_local_adj_matrix_partition_cols(). + * @param adj_matrix_row_key_first Iterator pointing to the adjacency matrix row key for the first + * (inclusive) column (assigned to this process in multi-GPU). `adj_matrix_row_key_last` (exclusive) + * is deduced as @p adj_matrix_row_key_first + @p graph_view.get_number_of_local_adj_matrix_rows(). + * @param e_op Quaternary (or quinary) operator takes edge source, edge destination, (optional edge + * weight), *(@p adj_matrix_row_value_input_first + i), and *(@p adj_matrix_col_value_input_first + + * j) (where i is in [0, graph_view.get_number_of_local_adj_matrix_partition_rows()) and j is in [0, + * get_number_of_local_adj_matrix_partition_cols())) and returns a transformed value to be reduced. + * @param init Initial value to be added to the value in each transform-reduced (key, value) pair. + * @return std::tuple Tuple of rmm::device_uvector and + * rmm::device_uvector (if T is arithmetic scalar) or a tuple of rmm::device_uvector objects (if + * T is a thrust::tuple type of arithmetic scalar types, one rmm::device_uvector object per scalar + * type). + */ +template +auto transform_reduce_by_adj_matrix_row_key_e( + raft::handle_t const& handle, + GraphViewType const& graph_view, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + VertexIterator adj_matrix_row_key_first, + EdgeOp e_op, + T init) +{ + static_assert(is_arithmetic_or_thrust_tuple_of_arithmetic::value); + static_assert(std::is_same::value_type, + typename GraphViewType::vertex_type>::value); + + return detail::transform_reduce_by_adj_matrix_row_col_key_e( + handle, + graph_view, + adj_matrix_row_value_input_first, + adj_matrix_col_value_input_first, + adj_matrix_row_key_first, + e_op, + init); +} + +// FIXME: EdgeOp & VertexOp in update_frontier_v_push_if_out_nbr concatenates push inidicator or +// bucket idx with the value while EdgeOp here does not. This is inconsistent. Better be fixed. +/** + * @brief Iterate over the entire set of edges and reduce @p edge_op outputs to (key, value) pairs. + * + * This function is inspired by thrust::transform_reduce() and thrust::reduce_by_key(). Keys for + * edges are determined by the graph adjacency matrix columns. + * + * @tparam GraphViewType Type of the passed non-owning graph object. + * @tparam AdjMatrixRowValueInputIterator Type of the iterator for graph adjacency matrix row + * input properties. + * @tparam AdjMatrixColValueInputIterator Type of the iterator for graph adjacency matrix column + * input properties. + * @tparam VertexIterator Type of the iterator for keys in (key, value) pairs (key type should + * coincide with vertex type). + * @tparam EdgeOp Type of the quaternary (or quinary) edge operator. + * @tparam T Type of the values in (key, value) pairs. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param graph_view Non-owning graph object. + * @param adj_matrix_row_value_input_first Iterator pointing to the adjacency matrix row input + * properties for the first (inclusive) row (assigned to this process in multi-GPU). + * `adj_matrix_row_value_input_last` (exclusive) is deduced as @p adj_matrix_row_value_input_first + + * @p graph_view.get_number_of_local_adj_matrix_partition_rows(). + * @param adj_matrix_col_value_input_first Iterator pointing to the adjacency matrix column input + * properties for the first (inclusive) column (assigned to this process in multi-GPU). + * `adj_matrix_col_value_output_last` (exclusive) is deduced as @p adj_matrix_col_value_output_first + * + @p graph_view.get_number_of_local_adj_matrix_partition_cols(). + * @param adj_matrix_col_key_first Iterator pointing to the adjacency matrix column key for the + * first (inclusive) column (assigned to this process in multi-GPU). + * `adj_matrix_col_key_last` (exclusive) is deduced as @p adj_matrix_col_key_first + @p + * graph_view.get_number_of_local_adj_matrix_cols(). + * @param e_op Quaternary (or quinary) operator takes edge source, edge destination, (optional edge + * weight), *(@p adj_matrix_row_value_input_first + i), and *(@p adj_matrix_col_value_input_first + + * j) (where i is in [0, graph_view.get_number_of_local_adj_matrix_partition_rows()) and j is in [0, + * get_number_of_local_adj_matrix_partition_cols())) and returns a transformed value to be reduced. + * @param init Initial value to be added to the value in each transform-reduced (key, value) pair. + * @return std::tuple Tuple of rmm::device_uvector and + * rmm::device_uvector (if T is arithmetic scalar) or a tuple of rmm::device_uvector objects (if + * T is a thrust::tuple type of arithmetic scalar types, one rmm::device_uvector object per scalar + * type). + */ +template +auto transform_reduce_by_adj_matrix_col_key_e( + raft::handle_t const& handle, + GraphViewType const& graph_view, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + VertexIterator adj_matrix_col_key_first, + EdgeOp e_op, + T init) +{ + static_assert(is_arithmetic_or_thrust_tuple_of_arithmetic::value); + static_assert(std::is_same::value_type, + typename GraphViewType::vertex_type>::value); + + return detail::transform_reduce_by_adj_matrix_row_col_key_e( + handle, + graph_view, + adj_matrix_row_value_input_first, + adj_matrix_col_value_input_first, + adj_matrix_col_key_first, + e_op, + init); +} + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/include/cugraph/prims/transform_reduce_e.cuh b/cpp/include/cugraph/prims/transform_reduce_e.cuh new file mode 100644 index 00000000000..5ce40ea20cf --- /dev/null +++ b/cpp/include/cugraph/prims/transform_reduce_e.cuh @@ -0,0 +1,521 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include +#include + +namespace cugraph { +namespace experimental { + +namespace detail { + +// FIXME: block size requires tuning +int32_t constexpr transform_reduce_e_for_all_block_size = 128; + +template +__global__ void for_all_major_for_all_nbr_hypersparse( + matrix_partition_device_view_t matrix_partition, + typename GraphViewType::vertex_type major_hypersparse_first, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + ResultIterator result_iter /* size 1 */, + EdgeOp e_op) +{ + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; + using e_op_result_t = typename std::iterator_traits::value_type; + + auto const tid = threadIdx.x + blockIdx.x * blockDim.x; + auto major_start_offset = + static_cast(major_hypersparse_first - matrix_partition.get_major_first()); + size_t idx = static_cast(tid); + + auto dcs_nzd_vertex_count = *(matrix_partition.get_dcs_nzd_vertex_count()); + + property_add edge_property_add{}; + e_op_result_t e_op_result_sum{}; + while (idx < static_cast(dcs_nzd_vertex_count)) { + auto major = + *(matrix_partition.get_major_from_major_hypersparse_idx_nocheck(static_cast(idx))); + auto major_idx = + major_start_offset + idx; // major_offset != major_idx in the hypersparse region + vertex_t const* indices{nullptr}; + thrust::optional weights{nullptr}; + edge_t local_degree{}; + thrust::tie(indices, weights, local_degree) = matrix_partition.get_local_edges(major_idx); + auto sum = thrust::transform_reduce( + thrust::seq, + thrust::make_counting_iterator(edge_t{0}), + thrust::make_counting_iterator(local_degree), + [&matrix_partition, + &adj_matrix_row_value_input_first, + &adj_matrix_col_value_input_first, + &e_op, + major, + indices, + weights] __device__(auto i) { + auto major_offset = matrix_partition.get_major_offset_from_major_nocheck(major); + auto minor = indices[i]; + auto weight = weights ? (*weights)[i] : weight_t{1.0}; + auto minor_offset = matrix_partition.get_minor_offset_from_minor_nocheck(minor); + auto row = GraphViewType::is_adj_matrix_transposed ? minor : major; + auto col = GraphViewType::is_adj_matrix_transposed ? major : minor; + auto row_offset = GraphViewType::is_adj_matrix_transposed + ? minor_offset + : static_cast(major_offset); + auto col_offset = GraphViewType::is_adj_matrix_transposed + ? static_cast(major_offset) + : minor_offset; + return evaluate_edge_op() + .compute(row, + col, + weight, + *(adj_matrix_row_value_input_first + row_offset), + *(adj_matrix_col_value_input_first + col_offset), + e_op); + }, + e_op_result_t{}, + edge_property_add); + + e_op_result_sum = edge_property_add(e_op_result_sum, sum); + idx += gridDim.x * blockDim.x; + } + + e_op_result_sum = + block_reduce_edge_op_result().compute( + e_op_result_sum); + if (threadIdx.x == 0) { atomic_accumulate_edge_op_result(result_iter, e_op_result_sum); } +} + +template +__global__ void for_all_major_for_all_nbr_low_degree( + matrix_partition_device_view_t matrix_partition, + typename GraphViewType::vertex_type major_first, + typename GraphViewType::vertex_type major_last, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + ResultIterator result_iter /* size 1 */, + EdgeOp e_op) +{ + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; + using e_op_result_t = typename std::iterator_traits::value_type; + + auto const tid = threadIdx.x + blockIdx.x * blockDim.x; + auto major_start_offset = static_cast(major_first - matrix_partition.get_major_first()); + size_t idx = static_cast(tid); + + property_add edge_property_add{}; + e_op_result_t e_op_result_sum{}; + while (idx < static_cast(major_last - major_first)) { + auto major_offset = major_start_offset + idx; + vertex_t const* indices{nullptr}; + thrust::optional weights{nullptr}; + edge_t local_degree{}; + thrust::tie(indices, weights, local_degree) = matrix_partition.get_local_edges(major_offset); + auto sum = thrust::transform_reduce( + thrust::seq, + thrust::make_counting_iterator(edge_t{0}), + thrust::make_counting_iterator(local_degree), + [&matrix_partition, + &adj_matrix_row_value_input_first, + &adj_matrix_col_value_input_first, + &e_op, + major_offset, + indices, + weights] __device__(auto i) { + auto minor = indices[i]; + auto weight = weights ? (*weights)[i] : weight_t{1.0}; + auto minor_offset = matrix_partition.get_minor_offset_from_minor_nocheck(minor); + auto row = GraphViewType::is_adj_matrix_transposed + ? minor + : matrix_partition.get_major_from_major_offset_nocheck(major_offset); + auto col = GraphViewType::is_adj_matrix_transposed + ? matrix_partition.get_major_from_major_offset_nocheck(major_offset) + : minor; + auto row_offset = GraphViewType::is_adj_matrix_transposed + ? minor_offset + : static_cast(major_offset); + auto col_offset = GraphViewType::is_adj_matrix_transposed + ? static_cast(major_offset) + : minor_offset; + return evaluate_edge_op() + .compute(row, + col, + weight, + *(adj_matrix_row_value_input_first + row_offset), + *(adj_matrix_col_value_input_first + col_offset), + e_op); + }, + e_op_result_t{}, + edge_property_add); + + e_op_result_sum = edge_property_add(e_op_result_sum, sum); + idx += gridDim.x * blockDim.x; + } + + e_op_result_sum = + block_reduce_edge_op_result().compute( + e_op_result_sum); + if (threadIdx.x == 0) { atomic_accumulate_edge_op_result(result_iter, e_op_result_sum); } +} + +template +__global__ void for_all_major_for_all_nbr_mid_degree( + matrix_partition_device_view_t matrix_partition, + typename GraphViewType::vertex_type major_first, + typename GraphViewType::vertex_type major_last, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + ResultIterator result_iter /* size 1 */, + EdgeOp e_op) +{ + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; + using e_op_result_t = typename std::iterator_traits::value_type; + + auto const tid = threadIdx.x + blockIdx.x * blockDim.x; + static_assert(transform_reduce_e_for_all_block_size % raft::warp_size() == 0); + auto const lane_id = tid % raft::warp_size(); + auto major_start_offset = static_cast(major_first - matrix_partition.get_major_first()); + size_t idx = static_cast(tid / raft::warp_size()); + + property_add edge_property_add{}; + e_op_result_t e_op_result_sum{}; + while (idx < static_cast(major_last - major_first)) { + auto major_offset = major_start_offset + idx; + vertex_t const* indices{nullptr}; + thrust::optional weights{nullptr}; + edge_t local_degree{}; + thrust::tie(indices, weights, local_degree) = matrix_partition.get_local_edges(major_offset); + for (edge_t i = lane_id; i < local_degree; i += raft::warp_size()) { + auto minor = indices[i]; + auto weight = weights ? (*weights)[i] : weight_t{1.0}; + auto minor_offset = matrix_partition.get_minor_offset_from_minor_nocheck(minor); + auto row = GraphViewType::is_adj_matrix_transposed + ? minor + : matrix_partition.get_major_from_major_offset_nocheck(major_offset); + auto col = GraphViewType::is_adj_matrix_transposed + ? matrix_partition.get_major_from_major_offset_nocheck(major_offset) + : minor; + auto row_offset = GraphViewType::is_adj_matrix_transposed + ? minor_offset + : static_cast(major_offset); + auto col_offset = GraphViewType::is_adj_matrix_transposed + ? static_cast(major_offset) + : minor_offset; + auto e_op_result = evaluate_edge_op() + .compute(row, + col, + weight, + *(adj_matrix_row_value_input_first + row_offset), + *(adj_matrix_col_value_input_first + col_offset), + e_op); + e_op_result_sum = edge_property_add(e_op_result_sum, e_op_result); + } + idx += gridDim.x * (blockDim.x / raft::warp_size()); + } + + e_op_result_sum = + block_reduce_edge_op_result().compute( + e_op_result_sum); + if (threadIdx.x == 0) { atomic_accumulate_edge_op_result(result_iter, e_op_result_sum); } +} + +template +__global__ void for_all_major_for_all_nbr_high_degree( + matrix_partition_device_view_t matrix_partition, + typename GraphViewType::vertex_type major_first, + typename GraphViewType::vertex_type major_last, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + ResultIterator result_iter /* size 1 */, + EdgeOp e_op) +{ + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; + using e_op_result_t = typename std::iterator_traits::value_type; + + auto major_start_offset = static_cast(major_first - matrix_partition.get_major_first()); + size_t idx = static_cast(blockIdx.x); + + property_add edge_property_add{}; + e_op_result_t e_op_result_sum{}; + while (idx < static_cast(major_last - major_first)) { + auto major_offset = major_start_offset + idx; + vertex_t const* indices{nullptr}; + thrust::optional weights{nullptr}; + edge_t local_degree{}; + thrust::tie(indices, weights, local_degree) = matrix_partition.get_local_edges(major_offset); + for (edge_t i = threadIdx.x; i < local_degree; i += blockDim.x) { + auto minor = indices[i]; + auto weight = weights ? (*weights)[i] : weight_t{1.0}; + auto minor_offset = matrix_partition.get_minor_offset_from_minor_nocheck(minor); + auto row = GraphViewType::is_adj_matrix_transposed + ? minor + : matrix_partition.get_major_from_major_offset_nocheck(major_offset); + auto col = GraphViewType::is_adj_matrix_transposed + ? matrix_partition.get_major_from_major_offset_nocheck(major_offset) + : minor; + auto row_offset = GraphViewType::is_adj_matrix_transposed + ? minor_offset + : static_cast(major_offset); + auto col_offset = GraphViewType::is_adj_matrix_transposed + ? static_cast(major_offset) + : minor_offset; + auto e_op_result = evaluate_edge_op() + .compute(row, + col, + weight, + *(adj_matrix_row_value_input_first + row_offset), + *(adj_matrix_col_value_input_first + col_offset), + e_op); + e_op_result_sum = edge_property_add(e_op_result_sum, e_op_result); + } + idx += gridDim.x; + } + + e_op_result_sum = + block_reduce_edge_op_result().compute( + e_op_result_sum); + if (threadIdx.x == 0) { atomic_accumulate_edge_op_result(result_iter, e_op_result_sum); } +} + +} // namespace detail + +/** + * @brief Iterate over the entire set of edges and reduce @p edge_op outputs. + * + * This function is inspired by thrust::transform_reduce(). + * + * @tparam GraphViewType Type of the passed non-owning graph object. + * @tparam AdjMatrixRowValueInputIterator Type of the iterator for graph adjacency matrix row + * input properties. + * @tparam AdjMatrixColValueInputIterator Type of the iterator for graph adjacency matrix column + * input properties. + * @tparam EdgeOp Type of the quaternary (or quinary) edge operator. + * @tparam T Type of the initial value. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param graph_view Non-owning graph object. + * @param adj_matrix_row_value_input_first Iterator pointing to the adjacency matrix row input + * properties for the first (inclusive) row (assigned to this process in multi-GPU). + * `adj_matrix_row_value_input_last` (exclusive) is deduced as @p adj_matrix_row_value_input_first + + * @p graph_view.get_number_of_local_adj_matrix_partition_rows(). + * @param adj_matrix_col_value_input_first Iterator pointing to the adjacency matrix column input + * properties for the first (inclusive) column (assigned to this process in multi-GPU). + * `adj_matrix_col_value_output_last` (exclusive) is deduced as @p adj_matrix_col_value_output_first + * + @p graph_view.get_number_of_local_adj_matrix_partition_cols(). + * @param e_op Quaternary (or quinary) operator takes edge source, edge destination, (optional edge + * weight), *(@p adj_matrix_row_value_input_first + i), and *(@p adj_matrix_col_value_input_first + + * j) (where i is in [0, graph_view.get_number_of_local_adj_matrix_partition_rows()) and j is in [0, + * get_number_of_local_adj_matrix_partition_cols())) and returns a transformed value to be reduced. + * @param init Initial value to be added to the transform-reduced input vertex properties. + * @return T Reduction of the @p edge_op outputs. + */ +template +T transform_reduce_e(raft::handle_t const& handle, + GraphViewType const& graph_view, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + EdgeOp e_op, + T init) +{ + static_assert(is_arithmetic_or_thrust_tuple_of_arithmetic::value); + + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; + + property_add edge_property_add{}; + + auto result_buffer = allocate_dataframe_buffer(1, handle.get_stream()); + thrust::fill(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + get_dataframe_buffer_begin(result_buffer), + get_dataframe_buffer_begin(result_buffer) + 1, + T{}); + + for (size_t i = 0; i < graph_view.get_number_of_local_adj_matrix_partitions(); ++i) { + auto matrix_partition = + matrix_partition_device_view_t( + graph_view.get_matrix_partition_view(i)); + + auto row_value_input_offset = GraphViewType::is_adj_matrix_transposed + ? vertex_t{0} + : matrix_partition.get_major_value_start_offset(); + auto col_value_input_offset = GraphViewType::is_adj_matrix_transposed + ? matrix_partition.get_major_value_start_offset() + : vertex_t{0}; + auto segment_offsets = graph_view.get_local_adj_matrix_partition_segment_offsets(i); + if (segment_offsets) { + // FIXME: we may further improve performance by 1) concurrently running kernels on different + // segments; 2) individually tuning block sizes for different segments; and 3) adding one more + // segment for very high degree vertices and running segmented reduction + static_assert(detail::num_sparse_segments_per_vertex_partition == 3); + if ((*segment_offsets)[1] > 0) { + raft::grid_1d_block_t update_grid((*segment_offsets)[1], + detail::transform_reduce_e_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + detail::for_all_major_for_all_nbr_high_degree + <<>>( + matrix_partition, + matrix_partition.get_major_first(), + matrix_partition.get_major_first() + (*segment_offsets)[1], + adj_matrix_row_value_input_first + row_value_input_offset, + adj_matrix_col_value_input_first + col_value_input_offset, + get_dataframe_buffer_begin(result_buffer), + e_op); + } + if ((*segment_offsets)[2] - (*segment_offsets)[1] > 0) { + raft::grid_1d_warp_t update_grid((*segment_offsets)[2] - (*segment_offsets)[1], + detail::transform_reduce_e_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + detail::for_all_major_for_all_nbr_mid_degree + <<>>( + matrix_partition, + matrix_partition.get_major_first() + (*segment_offsets)[1], + matrix_partition.get_major_first() + (*segment_offsets)[2], + adj_matrix_row_value_input_first + row_value_input_offset, + adj_matrix_col_value_input_first + col_value_input_offset, + get_dataframe_buffer_begin(result_buffer), + e_op); + } + if ((*segment_offsets)[3] - (*segment_offsets)[2] > 0) { + raft::grid_1d_thread_t update_grid((*segment_offsets)[3] - (*segment_offsets)[2], + detail::transform_reduce_e_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + detail::for_all_major_for_all_nbr_low_degree + <<>>( + matrix_partition, + matrix_partition.get_major_first() + (*segment_offsets)[2], + matrix_partition.get_major_first() + (*segment_offsets)[3], + adj_matrix_row_value_input_first + row_value_input_offset, + adj_matrix_col_value_input_first + col_value_input_offset, + get_dataframe_buffer_begin(result_buffer), + e_op); + } + if (matrix_partition.get_dcs_nzd_vertex_count() && + (*(matrix_partition.get_dcs_nzd_vertex_count()) > 0)) { + raft::grid_1d_thread_t update_grid(*(matrix_partition.get_dcs_nzd_vertex_count()), + detail::transform_reduce_e_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + detail::for_all_major_for_all_nbr_hypersparse + <<>>( + matrix_partition, + matrix_partition.get_major_first() + (*segment_offsets)[3], + adj_matrix_row_value_input_first + row_value_input_offset, + adj_matrix_col_value_input_first + col_value_input_offset, + get_dataframe_buffer_begin(result_buffer), + e_op); + } + } else { + if (matrix_partition.get_major_size() > 0) { + raft::grid_1d_thread_t update_grid(matrix_partition.get_major_size(), + detail::transform_reduce_e_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + + detail::for_all_major_for_all_nbr_low_degree + <<>>( + matrix_partition, + matrix_partition.get_major_first(), + matrix_partition.get_major_last(), + adj_matrix_row_value_input_first + row_value_input_offset, + adj_matrix_col_value_input_first + col_value_input_offset, + get_dataframe_buffer_begin(result_buffer), + e_op); + } + } + } + + auto result = thrust::reduce(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + get_dataframe_buffer_begin(result_buffer), + get_dataframe_buffer_begin(result_buffer) + 1, + T{}, + edge_property_add); + + if (GraphViewType::is_multi_gpu) { + result = host_scalar_allreduce(handle.get_comms(), result, handle.get_stream()); + } + + return edge_property_add(init, result); +} + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/include/patterns/transform_reduce_v.cuh b/cpp/include/cugraph/prims/transform_reduce_v.cuh similarity index 96% rename from cpp/include/patterns/transform_reduce_v.cuh rename to cpp/include/cugraph/prims/transform_reduce_v.cuh index 02538c36f47..0d5b4f9cbb6 100644 --- a/cpp/include/patterns/transform_reduce_v.cuh +++ b/cpp/include/cugraph/prims/transform_reduce_v.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -15,9 +15,9 @@ */ #pragma once -#include -#include -#include +#include +#include +#include #include diff --git a/cpp/include/cugraph/prims/update_frontier_v_push_if_out_nbr.cuh b/cpp/include/cugraph/prims/update_frontier_v_push_if_out_nbr.cuh new file mode 100644 index 00000000000..e2f72c66d0b --- /dev/null +++ b/cpp/include/cugraph/prims/update_frontier_v_push_if_out_nbr.cuh @@ -0,0 +1,1308 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace cugraph { +namespace experimental { + +namespace detail { + +int32_t constexpr update_frontier_v_push_if_out_nbr_for_all_block_size = 512; + +// we cannot use std::iterator_traits::value_type if Iterator is void* (reference to void +// is not allowed) +template +struct optional_payload_buffer_value_type_t; + +template +struct optional_payload_buffer_value_type_t< + PayloadIterator, + std::enable_if_t>> { + using value = typename std::iterator_traits::value_type; +}; + +template +struct optional_payload_buffer_value_type_t< + PayloadIterator, + std::enable_if_t>> { + using value = void; +}; + +// FIXME: to silence the spurious warning (missing return statement ...) due to the nvcc bug +// (https://stackoverflow.com/questions/64523302/cuda-missing-return-statement-at-end-of-non-void- +// function-in-constexpr-if-fun) +#if 1 +template >* = nullptr> +std::byte allocate_optional_payload_buffer(size_t size, cudaStream_t stream) +{ + return std::byte{0}; // dummy +} + +template >* = nullptr> +auto allocate_optional_payload_buffer(size_t size, cudaStream_t stream) +{ + return allocate_dataframe_buffer(size, stream); +} + +template >* = nullptr> +void* get_optional_payload_buffer_begin(std::byte& optional_payload_buffer) +{ + return static_cast(nullptr); +} + +template >* = nullptr> +auto get_optional_payload_buffer_begin( + std::add_lvalue_reference_t( + size_t{0}, cudaStream_t{nullptr}))> optional_payload_buffer) +{ + return get_dataframe_buffer_begin(optional_payload_buffer); +} +#else +auto allocate_optional_payload_buffer = [](size_t size, cudaStream_t stream) { + if constexpr (std::is_same_v) { + return std::byte{0}; // dummy + } else { + return allocate_dataframe_buffer(size, stream); + } +}; + +auto get_optional_payload_buffer_begin = [](auto& optional_payload_buffer) { + if constexpr (std::is_same_v) { + return static_cast(nullptr); + } else { + return get_dataframe_buffer_begin(optional_payload_buffer); + } +}; +#endif + +// FIXME: a temporary workaround for cudaErrorInvalidDeviceFunction error when device lambda is used +// in the else part in if constexpr else statement that involves device lambda +template +struct call_v_op_t { + VertexValueInputIterator vertex_value_input_first{}; + VertexValueOutputIterator vertex_value_output_first{}; + VertexOp v_op{}; + vertex_partition_device_view_t vertex_partition{}; + size_t invalid_bucket_idx; + + template + __device__ std::enable_if_t, uint8_t> operator()( + key_t key) const + { + auto v_offset = vertex_partition.get_local_vertex_offset_from_vertex_nocheck(key); + auto v_val = *(vertex_value_input_first + v_offset); + auto v_op_result = v_op(key, v_val); + if (v_op_result) { + *(vertex_value_output_first + v_offset) = thrust::get<1>(*v_op_result); + return static_cast(thrust::get<0>(*v_op_result)); + } else { + return std::numeric_limits::max(); + } + } + + template + __device__ std::enable_if_t, uint8_t> operator()( + key_t key) const + { + auto v_offset = + vertex_partition.get_local_vertex_offset_from_vertex_nocheck(thrust::get<0>(key)); + auto v_val = *(vertex_value_input_first + v_offset); + auto v_op_result = v_op(key, v_val); + if (v_op_result) { + *(vertex_value_output_first + v_offset) = thrust::get<1>(*v_op_result); + return static_cast(thrust::get<0>(*v_op_result)); + } else { + return std::numeric_limits::max(); + } + } +}; + +// FIXME: a temporary workaround for cudaErrorInvalidDeviceFunction error when device lambda is used +// after if constexpr else statement that involves device lambda (bug report submitted) +template +struct check_invalid_bucket_idx_t { + __device__ bool operator()(thrust::tuple pair) + { + return thrust::get<0>(pair) == std::numeric_limits::max(); + } +}; + +template +__device__ void push_if_buffer_element( + matrix_partition_device_view_t& matrix_partition, + typename std::iterator_traits::value_type key, + typename GraphViewType::vertex_type row_offset, + typename GraphViewType::vertex_type col, + typename GraphViewType::weight_type weight, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + BufferKeyOutputIterator buffer_key_output_first, + BufferPayloadOutputIterator buffer_payload_output_first, + size_t* buffer_idx_ptr, + EdgeOp e_op) +{ + using vertex_t = typename GraphViewType::vertex_type; + using key_t = typename std::iterator_traits::value_type; + using payload_t = + typename optional_payload_buffer_value_type_t::value; + + auto col_offset = matrix_partition.get_minor_offset_from_minor_nocheck(col); + auto e_op_result = evaluate_edge_op() + .compute(key, + col, + weight, + *(adj_matrix_row_value_input_first + row_offset), + *(adj_matrix_col_value_input_first + col_offset), + e_op); + if (e_op_result) { + static_assert(sizeof(unsigned long long int) == sizeof(size_t)); + auto buffer_idx = atomicAdd(reinterpret_cast(buffer_idx_ptr), + static_cast(1)); + if constexpr (std::is_same_v && std::is_same_v) { + *(buffer_key_output_first + buffer_idx) = col; + } else if constexpr (std::is_same_v && !std::is_same_v) { + *(buffer_key_output_first + buffer_idx) = col; + *(buffer_payload_output_first + buffer_idx) = *e_op_result; + } else if constexpr (!std::is_same_v && std::is_same_v) { + *(buffer_key_output_first + buffer_idx) = thrust::make_tuple(col, *e_op_result); + } else { + *(buffer_key_output_first + buffer_idx) = + thrust::make_tuple(col, thrust::get<0>(*e_op_result)); + *(buffer_payload_output_first + buffer_idx) = thrust::get<1>(*e_op_result); + } + } +} + +template +__global__ void for_all_frontier_row_for_all_nbr_hypersparse( + matrix_partition_device_view_t matrix_partition, + typename GraphViewType::vertex_type major_hypersparse_first, + KeyIterator key_first, + KeyIterator key_last, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + BufferKeyOutputIterator buffer_key_output_first, + BufferPayloadOutputIterator buffer_payload_output_first, + size_t* buffer_idx_ptr, + EdgeOp e_op) +{ + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; + using key_t = typename std::iterator_traits::value_type; + static_assert( + std::is_same_v::value_type>); + using payload_t = + typename optional_payload_buffer_value_type_t::value; + + static_assert(!GraphViewType::is_adj_matrix_transposed, + "GraphViewType should support the push model."); + + auto const tid = threadIdx.x + blockIdx.x * blockDim.x; + auto row_start_offset = + static_cast(major_hypersparse_first - matrix_partition.get_major_first()); + auto idx = static_cast(tid); + + auto dcs_nzd_vertices = *(matrix_partition.get_dcs_nzd_vertices()); + auto dcs_nzd_vertex_count = *(matrix_partition.get_dcs_nzd_vertex_count()); + + while (idx < static_cast(thrust::distance(key_first, key_last))) { + auto key = *(key_first + idx); + vertex_t row{}; + if constexpr (std::is_same_v) { + row = key; + } else { + row = thrust::get<0>(key); + } + auto row_hypersparse_idx = matrix_partition.get_major_hypersparse_idx_from_major_nocheck(row); + if (row_hypersparse_idx) { + auto row_offset = matrix_partition.get_major_offset_from_major_nocheck(row); + auto row_idx = row_start_offset + *row_hypersparse_idx; + vertex_t const* indices{nullptr}; + thrust::optional weights{nullptr}; + edge_t local_out_degree{}; + thrust::tie(indices, weights, local_out_degree) = matrix_partition.get_local_edges(row_idx); + for (edge_t i = 0; i < local_out_degree; ++i) { + push_if_buffer_element(matrix_partition, + key, + row_offset, + indices[i], + weights ? (*weights)[i] : weight_t{1.0}, + adj_matrix_row_value_input_first, + adj_matrix_col_value_input_first, + buffer_key_output_first, + buffer_payload_output_first, + buffer_idx_ptr, + e_op); + } + } + idx += gridDim.x * blockDim.x; + } +} + +template +__global__ void for_all_frontier_row_for_all_nbr_low_degree( + matrix_partition_device_view_t matrix_partition, + KeyIterator key_first, + KeyIterator key_last, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + BufferKeyOutputIterator buffer_key_output_first, + BufferPayloadOutputIterator buffer_payload_output_first, + size_t* buffer_idx_ptr, + EdgeOp e_op) +{ + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; + using key_t = typename std::iterator_traits::value_type; + static_assert( + std::is_same_v::value_type>); + using payload_t = + typename optional_payload_buffer_value_type_t::value; + + static_assert(!GraphViewType::is_adj_matrix_transposed, + "GraphViewType should support the push model."); + + auto const tid = threadIdx.x + blockIdx.x * blockDim.x; + auto idx = static_cast(tid); + + while (idx < static_cast(thrust::distance(key_first, key_last))) { + auto key = *(key_first + idx); + vertex_t row{}; + if constexpr (std::is_same_v) { + row = key; + } else { + row = thrust::get<0>(key); + } + auto row_offset = matrix_partition.get_major_offset_from_major_nocheck(row); + vertex_t const* indices{nullptr}; + thrust::optional weights{nullptr}; + edge_t local_out_degree{}; + thrust::tie(indices, weights, local_out_degree) = matrix_partition.get_local_edges(row_offset); + for (edge_t i = 0; i < local_out_degree; ++i) { + push_if_buffer_element(matrix_partition, + key, + row_offset, + indices[i], + weights ? (*weights)[i] : weight_t{1.0}, + adj_matrix_row_value_input_first, + adj_matrix_col_value_input_first, + buffer_key_output_first, + buffer_payload_output_first, + buffer_idx_ptr, + e_op); + } + idx += gridDim.x * blockDim.x; + } +} + +template +__global__ void for_all_frontier_row_for_all_nbr_mid_degree( + matrix_partition_device_view_t matrix_partition, + KeyIterator key_first, + KeyIterator key_last, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + BufferKeyOutputIterator buffer_key_output_first, + BufferPayloadOutputIterator buffer_payload_output_first, + size_t* buffer_idx_ptr, + EdgeOp e_op) +{ + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; + using key_t = typename std::iterator_traits::value_type; + static_assert( + std::is_same_v::value_type>); + using payload_t = + typename optional_payload_buffer_value_type_t::value; + + static_assert(!GraphViewType::is_adj_matrix_transposed, + "GraphViewType should support the push model."); + + auto const tid = threadIdx.x + blockIdx.x * blockDim.x; + static_assert(update_frontier_v_push_if_out_nbr_for_all_block_size % raft::warp_size() == 0); + auto const lane_id = tid % raft::warp_size(); + auto idx = static_cast(tid / raft::warp_size()); + + while (idx < static_cast(thrust::distance(key_first, key_last))) { + auto key = *(key_first + idx); + vertex_t row{}; + if constexpr (std::is_same_v) { + row = key; + } else { + row = thrust::get<0>(key); + } + auto row_offset = matrix_partition.get_major_offset_from_major_nocheck(row); + vertex_t const* indices{nullptr}; + thrust::optional weights{nullptr}; + edge_t local_out_degree{}; + thrust::tie(indices, weights, local_out_degree) = matrix_partition.get_local_edges(row_offset); + for (edge_t i = lane_id; i < local_out_degree; i += raft::warp_size()) { + push_if_buffer_element(matrix_partition, + key, + row_offset, + indices[i], + weights ? (*weights)[i] : weight_t{1.0}, + adj_matrix_row_value_input_first, + adj_matrix_col_value_input_first, + buffer_key_output_first, + buffer_payload_output_first, + buffer_idx_ptr, + e_op); + } + + idx += gridDim.x * (blockDim.x / raft::warp_size()); + } +} + +template +__global__ void for_all_frontier_row_for_all_nbr_high_degree( + matrix_partition_device_view_t matrix_partition, + KeyIterator key_first, + KeyIterator key_last, + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + BufferKeyOutputIterator buffer_key_output_first, + BufferPayloadOutputIterator buffer_payload_output_first, + size_t* buffer_idx_ptr, + EdgeOp e_op) +{ + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; + using key_t = typename std::iterator_traits::value_type; + static_assert( + std::is_same_v::value_type>); + using payload_t = + typename optional_payload_buffer_value_type_t::value; + + static_assert(!GraphViewType::is_adj_matrix_transposed, + "GraphViewType should support the push model."); + + auto idx = static_cast(blockIdx.x); + + while (idx < static_cast(thrust::distance(key_first, key_last))) { + auto key = *(key_first + idx); + vertex_t row{}; + if constexpr (std::is_same_v) { + row = key; + } else { + row = thrust::get<0>(key); + } + auto row_offset = matrix_partition.get_major_offset_from_major_nocheck(row); + vertex_t const* indices{nullptr}; + thrust::optional weights{nullptr}; + edge_t local_out_degree{}; + thrust::tie(indices, weights, local_out_degree) = matrix_partition.get_local_edges(row_offset); + for (edge_t i = threadIdx.x; i < local_out_degree; i += blockDim.x) { + push_if_buffer_element(matrix_partition, + key, + row_offset, + indices[i], + weights ? (*weights)[i] : weight_t{1.0}, + adj_matrix_row_value_input_first, + adj_matrix_col_value_input_first, + buffer_key_output_first, + buffer_payload_output_first, + buffer_idx_ptr, + e_op); + } + + idx += gridDim.x; + } +} + +template +size_t sort_and_reduce_buffer_elements(raft::handle_t const& handle, + BufferKeyOutputIterator buffer_key_output_first, + BufferPayloadOutputIterator buffer_payload_output_first, + size_t num_buffer_elements, + ReduceOp reduce_op) +{ + using key_t = typename std::iterator_traits::value_type; + using payload_t = + typename optional_payload_buffer_value_type_t::value; + + if constexpr (std::is_same_v) { + thrust::sort(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + buffer_key_output_first, + buffer_key_output_first + num_buffer_elements); + } else { + thrust::sort_by_key(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + buffer_key_output_first, + buffer_key_output_first + num_buffer_elements, + buffer_payload_output_first); + } + + size_t num_reduced_buffer_elements{}; + if constexpr (std::is_same_v) { + auto it = thrust::unique(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + buffer_key_output_first, + buffer_key_output_first + num_buffer_elements); + num_reduced_buffer_elements = + static_cast(thrust::distance(buffer_key_output_first, it)); + } else if constexpr (std::is_same>::value) { + // FIXME: if ReducOp is any, we may have a cheaper alternative than sort & uique (i.e. discard + // non-first elements) + auto it = thrust::unique_by_key(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + buffer_key_output_first, + buffer_key_output_first + num_buffer_elements, + buffer_payload_output_first); + num_reduced_buffer_elements = + static_cast(thrust::distance(buffer_key_output_first, thrust::get<0>(it))); + } else { + // FIXME: better avoid temporary buffer or at least limit the maximum buffer size (if we adopt + // CUDA cooperative group https://devblogs.nvidia.com/cooperative-groups and global sync(), we + // can use aggregate shared memory as a temporary buffer, or we can limit the buffer size, and + // split one thrust::reduce_by_key call to multiple thrust::reduce_by_key calls if the + // temporary buffer size exceeds the maximum buffer size (may be definied as percentage of the + // system HBM size or a function of the maximum number of threads in the system)) + // FIXME: actually, we can find how many unique keys are here by now. + // FIXME: if GraphViewType::is_multi_gpu is true, this should be executed on the GPU holding + // the vertex unless reduce_op is a pure function. + rmm::device_uvector keys(num_buffer_elements, handle.get_stream()); + auto value_buffer = + allocate_dataframe_buffer(num_buffer_elements, handle.get_stream()); + auto it = thrust::reduce_by_key(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + buffer_key_output_first, + buffer_key_output_first + num_buffer_elements, + buffer_payload_output_first, + keys.begin(), + get_dataframe_buffer_begin(value_buffer), + thrust::equal_to(), + reduce_op); + num_reduced_buffer_elements = + static_cast(thrust::distance(keys.begin(), thrust::get<0>(it))); + // FIXME: this copy can be replaced by move + thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + keys.begin(), + keys.begin() + num_reduced_buffer_elements, + buffer_key_output_first); + thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + get_dataframe_buffer_begin(value_buffer), + get_dataframe_buffer_begin(value_buffer) + num_reduced_buffer_elements, + buffer_payload_output_first); + } + + return num_reduced_buffer_elements; +} + +} // namespace detail + +template +typename GraphViewType::edge_type compute_num_out_nbrs_from_frontier( + raft::handle_t const& handle, + GraphViewType const& graph_view, + VertexFrontierType const& frontier, + size_t cur_frontier_bucket_idx) +{ + static_assert(!GraphViewType::is_adj_matrix_transposed, + "GraphViewType should support the push model."); + + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; + using key_t = typename VertexFrontierType::key_type; + + edge_t ret{0}; + + if (GraphViewType::is_multi_gpu) { + auto& comm = handle.get_comms(); + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (beginning of col_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + } + + auto const& cur_frontier_bucket = frontier.get_bucket(cur_frontier_bucket_idx); + vertex_t const* local_frontier_vertex_first{nullptr}; + vertex_t const* local_frontier_vertex_last{nullptr}; + if constexpr (std::is_same_v) { + local_frontier_vertex_first = cur_frontier_bucket.begin(); + local_frontier_vertex_last = cur_frontier_bucket.end(); + } else { + local_frontier_vertex_first = thrust::get<0>(cur_frontier_bucket.begin().get_iterator_tuple()); + local_frontier_vertex_last = thrust::get<0>(cur_frontier_bucket.end().get_iterator_tuple()); + } + + std::vector local_frontier_sizes{}; + if (GraphViewType::is_multi_gpu) { + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + local_frontier_sizes = + host_scalar_allgather(col_comm, cur_frontier_bucket.size(), handle.get_stream()); + } else { + local_frontier_sizes = std::vector{static_cast(cur_frontier_bucket.size())}; + } + for (size_t i = 0; i < graph_view.get_number_of_local_adj_matrix_partitions(); ++i) { + auto matrix_partition = + matrix_partition_device_view_t( + graph_view.get_matrix_partition_view(i)); + + if (GraphViewType::is_multi_gpu) { + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_rank = col_comm.get_rank(); + + rmm::device_uvector frontier_vertices(local_frontier_sizes[i], + handle.get_stream_view()); + // FIXME: this copy is unnecessary, better fix RAFT comm's bcast to take const iterators for + // input + if (col_comm_rank == static_cast(i)) { + thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + local_frontier_vertex_first, + local_frontier_vertex_last, + frontier_vertices.begin()); + } + device_bcast(col_comm, + frontier_vertices.data(), + frontier_vertices.data(), + frontier_vertices.size(), + static_cast(i), + handle.get_stream()); + + auto segment_offsets = graph_view.get_local_adj_matrix_partition_segment_offsets(i); + auto use_dcs = + segment_offsets + ? ((*segment_offsets).size() > (detail::num_sparse_segments_per_vertex_partition + 1)) + : false; + + ret += + use_dcs + ? thrust::transform_reduce( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + frontier_vertices.begin(), + frontier_vertices.end(), + [matrix_partition, + major_hypersparse_first = + matrix_partition.get_major_first() + + (*segment_offsets) + [detail::num_sparse_segments_per_vertex_partition]] __device__(auto major) { + if (major < major_hypersparse_first) { + auto major_offset = matrix_partition.get_major_offset_from_major_nocheck(major); + return matrix_partition.get_local_degree(major_offset); + } else { + auto major_hypersparse_idx = + matrix_partition.get_major_hypersparse_idx_from_major_nocheck(major); + return major_hypersparse_idx + ? matrix_partition.get_local_degree( + matrix_partition.get_major_offset_from_major_nocheck( + major_hypersparse_first) + + *major_hypersparse_idx) + : edge_t{0}; + } + }, + edge_t{0}, + thrust::plus()) + : thrust::transform_reduce( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + frontier_vertices.begin(), + frontier_vertices.end(), + [matrix_partition] __device__(auto major) { + auto major_offset = matrix_partition.get_major_offset_from_major_nocheck(major); + return matrix_partition.get_local_degree(major_offset); + }, + edge_t{0}, + thrust::plus()); + } else { + assert(i == 0); + ret += thrust::transform_reduce( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + local_frontier_vertex_first, + local_frontier_vertex_last, + [matrix_partition] __device__(auto major) { + auto major_offset = matrix_partition.get_major_offset_from_major_nocheck(major); + return matrix_partition.get_local_degree(major_offset); + }, + edge_t{0}, + thrust::plus()); + } + } + + if (GraphViewType::is_multi_gpu) { + auto& comm = handle.get_comms(); + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (end of col_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + } + + return ret; +} + +// FIXME: this documentation needs to be updated due to (tagged-)vertex support +/** + * @brief Update (tagged-)vertex frontier and (tagged-)vertex property values iterating over the + * outgoing edges from the frontier. + * + * @tparam GraphViewType Type of the passed non-owning graph object. + * @tparam VertexFrontierType Type of the vertex frontier class which abstracts vertex frontier + * managements. + * @tparam AdjMatrixRowValueInputIterator Type of the iterator for graph adjacency matrix row + * input properties. + * @tparam AdjMatrixColValueInputIterator Type of the iterator for graph adjacency matrix column + * input properties. + * @tparam EdgeOp Type of the quaternary (or quinary) edge operator. + * @tparam ReduceOp Type of the binary reduction operator. + * @tparam VertexValueInputIterator Type of the iterator for vertex properties. + * @tparam VertexValueOutputIterator Type of the iterator for vertex property variables. + * @tparam VertexOp Type of the binary vertex operator. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param graph_view Non-owning graph object. + * @param frontier VertexFrontier class object for vertex frontier managements. This object includes + * multiple bucket objects. + * @param cur_frontier_bucket_idx Index of the VertexFrontier bucket holding vertices for the + * current iteration. + * @param next_frontier_bucket_indices Indices of the VertexFrontier buckets to store new frontier + * vertices for the next iteration. + * @param adj_matrix_row_value_input_first Iterator pointing to the adjacency matrix row input + * properties for the first (inclusive) row (assigned to this process in multi-GPU). + * `adj_matrix_row_value_input_last` (exclusive) is deduced as @p adj_matrix_row_value_input_first + + * @p graph_view.get_number_of_local_adj_matrix_partition_rows(). + * @param adj_matrix_col_value_input_first Iterator pointing to the adjacency matrix column input + * properties for the first (inclusive) column (assigned to this process in multi-GPU). + * `adj_matrix_col_value_output_last` (exclusive) is deduced as @p adj_matrix_col_value_output_first + * + @p graph_view.get_number_of_local_adj_matrix_partition_cols(). + * @param e_op Quaternary (or quinary) operator takes edge source, edge destination, (optional edge + * weight), *(@p adj_matrix_row_value_input_first + i), and *(@p adj_matrix_col_value_input_first + + * j) (where i is in [0, graph_view.get_number_of_local_adj_matrix_partition_rows()) and j is in [0, + * get_number_of_local_adj_matrix_partition_cols())) and returns a value to reduced by the @p + * reduce_op. + * @param reduce_op Binary operator takes two input arguments and reduce the two variables to one. + * @param vertex_value_input_first Iterator pointing to the vertex properties for the first + * (inclusive) vertex (assigned to this process in multi-GPU). `vertex_value_input_last` (exclusive) + * is deduced as @p vertex_value_input_first + @p graph_view.get_number_of_local_vertices(). + * @param vertex_value_output_first Iterator pointing to the vertex property variables for the first + * (inclusive) vertex (assigned to tihs process in multi-GPU). `vertex_value_output_last` + * (exclusive) is deduced as @p vertex_value_output_first + @p + * graph_view.get_number_of_local_vertices(). + * @param v_op Ternary operator takes (tagged-)vertex ID, *(@p vertex_value_input_first + i) (where + * i is [0, @p graph_view.get_number_of_local_vertices())) and reduced value of the @p e_op outputs + * for this vertex and returns the target bucket index (for frontier update) and new verrtex + * property values (to update *(@p vertex_value_output_first + i)). The target bucket index should + * either be VertexFrontierType::kInvalidBucketIdx or an index in @p next_frontier_bucket_indices. + */ +template +void update_frontier_v_push_if_out_nbr( + raft::handle_t const& handle, + GraphViewType const& graph_view, + VertexFrontierType& frontier, + size_t cur_frontier_bucket_idx, + std::vector const& next_frontier_bucket_indices, + // FIXME: if vertices in the frontier are tagged, we should have an option to access with (vertex, + // tag) pair (currently we can access only with vertex, we may use cuco::static_map for this + // purpose) + AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, + AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, + EdgeOp e_op, + ReduceOp reduce_op, + // FIXME: if vertices in the frontier are tagged, we should have an option to access with (vertex, + // tag) pair (currently we can access only with vertex, we may use cuco::static_map for this + // purpose) + VertexValueInputIterator vertex_value_input_first, + // FIXME: if vertices in the frontier are tagged, we should have an option to access with (vertex, + // tag) pair (currently we can access only with vertex, we may use cuco::static_map for this + // purpose) + // FIXME: currently, it is undefined behavior if vertices in the frontier are tagged and the same + // vertex property is updated by multiple v_op invocations with the same vertex but with different + // tags. + VertexValueOutputIterator vertex_value_output_first, + // FIXME: this takes (tagged-)vertex ID in addition, think about consistency with the other + // primitives. + VertexOp v_op) +{ + static_assert(!GraphViewType::is_adj_matrix_transposed, + "GraphViewType should support the push model."); + + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; + using key_t = typename VertexFrontierType::key_type; + using payload_t = typename ReduceOp::type; + + auto frontier_key_first = frontier.get_bucket(cur_frontier_bucket_idx).begin(); + auto frontier_key_last = frontier.get_bucket(cur_frontier_bucket_idx).end(); + + // 1. fill the buffer + + if (GraphViewType::is_multi_gpu) { + auto& comm = handle.get_comms(); + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (beginning of col_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + } + + auto key_buffer = allocate_dataframe_buffer(size_t{0}, handle.get_stream()); + auto payload_buffer = + detail::allocate_optional_payload_buffer(size_t{0}, handle.get_stream()); + rmm::device_scalar buffer_idx(size_t{0}, handle.get_stream()); + std::vector local_frontier_sizes{}; + if (GraphViewType::is_multi_gpu) { + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + local_frontier_sizes = host_scalar_allgather( + col_comm, + static_cast(thrust::distance(frontier_key_first, frontier_key_last)), + handle.get_stream()); + } else { + local_frontier_sizes = std::vector{static_cast( + static_cast(thrust::distance(frontier_key_first, frontier_key_last)))}; + } + for (size_t i = 0; i < graph_view.get_number_of_local_adj_matrix_partitions(); ++i) { + auto matrix_partition = + matrix_partition_device_view_t( + graph_view.get_matrix_partition_view(i)); + + auto matrix_partition_frontier_key_buffer = + allocate_dataframe_buffer(size_t{0}, handle.get_stream()); + vertex_t matrix_partition_frontier_size = static_cast(local_frontier_sizes[i]); + if (GraphViewType::is_multi_gpu) { + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_rank = col_comm.get_rank(); + + resize_dataframe_buffer( + matrix_partition_frontier_key_buffer, matrix_partition_frontier_size, handle.get_stream()); + + if (static_cast(col_comm_rank) == i) { + thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + frontier_key_first, + frontier_key_last, + get_dataframe_buffer_begin(matrix_partition_frontier_key_buffer)); + } + + device_bcast(col_comm, + frontier_key_first, + get_dataframe_buffer_begin(matrix_partition_frontier_key_buffer), + matrix_partition_frontier_size, + i, + handle.get_stream()); + } else { + resize_dataframe_buffer( + matrix_partition_frontier_key_buffer, matrix_partition_frontier_size, handle.get_stream()); + thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + frontier_key_first, + frontier_key_last, + get_dataframe_buffer_begin(matrix_partition_frontier_key_buffer)); + } + + vertex_t const* matrix_partition_frontier_row_first{nullptr}; + vertex_t const* matrix_partition_frontier_row_last{nullptr}; + if constexpr (std::is_same_v) { + matrix_partition_frontier_row_first = + get_dataframe_buffer_begin(matrix_partition_frontier_key_buffer); + matrix_partition_frontier_row_last = + get_dataframe_buffer_end(matrix_partition_frontier_key_buffer); + } else { + matrix_partition_frontier_row_first = + thrust::get<0>(get_dataframe_buffer_begin(matrix_partition_frontier_key_buffer) + .get_iterator_tuple()); + matrix_partition_frontier_row_last = thrust::get<0>( + get_dataframe_buffer_end(matrix_partition_frontier_key_buffer).get_iterator_tuple()); + } + + auto segment_offsets = graph_view.get_local_adj_matrix_partition_segment_offsets(i); + auto use_dcs = + segment_offsets + ? ((*segment_offsets).size() > (detail::num_sparse_segments_per_vertex_partition + 1)) + : false; + + auto max_pushes = + use_dcs ? thrust::transform_reduce( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + matrix_partition_frontier_row_first, + matrix_partition_frontier_row_last, + [matrix_partition, + major_hypersparse_first = + matrix_partition.get_major_first() + + (*segment_offsets) + [detail::num_sparse_segments_per_vertex_partition]] __device__(auto row) { + if (row < major_hypersparse_first) { + auto row_offset = matrix_partition.get_major_offset_from_major_nocheck(row); + return matrix_partition.get_local_degree(row_offset); + } else { + auto row_hypersparse_idx = + matrix_partition.get_major_hypersparse_idx_from_major_nocheck(row); + return row_hypersparse_idx + ? matrix_partition.get_local_degree( + matrix_partition.get_major_offset_from_major_nocheck( + major_hypersparse_first) + + *row_hypersparse_idx) + : edge_t{0}; + } + }, + edge_t{0}, + thrust::plus()) + : thrust::transform_reduce( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + matrix_partition_frontier_row_first, + matrix_partition_frontier_row_last, + [matrix_partition] __device__(auto row) { + auto row_offset = matrix_partition.get_major_offset_from_major_nocheck(row); + return matrix_partition.get_local_degree(row_offset); + }, + edge_t{0}, + thrust::plus()); + + // FIXME: This is highly pessimistic for single GPU (and multi-GPU as well if we maintain + // additional per column data for filtering in e_op). If we can pause & resume execution if + // buffer needs to be increased (and if we reserve address space to avoid expensive + // reallocation; + // https://devblogs.nvidia.com/introducing-low-level-gpu-virtual-memory-management/), we can + // start with a smaller buffer size (especially when the frontier size is large). + // for special cases when we can assure that there is no more than one push per destination + // (e.g. if cugraph::experimental::reduce_op::any is used), we can limit the buffer size to + // std::min(max_pushes, matrix_partition.get_minor_size()). + // For Volta+, we can limit the buffer size to std::min(max_pushes, + // matrix_partition.get_minor_size()) if the reduction operation is a pure function if we use + // locking. + // FIXME: if i != 0, this will require costly reallocation if we don't use the new CUDA feature + // to reserve address space. + auto new_buffer_size = buffer_idx.value(handle.get_stream()) + max_pushes; + resize_dataframe_buffer(key_buffer, new_buffer_size, handle.get_stream()); + if constexpr (!std::is_same_v) { + resize_dataframe_buffer(payload_buffer, new_buffer_size, handle.get_stream()); + } + + auto row_value_input_offset = GraphViewType::is_adj_matrix_transposed + ? vertex_t{0} + : matrix_partition.get_major_value_start_offset(); + if (segment_offsets) { + static_assert(detail::num_sparse_segments_per_vertex_partition == 3); + std::vector h_thresholds(detail::num_sparse_segments_per_vertex_partition + + (use_dcs ? 1 : 0) - 1); + h_thresholds[0] = matrix_partition.get_major_first() + (*segment_offsets)[1]; + h_thresholds[1] = matrix_partition.get_major_first() + (*segment_offsets)[2]; + if (use_dcs) { h_thresholds[2] = matrix_partition.get_major_first() + (*segment_offsets)[3]; } + rmm::device_uvector d_thresholds(h_thresholds.size(), handle.get_stream()); + raft::update_device( + d_thresholds.data(), h_thresholds.data(), h_thresholds.size(), handle.get_stream()); + rmm::device_uvector d_offsets(d_thresholds.size(), handle.get_stream()); + thrust::lower_bound(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + matrix_partition_frontier_row_first, + matrix_partition_frontier_row_last, + d_thresholds.begin(), + d_thresholds.end(), + d_offsets.begin()); + std::vector h_offsets(d_offsets.size()); + raft::update_host(h_offsets.data(), d_offsets.data(), d_offsets.size(), handle.get_stream()); + CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); + h_offsets.push_back(matrix_partition_frontier_size); + // FIXME: we may further improve performance by 1) concurrently running kernels on different + // segments; 2) individually tuning block sizes for different segments; and 3) adding one more + // segment for very high degree vertices and running segmented reduction + if (h_offsets[0] > 0) { + raft::grid_1d_block_t update_grid( + h_offsets[0], + detail::update_frontier_v_push_if_out_nbr_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + detail::for_all_frontier_row_for_all_nbr_high_degree + <<>>( + matrix_partition, + get_dataframe_buffer_begin(matrix_partition_frontier_key_buffer), + get_dataframe_buffer_begin(matrix_partition_frontier_key_buffer) + h_offsets[0], + adj_matrix_row_value_input_first + row_value_input_offset, + adj_matrix_col_value_input_first, + get_dataframe_buffer_begin(key_buffer), + detail::get_optional_payload_buffer_begin(payload_buffer), + buffer_idx.data(), + e_op); + } + if (h_offsets[1] - h_offsets[0] > 0) { + raft::grid_1d_warp_t update_grid( + h_offsets[1] - h_offsets[0], + detail::update_frontier_v_push_if_out_nbr_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + detail::for_all_frontier_row_for_all_nbr_mid_degree + <<>>( + matrix_partition, + get_dataframe_buffer_begin(matrix_partition_frontier_key_buffer) + h_offsets[0], + get_dataframe_buffer_begin(matrix_partition_frontier_key_buffer) + h_offsets[1], + adj_matrix_row_value_input_first + row_value_input_offset, + adj_matrix_col_value_input_first, + get_dataframe_buffer_begin(key_buffer), + detail::get_optional_payload_buffer_begin(payload_buffer), + buffer_idx.data(), + e_op); + } + if (h_offsets[2] - h_offsets[1] > 0) { + raft::grid_1d_thread_t update_grid( + h_offsets[2] - h_offsets[1], + detail::update_frontier_v_push_if_out_nbr_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + detail::for_all_frontier_row_for_all_nbr_low_degree + <<>>( + matrix_partition, + get_dataframe_buffer_begin(matrix_partition_frontier_key_buffer) + h_offsets[1], + get_dataframe_buffer_begin(matrix_partition_frontier_key_buffer) + h_offsets[2], + adj_matrix_row_value_input_first + row_value_input_offset, + adj_matrix_col_value_input_first, + get_dataframe_buffer_begin(key_buffer), + detail::get_optional_payload_buffer_begin(payload_buffer), + buffer_idx.data(), + e_op); + } + if (matrix_partition.get_dcs_nzd_vertex_count() && (h_offsets[3] - h_offsets[2] > 0)) { + raft::grid_1d_thread_t update_grid( + h_offsets[3] - h_offsets[2], + detail::update_frontier_v_push_if_out_nbr_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + detail::for_all_frontier_row_for_all_nbr_hypersparse + <<>>( + matrix_partition, + matrix_partition.get_major_first() + (*segment_offsets)[3], + get_dataframe_buffer_begin(matrix_partition_frontier_key_buffer) + h_offsets[2], + get_dataframe_buffer_begin(matrix_partition_frontier_key_buffer) + h_offsets[3], + adj_matrix_row_value_input_first + row_value_input_offset, + adj_matrix_col_value_input_first, + get_dataframe_buffer_begin(key_buffer), + detail::get_optional_payload_buffer_begin(payload_buffer), + buffer_idx.data(), + e_op); + } + } else { + if (matrix_partition_frontier_size > 0) { + raft::grid_1d_thread_t update_grid( + matrix_partition_frontier_size, + detail::update_frontier_v_push_if_out_nbr_for_all_block_size, + handle.get_device_properties().maxGridSize[0]); + + detail::for_all_frontier_row_for_all_nbr_low_degree + <<>>( + matrix_partition, + get_dataframe_buffer_begin(matrix_partition_frontier_key_buffer), + get_dataframe_buffer_end(matrix_partition_frontier_key_buffer), + adj_matrix_row_value_input_first + row_value_input_offset, + adj_matrix_col_value_input_first, + get_dataframe_buffer_begin(key_buffer), + detail::get_optional_payload_buffer_begin(payload_buffer), + buffer_idx.data(), + e_op); + } + } + } + + if (GraphViewType::is_multi_gpu) { + auto& comm = handle.get_comms(); + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (beginning of col_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + } + + // 2. reduce the buffer + + auto num_buffer_elements = detail::sort_and_reduce_buffer_elements( + handle, + get_dataframe_buffer_begin(key_buffer), + detail::get_optional_payload_buffer_begin(payload_buffer), + buffer_idx.value(handle.get_stream()), + reduce_op); + if (GraphViewType::is_multi_gpu) { + // FIXME: this step is unnecessary if row_comm_size== 1 + auto& comm = handle.get_comms(); + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_size = row_comm.get_size(); + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_rank = col_comm.get_rank(); + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (beginning of row_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + + std::vector h_vertex_lasts(row_comm_size); + for (size_t i = 0; i < h_vertex_lasts.size(); ++i) { + h_vertex_lasts[i] = graph_view.get_vertex_partition_last(col_comm_rank * row_comm_size + i); + } + + rmm::device_uvector d_vertex_lasts(h_vertex_lasts.size(), handle.get_stream()); + raft::update_device( + d_vertex_lasts.data(), h_vertex_lasts.data(), h_vertex_lasts.size(), handle.get_stream()); + rmm::device_uvector d_tx_buffer_last_boundaries(d_vertex_lasts.size(), + handle.get_stream()); + vertex_t const* row_first{nullptr}; + if constexpr (std::is_same_v) { + row_first = get_dataframe_buffer_begin(key_buffer); + } else { + row_first = + thrust::get<0>(get_dataframe_buffer_begin(key_buffer).get_iterator_tuple()); + } + thrust::lower_bound(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + row_first, + row_first + num_buffer_elements, + d_vertex_lasts.begin(), + d_vertex_lasts.end(), + d_tx_buffer_last_boundaries.begin()); + std::vector h_tx_buffer_last_boundaries(d_tx_buffer_last_boundaries.size()); + raft::update_host(h_tx_buffer_last_boundaries.data(), + d_tx_buffer_last_boundaries.data(), + d_tx_buffer_last_boundaries.size(), + handle.get_stream()); + handle.get_stream_view().synchronize(); + std::vector tx_counts(h_tx_buffer_last_boundaries.size()); + std::adjacent_difference( + h_tx_buffer_last_boundaries.begin(), h_tx_buffer_last_boundaries.end(), tx_counts.begin()); + + auto rx_key_buffer = allocate_dataframe_buffer(size_t{0}, handle.get_stream()); + std::tie(rx_key_buffer, std::ignore) = shuffle_values( + row_comm, get_dataframe_buffer_begin(key_buffer), tx_counts, handle.get_stream()); + key_buffer = std::move(rx_key_buffer); + + if constexpr (!std::is_same_v) { + auto rx_payload_buffer = allocate_dataframe_buffer(size_t{0}, handle.get_stream()); + std::tie(rx_payload_buffer, std::ignore) = + shuffle_values(row_comm, + get_dataframe_buffer_begin(payload_buffer), + tx_counts, + handle.get_stream()); + payload_buffer = std::move(rx_payload_buffer); + } + + num_buffer_elements = detail::sort_and_reduce_buffer_elements( + handle, + get_dataframe_buffer_begin(key_buffer), + detail::get_optional_payload_buffer_begin(payload_buffer), + size_dataframe_buffer(key_buffer), + reduce_op); + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (end of row_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + } + + // 3. update vertex properties and frontier + + if (num_buffer_elements > 0) { + static_assert(VertexFrontierType::kNumBuckets <= std::numeric_limits::max()); + rmm::device_uvector bucket_indices(num_buffer_elements, handle.get_stream()); + + auto vertex_partition = vertex_partition_device_view_t( + graph_view.get_vertex_partition_view()); + + if constexpr (!std::is_same_v) { + auto key_payload_pair_first = thrust::make_zip_iterator( + thrust::make_tuple(get_dataframe_buffer_begin(key_buffer), + detail::get_optional_payload_buffer_begin(payload_buffer))); + thrust::transform( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + key_payload_pair_first, + key_payload_pair_first + num_buffer_elements, + bucket_indices.begin(), + [vertex_value_input_first, + vertex_value_output_first, + v_op, + vertex_partition, + invalid_bucket_idx = VertexFrontierType::kInvalidBucketIdx] __device__(auto pair) { + auto key = thrust::get<0>(pair); + auto payload = thrust::get<1>(pair); + vertex_t v_offset{}; + if constexpr (std::is_same_v) { + v_offset = vertex_partition.get_local_vertex_offset_from_vertex_nocheck(key); + } else { + v_offset = + vertex_partition.get_local_vertex_offset_from_vertex_nocheck(thrust::get<0>(key)); + } + auto v_val = *(vertex_value_input_first + v_offset); + auto v_op_result = v_op(key, v_val, payload); + if (v_op_result) { + *(vertex_value_output_first + v_offset) = thrust::get<1>(*v_op_result); + return static_cast(thrust::get<0>(*v_op_result)); + } else { + return std::numeric_limits::max(); + } + }); + + resize_dataframe_buffer(payload_buffer, size_t{0}, handle.get_stream()); + shrink_to_fit_dataframe_buffer(payload_buffer, handle.get_stream()); + } else { + thrust::transform( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + get_dataframe_buffer_begin(key_buffer), + get_dataframe_buffer_begin(key_buffer) + num_buffer_elements, + bucket_indices.begin(), + detail::call_v_op_t{vertex_value_input_first, + vertex_value_output_first, + v_op, + vertex_partition, + VertexFrontierType::kInvalidBucketIdx}); + } + + auto bucket_key_pair_first = thrust::make_zip_iterator( + thrust::make_tuple(bucket_indices.begin(), get_dataframe_buffer_begin(key_buffer))); + bucket_indices.resize( + thrust::distance( + bucket_key_pair_first, + thrust::remove_if(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + bucket_key_pair_first, + bucket_key_pair_first + num_buffer_elements, + detail::check_invalid_bucket_idx_t())), + handle.get_stream()); + resize_dataframe_buffer(key_buffer, bucket_indices.size(), handle.get_stream()); + bucket_indices.shrink_to_fit(handle.get_stream()); + shrink_to_fit_dataframe_buffer(key_buffer, handle.get_stream()); + + frontier.insert_to_buckets(bucket_indices.begin(), + bucket_indices.end(), + get_dataframe_buffer_begin(key_buffer), + next_frontier_bucket_indices); + } +} + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/include/cugraph/prims/vertex_frontier.cuh b/cpp/include/cugraph/prims/vertex_frontier.cuh new file mode 100644 index 00000000000..22c7ca867f5 --- /dev/null +++ b/cpp/include/cugraph/prims/vertex_frontier.cuh @@ -0,0 +1,470 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace cugraph { +namespace experimental { + +// stores unique key objects in the sorted (non-descending) order; key type is either vertex_t +// (tag_t == void) or thrust::tuple (tag_t != void) +template +class SortedUniqueKeyBucket { + static_assert(std::is_same_v || std::is_arithmetic_v); + + using optional_buffer_type = std:: + conditional_t, std::byte /* dummy */, rmm::device_uvector>; + + public: + template >* = nullptr> + SortedUniqueKeyBucket(raft::handle_t const& handle) + : handle_ptr_(&handle), vertices_(0, handle.get_stream()), tags_(std::byte{0}) + { + } + + template >* = nullptr> + SortedUniqueKeyBucket(raft::handle_t const& handle) + : handle_ptr_(&handle), vertices_(0, handle.get_stream()), tags_(0, handle.get_stream()) + { + } + + /** + * @ brief insert a vertex to the bucket + * + * @param vertex vertex to insert + */ + template >* = nullptr> + void insert(vertex_t vertex) + { + if (vertices_.size() > 0) { + rmm::device_scalar tmp(vertex, handle_ptr_->get_stream()); + insert(tmp.data(), tmp.data() + 1); + } else { + vertices_.resize(1, handle_ptr_->get_stream()); + raft::update_device(vertices_.data(), &vertex, size_t{1}, handle_ptr_->get_stream()); + } + } + + /** + * @ brief insert a (vertex, tag) pair to the bucket + * + * @param vertex vertex of the (vertex, tag) pair to insert + * @param tag tag of the (vertex, tag) pair to insert + */ + template >* = nullptr> + void insert(thrust::tuple key) + { + if (vertices_.size() > 0) { + rmm::device_scalar tmp_vertex(thrust::get<0>(key), handle_ptr_->get_stream()); + rmm::device_scalar tmp_tag(thrust::get<1>(key), handle_ptr_->get_stream()); + auto pair_first = + thrust::make_zip_iterator(thrust::make_tuple(tmp_vertex.data(), tmp_tag.data())); + insert(pair_first, pair_first + 1); + } else { + vertices_.resize(1, handle_ptr_->get_stream()); + tags_.resize(1, handle_ptr_->get_stream()); + auto pair_first = + thrust::make_tuple(thrust::make_zip_iterator(vertices_.begin(), tags_.begin())); + thrust::fill(rmm::exec_policy(handle_ptr_->get_stream())->on(handle_ptr_->get_stream()), + pair_first, + pair_first + 1, + key); + } + } + + /** + * @ brief insert a list of vertices to the bucket + * + * @param vertex_first Iterator pointing to the first (inclusive) element of the vertices stored + * in device memory. + * @param vertex_last Iterator pointing to the last (exclusive) element of the vertices stored in + * device memory. + */ + template >* = nullptr> + void insert(VertexIterator vertex_first, VertexIterator vertex_last) + { + static_assert( + std::is_same_v::value_type, vertex_t>); + + if (vertices_.size() > 0) { + rmm::device_uvector merged_vertices( + vertices_.size() + thrust::distance(vertex_first, vertex_last), handle_ptr_->get_stream()); + thrust::merge(rmm::exec_policy(handle_ptr_->get_stream())->on(handle_ptr_->get_stream()), + vertices_.begin(), + vertices_.end(), + vertex_first, + vertex_last, + merged_vertices.begin()); + merged_vertices.resize( + thrust::distance( + merged_vertices.begin(), + thrust::unique(rmm::exec_policy(handle_ptr_->get_stream())->on(handle_ptr_->get_stream()), + merged_vertices.begin(), + merged_vertices.end())), + handle_ptr_->get_stream()); + merged_vertices.shrink_to_fit(handle_ptr_->get_stream()); + vertices_ = std::move(merged_vertices); + } else { + vertices_.resize(thrust::distance(vertex_first, vertex_last), handle_ptr_->get_stream()); + thrust::copy(rmm::exec_policy(handle_ptr_->get_stream())->on(handle_ptr_->get_stream()), + vertex_first, + vertex_last, + vertices_.begin()); + } + } + + /** + * @ brief insert a list of (vertex, tag) pairs to the bucket + * + * @param key_first Iterator pointing to the first (inclusive) element of the (vertex,tag) pairs + * stored in device memory. + * @param key_last Iterator pointing to the last (exclusive) element of the (vertex,tag) pairs + * stored in device memory. + */ + template >* = nullptr> + void insert(KeyIterator key_first, KeyIterator key_last) + { + static_assert(std::is_same_v::value_type, + thrust::tuple>); + + if (vertices_.size() > 0) { + rmm::device_uvector merged_vertices( + vertices_.size() + thrust::distance(key_first, key_last), handle_ptr_->get_stream()); + rmm::device_uvector merged_tags(merged_vertices.size(), handle_ptr_->get_stream()); + auto old_pair_first = + thrust::make_zip_iterator(thrust::make_tuple(vertices_.begin(), tags_.begin())); + auto merged_pair_first = + thrust::make_zip_iterator(thrust::make_tuple(merged_vertices.begin(), merged_tags.begin())); + thrust::merge(rmm::exec_policy(handle_ptr_->get_stream())->on(handle_ptr_->get_stream()), + old_pair_first, + old_pair_first + vertices_.size(), + key_first, + key_last, + merged_pair_first); + merged_vertices.resize( + thrust::distance( + merged_pair_first, + thrust::unique(rmm::exec_policy(handle_ptr_->get_stream())->on(handle_ptr_->get_stream()), + merged_pair_first, + merged_pair_first + merged_vertices.size())), + handle_ptr_->get_stream()); + merged_tags.resize(merged_vertices.size(), handle_ptr_->get_stream()); + merged_vertices.shrink_to_fit(handle_ptr_->get_stream()); + merged_tags.shrink_to_fit(handle_ptr_->get_stream()); + vertices_ = std::move(merged_vertices); + tags_ = std::move(merged_tags); + } else { + vertices_.resize(thrust::distance(key_first, key_last), handle_ptr_->get_stream()); + tags_.resize(thrust::distance(key_first, key_last), handle_ptr_->get_stream()); + thrust::copy(rmm::exec_policy(handle_ptr_->get_stream())->on(handle_ptr_->get_stream()), + key_first, + key_last, + thrust::make_zip_iterator(thrust::make_tuple(vertices_.begin(), tags_.begin()))); + } + } + + size_t size() const { return vertices_.size(); } + + template + std::enable_if_t aggregate_size() const + { + return host_scalar_allreduce( + handle_ptr_->get_comms(), vertices_.size(), handle_ptr_->get_stream()); + } + + template + std::enable_if_t aggregate_size() const + { + return vertices_.size(); + } + + void resize(size_t size) + { + vertices_.resize(size, handle_ptr_->get_stream()); + if constexpr (!std::is_same_v) { tags_.resize(size, handle_ptr_->get_stream()); } + } + + void clear() { resize(0); } + + void shrink_to_fit() + { + vertices_.shrink_to_fit(handle_ptr_->get_stream()); + if constexpr (!std::is_same_v) { tags_.shrink_to_fit(handle_ptr_->get_stream()); } + } + +// FIXME: to silence the spurious warning (missing return statement ...) due to the nvcc bug +// (https://stackoverflow.com/questions/64523302/cuda-missing-return-statement-at-end-of-non-void- +// function-in-constexpr-if-fun) +#if 1 + template >* = nullptr> + auto const begin() const + { + return vertices_.begin(); + } + + template >* = nullptr> + auto begin() + { + return vertices_.begin(); + } + + template >* = nullptr> + auto const begin() const + { + return thrust::make_zip_iterator(thrust::make_tuple(vertices_.begin(), tags_.begin())); + } + + template >* = nullptr> + auto begin() + { + return thrust::make_zip_iterator(thrust::make_tuple(vertices_.begin(), tags_.begin())); + } +#else + auto const begin() const + { + if constexpr (std::is_same_v) { + return vertices_.begin(); + } else { + return thrust::make_zip_iterator(thrust::make_tuple(vertices_.begin(), tags_.begin())); + } + } + + auto begin() + { + if constexpr (std::is_same_v) { + return vertices_.begin(); + } else { + return thrust::make_zip_iterator(thrust::make_tuple(vertices_.begin(), tags_.begin())); + } + } +#endif + + auto const end() const { return begin() + vertices_.size(); } + + auto end() { return begin() + vertices_.size(); } + + private: + raft::handle_t const* handle_ptr_{nullptr}; + rmm::device_uvector vertices_; + optional_buffer_type tags_; +}; + +template +class VertexFrontier { + static_assert(std::is_same_v || std::is_arithmetic_v); + + public: + using key_type = + std::conditional_t, vertex_t, thrust::tuple>; + static size_t constexpr kNumBuckets = num_buckets; + static size_t constexpr kInvalidBucketIdx{std::numeric_limits::max()}; + + VertexFrontier(raft::handle_t const& handle) : handle_ptr_(&handle) + { + for (size_t i = 0; i < num_buckets; ++i) { + buckets_.emplace_back(handle); + } + } + + SortedUniqueKeyBucket& get_bucket(size_t bucket_idx) + { + return buckets_[bucket_idx]; + } + + SortedUniqueKeyBucket const& get_bucket(size_t bucket_idx) const + { + return buckets_[bucket_idx]; + } + + void swap_buckets(size_t bucket_idx0, size_t bucket_idx1) + { + std::swap(buckets_[bucket_idx0], buckets_[bucket_idx1]); + } + + template + void split_bucket(size_t this_bucket_idx, + std::vector const& move_to_bucket_indices, + SplitOp split_op) + { + auto& this_bucket = get_bucket(this_bucket_idx); + if (this_bucket.size() == 0) { return; } + + // 1. apply split_op to each bucket element + + static_assert(kNumBuckets <= std::numeric_limits::max()); + rmm::device_uvector bucket_indices(this_bucket.size(), handle_ptr_->get_stream()); + thrust::transform( + rmm::exec_policy(handle_ptr_->get_stream())->on(handle_ptr_->get_stream()), + this_bucket.begin(), + this_bucket.end(), + bucket_indices.begin(), + [split_op] __device__(auto key) { + auto split_op_result = split_op(key); + return static_cast(split_op_result ? *split_op_result : kInvalidBucketIdx); + }); + + // 2. remove elements with the invalid bucket indices + + auto pair_first = + thrust::make_zip_iterator(thrust::make_tuple(bucket_indices.begin(), this_bucket.begin())); + bucket_indices.resize( + thrust::distance(pair_first, + thrust::remove_if( + rmm::exec_policy(handle_ptr_->get_stream())->on(handle_ptr_->get_stream()), + pair_first, + pair_first + bucket_indices.size(), + [] __device__(auto pair) { + return thrust::get<0>(pair) == static_cast(kInvalidBucketIdx); + })), + handle_ptr_->get_stream()); + this_bucket.resize(bucket_indices.size()); + bucket_indices.shrink_to_fit(handle_ptr_->get_stream()); + this_bucket.shrink_to_fit(); + + // 3. separte the elements to stay in this bucket from the elements to be moved to other buckets + + pair_first = + thrust::make_zip_iterator(thrust::make_tuple(bucket_indices.begin(), this_bucket.begin())); + auto new_this_bucket_size = static_cast(thrust::distance( + pair_first, + thrust::stable_partition( // stalbe_partition to maintain sorted order within each bucket + rmm::exec_policy(handle_ptr_->get_stream())->on(handle_ptr_->get_stream()), + pair_first, + pair_first + bucket_indices.size(), + [this_bucket_idx = static_cast(this_bucket_idx)] __device__(auto pair) { + return thrust::get<0>(pair) == this_bucket_idx; + }))); + + // 4. insert to target buckets and resize this bucket + + insert_to_buckets(bucket_indices.begin() + new_this_bucket_size, + bucket_indices.end(), + this_bucket.begin() + new_this_bucket_size, + move_to_bucket_indices); + + this_bucket.resize(new_this_bucket_size); + this_bucket.shrink_to_fit(); + } + + template + void insert_to_buckets(uint8_t* bucket_idx_first /* [INOUT] */, + uint8_t* bucket_idx_last /* [INOUT] */, + KeyIterator key_first /* [INOUT] */, + std::vector const& to_bucket_indices) + { + // 1. group the elements by their target bucket indices + + auto pair_first = thrust::make_zip_iterator(thrust::make_tuple(bucket_idx_first, key_first)); + auto pair_last = pair_first + thrust::distance(bucket_idx_first, bucket_idx_last); + + std::vector insert_bucket_indices{}; + std::vector insert_offsets{}; + std::vector insert_sizes{}; + if (to_bucket_indices.size() == 1) { + insert_bucket_indices = to_bucket_indices; + insert_offsets = {0}; + insert_sizes = {static_cast(thrust::distance(pair_first, pair_last))}; + } else if (to_bucket_indices.size() == 2) { + auto next_bucket_size = static_cast(thrust::distance( + pair_first, + thrust::stable_partition( // stalbe_partition to maintain sorted order within each bucket + rmm::exec_policy(handle_ptr_->get_stream())->on(handle_ptr_->get_stream()), + pair_first, + pair_last, + [next_bucket_idx = static_cast(to_bucket_indices[0])] __device__(auto pair) { + return thrust::get<0>(pair) == next_bucket_idx; + }))); + insert_bucket_indices = to_bucket_indices; + insert_offsets = {0, next_bucket_size}; + insert_sizes = { + next_bucket_size, + static_cast(thrust::distance(pair_first + next_bucket_size, pair_last))}; + } else { + thrust::stable_sort( // stalbe_sort to maintain sorted order within each bucket + rmm::exec_policy(handle_ptr_->get_stream())->on(handle_ptr_->get_stream()), + pair_first, + pair_last, + [] __device__(auto lhs, auto rhs) { return thrust::get<0>(lhs) < thrust::get<0>(rhs); }); + rmm::device_uvector d_indices(to_bucket_indices.size(), handle_ptr_->get_stream()); + rmm::device_uvector d_counts(d_indices.size(), handle_ptr_->get_stream()); + auto it = thrust::reduce_by_key( + rmm::exec_policy(handle_ptr_->get_stream())->on(handle_ptr_->get_stream()), + bucket_idx_first, + bucket_idx_last, + thrust::make_constant_iterator(size_t{1}), + d_indices.begin(), + d_counts.begin()); + d_indices.resize(thrust::distance(d_indices.begin(), thrust::get<0>(it)), + handle_ptr_->get_stream()); + d_counts.resize(d_indices.size(), handle_ptr_->get_stream()); + std::vector h_indices(d_indices.size()); + std::vector h_counts(h_indices.size()); + raft::update_host( + h_indices.data(), d_indices.data(), d_indices.size(), handle_ptr_->get_stream()); + raft::update_host( + h_counts.data(), d_counts.data(), d_counts.size(), handle_ptr_->get_stream()); + handle_ptr_->get_stream_view().synchronize(); + + size_t offset{0}; + for (size_t i = 0; i < h_indices.size(); ++i) { + insert_bucket_indices[i] = static_cast(h_indices[i]); + insert_offsets[i] = offset; + insert_sizes[i] = h_counts[i]; + offset += insert_sizes[i]; + } + } + + // 2. insert to the target buckets + + for (size_t i = 0; i < insert_offsets.size(); ++i) { + get_bucket(insert_bucket_indices[i]) + .insert(key_first + insert_offsets[i], key_first + (insert_offsets[i] + insert_sizes[i])); + } + } + + private: + raft::handle_t const* handle_ptr_{nullptr}; + std::vector> buckets_{}; +}; + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/include/cugraph/serialization/serializer.hpp b/cpp/include/cugraph/serialization/serializer.hpp new file mode 100644 index 00000000000..240df1d304a --- /dev/null +++ b/cpp/include/cugraph/serialization/serializer.hpp @@ -0,0 +1,208 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// +#pragma once + +#include + +#include + +#include + +#include +#include + +namespace cugraph { +namespace serializer { + +using namespace cugraph::experimental; + +class serializer_t { + public: + using byte_t = uint8_t; + + using device_byte_it = typename rmm::device_uvector::iterator; + using device_byte_cit = typename rmm::device_uvector::const_iterator; + + // cnstr. for serialize() path: + // + serializer_t(raft::handle_t const& handle, size_t total_sz_bytes) + : handle_(handle), + d_storage_(total_sz_bytes, handle.get_stream()), + begin_(d_storage_.begin()), + cbegin_(d_storage_.begin()) + { + } + + // cnstr. for unserialize() path: + // + serializer_t(raft::handle_t const& handle, byte_t const* ptr_d_storage) + : handle_(handle), d_storage_(0, handle.get_stream()), cbegin_(ptr_d_storage) + { + } + + template + struct graph_meta_t; + + template + struct graph_meta_t> { + // purposely empty, for now; + // FIXME: provide implementation for multi-gpu version + }; + + template + struct graph_meta_t> { + using vertex_t = typename graph_t::vertex_type; + using bool_ser_t = uint8_t; + + graph_meta_t(void) {} + + explicit graph_meta_t(graph_t const& graph) + : num_vertices_(graph.get_number_of_vertices()), + num_edges_(graph.get_number_of_edges()), + properties_(graph.get_graph_properties()), + is_weighted_(graph.is_weighted()), + segment_offsets_(graph.view().get_local_adj_matrix_partition_segment_offsets(0)) + { + } + + graph_meta_t(size_t num_vertices, + size_t num_edges, + graph_properties_t const& properties, + bool is_weighted, + std::optional> const& segment_offsets) + : num_vertices_(num_vertices), + num_edges_(num_edges), + properties_(properties), + is_weighted_(is_weighted), + segment_offsets_(segment_offsets) + { + } + + size_t num_vertices_; + size_t num_edges_; + graph_properties_t properties_{}; + bool is_weighted_{}; + std::optional> segment_offsets_{}; + + size_t get_device_sz_bytes(void) const + { + return 2 * sizeof(size_t) + + (segment_offsets_ ? (*segment_offsets_).size() : size_t{0}) * sizeof(vertex_t) + + 3 * sizeof(bool_ser_t); + } + }; + + // POD-type serialization: + // + template + void serialize(value_t val); + + // POD-type unserialization: + // + template + value_t unserialize(void); + + // device array serialization: + // + template + void serialize(value_t const* p_d_src, size_t size); + + // device vector unserialization; + // extracts device_uvector of `size` bytes_to_value_t elements: + // + template + rmm::device_uvector unserialize( + size_t size); // size of device vector to be unserialized + + // graph serialization, + // with device storage and host metadata: + // (associated with target; e.g., num_vertices, etc.) + // + template + void serialize(graph_t const& graph, graph_meta_t& gmeta); // serialization target + + // graph unserialization, + // with device storage and host metadata: + // (associated with target; e.g., num_vertices, etc.) + // + template + graph_t unserialize(size_t device_sz_bytes, size_t host_sz_bytes); + + template + static std::pair get_device_graph_sz_bytes( + graph_meta_t const& graph_meta) + { + using vertex_t = typename graph_t::vertex_type; + using edge_t = typename graph_t::edge_type; + using weight_t = typename graph_t::weight_type; + + if constexpr (!graph_t::is_multi_gpu) { + size_t num_vertices = graph_meta.num_vertices_; + size_t num_edges = graph_meta.num_edges_; + + size_t weight_storage_sz = graph_meta.is_weighted_ ? num_edges * sizeof(weight_t) : 0; + + size_t device_ser_sz = + (num_vertices + 1) * sizeof(edge_t) + num_edges * sizeof(vertex_t) + weight_storage_sz; + + size_t host_ser_sz = graph_meta.get_device_sz_bytes(); + + return std::make_pair( + device_ser_sz, + host_ser_sz); // FIXME: remove when host_bcast() becomes available for host vectors + + } else { + CUGRAPH_FAIL("Unsupported graph type for un/serialization."); + + return std::pair{}; + } + } + + template + static std::pair get_device_graph_sz_bytes(graph_t const& graph) + { + graph_meta_t gmeta{graph}; + return get_device_graph_sz_bytes(gmeta); + } + + byte_t const* get_storage(void) const { return d_storage_.begin(); } + byte_t* get_storage(void) { return d_storage_.begin(); } + + private: + // serialization of graph metadata, via device orchestration: + // + template + void serialize(graph_meta_t const& graph_meta); + + // unserialization of graph metadata, via device orchestration: + // + template + graph_meta_t unserialize( + size_t graph_meta_sz_bytes, + graph_meta_t const& empty_meta); // tag dispatching to avoid conflict with + // `unserialize(size_t)` for device vectors + + raft::handle_t const& handle_; + rmm::device_uvector d_storage_; + device_byte_it begin_{nullptr}; // advances on serialize() + device_byte_cit cbegin_{nullptr}; // advances on unserialize() +}; + +} // namespace serializer +} // namespace cugraph diff --git a/cpp/include/cugraph/utilities/collect_comm.cuh b/cpp/include/cugraph/utilities/collect_comm.cuh new file mode 100644 index 00000000000..1e15afea1e5 --- /dev/null +++ b/cpp/include/cugraph/utilities/collect_comm.cuh @@ -0,0 +1,266 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +namespace cugraph { +namespace experimental { + +// for key = [map_key_first, map_key_last), key_to_gpu_id_op(key) should be coincide with +// comm.get_rank() +template +decltype(allocate_dataframe_buffer::value_type>( + 0, cudaStream_t{nullptr})) +collect_values_for_keys(raft::comms::comms_t const& comm, + VertexIterator0 map_key_first, + VertexIterator0 map_key_last, + ValueIterator map_value_first, + VertexIterator1 collect_key_first, + VertexIterator1 collect_key_last, + KeyToGPUIdOp key_to_gpu_id_op, + rmm::cuda_stream_view stream_view) +{ + using vertex_t = typename std::iterator_traits::value_type; + static_assert( + std::is_same::value_type, vertex_t>::value); + using value_t = typename std::iterator_traits::value_type; + + double constexpr load_factor = 0.7; + + // FIXME: we may compare the performance & memory footprint of this hash based approach vs binary + // search based approach (especially when thrust::distance(collect_key_first, collect_key_last) << + // thrust::distance(map_key_first, map_key_last) + + // 1. build a cuco::static_map object for the map k, v pairs. + + auto poly_alloc = rmm::mr::polymorphic_allocator(rmm::mr::get_current_device_resource()); + auto stream_adapter = rmm::mr::make_stream_allocator_adaptor(poly_alloc, stream_view); + auto kv_map_ptr = std::make_unique< + cuco::static_map>( + // cuco::static_map requires at least one empty slot + std::max(static_cast( + static_cast(thrust::distance(map_key_first, map_key_last)) / load_factor), + static_cast(thrust::distance(map_key_first, map_key_last)) + 1), + invalid_vertex_id::value, + invalid_vertex_id::value, + stream_adapter); + { + auto pair_first = thrust::make_zip_iterator(thrust::make_tuple(map_key_first, map_value_first)); + kv_map_ptr->insert(pair_first, pair_first + thrust::distance(map_key_first, map_key_last)); + } + + // 2. collect values for the unique keys in [collect_key_first, collect_key_last) + + rmm::device_uvector unique_keys(thrust::distance(collect_key_first, collect_key_last), + stream_view); + thrust::copy( + rmm::exec_policy(stream_view), collect_key_first, collect_key_last, unique_keys.begin()); + thrust::sort(rmm::exec_policy(stream_view), unique_keys.begin(), unique_keys.end()); + unique_keys.resize( + thrust::distance( + unique_keys.begin(), + thrust::unique(rmm::exec_policy(stream_view), unique_keys.begin(), unique_keys.end())), + stream_view); + + rmm::device_uvector values_for_unique_keys(0, stream_view); + { + rmm::device_uvector rx_unique_keys(0, stream_view); + std::vector rx_value_counts{}; + std::tie(rx_unique_keys, rx_value_counts) = groupby_gpuid_and_shuffle_values( + comm, + unique_keys.begin(), + unique_keys.end(), + [key_to_gpu_id_op] __device__(auto val) { return key_to_gpu_id_op(val); }, + stream_view); + + rmm::device_uvector values_for_rx_unique_keys(rx_unique_keys.size(), stream_view); + + stream_view.synchronize(); // cuco::static_map currently does not take stream + + kv_map_ptr->find( + rx_unique_keys.begin(), rx_unique_keys.end(), values_for_rx_unique_keys.begin()); + + rmm::device_uvector rx_values_for_unique_keys(0, stream_view); + std::tie(rx_values_for_unique_keys, std::ignore) = + shuffle_values(comm, values_for_rx_unique_keys.begin(), rx_value_counts, stream_view); + + values_for_unique_keys = std::move(rx_values_for_unique_keys); + } + + // 3. re-build a cuco::static_map object for the k, v pairs in unique_keys, + // values_for_unique_keys. + + stream_view.synchronize(); // cuco::static_map currently does not take stream + + kv_map_ptr.reset(); + + kv_map_ptr = std::make_unique< + cuco::static_map>( + // cuco::static_map requires at least one empty slot + std::max(static_cast(static_cast(unique_keys.size()) / load_factor), + unique_keys.size() + 1), + invalid_vertex_id::value, + invalid_vertex_id::value, + stream_adapter); + { + auto pair_first = thrust::make_zip_iterator( + thrust::make_tuple(unique_keys.begin(), values_for_unique_keys.begin())); + kv_map_ptr->insert(pair_first, pair_first + unique_keys.size()); + } + + // 4. find values for [collect_key_first, collect_key_last) + + auto value_buffer = allocate_dataframe_buffer( + thrust::distance(collect_key_first, collect_key_last), stream_view); + kv_map_ptr->find( + collect_key_first, collect_key_last, get_dataframe_buffer_begin(value_buffer)); + + return value_buffer; +} + +// for key = [map_key_first, map_key_last), key_to_gpu_id_op(key) should be coincide with +// comm.get_rank() +template +decltype(allocate_dataframe_buffer::value_type>( + 0, cudaStream_t{nullptr})) +collect_values_for_unique_keys(raft::comms::comms_t const& comm, + VertexIterator0 map_key_first, + VertexIterator0 map_key_last, + ValueIterator map_value_first, + VertexIterator1 collect_unique_key_first, + VertexIterator1 collect_unique_key_last, + KeyToGPUIdOp key_to_gpu_id_op, + rmm::cuda_stream_view stream_view) +{ + using vertex_t = typename std::iterator_traits::value_type; + static_assert( + std::is_same::value_type, vertex_t>::value); + using value_t = typename std::iterator_traits::value_type; + + double constexpr load_factor = 0.7; + + // FIXME: we may compare the performance & memory footprint of this hash based approach vs binary + // search based approach (especially when thrust::distance(collect_unique_key_first, + // collect_unique_key_last) << thrust::distance(map_key_first, map_key_last) + + // 1. build a cuco::static_map object for the map k, v pairs. + + auto poly_alloc = rmm::mr::polymorphic_allocator(rmm::mr::get_current_device_resource()); + auto stream_adapter = rmm::mr::make_stream_allocator_adaptor(poly_alloc, stream_view); + auto kv_map_ptr = std::make_unique< + cuco::static_map>( + // cuco::static_map requires at least one empty slot + std::max(static_cast( + static_cast(thrust::distance(map_key_first, map_key_last)) / load_factor), + static_cast(thrust::distance(map_key_first, map_key_last)) + 1), + invalid_vertex_id::value, + invalid_vertex_id::value, + stream_adapter); + { + auto pair_first = thrust::make_zip_iterator(thrust::make_tuple(map_key_first, map_value_first)); + kv_map_ptr->insert(pair_first, pair_first + thrust::distance(map_key_first, map_key_last)); + } + + // 2. collect values for the unique keys in [collect_unique_key_first, collect_unique_key_last) + + rmm::device_uvector unique_keys( + thrust::distance(collect_unique_key_first, collect_unique_key_last), stream_view); + thrust::copy(rmm::exec_policy(stream_view), + collect_unique_key_first, + collect_unique_key_last, + unique_keys.begin()); + + rmm::device_uvector values_for_unique_keys(0, stream_view); + { + rmm::device_uvector rx_unique_keys(0, stream_view); + std::vector rx_value_counts{}; + std::tie(rx_unique_keys, rx_value_counts) = groupby_gpuid_and_shuffle_values( + comm, + unique_keys.begin(), + unique_keys.end(), + [key_to_gpu_id_op] __device__(auto val) { return key_to_gpu_id_op(val); }, + stream_view); + + rmm::device_uvector values_for_rx_unique_keys(rx_unique_keys.size(), stream_view); + + stream_view.synchronize(); // cuco::static_map currently does not take stream + + kv_map_ptr->find( + rx_unique_keys.begin(), rx_unique_keys.end(), values_for_rx_unique_keys.begin()); + + rmm::device_uvector rx_values_for_unique_keys(0, stream_view); + std::tie(rx_values_for_unique_keys, std::ignore) = + shuffle_values(comm, values_for_rx_unique_keys.begin(), rx_value_counts, stream_view); + + values_for_unique_keys = std::move(rx_values_for_unique_keys); + } + + // 3. re-build a cuco::static_map object for the k, v pairs in unique_keys, + // values_for_unique_keys. + + stream_view.synchronize(); // cuco::static_map currently does not take stream + + kv_map_ptr.reset(); + + kv_map_ptr = std::make_unique< + cuco::static_map>( + // cuco::static_map requires at least one empty slot + std::max(static_cast(static_cast(unique_keys.size()) / load_factor), + unique_keys.size() + 1), + invalid_vertex_id::value, + invalid_vertex_id::value, + stream_adapter); + { + auto pair_first = thrust::make_zip_iterator( + thrust::make_tuple(unique_keys.begin(), values_for_unique_keys.begin())); + kv_map_ptr->insert(pair_first, pair_first + unique_keys.size()); + } + + // 4. find values for [collect_unique_key_first, collect_unique_key_last) + + auto value_buffer = allocate_dataframe_buffer( + thrust::distance(collect_unique_key_first, collect_unique_key_last), stream_view); + kv_map_ptr->find(collect_unique_key_first, + collect_unique_key_last, + get_dataframe_buffer_begin(value_buffer)); + + return value_buffer; +} + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/include/cugraph/utilities/cython.hpp b/cpp/include/cugraph/utilities/cython.hpp new file mode 100644 index 00000000000..2bd4e86aa7e --- /dev/null +++ b/cpp/include/cugraph/utilities/cython.hpp @@ -0,0 +1,609 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include +#include +#include + +#include + +#include + +namespace cugraph { +namespace cython { + +enum class numberTypeEnum : int { int32Type, int64Type, floatType, doubleType }; + +enum class graphTypeEnum : int { + // represents unintiialized or NULL ptr + null, + // represents some legacy Cxx type. This and other LegacyCxx values are not + // used for the unique_ptr in a graph_container_t, but instead for when this + // enum is used for determining high-level code paths to take to prevent + // needing to expose each legacy enum value to cython. + LegacyCSR, + LegacyCSC, + LegacyCOO, + // represents that a GraphCxxView* unique_ptr type is present in a + // graph_container_t. + GraphCSRViewFloat, + GraphCSRViewDouble, + GraphCSCViewFloat, + GraphCSCViewDouble, + GraphCOOViewFloat, + GraphCOOViewDouble, + // represents values present in the graph_container_t to construct a graph_t, + // but unlike legacy classes does not mean a graph_t unique_ptr is present in + // the container. + graph_t, +}; + +// "container" for a graph type instance which insulates the owner from the +// specifics of the actual graph type. This is intended to be used in Cython +// code that only needs to pass a graph object to another wrapped C++ API. This +// greatly simplifies the Cython code since the Cython definition only needs to +// define the container and not the various individual graph types in Cython. +struct graph_container_t { + // FIXME: This union is in place only to support legacy calls, remove when + // migration to graph_t types is complete, or when legacy graph objects are + // constructed in the call_< wrappers instead of the + // populate_graph_container_legacy() function. + union graphPtrUnion { + ~graphPtrUnion() {} + + void* null; + std::unique_ptr> GraphCSRViewFloatPtr; + std::unique_ptr> GraphCSRViewDoublePtr; + std::unique_ptr> GraphCSCViewFloatPtr; + std::unique_ptr> GraphCSCViewDoublePtr; + std::unique_ptr> GraphCOOViewFloatPtr; + std::unique_ptr> GraphCOOViewDoublePtr; + }; + + graph_container_t() : graph_ptr_union{nullptr}, graph_type{graphTypeEnum::null} {} + ~graph_container_t() {} + + // The expected usage of a graph_container_t is for it to be created as part + // of a cython wrapper simply for passing a templated instantiation of a + // particular graph class from one call to another, and not to exist outside + // of the individual wrapper function (deleted when the instance goes out of + // scope once the wrapper function returns). Therefore, copys and assignments + // to an instance are not supported and these methods are deleted. + graph_container_t(const graph_container_t&) = delete; + graph_container_t& operator=(const graph_container_t&) = delete; + + graphPtrUnion graph_ptr_union; + graphTypeEnum graph_type; + + // primitive data used for constructing graph_t instances. + void* src_vertices; + void* dst_vertices; + void* weights; + bool is_weighted; + void* vertex_partition_offsets; + void* segment_offsets; + size_t num_segments; + + size_t num_local_edges; + size_t num_global_vertices; + size_t num_global_edges; + numberTypeEnum vertexType; + numberTypeEnum edgeType; + numberTypeEnum weightType; + bool transposed; + bool is_multi_gpu; + bool do_expensive_check; + int row_comm_size; + int col_comm_size; + int row_comm_rank; + int col_comm_rank; + experimental::graph_properties_t graph_props; +}; + +/** + * @brief Owning struct. Allows returning multiple edge lists and edge offsets. + * cython only + * + * @param number_of_vertices The total number of vertices + * @param number_of_edges The total number of edges (number of elements in src_indices, + dst_indices and edge_data) + * @param number_of_subgraph The number of subgraphs, number of elements in subgraph_offsets - 1 + * @param source_indices This array of size E (number of edges) contains + * the index of the + * source for each edge. Indices must be in the range [0, V-1]. + * @param destination_indices This array of size E (number of edges) contains + * the index of the + * destination for each edge. Indices must be in the range [0, V-1]. + * @param edge_data This array size E (number of edges) contains + * the weight for each + * edge. This array can be null in which case the graph is considered + * unweighted. + * @param subgraph_offsets This array size number_of_subgraph + 1 contains edge offsets + for each subgraph + + + */ +struct cy_multi_edgelists_t { + size_t number_of_vertices; + size_t number_of_edges; + size_t number_of_subgraph; + std::unique_ptr src_indices; + std::unique_ptr dst_indices; + std::unique_ptr edge_data; + std::unique_ptr subgraph_offsets; +}; + +// replacement for std::tuple<,,>, since std::tuple is not +// supported in cython +// +template +struct major_minor_weights_t { + explicit major_minor_weights_t(raft::handle_t const& handle) + : shuffled_major_vertices_(0, handle.get_stream()), + shuffled_minor_vertices_(0, handle.get_stream()), + shuffled_weights_(0, handle.get_stream()) + { + } + + rmm::device_uvector& get_major(void) { return shuffled_major_vertices_; } + + rmm::device_uvector& get_minor(void) { return shuffled_minor_vertices_; } + + rmm::device_uvector& get_weights(void) { return shuffled_weights_; } + + std::vector& get_edge_counts(void) { return edge_counts_; } + + std::pair, size_t> get_major_wrap( + void) // const: triggers errors in Cython autogen-ed C++ + { + return std::make_pair(std::make_unique(shuffled_major_vertices_.release()), + sizeof(vertex_t)); + } + + std::pair, size_t> get_minor_wrap(void) // const + { + return std::make_pair(std::make_unique(shuffled_minor_vertices_.release()), + sizeof(vertex_t)); + } + + std::pair, size_t> get_weights_wrap(void) // const + { + return std::make_pair(std::make_unique(shuffled_weights_.release()), + sizeof(weight_t)); + } + + std::unique_ptr> get_edge_counts_wrap(void) // const + { + return std::make_unique>(edge_counts_); + } + + private: + rmm::device_uvector shuffled_major_vertices_; + rmm::device_uvector shuffled_minor_vertices_; + rmm::device_uvector shuffled_weights_; + std::vector edge_counts_{}; +}; + +// aggregate for random_walks() return type +// to be exposed to cython: +// +struct random_walk_ret_t { + size_t coalesced_sz_v_; + size_t coalesced_sz_w_; + size_t num_paths_; + size_t max_depth_; + std::unique_ptr d_coalesced_v_; + std::unique_ptr d_coalesced_w_; + std::unique_ptr d_sizes_; +}; + +struct random_walk_path_t { + std::unique_ptr d_v_offsets; + std::unique_ptr d_w_sizes; + std::unique_ptr d_w_offsets; +}; + +struct graph_generator_t { + std::unique_ptr d_source; + std::unique_ptr d_destination; +}; + +// enum class generator_distribution_t { POWER_LAW = 0, UNIFORM }; +// aggregate for random_walks() COO return type +// to be exposed to cython: +// +struct random_walk_coo_t { + size_t num_edges_; // total number of COO triplets (for all paths) + size_t num_offsets_; // offsets of where each COO set starts for each path; + // NOTE: this can differ than num_paths_, + // because paths with 0 edges (one vertex) + // don't participate to the COO + + std::unique_ptr + d_src_; // coalesced set of COO source vertices; |d_src_| = num_edges_ + std::unique_ptr + d_dst_; // coalesced set of COO destination vertices; |d_dst_| = num_edges_ + std::unique_ptr + d_weights_; // coalesced set of COO edge weights; |d_weights_| = num_edges_ + std::unique_ptr + d_offsets_; // offsets where each COO subset for each path starts; |d_offsets_| = num_offsets_ +}; + +// wrapper for renumber_edgelist() return +// (unrenumbering maps, etc.) +// +template +struct renum_tuple_t { + explicit renum_tuple_t(raft::handle_t const& handle) : dv_(0, handle.get_stream()), part_() {} + + rmm::device_uvector& get_dv(void) { return dv_; } + + std::pair, size_t> get_dv_wrap( + void) // const: see above explanation + { + return std::make_pair(std::make_unique(dv_.release()), sizeof(vertex_t)); + } + + cugraph::experimental::partition_t& get_partition(void) { return part_; } + vertex_t& get_num_vertices(void) { return nv_; } + edge_t& get_num_edges(void) { return ne_; } + + std::vector& get_segment_offsets(void) { return segment_offsets_; } + + std::unique_ptr> get_segment_offsets_wrap() + { // const + return std::make_unique>(segment_offsets_); + } + + // `partition_t` pass-through getters + // + int get_part_row_size() const { return part_.get_row_size(); } + + int get_part_col_size() const { return part_.get_col_size(); } + + int get_part_comm_rank() const { return part_.get_comm_rank(); } + + // FIXME: part_.get_vertex_partition_offsets() returns a std::vector + // + std::unique_ptr> get_partition_offsets_wrap(void) // const + { + return std::make_unique>(part_.get_vertex_partition_offsets()); + } + + std::pair get_part_local_vertex_range() const + { + auto tpl_v = part_.get_local_vertex_range(); + return std::make_pair(std::get<0>(tpl_v), std::get<1>(tpl_v)); + } + + vertex_t get_part_local_vertex_first() const { return part_.get_local_vertex_first(); } + + vertex_t get_part_local_vertex_last() const { return part_.get_local_vertex_last(); } + + std::pair get_part_vertex_partition_range(size_t vertex_partition_idx) const + { + auto tpl_v = part_.get_vertex_partition_range(vertex_partition_idx); + return std::make_pair(std::get<0>(tpl_v), std::get<1>(tpl_v)); + } + + vertex_t get_part_vertex_partition_first(size_t vertex_partition_idx) const + { + return part_.get_vertex_partition_first(vertex_partition_idx); + } + + vertex_t get_part_vertex_partition_last(size_t vertex_partition_idx) const + { + return part_.get_vertex_partition_last(vertex_partition_idx); + } + + vertex_t get_part_vertex_partition_size(size_t vertex_partition_idx) const + { + return part_.get_vertex_partition_size(vertex_partition_idx); + } + + size_t get_part_number_of_matrix_partitions() const + { + return part_.get_number_of_matrix_partitions(); + } + + std::pair get_part_matrix_partition_major_range(size_t partition_idx) const + { + auto tpl_v = part_.get_matrix_partition_major_range(partition_idx); + return std::make_pair(std::get<0>(tpl_v), std::get<1>(tpl_v)); + } + + vertex_t get_part_matrix_partition_major_first(size_t partition_idx) const + { + return part_.get_matrix_partition_major_first(partition_idx); + } + + vertex_t get_part_matrix_partition_major_last(size_t partition_idx) const + { + return part_.get_matrix_partition_major_last(partition_idx); + } + + vertex_t get_part_matrix_partition_major_value_start_offset(size_t partition_idx) const + { + return part_.get_part_matrix_partition_major_value_start_offset(partition_idx); + } + + std::pair get_part_matrix_partition_minor_range() const + { + auto tpl_v = part_.get_matrix_partition_minor_range(); + return std::make_pair(std::get<0>(tpl_v), std::get<1>(tpl_v)); + } + + vertex_t get_part_matrix_partition_minor_first() const + { + return part_.get_matrix_partition_minor_first(); + } + + vertex_t get_part_matrix_partition_minor_last() const + { + return part_.get_matrix_partition_minor_last(); + } + + private: + rmm::device_uvector dv_; + cugraph::experimental::partition_t part_; + vertex_t nv_{0}; + edge_t ne_{0}; + std::vector segment_offsets_; +}; + +// FIXME: finish description for vertex_partition_offsets +// +// Factory function for populating an empty graph container with a new graph +// object from basic types, and sets the corresponding meta-data. Args are: +// +// graph_container_t& graph_container +// Reference to the graph_container_t instance to +// populate. populate_graph_container() can only be called on an "empty" +// container (ie. a container that has not been previously populated by +// populate_graph_container()) +// +// graphTypeEnum legacyType +// Specifies the type of graph when instantiating a legacy graph type +// (GraphCSRViewFloat, etc.). +// NOTE: this parameter will be removed when the transition to exclusinve use +// of the new 2D graph classes is complete. +// +// raft::handle_t const& handle +// Raft handle to be set on the new graph instance in the container +// +// void* src_vertices, dst_vertices, weights +// Pointer to an array of values representing source and destination vertices, +// and edge weights respectively. The value types of the array are specified +// using numberTypeEnum values separately (see below). offsets should be size +// num_vertices+1, indices should be size num_edges, weights should also be +// size num_edges +// +// void* vertex_partition_offsets +// Pointer to an array of vertexType values representing offsets into the +// individual partitions for a multi-GPU paritioned graph. The offsets are used for ... +// +// numberTypeEnum vertexType, edgeType, weightType +// numberTypeEnum enum value describing the data type for the vertices, +// offsets, and weights arrays respectively. These enum values are used to +// instantiate the proper templated graph type and for casting the arrays +// accordingly. +// +// int num_vertices, num_edges +// The number of vertices and edges respectively in the graph represented by +// the above arrays. +// +// bool is_weighted +// true if the resulting graph object should store edge weights +// +// bool transposed +// true if the resulting graph object should store a transposed adjacency +// matrix +// +// bool multi_gpu +// true if the resulting graph object is to be used for a multi-gpu +// application +void populate_graph_container(graph_container_t& graph_container, + raft::handle_t& handle, + void* src_vertices, + void* dst_vertices, + void* weights, + void* vertex_partition_offsets, + void* segment_offsets, + size_t num_segments, + numberTypeEnum vertexType, + numberTypeEnum edgeType, + numberTypeEnum weightType, + size_t num_local_edges, + size_t num_global_vertices, + size_t num_global_edges, + bool is_weighted, + bool is_symmetric, + bool transposed, + bool multi_gpu); + +// FIXME: comment this function +// FIXME: Should local_* values be void* as well? +void populate_graph_container_legacy(graph_container_t& graph_container, + graphTypeEnum legacyType, + raft::handle_t const& handle, + void* offsets, + void* indices, + void* weights, + numberTypeEnum offsetType, + numberTypeEnum indexType, + numberTypeEnum weightType, + size_t num_global_vertices, + size_t num_global_edges, + int* local_vertices, + int* local_edges, + int* local_offsets); + +// Wrapper for calling Louvain using a graph container +template +std::pair call_louvain(raft::handle_t const& handle, + graph_container_t const& graph_container, + void* identifiers, + void* parts, + size_t max_level, + weight_t resolution); + +// Wrapper for calling Pagerank using a graph container +template +void call_pagerank(raft::handle_t const& handle, + graph_container_t const& graph_container, + vertex_t* identifiers, + weight_t* pagerank, + vertex_t personalization_subset_size, + vertex_t* personalization_subset, + weight_t* personalization_values, + double alpha, + double tolerance, + int64_t max_iter, + bool has_guess); + +// Wrapper for calling Katz centrality using a graph container +template +void call_katz_centrality(raft::handle_t const& handle, + graph_container_t const& graph_container, + vertex_t* identifiers, + weight_t* katz_centrality, + double alpha, + double beta, + double tolerance, + int64_t max_iter, + bool normalized, + bool has_guess); + +// Wrapper for calling BFS through a graph container +template +void call_bfs(raft::handle_t const& handle, + graph_container_t const& graph_container, + vertex_t* identifiers, + vertex_t* distances, + vertex_t* predecessors, + vertex_t depth_limit, + const vertex_t start_vertex, + bool direction_optimizing); + +// Wrapper for calling SSSP through a graph container +template +void call_sssp(raft::handle_t const& handle, + graph_container_t const& graph_container, + vertex_t* identifiers, + weight_t* distances, + vertex_t* predecessors, + const vertex_t source_vertex); + +// Wrapper for calling egonet through a graph container +template +std::unique_ptr call_egonet(raft::handle_t const& handle, + graph_container_t const& graph_container, + vertex_t* source_vertex, + vertex_t n_subgraphs, + vertex_t radius); + +// Wrapper for calling WCC through a graph container +template +void call_wcc(raft::handle_t const& handle, + graph_container_t const& graph_container, + vertex_t* components); + +// Wrapper for calling graph generator +template +std::unique_ptr call_generate_rmat_edgelist(raft::handle_t const& handle, + size_t scale, + size_t num_edges, + double a, + double b, + double c, + uint64_t seed, + bool clip_and_flip, + bool scramble_vertex_ids); +template +std::vector, std::unique_ptr>> +call_generate_rmat_edgelists(raft::handle_t const& handle, + size_t n_edgelists, + size_t min_scale, + size_t max_scale, + size_t edge_factor, + cugraph::generator_distribution_t size_distribution, + cugraph::generator_distribution_t edge_distribution, + uint64_t seed, + bool clip_and_flip, + bool scramble_vertex_ids); + +// wrapper for random_walks. +// +template +std::enable_if_t::value, + std::unique_ptr> +call_random_walks(raft::handle_t const& handle, + graph_container_t const& graph_container, + vertex_t const* ptr_start_set, + edge_t num_paths, + edge_t max_depth, + bool use_padding); + +template +std::unique_ptr call_rw_paths(raft::handle_t const& handle, + index_t num_paths, + index_t const* vertex_path_sizes); + +// convertor from random_walks return type to COO: +// +template +std::unique_ptr random_walks_to_coo(raft::handle_t const& handle, + random_walk_ret_t& rw_ret); + +// wrapper for shuffling: +// +template +std::unique_ptr> call_shuffle( + raft::handle_t const& handle, + vertex_t* + edgelist_major_vertices, // [IN / OUT]: groupby_gpuid_and_shuffle_values() sorts in-place + vertex_t* edgelist_minor_vertices, // [IN / OUT] + weight_t* edgelist_weights, // [IN / OUT] + edge_t num_edgelist_edges); + +// Wrapper for calling renumber_edeglist() inplace: +// +template +std::unique_ptr> call_renumber( + raft::handle_t const& handle, + vertex_t* shuffled_edgelist_major_vertices /* [INOUT] */, + vertex_t* shuffled_edgelist_minor_vertices /* [INOUT] */, + std::vector const& edge_counts, + bool do_expensive_check, + bool multi_gpu); + +// Helper for setting up subcommunicators, typically called as part of the +// user-initiated comms initialization in Python. +// +// raft::handle_t& handle +// Raft handle for which the new subcommunicators will be created. The +// subcommunicators will then be accessible from the handle passed to the +// parallel processes. +// +// size_t row_comm_size +// Number of items in a partition row (ie. pcols), needed for creating the +// appropriate number of subcommunicator instances. +void init_subcomms(raft::handle_t& handle, size_t row_comm_size); + +} // namespace cython +} // namespace cugraph diff --git a/cpp/include/cugraph/utilities/dataframe_buffer.cuh b/cpp/include/cugraph/utilities/dataframe_buffer.cuh new file mode 100644 index 00000000000..d730a3afcff --- /dev/null +++ b/cpp/include/cugraph/utilities/dataframe_buffer.cuh @@ -0,0 +1,223 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include + +#include +#include +#include + +#include +#include + +#include + +namespace cugraph { +namespace experimental { + +namespace detail { + +template +auto allocate_dataframe_buffer_tuple_element_impl(size_t buffer_size, + rmm::cuda_stream_view stream_view) +{ + using element_t = typename thrust::tuple_element::type; + return rmm::device_uvector(buffer_size, stream_view); +} + +template +auto allocate_dataframe_buffer_tuple_impl(std::index_sequence, + size_t buffer_size, + rmm::cuda_stream_view stream_view) +{ + return std::make_tuple( + allocate_dataframe_buffer_tuple_element_impl(buffer_size, stream_view)...); +} + +template +struct resize_dataframe_buffer_tuple_iterator_element_impl { + void run(BufferType& buffer, size_t new_buffer_size, rmm::cuda_stream_view stream_view) + { + std::get(buffer).resize(new_buffer_size, stream_view); + resize_dataframe_buffer_tuple_iterator_element_impl().run( + buffer, new_buffer_size, stream_view); + } +}; + +template +struct resize_dataframe_buffer_tuple_iterator_element_impl { + void run(BufferType& buffer, size_t new_buffer_size, rmm::cuda_stream_view stream_view) {} +}; + +template +struct shrink_to_fit_dataframe_buffer_tuple_iterator_element_impl { + void run(BufferType& buffer, rmm::cuda_stream_view stream_view) + { + std::get(buffer).shrink_to_fit(stream_view); + shrink_to_fit_dataframe_buffer_tuple_iterator_element_impl() + .run(buffer, stream_view); + } +}; + +template +struct shrink_to_fit_dataframe_buffer_tuple_iterator_element_impl { + void run(BufferType& buffer, rmm::cuda_stream_view stream_view) {} +}; + +template +auto get_dataframe_buffer_begin_tuple_element_impl(BufferType& buffer) +{ + using element_t = typename thrust::tuple_element::type; + return std::get(buffer).begin(); +} + +template +auto get_dataframe_buffer_begin_tuple_impl(std::index_sequence, BufferType& buffer) +{ + // thrust::make_tuple instead of std::make_tuple as this is fed to thrust::make_zip_iterator. + return thrust::make_tuple( + get_dataframe_buffer_begin_tuple_element_impl(buffer)...); +} + +template +auto get_dataframe_buffer_end_tuple_element_impl(BufferType& buffer) +{ + using element_t = typename thrust::tuple_element::type; + return std::get(buffer).end(); +} + +template +auto get_dataframe_buffer_end_tuple_impl(std::index_sequence, BufferType& buffer) +{ + // thrust::make_tuple instead of std::make_tuple as this is fed to thrust::make_zip_iterator. + return thrust::make_tuple(get_dataframe_buffer_end_tuple_element_impl(buffer)...); +} + +} // namespace detail + +template ::value>* = nullptr> +auto allocate_dataframe_buffer(size_t buffer_size, rmm::cuda_stream_view stream_view) +{ + return rmm::device_uvector(buffer_size, stream_view); +} + +template ::value>* = nullptr> +auto allocate_dataframe_buffer(size_t buffer_size, rmm::cuda_stream_view stream_view) +{ + size_t constexpr tuple_size = thrust::tuple_size::value; + return detail::allocate_dataframe_buffer_tuple_impl( + std::make_index_sequence(), buffer_size, stream_view); +} + +template ::value>* = nullptr> +void resize_dataframe_buffer(BufferType& buffer, + size_t new_buffer_size, + rmm::cuda_stream_view stream_view) +{ + buffer.resize(new_buffer_size, stream_view); +} + +template ::value>* = nullptr> +void resize_dataframe_buffer(BufferType& buffer, + size_t new_buffer_size, + rmm::cuda_stream_view stream_view) +{ + size_t constexpr tuple_size = thrust::tuple_size::value; + detail:: + resize_dataframe_buffer_tuple_iterator_element_impl() + .run(buffer, new_buffer_size, stream_view); +} + +template ::value>* = nullptr> +void shrink_to_fit_dataframe_buffer(BufferType& buffer, rmm::cuda_stream_view stream_view) +{ + buffer.shrink_to_fit(stream_view); +} + +template ::value>* = nullptr> +void shrink_to_fit_dataframe_buffer(BufferType& buffer, rmm::cuda_stream_view stream_view) +{ + size_t constexpr tuple_size = thrust::tuple_size::value; + detail::shrink_to_fit_dataframe_buffer_tuple_iterator_element_impl() + .run(buffer, stream_view); +} + +template ::value>* = nullptr> +size_t size_dataframe_buffer(BufferType& buffer) +{ + return buffer.size(); +} + +template ::value>* = nullptr> +size_t size_dataframe_buffer(BufferType& buffer) +{ + return std::get<0>(buffer).size(); +} + +template ::value>* = nullptr> +auto get_dataframe_buffer_begin(BufferType& buffer) +{ + return buffer.begin(); +} + +template ::value>* = nullptr> +auto get_dataframe_buffer_begin(BufferType& buffer) +{ + size_t constexpr tuple_size = thrust::tuple_size::value; + return thrust::make_zip_iterator(detail::get_dataframe_buffer_begin_tuple_impl( + std::make_index_sequence(), buffer)); +} + +template ::value>* = nullptr> +auto get_dataframe_buffer_end(BufferType& buffer) +{ + return buffer.end(); +} + +template ::value>* = nullptr> +auto get_dataframe_buffer_end(BufferType& buffer) +{ + size_t constexpr tuple_size = thrust::tuple_size::value; + return thrust::make_zip_iterator( + detail::get_dataframe_buffer_end_tuple_impl(std::make_index_sequence(), buffer)); +} + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/include/utilities/comm_utils.cuh b/cpp/include/cugraph/utilities/device_comm.cuh similarity index 68% rename from cpp/include/utilities/comm_utils.cuh rename to cpp/include/cugraph/utilities/device_comm.cuh index fb69fff49c9..59a01957272 100644 --- a/cpp/include/utilities/comm_utils.cuh +++ b/cpp/include/cugraph/utilities/device_comm.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -15,16 +15,16 @@ */ #pragma once -#include +#include #include +#include #include #include #include #include -#include #include namespace cugraph { @@ -32,66 +32,6 @@ namespace experimental { namespace detail { -template -struct update_vector_of_tuple_scalar_elements_from_tuple_impl { - void update(std::vector& tuple_scalar_elements, TupleType const& tuple) const - { - using element_t = typename thrust::tuple_element::type; - static_assert(sizeof(element_t) <= sizeof(int64_t)); - auto ptr = reinterpret_cast(tuple_scalar_elements.data() + I); - *ptr = thrust::get(tuple); - update_vector_of_tuple_scalar_elements_from_tuple_impl().update( - tuple_scalar_elements, tuple); - } -}; - -template -struct update_vector_of_tuple_scalar_elements_from_tuple_impl { - void update(std::vector& tuple_scalar_elements, TupleType const& tuple) const { return; } -}; - -template -struct update_tuple_from_vector_of_tuple_scalar_elements_impl { - void update(TupleType& tuple, std::vector const& tuple_scalar_elements) const - { - using element_t = typename thrust::tuple_element::type; - static_assert(sizeof(element_t) <= sizeof(int64_t)); - auto ptr = reinterpret_cast(tuple_scalar_elements.data() + I); - thrust::get(tuple) = *ptr; - update_tuple_from_vector_of_tuple_scalar_elements_impl().update( - tuple, tuple_scalar_elements); - } -}; - -template -struct update_tuple_from_vector_of_tuple_scalar_elements_impl { - void update(TupleType& tuple, std::vector const& tuple_scalar_elements) const { return; } -}; - -template -struct host_allreduce_tuple_scalar_element_impl { - void run(raft::comms::comms_t const& comm, - rmm::device_uvector& tuple_scalar_elements, - cudaStream_t stream) const - { - using element_t = typename thrust::tuple_element::type; - static_assert(sizeof(element_t) <= sizeof(int64_t)); - auto ptr = reinterpret_cast(tuple_scalar_elements.data() + I); - comm.allreduce(ptr, ptr, 1, raft::comms::op_t::SUM, stream); - host_allreduce_tuple_scalar_element_impl().run( - comm, tuple_scalar_elements, stream); - } -}; - -template -struct host_allreduce_tuple_scalar_element_impl { - void run(raft::comms::comms_t const& comm, - rmm::device_uvector& tuple_scalar_elements, - cudaStream_t stream) const - { - } -}; - template T* iter_to_raw_ptr(T* ptr) { @@ -236,7 +176,7 @@ device_sendrecv_impl(raft::comms::comms_t const& comm, OutputIterator output_first, size_t rx_count, int src, - cudaStream_t stream) + rmm::cuda_stream_view stream_view) { // no-op } @@ -252,26 +192,18 @@ device_sendrecv_impl(raft::comms::comms_t const& comm, OutputIterator output_first, size_t rx_count, int src, - cudaStream_t stream) + rmm::cuda_stream_view stream_view) { using value_type = typename std::iterator_traits::value_type; static_assert( std::is_same::value_type, value_type>::value); - // ncclSend/ncclRecv pair needs to be located inside ncclGroupStart/ncclGroupEnd to avoid deadlock - ncclGroupStart(); - ncclSend(iter_to_raw_ptr(input_first), - tx_count * sizeof(value_type), - ncclUint8, - dst, - comm.get_nccl_comm(), - stream); - ncclRecv(iter_to_raw_ptr(output_first), - rx_count * sizeof(value_type), - ncclUint8, - src, - comm.get_nccl_comm(), - stream); - ncclGroupEnd(); + comm.device_sendrecv(iter_to_raw_ptr(input_first), + tx_count, + dst, + iter_to_raw_ptr(output_first), + rx_count, + src, + stream_view.value()); } template @@ -283,7 +215,7 @@ struct device_sendrecv_tuple_iterator_element_impl { OutputIterator output_first, size_t rx_count, int src, - cudaStream_t stream) const + rmm::cuda_stream_view stream_view) const { using output_value_t = typename thrust:: tuple_element::value_type>::type; @@ -297,9 +229,9 @@ struct device_sendrecv_tuple_iterator_element_impl { tuple_element_output_first, rx_count, src, - stream); + stream_view.value()); device_sendrecv_tuple_iterator_element_impl().run( - comm, input_first, tx_count, dst, output_first, rx_count, src, stream); + comm, input_first, tx_count, dst, output_first, rx_count, src, stream_view); } }; @@ -307,10 +239,12 @@ template struct device_sendrecv_tuple_iterator_element_impl { void run(raft::comms::comms_t const& comm, InputIterator input_first, - size_t count, + size_t tx_count, int dst, - int base_tag, - raft::comms::request_t* requests) const + OutputIterator output_first, + size_t rx_count, + int src, + rmm::cuda_stream_view stream_view) const { } }; @@ -326,7 +260,7 @@ device_multicast_sendrecv_impl(raft::comms::comms_t const& comm, std::vector const& rx_counts, std::vector const& rx_offsets, std::vector const& rx_src_ranks, - cudaStream_t stream) + rmm::cuda_stream_view stream_view) { // no-op } @@ -344,30 +278,20 @@ device_multicast_sendrecv_impl(raft::comms::comms_t const& comm, std::vector const& rx_counts, std::vector const& rx_offsets, std::vector const& rx_src_ranks, - cudaStream_t stream) + rmm::cuda_stream_view stream_view) { using value_type = typename std::iterator_traits::value_type; static_assert( std::is_same::value_type, value_type>::value); - // ncclSend/ncclRecv pair needs to be located inside ncclGroupStart/ncclGroupEnd to avoid deadlock - ncclGroupStart(); - for (size_t i = 0; i < tx_counts.size(); ++i) { - ncclSend(iter_to_raw_ptr(input_first + tx_offsets[i]), - tx_counts[i] * sizeof(value_type), - ncclUint8, - tx_dst_ranks[i], - comm.get_nccl_comm(), - stream); - } - for (size_t i = 0; i < rx_counts.size(); ++i) { - ncclRecv(iter_to_raw_ptr(output_first + rx_offsets[i]), - rx_counts[i] * sizeof(value_type), - ncclUint8, - rx_src_ranks[i], - comm.get_nccl_comm(), - stream); - } - ncclGroupEnd(); + comm.device_multicast_sendrecv(iter_to_raw_ptr(input_first), + tx_counts, + tx_offsets, + tx_dst_ranks, + iter_to_raw_ptr(output_first), + rx_counts, + rx_offsets, + rx_src_ranks, + stream_view.value()); } template @@ -381,7 +305,7 @@ struct device_multicast_sendrecv_tuple_iterator_element_impl { std::vector const& rx_counts, std::vector const& rx_offsets, std::vector const& rx_src_ranks, - cudaStream_t stream) const + rmm::cuda_stream_view stream_view) const { using output_value_t = typename thrust:: tuple_element::value_type>::type; @@ -397,7 +321,7 @@ struct device_multicast_sendrecv_tuple_iterator_element_impl { rx_counts, rx_offsets, rx_src_ranks, - stream); + stream_view); device_multicast_sendrecv_tuple_iterator_element_impl() .run(comm, input_first, @@ -408,7 +332,7 @@ struct device_multicast_sendrecv_tuple_iterator_element_impl { rx_counts, rx_offsets, rx_src_ranks, - stream); + stream_view); } }; @@ -423,7 +347,7 @@ struct device_multicast_sendrecv_tuple_iterator_element_impl const& rx_counts, std::vector const& rx_offsets, std::vector const& rx_src_ranks, - cudaStream_t stream) const + rmm::cuda_stream_view stream_view) const { } }; @@ -435,7 +359,7 @@ device_bcast_impl(raft::comms::comms_t const& comm, OutputIterator output_first, size_t count, int root, - cudaStream_t stream) + rmm::cuda_stream_view stream_view) { // no-op } @@ -449,14 +373,14 @@ device_bcast_impl(raft::comms::comms_t const& comm, OutputIterator output_first, size_t count, int root, - cudaStream_t stream) + rmm::cuda_stream_view stream_view) { static_assert(std::is_same::value_type, typename std::iterator_traits::value_type>::value); if (comm.get_rank() == root) { - comm.bcast(iter_to_raw_ptr(input_first), count, root, stream); + comm.bcast(iter_to_raw_ptr(input_first), count, root, stream_view.value()); } else { - comm.bcast(iter_to_raw_ptr(output_first), count, root, stream); + comm.bcast(iter_to_raw_ptr(output_first), count, root, stream_view.value()); } } @@ -467,16 +391,16 @@ struct device_bcast_tuple_iterator_element_impl { OutputIterator output_first, size_t count, int root, - cudaStream_t stream) const + rmm::cuda_stream_view stream_view) const { device_bcast_impl(comm, thrust::get(input_first.get_iterator_tuple()), thrust::get(output_first.get_iterator_tuple()), count, root, - stream); - device_bcast_tuple_iterator_element_impl( - comm, input_first, output_first, count, root, stream); + stream_view); + device_bcast_tuple_iterator_element_impl().run( + comm, input_first, output_first, count, root, stream_view); } }; @@ -487,7 +411,68 @@ struct device_bcast_tuple_iterator_element_impl +std::enable_if_t::value, void> +device_allreduce_impl(raft::comms::comms_t const& comm, + InputIterator input_first, + OutputIterator output_first, + size_t count, + raft::comms::op_t op, + rmm::cuda_stream_view stream_view) +{ + // no-op +} + +template +std::enable_if_t< + std::is_arithmetic::value_type>::value, + void> +device_allreduce_impl(raft::comms::comms_t const& comm, + InputIterator input_first, + OutputIterator output_first, + size_t count, + raft::comms::op_t op, + rmm::cuda_stream_view stream_view) +{ + static_assert(std::is_same::value_type, + typename std::iterator_traits::value_type>::value); + comm.allreduce( + iter_to_raw_ptr(input_first), iter_to_raw_ptr(output_first), count, op, stream_view.value()); +} + +template +struct device_allreduce_tuple_iterator_element_impl { + void run(raft::comms::comms_t const& comm, + InputIterator input_first, + OutputIterator output_first, + size_t count, + raft::comms::op_t op, + rmm::cuda_stream_view stream_view) const + { + device_allreduce_impl(comm, + thrust::get(input_first.get_iterator_tuple()), + thrust::get(output_first.get_iterator_tuple()), + count, + op, + stream_view); + device_allreduce_tuple_iterator_element_impl().run( + comm, input_first, output_first, count, op, stream_view); + } +}; + +template +struct device_allreduce_tuple_iterator_element_impl { + void run(raft::comms::comms_t const& comm, + InputIterator input_first, + OutputIterator output_first, + size_t count, + raft::comms::op_t op, + rmm::cuda_stream_view stream_view) const { } }; @@ -500,7 +485,7 @@ device_reduce_impl(raft::comms::comms_t const& comm, size_t count, raft::comms::op_t op, int root, - cudaStream_t stream) + rmm::cuda_stream_view stream_view) { // no-op } @@ -515,11 +500,16 @@ device_reduce_impl(raft::comms::comms_t const& comm, size_t count, raft::comms::op_t op, int root, - cudaStream_t stream) + rmm::cuda_stream_view stream_view) { static_assert(std::is_same::value_type, typename std::iterator_traits::value_type>::value); - comm.reduce(iter_to_raw_ptr(input_first), iter_to_raw_ptr(output_first), count, op, root, stream); + comm.reduce(iter_to_raw_ptr(input_first), + iter_to_raw_ptr(output_first), + count, + op, + root, + stream_view.value()); } template @@ -530,7 +520,7 @@ struct device_reduce_tuple_iterator_element_impl { size_t count, raft::comms::op_t op, int root, - cudaStream_t stream) const + rmm::cuda_stream_view stream_view) const { device_reduce_impl(comm, thrust::get(input_first.get_iterator_tuple()), @@ -538,9 +528,9 @@ struct device_reduce_tuple_iterator_element_impl { count, op, root, - stream); - device_reduce_tuple_iterator_element_impl( - comm, input_first, output_first, count, op, root, stream); + stream_view); + device_reduce_tuple_iterator_element_impl().run( + comm, input_first, output_first, count, op, root, stream_view); } }; @@ -552,7 +542,7 @@ struct device_reduce_tuple_iterator_element_impl const& recvcounts, std::vector const& displacements, - cudaStream_t stream) + rmm::cuda_stream_view stream_view) { // no-op } @@ -578,7 +568,7 @@ device_allgatherv_impl(raft::comms::comms_t const& comm, OutputIterator output_first, std::vector const& recvcounts, std::vector const& displacements, - cudaStream_t stream) + rmm::cuda_stream_view stream_view) { static_assert(std::is_same::value_type, typename std::iterator_traits::value_type>::value); @@ -586,7 +576,7 @@ device_allgatherv_impl(raft::comms::comms_t const& comm, iter_to_raw_ptr(output_first), recvcounts.data(), displacements.data(), - stream); + stream_view.value()); } template @@ -596,16 +586,16 @@ struct device_allgatherv_tuple_iterator_element_impl { OutputIterator output_first, std::vector const& recvcounts, std::vector const& displacements, - cudaStream_t stream) const + rmm::cuda_stream_view stream_view) const { device_allgatherv_impl(comm, thrust::get(input_first.get_iterator_tuple()), thrust::get(output_first.get_iterator_tuple()), recvcounts, displacements, - stream); + stream_view); device_allgatherv_tuple_iterator_element_impl().run( - comm, input_first, output_first, recvcounts, displacements, stream); + comm, input_first, output_first, recvcounts, displacements, stream_view); } }; @@ -616,188 +606,88 @@ struct device_allgatherv_tuple_iterator_element_impl const& recvcounts, std::vector const& displacements, - cudaStream_t stream) const + rmm::cuda_stream_view stream_view) const { } }; -template -auto allocate_comm_buffer_tuple_element_impl(size_t buffer_size, cudaStream_t stream) -{ - using element_t = typename thrust::tuple_element::type; - return rmm::device_uvector(buffer_size, stream); -} - -template -auto allocate_comm_buffer_tuple_impl(std::index_sequence, - size_t buffer_size, - cudaStream_t stream) -{ - return thrust::make_tuple( - allocate_comm_buffer_tuple_element_impl(buffer_size, stream)...); -} - -template -auto get_comm_buffer_begin_tuple_element_impl(BufferType& buffer) -{ - using element_t = typename thrust::tuple_element::type; - return thrust::get(buffer).begin(); -} - -template -auto get_comm_buffer_begin_tuple_impl(std::index_sequence, BufferType& buffer) -{ - return thrust::make_tuple(get_comm_buffer_begin_tuple_element_impl(buffer)...); -} - -} // namespace detail - -template -std::enable_if_t::value, T> host_scalar_allreduce( - raft::comms::comms_t const& comm, T input, cudaStream_t stream) -{ - rmm::device_uvector d_input(1, stream); - raft::update_device(d_input.data(), &input, 1, stream); - comm.allreduce(d_input.data(), d_input.data(), 1, raft::comms::op_t::SUM, stream); - T h_input{}; - raft::update_host(&h_input, d_input.data(), 1, stream); - auto status = comm.sync_stream(stream); - CUGRAPH_EXPECTS(status == raft::comms::status_t::SUCCESS, "sync_stream() failure."); - return h_input; -} - -template -std::enable_if_t::value, T> -host_scalar_allreduce(raft::comms::comms_t const& comm, T input, cudaStream_t stream) +template +std::enable_if_t::value, void> +device_gatherv_impl(raft::comms::comms_t const& comm, + InputIterator input_first, + OutputIterator output_first, + size_t sendcount, + std::vector const& recvcounts, + std::vector const& displacements, + int root, + rmm::cuda_stream_view stream_view) { - size_t constexpr tuple_size = thrust::tuple_size::value; - std::vector h_tuple_scalar_elements(tuple_size); - rmm::device_uvector d_tuple_scalar_elements(tuple_size, stream); - T ret{}; - - detail::update_vector_of_tuple_scalar_elements_from_tuple_impl().update( - h_tuple_scalar_elements, input); - raft::update_device( - d_tuple_scalar_elements.data(), h_tuple_scalar_elements.data(), tuple_size, stream); - detail::host_allreduce_tuple_scalar_element_impl().run( - comm, d_tuple_scalar_elements, stream); - raft::update_host( - h_tuple_scalar_elements.data(), d_tuple_scalar_elements.data(), tuple_size, stream); - auto status = comm.sync_stream(stream); - CUGRAPH_EXPECTS(status == raft::comms::status_t::SUCCESS, "sync_stream() failure."); - detail::update_tuple_from_vector_of_tuple_scalar_elements_impl().update( - ret, h_tuple_scalar_elements); - - return ret; + // no-op } -template -std::enable_if_t::value, T> host_scalar_bcast( - raft::comms::comms_t const& comm, T input, int root, cudaStream_t stream) +template +std::enable_if_t< + std::is_arithmetic::value_type>::value, + void> +device_gatherv_impl(raft::comms::comms_t const& comm, + InputIterator input_first, + OutputIterator output_first, + size_t sendcount, + std::vector const& recvcounts, + std::vector const& displacements, + int root, + rmm::cuda_stream_view stream_view) { - rmm::device_uvector d_input(1, stream); - if (comm.get_rank() == root) { raft::update_device(d_input.data(), &input, 1, stream); } - comm.bcast(d_input.data(), 1, root, stream); - auto h_input = input; - if (comm.get_rank() != root) { raft::update_host(&h_input, d_input.data(), 1, stream); } - auto status = comm.sync_stream(stream); - CUGRAPH_EXPECTS(status == raft::comms::status_t::SUCCESS, "sync_stream() failure."); - return h_input; + static_assert(std::is_same::value_type, + typename std::iterator_traits::value_type>::value); + comm.gatherv(iter_to_raw_ptr(input_first), + iter_to_raw_ptr(output_first), + sendcount, + recvcounts.data(), + displacements.data(), + root, + stream_view.value()); } -template -std::enable_if_t::value, T> -host_scalar_bcast(raft::comms::comms_t const& comm, T input, int root, cudaStream_t stream) -{ - size_t constexpr tuple_size = thrust::tuple_size::value; - std::vector h_tuple_scalar_elements(tuple_size); - rmm::device_uvector d_tuple_scalar_elements(tuple_size, stream); - auto ret = input; - - if (comm.get_rank() == root) { - detail::update_vector_of_tuple_scalar_elements_from_tuple_impl() - .update(h_tuple_scalar_elements, input); - raft::update_device( - d_tuple_scalar_elements.data(), h_tuple_scalar_elements.data(), tuple_size, stream); - } - comm.bcast(d_tuple_scalar_elements.data(), d_tuple_scalar_elements.size(), root, stream); - if (comm.get_rank() != root) { - raft::update_host( - h_tuple_scalar_elements.data(), d_tuple_scalar_elements.data(), tuple_size, stream); - } - auto status = comm.sync_stream(stream); - CUGRAPH_EXPECTS(status == raft::comms::status_t::SUCCESS, "sync_stream() failure."); - if (comm.get_rank() != root) { - detail::update_tuple_from_vector_of_tuple_scalar_elements_impl() - .update(ret, h_tuple_scalar_elements); +template +struct device_gatherv_tuple_iterator_element_impl { + void run(raft::comms::comms_t const& comm, + InputIterator input_first, + OutputIterator output_first, + size_t sendcount, + std::vector const& recvcounts, + std::vector const& displacements, + int root, + rmm::cuda_stream_view stream_view) const + { + device_gatherv_impl(comm, + thrust::get(input_first.get_iterator_tuple()), + thrust::get(output_first.get_iterator_tuple()), + sendcount, + recvcounts, + displacements, + root, + stream_view); + device_gatherv_tuple_iterator_element_impl().run( + comm, input_first, output_first, sendcount, recvcounts, displacements, root, stream_view); } +}; - return ret; -} - -template -std::enable_if_t::value, std::vector> host_scalar_allgather( - raft::comms::comms_t const& comm, T input, cudaStream_t stream) -{ - std::vector rx_counts(comm.get_size(), size_t{1}); - std::vector displacements(rx_counts.size(), size_t{0}); - std::iota(displacements.begin(), displacements.end(), size_t{0}); - rmm::device_uvector d_outputs(rx_counts.size(), stream); - raft::update_device(d_outputs.data() + comm.get_rank(), &input, 1, stream); - comm.allgatherv(d_outputs.data() + comm.get_rank(), - d_outputs.data(), - rx_counts.data(), - displacements.data(), - stream); - std::vector h_outputs(rx_counts.size(), size_t{0}); - raft::update_host(h_outputs.data(), d_outputs.data(), rx_counts.size(), stream); - auto status = comm.sync_stream(stream); - CUGRAPH_EXPECTS(status == raft::comms::status_t::SUCCESS, "sync_stream() failure."); - return h_outputs; -} - -template -std::enable_if_t::value, std::vector> -host_scalar_allgather(raft::comms::comms_t const& comm, T input, cudaStream_t stream) -{ - size_t constexpr tuple_size = thrust::tuple_size::value; - std::vector rx_counts(comm.get_size(), tuple_size); - std::vector displacements(rx_counts.size(), size_t{0}); - for (size_t i = 0; i < displacements.size(); ++i) { displacements[i] = i * tuple_size; } - std::vector h_tuple_scalar_elements(tuple_size); - rmm::device_uvector d_allgathered_tuple_scalar_elements(comm.get_size() * tuple_size, - stream); - - detail::update_vector_of_tuple_scalar_elements_from_tuple_impl().update( - h_tuple_scalar_elements, input); - raft::update_device(d_allgathered_tuple_scalar_elements.data() + comm.get_rank() * tuple_size, - h_tuple_scalar_elements.data(), - tuple_size, - stream); - comm.allgatherv(d_allgathered_tuple_scalar_elements.data() + comm.get_rank() * tuple_size, - d_allgathered_tuple_scalar_elements.data(), - rx_counts.data(), - displacements.data(), - stream); - std::vector h_allgathered_tuple_scalar_elements(comm.get_size() * tuple_size); - raft::update_host(h_allgathered_tuple_scalar_elements.data(), - d_allgathered_tuple_scalar_elements.data(), - comm.get_size() * tuple_size, - stream); - auto status = comm.sync_stream(stream); - CUGRAPH_EXPECTS(status == raft::comms::status_t::SUCCESS, "sync_stream() failure."); - - std::vector ret(comm.get_size()); - for (size_t i = 0; i < ret.size(); ++i) { - std::vector h_tuple_scalar_elements( - h_allgathered_tuple_scalar_elements.data() + i * tuple_size, - h_allgathered_tuple_scalar_elements.data() + (i + 1) * tuple_size); - detail::update_tuple_from_vector_of_tuple_scalar_elements_impl() - .update(ret[i], h_tuple_scalar_elements); +template +struct device_gatherv_tuple_iterator_element_impl { + void run(raft::comms::comms_t const& comm, + InputIterator input_first, + OutputIterator output_first, + size_t sendcount, + std::vector const& recvcounts, + std::vector const& displacements, + int root, + rmm::cuda_stream_view stream_view) const + { } +}; - return ret; -} +} // namespace detail template std::enable_if_t< @@ -889,10 +779,10 @@ device_sendrecv(raft::comms::comms_t const& comm, OutputIterator output_first, size_t rx_count, int src, - cudaStream_t stream) + rmm::cuda_stream_view stream_view) { detail::device_sendrecv_impl( - comm, input_first, tx_count, dst, output_first, rx_count, src, stream); + comm, input_first, tx_count, dst, output_first, rx_count, src, stream_view); } template @@ -907,7 +797,7 @@ device_sendrecv(raft::comms::comms_t const& comm, OutputIterator output_first, size_t rx_count, int src, - cudaStream_t stream) + rmm::cuda_stream_view stream_view) { static_assert( thrust::tuple_size::value_type>::value == @@ -923,7 +813,7 @@ device_sendrecv(raft::comms::comms_t const& comm, OutputIterator, size_t{0}, tuple_size>() - .run(comm, input_first, tx_count, dst, output_first, rx_count, src, stream); + .run(comm, input_first, tx_count, dst, output_first, rx_count, src, stream_view); } template @@ -939,7 +829,7 @@ device_multicast_sendrecv(raft::comms::comms_t const& comm, std::vector const& rx_counts, std::vector const& rx_offsets, std::vector const& rx_src_ranks, - cudaStream_t stream) + rmm::cuda_stream_view stream_view) { detail::device_multicast_sendrecv_impl(comm, input_first, @@ -950,7 +840,7 @@ device_multicast_sendrecv(raft::comms::comms_t const& comm, rx_counts, rx_offsets, rx_src_ranks, - stream); + stream_view); } template @@ -967,7 +857,7 @@ device_multicast_sendrecv(raft::comms::comms_t const& comm, std::vector const& rx_counts, std::vector const& rx_offsets, std::vector const& rx_src_ranks, - cudaStream_t stream) + rmm::cuda_stream_view stream_view) { static_assert( thrust::tuple_size::value_type>::value == @@ -992,7 +882,7 @@ device_multicast_sendrecv(raft::comms::comms_t const& comm, rx_counts, rx_offsets, rx_src_ranks, - stream); + stream_view); } template @@ -1004,9 +894,9 @@ device_bcast(raft::comms::comms_t const& comm, OutputIterator output_first, size_t count, int root, - cudaStream_t stream) + rmm::cuda_stream_view stream_view) { - detail::device_bcast_impl(comm, input_first, output_first, count, root, stream); + detail::device_bcast_impl(comm, input_first, output_first, count, root, stream_view); } template @@ -1019,7 +909,7 @@ device_bcast(raft::comms::comms_t const& comm, OutputIterator output_first, size_t count, int root, - cudaStream_t stream) + rmm::cuda_stream_view stream_view) { static_assert( thrust::tuple_size::value_type>::value == @@ -1029,8 +919,48 @@ device_bcast(raft::comms::comms_t const& comm, thrust::tuple_size::value_type>::value; detail:: - device_bcast_tuple_iterator_element_impl( - comm, input_first, output_first, count, root, stream); + device_bcast_tuple_iterator_element_impl() + .run(comm, input_first, output_first, count, root, stream_view); +} + +template +std::enable_if_t< + std::is_arithmetic::value_type>::value, + void> +device_allreduce(raft::comms::comms_t const& comm, + InputIterator input_first, + OutputIterator output_first, + size_t count, + raft::comms::op_t op, + rmm::cuda_stream_view stream_view) +{ + detail::device_allreduce_impl(comm, input_first, output_first, count, op, stream_view); +} + +template +std::enable_if_t< + is_thrust_tuple_of_arithmetic::value_type>::value && + is_thrust_tuple::value_type>::value, + void> +device_allreduce(raft::comms::comms_t const& comm, + InputIterator input_first, + OutputIterator output_first, + size_t count, + raft::comms::op_t op, + rmm::cuda_stream_view stream_view) +{ + static_assert( + thrust::tuple_size::value_type>::value == + thrust::tuple_size::value_type>::value); + + size_t constexpr tuple_size = + thrust::tuple_size::value_type>::value; + + detail::device_allreduce_tuple_iterator_element_impl() + .run(comm, input_first, output_first, count, op, stream_view); } template @@ -1043,9 +973,9 @@ device_reduce(raft::comms::comms_t const& comm, size_t count, raft::comms::op_t op, int root, - cudaStream_t stream) + rmm::cuda_stream_view stream_view) { - detail::device_reduce_impl(comm, input_first, output_first, count, op, root, stream); + detail::device_reduce_impl(comm, input_first, output_first, count, op, root, stream_view); } template @@ -1059,7 +989,7 @@ device_reduce(raft::comms::comms_t const& comm, size_t count, raft::comms::op_t op, int root, - cudaStream_t stream) + rmm::cuda_stream_view stream_view) { static_assert( thrust::tuple_size::value_type>::value == @@ -1068,9 +998,11 @@ device_reduce(raft::comms::comms_t const& comm, size_t constexpr tuple_size = thrust::tuple_size::value_type>::value; - detail:: - device_reduce_tuple_iterator_element_impl( - comm, input_first, output_first, count, op, root, stream); + detail::device_reduce_tuple_iterator_element_impl() + .run(comm, input_first, output_first, count, op, root, stream_view); } template @@ -1082,10 +1014,10 @@ device_allgatherv(raft::comms::comms_t const& comm, OutputIterator output_first, std::vector const& recvcounts, std::vector const& displacements, - cudaStream_t stream) + rmm::cuda_stream_view stream_view) { detail::device_allgatherv_impl( - comm, input_first, output_first, recvcounts, displacements, stream); + comm, input_first, output_first, recvcounts, displacements, stream_view); } template @@ -1098,7 +1030,7 @@ device_allgatherv(raft::comms::comms_t const& comm, OutputIterator output_first, std::vector const& recvcounts, std::vector const& displacements, - cudaStream_t stream) + rmm::cuda_stream_view stream_view) { static_assert( thrust::tuple_size::value_type>::value == @@ -1111,39 +1043,52 @@ device_allgatherv(raft::comms::comms_t const& comm, OutputIterator, size_t{0}, tuple_size>() - .run(comm, input_first, output_first, recvcounts, displacements, stream); + .run(comm, input_first, output_first, recvcounts, displacements, stream_view); } -template ::value>* = nullptr> -auto allocate_comm_buffer(size_t buffer_size, cudaStream_t stream) +template +std::enable_if_t< + std::is_arithmetic::value_type>::value, + void> +device_gatherv(raft::comms::comms_t const& comm, + InputIterator input_first, + OutputIterator output_first, + size_t sendcount, + std::vector const& recvcounts, + std::vector const& displacements, + int root, + rmm::cuda_stream_view stream_view) { - return rmm::device_uvector(buffer_size, stream); + detail::device_gatherv_impl( + comm, input_first, output_first, sendcount, recvcounts, displacements, root, stream_view); } -template ::value>* = nullptr> -auto allocate_comm_buffer(size_t buffer_size, cudaStream_t stream) +template +std::enable_if_t< + is_thrust_tuple_of_arithmetic::value_type>::value && + is_thrust_tuple::value_type>::value, + void> +device_gatherv(raft::comms::comms_t const& comm, + InputIterator input_first, + OutputIterator output_first, + size_t sendcount, + std::vector const& recvcounts, + std::vector const& displacements, + int root, + rmm::cuda_stream_view stream_view) { - size_t constexpr tuple_size = thrust::tuple_size::value; - return detail::allocate_comm_buffer_tuple_impl( - std::make_index_sequence(), buffer_size, stream); -} + static_assert( + thrust::tuple_size::value_type>::value == + thrust::tuple_size::value_type>::value); -template ::value>* = nullptr> -auto get_comm_buffer_begin(BufferType& buffer) -{ - return buffer.begin(); -} + size_t constexpr tuple_size = + thrust::tuple_size::value_type>::value; -template ::value>* = nullptr> -auto get_comm_buffer_begin(BufferType& buffer) -{ - size_t constexpr tuple_size = thrust::tuple_size::value; - return thrust::make_zip_iterator( - detail::get_comm_buffer_begin_tuple_impl(std::make_index_sequence(), buffer)); + detail::device_gatherv_tuple_iterator_element_impl() + .run(comm, input_first, output_first, sendcount, recvcounts, displacements, root, stream_view); } } // namespace experimental diff --git a/cpp/include/utilities/error.hpp b/cpp/include/cugraph/utilities/error.hpp similarity index 98% rename from cpp/include/utilities/error.hpp rename to cpp/include/cugraph/utilities/error.hpp index e44e2c910ea..8cfb077cf7b 100644 --- a/cpp/include/utilities/error.hpp +++ b/cpp/include/cugraph/utilities/error.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. diff --git a/cpp/include/cugraph/utilities/graph_traits.hpp b/cpp/include/cugraph/utilities/graph_traits.hpp new file mode 100644 index 00000000000..363a13190be --- /dev/null +++ b/cpp/include/cugraph/utilities/graph_traits.hpp @@ -0,0 +1,61 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +namespace cugraph { +namespace experimental { + +// primary template: +// +template +struct is_one_of; // purposely empty + +// partial specializations: +// +template +struct is_one_of { + static constexpr bool value = std::is_same::value || is_one_of::value; +}; + +template +struct is_one_of { + static constexpr bool value = false; +}; + +// meta-function that constrains +// vertex_t and edge_t template param candidates: +// +template +struct is_vertex_edge_combo { + static constexpr bool value = is_one_of::value && + is_one_of::value && + (sizeof(vertex_t) <= sizeof(edge_t)); +}; + +// meta-function that constrains +// all 3 template param candidates: +// +template +struct is_candidate { + static constexpr bool value = + is_vertex_edge_combo::value && is_one_of::value; +}; + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/include/cugraph/utilities/host_barrier.hpp b/cpp/include/cugraph/utilities/host_barrier.hpp new file mode 100644 index 00000000000..11803a7bde4 --- /dev/null +++ b/cpp/include/cugraph/utilities/host_barrier.hpp @@ -0,0 +1,29 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include + +namespace cugraph { +namespace experimental { + +// FIXME: a temporary hack till UCC is integrated into RAFT (so we can use UCC barrier for DASK and +// MPI barrier for MPI) +void host_barrier(raft::comms::comms_t const& comm, rmm::cuda_stream_view stream_view); + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/include/cugraph/utilities/host_scalar_comm.cuh b/cpp/include/cugraph/utilities/host_scalar_comm.cuh new file mode 100644 index 00000000000..26994ebde14 --- /dev/null +++ b/cpp/include/cugraph/utilities/host_scalar_comm.cuh @@ -0,0 +1,392 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include + +#include +#include + +#include +#include + +namespace cugraph { +namespace experimental { + +namespace detail { + +template +struct update_vector_of_tuple_scalar_elements_from_tuple_impl { + void update(std::vector& tuple_scalar_elements, TupleType const& tuple) const + { + using element_t = typename thrust::tuple_element::type; + static_assert(sizeof(element_t) <= sizeof(int64_t)); + auto ptr = reinterpret_cast(tuple_scalar_elements.data() + I); + *ptr = thrust::get(tuple); + update_vector_of_tuple_scalar_elements_from_tuple_impl().update( + tuple_scalar_elements, tuple); + } +}; + +template +struct update_vector_of_tuple_scalar_elements_from_tuple_impl { + void update(std::vector& tuple_scalar_elements, TupleType const& tuple) const { return; } +}; + +template +struct update_tuple_from_vector_of_tuple_scalar_elements_impl { + void update(TupleType& tuple, std::vector const& tuple_scalar_elements) const + { + using element_t = typename thrust::tuple_element::type; + static_assert(sizeof(element_t) <= sizeof(int64_t)); + auto ptr = reinterpret_cast(tuple_scalar_elements.data() + I); + thrust::get(tuple) = *ptr; + update_tuple_from_vector_of_tuple_scalar_elements_impl().update( + tuple, tuple_scalar_elements); + } +}; + +template +struct update_tuple_from_vector_of_tuple_scalar_elements_impl { + void update(TupleType& tuple, std::vector const& tuple_scalar_elements) const { return; } +}; + +template +struct host_allreduce_tuple_scalar_element_impl { + void run(raft::comms::comms_t const& comm, + rmm::device_uvector& tuple_scalar_elements, + cudaStream_t stream) const + { + using element_t = typename thrust::tuple_element::type; + static_assert(sizeof(element_t) <= sizeof(int64_t)); + auto ptr = reinterpret_cast(tuple_scalar_elements.data() + I); + comm.allreduce(ptr, ptr, 1, raft::comms::op_t::SUM, stream); + host_allreduce_tuple_scalar_element_impl().run( + comm, tuple_scalar_elements, stream); + } +}; + +template +struct host_allreduce_tuple_scalar_element_impl { + void run(raft::comms::comms_t const& comm, + rmm::device_uvector& tuple_scalar_elements, + cudaStream_t stream) const + { + } +}; + +template +struct host_reduce_tuple_scalar_element_impl { + void run(raft::comms::comms_t const& comm, + rmm::device_uvector& tuple_scalar_elements, + int root, + cudaStream_t stream) const + { + using element_t = typename thrust::tuple_element::type; + static_assert(sizeof(element_t) <= sizeof(int64_t)); + auto ptr = reinterpret_cast(tuple_scalar_elements.data() + I); + comm.reduce(ptr, ptr, 1, raft::comms::op_t::SUM, root, stream); + host_reduce_tuple_scalar_element_impl().run( + comm, tuple_scalar_elements, root, stream); + } +}; + +template +struct host_reduce_tuple_scalar_element_impl { + void run(raft::comms::comms_t const& comm, + rmm::device_uvector& tuple_scalar_elements, + int root, + cudaStream_t stream) const + { + } +}; + +} // namespace detail + +template +std::enable_if_t::value, T> host_scalar_allreduce( + raft::comms::comms_t const& comm, T input, cudaStream_t stream) +{ + rmm::device_uvector d_input(1, stream); + raft::update_device(d_input.data(), &input, 1, stream); + comm.allreduce(d_input.data(), d_input.data(), 1, raft::comms::op_t::SUM, stream); + T h_input{}; + raft::update_host(&h_input, d_input.data(), 1, stream); + auto status = comm.sync_stream(stream); + CUGRAPH_EXPECTS(status == raft::comms::status_t::SUCCESS, "sync_stream() failure."); + return h_input; +} + +template +std::enable_if_t::value, T> +host_scalar_allreduce(raft::comms::comms_t const& comm, T input, cudaStream_t stream) +{ + size_t constexpr tuple_size = thrust::tuple_size::value; + std::vector h_tuple_scalar_elements(tuple_size); + rmm::device_uvector d_tuple_scalar_elements(tuple_size, stream); + T ret{}; + + detail::update_vector_of_tuple_scalar_elements_from_tuple_impl().update( + h_tuple_scalar_elements, input); + raft::update_device( + d_tuple_scalar_elements.data(), h_tuple_scalar_elements.data(), tuple_size, stream); + detail::host_allreduce_tuple_scalar_element_impl().run( + comm, d_tuple_scalar_elements, stream); + raft::update_host( + h_tuple_scalar_elements.data(), d_tuple_scalar_elements.data(), tuple_size, stream); + auto status = comm.sync_stream(stream); + CUGRAPH_EXPECTS(status == raft::comms::status_t::SUCCESS, "sync_stream() failure."); + detail::update_tuple_from_vector_of_tuple_scalar_elements_impl().update( + ret, h_tuple_scalar_elements); + + return ret; +} + +// Return value is valid only in root (return value may better be std::optional in C++17 or later) +template +std::enable_if_t::value, T> host_scalar_reduce( + raft::comms::comms_t const& comm, T input, int root, cudaStream_t stream) +{ + rmm::device_uvector d_input(1, stream); + raft::update_device(d_input.data(), &input, 1, stream); + comm.reduce(d_input.data(), d_input.data(), 1, raft::comms::op_t::SUM, stream); + T h_input{}; + if (comm.get_rank() == root) { raft::update_host(&h_input, d_input.data(), 1, stream); } + auto status = comm.sync_stream(stream); + CUGRAPH_EXPECTS(status == raft::comms::status_t::SUCCESS, "sync_stream() failure."); + return h_input; +} + +// Return value is valid only in root (return value may better be std::optional in C++17 or later) +template +std::enable_if_t::value, T> +host_scalar_reduce(raft::comms::comms_t const& comm, T input, int root, cudaStream_t stream) +{ + size_t constexpr tuple_size = thrust::tuple_size::value; + std::vector h_tuple_scalar_elements(tuple_size); + rmm::device_uvector d_tuple_scalar_elements(tuple_size, stream); + T ret{}; + + detail::update_vector_of_tuple_scalar_elements_from_tuple_impl().update( + h_tuple_scalar_elements, input); + raft::update_device( + d_tuple_scalar_elements.data(), h_tuple_scalar_elements.data(), tuple_size, stream); + detail::host_reduce_tuple_scalar_element_impl().run( + comm, d_tuple_scalar_elements, root, stream); + if (comm.get_rank() == root) { + raft::update_host( + h_tuple_scalar_elements.data(), d_tuple_scalar_elements.data(), tuple_size, stream); + } + auto status = comm.sync_stream(stream); + CUGRAPH_EXPECTS(status == raft::comms::status_t::SUCCESS, "sync_stream() failure."); + if (comm.get_rank() == root) { + detail::update_tuple_from_vector_of_tuple_scalar_elements_impl() + .update(ret, h_tuple_scalar_elements); + } + + return ret; +} + +template +std::enable_if_t::value, T> host_scalar_bcast( + raft::comms::comms_t const& comm, T input, int root, cudaStream_t stream) +{ + rmm::device_uvector d_input(1, stream); + if (comm.get_rank() == root) { raft::update_device(d_input.data(), &input, 1, stream); } + comm.bcast(d_input.data(), 1, root, stream); + auto h_input = input; + if (comm.get_rank() != root) { raft::update_host(&h_input, d_input.data(), 1, stream); } + auto status = comm.sync_stream(stream); + CUGRAPH_EXPECTS(status == raft::comms::status_t::SUCCESS, "sync_stream() failure."); + return h_input; +} + +template +std::enable_if_t::value, T> +host_scalar_bcast(raft::comms::comms_t const& comm, T input, int root, cudaStream_t stream) +{ + size_t constexpr tuple_size = thrust::tuple_size::value; + std::vector h_tuple_scalar_elements(tuple_size); + rmm::device_uvector d_tuple_scalar_elements(tuple_size, stream); + auto ret = input; + + if (comm.get_rank() == root) { + detail::update_vector_of_tuple_scalar_elements_from_tuple_impl() + .update(h_tuple_scalar_elements, input); + raft::update_device( + d_tuple_scalar_elements.data(), h_tuple_scalar_elements.data(), tuple_size, stream); + } + comm.bcast(d_tuple_scalar_elements.data(), d_tuple_scalar_elements.size(), root, stream); + if (comm.get_rank() != root) { + raft::update_host( + h_tuple_scalar_elements.data(), d_tuple_scalar_elements.data(), tuple_size, stream); + } + auto status = comm.sync_stream(stream); + CUGRAPH_EXPECTS(status == raft::comms::status_t::SUCCESS, "sync_stream() failure."); + if (comm.get_rank() != root) { + detail::update_tuple_from_vector_of_tuple_scalar_elements_impl() + .update(ret, h_tuple_scalar_elements); + } + + return ret; +} + +template +std::enable_if_t::value, std::vector> host_scalar_allgather( + raft::comms::comms_t const& comm, T input, cudaStream_t stream) +{ + std::vector rx_counts(comm.get_size(), size_t{1}); + std::vector displacements(rx_counts.size(), size_t{0}); + std::iota(displacements.begin(), displacements.end(), size_t{0}); + rmm::device_uvector d_outputs(rx_counts.size(), stream); + raft::update_device(d_outputs.data() + comm.get_rank(), &input, 1, stream); + // FIXME: better use allgather + comm.allgatherv(d_outputs.data() + comm.get_rank(), + d_outputs.data(), + rx_counts.data(), + displacements.data(), + stream); + std::vector h_outputs(rx_counts.size()); + raft::update_host(h_outputs.data(), d_outputs.data(), rx_counts.size(), stream); + auto status = comm.sync_stream(stream); + CUGRAPH_EXPECTS(status == raft::comms::status_t::SUCCESS, "sync_stream() failure."); + return h_outputs; +} + +template +std::enable_if_t::value, std::vector> +host_scalar_allgather(raft::comms::comms_t const& comm, T input, cudaStream_t stream) +{ + size_t constexpr tuple_size = thrust::tuple_size::value; + std::vector rx_counts(comm.get_size(), tuple_size); + std::vector displacements(rx_counts.size(), size_t{0}); + for (size_t i = 0; i < displacements.size(); ++i) { + displacements[i] = i * tuple_size; + } + std::vector h_tuple_scalar_elements(tuple_size); + rmm::device_uvector d_allgathered_tuple_scalar_elements(comm.get_size() * tuple_size, + stream); + + detail::update_vector_of_tuple_scalar_elements_from_tuple_impl().update( + h_tuple_scalar_elements, input); + raft::update_device(d_allgathered_tuple_scalar_elements.data() + comm.get_rank() * tuple_size, + h_tuple_scalar_elements.data(), + tuple_size, + stream); + // FIXME: better use allgather + comm.allgatherv(d_allgathered_tuple_scalar_elements.data() + comm.get_rank() * tuple_size, + d_allgathered_tuple_scalar_elements.data(), + rx_counts.data(), + displacements.data(), + stream); + std::vector h_allgathered_tuple_scalar_elements(comm.get_size() * tuple_size); + raft::update_host(h_allgathered_tuple_scalar_elements.data(), + d_allgathered_tuple_scalar_elements.data(), + comm.get_size() * tuple_size, + stream); + auto status = comm.sync_stream(stream); + CUGRAPH_EXPECTS(status == raft::comms::status_t::SUCCESS, "sync_stream() failure."); + + std::vector ret(comm.get_size()); + for (size_t i = 0; i < ret.size(); ++i) { + std::vector h_tuple_scalar_elements( + h_allgathered_tuple_scalar_elements.data() + i * tuple_size, + h_allgathered_tuple_scalar_elements.data() + (i + 1) * tuple_size); + detail::update_tuple_from_vector_of_tuple_scalar_elements_impl() + .update(ret[i], h_tuple_scalar_elements); + } + + return ret; +} + +// Return value is valid only in root (return value may better be std::optional in C++17 or later) +template +std::enable_if_t::value, std::vector> host_scalar_gather( + raft::comms::comms_t const& comm, T input, int root, cudaStream_t stream) +{ + rmm::device_uvector d_outputs(comm.get_rank() == root ? comm.get_size() : int{1}, stream); + raft::update_device( + comm.get_rank() == root ? d_outputs.data() + comm.get_rank() : d_outputs.data(), + &input, + 1, + stream); + comm.gather(comm.get_rank() == root ? d_outputs.data() + comm.get_rank() : d_outputs.data(), + d_outputs.data(), + size_t{1}, + root, + stream); + std::vector h_outputs(comm.get_rank() == root ? comm.get_size() : 0); + if (comm.get_rank() == root) { + raft::update_host(h_outputs.data(), d_outputs.data(), comm.get_size(), stream); + } + auto status = comm.sync_stream(stream); + CUGRAPH_EXPECTS(status == raft::comms::status_t::SUCCESS, "sync_stream() failure."); + return h_outputs; +} + +// Return value is valid only in root (return value may better be std::optional in C++17 or later) +template +std::enable_if_t::value, std::vector> +host_scalar_gather(raft::comms::comms_t const& comm, T input, int root, cudaStream_t stream) +{ + size_t constexpr tuple_size = thrust::tuple_size::value; + std::vector h_tuple_scalar_elements(tuple_size); + rmm::device_uvector d_gathered_tuple_scalar_elements( + comm.get_rank() == root ? comm.get_size() * tuple_size : tuple_size, stream); + + detail::update_vector_of_tuple_scalar_elements_from_tuple_impl().update( + h_tuple_scalar_elements, input); + raft::update_device(comm.get_rank() == root + ? d_gathered_tuple_scalar_elements.data() + comm.get_rank() * tuple_size + : d_gathered_tuple_scalar_elements.data(), + h_tuple_scalar_elements.data(), + tuple_size, + stream); + comm.gather(comm.get_rank() == root + ? d_gathered_tuple_scalar_elements.data() + comm.get_rank() * tuple_size + : d_gathered_tuple_scalar_elements.data(), + d_gathered_tuple_scalar_elements.data(), + tuple_size, + root, + stream); + std::vector h_gathered_tuple_scalar_elements( + comm.get_rank() == root ? comm.get_size() * tuple_size : size_t{0}); + if (comm.get_rank() == root) { + raft::update_host(h_gathered_tuple_scalar_elements.data(), + d_gathered_tuple_scalar_elements.data(), + comm.get_size() * tuple_size, + stream); + } + auto status = comm.sync_stream(stream); + CUGRAPH_EXPECTS(status == raft::comms::status_t::SUCCESS, "sync_stream() failure."); + + std::vector ret(comm.get_size()); + if (comm.get_rank() == root) { + for (size_t i = 0; i < ret.size(); ++i) { + std::vector h_tuple_scalar_elements( + h_gathered_tuple_scalar_elements.data() + i * tuple_size, + h_gathered_tuple_scalar_elements.data() + (i + 1) * tuple_size); + detail::update_tuple_from_vector_of_tuple_scalar_elements_impl() + .update(ret[i], h_tuple_scalar_elements); + } + } + + return ret; +} + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/include/cugraph/utilities/path_retrieval.hpp b/cpp/include/cugraph/utilities/path_retrieval.hpp new file mode 100644 index 00000000000..b4789c14c4b --- /dev/null +++ b/cpp/include/cugraph/utilities/path_retrieval.hpp @@ -0,0 +1,102 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +namespace cugraph { + +/** + * @brief Takes the results of BFS or SSSP function call and sums the given + * weights along the path to the starting vertex. + * + * @tparam vertex_t Type of vertex identifiers. Needs to be an integral type. + * @tparam weight_t Type of edge weights. Needs to be a floating point type. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. Must have at least one worker stream. + * @param vertices Pointer to vertex ids. + * @param preds Pointer to predecessors. + * @param info_weights Secondary weights along the edge from predecessor to vertex. + * @param out Contains for each index the sum of weights along the path unfolding. + * @param num_vertices Number of vertices. + **/ +template +void get_traversed_cost(raft::handle_t const& handle, + vertex_t const* vertices, + vertex_t const* preds, + weight_t const* info_weights, + weight_t* out, + vertex_t stop_vertex, + vertex_t num_vertices); + +namespace experimental { +/** + * @brief returns the COO format (src_vector, dst_vector) from the random walks (RW) + * paths. + * + * @tparam vertex_t Type of vertex indices. + * @tparam index_t Type used to store indexing and sizes. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param coalesced_sz_v coalesced vertex vector size. + * @param num_paths number of paths. + * @param d_coalesced_v coalesced vertex buffer. + * @param d_sizes paths size buffer. + * @return tuple of (src_vertex_vector, dst_Vertex_vector, path_offsets), where + * path_offsets are the offsets where the COO set of each path starts. + */ +template +std:: + tuple, rmm::device_uvector, rmm::device_uvector> + convert_paths_to_coo(raft::handle_t const& handle, + index_t coalesced_sz_v, + index_t num_paths, + rmm::device_buffer&& d_coalesced_v, + rmm::device_buffer&& d_sizes); + +/** + * @brief returns additional RW information on vertex paths offsets and weight path sizes and + * offsets, for the coalesced case (the padded case does not need or provide this information) + * + * @tparam index_t Type used to store indexing and sizes. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param num_paths number of paths. + * @param ptr_d_sizes sizes of vertex paths. + * @return tuple of (vertex_path_offsets, weight_path_sizes, weight_path_offsets), where offsets are + * exclusive scan of corresponding sizes. + */ +template +std::tuple, rmm::device_uvector, rmm::device_uvector> +query_rw_sizes_offsets(raft::handle_t const& handle, index_t num_paths, index_t const* ptr_d_sizes); +} // namespace experimental + +namespace broadcast { +/** + * @brief broadcasts graph_t object (only the single GPU version). + * + * @tparam graph_t Type of graph (view). + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param graph_ptr pointer to graph object: not `nullptr` on send, `nullptr` (ignored) on receive. + * @return graph_t object that was sent/received + */ +template +graph_t graph_broadcast(raft::handle_t const& handle, graph_t* graph_ptr); +}; // namespace broadcast + +} // namespace cugraph diff --git a/cpp/include/cugraph/utilities/shuffle_comm.cuh b/cpp/include/cugraph/utilities/shuffle_comm.cuh new file mode 100644 index 00000000000..18752897a58 --- /dev/null +++ b/cpp/include/cugraph/utilities/shuffle_comm.cuh @@ -0,0 +1,365 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace cugraph { +namespace experimental { + +namespace detail { + +// inline to suppress a complaint about ODR violation +inline std::tuple, + std::vector, + std::vector, + std::vector, + std::vector, + std::vector> +compute_tx_rx_counts_offsets_ranks(raft::comms::comms_t const& comm, + rmm::device_uvector const& d_tx_value_counts, + rmm::cuda_stream_view stream_view) +{ + auto const comm_size = comm.get_size(); + + rmm::device_uvector d_rx_value_counts(comm_size, stream_view); + + // FIXME: this needs to be replaced with AlltoAll once NCCL 2.8 is released. + std::vector tx_counts(comm_size, size_t{1}); + std::vector tx_offsets(comm_size); + std::iota(tx_offsets.begin(), tx_offsets.end(), size_t{0}); + std::vector tx_dst_ranks(comm_size); + std::iota(tx_dst_ranks.begin(), tx_dst_ranks.end(), int{0}); + std::vector rx_counts(comm_size, size_t{1}); + std::vector rx_offsets(comm_size); + std::iota(rx_offsets.begin(), rx_offsets.end(), size_t{0}); + std::vector rx_src_ranks(comm_size); + std::iota(rx_src_ranks.begin(), rx_src_ranks.end(), int{0}); + device_multicast_sendrecv(comm, + d_tx_value_counts.data(), + tx_counts, + tx_offsets, + tx_dst_ranks, + d_rx_value_counts.data(), + rx_counts, + rx_offsets, + rx_src_ranks, + stream_view); + + raft::update_host(tx_counts.data(), d_tx_value_counts.data(), comm_size, stream_view.value()); + raft::update_host(rx_counts.data(), d_rx_value_counts.data(), comm_size, stream_view.value()); + + stream_view.synchronize(); + + std::partial_sum(tx_counts.begin(), tx_counts.end() - 1, tx_offsets.begin() + 1); + std::partial_sum(rx_counts.begin(), rx_counts.end() - 1, rx_offsets.begin() + 1); + + int num_tx_dst_ranks{0}; + int num_rx_src_ranks{0}; + for (int i = 0; i < comm_size; ++i) { + if (tx_counts[i] != 0) { + tx_counts[num_tx_dst_ranks] = tx_counts[i]; + tx_offsets[num_tx_dst_ranks] = tx_offsets[i]; + tx_dst_ranks[num_tx_dst_ranks] = tx_dst_ranks[i]; + ++num_tx_dst_ranks; + } + if (rx_counts[i] != 0) { + rx_counts[num_rx_src_ranks] = rx_counts[i]; + rx_offsets[num_rx_src_ranks] = rx_offsets[i]; + rx_src_ranks[num_rx_src_ranks] = rx_src_ranks[i]; + ++num_rx_src_ranks; + } + } + tx_counts.resize(num_tx_dst_ranks); + tx_offsets.resize(num_tx_dst_ranks); + tx_dst_ranks.resize(num_tx_dst_ranks); + rx_counts.resize(num_rx_src_ranks); + rx_offsets.resize(num_rx_src_ranks); + rx_src_ranks.resize(num_rx_src_ranks); + + return std::make_tuple(tx_counts, tx_offsets, tx_dst_ranks, rx_counts, rx_offsets, rx_src_ranks); +} + +} // namespace detail + +template +rmm::device_uvector groupby_and_count(ValueIterator tx_value_first /* [INOUT */, + ValueIterator tx_value_last /* [INOUT */, + ValueToGPUIdOp value_to_group_id_op, + int num_groups, + rmm::cuda_stream_view stream_view) +{ + thrust::sort(rmm::exec_policy(stream_view), + tx_value_first, + tx_value_last, + [value_to_group_id_op] __device__(auto lhs, auto rhs) { + return value_to_group_id_op(lhs) < value_to_group_id_op(rhs); + }); + + auto group_id_first = thrust::make_transform_iterator( + tx_value_first, + [value_to_group_id_op] __device__(auto value) { return value_to_group_id_op(value); }); + rmm::device_uvector d_tx_dst_ranks(num_groups, stream_view); + rmm::device_uvector d_tx_value_counts(d_tx_dst_ranks.size(), stream_view); + auto last = + thrust::reduce_by_key(rmm::exec_policy(stream_view), + group_id_first, + group_id_first + thrust::distance(tx_value_first, tx_value_last), + thrust::make_constant_iterator(size_t{1}), + d_tx_dst_ranks.begin(), + d_tx_value_counts.begin()); + if (thrust::distance(d_tx_dst_ranks.begin(), thrust::get<0>(last)) < num_groups) { + rmm::device_uvector d_counts(num_groups, stream_view); + thrust::fill(rmm::exec_policy(stream_view), d_counts.begin(), d_counts.end(), size_t{0}); + thrust::scatter(rmm::exec_policy(stream_view), + d_tx_value_counts.begin(), + thrust::get<1>(last), + d_tx_dst_ranks.begin(), + d_counts.begin()); + d_tx_value_counts = std::move(d_counts); + } + + return d_tx_value_counts; +} + +template +rmm::device_uvector groupby_and_count(VertexIterator tx_key_first /* [INOUT */, + VertexIterator tx_key_last /* [INOUT */, + ValueIterator tx_value_first /* [INOUT */, + KeyToGPUIdOp key_to_group_id_op, + int num_groups, + rmm::cuda_stream_view stream_view) +{ + thrust::sort_by_key(rmm::exec_policy(stream_view), + tx_key_first, + tx_key_last, + tx_value_first, + [key_to_group_id_op] __device__(auto lhs, auto rhs) { + return key_to_group_id_op(lhs) < key_to_group_id_op(rhs); + }); + + auto group_id_first = thrust::make_transform_iterator( + tx_key_first, [key_to_group_id_op] __device__(auto key) { return key_to_group_id_op(key); }); + rmm::device_uvector d_tx_dst_ranks(num_groups, stream_view); + rmm::device_uvector d_tx_value_counts(d_tx_dst_ranks.size(), stream_view); + auto last = thrust::reduce_by_key(rmm::exec_policy(stream_view), + group_id_first, + group_id_first + thrust::distance(tx_key_first, tx_key_last), + thrust::make_constant_iterator(size_t{1}), + d_tx_dst_ranks.begin(), + d_tx_value_counts.begin()); + if (thrust::distance(d_tx_dst_ranks.begin(), thrust::get<0>(last)) < num_groups) { + rmm::device_uvector d_counts(num_groups, stream_view); + thrust::fill(rmm::exec_policy(stream_view), d_counts.begin(), d_counts.end(), size_t{0}); + thrust::scatter(rmm::exec_policy(stream_view), + d_tx_value_counts.begin(), + thrust::get<1>(last), + d_tx_dst_ranks.begin(), + d_counts.begin()); + d_tx_value_counts = std::move(d_counts); + } + + return d_tx_value_counts; +} + +template +auto shuffle_values(raft::comms::comms_t const& comm, + TxValueIterator tx_value_first, + std::vector const& tx_value_counts, + rmm::cuda_stream_view stream_view) +{ + auto const comm_size = comm.get_size(); + + rmm::device_uvector d_tx_value_counts(comm_size, stream_view); + raft::update_device( + d_tx_value_counts.data(), tx_value_counts.data(), comm_size, stream_view.value()); + + std::vector tx_counts{}; + std::vector tx_offsets{}; + std::vector tx_dst_ranks{}; + std::vector rx_counts{}; + std::vector rx_offsets{}; + std::vector rx_src_ranks{}; + std::tie(tx_counts, tx_offsets, tx_dst_ranks, rx_counts, rx_offsets, rx_src_ranks) = + detail::compute_tx_rx_counts_offsets_ranks(comm, d_tx_value_counts, stream_view); + + auto rx_value_buffer = + allocate_dataframe_buffer::value_type>( + rx_offsets.size() > 0 ? rx_offsets.back() + rx_counts.back() : size_t{0}, stream_view); + + // FIXME: this needs to be replaced with AlltoAll once NCCL 2.8 is released + // (if num_tx_dst_ranks == num_rx_src_ranks == comm_size). + device_multicast_sendrecv( + comm, + tx_value_first, + tx_counts, + tx_offsets, + tx_dst_ranks, + get_dataframe_buffer_begin::value_type>( + rx_value_buffer), + rx_counts, + rx_offsets, + rx_src_ranks, + stream_view); + + if (rx_counts.size() < static_cast(comm_size)) { + std::vector tmp_rx_counts(comm_size, size_t{0}); + for (size_t i = 0; i < rx_src_ranks.size(); ++i) { + assert(rx_src_ranks[i] < comm_size); + tmp_rx_counts[rx_src_ranks[i]] = rx_counts[i]; + } + rx_counts = std::move(tmp_rx_counts); + } + + return std::make_tuple(std::move(rx_value_buffer), rx_counts); +} + +template +auto groupby_gpuid_and_shuffle_values(raft::comms::comms_t const& comm, + ValueIterator tx_value_first /* [INOUT */, + ValueIterator tx_value_last /* [INOUT */, + ValueToGPUIdOp value_to_gpu_id_op, + rmm::cuda_stream_view stream_view) +{ + auto const comm_size = comm.get_size(); + + auto d_tx_value_counts = groupby_and_count( + tx_value_first, tx_value_last, value_to_gpu_id_op, comm.get_size(), stream_view); + + std::vector tx_counts{}; + std::vector tx_offsets{}; + std::vector tx_dst_ranks{}; + std::vector rx_counts{}; + std::vector rx_offsets{}; + std::vector rx_src_ranks{}; + std::tie(tx_counts, tx_offsets, tx_dst_ranks, rx_counts, rx_offsets, rx_src_ranks) = + detail::compute_tx_rx_counts_offsets_ranks(comm, d_tx_value_counts, stream_view); + + auto rx_value_buffer = + allocate_dataframe_buffer::value_type>( + rx_offsets.size() > 0 ? rx_offsets.back() + rx_counts.back() : size_t{0}, stream_view); + + // FIXME: this needs to be replaced with AlltoAll once NCCL 2.8 is released + // (if num_tx_dst_ranks == num_rx_src_ranks == comm_size). + device_multicast_sendrecv( + comm, + tx_value_first, + tx_counts, + tx_offsets, + tx_dst_ranks, + get_dataframe_buffer_begin::value_type>( + rx_value_buffer), + rx_counts, + rx_offsets, + rx_src_ranks, + stream_view); + + if (rx_counts.size() < static_cast(comm_size)) { + std::vector tmp_rx_counts(comm_size, size_t{0}); + for (size_t i = 0; i < rx_src_ranks.size(); ++i) { + tmp_rx_counts[rx_src_ranks[i]] = rx_counts[i]; + } + rx_counts = std::move(tmp_rx_counts); + } + + return std::make_tuple(std::move(rx_value_buffer), rx_counts); +} + +template +auto groupby_gpuid_and_shuffle_kv_pairs(raft::comms::comms_t const& comm, + VertexIterator tx_key_first /* [INOUT */, + VertexIterator tx_key_last /* [INOUT */, + ValueIterator tx_value_first /* [INOUT */, + KeyToGPUIdOp key_to_gpu_id_op, + rmm::cuda_stream_view stream_view) +{ + auto const comm_size = comm.get_size(); + + auto d_tx_value_counts = groupby_and_count( + tx_key_first, tx_key_last, tx_value_first, key_to_gpu_id_op, comm.get_size(), stream_view); + + std::vector tx_counts{}; + std::vector tx_offsets{}; + std::vector tx_dst_ranks{}; + std::vector rx_counts{}; + std::vector rx_offsets{}; + std::vector rx_src_ranks{}; + std::tie(tx_counts, tx_offsets, tx_dst_ranks, rx_counts, rx_offsets, rx_src_ranks) = + detail::compute_tx_rx_counts_offsets_ranks(comm, d_tx_value_counts, stream_view); + + rmm::device_uvector::value_type> rx_keys( + rx_offsets.size() > 0 ? rx_offsets.back() + rx_counts.back() : size_t{0}, stream_view); + auto rx_value_buffer = + allocate_dataframe_buffer::value_type>( + rx_keys.size(), stream_view); + + // FIXME: this needs to be replaced with AlltoAll once NCCL 2.8 is released + // (if num_tx_dst_ranks == num_rx_src_ranks == comm_size). + device_multicast_sendrecv(comm, + tx_key_first, + tx_counts, + tx_offsets, + tx_dst_ranks, + rx_keys.begin(), + rx_counts, + rx_offsets, + rx_src_ranks, + stream_view); + + // FIXME: this needs to be replaced with AlltoAll once NCCL 2.8 is released + // (if num_tx_dst_ranks == num_rx_src_ranks == comm_size). + device_multicast_sendrecv( + comm, + tx_value_first, + tx_counts, + tx_offsets, + tx_dst_ranks, + get_dataframe_buffer_begin::value_type>( + rx_value_buffer), + rx_counts, + rx_offsets, + rx_src_ranks, + stream_view); + + if (rx_counts.size() < static_cast(comm_size)) { + std::vector tmp_rx_counts(comm_size, size_t{0}); + for (size_t i = 0; i < rx_src_ranks.size(); ++i) { + assert(rx_src_ranks[i] < comm_size); + tmp_rx_counts[rx_src_ranks[i]] = rx_counts[i]; + } + rx_counts = std::move(tmp_rx_counts); + } + + return std::make_tuple(std::move(rx_keys), std::move(rx_value_buffer), rx_counts); +} + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/include/utilities/thrust_tuple_utils.cuh b/cpp/include/cugraph/utilities/thrust_tuple_utils.cuh similarity index 81% rename from cpp/include/utilities/thrust_tuple_utils.cuh rename to cpp/include/cugraph/utilities/thrust_tuple_utils.cuh index 0ad71ba5e05..ddc325b6bbb 100644 --- a/cpp/include/utilities/thrust_tuple_utils.cuh +++ b/cpp/include/cugraph/utilities/thrust_tuple_utils.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -15,6 +15,7 @@ */ #pragma once +#include #include #include @@ -60,27 +61,6 @@ struct compute_thrust_tuple_element_sizes_impl { void compute(std::array::value>& arr) const {} }; -template -__device__ constexpr auto remove_first_thrust_tuple_element_impl(TupleType const& tuple, - std::index_sequence) -{ - return thrust::make_tuple(thrust::get<1 + Is>(tuple)...); -} - -template -struct plus_thrust_tuple_impl { - __host__ __device__ constexpr void compute(TupleType& lhs, TupleType const& rhs) const - { - thrust::get(lhs) += thrust::get(rhs); - plus_thrust_tuple_impl().compute(lhs, rhs); - } -}; - -template -struct plus_thrust_tuple_impl { - __host__ __device__ constexpr void compute(TupleType& lhs, TupleType const& rhs) const {} -}; - template __device__ std::enable_if_t::value, void> atomic_accumulate_impl( thrust::detail::any_assign& /* dereferencing thrust::discard_iterator results in this type */ lhs, @@ -199,28 +179,6 @@ struct compute_thrust_tuple_element_sizes { } }; -template -struct remove_first_thrust_tuple_element { - __device__ constexpr auto operator()(TupleType const& tuple) const - { - size_t constexpr tuple_size = thrust::tuple_size::value; - return detail::remove_first_thrust_tuple_element_impl( - tuple, std::make_index_sequence()); - } -}; - -template -struct plus_thrust_tuple { - __host__ __device__ constexpr TupleType operator()(TupleType const& lhs, - TupleType const& rhs) const - { - size_t constexpr tuple_size = thrust::tuple_size::value; - auto ret = lhs; - detail::plus_thrust_tuple_impl().compute(ret, rhs); - return ret; - } -}; - template struct atomic_accumulate_thrust_tuple { __device__ constexpr void operator()(Iterator iter, TupleType const& value) const diff --git a/cpp/include/vertex_partition_device.cuh b/cpp/include/cugraph/vertex_partition_device.cuh similarity index 92% rename from cpp/include/vertex_partition_device.cuh rename to cpp/include/cugraph/vertex_partition_device.cuh index a6a78ad3878..f598c7d89d8 100644 --- a/cpp/include/vertex_partition_device.cuh +++ b/cpp/include/cugraph/vertex_partition_device.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -15,8 +15,8 @@ */ #pragma once -#include -#include +#include +#include #include @@ -66,8 +66,8 @@ class vertex_partition_device_t= first_) && (v < last_); } diff --git a/cpp/include/cugraph/vertex_partition_device_view.cuh b/cpp/include/cugraph/vertex_partition_device_view.cuh new file mode 100644 index 00000000000..39eb3e7238a --- /dev/null +++ b/cpp/include/cugraph/vertex_partition_device_view.cuh @@ -0,0 +1,111 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include + +#include + +namespace cugraph { +namespace experimental { + +namespace detail { + +template +class vertex_partition_device_view_base_t { + public: + vertex_partition_device_view_base_t(vertex_t number_of_vertices) + : number_of_vertices_(number_of_vertices) + { + } + + template + __host__ __device__ std::enable_if_t::value, bool> is_valid_vertex( + vertex_type v) const noexcept + { + return ((v >= 0) && (v < number_of_vertices_)); + } + + template + __host__ __device__ std::enable_if_t::value, bool> is_valid_vertex( + vertex_type v) const noexcept + { + return (v < number_of_vertices_); + } + + private: + // should be trivially copyable to device + vertex_t number_of_vertices_{0}; +}; + +} // namespace detail + +template +class vertex_partition_device_view_t; + +// multi-GPU version +template +class vertex_partition_device_view_t> + : public detail::vertex_partition_device_view_base_t { + public: + vertex_partition_device_view_t(vertex_partition_view_t view) + : detail::vertex_partition_device_view_base_t(view.get_number_of_vertices()), + local_vertex_first_(view.get_local_vertex_first()), + local_vertex_last_(view.get_local_vertex_last()) + { + } + + __host__ __device__ bool is_local_vertex_nocheck(vertex_t v) const noexcept + { + return (v >= local_vertex_first_) && (v < local_vertex_last_); + } + + __host__ __device__ vertex_t + get_local_vertex_offset_from_vertex_nocheck(vertex_t v) const noexcept + { + return v - local_vertex_first_; + } + + private: + // should be trivially copyable to device + vertex_t local_vertex_first_{0}; + vertex_t local_vertex_last_{0}; +}; + +// single-GPU version +template +class vertex_partition_device_view_t> + : public detail::vertex_partition_device_view_base_t { + public: + vertex_partition_device_view_t(vertex_partition_view_t view) + : detail::vertex_partition_device_view_base_t(view.get_number_of_vertices()) + { + } + + __host__ __device__ constexpr bool is_local_vertex_nocheck(vertex_t v) const noexcept + { + return true; + } + + __host__ __device__ constexpr vertex_t get_local_vertex_offset_from_vertex_nocheck( + vertex_t v) const noexcept + { + return v; + } +}; + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/include/cugraph/vertex_partition_view.hpp b/cpp/include/cugraph/vertex_partition_view.hpp new file mode 100644 index 00000000000..51badf162eb --- /dev/null +++ b/cpp/include/cugraph/vertex_partition_view.hpp @@ -0,0 +1,81 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include + +namespace cugraph { +namespace experimental { + +namespace detail { + +template +class vertex_partition_view_base_t { + public: + vertex_partition_view_base_t(vertex_t number_of_vertices) + : number_of_vertices_(number_of_vertices) + { + } + + vertex_t get_number_of_vertices() const { return number_of_vertices_; } + + private: + vertex_t number_of_vertices_{0}; +}; + +} // namespace detail + +template +class vertex_partition_view_t; + +// multi-GPU version +template +class vertex_partition_view_t> + : public detail::vertex_partition_view_base_t { + public: + vertex_partition_view_t(vertex_t number_of_vertices, + vertex_t local_vertex_first, + vertex_t local_vertex_last) + : detail::vertex_partition_view_base_t(number_of_vertices), + local_vertex_first_(local_vertex_first), + local_vertex_last_(local_vertex_last) + { + } + + vertex_t get_local_vertex_first() const { return local_vertex_first_; } + vertex_t get_local_vertex_last() const { return local_vertex_last_; } + + private: + vertex_t local_vertex_first_{0}; + vertex_t local_vertex_last_{0}; +}; + +// single-GPU version +template +class vertex_partition_view_t> + : public detail::vertex_partition_view_base_t { + public: + vertex_partition_view_t(vertex_t number_of_vertices) + : detail::vertex_partition_view_base_t(number_of_vertices) + { + } + + vertex_t get_local_vertex_first() const { return vertex_t{0}; } + vertex_t get_local_vertex_last() const { return this->get_number_of_vertices(); } +}; + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/include/cugraph/visitors/bfs_visitor.hpp b/cpp/include/cugraph/visitors/bfs_visitor.hpp new file mode 100644 index 00000000000..75b6d9169f0 --- /dev/null +++ b/cpp/include/cugraph/visitors/bfs_visitor.hpp @@ -0,0 +1,82 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// + +#pragma once +#include "erased_pack.hpp" +#include "graph_envelope.hpp" +#include "ret_terased.hpp" + +namespace cugraph { +namespace visitors { + +using namespace cugraph::experimental; + +// macro option: MAKE_VISITOR(bfs) + +// primary empty template: +// +template +struct bfs_visitor; + +// dummy out non-candidate instantiation paths: +// +template +struct bfs_visitor::value)>> + : visitor_t { + void visit_graph(graph_envelope_t::base_graph_t const&) override + { + // purposely empty + } + return_t const& get_result(void) const override + { + static return_t r{}; + return r; + } +}; + +template +struct bfs_visitor::value>> : visitor_t { + bfs_visitor(erased_pack_t& ep) : ep_(ep) {} + + void visit_graph(graph_envelope_t::base_graph_t const&) override; + + return_t const& get_result(void) const override { return result_; } + + private: + erased_pack_t& ep_; + return_t result_; +}; + +} // namespace visitors +} // namespace cugraph diff --git a/cpp/include/cugraph/visitors/cascaded_dispatch.hpp b/cpp/include/cugraph/visitors/cascaded_dispatch.hpp new file mode 100755 index 00000000000..b513c364ce7 --- /dev/null +++ b/cpp/include/cugraph/visitors/cascaded_dispatch.hpp @@ -0,0 +1,254 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "enum_mapping.hpp" +#include "graph_enum_mapping.hpp" + +#include +#include "graph_factory.hpp" + +namespace cugraph { +namespace visitors { + +using namespace cugraph::experimental; +using pair_uniques_t = graph_envelope_t::pair_uniques_t; + +// dummy-out non-candidate paths: +// +template ::value, void*> = nullptr> +constexpr pair_uniques_t graph_dispatcher(GTypes graph_type, erased_pack_t& ep) +{ + /// return nullptr; + return pair_uniques_t{nullptr, nullptr}; +} + +// final step of cascading: calls factory on erased pack: +// +template ::value, void*> = nullptr> +constexpr pair_uniques_t graph_dispatcher(GTypes graph_type, erased_pack_t& ep) +{ + switch (graph_type) { + case GTypes::GRAPH_T: { + using graph_t = typename GMapType::type; + graph_factory_t factory; + + pair_uniques_t p_uniques = + std::make_pair(factory.make_graph(ep), + std::make_unique>()); + + return p_uniques; + } break; + + default: { + std::stringstream ss; + ss << "ERROR: Unknown type enum:" << static_cast(graph_type); + throw std::runtime_error(ss.str()); + } + } +} + +// multi_gpu bool dispatcher: +// resolves bool `multi_gpu` +// and using template arguments vertex_t, edge_t, weight_t, store_transpose +// cascades into next level +// graph_dispatcher() +// +template +constexpr decltype(auto) multi_gpu_dispatcher(bool multi_gpu, GTypes graph_type, erased_pack_t& ep) +{ + switch (multi_gpu) { + case true: { + return graph_dispatcher(graph_type, ep); + } break; + case false: { + return graph_dispatcher(graph_type, ep); + } + } +} + +// transpose bool dispatcher: +// resolves bool `store_transpose` +// and using template arguments vertex_t, edge_t, weight_t +// cascades into next level +// multi_gpu_dispatcher() +// +template +constexpr decltype(auto) transp_dispatcher(bool store_transposed, + bool multi_gpu, + GTypes graph_type, + erased_pack_t& ep) +{ + switch (store_transposed) { + case true: { + return multi_gpu_dispatcher(multi_gpu, graph_type, ep); + } break; + case false: { + return multi_gpu_dispatcher(multi_gpu, graph_type, ep); + } + } +} + +// weight type dispatcher: +// resolves weigth_t from weight_type enum +// and using template arguments vertex_t, edge_t +// cascades into next level +// transp_dispatcher() +// +template +constexpr decltype(auto) weight_dispatcher( + DTypes weight_type, bool store_transposed, bool multi_gpu, GTypes graph_type, erased_pack_t& ep) +{ + switch (weight_type) { + case DTypes::INT32: { + using weight_t = typename DMapType::type; + return transp_dispatcher( + store_transposed, multi_gpu, graph_type, ep); + } break; + case DTypes::INT64: { + using weight_t = typename DMapType::type; + return transp_dispatcher( + store_transposed, multi_gpu, graph_type, ep); + } break; + case DTypes::FLOAT32: { + using weight_t = typename DMapType::type; + return transp_dispatcher( + store_transposed, multi_gpu, graph_type, ep); + } break; + case DTypes::FLOAT64: { + using weight_t = typename DMapType::type; + return transp_dispatcher( + store_transposed, multi_gpu, graph_type, ep); + } break; + default: { + std::stringstream ss; + ss << "ERROR: Unknown type enum:" << static_cast(weight_type); + throw std::runtime_error(ss.str()); + } + } +} + +// edge type dispatcher: +// resolves edge_t from edge_type enum +// and using template argument vertex_t +// cascades into the next level +// weight_dispatcher(); +// +template +constexpr decltype(auto) edge_dispatcher(DTypes edge_type, + DTypes weight_type, + bool store_transposed, + bool multi_gpu, + GTypes graph_type, + erased_pack_t& ep) +{ + switch (edge_type) { + case DTypes::INT32: { + using edge_t = typename DMapType::type; + return weight_dispatcher( + weight_type, store_transposed, multi_gpu, graph_type, ep); + } break; + case DTypes::INT64: { + using edge_t = typename DMapType::type; + return weight_dispatcher( + weight_type, store_transposed, multi_gpu, graph_type, ep); + } break; + case DTypes::FLOAT32: { + using edge_t = typename DMapType::type; + return weight_dispatcher( + weight_type, store_transposed, multi_gpu, graph_type, ep); + } break; + case DTypes::FLOAT64: { + using edge_t = typename DMapType::type; + return weight_dispatcher( + weight_type, store_transposed, multi_gpu, graph_type, ep); + } break; + default: { + std::stringstream ss; + ss << "ERROR: Unknown type enum:" << static_cast(edge_type); + throw std::runtime_error(ss.str()); + } + } +} + +// vertex type dispatcher: +// entry point, +// resolves vertex_t from vertex_type enum +// and cascades into the next level +// edge_dispatcher(); +// +inline decltype(auto) vertex_dispatcher(DTypes vertex_type, + DTypes edge_type, + DTypes weight_type, + bool store_transposed, + bool multi_gpu, + GTypes graph_type, + erased_pack_t& ep) +{ + switch (vertex_type) { + case DTypes::INT32: { + using vertex_t = typename DMapType::type; + return edge_dispatcher( + edge_type, weight_type, store_transposed, multi_gpu, graph_type, ep); + } break; + case DTypes::INT64: { + using vertex_t = typename DMapType::type; + return edge_dispatcher( + edge_type, weight_type, store_transposed, multi_gpu, graph_type, ep); + } break; + case DTypes::FLOAT32: { + using vertex_t = typename DMapType::type; + return edge_dispatcher( + edge_type, weight_type, store_transposed, multi_gpu, graph_type, ep); + } break; + case DTypes::FLOAT64: { + using vertex_t = typename DMapType::type; + return edge_dispatcher( + edge_type, weight_type, store_transposed, multi_gpu, graph_type, ep); + } break; + default: { + std::stringstream ss; + ss << "ERROR: Unknown type enum:" << static_cast(vertex_type); + throw std::runtime_error(ss.str()); + } + } +} + +} // namespace visitors +} // namespace cugraph diff --git a/cpp/include/cugraph/visitors/enum_mapping.hpp b/cpp/include/cugraph/visitors/enum_mapping.hpp new file mode 100755 index 00000000000..ab72f87bcab --- /dev/null +++ b/cpp/include/cugraph/visitors/enum_mapping.hpp @@ -0,0 +1,76 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// + +#pragma once + +#include + +namespace cugraph { +namespace visitors { + +enum class DTypes { INT32 = 0, INT64, FLOAT32, FLOAT64, NTYPES }; + +template +struct DMapType; + +template <> +struct DMapType { + using type = int32_t; +}; + +template <> +struct DMapType { + using type = int64_t; +}; + +template <> +struct DMapType { + using type = float; +}; + +template <> +struct DMapType { + using type = double; +}; + +template +struct reverse_dmap_t; + +template <> +struct reverse_dmap_t { + static constexpr DTypes type_id = DTypes::INT32; +}; + +template <> +struct reverse_dmap_t { + static constexpr DTypes type_id = DTypes::INT64; +}; + +template <> +struct reverse_dmap_t { + static constexpr DTypes type_id = DTypes::FLOAT32; +}; + +template <> +struct reverse_dmap_t { + static constexpr DTypes type_id = DTypes::FLOAT64; +}; + +} // namespace visitors +} // namespace cugraph diff --git a/cpp/include/cugraph/visitors/erased_api.hpp b/cpp/include/cugraph/visitors/erased_api.hpp new file mode 100644 index 00000000000..b85ee84bff6 --- /dev/null +++ b/cpp/include/cugraph/visitors/erased_api.hpp @@ -0,0 +1,46 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// + +/** + * @brief Set of type-erased wrappers, following the (almost) universal general signature: + * graph_envelope reference; erased_pack_t pack of erased arguments, that the caller is responsible + * to set correctly (FIXME: handshake protocol must be put in place); return set; + */ + +#pragma once + +namespace cugraph { +namespace api { + +using namespace cugraph::visitors; + +/** + * @brief Type-erased BFS wrapper. + * + * @param[in] g graph_envelope reference; + * @param[in] ep erased_pack_t pack of erased arguments, that the caller is responsible to set + * correctly (FIXME: handshake protocol must be put in place); + * @return return set; + */ +return_t bfs(graph_envelope_t const& g, erased_pack_t& ep); + +// TODO: more to follow... + +} // namespace api +} // namespace cugraph diff --git a/cpp/include/cugraph/visitors/erased_pack.hpp b/cpp/include/cugraph/visitors/erased_pack.hpp new file mode 100644 index 00000000000..eab1310eb02 --- /dev/null +++ b/cpp/include/cugraph/visitors/erased_pack.hpp @@ -0,0 +1,73 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// + +#pragma once + +#ifdef _DEBUG_ +#include +#endif + +#include +#include + +namespace cugraph { +namespace visitors { + +struct erased_pack_t { + erased_pack_t(void** p_args, size_t n) + : args_{[](void** p, size_t n) { + std::vector v; + v.insert(v.begin(), p, p + n); + return v; + }(p_args, n)} + { + // args_.insert(args_.begin(), p_args, p_args + n); + } + + erased_pack_t(std::initializer_list args) : args_{args} {} + + std::vector const& get_args(void) const { return args_; } + + erased_pack_t(erased_pack_t const&) = delete; + erased_pack_t& operator=(erased_pack_t const&) = delete; + + erased_pack_t(erased_pack_t&& other) : args_(std::move(other.args_)) {} + + erased_pack_t& operator=(erased_pack_t&& other) + { + args_ = std::move(other.args_); + return *this; + } + +#ifdef _DEBUG_ + void print(void) const + { + std::cout << "list args addresses:\n"; + for (auto&& elem : args_) + std::cout << elem << ", "; + std::cout << '\n'; + } +#endif + + private: + std::vector args_; +}; + +} // namespace visitors +} // namespace cugraph diff --git a/cpp/include/cugraph/visitors/graph_enum.hpp b/cpp/include/cugraph/visitors/graph_enum.hpp new file mode 100755 index 00000000000..eca53035313 --- /dev/null +++ b/cpp/include/cugraph/visitors/graph_enum.hpp @@ -0,0 +1,26 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// + +#pragma once + +namespace cugraph { +namespace visitors { +enum class GTypes { GRAPH_T = 0, GRAPH_VIEW_T, NTYPES }; +} // namespace visitors +} // namespace cugraph diff --git a/cpp/include/cugraph/visitors/graph_enum_mapping.hpp b/cpp/include/cugraph/visitors/graph_enum_mapping.hpp new file mode 100755 index 00000000000..dfde78b6b4b --- /dev/null +++ b/cpp/include/cugraph/visitors/graph_enum_mapping.hpp @@ -0,0 +1,44 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// + +#pragma once + +#include +#include "graph_enum.hpp" + +namespace cugraph { +namespace visitors { + +template +struct GMapType; // primary template, purposely empty + +// partial specializations: +// +template +struct GMapType { + using type = graph_t; +}; + +} // namespace visitors +} // namespace cugraph diff --git a/cpp/include/cugraph/visitors/graph_envelope.hpp b/cpp/include/cugraph/visitors/graph_envelope.hpp new file mode 100755 index 00000000000..d5701088783 --- /dev/null +++ b/cpp/include/cugraph/visitors/graph_envelope.hpp @@ -0,0 +1,208 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// + +/** + * @brief Set of classes abstracting the type-erasure, templates, and template constraints + * to client code that must supply run-time type information (RTTI) and has no template +constructs. + * + * Goal: be able to call an algorithm (say. louvain() on a type erased graph created from RTTI: + * { + * auto graph = make_graph(flags...); + * auto res = louvain(graph, params...); + * } + * params will be also type-erased (or same type regardless of graph-type); and will + * be appropriately passed to the Factory and then converted and passed to Visitor constructor +*/ + +#pragma once + +#include +#include +#include +#include +#include +#include + +#include "enum_mapping.hpp" +#include "graph_enum.hpp" + +#include + +namespace cugraph { +namespace visitors { + +using namespace cugraph::experimental; + +class erased_pack_t; // forward... +class return_t; // forward... + +// visitor base, incomplete: +// +class visitor_t; // forward... + +// envelope class around all +// graph classes: +// +struct graph_envelope_t { + struct base_graph_t { // necessary to avoid circular dependency + // between graph_base_t and graph_envelope_t + virtual ~base_graph_t() {} + + /// virtual void print(void) const = 0; + + virtual void apply(visitor_t& v) const = 0; + }; + + // abstract factory: + // + struct visitor_factory_t { + virtual std::unique_ptr make_louvain_visitor(erased_pack_t&) const = 0; + + virtual std::unique_ptr make_bfs_visitor(erased_pack_t&) const = 0; + }; + + using pair_uniques_t = + std::pair, std::unique_ptr>; + + void apply(visitor_t& v) const + { + if (p_impl_fact_.first) + p_impl_fact_.first->apply(v); + else + throw std::runtime_error("ERROR: Implementation not allocated."); + } + + // void print(void) const + // { + // if (p_impl_fact_.first) + // p_impl_fact_.first->print(); + // else + // throw std::runtime_error("ERROR: Implementation not allocated."); + // } + + std::unique_ptr const& graph(void) const { return p_impl_fact_.first; } + + std::unique_ptr const& factory(void) const { return p_impl_fact_.second; } + + graph_envelope_t(DTypes vertex_tid, + DTypes edge_tid, + DTypes weight_tid, + bool, + bool, + GTypes graph_tid, + erased_pack_t&); + + private: + // need it to hide the parameterization of + // (graph implementation, factory implementation) + // by dependent types: vertex_t, edge_t, weight_t + // + pair_uniques_t p_impl_fact_; +}; + +// visitor base: +// +class visitor_t { + public: + virtual ~visitor_t(void) {} + + virtual void visit_graph(graph_envelope_t::base_graph_t const&) = 0; + + virtual return_t const& get_result(void) const = 0; +}; + +// convenience templatized base: +// +template +struct dependent_graph_t : graph_envelope_t::base_graph_t { + using vertex_type = vertex_t; + using edge_type = edge_t; + using weight_type = weight_t; +}; + +// primary empty template: +// +template +struct dependent_factory_t; + +// dummy out non-candidate instantiation paths: +// +template +struct dependent_factory_t::value>> + : graph_envelope_t::visitor_factory_t { + using vertex_type = vertex_t; + using edge_type = edge_t; + using weight_type = weight_t; + + std::unique_ptr make_louvain_visitor(erased_pack_t&) const override { return nullptr; } + + std::unique_ptr make_bfs_visitor(erased_pack_t&) const override { return nullptr; } +}; + +template +struct dependent_factory_t::value>> + : graph_envelope_t::visitor_factory_t { + using vertex_type = vertex_t; + using edge_type = edge_t; + using weight_type = weight_t; + + std::unique_ptr make_louvain_visitor(erased_pack_t&) const override; + + std::unique_ptr make_bfs_visitor(erased_pack_t&) const override; +}; + +// utility factory selector: +// +template +std::unique_ptr make_visitor( + graph_type const& tag, // necessary to extract dependent types + std::function(graph_envelope_t::visitor_factory_t const&, + erased_pack_t&)> + f, // selector functor that picks up the make memf of the visitor_factory and passes `ep` to it + erased_pack_t& ep) // erased pack of args to be passed to factory +{ + using vertex_t = typename graph_type::vertex_type; + using edge_t = typename graph_type::edge_type; + using weight_t = typename graph_type::weight_type; + constexpr bool st = graph_type::is_adj_matrix_transposed; + constexpr bool mg = graph_type::is_multi_gpu; + + dependent_factory_t factory; + + return f(factory, ep); +} + +} // namespace visitors +} // namespace cugraph diff --git a/cpp/include/cugraph/visitors/graph_factory.hpp b/cpp/include/cugraph/visitors/graph_factory.hpp new file mode 100644 index 00000000000..22f53b72b01 --- /dev/null +++ b/cpp/include/cugraph/visitors/graph_factory.hpp @@ -0,0 +1,211 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// + +#pragma once + +#include +#include + +#include "graph_envelope.hpp" +// prevent clang-format to rearange order of headers +#include "erased_pack.hpp" +// +// not really needed here; +// just to make happy the clang-format policy +// of header inclusion to be order-independent... +// +#include +#include + +#define _DEBUG_ + +#ifdef _DEBUG_ +#include +#endif + +namespace cugraph { +namespace visitors { + +using namespace cugraph::experimental; + +struct graph_factory_base_t { + virtual ~graph_factory_base_t(void) {} + + virtual std::unique_ptr make_graph(erased_pack_t&) const = 0; +}; + +// argument unpacker (from `erased_pack_t`) +// for graph construction +// +template +struct graph_arg_unpacker_t { + using vertex_t = typename graph_type::vertex_type; + using edge_t = typename graph_type::edge_type; + using weight_t = typename graph_type::weight_type; + static constexpr bool st = graph_type::is_adj_matrix_transposed; + static constexpr bool mg = graph_type::is_multi_gpu; + + void operator()(erased_pack_t& ep, + std::tuple& t_args) const + { + } +}; + +// primary template factory; to be (partiallY) specialized; +// and explicitly instantiated for concrete graphs +// +template +struct graph_factory_t : graph_factory_base_t { + std::unique_ptr make_graph(erased_pack_t&) const override + { + throw std::runtime_error("Empty factory, not to be called..."); + } +}; + +// Linker PROBLEM (FIXED): +// dispatcher needs _ALL_ paths instantiated, +// not just the ones explicitly instantiated +// (EIDir) in `graph.cpp` +// +// Posiible SOLUTIONS: +// +// (1.) the _factory_ must provide "dummy" +// instantiations for paths not needed; +// +// or: +// +// (2.) (Adopted solution) +// the _dispatcher_ (graph_dispatcher()) +// must provide empty implementation +// for the instantiations that are not needed; (Done!) +// +template +struct graph_factory_t< + graph_t>> + : graph_factory_base_t { + std::unique_ptr make_graph(erased_pack_t& ep) const override + { + /// std::cout << "Multi-GPU factory.\n"; + std::vector const& v_args{ep.get_args()}; + + // invoke cnstr. using cython arg pack: + // + assert(v_args.size() == 9); + +#ifdef _DEBUG_ + std::cout << "Enter graph factory...\n"; +#endif + + // cnstr. args unpacking: + // + raft::handle_t const& handle = *static_cast(v_args[0]); + + vertex_t* src_vertices = static_cast(v_args[1]); + vertex_t* dst_vertices = static_cast(v_args[2]); + weight_t* weights = static_cast(v_args[3]); + vertex_t* vertex_partition_offsets = static_cast(v_args[4]); + edge_t num_partition_edges = *static_cast(v_args[5]); + vertex_t num_global_vertices = *static_cast(v_args[6]); + edge_t num_global_edges = *static_cast(v_args[7]); + bool sorted_by_degree = *static_cast(v_args[8]); + + // TODO: un-hardcode: have it passed int `ep` + // + graph_properties_t graph_props{.is_symmetric = false, .is_multigraph = false}; + bool do_expensive_check{false}; // FIXME: check what should this default to + + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_rank = row_comm.get_rank(); + auto const row_comm_size = row_comm.get_size(); // pcols + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_rank = col_comm.get_rank(); + auto const col_comm_size = col_comm.get_size(); // prows + + std::vector> edgelist( + {{src_vertices, dst_vertices, weights, num_partition_edges}}); + + std::vector partition_offsets_vector( + vertex_partition_offsets, vertex_partition_offsets + (row_comm_size * col_comm_size) + 1); + + partition_t partition( + partition_offsets_vector, row_comm_size, col_comm_size, row_comm_rank, col_comm_rank); + + std::optional> + opt_seg_off{}; // FIXME: may needd to pass/extract segment_offsets vector + + return std::make_unique>( + handle, + edgelist, + partition, + num_global_vertices, + num_global_edges, + graph_props, + opt_seg_off, + do_expensive_check); + } +}; + +template +struct graph_factory_t< + graph_t>> + : graph_factory_base_t { + std::unique_ptr make_graph(erased_pack_t& ep) const override + { + /// std::cout << "Single-GPU factory.\n"; + std::vector const& v_args{ep.get_args()}; + + assert(v_args.size() == 6); + + raft::handle_t const& handle = *static_cast(v_args[0]); + + auto const& elist = *static_cast const*>(v_args[1]); + + auto nv = *static_cast(v_args[2]); + + auto props = *static_cast(v_args[3]); + + bool sorted = *static_cast(v_args[4]); // FIXME: no need to pass this! + + bool check = *static_cast(v_args[5]); + + std::optional> opt_seg_off{}; // should not be needed for (!multi_gpu) + + return std::make_unique>( + handle, elist, nv, props, opt_seg_off, check); + } +}; + +} // namespace visitors +} // namespace cugraph diff --git a/cpp/include/cugraph/visitors/ret_terased.hpp b/cpp/include/cugraph/visitors/ret_terased.hpp new file mode 100644 index 00000000000..19a7920e81b --- /dev/null +++ b/cpp/include/cugraph/visitors/ret_terased.hpp @@ -0,0 +1,101 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// +#pragma once + +#include +#include + +namespace cugraph { +namespace visitors { + +struct return_t { + struct base_return_t { + virtual ~base_return_t(void) {} + + virtual void copy(return_t const&) = 0; + virtual std::unique_ptr clone(void) const = 0; + }; + + template + struct generic_return_t : base_return_t { + generic_return_t(T const& t) : return_(t) {} + + void copy(return_t const& r) override + { + base_return_t const* p_B = static_cast(r.p_impl_.get()); + return_ = *(dynamic_cast(p_B)); + } + + std::unique_ptr clone(void) const override + { + return std::make_unique>(return_); + } + + T const& get(void) const { return return_; } + + private: + T return_; + }; + + return_t(void) = default; + + template + return_t(T const& t) : p_impl_(std::make_unique>(t)) + { + } + + return_t(return_t const& r) : p_impl_{r.clone()} {} + + return_t& operator=(return_t const& r) + { + p_impl_ = r.clone(); + return *this; + } + + return_t(return_t&& other) : p_impl_(std::move(other.p_impl_)) {} + return_t& operator=(return_t&& other) + { + p_impl_ = std::move(other.p_impl_); + return *this; + } + + std::unique_ptr clone(void) const + { + if (p_impl_) + return p_impl_->clone(); + else + return nullptr; + } + + template + T get(void) const + { + if (p_impl_) { + generic_return_t const* p = static_cast const*>(p_impl_.get()); + return p->get(); + } else + throw std::runtime_error("ERROR: nullptr impl."); + } + + private: + std::unique_ptr p_impl_; +}; + +} // namespace visitors +} // namespace cugraph diff --git a/cpp/include/experimental/detail/graph_utils.cuh b/cpp/include/experimental/detail/graph_utils.cuh deleted file mode 100644 index bf56b2e6f80..00000000000 --- a/cpp/include/experimental/detail/graph_utils.cuh +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Copyright (c) 2020, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#pragma once - -#include -#include - -#include -#include -#include - -#include -#include - -#include -#include - -namespace cugraph { -namespace experimental { -namespace detail { - -// compute the numbers of nonzeros in rows (of the graph adjacency matrix, if store_transposed = -// false) or columns (of the graph adjacency matrix, if store_transposed = true) -template -rmm::device_uvector compute_major_degree( - raft::handle_t const &handle, - std::vector const &adj_matrix_partition_offsets, - partition_t const &partition) -{ - auto &row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); - auto const row_comm_rank = row_comm.get_rank(); - auto const row_comm_size = row_comm.get_size(); - auto &col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); - auto const col_comm_rank = col_comm.get_rank(); - auto const col_comm_size = col_comm.get_size(); - - rmm::device_uvector local_degrees(0, handle.get_stream()); - rmm::device_uvector degrees(0, handle.get_stream()); - - vertex_t max_num_local_degrees{0}; - for (int i = 0; i < (partition.is_hypergraph_partitioned() ? col_comm_size : row_comm_size); - ++i) { - auto vertex_partition_idx = partition.is_hypergraph_partitioned() - ? static_cast(i * row_comm_size + row_comm_rank) - : static_cast(col_comm_rank * row_comm_size + i); - auto vertex_partition_size = partition.get_vertex_partition_size(vertex_partition_idx); - max_num_local_degrees = std::max(max_num_local_degrees, vertex_partition_size); - if (i == (partition.is_hypergraph_partitioned() ? col_comm_rank : row_comm_rank)) { - degrees.resize(vertex_partition_size, handle.get_stream()); - } - } - local_degrees.resize(max_num_local_degrees, handle.get_stream()); - for (int i = 0; i < (partition.is_hypergraph_partitioned() ? col_comm_size : row_comm_size); - ++i) { - auto vertex_partition_idx = partition.is_hypergraph_partitioned() - ? static_cast(i * row_comm_size + row_comm_rank) - : static_cast(col_comm_rank * row_comm_size + i); - vertex_t major_first{}; - vertex_t major_last{}; - std::tie(major_first, major_last) = partition.get_vertex_partition_range(vertex_partition_idx); - auto p_offsets = - partition.is_hypergraph_partitioned() - ? adj_matrix_partition_offsets[i] - : adj_matrix_partition_offsets[0] + - (major_first - partition.get_vertex_partition_first(col_comm_rank * row_comm_size)); - thrust::transform(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - thrust::make_counting_iterator(vertex_t{0}), - thrust::make_counting_iterator(major_last - major_first), - local_degrees.data(), - [p_offsets] __device__(auto i) { return p_offsets[i + 1] - p_offsets[i]; }); - if (partition.is_hypergraph_partitioned()) { - col_comm.reduce(local_degrees.data(), - i == col_comm_rank ? degrees.data() : static_cast(nullptr), - static_cast(major_last - major_first), - raft::comms::op_t::SUM, - i, - handle.get_stream()); - } else { - row_comm.reduce(local_degrees.data(), - i == row_comm_rank ? degrees.data() : static_cast(nullptr), - static_cast(major_last - major_first), - raft::comms::op_t::SUM, - i, - handle.get_stream()); - } - } - - raft::comms::status_t status{}; - if (partition.is_hypergraph_partitioned()) { - status = - col_comm.sync_stream(handle.get_stream()); // this is neessary as local_degrees will become - // out-of-scope once this function returns. - } else { - status = - row_comm.sync_stream(handle.get_stream()); // this is neessary as local_degrees will become - // out-of-scope once this function returns. - } - CUGRAPH_EXPECTS(status == raft::comms::status_t::SUCCESS, "sync_stream() failure."); - - return degrees; -} - -// compute the numbers of nonzeros in rows (of the graph adjacency matrix, if store_transposed = -// false) or columns (of the graph adjacency matrix, if store_transposed = true) -template -rmm::device_uvector compute_major_degree( - raft::handle_t const &handle, - std::vector> const &adj_matrix_partition_offsets, - partition_t const &partition) -{ - // we can avoid creating this temporary with "if constexpr" supported from C++17 - std::vector tmp_offsets(adj_matrix_partition_offsets.size(), nullptr); - std::transform(adj_matrix_partition_offsets.begin(), - adj_matrix_partition_offsets.end(), - tmp_offsets.begin(), - [](auto const &offsets) { return offsets.data(); }); - return compute_major_degree(handle, tmp_offsets, partition); -} - -template -struct degree_from_offsets_t { - edge_t const *offsets{nullptr}; - - __device__ edge_t operator()(vertex_t v) { return offsets[v + 1] - offsets[v]; } -}; - -} // namespace detail -} // namespace experimental -} // namespace cugraph diff --git a/cpp/include/matrix_partition_device.cuh b/cpp/include/matrix_partition_device.cuh deleted file mode 100644 index 53796530f60..00000000000 --- a/cpp/include/matrix_partition_device.cuh +++ /dev/null @@ -1,246 +0,0 @@ -/* - * Copyright (c) 2020, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#pragma once - -#include -#include - -#include - -#include - -namespace cugraph { -namespace experimental { - -template -class matrix_partition_device_base_t { - public: - matrix_partition_device_base_t(edge_t const* offsets, - vertex_t const* indices, - weight_t const* weights) - : offsets_(offsets), indices_(indices), weights_(weights) - { - } - - __device__ thrust::tuple get_local_edges( - vertex_t major_offset) const noexcept - { - auto edge_offset = *(offsets_ + major_offset); - auto local_degree = *(offsets_ + (major_offset + 1)) - edge_offset; - auto indices = indices_ + edge_offset; - auto weights = weights_ != nullptr ? weights_ + edge_offset : nullptr; - return thrust::make_tuple(indices, weights, local_degree); - } - - __device__ edge_t get_local_degree(vertex_t major_offset) const noexcept - { - return *(offsets_ + (major_offset + 1)) - *(offsets_ + major_offset); - } - - private: - // should be trivially copyable to device - edge_t const* offsets_{nullptr}; - vertex_t const* indices_{nullptr}; - weight_t const* weights_{nullptr}; -}; - -template -class matrix_partition_device_t; - -// multi-GPU version -template -class matrix_partition_device_t> - : public matrix_partition_device_base_t { - public: - matrix_partition_device_t(GraphViewType const& graph_view, size_t partition_idx) - : matrix_partition_device_base_t( - graph_view.offsets(partition_idx), - graph_view.indices(partition_idx), - graph_view.weights(partition_idx)), - major_first_(GraphViewType::is_adj_matrix_transposed - ? graph_view.get_local_adj_matrix_partition_col_first(partition_idx) - : graph_view.get_local_adj_matrix_partition_row_first(partition_idx)), - major_last_(GraphViewType::is_adj_matrix_transposed - ? graph_view.get_local_adj_matrix_partition_col_last(partition_idx) - : graph_view.get_local_adj_matrix_partition_row_last(partition_idx)), - minor_first_(GraphViewType::is_adj_matrix_transposed - ? graph_view.get_local_adj_matrix_partition_row_first(partition_idx) - : graph_view.get_local_adj_matrix_partition_col_first(partition_idx)), - minor_last_(GraphViewType::is_adj_matrix_transposed - ? graph_view.get_local_adj_matrix_partition_row_last(partition_idx) - : graph_view.get_local_adj_matrix_partition_col_last(partition_idx)), - major_value_start_offset_( - GraphViewType::is_adj_matrix_transposed - ? graph_view.get_local_adj_matrix_partition_col_value_start_offset(partition_idx) - : graph_view.get_local_adj_matrix_partition_row_value_start_offset(partition_idx)) - { - } - - typename GraphViewType::vertex_type get_major_value_start_offset() const - { - return major_value_start_offset_; - } - - __host__ __device__ typename GraphViewType::vertex_type get_major_first() const noexcept - { - return major_first_; - } - - __host__ __device__ typename GraphViewType::vertex_type get_major_last() const noexcept - { - return major_last_; - } - - __host__ __device__ typename GraphViewType::vertex_type get_major_size() const noexcept - { - return major_last_ - major_first_; - } - - __host__ __device__ typename GraphViewType::vertex_type get_minor_first() const noexcept - { - return minor_first_; - } - - __host__ __device__ typename GraphViewType::vertex_type get_minor_last() const noexcept - { - return minor_last_; - } - - __host__ __device__ typename GraphViewType::vertex_type get_minor_size() const noexcept - { - return minor_last_ - minor_first_; - } - - __host__ __device__ typename GraphViewType::vertex_type get_major_offset_from_major_nocheck( - typename GraphViewType::vertex_type major) const noexcept - { - return major - major_first_; - } - - __host__ __device__ typename GraphViewType::vertex_type get_minor_offset_from_minor_nocheck( - typename GraphViewType::vertex_type minor) const noexcept - { - return minor - minor_first_; - } - - __host__ __device__ typename GraphViewType::vertex_type get_major_from_major_offset_nocheck( - typename GraphViewType::vertex_type major_offset) const noexcept - { - return major_first_ + major_offset; - } - - __host__ __device__ typename GraphViewType::vertex_type get_minor_from_minor_offset_nocheck( - typename GraphViewType::vertex_type minor_offset) const noexcept - { - return minor_first_ + minor_offset; - } - - private: - // should be trivially copyable to device - typename GraphViewType::vertex_type major_first_{0}; - typename GraphViewType::vertex_type major_last_{0}; - typename GraphViewType::vertex_type minor_first_{0}; - typename GraphViewType::vertex_type minor_last_{0}; - - typename GraphViewType::vertex_type major_value_start_offset_{0}; -}; - -// single-GPU version -template -class matrix_partition_device_t> - : public matrix_partition_device_base_t { - public: - matrix_partition_device_t(GraphViewType const& graph_view, size_t partition_idx) - : matrix_partition_device_base_t( - graph_view.offsets(), graph_view.indices(), graph_view.weights()), - number_of_vertices_(graph_view.get_number_of_vertices()) - { - assert(partition_idx == 0); - } - - typename GraphViewType::vertex_type get_major_value_start_offset() const - { - return typename GraphViewType::vertex_type{0}; - } - - __host__ __device__ constexpr typename GraphViewType::vertex_type get_major_first() const noexcept - { - return typename GraphViewType::vertex_type{0}; - } - - __host__ __device__ typename GraphViewType::vertex_type get_major_last() const noexcept - { - return number_of_vertices_; - } - - __host__ __device__ typename GraphViewType::vertex_type get_major_size() const noexcept - { - return number_of_vertices_; - } - - __host__ __device__ constexpr typename GraphViewType::vertex_type get_minor_first() const noexcept - { - return typename GraphViewType::vertex_type{0}; - } - - __host__ __device__ typename GraphViewType::vertex_type get_minor_last() const noexcept - { - return number_of_vertices_; - } - - __host__ __device__ typename GraphViewType::vertex_type get_minor_size() const noexcept - { - return number_of_vertices_; - } - - __host__ __device__ typename GraphViewType::vertex_type get_major_offset_from_major_nocheck( - typename GraphViewType::vertex_type major) const noexcept - { - return major; - } - - __host__ __device__ typename GraphViewType::vertex_type get_minor_offset_from_minor_nocheck( - typename GraphViewType::vertex_type minor) const noexcept - { - return minor; - } - - __host__ __device__ typename GraphViewType::vertex_type get_major_from_major_offset_nocheck( - typename GraphViewType::vertex_type major_offset) const noexcept - { - return major_offset; - } - - __host__ __device__ typename GraphViewType::vertex_type get_minor_from_minor_offset_nocheck( - typename GraphViewType::vertex_type minor_offset) const noexcept - { - return minor_offset; - } - - private: - typename GraphViewType::vertex_type number_of_vertices_; -}; - -} // namespace experimental -} // namespace cugraph diff --git a/cpp/include/patterns/any_of_adj_matrix_row.cuh b/cpp/include/patterns/any_of_adj_matrix_row.cuh deleted file mode 100644 index 199e7c230ef..00000000000 --- a/cpp/include/patterns/any_of_adj_matrix_row.cuh +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (c) 2020, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#pragma once - -#include -#include -#include - -#include -#include - -#include -#include - -namespace cugraph { -namespace experimental { - -/** - * @brief Check any of graph adjacency matrix row properties satisfy the given predicate. - * - * Returns true if @p row_op returns true for at least once (in any process in multi-GPU), returns - * false otherwise. This function is inspired by thrust::any_of(). - * - * @tparam GraphViewType Type of the passed non-owning graph object. - * @tparam AdjMatrixRowValueInputIterator Type of the iterator for graph adjacency matrix row - * input properties. - * @tparam RowOp Type of the unary predicate operator. - * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and - * handles to various CUDA libraries) to run graph algorithms. - * @param graph_view Non-owning graph object. - * @param adj_matrix_row_value_input_first Iterator pointing to the adjacency matrix row properties - * for the first (inclusive) row (assigned to this process in multi-GPU). - * `adj_matrix_row_value_input_last` (exclusive) is deduced as @p adj_matrix_row_value_input_first + - * @p graph_view.get_number_of_local_adj_matrix_partition_rows(). - * @param row_op Unary predicate operator that takes *(@p adj_matrix_row_value_input_first + i) - * (where i = [0, @p graph_view.get_number_of_local_adj_matrix_partition_rows()) and returns either - * true or false. - * @return true If the predicate returns true at least once (in any process in multi-GPU). - * @return false If the predicate never returns true (in any process in multi-GPU). - */ -template -bool any_of_adj_matrix_row(raft::handle_t const& handle, - GraphViewType const& graph_view, - AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, - RowOp row_op) -{ - // better use thrust::any_of once https://github.com/thrust/thrust/issues/1016 is resolved - auto count = thrust::count_if( - rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - adj_matrix_row_value_input_first, - adj_matrix_row_value_input_first + graph_view.get_number_of_local_adj_matrix_partition_rows(), - row_op); - if (GraphViewType::is_multi_gpu) { - count = host_scalar_allreduce(handle.get_comms(), count, handle.get_stream()); - } - return (count > 0); -} - -} // namespace experimental -} // namespace cugraph diff --git a/cpp/include/patterns/count_if_e.cuh b/cpp/include/patterns/count_if_e.cuh deleted file mode 100644 index 4f0f0a7a43e..00000000000 --- a/cpp/include/patterns/count_if_e.cuh +++ /dev/null @@ -1,237 +0,0 @@ -/* - * Copyright (c) 2020, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#pragma once - -#include -#include -#include -#include -#include - -#include -#include -#include - -#include -#include - -#include -#include - -namespace cugraph { -namespace experimental { - -namespace detail { - -// FIXME: block size requires tuning -int32_t constexpr count_if_e_for_all_block_size = 128; - -// FIXME: function names conflict if included with transform_reduce_e.cuh -template -__global__ void for_all_major_for_all_nbr_low_degree( - matrix_partition_device_t matrix_partition, - AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, - AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, - typename GraphViewType::edge_type* block_counts, - EdgeOp e_op) -{ - using vertex_t = typename GraphViewType::vertex_type; - using edge_t = typename GraphViewType::edge_type; - using weight_t = typename GraphViewType::weight_type; - - auto const tid = threadIdx.x + blockIdx.x * blockDim.x; - auto idx = static_cast(tid); - - edge_t count{0}; - while (idx < static_cast(matrix_partition.get_major_size())) { - vertex_t const* indices{nullptr}; - weight_t const* weights{nullptr}; - edge_t local_degree{}; - thrust::tie(indices, weights, local_degree) = matrix_partition.get_local_edges(idx); -#if 1 - count += thrust::count_if( - thrust::seq, - thrust::make_counting_iterator(edge_t{0}), - thrust::make_counting_iterator(local_degree), - [&matrix_partition, - &adj_matrix_row_value_input_first, - &adj_matrix_col_value_input_first, - &e_op, - idx, - indices, - weights] __device__(auto i) { - auto minor = indices[i]; - auto weight = weights != nullptr ? weights[i] : 1.0; - auto minor_offset = matrix_partition.get_minor_offset_from_minor_nocheck(minor); - auto row = GraphViewType::is_adj_matrix_transposed - ? minor - : matrix_partition.get_major_from_major_offset_nocheck(idx); - auto col = GraphViewType::is_adj_matrix_transposed - ? matrix_partition.get_major_from_major_offset_nocheck(idx) - : minor; - auto row_offset = - GraphViewType::is_adj_matrix_transposed ? minor_offset : static_cast(idx); - auto col_offset = - GraphViewType::is_adj_matrix_transposed ? static_cast(idx) : minor_offset; - auto e_op_result = evaluate_edge_op() - .compute(row, - col, - weight, - *(adj_matrix_row_value_input_first + row_offset), - *(adj_matrix_col_value_input_first + col_offset), - e_op); - - return e_op_result; - }); -#else - // FIXME: delete this once we verify that the code above is not slower than this. - for (vertex_t i = 0; i < local_degree; ++i) { - auto minor = indices[i]; - auto weight = weights != nullptr ? weights[i] : 1.0; - auto minor_offset = matrix_partition.get_minor_offset_from_minor_nocheck(minor); - auto row = GraphViewType::is_adj_matrix_transposed - ? minor - : matrix_partition.get_major_from_major_offset_nocheck(idx); - auto col = GraphViewType::is_adj_matrix_transposed - ? matrix_partition.get_major_from_major_offset_nocheck(idx) - : minor; - auto row_offset = - GraphViewType::is_adj_matrix_transposed ? minor_offset : static_cast(idx); - auto col_offset = - GraphViewType::is_adj_matrix_transposed ? static_cast(idx) : minor_offset; - auto e_op_result = evaluate_edge_op() - .compute(row, - col, - weight, - *(adj_matrix_row_value_input_first + row_offset), - *(adj_matrix_col_value_input_first + col_offset), - e_op); - if (e_op_result) { count++; } - } -#endif - idx += gridDim.x * blockDim.x; - } - - using BlockReduce = cub::BlockReduce; - __shared__ typename BlockReduce::TempStorage temp_storage; - count = BlockReduce(temp_storage).Sum(count); - if (threadIdx.x == 0) { *(block_counts + blockIdx.x) = count; } -} - -} // namespace detail - -/** - * @brief Count the number of edges that satisfies the given predicate. - * - * This function is inspired by thrust::count_if(). - * - * @tparam GraphViewType Type of the passed non-owning graph object. - * @tparam AdjMatrixRowValueInputIterator Type of the iterator for graph adjacency matrix row - * input properties. - * @tparam AdjMatrixColValueInputIterator Type of the iterator for graph adjacency matrix column - * input properties. - * @tparam EdgeOp Type of the quaternary (or quinary) edge operator. - * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and - * handles to various CUDA libraries) to run graph algorithms. - * @param graph_view Non-owning graph object. - * @param adj_matrix_row_value_input_first Iterator pointing to the adjacency matrix row input - * properties for the first (inclusive) row (assigned to this process in multi-GPU). - * `adj_matrix_row_value_input_last` (exclusive) is deduced as @p adj_matrix_row_value_input_first + - * @p graph_view.get_number_of_local_adj_matrix_partition_rows(). - * @param adj_matrix_col_value_input_first Iterator pointing to the adjacency matrix column input - * properties for the first (inclusive) column (assigned to this process in multi-GPU). - * `adj_matrix_col_value_output_last` (exclusive) is deduced as @p adj_matrix_col_value_output_first - * + @p graph_view.get_number_of_local_adj_matrix_partition_cols(). - * @param e_op Quaternary (or quinary) operator takes edge source, edge destination, (optional edge - * weight), *(@p adj_matrix_row_value_input_first + i), and *(@p adj_matrix_col_value_input_first + - * j) (where i is in [0, graph_view.get_number_of_local_adj_matrix_partition_rows()) and j is in [0, - * get_number_of_local_adj_matrix_partition_cols())) and returns true if this edge should be - * included in the returned count. - * @return GraphViewType::edge_type Number of times @p e_op returned true. - */ -template -typename GraphViewType::edge_type count_if_e( - raft::handle_t const& handle, - GraphViewType const& graph_view, - AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, - AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, - EdgeOp e_op) -{ - using vertex_t = typename GraphViewType::vertex_type; - using edge_t = typename GraphViewType::edge_type; - - edge_t count{0}; - for (size_t i = 0; i < graph_view.get_number_of_local_adj_matrix_partitions(); ++i) { - matrix_partition_device_t matrix_partition(graph_view, i); - - if (matrix_partition.get_major_size() > 0) { - auto row_value_input_offset = GraphViewType::is_adj_matrix_transposed - ? vertex_t{0} - : matrix_partition.get_major_value_start_offset(); - auto col_value_input_offset = GraphViewType::is_adj_matrix_transposed - ? matrix_partition.get_major_value_start_offset() - : vertex_t{0}; - - raft::grid_1d_thread_t update_grid(matrix_partition.get_major_size(), - detail::count_if_e_for_all_block_size, - handle.get_device_properties().maxGridSize[0]); - - rmm::device_vector block_counts(update_grid.num_blocks); - - detail::for_all_major_for_all_nbr_low_degree<<>>( - matrix_partition, - adj_matrix_row_value_input_first + row_value_input_offset, - adj_matrix_col_value_input_first + col_value_input_offset, - block_counts.data().get(), - e_op); - - // FIXME: we have several options to implement this. With cooperative group support - // (https://devblogs.nvidia.com/cooperative-groups/), we can run this synchronization within - // the previous kernel. Using atomics at the end of the previous kernel is another option - // (sequentialization due to atomics may not be bad as different blocks may reach the - // synchronization point in varying timings and the number of SMs is not very big) - count += thrust::reduce(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - block_counts.begin(), - block_counts.end(), - edge_t{0}, - thrust::plus()); - } - } - - if (GraphViewType::is_multi_gpu) { - count = host_scalar_allreduce(handle.get_comms(), count, handle.get_stream()); - } - - return count; -} - -} // namespace experimental -} // namespace cugraph diff --git a/cpp/include/patterns/transform_reduce_e.cuh b/cpp/include/patterns/transform_reduce_e.cuh deleted file mode 100644 index 797facd4657..00000000000 --- a/cpp/include/patterns/transform_reduce_e.cuh +++ /dev/null @@ -1,244 +0,0 @@ -/* - * Copyright (c) 2020, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#pragma once - -#include -#include -#include -#include - -#include -#include -#include - -#include - -#include -#include - -namespace cugraph { -namespace experimental { - -namespace detail { - -// FIXME: block size requires tuning -int32_t constexpr transform_reduce_e_for_all_block_size = 128; - -template -__global__ void for_all_major_for_all_nbr_low_degree( - matrix_partition_device_t matrix_partition, - AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, - AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, - BlockResultIterator block_result_first, - EdgeOp e_op) -{ - using vertex_t = typename GraphViewType::vertex_type; - using edge_t = typename GraphViewType::edge_type; - using weight_t = typename GraphViewType::weight_type; - using e_op_result_t = typename std::iterator_traits::value_type; - - auto const tid = threadIdx.x + blockIdx.x * blockDim.x; - size_t idx = static_cast(tid); - - e_op_result_t e_op_result_sum{}; - while (idx < static_cast(matrix_partition.get_major_size())) { - vertex_t const* indices{nullptr}; - weight_t const* weights{nullptr}; - edge_t local_degree{}; - thrust::tie(indices, weights, local_degree) = matrix_partition.get_local_edges(idx); -#if 1 - auto sum = thrust::transform_reduce( - thrust::seq, - thrust::make_counting_iterator(edge_t{0}), - thrust::make_counting_iterator(local_degree), - [&matrix_partition, - &adj_matrix_row_value_input_first, - &adj_matrix_col_value_input_first, - &e_op, - idx, - indices, - weights] __device__(auto i) { - auto minor = indices[i]; - auto weight = weights != nullptr ? weights[i] : weight_t{1.0}; - auto minor_offset = matrix_partition.get_minor_offset_from_minor_nocheck(minor); - auto row = GraphViewType::is_adj_matrix_transposed - ? minor - : matrix_partition.get_major_from_major_offset_nocheck(idx); - auto col = GraphViewType::is_adj_matrix_transposed - ? matrix_partition.get_major_from_major_offset_nocheck(idx) - : minor; - auto row_offset = - GraphViewType::is_adj_matrix_transposed ? minor_offset : static_cast(idx); - auto col_offset = - GraphViewType::is_adj_matrix_transposed ? static_cast(idx) : minor_offset; - return evaluate_edge_op() - .compute(row, - col, - weight, - *(adj_matrix_row_value_input_first + row_offset), - *(adj_matrix_col_value_input_first + col_offset), - e_op); - }, - e_op_result_t{}, - [] __device__(auto lhs, auto rhs) { return plus_edge_op_result(lhs, rhs); }); - - e_op_result_sum = plus_edge_op_result(e_op_result_sum, sum); -#else - // FIXME: delete this once we verify that the code above is not slower than this. - for (vertex_t i = 0; i < local_degree; ++i) { - auto minor = indices[i]; - auto weight = weights != nullptr ? weights[i] : weight_t{1.0}; - auto minor_offset = matrix_partition.get_minor_offset_from_minor_nocheck(minor); - auto row = GraphViewType::is_adj_matrix_transposed - ? minor - : matrix_partition.get_major_from_major_offset_nocheck(idx); - auto col = GraphViewType::is_adj_matrix_transposed - ? matrix_partition.get_major_from_major_offset_nocheck(idx) - : minor; - auto row_offset = - GraphViewType::is_adj_matrix_transposed ? minor_offset : static_cast(idx); - auto col_offset = - GraphViewType::is_adj_matrix_transposed ? static_cast(idx) : minor_offset; - auto e_op_result = evaluate_edge_op() - .compute(row, - col, - weight, - *(adj_matrix_row_value_input_first + row_offset), - *(adj_matrix_col_value_input_first + col_offset), - e_op); - e_op_result_sum = plus_edge_op_result(e_op_result_sum, e_op_result); - } -#endif - idx += gridDim.x * blockDim.x; - } - - e_op_result_sum = - block_reduce_edge_op_result().compute( - e_op_result_sum); - if (threadIdx.x == 0) { *(block_result_first + blockIdx.x) = e_op_result_sum; } -} - -} // namespace detail - -/** - * @brief Iterate over the entire set of edges and reduce @p edge_op outputs. - * - * This function is inspired by thrust::transform_reduce(). - * - * @tparam GraphViewType Type of the passed non-owning graph object. - * @tparam AdjMatrixRowValueInputIterator Type of the iterator for graph adjacency matrix row - * input properties. - * @tparam AdjMatrixColValueInputIterator Type of the iterator for graph adjacency matrix column - * input properties. - * @tparam EdgeOp Type of the quaternary (or quinary) edge operator. - * @tparam T Type of the initial value. - * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and - * handles to various CUDA libraries) to run graph algorithms. - * @param graph_view Non-owning graph object. - * @param adj_matrix_row_value_input_first Iterator pointing to the adjacency matrix row input - * properties for the first (inclusive) row (assigned to this process in multi-GPU). - * `adj_matrix_row_value_input_last` (exclusive) is deduced as @p adj_matrix_row_value_input_first + - * @p graph_view.get_number_of_local_adj_matrix_partition_rows(). - * @param adj_matrix_col_value_input_first Iterator pointing to the adjacency matrix column input - * properties for the first (inclusive) column (assigned to this process in multi-GPU). - * `adj_matrix_col_value_output_last` (exclusive) is deduced as @p adj_matrix_col_value_output_first - * + @p graph_view.get_number_of_local_adj_matrix_partition_cols(). - * @param e_op Quaternary (or quinary) operator takes edge source, edge destination, (optional edge - * weight), *(@p adj_matrix_row_value_input_first + i), and *(@p adj_matrix_col_value_input_first + - * j) (where i is in [0, graph_view.get_number_of_local_adj_matrix_partition_rows()) and j is in [0, - * get_number_of_local_adj_matrix_partition_cols())) and returns a transformed value to be reduced. - * @param init Initial value to be added to the transform-reduced input vertex properties. - * @return T Reduction of the @p edge_op outputs. - */ -template -T transform_reduce_e(raft::handle_t const& handle, - GraphViewType const& graph_view, - AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, - AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, - EdgeOp e_op, - T init) -{ - static_assert(is_arithmetic_or_thrust_tuple_of_arithmetic::value); - - using vertex_t = typename GraphViewType::vertex_type; - - T result{}; - for (size_t i = 0; i < graph_view.get_number_of_local_adj_matrix_partitions(); ++i) { - matrix_partition_device_t matrix_partition(graph_view, i); - - if (matrix_partition.get_major_size() > 0) { - auto row_value_input_offset = GraphViewType::is_adj_matrix_transposed - ? vertex_t{0} - : matrix_partition.get_major_value_start_offset(); - auto col_value_input_offset = GraphViewType::is_adj_matrix_transposed - ? matrix_partition.get_major_value_start_offset() - : vertex_t{0}; - - raft::grid_1d_thread_t update_grid(matrix_partition.get_major_size(), - detail::transform_reduce_e_for_all_block_size, - handle.get_device_properties().maxGridSize[0]); - - rmm::device_vector block_results(update_grid.num_blocks); - - detail::for_all_major_for_all_nbr_low_degree<<>>( - matrix_partition, - adj_matrix_row_value_input_first + row_value_input_offset, - adj_matrix_col_value_input_first + col_value_input_offset, - block_results.data(), - e_op); - - // FIXME: we have several options to implement this. With cooperative group support - // (https://devblogs.nvidia.com/cooperative-groups/), we can run this synchronization within - // the previous kernel. Using atomics at the end of the previous kernel is another option - // (sequentialization due to atomics may not be bad as different blocks may reach the - // synchronization point in varying timings and the number of SMs is not very big) - auto partial_result = - thrust::reduce(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - block_results.begin(), - block_results.end(), - T(), - [] __device__(auto lhs, auto rhs) { return plus_edge_op_result(lhs, rhs); }); - - result = plus_edge_op_result(result, partial_result); - } - } - - if (GraphViewType::is_multi_gpu) { - result = host_scalar_allreduce(handle.get_comms(), result, handle.get_stream()); - } - - return plus_edge_op_result(init, result); -} - -} // namespace experimental -} // namespace cugraph diff --git a/cpp/include/patterns/transform_reduce_v_with_adj_matrix_row.cuh b/cpp/include/patterns/transform_reduce_v_with_adj_matrix_row.cuh deleted file mode 100644 index f5af03d647c..00000000000 --- a/cpp/include/patterns/transform_reduce_v_with_adj_matrix_row.cuh +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2020, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#pragma once - -#include -#include -#include - -#include - -#include -#include -#include - -namespace cugraph { -namespace experimental { - -/** - * @brief Apply an operator to the matching vertex and adjacency matrix row properties and reduce. - * - * i'th vertex matches with the i'th row in the graph adjacency matrix. @p v_op takes vertex - * properties and adjacency matrix row properties for the matching row, and @p v_op outputs are - * reduced. This function is inspired by thrust::transform_reduce(). - * - * @tparam GraphViewType Type of the passed non-owning graph object. - * @tparam VertexValueInputIterator Type of the iterator for vertex properties. - * @tparam AdjMatrixRowValueInputIterator Type of the iterator for graph adjacency matrix column - * input properties. - * @tparam VertexOp Type of the binary vertex operator. - * @tparam T Type of the initial value. - * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and - * handles to various CUDA libraries) to run graph algorithms. - * @param graph_view Non-owning graph object. - * @param vertex_value_input_first Iterator pointing to the vertex properties for the first - * (inclusive) vertex (assigned to this process in multi-GPU). `vertex_value_input_last` (exclusive) - * is deduced as @p vertex_value_input_first + @p graph_view.get_number_of_local_vertices(). - * @param adj_matrix_row_value_input_first Iterator pointing to the adjacency matrix row input - * properties for the first (inclusive) row (assigned to this process in multi-GPU). - * `adj_matrix_row_value_input_last` (exclusive) is deduced as @p adj_matrix_row_value_input_first + - * @p graph_view.get_number_of_local_adj_matrix_partition_rows(). - * @param v_op Binary operator takes *(@p vertex_value_input_first + i) and *(@p - * adj_matrix_row_value_input_first + j) (where i and j are set for a vertex and the matching row) - * and returns a transformed value to be reduced. - * @param init Initial value to be added to the transform-reduced input vertex properties. - * @return T Reduction of the @p v_op outputs. - */ -template -T transform_reduce_v_with_adj_matrix_row( - raft::handle_t const& handle, - GraphViewType const& graph_view, - VertexValueInputIterator vertex_value_input_first, - AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, - VertexOp v_op, - T init) -{ - T ret{}; - - auto vertex_first = graph_view.get_local_vertex_first(); - auto vertex_last = graph_view.get_local_vertex_last(); - for (size_t i = 0; i < graph_view.get_number_of_local_adj_matrix_partitions(); ++i) { - auto row_first = graph_view.get_local_adj_matrix_partition_row_first(i); - auto row_last = graph_view.get_local_adj_matrix_partition_row_last(i); - - auto range_first = std::max(vertex_first, row_first); - auto range_last = std::min(vertex_last, row_last); - - if (range_last > range_first) { - matrix_partition_device_t matrix_partition(graph_view, i); - auto row_value_input_offset = GraphViewType::is_adj_matrix_transposed - ? 0 - : matrix_partition.get_major_value_start_offset(); - - auto input_first = thrust::make_zip_iterator(thrust::make_tuple( - vertex_value_input_first + (range_first - vertex_first), - adj_matrix_row_value_input_first + row_value_input_offset + (range_first - row_first))); - auto v_op_wrapper = [v_op] __device__(auto v_and_row_val) { - return v_op(thrust::get<0>(v_and_row_val), thrust::get<1>(v_and_row_val)); - }; - ret += - thrust::transform_reduce(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - input_first, - input_first + (range_last - range_first), - v_op_wrapper, - T{}, - thrust::plus()); - } - } - - if (GraphViewType::is_multi_gpu) { - ret = host_scalar_allreduce(handle.get_comms(), ret, handle.get_stream()); - } - - return init + ret; -} - -} // namespace experimental -} // namespace cugraph diff --git a/cpp/include/patterns/update_frontier_v_push_if_out_nbr.cuh b/cpp/include/patterns/update_frontier_v_push_if_out_nbr.cuh deleted file mode 100644 index a2250482c68..00000000000 --- a/cpp/include/patterns/update_frontier_v_push_if_out_nbr.cuh +++ /dev/null @@ -1,706 +0,0 @@ -/* - * Copyright (c) 2020, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -namespace cugraph { -namespace experimental { - -namespace detail { - -// FIXME: block size requires tuning -int32_t constexpr update_frontier_v_push_if_out_nbr_for_all_block_size = 128; -int32_t constexpr update_frontier_v_push_if_out_nbr_update_block_size = 128; - -template -__global__ void for_all_frontier_row_for_all_nbr_low_degree( - matrix_partition_device_t matrix_partition, - RowIterator row_first, - RowIterator row_last, - AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, - AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, - BufferKeyOutputIterator buffer_key_output_first, - BufferPayloadOutputIterator buffer_payload_output_first, - size_t* buffer_idx_ptr, - EdgeOp e_op) -{ - using vertex_t = typename GraphViewType::vertex_type; - using edge_t = typename GraphViewType::edge_type; - using weight_t = typename GraphViewType::weight_type; - - static_assert(!GraphViewType::is_adj_matrix_transposed, - "GraphViewType should support the push model."); - - auto num_rows = static_cast(thrust::distance(row_first, row_last)); - auto const tid = threadIdx.x + blockIdx.x * blockDim.x; - size_t idx = tid; - - while (idx < num_rows) { - vertex_t row = *(row_first + idx); - auto row_offset = matrix_partition.get_major_offset_from_major_nocheck(row); - vertex_t const* indices{nullptr}; - weight_t const* weights{nullptr}; - edge_t local_out_degree{}; - thrust::tie(indices, weights, local_out_degree) = matrix_partition.get_local_edges(row_offset); - for (vertex_t i = 0; i < local_out_degree; ++i) { - auto col = indices[i]; - auto weight = weights != nullptr ? weights[i] : 1.0; - auto col_offset = matrix_partition.get_minor_offset_from_minor_nocheck(col); - auto e_op_result = evaluate_edge_op() - .compute(row, - col, - weight, - *(adj_matrix_row_value_input_first + row_offset), - *(adj_matrix_col_value_input_first + col_offset), - e_op); - if (thrust::get<0>(e_op_result) == true) { - // FIXME: This atomicAdd serializes execution. If we renumber vertices to insure that rows - // within a partition are sorted by their out-degree in decreasing order, we can compute - // a tight uppper bound for the maximum number of pushes per warp/block and use shared - // memory buffer to reduce the number of atomicAdd operations. - static_assert(sizeof(unsigned long long int) == sizeof(size_t)); - auto buffer_idx = atomicAdd(reinterpret_cast(buffer_idx_ptr), - static_cast(1)); - *(buffer_key_output_first + buffer_idx) = col; - *(buffer_payload_output_first + buffer_idx) = - remove_first_thrust_tuple_element()(e_op_result); - } - } - - idx += gridDim.x * blockDim.x; - } -} - -template -size_t reduce_buffer_elements(raft::handle_t const& handle, - BufferKeyOutputIterator buffer_key_output_first, - BufferPayloadOutputIterator buffer_payload_output_first, - size_t num_buffer_elements, - ReduceOp reduce_op) -{ - thrust::sort_by_key(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - buffer_key_output_first, - buffer_key_output_first + num_buffer_elements, - buffer_payload_output_first); - - if (std::is_same>::value) { - // FIXME: if ReducOp is any, we may have a cheaper alternative than sort & uique (i.e. discard - // non-first elements) - auto it = thrust::unique_by_key(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - buffer_key_output_first, - buffer_key_output_first + num_buffer_elements, - buffer_payload_output_first); - return static_cast(thrust::distance(buffer_key_output_first, thrust::get<0>(it))); - } else { - using key_t = typename std::iterator_traits::value_type; - using payload_t = typename std::iterator_traits::value_type; - // FIXME: better avoid temporary buffer or at least limit the maximum buffer size (if we adopt - // CUDA cooperative group https://devblogs.nvidia.com/cooperative-groups and global sync(), we - // can use aggregate shared memory as a temporary buffer, or we can limit the buffer size, and - // split one thrust::reduce_by_key call to multiple thrust::reduce_by_key calls if the - // temporary buffer size exceeds the maximum buffer size (may be definied as percentage of the - // system HBM size or a function of the maximum number of threads in the system)) - // FIXME: actually, we can find how many unique keys are here by now. - // FIXME: if GraphViewType::is_multi_gpu is true, this should be executed on the GPU holding the - // vertex unless reduce_op is a pure function. - rmm::device_vector keys(num_buffer_elements); - rmm::device_vector values(num_buffer_elements); - auto it = thrust::reduce_by_key(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - buffer_key_output_first, - buffer_key_output_first + num_buffer_elements, - buffer_payload_output_first, - keys.begin(), - values.begin(), - thrust::equal_to(), - reduce_op); - auto num_reduced_buffer_elements = - static_cast(thrust::distance(keys.begin(), thrust::get<0>(it))); - thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - keys.begin(), - keys.begin() + num_reduced_buffer_elements, - buffer_key_output_first); - thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - values.begin(), - values.begin() + num_reduced_buffer_elements, - buffer_payload_output_first); - CUDA_TRY(cudaStreamSynchronize( - handle.get_stream())); // this is necessary as kyes & values will become out-of-scope once - // this function returns - return num_reduced_buffer_elements; - } -} - -template -__global__ void update_frontier_and_vertex_output_values( - vertex_partition_device_t vertex_partition, - BufferKeyInputIterator buffer_key_input_first, - BufferPayloadInputIterator buffer_payload_input_first, - size_t num_buffer_elements, - VertexValueInputIterator vertex_value_input_first, - VertexValueOutputIterator vertex_value_output_first, - vertex_t** bucket_ptrs, - size_t* bucket_sizes_ptr, - size_t invalid_bucket_idx, - vertex_t invalid_vertex, - VertexOp v_op) -{ - static_assert(std::is_same::value_type, - vertex_t>::value); - auto const tid = threadIdx.x + blockIdx.x * blockDim.x; - size_t idx = tid; - size_t block_idx = blockIdx.x; - // FIXME: it might be more performant to process more than one element per thread - auto num_blocks = (num_buffer_elements + blockDim.x - 1) / blockDim.x; - - using BlockScan = - cub::BlockScan; - __shared__ typename BlockScan::TempStorage temp_storage; - - __shared__ size_t bucket_block_start_offsets[num_buckets]; - - size_t bucket_block_local_offsets[num_buckets]; - size_t bucket_block_aggregate_sizes[num_buckets]; - - while (block_idx < num_blocks) { - for (size_t i = 0; i < num_buckets; ++i) { bucket_block_local_offsets[i] = 0; } - - size_t selected_bucket_idx{invalid_bucket_idx}; - vertex_t key{invalid_vertex}; - - if (idx < num_buffer_elements) { - key = *(buffer_key_input_first + idx); - auto key_offset = vertex_partition.get_local_vertex_offset_from_vertex_nocheck(key); - auto v_val = *(vertex_value_input_first + key_offset); - auto payload = *(buffer_payload_input_first + idx); - auto v_op_result = v_op(v_val, payload); - selected_bucket_idx = thrust::get<0>(v_op_result); - if (selected_bucket_idx != invalid_bucket_idx) { - *(vertex_value_output_first + key_offset) = - remove_first_thrust_tuple_element()(v_op_result); - bucket_block_local_offsets[selected_bucket_idx] = 1; - } - } - - for (size_t i = 0; i < num_buckets; ++i) { - BlockScan(temp_storage) - .ExclusiveSum(bucket_block_local_offsets[i], - bucket_block_local_offsets[i], - bucket_block_aggregate_sizes[i]); - } - - if (threadIdx.x == 0) { - for (size_t i = 0; i < num_buckets; ++i) { - static_assert(sizeof(unsigned long long int) == sizeof(size_t)); - bucket_block_start_offsets[i] = - atomicAdd(reinterpret_cast(bucket_sizes_ptr + i), - static_cast(bucket_block_aggregate_sizes[i])); - } - } - - __syncthreads(); - - // FIXME: better use shared memory buffer to aggreaget global memory writes - if (selected_bucket_idx != invalid_bucket_idx) { - bucket_ptrs[selected_bucket_idx][bucket_block_start_offsets[selected_bucket_idx] + - bucket_block_local_offsets[selected_bucket_idx]] = key; - } - - idx += gridDim.x * blockDim.x; - block_idx += gridDim.x; - } -} - -} // namespace detail - -/** - * @brief Update vertex frontier and vertex property values iterating over the outgoing edges. - * - * @tparam GraphViewType Type of the passed non-owning graph object. - * @tparam VertexIterator Type of the iterator for vertex identifiers. - * @tparam AdjMatrixRowValueInputIterator Type of the iterator for graph adjacency matrix row - * input properties. - * @tparam AdjMatrixColValueInputIterator Type of the iterator for graph adjacency matrix column - * input properties. - * @tparam EdgeOp Type of the quaternary (or quinary) edge operator. - * @tparam ReduceOp Type of the binary reduction operator. - * @tparam VertexValueInputIterator Type of the iterator for vertex properties. - * @tparam VertexValueOutputIterator Type of the iterator for vertex property variables. - * @tparam VertexFrontierType Type of the vertex frontier class which abstracts vertex frontier - * managements. - * @tparam VertexOp Type of the binary vertex operator. - * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and - * handles to various CUDA libraries) to run graph algorithms. - * @param graph_view Non-owning graph object. - * @param vertex_first Iterator pointing to the first (inclusive) vertex in the current frontier. v - * in [vertex_first, vertex_last) should be distinct (and should belong to this process in - * multi-GPU), otherwise undefined behavior - * @param vertex_last Iterator pointing to the last (exclusive) vertex in the current frontier. - * @param adj_matrix_row_value_input_first Iterator pointing to the adjacency matrix row input - * properties for the first (inclusive) row (assigned to this process in multi-GPU). - * `adj_matrix_row_value_input_last` (exclusive) is deduced as @p adj_matrix_row_value_input_first + - * @p graph_view.get_number_of_local_adj_matrix_partition_rows(). - * @param adj_matrix_col_value_input_first Iterator pointing to the adjacency matrix column input - * properties for the first (inclusive) column (assigned to this process in multi-GPU). - * `adj_matrix_col_value_output_last` (exclusive) is deduced as @p adj_matrix_col_value_output_first - * + @p graph_view.get_number_of_local_adj_matrix_partition_cols(). - * @param e_op Quaternary (or quinary) operator takes edge source, edge destination, (optional edge - * weight), *(@p adj_matrix_row_value_input_first + i), and *(@p adj_matrix_col_value_input_first + - * j) (where i is in [0, graph_view.get_number_of_local_adj_matrix_partition_rows()) and j is in [0, - * get_number_of_local_adj_matrix_partition_cols())) and returns a value to reduced by the @p - * reduce_op. - * @param reduce_op Binary operator takes two input arguments and reduce the two variables to one. - * @param vertex_value_input_first Iterator pointing to the vertex properties for the first - * (inclusive) vertex (assigned to this process in multi-GPU). `vertex_value_input_last` (exclusive) - * is deduced as @p vertex_value_input_first + @p graph_view.get_number_of_local_vertices(). - * @param vertex_value_output_first Iterator pointing to the vertex property variables for the first - * (inclusive) vertex (assigned to tihs process in multi-GPU). `vertex_value_output_last` - * (exclusive) is deduced as @p vertex_value_output_first + @p - * graph_view.get_number_of_local_vertices(). - * @param vertex_frontier vertex frontier class object for vertex frontier managements. This object - * includes multiple bucket objects. - * @param v_op Binary operator takes *(@p vertex_value_input_first + i) (where i is [0, @p - * graph_view.get_number_of_local_vertices())) and reduced value of the @p e_op outputs for - * this vertex and returns the target bucket index (for frontier update) and new verrtex property - * values (to update *(@p vertex_value_output_first + i)). - */ -template -void update_frontier_v_push_if_out_nbr( - raft::handle_t const& handle, - GraphViewType const& graph_view, - VertexIterator vertex_first, - VertexIterator vertex_last, - AdjMatrixRowValueInputIterator adj_matrix_row_value_input_first, - AdjMatrixColValueInputIterator adj_matrix_col_value_input_first, - EdgeOp e_op, - ReduceOp reduce_op, - VertexValueInputIterator vertex_value_input_first, - VertexValueOutputIterator vertex_value_output_first, - VertexFrontierType& vertex_frontier, - VertexOp v_op) -{ - static_assert(!GraphViewType::is_adj_matrix_transposed, - "GraphViewType should support the push model."); - - using vertex_t = typename GraphViewType::vertex_type; - using edge_t = typename GraphViewType::edge_type; - - // 1. fill the buffer - - vertex_frontier.set_buffer_idx_value(0); - - auto loop_count = size_t{1}; - if (GraphViewType::is_multi_gpu) { - auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); - auto const row_comm_size = row_comm.get_size(); - loop_count = graph_view.is_hypergraph_partitioned() - ? graph_view.get_number_of_local_adj_matrix_partitions() - : static_cast(row_comm_size); - } - - for (size_t i = 0; i < loop_count; ++i) { - matrix_partition_device_t matrix_partition( - graph_view, (GraphViewType::is_multi_gpu && !graph_view.is_hypergraph_partitioned()) ? 0 : i); - - rmm::device_uvector frontier_rows( - 0, handle.get_stream()); // relevant only if GraphViewType::is_multi_gpu is true - - size_t frontier_size{}; - if (GraphViewType::is_multi_gpu) { - auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); - auto const row_comm_rank = row_comm.get_rank(); - auto const row_comm_size = row_comm.get_size(); - auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); - auto const col_comm_rank = col_comm.get_rank(); - - auto sub_comm_rank = graph_view.is_hypergraph_partitioned() ? col_comm_rank : row_comm_rank; - frontier_size = host_scalar_bcast( - graph_view.is_hypergraph_partitioned() ? col_comm : row_comm, - (static_cast(sub_comm_rank) == i) ? thrust::distance(vertex_first, vertex_last) - : size_t{0}, - i, - handle.get_stream()); - if (static_cast(sub_comm_rank) != i) { - frontier_rows.resize(frontier_size, handle.get_stream()); - } - device_bcast(graph_view.is_hypergraph_partitioned() ? col_comm : row_comm, - vertex_first, - frontier_rows.begin(), - frontier_size, - i, - handle.get_stream()); - } else { - frontier_size = thrust::distance(vertex_first, vertex_last); - } - - edge_t max_pushes = - frontier_size > 0 - ? frontier_rows.size() > 0 - ? thrust::transform_reduce( - rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - frontier_rows.begin(), - frontier_rows.end(), - [matrix_partition] __device__(auto row) { - auto row_offset = matrix_partition.get_major_offset_from_major_nocheck(row); - return matrix_partition.get_local_degree(row_offset); - }, - edge_t{0}, - thrust::plus()) - : thrust::transform_reduce( - rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - vertex_first, - vertex_last, - [matrix_partition] __device__(auto row) { - auto row_offset = matrix_partition.get_major_offset_from_major_nocheck(row); - return matrix_partition.get_local_degree(row_offset); - }, - edge_t{0}, - thrust::plus()) - : edge_t{0}; - - // FIXME: This is highly pessimistic for single GPU (and multi-GPU as well if we maintain - // additional per column data for filtering in e_op). If we can pause & resume execution if - // buffer needs to be increased (and if we reserve address space to avoid expensive - // reallocation; - // https://devblogs.nvidia.com/introducing-low-level-gpu-virtual-memory-management/), we can - // start with a smaller buffer size (especially when the frontier size is large). - // for special cases when we can assure that there is no more than one push per destination - // (e.g. if cugraph::experimental::reduce_op::any is used), we can limit the buffer size to - // std::min(max_pushes, matrix_partition.get_minor_size()). - // For Volta+, we can limit the buffer size to std::min(max_pushes, - // matrix_partition.get_minor_size()) if the reduction operation is a pure function if we use - // locking. - // FIXME: if i != 0, this will require costly reallocation if we don't use the new CUDA feature - // to reserve address space. - vertex_frontier.resize_buffer(vertex_frontier.get_buffer_idx_value() + max_pushes); - auto buffer_first = vertex_frontier.buffer_begin(); - auto buffer_key_first = std::get<0>(buffer_first); - auto buffer_payload_first = std::get<1>(buffer_first); - - auto row_value_input_offset = GraphViewType::is_adj_matrix_transposed - ? vertex_t{0} - : matrix_partition.get_major_value_start_offset(); - - // FIXME: This is highly inefficeint for graphs with high-degree vertices. If we renumber - // vertices to insure that rows within a partition are sorted by their out-degree in decreasing - // order, we will apply this kernel only to low out-degree vertices. - if (frontier_size > 0) { - raft::grid_1d_thread_t for_all_low_degree_grid( - frontier_size, - detail::update_frontier_v_push_if_out_nbr_for_all_block_size, - handle.get_device_properties().maxGridSize[0]); - - if (frontier_rows.size() > 0) { - detail::for_all_frontier_row_for_all_nbr_low_degree<<>>( - matrix_partition, - frontier_rows.begin(), - frontier_rows.end(), - adj_matrix_row_value_input_first + row_value_input_offset, - adj_matrix_col_value_input_first, - buffer_key_first, - buffer_payload_first, - vertex_frontier.get_buffer_idx_ptr(), - e_op); - } else { - detail::for_all_frontier_row_for_all_nbr_low_degree<<>>( - matrix_partition, - vertex_first, - vertex_last, - adj_matrix_row_value_input_first + row_value_input_offset, - adj_matrix_col_value_input_first, - buffer_key_first, - buffer_payload_first, - vertex_frontier.get_buffer_idx_ptr(), - e_op); - } - } - } - - // 2. reduce the buffer - - auto num_buffer_offset = edge_t{0}; - - auto buffer_first = vertex_frontier.buffer_begin(); - auto buffer_key_first = std::get<0>(buffer_first) + num_buffer_offset; - auto buffer_payload_first = std::get<1>(buffer_first) + num_buffer_offset; - - auto num_buffer_elements = detail::reduce_buffer_elements(handle, - buffer_key_first, - buffer_payload_first, - vertex_frontier.get_buffer_idx_value(), - reduce_op); - - if (GraphViewType::is_multi_gpu) { - auto& comm = handle.get_comms(); - auto const comm_rank = comm.get_rank(); - auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); - auto const row_comm_rank = row_comm.get_rank(); - auto const row_comm_size = row_comm.get_size(); - auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); - auto const col_comm_rank = col_comm.get_rank(); - auto const col_comm_size = col_comm.get_size(); - - std::vector h_vertex_lasts(graph_view.is_hypergraph_partitioned() ? row_comm_size - : col_comm_size); - for (size_t i = 0; i < h_vertex_lasts.size(); ++i) { - h_vertex_lasts[i] = graph_view.get_vertex_partition_last( - graph_view.is_hypergraph_partitioned() ? col_comm_rank * row_comm_size + i - : row_comm_rank * col_comm_size + i); - } - - rmm::device_uvector d_vertex_lasts(h_vertex_lasts.size(), handle.get_stream()); - raft::update_device( - d_vertex_lasts.data(), h_vertex_lasts.data(), h_vertex_lasts.size(), handle.get_stream()); - rmm::device_uvector d_tx_buffer_last_boundaries(d_vertex_lasts.size(), - handle.get_stream()); - thrust::lower_bound(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - buffer_key_first, - buffer_key_first + num_buffer_elements, - d_vertex_lasts.begin(), - d_vertex_lasts.end(), - d_tx_buffer_last_boundaries.begin()); - std::vector h_tx_buffer_last_boundaries(d_tx_buffer_last_boundaries.size()); - raft::update_host(h_tx_buffer_last_boundaries.data(), - d_tx_buffer_last_boundaries.data(), - d_tx_buffer_last_boundaries.size(), - handle.get_stream()); - CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); - std::vector tx_counts(h_tx_buffer_last_boundaries.size()); - std::adjacent_difference( - h_tx_buffer_last_boundaries.begin(), h_tx_buffer_last_boundaries.end(), tx_counts.begin()); - - std::vector rx_counts(graph_view.is_hypergraph_partitioned() ? row_comm_size - : col_comm_size); - std::vector count_requests(tx_counts.size() + rx_counts.size()); - size_t tx_self_i = std::numeric_limits::max(); - for (size_t i = 0; i < tx_counts.size(); ++i) { - auto comm_dst_rank = graph_view.is_hypergraph_partitioned() - ? col_comm_rank * row_comm_size + static_cast(i) - : row_comm_rank * col_comm_size + static_cast(i); - if (comm_dst_rank == comm_rank) { - tx_self_i = i; - // FIXME: better define request_null (similar to MPI_REQUEST_NULL) under raft::comms - count_requests[i] = std::numeric_limits::max(); - } else { - comm.isend(&tx_counts[i], 1, comm_dst_rank, 0 /* tag */, count_requests.data() + i); - } - } - for (size_t i = 0; i < rx_counts.size(); ++i) { - auto comm_src_rank = graph_view.is_hypergraph_partitioned() - ? col_comm_rank * row_comm_size + static_cast(i) - : static_cast(i) * row_comm_size + comm_rank / col_comm_size; - if (comm_src_rank == comm_rank) { - assert(tx_self_i != std::numeric_limits::max()); - rx_counts[i] = tx_counts[tx_self_i]; - // FIXME: better define request_null (similar to MPI_REQUEST_NULL) under raft::comms - count_requests[tx_counts.size() + i] = std::numeric_limits::max(); - } else { - comm.irecv(&rx_counts[i], - 1, - comm_src_rank, - 0 /* tag */, - count_requests.data() + tx_counts.size() + i); - } - } - // FIXME: better define request_null (similar to MPI_REQUEST_NULL) under raft::comms, if - // raft::comms::wait immediately returns on seeing request_null, this remove is unnecessary - count_requests.erase(std::remove(count_requests.begin(), - count_requests.end(), - std::numeric_limits::max()), - count_requests.end()); - comm.waitall(count_requests.size(), count_requests.data()); - - std::vector tx_offsets(tx_counts.size() + 1, edge_t{0}); - std::partial_sum(tx_counts.begin(), tx_counts.end(), tx_offsets.begin() + 1); - std::vector rx_offsets(rx_counts.size() + 1, edge_t{0}); - std::partial_sum(rx_counts.begin(), rx_counts.end(), rx_offsets.begin() + 1); - - // FIXME: this will require costly reallocation if we don't use the new CUDA feature to reserve - // address space. - // FIXME: std::max(actual size, 1) as ncclRecv currently hangs if recvuff is nullptr even if - // count is 0 - vertex_frontier.resize_buffer(std::max(num_buffer_elements + rx_offsets.back(), size_t(1))); - - auto buffer_first = vertex_frontier.buffer_begin(); - auto buffer_key_first = std::get<0>(buffer_first) + num_buffer_offset; - auto buffer_payload_first = std::get<1>(buffer_first) + num_buffer_offset; - - std::vector tx_dst_ranks(tx_counts.size()); - std::vector rx_src_ranks(rx_counts.size()); - for (size_t i = 0; i < tx_dst_ranks.size(); ++i) { - tx_dst_ranks[i] = graph_view.is_hypergraph_partitioned() - ? col_comm_rank * row_comm_size + static_cast(i) - : row_comm_rank * col_comm_size + static_cast(i); - } - for (size_t i = 0; i < rx_src_ranks.size(); ++i) { - rx_src_ranks[i] = graph_view.is_hypergraph_partitioned() - ? col_comm_rank * row_comm_size + static_cast(i) - : static_cast(i) * row_comm_size + comm_rank / col_comm_size; - } - - device_multicast_sendrecv( - comm, - buffer_key_first, - tx_counts, - tx_offsets, - tx_dst_ranks, - buffer_key_first + num_buffer_elements, - rx_counts, - rx_offsets, - rx_src_ranks, - handle.get_stream()); - device_multicast_sendrecv( - comm, - buffer_payload_first, - tx_counts, - tx_offsets, - tx_dst_ranks, - buffer_payload_first + num_buffer_elements, - rx_counts, - rx_offsets, - rx_src_ranks, - handle.get_stream()); - - // FIXME: this does not exploit the fact that each segment is sorted. Lost performance - // optimization opportunities. - // FIXME: we can use [vertex_frontier.buffer_begin(), vertex_frontier.buffer_begin() + - // num_buffer_elements) as temporary buffer inside reduce_buffer_elements(). - num_buffer_offset = num_buffer_elements; - num_buffer_elements = detail::reduce_buffer_elements(handle, - buffer_key_first + num_buffer_elements, - buffer_payload_first + num_buffer_elements, - rx_offsets.back(), - reduce_op); - } - - // 3. update vertex properties - - if (num_buffer_elements > 0) { - auto buffer_first = vertex_frontier.buffer_begin(); - auto buffer_key_first = std::get<0>(buffer_first) + num_buffer_offset; - auto buffer_payload_first = std::get<1>(buffer_first) + num_buffer_offset; - - raft::grid_1d_thread_t update_grid(num_buffer_elements, - detail::update_frontier_v_push_if_out_nbr_update_block_size, - handle.get_device_properties().maxGridSize[0]); - - auto constexpr invalid_vertex = invalid_vertex_id::value; - - vertex_partition_device_t vertex_partition(graph_view); - - auto bucket_and_bucket_size_device_ptrs = - vertex_frontier.get_bucket_and_bucket_size_device_pointers(); - detail::update_frontier_and_vertex_output_values - <<>>( - vertex_partition, - buffer_key_first, - buffer_payload_first, - num_buffer_elements, - vertex_value_input_first, - vertex_value_output_first, - std::get<0>(bucket_and_bucket_size_device_ptrs).get(), - std::get<1>(bucket_and_bucket_size_device_ptrs).get(), - VertexFrontierType::kInvalidBucketIdx, - invalid_vertex, - v_op); - - auto bucket_sizes_device_ptr = std::get<1>(bucket_and_bucket_size_device_ptrs); - thrust::host_vector bucket_sizes( - bucket_sizes_device_ptr, bucket_sizes_device_ptr + VertexFrontierType::kNumBuckets); - for (size_t i = 0; i < VertexFrontierType::kNumBuckets; ++i) { - vertex_frontier.get_bucket(i).set_size(bucket_sizes[i]); - } - } -} - -/* - -FIXME: - -iterating over lower triangular (or upper triangular) : triangle counting -LRB might be necessary if the cost of processing an edge (i, j) is a function of degree(i) and -degree(j) : triangle counting -push-pull switching support (e.g. DOBFS), in this case, we need both -CSR & CSC (trade-off execution time vs memory requirement, unless graph is symmetric) -if graph is symmetric, there will be additional optimization opportunities (e.g. in-degree == -out-degree) For BFS, sending a bit vector (for the entire set of dest vertices per partitoin may -work better we can use thrust::set_intersection for triangle counting think about adding thrust -wrappers for reduction functions. Can I pass nullptr for dummy -instead of thrust::make_counting_iterator(0)? -*/ - -} // namespace experimental -} // namespace cugraph diff --git a/cpp/include/patterns/vertex_frontier.cuh b/cpp/include/patterns/vertex_frontier.cuh deleted file mode 100644 index ccb9e1a5a0d..00000000000 --- a/cpp/include/patterns/vertex_frontier.cuh +++ /dev/null @@ -1,384 +0,0 @@ -/* - * Copyright (c) 2020, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#pragma once - -#include -#include -#include - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include -#include -#include -#include - -namespace cugraph { -namespace experimental { - -namespace detail { - -// FIXME: block size requires tuning -int32_t constexpr move_and_invalidate_if_block_size = 128; - -// FIXME: better move to another file for reusability -inline size_t round_up(size_t number_to_round, size_t modulus) -{ - return ((number_to_round + (modulus - 1)) / modulus) * modulus; -} - -template -auto make_buffer_zip_iterator_impl(std::vector& buffer_ptrs, - size_t offset, - std::index_sequence) -{ - auto key_ptr = reinterpret_cast(buffer_ptrs[0]) + offset; - auto payload_it = thrust::make_zip_iterator( - thrust::make_tuple(reinterpret_cast::type*>( - buffer_ptrs[1 + Is])...)); - return std::make_tuple(key_ptr, payload_it); -} - -template -auto make_buffer_zip_iterator(std::vector& buffer_ptrs, size_t offset) -{ - size_t constexpr tuple_size = thrust::tuple_size::value; - return make_buffer_zip_iterator_impl( - buffer_ptrs, offset, std::make_index_sequence()); -} - -template -__global__ void move_and_invalidate_if(RowIterator row_first, - RowIterator row_last, - vertex_t** bucket_ptrs, - size_t* bucket_sizes_ptr, - size_t this_bucket_idx, - size_t invalid_bucket_idx, - vertex_t invalid_vertex, - SplitOp split_op) -{ - static_assert( - std::is_same::value_type, vertex_t>::value); - auto const tid = threadIdx.x + blockIdx.x * blockDim.x; - size_t idx = tid; - size_t block_idx = blockIdx.x; - auto num_elements = thrust::distance(row_first, row_last); - // FIXME: it might be more performant to process more than one element per thread - auto num_blocks = (num_elements + blockDim.x - 1) / blockDim.x; - - using BlockScan = cub::BlockScan; - __shared__ typename BlockScan::TempStorage temp_storage; - - __shared__ size_t bucket_block_start_offsets[num_buckets]; - - size_t bucket_block_local_offsets[num_buckets]; - size_t bucket_block_aggregate_sizes[num_buckets]; - - while (block_idx < num_blocks) { - for (size_t i = 0; i < num_buckets; ++i) { bucket_block_local_offsets[i] = 0; } - - size_t selected_bucket_idx{invalid_bucket_idx}; - vertex_t key{invalid_vertex}; - - if (idx < num_elements) { - key = *(row_first + idx); - selected_bucket_idx = split_op(key); - if (selected_bucket_idx != this_bucket_idx) { - *(row_first + idx) = invalid_vertex; - if (selected_bucket_idx != invalid_bucket_idx) { - bucket_block_local_offsets[selected_bucket_idx] = 1; - } - } - } - - for (size_t i = 0; i < num_buckets; ++i) { - BlockScan(temp_storage) - .ExclusiveSum(bucket_block_local_offsets[i], - bucket_block_local_offsets[i], - bucket_block_aggregate_sizes[i]); - } - - if (threadIdx.x == 0) { - for (size_t i = 0; i < num_buckets; ++i) { - static_assert(sizeof(unsigned long long int) == sizeof(size_t)); - bucket_block_start_offsets[i] = - atomicAdd(reinterpret_cast(bucket_sizes_ptr + i), - static_cast(bucket_block_aggregate_sizes[i])); - } - } - - __syncthreads(); - - // FIXME: better use shared memory buffer to aggreaget global memory writes - if ((selected_bucket_idx != this_bucket_idx) && (selected_bucket_idx != invalid_bucket_idx)) { - bucket_ptrs[selected_bucket_idx][bucket_block_start_offsets[selected_bucket_idx] + - bucket_block_local_offsets[selected_bucket_idx]] = key; - } - - idx += gridDim.x * blockDim.x; - block_idx += gridDim.x; - } -} - -} // namespace detail - -template -class Bucket { - public: - Bucket(raft::handle_t const& handle, size_t capacity) - : handle_ptr_(&handle), elements_(capacity, invalid_vertex_id::value) - { - } - - void insert(vertex_t v) - { - elements_[size_] = v; - ++size_; - } - - size_t size() const { return size_; } - - void set_size(size_t size) { size_ = size; } - - template - std::enable_if_t aggregate_size() const - { - return host_scalar_allreduce(handle_ptr_->get_comms(), size_, handle_ptr_->get_stream()); - } - - template - std::enable_if_t aggregate_size() const - { - return size_; - } - - void clear() { size_ = 0; } - - size_t capacity() const { return elements_.size(); } - - auto const data() const { return elements_.data().get(); } - - auto data() { return elements_.data().get(); } - - auto const begin() const { return elements_.begin(); } - - auto begin() { return elements_.begin(); } - - auto const end() const { return elements_.begin() + size_; } - - auto end() { return elements_.begin() + size_; } - - private: - raft::handle_t const* handle_ptr_{nullptr}; - rmm::device_vector elements_{}; - size_t size_{0}; -}; - -template -class VertexFrontier { - public: - static size_t constexpr kNumBuckets = num_buckets; - static size_t constexpr kInvalidBucketIdx{std::numeric_limits::max()}; - - VertexFrontier(raft::handle_t const& handle, std::vector bucket_capacities) - : handle_ptr_(&handle), - tmp_bucket_ptrs_(num_buckets, nullptr), - tmp_bucket_sizes_(num_buckets, 0), - buffer_ptrs_(kReduceInputTupleSize + 1 /* to store destination column number */, nullptr), - buffer_idx_(0, handle_ptr_->get_stream()) - { - CUGRAPH_EXPECTS(bucket_capacities.size() == num_buckets, - "invalid input argument bucket_capacities (size mismatch)"); - for (size_t i = 0; i < num_buckets; ++i) { - buckets_.emplace_back(handle, bucket_capacities[i]); - } - buffer_.set_stream(handle_ptr_->get_stream()); - } - - Bucket& get_bucket(size_t bucket_idx) { return buckets_[bucket_idx]; } - - Bucket const& get_bucket(size_t bucket_idx) const - { - return buckets_[bucket_idx]; - } - - void swap_buckets(size_t bucket_idx0, size_t bucket_idx1) - { - std::swap(buckets_[bucket_idx0], buckets_[bucket_idx1]); - } - - template - void split_bucket(size_t bucket_idx, SplitOp split_op) - { - auto constexpr invalid_vertex = invalid_vertex_id::value; - - auto bucket_and_bucket_size_device_ptrs = get_bucket_and_bucket_size_device_pointers(); - - auto& this_bucket = get_bucket(bucket_idx); - if (this_bucket.size() > 0) { - raft::grid_1d_thread_t move_and_invalidate_if_grid( - this_bucket.size(), - detail::move_and_invalidate_if_block_size, - handle_ptr_->get_device_properties().maxGridSize[0]); - - detail::move_and_invalidate_if - <<get_stream()>>>(this_bucket.begin(), - this_bucket.end(), - std::get<0>(bucket_and_bucket_size_device_ptrs).get(), - std::get<1>(bucket_and_bucket_size_device_ptrs).get(), - bucket_idx, - kInvalidBucketIdx, - invalid_vertex, - split_op); - } - - // FIXME: if we adopt CUDA cooperative group https://devblogs.nvidia.com/cooperative-groups - // and global sync(), we can merge this step with the above kernel (and rename the above kernel - // to move_if) - auto it = - thrust::remove_if(rmm::exec_policy(handle_ptr_->get_stream())->on(handle_ptr_->get_stream()), - get_bucket(bucket_idx).begin(), - get_bucket(bucket_idx).end(), - [] __device__(auto value) { return value == invalid_vertex; }); - - auto bucket_sizes_device_ptr = std::get<1>(bucket_and_bucket_size_device_ptrs); - thrust::host_vector bucket_sizes(bucket_sizes_device_ptr, - bucket_sizes_device_ptr + kNumBuckets); - for (size_t i = 0; i < kNumBuckets; ++i) { - if (i != bucket_idx) { get_bucket(i).set_size(bucket_sizes[i]); } - } - - auto size = thrust::distance(get_bucket(bucket_idx).begin(), it); - get_bucket(bucket_idx).set_size(size); - - return; - } - - auto get_bucket_and_bucket_size_device_pointers() - { - thrust::host_vector tmp_ptrs(buckets_.size(), nullptr); - thrust::host_vector tmp_sizes(buckets_.size(), 0); - for (size_t i = 0; i < buckets_.size(); ++i) { - tmp_ptrs[i] = get_bucket(i).data(); - tmp_sizes[i] = get_bucket(i).size(); - } - tmp_bucket_ptrs_ = tmp_ptrs; - tmp_bucket_sizes_ = tmp_sizes; - return std::make_tuple(tmp_bucket_ptrs_.data(), tmp_bucket_sizes_.data()); - } - - void resize_buffer(size_t size) - { - // FIXME: rmm::device_buffer resize incurs copy if memory is reallocated, which is unnecessary - // in this case. - buffer_.resize(compute_aggregate_buffer_size_in_bytes(size), handle_ptr_->get_stream()); - if (size > buffer_capacity_) { - buffer_capacity_ = size; - update_buffer_ptrs(); - } - buffer_size_ = size; - } - - void clear_buffer() { resize_buffer(0); } - - void shrink_to_fit_buffer() - { - if (buffer_size_ != buffer_capacity_) { - // FIXME: rmm::device_buffer shrink_to_fit incurs copy if memory is reallocated, which is - // unnecessary in this case. - buffer_.shrink_to_fit(handle_ptr_->get_stream()); - update_buffer_ptrs(); - buffer_capacity_ = buffer_size_; - } - } - - auto buffer_begin() - { - return detail::make_buffer_zip_iterator(buffer_ptrs_, 0); - } - - auto buffer_end() - { - return detail::make_buffer_zip_iterator(buffer_ptrs_, - buffer_size_); - } - - auto get_buffer_idx_ptr() { return buffer_idx_.data(); } - - size_t get_buffer_idx_value() { return buffer_idx_.value(handle_ptr_->get_stream()); } - - void set_buffer_idx_value(size_t value) - { - buffer_idx_.set_value(value, handle_ptr_->get_stream()); - } - - private: - static size_t constexpr kReduceInputTupleSize = thrust::tuple_size::value; - static size_t constexpr kBufferAlignment = 128; - - raft::handle_t const* handle_ptr_{nullptr}; - std::vector> buckets_{}; - rmm::device_vector tmp_bucket_ptrs_{}; - rmm::device_vector tmp_bucket_sizes_{}; - - std::array tuple_element_sizes_ = - compute_thrust_tuple_element_sizes()(); - std::vector buffer_ptrs_{}; - rmm::device_buffer buffer_{}; - size_t buffer_size_{0}; - size_t buffer_capacity_{0}; - rmm::device_scalar buffer_idx_{}; - - // FIXME: better pick between this apporach or the approach used in allocate_comm_buffer - size_t compute_aggregate_buffer_size_in_bytes(size_t size) - { - size_t aggregate_buffer_size_in_bytes = - detail::round_up(sizeof(vertex_t) * size, kBufferAlignment); - for (size_t i = 0; i < kReduceInputTupleSize; ++i) { - aggregate_buffer_size_in_bytes += - detail::round_up(tuple_element_sizes_[i] * size, kBufferAlignment); - } - return aggregate_buffer_size_in_bytes; - } - - void update_buffer_ptrs() - { - uintptr_t ptr = reinterpret_cast(buffer_.data()); - buffer_ptrs_[0] = reinterpret_cast(ptr); - ptr += detail::round_up(sizeof(vertex_t) * buffer_capacity_, kBufferAlignment); - for (size_t i = 0; i < kReduceInputTupleSize; ++i) { - buffer_ptrs_[1 + i] = reinterpret_cast(ptr); - ptr += detail::round_up(tuple_element_sizes_[i] * buffer_capacity_, kBufferAlignment); - } - } -}; - -} // namespace experimental -} // namespace cugraph diff --git a/cpp/include/utilities/cython.hpp b/cpp/include/utilities/cython.hpp deleted file mode 100644 index cd621a516ea..00000000000 --- a/cpp/include/utilities/cython.hpp +++ /dev/null @@ -1,263 +0,0 @@ -/* - * Copyright (c) 2020, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#pragma once - -#include -#include -#include - -namespace cugraph { -namespace cython { - -enum class numberTypeEnum : int { int32Type, int64Type, floatType, doubleType }; - -enum class graphTypeEnum : int { - // represents unintiialized or NULL ptr - null, - // represents some legacy Cxx type. This and other LegacyCxx values are not - // used for the unique_ptr in a graph_container_t, but instead for when this - // enum is used for determining high-level code paths to take to prevent - // needing to expose each legacy enum value to cython. - LegacyCSR, - LegacyCSC, - LegacyCOO, - // represents that a GraphCxxView* unique_ptr type is present in a - // graph_container_t. - GraphCSRViewFloat, - GraphCSRViewDouble, - GraphCSCViewFloat, - GraphCSCViewDouble, - GraphCOOViewFloat, - GraphCOOViewDouble, - // represents values present in the graph_container_t to construct a graph_t, - // but unlike legacy classes does not mean a graph_t unique_ptr is present in - // the container. - graph_t, -}; - -// "container" for a graph type instance which insulates the owner from the -// specifics of the actual graph type. This is intended to be used in Cython -// code that only needs to pass a graph object to another wrapped C++ API. This -// greatly simplifies the Cython code since the Cython definition only needs to -// define the container and not the various individual graph types in Cython. -struct graph_container_t { - // FIXME: This union is in place only to support legacy calls, remove when - // migration to graph_t types is complete, or when legacy graph objects are - // constructed in the call_< wrappers instead of the - // populate_graph_container_legacy() function. - union graphPtrUnion { - ~graphPtrUnion() {} - - void* null; - std::unique_ptr> GraphCSRViewFloatPtr; - std::unique_ptr> GraphCSRViewDoublePtr; - std::unique_ptr> GraphCSCViewFloatPtr; - std::unique_ptr> GraphCSCViewDoublePtr; - std::unique_ptr> GraphCOOViewFloatPtr; - std::unique_ptr> GraphCOOViewDoublePtr; - }; - - graph_container_t() : graph_ptr_union{nullptr}, graph_type{graphTypeEnum::null} {} - ~graph_container_t() {} - - // The expected usage of a graph_container_t is for it to be created as part - // of a cython wrapper simply for passing a templated instantiation of a - // particular graph class from one call to another, and not to exist outside - // of the individual wrapper function (deleted when the instance goes out of - // scope once the wrapper function returns). Therefore, copys and assignments - // to an instance are not supported and these methods are deleted. - graph_container_t(const graph_container_t&) = delete; - graph_container_t& operator=(const graph_container_t&) = delete; - - graphPtrUnion graph_ptr_union; - graphTypeEnum graph_type; - - // primitive data used for constructing graph_t instances. - void* src_vertices; - void* dst_vertices; - void* weights; - void* vertex_partition_offsets; - - size_t num_partition_edges; - size_t num_global_vertices; - size_t num_global_edges; - numberTypeEnum vertexType; - numberTypeEnum edgeType; - numberTypeEnum weightType; - bool transposed; - bool is_multi_gpu; - bool sorted_by_degree; - bool do_expensive_check; - bool hypergraph_partitioned; - int row_comm_size; - int col_comm_size; - int row_comm_rank; - int col_comm_rank; - experimental::graph_properties_t graph_props; -}; - -// FIXME: finish description for vertex_partition_offsets -// -// Factory function for populating an empty graph container with a new graph -// object from basic types, and sets the corresponding meta-data. Args are: -// -// graph_container_t& graph_container -// Reference to the graph_container_t instance to -// populate. populate_graph_container() can only be called on an "empty" -// container (ie. a container that has not been previously populated by -// populate_graph_container()) -// -// graphTypeEnum legacyType -// Specifies the type of graph when instantiating a legacy graph type -// (GraphCSRViewFloat, etc.). -// NOTE: this parameter will be removed when the transition to exclusinve use -// of the new 2D graph classes is complete. -// -// raft::handle_t const& handle -// Raft handle to be set on the new graph instance in the container -// -// void* src_vertices, dst_vertices, weights -// Pointer to an array of values representing source and destination vertices, -// and edge weights respectively. The value types of the array are specified -// using numberTypeEnum values separately (see below). offsets should be size -// num_vertices+1, indices should be size num_edges, weights should also be -// size num_edges -// -// void* vertex_partition_offsets -// Pointer to an array of vertexType values representing offsets into the -// individual partitions for a multi-GPU paritioned graph. The offsets are used for ... -// -// numberTypeEnum vertexType, edgeType, weightType -// numberTypeEnum enum value describing the data type for the vertices, -// offsets, and weights arrays respectively. These enum values are used to -// instantiate the proper templated graph type and for casting the arrays -// accordingly. -// -// int num_vertices, num_edges -// The number of vertices and edges respectively in the graph represented by -// the above arrays. -// -// bool transposed -// true if the resulting graph object should store a transposed adjacency -// matrix -// -// bool multi_gpu -// true if the resulting graph object is to be used for a multi-gpu -// application -void populate_graph_container(graph_container_t& graph_container, - raft::handle_t& handle, - void* src_vertices, - void* dst_vertices, - void* weights, - void* vertex_partition_offsets, - numberTypeEnum vertexType, - numberTypeEnum edgeType, - numberTypeEnum weightType, - size_t num_partition_edges, - size_t num_global_vertices, - size_t num_global_edges, - bool sorted_by_degree, - bool transposed, - bool multi_gpu); - -// FIXME: comment this function -// FIXME: Should local_* values be void* as well? -void populate_graph_container_legacy(graph_container_t& graph_container, - graphTypeEnum legacyType, - raft::handle_t const& handle, - void* offsets, - void* indices, - void* weights, - numberTypeEnum offsetType, - numberTypeEnum indexType, - numberTypeEnum weightType, - size_t num_global_vertices, - size_t num_global_edges, - int* local_vertices, - int* local_edges, - int* local_offsets); - -// Wrapper for calling Louvain using a graph container -template -std::pair call_louvain(raft::handle_t const& handle, - graph_container_t const& graph_container, - void* identifiers, - void* parts, - size_t max_level, - weight_t resolution); - -// Wrapper for calling Pagerank using a graph container -template -void call_pagerank(raft::handle_t const& handle, - graph_container_t const& graph_container, - vertex_t* identifiers, - weight_t* pagerank, - vertex_t personalization_subset_size, - vertex_t* personalization_subset, - weight_t* personalization_values, - double alpha, - double tolerance, - int64_t max_iter, - bool has_guess); - -// Wrapper for calling Katz centrality using a graph container -template -void call_katz_centrality(raft::handle_t const& handle, - graph_container_t const& graph_container, - vertex_t* identifiers, - weight_t* katz_centrality, - double alpha, - double beta, - double tolerance, - int64_t max_iter, - bool normalized, - bool has_guess); - -// Wrapper for calling BFS through a graph container -template -void call_bfs(raft::handle_t const& handle, - graph_container_t const& graph_container, - vertex_t* identifiers, - vertex_t* distances, - vertex_t* predecessors, - double* sp_counters, - const vertex_t start_vertex, - bool directed); - -// Wrapper for calling SSSP through a graph container -template -void call_sssp(raft::handle_t const& handle, - graph_container_t const& graph_container, - vertex_t* identifiers, - weight_t* distances, - vertex_t* predecessors, - const vertex_t source_vertex); - -// Helper for setting up subcommunicators, typically called as part of the -// user-initiated comms initialization in Python. -// -// raft::handle_t& handle -// Raft handle for which the new subcommunicators will be created. The -// subcommunicators will then be accessible from the handle passed to the -// parallel processes. -// -// size_t row_comm_size -// Number of items in a partition row (ie. pcols), needed for creating the -// appropriate number of subcommunicator instances. -void init_subcomms(raft::handle_t& handle, size_t row_comm_size); - -} // namespace cython -} // namespace cugraph diff --git a/cpp/scripts/run-clang-format.py b/cpp/scripts/run-clang-format.py index 9bd3c364329..02434278343 100644 --- a/cpp/scripts/run-clang-format.py +++ b/cpp/scripts/run-clang-format.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. @@ -22,7 +22,7 @@ import tempfile -EXPECTED_VERSION = "8.0.1" +EXPECTED_VERSION = "11.0.0" VERSION_REGEX = re.compile(r"clang-format version ([0-9.]+)") # NOTE: populate this list with more top-level dirs as we add more of them to the cugraph repo DEFAULT_DIRS = ["cpp/include", @@ -139,4 +139,3 @@ def main(): if __name__ == "__main__": main() - diff --git a/cpp/src/centrality/README.md b/cpp/src/centrality/README.md new file mode 100644 index 00000000000..31b5ed6720e --- /dev/null +++ b/cpp/src/centrality/README.md @@ -0,0 +1,81 @@ +# Centrality algorithms +cuGraph Pagerank is implemented using our graph primitive library + +## Pagerank + +The unit test code is the best place to search for examples on calling pagerank. + + * [SG Implementation](../../tests/experimental/pagerank_test.cpp) + * [MG Implementation](../../tests/pagerank/mg_pagerank_test.cpp) + +## Simple pagerank + +The example assumes that you create an SG or MG graph somehow. The caller must create the pageranks vector in device memory and pass in the raw pointer to that vector into the pagerank function. + +```cpp +#include +... +using vertex_t = int32_t; // or int64_t, whichever is appropriate +using weight_t = float; // or double, whichever is appropriate +using result_t = weight_t; // could specify float or double also +raft::handle_t handle; // Must be configured if MG +auto graph_view = graph.view(); // assumes you have created a graph somehow + +result_t constexpr alpha{0.85}; +result_t constexpr epsilon{1e-6}; + +rmm::device_uvector pageranks_v(graph_view.get_number_of_vertices(), handle.get_stream()); + +// pagerank optionally supports three additional parameters: +// max_iterations - maximum number of iterations, if pagerank doesn't coverge by +// then we abort +// has_initial_guess - if true, values in the pagerank array when the call is initiated +// will be used as the initial pagerank values. These values will +// be normalized before use. If false (the default), the values +// in the pagerank array will be set to 1/num_vertices before +// starting the computation. +// do_expensive_check - perform extensive validation of the input data before +// executing algorithm. Off by default. Note: turning this on +// is expensive +cugraph::experimental::pagerank(handle, graph_view, nullptr, nullptr, nullptr, vertex_t{0}, + pageranks_v.data(), alpha, epsilon); +``` + +## Personalized Pagerank + +The example assumes that you create an SG or MG graph somehow. The caller must create the pageranks vector in device memory and pass in the raw pointer to that vector into the pagerank function. Additionally, the caller must create personalization_vertices and personalized_values vectors in device memory, populate them and pass in the raw pointers to those vectors. + +```cpp +#include +... +using vertex_t = int32_t; // or int64_t, whichever is appropriate +using weight_t = float; // or double, whichever is appropriate +using result_t = weight_t; // could specify float or double also +raft::handle_t handle; // Must be configured if MG +auto graph_view = graph.view(); // assumes you have created a graph somehow +vertex_t number_of_personalization_vertices; // Provided by caller + +result_t constexpr alpha{0.85}; +result_t constexpr epsilon{1e-6}; + +rmm::device_uvector pageranks_v(graph_view.get_number_of_vertices(), handle.get_stream()); +rmm::device_uvector personalization_vertices(number_of_personalization_vertices, handle.get_stream()); +rmm::device_uvector personalization_values(number_of_personalization_vertices, handle.get_stream()); + +// Populate personalization_vertices, personalization_values with user provided data + +// pagerank optionally supports three additional parameters: +// max_iterations - maximum number of iterations, if pagerank doesn't coverge by +// then we abort +// has_initial_guess - if true, values in the pagerank array when the call is initiated +// will be used as the initial pagerank values. These values will +// be normalized before use. If false (the default), the values +// in the pagerank array will be set to 1/num_vertices before +// starting the computation. +// do_expensive_check - perform extensive validation of the input data before +// executing algorithm. Off by default. Note: turning this on +// is expensive +cugraph::experimental::pagerank(handle, graph_view, nullptr, personalization_vertices.data(), + personalization_values.data(), number_of_personalization_vertices, + pageranks_v.data(), alpha, epsilon); +``` diff --git a/cpp/src/centrality/betweenness_centrality.cu b/cpp/src/centrality/betweenness_centrality.cu index 8ff62f7ddb6..6949399b4b7 100644 --- a/cpp/src/centrality/betweenness_centrality.cu +++ b/cpp/src/centrality/betweenness_centrality.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -20,10 +20,12 @@ #include -#include -#include +#include +#include +#include + #include -#include +#include #include #include "betweenness_centrality.cuh" @@ -33,14 +35,14 @@ namespace cugraph { namespace detail { namespace { template -void betweenness_centrality_impl(raft::handle_t const &handle, - GraphCSRView const &graph, - result_t *result, +void betweenness_centrality_impl(raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + result_t* result, bool normalize, bool endpoints, - weight_t const *weight, + weight_t const* weight, vertex_t number_of_sources, - vertex_t const *sources, + vertex_t const* sources, vertex_t total_number_of_sources) { // Current Implementation relies on BFS @@ -57,13 +59,13 @@ void betweenness_centrality_impl(raft::handle_t const &handle, } template -void edge_betweenness_centrality_impl(raft::handle_t const &handle, - GraphCSRView const &graph, - result_t *result, +void edge_betweenness_centrality_impl(raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + result_t* result, bool normalize, - weight_t const *weight, + weight_t const* weight, vertex_t number_of_sources, - vertex_t const *sources, + vertex_t const* sources, vertex_t total_number_of_sources) { // Current Implementation relies on BFS @@ -82,7 +84,7 @@ void edge_betweenness_centrality_impl(raft::handle_t const &handle, // bc.rescale_by_total_sources_used(total_number_of_sources); } template -vertex_t get_total_number_of_sources(raft::handle_t const &handle, vertex_t local_number_of_sources) +vertex_t get_total_number_of_sources(raft::handle_t const& handle, vertex_t local_number_of_sources) { vertex_t total_number_of_sources_used = local_number_of_sources; if (handle.comms_initialized()) { @@ -101,13 +103,13 @@ vertex_t get_total_number_of_sources(raft::handle_t const &handle, vertex_t loca } // namespace template -void verify_betweenness_centrality_input(result_t *result, +void verify_betweenness_centrality_input(result_t* result, bool is_edge_betweenness, bool normalize, bool endpoints, - weight_t const *weights, + weight_t const* weights, vertex_t const number_of_sources, - vertex_t const *sources) + vertex_t const* sources) { static_assert(std::is_same::value, "vertex_t should be int"); static_assert(std::is_same::value, "edge_t should be int"); @@ -116,7 +118,7 @@ void verify_betweenness_centrality_input(result_t *result, static_assert(std::is_same::value || std::is_same::value, "result_t should be float or double"); - CUGRAPH_EXPECTS(result != nullptr, "Invalid API parameter: betwenness pointer is NULL"); + CUGRAPH_EXPECTS(result != nullptr, "Invalid input argument: betwenness pointer is NULL"); CUGRAPH_EXPECTS(number_of_sources >= 0, "Number of sources must be positive or equal to 0."); if (number_of_sources != 0) { CUGRAPH_EXPECTS(sources != nullptr, @@ -137,12 +139,12 @@ void BC::setup() } template -void BC::configure(result_t *betweenness, +void BC::configure(result_t* betweenness, bool is_edge_betweenness, bool normalized, bool endpoints, - weight_t const *weights, - vertex_t const *sources, + weight_t const* weights, + vertex_t const* sources, vertex_t number_of_sources) { // --- Bind betweenness output vector to internal --- @@ -227,15 +229,13 @@ void BC::compute_single_source(vertex_t so // the traversal, this value is avalaible within the bfs implementation and // there could be a way to access it directly and avoid both replace and the // max - thrust::replace(rmm::exec_policy(handle_.get_stream())->on(handle_.get_stream()), + thrust::replace(rmm::exec_policy(handle_.get_stream_view()), distances_, distances_ + number_of_vertices_, std::numeric_limits::max(), static_cast(-1)); - auto current_max_depth = - thrust::max_element(rmm::exec_policy(handle_.get_stream())->on(handle_.get_stream()), - distances_, - distances_ + number_of_vertices_); + auto current_max_depth = thrust::max_element( + rmm::exec_policy(handle_.get_stream_view()), distances_, distances_ + number_of_vertices_); vertex_t max_depth = 0; CUDA_TRY(cudaMemcpy(&max_depth, current_max_depth, sizeof(vertex_t), cudaMemcpyDeviceToHost)); // Step 2) Dependency accumulation @@ -265,7 +265,7 @@ void BC::accumulate(vertex_t source_vertex template void BC::initialize_dependencies() { - thrust::fill(rmm::exec_policy(handle_.get_stream())->on(handle_.get_stream()), + thrust::fill(rmm::exec_policy(handle_.get_stream_view()), deltas_, deltas_ + number_of_vertices_, static_cast(0)); @@ -315,16 +315,13 @@ template ::add_reached_endpoints_to_source_betweenness( vertex_t source_vertex) { - vertex_t number_of_unvisited_vertices = - thrust::count(rmm::exec_policy(handle_.get_stream())->on(handle_.get_stream()), - distances_, - distances_ + number_of_vertices_, - -1); + vertex_t number_of_unvisited_vertices = thrust::count( + rmm::exec_policy(handle_.get_stream_view()), distances_, distances_ + number_of_vertices_, -1); vertex_t number_of_visited_vertices_except_source = number_of_vertices_ - number_of_unvisited_vertices - 1; rmm::device_vector buffer(1); buffer[0] = number_of_visited_vertices_except_source; - thrust::transform(rmm::exec_policy(handle_.get_stream())->on(handle_.get_stream()), + thrust::transform(rmm::exec_policy(handle_.get_stream_view()), buffer.begin(), buffer.end(), betweenness_ + source_vertex, @@ -335,7 +332,7 @@ void BC::add_reached_endpoints_to_source_b template void BC::add_vertices_dependencies_to_betweenness() { - thrust::transform(rmm::exec_policy(handle_.get_stream())->on(handle_.get_stream()), + thrust::transform(rmm::exec_policy(handle_.get_stream_view()), deltas_, deltas_ + number_of_vertices_, betweenness_, @@ -420,7 +417,7 @@ void BC::apply_rescale_factor_to_betweenne { size_t result_size = number_of_vertices_; if (is_edge_betweenness_) result_size = number_of_edges_; - thrust::transform(rmm::exec_policy(handle_.get_stream())->on(handle_.get_stream()), + thrust::transform(rmm::exec_policy(handle_.get_stream_view()), betweenness_, betweenness_ + result_size, thrust::make_constant_iterator(rescale_factor), @@ -451,14 +448,14 @@ void BC::rescale_by_total_sources_used( } // namespace detail template -void betweenness_centrality(raft::handle_t const &handle, - GraphCSRView const &graph, - result_t *result, +void betweenness_centrality(raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + result_t* result, bool normalize, bool endpoints, - weight_t const *weight, + weight_t const* weight, vertex_t k, - vertex_t const *vertices) + vertex_t const* vertices) { vertex_t total_number_of_sources_used = detail::get_total_number_of_sources(handle, k); if (handle.comms_initialized()) { @@ -491,32 +488,33 @@ void betweenness_centrality(raft::handle_t const &handle, } } -template void betweenness_centrality(const raft::handle_t &, - GraphCSRView const &, - float *, - bool, - bool, - float const *, - int, - int const *); +template void betweenness_centrality( + const raft::handle_t&, + legacy::GraphCSRView const&, + float*, + bool, + bool, + float const*, + int, + int const*); template void betweenness_centrality( - const raft::handle_t &, - GraphCSRView const &, - double *, + const raft::handle_t&, + legacy::GraphCSRView const&, + double*, bool, bool, - double const *, + double const*, int, - int const *); + int const*); template -void edge_betweenness_centrality(raft::handle_t const &handle, - GraphCSRView const &graph, - result_t *result, +void edge_betweenness_centrality(raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + result_t* result, bool normalize, - weight_t const *weight, + weight_t const* weight, vertex_t k, - vertex_t const *vertices) + vertex_t const* vertices) { vertex_t total_number_of_sources_used = detail::get_total_number_of_sources(handle, k); if (handle.comms_initialized()) { @@ -542,20 +540,20 @@ void edge_betweenness_centrality(raft::handle_t const &handle, } template void edge_betweenness_centrality( - const raft::handle_t &, - GraphCSRView const &, - float *, + const raft::handle_t&, + legacy::GraphCSRView const&, + float*, bool, - float const *, + float const*, int, - int const *); + int const*); template void edge_betweenness_centrality( - raft::handle_t const &handle, - GraphCSRView const &, - double *, + raft::handle_t const& handle, + legacy::GraphCSRView const&, + double*, bool, - double const *, + double const*, int, - int const *); + int const*); } // namespace cugraph diff --git a/cpp/src/centrality/betweenness_centrality.cuh b/cpp/src/centrality/betweenness_centrality.cuh index 418ac06faa4..706b8bfebac 100644 --- a/cpp/src/centrality/betweenness_centrality.cuh +++ b/cpp/src/centrality/betweenness_centrality.cuh @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. @@ -22,69 +22,69 @@ namespace cugraph { namespace detail { template -void betweenness_centrality(raft::handle_t const &handle, - GraphCSRView const &graph, - result_t *result, +void betweenness_centrality(raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + result_t* result, bool normalize, bool endpoints, - weight_t const *weight, + weight_t const* weight, vertex_t const number_of_sources, - vertex_t const *sources); + vertex_t const* sources); template -void edge_betweenness_centrality(GraphCSRView const &graph, - result_t *result, +void edge_betweenness_centrality(legacy::GraphCSRView const& graph, + result_t* result, bool normalize, - weight_t const *weight, + weight_t const* weight, vertex_t const number_of_sources, - vertex_t const *sources); + vertex_t const* sources); template -void verify_betweenness_centrality_input(result_t *result, +void verify_betweenness_centrality_input(result_t* result, bool is_edge_betweenness, bool normalize, bool endpoints, - weight_t const *weights, + weight_t const* weights, vertex_t const number_of_sources, - vertex_t const *sources); + vertex_t const* sources); template class BC { public: virtual ~BC(void) {} - BC(raft::handle_t const &handle, - GraphCSRView const &graph, + BC(raft::handle_t const& handle, + legacy::GraphCSRView const& graph, cudaStream_t stream = 0) : handle_(handle), graph_(graph) { setup(); } - void configure(result_t *betweenness, + void configure(result_t* betweenness, bool is_edge_betweenness, bool normalize, bool endpoints, - weight_t const *weight, - vertex_t const *sources, + weight_t const* weight, + vertex_t const* sources, vertex_t const number_of_sources); - void configure_edge(result_t *betweenness, + void configure_edge(result_t* betweenness, bool normalize, - weight_t const *weight, - vertex_t const *sources, + weight_t const* weight, + vertex_t const* sources, vertex_t const number_of_sources); void compute(); void rescale_by_total_sources_used(vertex_t total_number_of_sources_used); private: // --- RAFT handle --- - raft::handle_t const &handle_; + raft::handle_t const& handle_; // --- Information concerning the graph --- - const GraphCSRView &graph_; + const legacy::GraphCSRView& graph_; // --- These information are extracted on setup --- vertex_t number_of_vertices_; // Number of vertices in the graph vertex_t number_of_edges_; // Number of edges in the graph - edge_t const *offsets_ptr_; // Pointer to the offsets - vertex_t const *indices_ptr_; // Pointers to the indices + edge_t const* offsets_ptr_; // Pointer to the offsets + vertex_t const* indices_ptr_; // Pointers to the indices // --- Information from configuration --- bool configured_ = false; // Flag to ensure configuration was called @@ -92,14 +92,14 @@ class BC { bool is_edge_betweenness_ = false; // If True compute edge_betweeness // FIXME: For weighted version - weight_t const *edge_weights_ptr_ = nullptr; // Pointer to the weights + weight_t const* edge_weights_ptr_ = nullptr; // Pointer to the weights bool endpoints_ = false; // If True normalize the betweenness - vertex_t const *sources_ = nullptr; // Subset of vertices to gather information from + vertex_t const* sources_ = nullptr; // Subset of vertices to gather information from vertex_t number_of_sources_; // Number of vertices in sources // --- Output ---- // betweenness is set/read by users - using Vectors - result_t *betweenness_ = nullptr; + result_t* betweenness_ = nullptr; // --- Data required to perform computation ---- rmm::device_vector distances_vec_; @@ -107,13 +107,13 @@ class BC { rmm::device_vector sp_counters_vec_; rmm::device_vector deltas_vec_; - vertex_t *distances_ = + vertex_t* distances_ = nullptr; // array(|V|) stores the distances gathered by the latest SSSP - vertex_t *predecessors_ = + vertex_t* predecessors_ = nullptr; // array(|V|) stores the predecessors of the latest SSSP - double *sp_counters_ = + double* sp_counters_ = nullptr; // array(|V|) stores the shortest path counter for the latest SSSP - double *deltas_ = nullptr; // array(|V|) stores the dependencies for the latest SSSP + double* deltas_ = nullptr; // array(|V|) stores the dependencies for the latest SSSP int max_grid_dim_1D_ = 0; int max_block_dim_1D_ = 0; diff --git a/cpp/src/centrality/betweenness_centrality_kernels.cuh b/cpp/src/centrality/betweenness_centrality_kernels.cuh index 3cb5add8ad6..27666095375 100644 --- a/cpp/src/centrality/betweenness_centrality_kernels.cuh +++ b/cpp/src/centrality/betweenness_centrality_kernels.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -25,13 +25,13 @@ namespace detail { // Should look into forAllEdge type primitive for different // load balancing template -__global__ void edges_accumulation_kernel(result_t *betweenness, +__global__ void edges_accumulation_kernel(result_t* betweenness, vertex_t number_vertices, - vertex_t const *indices, - edge_t const *offsets, - vertex_t *distances, - double *sp_counters, - double *deltas, + vertex_t const* indices, + edge_t const* offsets, + vertex_t* distances, + double* sp_counters, + double* deltas, vertex_t depth) { for (int thread_idx = blockIdx.x * blockDim.x + threadIdx.x; thread_idx < number_vertices; @@ -58,13 +58,13 @@ __global__ void edges_accumulation_kernel(result_t *betweenness, } template -__global__ void endpoints_accumulation_kernel(result_t *betweenness, +__global__ void endpoints_accumulation_kernel(result_t* betweenness, vertex_t number_vertices, - vertex_t const *indices, - edge_t const *offsets, - vertex_t *distances, - double *sp_counters, - double *deltas, + vertex_t const* indices, + edge_t const* offsets, + vertex_t* distances, + double* sp_counters, + double* deltas, vertex_t depth) { for (int thread_idx = blockIdx.x * blockDim.x + threadIdx.x; thread_idx < number_vertices; @@ -88,13 +88,13 @@ __global__ void endpoints_accumulation_kernel(result_t *betweenness, } } template -__global__ void accumulation_kernel(result_t *betweenness, +__global__ void accumulation_kernel(result_t* betweenness, vertex_t number_vertices, - vertex_t const *indices, - edge_t const *offsets, - vertex_t *distances, - double *sp_counters, - double *deltas, + vertex_t const* indices, + edge_t const* offsets, + vertex_t* distances, + double* sp_counters, + double* deltas, vertex_t depth) { for (int thread_idx = blockIdx.x * blockDim.x + threadIdx.x; thread_idx < number_vertices; @@ -117,4 +117,4 @@ __global__ void accumulation_kernel(result_t *betweenness, } } } // namespace detail -} // namespace cugraph \ No newline at end of file +} // namespace cugraph diff --git a/cpp/src/centrality/katz_centrality.cu b/cpp/src/centrality/katz_centrality.cu index 0119a388680..320d76e5c03 100644 --- a/cpp/src/centrality/katz_centrality.cu +++ b/cpp/src/centrality/katz_centrality.cu @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. @@ -23,14 +23,14 @@ #include #include -#include -#include "utilities/error.hpp" +#include +#include namespace cugraph { template -void katz_centrality(GraphCSRView const &graph, - result_t *result, +void katz_centrality(legacy::GraphCSRView const& graph, + result_t* result, double alpha, int max_iter, double tol, @@ -52,6 +52,6 @@ void katz_centrality(GraphCSRView const &graph, } template void katz_centrality( - GraphCSRView const &, double *, double, int, double, bool, bool); + legacy::GraphCSRView const&, double*, double, int, double, bool, bool); } // namespace cugraph diff --git a/cpp/src/community/ECG.cu b/cpp/src/community/ECG.cu deleted file mode 100644 index ce7e9dd1ad2..00000000000 --- a/cpp/src/community/ECG.cu +++ /dev/null @@ -1,193 +0,0 @@ -/* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include -#include -#include -#include -#include -#include "utilities/graph_utils.cuh" - -namespace { -template -__device__ IndexType -binsearch_maxle(const IndexType *vec, const IndexType val, IndexType low, IndexType high) -{ - while (true) { - if (low == high) return low; // we know it exists - if ((low + 1) == high) return (vec[high] <= val) ? high : low; - - IndexType mid = low + (high - low) / 2; - - if (vec[mid] > val) - high = mid - 1; - else - low = mid; - } -} - -template -__global__ void match_check_kernel(IdxT size, - IdxT num_verts, - IdxT *offsets, - IdxT *indices, - IdxT *permutation, - IdxT *parts, - ValT *weights) -{ - IdxT tid = blockIdx.x * blockDim.x + threadIdx.x; - while (tid < size) { - IdxT source = binsearch_maxle(offsets, tid, (IdxT)0, num_verts); - IdxT dest = indices[tid]; - if (parts[permutation[source]] == parts[permutation[dest]]) weights[tid] += 1; - tid += gridDim.x * blockDim.x; - } -} - -struct prg { - __host__ __device__ float operator()(int n) - { - thrust::default_random_engine rng; - thrust::uniform_real_distribution dist(0.0, 1.0); - rng.discard(n); - return dist(rng); - } -}; - -template -struct update_functor { - ValT min_value; - ValT ensemble_size; - update_functor(ValT minv, ValT es) : min_value(minv), ensemble_size(es) {} - __host__ __device__ ValT operator()(ValT input) - { - return min_value + (1 - min_value) * (input / ensemble_size); - } -}; - -/** - * Computes a random permutation vector of length size. A permutation vector of length n - * contains all values [0..n-1] exactly once. - * @param size The length of the permutation vector to generate - * @param seed A seed value for the random number generator, the generator will discard this many - * values before using values. Calling this method with the same seed will result in the same - * permutation vector. - * @return A pointer to memory containing the requested permutation vector. The caller is - * responsible for freeing the allocated memory using ALLOC_FREE_TRY(). - */ -template -void get_permutation_vector(T size, T seed, T *permutation, cudaStream_t stream) -{ - rmm::device_vector randoms_v(size); - - thrust::counting_iterator index(seed); - thrust::transform( - rmm::exec_policy(stream)->on(stream), index, index + size, randoms_v.begin(), prg()); - thrust::sequence(rmm::exec_policy(stream)->on(stream), permutation, permutation + size, 0); - thrust::sort_by_key( - rmm::exec_policy(stream)->on(stream), randoms_v.begin(), randoms_v.end(), permutation); -} - -} // anonymous namespace - -namespace cugraph { - -template -void ecg(raft::handle_t const &handle, - GraphCSRView const &graph, - weight_t min_weight, - vertex_t ensemble_size, - vertex_t *clustering) -{ - CUGRAPH_EXPECTS(graph.edge_data != nullptr, "API error, louvain expects a weighted graph"); - CUGRAPH_EXPECTS(clustering != nullptr, "Invalid input argument: clustering is NULL"); - - cudaStream_t stream{0}; - - rmm::device_vector ecg_weights_v(graph.edge_data, - graph.edge_data + graph.number_of_edges); - - vertex_t size{graph.number_of_vertices}; - vertex_t seed{1}; - - auto permuted_graph = std::make_unique>( - size, graph.number_of_edges, graph.has_data()); - - // Iterate over each member of the ensemble - for (vertex_t i = 0; i < ensemble_size; i++) { - // Take random permutation of the graph - rmm::device_vector permutation_v(size); - vertex_t *d_permutation = permutation_v.data().get(); - - get_permutation_vector(size, seed, d_permutation, stream); - seed += size; - - detail::permute_graph(graph, d_permutation, permuted_graph->view()); - - // Run one level of Louvain clustering on the random permutation - rmm::device_vector parts_v(size); - vertex_t *d_parts = parts_v.data().get(); - - cugraph::louvain(handle, permuted_graph->view(), d_parts, size_t{1}); - - // For each edge in the graph determine whether the endpoints are in the same partition - // Keep a sum for each edge of the total number of times its endpoints are in the same partition - dim3 grid, block; - block.x = 512; - grid.x = min(vertex_t{CUDA_MAX_BLOCKS}, (graph.number_of_edges / 512 + 1)); - match_check_kernel<<>>(graph.number_of_edges, - graph.number_of_vertices, - graph.offsets, - graph.indices, - permutation_v.data().get(), - d_parts, - ecg_weights_v.data().get()); - } - - // Set weights = min_weight + (1 - min-weight)*sum/ensemble_size - update_functor uf(min_weight, ensemble_size); - thrust::transform(rmm::exec_policy(stream)->on(stream), - ecg_weights_v.data().get(), - ecg_weights_v.data().get() + graph.number_of_edges, - ecg_weights_v.data().get(), - uf); - - // Run Louvain on the original graph using the computed weights - // (pass max_level = 100 for a "full run") - GraphCSRView louvain_graph; - louvain_graph.indices = graph.indices; - louvain_graph.offsets = graph.offsets; - louvain_graph.edge_data = ecg_weights_v.data().get(); - louvain_graph.number_of_vertices = graph.number_of_vertices; - louvain_graph.number_of_edges = graph.number_of_edges; - - cugraph::louvain(handle, louvain_graph, clustering, size_t{100}); -} - -// Explicit template instantiations. -template void ecg(raft::handle_t const &, - GraphCSRView const &graph, - float min_weight, - int32_t ensemble_size, - int32_t *clustering); -template void ecg(raft::handle_t const &, - GraphCSRView const &graph, - double min_weight, - int32_t ensemble_size, - int32_t *clustering); -} // namespace cugraph diff --git a/cpp/src/community/README.md b/cpp/src/community/README.md new file mode 100644 index 00000000000..9d635a6167f --- /dev/null +++ b/cpp/src/community/README.md @@ -0,0 +1,79 @@ +# Louvain and Related Clustering Algorithms +cuGraph contains a GPU implementation of the Louvain algorithm and several related clustering algorithms (Leiden and ECG). + +## Louvain + +The Louvain implementation is designed to assign clusters attempting to optimize modularity. The algorithm is derived from the serial implementation described in the following paper: + + * VD Blondel, J-L Guillaume, R Lambiotte and E Lefebvre: Fast unfolding of community hierarchies in large networks, J Stat Mech P10008 (2008), http://arxiv.org/abs/0803.0476 + +It leverages some parallelism ideas from the following paper: + * Hao Lu, Mahantesh Halappanavar, Ananth Kalyanaraman: Parallel heuristics for scalable community detection, Elsevier Parallel Computing (2015), https://www.sciencedirect.com/science/article/pii/S0167819115000472 + + +The challenge in parallelizing Louvain lies in the primary loop which visits the vertices in serial. For each vertex v the change in modularity is computed for moving the vertex from its currently assigned cluster to each of the clusters to which v's neighbors are assigned. The largest positive delta modularity is used to select a new cluster (if there are no positive delta modularities then the vertex is not moved). If the vertex v is moved to a new cluster then the statistics of the vertex v's old cluster and new cluster change. This change in cluster statistics may affect the delta modularity computations of all vertices that follow vertex v in the serial iteration, creating a dependency between the different iterations of the loop. + +In order to make efficient use of the GPU parallelism, the cuGraph implementation computes the delta modularity for *all* vertex/neighbor pairs using the *current* vertex assignment. Decisions on moving vertices will be made based upon these delta modularities. This will potentially make choices that the serial version would not make. In order to minimize some of the negative effects of this (as described in the Lu paper), the cuGraph implementation uses an Up/Down technique. In even numbered iterations a vertex can only move from cluster i to cluster j if i > j; in odd numbered iterations a vertex can only move from cluster i to cluster j if i < j. This prevents two vertices from swapping clusters in the same iteration of the loop. We have had great success in converging on high modularity clustering using this technique. + +## Calling Louvain + +The unit test code is the best place to search for examples on calling louvain. + + * [SG Implementation](../../tests/community/louvain_test.cpp) + * [MG Implementation](../../tests/community/mg_louvain_test.cpp) + +The API itself is very simple. There are two variations: + * Return a flat clustering + * Return a Dendrogram + +### Return a flat clustering + +The example assumes that you create an SG or MG graph somehow. The caller must create the clustering vector in device memory and pass in the raw pointer to that vector into the louvain function. + +```cpp +#include +... +using vertex_t = int32_t; // or int64_t, whichever is appropriate +using weight_t = float; // or double, whichever is appropriate +raft::handle_t handle; // Must be configured if MG +auto graph_view = graph.view(); // assumes you have created a graph somehow + +size_t level; +weight_t modularity; + +rmm::device_uvector clustering_v(graph_view.get_number_of_vertices(), handle.get_stream()); + +// louvain optionally supports two additional parameters: +// max_level - maximum level of the Dendrogram +// resolution - constant in the modularity computation +std::tie(level, modularity) = cugraph::louvain(handle, graph_view, clustering_v.data()); +``` + +### Return a Dendrogram + +The Dendrogram represents the levels of hierarchical clustering that the Louvain algorithm computes. There is a separate function that will flatten the clustering into the same result as above. Returning the Dendrogram, however, provides a finer level of detail on the intermediate results which can be helpful in more fully understanding the data. + +```cpp +#include +... +using vertex_t = int32_t; // or int64_t, whichever is appropriate +using weight_t = float; // or double, whichever is appropriate +raft::handle_t handle; // Must be configured if MG +auto graph_view = graph.view(); // assumes you have created a graph somehow + +cugraph::Dendrogram dendrogram; +weight_t modularity; + +// louvain optionally supports two additional parameters: +// max_level - maximum level of the Dendrogram +// resolution - constant in the modularity computation +std::tie(dendrogram, modularity) = cugraph::louvain(handle, graph_view); + +// This will get the equivalent result to the earlier example +rmm::device_uvector clustering_v(graph_view.get_number_of_vertices(), handle.get_stream()); +cugraph::flatten_dendrogram(handle, graph_view, dendrogram, clustering.data()); +``` + +## Leiden + +## ECG diff --git a/cpp/src/community/ecg.cu b/cpp/src/community/ecg.cu new file mode 100644 index 00000000000..ca0f50c4801 --- /dev/null +++ b/cpp/src/community/ecg.cu @@ -0,0 +1,216 @@ +/* + * Copyright (c) 2019-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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +#include +#include + +#include + +namespace { +template +__device__ IndexType +binsearch_maxle(const IndexType* vec, const IndexType val, IndexType low, IndexType high) +{ + while (true) { + if (low == high) return low; // we know it exists + if ((low + 1) == high) return (vec[high] <= val) ? high : low; + + IndexType mid = low + (high - low) / 2; + + if (vec[mid] > val) + high = mid - 1; + else + low = mid; + } +} + +// FIXME: This shouldn't need to be a custom kernel, this +// seems like it should just be a thrust::transform +template +__global__ void match_check_kernel( + IdxT size, IdxT num_verts, IdxT* offsets, IdxT* indices, IdxT* parts, ValT* weights) +{ + IdxT tid = blockIdx.x * blockDim.x + threadIdx.x; + while (tid < size) { + IdxT source = binsearch_maxle(offsets, tid, (IdxT)0, num_verts); + IdxT dest = indices[tid]; + if (parts[source] == parts[dest]) weights[tid] += 1; + tid += gridDim.x * blockDim.x; + } +} + +struct prg { + __device__ float operator()(int n) + { + thrust::default_random_engine rng; + thrust::uniform_real_distribution dist(0.0, 1.0); + rng.discard(n); + return dist(rng); + } +}; + +template +struct update_functor { + ValT min_value; + ValT ensemble_size; + update_functor(ValT minv, ValT es) : min_value(minv), ensemble_size(es) {} + __host__ __device__ ValT operator()(ValT input) + { + return min_value + (1 - min_value) * (input / ensemble_size); + } +}; + +/** + * Computes a random permutation vector of length size. A permutation vector of length n + * contains all values [0..n-1] exactly once. + * @param size The length of the permutation vector to generate + * @param seed A seed value for the random number generator, the generator will discard this many + * values before using values. Calling this method with the same seed will result in the same + * permutation vector. + * @return A pointer to memory containing the requested permutation vector. The caller is + * responsible for freeing the allocated memory using ALLOC_FREE_TRY(). + */ +template +void get_permutation_vector(T size, T seed, T* permutation, rmm::cuda_stream_view stream_view) +{ + rmm::device_uvector randoms_v(size, stream_view); + + thrust::counting_iterator index(seed); + thrust::transform(rmm::exec_policy(stream_view), index, index + size, randoms_v.begin(), prg()); + thrust::sequence(rmm::exec_policy(stream_view), permutation, permutation + size, 0); + thrust::sort_by_key( + rmm::exec_policy(stream_view), randoms_v.begin(), randoms_v.end(), permutation); +} + +template +class EcgLouvain : public cugraph::Louvain { + public: + using graph_t = graph_type; + using vertex_t = typename graph_type::vertex_type; + using edge_t = typename graph_type::edge_type; + using weight_t = typename graph_type::weight_type; + + EcgLouvain(raft::handle_t const& handle, graph_type const& graph, vertex_t seed) + : cugraph::Louvain(handle, graph), seed_(seed) + { + } + + void initialize_dendrogram_level(vertex_t num_vertices) override + { + this->dendrogram_->add_level(0, num_vertices, this->handle_.get_stream_view()); + + get_permutation_vector(num_vertices, + seed_, + this->dendrogram_->current_level_begin(), + this->handle_.get_stream_view()); + } + + private: + vertex_t seed_; +}; + +} // anonymous namespace + +namespace cugraph { + +template +void ecg(raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + weight_t min_weight, + vertex_t ensemble_size, + vertex_t* clustering) +{ + using graph_type = legacy::GraphCSRView; + + CUGRAPH_EXPECTS(graph.edge_data != nullptr, + "Invalid input argument: ecg expects a weighted graph"); + CUGRAPH_EXPECTS(clustering != nullptr, + "Invalid input argument: clustering is NULL, should be a device pointer to " + "memory for storing the result"); + + rmm::device_uvector ecg_weights_v(graph.number_of_edges, handle.get_stream_view()); + + thrust::copy(rmm::exec_policy(handle.get_stream_view()), + graph.edge_data, + graph.edge_data + graph.number_of_edges, + ecg_weights_v.data()); + + vertex_t size{graph.number_of_vertices}; + + // FIXME: This seed should be a parameter + vertex_t seed{1}; + + // Iterate over each member of the ensemble + for (vertex_t i = 0; i < ensemble_size; i++) { + EcgLouvain runner(handle, graph, seed); + seed += size; + + weight_t wt = runner(size_t{1}, weight_t{1}); + + // For each edge in the graph determine whether the endpoints are in the same partition + // Keep a sum for each edge of the total number of times its endpoints are in the same partition + dim3 grid, block; + block.x = 512; + grid.x = min(vertex_t{CUDA_MAX_BLOCKS}, (graph.number_of_edges / 512 + 1)); + match_check_kernel<<>>( + graph.number_of_edges, + graph.number_of_vertices, + graph.offsets, + graph.indices, + runner.get_dendrogram().get_level_ptr_nocheck(0), + ecg_weights_v.data()); + } + + // Set weights = min_weight + (1 - min-weight)*sum/ensemble_size + update_functor uf(min_weight, ensemble_size); + thrust::transform(rmm::exec_policy(handle.get_stream_view()), + ecg_weights_v.begin(), + ecg_weights_v.end(), + ecg_weights_v.begin(), + uf); + + // Run Louvain on the original graph using the computed weights + // (pass max_level = 100 for a "full run") + legacy::GraphCSRView louvain_graph; + louvain_graph.indices = graph.indices; + louvain_graph.offsets = graph.offsets; + louvain_graph.edge_data = ecg_weights_v.data(); + louvain_graph.number_of_vertices = graph.number_of_vertices; + louvain_graph.number_of_edges = graph.number_of_edges; + + cugraph::louvain(handle, louvain_graph, clustering, size_t{100}); +} + +// Explicit template instantiations. +template void ecg( + raft::handle_t const&, + legacy::GraphCSRView const& graph, + float min_weight, + int32_t ensemble_size, + int32_t* clustering); +template void ecg( + raft::handle_t const&, + legacy::GraphCSRView const& graph, + double min_weight, + int32_t ensemble_size, + int32_t* clustering); +} // namespace cugraph diff --git a/cpp/src/community/egonet.cu b/cpp/src/community/egonet.cu new file mode 100644 index 00000000000..1e4569715af --- /dev/null +++ b/cpp/src/community/egonet.cu @@ -0,0 +1,272 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Alex Fender afender@nvida.com +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include + +#include +#include +#include + +#include +#include + +#include + +namespace { + +/* +Description +Let the egonet graph of a node x be the subgraph that includes node x, the neighborhood of x, and +all edges between them. Naive algorithm +- Add center node x to the graph. +- Go through all the neighbors y of this center node x, add edge (x, y) to the graph. +- For each neighbor y of center node x, go through all the neighbors z of center node x, if there is +an edge between y and z in original graph, add edge (y, z) to our new graph. + +Rather than doing custom one/two hops features, we propose a generic k-hops solution leveraging BFS +cutoff and subgraph extraction +*/ + +template +std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract( + raft::handle_t const& handle, + cugraph::experimental::graph_view_t const& csr_view, + vertex_t* source_vertex, + vertex_t n_subgraphs, + vertex_t radius) +{ + auto v = csr_view.get_number_of_vertices(); + auto e = csr_view.get_number_of_edges(); + auto user_stream_view = handle.get_stream_view(); + rmm::device_vector neighbors_offsets(n_subgraphs + 1); + rmm::device_vector neighbors; + + std::vector h_source_vertex(n_subgraphs); + std::vector h_neighbors_offsets(n_subgraphs + 1); + + raft::update_host(&h_source_vertex[0], source_vertex, n_subgraphs, user_stream_view.value()); + + // Streams will allocate concurrently later + std::vector> reached{}; + reached.reserve(n_subgraphs); + for (vertex_t i = 0; i < n_subgraphs; i++) { + // Allocations and operations are attached to the worker stream + rmm::device_uvector local_reach(v, handle.get_internal_stream_view(i)); + reached.push_back(std::move(local_reach)); + } + + // h_source_vertex[i] is used by other streams in the for loop + user_stream_view.synchronize(); +#ifdef TIMING + HighResTimer hr_timer; + hr_timer.start("ego_neighbors"); +#endif + + for (vertex_t i = 0; i < n_subgraphs; i++) { + // get light handle from worker pool + raft::handle_t light_handle(handle, i); + auto worker_stream_view = light_handle.get_stream_view(); + + // BFS with cutoff + // consider adding a device API to BFS (ie. accept source on the device) + rmm::device_uvector predecessors(v, worker_stream_view); // not used + bool direction_optimizing = false; + thrust::fill(rmm::exec_policy(worker_stream_view), + reached[i].begin(), + reached[i].end(), + std::numeric_limits::max()); + thrust::fill( + rmm::exec_policy(worker_stream_view), reached[i].begin(), reached[i].begin() + 100, 1.0); + + cugraph::experimental::bfs(light_handle, + csr_view, + reached[i].data(), + predecessors.data(), + h_source_vertex[i], + direction_optimizing, + radius); + + // identify reached vertex ids from distance array + thrust::transform(rmm::exec_policy(worker_stream_view), + thrust::make_counting_iterator(vertex_t{0}), + thrust::make_counting_iterator(v), + reached[i].begin(), + reached[i].begin(), + [sentinel = std::numeric_limits::max()] __device__( + auto id, auto val) { return val < sentinel ? id : sentinel; }); + + // removes unreached data + auto reached_end = thrust::remove(rmm::exec_policy(worker_stream_view), + reached[i].begin(), + reached[i].end(), + std::numeric_limits::max()); + // release temp storage + reached[i].resize(thrust::distance(reached[i].begin(), reached_end), worker_stream_view); + reached[i].shrink_to_fit(worker_stream_view); + } + + // wait on every one to identify their neighboors before proceeding to concatenation + handle.wait_on_internal_streams(); + + // Construct neighboors offsets (just a scan on neighborhod vector sizes) + h_neighbors_offsets[0] = 0; + for (vertex_t i = 0; i < n_subgraphs; i++) { + h_neighbors_offsets[i + 1] = h_neighbors_offsets[i] + reached[i].size(); + } + raft::update_device(neighbors_offsets.data().get(), + &h_neighbors_offsets[0], + n_subgraphs + 1, + user_stream_view.value()); + neighbors.resize(h_neighbors_offsets[n_subgraphs]); + user_stream_view.synchronize(); + + // Construct the neighboors list concurrently + for (vertex_t i = 0; i < n_subgraphs; i++) { + auto worker_stream_view = handle.get_internal_stream_view(i); + thrust::copy(rmm::exec_policy(worker_stream_view), + reached[i].begin(), + reached[i].end(), + neighbors.begin() + h_neighbors_offsets[i]); + + // reached info is not needed anymore + reached[i].resize(0, worker_stream_view); + reached[i].shrink_to_fit(worker_stream_view); + } + + // wait on every one before proceeding to grouped extraction + handle.wait_on_internal_streams(); + +#ifdef TIMING + hr_timer.stop(); + hr_timer.display(std::cout); +#endif + + // extract + return cugraph::experimental::extract_induced_subgraphs( + handle, csr_view, neighbors_offsets.data().get(), neighbors.data().get(), n_subgraphs); +} + +} // namespace + +namespace cugraph { +namespace experimental { + +template +std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_ego(raft::handle_t const& handle, + graph_view_t const& graph_view, + vertex_t* source_vertex, + vertex_t n_subgraphs, + vertex_t radius) +{ + if (multi_gpu) { + CUGRAPH_FAIL("Unimplemented."); + return std::make_tuple(rmm::device_uvector(0, handle.get_stream()), + rmm::device_uvector(0, handle.get_stream()), + rmm::device_uvector(0, handle.get_stream()), + rmm::device_uvector(0, handle.get_stream())); + } + CUGRAPH_EXPECTS(n_subgraphs > 0, "Need at least one source to extract the egonet from"); + CUGRAPH_EXPECTS(n_subgraphs < graph_view.get_number_of_vertices(), + "Can't have more sources to extract from than vertices in the graph"); + CUGRAPH_EXPECTS(radius > 0, "Radius should be at least 1"); + CUGRAPH_EXPECTS(radius < graph_view.get_number_of_vertices(), "radius is too large"); + // source_vertex range is checked in bfs. + + return extract( + handle, graph_view, source_vertex, n_subgraphs, radius); +} + +// SG FP32 +template std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_ego(raft::handle_t const&, + graph_view_t const&, + int32_t*, + int32_t, + int32_t); +template std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_ego(raft::handle_t const&, + graph_view_t const&, + int32_t*, + int32_t, + int32_t); +template std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_ego(raft::handle_t const&, + graph_view_t const&, + int64_t*, + int64_t, + int64_t); + +// SG FP64 +template std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_ego(raft::handle_t const&, + graph_view_t const&, + int32_t*, + int32_t, + int32_t); +template std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_ego(raft::handle_t const&, + graph_view_t const&, + int32_t*, + int32_t, + int32_t); +template std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_ego(raft::handle_t const&, + graph_view_t const&, + int64_t*, + int64_t, + int64_t); +} // namespace experimental +} // namespace cugraph diff --git a/cpp/src/community/extract_subgraph_by_vertex.cu b/cpp/src/community/extract_subgraph_by_vertex.cu index c39b7f8ad0a..224a3417caf 100644 --- a/cpp/src/community/extract_subgraph_by_vertex.cu +++ b/cpp/src/community/extract_subgraph_by_vertex.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -14,9 +14,9 @@ * limitations under the License. */ -#include -#include -#include +#include +#include +#include #include #include @@ -24,9 +24,9 @@ namespace { template -std::unique_ptr> extract_subgraph_by_vertices( - cugraph::GraphCOOView const &graph, - vertex_t const *vertices, +std::unique_ptr> extract_subgraph_by_vertices( + cugraph::legacy::GraphCOOView const& graph, + vertex_t const* vertices, vertex_t num_vertices, cudaStream_t stream) { @@ -35,8 +35,8 @@ std::unique_ptr> extract_subgraph_ rmm::device_vector error_count_v{1, 0}; rmm::device_vector vertex_used_v{graph_num_verts, num_vertices}; - vertex_t *d_vertex_used = vertex_used_v.data().get(); - int64_t *d_error_count = error_count_v.data().get(); + vertex_t* d_vertex_used = vertex_used_v.data().get(); + int64_t* d_error_count = error_count_v.data().get(); thrust::for_each( rmm::exec_policy(stream)->on(stream), @@ -54,9 +54,9 @@ std::unique_ptr> extract_subgraph_ CUGRAPH_EXPECTS(error_count_v[0] == 0, "Input error... vertices specifies vertex id out of range"); - vertex_t *graph_src = graph.src_indices; - vertex_t *graph_dst = graph.dst_indices; - weight_t *graph_weight = graph.edge_data; + vertex_t* graph_src = graph.src_indices; + vertex_t* graph_dst = graph.dst_indices; + weight_t* graph_weight = graph.edge_data; // iterate over the edges and count how many make it into the output int64_t count = thrust::count_if( @@ -70,12 +70,12 @@ std::unique_ptr> extract_subgraph_ }); if (count > 0) { - auto result = std::make_unique>( + auto result = std::make_unique>( num_vertices, count, has_weight); - vertex_t *d_new_src = result->src_indices(); - vertex_t *d_new_dst = result->dst_indices(); - weight_t *d_new_weight = result->edge_data(); + vertex_t* d_new_src = result->src_indices(); + vertex_t* d_new_dst = result->dst_indices(); + weight_t* d_new_weight = result->edge_data(); // reusing error_count as a vertex counter... thrust::for_each(rmm::exec_policy(stream)->on(stream), @@ -106,7 +106,8 @@ std::unique_ptr> extract_subgraph_ return result; } else { - return std::make_unique>(0, 0, has_weight); + return std::make_unique>( + 0, 0, has_weight); } } } // namespace @@ -115,11 +116,10 @@ namespace cugraph { namespace subgraph { template -std::unique_ptr> extract_subgraph_vertex(GraphCOOView const &graph, - VT const *vertices, - VT num_vertices) +std::unique_ptr> extract_subgraph_vertex( + legacy::GraphCOOView const& graph, VT const* vertices, VT num_vertices) { - CUGRAPH_EXPECTS(vertices != nullptr, "API error, vertices must be non null"); + CUGRAPH_EXPECTS(vertices != nullptr, "Invalid input argument: vertices must be non null"); cudaStream_t stream{0}; @@ -130,14 +130,12 @@ std::unique_ptr> extract_subgraph_vertex(GraphCOOView> -extract_subgraph_vertex(GraphCOOView const &, - int32_t const *, - int32_t); -template std::unique_ptr> -extract_subgraph_vertex(GraphCOOView const &, - int32_t const *, - int32_t); +template std::unique_ptr> +extract_subgraph_vertex( + legacy::GraphCOOView const&, int32_t const*, int32_t); +template std::unique_ptr> +extract_subgraph_vertex( + legacy::GraphCOOView const&, int32_t const*, int32_t); } // namespace subgraph } // namespace cugraph diff --git a/cpp/src/community/flatten_dendrogram.cuh b/cpp/src/community/flatten_dendrogram.cuh new file mode 100644 index 00000000000..9a8d214f883 --- /dev/null +++ b/cpp/src/community/flatten_dendrogram.cuh @@ -0,0 +1,59 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include + +#include +#include + +namespace cugraph { + +template +void partition_at_level(raft::handle_t const& handle, + Dendrogram const& dendrogram, + vertex_t const* d_vertex_ids, + vertex_t* d_partition, + size_t level) +{ + vertex_t local_num_verts = dendrogram.get_level_size_nocheck(0); + rmm::device_uvector local_vertex_ids_v(local_num_verts, handle.get_stream()); + + raft::copy(d_partition, d_vertex_ids, local_num_verts, handle.get_stream()); + + std::for_each( + thrust::make_counting_iterator(0), + thrust::make_counting_iterator(level), + [&handle, &dendrogram, &local_vertex_ids_v, d_vertex_ids, &d_partition, local_num_verts]( + size_t l) { + thrust::sequence(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + local_vertex_ids_v.begin(), + local_vertex_ids_v.begin() + dendrogram.get_level_size_nocheck(l), + dendrogram.get_level_first_index_nocheck(l)); + + cugraph::experimental::relabel( + handle, + std::tuple(local_vertex_ids_v.data(), + dendrogram.get_level_ptr_nocheck(l)), + dendrogram.get_level_size_nocheck(l), + d_partition, + local_num_verts, + false); + }); +} + +} // namespace cugraph diff --git a/cpp/src/community/ktruss.cu b/cpp/src/community/ktruss.cu index 11a8ed6fbae..2216278add8 100644 --- a/cpp/src/community/ktruss.cu +++ b/cpp/src/community/ktruss.cu @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. @@ -21,11 +21,11 @@ * @file ktruss.cu * --------------------------------------------------------------------------*/ -#include +#include #include #include -#include +#include #include "Static/KTruss/KTruss.cuh" using namespace hornets_nest; @@ -35,9 +35,8 @@ namespace cugraph { namespace detail { template -std::unique_ptr> ktruss_subgraph_impl(GraphCOOView const &graph, - int k, - rmm::mr::device_memory_resource *mr) +std::unique_ptr> ktruss_subgraph_impl( + legacy::GraphCOOView const& graph, int k, rmm::mr::device_memory_resource* mr) { using HornetGraph = hornet::gpu::Hornet; using UpdatePtr = hornet::BatchUpdatePtr; @@ -68,7 +67,7 @@ std::unique_ptr> ktruss_subgraph_impl(GraphCOOView>( + auto out_graph = std::make_unique>( graph.number_of_vertices, kt.getGraphEdgeCount(), graph.has_data(), stream, mr); kt.copyGraph(out_graph->src_indices(), out_graph->dst_indices()); @@ -79,8 +78,8 @@ std::unique_ptr> ktruss_subgraph_impl(GraphCOOView -std::unique_ptr> weighted_ktruss_subgraph_impl( - GraphCOOView const &graph, int k, rmm::mr::device_memory_resource *mr) +std::unique_ptr> weighted_ktruss_subgraph_impl( + legacy::GraphCOOView const& graph, int k, rmm::mr::device_memory_resource* mr) { using HornetGraph = hornet::gpu::Hornet>; using UpdatePtr = hornet::BatchUpdatePtr, hornet::DeviceType::DEVICE>; @@ -111,7 +110,7 @@ std::unique_ptr> weighted_ktruss_subgraph_impl( kt.runForK(k); CUGRAPH_EXPECTS(cudaPeekAtLastError() == cudaSuccess, "KTruss : Failed to run"); - auto out_graph = std::make_unique>( + auto out_graph = std::make_unique>( graph.number_of_vertices, kt.getGraphEdgeCount(), graph.has_data(), stream, mr); kt.copyGraph(out_graph->src_indices(), out_graph->dst_indices(), out_graph->edge_data()); @@ -125,9 +124,8 @@ std::unique_ptr> weighted_ktruss_subgraph_impl( } // namespace detail template -std::unique_ptr> k_truss_subgraph(GraphCOOView const &graph, - int k, - rmm::mr::device_memory_resource *mr) +std::unique_ptr> k_truss_subgraph( + legacy::GraphCOOView const& graph, int k, rmm::mr::device_memory_resource* mr) { CUGRAPH_EXPECTS(graph.src_indices != nullptr, "Graph source indices cannot be a nullptr"); CUGRAPH_EXPECTS(graph.dst_indices != nullptr, "Graph destination indices cannot be a nullptr"); @@ -139,10 +137,14 @@ std::unique_ptr> k_truss_subgraph(GraphCOOView } } -template std::unique_ptr> k_truss_subgraph( - GraphCOOView const &, int, rmm::mr::device_memory_resource *); +template std::unique_ptr> +k_truss_subgraph(legacy::GraphCOOView const&, + int, + rmm::mr::device_memory_resource*); -template std::unique_ptr> k_truss_subgraph( - GraphCOOView const &, int, rmm::mr::device_memory_resource *); +template std::unique_ptr> +k_truss_subgraph(legacy::GraphCOOView const&, + int, + rmm::mr::device_memory_resource*); } // namespace cugraph diff --git a/cpp/src/community/leiden.cu b/cpp/src/community/leiden.cu index 9e5a847cdf0..f55321dbebb 100644 --- a/cpp/src/community/leiden.cu +++ b/cpp/src/community/leiden.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -14,33 +14,57 @@ * limitations under the License. */ +#include #include +#include + namespace cugraph { template -std::pair leiden(raft::handle_t const &handle, - GraphCSRView const &graph, - vertex_t *clustering, +std::pair leiden(raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + vertex_t* clustering, size_t max_level, weight_t resolution) { CUGRAPH_EXPECTS(graph.edge_data != nullptr, "Invalid input argument: leiden expects a weighted graph"); - CUGRAPH_EXPECTS(clustering != nullptr, "Invalid input argument: clustering is null"); + CUGRAPH_EXPECTS(clustering != nullptr, + "Invalid input argument: clustering is null, should be a device pointer to " + "memory for storing the result"); + + Leiden> runner(handle, graph); + weight_t wt = runner(max_level, resolution); + + rmm::device_uvector vertex_ids_v(graph.number_of_vertices, handle.get_stream()); + + thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + thrust::make_counting_iterator(0), // MNMG - base vertex id + thrust::make_counting_iterator( + graph.number_of_vertices), // MNMG - base vertex id + number_of_vertices + vertex_ids_v.begin()); - Leiden> runner(handle, graph); + partition_at_level(handle, + runner.get_dendrogram(), + vertex_ids_v.data(), + clustering, + runner.get_dendrogram().num_levels()); - return runner(clustering, max_level, resolution); + // FIXME: Consider returning the Dendrogram at some point + return std::make_pair(runner.get_dendrogram().num_levels(), wt); } // Explicit template instantations -template std::pair leiden( - raft::handle_t const &, GraphCSRView const &, int32_t *, size_t, float); +template std::pair leiden(raft::handle_t const&, + legacy::GraphCSRView const&, + int32_t*, + size_t, + float); -template std::pair leiden(raft::handle_t const &, - GraphCSRView const &, - int32_t *, +template std::pair leiden(raft::handle_t const&, + legacy::GraphCSRView const&, + int32_t*, size_t, double); diff --git a/cpp/src/community/leiden.cuh b/cpp/src/community/leiden.cuh index f2f84433284..252fdbf60a7 100644 --- a/cpp/src/community/leiden.cuh +++ b/cpp/src/community/leiden.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -17,6 +17,8 @@ #include +#include + namespace cugraph { template @@ -27,33 +29,42 @@ class Leiden : public Louvain { using edge_t = typename graph_type::edge_type; using weight_t = typename graph_type::weight_type; - Leiden(raft::handle_t const &handle, graph_type const &graph) - : Louvain(handle, graph), constraint_v_(graph.number_of_vertices) + Leiden(raft::handle_t const& handle, graph_type const& graph) + : Louvain(handle, graph), + constraint_v_(graph.number_of_vertices, handle.get_stream()) { } weight_t update_clustering_constrained(weight_t total_edge_weight, weight_t resolution, - graph_type const &graph) + graph_type const& graph) { this->timer_start("update_clustering_constrained"); - rmm::device_vector next_cluster_v(this->cluster_v_); - rmm::device_vector delta_Q_v(graph.number_of_edges); - rmm::device_vector cluster_hash_v(graph.number_of_edges); - rmm::device_vector old_cluster_sum_v(graph.number_of_vertices); - - vertex_t const *d_src_indices = this->src_indices_v_.data().get(); - vertex_t const *d_dst_indices = graph.indices; - vertex_t *d_cluster_hash = cluster_hash_v.data().get(); - vertex_t *d_cluster = this->cluster_v_.data().get(); - weight_t const *d_vertex_weights = this->vertex_weights_v_.data().get(); - weight_t *d_cluster_weights = this->cluster_weights_v_.data().get(); - weight_t *d_delta_Q = delta_Q_v.data().get(); - vertex_t *d_constraint = constraint_v_.data().get(); - - weight_t new_Q = - this->modularity(total_edge_weight, resolution, graph, this->cluster_v_.data().get()); + rmm::device_uvector next_cluster_v(this->dendrogram_->current_level_size(), + this->handle_.get_stream_view()); + rmm::device_uvector delta_Q_v(graph.number_of_edges, this->handle_.get_stream_view()); + rmm::device_uvector cluster_hash_v(graph.number_of_edges, + this->handle_.get_stream_view()); + rmm::device_uvector old_cluster_sum_v(graph.number_of_vertices, + this->handle_.get_stream_view()); + + vertex_t const* d_src_indices = this->src_indices_v_.data(); + vertex_t const* d_dst_indices = graph.indices; + vertex_t* d_cluster_hash = cluster_hash_v.data(); + vertex_t* d_cluster = this->dendrogram_->current_level_begin(); + weight_t const* d_vertex_weights = this->vertex_weights_v_.data(); + weight_t* d_cluster_weights = this->cluster_weights_v_.data(); + weight_t* d_delta_Q = delta_Q_v.data(); + vertex_t* d_constraint = constraint_v_.data(); + + thrust::copy(rmm::exec_policy(this->handle_.get_stream_view()), + this->dendrogram_->current_level_begin(), + this->dendrogram_->current_level_end(), + next_cluster_v.data()); + + weight_t new_Q = this->modularity( + total_edge_weight, resolution, graph, this->dendrogram_->current_level_begin()); weight_t cur_Q = new_Q - 1; @@ -70,7 +81,7 @@ class Leiden : public Louvain { // Filter out positive delta_Q values for nodes not in the same constraint group thrust::for_each( - rmm::exec_policy(this->stream_)->on(this->stream_), + rmm::exec_policy(this->handle_.get_stream_view()), thrust::make_counting_iterator(0), thrust::make_counting_iterator(graph.number_of_edges), [d_src_indices, d_dst_indices, d_constraint, d_delta_Q] __device__(vertex_t i) { @@ -83,83 +94,75 @@ class Leiden : public Louvain { up_down = !up_down; - new_Q = this->modularity(total_edge_weight, resolution, graph, next_cluster_v.data().get()); + new_Q = this->modularity(total_edge_weight, resolution, graph, next_cluster_v.data()); if (new_Q > cur_Q) { - thrust::copy(rmm::exec_policy(this->stream_)->on(this->stream_), + thrust::copy(rmm::exec_policy(this->handle_.get_stream_view()), next_cluster_v.begin(), next_cluster_v.end(), - this->cluster_v_.begin()); + this->dendrogram_->current_level_begin()); } } - this->timer_stop(this->stream_); + this->timer_stop(this->handle_.get_stream_view()); return cur_Q; } - std::pair operator()(vertex_t *d_cluster_vec, - size_t max_level, - weight_t resolution) + weight_t operator()(size_t max_level, weight_t resolution) override { size_t num_level{0}; - weight_t total_edge_weight = thrust::reduce(rmm::exec_policy(this->stream_)->on(this->stream_), + weight_t total_edge_weight = thrust::reduce(rmm::exec_policy(this->handle_.get_stream_view()), this->weights_v_.begin(), this->weights_v_.end()); weight_t best_modularity = weight_t{-1}; - // - // Initialize every cluster to reference each vertex to itself - // - thrust::sequence(rmm::exec_policy(this->stream_)->on(this->stream_), - this->cluster_v_.begin(), - this->cluster_v_.end()); - thrust::copy(rmm::exec_policy(this->stream_)->on(this->stream_), - this->cluster_v_.begin(), - this->cluster_v_.end(), - d_cluster_vec); - // // Our copy of the graph. Each iteration of the outer loop will // shrink this copy of the graph. // - GraphCSRView current_graph(this->offsets_v_.data().get(), - this->indices_v_.data().get(), - this->weights_v_.data().get(), - this->number_of_vertices_, - this->number_of_edges_); + legacy::GraphCSRView current_graph(this->offsets_v_.data(), + this->indices_v_.data(), + this->weights_v_.data(), + this->number_of_vertices_, + this->number_of_edges_); - current_graph.get_source_indices(this->src_indices_v_.data().get()); + current_graph.get_source_indices(this->src_indices_v_.data()); while (num_level < max_level) { + // + // Initialize every cluster to reference each vertex to itself + // + this->dendrogram_->add_level( + 0, current_graph.number_of_vertices, this->handle_.get_stream_view()); + + thrust::sequence(rmm::exec_policy(this->handle_.get_stream_view()), + this->dendrogram_->current_level_begin(), + this->dendrogram_->current_level_end()); + this->compute_vertex_and_cluster_weights(current_graph); weight_t new_Q = this->update_clustering(total_edge_weight, resolution, current_graph); - thrust::copy(rmm::exec_policy(this->stream_)->on(this->stream_), - this->cluster_v_.begin(), - this->cluster_v_.end(), - constraint_v_.begin()); - new_Q = update_clustering_constrained(total_edge_weight, resolution, current_graph); if (new_Q <= best_modularity) { break; } best_modularity = new_Q; - this->shrink_graph(current_graph, d_cluster_vec); + this->shrink_graph(current_graph); num_level++; } this->timer_display(std::cout); - return std::make_pair(num_level, best_modularity); + return best_modularity; } private: - rmm::device_vector constraint_v_; + rmm::device_uvector constraint_v_; }; } // namespace cugraph diff --git a/cpp/src/community/louvain.cu b/cpp/src/community/louvain.cu index 1044211a0ce..c3df4207283 100644 --- a/cpp/src/community/louvain.cu +++ b/cpp/src/community/louvain.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -14,161 +14,280 @@ * limitations under the License. */ +#include #include - -// "FIXME": remove the guards after support for Pascal will be dropped; -// -// Disable louvain(experimenta::graph_view_t,...) -// versions for GPU architectures < 700 -//(this is because cuco/static_map.cuh would not -// compile on those) -// -#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 700 -#include -#else +#include #include -#endif + +#include + +CUCO_DECLARE_BITWISE_COMPARABLE(float) +CUCO_DECLARE_BITWISE_COMPARABLE(double) namespace cugraph { namespace detail { template -std::pair louvain(raft::handle_t const &handle, - GraphCSRView const &graph_view, - vertex_t *clustering, - size_t max_level, - weight_t resolution) +std::pair>, weight_t> louvain( + raft::handle_t const& handle, + legacy::GraphCSRView const& graph_view, + size_t max_level, + weight_t resolution) { CUGRAPH_EXPECTS(graph_view.edge_data != nullptr, "Invalid input argument: louvain expects a weighted graph"); - CUGRAPH_EXPECTS(clustering != nullptr, "Invalid input argument: clustering is null"); - Louvain> runner(handle, graph_view); - return runner(clustering, max_level, resolution); + Louvain> runner(handle, graph_view); + weight_t wt = runner(max_level, resolution); + + return std::make_pair(runner.move_dendrogram(), wt); } template -std::pair louvain( - raft::handle_t const &handle, - experimental::graph_view_t const &graph_view, - vertex_t *clustering, +std::pair>, weight_t> louvain( + raft::handle_t const& handle, + experimental::graph_view_t const& graph_view, size_t max_level, weight_t resolution) { - CUGRAPH_EXPECTS(clustering != nullptr, "Invalid input argument: clustering is null"); - -#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 700 - CUGRAPH_FAIL("Louvain not supported on Pascal and older architectures"); -#else experimental::Louvain> runner(handle, graph_view); - return runner(clustering, max_level, resolution); -#endif + + weight_t wt = runner(max_level, resolution); + + return std::make_pair(runner.move_dendrogram(), wt); +} + +template +void flatten_dendrogram(raft::handle_t const& handle, + legacy::GraphCSRView const& graph_view, + Dendrogram const& dendrogram, + vertex_t* clustering) +{ + rmm::device_uvector vertex_ids_v(graph_view.number_of_vertices, handle.get_stream()); + + thrust::sequence(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + vertex_ids_v.begin(), + vertex_ids_v.end(), + vertex_t{0}); + + partition_at_level( + handle, dendrogram, vertex_ids_v.data(), clustering, dendrogram.num_levels()); +} + +template +void flatten_dendrogram( + raft::handle_t const& handle, + experimental::graph_view_t const& graph_view, + Dendrogram const& dendrogram, + vertex_t* clustering) +{ + rmm::device_uvector vertex_ids_v(graph_view.get_number_of_vertices(), + handle.get_stream()); + + thrust::sequence(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + vertex_ids_v.begin(), + vertex_ids_v.end(), + graph_view.get_local_vertex_first()); + + partition_at_level( + handle, dendrogram, vertex_ids_v.data(), clustering, dendrogram.num_levels()); } } // namespace detail -template -std::pair louvain(raft::handle_t const &handle, - graph_t const &graph, - typename graph_t::vertex_type *clustering, - size_t max_level, - typename graph_t::weight_type resolution) +template +std::pair>, + typename graph_view_t::weight_type> +louvain(raft::handle_t const& handle, + graph_view_t const& graph_view, + size_t max_level, + typename graph_view_t::weight_type resolution) +{ + return detail::louvain(handle, graph_view, max_level, resolution); +} + +template +void flatten_dendrogram(raft::handle_t const& handle, + graph_view_t const& graph_view, + Dendrogram const& dendrogram, + typename graph_view_t::vertex_type* clustering) +{ + detail::flatten_dendrogram(handle, graph_view, dendrogram, clustering); +} + +template +std::pair louvain( + raft::handle_t const& handle, + graph_view_t const& graph_view, + typename graph_view_t::vertex_type* clustering, + size_t max_level, + typename graph_view_t::weight_type resolution) { + using vertex_t = typename graph_view_t::vertex_type; + using weight_t = typename graph_view_t::weight_type; + CUGRAPH_EXPECTS(clustering != nullptr, "Invalid input argument: clustering is null"); -#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 700 - CUGRAPH_FAIL("Louvain not supported on Pascal and older architectures"); -#else - return detail::louvain(handle, graph, clustering, max_level, resolution); -#endif + std::unique_ptr> dendrogram; + weight_t modularity; + + std::tie(dendrogram, modularity) = louvain(handle, graph_view, max_level, resolution); + + flatten_dendrogram(handle, graph_view, *dendrogram, clustering); + + return std::make_pair(dendrogram->num_levels(), modularity); } // Explicit template instantations -template std::pair louvain( - raft::handle_t const &, GraphCSRView const &, int32_t *, size_t, float); -template std::pair louvain(raft::handle_t const &, - GraphCSRView const &, - int32_t *, +template std::pair>, float> louvain( + raft::handle_t const&, + experimental::graph_view_t const&, + size_t, + float); +template std::pair>, float> louvain( + raft::handle_t const&, + experimental::graph_view_t const&, + size_t, + float); +template std::pair>, float> louvain( + raft::handle_t const&, + experimental::graph_view_t const&, + size_t, + float); +template std::pair>, double> louvain( + raft::handle_t const&, + experimental::graph_view_t const&, + size_t, + double); +template std::pair>, double> louvain( + raft::handle_t const&, + experimental::graph_view_t const&, + size_t, + double); +template std::pair>, double> louvain( + raft::handle_t const&, + experimental::graph_view_t const&, + size_t, + double); +template std::pair>, float> louvain( + raft::handle_t const&, + experimental::graph_view_t const&, + size_t, + float); +template std::pair>, float> louvain( + raft::handle_t const&, + experimental::graph_view_t const&, + size_t, + float); +template std::pair>, float> louvain( + raft::handle_t const&, + experimental::graph_view_t const&, + size_t, + float); +template std::pair>, double> louvain( + raft::handle_t const&, + experimental::graph_view_t const&, + size_t, + double); +template std::pair>, double> louvain( + raft::handle_t const&, + experimental::graph_view_t const&, + size_t, + double); +template std::pair>, double> louvain( + raft::handle_t const&, + experimental::graph_view_t const&, + size_t, + double); + +template std::pair louvain(raft::handle_t const&, + legacy::GraphCSRView const&, + int32_t*, + size_t, + float); +template std::pair louvain(raft::handle_t const&, + legacy::GraphCSRView const&, + int32_t*, size_t, double); template std::pair louvain( - raft::handle_t const &, - experimental::graph_view_t const &, - int32_t *, + raft::handle_t const&, + experimental::graph_view_t const&, + int32_t*, size_t, float); template std::pair louvain( - raft::handle_t const &, - experimental::graph_view_t const &, - int32_t *, + raft::handle_t const&, + experimental::graph_view_t const&, + int32_t*, size_t, double); template std::pair louvain( - raft::handle_t const &, - experimental::graph_view_t const &, - int32_t *, + raft::handle_t const&, + experimental::graph_view_t const&, + int32_t*, size_t, float); template std::pair louvain( - raft::handle_t const &, - experimental::graph_view_t const &, - int32_t *, + raft::handle_t const&, + experimental::graph_view_t const&, + int32_t*, size_t, double); template std::pair louvain( - raft::handle_t const &, - experimental::graph_view_t const &, - int64_t *, + raft::handle_t const&, + experimental::graph_view_t const&, + int64_t*, size_t, float); template std::pair louvain( - raft::handle_t const &, - experimental::graph_view_t const &, - int64_t *, + raft::handle_t const&, + experimental::graph_view_t const&, + int64_t*, size_t, double); // instantations with multi_gpu = true template std::pair louvain( - raft::handle_t const &, - experimental::graph_view_t const &, - int32_t *, + raft::handle_t const&, + experimental::graph_view_t const&, + int32_t*, size_t, float); template std::pair louvain( - raft::handle_t const &, - experimental::graph_view_t const &, - int32_t *, + raft::handle_t const&, + experimental::graph_view_t const&, + int32_t*, size_t, double); template std::pair louvain( - raft::handle_t const &, - experimental::graph_view_t const &, - int32_t *, + raft::handle_t const&, + experimental::graph_view_t const&, + int32_t*, size_t, float); template std::pair louvain( - raft::handle_t const &, - experimental::graph_view_t const &, - int32_t *, + raft::handle_t const&, + experimental::graph_view_t const&, + int32_t*, size_t, double); template std::pair louvain( - raft::handle_t const &, - experimental::graph_view_t const &, - int64_t *, + raft::handle_t const&, + experimental::graph_view_t const&, + int64_t*, size_t, float); template std::pair louvain( - raft::handle_t const &, - experimental::graph_view_t const &, - int64_t *, + raft::handle_t const&, + experimental::graph_view_t const&, + int64_t*, size_t, double); } // namespace cugraph -#include +#include diff --git a/cpp/src/community/louvain.cuh b/cpp/src/community/louvain.cuh index 7ca3638f42b..31c5a2281ad 100644 --- a/cpp/src/community/louvain.cuh +++ b/cpp/src/community/louvain.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -15,13 +15,16 @@ */ #pragma once -#include - -#include +#include #include #include +#include + +#include +#include + //#define TIMING #ifdef TIMING @@ -38,76 +41,95 @@ class Louvain { using edge_t = typename graph_type::edge_type; using weight_t = typename graph_type::weight_type; - Louvain(raft::handle_t const &handle, graph_type const &graph) + Louvain(raft::handle_t const& handle, graph_type const& graph) : #ifdef TIMING hr_timer_(), #endif handle_(handle), + dendrogram_(std::make_unique>()), // FIXME: Don't really need to copy here but would need // to change the logic to populate this properly // in generate_superverticies_graph. // - offsets_v_(graph.offsets, graph.offsets + graph.number_of_vertices + 1), - indices_v_(graph.indices, graph.indices + graph.number_of_edges), - weights_v_(graph.edge_data, graph.edge_data + graph.number_of_edges), - src_indices_v_(graph.number_of_edges), - vertex_weights_v_(graph.number_of_vertices), - cluster_weights_v_(graph.number_of_vertices), - cluster_v_(graph.number_of_vertices), - tmp_arr_v_(graph.number_of_vertices), - cluster_inverse_v_(graph.number_of_vertices), + offsets_v_(graph.number_of_vertices + 1, handle.get_stream_view()), + indices_v_(graph.number_of_edges, handle.get_stream_view()), + weights_v_(graph.number_of_edges, handle.get_stream_view()), + src_indices_v_(graph.number_of_edges, handle.get_stream_view()), + vertex_weights_v_(graph.number_of_vertices, handle.get_stream_view()), + cluster_weights_v_(graph.number_of_vertices, handle.get_stream_view()), + tmp_arr_v_(graph.number_of_vertices, handle.get_stream_view()), + cluster_inverse_v_(graph.number_of_vertices, handle.get_stream_view()), number_of_vertices_(graph.number_of_vertices), - number_of_edges_(graph.number_of_edges), - stream_(handle.get_stream()) + number_of_edges_(graph.number_of_edges) { + thrust::copy(rmm::exec_policy(handle_.get_stream_view()), + graph.offsets, + graph.offsets + graph.number_of_vertices + 1, + offsets_v_.begin()); + + thrust::copy(rmm::exec_policy(handle_.get_stream_view()), + graph.indices, + graph.indices + graph.number_of_edges, + indices_v_.begin()); + + thrust::copy(rmm::exec_policy(handle_.get_stream_view()), + graph.edge_data, + graph.edge_data + graph.number_of_edges, + weights_v_.begin()); } + virtual ~Louvain() {} + weight_t modularity(weight_t total_edge_weight, weight_t resolution, - graph_t const &graph, - vertex_t const *d_cluster) + graph_t const& graph, + vertex_t const* d_cluster) { vertex_t n_verts = graph.number_of_vertices; - rmm::device_vector inc(n_verts, weight_t{0.0}); - rmm::device_vector deg(n_verts, weight_t{0.0}); + rmm::device_uvector inc(n_verts, handle_.get_stream_view()); + rmm::device_uvector deg(n_verts, handle_.get_stream_view()); - edge_t const *d_offsets = graph.offsets; - vertex_t const *d_indices = graph.indices; - weight_t const *d_weights = graph.edge_data; - weight_t *d_inc = inc.data().get(); - weight_t *d_deg = deg.data().get(); + thrust::fill( + rmm::exec_policy(handle_.get_stream_view()), inc.begin(), inc.end(), weight_t{0.0}); + thrust::fill( + rmm::exec_policy(handle_.get_stream_view()), deg.begin(), deg.end(), weight_t{0.0}); // FIXME: Already have weighted degree computed in main loop, // could pass that in rather than computing d_deg... which // would save an atomicAdd (synchronization) // - thrust::for_each( - rmm::exec_policy(stream_)->on(stream_), - thrust::make_counting_iterator(0), - thrust::make_counting_iterator(graph.number_of_vertices), - [d_inc, d_deg, d_offsets, d_indices, d_weights, d_cluster] __device__(vertex_t v) { - vertex_t community = d_cluster[v]; - weight_t increase{0.0}; - weight_t degree{0.0}; - - for (edge_t loc = d_offsets[v]; loc < d_offsets[v + 1]; ++loc) { - vertex_t neighbor = d_indices[loc]; - degree += d_weights[loc]; - if (d_cluster[neighbor] == community) { increase += d_weights[loc]; } - } + thrust::for_each(rmm::exec_policy(handle_.get_stream_view()), + thrust::make_counting_iterator(0), + thrust::make_counting_iterator(graph.number_of_vertices), + [d_inc = inc.data(), + d_deg = deg.data(), + d_offsets = graph.offsets, + d_indices = graph.indices, + d_weights = graph.edge_data, + d_cluster] __device__(vertex_t v) { + vertex_t community = d_cluster[v]; + weight_t increase{0.0}; + weight_t degree{0.0}; + + for (edge_t loc = d_offsets[v]; loc < d_offsets[v + 1]; ++loc) { + vertex_t neighbor = d_indices[loc]; + degree += d_weights[loc]; + if (d_cluster[neighbor] == community) { increase += d_weights[loc]; } + } - if (degree > weight_t{0.0}) atomicAdd(d_deg + community, degree); - if (increase > weight_t{0.0}) atomicAdd(d_inc + community, increase); - }); + if (degree > weight_t{0.0}) atomicAdd(d_deg + community, degree); + if (increase > weight_t{0.0}) atomicAdd(d_inc + community, increase); + }); weight_t Q = thrust::transform_reduce( - rmm::exec_policy(stream_)->on(stream_), + rmm::exec_policy(handle_.get_stream_view()), thrust::make_counting_iterator(0), thrust::make_counting_iterator(graph.number_of_vertices), - [d_deg, d_inc, total_edge_weight, resolution] __device__(vertex_t community) { + [d_deg = deg.data(), d_inc = inc.data(), total_edge_weight, resolution] __device__( + vertex_t community) { return ((d_inc[community] / total_edge_weight) - resolution * (d_deg[community] * d_deg[community]) / (total_edge_weight * total_edge_weight)); @@ -118,37 +140,37 @@ class Louvain { return Q; } - virtual std::pair operator()(vertex_t *d_cluster_vec, - size_t max_level, - weight_t resolution) - { - size_t num_level{0}; + Dendrogram const& get_dendrogram() const { return *dendrogram_; } - weight_t total_edge_weight = - thrust::reduce(rmm::exec_policy(stream_)->on(stream_), weights_v_.begin(), weights_v_.end()); + Dendrogram& get_dendrogram() { return *dendrogram_; } - weight_t best_modularity = weight_t{-1}; + std::unique_ptr> move_dendrogram() { return std::move(dendrogram_); } - // - // Initialize every cluster to reference each vertex to itself - // - thrust::sequence(rmm::exec_policy(stream_)->on(stream_), cluster_v_.begin(), cluster_v_.end()); - thrust::copy( - rmm::exec_policy(stream_)->on(stream_), cluster_v_.begin(), cluster_v_.end(), d_cluster_vec); + virtual weight_t operator()(size_t max_level, weight_t resolution) + { + weight_t total_edge_weight = thrust::reduce( + rmm::exec_policy(handle_.get_stream_view()), weights_v_.begin(), weights_v_.end()); + + weight_t best_modularity = weight_t{-1}; // // Our copy of the graph. Each iteration of the outer loop will // shrink this copy of the graph. // - GraphCSRView current_graph(offsets_v_.data().get(), - indices_v_.data().get(), - weights_v_.data().get(), - number_of_vertices_, - number_of_edges_); + legacy::GraphCSRView current_graph(offsets_v_.data(), + indices_v_.data(), + weights_v_.data(), + number_of_vertices_, + number_of_edges_); - current_graph.get_source_indices(src_indices_v_.data().get()); + current_graph.get_source_indices(src_indices_v_.data()); + + while (dendrogram_->num_levels() < max_level) { + // + // Initialize every cluster to reference each vertex to itself + // + initialize_dendrogram_level(current_graph.number_of_vertices); - while (num_level < max_level) { compute_vertex_and_cluster_weights(current_graph); weight_t new_Q = update_clustering(total_edge_weight, resolution, current_graph); @@ -157,55 +179,62 @@ class Louvain { best_modularity = new_Q; - shrink_graph(current_graph, d_cluster_vec); - - num_level++; + shrink_graph(current_graph); } timer_display(std::cout); - return std::make_pair(num_level, best_modularity); + return best_modularity; } protected: - void timer_start(std::string const ®ion) + void timer_start(std::string const& region) { #ifdef TIMING hr_timer_.start(region); #endif } - void timer_stop(cudaStream_t stream) + void timer_stop(rmm::cuda_stream_view stream_view) { #ifdef TIMING - CUDA_TRY(cudaStreamSynchronize(stream)); + stream_view.synchronize(); hr_timer_.stop(); #endif } - void timer_display(std::ostream &os) + void timer_display(std::ostream& os) { #ifdef TIMING hr_timer_.display(os); #endif } + virtual void initialize_dendrogram_level(vertex_t num_vertices) + { + dendrogram_->add_level(0, num_vertices, handle_.get_stream_view()); + + thrust::sequence(rmm::exec_policy(handle_.get_stream_view()), + dendrogram_->current_level_begin(), + dendrogram_->current_level_end()); + } + public: - void compute_vertex_and_cluster_weights(graph_type const &graph) + void compute_vertex_and_cluster_weights(graph_type const& graph) { timer_start("compute_vertex_and_cluster_weights"); - edge_t const *d_offsets = graph.offsets; - vertex_t const *d_indices = graph.indices; - weight_t const *d_weights = graph.edge_data; - weight_t *d_vertex_weights = vertex_weights_v_.data().get(); - weight_t *d_cluster_weights = cluster_weights_v_.data().get(); + edge_t const* d_offsets = graph.offsets; + vertex_t const* d_indices = graph.indices; + weight_t const* d_weights = graph.edge_data; + weight_t* d_vertex_weights = vertex_weights_v_.data(); + weight_t* d_cluster_weights = cluster_weights_v_.data(); // // MNMG: copy_v_transform_reduce_out_nbr, then copy // thrust::for_each( - rmm::exec_policy(stream_)->on(stream_), + rmm::exec_policy(handle_.get_stream_view()), thrust::make_counting_iterator(0), thrust::make_counting_iterator(graph.number_of_vertices), [d_offsets, d_indices, d_weights, d_vertex_weights, d_cluster_weights] __device__( @@ -217,30 +246,34 @@ class Louvain { d_cluster_weights[src] = sum; }); - timer_stop(stream_); + timer_stop(handle_.get_stream_view()); } virtual weight_t update_clustering(weight_t total_edge_weight, weight_t resolution, - graph_type const &graph) + graph_type const& graph) { timer_start("update_clustering"); - // - // MNMG: This is the hard one, see writeup - // - rmm::device_vector next_cluster_v(cluster_v_); - rmm::device_vector delta_Q_v(graph.number_of_edges); - rmm::device_vector cluster_hash_v(graph.number_of_edges); - rmm::device_vector old_cluster_sum_v(graph.number_of_vertices); + rmm::device_uvector next_cluster_v(dendrogram_->current_level_size(), + handle_.get_stream_view()); + rmm::device_uvector delta_Q_v(graph.number_of_edges, handle_.get_stream_view()); + rmm::device_uvector cluster_hash_v(graph.number_of_edges, handle_.get_stream_view()); + rmm::device_uvector old_cluster_sum_v(graph.number_of_vertices, + handle_.get_stream_view()); - vertex_t *d_cluster_hash = cluster_hash_v.data().get(); - vertex_t *d_cluster = cluster_v_.data().get(); - weight_t const *d_vertex_weights = vertex_weights_v_.data().get(); - weight_t *d_cluster_weights = cluster_weights_v_.data().get(); - weight_t *d_delta_Q = delta_Q_v.data().get(); + vertex_t* d_cluster = dendrogram_->current_level_begin(); + weight_t const* d_vertex_weights = vertex_weights_v_.data(); + weight_t* d_cluster_weights = cluster_weights_v_.data(); + weight_t* d_delta_Q = delta_Q_v.data(); - weight_t new_Q = modularity(total_edge_weight, resolution, graph, cluster_v_.data().get()); + thrust::copy(rmm::exec_policy(handle_.get_stream_view()), + dendrogram_->current_level_begin(), + dendrogram_->current_level_end(), + next_cluster_v.data()); + + weight_t new_Q = + modularity(total_edge_weight, resolution, graph, dendrogram_->current_level_begin()); weight_t cur_Q = new_Q - 1; @@ -259,62 +292,56 @@ class Louvain { up_down = !up_down; - new_Q = modularity(total_edge_weight, resolution, graph, next_cluster_v.data().get()); + new_Q = modularity(total_edge_weight, resolution, graph, next_cluster_v.data()); if (new_Q > cur_Q) { - thrust::copy(rmm::exec_policy(stream_)->on(stream_), + thrust::copy(rmm::exec_policy(handle_.get_stream_view()), next_cluster_v.begin(), next_cluster_v.end(), - cluster_v_.begin()); + dendrogram_->current_level_begin()); } } - timer_stop(stream_); + timer_stop(handle_.get_stream_view()); return cur_Q; } void compute_delta_modularity(weight_t total_edge_weight, weight_t resolution, - graph_type const &graph, - rmm::device_vector &cluster_hash_v, - rmm::device_vector &old_cluster_sum_v, - rmm::device_vector &delta_Q_v) + graph_type const& graph, + rmm::device_uvector& cluster_hash_v, + rmm::device_uvector& old_cluster_sum_v, + rmm::device_uvector& delta_Q_v) { - vertex_t const *d_src_indices = src_indices_v_.data().get(); - vertex_t const *d_dst_indices = graph.indices; - edge_t const *d_offsets = graph.offsets; - weight_t const *d_weights = graph.edge_data; - vertex_t const *d_cluster = cluster_v_.data().get(); - weight_t const *d_vertex_weights = vertex_weights_v_.data().get(); - weight_t const *d_cluster_weights = cluster_weights_v_.data().get(); - - vertex_t *d_cluster_hash = cluster_hash_v.data().get(); - weight_t *d_delta_Q = delta_Q_v.data().get(); - weight_t *d_old_cluster_sum = old_cluster_sum_v.data().get(); - weight_t *d_new_cluster_sum = d_delta_Q; - - thrust::fill(cluster_hash_v.begin(), cluster_hash_v.end(), vertex_t{-1}); - thrust::fill(delta_Q_v.begin(), delta_Q_v.end(), weight_t{0.0}); - thrust::fill(old_cluster_sum_v.begin(), old_cluster_sum_v.end(), weight_t{0.0}); - - // MNMG: New technique using reduce_by_key. Would require a segmented sort - // or a pair of sorts on each node, so probably slower than what's here. - // This might still be faster even in MNMG... - // - // - // FIXME: Eventually this should use cuCollections concurrent map - // implementation, but that won't be available for a while. - // - // For each source vertex, we're going to build a hash - // table to the destination cluster ids. We can use - // the offsets ranges to define the bounds of the hash - // table. - // - thrust::for_each(rmm::exec_policy(stream_)->on(stream_), + edge_t const* d_offsets = graph.offsets; + weight_t const* d_weights = graph.edge_data; + vertex_t const* d_cluster = dendrogram_->current_level_begin(); + weight_t const* d_vertex_weights = vertex_weights_v_.data(); + weight_t const* d_cluster_weights = cluster_weights_v_.data(); + + vertex_t* d_cluster_hash = cluster_hash_v.data(); + weight_t* d_delta_Q = delta_Q_v.data(); + weight_t* d_old_cluster_sum = old_cluster_sum_v.data(); + weight_t* d_new_cluster_sum = d_delta_Q; + + thrust::fill(rmm::exec_policy(handle_.get_stream_view()), + cluster_hash_v.begin(), + cluster_hash_v.end(), + vertex_t{-1}); + thrust::fill(rmm::exec_policy(handle_.get_stream_view()), + delta_Q_v.begin(), + delta_Q_v.end(), + weight_t{0.0}); + thrust::fill(rmm::exec_policy(handle_.get_stream_view()), + old_cluster_sum_v.begin(), + old_cluster_sum_v.end(), + weight_t{0.0}); + + thrust::for_each(rmm::exec_policy(handle_.get_stream_view()), thrust::make_counting_iterator(0), thrust::make_counting_iterator(graph.number_of_edges), - [d_src_indices, - d_dst_indices, + [d_src_indices = src_indices_v_.data(), + d_dst_indices = graph.indices, d_cluster, d_offsets, d_cluster_hash, @@ -349,13 +376,13 @@ class Louvain { }); thrust::for_each( - rmm::exec_policy(stream_)->on(stream_), + rmm::exec_policy(handle_.get_stream_view()), thrust::make_counting_iterator(0), thrust::make_counting_iterator(graph.number_of_edges), [total_edge_weight, resolution, d_cluster_hash, - d_src_indices, + d_src_indices = src_indices_v_.data(), d_cluster, d_vertex_weights, d_delta_Q, @@ -382,34 +409,41 @@ class Louvain { }); } - void assign_nodes(graph_type const &graph, - rmm::device_vector &cluster_hash_v, - rmm::device_vector &next_cluster_v, - rmm::device_vector &delta_Q_v, + void assign_nodes(graph_type const& graph, + rmm::device_uvector& cluster_hash_v, + rmm::device_uvector& next_cluster_v, + rmm::device_uvector& delta_Q_v, bool up_down) { - rmm::device_vector temp_vertices_v(graph.number_of_vertices); - rmm::device_vector temp_cluster_v(graph.number_of_vertices, vertex_t{-1}); - rmm::device_vector temp_delta_Q_v(graph.number_of_vertices, weight_t{0.0}); - - weight_t *d_delta_Q = delta_Q_v.data().get(); - vertex_t *d_next_cluster = next_cluster_v.data().get(); - vertex_t *d_cluster_hash = cluster_hash_v.data().get(); - weight_t const *d_vertex_weights = vertex_weights_v_.data().get(); - weight_t *d_cluster_weights = cluster_weights_v_.data().get(); + rmm::device_uvector temp_vertices_v(graph.number_of_vertices, + handle_.get_stream_view()); + rmm::device_uvector temp_cluster_v(graph.number_of_vertices, + handle_.get_stream_view()); + rmm::device_uvector temp_delta_Q_v(graph.number_of_vertices, + handle_.get_stream_view()); + + thrust::fill(rmm::exec_policy(handle_.get_stream_view()), + temp_cluster_v.begin(), + temp_cluster_v.end(), + vertex_t{-1}); + + thrust::fill(rmm::exec_policy(handle_.get_stream_view()), + temp_delta_Q_v.begin(), + temp_delta_Q_v.end(), + weight_t{0}); auto cluster_reduce_iterator = - thrust::make_zip_iterator(thrust::make_tuple(d_cluster_hash, d_delta_Q)); + thrust::make_zip_iterator(thrust::make_tuple(cluster_hash_v.begin(), delta_Q_v.begin())); - auto output_edge_iterator2 = thrust::make_zip_iterator( - thrust::make_tuple(temp_cluster_v.data().get(), temp_delta_Q_v.data().get())); + auto output_edge_iterator2 = + thrust::make_zip_iterator(thrust::make_tuple(temp_cluster_v.begin(), temp_delta_Q_v.begin())); auto cluster_reduce_end = - thrust::reduce_by_key(rmm::exec_policy(stream_)->on(stream_), + thrust::reduce_by_key(rmm::exec_policy(handle_.get_stream_view()), src_indices_v_.begin(), src_indices_v_.end(), cluster_reduce_iterator, - temp_vertices_v.data().get(), + temp_vertices_v.data(), output_edge_iterator2, thrust::equal_to(), [] __device__(auto pair1, auto pair2) { @@ -422,22 +456,18 @@ class Louvain { return pair2; }); - vertex_t final_size = thrust::distance(temp_vertices_v.data().get(), cluster_reduce_end.first); - - vertex_t *d_temp_vertices = temp_vertices_v.data().get(); - vertex_t *d_temp_clusters = temp_cluster_v.data().get(); - weight_t *d_temp_delta_Q = temp_delta_Q_v.data().get(); + vertex_t final_size = thrust::distance(temp_vertices_v.data(), cluster_reduce_end.first); - thrust::for_each(rmm::exec_policy(stream_)->on(stream_), + thrust::for_each(rmm::exec_policy(handle_.get_stream_view()), thrust::make_counting_iterator(0), thrust::make_counting_iterator(final_size), - [d_temp_delta_Q, - up_down, - d_next_cluster, - d_temp_vertices, - d_vertex_weights, - d_temp_clusters, - d_cluster_weights] __device__(vertex_t id) { + [up_down, + d_temp_delta_Q = temp_delta_Q_v.data(), + d_next_cluster = next_cluster_v.data(), + d_temp_vertices = temp_vertices_v.data(), + d_vertex_weights = vertex_weights_v_.data(), + d_temp_clusters = temp_cluster_v.data(), + d_cluster_weights = cluster_weights_v_.data()] __device__(vertex_t id) { if ((d_temp_clusters[id] >= 0) && (d_temp_delta_Q[id] > weight_t{0.0})) { vertex_t new_cluster = d_temp_clusters[id]; vertex_t old_cluster = d_next_cluster[d_temp_vertices[id]]; @@ -453,187 +483,176 @@ class Louvain { }); } - void shrink_graph(graph_t &graph, vertex_t *d_cluster_vec) + void shrink_graph(graph_t& graph) { timer_start("shrinking graph"); // renumber the clusters to the range 0..(num_clusters-1) - vertex_t num_clusters = renumber_clusters(d_cluster_vec); - cluster_weights_v_.resize(num_clusters); + vertex_t num_clusters = renumber_clusters(); + cluster_weights_v_.resize(num_clusters, handle_.get_stream_view()); // shrink our graph to represent the graph of supervertices generate_superverticies_graph(graph, num_clusters); - // assign each new vertex to its own cluster - thrust::sequence(rmm::exec_policy(stream_)->on(stream_), cluster_v_.begin(), cluster_v_.end()); - - timer_stop(stream_); + timer_stop(handle_.get_stream_view()); } - vertex_t renumber_clusters(vertex_t *d_cluster_vec) + vertex_t renumber_clusters() { - vertex_t *d_tmp_array = tmp_arr_v_.data().get(); - vertex_t *d_cluster_inverse = cluster_inverse_v_.data().get(); - vertex_t *d_cluster = cluster_v_.data().get(); + vertex_t* d_tmp_array = tmp_arr_v_.data(); + vertex_t* d_cluster_inverse = cluster_inverse_v_.data(); + vertex_t* d_cluster = dendrogram_->current_level_begin(); - vertex_t old_num_clusters = cluster_v_.size(); + vertex_t old_num_clusters = dendrogram_->current_level_size(); // // New technique. Initialize cluster_inverse_v_ to 0 // - thrust::fill(cluster_inverse_v_.begin(), cluster_inverse_v_.end(), vertex_t{0}); + thrust::fill(rmm::exec_policy(handle_.get_stream_view()), + cluster_inverse_v_.begin(), + cluster_inverse_v_.end(), + vertex_t{0}); // - // Iterate over every element c in cluster_v_ and set cluster_inverse_v to 1 + // Iterate over every element c in the current clustering and set cluster_inverse_v to 1 // auto first_1 = thrust::make_constant_iterator(1); auto last_1 = first_1 + old_num_clusters; - thrust::scatter(rmm::exec_policy(stream_)->on(stream_), + thrust::scatter(rmm::exec_policy(handle_.get_stream_view()), first_1, last_1, - cluster_v_.begin(), + dendrogram_->current_level_begin(), cluster_inverse_v_.begin()); // // Now we'll copy all of the clusters that have a value of 1 into a temporary array // auto copy_end = thrust::copy_if( - rmm::exec_policy(stream_)->on(stream_), + rmm::exec_policy(handle_.get_stream_view()), thrust::make_counting_iterator(0), thrust::make_counting_iterator(old_num_clusters), tmp_arr_v_.begin(), [d_cluster_inverse] __device__(const vertex_t idx) { return d_cluster_inverse[idx] == 1; }); vertex_t new_num_clusters = thrust::distance(tmp_arr_v_.begin(), copy_end); - tmp_arr_v_.resize(new_num_clusters); + tmp_arr_v_.resize(new_num_clusters, handle_.get_stream_view()); // // Now we can set each value in cluster_inverse of a cluster to its index // - thrust::for_each(rmm::exec_policy(stream_)->on(stream_), + thrust::for_each(rmm::exec_policy(handle_.get_stream_view()), thrust::make_counting_iterator(0), thrust::make_counting_iterator(new_num_clusters), [d_cluster_inverse, d_tmp_array] __device__(const vertex_t idx) { d_cluster_inverse[d_tmp_array[idx]] = idx; }); - thrust::for_each(rmm::exec_policy(stream_)->on(stream_), + thrust::for_each(rmm::exec_policy(handle_.get_stream_view()), thrust::make_counting_iterator(0), thrust::make_counting_iterator(old_num_clusters), [d_cluster, d_cluster_inverse] __device__(vertex_t i) { d_cluster[i] = d_cluster_inverse[d_cluster[i]]; }); - thrust::for_each(rmm::exec_policy(stream_)->on(stream_), - thrust::make_counting_iterator(0), - thrust::make_counting_iterator(number_of_vertices_), - [d_cluster_vec, d_cluster] __device__(vertex_t i) { - d_cluster_vec[i] = d_cluster[d_cluster_vec[i]]; - }); - - cluster_inverse_v_.resize(new_num_clusters); - cluster_v_.resize(new_num_clusters); + cluster_inverse_v_.resize(new_num_clusters, handle_.get_stream_view()); return new_num_clusters; } - void generate_superverticies_graph(graph_t &graph, vertex_t num_clusters) + void generate_superverticies_graph(graph_t& graph, vertex_t num_clusters) { - rmm::device_vector new_src_v(graph.number_of_edges); - rmm::device_vector new_dst_v(graph.number_of_edges); - rmm::device_vector new_weight_v(graph.number_of_edges); - - vertex_t *d_old_src = src_indices_v_.data().get(); - vertex_t *d_old_dst = graph.indices; - weight_t *d_old_weight = graph.edge_data; - vertex_t *d_new_src = new_src_v.data().get(); - vertex_t *d_new_dst = new_dst_v.data().get(); - vertex_t *d_clusters = cluster_v_.data().get(); - weight_t *d_new_weight = new_weight_v.data().get(); + rmm::device_uvector new_src_v(graph.number_of_edges, handle_.get_stream_view()); + rmm::device_uvector new_dst_v(graph.number_of_edges, handle_.get_stream_view()); + rmm::device_uvector new_weight_v(graph.number_of_edges, handle_.get_stream_view()); // // Renumber the COO // - thrust::for_each(rmm::exec_policy(stream_)->on(stream_), + thrust::for_each(rmm::exec_policy(handle_.get_stream_view()), thrust::make_counting_iterator(0), thrust::make_counting_iterator(graph.number_of_edges), - [d_old_src, - d_old_dst, - d_old_weight, - d_new_src, - d_new_dst, - d_new_weight, - d_clusters] __device__(edge_t e) { + [d_old_src = src_indices_v_.data(), + d_old_dst = graph.indices, + d_old_weight = graph.edge_data, + d_new_src = new_src_v.data(), + d_new_dst = new_dst_v.data(), + d_new_weight = new_weight_v.data(), + d_clusters = dendrogram_->current_level_begin()] __device__(edge_t e) { d_new_src[e] = d_clusters[d_old_src[e]]; d_new_dst[e] = d_clusters[d_old_dst[e]]; d_new_weight[e] = d_old_weight[e]; }); thrust::stable_sort_by_key( - rmm::exec_policy(stream_)->on(stream_), - d_new_dst, - d_new_dst + graph.number_of_edges, - thrust::make_zip_iterator(thrust::make_tuple(d_new_src, d_new_weight))); + rmm::exec_policy(handle_.get_stream_view()), + new_dst_v.begin(), + new_dst_v.end(), + thrust::make_zip_iterator(thrust::make_tuple(new_src_v.begin(), new_weight_v.begin()))); thrust::stable_sort_by_key( - rmm::exec_policy(stream_)->on(stream_), - d_new_src, - d_new_src + graph.number_of_edges, - thrust::make_zip_iterator(thrust::make_tuple(d_new_dst, d_new_weight))); + rmm::exec_policy(handle_.get_stream_view()), + new_src_v.begin(), + new_src_v.end(), + thrust::make_zip_iterator(thrust::make_tuple(new_dst_v.begin(), new_weight_v.begin()))); // // Now we reduce by key to combine the weights of duplicate // edges. // - auto start = thrust::make_zip_iterator(thrust::make_tuple(d_new_src, d_new_dst)); - auto new_start = thrust::make_zip_iterator(thrust::make_tuple(d_old_src, d_old_dst)); - auto new_end = thrust::reduce_by_key(rmm::exec_policy(stream_)->on(stream_), + auto start = + thrust::make_zip_iterator(thrust::make_tuple(new_src_v.begin(), new_dst_v.begin())); + auto new_start = + thrust::make_zip_iterator(thrust::make_tuple(src_indices_v_.data(), graph.indices)); + auto new_end = thrust::reduce_by_key(rmm::exec_policy(handle_.get_stream_view()), start, start + graph.number_of_edges, - d_new_weight, + new_weight_v.begin(), new_start, - d_old_weight, + graph.edge_data, thrust::equal_to>(), thrust::plus()); graph.number_of_edges = thrust::distance(new_start, new_end.first); graph.number_of_vertices = num_clusters; - detail::fill_offset(d_old_src, graph.offsets, num_clusters, graph.number_of_edges, stream_); - CHECK_CUDA(stream_); + detail::fill_offset(src_indices_v_.data(), + graph.offsets, + num_clusters, + graph.number_of_edges, + handle_.get_stream_view()); - src_indices_v_.resize(graph.number_of_edges); - indices_v_.resize(graph.number_of_edges); - weights_v_.resize(graph.number_of_edges); + src_indices_v_.resize(graph.number_of_edges, handle_.get_stream_view()); + indices_v_.resize(graph.number_of_edges, handle_.get_stream_view()); + weights_v_.resize(graph.number_of_edges, handle_.get_stream_view()); } protected: - raft::handle_t const &handle_; + raft::handle_t const& handle_; vertex_t number_of_vertices_; edge_t number_of_edges_; - cudaStream_t stream_; + + std::unique_ptr> dendrogram_; // // Copy of graph // - rmm::device_vector offsets_v_; - rmm::device_vector indices_v_; - rmm::device_vector weights_v_; - rmm::device_vector src_indices_v_; + rmm::device_uvector offsets_v_; + rmm::device_uvector indices_v_; + rmm::device_uvector weights_v_; + rmm::device_uvector src_indices_v_; // // Weights and clustering across iterations of algorithm // - rmm::device_vector vertex_weights_v_; - rmm::device_vector cluster_weights_v_; - rmm::device_vector cluster_v_; + rmm::device_uvector vertex_weights_v_; + rmm::device_uvector cluster_weights_v_; // // Temporaries used within kernels. Each iteration uses less // of this memory // - rmm::device_vector tmp_arr_v_; - rmm::device_vector cluster_inverse_v_; + rmm::device_uvector tmp_arr_v_; + rmm::device_uvector cluster_inverse_v_; #ifdef TIMING HighResTimer hr_timer_; diff --git a/cpp/src/community/spectral_clustering.cu b/cpp/src/community/spectral_clustering.cu index f32739ddf29..4dd27a56b70 100644 --- a/cpp/src/community/spectral_clustering.cu +++ b/cpp/src/community/spectral_clustering.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -20,14 +20,14 @@ * @file spectral_clustering.cu * ---------------------------------------------------------------------------**/ -#include +#include #include #include #include -#include -#include +#include +#include #include #include @@ -39,16 +39,16 @@ namespace ext_raft { namespace detail { template -void balancedCutClustering_impl(GraphCSRView const &graph, +void balancedCutClustering_impl(legacy::GraphCSRView const& graph, vertex_t n_clusters, vertex_t n_eig_vects, weight_t evs_tolerance, int evs_max_iter, weight_t kmean_tolerance, int kmean_max_iter, - vertex_t *clustering, - weight_t *eig_vals, - weight_t *eig_vects) + vertex_t* clustering, + weight_t* eig_vals, + weight_t* eig_vects) { RAFT_EXPECTS(graph.edge_data != nullptr, "API error, graph must have weights"); RAFT_EXPECTS(evs_tolerance >= weight_t{0.0}, @@ -109,16 +109,17 @@ void balancedCutClustering_impl(GraphCSRView const & } template -void spectralModularityMaximization_impl(GraphCSRView const &graph, - vertex_t n_clusters, - vertex_t n_eig_vects, - weight_t evs_tolerance, - int evs_max_iter, - weight_t kmean_tolerance, - int kmean_max_iter, - vertex_t *clustering, - weight_t *eig_vals, - weight_t *eig_vects) +void spectralModularityMaximization_impl( + legacy::GraphCSRView const& graph, + vertex_t n_clusters, + vertex_t n_eig_vects, + weight_t evs_tolerance, + int evs_max_iter, + weight_t kmean_tolerance, + int kmean_max_iter, + vertex_t* clustering, + weight_t* eig_vals, + weight_t* eig_vects) { RAFT_EXPECTS(graph.edge_data != nullptr, "API error, graph must have weights"); RAFT_EXPECTS(evs_tolerance >= weight_t{0.0}, @@ -186,10 +187,10 @@ void spectralModularityMaximization_impl(GraphCSRView -void analyzeModularityClustering_impl(GraphCSRView const &graph, +void analyzeModularityClustering_impl(legacy::GraphCSRView const& graph, int n_clusters, - vertex_t const *clustering, - weight_t *modularity) + vertex_t const* clustering, + weight_t* modularity) { raft::handle_t handle; auto stream = handle.get_stream(); @@ -207,11 +208,11 @@ void analyzeModularityClustering_impl(GraphCSRView c } template -void analyzeBalancedCut_impl(GraphCSRView const &graph, +void analyzeBalancedCut_impl(legacy::GraphCSRView const& graph, vertex_t n_clusters, - vertex_t const *clustering, - weight_t *edgeCut, - weight_t *ratioCut) + vertex_t const* clustering, + weight_t* edgeCut, + weight_t* ratioCut) { raft::handle_t handle; auto stream = handle.get_stream(); @@ -240,14 +241,14 @@ void analyzeBalancedCut_impl(GraphCSRView const &gra } // namespace detail template -void balancedCutClustering(GraphCSRView const &graph, +void balancedCutClustering(legacy::GraphCSRView const& graph, VT num_clusters, VT num_eigen_vects, WT evs_tolerance, int evs_max_iter, WT kmean_tolerance, int kmean_max_iter, - VT *clustering) + VT* clustering) { rmm::device_vector eig_vals(num_eigen_vects); rmm::device_vector eig_vects(num_eigen_vects * graph.number_of_vertices); @@ -265,14 +266,14 @@ void balancedCutClustering(GraphCSRView const &graph, } template -void spectralModularityMaximization(GraphCSRView const &graph, +void spectralModularityMaximization(legacy::GraphCSRView const& graph, VT n_clusters, VT n_eigen_vects, WT evs_tolerance, int evs_max_iter, WT kmean_tolerance, int kmean_max_iter, - VT *clustering) + VT* clustering) { rmm::device_vector eig_vals(n_eigen_vects); rmm::device_vector eig_vects(n_eigen_vects * graph.number_of_vertices); @@ -290,66 +291,54 @@ void spectralModularityMaximization(GraphCSRView const &graph, } template -void analyzeClustering_modularity(GraphCSRView const &graph, +void analyzeClustering_modularity(legacy::GraphCSRView const& graph, int n_clusters, - VT const *clustering, - WT *score) + VT const* clustering, + WT* score) { detail::analyzeModularityClustering_impl(graph, n_clusters, clustering, score); } template -void analyzeClustering_edge_cut(GraphCSRView const &graph, +void analyzeClustering_edge_cut(legacy::GraphCSRView const& graph, int n_clusters, - VT const *clustering, - WT *score) + VT const* clustering, + WT* score) { WT dummy{0.0}; detail::analyzeBalancedCut_impl(graph, n_clusters, clustering, score, &dummy); } template -void analyzeClustering_ratio_cut(GraphCSRView const &graph, +void analyzeClustering_ratio_cut(legacy::GraphCSRView const& graph, int n_clusters, - VT const *clustering, - WT *score) + VT const* clustering, + WT* score) { WT dummy{0.0}; detail::analyzeBalancedCut_impl(graph, n_clusters, clustering, &dummy, score); } template void balancedCutClustering( - GraphCSRView const &, int, int, float, int, float, int, int *); + legacy::GraphCSRView const&, int, int, float, int, float, int, int*); template void balancedCutClustering( - GraphCSRView const &, int, int, double, int, double, int, int *); + legacy::GraphCSRView const&, int, int, double, int, double, int, int*); template void spectralModularityMaximization( - GraphCSRView const &, int, int, float, int, float, int, int *); + legacy::GraphCSRView const&, int, int, float, int, float, int, int*); template void spectralModularityMaximization( - GraphCSRView const &, int, int, double, int, double, int, int *); -template void analyzeClustering_modularity(GraphCSRView const &, - int, - int const *, - float *); -template void analyzeClustering_modularity(GraphCSRView const &, - int, - int const *, - double *); -template void analyzeClustering_edge_cut(GraphCSRView const &, - int, - int const *, - float *); -template void analyzeClustering_edge_cut(GraphCSRView const &, - int, - int const *, - double *); -template void analyzeClustering_ratio_cut(GraphCSRView const &, - int, - int const *, - float *); -template void analyzeClustering_ratio_cut(GraphCSRView const &, - int, - int const *, - double *); + legacy::GraphCSRView const&, int, int, double, int, double, int, int*); +template void analyzeClustering_modularity( + legacy::GraphCSRView const&, int, int const*, float*); +template void analyzeClustering_modularity( + legacy::GraphCSRView const&, int, int const*, double*); +template void analyzeClustering_edge_cut( + legacy::GraphCSRView const&, int, int const*, float*); +template void analyzeClustering_edge_cut( + legacy::GraphCSRView const&, int, int const*, double*); +template void analyzeClustering_ratio_cut( + legacy::GraphCSRView const&, int, int const*, float*); +template void analyzeClustering_ratio_cut( + legacy::GraphCSRView const&, int, int const*, double*); } // namespace ext_raft } // namespace cugraph diff --git a/cpp/src/community/triangles_counting.cu b/cpp/src/community/triangles_counting.cu index f6670365652..97543d28c62 100644 --- a/cpp/src/community/triangles_counting.cu +++ b/cpp/src/community/triangles_counting.cu @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. @@ -17,10 +17,10 @@ #include #include -#include -#include +#include +#include -#include +#include #include #include @@ -31,8 +31,8 @@ #include "cub/cub.cuh" #define TH_CENT_K_LOCLEN (34) -#define WP_LEN_TH1 (24) -#define WP_LEN_TH2 (2) +#define WP_LEN_TH1 (24) +#define WP_LEN_TH2 (2) #if WP_LEN_TH1 > 32 #error WP_LEN_TH1 must be <= 32! @@ -41,9 +41,9 @@ #define MIN(x, y) (((x) < (y)) ? (x) : (y)) #define MAX(x, y) (((x) > (y)) ? (x) : (y)) -#define THREADS (128) +#define THREADS (128) #define DIV_UP(a, b) (((a) + ((b)-1)) / (b)) -#define BITSOF(x) (sizeof(*x) * 8) +#define BITSOF(x) (sizeof(*x) * 8) #define BLK_BWL0 (128) @@ -73,9 +73,9 @@ struct spmat_t { T N; T nnz; T nrows; - const T *roff_d; - const T *rows_d; - const T *cols_d; + const T* roff_d; + const T* rows_d; + const T* cols_d; bool is_lower_triangular; }; @@ -173,7 +173,9 @@ __device__ __forceinline__ T block_sum(T v) const int wid = threadIdx.x / 32 + ((BDIM_Y > 1) ? threadIdx.y * (BDIM_X / 32) : 0); #pragma unroll - for (int i = WSIZE / 2; i; i >>= 1) { v += __shfl_down_sync(raft::warp_full_mask(), v, i); } + for (int i = WSIZE / 2; i; i >>= 1) { + v += __shfl_down_sync(raft::warp_full_mask(), v, i); + } if (lid == 0) sh[wid] = v; __syncthreads(); @@ -197,13 +199,13 @@ template __global__ void tricnt_b2b_k(const ROW_T ner, - const ROW_T *__restrict__ rows, - const OFF_T *__restrict__ roff, - const ROW_T *__restrict__ cols, - CNT_T *__restrict__ ocnt, - MAP_T *__restrict__ bmapL0, + const ROW_T* __restrict__ rows, + const OFF_T* __restrict__ roff, + const ROW_T* __restrict__ cols, + CNT_T* __restrict__ ocnt, + MAP_T* __restrict__ bmapL0, const size_t bmldL0, - MAP_T *__restrict__ bmapL1, + MAP_T* __restrict__ bmapL1, const size_t bmldL1) { CNT_T __cnt = 0; @@ -277,11 +279,11 @@ __global__ void tricnt_b2b_k(const ROW_T ner, template void tricnt_b2b(T nblock, - spmat_t *m, - uint64_t *ocnt_d, - unsigned int *bmapL0_d, + spmat_t* m, + uint64_t* ocnt_d, + unsigned int* bmapL0_d, size_t bmldL0, - unsigned int *bmapL1_d, + unsigned int* bmapL1_d, size_t bmldL1, cudaStream_t stream) { @@ -294,13 +296,15 @@ void tricnt_b2b(T nblock, ////////////////////////////////////////////////////////////////////////////////////////// template -__device__ __forceinline__ T block_sum_sh(T v, T *sh) +__device__ __forceinline__ T block_sum_sh(T v, T* sh) { const int lid = threadIdx.x % 32; const int wid = threadIdx.x / 32 + ((BDIM_Y > 1) ? threadIdx.y * (BDIM_X / 32) : 0); #pragma unroll - for (int i = WSIZE / 2; i; i >>= 1) { v += __shfl_down_sync(raft::warp_full_mask(), v, i); } + for (int i = WSIZE / 2; i; i >>= 1) { + v += __shfl_down_sync(raft::warp_full_mask(), v, i); + } if (lid == 0) sh[wid] = v; __syncthreads(); @@ -317,10 +321,10 @@ __device__ __forceinline__ T block_sum_sh(T v, T *sh) template __global__ void tricnt_bsh_k(const ROW_T ner, - const ROW_T *__restrict__ rows, - const OFF_T *__restrict__ roff, - const ROW_T *__restrict__ cols, - CNT_T *__restrict__ ocnt, + const ROW_T* __restrict__ rows, + const OFF_T* __restrict__ roff, + const ROW_T* __restrict__ cols, + CNT_T* __restrict__ ocnt, const size_t bmld) { CNT_T __cnt = 0; @@ -374,7 +378,7 @@ __global__ void tricnt_bsh_k(const ROW_T ner, __syncthreads(); if (lastcol - firstcol < rend - rbeg) { for (int i = firstcol; i <= lastcol; i += BDIM) { - if (i + threadIdx.x <= lastcol) { ((unsigned long long *)shm)[i + threadIdx.x] = 0ull; } + if (i + threadIdx.x <= lastcol) { ((unsigned long long*)shm)[i + threadIdx.x] = 0ull; } } } else { for (int i = rbeg; i < rend; i += BDIM) { @@ -383,14 +387,14 @@ __global__ void tricnt_bsh_k(const ROW_T ner, } __syncthreads(); } - __cnt = block_sum_sh(__cnt, (uint64_t *)shm); + __cnt = block_sum_sh(__cnt, (uint64_t*)shm); if (threadIdx.x == 0) ocnt[blockIdx.x] = __cnt; return; } template -void tricnt_bsh(T nblock, spmat_t *m, uint64_t *ocnt_d, size_t bmld, cudaStream_t stream) +void tricnt_bsh(T nblock, spmat_t* m, uint64_t* ocnt_d, size_t bmld, cudaStream_t stream) { tricnt_bsh_k<<>>( m->nrows, m->rows_d, m->roff_d, m->cols_d, ocnt_d, bmld); @@ -408,11 +412,11 @@ template __global__ void tricnt_wrp_ps_k(const ROW_T ner, - const ROW_T *__restrict__ rows, - const OFF_T *__restrict__ roff, - const ROW_T *__restrict__ cols, - CNT_T *__restrict__ ocnt, - MAP_T *__restrict__ bmap, + const ROW_T* __restrict__ rows, + const OFF_T* __restrict__ roff, + const ROW_T* __restrict__ cols, + CNT_T* __restrict__ ocnt, + MAP_T* __restrict__ bmap, const size_t bmld) { __shared__ OFF_T sho[NWARP][WSIZE]; @@ -520,7 +524,7 @@ __global__ void tricnt_wrp_ps_k(const ROW_T ner, if (lastcol - firstcol < rend - rbeg) { for (int i = firstcol; i <= lastcol; i += WSIZE) { - if (i + threadIdx.x <= lastcol) { ((unsigned long long *)bmap)[i + threadIdx.x] = 0ull; } + if (i + threadIdx.x <= lastcol) { ((unsigned long long*)bmap)[i + threadIdx.x] = 0ull; } } } else { for (int i = rbeg; i < rend; i += WSIZE) { @@ -537,7 +541,7 @@ __global__ void tricnt_wrp_ps_k(const ROW_T ner, template void tricnt_wrp( - T nblock, spmat_t *m, uint64_t *ocnt_d, unsigned int *bmap_d, size_t bmld, cudaStream_t stream) + T nblock, spmat_t* m, uint64_t* ocnt_d, unsigned int* bmap_d, size_t bmld, cudaStream_t stream) { dim3 block(32, THREADS / 32); tricnt_wrp_ps_k<32, THREADS / 32, WP_LEN_TH1, WP_LEN_TH2> @@ -549,10 +553,10 @@ void tricnt_wrp( ////////////////////////////////////////////////////////////////////////////////////////// template __global__ void tricnt_thr_k(const ROW_T ner, - const ROW_T *__restrict__ rows, - const OFF_T *__restrict__ roff, - const ROW_T *__restrict__ cols, - CNT_T *__restrict__ ocnt) + const ROW_T* __restrict__ rows, + const OFF_T* __restrict__ roff, + const ROW_T* __restrict__ cols, + CNT_T* __restrict__ ocnt) { CNT_T __cnt = 0; const ROW_T tid = blockIdx.x * BDIM + threadIdx.x; @@ -619,7 +623,7 @@ __global__ void tricnt_thr_k(const ROW_T ner, } template -void tricnt_thr(T nblock, spmat_t *m, uint64_t *ocnt_d, cudaStream_t stream) +void tricnt_thr(T nblock, spmat_t* m, uint64_t* ocnt_d, cudaStream_t stream) { cudaFuncSetCacheConfig(tricnt_thr_k *m, uint64_t *ocnt_d, cudaStream_t stream) ///////////////////////////////////////////////////////////////// template struct NonEmptyRow { - const IndexType *p_roff; - __host__ __device__ NonEmptyRow(const IndexType *roff) : p_roff(roff) {} - __host__ __device__ __forceinline__ bool operator()(const IndexType &a) const + const IndexType* p_roff; + __host__ __device__ NonEmptyRow(const IndexType* roff) : p_roff(roff) {} + __host__ __device__ __forceinline__ bool operator()(const IndexType& a) const { return (p_roff[a] < p_roff[a + 1]); } @@ -647,7 +651,7 @@ struct NonEmptyRow { template void create_nondangling_vector( - const T *roff, T *p_nonempty, T *n_nonempty, size_t n, cudaStream_t stream) + const T* roff, T* p_nonempty, T* n_nonempty, size_t n, cudaStream_t stream) { if (n <= 0) return; thrust::counting_iterator it(0); @@ -660,7 +664,7 @@ void create_nondangling_vector( } template -uint64_t reduce(uint64_t *v_d, T n, cudaStream_t stream) +uint64_t reduce(uint64_t* v_d, T n, cudaStream_t stream) { rmm::device_vector tmp(1); @@ -694,8 +698,8 @@ class TrianglesCount { // Simple constructor TrianglesCount(IndexType num_vertices, IndexType num_edges, - IndexType const *row_offsets, - IndexType const *col_indices, + IndexType const* row_offsets, + IndexType const* col_indices, cudaStream_t stream = NULL); void count(); @@ -705,8 +709,8 @@ class TrianglesCount { template TrianglesCount::TrianglesCount(IndexType num_vertices, IndexType num_edges, - IndexType const *row_offsets, - IndexType const *col_indices, + IndexType const* row_offsets, + IndexType const* col_indices, cudaStream_t stream) : m_mat{num_vertices, num_edges, num_vertices, row_offsets, nullptr, col_indices}, m_stream{stream}, @@ -759,8 +763,9 @@ void TrianglesCount::tcount_b2b() cudaMemGetInfo(&free_bytes, &total_bytes); CHECK_CUDA(m_stream); - int nblock = (free_bytes * 95 / 100) / (sizeof(uint32_t) * bmldL1); //@TODO: what? - nblock = MIN(nblock, m_mat.nrows); + size_t nblock_available = (free_bytes * 95 / 100) / (sizeof(uint32_t) * bmldL1); + + int nblock = static_cast(MIN(nblock_available, static_cast(m_mat.nrows))); // allocate level 1 bitmap rmm::device_vector bmapL1_d(bmldL1 * nblock, uint32_t{0}); @@ -793,8 +798,10 @@ void TrianglesCount::tcount_wrp() cudaMemGetInfo(&free_bytes, &total_bytes); CHECK_CUDA(m_stream); - int nblock = (free_bytes * 95 / 100) / (sizeof(uint32_t) * bmld * (THREADS / 32)); - nblock = MIN(nblock, DIV_UP(m_mat.nrows, (THREADS / 32))); + size_t nblock_available = (free_bytes * 95 / 100) / (sizeof(uint32_t) * bmld * (THREADS / 32)); + + int nblock = static_cast( + MIN(nblock_available, static_cast(DIV_UP(m_mat.nrows, (THREADS / 32))))); size_t bmap_sz = bmld * nblock * (THREADS / 32); @@ -827,7 +834,8 @@ void TrianglesCount::count() tcount_wrp(); else { const int shMinBlkXSM = 6; - if (size_t{m_shared_mem_per_block * 8 / shMinBlkXSM} < (size_t)m_mat.N) + if (static_cast(m_shared_mem_per_block * 8 / shMinBlkXSM) < + static_cast(m_mat.N)) tcount_b2b(); else tcount_bsh(); @@ -837,7 +845,7 @@ void TrianglesCount::count() } // namespace template -uint64_t triangle_count(GraphCSRView const &graph) +uint64_t triangle_count(legacy::GraphCSRView const& graph) { TrianglesCount counter( graph.number_of_vertices, graph.number_of_edges, graph.offsets, graph.indices); @@ -847,7 +855,7 @@ uint64_t triangle_count(GraphCSRView const &graph) } template uint64_t triangle_count( - GraphCSRView const &); + legacy::GraphCSRView const&); } // namespace triangle } // namespace cugraph diff --git a/cpp/src/components/connectivity.cu b/cpp/src/components/connectivity.cu index 2cc1da017a9..85134c1ad67 100644 --- a/cpp/src/components/connectivity.cu +++ b/cpp/src/components/connectivity.cu @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. @@ -19,13 +19,13 @@ #include -#include #include -#include +#include +#include +#include #include #include -#include "utilities/error.hpp" -#include "utilities/graph_utils.cuh" +#include #include "topology/topology.cuh" @@ -57,15 +57,15 @@ namespace detail { */ template std::enable_if_t::value> connected_components_impl( - GraphCSRView const &graph, + legacy::GraphCSRView const& graph, cugraph_cc_t connectivity_type, - VT *labels, + VT* labels, cudaStream_t stream) { using ByteT = unsigned char; // minimum addressable unit - CUGRAPH_EXPECTS(graph.offsets != nullptr, "Invalid API parameter: graph.offsets is nullptr"); - CUGRAPH_EXPECTS(graph.indices != nullptr, "Invalid API parameter: graph.indices is nullptr"); + CUGRAPH_EXPECTS(graph.offsets != nullptr, "Invalid input argument: graph.offsets is nullptr"); + CUGRAPH_EXPECTS(graph.indices != nullptr, "Invalid input argument: graph.indices is nullptr"); VT nrows = graph.number_of_vertices; @@ -78,26 +78,26 @@ std::enable_if_t::value> connected_components_impl( stream); } else { SCC_Data sccd(nrows, graph.offsets, graph.indices); - sccd.run_scc(labels); + auto num_iters = sccd.run_scc(labels); } } } // namespace detail template -void connected_components(GraphCSRView const &graph, +void connected_components(legacy::GraphCSRView const& graph, cugraph_cc_t connectivity_type, - VT *labels) + VT* labels) { cudaStream_t stream{nullptr}; - CUGRAPH_EXPECTS(labels != nullptr, "Invalid API parameter: labels parameter is NULL"); + CUGRAPH_EXPECTS(labels != nullptr, "Invalid input argument: labels parameter is NULL"); return detail::connected_components_impl(graph, connectivity_type, labels, stream); } template void connected_components( - GraphCSRView const &, cugraph_cc_t, int32_t *); + legacy::GraphCSRView const&, cugraph_cc_t, int32_t*); template void connected_components( - GraphCSRView const &, cugraph_cc_t, int64_t *); + legacy::GraphCSRView const&, cugraph_cc_t, int64_t*); } // namespace cugraph diff --git a/cpp/src/components/scc_matrix.cuh b/cpp/src/components/scc_matrix.cuh index 801f1fe0fad..c7f4506b74e 100644 --- a/cpp/src/components/scc_matrix.cuh +++ b/cpp/src/components/scc_matrix.cuh @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. @@ -71,12 +71,13 @@ struct SCC_Data { p_d_r_o_(p_d_r_o), p_d_c_i_(p_d_c_i), d_C(nrows * nrows, 0), - d_Cprev(nrows * nrows, 0) + d_Cprev(nrows * nrows, 0), + p_d_C_(d_C.data().get()) { init(); } - const thrust::device_vector& get_C(void) const { return d_C; } + ByteT const* get_Cptr(void) const { return p_d_C_; } size_t nrows(void) const { return nrows_; } @@ -100,13 +101,12 @@ struct SCC_Data { void get_labels(IndexT* d_labels) const { - auto* p_d_C = d_C.data().get(); - size_t n = nrows_; // for lambda capture, since I cannot capture `this` (host), or `nrows_` + size_t n = nrows_; // for lambda capture, since I cannot capture `this` (host), or `nrows_` thrust::transform(thrust::device, thrust::make_counting_iterator(0), thrust::make_counting_iterator(nrows_), d_labels, - [n, p_d_C] __device__(IndexT k) { + [n, p_d_C = p_d_C_] __device__(IndexT k) { auto begin = p_d_C + k * n; auto end = begin + n; ByteT one{1}; @@ -124,7 +124,6 @@ struct SCC_Data { size_t nrows = nrows_; size_t count = 0; - ByteT* p_d_C = d_C.data().get(); ByteT* p_d_Cprev = get_Cprev().data().get(); size_t n2 = nrows * nrows; @@ -136,57 +135,60 @@ struct SCC_Data { do { flag.set(0); - thrust::for_each(thrust::device, - thrust::make_counting_iterator(0), - thrust::make_counting_iterator(n2), - [nrows, p_d_C, p_d_Cprev, p_d_flag, p_d_ro, p_d_ci] __device__(size_t indx) { - ByteT one{1}; - - auto i = indx / nrows; - auto j = indx % nrows; - - if ((i == j) || (p_d_Cprev[indx] == one)) - p_d_C[indx] = one; - else { - // this is where a hash-map could help: - // only need hashmap[(i,j)]={0,1} (`1` for "hit"); - // and only for new entries! - // already existent entries are covered by - // the `if`-branch above! - // Hence, hashmap[] can use limited space: - // M = max_l{number(new `1` entries)}, where - // l = #iterations in the do-loop! - // M ~ new `1` entries between A^k and A^{k+1}, - // k=1,2,... - // Might M actually be M ~ nnz(A) = |E| ?! - // Probably, because the primitive hash - //(via find_if) uses a search space of nnz(A) - // - // But, what if more than 1 entry pops-up in a row? - // Not an issue! Because the hash key is (i,j), and no - // more than one entry can exist in position (i,j)! - // - // And remember, we only need to store the new (i,j) keys - // that an iteration produces wrt to the previous iteration! - // - auto begin = p_d_ci + p_d_ro[i]; - auto end = p_d_ci + p_d_ro[i + 1]; - auto pos = thrust::find_if( - thrust::seq, begin, end, [one, j, nrows, p_d_Cprev, p_d_ci](IndexT k) { - return (p_d_Cprev[k * nrows + j] == one); - }); - - if (pos != end) p_d_C[indx] = one; - } - - if (p_d_C[indx] != p_d_Cprev[indx]) - *p_d_flag = 1; // race-condition: harmless, worst case many threads - // write the same value - }); + thrust::for_each( + thrust::device, + thrust::make_counting_iterator(0), + thrust::make_counting_iterator(n2), + [nrows, p_d_C = p_d_C_, p_d_Cprev, p_d_flag, p_d_ro, p_d_ci] __device__(size_t indx) { + ByteT one{1}; + + auto i = indx / nrows; + auto j = indx % nrows; + + if ((i == j) || (p_d_Cprev[indx] == one)) { + p_d_C[indx] = one; + } else { + // this ammounts to A (^,v) B + // (where A = adjacency matrix defined by (p_ro, p_ci), + // B := p_d_Cprev; (^,v) := (*,+) semiring); + // Here's why: + // (A (^,v) B)[i][j] := A[i][.] (^,v) B[j][.] + // (where X[i][.] := i-th row of X; + // X[.][j] := j-th column of X); + // which is: + // 1, iff A[i][.] and B[j][.] have a 1 in the same location, + // 0, otherwise; + // + // i.e., corresponfing entry in p_d_C is 1 + // if B[k][j] == 1 for any column k in A's i-th row; + // hence, for each column k of row A[i][.], + // which is the set: + // k \in {p_ci + p_ro[i], ..., p_ci + p_ro[i+1] - 1}, + // check if (B[k][j] == 1), + // i.e., p_d_Cprev[k*nrows + j]) == 1: + // + auto begin = p_d_ci + p_d_ro[i]; + auto end = p_d_ci + p_d_ro[i + 1]; + auto pos = thrust::find_if( + thrust::seq, begin, end, [one, j, nrows, p_d_Cprev, p_d_ci](IndexT k) { + return (p_d_Cprev[k * nrows + j] == one); + }); + + if (pos != end) p_d_C[indx] = one; + } + + if (p_d_C[indx] != p_d_Cprev[indx]) + *p_d_flag = 1; // race-condition: harmless, + // worst case many threads + // write the _same_ value + }); ++count; cudaDeviceSynchronize(); - std::swap(p_d_C, p_d_Cprev); + std::swap(p_d_C_, p_d_Cprev); // Note 1: this swap makes `p_d_Cprev` the + // most recently updated matrix pointer + // at the end of this loop + // (see `Note 2` why this matters); } while (flag.is_set()); // C & Ct: @@ -196,11 +198,13 @@ struct SCC_Data { thrust::for_each(thrust::device, thrust::make_counting_iterator(0), thrust::make_counting_iterator(n2), - [nrows, p_d_C, p_d_Cprev] __device__(size_t indx) { + [nrows, p_d_C = p_d_C_, p_d_Cprev] __device__(size_t indx) { auto i = indx / nrows; auto j = indx % nrows; auto tindx = j * nrows + i; + // Note 2: per Note 1, p_d_Cprev is latest: + // p_d_C[indx] = (p_d_Cprev[indx]) & (p_d_Cprev[tindx]); }); @@ -215,6 +219,9 @@ struct SCC_Data { const IndexT* p_d_c_i_; // column indices thrust::device_vector d_C; thrust::device_vector d_Cprev; + ByteT* p_d_C_{nullptr}; // holds the most recent update, + // which can have storage in any of d_C or d_Cprev, + // because the pointers get swapped! thrust::device_vector& get_Cprev(void) { return d_Cprev; } }; diff --git a/cpp/src/components/utils.h b/cpp/src/components/utils.h index c9ebb6ac4d1..4e7dbe075da 100644 --- a/cpp/src/components/utils.h +++ b/cpp/src/components/utils.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. @@ -27,7 +27,7 @@ #include -#include +#include namespace MLCommon { @@ -73,7 +73,9 @@ class Exception : public std::exception { return; } ///@todo: support for demangling of C++ symbol names - for (int i = 0; i < depth; ++i) { oss << "#" << i << " in " << strings.get()[i] << std::endl; } + for (int i = 0; i < depth; ++i) { + oss << "#" << i << " in " << strings.get()[i] << std::endl; + } msg += oss.str(); #endif // __GNUC__ } diff --git a/cpp/src/components/weak_cc.cuh b/cpp/src/components/weak_cc.cuh index d644a988117..e0da23c2ae8 100644 --- a/cpp/src/components/weak_cc.cuh +++ b/cpp/src/components/weak_cc.cuh @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. @@ -47,22 +47,22 @@ namespace Sparse { class WeakCCState { public: - bool *xa; - bool *fa; - bool *m; + bool* xa; + bool* fa; + bool* m; bool owner; - WeakCCState(bool *xa, bool *fa, bool *m) : xa(xa), fa(fa), m(m) {} + WeakCCState(bool* xa, bool* fa, bool* m) : xa(xa), fa(fa), m(m) {} }; template -__global__ void weak_cc_label_device(vertex_t *labels, - edge_t const *offsets, - vertex_t const *indices, +__global__ void weak_cc_label_device(vertex_t* labels, + edge_t const* offsets, + vertex_t const* indices, edge_t nnz, - bool *fa, - bool *xa, - bool *m, + bool* fa, + bool* xa, + bool* m, vertex_t startVertexId, vertex_t batchSize) { @@ -115,7 +115,7 @@ __global__ void weak_cc_label_device(vertex_t *labels, } template -__global__ void weak_cc_init_label_kernel(vertex_t *labels, +__global__ void weak_cc_init_label_kernel(vertex_t* labels, vertex_t startVertexId, vertex_t batchSize, vertex_t MAX_LABEL, @@ -132,7 +132,7 @@ __global__ void weak_cc_init_label_kernel(vertex_t *labels, template __global__ void weak_cc_init_all_kernel( - vertex_t *labels, bool *fa, bool *xa, vertex_t N, vertex_t MAX_LABEL) + vertex_t* labels, bool* fa, bool* xa, vertex_t N, vertex_t MAX_LABEL) { vertex_t tid = threadIdx.x + blockIdx.x * TPB_X; if (tid < N) { @@ -143,12 +143,12 @@ __global__ void weak_cc_init_all_kernel( } template -void weak_cc_label_batched(vertex_t *labels, - edge_t const *offsets, - vertex_t const *indices, +void weak_cc_label_batched(vertex_t* labels, + edge_t const* offsets, + vertex_t const* indices, edge_t nnz, vertex_t N, - WeakCCState &state, + WeakCCState& state, vertex_t startVertexId, vertex_t batchSize, cudaStream_t stream, @@ -217,14 +217,14 @@ template bool> -void weak_cc_batched(vertex_t *labels, - edge_t const *offsets, - vertex_t const *indices, +void weak_cc_batched(vertex_t* labels, + edge_t const* offsets, + vertex_t const* indices, edge_t nnz, vertex_t N, vertex_t startVertexId, vertex_t batchSize, - WeakCCState &state, + WeakCCState& state, cudaStream_t stream, Lambda filter_op) { @@ -273,9 +273,9 @@ template bool> -void weak_cc(vertex_t *labels, - edge_t const *offsets, - vertex_t const *indices, +void weak_cc(vertex_t* labels, + edge_t const* offsets, + vertex_t const* indices, edge_t nnz, vertex_t N, cudaStream_t stream, @@ -315,9 +315,9 @@ void weak_cc(vertex_t *labels, * @param stream Cuda stream to use */ template -void weak_cc_entry(vertex_t *labels, - edge_t const *offsets, - vertex_t const *indices, +void weak_cc_entry(vertex_t* labels, + edge_t const* offsets, + vertex_t const* indices, edge_t nnz, vertex_t N, cudaStream_t stream) diff --git a/cpp/src/components/weakly_connected_components.cu b/cpp/src/components/weakly_connected_components.cu new file mode 100644 index 00000000000..da63161da33 --- /dev/null +++ b/cpp/src/components/weakly_connected_components.cu @@ -0,0 +1,855 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace cugraph { +namespace experimental { + +namespace { + +// FIXME: this function (after modification) may be useful for SSSP with the near-far method to +// determine the near-far threshold. +// add new roots till the sum of the degrees first becomes no smaller than degree_sum_threshold and +// returns a triplet of (new roots, number of scanned candidates, sum of the degrees of the new +// roots) +template +std::tuple, + typename GraphViewType::vertex_type, + typename GraphViewType::edge_type> +accumulate_new_roots(raft::handle_t const& handle, + vertex_partition_device_view_t vertex_partition, + typename GraphViewType::vertex_type const* components, + typename GraphViewType::edge_type const* degrees, + typename GraphViewType::vertex_type const* candidate_first, + typename GraphViewType::vertex_type const* candidate_last, + typename GraphViewType::vertex_type max_new_roots, + typename GraphViewType::edge_type degree_sum_threshold) +{ + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + + // tuning parameter (time to scan max_scan_size elements should not take significantly longer than + // scanning a single element) + vertex_t max_scan_size = + static_cast(handle.get_device_properties().multiProcessorCount) * vertex_t{16384}; + + rmm::device_uvector new_roots(max_new_roots, handle.get_stream_view()); + vertex_t num_new_roots{0}; + vertex_t num_scanned{0}; + edge_t degree_sum{0}; + while ((candidate_first + num_scanned < candidate_last) && (degree_sum < degree_sum_threshold) && + (num_new_roots < max_new_roots)) { + auto scan_size = std::min( + max_scan_size, + static_cast(thrust::distance(candidate_first + num_scanned, candidate_last))); + + rmm::device_uvector tmp_new_roots(scan_size, handle.get_stream_view()); + rmm::device_uvector tmp_indices(tmp_new_roots.size(), handle.get_stream_view()); + auto input_pair_first = thrust::make_zip_iterator(thrust::make_tuple( + candidate_first + num_scanned, thrust::make_counting_iterator(vertex_t{0}))); + auto output_pair_first = + thrust::make_zip_iterator(thrust::make_tuple(tmp_new_roots.begin(), tmp_indices.begin())); + tmp_new_roots.resize( + static_cast(thrust::distance( + output_pair_first, + thrust::copy_if( + rmm::exec_policy(handle.get_stream_view()), + input_pair_first, + input_pair_first + scan_size, + output_pair_first, + [vertex_partition, components] __device__(auto pair) { + auto v = thrust::get<0>(pair); + return (components[vertex_partition.get_local_vertex_offset_from_vertex_nocheck(v)] == + invalid_component_id::value); + }))), + handle.get_stream_view()); + tmp_indices.resize(tmp_new_roots.size(), handle.get_stream_view()); + + if (tmp_new_roots.size() > 0) { + rmm::device_uvector tmp_cumulative_degrees(tmp_new_roots.size(), + handle.get_stream_view()); + thrust::transform( + rmm::exec_policy(handle.get_stream_view()), + tmp_new_roots.begin(), + tmp_new_roots.end(), + tmp_cumulative_degrees.begin(), + [vertex_partition, degrees] __device__(auto v) { + return degrees[vertex_partition.get_local_vertex_offset_from_vertex_nocheck(v)]; + }); + thrust::inclusive_scan(rmm::exec_policy(handle.get_stream_view()), + tmp_cumulative_degrees.begin(), + tmp_cumulative_degrees.end(), + tmp_cumulative_degrees.begin()); + auto last = thrust::lower_bound(rmm::exec_policy(handle.get_stream_view()), + tmp_cumulative_degrees.begin(), + tmp_cumulative_degrees.end(), + degree_sum_threshold - degree_sum); + if (last != tmp_cumulative_degrees.end()) { ++last; } + auto tmp_num_new_roots = + std::min(static_cast(thrust::distance(tmp_cumulative_degrees.begin(), last)), + max_new_roots - num_new_roots); + + thrust::copy(rmm::exec_policy(handle.get_stream_view()), + tmp_new_roots.begin(), + tmp_new_roots.begin() + tmp_num_new_roots, + new_roots.begin() + num_new_roots); + num_new_roots += tmp_num_new_roots; + vertex_t tmp_num_scanned{0}; + edge_t tmp_degree_sum{0}; + if (tmp_num_new_roots == static_cast(tmp_new_roots.size())) { + tmp_num_scanned = scan_size; + } else { + raft::update_host( + &tmp_num_scanned, tmp_indices.data() + tmp_num_new_roots, size_t{1}, handle.get_stream()); + } + raft::update_host(&tmp_degree_sum, + tmp_cumulative_degrees.data() + (tmp_num_new_roots - 1), + size_t{1}, + handle.get_stream()); + handle.get_stream_view().synchronize(); + num_scanned += tmp_num_scanned; + degree_sum += tmp_degree_sum; + } else { + num_scanned += scan_size; + } + } + + new_roots.resize(num_new_roots, handle.get_stream_view()); + new_roots.shrink_to_fit(handle.get_stream_view()); + + return std::make_tuple(std::move(new_roots), num_scanned, degree_sum); +} + +// FIXME: to silence the spurious warning (missing return statement ...) due to the nvcc bug +// (https://stackoverflow.com/questions/64523302/cuda-missing-return-statement-at-end-of-non-void- +// function-in-constexpr-if-fun) +template +struct v_op_t { + using vertex_type = typename GraphViewType::vertex_type; + + vertex_partition_device_view_t + vertex_partition{}; + vertex_type* level_components{}; + decltype(thrust::make_zip_iterator(thrust::make_tuple( + static_cast(nullptr), static_cast(nullptr)))) edge_buffer_first{}; + // FIXME: we can use cuda::atomic instead but currently on a system with x86 + GPU, this requires + // placing the atomic barrier on managed memory and this adds additional complication. + size_t* num_edge_inserts{}; + size_t next_bucket_idx{}; + size_t conflict_bucket_idx{}; // relevant only if GraphViewType::is_multi_gpu is true + + template + __device__ std::enable_if_t>> + operator()(thrust::tuple tagged_v, int v_val /* dummy */) const + { + auto tag = thrust::get<1>(tagged_v); + auto v_offset = + vertex_partition.get_local_vertex_offset_from_vertex_nocheck(thrust::get<0>(tagged_v)); + // FIXME: better switch to atomic_ref after + // https://github.com/nvidia/libcudacxx/milestone/2 + auto old = + atomicCAS(level_components + v_offset, invalid_component_id::value, tag); + if (old != invalid_component_id::value && old != tag) { // conflict + return thrust::optional>{ + thrust::make_tuple(conflict_bucket_idx, std::byte{0} /* dummy */)}; + } else { + return (old == invalid_component_id::value) + ? thrust::optional>{thrust::make_tuple( + next_bucket_idx, std::byte{0} /* dummy */)} + : thrust::nullopt; + } + } + + template + __device__ std::enable_if_t>> + operator()(thrust::tuple tagged_v, int v_val /* dummy */) const + { + return thrust::optional>{ + thrust::make_tuple(next_bucket_idx, std::byte{0} /* dummy */)}; + } +}; + +template +void weakly_connected_components_impl(raft::handle_t const& handle, + GraphViewType const& push_graph_view, + typename GraphViewType::vertex_type* components, + bool do_expensive_check) +{ + using vertex_t = typename GraphViewType::vertex_type; + using edge_t = typename GraphViewType::edge_type; + using weight_t = typename GraphViewType::weight_type; + + static_assert(std::is_integral::value, + "GraphViewType::vertex_type should be integral."); + static_assert(!GraphViewType::is_adj_matrix_transposed, + "GraphViewType should support the push model."); + + auto const num_vertices = push_graph_view.get_number_of_vertices(); + if (num_vertices == 0) { return; } + + // 1. check input arguments + + CUGRAPH_EXPECTS( + push_graph_view.is_symmetric(), + "Invalid input argument: input graph should be symmetric for weakly connected components."); + + if (do_expensive_check) { + // nothing to do + } + + // 2. recursively run multi-root frontier expansion + + enum class Bucket { + cur, + next, + conflict /* relevant only if GraphViewType::is_multi_gpu is true */, + num_buckets + }; + // tuning parameter to balance work per iteration (should be large enough to be throughput + // bounded) vs # conflicts between frontiers with different roots (# conflicts == # edges for the + // next level) + auto degree_sum_threshold = + static_cast(handle.get_device_properties().multiProcessorCount) * edge_t{1024}; + + size_t num_levels{0}; + graph_t + level_graph(handle); + rmm::device_uvector level_renumber_map(0, handle.get_stream_view()); + std::vector> level_component_vectors{}; + // vertex ID in this level to the component ID in the previous level + std::vector> level_renumber_map_vectors{}; + std::vector level_local_vertex_first_vectors{}; + while (true) { + auto level_graph_view = num_levels == 0 ? push_graph_view : level_graph.view(); + auto vertex_partition = vertex_partition_device_view_t( + level_graph_view.get_vertex_partition_view()); + level_component_vectors.push_back(rmm::device_uvector( + num_levels == 0 ? vertex_t{0} : level_graph_view.get_number_of_local_vertices(), + handle.get_stream_view())); + level_renumber_map_vectors.push_back(std::move(level_renumber_map)); + level_local_vertex_first_vectors.push_back(level_graph_view.get_local_vertex_first()); + auto level_components = + num_levels == 0 ? components : level_component_vectors[num_levels].data(); + ++num_levels; + auto degrees = level_graph_view.compute_out_degrees(handle); + + // 2-1. filter out isolated vertices + + auto pair_first = thrust::make_zip_iterator(thrust::make_tuple( + thrust::make_counting_iterator(level_graph_view.get_local_vertex_first()), degrees.begin())); + thrust::transform(rmm::exec_policy(handle.get_stream_view()), + pair_first, + pair_first + level_graph_view.get_number_of_local_vertices(), + level_components, + [] __device__(auto pair) { + auto v = thrust::get<0>(pair); + auto degree = thrust::get<1>(pair); + return degree > 0 ? invalid_component_id::value : v; + }); + + // 2-2. initialize new root candidates + + // Vertices are first partitioned to high-degree vertices and low-degree vertices, we can reach + // degree_sum_threshold with fewer high-degree vertices leading to a higher compression ratio. + // The degree threshold is set to ceil(sqrt(degree_sum_threshold * 2)); this guarantees the + // compression ratio of at least 50% (ignoring rounding errors) even if all the selected roots + // fall into a single connected component as there will be at least as many non-root vertices in + // the connected component (assuming there are no multi-edges, if there are multi-edges, we may + // not get 50% compression in # vertices but still get compression in # edges). the remaining + // low-degree vertices will be randomly shuffled so comparable ratios of vertices will be + // selected as roots in the remaining connected components. + + rmm::device_uvector new_root_candidates( + level_graph_view.get_number_of_local_vertices(), handle.get_stream_view()); + new_root_candidates.resize( + thrust::distance( + new_root_candidates.begin(), + thrust::copy_if( + rmm::exec_policy(handle.get_stream_view()), + thrust::make_counting_iterator(level_graph_view.get_local_vertex_first()), + thrust::make_counting_iterator(level_graph_view.get_local_vertex_last()), + new_root_candidates.begin(), + [vertex_partition, level_components] __device__(auto v) { + return level_components[vertex_partition.get_local_vertex_offset_from_vertex_nocheck( + v)] == invalid_component_id::value; + })), + handle.get_stream_view()); + auto high_degree_partition_last = thrust::stable_partition( + rmm::exec_policy(handle.get_stream_view()), + new_root_candidates.begin(), + new_root_candidates.end(), + [vertex_partition, + degrees = degrees.data(), + threshold = static_cast( + ceil(sqrt(static_cast(degree_sum_threshold) * 2.0)))] __device__(auto v) { + return degrees[vertex_partition.get_local_vertex_offset_from_vertex_nocheck(v)] >= + threshold; + }); + thrust::shuffle(rmm::exec_policy(handle.get_stream_view()), + high_degree_partition_last, + new_root_candidates.end(), + thrust::default_random_engine()); + + double constexpr max_new_roots_ratio = + 0.05; // to avoid selecting all the vertices as roots leading to zero compression + static_assert(max_new_roots_ratio > 0.0); + auto max_new_roots = std::max( + static_cast(new_root_candidates.size() * max_new_roots_ratio), vertex_t{1}); + + auto init_max_new_roots = max_new_roots; + if (GraphViewType::is_multi_gpu) { + auto& comm = handle.get_comms(); + auto const comm_rank = comm.get_rank(); + auto const comm_size = comm.get_size(); + + auto first_candidate_degree = thrust::transform_reduce( + rmm::exec_policy(handle.get_stream_view()), + new_root_candidates.begin(), + new_root_candidates.begin() + (new_root_candidates.size() > 0 ? 1 : 0), + [vertex_partition, degrees = degrees.data()] __device__(auto v) { + return degrees[vertex_partition.get_local_vertex_offset_from_vertex_nocheck(v)]; + }, + edge_t{0}, + thrust::plus{}); + + auto first_candidate_degrees = + host_scalar_gather(comm, first_candidate_degree, int{0}, handle.get_stream()); + auto new_root_candidate_counts = + host_scalar_gather(comm, new_root_candidates.size(), int{0}, handle.get_stream()); + + if (comm_rank == 0) { + std::vector init_max_new_root_counts(comm_size, vertex_t{0}); + + // if there exists very high degree vertices, we can exceed degree_sum_threshold * comm_size + // with fewer than one root per GPU + if (std::reduce(first_candidate_degrees.begin(), first_candidate_degrees.end()) > + degree_sum_threshold * comm_size) { + std::vector> degree_gpuid_pairs(comm_size); + for (int i = 0; i < comm_size; ++i) { + degree_gpuid_pairs[i] = std::make_tuple(first_candidate_degrees[i], i); + } + std::sort(degree_gpuid_pairs.begin(), degree_gpuid_pairs.end(), [](auto lhs, auto rhs) { + return std::get<0>(lhs) > std::get<0>(rhs); + }); + edge_t sum{0}; + for (size_t i = 0; i < degree_gpuid_pairs.size(); ++i) { + sum += std::get<0>(degree_gpuid_pairs[i]); + init_max_new_root_counts[std::get<1>(degree_gpuid_pairs[i])] = 1; + if (sum > degree_sum_threshold * comm_size) { break; } + } + } + // to avoid selecting too many (possibly all) vertices as initial roots leading to no + // compression in the worst case. + else if (level_graph_view.get_number_of_vertices() <= + static_cast(handle.get_comms().get_size() * + ceil(1.0 / max_new_roots_ratio))) { + std::vector gpuids{}; + gpuids.reserve( + std::reduce(new_root_candidate_counts.begin(), new_root_candidate_counts.end())); + for (size_t i = 0; i < new_root_candidate_counts.size(); ++i) { + gpuids.insert(gpuids.end(), new_root_candidate_counts[i], static_cast(i)); + } + std::random_device rd{}; + std::shuffle(gpuids.begin(), gpuids.end(), std::mt19937(rd())); + gpuids.resize( + std::max(static_cast(gpuids.size() * max_new_roots_ratio), vertex_t{1})); + for (size_t i = 0; i < gpuids.size(); ++i) { + ++init_max_new_root_counts[gpuids[i]]; + } + } else { + std::fill(init_max_new_root_counts.begin(), + init_max_new_root_counts.end(), + std::numeric_limits::max()); + } + + // FIXME: we need to add host_scalar_scatter +#if 1 + rmm::device_uvector d_counts(comm_size, handle.get_stream_view()); + raft::update_device(d_counts.data(), + init_max_new_root_counts.data(), + init_max_new_root_counts.size(), + handle.get_stream()); + device_bcast( + comm, d_counts.data(), d_counts.data(), d_counts.size(), int{0}, handle.get_stream()); + raft::update_host( + &init_max_new_roots, d_counts.data() + comm_rank, size_t{1}, handle.get_stream()); +#else + init_max_new_roots = + host_scalar_scatter(comm, init_max_new_root_counts.data(), int{0}, handle.get_stream()); +#endif + } else { + // FIXME: we need to add host_scalar_scatter +#if 1 + rmm::device_uvector d_counts(comm_size, handle.get_stream_view()); + device_bcast( + comm, d_counts.data(), d_counts.data(), d_counts.size(), int{0}, handle.get_stream()); + raft::update_host( + &init_max_new_roots, d_counts.data() + comm_rank, size_t{1}, handle.get_stream()); +#else + init_max_new_roots = + host_scalar_scatter(comm, init_max_new_root_counts.data(), int{0}, handle.get_stream()); +#endif + } + + handle.get_stream_view().synchronize(); + init_max_new_roots = std::min(init_max_new_roots, max_new_roots); + } + + // 2-3. initialize vertex frontier, edge_buffer, and col_components (if multi-gpu) + + VertexFrontier(Bucket::num_buckets)> + vertex_frontier(handle); + vertex_t next_candidate_offset{0}; + edge_t edge_count{0}; + + auto edge_buffer = + allocate_dataframe_buffer>(0, handle.get_stream()); + // FIXME: we can use cuda::atomic instead but currently on a system with x86 + GPU, this + // requires placing the atomic variable on managed memory and this make it less attractive. + rmm::device_scalar num_edge_inserts(size_t{0}, handle.get_stream_view()); + + rmm::device_uvector col_components( + GraphViewType::is_multi_gpu ? level_graph_view.get_number_of_local_adj_matrix_partition_cols() + : vertex_t{0}, + handle.get_stream_view()); + if (GraphViewType::is_multi_gpu) { + thrust::fill(rmm::exec_policy(handle.get_stream_view()), + col_components.begin(), + col_components.end(), + invalid_component_id::value); + } + + // 2.4 iterate till every vertex gets visited + + size_t iter{0}; + while (true) { + if ((edge_count < degree_sum_threshold) && + (next_candidate_offset < static_cast(new_root_candidates.size()))) { + auto [new_roots, num_scanned, degree_sum] = accumulate_new_roots( + handle, + vertex_partition, + level_components, + degrees.data(), + new_root_candidates.data() + next_candidate_offset, + new_root_candidates.data() + new_root_candidates.size(), + iter == 0 ? init_max_new_roots : max_new_roots, + degree_sum_threshold - edge_count); + next_candidate_offset += num_scanned; + edge_count += degree_sum; + + thrust::sort( + rmm::exec_policy(handle.get_stream_view()), new_roots.begin(), new_roots.end()); + + thrust::for_each( + rmm::exec_policy(handle.get_stream_view()), + new_roots.begin(), + new_roots.end(), + [vertex_partition, components = level_components] __device__(auto c) { + components[vertex_partition.get_local_vertex_offset_from_vertex_nocheck(c)] = c; + }); + + auto pair_first = + thrust::make_zip_iterator(thrust::make_tuple(new_roots.begin(), new_roots.begin())); + vertex_frontier.get_bucket(static_cast(Bucket::cur)) + .insert(pair_first, pair_first + new_roots.size()); + } + + if (vertex_frontier.get_bucket(static_cast(Bucket::cur)).aggregate_size() == 0) { + break; + } + + if (GraphViewType::is_multi_gpu) { + copy_to_adj_matrix_col( + handle, + level_graph_view, + thrust::get<0>(vertex_frontier.get_bucket(static_cast(Bucket::cur)) + .begin() + .get_iterator_tuple()), + thrust::get<0>(vertex_frontier.get_bucket(static_cast(Bucket::cur)) + .end() + .get_iterator_tuple()), + level_components, + col_components.begin()); + } + + auto max_pushes = + GraphViewType::is_multi_gpu + ? compute_num_out_nbrs_from_frontier( + handle, level_graph_view, vertex_frontier, static_cast(Bucket::cur)) + : edge_count; + + // FIXME: if we use cuco::static_map (no duplicates, ideally we need static_set), edge_buffer + // size cannot exceed (# roots)^2 and we can avoid additional sort & unique (but resizing the + // buffer may be more expensive). + auto old_num_edge_inserts = num_edge_inserts.value(handle.get_stream_view()); + resize_dataframe_buffer>( + edge_buffer, old_num_edge_inserts + max_pushes, handle.get_stream()); + + update_frontier_v_push_if_out_nbr( + handle, + level_graph_view, + vertex_frontier, + static_cast(Bucket::cur), + GraphViewType::is_multi_gpu ? std::vector{static_cast(Bucket::next), + static_cast(Bucket::conflict)} + : std::vector{static_cast(Bucket::next)}, + thrust::make_counting_iterator(0) /* dummy */, + thrust::make_counting_iterator(0) /* dummy */, + [col_components = GraphViewType::is_multi_gpu ? col_components.data() : level_components, + col_first = level_graph_view.get_local_adj_matrix_partition_col_first(), + edge_buffer_first = + get_dataframe_buffer_begin>(edge_buffer), + num_edge_inserts = num_edge_inserts.data()] __device__(auto tagged_src, + vertex_t dst, + auto src_val, + auto dst_val) { + auto tag = thrust::get<1>(tagged_src); + auto col_offset = dst - col_first; + // FIXME: better switch to atomic_ref after + // https://github.com/nvidia/libcudacxx/milestone/2 + auto old = + atomicCAS(col_components + col_offset, invalid_component_id::value, tag); + if (old != invalid_component_id::value && old != tag) { // conflict + static_assert(sizeof(unsigned long long int) == sizeof(size_t)); + auto edge_idx = atomicAdd(reinterpret_cast(num_edge_inserts), + static_cast(1)); + // keep only the edges in the lower triangular part + *(edge_buffer_first + edge_idx) = + tag >= old ? thrust::make_tuple(tag, old) : thrust::make_tuple(old, tag); + } + return (old == invalid_component_id::value) ? thrust::optional{tag} + : thrust::nullopt; + }, + reduce_op::null(), + thrust::make_constant_iterator(0) /* dummy */, + thrust::make_discard_iterator() /* dummy */, + v_op_t{ + vertex_partition, + level_components, + get_dataframe_buffer_begin>(edge_buffer), + num_edge_inserts.data(), + static_cast(Bucket::next), + static_cast(Bucket::conflict)}); + + if (GraphViewType::is_multi_gpu) { + auto cur_num_edge_inserts = num_edge_inserts.value(handle.get_stream_view()); + auto& conflict_bucket = vertex_frontier.get_bucket(static_cast(Bucket::conflict)); + resize_dataframe_buffer>( + edge_buffer, cur_num_edge_inserts + conflict_bucket.size(), handle.get_stream()); + thrust::for_each( + rmm::exec_policy(handle.get_stream_view()), + conflict_bucket.begin(), + conflict_bucket.end(), + [vertex_partition, + level_components, + edge_buffer_first = + get_dataframe_buffer_begin>(edge_buffer), + num_edge_inserts = num_edge_inserts.data()] __device__(auto tagged_v) { + auto v_offset = vertex_partition.get_local_vertex_offset_from_vertex_nocheck( + thrust::get<0>(tagged_v)); + auto old = *(level_components + v_offset); + auto tag = thrust::get<1>(tagged_v); + static_assert(sizeof(unsigned long long int) == sizeof(size_t)); + auto edge_idx = atomicAdd(reinterpret_cast(num_edge_inserts), + static_cast(1)); + // keep only the edges in the lower triangular part + *(edge_buffer_first + edge_idx) = + tag >= old ? thrust::make_tuple(tag, old) : thrust::make_tuple(old, tag); + }); + conflict_bucket.clear(); + } + + // maintain the list of sorted unique edges (we can avoid this if we use cuco::static_map(no + // duplicates, ideally we need static_set)). + auto new_num_edge_inserts = num_edge_inserts.value(handle.get_stream_view()); + if (new_num_edge_inserts > old_num_edge_inserts) { + auto edge_first = + get_dataframe_buffer_begin>(edge_buffer); + thrust::sort(rmm::exec_policy(handle.get_stream_view()), + edge_first + old_num_edge_inserts, + edge_first + new_num_edge_inserts); + if (old_num_edge_inserts > 0) { + auto tmp_edge_buffer = allocate_dataframe_buffer>( + new_num_edge_inserts, handle.get_stream()); + auto tmp_edge_first = + get_dataframe_buffer_begin>(tmp_edge_buffer); + thrust::merge(rmm::exec_policy(handle.get_stream_view()), + edge_first, + edge_first + old_num_edge_inserts, + edge_first + old_num_edge_inserts, + edge_first + new_num_edge_inserts, + tmp_edge_first); + edge_buffer = std::move(tmp_edge_buffer); + } + edge_first = get_dataframe_buffer_begin>(edge_buffer); + auto unique_edge_last = thrust::unique(rmm::exec_policy(handle.get_stream_view()), + edge_first, + edge_first + new_num_edge_inserts); + auto num_unique_edges = static_cast(thrust::distance(edge_first, unique_edge_last)); + num_edge_inserts.set_value_async(num_unique_edges, handle.get_stream_view()); + } + + vertex_frontier.get_bucket(static_cast(Bucket::cur)).clear(); + vertex_frontier.get_bucket(static_cast(Bucket::cur)).shrink_to_fit(); + vertex_frontier.swap_buckets(static_cast(Bucket::cur), + static_cast(Bucket::next)); + edge_count = thrust::transform_reduce( + rmm::exec_policy(handle.get_stream_view()), + thrust::get<0>(vertex_frontier.get_bucket(static_cast(Bucket::cur)) + .begin() + .get_iterator_tuple()), + thrust::get<0>( + vertex_frontier.get_bucket(static_cast(Bucket::cur)).end().get_iterator_tuple()), + [vertex_partition, degrees = degrees.data()] __device__(auto v) { + return degrees[vertex_partition.get_local_vertex_offset_from_vertex_nocheck(v)]; + }, + edge_t{0}, + thrust::plus()); + + ++iter; + } + + // 2-5. construct the next level graph from the edges emitted on conflicts + + auto num_inserts = num_edge_inserts.value(handle.get_stream_view()); + auto aggregate_num_inserts = num_inserts; + if (GraphViewType::is_multi_gpu) { + auto& comm = handle.get_comms(); + aggregate_num_inserts = host_scalar_allreduce(comm, num_inserts, handle.get_stream()); + } + + if (aggregate_num_inserts > 0) { + resize_dataframe_buffer>( + edge_buffer, static_cast(num_inserts * 2), handle.get_stream()); + auto input_first = get_dataframe_buffer_begin>(edge_buffer); + auto output_first = thrust::make_zip_iterator( + thrust::make_tuple(thrust::get<1>(input_first.get_iterator_tuple()), + thrust::get<0>(input_first.get_iterator_tuple()))) + + num_inserts; + thrust::copy(rmm::exec_policy(handle.get_stream_view()), + input_first, + input_first + num_inserts, + output_first); + + if (GraphViewType::is_multi_gpu) { + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_size = row_comm.get_size(); + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_size = col_comm.get_size(); + + std::tie(edge_buffer, std::ignore) = + cugraph::experimental::groupby_gpuid_and_shuffle_values( + comm, + get_dataframe_buffer_begin>(edge_buffer), + get_dataframe_buffer_end>(edge_buffer), + [key_func = + cugraph::experimental::detail::compute_gpu_id_from_edge_t{ + comm_size, row_comm_size, col_comm_size}] __device__(auto val) { + return key_func(thrust::get<0>(val), thrust::get<1>(val)); + }, + handle.get_stream()); + auto edge_first = + get_dataframe_buffer_begin>(edge_buffer); + auto edge_last = get_dataframe_buffer_end>(edge_buffer); + thrust::sort(rmm::exec_policy(handle.get_stream_view()), edge_first, edge_last); + auto unique_edge_last = + thrust::unique(rmm::exec_policy(handle.get_stream_view()), edge_first, edge_last); + resize_dataframe_buffer>( + edge_buffer, + static_cast(thrust::distance(edge_first, unique_edge_last)), + handle.get_stream()); + shrink_to_fit_dataframe_buffer>(edge_buffer, + handle.get_stream()); + } + + std::optional> tmp_renumber_map{std::nullopt}; + std::tie(level_graph, tmp_renumber_map) = + create_graph_from_edgelist(handle, + std::nullopt, + std::move(std::get<0>(edge_buffer)), + std::move(std::get<1>(edge_buffer)), + std::nullopt, + graph_properties_t{true, false}, + true); + level_renumber_map = std::move(*tmp_renumber_map); + } else { + break; + } + } + + // 3. recursive update the current level component IDs from the next level component IDs + + for (size_t i = 0; i < num_levels - 1; ++i) { + size_t next_level = num_levels - 1 - i; + size_t current_level = next_level - 1; + + rmm::device_uvector next_local_vertices(level_renumber_map_vectors[next_level].size(), + handle.get_stream_view()); + thrust::sequence(rmm::exec_policy(handle.get_stream_view()), + next_local_vertices.begin(), + next_local_vertices.end(), + level_local_vertex_first_vectors[next_level]); + relabel( + handle, + std::make_tuple(next_local_vertices.data(), level_renumber_map_vectors[next_level].data()), + next_local_vertices.size(), + level_component_vectors[next_level].data(), + level_component_vectors[next_level].size(), + false); + relabel( + handle, + std::make_tuple(level_renumber_map_vectors[next_level].data(), + level_component_vectors[next_level].data()), + level_renumber_map_vectors[next_level].size(), + current_level == 0 ? components : level_component_vectors[current_level].data(), + current_level == 0 ? push_graph_view.get_number_of_local_vertices() + : level_component_vectors[current_level].size(), + true); + } +} + +} // namespace + +template +void weakly_connected_components( + raft::handle_t const& handle, + graph_view_t const& graph_view, + vertex_t* components, + bool do_expensive_check) +{ + weakly_connected_components_impl(handle, graph_view, components, do_expensive_check); +} + +// explicit instantiation + +template void weakly_connected_components( + raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t* components, + bool do_expensive_check); + +template void weakly_connected_components( + raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t* components, + bool do_expensive_check); + +template void weakly_connected_components( + raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t* components, + bool do_expensive_check); + +template void weakly_connected_components( + raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t* components, + bool do_expensive_check); + +template void weakly_connected_components( + raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t* components, + bool do_expensive_check); + +template void weakly_connected_components( + raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t* components, + bool do_expensive_check); + +template void weakly_connected_components( + raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t* components, + bool do_expensive_check); + +template void weakly_connected_components( + raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t* components, + bool do_expensive_check); + +template void weakly_connected_components( + raft::handle_t const& handle, + graph_view_t const& graph_view, + int64_t* components, + bool do_expensive_check); + +template void weakly_connected_components( + raft::handle_t const& handle, + graph_view_t const& graph_view, + int64_t* components, + bool do_expensive_check); + +template void weakly_connected_components( + raft::handle_t const& handle, + graph_view_t const& graph_view, + int64_t* components, + bool do_expensive_check); + +template void weakly_connected_components( + raft::handle_t const& handle, + graph_view_t const& graph_view, + int64_t* components, + bool do_expensive_check); + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/src/converters/COOtoCSR.cu b/cpp/src/converters/COOtoCSR.cu index 787872742e9..49986810539 100644 --- a/cpp/src/converters/COOtoCSR.cu +++ b/cpp/src/converters/COOtoCSR.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -14,61 +14,71 @@ * limitations under the License. */ -#include +#include #include "COOtoCSR.cuh" namespace cugraph { // Explicit instantiation for uint32_t + float -template std::unique_ptr> coo_to_csr( - GraphCOOView const &graph, rmm::mr::device_memory_resource *); +template std::unique_ptr> +coo_to_csr(legacy::GraphCOOView const& graph, + rmm::mr::device_memory_resource*); // Explicit instantiation for uint32_t + double -template std::unique_ptr> -coo_to_csr(GraphCOOView const &graph, - rmm::mr::device_memory_resource *); +template std::unique_ptr> +coo_to_csr( + legacy::GraphCOOView const& graph, rmm::mr::device_memory_resource*); // Explicit instantiation for int + float -template std::unique_ptr> coo_to_csr( - GraphCOOView const &graph, rmm::mr::device_memory_resource *); +template std::unique_ptr> +coo_to_csr(legacy::GraphCOOView const& graph, + rmm::mr::device_memory_resource*); // Explicit instantiation for int + double -template std::unique_ptr> coo_to_csr( - GraphCOOView const &graph, rmm::mr::device_memory_resource *); +template std::unique_ptr> +coo_to_csr(legacy::GraphCOOView const& graph, + rmm::mr::device_memory_resource*); // Explicit instantiation for int64_t + float -template std::unique_ptr> coo_to_csr( - GraphCOOView const &graph, rmm::mr::device_memory_resource *); +template std::unique_ptr> +coo_to_csr(legacy::GraphCOOView const& graph, + rmm::mr::device_memory_resource*); // Explicit instantiation for int64_t + double -template std::unique_ptr> coo_to_csr( - GraphCOOView const &graph, rmm::mr::device_memory_resource *); +template std::unique_ptr> +coo_to_csr(legacy::GraphCOOView const& graph, + rmm::mr::device_memory_resource*); // in-place versions: // // Explicit instantiation for uint32_t + float template void coo_to_csr_inplace( - GraphCOOView &graph, GraphCSRView &result); + legacy::GraphCOOView& graph, + legacy::GraphCSRView& result); // Explicit instantiation for uint32_t + double template void coo_to_csr_inplace( - GraphCOOView &graph, - GraphCSRView &result); + legacy::GraphCOOView& graph, + legacy::GraphCSRView& result); // Explicit instantiation for int + float template void coo_to_csr_inplace( - GraphCOOView &graph, GraphCSRView &result); + legacy::GraphCOOView& graph, + legacy::GraphCSRView& result); // Explicit instantiation for int + double template void coo_to_csr_inplace( - GraphCOOView &graph, GraphCSRView &result); + legacy::GraphCOOView& graph, + legacy::GraphCSRView& result); // Explicit instantiation for int64_t + float template void coo_to_csr_inplace( - GraphCOOView &graph, GraphCSRView &result); + legacy::GraphCOOView& graph, + legacy::GraphCSRView& result); // Explicit instantiation for int64_t + double template void coo_to_csr_inplace( - GraphCOOView &graph, GraphCSRView &result); + legacy::GraphCOOView& graph, + legacy::GraphCSRView& result); } // namespace cugraph diff --git a/cpp/src/converters/COOtoCSR.cuh b/cpp/src/converters/COOtoCSR.cuh index b110e02a513..641b037efdd 100644 --- a/cpp/src/converters/COOtoCSR.cuh +++ b/cpp/src/converters/COOtoCSR.cuh @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. @@ -30,15 +30,15 @@ #include #include -#include -#include +#include +#include #include #include -#include +#include -#include +#include namespace cugraph { namespace detail { @@ -55,38 +55,38 @@ namespace detail { * @tparam WT Type of edge weights. Supported value : float or double. * * @param[in] graph The input graph object - * @param[in] stream The cuda stream for kernel calls + * @param[in] stream_view The cuda stream for kernel calls * * @param[out] result Total number of vertices */ template -VT sort(GraphCOOView &graph, cudaStream_t stream) +VT sort(legacy::GraphCOOView& graph, rmm::cuda_stream_view stream_view) { VT max_src_id; VT max_dst_id; if (graph.has_data()) { thrust::stable_sort_by_key( - rmm::exec_policy(stream)->on(stream), + rmm::exec_policy(stream_view), graph.dst_indices, graph.dst_indices + graph.number_of_edges, thrust::make_zip_iterator(thrust::make_tuple(graph.src_indices, graph.edge_data))); CUDA_TRY(cudaMemcpy( &max_dst_id, &(graph.dst_indices[graph.number_of_edges - 1]), sizeof(VT), cudaMemcpyDefault)); thrust::stable_sort_by_key( - rmm::exec_policy(stream)->on(stream), + rmm::exec_policy(stream_view), graph.src_indices, graph.src_indices + graph.number_of_edges, thrust::make_zip_iterator(thrust::make_tuple(graph.dst_indices, graph.edge_data))); CUDA_TRY(cudaMemcpy( &max_src_id, &(graph.src_indices[graph.number_of_edges - 1]), sizeof(VT), cudaMemcpyDefault)); } else { - thrust::stable_sort_by_key(rmm::exec_policy(stream)->on(stream), + thrust::stable_sort_by_key(rmm::exec_policy(stream_view), graph.dst_indices, graph.dst_indices + graph.number_of_edges, graph.src_indices); CUDA_TRY(cudaMemcpy( &max_dst_id, &(graph.dst_indices[graph.number_of_edges - 1]), sizeof(VT), cudaMemcpyDefault)); - thrust::stable_sort_by_key(rmm::exec_policy(stream)->on(stream), + thrust::stable_sort_by_key(rmm::exec_policy(stream_view), graph.src_indices, graph.src_indices + graph.number_of_edges, graph.dst_indices); @@ -97,14 +97,15 @@ VT sort(GraphCOOView &graph, cudaStream_t stream) } template -void fill_offset( - VT *source, ET *offsets, VT number_of_vertices, ET number_of_edges, cudaStream_t stream) +void fill_offset(VT* source, + ET* offsets, + VT number_of_vertices, + ET number_of_edges, + rmm::cuda_stream_view stream_view) { - thrust::fill(rmm::exec_policy(stream)->on(stream), - offsets, - offsets + number_of_vertices + 1, - number_of_edges); - thrust::for_each(rmm::exec_policy(stream)->on(stream), + thrust::fill( + rmm::exec_policy(stream_view), offsets, offsets + number_of_vertices + 1, number_of_edges); + thrust::for_each(rmm::exec_policy(stream_view), thrust::make_counting_iterator(1), thrust::make_counting_iterator(number_of_edges), [source, offsets] __device__(ET index) { @@ -116,7 +117,7 @@ void fill_offset( off[src[0]] = ET{0}; auto iter = thrust::make_reverse_iterator(offsets + number_of_vertices + 1); - thrust::inclusive_scan(rmm::exec_policy(stream)->on(stream), + thrust::inclusive_scan(rmm::exec_policy(stream_view), iter, iter + number_of_vertices + 1, iter, @@ -124,18 +125,18 @@ void fill_offset( } template -rmm::device_buffer create_offset(VT *source, +rmm::device_buffer create_offset(VT* source, VT number_of_vertices, ET number_of_edges, - cudaStream_t stream, - rmm::mr::device_memory_resource *mr) + rmm::cuda_stream_view stream_view, + rmm::mr::device_memory_resource* mr) { // Offset array needs an extra element at the end to contain the ending offsets // of the last vertex - rmm::device_buffer offsets_buffer(sizeof(ET) * (number_of_vertices + 1), stream, mr); - ET *offsets = static_cast(offsets_buffer.data()); + rmm::device_buffer offsets_buffer(sizeof(ET) * (number_of_vertices + 1), stream_view, mr); + ET* offsets = static_cast(offsets_buffer.data()); - fill_offset(source, offsets, number_of_vertices, number_of_edges, stream); + fill_offset(source, offsets, number_of_vertices, number_of_edges, stream_view); return offsets_buffer; } @@ -143,35 +144,39 @@ rmm::device_buffer create_offset(VT *source, } // namespace detail template -std::unique_ptr> coo_to_csr(GraphCOOView const &graph, - rmm::mr::device_memory_resource *mr) +std::unique_ptr> coo_to_csr( + legacy::GraphCOOView const& graph, rmm::mr::device_memory_resource* mr) { - cudaStream_t stream{nullptr}; + rmm::cuda_stream_view stream_view; - GraphCOO temp_graph(graph, stream, mr); - GraphCOOView temp_graph_view = temp_graph.view(); - VT total_vertex_count = detail::sort(temp_graph_view, stream); - rmm::device_buffer offsets = detail::create_offset( - temp_graph.src_indices(), total_vertex_count, temp_graph.number_of_edges(), stream, mr); + legacy::GraphCOO temp_graph(graph, stream_view.value(), mr); + legacy::GraphCOOView temp_graph_view = temp_graph.view(); + VT total_vertex_count = detail::sort(temp_graph_view, stream_view); + rmm::device_buffer offsets = detail::create_offset( + temp_graph.src_indices(), total_vertex_count, temp_graph.number_of_edges(), stream_view, mr); auto coo_contents = temp_graph.release(); - GraphSparseContents csr_contents{ + legacy::GraphSparseContents csr_contents{ total_vertex_count, coo_contents.number_of_edges, std::make_unique(std::move(offsets)), std::move(coo_contents.dst_indices), std::move(coo_contents.edge_data)}; - return std::make_unique>(std::move(csr_contents)); + return std::make_unique>(std::move(csr_contents)); } template -void coo_to_csr_inplace(GraphCOOView &graph, GraphCSRView &result) +void coo_to_csr_inplace(legacy::GraphCOOView& graph, + legacy::GraphCSRView& result) { - cudaStream_t stream{nullptr}; + rmm::cuda_stream_view stream_view; - detail::sort(graph, stream); - detail::fill_offset( - graph.src_indices, result.offsets, graph.number_of_vertices, graph.number_of_edges, stream); + detail::sort(graph, stream_view); + detail::fill_offset(graph.src_indices, + result.offsets, + graph.number_of_vertices, + graph.number_of_edges, + stream_view); CUDA_TRY(cudaMemcpy( result.indices, graph.dst_indices, sizeof(VT) * graph.number_of_edges, cudaMemcpyDefault)); @@ -184,60 +189,65 @@ void coo_to_csr_inplace(GraphCOOView &graph, GraphCSRView> -coo_to_csr(GraphCOOView const &graph, - rmm::mr::device_memory_resource *); +extern template std::unique_ptr> +coo_to_csr(legacy::GraphCOOView const& graph, + rmm::mr::device_memory_resource*); // EIDecl for uint32_t + double -extern template std::unique_ptr> -coo_to_csr(GraphCOOView const &graph, - rmm::mr::device_memory_resource *); +extern template std::unique_ptr> +coo_to_csr( + legacy::GraphCOOView const& graph, rmm::mr::device_memory_resource*); // EIDecl for int + float -extern template std::unique_ptr> -coo_to_csr(GraphCOOView const &graph, - rmm::mr::device_memory_resource *); +extern template std::unique_ptr> +coo_to_csr(legacy::GraphCOOView const& graph, + rmm::mr::device_memory_resource*); // EIDecl for int + double -extern template std::unique_ptr> -coo_to_csr(GraphCOOView const &graph, - rmm::mr::device_memory_resource *); +extern template std::unique_ptr> +coo_to_csr(legacy::GraphCOOView const& graph, + rmm::mr::device_memory_resource*); // EIDecl for int64_t + float -extern template std::unique_ptr> -coo_to_csr(GraphCOOView const &graph, - rmm::mr::device_memory_resource *); +extern template std::unique_ptr> +coo_to_csr(legacy::GraphCOOView const& graph, + rmm::mr::device_memory_resource*); // EIDecl for int64_t + double -extern template std::unique_ptr> -coo_to_csr(GraphCOOView const &graph, - rmm::mr::device_memory_resource *); +extern template std::unique_ptr> +coo_to_csr(legacy::GraphCOOView const& graph, + rmm::mr::device_memory_resource*); // in-place versions: // // EIDecl for uint32_t + float extern template void coo_to_csr_inplace( - GraphCOOView &graph, GraphCSRView &result); + legacy::GraphCOOView& graph, + legacy::GraphCSRView& result); // EIDecl for uint32_t + double extern template void coo_to_csr_inplace( - GraphCOOView &graph, - GraphCSRView &result); + legacy::GraphCOOView& graph, + legacy::GraphCSRView& result); // EIDecl for int + float extern template void coo_to_csr_inplace( - GraphCOOView &graph, GraphCSRView &result); + legacy::GraphCOOView& graph, + legacy::GraphCSRView& result); // EIDecl for int + double extern template void coo_to_csr_inplace( - GraphCOOView &graph, GraphCSRView &result); + legacy::GraphCOOView& graph, + legacy::GraphCSRView& result); // EIDecl for int64_t + float extern template void coo_to_csr_inplace( - GraphCOOView &graph, GraphCSRView &result); + legacy::GraphCOOView& graph, + legacy::GraphCSRView& result); // EIDecl for int64_t + double extern template void coo_to_csr_inplace( - GraphCOOView &graph, GraphCSRView &result); + legacy::GraphCOOView& graph, + legacy::GraphCSRView& result); } // namespace cugraph diff --git a/cpp/src/converters/permute_graph.cuh b/cpp/src/converters/permute_graph.cuh index b5b2de83e9b..024dfc2f3a7 100644 --- a/cpp/src/converters/permute_graph.cuh +++ b/cpp/src/converters/permute_graph.cuh @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. @@ -14,18 +14,18 @@ * limitations under the License. */ #include -#include -#include +#include +#include +#include #include "converters/COOtoCSR.cuh" -#include "utilities/graph_utils.cuh" namespace cugraph { namespace detail { template struct permutation_functor { - IdxT const *permutation; - permutation_functor(IdxT const *p) : permutation(p) {} + IdxT const* permutation; + permutation_functor(IdxT const* p) : permutation(p) {} __host__ __device__ IdxT operator()(IdxT in) const { return permutation[in]; } }; @@ -42,9 +42,9 @@ struct permutation_functor { * @return The permuted graph. */ template -void permute_graph(GraphCSRView const &graph, - vertex_t const *permutation, - GraphCSRView result, +void permute_graph(legacy::GraphCSRView const& graph, + vertex_t const* permutation, + legacy::GraphCSRView result, cudaStream_t stream = 0) { // Create a COO out of the CSR @@ -52,9 +52,9 @@ void permute_graph(GraphCSRView const &graph, rmm::device_vector dst_vertices_v(graph.number_of_edges); rmm::device_vector weights_v(graph.number_of_edges); - vertex_t *d_src = src_vertices_v.data().get(); - vertex_t *d_dst = dst_vertices_v.data().get(); - weight_t *d_weights = weights_v.data().get(); + vertex_t* d_src = src_vertices_v.data().get(); + vertex_t* d_dst = dst_vertices_v.data().get(); + weight_t* d_weights = weights_v.data().get(); graph.get_source_indices(d_src); @@ -76,7 +76,7 @@ void permute_graph(GraphCSRView const &graph, d_dst, pf); - GraphCOOView graph_coo; + legacy::GraphCOOView graph_coo; graph_coo.number_of_vertices = graph.number_of_vertices; graph_coo.number_of_edges = graph.number_of_edges; diff --git a/cpp/src/converters/renumber.cu b/cpp/src/converters/renumber.cu deleted file mode 100644 index 9aedbc70e8b..00000000000 --- a/cpp/src/converters/renumber.cu +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "renumber.cuh" - -namespace cugraph { - -template -std::unique_ptr renumber_vertices( - ET number_of_edges, - VT_IN const *src, - VT_IN const *dst, - VT_OUT *src_renumbered, - VT_OUT *dst_renumbered, - ET *map_size, - rmm::mr::device_memory_resource *mr = rmm::mr::get_current_device_resource()) - -{ - // - // For now, let's just specify a default value of the hash size. - // This should be configurable. - // - // FIXME: cudf has a hash table implementation (moving to cuCollections) - // that is dynamic. We should use it instead, it will be faster - // and dynamically adjust to data sizes. - // - int hash_size = 8191; - - return cugraph::detail::renumber_vertices(number_of_edges, - src, - dst, - src_renumbered, - dst_renumbered, - map_size, - cugraph::detail::HashFunctionObjectInt(hash_size), - thrust::less(), - mr); -} - -template std::unique_ptr renumber_vertices(int32_t, - int64_t const *, - int64_t const *, - int32_t *, - int32_t *, - int32_t *, - rmm::mr::device_memory_resource *); -template std::unique_ptr renumber_vertices(int32_t, - int32_t const *, - int32_t const *, - int32_t *, - int32_t *, - int32_t *, - rmm::mr::device_memory_resource *); - -} // namespace cugraph diff --git a/cpp/src/converters/renumber.cuh b/cpp/src/converters/renumber.cuh deleted file mode 100644 index 263d7199c10..00000000000 --- a/cpp/src/converters/renumber.cuh +++ /dev/null @@ -1,321 +0,0 @@ -/* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#define CUB_STDERR - -#include - -#include - -#include -#include -#include -#include - -#include -#include - -#include -#include "sort/bitonic.cuh" -#include "utilities/graph_utils.cuh" - -namespace cugraph { -namespace detail { - -namespace renumber { -typedef uint32_t hash_type; -typedef uint32_t index_type; -} // namespace renumber - -class HashFunctionObjectInt { - public: - HashFunctionObjectInt(renumber::hash_type hash_size) : hash_size_(hash_size) {} - - template - __device__ __inline__ renumber::hash_type operator()(const VertexIdType &vertex_id) const - { - return ((vertex_id % hash_size_) + hash_size_) % hash_size_; - } - - renumber::hash_type getHashSize() const { return hash_size_; } - - private: - renumber::hash_type hash_size_; -}; - -/** - * @brief Renumber vertices to a dense numbering (0..vertex_size-1) - * - * This is a templated function so it can take 32 or 64 bit integers. The - * intention is to take source and destination vertex ids that might be - * sparsely scattered across the range and push things down to a dense - * numbering. - * - * Arrays src, dst, src_renumbered, dst_renumbered and numbering_map are - * assumed to be pre-allocated. numbering_map is best safely allocated - * to store 2 * size vertices. - * - * @param[in] size Number of edges - * @param[in] src List of source vertices - * @param[in] dst List of dest vertices - * @param[out] src_renumbered List of source vertices, renumbered - * @param[out] dst_renumbered List of dest vertices, renumbered - * @param[out] vertex_size Number of unique vertices - * @param[out] numbering_map Map of new vertex id to original vertex id. numbering_map[newId] - * = oldId - * - */ -template -std::unique_ptr renumber_vertices(T_size size, - const T_in *src, - const T_in *dst, - T_out *src_renumbered, - T_out *dst_renumbered, - T_size *map_size, - Hash_t hash, - Compare_t compare, - rmm::mr::device_memory_resource *mr) -{ - // - // This function will allocate numbering_map to be the exact size needed - // (user doesn't know a priori how many unique vertices there are. - // - // Here's the idea: Create a hash table. Since we're dealing with integers, - // we can take the integer modulo some prime p to create hash buckets. Then - // we dedupe the hash buckets to create a deduped set of entries. This hash - // table can then be used to renumber everything. - // - // We need 2 arrays for hash indexes, and one array for data - // - cudaStream_t stream = nullptr; - - renumber::hash_type hash_size = hash.getHashSize(); - - rmm::device_vector hash_data_v(2 * size); - rmm::device_vector hash_bins_start_v(1 + hash_size, - renumber::index_type{0}); - rmm::device_vector hash_bins_end_v(1 + hash_size); - - T_in *hash_data = hash_data_v.data().get(); - renumber::index_type *hash_bins_start = hash_bins_start_v.data().get(); - renumber::index_type *hash_bins_end = hash_bins_end_v.data().get(); - - // - // Pass 1: count how many vertex ids end up in each hash bin - // - thrust::for_each(rmm::exec_policy(stream)->on(stream), - src, - src + size, - [hash_bins_start, hash] __device__(T_in vid) { - atomicAdd(hash_bins_start + hash(vid), renumber::index_type{1}); - }); - - thrust::for_each(rmm::exec_policy(stream)->on(stream), - dst, - dst + size, - [hash_bins_start, hash] __device__(T_in vid) { - atomicAdd(hash_bins_start + hash(vid), renumber::index_type{1}); - }); - - // - // Compute exclusive sum and copy it into both hash_bins_start and - // hash_bins_end. hash_bins_end will be used to populate the - // hash_data array and at the end will identify the end of - // each range. - // - thrust::exclusive_scan(rmm::exec_policy(stream)->on(stream), - hash_bins_start, - hash_bins_start + hash_size + 1, - hash_bins_end); - - CUDA_TRY(cudaMemcpy(hash_bins_start, - hash_bins_end, - (hash_size + 1) * sizeof(renumber::hash_type), - cudaMemcpyDeviceToDevice)); - - // - // Pass 2: Populate hash_data with data from the hash bins. - // - thrust::for_each(rmm::exec_policy(stream)->on(stream), - src, - src + size, - [hash_bins_end, hash_data, hash] __device__(T_in vid) { - uint32_t hash_index = hash(vid); - renumber::index_type hash_offset = atomicAdd(&hash_bins_end[hash_index], 1); - hash_data[hash_offset] = vid; - }); - - thrust::for_each(rmm::exec_policy(stream)->on(stream), - dst, - dst + size, - [hash_bins_end, hash_data, hash] __device__(T_in vid) { - uint32_t hash_index = hash(vid); - renumber::index_type hash_offset = atomicAdd(&hash_bins_end[hash_index], 1); - hash_data[hash_offset] = vid; - }); - - // - // Now that we have data in hash bins, we'll do a segmented sort of the has bins - // to sort each bin. This will allow us to identify duplicates (all duplicates - // are in the same hash bin so they will end up sorted consecutively). - // - renumber::index_type size_as_int = size; - cugraph::sort::bitonic::segmented_sort( - hash_size, size_as_int, hash_bins_start, hash_bins_end, hash_data, compare, stream); - - // - // Now we rinse and repeat. hash_data contains the data organized into sorted - // hash bins. This allows us to identify duplicates. We'll start over but - // we'll skip the duplicates when we repopulate the hash table. - // - - // - // Pass 3: count how many vertex ids end up in each hash bin after deduping - // - CUDA_TRY(cudaMemset(hash_bins_start, 0, (1 + hash_size) * sizeof(renumber::index_type))); - - thrust::for_each( - rmm::exec_policy(stream)->on(stream), - thrust::make_counting_iterator(0), - thrust::make_counting_iterator(2 * size), - [hash_data, hash_bins_start, hash, compare, size] __device__(renumber::index_type idx) { - // - // Two items (a and b) are equal if - // compare(a,b) is false and compare(b,a) - // is also false. If either is true then - // a and b are not equal. - // - // Note that if there are k duplicate - // instances of an entry, only the LAST - // entry will be counted - // - bool unique = ((idx + 1) == (2 * size)) || compare(hash_data[idx], hash_data[idx + 1]) || - compare(hash_data[idx + 1], hash_data[idx]); - - if (unique) atomicAdd(hash_bins_start + hash(hash_data[idx]), renumber::index_type{1}); - }); - - // - // Compute exclusive sum and copy it into both hash_bins_start and - // hash bins end. - // - thrust::exclusive_scan(rmm::exec_policy(stream)->on(stream), - hash_bins_start, - hash_bins_start + hash_size + 1, - hash_bins_end); - - CUDA_TRY(cudaMemcpy(hash_bins_start, - hash_bins_end, - (hash_size + 1) * sizeof(renumber::hash_type), - cudaMemcpyDeviceToDevice)); - - // - // The last entry in the array (hash_bins_end[hash_size]) is the - // total number of unique vertices - // - renumber::index_type temp = 0; - CUDA_TRY(cudaMemcpy( - &temp, hash_bins_end + hash_size, sizeof(renumber::index_type), cudaMemcpyDeviceToHost)); - *map_size = temp; - - rmm::device_buffer numbering_map(temp * sizeof(T_in), stream, mr); - T_in *local_numbering_map = static_cast(numbering_map.data()); - - // - // Pass 4: Populate hash_data with data from the hash bins after deduping - // - thrust::for_each(rmm::exec_policy(stream)->on(stream), - thrust::make_counting_iterator(0), - thrust::make_counting_iterator(2 * size), - [hash_bins_end, hash_data, local_numbering_map, hash, compare, size] __device__( - renumber::index_type idx) { - bool unique = ((idx + 1) == (2 * size)) || - compare(hash_data[idx], hash_data[idx + 1]) || - compare(hash_data[idx + 1], hash_data[idx]); - - if (unique) { - uint32_t hash_index = hash(hash_data[idx]); - renumber::index_type hash_offset = atomicAdd(&hash_bins_end[hash_index], 1); - local_numbering_map[hash_offset] = hash_data[idx]; - } - }); - - // - // At this point, hash_bins_start and numbering_map partition the - // unique data into a hash table. - // - - // - // If we do a segmented sort now, we can do the final lookups. - // - size_as_int = size; - cugraph::sort::bitonic::segmented_sort( - hash_size, size_as_int, hash_bins_start, hash_bins_end, local_numbering_map, compare, stream); - - // - // Renumber the input. For each vertex, identify the - // hash bin, and then search the hash bin for the - // record that matches, the relative offset between that - // element and the beginning of the array is the vertex - // id in the renumbered map. - // - thrust::for_each(rmm::exec_policy(stream)->on(stream), - thrust::make_counting_iterator(0), - thrust::make_counting_iterator(size), - [local_numbering_map, - hash_bins_start, - hash_bins_end, - hash, - src, - src_renumbered, - compare] __device__(renumber::index_type idx) { - renumber::hash_type tmp = hash(src[idx]); - const T_in *id = - thrust::lower_bound(thrust::seq, - local_numbering_map + hash_bins_start[tmp], - local_numbering_map + hash_bins_end[tmp], - src[idx], - compare); - src_renumbered[idx] = id - local_numbering_map; - }); - - thrust::for_each(rmm::exec_policy(stream)->on(stream), - thrust::make_counting_iterator(0), - thrust::make_counting_iterator(size), - [local_numbering_map, - hash_bins_start, - hash_bins_end, - hash, - dst, - dst_renumbered, - compare] __device__(renumber::index_type idx) { - renumber::hash_type tmp = hash(dst[idx]); - const T_in *id = - thrust::lower_bound(thrust::seq, - local_numbering_map + hash_bins_start[tmp], - local_numbering_map + hash_bins_end[tmp], - dst[idx], - compare); - dst_renumbered[idx] = id - local_numbering_map; - }); - - return std::make_unique(std::move(numbering_map)); -} - -} // namespace detail -} // namespace cugraph diff --git a/cpp/src/cores/core_number.cu b/cpp/src/cores/core_number.cu index cd2b928a81e..b23e7a25405 100644 --- a/cpp/src/cores/core_number.cu +++ b/cpp/src/cores/core_number.cu @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. @@ -17,15 +17,15 @@ #include #include #include -#include -#include +#include +#include //#include namespace cugraph { namespace detail { template -void core_number(GraphCSRView const &graph, int *core_number) +void core_number(legacy::GraphCSRView const& graph, int* core_number) { using HornetGraph = hornet::gpu::HornetStatic; using HornetInit = hornet::HornetInit; @@ -38,9 +38,9 @@ void core_number(GraphCSRView const &graph, int *core_number) struct FilterEdges { int k; - int *core_number; + int* core_number; - FilterEdges(int _k, int *d_core_num) : k(_k), core_number(d_core_num) {} + FilterEdges(int _k, int* d_core_num) : k(_k), core_number(d_core_num) {} template __host__ __device__ bool operator()(T t) @@ -52,9 +52,9 @@ struct FilterEdges { }; template -void extract_edges(GraphCOOView const &i_graph, - GraphCOOView &o_graph, - VT *d_core, +void extract_edges(legacy::GraphCOOView const& i_graph, + legacy::GraphCOOView& o_graph, + VT* d_core, int k) { cudaStream_t stream{nullptr}; @@ -96,13 +96,13 @@ void extract_edges(GraphCOOView const &i_graph, // i.e. All edges (s,d,w) in in_graph are copied over to out_graph // if core_num[s] and core_num[d] are greater than or equal to k. template -std::unique_ptr> extract_subgraph( - GraphCOOView const &in_graph, - int const *vid, - int const *core_num, +std::unique_ptr> extract_subgraph( + legacy::GraphCOOView const& in_graph, + int const* vid, + int const* core_num, int k, int len, - rmm::mr::device_memory_resource *mr = rmm::mr::get_current_device_resource()) + rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource()) { cudaStream_t stream{nullptr}; @@ -112,14 +112,14 @@ std::unique_ptr> extract_subgraph( thrust::scatter( rmm::exec_policy(stream)->on(stream), core_num, core_num + len, vid, sorted_core_num.begin()); - VT *d_sorted_core_num = sorted_core_num.data().get(); + VT* d_sorted_core_num = sorted_core_num.data().get(); // Count number of edges in the input graph that satisfy kcore conditions // i.e. core_num[src] and core_num[dst] are both greater than or equal to k auto edge = thrust::make_zip_iterator(thrust::make_tuple(in_graph.src_indices, in_graph.dst_indices)); - auto out_graph = std::make_unique>( + auto out_graph = std::make_unique>( in_graph.number_of_vertices, thrust::count_if(rmm::exec_policy(stream)->on(stream), edge, @@ -129,7 +129,7 @@ std::unique_ptr> extract_subgraph( stream, mr); - GraphCOOView out_graph_view = out_graph->view(); + legacy::GraphCOOView out_graph_view = out_graph->view(); extract_edges(in_graph, out_graph_view, d_sorted_core_num, k); return out_graph; @@ -138,41 +138,42 @@ std::unique_ptr> extract_subgraph( } // namespace detail template -void core_number(GraphCSRView const &graph, VT *core_number) +void core_number(legacy::GraphCSRView const& graph, VT* core_number) { return detail::core_number(graph, core_number); } template -std::unique_ptr> k_core(GraphCOOView const &in_graph, - int k, - VT const *vertex_id, - VT const *core_number, - VT num_vertex_ids, - rmm::mr::device_memory_resource *mr) +std::unique_ptr> k_core( + legacy::GraphCOOView const& in_graph, + int k, + VT const* vertex_id, + VT const* core_number, + VT num_vertex_ids, + rmm::mr::device_memory_resource* mr) { - CUGRAPH_EXPECTS(vertex_id != nullptr, "Invalid API parameter: vertex_id is NULL"); - CUGRAPH_EXPECTS(core_number != nullptr, "Invalid API parameter: core_number is NULL"); - CUGRAPH_EXPECTS(k >= 0, "Invalid API parameter: k must be >= 0"); + CUGRAPH_EXPECTS(vertex_id != nullptr, "Invalid input argument: vertex_id is NULL"); + CUGRAPH_EXPECTS(core_number != nullptr, "Invalid input argument: core_number is NULL"); + CUGRAPH_EXPECTS(k >= 0, "Invalid input argument: k must be >= 0"); return detail::extract_subgraph(in_graph, vertex_id, core_number, k, num_vertex_ids, mr); } -template void core_number(GraphCSRView const &, - int32_t *core_number); -template std::unique_ptr> k_core( - GraphCOOView const &, - int, - int32_t const *, - int32_t const *, - int32_t, - rmm::mr::device_memory_resource *); -template std::unique_ptr> k_core( - GraphCOOView const &, +template void core_number( + legacy::GraphCSRView const&, int32_t* core_number); +template std::unique_ptr> k_core( + legacy::GraphCOOView const&, int, - int32_t const *, - int32_t const *, + int32_t const*, + int32_t const*, int32_t, - rmm::mr::device_memory_resource *); + rmm::mr::device_memory_resource*); +template std::unique_ptr> +k_core(legacy::GraphCOOView const&, + int, + int32_t const*, + int32_t const*, + int32_t, + rmm::mr::device_memory_resource*); } // namespace cugraph diff --git a/cpp/src/detail/shuffle_wrappers.cu b/cpp/src/detail/shuffle_wrappers.cu new file mode 100644 index 00000000000..adf5fdfbc11 --- /dev/null +++ b/cpp/src/detail/shuffle_wrappers.cu @@ -0,0 +1,229 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include + +#include + +#include + +#include + +namespace cugraph { +namespace detail { + +template +std::tuple, + rmm::device_uvector, + std::optional>> +shuffle_edgelist_by_edge(raft::handle_t const& handle, + rmm::device_uvector& d_edgelist_rows, + rmm::device_uvector& d_edgelist_cols, + std::optional>& d_edgelist_weights, + bool store_transposed) +{ + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_size = row_comm.get_size(); + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_size = col_comm.get_size(); + + // TODO: Make a shuffle_edges and shuffle_vertices out of these... + rmm::device_uvector d_rx_edgelist_rows(0, handle.get_stream()); + rmm::device_uvector d_rx_edgelist_cols(0, handle.get_stream()); + std::optional> d_rx_edgelist_weights{std::nullopt}; + if (d_edgelist_weights) { + auto edge_first = thrust::make_zip_iterator( + thrust::make_tuple(store_transposed ? d_edgelist_cols.begin() : d_edgelist_rows.begin(), + store_transposed ? d_edgelist_rows.begin() : d_edgelist_cols.begin(), + (*d_edgelist_weights).begin())); + + std::forward_as_tuple(std::tie(store_transposed ? d_rx_edgelist_cols : d_rx_edgelist_rows, + store_transposed ? d_rx_edgelist_rows : d_rx_edgelist_cols, + d_rx_edgelist_weights), + std::ignore) = + cugraph::experimental::groupby_gpuid_and_shuffle_values( + comm, // handle.get_comms(), + edge_first, + edge_first + d_edgelist_rows.size(), + [key_func = + cugraph::experimental::detail::compute_gpu_id_from_edge_t{ + comm_size, row_comm_size, col_comm_size}] __device__(auto val) { + return key_func(thrust::get<0>(val), thrust::get<1>(val)); + }, + handle.get_stream()); + } else { + auto edge_first = thrust::make_zip_iterator( + thrust::make_tuple(store_transposed ? d_edgelist_cols.begin() : d_edgelist_rows.begin(), + store_transposed ? d_edgelist_rows.begin() : d_edgelist_cols.begin())); + + std::forward_as_tuple(std::tie(store_transposed ? d_rx_edgelist_cols : d_rx_edgelist_rows, + store_transposed ? d_rx_edgelist_rows : d_rx_edgelist_cols), + std::ignore) = + cugraph::experimental::groupby_gpuid_and_shuffle_values( + comm, // handle.get_comms(), + edge_first, + edge_first + d_edgelist_rows.size(), + [key_func = + cugraph::experimental::detail::compute_gpu_id_from_edge_t{ + comm_size, row_comm_size, col_comm_size}] __device__(auto val) { + return key_func(thrust::get<0>(val), thrust::get<1>(val)); + }, + handle.get_stream()); + } + + return std::make_tuple( + std::move(d_rx_edgelist_rows), std::move(d_rx_edgelist_cols), std::move(d_rx_edgelist_weights)); +} + +template std::tuple, + rmm::device_uvector, + std::optional>> +shuffle_edgelist_by_edge(raft::handle_t const& handle, + rmm::device_uvector& d_edgelist_rows, + rmm::device_uvector& d_edgelist_cols, + std::optional>& d_edgelist_weights, + bool store_transposed); + +template std::tuple, + rmm::device_uvector, + std::optional>> +shuffle_edgelist_by_edge(raft::handle_t const& handle, + rmm::device_uvector& d_edgelist_rows, + rmm::device_uvector& d_edgelist_cols, + std::optional>& d_edgelist_weights, + bool store_transposed); + +template std::tuple, + rmm::device_uvector, + std::optional>> +shuffle_edgelist_by_edge(raft::handle_t const& handle, + rmm::device_uvector& d_edgelist_rows, + rmm::device_uvector& d_edgelist_cols, + std::optional>& d_edgelist_weights, + bool store_transposed); + +template std::tuple, + rmm::device_uvector, + std::optional>> +shuffle_edgelist_by_edge(raft::handle_t const& handle, + rmm::device_uvector& d_edgelist_rows, + rmm::device_uvector& d_edgelist_cols, + std::optional>& d_edgelist_weights, + bool store_transposed); + +template +rmm::device_uvector shuffle_vertices(raft::handle_t const& handle, + rmm::device_uvector& d_vertices) +{ + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + + rmm::device_uvector d_rx_vertices(0, handle.get_stream()); + std::tie(d_rx_vertices, std::ignore) = cugraph::experimental::groupby_gpuid_and_shuffle_values( + comm, // handle.get_comms(), + d_vertices.begin(), + d_vertices.end(), + [key_func = + cugraph::experimental::detail::compute_gpu_id_from_vertex_t{ + comm_size}] __device__(auto val) { return key_func(val); }, + handle.get_stream()); + + return d_rx_vertices; +} + +template rmm::device_uvector shuffle_vertices(raft::handle_t const& handle, + rmm::device_uvector& d_vertices); + +template rmm::device_uvector shuffle_vertices(raft::handle_t const& handle, + rmm::device_uvector& d_vertices); + +template +rmm::device_uvector groupby_and_count_by_edge( + raft::handle_t const& handle, + rmm::device_uvector& d_edgelist_rows, + rmm::device_uvector& d_edgelist_cols, + std::optional>& d_edgelist_weights, + size_t number_of_local_adj_matrix_partitions) +{ + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + auto const comm_rank = comm.get_rank(); + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_size = row_comm.get_size(); + auto const row_comm_rank = row_comm.get_rank(); + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_size = col_comm.get_size(); + auto const col_comm_rank = col_comm.get_rank(); + + auto local_partition_id_op = + [comm_size, + key_func = cugraph::experimental::detail::compute_partition_id_from_edge_t{ + comm_size, row_comm_size, col_comm_size}] __device__(auto pair) { + return key_func(thrust::get<0>(pair), thrust::get<1>(pair)) / + comm_size; // global partition id to local partition id + }; + + auto pair_first = + thrust::make_zip_iterator(thrust::make_tuple(d_edgelist_rows.begin(), d_edgelist_cols.begin())); + + return d_edgelist_weights + ? cugraph::experimental::groupby_and_count(pair_first, + pair_first + d_edgelist_rows.size(), + d_edgelist_weights->begin(), + local_partition_id_op, + number_of_local_adj_matrix_partitions, + handle.get_stream()) + : cugraph::experimental::groupby_and_count(pair_first, + pair_first + d_edgelist_rows.size(), + local_partition_id_op, + number_of_local_adj_matrix_partitions, + handle.get_stream()); +} + +template rmm::device_uvector groupby_and_count_by_edge( + raft::handle_t const& handle, + rmm::device_uvector& d_edgelist_rows, + rmm::device_uvector& d_edgelist_cols, + std::optional>& d_edgelist_weights, + size_t number_of_local_adj_matrix_partitions); + +template rmm::device_uvector groupby_and_count_by_edge( + raft::handle_t const& handle, + rmm::device_uvector& d_edgelist_rows, + rmm::device_uvector& d_edgelist_cols, + std::optional>& d_edgelist_weights, + size_t number_of_local_adj_matrix_partitions); + +template rmm::device_uvector groupby_and_count_by_edge( + raft::handle_t const& handle, + rmm::device_uvector& d_edgelist_rows, + rmm::device_uvector& d_edgelist_cols, + std::optional>& d_edgelist_weights, + size_t number_of_local_adj_matrix_partitions); + +template rmm::device_uvector groupby_and_count_by_edge( + raft::handle_t const& handle, + rmm::device_uvector& d_edgelist_rows, + rmm::device_uvector& d_edgelist_cols, + std::optional>& d_edgelist_weights, + size_t number_of_local_adj_matrix_partitions); + +} // namespace detail +} // namespace cugraph diff --git a/cpp/src/detail/utility_wrappers.cu b/cpp/src/detail/utility_wrappers.cu new file mode 100644 index 00000000000..83a37d6b316 --- /dev/null +++ b/cpp/src/detail/utility_wrappers.cu @@ -0,0 +1,97 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include + +#include + +#include +#include + +namespace cugraph { +namespace detail { + +template +void uniform_random_fill(rmm::cuda_stream_view const& stream_view, + value_t* d_value, + size_t size, + value_t min_value, + value_t max_value, + uint64_t seed) +{ + raft::random::Rng rng(seed); + rng.uniform(d_value, size, min_value, max_value, stream_view.value()); +} + +template void uniform_random_fill(rmm::cuda_stream_view const& stream_view, + float* d_value, + size_t size, + float min_value, + float max_value, + uint64_t seed); + +template void uniform_random_fill(rmm::cuda_stream_view const& stream_view, + double* d_value, + size_t size, + double min_value, + double max_value, + uint64_t seed); + +template +void sequence_fill(rmm::cuda_stream_view const& stream_view, + value_t* d_value, + size_t size, + value_t start_value) +{ + thrust::sequence(rmm::exec_policy(stream_view), d_value, d_value + size, start_value); +} + +template void sequence_fill(rmm::cuda_stream_view const& stream_view, + int32_t* d_value, + size_t size, + int32_t start_value); + +template void sequence_fill(rmm::cuda_stream_view const& stream_view, + int64_t* d_value, + size_t size, + int64_t start_value); + +template +vertex_t compute_maximum_vertex_id(rmm::cuda_stream_view const& stream_view, + rmm::device_uvector const& d_edgelist_rows, + rmm::device_uvector const& d_edgelist_cols) +{ + auto edge_first = + thrust::make_zip_iterator(thrust::make_tuple(d_edgelist_rows.begin(), d_edgelist_cols.begin())); + + return thrust::transform_reduce( + rmm::exec_policy(stream_view), + edge_first, + edge_first + d_edgelist_rows.size(), + [] __device__(auto e) { return std::max(thrust::get<0>(e), thrust::get<1>(e)); }, + vertex_t{0}, + thrust::maximum()); +} + +template int32_t compute_maximum_vertex_id(rmm::cuda_stream_view const& stream_view, + rmm::device_uvector const& d_edgelist_rows, + rmm::device_uvector const& d_edgelist_cols); + +template int64_t compute_maximum_vertex_id(rmm::cuda_stream_view const& stream_view, + rmm::device_uvector const& d_edgelist_rows, + rmm::device_uvector const& d_edgelist_cols); + +} // namespace detail +} // namespace cugraph diff --git a/cpp/src/experimental/bfs.cu b/cpp/src/experimental/bfs.cu index f297587a1d6..903228a79f6 100644 --- a/cpp/src/experimental/bfs.cu +++ b/cpp/src/experimental/bfs.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -14,13 +14,13 @@ * limitations under the License. */ -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include #include #include @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -41,9 +42,9 @@ namespace experimental { namespace detail { template -void bfs(raft::handle_t const &handle, - GraphViewType const &push_graph_view, - typename GraphViewType::vertex_type *distances, +void bfs(raft::handle_t const& handle, + GraphViewType const& push_graph_view, + typename GraphViewType::vertex_type* distances, PredecessorIterator predecessor_first, typename GraphViewType::vertex_type source_vertex, bool direction_optimizing, @@ -90,14 +91,12 @@ void bfs(raft::handle_t const &handle, // 3. initialize BFS frontier - enum class Bucket { cur, num_buckets }; - std::vector bucket_sizes(static_cast(Bucket::num_buckets), - push_graph_view.get_number_of_local_vertices()); - VertexFrontier, - vertex_t, + enum class Bucket { cur, next, num_buckets }; + VertexFrontier(Bucket::num_buckets)> - vertex_frontier(handle, bucket_sizes); + vertex_frontier(handle); if (push_graph_view.is_local_vertex_nocheck(source_vertex)) { vertex_frontier.get_bucket(static_cast(Bucket::cur)).insert(source_vertex); @@ -106,23 +105,19 @@ void bfs(raft::handle_t const &handle, // 4. BFS iteration vertex_t depth{0}; - auto cur_local_vertex_frontier_first = - vertex_frontier.get_bucket(static_cast(Bucket::cur)).begin(); - auto cur_vertex_frontier_aggregate_size = - vertex_frontier.get_bucket(static_cast(Bucket::cur)).aggregate_size(); while (true) { if (direction_optimizing) { CUGRAPH_FAIL("unimplemented."); } else { - vertex_partition_device_t vertex_partition(push_graph_view); + auto vertex_partition = vertex_partition_device_view_t( + push_graph_view.get_vertex_partition_view()); - auto cur_local_vertex_frontier_last = - vertex_frontier.get_bucket(static_cast(Bucket::cur)).end(); update_frontier_v_push_if_out_nbr( handle, push_graph_view, - cur_local_vertex_frontier_first, - cur_local_vertex_frontier_last, + vertex_frontier, + static_cast(Bucket::cur), + std::vector{static_cast(Bucket::next)}, thrust::make_constant_iterator(0) /* dummy */, thrust::make_constant_iterator(0) /* dummy */, [vertex_partition, distances] __device__( @@ -133,28 +128,27 @@ void bfs(raft::handle_t const &handle, *(distances + vertex_partition.get_local_vertex_offset_from_vertex_nocheck(dst)); if (distance != invalid_distance) { push = false; } } - // FIXME: need to test this works properly if payload size is 0 (returns a tuple of size - // 1) - return thrust::make_tuple(push, src); + return push ? thrust::optional{src} : thrust::nullopt; }, - reduce_op::any>(), + reduce_op::any(), distances, thrust::make_zip_iterator(thrust::make_tuple(distances, predecessor_first)), - vertex_frontier, - [depth] __device__(auto v_val, auto pushed_val) { - auto idx = (v_val == invalid_distance) - ? static_cast(Bucket::cur) - : VertexFrontier, vertex_t>::kInvalidBucketIdx; - return thrust::make_tuple(idx, depth + 1, thrust::get<0>(pushed_val)); + [depth] __device__(auto v, auto v_val, auto pushed_val) { + return (v_val == invalid_distance) + ? thrust::optional< + thrust::tuple>>{thrust::make_tuple( + static_cast(Bucket::next), + thrust::make_tuple(depth + 1, pushed_val))} + : thrust::nullopt; }); - auto new_vertex_frontier_aggregate_size = - vertex_frontier.get_bucket(static_cast(Bucket::cur)).aggregate_size() - - cur_vertex_frontier_aggregate_size; - if (new_vertex_frontier_aggregate_size == 0) { break; } - - cur_local_vertex_frontier_first = cur_local_vertex_frontier_last; - cur_vertex_frontier_aggregate_size += new_vertex_frontier_aggregate_size; + vertex_frontier.get_bucket(static_cast(Bucket::cur)).clear(); + vertex_frontier.get_bucket(static_cast(Bucket::cur)).shrink_to_fit(); + vertex_frontier.swap_buckets(static_cast(Bucket::cur), + static_cast(Bucket::next)); + if (vertex_frontier.get_bucket(static_cast(Bucket::cur)).aggregate_size() == 0) { + break; + } } depth++; @@ -165,17 +159,15 @@ void bfs(raft::handle_t const &handle, handle.get_stream())); // this is as necessary vertex_frontier will become out-of-scope once // this function returns (FIXME: should I stream sync in VertexFrontier // destructor?) - - return; } } // namespace detail template -void bfs(raft::handle_t const &handle, - graph_view_t const &graph_view, - vertex_t *distances, - vertex_t *predecessors, +void bfs(raft::handle_t const& handle, + graph_view_t const& graph_view, + vertex_t* distances, + vertex_t* predecessors, vertex_t source_vertex, bool direction_optimizing, vertex_t depth_limit, @@ -204,109 +196,109 @@ void bfs(raft::handle_t const &handle, // explicit instantiation -template void bfs(raft::handle_t const &handle, - graph_view_t const &graph_view, - int32_t *distances, - int32_t *predecessors, +template void bfs(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t* distances, + int32_t* predecessors, int32_t source_vertex, bool direction_optimizing, int32_t depth_limit, bool do_expensive_check); -template void bfs(raft::handle_t const &handle, - graph_view_t const &graph_view, - int32_t *distances, - int32_t *predecessors, +template void bfs(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t* distances, + int32_t* predecessors, int32_t source_vertex, bool direction_optimizing, int32_t depth_limit, bool do_expensive_check); -template void bfs(raft::handle_t const &handle, - graph_view_t const &graph_view, - int32_t *distances, - int32_t *predecessors, +template void bfs(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t* distances, + int32_t* predecessors, int32_t source_vertex, bool direction_optimizing, int32_t depth_limit, bool do_expensive_check); -template void bfs(raft::handle_t const &handle, - graph_view_t const &graph_view, - int32_t *distances, - int32_t *predecessors, +template void bfs(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t* distances, + int32_t* predecessors, int32_t source_vertex, bool direction_optimizing, int32_t depth_limit, bool do_expensive_check); -template void bfs(raft::handle_t const &handle, - graph_view_t const &graph_view, - int64_t *distances, - int64_t *predecessors, +template void bfs(raft::handle_t const& handle, + graph_view_t const& graph_view, + int64_t* distances, + int64_t* predecessors, int64_t source_vertex, bool direction_optimizing, int64_t depth_limit, bool do_expensive_check); -template void bfs(raft::handle_t const &handle, - graph_view_t const &graph_view, - int64_t *distances, - int64_t *predecessors, +template void bfs(raft::handle_t const& handle, + graph_view_t const& graph_view, + int64_t* distances, + int64_t* predecessors, int64_t source_vertex, bool direction_optimizing, int64_t depth_limit, bool do_expensive_check); -template void bfs(raft::handle_t const &handle, - graph_view_t const &graph_view, - int32_t *distances, - int32_t *predecessors, +template void bfs(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t* distances, + int32_t* predecessors, int32_t source_vertex, bool direction_optimizing, int32_t depth_limit, bool do_expensive_check); -template void bfs(raft::handle_t const &handle, - graph_view_t const &graph_view, - int32_t *distances, - int32_t *predecessors, +template void bfs(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t* distances, + int32_t* predecessors, int32_t source_vertex, bool direction_optimizing, int32_t depth_limit, bool do_expensive_check); -template void bfs(raft::handle_t const &handle, - graph_view_t const &graph_view, - int32_t *distances, - int32_t *predecessors, +template void bfs(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t* distances, + int32_t* predecessors, int32_t source_vertex, bool direction_optimizing, int32_t depth_limit, bool do_expensive_check); -template void bfs(raft::handle_t const &handle, - graph_view_t const &graph_view, - int32_t *distances, - int32_t *predecessors, +template void bfs(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t* distances, + int32_t* predecessors, int32_t source_vertex, bool direction_optimizing, int32_t depth_limit, bool do_expensive_check); -template void bfs(raft::handle_t const &handle, - graph_view_t const &graph_view, - int64_t *distances, - int64_t *predecessors, +template void bfs(raft::handle_t const& handle, + graph_view_t const& graph_view, + int64_t* distances, + int64_t* predecessors, int64_t source_vertex, bool direction_optimizing, int64_t depth_limit, bool do_expensive_check); -template void bfs(raft::handle_t const &handle, - graph_view_t const &graph_view, - int64_t *distances, - int64_t *predecessors, +template void bfs(raft::handle_t const& handle, + graph_view_t const& graph_view, + int64_t* distances, + int64_t* predecessors, int64_t source_vertex, bool direction_optimizing, int64_t depth_limit, diff --git a/cpp/src/experimental/coarsen_graph.cu b/cpp/src/experimental/coarsen_graph.cu new file mode 100644 index 00000000000..f34332464f2 --- /dev/null +++ b/cpp/src/experimental/coarsen_graph.cu @@ -0,0 +1,747 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace cugraph { +namespace experimental { +namespace detail { + +template +std::tuple, + rmm::device_uvector, + std::optional>> +decompress_matrix_partition_to_edgelist( + raft::handle_t const& handle, + matrix_partition_device_view_t const matrix_partition, + std::optional> const& segment_offsets) +{ + auto number_of_edges = matrix_partition.get_number_of_edges(); + rmm::device_uvector edgelist_major_vertices(number_of_edges, handle.get_stream()); + rmm::device_uvector edgelist_minor_vertices(number_of_edges, handle.get_stream()); + auto edgelist_weights = + matrix_partition.get_weights() + ? std::make_optional>(number_of_edges, handle.get_stream()) + : std::nullopt; + + decompress_matrix_partition_to_fill_edgelist_majors( + handle, matrix_partition, edgelist_major_vertices.data(), segment_offsets); + thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + matrix_partition.get_indices(), + matrix_partition.get_indices() + number_of_edges, + edgelist_minor_vertices.begin()); + if (edgelist_weights) { + thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + *(matrix_partition.get_weights()), + *(matrix_partition.get_weights()) + number_of_edges, + (*edgelist_weights).data()); + } + + return std::make_tuple(std::move(edgelist_major_vertices), + std::move(edgelist_minor_vertices), + std::move(edgelist_weights)); +} + +template +edge_t groupby_e_and_coarsen_edgelist(vertex_t* edgelist_major_vertices /* [INOUT] */, + vertex_t* edgelist_minor_vertices /* [INOUT] */, + std::optional edgelist_weights /* [INOUT] */, + edge_t number_of_edges, + cudaStream_t stream) +{ + auto pair_first = + thrust::make_zip_iterator(thrust::make_tuple(edgelist_major_vertices, edgelist_minor_vertices)); + + if (edgelist_weights) { + thrust::sort_by_key(rmm::exec_policy(stream)->on(stream), + pair_first, + pair_first + number_of_edges, + *edgelist_weights); + + rmm::device_uvector tmp_edgelist_major_vertices(number_of_edges, stream); + rmm::device_uvector tmp_edgelist_minor_vertices(tmp_edgelist_major_vertices.size(), + stream); + rmm::device_uvector tmp_edgelist_weights(tmp_edgelist_major_vertices.size(), stream); + auto it = thrust::reduce_by_key( + rmm::exec_policy(stream)->on(stream), + pair_first, + pair_first + number_of_edges, + (*edgelist_weights), + thrust::make_zip_iterator(thrust::make_tuple(tmp_edgelist_major_vertices.begin(), + tmp_edgelist_minor_vertices.begin())), + tmp_edgelist_weights.begin()); + auto ret = + static_cast(thrust::distance(tmp_edgelist_weights.begin(), thrust::get<1>(it))); + + auto edge_first = + thrust::make_zip_iterator(thrust::make_tuple(tmp_edgelist_major_vertices.begin(), + tmp_edgelist_minor_vertices.begin(), + tmp_edgelist_weights.begin())); + thrust::copy(rmm::exec_policy(stream)->on(stream), + edge_first, + edge_first + ret, + thrust::make_zip_iterator(thrust::make_tuple( + edgelist_major_vertices, edgelist_minor_vertices, *edgelist_weights))); + + return ret; + } else { + thrust::sort(rmm::exec_policy(stream)->on(stream), pair_first, pair_first + number_of_edges); + return static_cast(thrust::distance( + pair_first, + thrust::unique( + rmm::exec_policy(stream)->on(stream), pair_first, pair_first + number_of_edges))); + } +} + +template +std::tuple, + rmm::device_uvector, + std::optional>> +decompress_matrix_partition_to_relabeled_and_grouped_and_coarsened_edgelist( + raft::handle_t const& handle, + matrix_partition_device_view_t const matrix_partition, + vertex_t const* p_major_labels, + vertex_t const* p_minor_labels, + std::optional> const& segment_offsets) +{ + // FIXME: it might be possible to directly create relabled & coarsened edgelist from the + // compressed sparse format to save memory + + auto [edgelist_major_vertices, edgelist_minor_vertices, edgelist_weights] = + decompress_matrix_partition_to_edgelist(handle, matrix_partition, segment_offsets); + + auto pair_first = thrust::make_zip_iterator( + thrust::make_tuple(edgelist_major_vertices.begin(), edgelist_minor_vertices.begin())); + thrust::transform(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + pair_first, + pair_first + edgelist_major_vertices.size(), + pair_first, + [p_major_labels, + p_minor_labels, + major_first = matrix_partition.get_major_first(), + minor_first = matrix_partition.get_minor_first()] __device__(auto val) { + return thrust::make_tuple(p_major_labels[thrust::get<0>(val) - major_first], + p_minor_labels[thrust::get<1>(val) - minor_first]); + }); + + auto number_of_edges = groupby_e_and_coarsen_edgelist( + edgelist_major_vertices.data(), + edgelist_minor_vertices.data(), + edgelist_weights ? std::optional{(*edgelist_weights).data()} : std::nullopt, + static_cast(edgelist_major_vertices.size()), + handle.get_stream()); + edgelist_major_vertices.resize(number_of_edges, handle.get_stream()); + edgelist_major_vertices.shrink_to_fit(handle.get_stream()); + edgelist_minor_vertices.resize(number_of_edges, handle.get_stream()); + edgelist_minor_vertices.shrink_to_fit(handle.get_stream()); + if (edgelist_weights) { + (*edgelist_weights).resize(number_of_edges, handle.get_stream()); + (*edgelist_weights).shrink_to_fit(handle.get_stream()); + } + + return std::make_tuple(std::move(edgelist_major_vertices), + std::move(edgelist_minor_vertices), + std::move(edgelist_weights)); +} + +// multi-GPU version +template +std::enable_if_t< + multi_gpu, + std::tuple>, + rmm::device_uvector>> +coarsen_graph( + raft::handle_t const& handle, + graph_view_t const& graph_view, + vertex_t const* labels, + bool do_expensive_check) +{ + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + auto const comm_rank = comm.get_rank(); + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_size = row_comm.get_size(); + auto const row_comm_rank = row_comm.get_rank(); + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_size = col_comm.get_size(); + auto const col_comm_rank = col_comm.get_rank(); + + if (do_expensive_check) { + // currently, nothing to do + } + + // 1. construct coarsened edge list + + rmm::device_uvector adj_matrix_minor_labels( + store_transposed ? graph_view.get_number_of_local_adj_matrix_partition_rows() + : graph_view.get_number_of_local_adj_matrix_partition_cols(), + handle.get_stream()); + if (store_transposed) { + copy_to_adj_matrix_row(handle, graph_view, labels, adj_matrix_minor_labels.data()); + } else { + copy_to_adj_matrix_col(handle, graph_view, labels, adj_matrix_minor_labels.data()); + } + + std::vector> coarsened_edgelist_major_vertices{}; + std::vector> coarsened_edgelist_minor_vertices{}; + auto coarsened_edgelist_weights = + graph_view.is_weighted() ? std::make_optional>>({}) + : std::nullopt; + coarsened_edgelist_major_vertices.reserve(graph_view.get_number_of_local_adj_matrix_partitions()); + coarsened_edgelist_minor_vertices.reserve(coarsened_edgelist_major_vertices.size()); + if (coarsened_edgelist_weights) { + (*coarsened_edgelist_weights).reserve(coarsened_edgelist_major_vertices.size()); + } + for (size_t i = 0; i < graph_view.get_number_of_local_adj_matrix_partitions(); ++i) { + coarsened_edgelist_major_vertices.emplace_back(0, handle.get_stream()); + coarsened_edgelist_minor_vertices.emplace_back(0, handle.get_stream()); + if (coarsened_edgelist_weights) { + (*coarsened_edgelist_weights).emplace_back(0, handle.get_stream()); + } + } + // FIXME: we may compare performance/memory footprint with the hash_based approach especially when + // cuco::dynamic_map becomes available (so we don't need to preallocate memory assuming the worst + // case). We may be able to limit the memory requirement close to the final coarsened edgelist + // with the hash based approach. + for (size_t i = 0; i < graph_view.get_number_of_local_adj_matrix_partitions(); ++i) { + // 1-1. locally construct coarsened edge list + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (beginning of col_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + rmm::device_uvector major_labels( + store_transposed ? graph_view.get_number_of_local_adj_matrix_partition_cols(i) + : graph_view.get_number_of_local_adj_matrix_partition_rows(i), + handle.get_stream()); + if (col_comm_rank == static_cast(i)) { + // FIXME: this copy is unnecessary, beter fix RAFT comm's bcast to take const iterators for + // input + thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + labels, + labels + major_labels.size(), + major_labels.begin()); + } + device_bcast(col_comm, + major_labels.data(), + major_labels.data(), + major_labels.size(), + static_cast(i), + handle.get_stream()); + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (end of col_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + + auto [edgelist_major_vertices, edgelist_minor_vertices, edgelist_weights] = + decompress_matrix_partition_to_relabeled_and_grouped_and_coarsened_edgelist( + handle, + matrix_partition_device_view_t( + graph_view.get_matrix_partition_view(i)), + major_labels.data(), + adj_matrix_minor_labels.data(), + graph_view.get_local_adj_matrix_partition_segment_offsets(i)); + + // 1-2. globally shuffle + + std::tie(edgelist_major_vertices, edgelist_minor_vertices, edgelist_weights) = + cugraph::detail::shuffle_edgelist_by_edge( + handle, edgelist_major_vertices, edgelist_minor_vertices, edgelist_weights, false); + + // 1-3. append data to local adjacency matrix partitions + + // FIXME: we can skip this if groupby_gpuid_and_shuffle_values is updated to return sorted edge + // list based on the final matrix partition (maybe add + // groupby_adj_matrix_partition_and_shuffle_values). + + auto counts = cugraph::detail::groupby_and_count_by_edge( + handle, + edgelist_major_vertices, + edgelist_minor_vertices, + edgelist_weights, + graph_view.get_number_of_local_adj_matrix_partitions()); + + std::vector h_counts(counts.size()); + raft::update_host(h_counts.data(), counts.data(), counts.size(), handle.get_stream()); + handle.get_stream_view().synchronize(); + + std::vector h_displacements(h_counts.size(), size_t{0}); + std::partial_sum(h_counts.begin(), h_counts.end() - 1, h_displacements.begin() + 1); + + for (int j = 0; j < col_comm_size; ++j) { + auto number_of_partition_edges = groupby_e_and_coarsen_edgelist( + edgelist_major_vertices.begin() + h_displacements[j], + edgelist_minor_vertices.begin() + h_displacements[j], + edgelist_weights ? std::optional{(*edgelist_weights).data() + h_displacements[j]} + : std::nullopt, + h_counts[j], + handle.get_stream()); + + auto cur_size = coarsened_edgelist_major_vertices[j].size(); + // FIXME: this can lead to frequent costly reallocation; we may be able to avoid this if we + // can reserve address space to avoid expensive reallocation. + // https://devblogs.nvidia.com/introducing-low-level-gpu-virtual-memory-management + coarsened_edgelist_major_vertices[j].resize(cur_size + number_of_partition_edges, + handle.get_stream()); + coarsened_edgelist_minor_vertices[j].resize(coarsened_edgelist_major_vertices[j].size(), + handle.get_stream()); + if (coarsened_edgelist_weights) { + (*coarsened_edgelist_weights)[j].resize(coarsened_edgelist_major_vertices[j].size(), + handle.get_stream()); + + auto src_edge_first = + thrust::make_zip_iterator(thrust::make_tuple(edgelist_major_vertices.begin(), + edgelist_minor_vertices.begin(), + (*edgelist_weights).begin())) + + h_displacements[j]; + auto dst_edge_first = + thrust::make_zip_iterator(thrust::make_tuple(coarsened_edgelist_major_vertices[j].begin(), + coarsened_edgelist_minor_vertices[j].begin(), + (*coarsened_edgelist_weights)[j].begin())) + + cur_size; + thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + src_edge_first, + src_edge_first + number_of_partition_edges, + dst_edge_first); + } else { + auto src_edge_first = thrust::make_zip_iterator(thrust::make_tuple( + edgelist_major_vertices.begin(), edgelist_minor_vertices.begin())) + + h_displacements[j]; + auto dst_edge_first = thrust::make_zip_iterator( + thrust::make_tuple(coarsened_edgelist_major_vertices[j].begin(), + coarsened_edgelist_minor_vertices[j].begin())) + + cur_size; + thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + src_edge_first, + src_edge_first + number_of_partition_edges, + dst_edge_first); + } + } + } + + for (size_t i = 0; i < coarsened_edgelist_major_vertices.size(); ++i) { + auto number_of_partition_edges = groupby_e_and_coarsen_edgelist( + coarsened_edgelist_major_vertices[i].data(), + coarsened_edgelist_minor_vertices[i].data(), + coarsened_edgelist_weights ? std::optional{(*coarsened_edgelist_weights)[i].data()} + : std::nullopt, + static_cast(coarsened_edgelist_major_vertices[i].size()), + handle.get_stream()); + coarsened_edgelist_major_vertices[i].resize(number_of_partition_edges, handle.get_stream()); + coarsened_edgelist_major_vertices[i].shrink_to_fit(handle.get_stream()); + coarsened_edgelist_minor_vertices[i].resize(number_of_partition_edges, handle.get_stream()); + coarsened_edgelist_minor_vertices[i].shrink_to_fit(handle.get_stream()); + if (coarsened_edgelist_weights) { + (*coarsened_edgelist_weights)[i].resize(number_of_partition_edges, handle.get_stream()); + (*coarsened_edgelist_weights)[i].shrink_to_fit(handle.get_stream()); + } + } + + // 3. find unique labels for this GPU + + rmm::device_uvector unique_labels(graph_view.get_number_of_local_vertices(), + handle.get_stream()); + thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + labels, + labels + unique_labels.size(), + unique_labels.begin()); + thrust::sort(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + unique_labels.begin(), + unique_labels.end()); + unique_labels.resize( + thrust::distance(unique_labels.begin(), + thrust::unique(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + unique_labels.begin(), + unique_labels.end())), + handle.get_stream()); + + unique_labels = cugraph::detail::shuffle_vertices(handle, unique_labels); + + thrust::sort(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + unique_labels.begin(), + unique_labels.end()); + unique_labels.resize( + thrust::distance(unique_labels.begin(), + thrust::unique(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + unique_labels.begin(), + unique_labels.end())), + handle.get_stream()); + + // 4. renumber + + rmm::device_uvector renumber_map_labels(0, handle.get_stream()); + partition_t partition(std::vector(comm_size + 1, 0), + row_comm_size, + col_comm_size, + row_comm_rank, + col_comm_rank); + vertex_t number_of_vertices{}; + edge_t number_of_edges{}; + std::optional> segment_offsets{}; + { + std::vector major_ptrs(coarsened_edgelist_major_vertices.size()); + std::vector minor_ptrs(major_ptrs.size()); + std::vector counts(major_ptrs.size()); + for (size_t i = 0; i < coarsened_edgelist_major_vertices.size(); ++i) { + major_ptrs[i] = coarsened_edgelist_major_vertices[i].data(); + minor_ptrs[i] = coarsened_edgelist_minor_vertices[i].data(); + counts[i] = static_cast(coarsened_edgelist_major_vertices[i].size()); + } + std::tie(renumber_map_labels, partition, number_of_vertices, number_of_edges, segment_offsets) = + renumber_edgelist( + handle, + std::optional>{ + std::make_tuple(unique_labels.data(), static_cast(unique_labels.size()))}, + major_ptrs, + minor_ptrs, + counts, + do_expensive_check); + } + + // 5. build a graph + + std::vector> edgelists{}; + edgelists.resize(graph_view.get_number_of_local_adj_matrix_partitions()); + for (size_t i = 0; i < edgelists.size(); ++i) { + edgelists[i].p_src_vertices = store_transposed ? coarsened_edgelist_minor_vertices[i].data() + : coarsened_edgelist_major_vertices[i].data(); + edgelists[i].p_dst_vertices = store_transposed ? coarsened_edgelist_major_vertices[i].data() + : coarsened_edgelist_minor_vertices[i].data(); + edgelists[i].p_edge_weights = + coarsened_edgelist_weights + ? std::optional{(*coarsened_edgelist_weights)[i].data()} + : std::nullopt, + edgelists[i].number_of_edges = static_cast(coarsened_edgelist_major_vertices[i].size()); + } + + return std::make_tuple( + std::make_unique>( + handle, + edgelists, + partition, + number_of_vertices, + number_of_edges, + graph_properties_t{graph_view.is_symmetric(), false}, + segment_offsets), + std::move(renumber_map_labels)); +} + +// single-GPU version +template +std::enable_if_t< + !multi_gpu, + std::tuple>, + rmm::device_uvector>> +coarsen_graph( + raft::handle_t const& handle, + graph_view_t const& graph_view, + vertex_t const* labels, + bool do_expensive_check) +{ + if (do_expensive_check) { + // currently, nothing to do + } + + auto [coarsened_edgelist_major_vertices, + coarsened_edgelist_minor_vertices, + coarsened_edgelist_weights] = + decompress_matrix_partition_to_relabeled_and_grouped_and_coarsened_edgelist( + handle, + matrix_partition_device_view_t( + graph_view.get_matrix_partition_view()), + labels, + labels, + graph_view.get_local_adj_matrix_partition_segment_offsets(0)); + + rmm::device_uvector unique_labels(graph_view.get_number_of_vertices(), + handle.get_stream()); + thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + labels, + labels + unique_labels.size(), + unique_labels.begin()); + thrust::sort(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + unique_labels.begin(), + unique_labels.end()); + unique_labels.resize( + thrust::distance(unique_labels.begin(), + thrust::unique(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + unique_labels.begin(), + unique_labels.end())), + handle.get_stream()); + + auto [renumber_map_labels, segment_offsets] = renumber_edgelist( + handle, + std::optional>{ + std::make_tuple(unique_labels.data(), static_cast(unique_labels.size()))}, + coarsened_edgelist_major_vertices.data(), + coarsened_edgelist_minor_vertices.data(), + static_cast(coarsened_edgelist_major_vertices.size()), + do_expensive_check); + + edgelist_t edgelist{}; + edgelist.p_src_vertices = store_transposed ? coarsened_edgelist_minor_vertices.data() + : coarsened_edgelist_major_vertices.data(); + edgelist.p_dst_vertices = store_transposed ? coarsened_edgelist_major_vertices.data() + : coarsened_edgelist_minor_vertices.data(); + edgelist.p_edge_weights = coarsened_edgelist_weights + ? std::optional{(*coarsened_edgelist_weights).data()} + : std::nullopt; + edgelist.number_of_edges = static_cast(coarsened_edgelist_major_vertices.size()); + + return std::make_tuple( + std::make_unique>( + handle, + edgelist, + static_cast(renumber_map_labels.size()), + graph_properties_t{graph_view.is_symmetric(), false}, + segment_offsets), + std::move(renumber_map_labels)); +} + +} // namespace detail + +template +std::tuple>, + rmm::device_uvector> +coarsen_graph( + raft::handle_t const& handle, + graph_view_t const& graph_view, + vertex_t const* labels, + bool do_expensive_check) +{ + return detail::coarsen_graph(handle, graph_view, labels, do_expensive_check); +} + +// explicit instantiation + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int64_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int64_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int64_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int64_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int32_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int64_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int64_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int64_t const* labels, + bool do_expensive_check); + +template std::tuple>, + rmm::device_uvector> +coarsen_graph(raft::handle_t const& handle, + graph_view_t const& graph_view, + int64_t const* labels, + bool do_expensive_check); + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/src/experimental/graph.cu b/cpp/src/experimental/graph.cu index 3a2b7126d22..980356a96db 100644 --- a/cpp/src/experimental/graph.cu +++ b/cpp/src/experimental/graph.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -14,16 +14,16 @@ * limitations under the License. */ -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include -#include #include #include #include +#include #include #include @@ -59,62 +59,46 @@ struct out_of_range_t { } }; +// compress edge list (COO) to CSR (or CSC) or CSR + DCSR (CSC + DCSC) hybrid template -std:: - tuple, rmm::device_uvector, rmm::device_uvector> - edge_list_to_compressed_sparse(raft::handle_t const &handle, - edgelist_t const &edgelist, - vertex_t major_first, - vertex_t major_last, - vertex_t minor_first, - vertex_t minor_last) +std::tuple, + rmm::device_uvector, + std::optional>, + std::optional>> +compress_edgelist(edgelist_t const& edgelist, + vertex_t major_first, + std::optional major_hypersparse_first, + vertex_t major_last, + vertex_t minor_first, + vertex_t minor_last, + rmm::cuda_stream_view stream_view) { - rmm::device_uvector offsets((major_last - major_first) + 1, handle.get_stream()); - rmm::device_uvector indices(edgelist.number_of_edges, handle.get_stream()); - rmm::device_uvector weights( - edgelist.p_edge_weights != nullptr ? edgelist.number_of_edges : 0, handle.get_stream()); - thrust::fill(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - offsets.begin(), - offsets.end(), - edge_t{0}); - thrust::fill(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - indices.begin(), - indices.end(), - vertex_t{0}); - - // FIXME: need to performance test this code with R-mat graphs having highly-skewed degree - // distribution. If there is a small number of vertices with very large degrees, atomicAdd can - // sequentialize execution. CUDA9+ & Kepler+ provide complier/architectural optimizations to - // mitigate this impact - // (https://developer.nvidia.com/blog/cuda-pro-tip-optimized-filtering-warp-aggregated-atomics/), - // and we need to check this thrust::for_each based approach delivers the expected performance. - - // FIXME: also need to verify this approach is at least not significantly slower than the sorting - // based approach (this approach does not use extra memory, so better stick to this approach - // unless performance is significantly worse). + rmm::device_uvector offsets((major_last - major_first) + 1, stream_view); + rmm::device_uvector indices(edgelist.number_of_edges, stream_view); + auto weights = edgelist.p_edge_weights ? std::make_optional>( + edgelist.number_of_edges, stream_view) + : std::nullopt; + thrust::fill(rmm::exec_policy(stream_view), offsets.begin(), offsets.end(), edge_t{0}); + thrust::fill(rmm::exec_policy(stream_view), indices.begin(), indices.end(), vertex_t{0}); auto p_offsets = offsets.data(); - auto p_indices = indices.data(); - auto p_weights = - edgelist.p_edge_weights != nullptr ? weights.data() : static_cast(nullptr); - - thrust::for_each(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + thrust::for_each(rmm::exec_policy(stream_view), store_transposed ? edgelist.p_dst_vertices : edgelist.p_src_vertices, store_transposed ? edgelist.p_dst_vertices + edgelist.number_of_edges : edgelist.p_src_vertices + edgelist.number_of_edges, [p_offsets, major_first] __device__(auto v) { atomicAdd(p_offsets + (v - major_first), edge_t{1}); }); + thrust::exclusive_scan( + rmm::exec_policy(stream_view), offsets.begin(), offsets.end(), offsets.begin()); - thrust::exclusive_scan(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - offsets.begin(), - offsets.end(), - offsets.begin()); + auto p_indices = indices.data(); + if (edgelist.p_edge_weights) { + auto p_weights = (*weights).data(); - if (edgelist.p_edge_weights != nullptr) { auto edge_first = thrust::make_zip_iterator(thrust::make_tuple( - edgelist.p_src_vertices, edgelist.p_dst_vertices, edgelist.p_edge_weights)); - thrust::for_each(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + edgelist.p_src_vertices, edgelist.p_dst_vertices, *(edgelist.p_edge_weights))); + thrust::for_each(rmm::exec_policy(stream_view), edge_first, edge_first + edgelist.number_of_edges, [p_offsets, p_indices, p_weights, major_first] __device__(auto e) { @@ -137,10 +121,10 @@ std:: } else { auto edge_first = thrust::make_zip_iterator( thrust::make_tuple(edgelist.p_src_vertices, edgelist.p_dst_vertices)); - thrust::for_each(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + thrust::for_each(rmm::exec_policy(stream_view), edge_first, edge_first + edgelist.number_of_edges, - [p_offsets, p_indices, p_weights, major_first] __device__(auto e) { + [p_offsets, p_indices, major_first] __device__(auto e) { auto s = thrust::get<0>(e); auto d = thrust::get<1>(e); auto major = store_transposed ? d : s; @@ -157,45 +141,51 @@ std:: }); } - // FIXME: need to add an option to sort neighbor lists - - return std::make_tuple(std::move(offsets), std::move(indices), std::move(weights)); -} - -template -std::vector segment_degree_sorted_vertex_partition(raft::handle_t const &handle, - DegreeIterator degree_first, - DegreeIterator degree_last, - ThresholdIterator threshold_first, - ThresholdIterator threshold_last) -{ - auto num_elements = thrust::distance(degree_first, degree_last); - auto num_segments = thrust::distance(threshold_first, threshold_last) + 1; - - std::vector h_segment_offsets(num_segments + 1); - h_segment_offsets[0] = 0; - h_segment_offsets.back() = num_elements; - - rmm::device_uvector d_segment_offsets(num_segments - 1, handle.get_stream()); - - thrust::upper_bound(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), - degree_first, - degree_last, - threshold_first, - threshold_last, - d_segment_offsets.begin()); - - raft::update_host(h_segment_offsets.begin() + 1, - d_segment_offsets.begin(), - d_segment_offsets.size(), - handle.get_stream()); + auto dcs_nzd_vertices = major_hypersparse_first + ? std::make_optional>( + major_last - *major_hypersparse_first, stream_view) + : std::nullopt; + if (dcs_nzd_vertices) { + auto constexpr invalid_vertex = invalid_vertex_id::value; + + thrust::transform( + rmm::exec_policy(stream_view), + thrust::make_counting_iterator(*major_hypersparse_first), + thrust::make_counting_iterator(major_last), + (*dcs_nzd_vertices).begin(), + [major_first, offsets = offsets.data()] __device__(auto major) { + auto major_offset = major - major_first; + return offsets[major_offset + 1] - offsets[major_offset] > 0 ? major : invalid_vertex; + }); + + auto pair_first = thrust::make_zip_iterator(thrust::make_tuple( + (*dcs_nzd_vertices).begin(), offsets.begin() + (*major_hypersparse_first - major_first))); + (*dcs_nzd_vertices) + .resize(thrust::distance(pair_first, + thrust::remove_if(rmm::exec_policy(stream_view), + pair_first, + pair_first + (*dcs_nzd_vertices).size(), + [] __device__(auto pair) { + return thrust::get<0>(pair) == invalid_vertex; + })), + stream_view); + (*dcs_nzd_vertices).shrink_to_fit(stream_view); + if (static_cast((*dcs_nzd_vertices).size()) < major_last - *major_hypersparse_first) { + thrust::copy( + rmm::exec_policy(stream_view), + offsets.begin() + (major_last - major_first), + offsets.end(), + offsets.begin() + (*major_hypersparse_first - major_first) + (*dcs_nzd_vertices).size()); + offsets.resize((*major_hypersparse_first - major_first) + (*dcs_nzd_vertices).size() + 1, + stream_view); + offsets.shrink_to_fit(stream_view); + } + } - CUDA_TRY(cudaStreamSynchronize( - handle.get_stream())); // this is necessary as d_segment_offsets will become out-of-scope once - // this function returns and this function returns a host variable which - // can be used right after return. + // FIXME: need to add an option to sort neighbor lists - return h_segment_offsets; + return std::make_tuple( + std::move(offsets), std::move(indices), std::move(weights), std::move(dcs_nzd_vertices)); } } // namespace @@ -206,13 +196,13 @@ template graph_t>:: - graph_t(raft::handle_t const &handle, - std::vector> const &edgelists, - partition_t const &partition, + graph_t(raft::handle_t const& handle, + std::vector> const& edgelists, + partition_t const& partition, vertex_t number_of_vertices, edge_t number_of_edges, graph_properties_t properties, - bool sorted_by_global_degree_within_vertex_partition, + std::optional> const& segment_offsets, bool do_expensive_check) : detail::graph_base_t( handle, number_of_vertices, number_of_edges, properties), @@ -220,52 +210,53 @@ graph_tget_handle_ptr()->get_comms(); + auto& comm = this->get_handle_ptr()->get_comms(); auto const comm_size = comm.get_size(); - auto &row_comm = + auto& row_comm = this->get_handle_ptr()->get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); auto const row_comm_rank = row_comm.get_rank(); auto const row_comm_size = row_comm.get_size(); - auto &col_comm = + auto& col_comm = this->get_handle_ptr()->get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); auto const col_comm_rank = col_comm.get_rank(); auto const col_comm_size = col_comm.get_size(); - auto default_stream = this->get_handle_ptr()->get_stream(); + auto default_stream_view = this->get_handle_ptr()->get_stream_view(); - CUGRAPH_EXPECTS(edgelists.size() > 0, - "Invalid API parameter: edgelists.size() should be non-zero."); + CUGRAPH_EXPECTS(edgelists.size() == static_cast(col_comm_size), + "Invalid input argument: errneous edgelists.size()."); + CUGRAPH_EXPECTS( + !segment_offsets.has_value() || + ((*segment_offsets).size() == (detail::num_sparse_segments_per_vertex_partition + 1)) || + ((*segment_offsets).size() == (detail::num_sparse_segments_per_vertex_partition + 2)), + "Invalid input argument: segment_offsets.size() returns an invalid value."); - bool is_weighted = edgelists[0].p_edge_weights != nullptr; + auto is_weighted = edgelists[0].p_edge_weights.has_value(); + auto use_dcs = + segment_offsets + ? ((*segment_offsets).size() > (detail::num_sparse_segments_per_vertex_partition + 1)) + : false; CUGRAPH_EXPECTS( - std::any_of(edgelists.begin() + 1, + std::any_of(edgelists.begin(), edgelists.end(), [is_weighted](auto edgelist) { - return (edgelist.p_src_vertices == nullptr) || - (edgelist.p_dst_vertices == nullptr) || - (is_weighted && (edgelist.p_edge_weights == nullptr)) || - (!is_weighted && (edgelist.p_edge_weights != nullptr)); + return ((edgelist.number_of_edges > 0) && (edgelist.p_src_vertices == nullptr)) || + ((edgelist.number_of_edges > 0) && (edgelist.p_dst_vertices == nullptr)) || + (is_weighted && (edgelist.number_of_edges > 0) && + ((edgelist.p_edge_weights.has_value() == false) || + (*(edgelist.p_edge_weights) == nullptr))); }) == false, - "Invalid API parameter: edgelists[].p_src_vertices and edgelists[].p_dst_vertices should not " - "be nullptr and edgelists[].p_edge_weights should be nullptr (if edgelists[0].p_edge_weights " - "is nullptr) or should not be nullptr (otherwise)."); - - CUGRAPH_EXPECTS((partition.is_hypergraph_partitioned() && - (edgelists.size() == static_cast(col_comm_size))) || - (!(partition.is_hypergraph_partitioned()) && (edgelists.size() == 1)), - "Invalid API parameter: errneous edgelists.size()."); + "Invalid input argument: edgelists[].p_src_vertices and edgelists[].p_dst_vertices should not " + "be nullptr if edgelists[].number_of_edges > 0 and edgelists[].p_edge_weights should be " + "neither std::nullopt nor nullptr if weighted and edgelists[].number_of_edges > 0."); - // optional expensive checks (part 1/3) + // optional expensive checks (part 1/2) if (do_expensive_check) { edge_t number_of_local_edges_sum{}; for (size_t i = 0; i < edgelists.size(); ++i) { - vertex_t major_first{}; - vertex_t major_last{}; - vertex_t minor_first{}; - vertex_t minor_last{}; - std::tie(major_first, major_last) = partition.get_matrix_partition_major_range(i); - std::tie(minor_first, minor_last) = partition.get_matrix_partition_minor_range(); + auto [major_first, major_last] = partition.get_matrix_partition_major_range(i); + auto [minor_first, minor_last] = partition.get_matrix_partition_minor_range(); number_of_local_edges_sum += edgelists[i].number_of_edges; @@ -273,126 +264,95 @@ graph_ton(default_stream), + CUGRAPH_EXPECTS(thrust::count_if(rmm::exec_policy(default_stream_view), edge_first, edge_first + edgelists[i].number_of_edges, out_of_range_t{ major_first, major_last, minor_first, minor_last}) == 0, - "Invalid API parameter: edgelists[] have out-of-range values."); + "Invalid input argument: edgelists[] have out-of-range values."); } number_of_local_edges_sum = - host_scalar_allreduce(comm, number_of_local_edges_sum, default_stream); - CUGRAPH_EXPECTS(number_of_local_edges_sum == this->get_number_of_edges(), - "Invalid API parameter: the sum of local edges doe counts not match with " - "number_of_local_edges."); + host_scalar_allreduce(comm, number_of_local_edges_sum, default_stream_view.value()); + CUGRAPH_EXPECTS( + number_of_local_edges_sum == this->get_number_of_edges(), + "Invalid input argument: the sum of local edge counts does not match with number_of_edges."); CUGRAPH_EXPECTS( partition.get_vertex_partition_last(comm_size - 1) == number_of_vertices, - "Invalid API parameter: vertex partition should cover [0, number_of_vertices)."); + "Invalid input argument: vertex partition should cover [0, number_of_vertices)."); } - // convert edge list (COO) to compressed sparse format (CSR or CSC) + // aggregate segment_offsets + + if (segment_offsets) { + // FIXME: we need to add host_allgather + rmm::device_uvector d_segment_offsets((*segment_offsets).size(), default_stream_view); + raft::update_device(d_segment_offsets.data(), + (*segment_offsets).data(), + (*segment_offsets).size(), + default_stream_view.value()); + rmm::device_uvector d_aggregate_segment_offsets( + col_comm_size * d_segment_offsets.size(), default_stream_view); + col_comm.allgather(d_segment_offsets.data(), + d_aggregate_segment_offsets.data(), + d_segment_offsets.size(), + default_stream_view.value()); + + adj_matrix_partition_segment_offsets_ = + std::vector(d_aggregate_segment_offsets.size(), vertex_t{0}); + raft::update_host((*adj_matrix_partition_segment_offsets_).data(), + d_aggregate_segment_offsets.data(), + d_aggregate_segment_offsets.size(), + default_stream_view.value()); + + default_stream_view + .synchronize(); // this is necessary as adj_matrix_partition_segment_offsets_ can be used + // right after return. + } + + // compress edge list (COO) to CSR (or CSC) or CSR + DCSR (CSC + DCSC) hybrid adj_matrix_partition_offsets_.reserve(edgelists.size()); adj_matrix_partition_indices_.reserve(edgelists.size()); - adj_matrix_partition_weights_.reserve(is_weighted ? edgelists.size() : 0); + if (is_weighted) { + adj_matrix_partition_weights_ = std::vector>{}; + (*adj_matrix_partition_weights_).reserve(edgelists.size()); + } + if (use_dcs) { + adj_matrix_partition_dcs_nzd_vertices_ = std::vector>{}; + adj_matrix_partition_dcs_nzd_vertex_counts_ = std::vector{}; + (*adj_matrix_partition_dcs_nzd_vertices_).reserve(edgelists.size()); + (*adj_matrix_partition_dcs_nzd_vertex_counts_).reserve(edgelists.size()); + } for (size_t i = 0; i < edgelists.size(); ++i) { - vertex_t major_first{}; - vertex_t major_last{}; - vertex_t minor_first{}; - vertex_t minor_last{}; - std::tie(major_first, major_last) = partition.get_matrix_partition_major_range(i); - std::tie(minor_first, minor_last) = partition.get_matrix_partition_minor_range(); - - rmm::device_uvector offsets(0, default_stream); - rmm::device_uvector indices(0, default_stream); - rmm::device_uvector weights(0, default_stream); - std::tie(offsets, indices, weights) = edge_list_to_compressed_sparse( - *(this->get_handle_ptr()), edgelists[i], major_first, major_last, minor_first, minor_last); + auto [major_first, major_last] = partition.get_matrix_partition_major_range(i); + auto [minor_first, minor_last] = partition.get_matrix_partition_minor_range(); + auto major_hypersparse_first = + use_dcs ? std::optional{major_first + + (*adj_matrix_partition_segment_offsets_) + [(*segment_offsets).size() * i + + detail::num_sparse_segments_per_vertex_partition]} + : std::nullopt; + auto [offsets, indices, weights, dcs_nzd_vertices] = + compress_edgelist(edgelists[i], + major_first, + major_hypersparse_first, + major_last, + minor_first, + minor_last, + default_stream_view); + adj_matrix_partition_offsets_.push_back(std::move(offsets)); adj_matrix_partition_indices_.push_back(std::move(indices)); - if (is_weighted) { adj_matrix_partition_weights_.push_back(std::move(weights)); } - } - - // update degree-based segment offsets (to be used for graph analytics kernel optimization) - - if (sorted_by_global_degree_within_vertex_partition) { - auto degrees = detail::compute_major_degree( - *(this->get_handle_ptr()), adj_matrix_partition_offsets_, partition_); - - // optional expensive checks (part 2/3) - - if (do_expensive_check) { - CUGRAPH_EXPECTS(thrust::is_sorted(rmm::exec_policy(default_stream)->on(default_stream), - degrees.begin(), - degrees.end(), - thrust::greater{}), - "Invalid API parameter: sorted_by_global_degree_within_vertex_partition is " - "set to true, but degrees are not non-ascending."); + if (is_weighted) { (*adj_matrix_partition_weights_).push_back(std::move(*weights)); } + if (use_dcs) { + auto dcs_nzd_vertex_count = static_cast((*dcs_nzd_vertices).size()); + (*adj_matrix_partition_dcs_nzd_vertices_).push_back(std::move(*dcs_nzd_vertices)); + (*adj_matrix_partition_dcs_nzd_vertex_counts_).push_back(dcs_nzd_vertex_count); } - - static_assert(detail::num_segments_per_vertex_partition == 3); - static_assert((detail::low_degree_threshold <= detail::mid_degree_threshold) && - (detail::mid_degree_threshold <= std::numeric_limits::max())); - rmm::device_uvector d_thresholds(detail::num_segments_per_vertex_partition - 1, - default_stream); - std::vector h_thresholds = {static_cast(detail::low_degree_threshold), - static_cast(detail::mid_degree_threshold)}; - raft::update_device( - d_thresholds.data(), h_thresholds.data(), h_thresholds.size(), default_stream); - - rmm::device_uvector segment_offsets(detail::num_segments_per_vertex_partition + 1, - default_stream); - segment_offsets.set_element_async(0, 0, default_stream); - segment_offsets.set_element_async( - detail::num_segments_per_vertex_partition, degrees.size(), default_stream); - - thrust::upper_bound(rmm::exec_policy(default_stream)->on(default_stream), - degrees.begin(), - degrees.end(), - d_thresholds.begin(), - d_thresholds.end(), - segment_offsets.begin() + 1); - - rmm::device_uvector aggregate_segment_offsets(0, default_stream); - if (partition.is_hypergraph_partitioned()) { - rmm::device_uvector aggregate_segment_offsets( - col_comm_size * segment_offsets.size(), default_stream); - col_comm.allgather(segment_offsets.data(), - aggregate_segment_offsets.data(), - segment_offsets.size(), - default_stream); - } else { - rmm::device_uvector aggregate_segment_offsets( - row_comm_size * segment_offsets.size(), default_stream); - row_comm.allgather(segment_offsets.data(), - aggregate_segment_offsets.data(), - segment_offsets.size(), - default_stream); - } - - vertex_partition_segment_offsets_.resize(aggregate_segment_offsets.size()); - raft::update_host(vertex_partition_segment_offsets_.data(), - aggregate_segment_offsets.data(), - aggregate_segment_offsets.size(), - default_stream); - - raft::comms::status_t status{}; - if (partition.is_hypergraph_partitioned()) { - status = col_comm.sync_stream( - default_stream); // this is necessary as degrees, d_thresholds, and segment_offsets will - // become out-of-scope once control flow exits this block and - // vertex_partition_segment_offsets_ can be used right after return. - } else { - status = row_comm.sync_stream( - default_stream); // this is necessary as degrees, d_thresholds, and segment_offsets will - // become out-of-scope once control flow exits this block and - // vertex_partition_segment_offsets_ can be used right after return. - } - CUGRAPH_EXPECTS(status == raft::comms::status_t::SUCCESS, "sync_stream() failure."); } - // optional expensive checks (part 3/3) + // optional expensive checks (part 2/2) if (do_expensive_check) { // FIXME: check for symmetricity may better be implemetned with transpose(). @@ -409,26 +369,37 @@ template graph_t>:: - graph_t(raft::handle_t const &handle, - edgelist_t const &edgelist, + graph_t(raft::handle_t const& handle, + edgelist_t const& edgelist, vertex_t number_of_vertices, graph_properties_t properties, - bool sorted_by_degree, + std::optional> const& segment_offsets, bool do_expensive_check) : detail::graph_base_t( handle, number_of_vertices, edgelist.number_of_edges, properties), - offsets_(rmm::device_uvector(0, handle.get_stream())), - indices_(rmm::device_uvector(0, handle.get_stream())), - weights_(rmm::device_uvector(0, handle.get_stream())) + offsets_(rmm::device_uvector(0, handle.get_stream_view())), + indices_(rmm::device_uvector(0, handle.get_stream_view())), + segment_offsets_(segment_offsets) { // cheap error checks - auto default_stream = this->get_handle_ptr()->get_stream(); + auto default_stream_view = this->get_handle_ptr()->get_stream_view(); + + auto is_weighted = edgelist.p_edge_weights.has_value(); CUGRAPH_EXPECTS( - (edgelist.p_src_vertices != nullptr) && (edgelist.p_dst_vertices != nullptr), - "Invalid API parameter: edgelist.p_src_vertices and edgelist.p_dst_vertices should " - "not be nullptr."); + ((edgelist.number_of_edges == 0) || (edgelist.p_src_vertices != nullptr)) && + ((edgelist.number_of_edges == 0) || (edgelist.p_dst_vertices != nullptr)) && + (!is_weighted || (is_weighted && ((edgelist.number_of_edges == 0) || + (*(edgelist.p_edge_weights) != nullptr)))), + "Invalid input argument: edgelist.p_src_vertices and edgelist.p_dst_vertices should not be " + "nullptr if edgelist.number_of_edges > 0 and edgelist.p_edge_weights should be neither " + "std::nullopt nor nullptr if weighted and edgelist.number_of_edges > 0."); + + CUGRAPH_EXPECTS( + !segment_offsets.has_value() || + ((*segment_offsets).size() == (detail::num_sparse_segments_per_vertex_partition + 1)), + "Invalid input argument: segment_offsets.size() returns an invalid value."); // optional expensive checks (part 1/2) @@ -438,12 +409,12 @@ graph_ton(default_stream), + rmm::exec_policy(default_stream_view), edge_first, edge_first + edgelist.number_of_edges, out_of_range_t{ 0, this->get_number_of_vertices(), 0, this->get_number_of_vertices()}) == 0, - "Invalid API parameter: edgelist have out-of-range values."); + "Invalid input argument: edgelist have out-of-range values."); // FIXME: check for symmetricity may better be implemetned with transpose(). if (this->is_symmetric()) {} @@ -454,64 +425,14 @@ graph_t(*(this->get_handle_ptr()), - edgelist, - vertex_t{0}, - this->get_number_of_vertices(), - vertex_t{0}, - this->get_number_of_vertices()); - - // update degree-based segment offsets (to be used for graph analytics kernel optimization) - - if (sorted_by_degree) { - auto degree_first = thrust::make_transform_iterator( - thrust::make_counting_iterator(vertex_t{0}), - detail::degree_from_offsets_t{offsets_.data()}); - - // optional expensive checks (part 2/2) - - if (do_expensive_check) { - CUGRAPH_EXPECTS(thrust::is_sorted(rmm::exec_policy(default_stream)->on(default_stream), - degree_first, - degree_first + this->get_number_of_vertices(), - thrust::greater{}), - "Invalid API parameter: sorted_by_degree is set to true, but degrees are not " - "non-ascending."); - } - - static_assert(detail::num_segments_per_vertex_partition == 3); - static_assert((detail::low_degree_threshold <= detail::mid_degree_threshold) && - (detail::mid_degree_threshold <= std::numeric_limits::max())); - rmm::device_uvector d_thresholds(detail::num_segments_per_vertex_partition - 1, - default_stream); - std::vector h_thresholds = {static_cast(detail::low_degree_threshold), - static_cast(detail::mid_degree_threshold)}; - raft::update_device( - d_thresholds.data(), h_thresholds.data(), h_thresholds.size(), default_stream); - - rmm::device_uvector segment_offsets(detail::num_segments_per_vertex_partition + 1, - default_stream); - segment_offsets.set_element_async(0, 0, default_stream); - segment_offsets.set_element_async( - detail::num_segments_per_vertex_partition, this->get_number_of_vertices(), default_stream); - - thrust::upper_bound(rmm::exec_policy(default_stream)->on(default_stream), - degree_first, - degree_first + this->get_number_of_vertices(), - d_thresholds.begin(), - d_thresholds.end(), - segment_offsets.begin() + 1); - - segment_offsets_.resize(segment_offsets.size()); - raft::update_host( - segment_offsets_.data(), segment_offsets.data(), segment_offsets.size(), default_stream); - - CUDA_TRY(cudaStreamSynchronize( - default_stream)); // this is necessary as d_thresholds and segment_offsets will become - // out-of-scpe once control flow exits this block and segment_offsets_ can - // be used right after return. - } + std::tie(offsets_, indices_, weights_, std::ignore) = + compress_edgelist(edgelist, + vertex_t{0}, + std::optional{std::nullopt}, + this->get_number_of_vertices(), + vertex_t{0}, + this->get_number_of_vertices(), + default_stream_view); // optional expensive checks (part 3/3) @@ -555,4 +476,4 @@ template class graph_t; } // namespace experimental } // namespace cugraph -#include +#include diff --git a/cpp/src/experimental/graph_view.cu b/cpp/src/experimental/graph_view.cu index 04d2ea990df..70dd6a326ac 100644 --- a/cpp/src/experimental/graph_view.cu +++ b/cpp/src/experimental/graph_view.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -14,15 +14,17 @@ * limitations under the License. */ -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #include -#include #include +#include +#include #include #include @@ -49,6 +51,110 @@ struct out_of_range_t { __device__ bool operator()(vertex_t v) { return (v < min) || (v >= max); } }; +template +std::vector update_adj_matrix_partition_edge_counts( + std::vector const& adj_matrix_partition_offsets, + std::optional> const& adj_matrix_partition_dcs_nzd_vertex_counts, + partition_t const& partition, + std::optional> const& adj_matrix_partition_segment_offsets, + cudaStream_t stream) +{ + std::vector adj_matrix_partition_edge_counts(partition.get_number_of_matrix_partitions(), + 0); + auto use_dcs = adj_matrix_partition_dcs_nzd_vertex_counts.has_value(); + for (size_t i = 0; i < adj_matrix_partition_offsets.size(); ++i) { + auto [major_first, major_last] = partition.get_matrix_partition_major_range(i); + raft::update_host(&(adj_matrix_partition_edge_counts[i]), + adj_matrix_partition_offsets[i] + + (use_dcs ? ((*adj_matrix_partition_segment_offsets) + [(detail::num_sparse_segments_per_vertex_partition + 2) * i + + detail::num_sparse_segments_per_vertex_partition] + + (*adj_matrix_partition_dcs_nzd_vertex_counts)[i]) + : (major_last - major_first)), + 1, + stream); + } + CUDA_TRY(cudaStreamSynchronize(stream)); + return adj_matrix_partition_edge_counts; +} + +template +rmm::device_uvector compute_minor_degrees( + raft::handle_t const& handle, + graph_view_t const& graph_view) +{ + rmm::device_uvector minor_degrees(graph_view.get_number_of_local_vertices(), + handle.get_stream()); + if (store_transposed) { + copy_v_transform_reduce_out_nbr( + handle, + graph_view, + thrust::make_constant_iterator(0) /* dummy */, + thrust::make_constant_iterator(0) /* dummy */, + [] __device__(vertex_t src, vertex_t dst, weight_t w, auto src_val, auto dst_val) { + return edge_t{1}; + }, + edge_t{0}, + minor_degrees.data()); + } else { + copy_v_transform_reduce_in_nbr( + handle, + graph_view, + thrust::make_constant_iterator(0) /* dummy */, + thrust::make_constant_iterator(0) /* dummy */, + [] __device__(vertex_t src, vertex_t dst, weight_t w, auto src_val, auto dst_val) { + return edge_t{1}; + }, + edge_t{0}, + minor_degrees.data()); + } + + return minor_degrees; +} + +template +rmm::device_uvector compute_weight_sums( + raft::handle_t const& handle, + graph_view_t const& graph_view) +{ + rmm::device_uvector weight_sums(graph_view.get_number_of_local_vertices(), + handle.get_stream()); + if (major == store_transposed) { + copy_v_transform_reduce_in_nbr( + handle, + graph_view, + thrust::make_constant_iterator(0) /* dummy */, + thrust::make_constant_iterator(0) /* dummy */, + [] __device__(vertex_t src, vertex_t dst, weight_t w, auto src_val, auto dst_val) { + return w; + }, + weight_t{0.0}, + weight_sums.data()); + } else { + copy_v_transform_reduce_out_nbr( + handle, + graph_view, + thrust::make_constant_iterator(0) /* dummy */, + thrust::make_constant_iterator(0) /* dummy */, + [] __device__(vertex_t src, vertex_t dst, weight_t w, auto src_val, auto dst_val) { + return w; + }, + weight_t{0.0}, + weight_sums.data()); + } + + return weight_sums; +} + } // namespace template graph_view_t>:: - graph_view_t(raft::handle_t const& handle, - std::vector const& adj_matrix_partition_offsets, - std::vector const& adj_matrix_partition_indices, - std::vector const& adj_matrix_partition_weights, - std::vector const& vertex_partition_segment_offsets, - partition_t const& partition, - vertex_t number_of_vertices, - edge_t number_of_edges, - graph_properties_t properties, - bool sorted_by_global_degree_within_vertex_partition, - bool do_expensive_check) + graph_view_t( + raft::handle_t const& handle, + std::vector const& adj_matrix_partition_offsets, + std::vector const& adj_matrix_partition_indices, + std::optional> const& adj_matrix_partition_weights, + std::optional> const& adj_matrix_partition_dcs_nzd_vertices, + std::optional> const& adj_matrix_partition_dcs_nzd_vertex_counts, + partition_t const& partition, + vertex_t number_of_vertices, + edge_t number_of_edges, + graph_properties_t properties, + std::optional> const& adj_matrix_partition_segment_offsets, + bool do_expensive_check) : detail::graph_base_t( handle, number_of_vertices, number_of_edges, properties), adj_matrix_partition_offsets_(adj_matrix_partition_offsets), adj_matrix_partition_indices_(adj_matrix_partition_indices), adj_matrix_partition_weights_(adj_matrix_partition_weights), + adj_matrix_partition_dcs_nzd_vertices_(adj_matrix_partition_dcs_nzd_vertices), + adj_matrix_partition_dcs_nzd_vertex_counts_(adj_matrix_partition_dcs_nzd_vertex_counts), + adj_matrix_partition_number_of_edges_( + update_adj_matrix_partition_edge_counts(adj_matrix_partition_offsets, + adj_matrix_partition_dcs_nzd_vertex_counts, + partition, + adj_matrix_partition_segment_offsets, + handle.get_stream())), partition_(partition), - vertex_partition_segment_offsets_(vertex_partition_segment_offsets) + adj_matrix_partition_segment_offsets_(adj_matrix_partition_segment_offsets) { // cheap error checks @@ -86,33 +202,41 @@ graph_view_tget_subcomm(cugraph::partition_2d::key_naming_t().col_name()) .get_size(); + auto is_weighted = adj_matrix_partition_weights.has_value(); + auto use_dcs = adj_matrix_partition_dcs_nzd_vertices.has_value(); + CUGRAPH_EXPECTS(adj_matrix_partition_offsets.size() == adj_matrix_partition_indices.size(), - "Invalid API parameter: adj_matrix_partition_offsets.size() and " + "Internal Error: adj_matrix_partition_offsets.size() and " "adj_matrix_partition_indices.size() should coincide."); - CUGRAPH_EXPECTS((adj_matrix_partition_weights.size() == adj_matrix_partition_offsets.size()) || - (adj_matrix_partition_weights.size() == 0), - "Invalid API parameter: adj_matrix_partition_weights.size() should coincide with " - "adj_matrix_partition_offsets.size() (if weighted) or 0 (if unweighted)."); + CUGRAPH_EXPECTS( + !is_weighted || ((*adj_matrix_partition_weights).size() == adj_matrix_partition_offsets.size()), + "Internal Error: adj_matrix_partition_weights.size() should coincide with " + "adj_matrix_partition_offsets.size() (if weighted)."); + CUGRAPH_EXPECTS(adj_matrix_partition_dcs_nzd_vertex_counts.has_value() == use_dcs, + "adj_matrix_partition_dcs_nzd_vertices.has_value() and " + "adj_matrix_partition_dcs_nzd_vertex_counts.has_value() should coincide"); + CUGRAPH_EXPECTS(!use_dcs || ((*adj_matrix_partition_dcs_nzd_vertices).size() == + (*adj_matrix_partition_dcs_nzd_vertex_counts).size()), + "Internal Error: adj_matrix_partition_dcs_nzd_vertices.size() and " + "adj_matrix_partition_dcs_nzd_vertex_counts.size() should coincide (if used)."); + CUGRAPH_EXPECTS(!use_dcs || ((*adj_matrix_partition_dcs_nzd_vertices).size() == + adj_matrix_partition_offsets.size()), + "Internal Error: adj_matrix_partition_dcs_nzd_vertices.size() should coincide " + "with adj_matrix_partition_offsets.size() (if used)."); + + CUGRAPH_EXPECTS(adj_matrix_partition_offsets.size() == static_cast(col_comm_size), + "Internal Error: erroneous adj_matrix_partition_offsets.size()."); CUGRAPH_EXPECTS( - (partition.is_hypergraph_partitioned() && - (adj_matrix_partition_offsets.size() == static_cast(row_comm_size))) || - (!(partition.is_hypergraph_partitioned()) && (adj_matrix_partition_offsets.size() == 1)), - "Invalid API parameter: errneous adj_matrix_partition_offsets.size()."); - - CUGRAPH_EXPECTS((sorted_by_global_degree_within_vertex_partition && - (vertex_partition_segment_offsets.size() == - (partition.is_hypergraph_partitioned() ? col_comm_size : row_comm_size) * - (detail::num_segments_per_vertex_partition + 1))) || - (!sorted_by_global_degree_within_vertex_partition && - (vertex_partition_segment_offsets.size() == 0)), - "Invalid API parameter: vertex_partition_segment_offsets.size() does not match " - "with sorted_by_global_degree_within_vertex_partition."); + !adj_matrix_partition_segment_offsets.has_value() || + ((*adj_matrix_partition_segment_offsets).size() == + col_comm_size * (detail::num_sparse_segments_per_vertex_partition + (use_dcs ? 2 : 1))), + "Internal Error: invalid adj_matrix_partition_segment_offsets.size()."); // optional expensive checks if (do_expensive_check) { - auto default_stream = this->get_handle_ptr()->get_stream(); + auto default_stream_view = this->get_handle_ptr()->get_stream_view(); auto const row_comm_rank = this->get_handle_ptr() ->get_subcomm(cugraph::partition_2d::key_naming_t().row_name()) @@ -123,73 +247,79 @@ graph_view_ton(default_stream), - adj_matrix_partition_offsets[i], - adj_matrix_partition_offsets[i] + (major_last - major_first + 1)), - "Invalid API parameter: adj_matrix_partition_offsets[] is not sorted."); + auto [major_first, major_last] = partition.get_matrix_partition_major_range(i); + auto [minor_first, minor_last] = partition.get_matrix_partition_minor_range(); + auto offset_array_size = major_last - major_first + 1; + if (use_dcs) { + auto major_hypersparse_first = + major_first + (*adj_matrix_partition_segment_offsets) + [(detail::num_sparse_segments_per_vertex_partition + 2) * i + + detail::num_sparse_segments_per_vertex_partition]; + offset_array_size = major_hypersparse_first - major_first + + (*adj_matrix_partition_dcs_nzd_vertex_counts)[i] + 1; + } + CUGRAPH_EXPECTS(thrust::is_sorted(rmm::exec_policy(default_stream_view), + adj_matrix_partition_offsets[i], + adj_matrix_partition_offsets[i] + offset_array_size), + "Internal Error: adj_matrix_partition_offsets[] is not sorted."); edge_t number_of_local_edges{}; raft::update_host(&number_of_local_edges, - adj_matrix_partition_offsets[i] + (major_last - major_first), + adj_matrix_partition_offsets[i] + offset_array_size - 1, 1, - default_stream); - CUDA_TRY(cudaStreamSynchronize(default_stream)); + default_stream_view.value()); + default_stream_view.synchronize(); number_of_local_edges_sum += number_of_local_edges; // better use thrust::any_of once https://github.com/thrust/thrust/issues/1016 is resolved CUGRAPH_EXPECTS( - thrust::count_if(rmm::exec_policy(default_stream)->on(default_stream), + thrust::count_if(rmm::exec_policy(default_stream_view), adj_matrix_partition_indices[i], adj_matrix_partition_indices[i] + number_of_local_edges, out_of_range_t{minor_first, minor_last}) == 0, - "Invalid API parameter: adj_matrix_partition_indices[] have out-of-range vertex IDs."); + "Internal Error: adj_matrix_partition_indices[] have out-of-range vertex IDs."); } number_of_local_edges_sum = host_scalar_allreduce( - this->get_handle_ptr()->get_comms(), number_of_local_edges_sum, default_stream); + this->get_handle_ptr()->get_comms(), number_of_local_edges_sum, default_stream_view.value()); CUGRAPH_EXPECTS(number_of_local_edges_sum == this->get_number_of_edges(), - "Invalid API parameter: the sum of local edges doe counts not match with " + "Internal Error: the sum of local edges counts does not match with " "number_of_local_edges."); - if (sorted_by_global_degree_within_vertex_partition) { - auto degrees = detail::compute_major_degree(handle, adj_matrix_partition_offsets, partition); - CUGRAPH_EXPECTS(thrust::is_sorted(rmm::exec_policy(default_stream)->on(default_stream), + if (adj_matrix_partition_segment_offsets) { + auto degrees = detail::compute_major_degrees(handle, + adj_matrix_partition_offsets, + adj_matrix_partition_dcs_nzd_vertices, + adj_matrix_partition_dcs_nzd_vertex_counts, + partition, + adj_matrix_partition_segment_offsets); + CUGRAPH_EXPECTS(thrust::is_sorted(rmm::exec_policy(default_stream_view), degrees.begin(), degrees.end(), thrust::greater{}), - "Invalid API parameter: sorted_by_global_degree_within_vertex_partition is " - "set to true, but degrees are not non-ascending."); - - for (int i = 0; i < (partition.is_hypergraph_partitioned() ? col_comm_size : row_comm_size); - ++i) { - CUGRAPH_EXPECTS(std::is_sorted(vertex_partition_segment_offsets.begin() + - (detail::num_segments_per_vertex_partition + 1) * i, - vertex_partition_segment_offsets.begin() + - (detail::num_segments_per_vertex_partition + 1) * (i + 1)), - "Invalid API parameter: erroneous vertex_partition_segment_offsets."); + "Invalid Invalid input argument: adj_matrix_partition_segment_offsets are " + "provided, but degrees are not in descending order."); + + auto num_segments_per_vertex_partition = + detail::num_sparse_segments_per_vertex_partition + (use_dcs ? 1 : 0); + for (int i = 0; i < col_comm_size; ++i) { + CUGRAPH_EXPECTS(std::is_sorted((*adj_matrix_partition_segment_offsets).begin() + + (num_segments_per_vertex_partition + 1) * i, + (*adj_matrix_partition_segment_offsets).begin() + + (num_segments_per_vertex_partition + 1) * (i + 1)), + "Internal Error: erroneous adj_matrix_partition_segment_offsets."); CUGRAPH_EXPECTS( - vertex_partition_segment_offsets[(detail::num_segments_per_vertex_partition + 1) * i] == - 0, - "Invalid API parameter: erroneous vertex_partition_segment_offsets."); - auto vertex_partition_idx = partition.is_hypergraph_partitioned() - ? row_comm_size * i + row_comm_rank - : col_comm_rank * row_comm_size + i; + (*adj_matrix_partition_segment_offsets)[(num_segments_per_vertex_partition + 1) * i] == 0, + "Internal Error: erroneous adj_matrix_partition_segment_offsets."); + auto vertex_partition_idx = row_comm_size * i + row_comm_rank; CUGRAPH_EXPECTS( - vertex_partition_segment_offsets[(detail::num_segments_per_vertex_partition + 1) * i + - detail::num_segments_per_vertex_partition] == + (*adj_matrix_partition_segment_offsets)[(num_segments_per_vertex_partition + 1) * i + + num_segments_per_vertex_partition] == partition.get_vertex_partition_size(vertex_partition_idx), - "Invalid API parameter: erroneous vertex_partition_segment_offsets."); + "Internal Error: erroneous adj_matrix_partition_segment_offsets."); } } - CUGRAPH_EXPECTS( - partition.get_vertex_partition_last(comm_size - 1) == number_of_vertices, - "Invalid API parameter: vertex partition should cover [0, number_of_vertices)."); + CUGRAPH_EXPECTS(partition.get_vertex_partition_last(comm_size - 1) == number_of_vertices, + "Internal Error: vertex partition should cover [0, number_of_vertices)."); // FIXME: check for symmetricity may better be implemetned with transpose(). if (this->is_symmetric()) {} @@ -212,13 +342,12 @@ graph_view_t>::graph_view_t(raft::handle_t const& handle, edge_t const* offsets, vertex_t const* indices, - weight_t const* weights, - std::vector const& - segment_offsets, + std::optional weights, vertex_t number_of_vertices, edge_t number_of_edges, graph_properties_t properties, - bool sorted_by_degree, + std::optional> const& + segment_offsets, bool do_expensive_check) : detail::graph_base_t( handle, number_of_vertices, number_of_edges, properties), @@ -230,45 +359,42 @@ graph_view_tget_handle_ptr()->get_stream(); + auto default_stream_view = this->get_handle_ptr()->get_stream_view(); - CUGRAPH_EXPECTS(thrust::is_sorted(rmm::exec_policy(default_stream)->on(default_stream), + CUGRAPH_EXPECTS(thrust::is_sorted(rmm::exec_policy(default_stream_view), offsets, offsets + (this->get_number_of_vertices() + 1)), - "Invalid API parameter: offsets is not sorted."); + "Internal Error: offsets is not sorted."); // better use thrust::any_of once https://github.com/thrust/thrust/issues/1016 is resolved CUGRAPH_EXPECTS( - thrust::count_if(rmm::exec_policy(default_stream)->on(default_stream), + thrust::count_if(rmm::exec_policy(default_stream_view), indices, indices + this->get_number_of_edges(), out_of_range_t{0, this->get_number_of_vertices()}) == 0, - "Invalid API parameter: adj_matrix_partition_indices[] have out-of-range vertex IDs."); - - if (sorted_by_degree) { - auto degree_first = - thrust::make_transform_iterator(thrust::make_counting_iterator(vertex_t{0}), - detail::degree_from_offsets_t{offsets}); - CUGRAPH_EXPECTS(thrust::is_sorted(rmm::exec_policy(default_stream)->on(default_stream), - degree_first, - degree_first + this->get_number_of_vertices(), + "Internal Error: adj_matrix_partition_indices[] have out-of-range vertex IDs."); + + if (segment_offsets) { + auto degrees = detail::compute_major_degrees(handle, offsets, number_of_vertices); + CUGRAPH_EXPECTS(thrust::is_sorted(rmm::exec_policy(default_stream_view), + degrees.begin(), + degrees.end(), thrust::greater{}), - "Invalid API parameter: sorted_by_degree is set to true, but degrees are not " - "non-ascending."); - - CUGRAPH_EXPECTS(std::is_sorted(segment_offsets.begin(), segment_offsets.end()), - "Invalid API parameter: erroneous segment_offsets."); - CUGRAPH_EXPECTS(segment_offsets[0] == 0, "Invalid API parameter: segment_offsets."); - CUGRAPH_EXPECTS(segment_offsets.back() == this->get_number_of_vertices(), - "Invalid API parameter: segment_offsets."); + "Invalid Invalid input argument: segment_offsets are provided, but degrees " + "are not in descending order."); + + CUGRAPH_EXPECTS(std::is_sorted((*segment_offsets).begin(), (*segment_offsets).end()), + "Internal Error: erroneous segment_offsets."); + CUGRAPH_EXPECTS((*segment_offsets)[0] == 0, "Invalid input argument segment_offsets."); + CUGRAPH_EXPECTS((*segment_offsets).back() == this->get_number_of_vertices(), + "Invalid input argument: segment_offsets."); } // FIXME: check for symmetricity may better be implemetned with transpose(). @@ -279,6 +405,338 @@ graph_view_t +rmm::device_uvector +graph_view_t>:: + compute_in_degrees(raft::handle_t const& handle) const +{ + if (store_transposed) { + return detail::compute_major_degrees(handle, + this->adj_matrix_partition_offsets_, + this->adj_matrix_partition_dcs_nzd_vertices_, + this->adj_matrix_partition_dcs_nzd_vertex_counts_, + this->partition_, + this->adj_matrix_partition_segment_offsets_); + } else { + return compute_minor_degrees(handle, *this); + } +} + +template +rmm::device_uvector +graph_view_t>::compute_in_degrees(raft::handle_t const& handle) const +{ + if (store_transposed) { + return detail::compute_major_degrees( + handle, this->offsets_, this->get_number_of_local_vertices()); + } else { + return compute_minor_degrees(handle, *this); + } +} + +template +rmm::device_uvector +graph_view_t>:: + compute_out_degrees(raft::handle_t const& handle) const +{ + if (store_transposed) { + return compute_minor_degrees(handle, *this); + } else { + return detail::compute_major_degrees(handle, + this->adj_matrix_partition_offsets_, + this->adj_matrix_partition_dcs_nzd_vertices_, + this->adj_matrix_partition_dcs_nzd_vertex_counts_, + this->partition_, + this->adj_matrix_partition_segment_offsets_); + } +} + +template +rmm::device_uvector +graph_view_t>::compute_out_degrees(raft::handle_t const& handle) const +{ + if (store_transposed) { + return compute_minor_degrees(handle, *this); + } else { + return detail::compute_major_degrees( + handle, this->offsets_, this->get_number_of_local_vertices()); + } +} + +template +rmm::device_uvector +graph_view_t>:: + compute_in_weight_sums(raft::handle_t const& handle) const +{ + if (store_transposed) { + return compute_weight_sums(handle, *this); + } else { + return compute_weight_sums(handle, *this); + } +} + +template +rmm::device_uvector graph_view_t< + vertex_t, + edge_t, + weight_t, + store_transposed, + multi_gpu, + std::enable_if_t>::compute_in_weight_sums(raft::handle_t const& handle) const +{ + if (store_transposed) { + return compute_weight_sums(handle, *this); + } else { + return compute_weight_sums(handle, *this); + } +} + +template +rmm::device_uvector +graph_view_t>:: + compute_out_weight_sums(raft::handle_t const& handle) const +{ + if (store_transposed) { + return compute_weight_sums(handle, *this); + } else { + return compute_weight_sums(handle, *this); + } +} + +template +rmm::device_uvector graph_view_t< + vertex_t, + edge_t, + weight_t, + store_transposed, + multi_gpu, + std::enable_if_t>::compute_out_weight_sums(raft::handle_t const& handle) const +{ + if (store_transposed) { + return compute_weight_sums(handle, *this); + } else { + return compute_weight_sums(handle, *this); + } +} + +template +edge_t +graph_view_t>:: + compute_max_in_degree(raft::handle_t const& handle) const +{ + auto in_degrees = compute_in_degrees(handle); + auto it = thrust::max_element( + rmm::exec_policy(handle.get_stream_view()), in_degrees.begin(), in_degrees.end()); + rmm::device_scalar ret(edge_t{0}, handle.get_stream()); + device_allreduce(handle.get_comms(), + it != in_degrees.end() ? it : ret.data(), + ret.data(), + 1, + raft::comms::op_t::MAX, + handle.get_stream()); + return ret.value(handle.get_stream()); +} + +template +edge_t graph_view_t>::compute_max_in_degree(raft::handle_t const& + handle) const +{ + auto in_degrees = compute_in_degrees(handle); + auto it = thrust::max_element( + rmm::exec_policy(handle.get_stream_view()), in_degrees.begin(), in_degrees.end()); + edge_t ret{0}; + if (it != in_degrees.end()) { raft::update_host(&ret, it, 1, handle.get_stream()); } + handle.get_stream_view().synchronize(); + return ret; +} + +template +edge_t +graph_view_t>:: + compute_max_out_degree(raft::handle_t const& handle) const +{ + auto out_degrees = compute_out_degrees(handle); + auto it = thrust::max_element( + rmm::exec_policy(handle.get_stream_view()), out_degrees.begin(), out_degrees.end()); + rmm::device_scalar ret(edge_t{0}, handle.get_stream()); + device_allreduce(handle.get_comms(), + it != out_degrees.end() ? it : ret.data(), + ret.data(), + 1, + raft::comms::op_t::MAX, + handle.get_stream()); + return ret.value(handle.get_stream()); +} + +template +edge_t graph_view_t>::compute_max_out_degree(raft::handle_t const& + handle) const +{ + auto out_degrees = compute_out_degrees(handle); + auto it = thrust::max_element( + rmm::exec_policy(handle.get_stream_view()), out_degrees.begin(), out_degrees.end()); + edge_t ret{0}; + if (it != out_degrees.end()) { raft::update_host(&ret, it, 1, handle.get_stream()); } + handle.get_stream_view().synchronize(); + return ret; +} + +template +weight_t +graph_view_t>:: + compute_max_in_weight_sum(raft::handle_t const& handle) const +{ + auto in_weight_sums = compute_in_weight_sums(handle); + auto it = thrust::max_element( + rmm::exec_policy(handle.get_stream_view()), in_weight_sums.begin(), in_weight_sums.end()); + rmm::device_scalar ret(weight_t{0.0}, handle.get_stream()); + device_allreduce(handle.get_comms(), + it != in_weight_sums.end() ? it : ret.data(), + ret.data(), + 1, + raft::comms::op_t::MAX, + handle.get_stream()); + return ret.value(handle.get_stream()); +} + +template +weight_t graph_view_t>::compute_max_in_weight_sum(raft::handle_t const& + handle) const +{ + auto in_weight_sums = compute_in_weight_sums(handle); + auto it = thrust::max_element( + rmm::exec_policy(handle.get_stream_view()), in_weight_sums.begin(), in_weight_sums.end()); + weight_t ret{0.0}; + if (it != in_weight_sums.end()) { raft::update_host(&ret, it, 1, handle.get_stream()); } + handle.get_stream_view().synchronize(); + return ret; +} + +template +weight_t +graph_view_t>:: + compute_max_out_weight_sum(raft::handle_t const& handle) const +{ + auto out_weight_sums = compute_out_weight_sums(handle); + auto it = thrust::max_element( + rmm::exec_policy(handle.get_stream_view()), out_weight_sums.begin(), out_weight_sums.end()); + rmm::device_scalar ret(weight_t{0.0}, handle.get_stream()); + device_allreduce(handle.get_comms(), + it != out_weight_sums.end() ? it : ret.data(), + ret.data(), + 1, + raft::comms::op_t::MAX, + handle.get_stream()); + return ret.value(handle.get_stream()); +} + +template +weight_t graph_view_t< + vertex_t, + edge_t, + weight_t, + store_transposed, + multi_gpu, + std::enable_if_t>::compute_max_out_weight_sum(raft::handle_t const& handle) const +{ + auto out_weight_sums = compute_out_weight_sums(handle); + auto it = thrust::max_element( + rmm::exec_policy(handle.get_stream_view()), out_weight_sums.begin(), out_weight_sums.end()); + weight_t ret{0.0}; + if (it != out_weight_sums.end()) { raft::update_host(&ret, it, 1, handle.get_stream()); } + handle.get_stream_view().synchronize(); + return ret; +} + // explicit instantiation template class graph_view_t; diff --git a/cpp/src/experimental/induced_subgraph.cu b/cpp/src/experimental/induced_subgraph.cu new file mode 100644 index 00000000000..c43f81bbf39 --- /dev/null +++ b/cpp/src/experimental/induced_subgraph.cu @@ -0,0 +1,401 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include + +namespace cugraph { +namespace experimental { + +template +std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_induced_subgraphs( + raft::handle_t const& handle, + graph_view_t const& graph_view, + size_t const* subgraph_offsets /* size == num_subgraphs + 1 */, + vertex_t const* subgraph_vertices /* size == subgraph_offsets[num_subgraphs] */, + size_t num_subgraphs, + bool do_expensive_check) +{ +#ifdef TIMING + HighResTimer hr_timer; + hr_timer.start("extract_induced_subgraphs"); +#endif + // FIXME: this code is inefficient for the vertices with their local degrees much larger than the + // number of vertices in the subgraphs (in this case, searching that the subgraph vertices are + // included in the local neighbors is more efficient than searching the local neighbors are + // included in the subgraph vertices). We may later add additional code to handle such cases. + // FIXME: we may consider the performance (speed & memory footprint, hash based approach uses + // extra-memory) of hash table based and binary search based approaches + + // 1. check input arguments + + if (do_expensive_check) { + size_t should_be_zero{std::numeric_limits::max()}; + size_t num_aggregate_subgraph_vertices{}; + raft::update_host(&should_be_zero, subgraph_offsets, 1, handle.get_stream()); + raft::update_host( + &num_aggregate_subgraph_vertices, subgraph_offsets + num_subgraphs, 1, handle.get_stream()); + handle.get_stream_view().synchronize(); + CUGRAPH_EXPECTS(should_be_zero == 0, + "Invalid input argument: subgraph_offsets[0] should be 0."); + + CUGRAPH_EXPECTS(thrust::is_sorted(rmm::exec_policy(handle.get_stream_view()), + subgraph_offsets, + subgraph_offsets + (num_subgraphs + 1)), + "Invalid input argument: subgraph_offsets is not sorted."); + auto vertex_partition = + vertex_partition_device_view_t(graph_view.get_vertex_partition_view()); + CUGRAPH_EXPECTS(thrust::count_if(rmm::exec_policy(handle.get_stream_view()), + subgraph_vertices, + subgraph_vertices + num_aggregate_subgraph_vertices, + [vertex_partition] __device__(auto v) { + return !vertex_partition.is_valid_vertex(v) || + !vertex_partition.is_local_vertex_nocheck(v); + }) == 0, + "Invalid input argument: subgraph_vertices has invalid vertex IDs."); + + CUGRAPH_EXPECTS( + thrust::count_if( + rmm::exec_policy(handle.get_stream_view()), + thrust::make_counting_iterator(size_t{0}), + thrust::make_counting_iterator(num_subgraphs), + [subgraph_offsets, subgraph_vertices] __device__(auto i) { + // vertices are sorted and unique + return !thrust::is_sorted(thrust::seq, + subgraph_vertices + subgraph_offsets[i], + subgraph_vertices + subgraph_offsets[i + 1]) || + (thrust::count_if( + thrust::seq, + thrust::make_counting_iterator(subgraph_offsets[i]), + thrust::make_counting_iterator(subgraph_offsets[i + 1]), + [subgraph_vertices, last = subgraph_offsets[i + 1] - 1] __device__(auto i) { + return (i != last) && (subgraph_vertices[i] == subgraph_vertices[i + 1]); + }) != 0); + }) == 0, + "Invalid input argument: subgraph_vertices for each subgraph idx should be sorted in " + "ascending order and unique."); + } + + // 2. extract induced subgraphs + + if (multi_gpu) { + CUGRAPH_FAIL("Unimplemented."); + return std::make_tuple(rmm::device_uvector(0, handle.get_stream_view()), + rmm::device_uvector(0, handle.get_stream_view()), + rmm::device_uvector(0, handle.get_stream_view()), + rmm::device_uvector(0, handle.get_stream_view())); + } else { + // 2-1. Phase 1: calculate memory requirements + + size_t num_aggregate_subgraph_vertices{}; + raft::update_host( + &num_aggregate_subgraph_vertices, subgraph_offsets + num_subgraphs, 1, handle.get_stream()); + handle.get_stream_view().synchronize(); + + rmm::device_uvector subgraph_vertex_output_offsets( + num_aggregate_subgraph_vertices + 1, + handle.get_stream_view()); // for each element of subgraph_vertices + + auto matrix_partition = matrix_partition_device_view_t( + graph_view.get_matrix_partition_view()); + // count the numbers of the induced subgraph edges for each vertex in the aggregate subgraph + // vertex list. + thrust::transform( + rmm::exec_policy(handle.get_stream_view()), + thrust::make_counting_iterator(size_t{0}), + thrust::make_counting_iterator(num_aggregate_subgraph_vertices), + subgraph_vertex_output_offsets.begin(), + [subgraph_offsets, subgraph_vertices, num_subgraphs, matrix_partition] __device__(auto i) { + auto subgraph_idx = thrust::distance( + subgraph_offsets + 1, + thrust::upper_bound(thrust::seq, subgraph_offsets, subgraph_offsets + num_subgraphs, i)); + vertex_t const* indices{nullptr}; + thrust::optional weights{thrust::nullopt}; + edge_t local_degree{}; + auto major_offset = + matrix_partition.get_major_offset_from_major_nocheck(subgraph_vertices[i]); + thrust::tie(indices, weights, local_degree) = + matrix_partition.get_local_edges(major_offset); + // FIXME: this is inefficient for high local degree vertices + return thrust::count_if( + thrust::seq, + indices, + indices + local_degree, + [vertex_first = subgraph_vertices + subgraph_offsets[subgraph_idx], + vertex_last = + subgraph_vertices + subgraph_offsets[subgraph_idx + 1]] __device__(auto nbr) { + return thrust::binary_search(thrust::seq, vertex_first, vertex_last, nbr); + }); + }); + thrust::exclusive_scan(rmm::exec_policy(handle.get_stream_view()), + subgraph_vertex_output_offsets.begin(), + subgraph_vertex_output_offsets.end(), + subgraph_vertex_output_offsets.begin()); + + size_t num_aggregate_edges{}; + raft::update_host(&num_aggregate_edges, + subgraph_vertex_output_offsets.data() + num_aggregate_subgraph_vertices, + 1, + handle.get_stream()); + handle.get_stream_view().synchronize(); + + // 2-2. Phase 2: find the edges in the induced subgraphs + + rmm::device_uvector edge_majors(num_aggregate_edges, handle.get_stream_view()); + rmm::device_uvector edge_minors(num_aggregate_edges, handle.get_stream_view()); + auto edge_weights = graph_view.is_weighted() + ? std::make_optional>( + num_aggregate_edges, handle.get_stream_view()) + : std::nullopt; + + // fill the edge list buffer (to be returned) for each vetex in the aggregate subgraph vertex + // list (use the offsets computed in the Phase 1) + thrust::for_each( + rmm::exec_policy(handle.get_stream_view()), + thrust::make_counting_iterator(size_t{0}), + thrust::make_counting_iterator(num_aggregate_subgraph_vertices), + [subgraph_offsets, + subgraph_vertices, + num_subgraphs, + matrix_partition, + subgraph_vertex_output_offsets = subgraph_vertex_output_offsets.data(), + edge_majors = edge_majors.data(), + edge_minors = edge_minors.data(), + edge_weights = edge_weights ? thrust::optional{(*edge_weights).data()} + : thrust::nullopt] __device__(auto i) { + auto subgraph_idx = thrust::distance( + subgraph_offsets + 1, + thrust::upper_bound( + thrust::seq, subgraph_offsets, subgraph_offsets + num_subgraphs, size_t{i})); + vertex_t const* indices{nullptr}; + thrust::optional weights{thrust::nullopt}; + edge_t local_degree{}; + auto major_offset = + matrix_partition.get_major_offset_from_major_nocheck(subgraph_vertices[i]); + thrust::tie(indices, weights, local_degree) = + matrix_partition.get_local_edges(major_offset); + if (weights) { + auto triplet_first = thrust::make_zip_iterator(thrust::make_tuple( + thrust::make_constant_iterator(subgraph_vertices[i]), indices, *weights)); + // FIXME: this is inefficient for high local degree vertices + thrust::copy_if( + thrust::seq, + triplet_first, + triplet_first + local_degree, + thrust::make_zip_iterator(thrust::make_tuple(edge_majors, edge_minors, *edge_weights)) + + subgraph_vertex_output_offsets[i], + [vertex_first = subgraph_vertices + subgraph_offsets[subgraph_idx], + vertex_last = + subgraph_vertices + subgraph_offsets[subgraph_idx + 1]] __device__(auto t) { + return thrust::binary_search( + thrust::seq, vertex_first, vertex_last, thrust::get<1>(t)); + }); + } else { + auto pair_first = thrust::make_zip_iterator( + thrust::make_tuple(thrust::make_constant_iterator(subgraph_vertices[i]), indices)); + // FIXME: this is inefficient for high local degree vertices + thrust::copy_if(thrust::seq, + pair_first, + pair_first + local_degree, + thrust::make_zip_iterator(thrust::make_tuple(edge_majors, edge_minors)) + + subgraph_vertex_output_offsets[i], + [vertex_first = subgraph_vertices + subgraph_offsets[subgraph_idx], + vertex_last = subgraph_vertices + + subgraph_offsets[subgraph_idx + 1]] __device__(auto t) { + return thrust::binary_search( + thrust::seq, vertex_first, vertex_last, thrust::get<1>(t)); + }); + } + }); + + rmm::device_uvector subgraph_edge_offsets(num_subgraphs + 1, handle.get_stream_view()); + thrust::gather(rmm::exec_policy(handle.get_stream_view()), + subgraph_offsets, + subgraph_offsets + (num_subgraphs + 1), + subgraph_vertex_output_offsets.begin(), + subgraph_edge_offsets.begin()); +#ifdef TIMING + hr_timer.stop(); + hr_timer.display(std::cout); +#endif + return std::make_tuple(std::move(edge_majors), + std::move(edge_minors), + std::move(edge_weights), + std::move(subgraph_edge_offsets)); + } +} + +// explicit instantiation + +template std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_induced_subgraphs(raft::handle_t const& handle, + graph_view_t const& graph_view, + size_t const* subgraph_offsets, + int32_t const* subgraph_vertices, + size_t num_subgraphs, + bool do_expensive_check); + +template std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_induced_subgraphs(raft::handle_t const& handle, + graph_view_t const& graph_view, + size_t const* subgraph_offsets, + int32_t const* subgraph_vertices, + size_t num_subgraphs, + bool do_expensive_check); + +template std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_induced_subgraphs(raft::handle_t const& handle, + graph_view_t const& graph_view, + size_t const* subgraph_offsets, + int32_t const* subgraph_vertices, + size_t num_subgraphs, + bool do_expensive_check); + +template std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_induced_subgraphs(raft::handle_t const& handle, + graph_view_t const& graph_view, + size_t const* subgraph_offsets, + int32_t const* subgraph_vertices, + size_t num_subgraphs, + bool do_expensive_check); + +template std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_induced_subgraphs(raft::handle_t const& handle, + graph_view_t const& graph_view, + size_t const* subgraph_offsets, + int32_t const* subgraph_vertices, + size_t num_subgraphs, + bool do_expensive_check); + +template std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_induced_subgraphs(raft::handle_t const& handle, + graph_view_t const& graph_view, + size_t const* subgraph_offsets, + int32_t const* subgraph_vertices, + size_t num_subgraphs, + bool do_expensive_check); + +template std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_induced_subgraphs(raft::handle_t const& handle, + graph_view_t const& graph_view, + size_t const* subgraph_offsets, + int32_t const* subgraph_vertices, + size_t num_subgraphs, + bool do_expensive_check); + +template std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_induced_subgraphs(raft::handle_t const& handle, + graph_view_t const& graph_view, + size_t const* subgraph_offsets, + int32_t const* subgraph_vertices, + size_t num_subgraphs, + bool do_expensive_check); + +template std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_induced_subgraphs(raft::handle_t const& handle, + graph_view_t const& graph_view, + size_t const* subgraph_offsets, + int64_t const* subgraph_vertices, + size_t num_subgraphs, + bool do_expensive_check); + +template std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_induced_subgraphs(raft::handle_t const& handle, + graph_view_t const& graph_view, + size_t const* subgraph_offsets, + int64_t const* subgraph_vertices, + size_t num_subgraphs, + bool do_expensive_check); + +template std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_induced_subgraphs(raft::handle_t const& handle, + graph_view_t const& graph_view, + size_t const* subgraph_offsets, + int64_t const* subgraph_vertices, + size_t num_subgraphs, + bool do_expensive_check); + +template std::tuple, + rmm::device_uvector, + std::optional>, + rmm::device_uvector> +extract_induced_subgraphs(raft::handle_t const& handle, + graph_view_t const& graph_view, + size_t const* subgraph_offsets, + int64_t const* subgraph_vertices, + size_t num_subgraphs, + bool do_expensive_check); + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/src/experimental/katz_centrality.cu b/cpp/src/experimental/katz_centrality.cu index 587011da817..b8ab45277fc 100644 --- a/cpp/src/experimental/katz_centrality.cu +++ b/cpp/src/experimental/katz_centrality.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -14,13 +14,13 @@ * limitations under the License. */ -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include #include #include @@ -36,10 +36,10 @@ namespace experimental { namespace detail { template -void katz_centrality(raft::handle_t const &handle, - GraphViewType const &pull_graph_view, - result_t *betas, - result_t *katz_centralities, +void katz_centrality(raft::handle_t const& handle, + GraphViewType const& pull_graph_view, + result_t const* betas, + result_t* katz_centralities, result_t alpha, result_t beta, // relevant only if betas == nullptr result_t epsilon, @@ -166,17 +166,15 @@ void katz_centrality(raft::handle_t const &handle, katz_centralities, [l2_norm] __device__(auto val) { return val / l2_norm; }); } - - return; } } // namespace detail template -void katz_centrality(raft::handle_t const &handle, - graph_view_t const &graph_view, - result_t *betas, - result_t *katz_centralities, +void katz_centrality(raft::handle_t const& handle, + graph_view_t const& graph_view, + result_t const* betas, + result_t* katz_centralities, result_t alpha, result_t beta, // relevant only if beta == nullptr result_t epsilon, @@ -200,10 +198,10 @@ void katz_centrality(raft::handle_t const &handle, // explicit instantiation -template void katz_centrality(raft::handle_t const &handle, - graph_view_t const &graph_view, - float *betas, - float *katz_centralities, +template void katz_centrality(raft::handle_t const& handle, + graph_view_t const& graph_view, + float const* betas, + float* katz_centralities, float alpha, float beta, float epsilon, @@ -212,10 +210,10 @@ template void katz_centrality(raft::handle_t const &handle, bool normalize, bool do_expensive_check); -template void katz_centrality(raft::handle_t const &handle, - graph_view_t const &graph_view, - double *betas, - double *katz_centralities, +template void katz_centrality(raft::handle_t const& handle, + graph_view_t const& graph_view, + double const* betas, + double* katz_centralities, double alpha, double beta, double epsilon, @@ -224,10 +222,10 @@ template void katz_centrality(raft::handle_t const &handle, bool normalize, bool do_expensive_check); -template void katz_centrality(raft::handle_t const &handle, - graph_view_t const &graph_view, - float *betas, - float *katz_centralities, +template void katz_centrality(raft::handle_t const& handle, + graph_view_t const& graph_view, + float const* betas, + float* katz_centralities, float alpha, float beta, float epsilon, @@ -236,10 +234,10 @@ template void katz_centrality(raft::handle_t const &handle, bool normalize, bool do_expensive_check); -template void katz_centrality(raft::handle_t const &handle, - graph_view_t const &graph_view, - double *betas, - double *katz_centralities, +template void katz_centrality(raft::handle_t const& handle, + graph_view_t const& graph_view, + double const* betas, + double* katz_centralities, double alpha, double beta, double epsilon, @@ -248,10 +246,10 @@ template void katz_centrality(raft::handle_t const &handle, bool normalize, bool do_expensive_check); -template void katz_centrality(raft::handle_t const &handle, - graph_view_t const &graph_view, - float *betas, - float *katz_centralities, +template void katz_centrality(raft::handle_t const& handle, + graph_view_t const& graph_view, + float const* betas, + float* katz_centralities, float alpha, float beta, float epsilon, @@ -260,10 +258,10 @@ template void katz_centrality(raft::handle_t const &handle, bool normalize, bool do_expensive_check); -template void katz_centrality(raft::handle_t const &handle, - graph_view_t const &graph_view, - double *betas, - double *katz_centralities, +template void katz_centrality(raft::handle_t const& handle, + graph_view_t const& graph_view, + double const* betas, + double* katz_centralities, double alpha, double beta, double epsilon, @@ -272,10 +270,10 @@ template void katz_centrality(raft::handle_t const &handle, bool normalize, bool do_expensive_check); -template void katz_centrality(raft::handle_t const &handle, - graph_view_t const &graph_view, - float *betas, - float *katz_centralities, +template void katz_centrality(raft::handle_t const& handle, + graph_view_t const& graph_view, + float const* betas, + float* katz_centralities, float alpha, float beta, float epsilon, @@ -284,10 +282,10 @@ template void katz_centrality(raft::handle_t const &handle, bool normalize, bool do_expensive_check); -template void katz_centrality(raft::handle_t const &handle, - graph_view_t const &graph_view, - double *betas, - double *katz_centralities, +template void katz_centrality(raft::handle_t const& handle, + graph_view_t const& graph_view, + double const* betas, + double* katz_centralities, double alpha, double beta, double epsilon, @@ -296,10 +294,10 @@ template void katz_centrality(raft::handle_t const &handle, bool normalize, bool do_expensive_check); -template void katz_centrality(raft::handle_t const &handle, - graph_view_t const &graph_view, - float *betas, - float *katz_centralities, +template void katz_centrality(raft::handle_t const& handle, + graph_view_t const& graph_view, + float const* betas, + float* katz_centralities, float alpha, float beta, float epsilon, @@ -308,10 +306,10 @@ template void katz_centrality(raft::handle_t const &handle, bool normalize, bool do_expensive_check); -template void katz_centrality(raft::handle_t const &handle, - graph_view_t const &graph_view, - double *betas, - double *katz_centralities, +template void katz_centrality(raft::handle_t const& handle, + graph_view_t const& graph_view, + double const* betas, + double* katz_centralities, double alpha, double beta, double epsilon, @@ -320,10 +318,10 @@ template void katz_centrality(raft::handle_t const &handle, bool normalize, bool do_expensive_check); -template void katz_centrality(raft::handle_t const &handle, - graph_view_t const &graph_view, - float *betas, - float *katz_centralities, +template void katz_centrality(raft::handle_t const& handle, + graph_view_t const& graph_view, + float const* betas, + float* katz_centralities, float alpha, float beta, float epsilon, @@ -332,10 +330,10 @@ template void katz_centrality(raft::handle_t const &handle, bool normalize, bool do_expensive_check); -template void katz_centrality(raft::handle_t const &handle, - graph_view_t const &graph_view, - double *betas, - double *katz_centralities, +template void katz_centrality(raft::handle_t const& handle, + graph_view_t const& graph_view, + double const* betas, + double* katz_centralities, double alpha, double beta, double epsilon, diff --git a/cpp/src/experimental/louvain.cuh b/cpp/src/experimental/louvain.cuh index 1f6f8633bcd..e3b5bf91ccc 100644 --- a/cpp/src/experimental/louvain.cuh +++ b/cpp/src/experimental/louvain.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -15,23 +15,21 @@ */ #pragma once -#include - -#include +#include -#include -#include -#include -#include -#include -#include +#include +#include -#include +#include +#include +#include +#include +#include +#include +#include -#include -#include -#include -#include +#include +#include //#define TIMING @@ -42,341 +40,6 @@ namespace cugraph { namespace experimental { -namespace detail { - -template -struct create_cuco_pair_t { - cuco::pair_type __device__ operator()(data_t data) - { - cuco::pair_type tmp; - tmp.first = data; - tmp.second = data_t{0}; - return tmp; - } -}; - -// -// These classes should allow cuco::static_map to generate hash tables of -// different configurations. -// - -// -// Compare edges based on src[e] and dst[e] matching -// -template -class src_dst_equality_comparator_t { - public: - src_dst_equality_comparator_t(rmm::device_vector const &src, - rmm::device_vector const &dst, - sentinel_t sentinel_value) - : d_src_{src.data().get()}, d_dst_{dst.data().get()}, sentinel_value_(sentinel_value) - { - } - - src_dst_equality_comparator_t(data_t const *d_src, data_t const *d_dst, sentinel_t sentinel_value) - : d_src_{d_src}, d_dst_{d_dst}, sentinel_value_(sentinel_value) - { - } - - template - __device__ bool operator()(idx_type lhs_index, idx_type rhs_index) const noexcept - { - return (lhs_index != sentinel_value_) && (rhs_index != sentinel_value_) && - (d_src_[lhs_index] == d_src_[rhs_index]) && (d_dst_[lhs_index] == d_dst_[rhs_index]); - } - - private: - data_t const *d_src_; - data_t const *d_dst_; - sentinel_t sentinel_value_; -}; - -// -// Hash edges based src[e] and dst[e] -// -template -class src_dst_hasher_t { - public: - src_dst_hasher_t(rmm::device_vector const &src, rmm::device_vector const &dst) - : d_src_{src.data().get()}, d_dst_{dst.data().get()} - { - } - - src_dst_hasher_t(data_t const *d_src, data_t const *d_dst) : d_src_{d_src}, d_dst_{d_dst} {} - - template - __device__ auto operator()(idx_type index) const - { - cuco::detail::MurmurHash3_32 hasher; - - auto h_src = hasher(d_src_[index]); - auto h_dst = hasher(d_dst_[index]); - - /* - * Combine the source hash and the dest hash into a single hash value - * - * Taken from the Boost hash_combine function - * https://www.boost.org/doc/libs/1_35_0/doc/html/boost/hash_combine_id241013.html - */ - h_src ^= h_dst + 0x9e3779b9 + (h_src << 6) + (h_src >> 2); - - return h_src; - } - - private: - data_t const *d_src_; - data_t const *d_dst_; -}; - -// -// Compare edges based on src[e] and cluster[dst[e]] matching -// -template -class src_cluster_equality_comparator_t { - public: - src_cluster_equality_comparator_t(rmm::device_vector const &src, - rmm::device_vector const &dst, - rmm::device_vector const &dst_cluster_cache, - data_t base_dst_id, - sentinel_t sentinel_value) - : d_src_{src.data().get()}, - d_dst_{dst.data().get()}, - d_dst_cluster_{dst_cluster_cache.data().get()}, - base_dst_id_(base_dst_id), - sentinel_value_(sentinel_value) - { - } - - src_cluster_equality_comparator_t(data_t const *d_src, - data_t const *d_dst, - data_t const *d_dst_cluster_cache, - data_t base_dst_id, - sentinel_t sentinel_value) - : d_src_{d_src}, - d_dst_{d_dst}, - d_dst_cluster_{d_dst_cluster_cache}, - base_dst_id_(base_dst_id), - sentinel_value_(sentinel_value) - { - } - - __device__ bool operator()(sentinel_t lhs_index, sentinel_t rhs_index) const noexcept - { - return (lhs_index != sentinel_value_) && (rhs_index != sentinel_value_) && - (d_src_[lhs_index] == d_src_[rhs_index]) && - (d_dst_cluster_[d_dst_[lhs_index] - base_dst_id_] == - d_dst_cluster_[d_dst_[rhs_index] - base_dst_id_]); - } - - private: - data_t const *d_src_; - data_t const *d_dst_; - data_t const *d_dst_cluster_; - data_t base_dst_id_; - sentinel_t sentinel_value_; -}; - -// -// Hash edges based src[e] and cluster[dst[e]] -// -template -class src_cluster_hasher_t { - public: - src_cluster_hasher_t(rmm::device_vector const &src, - rmm::device_vector const &dst, - rmm::device_vector const &dst_cluster_cache, - data_t base_dst_id) - : d_src_{src.data().get()}, - d_dst_{dst.data().get()}, - d_dst_cluster_{dst_cluster_cache.data().get()}, - base_dst_id_(base_dst_id) - { - } - - src_cluster_hasher_t(data_t const *d_src, - data_t const *d_dst, - data_t const *d_dst_cluster_cache, - data_t base_dst_id) - : d_src_{d_src}, d_dst_{d_dst}, d_dst_cluster_{d_dst_cluster_cache}, base_dst_id_(base_dst_id) - { - } - - template - __device__ auto operator()(idx_type index) const - { - cuco::detail::MurmurHash3_32 hasher; - - auto h_src = hasher(d_src_[index]); - auto h_cluster = hasher(d_dst_cluster_[d_dst_[index] - base_dst_id_]); - - /* - * Combine the source hash and the cluster hash into a single hash value - * - * Taken from the Boost hash_combine function - * https://www.boost.org/doc/libs/1_35_0/doc/html/boost/hash_combine_id241013.html - */ - h_src ^= h_cluster + 0x9e3779b9 + (h_src << 6) + (h_src >> 2); - - return h_src; - } - - private: - data_t const *d_src_; - data_t const *d_dst_; - data_t const *d_dst_cluster_; - data_t base_dst_id_; -}; - -// -// Skip edges where src[e] == dst[e] -// -template -class skip_edge_t { - public: - skip_edge_t(rmm::device_vector const &src, rmm::device_vector const &dst) - : d_src_{src.data().get()}, d_dst_{dst.data().get()} - { - } - - skip_edge_t(data_t const *src, data_t const *dst) : d_src_{src}, d_dst_{dst} {} - - template - __device__ auto operator()(idx_type index) const - { - return d_src_[index] == d_dst_[index]; - } - - private: - data_t const *d_src_; - data_t const *d_dst_; -}; - -template -struct lookup_by_vertex_id { - public: - lookup_by_vertex_id(data_t const *d_array, vertex_t const *d_vertices, vertex_t base_vertex_id) - : d_array_(d_array), d_vertices_(d_vertices), base_vertex_id_(base_vertex_id) - { - } - - template - data_t operator() __device__(edge_t edge_id) const - { - return d_array_[d_vertices_[edge_id] - base_vertex_id_]; - } - - private: - data_t const *d_array_; - vertex_t const *d_vertices_; - vertex_t base_vertex_id_; -}; - -template -vector_t remove_elements_from_vector(vector_t const &input_v, - iterator_t iterator_begin, - iterator_t iterator_end, - function_t function, - cudaStream_t stream) -{ - vector_t temp_v(input_v.size()); - - auto last = thrust::copy_if( - rmm::exec_policy(stream)->on(stream), iterator_begin, iterator_end, temp_v.begin(), function); - - temp_v.resize(thrust::distance(temp_v.begin(), last)); - - return temp_v; -} - -template -vector_t remove_elements_from_vector(vector_t const &input_v, - function_t function, - cudaStream_t stream) -{ - return remove_elements_from_vector(input_v, input_v.begin(), input_v.end(), function, stream); -} - -// FIXME: This should be a generic utility. The one in cython.cu -// is very close to this -template * = nullptr> -std::unique_ptr> -create_graph(raft::handle_t const &handle, - rmm::device_vector const &src_v, - rmm::device_vector const &dst_v, - rmm::device_vector const &weight_v, - std::size_t num_local_verts, - experimental::graph_properties_t graph_props, - view_t const &view) -{ - std::vector> edgelist( - {{src_v.data().get(), - dst_v.data().get(), - weight_v.data().get(), - static_cast(src_v.size())}}); - - return std::make_unique>( - handle, - edgelist, - view.get_partition(), - num_local_verts, - src_v.size(), - graph_props, - false, - false); -} - -template * = nullptr> -std::unique_ptr> -create_graph(raft::handle_t const &handle, - rmm::device_vector const &src_v, - rmm::device_vector const &dst_v, - rmm::device_vector const &weight_v, - std::size_t num_local_verts, - experimental::graph_properties_t graph_props, - view_t const &view) -{ - experimental::edgelist_t edgelist{ - src_v.data().get(), - dst_v.data().get(), - weight_v.data().get(), - static_cast(src_v.size())}; - - return std::make_unique>( - handle, edgelist, num_local_verts, graph_props, false, false); -} - -} // namespace detail - -// -// FIXME: Ultimately, this would be cleaner and more efficient if we did the following: -// -// 1) Create an object that does a single level Louvain computation on an input graph -// (no graph contraction) -// 2) Create an object that does graph contraction -// 3) Create Louvain to use these objects in sequence to compute the aggregate result. -// -// In MNMG-world, the graph contraction step is going to create another graph that likely -// fits efficiently in a smaller number of GPUs (eventually one). Decomposing the algorithm -// as above would allow us to eventually run the single GPU version of single level Louvain -// on the contracted graphs - which should be more efficient. -// -// FIXME: We should return the dendogram and let the python layer clean it up (or have a -// separate C++ function to flatten the dendogram). There are customers that might -// like the dendogram and the implementation would be a bit cleaner if we did the -// collapsing as a separate step -// template class Louvain { public: @@ -390,82 +53,47 @@ class Louvain { graph_view_t::is_adj_matrix_transposed, graph_view_t::is_multi_gpu>; - Louvain(raft::handle_t const &handle, graph_view_t const &graph_view) + Louvain(raft::handle_t const& handle, graph_view_t const& graph_view) : #ifdef TIMING hr_timer_(), #endif handle_(handle), + dendrogram_(std::make_unique>()), current_graph_view_(graph_view), - compute_partition_(graph_view), - local_num_vertices_(graph_view.get_number_of_local_vertices()), - local_num_rows_(graph_view.get_number_of_local_adj_matrix_partition_rows()), - local_num_cols_(graph_view.get_number_of_local_adj_matrix_partition_cols()), - local_num_edges_(graph_view.get_number_of_edges()), - vertex_weights_v_(graph_view.get_number_of_local_vertices()), - cluster_weights_v_(graph_view.get_number_of_local_vertices()), - cluster_v_(graph_view.get_number_of_local_vertices()), - number_of_vertices_(graph_view.get_number_of_local_vertices()), - stream_(handle.get_stream()) + cluster_keys_v_(graph_view.get_number_of_local_vertices(), handle.get_stream_view()), + cluster_weights_v_(graph_view.get_number_of_local_vertices(), handle.get_stream_view()), + vertex_weights_v_(graph_view.get_number_of_local_vertices(), handle.get_stream_view()), + src_vertex_weights_cache_v_(0, handle.get_stream_view()), + src_cluster_cache_v_(0, handle.get_stream_view()), + dst_cluster_cache_v_(0, handle.get_stream_view()) { - if (graph_view_t::is_multi_gpu) { - rank_ = handle.get_comms().get_rank(); - base_vertex_id_ = graph_view.get_local_vertex_first(); - base_src_vertex_id_ = graph_view.get_local_adj_matrix_partition_row_first(0); - base_dst_vertex_id_ = graph_view.get_local_adj_matrix_partition_col_first(0); - - raft::copy(&local_num_edges_, - graph_view.offsets() + graph_view.get_local_adj_matrix_partition_row_last(0) - - graph_view.get_local_adj_matrix_partition_row_first(0), - 1, - stream_); - - CUDA_TRY(cudaStreamSynchronize(stream_)); - } + } - src_indices_v_.resize(local_num_edges_); + Dendrogram const& get_dendrogram() const { return *dendrogram_; } - cugraph::detail::offsets_to_indices( - current_graph_view_.offsets(), local_num_rows_, src_indices_v_.data().get()); + Dendrogram& get_dendrogram() { return *dendrogram_; } - if (base_src_vertex_id_ > 0) { - thrust::transform(rmm::exec_policy(stream_)->on(stream_), - src_indices_v_.begin(), - src_indices_v_.end(), - thrust::make_constant_iterator(base_src_vertex_id_), - src_indices_v_.begin(), - thrust::plus()); - } - } + std::unique_ptr> move_dendrogram() { return std::move(dendrogram_); } - virtual std::pair operator()(vertex_t *d_cluster_vec, - size_t max_level, - weight_t resolution) + virtual weight_t operator()(size_t max_level, weight_t resolution) { - size_t num_level{0}; + weight_t best_modularity = weight_t{-1}; - weight_t total_edge_weight; - total_edge_weight = experimental::transform_reduce_e( + weight_t total_edge_weight = experimental::transform_reduce_e( handle_, current_graph_view_, thrust::make_constant_iterator(0), thrust::make_constant_iterator(0), - [] __device__(auto, auto, weight_t wt, auto, auto) { return wt; }, + [] __device__(auto src, auto dst, weight_t wt, auto, auto) { return wt; }, weight_t{0}); - weight_t best_modularity = weight_t{-1}; + while (dendrogram_->num_levels() < max_level) { + // + // Initialize every cluster to reference each vertex to itself + // + initialize_dendrogram_level(current_graph_view_.get_number_of_local_vertices()); - // - // Initialize every cluster to reference each vertex to itself - // - thrust::sequence(rmm::exec_policy(stream_)->on(stream_), - cluster_v_.begin(), - cluster_v_.end(), - base_vertex_id_); - thrust::copy( - rmm::exec_policy(stream_)->on(stream_), cluster_v_.begin(), cluster_v_.end(), d_cluster_vec); - - while (num_level < max_level) { compute_vertex_and_cluster_weights(); weight_t new_Q = update_clustering(total_edge_weight, resolution); @@ -474,56 +102,85 @@ class Louvain { best_modularity = new_Q; - shrink_graph(d_cluster_vec); - - num_level++; + shrink_graph(); } timer_display(std::cout); - return std::make_pair(num_level, best_modularity); + return best_modularity; } protected: - void timer_start(std::string const ®ion) + void timer_start(std::string const& region) { #ifdef TIMING - if (rank_ == 0) hr_timer_.start(region); + if (graph_view_t::is_multi_gpu) { + if (handle.get_comms().get_rank() == 0) hr_timer_.start(region); + } else { + hr_timer_.start(region); + } #endif } - void timer_stop(cudaStream_t stream) + void timer_stop(rmm::cuda_stream_view stream_view) { #ifdef TIMING - if (rank_ == 0) { - CUDA_TRY(cudaStreamSynchronize(stream)); + if (graph_view_t::is_multi_gpu) { + if (handle.get_comms().get_rank() == 0) { + stream_view.synchronize(); + hr_timer_.stop(); + } + } else { + stream_view.synchronize(); hr_timer_.stop(); } #endif } - void timer_display(std::ostream &os) + void timer_display(std::ostream& os) { #ifdef TIMING - if (rank_ == 0) hr_timer_.display(os); + if (graph_view_t::is_multi_gpu) { + if (handle.get_comms().get_rank() == 0) hr_timer_.display(os); + } else { + hr_timer_.display(os); + } #endif } + protected: + void initialize_dendrogram_level(vertex_t num_vertices) + { + dendrogram_->add_level( + current_graph_view_.get_local_vertex_first(), num_vertices, handle_.get_stream_view()); + + thrust::sequence(rmm::exec_policy(handle_.get_stream_view()), + dendrogram_->current_level_begin(), + dendrogram_->current_level_end(), + current_graph_view_.get_local_vertex_first()); + } + public: weight_t modularity(weight_t total_edge_weight, weight_t resolution) { - weight_t sum_degree_squared = experimental::transform_reduce_v( - handle_, - current_graph_view_, + weight_t sum_degree_squared = thrust::transform_reduce( + rmm::exec_policy(handle_.get_stream_view()), cluster_weights_v_.begin(), + cluster_weights_v_.end(), [] __device__(weight_t p) { return p * p; }, - weight_t{0}); + weight_t{0}, + thrust::plus()); + + if (graph_t::is_multi_gpu) { + sum_degree_squared = + host_scalar_allreduce(handle_.get_comms(), sum_degree_squared, handle_.get_stream()); + } weight_t sum_internal = experimental::transform_reduce_e( handle_, current_graph_view_, - src_cluster_cache_v_.begin(), - dst_cluster_cache_v_.begin(), + d_src_cluster_cache_, + d_dst_cluster_cache_, [] __device__(auto src, auto dst, weight_t wt, auto src_cluster, auto nbr_cluster) { if (src_cluster == nbr_cluster) { return wt; @@ -543,53 +200,71 @@ class Louvain { { timer_start("compute_vertex_and_cluster_weights"); - experimental::copy_v_transform_reduce_out_nbr( - handle_, - current_graph_view_, - thrust::make_constant_iterator(0), - thrust::make_constant_iterator(0), - [] __device__(auto src, auto, auto wt, auto, auto) { return wt; }, - weight_t{0}, - vertex_weights_v_.begin()); + vertex_weights_v_ = current_graph_view_.compute_out_weight_sums(handle_); + cluster_keys_v_.resize(vertex_weights_v_.size(), handle_.get_stream_view()); + cluster_weights_v_.resize(vertex_weights_v_.size(), handle_.get_stream_view()); - thrust::copy(rmm::exec_policy(stream_)->on(stream_), - vertex_weights_v_.begin(), - vertex_weights_v_.end(), - cluster_weights_v_.begin()); + thrust::sequence(rmm::exec_policy(handle_.get_stream_view()), + cluster_keys_v_.begin(), + cluster_keys_v_.end(), + current_graph_view_.get_local_vertex_first()); - cache_vertex_properties( - vertex_weights_v_, src_vertex_weights_cache_v_, dst_vertex_weights_cache_v_); + raft::copy(cluster_weights_v_.begin(), + vertex_weights_v_.begin(), + vertex_weights_v_.size(), + handle_.get_stream()); - cache_vertex_properties( - cluster_weights_v_, src_cluster_weights_cache_v_, dst_cluster_weights_cache_v_); + d_src_vertex_weights_cache_ = + cache_src_vertex_properties(vertex_weights_v_, src_vertex_weights_cache_v_); - timer_stop(stream_); + if (graph_view_t::is_multi_gpu) { + auto const comm_size = handle_.get_comms().get_size(); + rmm::device_uvector rx_keys_v(0, handle_.get_stream_view()); + rmm::device_uvector rx_weights_v(0, handle_.get_stream_view()); + + auto pair_first = thrust::make_zip_iterator( + thrust::make_tuple(cluster_keys_v_.begin(), cluster_weights_v_.begin())); + + std::forward_as_tuple(std::tie(rx_keys_v, rx_weights_v), std::ignore) = + groupby_gpuid_and_shuffle_values( + handle_.get_comms(), + pair_first, + pair_first + current_graph_view_.get_number_of_local_vertices(), + [key_func = + cugraph::experimental::detail::compute_gpu_id_from_vertex_t{ + comm_size}] __device__(auto val) { return key_func(thrust::get<0>(val)); }, + handle_.get_stream_view()); + + cluster_keys_v_ = std::move(rx_keys_v); + cluster_weights_v_ = std::move(rx_weights_v); + } + + timer_stop(handle_.get_stream_view()); } - // - // FIXME: Consider returning d_src_cache and d_dst_cache - // (as a pair). This would be a nice optimization - // for single GPU, as we wouldn't need to make 3 copies - // of the data, could return a pair of device pointers to - // local_input_v. - // template - void cache_vertex_properties(rmm::device_vector const &local_input_v, - rmm::device_vector &src_cache_v, - rmm::device_vector &dst_cache_v, - bool src = true, - bool dst = true) + T* cache_src_vertex_properties(rmm::device_uvector& input, rmm::device_uvector& src_cache_v) { - if (src) { - src_cache_v.resize(current_graph_view_.get_number_of_local_adj_matrix_partition_rows()); - copy_to_adj_matrix_row( - handle_, current_graph_view_, local_input_v.begin(), src_cache_v.begin()); + if (graph_view_t::is_multi_gpu) { + src_cache_v.resize(current_graph_view_.get_number_of_local_adj_matrix_partition_rows(), + handle_.get_stream_view()); + copy_to_adj_matrix_row(handle_, current_graph_view_, input.begin(), src_cache_v.begin()); + return src_cache_v.begin(); + } else { + return input.begin(); } + } - if (dst) { - dst_cache_v.resize(current_graph_view_.get_number_of_local_adj_matrix_partition_cols()); - copy_to_adj_matrix_col( - handle_, current_graph_view_, local_input_v.begin(), dst_cache_v.begin()); + template + T* cache_dst_vertex_properties(rmm::device_uvector& input, rmm::device_uvector& dst_cache_v) + { + if (graph_view_t::is_multi_gpu) { + dst_cache_v.resize(current_graph_view_.get_number_of_local_adj_matrix_partition_cols(), + handle_.get_stream_view()); + copy_to_adj_matrix_col(handle_, current_graph_view_, input.begin(), dst_cache_v.begin()); + return dst_cache_v.begin(); + } else { + return input.begin(); } } @@ -597,9 +272,16 @@ class Louvain { { timer_start("update_clustering"); - rmm::device_vector next_cluster_v(cluster_v_); + rmm::device_uvector next_cluster_v(dendrogram_->current_level_size(), + handle_.get_stream_view()); + + raft::copy(next_cluster_v.begin(), + dendrogram_->current_level_begin(), + dendrogram_->current_level_size(), + handle_.get_stream()); - cache_vertex_properties(next_cluster_v, src_cluster_cache_v_, dst_cluster_cache_v_); + d_src_cluster_cache_ = cache_src_vertex_properties(next_cluster_v, src_cluster_cache_v_); + d_dst_cluster_cache_ = cache_dst_vertex_properties(next_cluster_v, dst_cluster_cache_v_); weight_t new_Q = modularity(total_edge_weight, resolution); weight_t cur_Q = new_Q - 1; @@ -616,1102 +298,267 @@ class Louvain { up_down = !up_down; - cache_vertex_properties(next_cluster_v, src_cluster_cache_v_, dst_cluster_cache_v_); - new_Q = modularity(total_edge_weight, resolution); if (new_Q > cur_Q) { - thrust::copy(rmm::exec_policy(stream_)->on(stream_), - next_cluster_v.begin(), - next_cluster_v.end(), - cluster_v_.begin()); + raft::copy(dendrogram_->current_level_begin(), + next_cluster_v.begin(), + next_cluster_v.size(), + handle_.get_stream()); } } - // cache the final clustering locally on each cpu - cache_vertex_properties(cluster_v_, src_cluster_cache_v_, dst_cluster_cache_v_); - - timer_stop(stream_); + timer_stop(handle_.get_stream_view()); return cur_Q; } - void update_by_delta_modularity(weight_t total_edge_weight, - weight_t resolution, - rmm::device_vector &next_cluster_v, - bool up_down) + void compute_cluster_sum_and_subtract(rmm::device_uvector& old_cluster_sum_v, + rmm::device_uvector& cluster_subtract_v) { - rmm::device_vector old_cluster_sum_v(local_num_vertices_); - rmm::device_vector src_old_cluster_sum_cache_v; + auto output_buffer = + cugraph::experimental::allocate_dataframe_buffer>( + current_graph_view_.get_number_of_local_vertices(), handle_.get_stream_view()); experimental::copy_v_transform_reduce_out_nbr( handle_, current_graph_view_, - src_cluster_cache_v_.begin(), - dst_cluster_cache_v_.begin(), + d_src_cluster_cache_, + d_dst_cluster_cache_, [] __device__(auto src, auto dst, auto wt, auto src_cluster, auto nbr_cluster) { - if ((src != dst) && (src_cluster == nbr_cluster)) { - return wt; - } else - return weight_t{0}; - }, - weight_t{0}, - old_cluster_sum_v.begin()); - - cache_vertex_properties( - old_cluster_sum_v, src_old_cluster_sum_cache_v, empty_cache_weight_v_, true, false); - - detail::src_cluster_equality_comparator_t compare( - src_indices_v_.data().get(), - current_graph_view_.indices(), - dst_cluster_cache_v_.data().get(), - base_dst_vertex_id_, - std::numeric_limits::max()); - detail::src_cluster_hasher_t hasher(src_indices_v_.data().get(), - current_graph_view_.indices(), - dst_cluster_cache_v_.data().get(), - base_dst_vertex_id_); - detail::skip_edge_t skip_edge(src_indices_v_.data().get(), - current_graph_view_.indices()); - - // - // Group edges that lead from same source to same neighboring cluster together - // local_cluster_edge_ids_v will contain edge ids of unique pairs of (src,nbr_cluster). - // If multiple edges exist, one edge id will be chosen (by a parallel race). - // nbr_weights_v will contain the combined weight of all of the edges that connect - // that pair. - // - rmm::device_vector local_cluster_edge_ids_v; - rmm::device_vector nbr_weights_v; - - // - // Perform this combining on the local edges - // - std::tie(local_cluster_edge_ids_v, nbr_weights_v) = combine_local_src_nbr_cluster_weights( - hasher, compare, skip_edge, current_graph_view_.weights(), local_num_edges_); - - // - // In order to compute delta_Q for a given src/nbr_cluster pair, I need the following - // information: - // src - // old_cluster - the cluster that src is currently assigned to - // nbr_cluster - // sum of edges going to new cluster - // vertex weight of the src vertex - // sum of edges going to old cluster - // cluster_weights of old cluster - // cluster_weights of nbr_cluster - // - // Each GPU has locally cached: - // The sum of edges going to the old cluster (computed from - // experimental::copy_v_transform_reduce_out_nbr call above. - // old_cluster - // nbr_cluster - // vertex weight of src vertex - // partial sum of edges going to the new cluster (in nbr_weights) - // - // So the plan is to take the tuple: - // (src, old_cluster, src_vertex_weight, old_cluster_sum, nbr_cluster, nbr_weights) - // and shuffle it around the cluster so that they arrive at the GPU where the pair - // (old_cluster, new_cluster) would be assigned. Then we can aggregate this information - // and compute the delta_Q values. - // - - // - // Define the communication pattern, we're going to send detail - // for edge i to the GPU that is responsible for the vertex - // pair (cluster[src[i]], cluster[dst[i]]) - // - auto communication_schedule = thrust::make_transform_iterator( - local_cluster_edge_ids_v.begin(), - [d_edge_device_view = compute_partition_.edge_device_view(), - d_src_indices = src_indices_v_.data().get(), - d_src_cluster = src_cluster_cache_v_.data().get(), - d_dst_indices = current_graph_view_.indices(), - d_dst_cluster = dst_cluster_cache_v_.data().get(), - base_src_vertex_id = base_src_vertex_id_, - base_dst_vertex_id = base_dst_vertex_id_] __device__(edge_t edge_id) { - return d_edge_device_view(d_src_cluster[d_src_indices[edge_id] - base_src_vertex_id], - d_dst_cluster[d_dst_indices[edge_id] - base_dst_vertex_id]); - }); + weight_t subtract{0}; + weight_t sum{0}; - // FIXME: This should really be a variable_shuffle of a tuple, for time - // reasons I'm just doing 6 independent shuffles. - // - rmm::device_vector ocs_v = variable_shuffle( - handle_, - local_cluster_edge_ids_v.size(), - thrust::make_transform_iterator( - local_cluster_edge_ids_v.begin(), - detail::lookup_by_vertex_id(src_old_cluster_sum_cache_v.data().get(), - src_indices_v_.data().get(), - base_src_vertex_id_)), - communication_schedule); - - rmm::device_vector src_cluster_v = - variable_shuffle( - handle_, - local_cluster_edge_ids_v.size(), - thrust::make_transform_iterator( - local_cluster_edge_ids_v.begin(), - detail::lookup_by_vertex_id( - src_cluster_cache_v_.data().get(), src_indices_v_.data().get(), base_src_vertex_id_)), - communication_schedule); - - rmm::device_vector src_vertex_weight_v = - variable_shuffle( - handle_, - local_cluster_edge_ids_v.size(), - thrust::make_transform_iterator( - local_cluster_edge_ids_v.begin(), - detail::lookup_by_vertex_id(src_vertex_weights_cache_v_.data().get(), - src_indices_v_.data().get(), - base_src_vertex_id_)), - communication_schedule); - - rmm::device_vector src_v = variable_shuffle( - handle_, - local_cluster_edge_ids_v.size(), - thrust::make_permutation_iterator(src_indices_v_.begin(), local_cluster_edge_ids_v.begin()), - communication_schedule); + if (src == dst) + subtract = wt; + else if (src_cluster == nbr_cluster) + sum = wt; - rmm::device_vector nbr_cluster_v = - variable_shuffle( - handle_, - local_cluster_edge_ids_v.size(), - thrust::make_transform_iterator( - local_cluster_edge_ids_v.begin(), - detail::lookup_by_vertex_id( - dst_cluster_cache_v_.data().get(), current_graph_view_.indices(), base_dst_vertex_id_)), - communication_schedule); - - nbr_weights_v = variable_shuffle( - handle_, nbr_weights_v.size(), nbr_weights_v.begin(), communication_schedule); - - // - // At this point, src_v, nbr_cluster_v and nbr_weights_v have been - // shuffled to the correct GPU. We can now compute the final - // value of delta_Q for each neigboring cluster - // - // Again, we'll combine edges that connect the same source to the same - // neighboring cluster and sum their weights. - // - detail::src_dst_equality_comparator_t compare2( - src_v, nbr_cluster_v, std::numeric_limits::max()); - detail::src_dst_hasher_t hasher2(src_v, nbr_cluster_v); - - auto skip_edge2 = [] __device__(auto) { return false; }; - - std::tie(local_cluster_edge_ids_v, nbr_weights_v) = combine_local_src_nbr_cluster_weights( - hasher2, compare2, skip_edge2, nbr_weights_v.data().get(), src_v.size()); - - // - // Now local_cluster_edge_ids_v contains the edge ids of the src id/dest - // cluster id pairs, and nbr_weights_v contains the weight of edges - // going to that cluster id - // - // Now we can compute (locally) each delta_Q value - // - auto iter = thrust::make_zip_iterator( - thrust::make_tuple(local_cluster_edge_ids_v.begin(), nbr_weights_v.begin())); - - thrust::transform(rmm::exec_policy(stream_)->on(stream_), - iter, - iter + local_cluster_edge_ids_v.size(), - nbr_weights_v.begin(), - [total_edge_weight, - resolution, - d_src = src_v.data().get(), - d_src_cluster = src_cluster_v.data().get(), - d_nbr_cluster = nbr_cluster_v.data().get(), - d_src_vertex_weights = src_vertex_weight_v.data().get(), - d_src_cluster_weights = src_cluster_weights_cache_v_.data().get(), - d_dst_cluster_weights = dst_cluster_weights_cache_v_.data().get(), - d_ocs = ocs_v.data().get(), - base_src_vertex_id = base_src_vertex_id_, - base_dst_vertex_id = base_dst_vertex_id_] __device__(auto tuple) { - edge_t edge_id = thrust::get<0>(tuple); - vertex_t nbr_cluster = d_nbr_cluster[edge_id]; - weight_t new_cluster_sum = thrust::get<1>(tuple); - vertex_t old_cluster = d_src_cluster[edge_id]; - weight_t k_k = d_src_vertex_weights[edge_id]; - weight_t old_cluster_sum = d_ocs[edge_id]; - - weight_t a_old = d_src_cluster_weights[old_cluster - base_src_vertex_id]; - weight_t a_new = d_dst_cluster_weights[nbr_cluster - base_dst_vertex_id]; - - return 2 * (((new_cluster_sum - old_cluster_sum) / total_edge_weight) - - resolution * (a_new * k_k - a_old * k_k + k_k * k_k) / - (total_edge_weight * total_edge_weight)); - }); - - // - // Pick the largest delta_Q value for each vertex on this gpu. - // Then we will shuffle back to the gpu by vertex id - // - rmm::device_vector final_src_v(local_cluster_edge_ids_v.size()); - rmm::device_vector final_nbr_cluster_v(local_cluster_edge_ids_v.size()); - rmm::device_vector final_nbr_weights_v(local_cluster_edge_ids_v.size()); - - auto final_input_iter = thrust::make_zip_iterator(thrust::make_tuple( - thrust::make_permutation_iterator(src_v.begin(), local_cluster_edge_ids_v.begin()), - thrust::make_permutation_iterator(nbr_cluster_v.begin(), local_cluster_edge_ids_v.begin()), - nbr_weights_v.begin())); - - auto final_output_iter = thrust::make_zip_iterator(thrust::make_tuple( - final_src_v.begin(), final_nbr_cluster_v.begin(), final_nbr_weights_v.begin())); - - auto final_output_pos = - thrust::copy_if(rmm::exec_policy(stream_)->on(stream_), - final_input_iter, - final_input_iter + local_cluster_edge_ids_v.size(), - final_output_iter, - [] __device__(auto p) { return (thrust::get<2>(p) > weight_t{0}); }); - - final_src_v.resize(thrust::distance(final_output_iter, final_output_pos)); - final_nbr_cluster_v.resize(thrust::distance(final_output_iter, final_output_pos)); - final_nbr_weights_v.resize(thrust::distance(final_output_iter, final_output_pos)); - - // - // Sort the results, pick the largest version - // - thrust::sort(rmm::exec_policy(stream_)->on(stream_), - thrust::make_zip_iterator(thrust::make_tuple( - final_src_v.begin(), final_nbr_weights_v.begin(), final_nbr_cluster_v.begin())), - thrust::make_zip_iterator(thrust::make_tuple( - final_src_v.end(), final_nbr_weights_v.end(), final_nbr_cluster_v.begin())), - [] __device__(auto left, auto right) { - if (thrust::get<0>(left) < thrust::get<0>(right)) return true; - if (thrust::get<0>(left) > thrust::get<0>(right)) return false; - if (thrust::get<1>(left) > thrust::get<1>(right)) return true; - if (thrust::get<1>(left) < thrust::get<1>(right)) return false; - return (thrust::get<2>(left) < thrust::get<2>(right)); - }); - - // - // Now that we're sorted the first entry for each src value is the largest. - // - local_cluster_edge_ids_v.resize(final_src_v.size()); - - thrust::transform(rmm::exec_policy(stream_)->on(stream_), - thrust::make_counting_iterator(0), - thrust::make_counting_iterator(final_src_v.size()), - local_cluster_edge_ids_v.begin(), - [sentinel = std::numeric_limits::max(), - d_src = final_src_v.data().get()] __device__(edge_t edge_id) { - if (edge_id == 0) { return edge_id; } - - if (d_src[edge_id - 1] != d_src[edge_id]) { return edge_id; } - - return sentinel; - }); - - local_cluster_edge_ids_v = detail::remove_elements_from_vector( - local_cluster_edge_ids_v, - [sentinel = std::numeric_limits::max()] __device__(auto edge_id) { - return (edge_id != sentinel); + return thrust::make_tuple(subtract, sum); }, - stream_); - - final_nbr_cluster_v = variable_shuffle( - handle_, - local_cluster_edge_ids_v.size(), - thrust::make_permutation_iterator(final_nbr_cluster_v.begin(), - local_cluster_edge_ids_v.begin()), - thrust::make_transform_iterator( - thrust::make_permutation_iterator(final_src_v.begin(), local_cluster_edge_ids_v.begin()), - [d_vertex_device_view = compute_partition_.vertex_device_view()] __device__(vertex_t v) { - return d_vertex_device_view(v); - })); - - final_nbr_weights_v = variable_shuffle( - handle_, - local_cluster_edge_ids_v.size(), - thrust::make_permutation_iterator(final_nbr_weights_v.begin(), - local_cluster_edge_ids_v.begin()), - thrust::make_transform_iterator( - thrust::make_permutation_iterator(final_src_v.begin(), local_cluster_edge_ids_v.begin()), - [d_vertex_device_view = compute_partition_.vertex_device_view()] __device__(vertex_t v) { - return d_vertex_device_view(v); - })); - - final_src_v = variable_shuffle( - handle_, - local_cluster_edge_ids_v.size(), - thrust::make_permutation_iterator(final_src_v.begin(), local_cluster_edge_ids_v.begin()), - thrust::make_transform_iterator( - thrust::make_permutation_iterator(final_src_v.begin(), local_cluster_edge_ids_v.begin()), - [d_vertex_device_view = compute_partition_.vertex_device_view()] __device__(vertex_t v) { - return d_vertex_device_view(v); - })); - - // - // At this point... - // final_src_v contains the source indices - // final_nbr_cluster_v contains the neighboring clusters - // final_nbr_weights_v contains delta_Q for moving src to the neighboring - // - // They have been shuffled to the gpus responsible for their source vertex - // - // FIXME: Think about how this should work. - // I think Leiden is broken. I don't think that the code we have - // actually does anything. For now I'm going to ignore Leiden in - // MNMG, we can reconsider this later. - // - // If we ignore Leiden, I'd like to think about whether the reduction - // should occur now... - // - - // - // Sort the results, pick the largest version - // - thrust::sort(rmm::exec_policy(stream_)->on(stream_), - thrust::make_zip_iterator(thrust::make_tuple( - final_src_v.begin(), final_nbr_weights_v.begin(), final_nbr_cluster_v.begin())), - thrust::make_zip_iterator(thrust::make_tuple( - final_src_v.end(), final_nbr_weights_v.end(), final_nbr_cluster_v.begin())), - [] __device__(auto left, auto right) { - if (thrust::get<0>(left) < thrust::get<0>(right)) return true; - if (thrust::get<0>(left) > thrust::get<0>(right)) return false; - if (thrust::get<1>(left) > thrust::get<1>(right)) return true; - if (thrust::get<1>(left) < thrust::get<1>(right)) return false; - return (thrust::get<2>(left) < thrust::get<2>(right)); - }); - - // - // Now that we're sorted (ascending), the last entry for each src value is the largest. - // - local_cluster_edge_ids_v.resize(final_src_v.size()); - - thrust::transform(rmm::exec_policy(stream_)->on(stream_), - thrust::make_counting_iterator(0), - thrust::make_counting_iterator(final_src_v.size()), - local_cluster_edge_ids_v.begin(), - [sentinel = std::numeric_limits::max(), - d_src = final_src_v.data().get()] __device__(edge_t edge_id) { - if (edge_id == 0) { return edge_id; } - - if (d_src[edge_id - 1] != d_src[edge_id]) { return edge_id; } - - return sentinel; - }); - - local_cluster_edge_ids_v = detail::remove_elements_from_vector( - local_cluster_edge_ids_v, - [sentinel = std::numeric_limits::max()] __device__(auto edge_id) { - return (edge_id != sentinel); - }, - stream_); - - rmm::device_vector cluster_increase_v(final_src_v.size()); - rmm::device_vector cluster_decrease_v(final_src_v.size()); - rmm::device_vector old_cluster_v(final_src_v.size()); - - // - // Then we can, on each gpu, do a local assignment for all of the - // vertices assigned to that gpu using the up_down logic - // - local_cluster_edge_ids_v = detail::remove_elements_from_vector( - local_cluster_edge_ids_v, - local_cluster_edge_ids_v.begin(), - local_cluster_edge_ids_v.end(), - [d_final_src = final_src_v.data().get(), - d_final_nbr_cluster = final_nbr_cluster_v.data().get(), - d_final_nbr_weights = final_nbr_weights_v.data().get(), - d_cluster_increase = cluster_increase_v.data().get(), - d_cluster_decrease = cluster_decrease_v.data().get(), - d_vertex_weights = src_vertex_weights_cache_v_.data().get(), - d_next_cluster = next_cluster_v.data().get(), - d_old_cluster = old_cluster_v.data().get(), - base_vertex_id = base_vertex_id_, - base_src_vertex_id = base_src_vertex_id_, - up_down] __device__(edge_t idx) { - vertex_t src = d_final_src[idx]; - vertex_t new_cluster = d_final_nbr_cluster[idx]; - vertex_t old_cluster = d_next_cluster[src - base_vertex_id]; - weight_t src_weight = d_vertex_weights[src - base_src_vertex_id]; - - if (d_final_nbr_weights[idx] <= weight_t{0}) return false; - if (new_cluster == old_cluster) return false; - if ((new_cluster > old_cluster) != up_down) return false; - - d_next_cluster[src - base_vertex_id] = new_cluster; - d_cluster_increase[idx] = src_weight; - d_cluster_decrease[idx] = src_weight; - d_old_cluster[idx] = old_cluster; - return true; - }, - stream_); - - cluster_increase_v = variable_shuffle( - handle_, - local_cluster_edge_ids_v.size(), - thrust::make_permutation_iterator(cluster_increase_v.begin(), - local_cluster_edge_ids_v.begin()), - thrust::make_transform_iterator( - thrust::make_permutation_iterator(final_nbr_cluster_v.begin(), - local_cluster_edge_ids_v.begin()), - [d_vertex_device_view = compute_partition_.vertex_device_view()] __device__(vertex_t v) { - return d_vertex_device_view(v); - })); - - final_nbr_cluster_v = variable_shuffle( - handle_, - local_cluster_edge_ids_v.size(), - thrust::make_permutation_iterator(final_nbr_cluster_v.begin(), - local_cluster_edge_ids_v.begin()), - thrust::make_transform_iterator( - thrust::make_permutation_iterator(final_nbr_cluster_v.begin(), - local_cluster_edge_ids_v.begin()), - [d_vertex_device_view = compute_partition_.vertex_device_view()] __device__(vertex_t v) { - return d_vertex_device_view(v); - })); - - cluster_decrease_v = variable_shuffle( - handle_, - local_cluster_edge_ids_v.size(), - thrust::make_permutation_iterator(cluster_decrease_v.begin(), - local_cluster_edge_ids_v.begin()), - thrust::make_transform_iterator( - thrust::make_permutation_iterator(old_cluster_v.begin(), local_cluster_edge_ids_v.begin()), - [d_vertex_device_view = compute_partition_.vertex_device_view()] __device__(vertex_t v) { - return d_vertex_device_view(v); - })); - - old_cluster_v = variable_shuffle( - handle_, - local_cluster_edge_ids_v.size(), - thrust::make_permutation_iterator(old_cluster_v.begin(), local_cluster_edge_ids_v.begin()), - thrust::make_transform_iterator( - thrust::make_permutation_iterator(old_cluster_v.begin(), local_cluster_edge_ids_v.begin()), - [d_vertex_device_view = compute_partition_.vertex_device_view()] __device__(vertex_t v) { - return d_vertex_device_view(v); - })); - - thrust::for_each(rmm::exec_policy(stream_)->on(stream_), - thrust::make_zip_iterator( - thrust::make_tuple(final_nbr_cluster_v.begin(), cluster_increase_v.begin())), - thrust::make_zip_iterator( - thrust::make_tuple(final_nbr_cluster_v.end(), cluster_increase_v.end())), - [d_cluster_weights = cluster_weights_v_.data().get(), - base_vertex_id = base_vertex_id_] __device__(auto p) { - vertex_t cluster_id = thrust::get<0>(p); - weight_t weight = thrust::get<1>(p); - - atomicAdd(d_cluster_weights + cluster_id - base_vertex_id, weight); - }); - - thrust::for_each( - rmm::exec_policy(stream_)->on(stream_), - thrust::make_zip_iterator( - thrust::make_tuple(old_cluster_v.begin(), cluster_decrease_v.begin())), - thrust::make_zip_iterator(thrust::make_tuple(old_cluster_v.end(), cluster_decrease_v.end())), - [d_cluster_weights = cluster_weights_v_.data().get(), - base_vertex_id = base_vertex_id_] __device__(auto p) { - vertex_t cluster_id = thrust::get<0>(p); - weight_t weight = thrust::get<1>(p); - - atomicAdd(d_cluster_weights + cluster_id - base_vertex_id, -weight); - }); - - cache_vertex_properties( - cluster_weights_v_, src_cluster_weights_cache_v_, dst_cluster_weights_cache_v_); - } - - template - std::pair, rmm::device_vector> - combine_local_src_nbr_cluster_weights(hash_t hasher, - compare_t compare, - skip_edge_t skip_edge, - weight_t const *d_weights, - count_t num_weights) - { - rmm::device_vector relevant_edges_v; - rmm::device_vector relevant_edge_weights_v; - - if (num_weights > 0) { - std::size_t capacity{static_cast(num_weights / 0.7)}; - - cuco::static_map hash_map( - capacity, std::numeric_limits::max(), count_t{0}); - detail::create_cuco_pair_t create_cuco_pair; - - CUDA_TRY(cudaStreamSynchronize(stream_)); - - hash_map.insert(thrust::make_transform_iterator(thrust::make_counting_iterator(0), - create_cuco_pair), - thrust::make_transform_iterator( - thrust::make_counting_iterator(num_weights), create_cuco_pair), - hasher, - compare); - - CUDA_TRY(cudaStreamSynchronize(stream_)); - - relevant_edges_v.resize(num_weights); - - relevant_edges_v = detail::remove_elements_from_vector( - relevant_edges_v, - thrust::make_counting_iterator(0), - thrust::make_counting_iterator(num_weights), - [d_hash_map = hash_map.get_device_view(), hasher, compare] __device__(count_t idx) { - auto pos = d_hash_map.find(idx, hasher, compare); - return (pos->first == idx); - }, - stream_); - - thrust::for_each_n( - rmm::exec_policy(stream_)->on(stream_), - thrust::make_counting_iterator(0), - relevant_edges_v.size(), - [d_hash_map = hash_map.get_device_view(), - hasher, - compare, - d_relevant_edges = relevant_edges_v.data().get()] __device__(count_t idx) mutable { - count_t edge_id = d_relevant_edges[idx]; - auto pos = d_hash_map.find(edge_id, hasher, compare); - pos->second.store(idx); - }); - - relevant_edge_weights_v.resize(relevant_edges_v.size()); - thrust::fill(rmm::exec_policy(stream_)->on(stream_), - relevant_edge_weights_v.begin(), - relevant_edge_weights_v.end(), - weight_t{0}); - - thrust::for_each_n( - rmm::exec_policy(stream_)->on(stream_), - thrust::make_counting_iterator(0), - num_weights, - [d_hash_map = hash_map.get_device_view(), - hasher, - compare, - skip_edge, - d_relevant_edge_weights = relevant_edge_weights_v.data().get(), - d_weights] __device__(count_t idx) { - if (!skip_edge(idx)) { - auto pos = d_hash_map.find(idx, hasher, compare); - if (pos != d_hash_map.end()) { - atomicAdd(d_relevant_edge_weights + pos->second.load(cuda::std::memory_order_relaxed), - d_weights[idx]); - } - } - }); - } - - return std::make_pair(relevant_edges_v, relevant_edge_weights_v); + thrust::make_tuple(weight_t{0}, weight_t{0}), + cugraph::experimental::get_dataframe_buffer_begin>( + output_buffer)); + + thrust::transform( + rmm::exec_policy(handle_.get_stream_view()), + cugraph::experimental::get_dataframe_buffer_begin>( + output_buffer), + cugraph::experimental::get_dataframe_buffer_begin>( + output_buffer) + + current_graph_view_.get_number_of_local_vertices(), + old_cluster_sum_v.begin(), + [] __device__(auto p) { return thrust::get<1>(p); }); + + thrust::transform( + rmm::exec_policy(handle_.get_stream_view()), + cugraph::experimental::get_dataframe_buffer_begin>( + output_buffer), + cugraph::experimental::get_dataframe_buffer_begin>( + output_buffer) + + current_graph_view_.get_number_of_local_vertices(), + cluster_subtract_v.begin(), + [] __device__(auto p) { return thrust::get<0>(p); }); } - void shrink_graph(vertex_t *d_cluster_vec) - { - timer_start("shrinking graph"); - - std::size_t capacity{static_cast((local_num_rows_ + local_num_cols_) / 0.7)}; - - cuco::static_map hash_map( - capacity, std::numeric_limits::max(), std::numeric_limits::max()); - - // renumber the clusters to the range 0..(num_clusters-1) - vertex_t num_clusters = renumber_clusters(hash_map); - - renumber_result(hash_map, d_cluster_vec, num_clusters); - - // shrink our graph to represent the graph of supervertices - generate_supervertices_graph(hash_map, num_clusters); - - // assign each new vertex to its own cluster - // MNMG: This can be done locally with no communication required - thrust::sequence(rmm::exec_policy(stream_)->on(stream_), - cluster_v_.begin(), - cluster_v_.end(), - base_vertex_id_); - - timer_stop(stream_); - } - - vertex_t renumber_clusters(cuco::static_map &hash_map) + void update_by_delta_modularity(weight_t total_edge_weight, + weight_t resolution, + rmm::device_uvector& next_cluster_v, + bool up_down) { - rmm::device_vector cluster_inverse_v(local_num_vertices_, vertex_t{0}); - - // - // FIXME: Faster to iterate from graph_.get_vertex_partition_first() - // to graph_.get_vertex_partition_last()? That would potentially - // result in adding a cluster that isn't used on this GPU, - // although I don't think it would break the result in any way. - // - // This would also eliminate this use of src_indices_v_. - // - auto it_src = thrust::make_transform_iterator( - src_indices_v_.begin(), - [base_src_vertex_id = base_src_vertex_id_, - d_src_cluster_cache = src_cluster_cache_v_.data().get()] __device__(auto idx) { - return detail::create_cuco_pair_t()( - d_src_cluster_cache[idx - base_src_vertex_id]); - }); + rmm::device_uvector old_cluster_sum_v( + current_graph_view_.get_number_of_local_vertices(), handle_.get_stream()); + rmm::device_uvector cluster_subtract_v( + current_graph_view_.get_number_of_local_vertices(), handle_.get_stream()); + rmm::device_uvector src_cluster_weights_v(next_cluster_v.size(), + handle_.get_stream()); - auto it_dst = thrust::make_transform_iterator( - current_graph_view_.indices(), - [base_dst_vertex_id = base_dst_vertex_id_, - d_dst_cluster_cache = dst_cluster_cache_v_.data().get()] __device__(auto idx) { - return detail::create_cuco_pair_t()( - d_dst_cluster_cache[idx - base_dst_vertex_id]); - }); + compute_cluster_sum_and_subtract(old_cluster_sum_v, cluster_subtract_v); - hash_map.insert(it_src, it_src + local_num_edges_); - hash_map.insert(it_dst, it_dst + local_num_edges_); + auto output_buffer = + cugraph::experimental::allocate_dataframe_buffer>( + current_graph_view_.get_number_of_local_vertices(), handle_.get_stream()); - // Now I need to get the keys into an array and shuffle them - rmm::device_vector used_cluster_ids_v(hash_map.get_size()); + vertex_t* map_key_first; + vertex_t* map_key_last; + weight_t* map_value_first; - auto transform_iter = thrust::make_transform_iterator( - thrust::make_counting_iterator(0), - [d_hash_map = hash_map.get_device_view()] __device__(std::size_t idx) { - return d_hash_map.begin_slot()[idx].first.load(); - }); - - used_cluster_ids_v = detail::remove_elements_from_vector( - used_cluster_ids_v, - transform_iter, - transform_iter + hash_map.get_capacity(), - [vmax = std::numeric_limits::max()] __device__(vertex_t cluster) { - return cluster != vmax; - }, - stream_); - - auto partition_cluster_ids_iter = thrust::make_transform_iterator( - used_cluster_ids_v.begin(), - [d_vertex_device_view = compute_partition_.vertex_device_view()] __device__(vertex_t v) { - return d_vertex_device_view(v); - }); - - rmm::device_vector original_gpus_v; - rmm::device_vector my_cluster_ids_v = - variable_shuffle( - handle_, used_cluster_ids_v.size(), used_cluster_ids_v.begin(), partition_cluster_ids_iter); + if (graph_t::is_multi_gpu) { + cugraph::experimental::detail::compute_gpu_id_from_vertex_t vertex_to_gpu_id_op{ + handle_.get_comms().get_size()}; + + src_cluster_weights_v = cugraph::experimental::collect_values_for_keys( + handle_.get_comms(), + cluster_keys_v_.begin(), + cluster_keys_v_.end(), + cluster_weights_v_.data(), + d_src_cluster_cache_, + d_src_cluster_cache_ + src_cluster_cache_v_.size(), + vertex_to_gpu_id_op, + handle_.get_stream()); + + map_key_first = cluster_keys_v_.begin(); + map_key_last = cluster_keys_v_.end(); + map_value_first = cluster_weights_v_.begin(); + } else { + thrust::sort_by_key(rmm::exec_policy(handle_.get_stream_view()), + cluster_keys_v_.begin(), + cluster_keys_v_.end(), + cluster_weights_v_.begin()); + + thrust::transform(rmm::exec_policy(handle_.get_stream_view()), + next_cluster_v.begin(), + next_cluster_v.end(), + src_cluster_weights_v.begin(), + [d_cluster_weights = cluster_weights_v_.data(), + d_cluster_keys = cluster_keys_v_.data(), + num_clusters = cluster_keys_v_.size()] __device__(vertex_t cluster) { + auto pos = thrust::lower_bound( + thrust::seq, d_cluster_keys, d_cluster_keys + num_clusters, cluster); + return d_cluster_weights[pos - d_cluster_keys]; + }); - if (graph_view_t::is_multi_gpu) { - original_gpus_v = variable_shuffle( - handle_, - used_cluster_ids_v.size(), - thrust::make_constant_iterator(rank_), - partition_cluster_ids_iter); + map_key_first = d_src_cluster_cache_; + map_key_last = d_src_cluster_cache_ + src_cluster_weights_v.size(); + map_value_first = src_cluster_weights_v.begin(); } - // - // Now my_cluster_ids contains the cluster ids that this gpu is - // responsible for. I'm going to set cluster_inverse_v to one - // for each cluster in this list. - // - thrust::for_each( - rmm::exec_policy(stream_)->on(stream_), - my_cluster_ids_v.begin(), - my_cluster_ids_v.end(), - [base_vertex_id = base_vertex_id_, - d_cluster_inverse = cluster_inverse_v.data().get()] __device__(vertex_t cluster) { - d_cluster_inverse[cluster - base_vertex_id] = 1; - }); + rmm::device_uvector src_old_cluster_sum_v( + current_graph_view_.get_number_of_local_adj_matrix_partition_rows(), handle_.get_stream()); + rmm::device_uvector src_cluster_subtract_v( + current_graph_view_.get_number_of_local_adj_matrix_partition_rows(), handle_.get_stream()); + copy_to_adj_matrix_row( + handle_, current_graph_view_, old_cluster_sum_v.begin(), src_old_cluster_sum_v.begin()); + copy_to_adj_matrix_row( + handle_, current_graph_view_, cluster_subtract_v.begin(), src_cluster_subtract_v.begin()); - rmm::device_vector my_cluster_ids_deduped_v = detail::remove_elements_from_vector( - my_cluster_ids_v, - thrust::make_counting_iterator(0), - thrust::make_counting_iterator(cluster_inverse_v.size()), - [d_cluster_inverse = cluster_inverse_v.data().get()] __device__(auto idx) { - return d_cluster_inverse[idx] == 1; + copy_v_transform_reduce_key_aggregated_out_nbr( + handle_, + current_graph_view_, + thrust::make_zip_iterator(thrust::make_tuple(src_old_cluster_sum_v.begin(), + d_src_vertex_weights_cache_, + src_cluster_subtract_v.begin(), + d_src_cluster_cache_, + src_cluster_weights_v.begin())), + + d_dst_cluster_cache_, + map_key_first, + map_key_last, + map_value_first, + [total_edge_weight, resolution] __device__( + auto src, auto neighbor_cluster, auto new_cluster_sum, auto src_info, auto a_new) { + auto old_cluster_sum = thrust::get<0>(src_info); + auto k_k = thrust::get<1>(src_info); + auto cluster_subtract = thrust::get<2>(src_info); + auto src_cluster = thrust::get<3>(src_info); + auto a_old = thrust::get<4>(src_info); + + if (src_cluster == neighbor_cluster) new_cluster_sum -= cluster_subtract; + + weight_t delta_modularity = 2 * (((new_cluster_sum - old_cluster_sum) / total_edge_weight) - + resolution * (a_new * k_k - a_old * k_k + k_k * k_k) / + (total_edge_weight * total_edge_weight)); + + return thrust::make_tuple(neighbor_cluster, delta_modularity); }, - stream_); - - // - // Need to gather everything to be able to compute base addresses - // - vertex_t base_address{0}; + [] __device__(auto p1, auto p2) { + auto id1 = thrust::get<0>(p1); + auto id2 = thrust::get<0>(p2); + auto wt1 = thrust::get<1>(p1); + auto wt2 = thrust::get<1>(p2); - if (graph_view_t::is_multi_gpu) { - int num_gpus{1}; - rmm::device_vector sizes_v(num_gpus + 1, my_cluster_ids_deduped_v.size()); - - handle_.get_comms().allgather( - sizes_v.data().get() + num_gpus, sizes_v.data().get(), num_gpus, stream_); - - base_address = thrust::reduce(rmm::exec_policy(stream_)->on(stream_), - sizes_v.begin(), - sizes_v.begin() + rank_, - vertex_t{0}); - } - - // - // Now let's update cluster_inverse_v to contain - // the mapping of old cluster id to new vertex id - // - thrust::fill( - cluster_inverse_v.begin(), cluster_inverse_v.end(), std::numeric_limits::max()); - - thrust::for_each_n(rmm::exec_policy(stream_)->on(stream_), - thrust::make_counting_iterator(0), - my_cluster_ids_deduped_v.size(), - [base_address, - d_my_cluster_ids_deduped = my_cluster_ids_deduped_v.data().get(), - d_cluster_inverse = cluster_inverse_v.data().get()] __device__(auto idx) { - d_cluster_inverse[d_my_cluster_ids_deduped[idx]] = idx + base_address; - }); - - // - // Now I need to shuffle back to original gpus the - // subset of my mapping that is required - // - rmm::device_vector new_vertex_ids_v = - variable_shuffle( - handle_, - my_cluster_ids_v.size(), - thrust::make_transform_iterator(my_cluster_ids_v.begin(), - [d_cluster_inverse = cluster_inverse_v.data().get(), - base_vertex_id = base_vertex_id_] __device__(auto v) { - return d_cluster_inverse[v - base_vertex_id]; - }), - original_gpus_v.begin()); - - if (graph_view_t::is_multi_gpu) { - my_cluster_ids_v = variable_shuffle( - handle_, my_cluster_ids_v.size(), my_cluster_ids_v.begin(), original_gpus_v.begin()); - } - - // - // Now update the hash map with the new vertex id - // - thrust::for_each_n(rmm::exec_policy(stream_)->on(stream_), - thrust::make_zip_iterator( - thrust::make_tuple(my_cluster_ids_v.begin(), new_vertex_ids_v.begin())), - my_cluster_ids_v.size(), - [d_hash_map = hash_map.get_device_view()] __device__(auto p) mutable { - auto pos = d_hash_map.find(thrust::get<0>(p)); - pos->second.store(thrust::get<1>(p)); - }); - - // - // At this point we have a renumbered COO that is - // improperly distributed around the cluster, which - // will be fixed by generate_supervertices_graph - // - if (graph_t::is_multi_gpu) { - return host_scalar_allreduce( - handle_.get_comms(), static_cast(my_cluster_ids_deduped_v.size()), stream_); - } else { - return static_cast(my_cluster_ids_deduped_v.size()); - } - } - - void renumber_result(cuco::static_map const &hash_map, - vertex_t *d_cluster_vec, - vertex_t num_clusters) - { - if (graph_view_t::is_multi_gpu) { - // - // FIXME: Perhaps there's a general purpose function hidden here... - // Given a set of vertex_t values, and a distributed set of - // vertex properties, go to the proper node and retrieve - // the vertex properties and return them to this gpu. - // - std::size_t capacity{static_cast((local_num_vertices_) / 0.7)}; - cuco::static_map result_hash_map( - capacity, std::numeric_limits::max(), std::numeric_limits::max()); - - auto cluster_iter = thrust::make_transform_iterator(d_cluster_vec, [] __device__(vertex_t c) { - return detail::create_cuco_pair_t()(c); + return (wt1 < wt2) ? p2 : ((wt1 > wt2) ? p1 : ((id1 < id2) ? p1 : p2)); + }, + thrust::make_tuple(vertex_t{-1}, weight_t{0}), + cugraph::experimental::get_dataframe_buffer_begin>( + output_buffer)); + + thrust::transform( + rmm::exec_policy(handle_.get_stream_view()), + next_cluster_v.begin(), + next_cluster_v.end(), + cugraph::experimental::get_dataframe_buffer_begin>( + output_buffer), + next_cluster_v.begin(), + [up_down] __device__(vertex_t old_cluster, auto p) { + vertex_t new_cluster = thrust::get<0>(p); + weight_t delta_modularity = thrust::get<1>(p); + + return (delta_modularity > weight_t{0}) + ? (((new_cluster > old_cluster) != up_down) ? old_cluster : new_cluster) + : old_cluster; }); - result_hash_map.insert(cluster_iter, cluster_iter + local_num_vertices_); - - rmm::device_vector used_cluster_ids_v(result_hash_map.get_size()); - - auto transform_iter = thrust::make_transform_iterator( - thrust::make_counting_iterator(0), - [d_result_hash_map = result_hash_map.get_device_view()] __device__(std::size_t idx) { - return d_result_hash_map.begin_slot()[idx].first.load(); - }); - - used_cluster_ids_v = detail::remove_elements_from_vector( - used_cluster_ids_v, - transform_iter, - transform_iter + result_hash_map.get_capacity(), - [vmax = std::numeric_limits::max()] __device__(vertex_t cluster) { - return cluster != vmax; - }, - stream_); - - auto partition_cluster_ids_iter = thrust::make_transform_iterator( - used_cluster_ids_v.begin(), - [d_vertex_device_view = compute_partition_.vertex_device_view()] __device__(vertex_t v) { - return d_vertex_device_view(v); - }); - - rmm::device_vector old_cluster_ids_v = - variable_shuffle(handle_, - used_cluster_ids_v.size(), - used_cluster_ids_v.begin(), - partition_cluster_ids_iter); - - rmm::device_vector original_gpus_v = - variable_shuffle( - handle_, - used_cluster_ids_v.size(), - thrust::make_constant_iterator(rank_), - partition_cluster_ids_iter); - - // Now each GPU has old cluster ids, let's compute new cluster ids - rmm::device_vector new_cluster_ids_v(old_cluster_ids_v.size()); - - thrust::transform(rmm::exec_policy(stream_)->on(stream_), - old_cluster_ids_v.begin(), - old_cluster_ids_v.end(), - new_cluster_ids_v.begin(), - [base_vertex_id = base_vertex_id_, - d_cluster = cluster_v_.data().get(), - d_hash_map = hash_map.get_device_view()] __device__(vertex_t cluster_id) { - vertex_t c = d_cluster[cluster_id - base_vertex_id]; - auto pos = d_hash_map.find(c); - return pos->second.load(); - }); - - // Shuffle everything back - old_cluster_ids_v = variable_shuffle( - handle_, old_cluster_ids_v.size(), old_cluster_ids_v.begin(), original_gpus_v.begin()); - new_cluster_ids_v = variable_shuffle( - handle_, new_cluster_ids_v.size(), new_cluster_ids_v.begin(), original_gpus_v.begin()); - - // Update result_hash_map - thrust::for_each_n( - rmm::exec_policy(stream_)->on(stream_), - thrust::make_zip_iterator( - thrust::make_tuple(old_cluster_ids_v.begin(), new_cluster_ids_v.begin())), - old_cluster_ids_v.size(), - [d_result_hash_map = result_hash_map.get_device_view()] __device__(auto pair) mutable { - auto pos = d_result_hash_map.find(thrust::get<0>(pair)); - pos->second.store(thrust::get<1>(pair)); - }); - - thrust::transform( - rmm::exec_policy(stream_)->on(stream_), - d_cluster_vec, - d_cluster_vec + number_of_vertices_, - d_cluster_vec, - [d_result_hash_map = result_hash_map.get_device_view()] __device__(vertex_t c) { - auto pos = d_result_hash_map.find(c); - return pos->second.load(); - }); + d_src_cluster_cache_ = cache_src_vertex_properties(next_cluster_v, src_cluster_cache_v_); + d_dst_cluster_cache_ = cache_dst_vertex_properties(next_cluster_v, dst_cluster_cache_v_); - } else { - thrust::transform(rmm::exec_policy(stream_)->on(stream_), - d_cluster_vec, - d_cluster_vec + number_of_vertices_, - d_cluster_vec, - [d_hash_map = hash_map.get_device_view(), - d_dst_cluster = dst_cluster_cache_v_.data()] __device__(vertex_t v) { - vertex_t c = d_dst_cluster[v]; - auto pos = d_hash_map.find(c); - return pos->second.load(); - }); - } + std::tie(cluster_keys_v_, cluster_weights_v_) = + cugraph::experimental::transform_reduce_by_adj_matrix_row_key_e( + handle_, + current_graph_view_, + thrust::make_constant_iterator(0), + thrust::make_constant_iterator(0), + d_src_cluster_cache_, + [] __device__(auto src, auto dst, auto wt, auto x, auto y) { return wt; }, + weight_t{0}); } - void generate_supervertices_graph(cuco::static_map const &hash_map, - vertex_t num_clusters) + void shrink_graph() { - rmm::device_vector new_src_v(local_num_edges_); - rmm::device_vector new_dst_v(local_num_edges_); - rmm::device_vector new_weight_v(current_graph_view_.weights(), - current_graph_view_.weights() + local_num_edges_); - - thrust::transform(rmm::exec_policy(stream_)->on(stream_), - src_indices_v_.begin(), - src_indices_v_.end(), - new_src_v.begin(), - [base_src_vertex_id = base_src_vertex_id_, - d_src_cluster = src_cluster_cache_v_.data().get(), - d_hash_map = hash_map.get_device_view()] __device__(vertex_t v) { - vertex_t c = d_src_cluster[v - base_src_vertex_id]; - auto pos = d_hash_map.find(c); - return pos->second.load(); - }); - - thrust::transform(rmm::exec_policy(stream_)->on(stream_), - current_graph_view_.indices(), - current_graph_view_.indices() + local_num_edges_, - new_dst_v.begin(), - [base_dst_vertex_id = base_dst_vertex_id_, - d_dst_cluster = dst_cluster_cache_v_.data().get(), - d_hash_map = hash_map.get_device_view()] __device__(vertex_t v) { - vertex_t c = d_dst_cluster[v - base_dst_vertex_id]; - auto pos = d_hash_map.find(c); - return pos->second.load(); - }); - - // Combine common edges on local gpu - std::tie(new_src_v, new_dst_v, new_weight_v) = - combine_local_edges(new_src_v, new_dst_v, new_weight_v); - - if (graph_view_t::is_multi_gpu) { - // - // Shuffle the data to the proper GPU - // FIXME: This needs some performance exploration. It is - // possible (likely?) that the shrunken graph is - // more dense than the original graph. Perhaps that - // changes the dynamic of partitioning efficiently. - // - // For now, we're going to keep the partitioning the same, - // but because we've renumbered to lower numbers, fewer - // partitions will actually have data. - // - rmm::device_vector partition_v(new_src_v.size()); - - thrust::transform( - rmm::exec_policy(stream_)->on(stream_), - thrust::make_zip_iterator(thrust::make_tuple(new_src_v.begin(), new_dst_v.begin())), - thrust::make_zip_iterator(thrust::make_tuple(new_src_v.end(), new_dst_v.end())), - partition_v.begin(), - [d_edge_device_view = compute_partition_.edge_device_view()] __device__( - thrust::tuple tuple) { - return d_edge_device_view(thrust::get<0>(tuple), thrust::get<1>(tuple)); - }); - - new_src_v = variable_shuffle( - handle_, partition_v.size(), new_src_v.begin(), partition_v.begin()); - - new_dst_v = variable_shuffle( - handle_, partition_v.size(), new_dst_v.begin(), partition_v.begin()); + timer_start("shrinking graph"); - new_weight_v = variable_shuffle( - handle_, partition_v.size(), new_weight_v.begin(), partition_v.begin()); + rmm::device_uvector numbering_map(0, handle_.get_stream()); - // - // Now everything is on the correct node, again combine like edges - // - std::tie(new_src_v, new_dst_v, new_weight_v) = - combine_local_edges(new_src_v, new_dst_v, new_weight_v); - } - - // - // Now I have a COO of the new graph, distributed according to the - // original clustering (eventually this likely fits on one GPU and - // everything else is empty). - // - current_graph_ = - detail::create_graph(handle_, - new_src_v, - new_dst_v, - new_weight_v, - num_clusters, - experimental::graph_properties_t{true, true}, - current_graph_view_); + std::tie(current_graph_, numbering_map) = + coarsen_graph(handle_, current_graph_view_, dendrogram_->current_level_begin()); current_graph_view_ = current_graph_->view(); - src_indices_v_.resize(new_src_v.size()); - - local_num_vertices_ = current_graph_view_.get_number_of_local_vertices(); - local_num_rows_ = current_graph_view_.get_number_of_local_adj_matrix_partition_rows(); - local_num_cols_ = current_graph_view_.get_number_of_local_adj_matrix_partition_cols(); - local_num_edges_ = new_src_v.size(); + rmm::device_uvector numbering_indices(numbering_map.size(), handle_.get_stream()); + thrust::sequence(rmm::exec_policy(handle_.get_stream_view()), + numbering_indices.begin(), + numbering_indices.end(), + current_graph_view_.get_local_vertex_first()); - cugraph::detail::offsets_to_indices( - current_graph_view_.offsets(), local_num_rows_, src_indices_v_.data().get()); - } + relabel( + handle_, + std::make_tuple(static_cast(numbering_map.begin()), + static_cast(numbering_indices.begin())), + current_graph_view_.get_number_of_local_vertices(), + dendrogram_->current_level_begin(), + dendrogram_->current_level_size(), + false); - std:: - tuple, rmm::device_vector, rmm::device_vector> - combine_local_edges(rmm::device_vector &src_v, - rmm::device_vector &dst_v, - rmm::device_vector &weight_v) - { - thrust::stable_sort_by_key( - rmm::exec_policy(stream_)->on(stream_), - dst_v.begin(), - dst_v.end(), - thrust::make_zip_iterator(thrust::make_tuple(src_v.begin(), weight_v.begin()))); - thrust::stable_sort_by_key( - rmm::exec_policy(stream_)->on(stream_), - src_v.begin(), - src_v.end(), - thrust::make_zip_iterator(thrust::make_tuple(dst_v.begin(), weight_v.begin()))); - - rmm::device_vector combined_src_v(src_v.size()); - rmm::device_vector combined_dst_v(src_v.size()); - rmm::device_vector combined_weight_v(src_v.size()); - - // - // Now we reduce by key to combine the weights of duplicate - // edges. - // - auto start = thrust::make_zip_iterator(thrust::make_tuple(src_v.begin(), dst_v.begin())); - auto new_start = - thrust::make_zip_iterator(thrust::make_tuple(combined_src_v.begin(), combined_dst_v.begin())); - auto new_end = thrust::reduce_by_key(rmm::exec_policy(stream_)->on(stream_), - start, - start + src_v.size(), - weight_v.begin(), - new_start, - combined_weight_v.begin(), - thrust::equal_to>(), - thrust::plus()); - - auto num_edges = thrust::distance(new_start, new_end.first); - - combined_src_v.resize(num_edges); - combined_dst_v.resize(num_edges); - combined_weight_v.resize(num_edges); - - return std::make_tuple(combined_src_v, combined_dst_v, combined_weight_v); + timer_stop(handle_.get_stream()); } protected: - raft::handle_t const &handle_; - cudaStream_t stream_; - - vertex_t number_of_vertices_; - vertex_t base_vertex_id_{0}; - vertex_t base_src_vertex_id_{0}; - vertex_t base_dst_vertex_id_{0}; - int rank_{0}; + raft::handle_t const& handle_; - vertex_t local_num_vertices_; - vertex_t local_num_rows_; - vertex_t local_num_cols_; - edge_t local_num_edges_; + std::unique_ptr> dendrogram_; // - // Copy of graph + // Initially we run on the input graph view, + // but as we shrink the graph we'll keep the + // current graph here // std::unique_ptr current_graph_{}; graph_view_t current_graph_view_; - // - // For partitioning - // - detail::compute_partition_t compute_partition_; + rmm::device_uvector vertex_weights_v_; + rmm::device_uvector src_vertex_weights_cache_v_; + rmm::device_uvector src_cluster_cache_v_; + rmm::device_uvector dst_cluster_cache_v_; + rmm::device_uvector cluster_keys_v_; + rmm::device_uvector cluster_weights_v_; - rmm::device_vector src_indices_v_; - - // - // Weights and clustering across iterations of algorithm - // - rmm::device_vector vertex_weights_v_; - rmm::device_vector src_vertex_weights_cache_v_{}; - rmm::device_vector dst_vertex_weights_cache_v_{}; - - rmm::device_vector cluster_weights_v_; - rmm::device_vector src_cluster_weights_cache_v_{}; - rmm::device_vector dst_cluster_weights_cache_v_{}; - - rmm::device_vector cluster_v_; - rmm::device_vector src_cluster_cache_v_{}; - rmm::device_vector dst_cluster_cache_v_{}; - - rmm::device_vector empty_cache_weight_v_{}; + weight_t* d_src_vertex_weights_cache_; + vertex_t* d_src_cluster_cache_; + vertex_t* d_dst_cluster_cache_; #ifdef TIMING HighResTimer hr_timer_; #endif -}; // namespace experimental +}; } // namespace experimental } // namespace cugraph diff --git a/cpp/src/experimental/pagerank.cu b/cpp/src/experimental/pagerank.cu index 1aa7f37fa6b..7c3e4b03e9e 100644 --- a/cpp/src/experimental/pagerank.cu +++ b/cpp/src/experimental/pagerank.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -14,17 +14,15 @@ * limitations under the License. */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include @@ -42,18 +40,19 @@ namespace detail { // FIXME: personalization_vector_size is confusing in OPG (local or aggregate?) template -void pagerank(raft::handle_t const& handle, - GraphViewType const& pull_graph_view, - typename GraphViewType::weight_type* precomputed_vertex_out_weight_sums, - typename GraphViewType::vertex_type* personalization_vertices, - result_t* personalization_values, - typename GraphViewType::vertex_type personalization_vector_size, - result_t* pageranks, - result_t alpha, - result_t epsilon, - size_t max_iterations, - bool has_initial_guess, - bool do_expensive_check) +void pagerank( + raft::handle_t const& handle, + GraphViewType const& pull_graph_view, + std::optional precomputed_vertex_out_weight_sums, + std::optional personalization_vertices, + std::optional personalization_values, + std::optional personalization_vector_size, + result_t* pageranks, + result_t alpha, + result_t epsilon, + size_t max_iterations, + bool has_initial_guess, + bool do_expensive_check) { using vertex_t = typename GraphViewType::vertex_type; using weight_t = typename GraphViewType::weight_type; @@ -69,27 +68,28 @@ void pagerank(raft::handle_t const& handle, if (num_vertices == 0) { return; } auto aggregate_personalization_vector_size = - GraphViewType::is_multi_gpu - ? host_scalar_allreduce(handle.get_comms(), personalization_vector_size, handle.get_stream()) - : personalization_vector_size; + personalization_vertices + ? GraphViewType::is_multi_gpu + ? host_scalar_allreduce( + handle.get_comms(), *personalization_vector_size, handle.get_stream()) + : *personalization_vector_size + : vertex_t{0}; // 1. check input arguments - CUGRAPH_EXPECTS( - ((personalization_vector_size > 0) && (personalization_vertices != nullptr) && - (personalization_values != nullptr)) || - ((personalization_vector_size == 0) && (personalization_vertices == nullptr) && - (personalization_values == nullptr)), - "Invalid input argument: if personalization_vector_size is non-zero, personalization verties " - "and personalization values should be provided. Otherwise, they should not be provided."); + CUGRAPH_EXPECTS((personalization_vertices.has_value() == false) || + (personalization_values.has_value() && personalization_vector_size.has_value()), + "Invalid input argument: if personalization_vertices.has_value() is true, " + "personalization_values.has_value() and personalization_vector_size.has_value() " + "should be true as well."); CUGRAPH_EXPECTS((alpha >= 0.0) && (alpha <= 1.0), "Invalid input argument: alpha should be in [0.0, 1.0]."); CUGRAPH_EXPECTS(epsilon >= 0.0, "Invalid input argument: epsilon should be non-negative."); if (do_expensive_check) { - if (precomputed_vertex_out_weight_sums != nullptr) { + if (precomputed_vertex_out_weight_sums) { auto num_negative_precomputed_vertex_out_weight_sums = count_if_v( - handle, pull_graph_view, precomputed_vertex_out_weight_sums, [] __device__(auto val) { + handle, pull_graph_view, *precomputed_vertex_out_weight_sums, [] __device__(auto val) { return val < result_t{0.0}; }); CUGRAPH_EXPECTS( @@ -118,12 +118,13 @@ void pagerank(raft::handle_t const& handle, } if (aggregate_personalization_vector_size > 0) { - vertex_partition_device_t vertex_partition(pull_graph_view); + auto vertex_partition = vertex_partition_device_view_t( + pull_graph_view.get_vertex_partition_view()); auto num_invalid_vertices = count_if_v(handle, pull_graph_view, - personalization_vertices, - personalization_vertices + personalization_vector_size, + *personalization_vertices, + *personalization_vertices + *personalization_vector_size, [vertex_partition] __device__(auto val) { return !(vertex_partition.is_valid_vertex(val) && vertex_partition.is_local_vertex_nocheck(val)); @@ -132,8 +133,8 @@ void pagerank(raft::handle_t const& handle, "Invalid input argument: peresonalization vertices have invalid vertex IDs."); auto num_negative_values = count_if_v(handle, pull_graph_view, - personalization_values, - personalization_values + personalization_vector_size, + *personalization_values, + *personalization_values + *personalization_vector_size, [] __device__(auto val) { return val < 0.0; }); CUGRAPH_EXPECTS(num_negative_values == 0, "Invalid input argument: peresonalization values should be non-negative."); @@ -142,34 +143,21 @@ void pagerank(raft::handle_t const& handle, // 2. compute the sums of the out-going edge weights (if not provided) - rmm::device_uvector tmp_vertex_out_weight_sums(0, handle.get_stream()); - if (precomputed_vertex_out_weight_sums == nullptr) { - tmp_vertex_out_weight_sums.resize(pull_graph_view.get_number_of_local_vertices(), - handle.get_stream()); - // FIXME: better refactor this out (computing out-degree). - copy_v_transform_reduce_out_nbr( - handle, - pull_graph_view, - thrust::make_constant_iterator(0) /* dummy */, - thrust::make_constant_iterator(0) /* dummy */, - [alpha] __device__(vertex_t src, vertex_t dst, weight_t w, auto src_val, auto dst_val) { - return w; - }, - weight_t{0.0}, - tmp_vertex_out_weight_sums.data()); - } - - auto vertex_out_weight_sums = precomputed_vertex_out_weight_sums != nullptr - ? precomputed_vertex_out_weight_sums - : tmp_vertex_out_weight_sums.data(); + auto tmp_vertex_out_weight_sums = precomputed_vertex_out_weight_sums + ? std::nullopt + : std::optional>{ + pull_graph_view.compute_out_weight_sums(handle)}; + auto vertex_out_weight_sums = precomputed_vertex_out_weight_sums + ? *precomputed_vertex_out_weight_sums + : (*tmp_vertex_out_weight_sums).data(); // 3. initialize pagerank values if (has_initial_guess) { auto sum = reduce_v(handle, pull_graph_view, pageranks, result_t{0.0}); - CUGRAPH_EXPECTS( - sum > 0.0, - "Invalid input argument: sum of the PageRank initial guess values should be positive."); + CUGRAPH_EXPECTS(sum > 0.0, + "Invalid input argument: sum of the PageRank initial " + "guess values should be positive."); thrust::transform(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), pageranks, pageranks + pull_graph_view.get_number_of_local_vertices(), @@ -188,11 +176,12 @@ void pagerank(raft::handle_t const& handle, if (aggregate_personalization_vector_size > 0) { personalization_sum = reduce_v(handle, pull_graph_view, - personalization_values, - personalization_values + personalization_vector_size, + *personalization_values, + *personalization_values + *personalization_vector_size, result_t{0.0}); CUGRAPH_EXPECTS(personalization_sum > 0.0, - "Invalid input argument: sum of personalization valuese should be positive."); + "Invalid input argument: sum of personalization valuese " + "should be positive."); } // 5. pagerank iteration @@ -254,13 +243,14 @@ void pagerank(raft::handle_t const& handle, pageranks); if (aggregate_personalization_vector_size > 0) { - vertex_partition_device_t vertex_partition(pull_graph_view); + auto vertex_partition = vertex_partition_device_view_t( + pull_graph_view.get_vertex_partition_view()); auto val_first = thrust::make_zip_iterator( - thrust::make_tuple(personalization_vertices, personalization_values)); + thrust::make_tuple(*personalization_vertices, *personalization_values)); thrust::for_each( rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), val_first, - val_first + personalization_vector_size, + val_first + *personalization_vector_size, [vertex_partition, pageranks, dangling_sum, personalization_sum, alpha] __device__( auto val) { auto v = thrust::get<0>(val); @@ -286,8 +276,6 @@ void pagerank(raft::handle_t const& handle, CUGRAPH_FAIL("PageRank failed to converge."); } } - - return; } } // namespace detail @@ -295,10 +283,10 @@ void pagerank(raft::handle_t const& handle, template void pagerank(raft::handle_t const& handle, graph_view_t const& graph_view, - weight_t* precomputed_vertex_out_weight_sums, - vertex_t* personalization_vertices, - result_t* personalization_values, - vertex_t personalization_vector_size, + std::optional precomputed_vertex_out_weight_sums, + std::optional personalization_vertices, + std::optional personalization_values, + std::optional personalization_vector_size, result_t* pageranks, result_t alpha, result_t epsilon, @@ -324,10 +312,10 @@ void pagerank(raft::handle_t const& handle, template void pagerank(raft::handle_t const& handle, graph_view_t const& graph_view, - float* precomputed_vertex_out_weight_sums, - int32_t* personalization_vertices, - float* personalization_values, - int32_t personalization_vector_size, + std::optional precomputed_vertex_out_weight_sums, + std::optional personalization_vertices, + std::optional personalization_values, + std::optional personalization_vector_size, float* pageranks, float alpha, float epsilon, @@ -337,10 +325,10 @@ template void pagerank(raft::handle_t const& handle, template void pagerank(raft::handle_t const& handle, graph_view_t const& graph_view, - double* precomputed_vertex_out_weight_sums, - int32_t* personalization_vertices, - double* personalization_values, - int32_t personalization_vector_size, + std::optional precomputed_vertex_out_weight_sums, + std::optional personalization_vertices, + std::optional personalization_values, + std::optional personalization_vector_size, double* pageranks, double alpha, double epsilon, @@ -350,10 +338,10 @@ template void pagerank(raft::handle_t const& handle, template void pagerank(raft::handle_t const& handle, graph_view_t const& graph_view, - float* precomputed_vertex_out_weight_sums, - int32_t* personalization_vertices, - float* personalization_values, - int32_t personalization_vector_size, + std::optional precomputed_vertex_out_weight_sums, + std::optional personalization_vertices, + std::optional personalization_values, + std::optional personalization_vector_size, float* pageranks, float alpha, float epsilon, @@ -363,10 +351,10 @@ template void pagerank(raft::handle_t const& handle, template void pagerank(raft::handle_t const& handle, graph_view_t const& graph_view, - double* precomputed_vertex_out_weight_sums, - int32_t* personalization_vertices, - double* personalization_values, - int32_t personalization_vector_size, + std::optional precomputed_vertex_out_weight_sums, + std::optional personalization_vertices, + std::optional personalization_values, + std::optional personalization_vector_size, double* pageranks, double alpha, double epsilon, @@ -376,10 +364,10 @@ template void pagerank(raft::handle_t const& handle, template void pagerank(raft::handle_t const& handle, graph_view_t const& graph_view, - float* precomputed_vertex_out_weight_sums, - int64_t* personalization_vertices, - float* personalization_values, - int64_t personalization_vector_size, + std::optional precomputed_vertex_out_weight_sums, + std::optional personalization_vertices, + std::optional personalization_values, + std::optional personalization_vector_size, float* pageranks, float alpha, float epsilon, @@ -389,10 +377,10 @@ template void pagerank(raft::handle_t const& handle, template void pagerank(raft::handle_t const& handle, graph_view_t const& graph_view, - double* precomputed_vertex_out_weight_sums, - int64_t* personalization_vertices, - double* personalization_values, - int64_t personalization_vector_size, + std::optional precomputed_vertex_out_weight_sums, + std::optional personalization_vertices, + std::optional personalization_values, + std::optional personalization_vector_size, double* pageranks, double alpha, double epsilon, @@ -402,10 +390,10 @@ template void pagerank(raft::handle_t const& handle, template void pagerank(raft::handle_t const& handle, graph_view_t const& graph_view, - float* precomputed_vertex_out_weight_sums, - int32_t* personalization_vertices, - float* personalization_values, - int32_t personalization_vector_size, + std::optional precomputed_vertex_out_weight_sums, + std::optional personalization_vertices, + std::optional personalization_values, + std::optional personalization_vector_size, float* pageranks, float alpha, float epsilon, @@ -415,10 +403,10 @@ template void pagerank(raft::handle_t const& handle, template void pagerank(raft::handle_t const& handle, graph_view_t const& graph_view, - double* precomputed_vertex_out_weight_sums, - int32_t* personalization_vertices, - double* personalization_values, - int32_t personalization_vector_size, + std::optional precomputed_vertex_out_weight_sums, + std::optional personalization_vertices, + std::optional personalization_values, + std::optional personalization_vector_size, double* pageranks, double alpha, double epsilon, @@ -428,10 +416,10 @@ template void pagerank(raft::handle_t const& handle, template void pagerank(raft::handle_t const& handle, graph_view_t const& graph_view, - float* precomputed_vertex_out_weight_sums, - int32_t* personalization_vertices, - float* personalization_values, - int32_t personalization_vector_size, + std::optional precomputed_vertex_out_weight_sums, + std::optional personalization_vertices, + std::optional personalization_values, + std::optional personalization_vector_size, float* pageranks, float alpha, float epsilon, @@ -441,10 +429,10 @@ template void pagerank(raft::handle_t const& handle, template void pagerank(raft::handle_t const& handle, graph_view_t const& graph_view, - double* precomputed_vertex_out_weight_sums, - int32_t* personalization_vertices, - double* personalization_values, - int32_t personalization_vector_size, + std::optional precomputed_vertex_out_weight_sums, + std::optional personalization_vertices, + std::optional personalization_values, + std::optional personalization_vector_size, double* pageranks, double alpha, double epsilon, @@ -454,10 +442,10 @@ template void pagerank(raft::handle_t const& handle, template void pagerank(raft::handle_t const& handle, graph_view_t const& graph_view, - float* precomputed_vertex_out_weight_sums, - int64_t* personalization_vertices, - float* personalization_values, - int64_t personalization_vector_size, + std::optional precomputed_vertex_out_weight_sums, + std::optional personalization_vertices, + std::optional personalization_values, + std::optional personalization_vector_size, float* pageranks, float alpha, float epsilon, @@ -467,10 +455,10 @@ template void pagerank(raft::handle_t const& handle, template void pagerank(raft::handle_t const& handle, graph_view_t const& graph_view, - double* precomputed_vertex_out_weight_sums, - int64_t* personalization_vertices, - double* personalization_values, - int64_t personalization_vector_size, + std::optional precomputed_vertex_out_weight_sums, + std::optional personalization_vertices, + std::optional personalization_values, + std::optional personalization_vector_size, double* pageranks, double alpha, double epsilon, diff --git a/cpp/src/experimental/relabel.cu b/cpp/src/experimental/relabel.cu new file mode 100644 index 00000000000..caefe0be806 --- /dev/null +++ b/cpp/src/experimental/relabel.cu @@ -0,0 +1,280 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace cugraph { +namespace experimental { + +// FIXME: think about requiring old_new_label_pairs to be pre-shuffled +template +void relabel(raft::handle_t const& handle, + std::tuple old_new_label_pairs, + vertex_t num_label_pairs, + vertex_t* labels /* [INOUT] */, + vertex_t num_labels, + bool skip_missing_labels, + bool do_expensive_check) +{ + double constexpr load_factor = 0.7; + + if (multi_gpu) { + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + + auto key_func = detail::compute_gpu_id_from_vertex_t{comm_size}; + + // find unique old labels (to be relabeled) + + rmm::device_uvector unique_old_labels(num_labels, handle.get_stream_view()); + thrust::copy(rmm::exec_policy(handle.get_stream_view()), + labels, + labels + num_labels, + unique_old_labels.data()); + thrust::sort(rmm::exec_policy(handle.get_stream_view()), + unique_old_labels.begin(), + unique_old_labels.end()); + unique_old_labels.resize( + thrust::distance(unique_old_labels.begin(), + thrust::unique(rmm::exec_policy(handle.get_stream_view()), + unique_old_labels.begin(), + unique_old_labels.end())), + handle.get_stream_view()); + unique_old_labels.shrink_to_fit(handle.get_stream_view()); + + // collect new labels for the unique old labels + + rmm::device_uvector new_labels_for_unique_old_labels(0, handle.get_stream_view()); + { + // shuffle the old_new_label_pairs based on applying the compute_gpu_id_from_vertex_t functor + // to the old labels + + rmm::device_uvector rx_label_pair_old_labels(0, handle.get_stream_view()); + rmm::device_uvector rx_label_pair_new_labels(0, handle.get_stream_view()); + { + rmm::device_uvector label_pair_old_labels(num_label_pairs, + handle.get_stream_view()); + rmm::device_uvector label_pair_new_labels(num_label_pairs, + handle.get_stream_view()); + thrust::copy(rmm::exec_policy(handle.get_stream_view()), + std::get<0>(old_new_label_pairs), + std::get<0>(old_new_label_pairs) + num_label_pairs, + label_pair_old_labels.begin()); + thrust::copy(rmm::exec_policy(handle.get_stream_view()), + std::get<1>(old_new_label_pairs), + std::get<1>(old_new_label_pairs) + num_label_pairs, + label_pair_new_labels.begin()); + auto pair_first = thrust::make_zip_iterator( + thrust::make_tuple(label_pair_old_labels.begin(), label_pair_new_labels.begin())); + std::forward_as_tuple(std::tie(rx_label_pair_old_labels, rx_label_pair_new_labels), + std::ignore) = + groupby_gpuid_and_shuffle_values( + handle.get_comms(), + pair_first, + pair_first + num_label_pairs, + [key_func] __device__(auto val) { return key_func(thrust::get<0>(val)); }, + handle.get_stream_view()); + } + + // update intermediate relabel map + + handle.get_stream_view().synchronize(); // cuco::static_map currently does not take stream + + auto poly_alloc = + rmm::mr::polymorphic_allocator(rmm::mr::get_current_device_resource()); + auto stream_adapter = + rmm::mr::make_stream_allocator_adaptor(poly_alloc, cudaStream_t{nullptr}); + cuco::static_map + relabel_map{// cuco::static_map requires at least one empty slot + std::max(static_cast( + static_cast(rx_label_pair_old_labels.size()) / load_factor), + rx_label_pair_old_labels.size() + 1), + invalid_vertex_id::value, + invalid_vertex_id::value, + stream_adapter}; + + auto pair_first = thrust::make_zip_iterator( + thrust::make_tuple(rx_label_pair_old_labels.begin(), rx_label_pair_new_labels.begin())); + relabel_map.insert(pair_first, pair_first + rx_label_pair_old_labels.size()); + + rx_label_pair_old_labels.resize(0, handle.get_stream_view()); + rx_label_pair_new_labels.resize(0, handle.get_stream_view()); + rx_label_pair_old_labels.shrink_to_fit(handle.get_stream_view()); + rx_label_pair_new_labels.shrink_to_fit(handle.get_stream_view()); + + // shuffle unique_old_labels, relabel using the intermediate relabel map, and shuffle back + + { + rmm::device_uvector rx_unique_old_labels(0, handle.get_stream_view()); + std::vector rx_value_counts{}; + std::tie(rx_unique_old_labels, rx_value_counts) = groupby_gpuid_and_shuffle_values( + handle.get_comms(), + unique_old_labels.begin(), + unique_old_labels.end(), + [key_func] __device__(auto val) { return key_func(val); }, + handle.get_stream_view()); + + handle.get_stream_view().synchronize(); // cuco::static_map currently does not take stream + + if (skip_missing_labels) { + thrust::transform(rmm::exec_policy(handle.get_stream_view()), + rx_unique_old_labels.begin(), + rx_unique_old_labels.end(), + rx_unique_old_labels.begin(), + [view = relabel_map.get_device_view()] __device__(auto old_label) { + auto found = view.find(old_label); + return found != view.end() ? view.find(old_label)->second.load( + cuda::std::memory_order_relaxed) + : old_label; + }); + } else { + relabel_map.find( + rx_unique_old_labels.begin(), + rx_unique_old_labels.end(), + rx_unique_old_labels.begin()); // now rx_unique_old_lables hold new labels for the + // corresponding old labels + } + + std::tie(new_labels_for_unique_old_labels, std::ignore) = + shuffle_values(handle.get_comms(), + rx_unique_old_labels.begin(), + rx_value_counts, + handle.get_stream_view()); + } + } + + handle.get_stream_view().synchronize(); // cuco::static_map currently does not take stream + + { + auto poly_alloc = + rmm::mr::polymorphic_allocator(rmm::mr::get_current_device_resource()); + auto stream_adapter = + rmm::mr::make_stream_allocator_adaptor(poly_alloc, cudaStream_t{nullptr}); + cuco::static_map + relabel_map{ + // cuco::static_map requires at least one empty slot + std::max(static_cast(static_cast(unique_old_labels.size()) / load_factor), + unique_old_labels.size() + 1), + invalid_vertex_id::value, + invalid_vertex_id::value, + stream_adapter}; + + auto pair_first = thrust::make_zip_iterator( + thrust::make_tuple(unique_old_labels.begin(), new_labels_for_unique_old_labels.begin())); + relabel_map.insert(pair_first, pair_first + unique_old_labels.size()); + relabel_map.find(labels, labels + num_labels, labels); + } + } else { + cuco::static_map relabel_map( + // cuco::static_map requires at least one empty slot + std::max(static_cast(static_cast(num_label_pairs) / load_factor), + static_cast(num_label_pairs) + 1), + invalid_vertex_id::value, + invalid_vertex_id::value); + + auto pair_first = thrust::make_zip_iterator( + thrust::make_tuple(std::get<0>(old_new_label_pairs), std::get<1>(old_new_label_pairs))); + relabel_map.insert(pair_first, pair_first + num_label_pairs); + if (skip_missing_labels) { + thrust::transform(rmm::exec_policy(handle.get_stream_view()), + labels, + labels + num_labels, + labels, + [view = relabel_map.get_device_view()] __device__(auto old_label) { + auto found = view.find(old_label); + return found != view.end() ? view.find(old_label)->second.load( + cuda::std::memory_order_relaxed) + : old_label; + }); + } else { + relabel_map.find(labels, labels + num_labels, labels); + } + } + + if (do_expensive_check && !skip_missing_labels) { + CUGRAPH_EXPECTS( + thrust::count(rmm::exec_policy(handle.get_stream_view()), + labels, + labels + num_labels, + invalid_vertex_id::value) == 0, + "Invalid input argument: labels include old label values missing in old_new_label_pairs."); + } + + return; +} + +// explicit instantiation + +template void relabel(raft::handle_t const& handle, + std::tuple old_new_label_pairs, + int32_t num_label_pairs, + int32_t* labels, + int32_t num_labels, + bool skip_missing_labels, + bool do_expensive_check); + +template void relabel( + raft::handle_t const& handle, + std::tuple old_new_label_pairs, + int32_t num_label_pairs, + int32_t* labels, + int32_t num_labels, + bool skip_missing_labels, + bool do_expensive_check); + +template void relabel(raft::handle_t const& handle, + std::tuple old_new_label_pairs, + int64_t num_label_pairs, + int64_t* labels, + int64_t num_labels, + bool skip_missing_labels, + bool do_expensive_check); + +template void relabel( + raft::handle_t const& handle, + std::tuple old_new_label_pairs, + int64_t num_label_pairs, + int64_t* labels, + int64_t num_labels, + bool skip_missing_labels, + bool do_expensive_check); + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/src/experimental/renumber_edgelist.cu b/cpp/src/experimental/renumber_edgelist.cu new file mode 100644 index 00000000000..6dd48c326ec --- /dev/null +++ b/cpp/src/experimental/renumber_edgelist.cu @@ -0,0 +1,940 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace cugraph { +namespace experimental { +namespace detail { + +template +std::tuple, std::vector> compute_renumber_map( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + std::vector const& edgelist_major_vertices, + std::vector const& edgelist_minor_vertices, + std::vector const& edgelist_edge_counts) +{ + // FIXME: compare this sort based approach with hash based approach in both speed and memory + // footprint + + // 1. acquire (unique major label, count) pairs + + if (multi_gpu) { + auto& comm = handle.get_comms(); + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (beginning of col_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + ; + comm.barrier(); // currently, this is ncclAllReduce +#endif + } + + rmm::device_uvector major_labels(0, handle.get_stream()); + rmm::device_uvector major_counts(0, handle.get_stream()); + for (size_t i = 0; i < edgelist_major_vertices.size(); ++i) { + rmm::device_uvector tmp_major_labels(0, handle.get_stream()); + rmm::device_uvector tmp_major_counts(0, handle.get_stream()); + { + rmm::device_uvector sorted_major_labels(edgelist_edge_counts[i], + handle.get_stream()); + thrust::copy(rmm::exec_policy(handle.get_stream_view()), + edgelist_major_vertices[i], + edgelist_major_vertices[i] + edgelist_edge_counts[i], + sorted_major_labels.begin()); + // FIXME: better refactor this sort-count_if-reduce_by_key routine for reuse + thrust::sort(rmm::exec_policy(handle.get_stream_view()), + sorted_major_labels.begin(), + sorted_major_labels.end()); + auto num_unique_labels = + thrust::count_if(rmm::exec_policy(handle.get_stream_view()), + thrust::make_counting_iterator(size_t{0}), + thrust::make_counting_iterator(sorted_major_labels.size()), + [labels = sorted_major_labels.data()] __device__(auto i) { + return (i == 0) || (labels[i - 1] != labels[i]); + }); + tmp_major_labels.resize(num_unique_labels, handle.get_stream()); + tmp_major_counts.resize(tmp_major_labels.size(), handle.get_stream()); + thrust::reduce_by_key(rmm::exec_policy(handle.get_stream_view()), + sorted_major_labels.begin(), + sorted_major_labels.end(), + thrust::make_constant_iterator(edge_t{1}), + tmp_major_labels.begin(), + tmp_major_counts.begin()); + } + + if (multi_gpu) { + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_rank = col_comm.get_rank(); + auto const col_comm_size = col_comm.get_size(); + + rmm::device_uvector rx_major_labels(0, handle.get_stream()); + rmm::device_uvector rx_major_counts(0, handle.get_stream()); + auto rx_sizes = host_scalar_gather( + col_comm, tmp_major_labels.size(), static_cast(i), handle.get_stream()); + std::vector rx_displs{}; + if (static_cast(i) == col_comm_rank) { + rx_displs.assign(col_comm_size, size_t{0}); + std::partial_sum(rx_sizes.begin(), rx_sizes.end() - 1, rx_displs.begin() + 1); + rx_major_labels.resize(rx_displs.back() + rx_sizes.back(), handle.get_stream()); + rx_major_counts.resize(rx_major_labels.size(), handle.get_stream()); + } + device_gatherv(col_comm, + thrust::make_zip_iterator( + thrust::make_tuple(tmp_major_labels.begin(), tmp_major_counts.begin())), + thrust::make_zip_iterator( + thrust::make_tuple(rx_major_labels.begin(), rx_major_counts.begin())), + tmp_major_labels.size(), + rx_sizes, + rx_displs, + static_cast(i), + handle.get_stream()); + if (static_cast(i) == col_comm_rank) { + major_labels = std::move(rx_major_labels); + major_counts = std::move(rx_major_counts); + } + } else { + assert(i == 0); + major_labels = std::move(tmp_major_labels); + major_counts = std::move(tmp_major_counts); + } + } + if (multi_gpu) { + // FIXME: better refactor this sort-count_if-reduce_by_key routine for reuse + thrust::sort_by_key(rmm::exec_policy(handle.get_stream_view()), + major_labels.begin(), + major_labels.end(), + major_counts.begin()); + auto num_unique_labels = thrust::count_if(rmm::exec_policy(handle.get_stream_view()), + thrust::make_counting_iterator(size_t{0}), + thrust::make_counting_iterator(major_labels.size()), + [labels = major_labels.data()] __device__(auto i) { + return (i == 0) || (labels[i - 1] != labels[i]); + }); + rmm::device_uvector tmp_major_labels(num_unique_labels, handle.get_stream()); + rmm::device_uvector tmp_major_counts(tmp_major_labels.size(), handle.get_stream()); + thrust::reduce_by_key(rmm::exec_policy(handle.get_stream_view()), + major_labels.begin(), + major_labels.end(), + major_counts.begin(), + tmp_major_labels.begin(), + tmp_major_counts.begin()); + major_labels = std::move(tmp_major_labels); + major_counts = std::move(tmp_major_counts); + } + + // 2. acquire unique minor labels + + std::vector minor_displs(edgelist_minor_vertices.size(), edge_t{0}); + std::partial_sum( + edgelist_edge_counts.begin(), edgelist_edge_counts.end() - 1, minor_displs.begin() + 1); + rmm::device_uvector minor_labels(minor_displs.back() + edgelist_edge_counts.back(), + handle.get_stream()); + for (size_t i = 0; i < edgelist_minor_vertices.size(); ++i) { + thrust::copy(rmm::exec_policy(handle.get_stream_view()), + edgelist_minor_vertices[i], + edgelist_minor_vertices[i] + edgelist_edge_counts[i], + minor_labels.begin() + minor_displs[i]); + } + thrust::sort( + rmm::exec_policy(handle.get_stream_view()), minor_labels.begin(), minor_labels.end()); + minor_labels.resize(thrust::distance(minor_labels.begin(), + thrust::unique(rmm::exec_policy(handle.get_stream_view()), + minor_labels.begin(), + minor_labels.end())), + handle.get_stream()); + if (multi_gpu) { + auto& comm = handle.get_comms(); + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_size = row_comm.get_size(); + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (beginning of row_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + + if (row_comm_size > 1) { + rmm::device_uvector rx_minor_labels(0, handle.get_stream()); + std::tie(rx_minor_labels, std::ignore) = groupby_gpuid_and_shuffle_values( + row_comm, + minor_labels.begin(), + minor_labels.end(), + [key_func = detail::compute_gpu_id_from_vertex_t{row_comm_size}] __device__( + auto val) { return key_func(val); }, + handle.get_stream()); + thrust::sort( + rmm::exec_policy(handle.get_stream_view()), rx_minor_labels.begin(), rx_minor_labels.end()); + rx_minor_labels.resize( + thrust::distance(rx_minor_labels.begin(), + thrust::unique(rmm::exec_policy(handle.get_stream_view()), + rx_minor_labels.begin(), + rx_minor_labels.end())), + handle.get_stream()); + minor_labels = std::move(rx_minor_labels); + } + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between + // two different communicators (end of row_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK + // and MPI barrier with MPI) + // + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + } + minor_labels.shrink_to_fit(handle.get_stream_view()); + + // 3. merge major and minor labels and vertex labels + + rmm::device_uvector merged_labels(major_labels.size() + minor_labels.size(), + handle.get_stream_view()); + rmm::device_uvector merged_counts(merged_labels.size(), handle.get_stream_view()); + thrust::merge_by_key(rmm::exec_policy(handle.get_stream_view()), + major_labels.begin(), + major_labels.end(), + minor_labels.begin(), + minor_labels.end(), + major_counts.begin(), + thrust::make_constant_iterator(edge_t{0}), + merged_labels.begin(), + merged_counts.begin()); + + major_labels.resize(0, handle.get_stream()); + major_counts.resize(0, handle.get_stream()); + minor_labels.resize(0, handle.get_stream()); + major_labels.shrink_to_fit(handle.get_stream()); + major_counts.shrink_to_fit(handle.get_stream()); + minor_labels.shrink_to_fit(handle.get_stream()); + + rmm::device_uvector labels(merged_labels.size(), handle.get_stream()); + rmm::device_uvector counts(labels.size(), handle.get_stream()); + auto pair_it = thrust::reduce_by_key(rmm::exec_policy(handle.get_stream_view()), + merged_labels.begin(), + merged_labels.end(), + merged_counts.begin(), + labels.begin(), + counts.begin()); + merged_labels.resize(0, handle.get_stream()); + merged_counts.resize(0, handle.get_stream()); + merged_labels.shrink_to_fit(handle.get_stream()); + merged_counts.shrink_to_fit(handle.get_stream()); + labels.resize(thrust::distance(labels.begin(), thrust::get<0>(pair_it)), handle.get_stream()); + counts.resize(labels.size(), handle.get_stream()); + labels.shrink_to_fit(handle.get_stream()); + counts.shrink_to_fit(handle.get_stream()); + + // 4. if vertices != nullptr, add isolated vertices + + rmm::device_uvector isolated_vertices(0, handle.get_stream()); + if (optional_vertex_span) { + auto [vertices, num_vertices] = *optional_vertex_span; + auto num_isolated_vertices = thrust::count_if( + rmm::exec_policy(handle.get_stream_view()), + vertices, + vertices + num_vertices, + [label_first = labels.begin(), label_last = labels.end()] __device__(auto v) { + return !thrust::binary_search(thrust::seq, label_first, label_last, v); + }); + isolated_vertices.resize(num_isolated_vertices, handle.get_stream()); + thrust::copy_if(rmm::exec_policy(handle.get_stream_view()), + vertices, + vertices + num_vertices, + isolated_vertices.begin(), + [label_first = labels.begin(), label_last = labels.end()] __device__(auto v) { + return !thrust::binary_search(thrust::seq, label_first, label_last, v); + }); + } + + if (isolated_vertices.size() > 0) { + labels.resize(labels.size() + isolated_vertices.size(), handle.get_stream()); + counts.resize(labels.size(), handle.get_stream()); + thrust::copy(rmm::exec_policy(handle.get_stream_view()), + isolated_vertices.begin(), + isolated_vertices.end(), + labels.end() - isolated_vertices.size()); + thrust::fill(rmm::exec_policy(handle.get_stream_view()), + counts.end() - isolated_vertices.size(), + counts.end(), + edge_t{0}); + } + + // 6. sort by degree + + thrust::sort_by_key(rmm::exec_policy(handle.get_stream_view()), + counts.begin(), + counts.end(), + labels.begin(), + thrust::greater()); + + // 7. compute segment_offsets + + static_assert(detail::num_sparse_segments_per_vertex_partition == 3); + static_assert((detail::low_degree_threshold <= detail::mid_degree_threshold) && + (detail::mid_degree_threshold <= std::numeric_limits::max())); + size_t mid_degree_threshold{detail::mid_degree_threshold}; + size_t low_degree_threshold{detail::low_degree_threshold}; + size_t hypersparse_degree_threshold{0}; + if (multi_gpu) { + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_size = col_comm.get_size(); + mid_degree_threshold *= col_comm_size; + low_degree_threshold *= col_comm_size; + hypersparse_degree_threshold = + static_cast(col_comm_size * detail::hypersparse_threshold_ratio); + } + auto num_segments_per_vertex_partition = + detail::num_sparse_segments_per_vertex_partition + + (hypersparse_degree_threshold > 0 ? size_t{1} : size_t{0}); + rmm::device_uvector d_thresholds(num_segments_per_vertex_partition - 1, + handle.get_stream()); + auto h_thresholds = hypersparse_degree_threshold > 0 + ? std::vector{static_cast(mid_degree_threshold), + static_cast(low_degree_threshold), + static_cast(hypersparse_degree_threshold)} + : std::vector{static_cast(mid_degree_threshold), + static_cast(low_degree_threshold)}; + raft::update_device( + d_thresholds.data(), h_thresholds.data(), h_thresholds.size(), handle.get_stream()); + + rmm::device_uvector d_segment_offsets(num_segments_per_vertex_partition + 1, + handle.get_stream()); + + auto zero_vertex = vertex_t{0}; + auto vertex_count = static_cast(counts.size()); + d_segment_offsets.set_element_async(0, zero_vertex, handle.get_stream()); + d_segment_offsets.set_element_async( + num_segments_per_vertex_partition, vertex_count, handle.get_stream()); + + thrust::upper_bound(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + counts.begin(), + counts.end(), + d_thresholds.begin(), + d_thresholds.end(), + d_segment_offsets.begin() + 1, + thrust::greater{}); + + std::vector h_segment_offsets(d_segment_offsets.size()); + raft::update_host(h_segment_offsets.data(), + d_segment_offsets.data(), + d_segment_offsets.size(), + handle.get_stream()); + handle.get_stream_view().synchronize(); + + return std::make_tuple(std::move(labels), h_segment_offsets); +} + +template +void expensive_check_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + std::vector const& edgelist_major_vertices, + std::vector const& edgelist_minor_vertices, + std::vector const& edgelist_edge_counts) +{ + rmm::device_uvector sorted_local_vertices(size_t{0}, handle.get_stream()); + if (optional_vertex_span) { + auto [vertices, num_vertices] = *optional_vertex_span; + sorted_local_vertices.resize(num_vertices, handle.get_stream()); + thrust::copy(rmm::exec_policy(handle.get_stream_view()), + vertices, + vertices + num_vertices, + sorted_local_vertices.begin()); + thrust::sort(rmm::exec_policy(handle.get_stream_view()), + sorted_local_vertices.begin(), + sorted_local_vertices.end()); + CUGRAPH_EXPECTS(static_cast(thrust::distance( + sorted_local_vertices.begin(), + thrust::unique(rmm::exec_policy(handle.get_stream_view()), + sorted_local_vertices.begin(), + sorted_local_vertices.end()))) == sorted_local_vertices.size(), + "Invalid input argument: local_vertices should not have duplicates."); + } + + if (multi_gpu) { + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + auto const comm_rank = comm.get_rank(); + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_size = row_comm.get_size(); + auto const row_comm_rank = row_comm.get_rank(); + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_size = col_comm.get_size(); + auto const col_comm_rank = col_comm.get_rank(); + + CUGRAPH_EXPECTS((edgelist_major_vertices.size() == edgelist_minor_vertices.size()) && + (edgelist_major_vertices.size() == static_cast(col_comm_size)), + "Invalid input argument: both edgelist_major_vertices.size() & " + "edgelist_minor_vertices.size() should coincide with col_comm_size."); + + auto [local_vertices, num_local_vertices] = *optional_vertex_span; + CUGRAPH_EXPECTS( + thrust::count_if( + rmm::exec_policy(handle.get_stream_view()), + local_vertices, + local_vertices + num_local_vertices, + [comm_rank, + key_func = + detail::compute_gpu_id_from_vertex_t{comm_size}] __device__(auto val) { + return key_func(val) != comm_rank; + }) == 0, + "Invalid input argument: local_vertices should be pre-shuffled."); + + for (size_t i = 0; i < edgelist_major_vertices.size(); ++i) { + auto edge_first = thrust::make_zip_iterator( + thrust::make_tuple(edgelist_major_vertices[i], edgelist_minor_vertices[i])); + CUGRAPH_EXPECTS( + thrust::count_if( + rmm::exec_policy(handle.get_stream_view()), + edge_first, + edge_first + edgelist_edge_counts[i], + [comm_size, + comm_rank, + row_comm_rank, + col_comm_size, + col_comm_rank, + i, + gpu_id_key_func = + detail::compute_gpu_id_from_edge_t{comm_size, row_comm_size, col_comm_size}, + partition_id_key_func = + detail::compute_partition_id_from_edge_t{ + comm_size, row_comm_size, col_comm_size}] __device__(auto edge) { + return (gpu_id_key_func(thrust::get<0>(edge), thrust::get<1>(edge)) != comm_rank) || + (partition_id_key_func(thrust::get<0>(edge), thrust::get<1>(edge)) != + row_comm_rank * col_comm_size + col_comm_rank + i * comm_size); + }) == 0, + "Invalid input argument: edgelist_major_vertices & edgelist_minor_vertices should be " + "pre-shuffled."); + + if (optional_vertex_span) { + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + + // FIXME: this barrier is unnecessary if the above host_scalar_allreduce is a true host + // operation (as it serves as a barrier) barrier is necessary here to avoid potential + // overlap (which can leads to deadlock) between two different communicators (beginning of + // col_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with + // DASK and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + + rmm::device_uvector sorted_major_vertices(0, handle.get_stream()); + { + auto recvcounts = + host_scalar_allgather(col_comm, sorted_local_vertices.size(), handle.get_stream()); + std::vector displacements(recvcounts.size(), size_t{0}); + std::partial_sum(recvcounts.begin(), recvcounts.end() - 1, displacements.begin() + 1); + sorted_major_vertices.resize(displacements.back() + recvcounts.back(), + handle.get_stream()); + device_allgatherv(col_comm, + sorted_local_vertices.data(), + sorted_major_vertices.data(), + recvcounts, + displacements, + handle.get_stream()); + thrust::sort(rmm::exec_policy(handle.get_stream_view()), + sorted_major_vertices.begin(), + sorted_major_vertices.end()); + } + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) + // between two different communicators (beginning of row_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with + // DASK and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + + rmm::device_uvector sorted_minor_vertices(0, handle.get_stream()); + { + auto recvcounts = + host_scalar_allgather(row_comm, sorted_local_vertices.size(), handle.get_stream()); + std::vector displacements(recvcounts.size(), size_t{0}); + std::partial_sum(recvcounts.begin(), recvcounts.end() - 1, displacements.begin() + 1); + sorted_minor_vertices.resize(displacements.back() + recvcounts.back(), + handle.get_stream()); + device_allgatherv(row_comm, + sorted_local_vertices.data(), + sorted_minor_vertices.data(), + recvcounts, + displacements, + handle.get_stream()); + thrust::sort(rmm::exec_policy(handle.get_stream_view()), + sorted_minor_vertices.begin(), + sorted_minor_vertices.end()); + } + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) + // between two different communicators (end of row_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with + // DASK and MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + + auto edge_first = thrust::make_zip_iterator( + thrust::make_tuple(edgelist_major_vertices[i], edgelist_minor_vertices[i])); + CUGRAPH_EXPECTS( + thrust::count_if( + rmm::exec_policy(handle.get_stream_view()), + edge_first, + edge_first + edgelist_edge_counts[i], + [num_major_vertices = static_cast(sorted_major_vertices.size()), + sorted_major_vertices = sorted_major_vertices.data(), + num_minor_vertices = static_cast(sorted_minor_vertices.size()), + sorted_minor_vertices = sorted_minor_vertices.data()] __device__(auto e) { + return !thrust::binary_search(thrust::seq, + sorted_major_vertices, + sorted_major_vertices + num_major_vertices, + thrust::get<0>(e)) || + !thrust::binary_search(thrust::seq, + sorted_minor_vertices, + sorted_minor_vertices + num_minor_vertices, + thrust::get<1>(e)); + }) == 0, + "Invalid input argument: edgelist_major_vertices and/or edgelist_mior_vertices have " + "invalid vertex ID(s)."); + } + } + } else { + assert(edgelist_major_vertices.size() == 1); + assert(edgelist_minor_vertices.size() == 1); + + if (optional_vertex_span) { + auto edge_first = thrust::make_zip_iterator( + thrust::make_tuple(edgelist_major_vertices[0], edgelist_minor_vertices[0])); + CUGRAPH_EXPECTS( + thrust::count_if( + rmm::exec_policy(handle.get_stream_view()), + edge_first, + edge_first + edgelist_edge_counts[0], + [sorted_local_vertices = sorted_local_vertices.data(), + num_sorted_local_vertices = + static_cast(sorted_local_vertices.size())] __device__(auto e) { + return !thrust::binary_search(thrust::seq, + sorted_local_vertices, + sorted_local_vertices + num_sorted_local_vertices, + thrust::get<0>(e)) || + !thrust::binary_search(thrust::seq, + sorted_local_vertices, + sorted_local_vertices + num_sorted_local_vertices, + thrust::get<1>(e)); + }) == 0, + "Invalid input argument: edgelist_major_vertices and/or edgelist_minor_vertices have " + "invalid vertex ID(s)."); + } + } +} + +template +std::enable_if_t, + partition_t, + vertex_t, + edge_t, + std::vector>> +renumber_edgelist(raft::handle_t const& handle, + std::optional> optional_local_vertex_span, + std::vector const& edgelist_major_vertices /* [INOUT] */, + std::vector const& edgelist_minor_vertices /* [INOUT] */, + std::vector const& edgelist_edge_counts, + bool do_expensive_check) +{ + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + auto const comm_rank = comm.get_rank(); + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_size = row_comm.get_size(); + auto const row_comm_rank = row_comm.get_rank(); + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_size = col_comm.get_size(); + auto const col_comm_rank = col_comm.get_rank(); + + std::vector edgelist_const_major_vertices(edgelist_major_vertices.size()); + std::vector edgelist_const_minor_vertices(edgelist_const_major_vertices.size()); + for (size_t i = 0; i < edgelist_const_major_vertices.size(); ++i) { + edgelist_const_major_vertices[i] = edgelist_major_vertices[i]; + edgelist_const_minor_vertices[i] = edgelist_minor_vertices[i]; + } + + if (do_expensive_check) { + expensive_check_edgelist(handle, + optional_local_vertex_span, + edgelist_const_major_vertices, + edgelist_const_minor_vertices, + edgelist_edge_counts); + } + + // 1. compute renumber map + + auto [renumber_map_labels, segment_offsets] = + detail::compute_renumber_map(handle, + optional_local_vertex_span, + edgelist_const_major_vertices, + edgelist_const_minor_vertices, + edgelist_edge_counts); + // 2. initialize partition_t object, number_of_vertices, and number_of_edges for the coarsened + // graph + + auto vertex_counts = host_scalar_allgather( + comm, static_cast(renumber_map_labels.size()), handle.get_stream()); + std::vector vertex_partition_offsets(comm_size + 1, 0); + std::partial_sum( + vertex_counts.begin(), vertex_counts.end(), vertex_partition_offsets.begin() + 1); + + partition_t partition( + vertex_partition_offsets, row_comm_size, col_comm_size, row_comm_rank, col_comm_rank); + + auto number_of_vertices = vertex_partition_offsets.back(); + auto number_of_edges = host_scalar_allreduce( + comm, + std::accumulate(edgelist_edge_counts.begin(), edgelist_edge_counts.end(), edge_t{0}), + handle.get_stream()); + + // 3. renumber edges + + double constexpr load_factor = 0.7; + + // FIXME: compare this hash based approach with a binary search based approach in both memory + // footprint and execution time + + // FIXME: this barrier is unnecessary if the above host_scalar_allgather is a true host operation + // (as it serves as a barrier) barrier is necessary here to avoid potential overlap (which can + // leads to deadlock) between two different communicators (beginning of col_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK and + // MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + + for (size_t i = 0; i < edgelist_major_vertices.size(); ++i) { + rmm::device_uvector renumber_map_major_labels( + col_comm_rank == static_cast(i) ? vertex_t{0} + : partition.get_matrix_partition_major_size(i), + handle.get_stream()); + device_bcast(col_comm, + renumber_map_labels.data(), + renumber_map_major_labels.data(), + partition.get_matrix_partition_major_size(i), + i, + handle.get_stream()); + + CUDA_TRY(cudaStreamSynchronize( + handle.get_stream())); // cuco::static_map currently does not take stream + + auto poly_alloc = rmm::mr::polymorphic_allocator(rmm::mr::get_current_device_resource()); + auto stream_adapter = rmm::mr::make_stream_allocator_adaptor(poly_alloc, cudaStream_t{nullptr}); + cuco::static_map + renumber_map{ + // cuco::static_map requires at least one empty slot + std::max(static_cast( + static_cast(partition.get_matrix_partition_major_size(i)) / load_factor), + static_cast(partition.get_matrix_partition_major_size(i)) + 1), + invalid_vertex_id::value, + invalid_vertex_id::value, + stream_adapter}; + auto pair_first = thrust::make_zip_iterator(thrust::make_tuple( + col_comm_rank == static_cast(i) ? renumber_map_labels.begin() + : renumber_map_major_labels.begin(), + thrust::make_counting_iterator(partition.get_matrix_partition_major_first(i)))); + renumber_map.insert(pair_first, pair_first + partition.get_matrix_partition_major_size(i)); + renumber_map.find(edgelist_major_vertices[i], + edgelist_major_vertices[i] + edgelist_edge_counts[i], + edgelist_major_vertices[i]); + } + + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between two + // different communicators (beginning of row_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK and + // MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + { + rmm::device_uvector renumber_map_minor_labels( + partition.get_matrix_partition_minor_size(), handle.get_stream()); + std::vector recvcounts(row_comm_size); + for (int i = 0; i < row_comm_size; ++i) { + recvcounts[i] = partition.get_vertex_partition_size(col_comm_rank * row_comm_size + i); + } + std::vector displacements(recvcounts.size(), 0); + std::partial_sum(recvcounts.begin(), recvcounts.end() - 1, displacements.begin() + 1); + device_allgatherv(row_comm, + renumber_map_labels.begin(), + renumber_map_minor_labels.begin(), + recvcounts, + displacements, + handle.get_stream()); + + CUDA_TRY(cudaStreamSynchronize( + handle.get_stream())); // cuco::static_map currently does not take stream + + auto poly_alloc = rmm::mr::polymorphic_allocator(rmm::mr::get_current_device_resource()); + auto stream_adapter = rmm::mr::make_stream_allocator_adaptor(poly_alloc, cudaStream_t{nullptr}); + cuco::static_map + renumber_map{// cuco::static_map requires at least one empty slot + std::max(static_cast( + static_cast(renumber_map_minor_labels.size()) / load_factor), + renumber_map_minor_labels.size() + 1), + invalid_vertex_id::value, + invalid_vertex_id::value, + stream_adapter}; + auto pair_first = thrust::make_zip_iterator(thrust::make_tuple( + renumber_map_minor_labels.begin(), + thrust::make_counting_iterator(partition.get_matrix_partition_minor_first()))); + renumber_map.insert(pair_first, pair_first + renumber_map_minor_labels.size()); + for (size_t i = 0; i < edgelist_major_vertices.size(); ++i) { + renumber_map.find(edgelist_minor_vertices[i], + edgelist_minor_vertices[i] + edgelist_edge_counts[i], + edgelist_minor_vertices[i]); + } + } + // barrier is necessary here to avoid potential overlap (which can leads to deadlock) between two + // different communicators (end of row_comm) +#if 1 + // FIXME: temporary hack till UCC is integrated into RAFT (so we can use UCC barrier with DASK and + // MPI barrier with MPI) + host_barrier(comm, handle.get_stream_view()); +#else + handle.get_stream_view().synchronize(); + comm.barrier(); // currently, this is ncclAllReduce +#endif + + return std::make_tuple(std::move(renumber_map_labels), + partition, + number_of_vertices, + number_of_edges, + segment_offsets); +} + +template +std::enable_if_t, std::vector>> +renumber_edgelist(raft::handle_t const& handle, + std::optional> optional_vertex_span, + vertex_t* edgelist_major_vertices /* [INOUT] */, + vertex_t* edgelist_minor_vertices /* [INOUT] */, + edge_t num_edgelist_edges, + bool do_expensive_check) +{ + if (do_expensive_check) { + expensive_check_edgelist( + handle, + optional_vertex_span, + std::vector{edgelist_major_vertices}, + std::vector{edgelist_minor_vertices}, + std::vector{num_edgelist_edges}); + } + + auto [renumber_map_labels, segment_offsets] = + detail::compute_renumber_map( + handle, + optional_vertex_span, + std::vector{edgelist_major_vertices}, + std::vector{edgelist_minor_vertices}, + std::vector{num_edgelist_edges}); + + double constexpr load_factor = 0.7; + + // FIXME: compare this hash based approach with a binary search based approach in both memory + // footprint and execution time + + auto poly_alloc = rmm::mr::polymorphic_allocator(rmm::mr::get_current_device_resource()); + auto stream_adapter = rmm::mr::make_stream_allocator_adaptor(poly_alloc, cudaStream_t{nullptr}); + cuco::static_map + renumber_map{ + // cuco::static_map requires at least one empty slot + std::max(static_cast(static_cast(renumber_map_labels.size()) / load_factor), + renumber_map_labels.size() + 1), + invalid_vertex_id::value, + invalid_vertex_id::value, + stream_adapter}; + auto pair_first = thrust::make_zip_iterator( + thrust::make_tuple(renumber_map_labels.begin(), thrust::make_counting_iterator(vertex_t{0}))); + renumber_map.insert(pair_first, pair_first + renumber_map_labels.size()); + renumber_map.find( + edgelist_major_vertices, edgelist_major_vertices + num_edgelist_edges, edgelist_major_vertices); + renumber_map.find( + edgelist_minor_vertices, edgelist_minor_vertices + num_edgelist_edges, edgelist_minor_vertices); + + return std::make_tuple(std::move(renumber_map_labels), segment_offsets); +} + +} // namespace detail + +template +std::enable_if_t, + partition_t, + vertex_t, + edge_t, + std::vector>> +renumber_edgelist(raft::handle_t const& handle, + std::optional> optional_local_vertex_span, + std::vector const& edgelist_major_vertices /* [INOUT] */, + std::vector const& edgelist_minor_vertices /* [INOUT] */, + std::vector const& edgelist_edge_counts, + bool do_expensive_check) +{ + return detail::renumber_edgelist(handle, + optional_local_vertex_span, + edgelist_major_vertices, + edgelist_minor_vertices, + edgelist_edge_counts, + do_expensive_check); +} + +template +std::enable_if_t, std::vector>> +renumber_edgelist(raft::handle_t const& handle, + std::optional> optional_vertex_span, + vertex_t* edgelist_major_vertices /* [INOUT] */, + vertex_t* edgelist_minor_vertices /* [INOUT] */, + edge_t num_edgelist_edges, + bool do_expensive_check) +{ + return detail::renumber_edgelist(handle, + optional_vertex_span, + edgelist_major_vertices, + edgelist_minor_vertices, + num_edgelist_edges, + do_expensive_check); +} + +// explicit instantiation directives (EIDir's): +// + +// instantiations for +// +template std:: + tuple, partition_t, int32_t, int32_t, std::vector> + renumber_edgelist( + raft::handle_t const& handle, + std::optional> optional_local_vertex_span, + std::vector const& edgelist_major_vertices /* [INOUT] */, + std::vector const& edgelist_minor_vertices /* [INOUT] */, + std::vector const& edgelist_edge_counts, + bool do_expensive_check); + +template std::tuple, std::vector> +renumber_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + int32_t* edgelist_major_vertices /* [INOUT] */, + int32_t* edgelist_minor_vertices /* [INOUT] */, + int32_t num_edgelist_edges, + bool do_expensive_check); + +// instantiations for +// +template std:: + tuple, partition_t, int32_t, int64_t, std::vector> + renumber_edgelist( + raft::handle_t const& handle, + std::optional> optional_local_vertex_span, + std::vector const& edgelist_major_vertices /* [INOUT] */, + std::vector const& edgelist_minor_vertices /* [INOUT] */, + std::vector const& edgelist_edge_counts, + bool do_expensive_check); + +template std::tuple, std::vector> +renumber_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + int32_t* edgelist_major_vertices /* [INOUT] */, + int32_t* edgelist_minor_vertices /* [INOUT] */, + int64_t num_edgelist_edges, + bool do_expensive_check); + +// instantiations for +// +template std:: + tuple, partition_t, int64_t, int64_t, std::vector> + renumber_edgelist( + raft::handle_t const& handle, + std::optional> optional_local_vertex_span, + std::vector const& edgelist_major_vertices /* [INOUT] */, + std::vector const& edgelist_minor_vertices /* [INOUT] */, + std::vector const& edgelist_edge_counts, + bool do_expensive_check); + +template std::tuple, std::vector> +renumber_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + int64_t* edgelist_major_vertices /* [INOUT] */, + int64_t* edgelist_minor_vertices /* [INOUT] */, + int64_t num_edgelist_edges, + bool do_expensive_check); + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/src/experimental/renumber_utils.cu b/cpp/src/experimental/renumber_utils.cu new file mode 100644 index 00000000000..dc2d44a139a --- /dev/null +++ b/cpp/src/experimental/renumber_utils.cu @@ -0,0 +1,413 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace cugraph { +namespace experimental { + +template +void renumber_ext_vertices(raft::handle_t const& handle, + vertex_t* vertices /* [INOUT] */, + size_t num_vertices, + vertex_t const* renumber_map_labels, + vertex_t local_int_vertex_first, + vertex_t local_int_vertex_last, + bool do_expensive_check) +{ + double constexpr load_factor = 0.7; + + if (do_expensive_check) { + rmm::device_uvector labels(local_int_vertex_last - local_int_vertex_first, + handle.get_stream_view()); + thrust::copy(rmm::exec_policy(handle.get_stream_view()), + renumber_map_labels, + renumber_map_labels + labels.size(), + labels.begin()); + thrust::sort(rmm::exec_policy(handle.get_stream_view()), labels.begin(), labels.end()); + CUGRAPH_EXPECTS( + thrust::unique(rmm::exec_policy(handle.get_stream_view()), labels.begin(), labels.end()) == + labels.end(), + "Invalid input arguments: renumber_map_labels have duplicate elements."); + } + + auto poly_alloc = rmm::mr::polymorphic_allocator(rmm::mr::get_current_device_resource()); + auto stream_adapter = rmm::mr::make_stream_allocator_adaptor(poly_alloc, cudaStream_t{nullptr}); + auto renumber_map_ptr = std::make_unique< + cuco::static_map>( + size_t{0}, + invalid_vertex_id::value, + invalid_vertex_id::value, + stream_adapter); + if (multi_gpu) { + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + + rmm::device_uvector sorted_unique_ext_vertices(num_vertices, + handle.get_stream_view()); + sorted_unique_ext_vertices.resize( + thrust::distance( + sorted_unique_ext_vertices.begin(), + thrust::copy_if(rmm::exec_policy(handle.get_stream_view()), + vertices, + vertices + num_vertices, + sorted_unique_ext_vertices.begin(), + [] __device__(auto v) { return v != invalid_vertex_id::value; })), + handle.get_stream_view()); + thrust::sort(rmm::exec_policy(handle.get_stream_view()), + sorted_unique_ext_vertices.begin(), + sorted_unique_ext_vertices.end()); + sorted_unique_ext_vertices.resize( + thrust::distance(sorted_unique_ext_vertices.begin(), + thrust::unique(rmm::exec_policy(handle.get_stream_view()), + sorted_unique_ext_vertices.begin(), + sorted_unique_ext_vertices.end())), + handle.get_stream_view()); + + auto int_vertices_for_sorted_unique_ext_vertices = collect_values_for_unique_keys( + comm, + renumber_map_labels, + renumber_map_labels + (local_int_vertex_last - local_int_vertex_first), + thrust::make_counting_iterator(local_int_vertex_first), + sorted_unique_ext_vertices.begin(), + sorted_unique_ext_vertices.end(), + detail::compute_gpu_id_from_vertex_t{comm_size}, + handle.get_stream_view()); + + handle.get_stream_view().synchronize(); // cuco::static_map currently does not take stream + + renumber_map_ptr.reset(); + + renumber_map_ptr = std::make_unique< + cuco::static_map>( + // cuco::static_map requires at least one empty slot + std::max( + static_cast(static_cast(sorted_unique_ext_vertices.size()) / load_factor), + sorted_unique_ext_vertices.size() + 1), + invalid_vertex_id::value, + invalid_vertex_id::value, + stream_adapter); + + auto kv_pair_first = thrust::make_zip_iterator(thrust::make_tuple( + sorted_unique_ext_vertices.begin(), int_vertices_for_sorted_unique_ext_vertices.begin())); + renumber_map_ptr->insert(kv_pair_first, kv_pair_first + sorted_unique_ext_vertices.size()); + } else { + handle.get_stream_view().synchronize(); // cuco::static_map currently does not take stream + + renumber_map_ptr.reset(); + + renumber_map_ptr = std::make_unique< + cuco::static_map>( + // cuco::static_map requires at least one empty slot + std::max(static_cast( + static_cast(local_int_vertex_last - local_int_vertex_first) / load_factor), + static_cast(local_int_vertex_last - local_int_vertex_first) + 1), + invalid_vertex_id::value, + invalid_vertex_id::value, + stream_adapter); + + auto pair_first = thrust::make_zip_iterator( + thrust::make_tuple(renumber_map_labels, thrust::make_counting_iterator(vertex_t{0}))); + renumber_map_ptr->insert(pair_first, + pair_first + (local_int_vertex_last - local_int_vertex_first)); + } + + if (do_expensive_check) { + rmm::device_uvector contains(num_vertices, handle.get_stream_view()); + renumber_map_ptr->contains(vertices, vertices + num_vertices, contains.begin()); + auto vc_pair_first = thrust::make_zip_iterator(thrust::make_tuple(vertices, contains.begin())); + CUGRAPH_EXPECTS(thrust::count_if(rmm::exec_policy(handle.get_stream_view()), + vc_pair_first, + vc_pair_first + num_vertices, + [] __device__(auto pair) { + auto v = thrust::get<0>(pair); + auto c = thrust::get<1>(pair); + return v == invalid_vertex_id::value + ? (c == true) + : (c == false); + }) == 0, + "Invalid input arguments: vertices have elements that are missing in " + "(aggregate) renumber_map_labels."); + } + + renumber_map_ptr->find(vertices, vertices + num_vertices, vertices); +} + +template +void unrenumber_local_int_vertices( + raft::handle_t const& handle, + vertex_t* vertices /* [INOUT] */, + size_t num_vertices, + vertex_t const* renumber_map_labels /* size = local_int_vertex_last - local_int_vertex_first */, + vertex_t local_int_vertex_first, + vertex_t local_int_vertex_last, + bool do_expensive_check) +{ + if (do_expensive_check) { + CUGRAPH_EXPECTS( + thrust::count_if(rmm::exec_policy(handle.get_stream_view()), + vertices, + vertices + num_vertices, + [local_int_vertex_first, local_int_vertex_last] __device__(auto v) { + return v != invalid_vertex_id::value && + (v < local_int_vertex_first || v >= local_int_vertex_last); + }) == 0, + "Invalid input arguments: there are non-local vertices in [vertices, vertices " + "+ num_vertices)."); + } + + thrust::transform(rmm::exec_policy(handle.get_stream_view()), + vertices, + vertices + num_vertices, + vertices, + [renumber_map_labels, local_int_vertex_first] __device__(auto v) { + return v == invalid_vertex_id::value + ? v + : renumber_map_labels[v - local_int_vertex_first]; + }); +} + +template +void unrenumber_int_vertices(raft::handle_t const& handle, + vertex_t* vertices /* [INOUT] */, + size_t num_vertices, + vertex_t const* renumber_map_labels, + vertex_t local_int_vertex_first, + vertex_t local_int_vertex_last, + std::vector const& vertex_partition_lasts, + bool do_expensive_check) +{ + double constexpr load_factor = 0.7; + + if (do_expensive_check) { + CUGRAPH_EXPECTS( + thrust::count_if(rmm::exec_policy(handle.get_stream_view()), + vertices, + vertices + num_vertices, + [int_vertex_last = vertex_partition_lasts.back()] __device__(auto v) { + return v != invalid_vertex_id::value && + !is_valid_vertex(int_vertex_last, v); + }) == 0, + "Invalid input arguments: there are out-of-range vertices in [vertices, vertices " + "+ num_vertices)."); + } + + if (multi_gpu) { + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + + rmm::device_uvector sorted_unique_int_vertices(num_vertices, + handle.get_stream_view()); + sorted_unique_int_vertices.resize( + thrust::distance( + sorted_unique_int_vertices.begin(), + thrust::copy_if(rmm::exec_policy(handle.get_stream_view()), + vertices, + vertices + num_vertices, + sorted_unique_int_vertices.begin(), + [] __device__(auto v) { return v != invalid_vertex_id::value; })), + handle.get_stream_view()); + thrust::sort(rmm::exec_policy(handle.get_stream_view()), + sorted_unique_int_vertices.begin(), + sorted_unique_int_vertices.end()); + sorted_unique_int_vertices.resize( + thrust::distance(sorted_unique_int_vertices.begin(), + thrust::unique(rmm::exec_policy(handle.get_stream_view()), + sorted_unique_int_vertices.begin(), + sorted_unique_int_vertices.end())), + handle.get_stream_view()); + + rmm::device_uvector d_vertex_partition_lasts(vertex_partition_lasts.size(), + handle.get_stream_view()); + raft::update_device(d_vertex_partition_lasts.data(), + vertex_partition_lasts.data(), + vertex_partition_lasts.size(), + handle.get_stream()); + rmm::device_uvector d_tx_int_vertex_offsets(d_vertex_partition_lasts.size(), + handle.get_stream_view()); + thrust::lower_bound(rmm::exec_policy(handle.get_stream_view()), + sorted_unique_int_vertices.begin(), + sorted_unique_int_vertices.end(), + d_vertex_partition_lasts.begin(), + d_vertex_partition_lasts.end(), + d_tx_int_vertex_offsets.begin()); + std::vector h_tx_int_vertex_counts(d_tx_int_vertex_offsets.size()); + raft::update_host(h_tx_int_vertex_counts.data(), + d_tx_int_vertex_offsets.data(), + d_tx_int_vertex_offsets.size(), + handle.get_stream()); + handle.get_stream_view().synchronize(); + std::adjacent_difference( + h_tx_int_vertex_counts.begin(), h_tx_int_vertex_counts.end(), h_tx_int_vertex_counts.begin()); + + rmm::device_uvector rx_int_vertices(0, handle.get_stream_view()); + std::vector rx_int_vertex_counts{}; + std::tie(rx_int_vertices, rx_int_vertex_counts) = shuffle_values( + comm, sorted_unique_int_vertices.begin(), h_tx_int_vertex_counts, handle.get_stream_view()); + + auto tx_ext_vertices = std::move(rx_int_vertices); + thrust::transform(rmm::exec_policy(handle.get_stream_view()), + tx_ext_vertices.begin(), + tx_ext_vertices.end(), + tx_ext_vertices.begin(), + [renumber_map_labels, local_int_vertex_first] __device__(auto v) { + return renumber_map_labels[v - local_int_vertex_first]; + }); + + rmm::device_uvector rx_ext_vertices_for_sorted_unique_int_vertices( + 0, handle.get_stream_view()); + std::tie(rx_ext_vertices_for_sorted_unique_int_vertices, std::ignore) = + shuffle_values(comm, tx_ext_vertices.begin(), rx_int_vertex_counts, handle.get_stream_view()); + + handle.get_stream_view().synchronize(); // cuco::static_map currently does not take stream + + auto poly_alloc = rmm::mr::polymorphic_allocator(rmm::mr::get_current_device_resource()); + auto stream_adapter = rmm::mr::make_stream_allocator_adaptor(poly_alloc, cudaStream_t{nullptr}); + cuco::static_map + unrenumber_map{ + // cuco::static_map requires at least one empty slot + std::max( + static_cast(static_cast(sorted_unique_int_vertices.size()) / load_factor), + sorted_unique_int_vertices.size() + 1), + invalid_vertex_id::value, + invalid_vertex_id::value, + stream_adapter}; + + auto pair_first = thrust::make_zip_iterator(thrust::make_tuple( + sorted_unique_int_vertices.begin(), rx_ext_vertices_for_sorted_unique_int_vertices.begin())); + unrenumber_map.insert(pair_first, pair_first + sorted_unique_int_vertices.size()); + unrenumber_map.find(vertices, vertices + num_vertices, vertices); + } else { + unrenumber_local_int_vertices(handle, + vertices, + num_vertices, + renumber_map_labels, + local_int_vertex_first, + local_int_vertex_last, + do_expensive_check); + } +} + +// explicit instantiation + +template void renumber_ext_vertices(raft::handle_t const& handle, + int32_t* vertices, + size_t num_vertices, + int32_t const* renumber_map_labels, + int32_t local_int_vertex_first, + int32_t local_int_vertex_last, + bool do_expensive_check); + +template void renumber_ext_vertices(raft::handle_t const& handle, + int32_t* vertices, + size_t num_vertices, + int32_t const* renumber_map_labels, + int32_t local_int_vertex_first, + int32_t local_int_vertex_last, + bool do_expensive_check); + +template void renumber_ext_vertices(raft::handle_t const& handle, + int64_t* vertices, + size_t num_vertices, + int64_t const* renumber_map_labels, + int64_t local_int_vertex_first, + int64_t local_int_vertex_last, + bool do_expensive_check); + +template void renumber_ext_vertices(raft::handle_t const& handle, + int64_t* vertices, + size_t num_vertices, + int64_t const* renumber_map_labels, + int64_t local_int_vertex_first, + int64_t local_int_vertex_last, + bool do_expensive_check); + +template void unrenumber_local_int_vertices(raft::handle_t const& handle, + int32_t* vertices, + size_t num_vertices, + int32_t const* renumber_map_labels, + int32_t local_int_vertex_first, + int32_t local_int_vertex_last, + bool do_expensive_check); + +template void unrenumber_local_int_vertices(raft::handle_t const& handle, + int64_t* vertices, + size_t num_vertices, + int64_t const* renumber_map_labels, + int64_t local_int_vertex_first, + int64_t local_int_vertex_last, + bool do_expensive_check); + +template void unrenumber_int_vertices( + raft::handle_t const& handle, + int32_t* vertices, + size_t num_vertices, + int32_t const* renumber_map_labels, + int32_t local_int_vertex_first, + int32_t local_int_vertex_last, + std::vector const& vertex_partition_lasts, + bool do_expensive_check); + +template void unrenumber_int_vertices( + raft::handle_t const& handle, + int32_t* vertices, + size_t num_vertices, + int32_t const* renumber_map_labels, + int32_t local_int_vertex_first, + int32_t local_int_vertex_last, + std::vector const& vertex_partition_lasts, + bool do_expensive_check); + +template void unrenumber_int_vertices( + raft::handle_t const& handle, + int64_t* vertices, + size_t num_vertices, + int64_t const* renumber_map_labels, + int64_t local_int_vertex_first, + int64_t local_int_vertex_last, + std::vector const& vertex_partition_lasts, + bool do_expensive_check); + +template void unrenumber_int_vertices( + raft::handle_t const& handle, + int64_t* vertices, + size_t num_vertices, + int64_t const* renumber_map_labels, + int64_t local_int_vertex_first, + int64_t local_int_vertex_last, + std::vector const& vertex_partition_lasts, + bool do_expensive_check); + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/src/experimental/scramble.cuh b/cpp/src/experimental/scramble.cuh new file mode 100644 index 00000000000..875bb5feff0 --- /dev/null +++ b/cpp/src/experimental/scramble.cuh @@ -0,0 +1,82 @@ +/* Copyright (C) 2009-2010 The Trustees of Indiana University. */ +/* */ +/* Use, modification and distribution is subject to the Boost Software */ +/* License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at */ +/* http://www.boost.org/LICENSE_1_0.txt) */ +/* */ +/* Authors: Jeremiah Willcock */ +/* Andrew Lumsdaine */ + +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include + +namespace cugraph { +namespace experimental { +namespace detail { + +template +__device__ std::enable_if_t bitreversal(uvertex_t value) +{ + return __brevll(value); +} + +template +__device__ std::enable_if_t bitreversal(uvertex_t value) +{ + return __brev(value); +} + +template +__device__ std::enable_if_t bitreversal(uvertex_t value) +{ + return static_cast(__brev(value) >> 16); +} + +/* Apply a permutation to scramble vertex numbers; a randomly generated + * permutation is not used because applying it at scale is too expensive. */ +template +__device__ vertex_t scramble(vertex_t value, size_t lgN) +{ + constexpr size_t number_of_bits = sizeof(vertex_t) * 8; + + static_assert((number_of_bits == 64) || (number_of_bits == 32) || (number_of_bits == 16)); + assert((std::is_unsigned::value && lgN <= number_of_bits) || + (!std::is_unsigned::value && lgN < number_of_bits)); + assert(value >= 0); + + using uvertex_t = typename std::make_unsigned::type; + + constexpr auto scramble_value0 = static_cast( + sizeof(vertex_t) == 8 ? 606610977102444280 : (sizeof(vertex_t) == 4 ? 282475248 : 0)); + constexpr auto scramble_value1 = static_cast( + sizeof(vertex_t) == 8 ? 11680327234415193037 : (sizeof(vertex_t) == 4 ? 2617694917 : 8620)); + + auto v = static_cast(value); + v += scramble_value0 + scramble_value1; + v *= (scramble_value0 | static_cast(0x4519840211493211)); + v = bitreversal(v) >> (number_of_bits - lgN); + v *= (scramble_value1 | static_cast(0x3050852102C843A5)); + v = bitreversal(v) >> (number_of_bits - lgN); + return static_cast(v); +} + +} // namespace detail +} // namespace experimental +} // namespace cugraph diff --git a/cpp/src/experimental/shuffle.cuh b/cpp/src/experimental/shuffle.cuh deleted file mode 100644 index 40f3b510b10..00000000000 --- a/cpp/src/experimental/shuffle.cuh +++ /dev/null @@ -1,226 +0,0 @@ -/* - * Copyright (c) 2020, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#pragma once - -#include -#include - -namespace cugraph { -namespace experimental { - -namespace detail { - -// -// FIXME: This implementation of variable_shuffle stages the data for transfer -// in host memory. It would be more efficient, I believe, to stage the -// data in device memory, but it would require actually instantiating -// the data in device memory which is already precious in the Louvain -// implementation. We should explore if it's actually more efficient -// through device memory and whether the improvement is worth the extra -// memory required. -// -template -rmm::device_vector variable_shuffle(raft::handle_t const &handle, - std::size_t n_elements, - iterator_t data_iter, - partition_iter_t partition_iter) -{ - // - // We need to compute the size of data movement - // - raft::comms::comms_t const &comms = handle.get_comms(); - - cudaStream_t stream = handle.get_stream(); - int num_gpus = comms.get_size(); - int my_gpu = comms.get_rank(); - - rmm::device_vector local_sizes_v(num_gpus, size_t{0}); - - thrust::for_each(rmm::exec_policy(stream)->on(stream), - partition_iter, - partition_iter + n_elements, - [num_gpus, d_local_sizes = local_sizes_v.data().get()] __device__(auto p) { - atomicAdd(d_local_sizes + p, size_t{1}); - }); - - std::vector h_local_sizes_v(num_gpus); - std::vector h_global_sizes_v(num_gpus); - std::vector h_input_v(n_elements); - std::vector h_partitions_v(n_elements); - - thrust::copy(local_sizes_v.begin(), local_sizes_v.end(), h_local_sizes_v.begin()); - thrust::copy(partition_iter, partition_iter + n_elements, h_partitions_v.begin()); - - std::vector requests(2 * num_gpus); - - int request_pos = 0; - - for (int gpu = 0; gpu < num_gpus; ++gpu) { - if (gpu != my_gpu) { - comms.irecv(&h_global_sizes_v[gpu], 1, gpu, 0, &requests[request_pos]); - ++request_pos; - comms.isend(&h_local_sizes_v[gpu], 1, gpu, 0, &requests[request_pos]); - ++request_pos; - } else { - h_global_sizes_v[gpu] = h_local_sizes_v[gpu]; - } - } - - if (request_pos > 0) { comms.waitall(request_pos, requests.data()); } - - comms.barrier(); - - // - // Now global_sizes contains all of the counts, we need to - // allocate an array of the appropriate size - // - int64_t receive_size = - thrust::reduce(thrust::host, h_global_sizes_v.begin(), h_global_sizes_v.end()); - - std::vector temp_data; - - if (receive_size > 0) temp_data.resize(receive_size); - - rmm::device_vector input_v(n_elements); - - auto input_start = input_v.begin(); - - for (int gpu = 0; gpu < num_gpus; ++gpu) { - input_start = thrust::copy_if(rmm::exec_policy(stream)->on(stream), - data_iter, - data_iter + n_elements, - partition_iter, - input_start, - [gpu] __device__(int32_t p) { return p == gpu; }); - } - - thrust::copy(input_v.begin(), input_v.end(), h_input_v.begin()); - - std::vector temp_v(num_gpus + 1); - - thrust::exclusive_scan( - thrust::host, h_global_sizes_v.begin(), h_global_sizes_v.end(), temp_v.begin()); - - temp_v[num_gpus] = temp_v[num_gpus - 1] + h_global_sizes_v[num_gpus - 1]; - h_global_sizes_v = temp_v; - - thrust::exclusive_scan( - thrust::host, h_local_sizes_v.begin(), h_local_sizes_v.end(), temp_v.begin()); - - temp_v[num_gpus] = temp_v[num_gpus - 1] + h_local_sizes_v[num_gpus - 1]; - h_local_sizes_v = temp_v; - - CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); - comms.barrier(); - - request_pos = 0; - - for (int gpu = 0; gpu < num_gpus; ++gpu) { - size_t to_receive = h_global_sizes_v[gpu + 1] - h_global_sizes_v[gpu]; - size_t to_send = h_local_sizes_v[gpu + 1] - h_local_sizes_v[gpu]; - - if (gpu != my_gpu) { - if (to_receive > 0) { - comms.irecv( - temp_data.data() + h_global_sizes_v[gpu], to_receive, gpu, 0, &requests[request_pos]); - ++request_pos; - } - - if (to_send > 0) { - comms.isend( - h_input_v.data() + h_local_sizes_v[gpu], to_send, gpu, 0, &requests[request_pos]); - ++request_pos; - } - } else if (to_receive > 0) { - std::copy(h_input_v.begin() + h_local_sizes_v[gpu], - h_input_v.begin() + h_local_sizes_v[gpu + 1], - temp_data.begin() + h_global_sizes_v[gpu]); - } - } - - comms.barrier(); - - if (request_pos > 0) { comms.waitall(request_pos, requests.data()); } - - comms.barrier(); - - return rmm::device_vector(temp_data); -} - -} // namespace detail - -/** - * @brief shuffle data to the desired partition - * - * MNMG algorithms require shuffling data between partitions - * to get the data to the right location for computation. - * - * This function operates dynamically, there is no - * a priori knowledge about where the data will need - * to be transferred. - * - * This function will be executed on each GPU. Each gpu - * has a portion of the data (specified by begin_data and - * end_data iterators) and an iterator that identifies - * (for each corresponding element) which GPU the data - * should be shuffled to. - * - * The return value will be a device vector containing - * the data received by this GPU. - * - * Note that this function accepts iterators as input. - * `partition_iterator` will be traversed multiple times. - * - * @tparam is_multi_gpu If true, multi-gpu - shuffle will occur - * If false, single GPU - simple copy will occur - * @tparam data_t Type of the data being shuffled - * @tparam iterator_t Iterator referencing data to be shuffled - * @tparam partition_iter_t Iterator identifying the destination partition - * - * @param handle Library handle (RAFT) - * @param n_elements Number of elements to transfer - * @param data_iter Iterator that returns the elements to be transfered - * @param partition_iter Iterator that returns the partition where elements - * should be transfered. - */ -template * = nullptr> -rmm::device_vector variable_shuffle(raft::handle_t const &handle, - std::size_t n_elements, - iterator_t data_iter, - partition_iter_t partition_iter) -{ - return detail::variable_shuffle(handle, n_elements, data_iter, partition_iter); -} - -template * = nullptr> -rmm::device_vector variable_shuffle(raft::handle_t const &handle, - std::size_t n_elements, - iterator_t data_iter, - partition_iter_t partition_iter) -{ - return rmm::device_vector(data_iter, data_iter + n_elements); -} - -} // namespace experimental -} // namespace cugraph diff --git a/cpp/src/experimental/sssp.cu b/cpp/src/experimental/sssp.cu index ebcde1b1444..b6f85ddb3c2 100644 --- a/cpp/src/experimental/sssp.cu +++ b/cpp/src/experimental/sssp.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -14,16 +14,16 @@ * limitations under the License. */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -42,9 +43,9 @@ namespace experimental { namespace detail { template -void sssp(raft::handle_t const &handle, - GraphViewType const &push_graph_view, - typename GraphViewType::weight_type *distances, +void sssp(raft::handle_t const& handle, + GraphViewType const& push_graph_view, + typename GraphViewType::weight_type* distances, PredecessorIterator predecessor_first, typename GraphViewType::vertex_type source_vertex, typename GraphViewType::weight_type cutoff, @@ -70,6 +71,9 @@ void sssp(raft::handle_t const &handle, CUGRAPH_EXPECTS(push_graph_view.is_valid_vertex(source_vertex), "Invalid input argument: source vertex out-of-range."); + CUGRAPH_EXPECTS(push_graph_view.is_weighted(), + "Invalid input argument: an unweighted graph is passed to SSSP, BFS is more " + "efficient for unweighted graphs."); if (do_expensive_check) { auto num_negative_edge_weights = @@ -122,15 +126,12 @@ void sssp(raft::handle_t const &handle, // 4. initialize SSSP frontier - enum class Bucket { cur_near, new_near, far, num_buckets }; - // FIXME: need to double check the bucket sizes are sufficient - std::vector bucket_sizes(static_cast(Bucket::num_buckets), - push_graph_view.get_number_of_local_vertices()); - VertexFrontier, - vertex_t, + enum class Bucket { cur_near, next_near, far, num_buckets }; + VertexFrontier(Bucket::num_buckets)> - vertex_frontier(handle, bucket_sizes); + vertex_frontier(handle); // 5. SSSP iteration @@ -167,13 +168,15 @@ void sssp(raft::handle_t const &handle, row_distances); } - vertex_partition_device_t vertex_partition(push_graph_view); + auto vertex_partition = vertex_partition_device_view_t( + push_graph_view.get_vertex_partition_view()); update_frontier_v_push_if_out_nbr( handle, push_graph_view, - vertex_frontier.get_bucket(static_cast(Bucket::cur_near)).begin(), - vertex_frontier.get_bucket(static_cast(Bucket::cur_near)).end(), + vertex_frontier, + static_cast(Bucket::cur_near), + std::vector{static_cast(Bucket::next_near), static_cast(Bucket::far)}, row_distances, thrust::make_constant_iterator(0) /* dummy */, [vertex_partition, distances, cutoff] __device__( @@ -188,58 +191,62 @@ void sssp(raft::handle_t const &handle, threshold = old_distance < threshold ? old_distance : threshold; } if (new_distance >= threshold) { push = false; } - return thrust::make_tuple(push, new_distance, src); + return push ? thrust::optional>{thrust::make_tuple( + new_distance, src)} + : thrust::nullopt; }, reduce_op::min>(), distances, thrust::make_zip_iterator(thrust::make_tuple(distances, predecessor_first)), - vertex_frontier, - [near_far_threshold] __device__(auto v_val, auto pushed_val) { + [near_far_threshold] __device__(auto v, auto v_val, auto pushed_val) { auto new_dist = thrust::get<0>(pushed_val); auto idx = new_dist < v_val - ? (new_dist < near_far_threshold ? static_cast(Bucket::new_near) - : static_cast(Bucket::far)) - : VertexFrontier, vertex_t>::kInvalidBucketIdx; - return thrust::make_tuple(idx, thrust::get<0>(pushed_val), thrust::get<1>(pushed_val)); + ? (new_dist < near_far_threshold ? static_cast(Bucket::next_near) + : static_cast(Bucket::far)) + : VertexFrontier::kInvalidBucketIdx; + return new_dist < v_val + ? thrust::optional>{thrust::make_tuple( + static_cast(new_dist < near_far_threshold ? Bucket::next_near + : Bucket::far), + pushed_val)} + : thrust::nullopt; }); vertex_frontier.get_bucket(static_cast(Bucket::cur_near)).clear(); - if (vertex_frontier.get_bucket(static_cast(Bucket::new_near)).aggregate_size() > 0) { + vertex_frontier.get_bucket(static_cast(Bucket::cur_near)).shrink_to_fit(); + if (vertex_frontier.get_bucket(static_cast(Bucket::next_near)).aggregate_size() > 0) { vertex_frontier.swap_buckets(static_cast(Bucket::cur_near), - static_cast(Bucket::new_near)); + static_cast(Bucket::next_near)); } else if (vertex_frontier.get_bucket(static_cast(Bucket::far)).aggregate_size() > 0) { // near queue is empty, split the far queue auto old_near_far_threshold = near_far_threshold; near_far_threshold += delta; - size_t new_near_size{0}; - size_t new_far_size{0}; + size_t near_size{0}; + size_t far_size{0}; while (true) { vertex_frontier.split_bucket( static_cast(Bucket::far), + std::vector{static_cast(Bucket::cur_near)}, [vertex_partition, distances, old_near_far_threshold, near_far_threshold] __device__( auto v) { auto dist = *(distances + vertex_partition.get_local_vertex_offset_from_vertex_nocheck(v)); - if (dist < old_near_far_threshold) { - return VertexFrontier, vertex_t>::kInvalidBucketIdx; - } else if (dist < near_far_threshold) { - return static_cast(Bucket::cur_near); - } else { - return static_cast(Bucket::far); - } + return dist >= old_near_far_threshold + ? thrust::optional{static_cast( + dist < near_far_threshold ? Bucket::cur_near : Bucket::far)} + : thrust::nullopt; }); - new_near_size = + near_size = vertex_frontier.get_bucket(static_cast(Bucket::cur_near)).aggregate_size(); - new_far_size = - vertex_frontier.get_bucket(static_cast(Bucket::far)).aggregate_size(); - if ((new_near_size > 0) || (new_far_size == 0)) { + far_size = vertex_frontier.get_bucket(static_cast(Bucket::far)).aggregate_size(); + if ((near_size > 0) || (far_size == 0)) { break; } else { near_far_threshold += delta; } } - if ((new_near_size == 0) && (new_far_size == 0)) { break; } + if ((near_size == 0) && (far_size == 0)) { break; } } else { break; } @@ -249,17 +256,15 @@ void sssp(raft::handle_t const &handle, handle.get_stream())); // this is as necessary vertex_frontier will become out-of-scope once // this function returns (FIXME: should I stream sync in VertexFrontier // destructor?) - - return; } } // namespace detail template -void sssp(raft::handle_t const &handle, - graph_view_t const &graph_view, - weight_t *distances, - vertex_t *predecessors, +void sssp(raft::handle_t const& handle, + graph_view_t const& graph_view, + weight_t* distances, + vertex_t* predecessors, vertex_t source_vertex, weight_t cutoff, bool do_expensive_check) @@ -280,98 +285,98 @@ void sssp(raft::handle_t const &handle, // explicit instantiation -template void sssp(raft::handle_t const &handle, - graph_view_t const &graph_view, - float *distances, - int32_t *predecessors, +template void sssp(raft::handle_t const& handle, + graph_view_t const& graph_view, + float* distances, + int32_t* predecessors, int32_t source_vertex, float cutoff, bool do_expensive_check); -template void sssp(raft::handle_t const &handle, - graph_view_t const &graph_view, - double *distances, - int32_t *predecessors, +template void sssp(raft::handle_t const& handle, + graph_view_t const& graph_view, + double* distances, + int32_t* predecessors, int32_t source_vertex, double cutoff, bool do_expensive_check); -template void sssp(raft::handle_t const &handle, - graph_view_t const &graph_view, - float *distances, - int32_t *predecessors, +template void sssp(raft::handle_t const& handle, + graph_view_t const& graph_view, + float* distances, + int32_t* predecessors, int32_t source_vertex, float cutoff, bool do_expensive_check); -template void sssp(raft::handle_t const &handle, - graph_view_t const &graph_view, - double *distances, - int32_t *predecessors, +template void sssp(raft::handle_t const& handle, + graph_view_t const& graph_view, + double* distances, + int32_t* predecessors, int32_t source_vertex, double cutoff, bool do_expensive_check); -template void sssp(raft::handle_t const &handle, - graph_view_t const &graph_view, - float *distances, - int64_t *predecessors, +template void sssp(raft::handle_t const& handle, + graph_view_t const& graph_view, + float* distances, + int64_t* predecessors, int64_t source_vertex, float cutoff, bool do_expensive_check); -template void sssp(raft::handle_t const &handle, - graph_view_t const &graph_view, - double *distances, - int64_t *predecessors, +template void sssp(raft::handle_t const& handle, + graph_view_t const& graph_view, + double* distances, + int64_t* predecessors, int64_t source_vertex, double cutoff, bool do_expensive_check); -template void sssp(raft::handle_t const &handle, - graph_view_t const &graph_view, - float *distances, - int32_t *predecessors, +template void sssp(raft::handle_t const& handle, + graph_view_t const& graph_view, + float* distances, + int32_t* predecessors, int32_t source_vertex, float cutoff, bool do_expensive_check); -template void sssp(raft::handle_t const &handle, - graph_view_t const &graph_view, - double *distances, - int32_t *predecessors, +template void sssp(raft::handle_t const& handle, + graph_view_t const& graph_view, + double* distances, + int32_t* predecessors, int32_t source_vertex, double cutoff, bool do_expensive_check); -template void sssp(raft::handle_t const &handle, - graph_view_t const &graph_view, - float *distances, - int32_t *predecessors, +template void sssp(raft::handle_t const& handle, + graph_view_t const& graph_view, + float* distances, + int32_t* predecessors, int32_t source_vertex, float cutoff, bool do_expensive_check); -template void sssp(raft::handle_t const &handle, - graph_view_t const &graph_view, - double *distances, - int32_t *predecessors, +template void sssp(raft::handle_t const& handle, + graph_view_t const& graph_view, + double* distances, + int32_t* predecessors, int32_t source_vertex, double cutoff, bool do_expensive_check); -template void sssp(raft::handle_t const &handle, - graph_view_t const &graph_view, - float *distances, - int64_t *predecessors, +template void sssp(raft::handle_t const& handle, + graph_view_t const& graph_view, + float* distances, + int64_t* predecessors, int64_t source_vertex, float cutoff, bool do_expensive_check); -template void sssp(raft::handle_t const &handle, - graph_view_t const &graph_view, - double *distances, - int64_t *predecessors, +template void sssp(raft::handle_t const& handle, + graph_view_t const& graph_view, + double* distances, + int64_t* predecessors, int64_t source_vertex, double cutoff, bool do_expensive_check); diff --git a/cpp/src/generators/erdos_renyi_generator.cu b/cpp/src/generators/erdos_renyi_generator.cu new file mode 100644 index 00000000000..8452a613174 --- /dev/null +++ b/cpp/src/generators/erdos_renyi_generator.cu @@ -0,0 +1,122 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +namespace cugraph { + +template +std::tuple, rmm::device_uvector> +generate_erdos_renyi_graph_edgelist_gnp(raft::handle_t const& handle, + vertex_t num_vertices, + float p, + vertex_t base_vertex_id, + uint64_t seed) +{ + CUGRAPH_EXPECTS(num_vertices < std::numeric_limits::max(), + "Implementation cannot support specified value"); + + auto random_iterator = thrust::make_transform_iterator( + thrust::make_counting_iterator(0), [seed] __device__(size_t index) { + thrust::default_random_engine rng(seed); + thrust::uniform_real_distribution dist(0.0, 1.0); + rng.discard(index); + return dist(rng); + }); + + size_t count = thrust::count_if(rmm::exec_policy(handle.get_stream()), + random_iterator, + random_iterator + num_vertices * num_vertices, + [p] __device__(float prob) { return prob < p; }); + + rmm::device_uvector indices_v(count, handle.get_stream()); + + thrust::copy_if(rmm::exec_policy(handle.get_stream()), + random_iterator, + random_iterator + num_vertices * num_vertices, + indices_v.begin(), + [p] __device__(float prob) { return prob < p; }); + + rmm::device_uvector src_v(count, handle.get_stream()); + rmm::device_uvector dst_v(count, handle.get_stream()); + + thrust::transform(rmm::exec_policy(handle.get_stream()), + indices_v.begin(), + indices_v.end(), + thrust::make_zip_iterator(thrust::make_tuple(src_v.begin(), src_v.end())), + [num_vertices] __device__(size_t index) { + size_t src = index / num_vertices; + size_t dst = index % num_vertices; + + return thrust::make_tuple(static_cast(src), + static_cast(dst)); + }); + + handle.get_stream_view().synchronize(); + + return std::make_tuple(std::move(src_v), std::move(dst_v)); +} + +template +std::tuple, rmm::device_uvector> +generate_erdos_renyi_graph_edgelist_gnm(raft::handle_t const& handle, + vertex_t num_vertices, + size_t m, + vertex_t base_vertex_id, + uint64_t seed) +{ + CUGRAPH_FAIL("Not implemented"); +} + +template std::tuple, rmm::device_uvector> +generate_erdos_renyi_graph_edgelist_gnp(raft::handle_t const& handle, + int32_t num_vertices, + float p, + int32_t base_vertex_id, + uint64_t seed); + +template std::tuple, rmm::device_uvector> +generate_erdos_renyi_graph_edgelist_gnp(raft::handle_t const& handle, + int64_t num_vertices, + float p, + int64_t base_vertex_id, + uint64_t seed); + +template std::tuple, rmm::device_uvector> +generate_erdos_renyi_graph_edgelist_gnm(raft::handle_t const& handle, + int32_t num_vertices, + size_t m, + int32_t base_vertex_id, + uint64_t seed); + +template std::tuple, rmm::device_uvector> +generate_erdos_renyi_graph_edgelist_gnm(raft::handle_t const& handle, + int64_t num_vertices, + size_t m, + int64_t base_vertex_id, + uint64_t seed); + +} // namespace cugraph diff --git a/cpp/src/generators/generate_rmat_edgelist.cu b/cpp/src/generators/generate_rmat_edgelist.cu new file mode 100644 index 00000000000..c7d8a5682bc --- /dev/null +++ b/cpp/src/generators/generate_rmat_edgelist.cu @@ -0,0 +1,203 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include +#include +#include + +namespace cugraph { + +template +std::tuple, rmm::device_uvector> generate_rmat_edgelist( + raft::handle_t const& handle, + size_t scale, + size_t num_edges, + double a, + double b, + double c, + uint64_t seed, + bool clip_and_flip) +{ + CUGRAPH_EXPECTS((size_t{1} << scale) <= static_cast(std::numeric_limits::max()), + "Invalid input argument: scale too large for vertex_t."); + CUGRAPH_EXPECTS((a >= 0.0) && (b >= 0.0) && (c >= 0.0) && (a + b + c <= 1.0), + "Invalid input argument: a, b, c should be non-negative and a + b + c should not " + "be larger than 1.0."); + + // to limit memory footprint (1024 is a tuning parameter) + auto max_edges_to_generate_per_iteration = + static_cast(handle.get_device_properties().multiProcessorCount) * 1024; + rmm::device_uvector rands( + std::min(num_edges, max_edges_to_generate_per_iteration) * 2 * scale, handle.get_stream_view()); + + rmm::device_uvector srcs(num_edges, handle.get_stream_view()); + rmm::device_uvector dsts(num_edges, handle.get_stream_view()); + + size_t num_edges_generated{0}; + while (num_edges_generated < num_edges) { + auto num_edges_to_generate = + std::min(num_edges - num_edges_generated, max_edges_to_generate_per_iteration); + auto pair_first = thrust::make_zip_iterator(thrust::make_tuple(srcs.begin(), dsts.begin())) + + num_edges_generated; + + detail::uniform_random_fill( + handle.get_stream_view(), rands.data(), num_edges_to_generate * 2 * scale, 0.0f, 1.0f, seed); + seed += num_edges_to_generate * 2 * scale; + + thrust::transform( + rmm::exec_policy(handle.get_stream_view()), + thrust::make_counting_iterator(size_t{0}), + thrust::make_counting_iterator(num_edges_to_generate), + pair_first, + // if a + b == 0.0, a_norm is irrelevant, if (1.0 - (a+b)) == 0.0, c_norm is irrelevant + [scale, + clip_and_flip, + rands = rands.data(), + a_plus_b = a + b, + a_norm = (a + b) > 0.0 ? a / (a + b) : 0.0, + c_norm = (1.0 - (a + b)) > 0.0 ? c / (1.0 - (a + b)) : 0.0] __device__(auto i) { + vertex_t src{0}; + vertex_t dst{0}; + for (size_t bit = scale - 1; bit != 0; --bit) { + auto r0 = rands[i * 2 * scale + 2 * bit]; + auto r1 = rands[i * 2 * scale + 2 * bit + 1]; + auto src_bit_set = r0 > a_plus_b; + auto dst_bit_set = r1 > (src_bit_set ? c_norm : a_norm); + if (clip_and_flip) { + if (src == dst) { + if (!src_bit_set && dst_bit_set) { + src_bit_set = !src_bit_set; + dst_bit_set = !dst_bit_set; + } + } + } + src += src_bit_set ? static_cast(1 << bit) : 0; + dst += dst_bit_set ? static_cast(1 << bit) : 0; + } + return thrust::make_tuple(src, dst); + }); + num_edges_generated += num_edges_to_generate; + } + + return std::make_tuple(std::move(srcs), std::move(dsts)); +} + +template +std::vector, rmm::device_uvector>> +generate_rmat_edgelists(raft::handle_t const& handle, + size_t n_edgelists, + size_t min_scale, + size_t max_scale, + size_t edge_factor, + generator_distribution_t component_distribution, + generator_distribution_t edge_distribution, + uint64_t seed, + bool clip_and_flip) +{ + CUGRAPH_EXPECTS(min_scale > 0, "minimum graph scale is 1."); + CUGRAPH_EXPECTS( + size_t{1} << max_scale <= static_cast(std::numeric_limits::max()), + "Invalid input argument: scale too large for vertex_t."); + + std::vector, rmm::device_uvector>> output{}; + output.reserve(n_edgelists); + std::vector scale(n_edgelists); + + std::default_random_engine eng; + eng.seed(seed); + if (component_distribution == generator_distribution_t::UNIFORM) { + std::uniform_int_distribution dist(min_scale, max_scale); + std::generate(scale.begin(), scale.end(), [&dist, &eng]() { return dist(eng); }); + } else { + // May expose this as a parameter in the future + std::exponential_distribution dist(4); + // The modulo is here to protect the range because exponential distribution is defined on + // [0,infinity). With exponent 4 most values are between 0 and 1 + auto range = max_scale - min_scale; + std::generate(scale.begin(), scale.end(), [&dist, &eng, &min_scale, &range]() { + return min_scale + static_cast(static_cast(range) * dist(eng)) % range; + }); + } + + // intialized to standard powerlaw values + double a = 0.57, b = 0.19, c = 0.19; + if (edge_distribution == generator_distribution_t::UNIFORM) { + a = 0.25; + b = a; + c = a; + } + + for (size_t i = 0; i < n_edgelists; i++) { + output.push_back(generate_rmat_edgelist( + handle, scale[i], scale[i] * edge_factor, a, b, c, i, clip_and_flip)); + } + return output; +} + +template std::tuple, rmm::device_uvector> +generate_rmat_edgelist(raft::handle_t const& handle, + size_t scale, + size_t num_edges, + double a, + double b, + double c, + uint64_t seed, + bool clip_and_flip); + +template std::tuple, rmm::device_uvector> +generate_rmat_edgelist(raft::handle_t const& handle, + size_t scale, + size_t num_edges, + double a, + double b, + double c, + uint64_t seed, + bool clip_and_flip); + +template std::vector, rmm::device_uvector>> +generate_rmat_edgelists(raft::handle_t const& handle, + size_t n_edgelists, + size_t min_scale, + size_t max_scale, + size_t edge_factor, + generator_distribution_t component_distribution, + generator_distribution_t edge_distribution, + uint64_t seed, + bool clip_and_flip); + +template std::vector, rmm::device_uvector>> +generate_rmat_edgelists(raft::handle_t const& handle, + size_t n_edgelists, + size_t min_scale, + size_t max_scale, + size_t edge_factor, + generator_distribution_t component_distribution, + generator_distribution_t edge_distribution, + uint64_t seed, + bool clip_and_flip); + +} // namespace cugraph diff --git a/cpp/src/generators/generator_tools.cu b/cpp/src/generators/generator_tools.cu new file mode 100644 index 00000000000..375d96b0d99 --- /dev/null +++ b/cpp/src/generators/generator_tools.cu @@ -0,0 +1,302 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include + +#include +#include + +#include +#include + +#include + +namespace cugraph { + +namespace detail { + +template +rmm::device_uvector append_all(raft::handle_t const& handle, + std::vector>&& input) +{ + size_t size{0}; + // for (size_t i = 0; i < input.size(); ++i) size += input[i].size(); + for (auto& element : input) + size += element.size(); + + rmm::device_uvector output(size, handle.get_stream()); + auto output_iter = output.begin(); + + for (auto& element : input) { + raft::copy(output_iter, element.begin(), element.size(), handle.get_stream()); + output_iter += element.size(); + } + + /* +for (size_t i = 0; i < input.size(); ++i) { + raft::copy(output_iter, input[i].begin(), input[i].size(), handle.get_stream()); + output_iter += input[i].size(); +} + */ + + return output; +} + +} // namespace detail + +template +void scramble_vertex_ids(raft::handle_t const& handle, + rmm::device_uvector& d_src_v, + rmm::device_uvector& d_dst_v, + vertex_t vertex_id_offset, + uint64_t seed) +{ + vertex_t scale = 1 + raft::log2(d_src_v.size()); + + auto pair_first = thrust::make_zip_iterator(thrust::make_tuple(d_src_v.begin(), d_dst_v.begin())); + thrust::transform(rmm::exec_policy(handle.get_stream()), + pair_first, + pair_first + d_src_v.size(), + pair_first, + [scale] __device__(auto pair) { + return thrust::make_tuple( + experimental::detail::scramble(thrust::get<0>(pair), scale), + experimental::detail::scramble(thrust::get<1>(pair), scale)); + }); +} + +template +std::tuple, + rmm::device_uvector, + std::optional>> +combine_edgelists(raft::handle_t const& handle, + std::vector>&& sources, + std::vector>&& dests, + std::optional>>&& optional_d_weights, + bool remove_multi_edges) +{ + CUGRAPH_EXPECTS(sources.size() == dests.size(), + "sources and dests vertex lists must be the same size"); + + if (optional_d_weights) { + CUGRAPH_EXPECTS(sources.size() == optional_d_weights.value().size(), + "has_weights is specified, sources and weights must be the same size"); + + thrust::for_each_n( + thrust::host, + thrust::make_zip_iterator( + thrust::make_tuple(sources.begin(), dests.begin(), optional_d_weights.value().begin())), + sources.size(), + [](auto tuple) { + CUGRAPH_EXPECTS(thrust::get<0>(tuple).size() != thrust::get<1>(tuple).size(), + "source vertex and dest vertex uvectors must be same size"); + CUGRAPH_EXPECTS(thrust::get<0>(tuple).size() != thrust::get<2>(tuple).size(), + "source vertex and weights uvectors must be same size"); + }); + } else { + thrust::for_each_n( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(sources.begin(), dests.begin())), + sources.size(), + [](auto tuple) { + CUGRAPH_EXPECTS(thrust::get<0>(tuple).size() == thrust::get<1>(tuple).size(), + "source vertex and dest vertex uvectors must be same size"); + }); + } + + std::vector> d_weights; + + rmm::device_uvector srcs_v(0, handle.get_stream()); + rmm::device_uvector dsts_v(0, handle.get_stream()); + rmm::device_uvector weights_v(0, handle.get_stream()); + + srcs_v = detail::append_all(handle, std::move(sources)); + dsts_v = detail::append_all(handle, std::move(dests)); + + if (optional_d_weights) { + weights_v = detail::append_all(handle, std::move(optional_d_weights.value())); + } + + if (remove_multi_edges) { + size_t number_of_edges{srcs_v.size()}; + + if (optional_d_weights) { + thrust::sort( + rmm::exec_policy(handle.get_stream()), + thrust::make_zip_iterator( + thrust::make_tuple(srcs_v.begin(), dsts_v.begin(), weights_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(srcs_v.end(), dsts_v.end(), weights_v.end()))); + + auto pair_first = + thrust::make_zip_iterator(thrust::make_tuple(srcs_v.begin(), dsts_v.begin())); + auto end_iter = thrust::unique_by_key(rmm::exec_policy(handle.get_stream()), + pair_first, + pair_first + srcs_v.size(), + weights_v.begin()); + + number_of_edges = thrust::distance(pair_first, thrust::get<0>(end_iter)); + } else { + thrust::sort(rmm::exec_policy(handle.get_stream()), + thrust::make_zip_iterator(thrust::make_tuple(srcs_v.begin(), dsts_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(srcs_v.end(), dsts_v.end()))); + + auto pair_first = + thrust::make_zip_iterator(thrust::make_tuple(srcs_v.begin(), dsts_v.begin())); + + auto end_iter = thrust::unique( + rmm::exec_policy(handle.get_stream()), + thrust::make_zip_iterator(thrust::make_tuple(srcs_v.begin(), dsts_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(srcs_v.end(), dsts_v.end()))); + + number_of_edges = thrust::distance(pair_first, end_iter); + } + + srcs_v.resize(number_of_edges, handle.get_stream()); + srcs_v.shrink_to_fit(handle.get_stream()); + dsts_v.resize(number_of_edges, handle.get_stream()); + dsts_v.shrink_to_fit(handle.get_stream()); + + if (optional_d_weights) { + weights_v.resize(number_of_edges, handle.get_stream()); + weights_v.shrink_to_fit(handle.get_stream()); + } + } + + return std::make_tuple( + std::move(srcs_v), + std::move(dsts_v), + optional_d_weights + ? std::move(std::optional>(std::move(weights_v))) + : std::nullopt); +} + +template +std::tuple, + rmm::device_uvector, + std::optional>> +symmetrize_edgelist(raft::handle_t const& handle, + rmm::device_uvector&& d_src_v, + rmm::device_uvector&& d_dst_v, + std::optional>&& optional_d_weights_v) +{ + auto offset = d_src_v.size(); + d_src_v.resize(offset * 2, handle.get_stream_view()); + d_dst_v.resize(offset * 2, handle.get_stream_view()); + + thrust::copy(rmm::exec_policy(handle.get_stream_view()), + d_dst_v.begin(), + d_dst_v.begin() + offset, + d_src_v.begin() + offset); + thrust::copy(rmm::exec_policy(handle.get_stream_view()), + d_src_v.begin(), + d_src_v.begin() + offset, + d_dst_v.begin() + offset); + if (optional_d_weights_v) { + optional_d_weights_v->resize(d_src_v.size(), handle.get_stream_view()); + thrust::copy(rmm::exec_policy(handle.get_stream_view()), + optional_d_weights_v->begin(), + optional_d_weights_v->begin() + offset, + optional_d_weights_v->begin() + offset); + } + + return std::make_tuple(std::move(d_src_v), + std::move(d_dst_v), + optional_d_weights_v ? std::move(optional_d_weights_v) : std::nullopt); +} + +template void scramble_vertex_ids(raft::handle_t const& handle, + rmm::device_uvector& d_src_v, + rmm::device_uvector& d_dst_v, + int32_t vertex_id_offset, + uint64_t seed); + +template void scramble_vertex_ids(raft::handle_t const& handle, + rmm::device_uvector& d_src_v, + rmm::device_uvector& d_dst_v, + int64_t vertex_id_offset, + uint64_t seed); + +template std::tuple, + rmm::device_uvector, + std::optional>> +combine_edgelists(raft::handle_t const& handle, + std::vector>&& sources, + std::vector>&& dests, + std::optional>>&& optional_d_weights, + bool remove_multi_edges); + +template std::tuple, + rmm::device_uvector, + std::optional>> +combine_edgelists(raft::handle_t const& handle, + std::vector>&& sources, + std::vector>&& dests, + std::optional>>&& optional_d_weights, + bool remove_multi_edges); + +template std::tuple, + rmm::device_uvector, + std::optional>> +combine_edgelists(raft::handle_t const& handle, + std::vector>&& sources, + std::vector>&& dests, + std::optional>>&& optional_d_weights, + bool remove_multi_edges); + +template std::tuple, + rmm::device_uvector, + std::optional>> +combine_edgelists(raft::handle_t const& handle, + std::vector>&& sources, + std::vector>&& dests, + std::optional>>&& optional_d_weights, + bool remove_multi_edges); + +template std::tuple, + rmm::device_uvector, + std::optional>> +symmetrize_edgelist(raft::handle_t const& handle, + rmm::device_uvector&& d_src_v, + rmm::device_uvector&& d_dst_v, + std::optional>&& optional_d_weights_v); +template std::tuple, + rmm::device_uvector, + std::optional>> +symmetrize_edgelist(raft::handle_t const& handle, + rmm::device_uvector&& d_src_v, + rmm::device_uvector&& d_dst_v, + std::optional>&& optional_d_weights_v); + +template std::tuple, + rmm::device_uvector, + std::optional>> +symmetrize_edgelist(raft::handle_t const& handle, + rmm::device_uvector&& d_src_v, + rmm::device_uvector&& d_dst_v, + std::optional>&& optional_d_weights_v); +template std::tuple, + rmm::device_uvector, + std::optional>> +symmetrize_edgelist(raft::handle_t const& handle, + rmm::device_uvector&& d_src_v, + rmm::device_uvector&& d_dst_v, + std::optional>&& optional_d_weights_v); + +} // namespace cugraph diff --git a/cpp/src/generators/simple_generators.cu b/cpp/src/generators/simple_generators.cu new file mode 100644 index 00000000000..413e08962e7 --- /dev/null +++ b/cpp/src/generators/simple_generators.cu @@ -0,0 +1,343 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include +#include + +#include + +#include + +namespace cugraph { + +template +std::tuple, rmm::device_uvector> +generate_path_graph_edgelist(raft::handle_t const& handle, + std::vector> const& component_parms_v) +{ + size_t num_edges = thrust::transform_reduce( + thrust::host, + component_parms_v.begin(), + component_parms_v.end(), + [](auto tuple) { return (std::get<0>(tuple) - 1); }, + size_t{0}, + std::plus()); + + bool edge_off_end{false}; + + if (handle.comms_initialized()) { + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + auto const comm_rank = comm.get_rank(); + + if (comm_size > 1) { + if (comm_rank < comm_size) { + num_edges += component_parms_v.size(); + edge_off_end = true; + } + } + } + + rmm::device_uvector d_src_v(num_edges, handle.get_stream()); + rmm::device_uvector d_dst_v(num_edges, handle.get_stream()); + + auto src_iterator = d_src_v.begin(); + auto dst_iterator = d_dst_v.begin(); + + for (auto tuple : component_parms_v) { + vertex_t num_vertices, base_vertex_id; + std::tie(num_vertices, base_vertex_id) = tuple; + + vertex_t num_edges{num_vertices - 1}; + + if (edge_off_end) ++num_edges; + + thrust::sequence(rmm::exec_policy(handle.get_stream()), + src_iterator, + src_iterator + num_edges, + base_vertex_id); + + thrust::sequence(rmm::exec_policy(handle.get_stream()), + dst_iterator, + dst_iterator + num_edges, + base_vertex_id + 1); + + src_iterator += num_edges; + dst_iterator += num_edges; + } + + handle.get_stream_view().synchronize(); + + return std::make_tuple(std::move(d_src_v), std::move(d_dst_v)); +} + +template +std::tuple, rmm::device_uvector> +generate_2d_mesh_graph_edgelist( + raft::handle_t const& handle, + std::vector> const& component_parms_v) +{ + size_t num_edges = thrust::transform_reduce( + thrust::host, + component_parms_v.begin(), + component_parms_v.end(), + [](auto tuple) { + vertex_t x, y; + std::tie(x, y, std::ignore) = tuple; + + return ((x - 1) * y) + (x * (y - 1)); + }, + size_t{0}, + std::plus()); + + rmm::device_uvector d_src_v(num_edges, handle.get_stream()); + rmm::device_uvector d_dst_v(num_edges, handle.get_stream()); + + auto output_iterator = + thrust::make_zip_iterator(thrust::make_tuple(d_src_v.begin(), d_dst_v.begin())); + + for (auto tuple : component_parms_v) { + vertex_t x, y, base_vertex_id; + std::tie(x, y, base_vertex_id) = tuple; + + vertex_t num_vertices = x * y; + + auto x_iterator = thrust::make_zip_iterator( + thrust::make_tuple(thrust::make_counting_iterator(base_vertex_id), + thrust::make_counting_iterator(base_vertex_id + 1))); + + output_iterator = thrust::copy_if(rmm::exec_policy(handle.get_stream()), + x_iterator, + x_iterator + num_vertices - 1, + output_iterator, + [base_vertex_id, x] __device__(auto pair) { + vertex_t dst = thrust::get<1>(pair); + // Want to skip if dst is in the last column of a graph + return ((dst - base_vertex_id) % x) != 0; + }); + + auto y_iterator = thrust::make_zip_iterator( + thrust::make_tuple(thrust::make_counting_iterator(base_vertex_id), + thrust::make_counting_iterator(base_vertex_id + x))); + + output_iterator = thrust::copy_if(rmm::exec_policy(handle.get_stream()), + y_iterator, + y_iterator + num_vertices - x, + output_iterator, + [base_vertex_id, x, y] __device__(auto pair) { + vertex_t dst = thrust::get<1>(pair); + + // Want to skip if dst is in the first row of a new graph + return ((dst - base_vertex_id) % (x * y)) >= x; + }); + } + + handle.get_stream_view().synchronize(); + + return std::make_tuple(std::move(d_src_v), std::move(d_dst_v)); +} + +template +std::tuple, rmm::device_uvector> +generate_3d_mesh_graph_edgelist( + raft::handle_t const& handle, + std::vector> const& component_parms_v) +{ + size_t num_edges = thrust::transform_reduce( + thrust::host, + component_parms_v.begin(), + component_parms_v.end(), + [](auto tuple) { + vertex_t x, y, z; + std::tie(x, y, z, std::ignore) = tuple; + + return ((x - 1) * y * z) + (x * (y - 1) * z) + (x * y * (z - 1)); + }, + size_t{0}, + std::plus()); + + rmm::device_uvector d_src_v(num_edges, handle.get_stream()); + rmm::device_uvector d_dst_v(num_edges, handle.get_stream()); + + auto output_iterator = + thrust::make_zip_iterator(thrust::make_tuple(d_src_v.begin(), d_dst_v.begin())); + + for (auto tuple : component_parms_v) { + vertex_t x, y, z, base_vertex_id; + std::tie(x, y, z, base_vertex_id) = tuple; + + vertex_t num_vertices = x * y * z; + + auto x_iterator = thrust::make_zip_iterator( + thrust::make_tuple(thrust::make_counting_iterator(base_vertex_id), + thrust::make_counting_iterator(base_vertex_id + 1))); + + output_iterator = thrust::copy_if(rmm::exec_policy(handle.get_stream()), + x_iterator, + x_iterator + num_vertices - 1, + output_iterator, + [base_vertex_id, x] __device__(auto pair) { + vertex_t dst = thrust::get<1>(pair); + // Want to skip if dst is in the last column of a graph + return ((dst - base_vertex_id) % x) != 0; + }); + + auto y_iterator = thrust::make_zip_iterator( + thrust::make_tuple(thrust::make_counting_iterator(base_vertex_id), + thrust::make_counting_iterator(base_vertex_id + x))); + + output_iterator = thrust::copy_if(rmm::exec_policy(handle.get_stream()), + y_iterator, + y_iterator + num_vertices - x, + output_iterator, + [base_vertex_id, x, y] __device__(auto pair) { + vertex_t dst = thrust::get<1>(pair); + // Want to skip if dst is in the first row of a new graph + return ((dst - base_vertex_id) % (x * y)) >= x; + }); + + auto z_iterator = thrust::make_zip_iterator( + thrust::make_tuple(thrust::make_counting_iterator(base_vertex_id), + thrust::make_counting_iterator(base_vertex_id + x * y))); + + output_iterator = thrust::copy_if(rmm::exec_policy(handle.get_stream()), + z_iterator, + z_iterator + num_vertices - x * y, + output_iterator, + [base_vertex_id, x, y, z] __device__(auto pair) { + vertex_t dst = thrust::get<1>(pair); + // Want to skip if dst is in the first row of a new graph + return ((dst - base_vertex_id) % (x * y * z)) >= (x * y); + }); + } + + handle.get_stream_view().synchronize(); + + return std::make_tuple(std::move(d_src_v), std::move(d_dst_v)); +} + +template +std::tuple, rmm::device_uvector> +generate_complete_graph_edgelist( + raft::handle_t const& handle, + std::vector> const& component_parms_v) +{ + std::for_each(component_parms_v.begin(), component_parms_v.end(), [](auto tuple) { + vertex_t num_vertices = std::get<0>(tuple); + CUGRAPH_EXPECTS(num_vertices < std::numeric_limits::max(), + "Implementation cannot support specified value"); + }); + + size_t num_edges = thrust::transform_reduce( + thrust::host, + component_parms_v.begin(), + component_parms_v.end(), + [](auto tuple) { + vertex_t num_vertices = std::get<0>(tuple); + return num_vertices * (num_vertices - 1) / 2; + }, + size_t{0}, + std::plus()); + + vertex_t invalid_vertex{std::numeric_limits::max()}; + + rmm::device_uvector d_src_v(num_edges, handle.get_stream()); + rmm::device_uvector d_dst_v(num_edges, handle.get_stream()); + + auto output_iterator = + thrust::make_zip_iterator(thrust::make_tuple(d_src_v.begin(), d_dst_v.begin())); + + for (auto tuple : component_parms_v) { + vertex_t num_vertices, base_vertex_id; + std::tie(num_vertices, base_vertex_id) = tuple; + + auto transform_iter = thrust::make_transform_iterator( + thrust::make_counting_iterator(0), + [base_vertex_id, num_vertices, invalid_vertex] __device__(size_t index) { + size_t graph_index = index / (num_vertices * num_vertices); + size_t local_index = index % (num_vertices * num_vertices); + + vertex_t src = base_vertex_id + static_cast(local_index / num_vertices); + vertex_t dst = base_vertex_id + static_cast(local_index % num_vertices); + + if (src == dst) { + src = invalid_vertex; + dst = invalid_vertex; + } else { + src += (graph_index * num_vertices); + dst += (graph_index * num_vertices); + } + + return thrust::make_tuple(src, dst); + }); + + output_iterator = thrust::copy_if(rmm::exec_policy(handle.get_stream()), + transform_iter, + transform_iter + num_vertices * num_vertices, + output_iterator, + [invalid_vertex] __device__(auto tuple) { + auto src = thrust::get<0>(tuple); + auto dst = thrust::get<1>(tuple); + + return (src != invalid_vertex) && (src < dst); + }); + } + + handle.get_stream_view().synchronize(); + + return std::make_tuple(std::move(d_src_v), std::move(d_dst_v)); +} + +template std::tuple, rmm::device_uvector> +generate_path_graph_edgelist(raft::handle_t const& handle, + std::vector> const& component_parms_v); + +template std::tuple, rmm::device_uvector> +generate_path_graph_edgelist(raft::handle_t const& handle, + std::vector> const& component_parms_v); + +template std::tuple, rmm::device_uvector> +generate_2d_mesh_graph_edgelist( + raft::handle_t const& handle, + std::vector> const& component_parms_v); + +template std::tuple, rmm::device_uvector> +generate_2d_mesh_graph_edgelist( + raft::handle_t const& handle, + std::vector> const& component_parms_v); + +template std::tuple, rmm::device_uvector> +generate_3d_mesh_graph_edgelist( + raft::handle_t const& handle, + std::vector> const& component_parms_v); + +template std::tuple, rmm::device_uvector> +generate_3d_mesh_graph_edgelist( + raft::handle_t const& handle, + std::vector> const& component_parms_v); + +template std::tuple, rmm::device_uvector> +generate_complete_graph_edgelist( + raft::handle_t const& handle, std::vector> const& component_parms_v); + +template std::tuple, rmm::device_uvector> +generate_complete_graph_edgelist( + raft::handle_t const& handle, std::vector> const& component_parms_v); + +} // namespace cugraph diff --git a/cpp/src/layout/barnes_hut.cuh b/cpp/src/layout/barnes_hut.cuh new file mode 100644 index 00000000000..d05c6051d8b --- /dev/null +++ b/cpp/src/layout/barnes_hut.cuh @@ -0,0 +1,345 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include "bh_kernels.cuh" +#include "fa2_kernels.cuh" +#include "utils.hpp" + +#include +#include + +#include +#include +#include +#include + +#include +#include + +namespace cugraph { +namespace detail { + +template +void barnes_hut(raft::handle_t const& handle, + legacy::GraphCOOView& graph, + float* pos, + const int max_iter = 500, + float* x_start = nullptr, + float* y_start = nullptr, + bool outbound_attraction_distribution = true, + bool lin_log_mode = false, + bool prevent_overlapping = false, + const float edge_weight_influence = 1.0, + const float jitter_tolerance = 1.0, + const float theta = 0.5, + const float scaling_ratio = 2.0, + bool strong_gravity_mode = false, + const float gravity = 1.0, + bool verbose = false, + internals::GraphBasedDimRedCallback* callback = nullptr) +{ + rmm::cuda_stream_view stream_view(handle.get_stream_view()); + const edge_t e = graph.number_of_edges; + const vertex_t n = graph.number_of_vertices; + + const int blocks = getMultiProcessorCount(); + // A tiny jitter to promote numerical stability/ + const float epssq = 0.0025; + // We use the same array for nodes and cells. + int nnodes = n * 2; + if (nnodes < 1024 * blocks) nnodes = 1024 * blocks; + while ((nnodes & (32 - 1)) != 0) + nnodes++; + nnodes--; + + // Allocate more space + //--------------------------------------------------- + rmm::device_uvector d_limiter(1, stream_view); + rmm::device_uvector d_maxdepthd(1, stream_view); + rmm::device_uvector d_bottomd(1, stream_view); + rmm::device_uvector d_radiusd(1, stream_view); + + unsigned* limiter = d_limiter.data(); + int* maxdepthd = d_maxdepthd.data(); + int* bottomd = d_bottomd.data(); + float* radiusd = d_radiusd.data(); + + InitializationKernel<<<1, 1, 0, stream_view.value()>>>(limiter, maxdepthd, radiusd); + CHECK_CUDA(stream_view.value()); + + const int FOUR_NNODES = 4 * nnodes; + const int FOUR_N = 4 * n; + const float theta_squared = theta * theta; + const int NNODES = nnodes; + + rmm::device_uvector d_startl(nnodes + 1, stream_view); + rmm::device_uvector d_childl((nnodes + 1) * 4, stream_view); + // FA2 requires degree + 1 + rmm::device_uvector d_massl(nnodes + 1, stream_view); + thrust::fill(rmm::exec_policy(stream_view), d_massl.begin(), d_massl.end(), 1); + + rmm::device_uvector d_maxxl(blocks * FACTOR1, stream_view); + rmm::device_uvector d_maxyl(blocks * FACTOR1, stream_view); + rmm::device_uvector d_minxl(blocks * FACTOR1, stream_view); + rmm::device_uvector d_minyl(blocks * FACTOR1, stream_view); + + // Actual mallocs + int* startl = d_startl.data(); + int* childl = d_childl.data(); + int* massl = d_massl.data(); + + float* maxxl = d_maxxl.data(); + float* maxyl = d_maxyl.data(); + float* minxl = d_minxl.data(); + float* minyl = d_minyl.data(); + + // SummarizationKernel + rmm::device_uvector d_countl(nnodes + 1, stream_view); + int* countl = d_countl.data(); + + // SortKernel + rmm::device_uvector d_sortl(nnodes + 1, stream_view); + int* sortl = d_sortl.data(); + + // RepulsionKernel + rmm::device_uvector d_rep_forces((nnodes + 1) * 2, stream_view); + float* rep_forces = d_rep_forces.data(); + + rmm::device_uvector d_radius_squared(1, stream_view); + float* radiusd_squared = d_radius_squared.data(); + + rmm::device_uvector d_nodes_pos((nnodes + 1) * 2, stream_view); + float* nodes_pos = d_nodes_pos.data(); + + // Initialize positions with random values + int random_state = 0; + + // Copy start x and y positions. + if (x_start && y_start) { + raft::copy(nodes_pos, x_start, n, stream_view.value()); + raft::copy(nodes_pos + nnodes + 1, y_start, n, stream_view.value()); + } else { + uniform_random_fill( + handle.get_stream_view(), nodes_pos, (nnodes + 1) * 2, -100.0f, 100.0f, random_state); + } + + // Allocate arrays for force computation + float* attract{nullptr}; + float* old_forces{nullptr}; + float* swinging{nullptr}; + float* traction{nullptr}; + + rmm::device_uvector d_attract(n * 2, stream_view); + rmm::device_uvector d_old_forces(n * 2, stream_view); + rmm::device_uvector d_swinging(n, stream_view); + rmm::device_uvector d_traction(n, stream_view); + + attract = d_attract.data(); + old_forces = d_old_forces.data(); + swinging = d_swinging.data(); + traction = d_traction.data(); + + thrust::fill(rmm::exec_policy(stream_view), d_old_forces.begin(), d_old_forces.end(), 0.f); + + // Sort COO for coalesced memory access. + sort(graph, stream_view.value()); + CHECK_CUDA(stream_view.value()); + + graph.degree(massl, cugraph::legacy::DegreeDirection::OUT); + CHECK_CUDA(stream_view.value()); + + const vertex_t* row = graph.src_indices; + const vertex_t* col = graph.dst_indices; + const weight_t* v = graph.edge_data; + + // Scalars used to adapt global speed. + float speed = 1.f; + float speed_efficiency = 1.f; + float outbound_att_compensation = 1.f; + float jt = 0.f; + + // If outboundAttractionDistribution active, compensate. + if (outbound_attraction_distribution) { + int sum = thrust::reduce(rmm::exec_policy(stream_view), d_massl.begin(), d_massl.begin() + n); + outbound_att_compensation = sum / (float)n; + } + + // + // Set cache levels for faster algorithm execution + //--------------------------------------------------- + cudaFuncSetCacheConfig(BoundingBoxKernel, cudaFuncCachePreferShared); + cudaFuncSetCacheConfig(TreeBuildingKernel, cudaFuncCachePreferL1); + cudaFuncSetCacheConfig(ClearKernel1, cudaFuncCachePreferL1); + cudaFuncSetCacheConfig(ClearKernel2, cudaFuncCachePreferL1); + cudaFuncSetCacheConfig(SummarizationKernel, cudaFuncCachePreferShared); + cudaFuncSetCacheConfig(SortKernel, cudaFuncCachePreferL1); + cudaFuncSetCacheConfig(RepulsionKernel, cudaFuncCachePreferL1); + cudaFuncSetCacheConfig(apply_forces_bh, cudaFuncCachePreferL1); + + if (callback) { + callback->setup(nnodes + 1, 2); + callback->on_preprocess_end(nodes_pos); + } + + for (int iter = 0; iter < max_iter; ++iter) { + // Reset force values + thrust::fill(rmm::exec_policy(stream_view), d_rep_forces.begin(), d_rep_forces.end(), 0.f); + thrust::fill(rmm::exec_policy(stream_view), d_attract.begin(), d_attract.end(), 0.f); + thrust::fill(rmm::exec_policy(stream_view), d_swinging.begin(), d_swinging.end(), 0.f); + thrust::fill(rmm::exec_policy(stream_view), d_traction.begin(), d_traction.end(), 0.f); + + ResetKernel<<<1, 1, 0, stream_view.value()>>>(radiusd_squared, bottomd, NNODES, radiusd); + CHECK_CUDA(stream_view.value()); + + // Compute bounding box arround all bodies + BoundingBoxKernel<<>>( + startl, + childl, + massl, + nodes_pos, + nodes_pos + nnodes + 1, + maxxl, + maxyl, + minxl, + minyl, + FOUR_NNODES, + NNODES, + n, + limiter, + radiusd); + CHECK_CUDA(stream_view.value()); + + ClearKernel1<<>>(childl, FOUR_NNODES, FOUR_N); + CHECK_CUDA(stream_view.value()); + + // Build quadtree + TreeBuildingKernel<<>>( + childl, nodes_pos, nodes_pos + nnodes + 1, NNODES, n, maxdepthd, bottomd, radiusd); + CHECK_CUDA(stream_view.value()); + + ClearKernel2<<>>(startl, massl, NNODES, bottomd); + CHECK_CUDA(stream_view.value()); + + // Summarizes mass and position for each cell, bottom up approach + SummarizationKernel<<>>( + countl, childl, massl, nodes_pos, nodes_pos + nnodes + 1, NNODES, n, bottomd); + CHECK_CUDA(stream_view.value()); + + // Group closed bodies together, used to speed up Repulsion kernel + SortKernel<<>>( + sortl, countl, startl, childl, NNODES, n, bottomd); + CHECK_CUDA(stream_view.value()); + + // Force computation O(n . log(n)) + RepulsionKernel<<>>(scaling_ratio, + theta, + epssq, + sortl, + childl, + massl, + nodes_pos, + nodes_pos + nnodes + 1, + rep_forces, + rep_forces + nnodes + 1, + theta_squared, + NNODES, + FOUR_NNODES, + n, + radiusd_squared, + maxdepthd); + CHECK_CUDA(stream_view.value()); + + apply_gravity(nodes_pos, + nodes_pos + nnodes + 1, + attract, + attract + n, + massl, + gravity, + strong_gravity_mode, + scaling_ratio, + n, + stream_view.value()); + + apply_attraction(row, + col, + v, + e, + nodes_pos, + nodes_pos + nnodes + 1, + attract, + attract + n, + massl, + outbound_attraction_distribution, + lin_log_mode, + edge_weight_influence, + outbound_att_compensation, + stream_view.value()); + + compute_local_speed(rep_forces, + rep_forces + nnodes + 1, + attract, + attract + n, + old_forces, + old_forces + n, + massl, + swinging, + traction, + n, + stream_view.value()); + + // Compute global swinging and traction values + const float s = + thrust::reduce(rmm::exec_policy(stream_view), d_swinging.begin(), d_swinging.end()); + + const float t = + thrust::reduce(rmm::exec_policy(stream_view), d_traction.begin(), d_traction.end()); + + // Compute global speed based on gloab and local swinging and traction. + adapt_speed(jitter_tolerance, &jt, &speed, &speed_efficiency, s, t, n); + + // Update positions + apply_forces_bh<<>>(nodes_pos, + nodes_pos + nnodes + 1, + attract, + attract + n, + rep_forces, + rep_forces + nnodes + 1, + old_forces, + old_forces + n, + swinging, + speed, + n); + + if (callback) callback->on_epoch_end(nodes_pos); + + if (verbose) { + std::cout << "iteration: " << iter + 1 << ", speed: " << speed + << ", speed_efficiency: " << speed_efficiency << ", jt: " << jt + << ", swinging: " << s << ", traction: " << t << "\n"; + } + } + + // Copy nodes positions into final output pos + raft::copy(pos, nodes_pos, n, stream_view.value()); + raft::copy(pos + n, nodes_pos + nnodes + 1, n, stream_view.value()); + + if (callback) callback->on_train_end(nodes_pos); +} + +} // namespace detail +} // namespace cugraph diff --git a/cpp/src/layout/barnes_hut.hpp b/cpp/src/layout/barnes_hut.hpp deleted file mode 100644 index 437c98fce4b..00000000000 --- a/cpp/src/layout/barnes_hut.hpp +++ /dev/null @@ -1,337 +0,0 @@ -/* - * Copyright (c) 2020, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include -#include - -#include -#include -#include -#include - -#include "bh_kernels.hpp" -#include "fa2_kernels.hpp" -#include "utilities/graph_utils.cuh" -#include "utils.hpp" - -namespace cugraph { -namespace detail { - -template -void barnes_hut(GraphCOOView &graph, - float *pos, - const int max_iter = 500, - float *x_start = nullptr, - float *y_start = nullptr, - bool outbound_attraction_distribution = true, - bool lin_log_mode = false, - bool prevent_overlapping = false, - const float edge_weight_influence = 1.0, - const float jitter_tolerance = 1.0, - const float theta = 0.5, - const float scaling_ratio = 2.0, - bool strong_gravity_mode = false, - const float gravity = 1.0, - bool verbose = false, - internals::GraphBasedDimRedCallback *callback = nullptr) -{ - cudaStream_t stream = {nullptr}; - const edge_t e = graph.number_of_edges; - const vertex_t n = graph.number_of_vertices; - - const int blocks = getMultiProcessorCount(); - // A tiny jitter to promote numerical stability/ - const float epssq = 0.0025; - // We use the same array for nodes and cells. - int nnodes = n * 2; - if (nnodes < 1024 * blocks) nnodes = 1024 * blocks; - while ((nnodes & (32 - 1)) != 0) nnodes++; - nnodes--; - - // Allocate more space - //--------------------------------------------------- - rmm::device_vector d_limiter(1); - rmm::device_vector d_maxdepthd(1); - rmm::device_vector d_bottomd(1); - rmm::device_vector d_radiusd(1); - - unsigned *limiter = d_limiter.data().get(); - int *maxdepthd = d_maxdepthd.data().get(); - int *bottomd = d_bottomd.data().get(); - float *radiusd = d_radiusd.data().get(); - - InitializationKernel<<<1, 1, 0, stream>>>(limiter, maxdepthd, radiusd); - CHECK_CUDA(stream); - - const int FOUR_NNODES = 4 * nnodes; - const int FOUR_N = 4 * n; - const float theta_squared = theta * theta; - const int NNODES = nnodes; - - rmm::device_vector d_startl(nnodes + 1, 0); - rmm::device_vector d_childl((nnodes + 1) * 4, 0); - // FA2 requires degree + 1 - rmm::device_vector d_massl(nnodes + 1, 1.f); - - rmm::device_vector d_maxxl(blocks * FACTOR1, 0); - rmm::device_vector d_maxyl(blocks * FACTOR1, 0); - rmm::device_vector d_minxl(blocks * FACTOR1, 0); - rmm::device_vector d_minyl(blocks * FACTOR1, 0); - - // Actual mallocs - int *startl = d_startl.data().get(); - int *childl = d_childl.data().get(); - int *massl = d_massl.data().get(); - - float *maxxl = d_maxxl.data().get(); - float *maxyl = d_maxyl.data().get(); - float *minxl = d_minxl.data().get(); - float *minyl = d_minyl.data().get(); - - // SummarizationKernel - rmm::device_vector d_countl(nnodes + 1, 0); - int *countl = d_countl.data().get(); - - // SortKernel - rmm::device_vector d_sortl(nnodes + 1, 0); - int *sortl = d_sortl.data().get(); - - // RepulsionKernel - rmm::device_vector d_rep_forces((nnodes + 1) * 2, 0); - float *rep_forces = d_rep_forces.data().get(); - - rmm::device_vector d_radius_squared(1, 0); - float *radiusd_squared = d_radius_squared.data().get(); - - rmm::device_vector d_nodes_pos((nnodes + 1) * 2, 0); - float *nodes_pos = d_nodes_pos.data().get(); - - // Initialize positions with random values - int random_state = 0; - - // Copy start x and y positions. - if (x_start && y_start) { - copy(n, x_start, nodes_pos); - copy(n, y_start, nodes_pos + nnodes + 1); - } else { - random_vector(nodes_pos, (nnodes + 1) * 2, random_state, stream); - } - - // Allocate arrays for force computation - float *attract{nullptr}; - float *old_forces{nullptr}; - float *swinging{nullptr}; - float *traction{nullptr}; - - rmm::device_vector d_attract(n * 2, 0); - rmm::device_vector d_old_forces(n * 2, 0); - rmm::device_vector d_swinging(n, 0); - rmm::device_vector d_traction(n, 0); - - attract = d_attract.data().get(); - old_forces = d_old_forces.data().get(); - swinging = d_swinging.data().get(); - traction = d_traction.data().get(); - - // Sort COO for coalesced memory access. - sort(graph, stream); - CHECK_CUDA(stream); - - graph.degree(massl, cugraph::DegreeDirection::OUT); - CHECK_CUDA(stream); - - const vertex_t *row = graph.src_indices; - const vertex_t *col = graph.dst_indices; - const weight_t *v = graph.edge_data; - - // Scalars used to adapt global speed. - float speed = 1.f; - float speed_efficiency = 1.f; - float outbound_att_compensation = 1.f; - float jt = 0.f; - - // If outboundAttractionDistribution active, compensate. - if (outbound_attraction_distribution) { - int sum = - thrust::reduce(rmm::exec_policy(stream)->on(stream), d_massl.begin(), d_massl.begin() + n); - outbound_att_compensation = sum / (float)n; - } - - // - // Set cache levels for faster algorithm execution - //--------------------------------------------------- - cudaFuncSetCacheConfig(BoundingBoxKernel, cudaFuncCachePreferShared); - cudaFuncSetCacheConfig(TreeBuildingKernel, cudaFuncCachePreferL1); - cudaFuncSetCacheConfig(ClearKernel1, cudaFuncCachePreferL1); - cudaFuncSetCacheConfig(ClearKernel2, cudaFuncCachePreferL1); - cudaFuncSetCacheConfig(SummarizationKernel, cudaFuncCachePreferShared); - cudaFuncSetCacheConfig(SortKernel, cudaFuncCachePreferL1); - cudaFuncSetCacheConfig(RepulsionKernel, cudaFuncCachePreferL1); - cudaFuncSetCacheConfig(apply_forces_bh, cudaFuncCachePreferL1); - - if (callback) { - callback->setup(nnodes + 1, 2); - callback->on_preprocess_end(nodes_pos); - } - - for (int iter = 0; iter < max_iter; ++iter) { - // Reset force values - fill((nnodes + 1) * 2, rep_forces, 0.f); - fill(n * 2, attract, 0.f); - fill(n, swinging, 0.f); - fill(n, traction, 0.f); - - ResetKernel<<<1, 1, 0, stream>>>(radiusd_squared, bottomd, NNODES, radiusd); - CHECK_CUDA(stream); - - // Compute bounding box arround all bodies - BoundingBoxKernel<<>>(startl, - childl, - massl, - nodes_pos, - nodes_pos + nnodes + 1, - maxxl, - maxyl, - minxl, - minyl, - FOUR_NNODES, - NNODES, - n, - limiter, - radiusd); - CHECK_CUDA(stream); - - ClearKernel1<<>>(childl, FOUR_NNODES, FOUR_N); - CHECK_CUDA(stream); - - // Build quadtree - TreeBuildingKernel<<>>( - childl, nodes_pos, nodes_pos + nnodes + 1, NNODES, n, maxdepthd, bottomd, radiusd); - CHECK_CUDA(stream); - - ClearKernel2<<>>(startl, massl, NNODES, bottomd); - CHECK_CUDA(stream); - - // Summarizes mass and position for each cell, bottom up approach - SummarizationKernel<<>>( - countl, childl, massl, nodes_pos, nodes_pos + nnodes + 1, NNODES, n, bottomd); - CHECK_CUDA(stream); - - // Group closed bodies together, used to speed up Repulsion kernel - SortKernel<<>>( - sortl, countl, startl, childl, NNODES, n, bottomd); - CHECK_CUDA(stream); - - // Force computation O(n . log(n)) - RepulsionKernel<<>>(scaling_ratio, - theta, - epssq, - sortl, - childl, - massl, - nodes_pos, - nodes_pos + nnodes + 1, - rep_forces, - rep_forces + nnodes + 1, - theta_squared, - NNODES, - FOUR_NNODES, - n, - radiusd_squared, - maxdepthd); - CHECK_CUDA(stream); - - apply_gravity(nodes_pos, - nodes_pos + nnodes + 1, - attract, - attract + n, - massl, - gravity, - strong_gravity_mode, - scaling_ratio, - n, - stream); - - apply_attraction(row, - col, - v, - e, - nodes_pos, - nodes_pos + nnodes + 1, - attract, - attract + n, - massl, - outbound_attraction_distribution, - lin_log_mode, - edge_weight_influence, - outbound_att_compensation, - stream); - - compute_local_speed(rep_forces, - rep_forces + nnodes + 1, - attract, - attract + n, - old_forces, - old_forces + n, - massl, - swinging, - traction, - n, - stream); - - // Compute global swinging and traction values - const float s = - thrust::reduce(rmm::exec_policy(stream)->on(stream), d_swinging.begin(), d_swinging.end()); - - const float t = - thrust::reduce(rmm::exec_policy(stream)->on(stream), d_traction.begin(), d_traction.end()); - - // Compute global speed based on gloab and local swinging and traction. - adapt_speed(jitter_tolerance, &jt, &speed, &speed_efficiency, s, t, n); - - // Update positions - apply_forces_bh<<>>(nodes_pos, - nodes_pos + nnodes + 1, - attract, - attract + n, - rep_forces, - rep_forces + nnodes + 1, - old_forces, - old_forces + n, - swinging, - speed, - n); - - if (callback) callback->on_epoch_end(nodes_pos); - - if (verbose) { - printf("iteration %i, speed: %f, speed_efficiency: %f, ", iter + 1, speed, speed_efficiency); - printf("jt: %f, ", jt); - printf("swinging: %f, traction: %f\n", s, t); - } - } - - // Copy nodes positions into final output pos - copy(n, nodes_pos, pos); - copy(n, nodes_pos + nnodes + 1, pos + n); - - if (callback) callback->on_train_end(nodes_pos); -} - -} // namespace detail -} // namespace cugraph diff --git a/cpp/src/layout/bh_kernels.hpp b/cpp/src/layout/bh_kernels.cuh similarity index 88% rename from cpp/src/layout/bh_kernels.hpp rename to cpp/src/layout/bh_kernels.cuh index 5c170e147c9..8dbc08a6440 100644 --- a/cpp/src/layout/bh_kernels.hpp +++ b/cpp/src/layout/bh_kernels.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -42,9 +42,9 @@ namespace detail { /** * Intializes the states of objects. This speeds the overall kernel up. */ -__global__ void InitializationKernel(unsigned *restrict limiter, - int *restrict maxdepthd, - float *restrict radiusd) +__global__ void InitializationKernel(unsigned* restrict limiter, + int* restrict maxdepthd, + float* restrict radiusd) { maxdepthd[0] = 1; limiter[0] = 0; @@ -54,10 +54,10 @@ __global__ void InitializationKernel(unsigned *restrict limiter, /** * Reset root. */ -__global__ void ResetKernel(float *restrict radiusd_squared, - int *restrict bottomd, +__global__ void ResetKernel(float* restrict radiusd_squared, + int* restrict bottomd, const int NNODES, - const float *restrict radiusd) + const float* restrict radiusd) { radiusd_squared[0] = radiusd[0] * radiusd[0]; // create root node @@ -67,20 +67,20 @@ __global__ void ResetKernel(float *restrict radiusd_squared, /** * Figures the bounding boxes for every point in the embedding. */ -__global__ __launch_bounds__(THREADS1, FACTOR1) void BoundingBoxKernel(int *restrict startd, - int *restrict childd, - int *restrict massd, - float *restrict posxd, - float *restrict posyd, - float *restrict maxxd, - float *restrict maxyd, - float *restrict minxd, - float *restrict minyd, +__global__ __launch_bounds__(THREADS1, FACTOR1) void BoundingBoxKernel(int* restrict startd, + int* restrict childd, + int* restrict massd, + float* restrict posxd, + float* restrict posyd, + float* restrict maxxd, + float* restrict maxyd, + float* restrict minxd, + float* restrict minyd, const int FOUR_NNODES, const int NNODES, const int N, - unsigned *restrict limiter, - float *restrict radiusd) + unsigned* restrict limiter, + float* restrict radiusd) { float val, minx, maxx, miny, maxy; __shared__ float sminx[THREADS1], smaxx[THREADS1], sminy[THREADS1], smaxy[THREADS1]; @@ -150,14 +150,15 @@ __global__ __launch_bounds__(THREADS1, FACTOR1) void BoundingBoxKernel(int *rest posyd[NNODES] = (miny + maxy) * 0.5f; #pragma unroll - for (int a = 0; a < 4; a++) childd[FOUR_NNODES + a] = -1; + for (int a = 0; a < 4; a++) + childd[FOUR_NNODES + a] = -1; } } /** * Clear some of the state vectors up. */ -__global__ __launch_bounds__(1024, 1) void ClearKernel1(int *restrict childd, +__global__ __launch_bounds__(1024, 1) void ClearKernel1(int* restrict childd, const int FOUR_NNODES, const int FOUR_N) { @@ -167,21 +168,22 @@ __global__ __launch_bounds__(1024, 1) void ClearKernel1(int *restrict childd, // iterate over all cells assigned to thread #pragma unroll - for (; k < FOUR_NNODES; k += inc) childd[k] = -1; + for (; k < FOUR_NNODES; k += inc) + childd[k] = -1; } /** * Build the actual KD Tree. */ __global__ __launch_bounds__(THREADS2, - FACTOR2) void TreeBuildingKernel(int *restrict childd, - const float *restrict posxd, - const float *restrict posyd, + FACTOR2) void TreeBuildingKernel(int* restrict childd, + const float* restrict posxd, + const float* restrict posyd, const int NNODES, const int N, - int *restrict maxdepthd, - int *restrict bottomd, - const float *restrict radiusd) + int* restrict maxdepthd, + int* restrict bottomd, + const float* restrict radiusd) { int j, depth; float x, y, r; @@ -294,10 +296,10 @@ __global__ __launch_bounds__(THREADS2, /** * Clean more state vectors. */ -__global__ __launch_bounds__(1024, 1) void ClearKernel2(int *restrict startd, - int *restrict massd, +__global__ __launch_bounds__(1024, 1) void ClearKernel2(int* restrict startd, + int* restrict massd, const int NNODES, - const int *restrict bottomd) + const int* restrict bottomd) { const int bottom = bottomd[0]; const int inc = blockDim.x * gridDim.x; @@ -316,14 +318,14 @@ __global__ __launch_bounds__(1024, 1) void ClearKernel2(int *restrict startd, * Summarize the KD Tree via cell gathering */ __global__ __launch_bounds__(THREADS3, - FACTOR3) void SummarizationKernel(int *restrict countd, - const int *restrict childd, - volatile int *restrict massd, - float *restrict posxd, - float *restrict posyd, + FACTOR3) void SummarizationKernel(int* restrict countd, + const int* restrict childd, + volatile int* restrict massd, + float* restrict posxd, + float* restrict posyd, const int NNODES, const int N, - const int *restrict bottomd) + const int* restrict bottomd) { bool flag = 0; float cm, px, py; @@ -451,13 +453,13 @@ __global__ __launch_bounds__(THREADS3, /** * Sort the cells */ -__global__ __launch_bounds__(THREADS4, FACTOR4) void SortKernel(int *restrict sortd, - const int *restrict countd, - volatile int *restrict startd, - int *restrict childd, +__global__ __launch_bounds__(THREADS4, FACTOR4) void SortKernel(int* restrict sortd, + const int* restrict countd, + volatile int* restrict startd, + int* restrict childd, const int NNODES, const int N, - const int *restrict bottomd) + const int* restrict bottomd) { const int bottom = bottomd[0]; const int dec = blockDim.x * gridDim.x; @@ -505,19 +507,19 @@ __global__ __launch_bounds__( const float scaling_ratio, const float theta, const float epssqd, // correction for zero distance - const int *restrict sortd, - const int *restrict childd, - const int *restrict massd, - const float *restrict posxd, - const float *restrict posyd, - float *restrict velxd, - float *restrict velyd, + const int* restrict sortd, + const int* restrict childd, + const int* restrict massd, + const float* restrict posxd, + const float* restrict posyd, + float* restrict velxd, + float* restrict velyd, const float theta_squared, const int NNODES, const int FOUR_NNODES, const int N, - const float *restrict radiusd_squared, - const int *restrict maxdepthd) + const float* restrict radiusd_squared, + const int* restrict maxdepthd) { __shared__ int pos[THREADS5], node[THREADS5]; __shared__ float dq[THREADS5]; @@ -611,15 +613,15 @@ __global__ __launch_bounds__( } __global__ __launch_bounds__(THREADS6, - FACTOR6) void apply_forces_bh(float *restrict Y_x, - float *restrict Y_y, - const float *restrict attract_x, - const float *restrict attract_y, - const float *restrict repel_x, - const float *restrict repel_y, - float *restrict old_dx, - float *restrict old_dy, - const float *restrict swinging, + FACTOR6) void apply_forces_bh(float* restrict Y_x, + float* restrict Y_y, + const float* restrict attract_x, + const float* restrict attract_y, + const float* restrict repel_x, + const float* restrict repel_y, + float* restrict old_dx, + float* restrict old_dy, + const float* restrict swinging, const float speed, const int n) { diff --git a/cpp/src/layout/exact_fa2.hpp b/cpp/src/layout/exact_fa2.cuh similarity index 55% rename from cpp/src/layout/exact_fa2.hpp rename to cpp/src/layout/exact_fa2.cuh index 0b90e417968..5b5c3f5e82e 100644 --- a/cpp/src/layout/exact_fa2.hpp +++ b/cpp/src/layout/exact_fa2.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -16,27 +16,30 @@ #pragma once -#include -#include +#include +#include -#include #include -#include -#include -#include "exact_repulsion.hpp" -#include "fa2_kernels.hpp" +#include +#include +#include +#include + +#include "exact_repulsion.cuh" +#include "fa2_kernels.cuh" #include "utils.hpp" namespace cugraph { namespace detail { template -void exact_fa2(GraphCOOView &graph, - float *pos, +void exact_fa2(raft::handle_t const& handle, + legacy::GraphCOOView& graph, + float* pos, const int max_iter = 500, - float *x_start = nullptr, - float *y_start = nullptr, + float* x_start = nullptr, + float* y_start = nullptr, bool outbound_attraction_distribution = true, bool lin_log_mode = false, bool prevent_overlapping = false, @@ -46,52 +49,53 @@ void exact_fa2(GraphCOOView &graph, bool strong_gravity_mode = false, const float gravity = 1.0, bool verbose = false, - internals::GraphBasedDimRedCallback *callback = nullptr) + internals::GraphBasedDimRedCallback* callback = nullptr) { - cudaStream_t stream = {nullptr}; - const edge_t e = graph.number_of_edges; - const vertex_t n = graph.number_of_vertices; - - float *d_repel{nullptr}; - float *d_attract{nullptr}; - float *d_old_forces{nullptr}; - int *d_mass{nullptr}; - float *d_swinging{nullptr}; - float *d_traction{nullptr}; - - rmm::device_vector repel(n * 2, 0); - rmm::device_vector attract(n * 2, 0); - rmm::device_vector old_forces(n * 2, 0); + auto stream_view = handle.get_stream_view(); + const edge_t e = graph.number_of_edges; + const vertex_t n = graph.number_of_vertices; + + float* d_repel{nullptr}; + float* d_attract{nullptr}; + float* d_old_forces{nullptr}; + int* d_mass{nullptr}; + float* d_swinging{nullptr}; + float* d_traction{nullptr}; + + rmm::device_uvector repel(n * 2, stream_view); + rmm::device_uvector attract(n * 2, stream_view); + rmm::device_uvector old_forces(n * 2, stream_view); + thrust::fill(rmm::exec_policy(stream_view), old_forces.begin(), old_forces.end(), 0.f); // FA2 requires degree + 1. - rmm::device_vector mass(n, 1); - rmm::device_vector swinging(n, 0); - rmm::device_vector traction(n, 0); + rmm::device_uvector mass(n, stream_view); + thrust::fill(rmm::exec_policy(stream_view), mass.begin(), mass.end(), 1); + rmm::device_uvector swinging(n, stream_view); + rmm::device_uvector traction(n, stream_view); - d_repel = repel.data().get(); - d_attract = attract.data().get(); - d_old_forces = old_forces.data().get(); - d_mass = mass.data().get(); - d_swinging = swinging.data().get(); - d_traction = traction.data().get(); + d_repel = repel.data(); + d_attract = attract.data(); + d_old_forces = old_forces.data(); + d_mass = mass.data(); + d_swinging = swinging.data(); + d_traction = traction.data(); - int random_state = 0; - random_vector(pos, n * 2, random_state, stream); + uniform_random_fill(handle.get_stream_view(), pos, n * 2, -100.0f, 100.0f, uint64_t{0}); if (x_start && y_start) { - copy(n, x_start, pos); - copy(n, y_start, pos + n); + raft::copy(pos, x_start, n, stream_view.value()); + raft::copy(pos + n, y_start, n, stream_view.value()); } // Sort COO for coalesced memory access. - sort(graph, stream); - CHECK_CUDA(stream); + sort(graph, stream_view.value()); + CHECK_CUDA(stream_view.value()); - graph.degree(d_mass, cugraph::DegreeDirection::OUT); - CHECK_CUDA(stream); + graph.degree(d_mass, cugraph::legacy::DegreeDirection::OUT); + CHECK_CUDA(stream_view.value()); - const vertex_t *row = graph.src_indices; - const vertex_t *col = graph.dst_indices; - const weight_t *v = graph.edge_data; + const vertex_t* row = graph.src_indices; + const vertex_t* col = graph.dst_indices; + const weight_t* v = graph.edge_data; float speed = 1.f; float speed_efficiency = 1.f; @@ -99,7 +103,7 @@ void exact_fa2(GraphCOOView &graph, float jt = 0.f; if (outbound_attraction_distribution) { - int sum = thrust::reduce(rmm::exec_policy(stream)->on(stream), mass.begin(), mass.end()); + int sum = thrust::reduce(rmm::exec_policy(stream_view), mass.begin(), mass.end()); outbound_att_compensation = sum / (float)n; } @@ -110,13 +114,14 @@ void exact_fa2(GraphCOOView &graph, for (int iter = 0; iter < max_iter; ++iter) { // Reset force arrays - fill(n * 2, d_repel, 0.f); - fill(n * 2, d_attract, 0.f); - fill(n, d_swinging, 0.f); - fill(n, d_traction, 0.f); + thrust::fill(rmm::exec_policy(stream_view), repel.begin(), repel.end(), 0.f); + thrust::fill(rmm::exec_policy(stream_view), attract.begin(), attract.end(), 0.f); + thrust::fill(rmm::exec_policy(stream_view), swinging.begin(), swinging.end(), 0.f); + thrust::fill(rmm::exec_policy(stream_view), traction.begin(), traction.end(), 0.f); // Exact repulsion - apply_repulsion(pos, pos + n, d_repel, d_repel + n, d_mass, scaling_ratio, n, stream); + apply_repulsion( + pos, pos + n, d_repel, d_repel + n, d_mass, scaling_ratio, n, stream_view.value()); apply_gravity(pos, pos + n, @@ -127,7 +132,7 @@ void exact_fa2(GraphCOOView &graph, strong_gravity_mode, scaling_ratio, n, - stream); + stream_view.value()); apply_attraction(row, col, @@ -142,7 +147,7 @@ void exact_fa2(GraphCOOView &graph, lin_log_mode, edge_weight_influence, outbound_att_compensation, - stream); + stream_view.value()); compute_local_speed(d_repel, d_repel + n, @@ -154,13 +159,11 @@ void exact_fa2(GraphCOOView &graph, d_swinging, d_traction, n, - stream); + stream_view.value()); // Compute global swinging and traction values. - const float s = - thrust::reduce(rmm::exec_policy(stream)->on(stream), swinging.begin(), swinging.end()); - const float t = - thrust::reduce(rmm::exec_policy(stream)->on(stream), traction.begin(), traction.end()); + const float s = thrust::reduce(rmm::exec_policy(stream_view), swinging.begin(), swinging.end()); + const float t = thrust::reduce(rmm::exec_policy(stream_view), traction.begin(), traction.end()); adapt_speed(jitter_tolerance, &jt, &speed, &speed_efficiency, s, t, n); @@ -175,14 +178,14 @@ void exact_fa2(GraphCOOView &graph, d_swinging, speed, n, - stream); + stream_view.value()); if (callback) callback->on_epoch_end(pos); if (verbose) { - printf("iteration %i, speed: %f, speed_efficiency: %f, ", iter + 1, speed, speed_efficiency); - printf("jt: %f, ", jt); - printf("swinging: %f, traction: %f\n", s, t); + std::cout << "iteration: " << iter + 1 << ", speed: " << speed + << ", speed_efficiency: " << speed_efficiency << ", jt: " << jt + << ", swinging: " << s << ", traction: " << t << "\n"; } } diff --git a/cpp/src/layout/exact_repulsion.hpp b/cpp/src/layout/exact_repulsion.cuh similarity index 77% rename from cpp/src/layout/exact_repulsion.hpp rename to cpp/src/layout/exact_repulsion.cuh index 583d5c81e30..49b4f46c5c3 100644 --- a/cpp/src/layout/exact_repulsion.hpp +++ b/cpp/src/layout/exact_repulsion.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -15,18 +15,18 @@ */ #pragma once -#define restrict __restrict__ +#define restrict __restrict__ #define CUDA_MAX_BLOCKS_2D 256 namespace cugraph { namespace detail { template -__global__ void repulsion_kernel(const float *restrict x_pos, - const float *restrict y_pos, - float *restrict repel_x, - float *restrict repel_y, - const int *restrict mass, +__global__ void repulsion_kernel(const float* restrict x_pos, + const float* restrict y_pos, + float* restrict repel_x, + float* restrict repel_y, + const int* restrict mass, const float scaling_ratio, const vertex_t n) { @@ -50,11 +50,11 @@ __global__ void repulsion_kernel(const float *restrict x_pos, } template -void apply_repulsion(const float *restrict x_pos, - const float *restrict y_pos, - float *restrict repel_x, - float *restrict repel_y, - const int *restrict mass, +void apply_repulsion(const float* restrict x_pos, + const float* restrict y_pos, + float* restrict repel_x, + float* restrict repel_y, + const int* restrict mass, const float scaling_ratio, const vertex_t n, cudaStream_t stream) diff --git a/cpp/src/layout/fa2_kernels.hpp b/cpp/src/layout/fa2_kernels.cuh similarity index 67% rename from cpp/src/layout/fa2_kernels.hpp rename to cpp/src/layout/fa2_kernels.cuh index 0c7e9b1d193..c46b249628b 100644 --- a/cpp/src/layout/fa2_kernels.hpp +++ b/cpp/src/layout/fa2_kernels.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -17,21 +17,21 @@ #pragma once #define restrict __restrict__ -#include "utilities/graph_utils.cuh" +#include namespace cugraph { namespace detail { template -__global__ void attraction_kernel(const vertex_t *restrict row, - const vertex_t *restrict col, - const weight_t *restrict v, +__global__ void attraction_kernel(const vertex_t* restrict row, + const vertex_t* restrict col, + const weight_t* restrict v, const edge_t e, - const float *restrict x_pos, - const float *restrict y_pos, - float *restrict attract_x, - float *restrict attract_y, - const int *restrict mass, + const float* restrict x_pos, + const float* restrict y_pos, + float* restrict attract_x, + float* restrict attract_y, + const int* restrict mass, bool outbound_attraction_distribution, bool lin_log_mode, const float edge_weight_influence, @@ -71,15 +71,15 @@ __global__ void attraction_kernel(const vertex_t *restrict row, } template -void apply_attraction(const vertex_t *restrict row, - const vertex_t *restrict col, - const weight_t *restrict v, +void apply_attraction(const vertex_t* restrict row, + const vertex_t* restrict col, + const weight_t* restrict v, const edge_t e, - const float *restrict x_pos, - const float *restrict y_pos, - float *restrict attract_x, - float *restrict attract_y, - const int *restrict mass, + const float* restrict x_pos, + const float* restrict y_pos, + float* restrict attract_x, + float* restrict attract_y, + const int* restrict mass, bool outbound_attraction_distribution, bool lin_log_mode, const float edge_weight_influence, @@ -116,11 +116,11 @@ void apply_attraction(const vertex_t *restrict row, } template -__global__ void linear_gravity_kernel(const float *restrict x_pos, - const float *restrict y_pos, - float *restrict attract_x, - float *restrict attract_y, - const int *restrict mass, +__global__ void linear_gravity_kernel(const float* restrict x_pos, + const float* restrict y_pos, + float* restrict attract_x, + float* restrict attract_y, + const int* restrict mass, const float gravity, const vertex_t n) { @@ -136,11 +136,11 @@ __global__ void linear_gravity_kernel(const float *restrict x_pos, } template -__global__ void strong_gravity_kernel(const float *restrict x_pos, - const float *restrict y_pos, - float *restrict attract_x, - float *restrict attract_y, - const int *restrict mass, +__global__ void strong_gravity_kernel(const float* restrict x_pos, + const float* restrict y_pos, + float* restrict attract_x, + float* restrict attract_y, + const int* restrict mass, const float gravity, const float scaling_ratio, const vertex_t n) @@ -157,11 +157,11 @@ __global__ void strong_gravity_kernel(const float *restrict x_pos, } template -void apply_gravity(const float *restrict x_pos, - const float *restrict y_pos, - float *restrict attract_x, - float *restrict attract_y, - const int *restrict mass, +void apply_gravity(const float* restrict x_pos, + const float* restrict y_pos, + float* restrict attract_x, + float* restrict attract_y, + const int* restrict mass, const float gravity, bool strong_gravity_mode, const float scaling_ratio, @@ -187,15 +187,15 @@ void apply_gravity(const float *restrict x_pos, } template -__global__ void local_speed_kernel(const float *restrict repel_x, - const float *restrict repel_y, - const float *restrict attract_x, - const float *restrict attract_y, - const float *restrict old_dx, - const float *restrict old_dy, - const int *restrict mass, - float *restrict swinging, - float *restrict traction, +__global__ void local_speed_kernel(const float* restrict repel_x, + const float* restrict repel_y, + const float* restrict attract_x, + const float* restrict attract_y, + const float* restrict old_dx, + const float* restrict old_dy, + const int* restrict mass, + float* restrict swinging, + float* restrict traction, const vertex_t n) { // For every node. @@ -210,15 +210,15 @@ __global__ void local_speed_kernel(const float *restrict repel_x, } template -void compute_local_speed(const float *restrict repel_x, - const float *restrict repel_y, - const float *restrict attract_x, - const float *restrict attract_y, - float *restrict old_dx, - float *restrict old_dy, - const int *restrict mass, - float *restrict swinging, - float *restrict traction, +void compute_local_speed(const float* restrict repel_x, + const float* restrict repel_y, + const float* restrict attract_x, + const float* restrict attract_y, + float* restrict old_dx, + float* restrict old_dy, + const int* restrict mass, + float* restrict swinging, + float* restrict traction, const vertex_t n, cudaStream_t stream) { @@ -237,9 +237,9 @@ void compute_local_speed(const float *restrict repel_x, template void adapt_speed(const float jitter_tolerance, - float *restrict jt, - float *restrict speed, - float *restrict speed_efficiency, + float* restrict jt, + float* restrict speed, + float* restrict speed_efficiency, const float s, const float t, const vertex_t n) @@ -272,15 +272,15 @@ void adapt_speed(const float jitter_tolerance, } template -__global__ void update_positions_kernel(float *restrict x_pos, - float *restrict y_pos, - const float *restrict repel_x, - const float *restrict repel_y, - const float *restrict attract_x, - const float *restrict attract_y, - float *restrict old_dx, - float *restrict old_dy, - const float *restrict swinging, +__global__ void update_positions_kernel(float* restrict x_pos, + float* restrict y_pos, + const float* restrict repel_x, + const float* restrict repel_y, + const float* restrict attract_x, + const float* restrict attract_y, + float* restrict old_dx, + float* restrict old_dy, + const float* restrict swinging, const float speed, const vertex_t n) { @@ -298,15 +298,15 @@ __global__ void update_positions_kernel(float *restrict x_pos, } template -void apply_forces(float *restrict x_pos, - float *restrict y_pos, - const float *restrict repel_x, - const float *restrict repel_y, - const float *restrict attract_x, - const float *restrict attract_y, - float *restrict old_dx, - float *restrict old_dy, - const float *restrict swinging, +void apply_forces(float* restrict x_pos, + float* restrict y_pos, + const float* restrict repel_x, + const float* restrict repel_y, + const float* restrict attract_x, + const float* restrict attract_y, + float* restrict old_dx, + float* restrict old_dy, + const float* restrict swinging, const float speed, const vertex_t n, cudaStream_t stream) diff --git a/cpp/src/layout/force_atlas2.cu b/cpp/src/layout/force_atlas2.cu index ef00f504d86..9dc4ee286b0 100644 --- a/cpp/src/layout/force_atlas2.cu +++ b/cpp/src/layout/force_atlas2.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -14,17 +14,18 @@ * limitations under the License. */ -#include "barnes_hut.hpp" -#include "exact_fa2.hpp" +#include "barnes_hut.cuh" +#include "exact_fa2.cuh" namespace cugraph { template -void force_atlas2(GraphCOOView &graph, - float *pos, +void force_atlas2(raft::handle_t const& handle, + legacy::GraphCOOView& graph, + float* pos, const int max_iter, - float *x_start, - float *y_start, + float* x_start, + float* y_start, bool outbound_attraction_distribution, bool lin_log_mode, bool prevent_overlapping, @@ -36,13 +37,14 @@ void force_atlas2(GraphCOOView &graph, bool strong_gravity_mode, const float gravity, bool verbose, - internals::GraphBasedDimRedCallback *callback) + internals::GraphBasedDimRedCallback* callback) { - CUGRAPH_EXPECTS(pos != nullptr, "Invalid API parameter: pos array should be of size 2 * V"); + CUGRAPH_EXPECTS(pos != nullptr, "Invalid input argument: pos array should be of size 2 * V"); CUGRAPH_EXPECTS(graph.number_of_vertices != 0, "Invalid input: Graph is empty"); if (!barnes_hut_optimize) { - cugraph::detail::exact_fa2(graph, + cugraph::detail::exact_fa2(handle, + graph, pos, max_iter, x_start, @@ -58,7 +60,8 @@ void force_atlas2(GraphCOOView &graph, verbose, callback); } else { - cugraph::detail::barnes_hut(graph, + cugraph::detail::barnes_hut(handle, + graph, pos, max_iter, x_start, @@ -77,11 +80,12 @@ void force_atlas2(GraphCOOView &graph, } } -template void force_atlas2(GraphCOOView &graph, - float *pos, +template void force_atlas2(raft::handle_t const& handle, + legacy::GraphCOOView& graph, + float* pos, const int max_iter, - float *x_start, - float *y_start, + float* x_start, + float* y_start, bool outbound_attraction_distribution, bool lin_log_mode, bool prevent_overlapping, @@ -93,13 +97,14 @@ template void force_atlas2(GraphCOOView &graph bool strong_gravity_mode, const float gravity, bool verbose, - internals::GraphBasedDimRedCallback *callback); + internals::GraphBasedDimRedCallback* callback); -template void force_atlas2(GraphCOOView &graph, - float *pos, +template void force_atlas2(raft::handle_t const& handle, + legacy::GraphCOOView& graph, + float* pos, const int max_iter, - float *x_start, - float *y_start, + float* x_start, + float* y_start, bool outbound_attraction_distribution, bool lin_log_mode, bool prevent_overlapping, @@ -111,6 +116,6 @@ template void force_atlas2(GraphCOOView &gra bool strong_gravity_mode, const float gravity, bool verbose, - internals::GraphBasedDimRedCallback *callback); + internals::GraphBasedDimRedCallback* callback); } // namespace cugraph diff --git a/cpp/src/layout/utils.hpp b/cpp/src/layout/utils.hpp index 335b8ea986c..ffbeb291e58 100644 --- a/cpp/src/layout/utils.hpp +++ b/cpp/src/layout/utils.hpp @@ -1,5 +1,5 @@ /* - * 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. @@ -18,27 +18,9 @@ #include -#include - namespace cugraph { namespace detail { -struct prg { - __host__ __device__ float operator()(int n) - { - thrust::default_random_engine rng; - thrust::uniform_real_distribution dist(-100.f, 100.f); - rng.discard(n); - return dist(rng); - } -}; - -void random_vector(float *vec, int n, int seed, cudaStream_t stream) -{ - thrust::counting_iterator index(seed); - thrust::transform(rmm::exec_policy(stream)->on(stream), index, index + n, vec, prg()); -} - /** helper method to get multi-processor count parameter */ inline int getMultiProcessorCount() { diff --git a/cpp/src/linear_assignment/README-hungarian.md b/cpp/src/linear_assignment/README-hungarian.md new file mode 100644 index 00000000000..42dabd7cfbc --- /dev/null +++ b/cpp/src/linear_assignment/README-hungarian.md @@ -0,0 +1,36 @@ +# LAP +Implementation of ***O(n^3) Alternating Tree Variant*** of Hungarian Algorithm on NVIDIA CUDA-enabled GPU. + +This implementation solves a batch of ***k*** **Linear Assignment Problems (LAP)**, each with ***nxn*** matrix of single floating point cost values. At optimality, the algorithm produces an assignment with ***minimum*** cost. + +The API can be used to query optimal primal and dual costs, optimal assignment vector, and optimal row/column dual vectors for each subproblem in the batch. + +cuGraph exposes the Hungarian algorithm, the actual implementation is contained in the RAFT library which contains some common tools and kernels shared between cuGraph and cuML. + +Following parameters can be used to tune the performance of algorithm: + +1. epsilon: (in raft/lap/lap_kernels.cuh) This parameter controls the tolerance on the floating point precision. Setting this too small will result in increased solution time because the algorithm will search for precise solutions. Setting it too high may cause some inaccuracies. + +2. BLOCKDIMX, BLOCKDIMY: (in raft/lap/lap_functions.cuh) These parameters control threads_per_block to be used along the given dimension. Set these according to the device specifications and occupancy calculation. + +***This library is licensed under Apache License 2.0. Please cite our paper, if this library helps you in your research.*** + +- Harvard citation style + + Date, K. and Nagi, R., 2016. GPU-accelerated Hungarian algorithms for the Linear Assignment Problem. Parallel Computing, 57, pp.52-72. + +- BibTeX Citation block to be used in LaTeX bibliography file: + +``` +@article{date2016gpu, + title={GPU-accelerated Hungarian algorithms for the Linear Assignment Problem}, + author={Date, Ketan and Nagi, Rakesh}, + journal={Parallel Computing}, + volume={57}, + pages={52--72}, + year={2016}, + publisher={Elsevier} +} +``` + +The paper is available online on [ScienceDirect](https://www.sciencedirect.com/science/article/abs/pii/S016781911630045X). diff --git a/cpp/src/linear_assignment/hungarian.cu b/cpp/src/linear_assignment/hungarian.cu index 164a386c6dd..368e119e93c 100644 --- a/cpp/src/linear_assignment/hungarian.cu +++ b/cpp/src/linear_assignment/hungarian.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -13,18 +13,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include -#include +#include +#include + +#include -#include -#include +#include +#include +#include #include #include -#include - -#include +#include +#include //#define TIMING @@ -35,41 +37,95 @@ namespace cugraph { namespace detail { -template -weight_t hungarian(raft::handle_t const &handle, - index_t num_rows, - index_t num_cols, - weight_t const *d_original_cost, - index_t *d_assignment, - cudaStream_t stream) +template +weight_t default_epsilon() { - // - // TODO: Can Date/Nagi implementation in raft handle rectangular matrices? - // - CUGRAPH_EXPECTS(num_rows == num_cols, "Current implementation only supports square matrices"); - - rmm::device_vector col_assignments_v(num_rows); + return 0; +} - // Create an instance of LinearAssignmentProblem using problem size, number of subproblems - raft::lap::LinearAssignmentProblem lpx(handle, num_rows, 1); +template <> +float default_epsilon() +{ + return float{1e-6}; +} - // Solve LAP(s) for given cost matrix - lpx.solve(d_original_cost, d_assignment, col_assignments_v.data().get()); +template <> +double default_epsilon() +{ + return double{1e-6}; +} - return lpx.getPrimalObjectiveValue(0); +template +weight_t hungarian(raft::handle_t const& handle, + index_t num_rows, + index_t num_cols, + weight_t const* d_original_cost, + index_t* d_assignment, + weight_t epsilon) +{ + if (num_rows == num_cols) { + rmm::device_uvector col_assignments_v(num_rows, handle.get_stream_view()); + + // Create an instance of LinearAssignmentProblem using problem size, number of subproblems + raft::lap::LinearAssignmentProblem lpx(handle, num_rows, 1, epsilon); + + // Solve LAP(s) for given cost matrix + lpx.solve(d_original_cost, d_assignment, col_assignments_v.data()); + + return lpx.getPrimalObjectiveValue(0); + } else { + // + // Create a square matrix, copy d_original_cost into it. + // Fill the extra rows/columns with max(d_original_cost) + // + index_t n = std::max(num_rows, num_cols); + weight_t max_cost = thrust::reduce(rmm::exec_policy(handle.get_stream_view()), + d_original_cost, + d_original_cost + (num_rows * num_cols), + weight_t{0}, + thrust::maximum()); + + rmm::device_uvector tmp_cost_v(n * n, handle.get_stream_view()); + rmm::device_uvector tmp_row_assignment_v(n, handle.get_stream_view()); + rmm::device_uvector tmp_col_assignment_v(n, handle.get_stream_view()); + + thrust::transform(rmm::exec_policy(handle.get_stream_view()), + thrust::make_counting_iterator(0), + thrust::make_counting_iterator(n * n), + tmp_cost_v.begin(), + [max_cost, d_original_cost, n, num_rows, num_cols] __device__(index_t i) { + index_t row = i / n; + index_t col = i % n; + + return ((row < num_rows) && (col < num_cols)) + ? d_original_cost[row * num_cols + col] + : max_cost; + }); + + raft::lap::LinearAssignmentProblem lpx(handle, n, 1, epsilon); + + // Solve LAP(s) for given cost matrix + lpx.solve(tmp_cost_v.begin(), tmp_row_assignment_v.begin(), tmp_col_assignment_v.begin()); + + weight_t tmp_objective_value = lpx.getPrimalObjectiveValue(0); + + raft::copy(d_assignment, tmp_row_assignment_v.begin(), num_rows, handle.get_stream()); + + return tmp_objective_value - max_cost * std::abs(num_rows - num_cols); + } } template -weight_t hungarian_sparse(raft::handle_t const &handle, - GraphCOOView const &graph, +weight_t hungarian_sparse(raft::handle_t const& handle, + legacy::GraphCOOView const& graph, vertex_t num_workers, - vertex_t const *workers, - vertex_t *assignment, - cudaStream_t stream) + vertex_t const* workers, + vertex_t* assignment, + weight_t epsilon) { - CUGRAPH_EXPECTS(assignment != nullptr, "Invalid API parameter: assignment pointer is NULL"); + CUGRAPH_EXPECTS(assignment != nullptr, "Invalid input argument: assignment pointer is NULL"); CUGRAPH_EXPECTS(graph.edge_data != nullptr, - "Invalid API parameter: graph must have edge data (costs)"); + "Invalid input argument: graph must have edge data (costs)"); #ifdef TIMING HighResTimer hr_timer; @@ -86,63 +142,68 @@ weight_t hungarian_sparse(raft::handle_t const &handle, vertex_t matrix_dimension = std::max(num_rows, num_cols); - rmm::device_vector cost_v(matrix_dimension * matrix_dimension); - rmm::device_vector tasks_v(num_cols); - rmm::device_vector temp_tasks_v(graph.number_of_vertices); - rmm::device_vector temp_workers_v(graph.number_of_vertices); + rmm::device_uvector cost_v(matrix_dimension * matrix_dimension, + handle.get_stream_view()); + rmm::device_uvector tasks_v(num_cols, handle.get_stream_view()); + rmm::device_uvector temp_tasks_v(graph.number_of_vertices, handle.get_stream_view()); + rmm::device_uvector temp_workers_v(graph.number_of_vertices, handle.get_stream_view()); - weight_t *d_cost = cost_v.data().get(); - vertex_t *d_tasks = tasks_v.data().get(); - vertex_t *d_temp_tasks = temp_tasks_v.data().get(); - vertex_t *d_temp_workers = temp_workers_v.data().get(); - vertex_t *d_src_indices = graph.src_indices; - vertex_t *d_dst_indices = graph.dst_indices; - weight_t *d_edge_data = graph.edge_data; + weight_t* d_cost = cost_v.data(); + vertex_t* d_tasks = tasks_v.data(); + vertex_t* d_temp_tasks = temp_tasks_v.data(); + vertex_t* d_temp_workers = temp_workers_v.data(); + vertex_t* d_src_indices = graph.src_indices; + vertex_t* d_dst_indices = graph.dst_indices; + weight_t* d_edge_data = graph.edge_data; // // Renumber vertices internally. Workers will become // rows, tasks will become columns // - thrust::sequence(rmm::exec_policy(stream)->on(stream), temp_tasks_v.begin(), temp_tasks_v.end()); + thrust::sequence( + rmm::exec_policy(handle.get_stream_view()), temp_tasks_v.begin(), temp_tasks_v.end()); - thrust::for_each(rmm::exec_policy(stream)->on(stream), + thrust::for_each(rmm::exec_policy(handle.get_stream_view()), workers, workers + num_workers, [d_temp_tasks] __device__(vertex_t v) { d_temp_tasks[v] = -1; }); - auto temp_end = thrust::copy_if(rmm::exec_policy(stream)->on(stream), + auto temp_end = thrust::copy_if(rmm::exec_policy(handle.get_stream_view()), temp_tasks_v.begin(), temp_tasks_v.end(), d_tasks, [] __device__(vertex_t v) { return v >= 0; }); vertex_t size = thrust::distance(d_tasks, temp_end); - tasks_v.resize(size); + tasks_v.resize(size, handle.get_stream_view()); // // Now we'll assign costs into the dense array // - thrust::fill(rmm::exec_policy(stream)->on(stream), + thrust::fill(rmm::exec_policy(handle.get_stream_view()), temp_workers_v.begin(), temp_workers_v.end(), vertex_t{-1}); + thrust::fill(rmm::exec_policy(handle.get_stream_view()), + temp_tasks_v.begin(), + temp_tasks_v.end(), + vertex_t{-1}); thrust::fill( - rmm::exec_policy(stream)->on(stream), temp_tasks_v.begin(), temp_tasks_v.end(), vertex_t{-1}); - thrust::fill(rmm::exec_policy(stream)->on(stream), cost_v.begin(), cost_v.end(), weight_t{0}); + rmm::exec_policy(handle.get_stream_view()), cost_v.begin(), cost_v.end(), weight_t{0}); thrust::for_each( - rmm::exec_policy(stream)->on(stream), + rmm::exec_policy(handle.get_stream_view()), thrust::make_counting_iterator(0), thrust::make_counting_iterator(num_rows), [d_temp_workers, workers] __device__(vertex_t v) { d_temp_workers[workers[v]] = v; }); thrust::for_each( - rmm::exec_policy(stream)->on(stream), + rmm::exec_policy(handle.get_stream_view()), thrust::make_counting_iterator(0), thrust::make_counting_iterator(num_cols), [d_temp_tasks, d_tasks] __device__(vertex_t v) { d_temp_tasks[d_tasks[v]] = v; }); - thrust::for_each(rmm::exec_policy(stream)->on(stream), + thrust::for_each(rmm::exec_policy(handle.get_stream_view()), thrust::make_counting_iterator(0), thrust::make_counting_iterator(graph.number_of_edges), [d_temp_workers, @@ -170,11 +231,11 @@ weight_t hungarian_sparse(raft::handle_t const &handle, // temp_assignment_v will hold the assignment in the dense // bipartite matrix numbering // - rmm::device_vector temp_assignment_v(matrix_dimension); - vertex_t *d_temp_assignment = temp_assignment_v.data().get(); + rmm::device_uvector temp_assignment_v(matrix_dimension, handle.get_stream_view()); + vertex_t* d_temp_assignment = temp_assignment_v.data(); weight_t min_cost = detail::hungarian( - handle, matrix_dimension, matrix_dimension, d_cost, d_temp_assignment, stream); + handle, matrix_dimension, matrix_dimension, d_cost, d_temp_assignment, epsilon); #ifdef TIMING hr_timer.stop(); @@ -185,7 +246,7 @@ weight_t hungarian_sparse(raft::handle_t const &handle, // // Translate the assignment back to the original vertex ids // - thrust::for_each(rmm::exec_policy(stream)->on(stream), + thrust::for_each(rmm::exec_policy(handle.get_stream_view()), thrust::make_counting_iterator(0), thrust::make_counting_iterator(num_rows), [d_tasks, d_temp_assignment, assignment] __device__(vertex_t id) { @@ -204,54 +265,106 @@ weight_t hungarian_sparse(raft::handle_t const &handle, } // namespace detail template -weight_t hungarian(raft::handle_t const &handle, - GraphCOOView const &graph, +weight_t hungarian(raft::handle_t const& handle, + legacy::GraphCOOView const& graph, vertex_t num_workers, - vertex_t const *workers, - vertex_t *assignment) + vertex_t const* workers, + vertex_t* assignment) { - cudaStream_t stream{0}; + return detail::hungarian_sparse( + handle, graph, num_workers, workers, assignment, detail::default_epsilon()); +} - return detail::hungarian_sparse(handle, graph, num_workers, workers, assignment, stream); +template +weight_t hungarian(raft::handle_t const& handle, + legacy::GraphCOOView const& graph, + vertex_t num_workers, + vertex_t const* workers, + vertex_t* assignment, + weight_t epsilon) +{ + return detail::hungarian_sparse(handle, graph, num_workers, workers, assignment, epsilon); } template int32_t hungarian( - raft::handle_t const &, - GraphCOOView const &, + raft::handle_t const&, + legacy::GraphCOOView const&, + int32_t, + int32_t const*, + int32_t*, + int32_t); + +template float hungarian( + raft::handle_t const&, + legacy::GraphCOOView const&, + int32_t, + int32_t const*, + int32_t*, + float); +template double hungarian( + raft::handle_t const&, + legacy::GraphCOOView const&, + int32_t, + int32_t const*, + int32_t*, + double); + +template int32_t hungarian( + raft::handle_t const&, + legacy::GraphCOOView const&, int32_t, - int32_t const *, - int32_t *); -template float hungarian(raft::handle_t const &, - GraphCOOView const &, - int32_t, - int32_t const *, - int32_t *); -template double hungarian(raft::handle_t const &, - GraphCOOView const &, - int32_t, - int32_t const *, - int32_t *); + int32_t const*, + int32_t*); + +template float hungarian( + raft::handle_t const&, + legacy::GraphCOOView const&, + int32_t, + int32_t const*, + int32_t*); +template double hungarian( + raft::handle_t const&, + legacy::GraphCOOView const&, + int32_t, + int32_t const*, + int32_t*); namespace dense { template -weight_t hungarian(raft::handle_t const &handle, - weight_t const *costs, +weight_t hungarian(raft::handle_t const& handle, + weight_t const* costs, index_t num_rows, index_t num_cols, - index_t *assignment) + index_t* assignment) { - cudaStream_t stream{0}; + return detail::hungarian( + handle, num_rows, num_cols, costs, assignment, detail::default_epsilon()); +} - return detail::hungarian(handle, num_rows, num_cols, costs, assignment, stream); +template +weight_t hungarian(raft::handle_t const& handle, + weight_t const* costs, + index_t num_rows, + index_t num_cols, + index_t* assignment, + weight_t epsilon) +{ + return detail::hungarian(handle, num_rows, num_cols, costs, assignment, epsilon); } template int32_t hungarian( - raft::handle_t const &, int32_t const *, int32_t, int32_t, int32_t *); + raft::handle_t const&, int32_t const*, int32_t, int32_t, int32_t*); +template float hungarian( + raft::handle_t const&, float const*, int32_t, int32_t, int32_t*); +template double hungarian( + raft::handle_t const&, double const*, int32_t, int32_t, int32_t*); +template int32_t hungarian( + raft::handle_t const&, int32_t const*, int32_t, int32_t, int32_t*, int32_t); template float hungarian( - raft::handle_t const &, float const *, int32_t, int32_t, int32_t *); + raft::handle_t const&, float const*, int32_t, int32_t, int32_t*, float); template double hungarian( - raft::handle_t const &, double const *, int32_t, int32_t, int32_t *); + raft::handle_t const&, double const*, int32_t, int32_t, int32_t*, double); } // namespace dense diff --git a/cpp/src/link_analysis/gunrock_hits.cpp b/cpp/src/link_analysis/gunrock_hits.cpp index 8662c3bea79..a86210cc521 100644 --- a/cpp/src/link_analysis/gunrock_hits.cpp +++ b/cpp/src/link_analysis/gunrock_hits.cpp @@ -1,5 +1,5 @@ /* - * 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. @@ -19,10 +19,10 @@ * @brief wrapper calling gunrock's HITS analytic * --------------------------------------------------------------------------*/ -#include -#include +#include +#include -#include +#include #include @@ -34,17 +34,17 @@ const int HOST{1}; // gunrock should expose the device constant at the API le const int DEVICE{2}; // gunrock should expose the device constant at the API level. template -void hits(cugraph::GraphCSRView const &graph, +void hits(cugraph::legacy::GraphCSRView const& graph, int max_iter, weight_t tolerance, - weight_t const *starting_value, + weight_t const* starting_value, bool normalized, - weight_t *hubs, - weight_t *authorities) + weight_t* hubs, + weight_t* authorities) { - CUGRAPH_EXPECTS(hubs != nullptr, "Invalid API parameter: hubs array should be of size V"); + CUGRAPH_EXPECTS(hubs != nullptr, "Invalid input argument: hubs array should be of size V"); CUGRAPH_EXPECTS(authorities != nullptr, - "Invalid API parameter: authorities array should be of size V"); + "Invalid input argument: authorities array should be of size V"); // // NOTE: gunrock doesn't support passing a starting value @@ -61,13 +61,13 @@ void hits(cugraph::GraphCSRView const &graph, DEVICE); } -template void hits(cugraph::GraphCSRView const &, +template void hits(cugraph::legacy::GraphCSRView const&, int, float, - float const *, + float const*, bool, - float *, - float *); + float*, + float*); } // namespace gunrock } // namespace cugraph diff --git a/cpp/src/link_analysis/pagerank.cu b/cpp/src/link_analysis/pagerank.cu deleted file mode 100644 index e5da24e328d..00000000000 --- a/cpp/src/link_analysis/pagerank.cu +++ /dev/null @@ -1,432 +0,0 @@ -/* - * Copyright (c) 2018-2020, NVIDIA CORPORATION. All rights reserved. - * - * NVIDIA CORPORATION and its licensors retain all intellectual property - * and proprietary rights in and to this software, related documentation - * and any modifications thereto. Any use, reproduction, disclosure or - * distribution of this software and related documentation without an express - * license agreement from NVIDIA CORPORATION is strictly prohibited. - * - */ - -// Pagerank solver -// Author: Alex Fender afender@nvidia.com - -#include -#include -#include -#include -#include -#include -#include -#include -#include "cub/cub.cuh" - -#include -#include -#include - -#include -#include "pagerank_1D.cuh" -#include "utilities/graph_utils.cuh" - -#include - -namespace cugraph { -namespace detail { - -#ifdef DEBUG -#define PR_VERBOSE -#endif - -template -bool pagerankIteration(raft::handle_t const &handle, - IndexType n, - IndexType e, - IndexType const *cscPtr, - IndexType const *cscInd, - ValueType *cscVal, - ValueType alpha, - ValueType *a, - ValueType *b, - float tolerance, - int iter, - int max_iter, - ValueType *&tmp, - void *cub_d_temp_storage, - size_t cub_temp_storage_bytes, - ValueType *&pr, - ValueType *residual) -{ - ValueType dot_res; -//#if defined(CUDART_VERSION) and CUDART_VERSION >= 11000 -#if 1 - { - raft::matrix::sparse_matrix_t const r_csr_m{ - handle, cscPtr, cscInd, cscVal, n, e}; - r_csr_m.mv(1.0, tmp, 0.0, pr); - } -#else - CUDA_TRY(cub::DeviceSpmv::CsrMV(cub_d_temp_storage, - cub_temp_storage_bytes, - cscVal, - (IndexType *)cscPtr, - (IndexType *)cscInd, - tmp, - pr, - n, - n, - e)); -#endif - scal(n, alpha, pr); - dot_res = dot(n, a, tmp); - axpy(n, dot_res, b, pr); - scal(n, (ValueType)1.0 / nrm2(n, pr), pr); - axpy(n, (ValueType)-1.0, pr, tmp); - *residual = nrm2(n, tmp); - if (*residual < tolerance) { - scal(n, (ValueType)1.0 / nrm1(n, pr), pr); - return true; - } else { - if (iter < max_iter) { - // FIXME: Copy the pagerank vector results to the tmp vector, since there - // are still raw pointers in pagerank pointing to tmp vector locations - // that were std::swapped out in the solver. A thrust::swap would - // probably be more efficent if the vectors were passed everywhere instead - // of pointers. std::swap is unsafe though. Just copying for now, as this - // may soon be replaced by the pattern accelerator. - copy(n, pr, tmp); - } else { - scal(n, (ValueType)1.0 / nrm1(n, pr), pr); - } - return false; - } -} - -template -int pagerankSolver(raft::handle_t const &handle, - IndexType n, - IndexType e, - IndexType const *cscPtr, - IndexType const *cscInd, - ValueType *cscVal, - IndexType *prsVtx, - ValueType *prsVal, - IndexType prsLen, - bool has_personalization, - ValueType alpha, - ValueType *a, - bool has_guess, - float tolerance, - int max_iter, - ValueType *&pagerank_vector, - ValueType *&residual) -{ - int max_it, i = 0; - float tol; - bool converged = false; - ValueType randomProbability = static_cast(1.0 / n); - ValueType *tmp_d{nullptr}; - ValueType *b_d{nullptr}; - void *cub_d_temp_storage = NULL; - size_t cub_temp_storage_bytes = 0; - - if (max_iter > 0) - max_it = max_iter; - else - max_it = 500; - - if (tolerance == 0.0f) - tol = 1.0E-6f; - else if (tolerance < 1.0f && tolerance > 0.0f) - tol = tolerance; - else - return -1; - - if (alpha <= 0.0f || alpha >= 1.0f) return -1; - - rmm::device_vector b(n); - b_d = b.data().get(); - -#if 1 /* temporary solution till https://github.com/NVlabs/cub/issues/162 is resolved */ - thrust::device_vector tmp(n); - tmp_d = tmp.data().get(); -#else - rmm::device_vector tmp(n); - tmp_d = pr.data().get(); -#endif - // FIXME: this should take a passed CUDA strema instead of default nullptr - CHECK_CUDA(nullptr); - - if (!has_guess) { - fill(n, pagerank_vector, randomProbability); - fill(n, tmp_d, randomProbability); - } else { - copy(n, pagerank_vector, tmp_d); - } - - if (has_personalization) { - ValueType sum = nrm1(prsLen, prsVal); - if (static_cast(0) == sum) { - fill(n, b_d, randomProbability); - } else { - scal(n, static_cast(1.0 / sum), prsVal); - fill(n, b_d, static_cast(0)); - scatter(prsLen, prsVal, b_d, prsVtx); - } - } else { - fill(n, b_d, randomProbability); - } - update_dangling_nodes(n, a, alpha); - -//#if defined(CUDART_VERSION) and CUDART_VERSION >= 11000 -#if 1 - { - raft::matrix::sparse_matrix_t const r_csr_m{ - handle, cscPtr, cscInd, cscVal, n, e}; - r_csr_m.mv(1.0, tmp_d, 0.0, pagerank_vector); - } -#else - CUDA_TRY(cub::DeviceSpmv::CsrMV(cub_d_temp_storage, - cub_temp_storage_bytes, - cscVal, - (IndexType *)cscPtr, - (IndexType *)cscInd, - tmp_d, - pagerank_vector, - n, - n, - e)); -#endif - // Allocate temporary storage - rmm::device_buffer cub_temp_storage(cub_temp_storage_bytes); - cub_d_temp_storage = cub_temp_storage.data(); - -#ifdef PR_VERBOSE - std::stringstream ss; - ss.str(std::string()); - ss << " ------------------PageRank------------------" << std::endl; - ss << " --------------------------------------------" << std::endl; - ss << std::setw(10) << "Iteration" << std::setw(15) << "Residual" << std::endl; - ss << " --------------------------------------------" << std::endl; - std::cout << ss.str(); -#endif - - while (!converged && i < max_it) { - i++; - converged = pagerankIteration(handle, - n, - e, - cscPtr, - cscInd, - cscVal, - alpha, - a, - b_d, - tol, - i, - max_it, - tmp_d, - cub_d_temp_storage, - cub_temp_storage_bytes, - pagerank_vector, - residual); -#ifdef PR_VERBOSE - ss.str(std::string()); - ss << std::setw(10) << i; - ss.precision(3); - ss << std::setw(15) << std::scientific << *residual << std::endl; - std::cout << ss.str(); -#endif - } -#ifdef PR_VERBOSE - std::cout << " --------------------------------------------" << std::endl; -#endif - - return converged ? 0 : 1; -} - -// template int pagerankSolver ( int n, int e, int *cscPtr, int *cscInd,half *cscVal, -// half alpha, half *a, bool has_guess, float tolerance, int max_iter, half * &pagerank_vector, half -// * &residual); -template int pagerankSolver(raft::handle_t const &handle, - int n, - int e, - int const *cscPtr, - int const *cscInd, - float *cscVal, - int *prsVtx, - float *prsVal, - int prsLen, - bool has_personalization, - float alpha, - float *a, - bool has_guess, - float tolerance, - int max_iter, - float *&pagerank_vector, - float *&residual); -template int pagerankSolver(raft::handle_t const &handle, - int n, - int e, - const int *cscPtr, - int const *cscInd, - double *cscVal, - int *prsVtx, - double *prsVal, - int prsLen, - bool has_personalization, - double alpha, - double *a, - bool has_guess, - float tolerance, - int max_iter, - double *&pagerank_vector, - double *&residual); - -template -void pagerank_impl(raft::handle_t const &handle, - GraphCSCView const &graph, - WT *pagerank, - VT personalization_subset_size = 0, - VT *personalization_subset = nullptr, - WT *personalization_values = nullptr, - double alpha = 0.85, - double tolerance = 1e-5, - int64_t max_iter = 100, - bool has_guess = false) -{ - bool has_personalization = false; - int prsLen = 0; - VT m = graph.number_of_vertices; - ET nnz = graph.number_of_edges; - int status{0}; - WT *d_pr{nullptr}, *d_val{nullptr}, *d_leaf_vector{nullptr}; - WT res = 1.0; - WT *residual = &res; - - if (personalization_subset_size != 0) { - CUGRAPH_EXPECTS(personalization_subset != nullptr, - "Invalid API parameter: personalization_subset array should be of size " - "personalization_subset_size"); - CUGRAPH_EXPECTS(personalization_values != nullptr, - "Invalid API parameter: personalization_values array should be of size " - "personalization_subset_size"); - CUGRAPH_EXPECTS(personalization_subset_size <= m, - "Personalization size should be smaller than V"); - has_personalization = true; - prsLen = static_cast(personalization_subset_size); - } - -#if 1 /* temporary solution till https://github.com/NVlabs/cub/issues/162 is resolved */ - thrust::device_vector pr(m); - d_pr = pr.data().get(); -#else - rmm::device_vector pr(m); - d_pr = pr.data().get(); -#endif - - rmm::device_vector leaf_vector(m); - rmm::device_vector val(nnz); - - d_leaf_vector = leaf_vector.data().get(); - d_val = val.data().get(); - - // The templating for HT_matrix_csc_coo assumes that m, nnz and data are all the same type - HT_matrix_csc_coo(m, nnz, graph.offsets, graph.indices, d_val, d_leaf_vector); - - if (has_guess) { copy(m, (WT *)pagerank, d_pr); } - - status = pagerankSolver(handle, - m, - nnz, - graph.offsets, - graph.indices, - d_val, - personalization_subset, - personalization_values, - prsLen, - has_personalization, - alpha, - d_leaf_vector, - has_guess, - tolerance, - max_iter, - d_pr, - residual); - - switch (status) { - case 0: break; - case -1: CUGRAPH_FAIL("Error : bad parameters in Pagerank"); - case 1: break; // Warning : Pagerank did not reached the desired tolerance - default: CUGRAPH_FAIL("Pagerank exec failed"); - } - - copy(m, d_pr, (WT *)pagerank); -} -} // namespace detail - -template -void pagerank(raft::handle_t const &handle, - GraphCSCView const &graph, - WT *pagerank, - VT personalization_subset_size, - VT *personalization_subset, - WT *personalization_values, - double alpha, - double tolerance, - int64_t max_iter, - bool has_guess) -{ - CUGRAPH_EXPECTS(pagerank != nullptr, "Invalid API parameter: Pagerank array should be of size V"); - // Multi-GPU - if (handle.comms_initialized()) { - CUGRAPH_EXPECTS(has_guess == false, - "Invalid API parameter: Multi-GPU Pagerank does not guess, please use the " - "single GPU version for this feature"); - CUGRAPH_EXPECTS(max_iter > 0, "The number of iteration must be positive"); - cugraph::mg::pagerank(handle, - graph, - pagerank, - personalization_subset_size, - personalization_subset, - personalization_values, - alpha, - max_iter, - tolerance); - } else // Single GPU - return detail::pagerank_impl(handle, - graph, - pagerank, - personalization_subset_size, - personalization_subset, - personalization_values, - alpha, - tolerance, - max_iter, - has_guess); -} - -// explicit instantiation -template void pagerank(raft::handle_t const &handle, - GraphCSCView const &graph, - float *pagerank, - int personalization_subset_size, - int *personalization_subset, - float *personalization_values, - double alpha, - double tolerance, - int64_t max_iter, - bool has_guess); -template void pagerank(raft::handle_t const &handle, - GraphCSCView const &graph, - double *pagerank, - int personalization_subset_size, - int *personalization_subset, - double *personalization_values, - double alpha, - double tolerance, - int64_t max_iter, - bool has_guess); - -} // namespace cugraph diff --git a/cpp/src/link_analysis/pagerank_1D.cu b/cpp/src/link_analysis/pagerank_1D.cu deleted file mode 100644 index 3774a364cf1..00000000000 --- a/cpp/src/link_analysis/pagerank_1D.cu +++ /dev/null @@ -1,188 +0,0 @@ -/* - * Copyright (c) 2020, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Author: Alex Fender afender@nvidia.com - -#include -#include -#include "pagerank_1D.cuh" -#include "utilities/graph_utils.cuh" - -namespace cugraph { -namespace mg { - -template -__global__ void transition_kernel(const size_t e, const VT *ind, const VT *degree, WT *val) -{ - for (auto i = threadIdx.x + blockIdx.x * blockDim.x; i < e; i += gridDim.x * blockDim.x) - val[i] = 1.0 / degree[ind[i]]; // Degree contains IN degree. So all degree[ind[i]] were - // incremented by definition (no div by 0). -} - -template -Pagerank::Pagerank(const raft::handle_t &handle_, GraphCSCView const &G) - : comm(handle_.get_comms()), - bookmark(G.number_of_vertices), - prev_pr(G.number_of_vertices), - val(G.local_edges[comm.get_rank()]), - handle(handle_), - has_personalization(false) -{ - v_glob = G.number_of_vertices; - v_loc = G.local_vertices[comm.get_rank()]; - e_loc = G.local_edges[comm.get_rank()]; - part_off = G.local_offsets; - local_vertices = G.local_vertices; - off = G.offsets; - ind = G.indices; - blocks = handle_.get_device_properties().maxGridSize[0]; - threads = handle_.get_device_properties().maxThreadsPerBlock; - sm_count = handle_.get_device_properties().multiProcessorCount; - - is_setup = false; -} - -template -Pagerank::~Pagerank() -{ -} - -template -void Pagerank::transition_vals(const VT *degree) -{ - if (e_loc > 0) { - int threads = std::min(e_loc, this->threads); - int blocks = std::min(32 * sm_count, this->blocks); - transition_kernel<<>>(e_loc, ind, degree, val.data().get()); - CHECK_CUDA(nullptr); - } -} - -template -void Pagerank::flag_leafs(const VT *degree) -{ - if (v_glob > 0) { - int threads = std::min(v_glob, this->threads); - int blocks = std::min(32 * sm_count, this->blocks); - cugraph::detail::flag_leafs_kernel - <<>>(v_glob, degree, bookmark.data().get()); - CHECK_CUDA(nullptr); - } -} - -// Artificially create the google matrix by setting val and bookmark -template -void Pagerank::setup(WT _alpha, - VT *degree, - VT personalization_subset_size, - VT *personalization_subset, - WT *personalization_values) -{ - if (!is_setup) { - alpha = _alpha; - WT zero = 0.0; - WT one = 1.0; - // Update dangling node vector - cugraph::detail::fill(v_glob, bookmark.data().get(), zero); - flag_leafs(degree); - cugraph::detail::update_dangling_nodes(v_glob, bookmark.data().get(), alpha); - - // Transition matrix - transition_vals(degree); - - // personalize - if (personalization_subset_size != 0) { - CUGRAPH_EXPECTS(personalization_subset != nullptr, - "Invalid API parameter: personalization_subset array should be of size " - "personalization_subset_size"); - CUGRAPH_EXPECTS(personalization_values != nullptr, - "Invalid API parameter: personalization_values array should be of size " - "personalization_subset_size"); - CUGRAPH_EXPECTS(personalization_subset_size <= v_glob, - "Personalization size should be smaller than V"); - - WT sum = cugraph::detail::nrm1(personalization_subset_size, personalization_values); - if (sum != zero) { - has_personalization = true; - personalization_vector.resize(v_glob); - cugraph::detail::fill(v_glob, personalization_vector.data().get(), zero); - cugraph::detail::scal(v_glob, one / sum, personalization_values); - cugraph::detail::scatter(personalization_subset_size, - personalization_values, - personalization_vector.data().get(), - personalization_subset); - } - } - is_setup = true; - } else - CUGRAPH_FAIL("MG PageRank : Setup can be called only once"); -} - -// run the power iteration on the google matrix -template -int Pagerank::solve(int max_iter, float tolerance, WT *pagerank) -{ - if (is_setup) { - WT dot_res; - WT one = 1.0; - WT *pr = pagerank; - cugraph::detail::fill(v_glob, pagerank, one / v_glob); - cugraph::detail::fill(v_glob, prev_pr.data().get(), one / v_glob); - // This cuda sync was added to fix #426 - // This should not be requiered in theory - // This is not needed on one GPU at this time - cudaDeviceSynchronize(); - dot_res = cugraph::detail::dot(v_glob, bookmark.data().get(), pr); - MGcsrmv spmv_solver( - handle, local_vertices, part_off, off, ind, val.data().get(), pagerank); - - WT residual; - int i; - for (i = 0; i < max_iter; ++i) { - spmv_solver.run(pagerank); - cugraph::detail::scal(v_glob, alpha, pr); - - // personalization - if (has_personalization) - cugraph::detail::axpy(v_glob, dot_res, personalization_vector.data().get(), pr); - else - cugraph::detail::addv(v_glob, dot_res * (one / v_glob), pr); - - dot_res = cugraph::detail::dot(v_glob, bookmark.data().get(), pr); - cugraph::detail::scal(v_glob, one / cugraph::detail::nrm2(v_glob, pr), pr); - - // convergence check - cugraph::detail::axpy(v_glob, (WT)-1.0, pr, prev_pr.data().get()); - residual = cugraph::detail::nrm2(v_glob, prev_pr.data().get()); - if (residual < tolerance) - break; - else - cugraph::detail::copy(v_glob, pr, prev_pr.data().get()); - } - cugraph::detail::scal(v_glob, one / cugraph::detail::nrm1(v_glob, pr), pr); - return i; - } else { - CUGRAPH_FAIL("MG PageRank : Solve was called before setup"); - } -} - -template class Pagerank; -template class Pagerank; - -} // namespace mg -} // namespace cugraph - -#include "utilities/eidir_graph_utils.hpp" diff --git a/cpp/src/link_analysis/pagerank_1D.cuh b/cpp/src/link_analysis/pagerank_1D.cuh deleted file mode 100644 index feb410daa9a..00000000000 --- a/cpp/src/link_analysis/pagerank_1D.cuh +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright (c) 2020, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Author: Alex Fender afender@nvidia.com - -#pragma once - -#include -#include -#include - -#include "utilities/error.hpp" -#include "utilities/spmv_1D.cuh" - -namespace cugraph { -namespace mg { - -template -class Pagerank { - private: - VT v_glob{}; // global number of vertices - VT v_loc{}; // local number of vertices - ET e_loc{}; // local number of edges - WT alpha{}; // damping factor - bool has_personalization; - // CUDA - const raft::comms::comms_t &comm; // info about the mg comm setup - cudaStream_t stream; - int blocks; - int threads; - int sm_count; - - // Vertex offsets for each partition. - VT *part_off; - VT *local_vertices; - - // Google matrix - ET *off; - VT *ind; - - rmm::device_vector val; // values of the substochastic matrix - rmm::device_vector bookmark; // constant vector with dangling node info - rmm::device_vector prev_pr; // record the last pagerank for convergence check - rmm::device_vector personalization_vector; // personalization vector after reconstruction - - bool is_setup; - raft::handle_t const &handle; // raft handle propagation for SpMV, etc. - - public: - Pagerank(const raft::handle_t &handle, const GraphCSCView &G); - ~Pagerank(); - - void transition_vals(const VT *degree); - - void flag_leafs(const VT *degree); - - // Artificially create the google matrix by setting val and bookmark - void setup(WT _alpha, - VT *degree, - VT personalization_subset_size, - VT *personalization_subset, - WT *personalization_values); - - // run the power iteration on the google matrix, return the number of iterations - int solve(int max_iter, float tolerance, WT *pagerank); -}; - -template -int pagerank(raft::handle_t const &handle, - const GraphCSCView &G, - WT *pagerank_result, - VT personalization_subset_size, - VT *personalization_subset, - WT *personalization_values, - const double damping_factor = 0.85, - const int64_t n_iter = 100, - const double tolerance = 1e-5) -{ - // null pointers check - CUGRAPH_EXPECTS(G.offsets != nullptr, "Invalid API parameter - offsets is null"); - CUGRAPH_EXPECTS(G.indices != nullptr, "Invalid API parameter - indidices is null"); - CUGRAPH_EXPECTS(pagerank_result != nullptr, - "Invalid API parameter - pagerank output memory must be allocated"); - - // parameter values - CUGRAPH_EXPECTS(damping_factor > 0.0, - "Invalid API parameter - invalid damping factor value (alpha<0)"); - CUGRAPH_EXPECTS(damping_factor < 1.0, - "Invalid API parameter - invalid damping factor value (alpha>1)"); - CUGRAPH_EXPECTS(n_iter > 0, "Invalid API parameter - n_iter must be > 0"); - - rmm::device_vector degree(G.number_of_vertices); - - // in-degree of CSC (equivalent to out-degree of original edge list) - G.degree(degree.data().get(), DegreeDirection::IN); - - // Allocate and intialize Pagerank class - Pagerank pr_solver(handle, G); - - // Set all constants info - pr_solver.setup(damping_factor, - degree.data().get(), - personalization_subset_size, - personalization_subset, - personalization_values); - - // Run pagerank - return pr_solver.solve(n_iter, tolerance, pagerank_result); -} - -} // namespace mg -} // namespace cugraph diff --git a/cpp/src/link_prediction/jaccard.cu b/cpp/src/link_prediction/jaccard.cu index 70952974b39..25186a6492b 100644 --- a/cpp/src/link_prediction/jaccard.cu +++ b/cpp/src/link_prediction/jaccard.cu @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. @@ -19,10 +19,12 @@ * @file jaccard.cu * ---------------------------------------------------------------------------**/ -#include -#include -#include "graph.hpp" -#include "utilities/graph_utils.cuh" +#include +#include +#include + +#include +#include namespace cugraph { namespace detail { @@ -30,7 +32,7 @@ namespace detail { // Volume of neighboors (*weight_s) template __global__ void jaccard_row_sum( - vertex_t n, edge_t const *csrPtr, vertex_t const *csrInd, weight_t const *v, weight_t *work) + vertex_t n, edge_t const* csrPtr, vertex_t const* csrInd, weight_t const* v, weight_t* work) { vertex_t row; edge_t start, end, length; @@ -54,12 +56,12 @@ __global__ void jaccard_row_sum( // Volume of intersections (*weight_i) and cumulated volume of neighboors (*weight_s) template __global__ void jaccard_is(vertex_t n, - edge_t const *csrPtr, - vertex_t const *csrInd, - weight_t const *v, - weight_t *work, - weight_t *weight_i, - weight_t *weight_s) + edge_t const* csrPtr, + vertex_t const* csrInd, + weight_t const* v, + weight_t* work, + weight_t* weight_i, + weight_t* weight_s) { edge_t i, j, Ni, Nj; vertex_t row, col; @@ -118,14 +120,14 @@ __global__ void jaccard_is(vertex_t n, // Using list of node pairs template __global__ void jaccard_is_pairs(edge_t num_pairs, - edge_t const *csrPtr, - vertex_t const *csrInd, - vertex_t const *first_pair, - vertex_t const *second_pair, - weight_t const *v, - weight_t *work, - weight_t *weight_i, - weight_t *weight_s) + edge_t const* csrPtr, + vertex_t const* csrInd, + vertex_t const* first_pair, + vertex_t const* second_pair, + weight_t const* v, + weight_t* work, + weight_t* weight_i, + weight_t* weight_s) { edge_t i, idx, Ni, Nj, match; vertex_t row, col, ref, cur, ref_col, cur_col; @@ -182,9 +184,9 @@ __global__ void jaccard_is_pairs(edge_t num_pairs, // Jaccard weights (*weight) template __global__ void jaccard_jw(edge_t e, - weight_t const *weight_i, - weight_t const *weight_s, - weight_t *weight_j) + weight_t const* weight_i, + weight_t const* weight_s, + weight_t* weight_j) { edge_t j; weight_t Wi, Ws, Wu; @@ -200,14 +202,15 @@ __global__ void jaccard_jw(edge_t e, template int jaccard(vertex_t n, edge_t e, - edge_t const *csrPtr, - vertex_t const *csrInd, - weight_t const *weight_in, - weight_t *work, - weight_t *weight_i, - weight_t *weight_s, - weight_t *weight_j) + edge_t const* csrPtr, + vertex_t const* csrInd, + weight_t const* weight_in, + weight_t* work, + weight_t* weight_i, + weight_t* weight_s, + weight_t* weight_j) { + rmm::cuda_stream_view stream_view; dim3 nthreads, nblocks; int y = 4; @@ -221,9 +224,9 @@ int jaccard(vertex_t n, // launch kernel jaccard_row_sum - <<>>(n, csrPtr, csrInd, weight_in, work); - cudaDeviceSynchronize(); - fill(e, weight_i, weight_t{0.0}); + <<>>(n, csrPtr, csrInd, weight_in, work); + + thrust::fill(rmm::exec_policy(stream_view), weight_i, weight_i + e, weight_t{0.0}); // setup launch configuration nthreads.x = 32 / y; @@ -234,8 +237,8 @@ int jaccard(vertex_t n, nblocks.z = min((n + nthreads.z - 1) / nthreads.z, vertex_t{CUDA_MAX_BLOCKS}); // 1; // launch kernel - jaccard_is - <<>>(n, csrPtr, csrInd, weight_in, work, weight_i, weight_s); + jaccard_is<<>>( + n, csrPtr, csrInd, weight_in, work, weight_i, weight_s); // setup launch configuration nthreads.x = min(e, edge_t{CUDA_MAX_KERNEL_THREADS}); @@ -247,7 +250,7 @@ int jaccard(vertex_t n, // launch kernel jaccard_jw - <<>>(e, weight_i, weight_s, weight_j); + <<>>(e, weight_i, weight_s, weight_j); return 0; } @@ -255,15 +258,15 @@ int jaccard(vertex_t n, template int jaccard_pairs(vertex_t n, edge_t num_pairs, - edge_t const *csrPtr, - vertex_t const *csrInd, - vertex_t const *first_pair, - vertex_t const *second_pair, - weight_t const *weight_in, - weight_t *work, - weight_t *weight_i, - weight_t *weight_s, - weight_t *weight_j) + edge_t const* csrPtr, + vertex_t const* csrInd, + vertex_t const* first_pair, + vertex_t const* second_pair, + weight_t const* weight_in, + weight_t* work, + weight_t* weight_i, + weight_t* weight_s, + weight_t* weight_j) { dim3 nthreads, nblocks; int y = 4; @@ -313,9 +316,9 @@ int jaccard_pairs(vertex_t n, } // namespace detail template -void jaccard(GraphCSRView const &graph, WT const *weights, WT *result) +void jaccard(legacy::GraphCSRView const& graph, WT const* weights, WT* result) { - CUGRAPH_EXPECTS(result != nullptr, "Invalid API parameter: result pointer is NULL"); + CUGRAPH_EXPECTS(result != nullptr, "Invalid input argument: result pointer is NULL"); rmm::device_vector weight_i(graph.number_of_edges); rmm::device_vector weight_s(graph.number_of_edges); @@ -345,16 +348,16 @@ void jaccard(GraphCSRView const &graph, WT const *weights, WT *resul } template -void jaccard_list(GraphCSRView const &graph, - WT const *weights, +void jaccard_list(legacy::GraphCSRView const& graph, + WT const* weights, ET num_pairs, - VT const *first, - VT const *second, - WT *result) + VT const* first, + VT const* second, + WT* result) { - CUGRAPH_EXPECTS(result != nullptr, "Invalid API parameter: result pointer is NULL"); - CUGRAPH_EXPECTS(first != nullptr, "Invalid API parameter: first is NULL"); - CUGRAPH_EXPECTS(second != nullptr, "Invalid API parameter: second in NULL"); + CUGRAPH_EXPECTS(result != nullptr, "Invalid input argument: result pointer is NULL"); + CUGRAPH_EXPECTS(first != nullptr, "Invalid input argument: first is NULL"); + CUGRAPH_EXPECTS(second != nullptr, "Invalid input argument: second in NULL"); rmm::device_vector weight_i(num_pairs, WT{0.0}); rmm::device_vector weight_s(num_pairs); @@ -387,41 +390,43 @@ void jaccard_list(GraphCSRView const &graph, } } -template void jaccard(GraphCSRView const &, - float const *, - float *); -template void jaccard(GraphCSRView const &, - double const *, - double *); -template void jaccard(GraphCSRView const &, - float const *, - float *); -template void jaccard(GraphCSRView const &, - double const *, - double *); -template void jaccard_list(GraphCSRView const &, - float const *, - int32_t, - int32_t const *, - int32_t const *, - float *); -template void jaccard_list(GraphCSRView const &, - double const *, - int32_t, - int32_t const *, - int32_t const *, - double *); -template void jaccard_list(GraphCSRView const &, - float const *, - int64_t, - int64_t const *, - int64_t const *, - float *); -template void jaccard_list(GraphCSRView const &, - double const *, - int64_t, - int64_t const *, - int64_t const *, - double *); +template void jaccard(legacy::GraphCSRView const&, + float const*, + float*); +template void jaccard( + legacy::GraphCSRView const&, double const*, double*); +template void jaccard(legacy::GraphCSRView const&, + float const*, + float*); +template void jaccard( + legacy::GraphCSRView const&, double const*, double*); +template void jaccard_list( + legacy::GraphCSRView const&, + float const*, + int32_t, + int32_t const*, + int32_t const*, + float*); +template void jaccard_list( + legacy::GraphCSRView const&, + double const*, + int32_t, + int32_t const*, + int32_t const*, + double*); +template void jaccard_list( + legacy::GraphCSRView const&, + float const*, + int64_t, + int64_t const*, + int64_t const*, + float*); +template void jaccard_list( + legacy::GraphCSRView const&, + double const*, + int64_t, + int64_t const*, + int64_t const*, + double*); } // namespace cugraph diff --git a/cpp/src/link_prediction/overlap.cu b/cpp/src/link_prediction/overlap.cu index e3f80b50d9a..7b7470da7fc 100644 --- a/cpp/src/link_prediction/overlap.cu +++ b/cpp/src/link_prediction/overlap.cu @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. @@ -20,9 +20,9 @@ * ---------------------------------------------------------------------------**/ #include -#include -#include "graph.hpp" -#include "utilities/graph_utils.cuh" +#include +#include +#include namespace cugraph { namespace detail { @@ -31,7 +31,7 @@ namespace detail { // TODO: Identical kernel to jaccard_row_sum!! template __global__ void overlap_row_sum( - vertex_t n, edge_t const *csrPtr, vertex_t const *csrInd, weight_t const *v, weight_t *work) + vertex_t n, edge_t const* csrPtr, vertex_t const* csrInd, weight_t const* v, weight_t* work) { vertex_t row; edge_t start, end, length; @@ -56,12 +56,12 @@ __global__ void overlap_row_sum( // TODO: Identical kernel to jaccard_row_sum!! template __global__ void overlap_is(vertex_t n, - edge_t const *csrPtr, - vertex_t const *csrInd, - weight_t const *v, - weight_t *work, - weight_t *weight_i, - weight_t *weight_s) + edge_t const* csrPtr, + vertex_t const* csrInd, + weight_t const* v, + weight_t* work, + weight_t* weight_i, + weight_t* weight_s) { edge_t i, j, Ni, Nj; vertex_t row, col; @@ -121,14 +121,14 @@ __global__ void overlap_is(vertex_t n, // NOTE: NOT the same as jaccard template __global__ void overlap_is_pairs(edge_t num_pairs, - edge_t const *csrPtr, - vertex_t const *csrInd, - vertex_t const *first_pair, - vertex_t const *second_pair, - weight_t const *v, - weight_t *work, - weight_t *weight_i, - weight_t *weight_s) + edge_t const* csrPtr, + vertex_t const* csrInd, + vertex_t const* first_pair, + vertex_t const* second_pair, + weight_t const* v, + weight_t* work, + weight_t* weight_i, + weight_t* weight_s) { edge_t i, idx, Ni, Nj, match; vertex_t row, col, ref, cur, ref_col, cur_col; @@ -185,11 +185,11 @@ __global__ void overlap_is_pairs(edge_t num_pairs, // Overlap weights (*weight) template __global__ void overlap_jw(edge_t e, - edge_t const *csrPtr, - vertex_t const *csrInd, - weight_t *weight_i, - weight_t *weight_s, - weight_t *weight_j) + edge_t const* csrPtr, + vertex_t const* csrInd, + weight_t* weight_i, + weight_t* weight_s, + weight_t* weight_j) { edge_t j; weight_t Wi, Wu; @@ -204,13 +204,13 @@ __global__ void overlap_jw(edge_t e, template int overlap(vertex_t n, edge_t e, - edge_t const *csrPtr, - vertex_t const *csrInd, - weight_t const *weight_in, - weight_t *work, - weight_t *weight_i, - weight_t *weight_s, - weight_t *weight_j) + edge_t const* csrPtr, + vertex_t const* csrInd, + weight_t const* weight_in, + weight_t* work, + weight_t* weight_i, + weight_t* weight_s, + weight_t* weight_j) { dim3 nthreads, nblocks; int y = 4; @@ -259,15 +259,15 @@ int overlap(vertex_t n, template int overlap_pairs(vertex_t n, edge_t num_pairs, - edge_t const *csrPtr, - vertex_t const *csrInd, - vertex_t const *first_pair, - vertex_t const *second_pair, - weight_t const *weight_in, - weight_t *work, - weight_t *weight_i, - weight_t *weight_s, - weight_t *weight_j) + edge_t const* csrPtr, + vertex_t const* csrInd, + vertex_t const* first_pair, + vertex_t const* second_pair, + weight_t const* weight_in, + weight_t* work, + weight_t* weight_i, + weight_t* weight_s, + weight_t* weight_j) { dim3 nthreads, nblocks; int y = 4; @@ -314,9 +314,9 @@ int overlap_pairs(vertex_t n, } // namespace detail template -void overlap(GraphCSRView const &graph, WT const *weights, WT *result) +void overlap(legacy::GraphCSRView const& graph, WT const* weights, WT* result) { - CUGRAPH_EXPECTS(result != nullptr, "Invalid API parameter: result pointer is NULL"); + CUGRAPH_EXPECTS(result != nullptr, "Invalid input argument: result pointer is NULL"); rmm::device_vector weight_i(graph.number_of_edges); rmm::device_vector weight_s(graph.number_of_edges); @@ -346,16 +346,16 @@ void overlap(GraphCSRView const &graph, WT const *weights, WT *resul } template -void overlap_list(GraphCSRView const &graph, - WT const *weights, +void overlap_list(legacy::GraphCSRView const& graph, + WT const* weights, ET num_pairs, - VT const *first, - VT const *second, - WT *result) + VT const* first, + VT const* second, + WT* result) { - CUGRAPH_EXPECTS(result != nullptr, "Invalid API parameter: result pointer is NULL"); - CUGRAPH_EXPECTS(first != nullptr, "Invalid API parameter: first column is NULL"); - CUGRAPH_EXPECTS(second != nullptr, "Invalid API parameter: second column is NULL"); + CUGRAPH_EXPECTS(result != nullptr, "Invalid input argument: result pointer is NULL"); + CUGRAPH_EXPECTS(first != nullptr, "Invalid input argument: first column is NULL"); + CUGRAPH_EXPECTS(second != nullptr, "Invalid input argument: second column is NULL"); rmm::device_vector weight_i(num_pairs); rmm::device_vector weight_s(num_pairs); @@ -388,41 +388,43 @@ void overlap_list(GraphCSRView const &graph, } } -template void overlap(GraphCSRView const &, - float const *, - float *); -template void overlap(GraphCSRView const &, - double const *, - double *); -template void overlap(GraphCSRView const &, - float const *, - float *); -template void overlap(GraphCSRView const &, - double const *, - double *); -template void overlap_list(GraphCSRView const &, - float const *, - int32_t, - int32_t const *, - int32_t const *, - float *); -template void overlap_list(GraphCSRView const &, - double const *, - int32_t, - int32_t const *, - int32_t const *, - double *); -template void overlap_list(GraphCSRView const &, - float const *, - int64_t, - int64_t const *, - int64_t const *, - float *); -template void overlap_list(GraphCSRView const &, - double const *, - int64_t, - int64_t const *, - int64_t const *, - double *); +template void overlap(legacy::GraphCSRView const&, + float const*, + float*); +template void overlap( + legacy::GraphCSRView const&, double const*, double*); +template void overlap(legacy::GraphCSRView const&, + float const*, + float*); +template void overlap( + legacy::GraphCSRView const&, double const*, double*); +template void overlap_list( + legacy::GraphCSRView const&, + float const*, + int32_t, + int32_t const*, + int32_t const*, + float*); +template void overlap_list( + legacy::GraphCSRView const&, + double const*, + int32_t, + int32_t const*, + int32_t const*, + double*); +template void overlap_list( + legacy::GraphCSRView const&, + float const*, + int64_t, + int64_t const*, + int64_t const*, + float*); +template void overlap_list( + legacy::GraphCSRView const&, + double const*, + int64_t, + int64_t const*, + int64_t const*, + double*); } // namespace cugraph diff --git a/cpp/src/sampling/random_walks.cu b/cpp/src/sampling/random_walks.cu new file mode 100644 index 00000000000..1883535bf70 --- /dev/null +++ b/cpp/src/sampling/random_walks.cu @@ -0,0 +1,119 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// +#include +#include "random_walks.cuh" + +namespace cugraph { +namespace experimental { +// template explicit instantiation directives (EIDir's): +// +// SG FP32{ +template std:: + tuple, rmm::device_uvector, rmm::device_uvector> + random_walks(raft::handle_t const& handle, + graph_view_t const& gview, + int32_t const* ptr_d_start, + int32_t num_paths, + int32_t max_depth, + bool use_padding); + +template std:: + tuple, rmm::device_uvector, rmm::device_uvector> + random_walks(raft::handle_t const& handle, + graph_view_t const& gview, + int32_t const* ptr_d_start, + int64_t num_paths, + int64_t max_depth, + bool use_padding); + +template std:: + tuple, rmm::device_uvector, rmm::device_uvector> + random_walks(raft::handle_t const& handle, + graph_view_t const& gview, + int64_t const* ptr_d_start, + int64_t num_paths, + int64_t max_depth, + bool use_padding); +//} +// +// SG FP64{ +template std:: + tuple, rmm::device_uvector, rmm::device_uvector> + random_walks(raft::handle_t const& handle, + graph_view_t const& gview, + int32_t const* ptr_d_start, + int32_t num_paths, + int32_t max_depth, + bool use_padding); + +template std:: + tuple, rmm::device_uvector, rmm::device_uvector> + random_walks(raft::handle_t const& handle, + graph_view_t const& gview, + int32_t const* ptr_d_start, + int64_t num_paths, + int64_t max_depth, + bool use_padding); + +template std:: + tuple, rmm::device_uvector, rmm::device_uvector> + random_walks(raft::handle_t const& handle, + graph_view_t const& gview, + int64_t const* ptr_d_start, + int64_t num_paths, + int64_t max_depth, + bool use_padding); +//} + +template std:: + tuple, rmm::device_uvector, rmm::device_uvector> + convert_paths_to_coo(raft::handle_t const& handle, + int32_t coalesced_sz_v, + int32_t num_paths, + rmm::device_buffer&& d_coalesced_v, + rmm::device_buffer&& d_sizes); + +template std:: + tuple, rmm::device_uvector, rmm::device_uvector> + convert_paths_to_coo(raft::handle_t const& handle, + int64_t coalesced_sz_v, + int64_t num_paths, + rmm::device_buffer&& d_coalesced_v, + rmm::device_buffer&& d_sizes); + +template std:: + tuple, rmm::device_uvector, rmm::device_uvector> + convert_paths_to_coo(raft::handle_t const& handle, + int64_t coalesced_sz_v, + int64_t num_paths, + rmm::device_buffer&& d_coalesced_v, + rmm::device_buffer&& d_sizes); + +template std::tuple, + rmm::device_uvector, + rmm::device_uvector> +query_rw_sizes_offsets(raft::handle_t const& handle, int32_t num_paths, int32_t const* ptr_d_sizes); + +template std::tuple, + rmm::device_uvector, + rmm::device_uvector> +query_rw_sizes_offsets(raft::handle_t const& handle, int64_t num_paths, int64_t const* ptr_d_sizes); + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/src/sampling/random_walks.cuh b/cpp/src/sampling/random_walks.cuh new file mode 100644 index 00000000000..2286fa28697 --- /dev/null +++ b/cpp/src/sampling/random_walks.cuh @@ -0,0 +1,1251 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// +#pragma once + +#include +#include + +#include + +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include // FIXME: requirement for temporary std::getenv() +#include +#include +#include +#include + +#include "rw_traversals.hpp" + +namespace cugraph { +namespace experimental { + +namespace detail { + +// raft random generator: +// (using upper-bound cached "map" +// giving out_deg(v) for each v in [0, |V|); +// and a pre-generated vector of float random values +// in [0,1] to be brought into [0, d_ub[v])) +// +template +struct rrandom_gen_t { + using seed_type = seed_t; + using real_type = real_t; + + // cnstr. version that provides step-wise in-place + // rnd generation: + // + rrandom_gen_t(raft::handle_t const& handle, + index_t num_paths, + device_vec_t& d_random, // scratch-pad, non-coalesced + device_vec_t const& d_crt_out_deg, // non-coalesced + seed_t seed = seed_t{}) + : handle_(handle), + seed_(seed), + num_paths_(num_paths), + d_ptr_out_degs_(raw_const_ptr(d_crt_out_deg)), + d_ptr_random_(raw_ptr(d_random)) + { + auto rnd_sz = d_random.size(); + + CUGRAPH_EXPECTS(rnd_sz >= static_cast(num_paths), + "Un-allocated random buffer."); + + // done in constructor; + // this must be done at each step, + // but this object is constructed at each step; + // + generate_random(handle, d_ptr_random_, num_paths, seed_); + } + + // cnstr. version for the case when the + // random vector is provided by the caller: + // + rrandom_gen_t(raft::handle_t const& handle, + index_t num_paths, + real_t* ptr_d_rnd, // supplied + device_vec_t const& d_crt_out_deg, // non-coalesced + seed_t seed = seed_t{}) + : handle_(handle), + seed_(seed), + num_paths_(num_paths), + d_ptr_out_degs_(raw_const_ptr(d_crt_out_deg)), + d_ptr_random_(ptr_d_rnd) + { + } + + // in place: + // for each v in [0, num_paths) { + // if out_deg(v) > 0 + // d_col_indx[v] = random index in [0, out_deg(v)) + //} + void generate_col_indices(device_vec_t& d_col_indx) const + { + thrust::transform_if( + rmm::exec_policy(handle_.get_stream_view()), + d_ptr_random_, + d_ptr_random_ + num_paths_, // input1 + d_ptr_out_degs_, // input2 + d_ptr_out_degs_, // also stencil + d_col_indx.begin(), + [] __device__(real_t rnd_vindx, edge_t crt_out_deg) { + real_t max_ub = static_cast(crt_out_deg - 1); + auto interp_vindx = rnd_vindx * max_ub + real_t{.5}; + vertex_t v_indx = static_cast(interp_vindx); + return (v_indx >= crt_out_deg ? crt_out_deg - 1 : v_indx); + }, + [] __device__(auto crt_out_deg) { return crt_out_deg > 0; }); + } + + // abstracts away the random values generation: + // + static void generate_random(raft::handle_t const& handle, real_t* p_d_rnd, size_t sz, seed_t seed) + { + cugraph::detail::uniform_random_fill( + handle.get_stream_view(), p_d_rnd, sz, real_t{0.0}, real_t{1.0}, seed); + } + + private: + raft::handle_t const& handle_; + index_t num_paths_; + edge_t const* d_ptr_out_degs_; // device buffer with out-deg of current set of vertices (most + // recent vertex in each path); size = num_paths_ + real_t* d_ptr_random_; // device buffer with real random values; size = num_paths_ + seed_t seed_; // seed to be used for current batch +}; + +// seeding policy: time (clock) dependent, +// to avoid RW calls repeating same random data: +// +template +struct clock_seeding_t { + clock_seeding_t(void) = default; + + seed_t operator()(void) { return static_cast(std::time(nullptr)); } +}; + +// seeding policy: fixed for debug/testing repro +// +template +struct fixed_seeding_t { + // purposely no default cnstr. + + fixed_seeding_t(seed_t seed) : seed_(seed) {} + seed_t operator()(void) { return seed_; } + + private: + seed_t seed_; +}; + +// classes abstracting the next vertex extraction mechanism: +// +// primary template, purposely undefined +template +struct col_indx_extract_t; + +// specialization for single-gpu functionality: +// +template +struct col_indx_extract_t> { + using vertex_t = typename graph_t::vertex_type; + using edge_t = typename graph_t::edge_type; + using weight_t = typename graph_t::weight_type; + + col_indx_extract_t(raft::handle_t const& handle, + graph_t const& graph, + edge_t const* p_d_crt_out_degs, + index_t const* p_d_sizes, + index_t num_paths, + index_t max_depth) + : handle_(handle), + col_indices_(graph.get_matrix_partition_view().get_indices()), + row_offsets_(graph.get_matrix_partition_view().get_offsets()), + values_(graph.get_matrix_partition_view().get_weights()), + out_degs_(p_d_crt_out_degs), + sizes_(p_d_sizes), + num_paths_(num_paths), + max_depth_(max_depth) + { + } + + // in-place extractor of next set of vertices and weights, + // (d_v_next_vertices, d_v_next_weights), + // given start set of vertices. d_v_src_vertices, + // and corresponding column index set, d_v_col_indx: + // + // for each indx in [0, num_paths){ + // v_indx = d_v_src_vertices[indx*max_depth + d_sizes[indx] - 1]; + // if( out_degs_[v_indx] > 0 ) { + // start_row = row_offsets_[v_indx]; + // delta = d_v_col_indx[indx]; + // d_v_next_vertices[indx] = col_indices_[start_row + delta]; + // } + // (use tranform_if() with transform iterator) + // + void operator()( + device_vec_t const& d_coalesced_src_v, // in: coalesced vector of vertices + device_vec_t const& + d_v_col_indx, // in: column indices, given by stepper's random engine + device_vec_t& d_v_next_vertices, // out: set of destination vertices, for next step + device_vec_t& + d_v_next_weights) // out: set of weights between src and destination vertices, for next step + const + { + thrust::transform_if( + rmm::exec_policy(handle_.get_stream_view()), + thrust::make_counting_iterator(0), + thrust::make_counting_iterator(num_paths_), // input1 + d_v_col_indx.begin(), // input2 + out_degs_, // stencil + thrust::make_zip_iterator( + thrust::make_tuple(d_v_next_vertices.begin(), d_v_next_weights.begin())), // output + [max_depth = max_depth_, + ptr_d_sizes = sizes_, + ptr_d_coalesced_v = raw_const_ptr(d_coalesced_src_v), + row_offsets = row_offsets_, + col_indices = col_indices_, + values = values_ ? thrust::optional{*values_} + : thrust::nullopt] __device__(auto indx, auto col_indx) { + auto delta = ptr_d_sizes[indx] - 1; + auto v_indx = ptr_d_coalesced_v[indx * max_depth + delta]; + auto start_row = row_offsets[v_indx]; + + auto weight_value = (values ? (*values)[start_row + col_indx] + : weight_t{1}); // account for un-weighted graphs + return thrust::make_tuple(col_indices[start_row + col_indx], weight_value); + }, + [] __device__(auto crt_out_deg) { return crt_out_deg > 0; }); + } + + private: + raft::handle_t const& handle_; + vertex_t const* col_indices_; + edge_t const* row_offsets_; + std::optional values_; + + edge_t const* out_degs_; + index_t const* sizes_; + index_t num_paths_; + index_t max_depth_; +}; + +/** + * @brief Class abstracting the RW initialization, stepping, and stopping functionality + * The outline of the algorithm is as follows: + * + * (1) vertex sets are coalesced into d_coalesced_v, + * weight sets are coalesced into d_coalesced_w; + * i.e., the 2 coalesced vectors are allocated to + * num_paths * max_depth, and num_paths * (max_depth -1), respectively + * (since each path has a number of edges equal one + * less than the number of vertices); + * d_coalesced_v is initialized for each i*max_depth entry + * (i=0,,,,num_paths-1) to the corresponding starting vertices; + * (2) d_sizes maintains the current size is for each path; + * Note that a path may end prematurely if it reaches a sink vertex; + * (3) d_crt_out_degs maintains the out-degree of each of the latest + * vertices in the path; i.e., if N(v) := set of destination + * vertices from v, then this vector stores |N(v)| + * for last v in each path; i.e., + * d_crt_out_degs[i] = + * out-degree( d_coalesced_v[i*max_depth + d_sizes[i]-1] ), + * for i in {0,..., num_paths-1}; + * (4) a set of num_paths floating point numbers between [0,1] + * are generated at each step; then they get translated into + * _indices_ k in {0,...d_crt_out_degs[i]-1}; + * (5) the next vertex v is then picked as the k-th out-neighbor: + * next(v) = N(v)[k]; + * (6) d_sizes are incremented accordingly; i.e., for those paths whose + * corresponding last vertex has out-degree > 0; + * (7) then next(v) and corresponding weight of (v, next(v)) are stored + * at appropriate location in their corresponding coalesced vectors; + * (8) the client of this class (the random_walks() function) then repeats + * this process max_depth times or until all paths + * have reached sinks; i.e., d_crt_out_degs = {0, 0,...,0}, + * whichever comes first; + * (9) in the end some post-processing is done (stop()) to remove + * unused entries from the 2 coalesced vectors; + * (10) the triplet made of the 2 coalesced vectors and d_sizes is then returned; + * + */ +template , + typename index_t = typename graph_t::edge_type> +struct random_walker_t { + using vertex_t = typename graph_t::vertex_type; + using edge_t = typename graph_t::edge_type; + using weight_t = typename graph_t::weight_type; + using seed_t = typename random_engine_t::seed_type; + using real_t = typename random_engine_t::real_type; + using rnd_engine_t = random_engine_t; + + random_walker_t(raft::handle_t const& handle, + graph_t const& graph, + index_t num_paths, + index_t max_depth, + vertex_t v_padding_val = 0, + weight_t w_padding_val = 0) + : handle_(handle), + num_paths_(num_paths), + max_depth_(max_depth), + d_cached_out_degs_(graph.compute_out_degrees(handle_)), + vertex_padding_value_(v_padding_val != 0 ? v_padding_val : graph.get_number_of_vertices()), + weight_padding_value_(w_padding_val) + { + } + + // for each i in [0..num_paths_) { + // d_paths_v_set[i*max_depth] = d_src_init_v[i]; + // + void start(device_const_vector_view& d_src_init_v, // in: start set + device_vec_t& d_paths_v_set, // out: coalesced v + device_vec_t& d_sizes) const // out: init sizes to {1,...} + { + // intialize path sizes to 1, as they contain at least one vertex each: + // the initial set: d_src_init_v; + // + thrust::copy_n(rmm::exec_policy(handle_.get_stream_view()), + thrust::make_constant_iterator(1), + num_paths_, + d_sizes.begin()); + + // scatter d_src_init_v to coalesced vertex vector: + // + auto dlambda = [stride = max_depth_] __device__(auto indx) { return indx * stride; }; + + // use the transform iterator as map: + // + auto map_it_begin = + thrust::make_transform_iterator(thrust::make_counting_iterator(0), dlambda); + + thrust::scatter(rmm::exec_policy(handle_.get_stream_view()), + d_src_init_v.begin(), + d_src_init_v.end(), + map_it_begin, + d_paths_v_set.begin()); + } + + // overload for start() with device_uvector d_v_start + // (handy for testing) + // + void start(device_vec_t const& d_start, // in: start set + device_vec_t& d_paths_v_set, // out: coalesced v + device_vec_t& d_sizes) const // out: init sizes to {1,...} + { + device_const_vector_view d_start_cview{d_start.data(), + static_cast(d_start.size())}; + + start(d_start_cview, d_paths_v_set, d_sizes); + } + + // in-place updates its arguments from one step to next + // (to avoid copying); all "crt" arguments are updated at each step() + // and passed as scratchpad space to avoid copying them + // from one step to another + // + // take one step in sync for all paths that have not reached sinks: + // + void step( + graph_t const& graph, + seed_t seed, + device_vec_t& d_coalesced_v, // crt coalesced vertex set + device_vec_t& d_coalesced_w, // crt coalesced weight set + device_vec_t& d_paths_sz, // crt paths sizes + device_vec_t& d_crt_out_degs, // crt out-degs for current set of vertices + device_vec_t& d_random, // crt set of random real values + device_vec_t& d_col_indx, // crt col col indices to be used for retrieving next step + device_vec_t& d_next_v, // crt set of destination vertices, for next step + device_vec_t& d_next_w) + const // set of weights between src and destination vertices, for next step + { + // update crt snapshot of out-degs, + // from cached out degs, using + // latest vertex in each path as source: + // + gather_from_coalesced( + d_coalesced_v, d_cached_out_degs_, d_paths_sz, d_crt_out_degs, max_depth_, num_paths_); + + // generate random destination indices: + // + random_engine_t rgen(handle_, num_paths_, d_random, d_crt_out_degs, seed); + + rgen.generate_col_indices(d_col_indx); + + // dst extraction from dst indices: + // + col_indx_extract_t col_extractor(handle_, + graph, + raw_const_ptr(d_crt_out_degs), + raw_const_ptr(d_paths_sz), + num_paths_, + max_depth_); + + // The following steps update the next entry in each path, + // except the paths that reached sinks; + // + // for each indx in [0..num_paths) { + // v_indx = d_v_rnd_n_indx[indx]; + // + // -- get the `v_indx`-th out-vertex of d_v_paths_v_set[indx] vertex: + // -- also, note the size deltas increased by 1 in dst (d_sizes[]): + // + // d_coalesced_v[indx*num_paths + d_sizes[indx]] = + // get_out_vertex(graph, d_coalesced_v[indx*num_paths + d_sizes[indx] -1)], v_indx); + // d_coalesced_w[indx*(num_paths-1) + d_sizes[indx] - 1] = + // get_out_edge_weight(graph, d_coalesced_v[indx*num_paths + d_sizes[indx]-2], v_indx); + // + // (1) generate actual vertex destinations: + // + col_extractor(d_coalesced_v, d_col_indx, d_next_v, d_next_w); + + // (2) update path sizes: + // + update_path_sizes(d_crt_out_degs, d_paths_sz); + + // (3) actual coalesced updates: + // + scatter_vertices(d_next_v, d_coalesced_v, d_crt_out_degs, d_paths_sz); + scatter_weights(d_next_w, d_coalesced_w, d_crt_out_degs, d_paths_sz); + } + + // step() version that doesn't update the random vector: + // (the caller supplies it) + // + void step_only( + graph_t const& graph, + device_vec_t& d_coalesced_v, // crt coalesced vertex set + device_vec_t& d_coalesced_w, // crt coalesced weight set + device_vec_t& d_paths_sz, // crt paths sizes + device_vec_t& d_crt_out_degs, // crt out-degs for current set of vertices + real_t* ptr_d_random, // crt set of random real values (supplied) + device_vec_t& d_col_indx, // crt col col indices to be used for retrieving next step + device_vec_t& d_next_v, // crt set of destination vertices, for next step + device_vec_t& d_next_w) + const // set of weights between src and destination vertices, for next step + { + // update crt snapshot of out-degs, + // from cached out degs, using + // latest vertex in each path as source: + // + gather_from_coalesced( + d_coalesced_v, d_cached_out_degs_, d_paths_sz, d_crt_out_degs, max_depth_, num_paths_); + + // generate random destination indices: + // + random_engine_t rgen(handle_, num_paths_, ptr_d_random, d_crt_out_degs); + + rgen.generate_col_indices(d_col_indx); + + // dst extraction from dst indices: + // + col_indx_extract_t col_extractor(handle_, + graph, + raw_const_ptr(d_crt_out_degs), + raw_const_ptr(d_paths_sz), + num_paths_, + max_depth_); + + // The following steps update the next entry in each path, + // except the paths that reached sinks; + // + // for each indx in [0..num_paths) { + // v_indx = d_v_rnd_n_indx[indx]; + // + // -- get the `v_indx`-th out-vertex of d_v_paths_v_set[indx] vertex: + // -- also, note the size deltas increased by 1 in dst (d_sizes[]): + // + // d_coalesced_v[indx*num_paths + d_sizes[indx]] = + // get_out_vertex(graph, d_coalesced_v[indx*num_paths + d_sizes[indx] -1)], v_indx); + // d_coalesced_w[indx*(num_paths-1) + d_sizes[indx] - 1] = + // get_out_edge_weight(graph, d_coalesced_v[indx*num_paths + d_sizes[indx]-2], v_indx); + // + // (1) generate actual vertex destinations: + // + col_extractor(d_coalesced_v, d_col_indx, d_next_v, d_next_w); + + // (2) update path sizes: + // + update_path_sizes(d_crt_out_degs, d_paths_sz); + + // (3) actual coalesced updates: + // + scatter_vertices(d_next_v, d_coalesced_v, d_crt_out_degs, d_paths_sz); + scatter_weights(d_next_w, d_coalesced_w, d_crt_out_degs, d_paths_sz); + } + + // returns true if all paths reached sinks: + // + bool all_paths_stopped(device_vec_t const& d_crt_out_degs) const + { + auto how_many_stopped = + thrust::count_if(rmm::exec_policy(handle_.get_stream_view()), + d_crt_out_degs.begin(), + d_crt_out_degs.end(), + [] __device__(auto crt_out_deg) { return crt_out_deg == 0; }); + return (static_cast(how_many_stopped) == d_crt_out_degs.size()); + } + + // wrap-up, post-process: + // truncate v_set, w_set to actual space used + // + void stop(device_vec_t& d_coalesced_v, // coalesced vertex set + device_vec_t& d_coalesced_w, // coalesced weight set + device_vec_t const& d_sizes) const // paths sizes + { + assert(max_depth_ > 1); // else, no need to step; and no edges + + index_t const* ptr_d_sizes = d_sizes.data(); + + auto predicate_v = [max_depth = max_depth_, ptr_d_sizes] __device__(auto indx) { + auto row_indx = indx / max_depth; + auto col_indx = indx % max_depth; + + return (col_indx >= ptr_d_sizes[row_indx]); + }; + + auto predicate_w = [max_depth = max_depth_, ptr_d_sizes] __device__(auto indx) { + auto row_indx = indx / (max_depth - 1); + auto col_indx = indx % (max_depth - 1); + + return (col_indx >= ptr_d_sizes[row_indx] - 1); + }; + + auto new_end_v = thrust::remove_if(rmm::exec_policy(handle_.get_stream_view()), + d_coalesced_v.begin(), + d_coalesced_v.end(), + thrust::make_counting_iterator(0), + predicate_v); + + auto new_end_w = thrust::remove_if(rmm::exec_policy(handle_.get_stream_view()), + d_coalesced_w.begin(), + d_coalesced_w.end(), + thrust::make_counting_iterator(0), + predicate_w); + + handle_.get_stream_view().synchronize(); + + d_coalesced_v.resize(thrust::distance(d_coalesced_v.begin(), new_end_v), handle_.get_stream()); + d_coalesced_w.resize(thrust::distance(d_coalesced_w.begin(), new_end_w), handle_.get_stream()); + } + + // in-place non-static (needs handle_): + // for indx in [0, nelems): + // gather d_result[indx] = d_src[d_coalesced[indx*stride + d_sizes[indx] -1]] + // + template + void gather_from_coalesced( + device_vec_t const& d_coalesced, // |gather map| = stride*nelems + device_vec_t const& d_src, // |gather input| = nelems + device_vec_t const& d_sizes, // |paths sizes| = nelems, elems in [1, stride] + device_vec_t& d_result, // |output| = nelems + index_t stride, // stride = coalesce block size (typically max_depth) + index_t nelems) const // nelems = number of elements to gather (typically num_paths_) + { + vertex_t const* ptr_d_coalesced = raw_const_ptr(d_coalesced); + index_t const* ptr_d_sizes = raw_const_ptr(d_sizes); + + // delta = ptr_d_sizes[indx] - 1 + // + auto dlambda = [stride, ptr_d_sizes, ptr_d_coalesced] __device__(auto indx) { + auto delta = ptr_d_sizes[indx] - 1; + return ptr_d_coalesced[indx * stride + delta]; + }; + + // use the transform iterator as map: + // + auto map_it_begin = + thrust::make_transform_iterator(thrust::make_counting_iterator(0), dlambda); + + thrust::gather(rmm::exec_policy(handle_.get_stream_view()), + map_it_begin, + map_it_begin + nelems, + d_src.begin(), + d_result.begin()); + } + + // in-place non-static (needs handle_); + // pre-condition: path sizes are assumed updated + // to reflect new vertex additions; + // + // for indx in [0, nelems): + // if ( d_crt_out_degs[indx] > 0 ) + // d_coalesced[indx*stride + (d_sizes[indx] - adjust)- 1] = d_src[indx] + // + // adjust := 0 for coalesced vertices; 1 for weights + // (because |edges| = |vertices| - 1, in each path); + // + template + void scatter_to_coalesced( + device_vec_t const& d_src, // |scatter input| = nelems + device_vec_t& d_coalesced, // |scatter input| = stride*nelems + device_vec_t const& d_crt_out_degs, // |current set of vertex out degrees| = nelems, + // to be used as stencil (don't scatter if 0) + device_vec_t const& + d_sizes, // paths sizes used to provide delta in coalesced paths; + // pre-condition: assumed as updated to reflect new vertex additions; + // also, this is the number of _vertices_ in each path; + // hence for scattering weights this needs to be adjusted; hence the `adjust` parameter + index_t + stride, // stride = coalesce block size (max_depth for vertices; max_depth-1 for weights) + index_t nelems, // nelems = number of elements to gather (typically num_paths_) + index_t adjust = 0) + const // adjusting parameter for scattering vertices (0) or weights (1); see above for more; + { + index_t const* ptr_d_sizes = raw_const_ptr(d_sizes); + + auto dlambda = [stride, adjust, ptr_d_sizes] __device__(auto indx) { + auto delta = ptr_d_sizes[indx] - adjust - 1; + return indx * stride + delta; + }; + + // use the transform iterator as map: + // + auto map_it_begin = + thrust::make_transform_iterator(thrust::make_counting_iterator(0), dlambda); + + thrust::scatter_if(rmm::exec_policy(handle_.get_stream_view()), + d_src.begin(), + d_src.end(), + map_it_begin, + d_crt_out_degs.begin(), + d_coalesced.begin(), + [] __device__(auto crt_out_deg) { + return crt_out_deg > 0; // predicate + }); + } + + // updates the entries in the corresponding coalesced vector, + // for which out_deg > 0 + // + void scatter_vertices(device_vec_t const& d_src, + device_vec_t& d_coalesced, + device_vec_t const& d_crt_out_degs, + device_vec_t const& d_sizes) const + { + scatter_to_coalesced(d_src, d_coalesced, d_crt_out_degs, d_sizes, max_depth_, num_paths_); + } + // + void scatter_weights(device_vec_t const& d_src, + device_vec_t& d_coalesced, + device_vec_t const& d_crt_out_degs, + device_vec_t const& d_sizes) const + { + scatter_to_coalesced( + d_src, d_coalesced, d_crt_out_degs, d_sizes, max_depth_ - 1, num_paths_, 1); + } + + // in-place update (increment) path sizes for paths + // that have not reached a sink; i.e., for which + // d_crt_out_degs[indx]>0: + // + void update_path_sizes(device_vec_t const& d_crt_out_degs, + device_vec_t& d_sizes) const + { + thrust::transform_if( + rmm::exec_policy(handle_.get_stream_view()), + d_sizes.begin(), + d_sizes.end(), // input + d_crt_out_degs.begin(), // stencil + d_sizes.begin(), // output: in-place + [] __device__(auto crt_sz) { return crt_sz + 1; }, + [] __device__(auto crt_out_deg) { return crt_out_deg > 0; }); + } + + device_vec_t const& get_out_degs(void) const { return d_cached_out_degs_; } + + vertex_t get_vertex_padding_value(void) const { return vertex_padding_value_; } + + weight_t get_weight_padding_value(void) const { return weight_padding_value_; } + + void init_padding(device_vec_t& d_coalesced_v, + device_vec_t& d_coalesced_w) const + { + thrust::fill(rmm::exec_policy(handle_.get_stream_view()), + d_coalesced_v.begin(), + d_coalesced_v.end(), + vertex_padding_value_); + + thrust::fill(rmm::exec_policy(handle_.get_stream_view()), + d_coalesced_w.begin(), + d_coalesced_w.end(), + weight_padding_value_); + } + + decltype(auto) get_handle(void) const { return handle_; } + + private: + raft::handle_t const& handle_; + index_t num_paths_; + index_t max_depth_; + device_vec_t d_cached_out_degs_; + vertex_t const vertex_padding_value_; + weight_t const weight_padding_value_; +}; + +/** + * @brief returns random walks (RW) from starting sources, where each path is of given maximum + * length. Single-GPU specialization. + * + * @tparam graph_t Type of graph (view). + * @tparam traversal_t Traversal policy. Either horizontal (faster but requires more memory) or + * vertical. Defaults to horizontal. + * @tparam random_engine_t Type of random engine used to generate RW. + * @tparam seeding_policy_t Random engine seeding policy: variable or fixed (for reproducibility). + * Defaults to variable, clock dependent. + * @tparam index_t Indexing type. Defaults to edge_type. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param graph Graph object to generate RW on. + * @param d_v_start Device (view) set of starting vertex indices for the RW. + * number(paths) == d_v_start.size(). + * @param max_depth maximum length of RWs. + * @param use_padding (optional) specifies if return uses padded format (true), or coalesced + * (compressed) format; when padding is used the output is a matrix of vertex paths and a matrix of + * edges paths (weights); in this case the matrices are stored in row major order; the vertex path + * matrix is padded with `num_vertices` values and the weight matrix is padded with `0` values; + * @param seeder (optional) is object providing the random seeding mechanism. Defaults to local + * clock time as initial seed. + * @return std::tuple, device_vec_t, + * device_vec_t> Triplet of either padded or coalesced RW paths; in the coalesced case + * (default), the return consists of corresponding vertex and edge weights for each, and + * corresponding path sizes. This is meant to minimize the number of DF's to be passed to the Python + * layer. The meaning of "coalesced" here is that a 2D array of paths of different sizes is + * represented as a 1D contiguous array. In the padded case the return is a matrix of num_paths x + * max_depth vertex paths; and num_paths x (max_depth-1) edge (weight) paths, with an empty array of + * sizes. Note: if the graph is un-weighted the edge (weight) paths consists of `weight_t{1}` + * entries; + */ +template , + typename seeding_policy_t = clock_seeding_t, + typename index_t = typename graph_t::edge_type> +std::enable_if_t, + device_vec_t, + device_vec_t, + typename random_engine_t::seed_type>> +random_walks_impl(raft::handle_t const& handle, + graph_t const& graph, + device_const_vector_view& d_v_start, + index_t max_depth, + bool use_padding = false, + seeding_policy_t seeder = clock_seeding_t{}) +{ + using vertex_t = typename graph_t::vertex_type; + using edge_t = typename graph_t::edge_type; + using weight_t = typename graph_t::weight_type; + using seed_t = typename random_engine_t::seed_type; + using real_t = typename random_engine_t::real_type; + + vertex_t num_vertices = graph.get_number_of_vertices(); + + auto how_many_valid = thrust::count_if(rmm::exec_policy(handle.get_stream_view()), + d_v_start.begin(), + d_v_start.end(), + [num_vertices] __device__(auto crt_vertex) { + return (crt_vertex >= 0) && (crt_vertex < num_vertices); + }); + + CUGRAPH_EXPECTS(static_cast(how_many_valid) == d_v_start.size(), + "Invalid set of starting vertices."); + + auto num_paths = d_v_start.size(); + auto stream = handle.get_stream(); + + random_walker_t rand_walker{ + handle, graph, static_cast(num_paths), static_cast(max_depth)}; + + // pre-allocate num_paths * max_depth; + // + auto coalesced_sz = num_paths * max_depth; + device_vec_t d_coalesced_v(coalesced_sz, stream); // coalesced vertex set + device_vec_t d_coalesced_w(coalesced_sz, stream); // coalesced weight set + device_vec_t d_paths_sz(num_paths, stream); // paths sizes + + // traversal policy: + // + traversal_t traversor(num_paths, max_depth); + + auto tmp_buff_sz = traversor.get_tmp_buff_sz(); + + device_vec_t d_crt_out_degs(tmp_buff_sz, stream); // crt vertex set out-degs + device_vec_t d_col_indx(tmp_buff_sz, stream); // \in {0,..,out-deg(v)} + device_vec_t d_next_v(tmp_buff_sz, stream); // crt set of next vertices + device_vec_t d_next_w(tmp_buff_sz, stream); // crt set of next weights + + // random data handling: + // + auto rnd_data_sz = traversor.get_random_buff_sz(); + device_vec_t d_random(rnd_data_sz, stream); + // abstracted out seed initialization: + // + seed_t seed0 = static_cast(seeder()); + + // if padding used, initialize padding values: + // + if (use_padding) rand_walker.init_padding(d_coalesced_v, d_coalesced_w); + + // very first vertex, for each path: + // + rand_walker.start(d_v_start, d_coalesced_v, d_paths_sz); + + // traverse paths: + // + traversor(graph, + rand_walker, + seed0, + d_coalesced_v, + d_coalesced_w, + d_paths_sz, + d_crt_out_degs, + d_random, + d_col_indx, + d_next_v, + d_next_w); + + // wrap-up, post-process: + // truncate v_set, w_set to actual space used + // unless padding is used + // + if (!use_padding) { rand_walker.stop(d_coalesced_v, d_coalesced_w, d_paths_sz); } + + // because device_uvector is not copy-cnstr-able: + // + if (!use_padding) { + return std::make_tuple(std::move(d_coalesced_v), + std::move(d_coalesced_w), + std::move(d_paths_sz), + seed0); // also return seed for repro + } else { + return std::make_tuple( + std::move(d_coalesced_v), + std::move(d_coalesced_w), + device_vec_t(0, stream), // purposely empty size array for the padded case, to avoid + // unnecessary allocations + seed0); // also return seed for repro + } +} + +/** + * @brief returns random walks (RW) from starting sources, where each path is of given maximum + * length. Multi-GPU specialization. + * + * @tparam graph_t Type of graph (view). + * @tparam traversal_t Traversal policy. Either horizontal (faster but requires more memory) or + * vertical. Defaults to horizontal. + * @tparam random_engine_t Type of random engine used to generate RW. + * @tparam seeding_policy_t Random engine seeding policy: variable or fixed (for reproducibility). + * Defaults to variable, clock dependent. + * @tparam index_t Indexing type. Defaults to edge_type. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param graph Graph object to generate RW on. + * @param d_v_start Device (view) set of starting vertex indices for the RW. number(RW) == + * d_v_start.size(). + * @param max_depth maximum length of RWs. + * @param use_padding (optional) specifies if return uses padded format (true), or coalesced + * (compressed) format; when padding is used the output is a matrix of vertex paths and a matrix of + * edges paths (weights); in this case the matrices are stored in row major order; the vertex path + * matrix is padded with `num_vertices` values and the weight matrix is padded with `0` values; + * @param seeder (optional) is object providing the random seeding mechanism. Defaults to local + * clock time as initial seed. + * @return std::tuple, device_vec_t, + * device_vec_t> Triplet of either padded or coalesced RW paths; in the coalesced case + * (default), the return consists of corresponding vertex and edge weights for each, and + * corresponding path sizes. This is meant to minimize the number of DF's to be passed to the Python + * layer. The meaning of "coalesced" here is that a 2D array of paths of different sizes is + * represented as a 1D contiguous array. In the padded case the return is a matrix of num_paths x + * max_depth vertex paths; and num_paths x (max_depth-1) edge (weight) paths, with an empty array of + * sizes. Note: if the graph is un-weighted the edge (weight) paths consists of `weight_t{1}` + * entries; + */ +template , + typename seeding_policy_t = clock_seeding_t, + typename index_t = typename graph_t::edge_type> +std::enable_if_t, + device_vec_t, + device_vec_t, + typename random_engine_t::seed_type>> +random_walks_impl(raft::handle_t const& handle, + graph_t const& graph, + device_const_vector_view& d_v_start, + index_t max_depth, + bool use_padding = false, + seeding_policy_t seeder = clock_seeding_t{}) +{ + CUGRAPH_FAIL("Not implemented yet."); +} + +// provides conversion to (coalesced) path to COO format: +// (which in turn provides an API consistent with egonet) +// +template +struct coo_convertor_t { + coo_convertor_t(raft::handle_t const& handle, index_t num_paths) + : handle_(handle), num_paths_(num_paths) + { + } + + std::tuple, device_vec_t, device_vec_t> operator()( + device_const_vector_view& d_coalesced_v, + device_const_vector_view& d_sizes) const + { + CUGRAPH_EXPECTS(static_cast(d_sizes.size()) == num_paths_, "Invalid size vector."); + + auto tupl_fill = fill_stencil(d_sizes); + auto&& d_stencil = std::move(std::get<0>(tupl_fill)); + auto total_sz_v = std::get<1>(tupl_fill); + auto&& d_sz_incl_scan = std::move(std::get<2>(tupl_fill)); + + CUGRAPH_EXPECTS(static_cast(d_coalesced_v.size()) == total_sz_v, + "Inconsistent vertex coalesced size data."); + + auto src_dst_tpl = gather_pairs(d_coalesced_v, d_stencil, total_sz_v); + + auto&& d_src = std::move(std::get<0>(src_dst_tpl)); + auto&& d_dst = std::move(std::get<1>(src_dst_tpl)); + + device_vec_t d_sz_w_scan(num_paths_, handle_.get_stream()); + + // copy vertex path sizes that are > 1: + // (because vertex_path_sz translates + // into edge_path_sz = vertex_path_sz - 1, + // and edge_paths_sz == 0 don't contribute + // anything): + // + auto new_end_it = thrust::copy_if(rmm::exec_policy(handle_.get_stream_view()), + d_sizes.begin(), + d_sizes.end(), + d_sz_w_scan.begin(), + [] __device__(auto sz_value) { return sz_value > 1; }); + + // resize to new_end: + // + d_sz_w_scan.resize(thrust::distance(d_sz_w_scan.begin(), new_end_it), handle_.get_stream()); + + // get paths' edge number exclusive scan + // by transforming paths' vertex numbers that + // are > 1, via tranaformation: + // edge_path_sz = (vertex_path_sz-1): + // + thrust::transform_exclusive_scan( + rmm::exec_policy(handle_.get_stream_view()), + d_sz_w_scan.begin(), + d_sz_w_scan.end(), + d_sz_w_scan.begin(), + [] __device__(auto sz) { return sz - 1; }, + index_t{0}, + thrust::plus{}); + + return std::make_tuple(std::move(d_src), std::move(d_dst), std::move(d_sz_w_scan)); + } + + std::tuple, index_t, device_vec_t> fill_stencil( + device_const_vector_view& d_sizes) const + { + device_vec_t d_scan(num_paths_, handle_.get_stream()); + thrust::inclusive_scan( + rmm::exec_policy(handle_.get_stream_view()), d_sizes.begin(), d_sizes.end(), d_scan.begin()); + + index_t total_sz{0}; + CUDA_TRY(cudaMemcpy( + &total_sz, raw_ptr(d_scan) + num_paths_ - 1, sizeof(index_t), cudaMemcpyDeviceToHost)); + + device_vec_t d_stencil(total_sz, handle_.get_stream()); + + // initialize stencil to all 1's: + // + thrust::copy_n(rmm::exec_policy(handle_.get_stream_view()), + thrust::make_constant_iterator(1), + d_stencil.size(), + d_stencil.begin()); + + // set to 0 entries positioned at inclusive_scan(sizes[]), + // because those are path "breakpoints", where a path end + // and the next one starts, hence there cannot be an edge + // between a path ending vertex and next path starting vertex; + // + thrust::scatter(rmm::exec_policy(handle_.get_stream_view()), + thrust::make_constant_iterator(0), + thrust::make_constant_iterator(0) + num_paths_, + d_scan.begin(), + d_stencil.begin()); + + return std::make_tuple(std::move(d_stencil), total_sz, std::move(d_scan)); + } + + std::tuple, device_vec_t> gather_pairs( + device_const_vector_view& d_coalesced_v, + device_vec_t const& d_stencil, + index_t total_sz_v) const + { + auto total_sz_w = total_sz_v - num_paths_; + device_vec_t valid_src_indx(total_sz_w, handle_.get_stream()); + + // generate valid vertex src indices, + // which is any index in {0,...,total_sz_v - 2} + // provided the next index position; i.e., (index+1), + // in stencil is not 0; (if it is, there's no "next" + // or dst index, because the path has ended); + // + thrust::copy_if(rmm::exec_policy(handle_.get_stream_view()), + thrust::make_counting_iterator(0), + thrust::make_counting_iterator(total_sz_v - 1), + valid_src_indx.begin(), + [ptr_d_stencil = raw_const_ptr(d_stencil)] __device__(auto indx) { + auto dst_indx = indx + 1; + return ptr_d_stencil[dst_indx] == 1; + }); + + device_vec_t d_src_v(total_sz_w, handle_.get_stream()); + device_vec_t d_dst_v(total_sz_w, handle_.get_stream()); + + // construct pair of src[], dst[] by gathering + // from d_coalesced_v all pairs + // at entries (valid_src_indx, valid_src_indx+1), + // where the set of valid_src_indx was + // generated at the previous step; + // + thrust::transform( + rmm::exec_policy(handle_.get_stream_view()), + valid_src_indx.begin(), + valid_src_indx.end(), + thrust::make_zip_iterator(thrust::make_tuple(d_src_v.begin(), d_dst_v.begin())), // start_zip + [ptr_d_vertex = raw_const_ptr(d_coalesced_v)] __device__(auto indx) { + return thrust::make_tuple(ptr_d_vertex[indx], ptr_d_vertex[indx + 1]); + }); + + return std::make_tuple(std::move(d_src_v), std::move(d_dst_v)); + } + + private: + raft::handle_t const& handle_; + index_t num_paths_; +}; + +} // namespace detail + +/** + * @brief returns random walks (RW) from starting sources, where each path is of given maximum + * length. Uniform distribution is assumed for the random engine. + * + * @tparam graph_t Type of graph/view (typically, graph_view_t). + * @tparam index_t Type used to store indexing and sizes. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param graph Graph (view )object to generate RW on. + * @param ptr_d_start Device pointer to set of starting vertex indices for the RW. + * @param num_paths = number(paths). + * @param max_depth maximum length of RWs. + * @param use_padding (optional) specifies if return uses padded format (true), or coalesced + * (compressed) format; when padding is used the output is a matrix of vertex paths and a matrix of + * edges paths (weights); in this case the matrices are stored in row major order; the vertex path + * matrix is padded with `num_vertices` values and the weight matrix is padded with `0` values; + * @return std::tuple, rmm::device_uvector, + * rmm::device_uvector> Triplet of either padded or coalesced RW paths; in the coalesced + * case (default), the return consists of corresponding vertex and edge weights for each, and + * corresponding path sizes. This is meant to minimize the number of DF's to be passed to the Python + * layer. The meaning of "coalesced" here is that a 2D array of paths of different sizes is + * represented as a 1D contiguous array. In the padded case the return is a matrix of num_paths x + * max_depth vertex paths; and num_paths x (max_depth-1) edge (weight) paths, with an empty array of + * sizes. Note: if the graph is un-weighted the edge (weight) paths consists of `weight_t{1}` + * entries; + */ +template +std::tuple, + rmm::device_uvector, + rmm::device_uvector> +random_walks(raft::handle_t const& handle, + graph_t const& graph, + typename graph_t::vertex_type const* ptr_d_start, + index_t num_paths, + index_t max_depth, + bool use_padding) +{ + using vertex_t = typename graph_t::vertex_type; + using edge_t = typename graph_t::edge_type; + using weight_t = typename graph_t::weight_type; + using rnd_engine_t = float; + + // 0-copy const device view: + // + detail::device_const_vector_view d_v_start{ptr_d_start, num_paths}; + + // GPU memory availability: + // + size_t free_mem_sp_bytes{0}; + size_t total_mem_sp_bytes{0}; + cudaMemGetInfo(&free_mem_sp_bytes, &total_mem_sp_bytes); + + // GPU memory requirements: + // + size_t coalesced_v_count = num_paths * max_depth; + auto coalesced_e_count = coalesced_v_count - num_paths; + size_t req_mem_common = sizeof(vertex_t) * coalesced_v_count + + sizeof(weight_t) * coalesced_e_count + // coalesced_v + coalesced_w + (sizeof(vertex_t) + sizeof(index_t)) * num_paths; // start_v + sizes + + size_t req_mem_horizontal = + req_mem_common + sizeof(rnd_engine_t) * coalesced_e_count; // + rnd_buff + size_t req_mem_vertical = req_mem_common + (sizeof(edge_t) + 2 * sizeof(vertex_t) + + sizeof(weight_t) + sizeof(rnd_engine_t)) * + num_paths; // + smaller_rnd_buff + tmp_buffs + + bool use_vertical_strategy{false}; + if (req_mem_horizontal > req_mem_vertical && req_mem_horizontal > free_mem_sp_bytes) { + use_vertical_strategy = true; + std::cerr + << "WARNING: Due to GPU memory availability, slower vertical traversal will be used.\n"; + } + + if (use_vertical_strategy) { + auto quad_tuple = detail::random_walks_impl( + handle, graph, d_v_start, max_depth, use_padding); + // ignore last element of the quad, seed, + // since it's meant for testing / debugging, only: + // + return std::make_tuple(std::move(std::get<0>(quad_tuple)), + std::move(std::get<1>(quad_tuple)), + std::move(std::get<2>(quad_tuple))); + } else { + auto quad_tuple = detail::random_walks_impl(handle, graph, d_v_start, max_depth, use_padding); + // ignore last element of the quad, seed, + // since it's meant for testing / debugging, only: + // + return std::make_tuple(std::move(std::get<0>(quad_tuple)), + std::move(std::get<1>(quad_tuple)), + std::move(std::get<2>(quad_tuple))); + } +} + +/** + * @brief returns the COO format (src_vector, dst_vector) from the random walks (RW) + * paths. + * + * @tparam vertex_t Type of vertex indices. + * @tparam index_t Type used to store indexing and sizes. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param coalesced_sz_v coalesced vertex vector size. + * @param num_paths number of paths. + * @param d_coalesced_v coalesced vertex buffer. + * @param d_sizes paths size buffer. + * @return tuple of (src_vertex_vector, dst_Vertex_vector, path_offsets), where + * path_offsets are the offsets where the COO set of each path starts. + */ +template +std:: + tuple, rmm::device_uvector, rmm::device_uvector> + convert_paths_to_coo(raft::handle_t const& handle, + index_t coalesced_sz_v, + index_t num_paths, + rmm::device_buffer&& d_coalesced_v, + rmm::device_buffer&& d_sizes) +{ + detail::coo_convertor_t to_coo(handle, num_paths); + + detail::device_const_vector_view d_v_view( + static_cast(d_coalesced_v.data()), coalesced_sz_v); + + detail::device_const_vector_view d_sz_view(static_cast(d_sizes.data()), + num_paths); + + return to_coo(d_v_view, d_sz_view); +} + +/** + * @brief returns additional RW information on vertex paths offsets and weight path sizes and + * offsets, for the coalesced case (the padded case does not need or provide this information) + * + * @tparam index_t Type used to store indexing and sizes. + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param num_paths number of paths. + * @param ptr_d_sizes sizes of vertex paths. + * @return tuple of (vertex_path_offsets, weight_path_sizes, weight_path_offsets), where offsets are + * exclusive scan of corresponding sizes. + */ +template +std::tuple, rmm::device_uvector, rmm::device_uvector> +query_rw_sizes_offsets(raft::handle_t const& handle, index_t num_paths, index_t const* ptr_d_sizes) +{ + rmm::device_uvector d_vertex_offsets(num_paths, handle.get_stream()); + rmm::device_uvector d_weight_sizes(num_paths, handle.get_stream()); + rmm::device_uvector d_weight_offsets(num_paths, handle.get_stream()); + + thrust::exclusive_scan(rmm::exec_policy(handle.get_stream_view()), + ptr_d_sizes, + ptr_d_sizes + num_paths, + d_vertex_offsets.begin()); + + thrust::transform(rmm::exec_policy(handle.get_stream_view()), + ptr_d_sizes, + ptr_d_sizes + num_paths, + d_weight_sizes.begin(), + [] __device__(auto vertex_path_sz) { return vertex_path_sz - 1; }); + + handle.get_stream_view().synchronize(); + + thrust::exclusive_scan(rmm::exec_policy(handle.get_stream_view()), + d_weight_sizes.begin(), + d_weight_sizes.end(), + d_weight_offsets.begin()); + + return std::make_tuple( + std::move(d_vertex_offsets), std::move(d_weight_sizes), std::move(d_weight_offsets)); +} + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/src/sampling/rw_traversals.hpp b/cpp/src/sampling/rw_traversals.hpp new file mode 100644 index 00000000000..b2ba74e97a2 --- /dev/null +++ b/cpp/src/sampling/rw_traversals.hpp @@ -0,0 +1,299 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// +#pragma once + +#include + +#include + +#include +#include +#include + +#include +#include + +#include + +#include +#include +#include + +namespace cugraph { +namespace experimental { + +namespace detail { + +template +using device_vec_t = rmm::device_uvector; + +template +using device_v_it = typename device_vec_t::iterator; + +template +value_t* raw_ptr(device_vec_t& dv) +{ + return dv.data(); +} + +template +value_t const* raw_const_ptr(device_vec_t const& dv) +{ + return dv.data(); +} + +template +struct device_const_vector_view { + device_const_vector_view(value_t const* d_buffer, index_t size) : d_buffer_(d_buffer), size_(size) + { + } + + device_const_vector_view(device_const_vector_view const& other) = delete; + device_const_vector_view& operator=(device_const_vector_view const& other) = delete; + + device_const_vector_view(device_const_vector_view&& other) + { + d_buffer_ = other.d_buffer_; + size_ = other.size_; + } + device_const_vector_view& operator=(device_const_vector_view&& other) + { + d_buffer_ = other.d_buffer_; + size_ = other.size_; + + return *this; + } + + value_t const* begin(void) const { return d_buffer_; } + + value_t const* end() const { return d_buffer_ + size_; } + + index_t size(void) const { return size_; } + + private: + value_t const* d_buffer_{nullptr}; + index_t size_; +}; + +template +value_t const* raw_const_ptr(device_const_vector_view& dv) +{ + return dv.begin(); +} + +// classes abstracting the way the random walks path are generated: +// + +// vertical traversal proxy: +// a device vector of next vertices is generated for each path; +// when a vertex is a sink the corresponding path doesn't advance anymore; +// +// smaller memory footprint; +// +struct vertical_traversal_t { + vertical_traversal_t(size_t num_paths, size_t max_depth) + : num_paths_(num_paths), max_depth_(max_depth) + { + } + + template + void operator()( + graph_t const& graph, // graph being traversed + random_walker_t const& rand_walker, // random walker object for which traversal is driven + seed_t seed0, // initial seed value + device_vec_t& d_coalesced_v, // crt coalesced vertex set + device_vec_t& d_coalesced_w, // crt coalesced weight set + device_vec_t& d_paths_sz, // crt paths sizes + device_vec_t& + d_crt_out_degs, // crt out-degs for current set of vertices + device_vec_t& d_random, // crt set of random real values + device_vec_t& + d_col_indx, // crt col col indices to be used for retrieving next step + device_vec_t& + d_next_v, // crt set of destination vertices, for next step + device_vec_t& + d_next_w) // set of weights between src and destination vertices, for next step + const + { + // start from 1, as 0-th was initialized above: + // + for (decltype(max_depth_) step_indx = 1; step_indx < max_depth_; ++step_indx) { + // take one-step in-sync for each path in parallel: + // + rand_walker.step(graph, + seed0 + static_cast(step_indx), + d_coalesced_v, + d_coalesced_w, + d_paths_sz, + d_crt_out_degs, + d_random, + d_col_indx, + d_next_v, + d_next_w); + + // early exit: all paths have reached sinks: + // + if (rand_walker.all_paths_stopped(d_crt_out_degs)) break; + } + } + + size_t get_random_buff_sz(void) const { return num_paths_; } + size_t get_tmp_buff_sz(void) const { return num_paths_; } + + private: + size_t num_paths_; + size_t max_depth_; +}; + +// horizontal traversal proxy: +// each path is generated independently from start to finish; +// when a vertex is a sink the corresponding path doesn't advance anymore; +// requires (num_paths x max_depth) precomputed real random values in [0,1]; +// +// larger memory footprint, but potentially more efficient; +// +struct horizontal_traversal_t { + horizontal_traversal_t(size_t num_paths, size_t max_depth) + : num_paths_(num_paths), max_depth_(max_depth) + { + } + + template + void operator()( + graph_t const& graph, // graph being traversed + random_walker_t const& rand_walker, // random walker object for which traversal is driven + seed_t seed0, // initial seed value + device_vec_t& d_coalesced_v, // crt coalesced vertex set + device_vec_t& d_coalesced_w, // crt coalesced weight set + device_vec_t& d_paths_sz, // crt paths sizes + device_vec_t& + d_crt_out_degs, // ignored: out-degs for the current set of vertices + device_vec_t& d_random, // _entire_ set of random real values + device_vec_t& + d_col_indx, // ignored: crt col indices to be used for retrieving next step + device_vec_t& + d_next_v, // ignored: crt set of destination vertices, for next step (coalesced set + // updated directly, instead) + device_vec_t& + d_next_w) // ignored: set of weights between src and destination vertices, for next step + // (coalesced set updated directly, instead) + const + { + using vertex_t = typename graph_t::vertex_type; + using edge_t = typename graph_t::edge_type; + using weight_t = typename graph_t::weight_type; + using random_engine_t = typename random_walker_t::rnd_engine_t; + + auto const& handle = rand_walker.get_handle(); + auto* ptr_d_random = raw_ptr(d_random); + + random_engine_t::generate_random(handle, ptr_d_random, d_random.size(), seed0); + + auto const* col_indices = graph.get_matrix_partition_view().get_indices(); + auto const* row_offsets = graph.get_matrix_partition_view().get_offsets(); + auto const* values = graph.get_matrix_partition_view().get_weights() + ? *(graph.get_matrix_partition_view().get_weights()) + : static_cast(nullptr); + auto* ptr_d_sizes = raw_ptr(d_paths_sz); + auto const& d_cached_out_degs = rand_walker.get_out_degs(); + + auto rnd_to_indx_convertor = [] __device__(real_t rnd_vindx, edge_t crt_out_deg) { + real_t max_ub = static_cast(crt_out_deg - 1); + auto interp_vindx = rnd_vindx * max_ub + real_t{.5}; + vertex_t v_indx = static_cast(interp_vindx); + return (v_indx >= crt_out_deg ? crt_out_deg - 1 : v_indx); + }; + + auto next_vw = + [row_offsets, + col_indices, + values] __device__(auto v_indx, // src vertex to find dst from + auto col_indx) { // column index, in {0,...,out_deg(v_indx)-1}, + // extracted from random value in [0..1] + auto start_row = row_offsets[v_indx]; + + auto weight_value = + (values == nullptr ? weight_t{1} + : values[start_row + col_indx]); // account for un-weighted graphs + return thrust::make_tuple(col_indices[start_row + col_indx], weight_value); + }; + + // start from 1, as 0-th was initialized above: + // + thrust::for_each(rmm::exec_policy(handle.get_stream_view()), + thrust::make_counting_iterator(0), + thrust::make_counting_iterator(num_paths_), + [max_depth = max_depth_, + ptr_d_cache_out_degs = raw_const_ptr(d_cached_out_degs), + ptr_coalesced_v = raw_ptr(d_coalesced_v), + ptr_coalesced_w = raw_ptr(d_coalesced_w), + ptr_d_random, + ptr_d_sizes, + rnd_to_indx_convertor, + next_vw] __device__(auto path_index) { + auto chunk_offset = path_index * max_depth; + vertex_t src_vertex = ptr_coalesced_v[chunk_offset]; + + for (index_t step_indx = 1; step_indx < max_depth; ++step_indx) { + auto crt_out_deg = ptr_d_cache_out_degs[src_vertex]; + if (crt_out_deg == 0) break; + + // indexing into coalesced arrays of size num_paths x (max_depth -1): + // (d_random, d_coalesced_w) + // + auto stepping_index = chunk_offset - path_index + step_indx - 1; + + auto real_rnd_indx = ptr_d_random[stepping_index]; + + auto col_indx = rnd_to_indx_convertor(real_rnd_indx, crt_out_deg); + auto pair_vw = next_vw(src_vertex, col_indx); + + src_vertex = thrust::get<0>(pair_vw); + auto crt_weight = thrust::get<1>(pair_vw); + + ptr_coalesced_v[chunk_offset + step_indx] = src_vertex; + ptr_coalesced_w[stepping_index] = crt_weight; + ptr_d_sizes[path_index]++; + } + }); + } + + size_t get_random_buff_sz(void) const { return num_paths_ * (max_depth_ - 1); } + size_t get_tmp_buff_sz(void) const + { + return 0; + } // no need for tmp buffers + //(see "ignored" above) + + private: + size_t num_paths_; + size_t max_depth_; +}; // namespace detail + +} // namespace detail +} // namespace experimental +} // namespace cugraph diff --git a/cpp/src/serialization/serializer.cu b/cpp/src/serialization/serializer.cu new file mode 100644 index 00000000000..28529c9f3ed --- /dev/null +++ b/cpp/src/serialization/serializer.cu @@ -0,0 +1,314 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +namespace cugraph { +namespace serializer { + +template +void serializer_t::serialize(value_t val) +{ + auto byte_buff_sz = sizeof(value_t); + auto it_end = begin_ + byte_buff_sz; + + raft::update_device( + begin_, reinterpret_cast(&val), byte_buff_sz, handle_.get_stream()); + + begin_ = it_end; +} + +template +value_t serializer_t::unserialize(void) +{ + value_t val{}; + auto byte_buff_sz = sizeof(value_t); + + raft::update_host(&val, reinterpret_cast(cbegin_), 1, handle_.get_stream()); + + cbegin_ += byte_buff_sz; + return val; +} + +template +void serializer_t::serialize(value_t const* p_d_src, size_t size) +{ + auto byte_buff_sz = size * sizeof(value_t); + auto it_end = begin_ + byte_buff_sz; + byte_t const* byte_buff = reinterpret_cast(p_d_src); + + thrust::copy_n(rmm::exec_policy(handle_.get_stream_view()), byte_buff, byte_buff_sz, begin_); + + begin_ = it_end; +} + +template +rmm::device_uvector serializer_t::unserialize(size_t size) +{ + auto byte_buff_sz = size * sizeof(value_t); + rmm::device_uvector d_dest(size, handle_.get_stream()); + byte_t* byte_buff = reinterpret_cast(d_dest.data()); + + thrust::copy_n(rmm::exec_policy(handle_.get_stream_view()), cbegin_, byte_buff_sz, byte_buff); + + cbegin_ += byte_buff_sz; + return d_dest; +} + +// serialization of graph metadata, via device orchestration: +// +template +void serializer_t::serialize(serializer_t::graph_meta_t const& gmeta) +{ + using vertex_t = typename graph_t::vertex_type; + using edge_t = typename graph_t::edge_type; + using weight_t = typename graph_t::weight_type; + + if constexpr (!graph_t::is_multi_gpu) { + using bool_t = typename graph_meta_t::bool_ser_t; + + serialize(gmeta.num_vertices_); + serialize(gmeta.num_edges_); + serialize(static_cast(gmeta.properties_.is_symmetric)); + serialize(static_cast(gmeta.properties_.is_multigraph)); + serialize(static_cast(gmeta.is_weighted_)); + + auto seg_off_sz_bytes = + (gmeta.segment_offsets_ ? (*(gmeta.segment_offsets_)).size() : size_t{0}) * sizeof(vertex_t); + if (seg_off_sz_bytes > 0) { + auto it_end = begin_ + seg_off_sz_bytes; + + raft::update_device(begin_, + reinterpret_cast((*(gmeta.segment_offsets_)).data()), + seg_off_sz_bytes, + handle_.get_stream()); + + begin_ = it_end; + } + + } else { + CUGRAPH_FAIL("Unsupported graph type for serialization."); + } +} + +// unserialization of graph metadata, via device orchestration: +// +template +serializer_t::graph_meta_t serializer_t::unserialize( + size_t graph_meta_sz_bytes, + serializer_t::graph_meta_t const& empty_meta) // tag dispatching parameter +{ + using vertex_t = typename graph_t::vertex_type; + using edge_t = typename graph_t::edge_type; + using weight_t = typename graph_t::weight_type; + + if constexpr (!graph_t::is_multi_gpu) { + using bool_t = typename graph_meta_t::bool_ser_t; + + CUGRAPH_EXPECTS(graph_meta_sz_bytes >= 2 * sizeof(size_t) + 3 * sizeof(bool_t), + "Un/serialization meta size mismatch."); + + size_t num_vertices = unserialize(); + size_t num_edges = unserialize(); + bool_t is_symmetric = unserialize(); + bool_t is_multigraph = unserialize(); + bool_t is_weighted = unserialize(); + + graph_properties_t properties{static_cast(is_symmetric), + static_cast(is_multigraph)}; + + std::optional> segment_offsets{std::nullopt}; + + size_t seg_off_sz_bytes = graph_meta_sz_bytes - 2 * sizeof(size_t) - 3 * sizeof(bool_t); + + if (seg_off_sz_bytes > 0) { + segment_offsets = std::vector(seg_off_sz_bytes / sizeof(vertex_t), vertex_t{0}); + raft::update_host((*segment_offsets).data(), + reinterpret_cast(cbegin_), + seg_off_sz_bytes, + handle_.get_stream()); + + cbegin_ += seg_off_sz_bytes; + } + + return graph_meta_t{ + num_vertices, num_edges, properties, static_cast(is_weighted), segment_offsets}; + + } else { + CUGRAPH_FAIL("Unsupported graph type for unserialization."); + return graph_meta_t{}; + } +} + +// graph serialization: +// metadata argument (gvmeta) can be used for checking / testing; +// +template +void serializer_t::serialize(graph_t const& graph, serializer_t::graph_meta_t& gvmeta) +{ + using vertex_t = typename graph_t::vertex_type; + using edge_t = typename graph_t::edge_type; + using weight_t = typename graph_t::weight_type; + + if constexpr (!graph_t::is_multi_gpu) { + size_t num_vertices = graph.get_number_of_vertices(); + size_t num_edges = graph.get_number_of_edges(); + auto&& gview = graph.view(); + + gvmeta = graph_meta_t{graph}; + + auto offsets = gview.get_matrix_partition_view().get_offsets(); + auto indices = gview.get_matrix_partition_view().get_indices(); + auto weights = gview.get_matrix_partition_view().get_weights(); + + // FIXME: remove when host_bcast() becomes available for vectors; + // + // for now, this must come first, because unserialize() + // needs it at the beginning to extract graph metadata + // to be able to finish the rest of the graph unserialization; + // + serialize(gvmeta); + + serialize(offsets, num_vertices + 1); + serialize(indices, num_edges); + + if (weights) serialize(*weights, num_edges); + + } else { + CUGRAPH_FAIL("Unsupported graph type for serialization."); + } +} + +// graph unserialization: +// +template +graph_t serializer_t::unserialize(size_t device_sz_bytes, size_t host_sz_bytes) +{ + using vertex_t = typename graph_t::vertex_type; + using edge_t = typename graph_t::edge_type; + using weight_t = typename graph_t::weight_type; + + if constexpr (!graph_t::is_multi_gpu) { + graph_meta_t empty_meta{}; // tag-dispatching only + + // FIXME: remove when host_bcast() becomes available for vectors; + // + // for now, this must come first, because unserialize() + // needs it at the beginning to extract graph metadata + // to be able to finish the rest of the graph unserialization; + // + auto gvmeta = unserialize(host_sz_bytes, empty_meta); + + auto pair_sz = get_device_graph_sz_bytes(gvmeta); + + CUGRAPH_EXPECTS((pair_sz.first == device_sz_bytes) && (pair_sz.second == host_sz_bytes), + "Un/serialization size mismatch."); + + vertex_t num_vertices = gvmeta.num_vertices_; + edge_t num_edges = gvmeta.num_edges_; + auto g_props = gvmeta.properties_; + auto is_weighted = gvmeta.is_weighted_; + auto seg_offsets = gvmeta.segment_offsets_; + + auto d_offsets = unserialize(num_vertices + 1); + auto d_indices = unserialize(num_edges); + + return graph_t( + handle_, + num_vertices, + num_edges, + g_props, + std::move(d_offsets), + std::move(d_indices), + is_weighted ? std::optional>{unserialize(num_edges)} + : std::nullopt, + std::move(seg_offsets)); // RVO-ed + } else { + CUGRAPH_FAIL("Unsupported graph type for unserialization."); + + return graph_t{handle_}; + } +} + +// Manual template instantiations (EIDir's): +// +template void serializer_t::serialize(int32_t const* p_d_src, size_t size); +template void serializer_t::serialize(int64_t const* p_d_src, size_t size); +template void serializer_t::serialize(float const* p_d_src, size_t size); +template void serializer_t::serialize(double const* p_d_src, size_t size); + +template rmm::device_uvector serializer_t::unserialize(size_t size); +template rmm::device_uvector serializer_t::unserialize(size_t size); +template rmm::device_uvector serializer_t::unserialize(size_t size); +template rmm::device_uvector serializer_t::unserialize(size_t size); + +// serialize graph: +// +template void serializer_t::serialize( + graph_t const& graph, + serializer_t::graph_meta_t>&); + +template void serializer_t::serialize( + graph_t const& graph, + serializer_t::graph_meta_t>&); + +template void serializer_t::serialize( + graph_t const& graph, + serializer_t::graph_meta_t>&); + +template void serializer_t::serialize( + graph_t const& graph, + serializer_t::graph_meta_t>&); + +template void serializer_t::serialize( + graph_t const& graph, + serializer_t::graph_meta_t>&); + +template void serializer_t::serialize( + graph_t const& graph, + serializer_t::graph_meta_t>&); + +// unserialize graph: +// +template graph_t serializer_t::unserialize(size_t, size_t); + +template graph_t serializer_t::unserialize(size_t, size_t); + +template graph_t serializer_t::unserialize(size_t, size_t); + +template graph_t serializer_t::unserialize(size_t, size_t); + +template graph_t serializer_t::unserialize(size_t, size_t); + +template graph_t serializer_t::unserialize(size_t, size_t); + +} // namespace serializer +} // namespace cugraph diff --git a/cpp/src/sort/bitonic.cuh b/cpp/src/sort/bitonic.cuh deleted file mode 100644 index e2922a58d39..00000000000 --- a/cpp/src/sort/bitonic.cuh +++ /dev/null @@ -1,546 +0,0 @@ -// -*-c++-*- - -/* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Bitonic sort implementation -// Author: Chuck Hastings charlesh@nvidia.com - -// TODO: Read a paper (Hagen Peters 2011) that suggests some -// ways to optimize this. Need to shift into a kernel -// and then organize to support multiple passes in -// a single kernel call. This should reduce kernel -// launch overhead and the number of memory references, -// which should drive down the overall time. -// - -#ifndef BITONIC_SORT_H -#define BITONIC_SORT_H - -#include -#include - -#include -#include - -namespace cugraph { -namespace sort { - -namespace bitonic { -/* - * This implementation is based upon the bitonic sort technique. - * This should be pretty efficient in a SIMT environment. - */ -namespace detail { -/** - * @brief Compare two items, if the compare functor returns true - * then swap them. - * - * @param a - reference to the first item - * @param b - reference to the second item - * @param compare - reference to a comparison functor - */ -template -inline void __device__ compareAndSwap(ValueT &a, ValueT &b, CompareT &compare) -{ - if (!compare(a, b)) { thrust::swap(a, b); } -} - -/* - * @brief perform repartitioning of two sorted partitions. This - * is analagous to the bitonic merge step. But it only - * performs the compare and swap portion of the bitonic - * merge. The subsequent sorts are handled externally. - * - * The repartition assumes that the data is segregated - * into partitions of binSize. So if there are 8 elements - * and a bin size of 2 then the array will be partitioned - * into 4 bins of size 2. Each bin is assumed to be - * sorted. The repartition takes consecutive bins and - * repartitions them so that the first bin contains the - * low elements and the second bin contains the high elements. - * - * @param array - the array containing the data we need to repartition - * @param count - the number of elements in the array - * @param binSize - the size of the bin - * @param compare - comparison functor - */ -template -void repartition(ValueT *array, int count, int binSize, CompareT &compare) -{ - thrust::for_each(thrust::make_counting_iterator(0), - thrust::make_counting_iterator(count / 2), - - [array, count, binSize, compare] __device__(int idx) { - // - // Identify which elements in which partition - // we are responsible for comparing and swapping - // - // We're running count/2 iterations. Each iteration - // needs to operate on a pair of elements. Consider - // the pairs of partitions, this will let us determine - // which elements we compare. - // - int bi_partition = idx / binSize; - - // - // bi_partition identifies which pair of partitions - // we're operating on. Out of each bin we're only - // going to do binSize comparisons, so the first - // element in the comparison will be based on - // idx % binSize. - // - int offset = idx % binSize; - - // - // First element is easy. - // Second element is "easy" but we'll fix - // special cases below. - // - int i = bi_partition * (binSize * 2) + offset; - int j = (bi_partition + 1) * (binSize * 2) - 1 - offset; - - // - // The last partition pair is the problem. - // There are several cases: - // 1) Both partitions are full. This - // is the easy case, we can just - // compare and swap elements - // 2) First partition is full, the second - // partition is not full (possibly - // empty). In this case, we only - // compare some of the elements. - // 3) First partition is not full, there - // is no second partition. In this - // case we actually don't have any - // work to do. - // - // This should be a simple check. If the - // second element is beyond the end of - // the array then there is nothing to compare - // and swap. Note that if the first - // element is beyond the end of the array - // there is also nothing to compare and swap, - // but if the first element is beyond the - // end of the array then the second element - // will also be beyond the end of the array. - // - if (j < count) compareAndSwap(array[i], array[j], compare); - }); -} - -/* - * @brief perform shuffles. After the repartition we need - * to perform shuffles of the halves to get things in - * order. - * - * @param array - the array containing the data we need to repartition - * @param count - the number of elements in the array - * @param binSize - the size of the bin - * @param compare - comparison functor - */ -template -void shuffles(ValueT *array, int count, int binSize, CompareT &compare) -{ - thrust::for_each(thrust::make_counting_iterator(0), - thrust::make_counting_iterator((count + 1) / 2), - [array, count, binSize, compare] __device__(int idx) { - // - // Identify which elements in which partition - // we are responsible for comparing and swapping - // - // We're running count/2 iterations. Each iteration - // needs to operate on a pair of elements. Consider - // the pairs of partitions, this will let us determine - // which elements we compare. - // - int bi_partition = idx / binSize; - - // - // bi_partition identifies which pair of partitions - // we're operating on. Out of each bin we're only - // going to do binSize comparisons, so the first - // element in the comparison will be based on - // idx % binSize. - // - int offset = idx % binSize; - - // - // First element is easy. - // Second element is "easy" i + binSize. - // - int i = bi_partition * (binSize * 2) + offset; - int j = i + binSize; - - // - // If the second element is beyond the end of - // the array then there is nothing to compare - // and swap. - // - if (j < count) compareAndSwap(array[i], array[j], compare); - }); -} - -/* - * @brief perform repartitioning of two sorted partitions in the - * segmented sort case. - * - * The repartition assumes that the data is segregated - * into partitions of binSize. So if there are 8 elements - * and a bin size of 2 then the array will be partitioned - * into 4 bins of size 2. Each bin is assumed to be - * sorted. The repartition takes consecutive bins and - * repartitions them so that the first bin contains the - * low elements and the second bin contains the high elements. - * - * @param array - the array containing the data we need to repartition - * @param count - the number of elements in the array - * @param binSize - the size of the bin - * @param compare - comparison functor - */ -template -void repartition_segmented(const IndexT *d_begin_offsets, - const IndexT *d_end_offsets, - ValueT *d_items, - IndexT start, - IndexT stop, - IndexT *d_grouped_bins, - int binSize, - int max_count, - int bin_pairs, - CompareT &compare) -{ - thrust::for_each(thrust::device, - thrust::make_counting_iterator(0), - thrust::make_counting_iterator(max_count / 2), - [d_begin_offsets, - d_end_offsets, - d_items, - start, - stop, - d_grouped_bins, - bin_pairs, - binSize, - compare] __device__(int idx) { - // - // idx needs to be mapped into the correct place - // - int entry = idx / bin_pairs; - int entry_idx = idx % bin_pairs; - int base = d_begin_offsets[d_grouped_bins[start + entry]]; - int count = d_end_offsets[d_grouped_bins[start + entry]] - base; - - // - // Identify which elements in which partition - // we are responsible for comparing and swapping - // - // We're running count/2 iterations. Each iteration - // needs to operate on a pair of elements. Consider - // the pairs of partitions, this will let us determine - // which elements we compare. - // - int bi_partition = entry_idx / binSize; - - // - // bi_partition identifies which pair of partitions - // we're operating on. Out of each bin we're only - // going to do binSize comparisons, so the first - // element in the comparison will be based on - // idx % binSize. - // - int offset = entry_idx % binSize; - - // - // First element is easy. - // Second element is "easy" but we'll fix - // special cases below. - // - int i = bi_partition * (binSize * 2) + offset; - int j = (bi_partition + 1) * (binSize * 2) - 1 - offset; - - // - // The last partition pair is the problem. - // There are several cases: - // 1) Both partitions are full. This - // is the easy case, we can just - // compare and swap elements - // 2) First partition is full, the second - // partition is not full (possibly - // empty). In this case, we only - // compare some of the elements. - // 3) First partition is not full, there - // is no second partition. In this - // case we actually don't have any - // work to do. - // - // This should be a simple check. If the - // second element is beyond the end of - // the array then there is nothing to compare - // and swap. Note that if the first - // element is beyond the end of the array - // there is also nothing to compare and swap, - // but if the first element is beyond the - // end of the array then the second element - // will also be beyond the end of the array. - // - if (j < count) { - compareAndSwap(d_items[base + i], d_items[base + j], compare); - } - }); -} - -/* - * @brief perform shuffles. After the repartition we need - * to perform shuffles of the halves to get things in - * order. - * - * @param rowOffsets - the row offsets identifying the segments - * @param colIndices - the values to sort within the segments - * @param start - position within the grouped bins where we - * start this pass - * @param stop - position within the grouped bins where we stop - * this pass - * @param d_grouped_bins - lrb grouped bins. All bins between - * start and stop are in the same lrb bin - * @param binSize - the bitonic bin size for this pass of the shuffles - * @param max_count - maximum number of elements possible for - * this call - * @param bin_pairs - the number of bin pairs - * @param compare - the comparison functor - */ -template -void shuffles_segmented(const IndexT *d_begin_offsets, - const IndexT *d_end_offsets, - ValueT *d_items, - IndexT start, - IndexT stop, - IndexT *d_grouped_bins, - int binSize, - long max_count, - int bin_pairs, - CompareT &compare) -{ - thrust::for_each(thrust::make_counting_iterator(0), - thrust::make_counting_iterator(max_count / 2), - [d_begin_offsets, - d_end_offsets, - d_items, - start, - stop, - d_grouped_bins, - compare, - max_count, - bin_pairs, - binSize] __device__(int idx) { - // - // idx needs to be mapped into the correct place - // - int entry = idx / bin_pairs; - int entry_idx = idx % bin_pairs; - int base = d_begin_offsets[d_grouped_bins[start + entry]]; - int count = d_end_offsets[d_grouped_bins[start + entry]] - base; - - // - // Identify which elements in which partition - // we are responsible for comparing and swapping - // - // We're running count/2 iterations. Each iteration - // needs to operate on a pair of elements. Consider - // the pairs of partitions, this will let us determine - // which elements we compare. - // - int bi_partition = entry_idx / binSize; - - // - // bi_partition identifies which pair of partitions - // we're operating on. Out of each bin we're only - // going to do binSize comparisons, so the first - // element in the comparison will be based on - // idx % binSize. - // - int offset = entry_idx % binSize; - - // - // First element is easy. - // Second element is "easy" i + binSize. - // - int i = bi_partition * (binSize * 2) + offset; - int j = i + binSize; - - // - // If the second element is beyond the end of - // the array then there is nothing to compare - // and swap. - // - if (j < count) compareAndSwap(d_items[base + i], d_items[base + j], compare); - }); -} -} // namespace detail - -template -void sort(ValueT *array, int count, CompareT &compare) -{ - for (int i = 1; i < count; i *= 2) { - detail::repartition(array, count, i, compare); - - for (int j = i / 2; j > 0; j /= 2) { detail::shuffles(array, count, j, compare); } - } -} - -/** - * @brief Perform a segmented sort. This function performs a sort - * on each segment of the specified input. This sort is done - * in place, so the d_items array is modified during this call. - * Sort is done according to the (optionally) specified - * comparison function. - * - * Note that this function uses O(num_segments) temporary - * memory during execution. - * - * @param [in] num_segments - the number of segments that the items array is divided into - * @param [in] num_items - the number of items in the array - * @param [in] d_begin_offsets - device array containing the offset denoting the start - * of each segment - * @param [in] d_end_offsets - device array containing the offset denoting the end - * of each segment. - * @param [in/out] d_items - device array containing the items to sort - * @param [in] compare - [optional] comparison function. Default is thrust::less. - * @param [in] stream - [optional] CUDA stream to launch kernels with. Default is stream 0. - * - * @return error code - */ -template -void segmented_sort(IndexT num_segments, - IndexT num_items, - const IndexT *d_begin_offsets, - const IndexT *d_end_offsets, - ValueT *d_items, - CompareT compare = thrust::less(), - cudaStream_t stream = nullptr) -{ - // - // NOTE: This should probably be computed somehow. At the moment - // we are limited to 32 bits because of memory sizes. - // - int lrb_size = 32; - IndexT lrb[lrb_size + 1]; - - rmm::device_vector lrb_v(lrb_size + 1); - rmm::device_vector grouped_bins_v(num_segments + 1); - - IndexT *d_lrb = lrb_v.data().get(); - IndexT *d_grouped_bins = grouped_bins_v.data().get(); - - CUDA_TRY(cudaMemset(d_lrb, 0, (lrb_size + 1) * sizeof(IndexT))); - - // - // First we'll count how many entries go in each bin - // - thrust::for_each(thrust::make_counting_iterator(0), - thrust::make_counting_iterator(num_segments), - [d_begin_offsets, d_end_offsets, d_lrb] __device__(int idx) { - int size = d_end_offsets[idx] - d_begin_offsets[idx]; - // - // NOTE: If size is 0 or 1 then no - // sorting is required, so we'll - // eliminate those bins here - // - if (size > 1) atomicAdd(d_lrb + __clz(size), 1); - }); - - // - // Exclusive sum will identify where each bin begins - // - thrust::exclusive_scan( - rmm::exec_policy(stream)->on(stream), d_lrb, d_lrb + (lrb_size + 1), d_lrb); - - // - // Copy the start of each bin to local memory - // - CUDA_TRY(cudaMemcpy(lrb, d_lrb, (lrb_size + 1) * sizeof(IndexT), cudaMemcpyDeviceToHost)); - - // - // Now we'll populate grouped_bins. This will corrupt - // d_lrb, but we've already copied it locally. - // - thrust::for_each(thrust::make_counting_iterator(0), - thrust::make_counting_iterator(num_segments), - [d_begin_offsets, d_end_offsets, d_lrb, d_grouped_bins] __device__(int idx) { - int size = d_end_offsets[idx] - d_begin_offsets[idx]; - if (size > 1) { - int pos = atomicAdd(d_lrb + __clz(size), 1); - d_grouped_bins[pos] = idx; - } - }); - - // - // At this point, d_grouped_bins contains the index of the - // different segments, ordered into log2 bins. - // - - // - // Now we're ready to go. - // - // For simplicity (at least for now), let's just - // iterate over each lrb bin. Note that the larger - // the index i, the smaller the size of each bin... but - // there will likely be many more inhabitants of that bin. - // - for (int i = 0; i < lrb_size; ++i) { - int size = lrb[i + 1] - lrb[i]; - if (size > 0) { - // - // There are inhabitants of this lrb range - // - // max_count will be used to drive the bitonic - // passes (1, 2, 4, 8, ... up to max_count) - // - int max_count = 1 << (lrb_size - i); - - for (int j = 1; j < max_count; j *= 2) { - detail::repartition_segmented(d_begin_offsets, - d_end_offsets, - d_items, - lrb[i], - lrb[i + 1], - d_grouped_bins, - j, - size * max_count, - max_count / 2, - compare); - - for (int k = j / 2; k > 0; k /= 2) { - detail::shuffles_segmented(d_begin_offsets, - d_end_offsets, - d_items, - lrb[i], - lrb[i + 1], - d_grouped_bins, - k, - size * max_count, - max_count / 2, - compare); - } - } - } - } -} - -} // namespace bitonic -} // namespace sort -} // namespace cugraph - -#endif diff --git a/cpp/src/structure/create_graph_from_edgelist.cpp b/cpp/src/structure/create_graph_from_edgelist.cpp new file mode 100644 index 00000000000..6ce10c7ccdf --- /dev/null +++ b/cpp/src/structure/create_graph_from_edgelist.cpp @@ -0,0 +1,485 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +#include + +#include + +#include +#include + +#include +#include + +namespace cugraph { +namespace experimental { + +namespace { + +template +std::enable_if_t< + multi_gpu, + std::tuple< + cugraph::experimental::graph_t, + std::optional>>> +create_graph_from_edgelist_impl( + raft::handle_t const& handle, + std::optional> optional_local_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber) +{ + CUGRAPH_EXPECTS(renumber, "renumber should be true if multi_gpu is true."); + + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + auto const comm_rank = comm.get_rank(); + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_size = row_comm.get_size(); + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_size = col_comm.get_size(); + + auto edge_counts = + cugraph::detail::groupby_and_count_by_edge(handle, + store_transposed ? edgelist_cols : edgelist_rows, + store_transposed ? edgelist_rows : edgelist_cols, + edgelist_weights, + col_comm_size); + + std::vector h_edge_counts(edge_counts.size()); + raft::update_host( + h_edge_counts.data(), edge_counts.data(), edge_counts.size(), handle.get_stream()); + handle.get_stream_view().synchronize(); + + std::vector h_displacements(h_edge_counts.size(), size_t{0}); + std::partial_sum(h_edge_counts.begin(), h_edge_counts.end() - 1, h_displacements.begin() + 1); + + // 3. renumber + + rmm::device_uvector renumber_map_labels(0, handle.get_stream()); + cugraph::experimental::partition_t partition{}; + vertex_t number_of_vertices{}; + edge_t number_of_edges{}; + auto segment_offsets = std::make_optional>(0); + { + std::vector major_ptrs(h_edge_counts.size()); + std::vector minor_ptrs(major_ptrs.size()); + std::vector counts(major_ptrs.size()); + for (size_t i = 0; i < h_edge_counts.size(); ++i) { + major_ptrs[i] = + (store_transposed ? edgelist_cols.begin() : edgelist_rows.begin()) + h_displacements[i]; + minor_ptrs[i] = + (store_transposed ? edgelist_rows.begin() : edgelist_cols.begin()) + h_displacements[i]; + counts[i] = static_cast(h_edge_counts[i]); + } + std::tie( + renumber_map_labels, partition, number_of_vertices, number_of_edges, *segment_offsets) = + cugraph::experimental::renumber_edgelist( + handle, optional_local_vertex_span, major_ptrs, minor_ptrs, counts); + } + + // 4. create a graph + + std::vector> edgelists( + h_edge_counts.size()); + for (size_t i = 0; i < h_edge_counts.size(); ++i) { + edgelists[i] = cugraph::experimental::edgelist_t{ + edgelist_rows.data() + h_displacements[i], + edgelist_cols.data() + h_displacements[i], + edgelist_weights + ? std::optional{(*edgelist_weights).data() + h_displacements[i]} + : std::nullopt, + static_cast(h_edge_counts[i])}; + } + + return std::make_tuple( + cugraph::experimental::graph_t( + handle, + edgelists, + partition, + number_of_vertices, + number_of_edges, + graph_properties, + std::optional>{segment_offsets}), + std::optional>{std::move(renumber_map_labels)}); +} + +template +std::enable_if_t< + !multi_gpu, + std::tuple< + cugraph::experimental::graph_t, + std::optional>>> +create_graph_from_edgelist_impl( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber) +{ + auto renumber_map_labels = + renumber ? std::make_optional>(0, handle.get_stream()) + : std::nullopt; + std::optional> segment_offsets{std::nullopt}; + if (renumber) { + segment_offsets = std::vector{}; + std::tie(*renumber_map_labels, *segment_offsets) = + cugraph::experimental::renumber_edgelist( + handle, + optional_vertex_span, + store_transposed ? edgelist_cols.data() : edgelist_rows.data(), + store_transposed ? edgelist_rows.data() : edgelist_cols.data(), + static_cast(edgelist_rows.size())); + } + + vertex_t num_vertices{}; + if (renumber) { + num_vertices = static_cast((*renumber_map_labels).size()); + } else { + if (optional_vertex_span) { + num_vertices = std::get<1>(*optional_vertex_span); + } else { + num_vertices = 1 + cugraph::detail::compute_maximum_vertex_id( + handle.get_stream_view(), edgelist_rows, edgelist_cols); + } + } + + return std::make_tuple( + cugraph::experimental::graph_t( + handle, + cugraph::experimental::edgelist_t{ + edgelist_rows.data(), + edgelist_cols.data(), + edgelist_weights ? std::optional{(*edgelist_weights).data()} + : std::nullopt, + static_cast(edgelist_rows.size())}, + num_vertices, + graph_properties, + std::optional>{segment_offsets}), + std::move(renumber_map_labels)); +} + +} // namespace + +template +std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber) +{ + return create_graph_from_edgelist_impl( + handle, + optional_vertex_span, + std::move(edgelist_rows), + std::move(edgelist_cols), + std::move(edgelist_weights), + graph_properties, + renumber); +} + +// explicit instantiations + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +template std::tuple, + std::optional>> +create_graph_from_edgelist( + raft::handle_t const& handle, + std::optional> optional_vertex_span, + rmm::device_uvector&& edgelist_rows, + rmm::device_uvector&& edgelist_cols, + std::optional>&& edgelist_weights, + graph_properties_t graph_properties, + bool renumber); + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/src/structure/graph.cu b/cpp/src/structure/graph.cu index 63ef725c3b7..e3bdd1d5c67 100644 --- a/cpp/src/structure/graph.cu +++ b/cpp/src/structure/graph.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -14,58 +14,60 @@ * limitations under the License. */ -#include -#include "utilities/error.hpp" -#include "utilities/graph_utils.cuh" +#include +#include +#include #include +#include namespace { template void degree_from_offsets(vertex_t number_of_vertices, - edge_t const *offsets, - edge_t *degree, - cudaStream_t stream) + edge_t const* offsets, + edge_t* degree, + rmm::cuda_stream_view stream_view) { // Computes out-degree for x = 0 and x = 2 thrust::for_each( - rmm::exec_policy(stream)->on(stream), + rmm::exec_policy(stream_view), thrust::make_counting_iterator(0), thrust::make_counting_iterator(number_of_vertices), [offsets, degree] __device__(vertex_t v) { degree[v] = offsets[v + 1] - offsets[v]; }); } template -void degree_from_vertex_ids(const raft::handle_t *handle, +void degree_from_vertex_ids(const raft::handle_t* handle, vertex_t number_of_vertices, edge_t number_of_edges, - vertex_t const *indices, - edge_t *degree, - cudaStream_t stream) + vertex_t const* indices, + edge_t* degree, + rmm::cuda_stream_view stream_view) { - thrust::for_each(rmm::exec_policy(stream)->on(stream), + thrust::for_each(rmm::exec_policy(stream_view), thrust::make_counting_iterator(0), thrust::make_counting_iterator(number_of_edges), [indices, degree] __device__(edge_t e) { atomicAdd(degree + indices[e], 1); }); if ((handle != nullptr) && (handle->comms_initialized())) { - auto &comm = handle->get_comms(); - comm.allreduce(degree, degree, number_of_vertices, raft::comms::op_t::SUM, stream); + auto& comm = handle->get_comms(); + comm.allreduce(degree, degree, number_of_vertices, raft::comms::op_t::SUM, stream_view.value()); } } } // namespace namespace cugraph { +namespace legacy { template -void GraphViewBase::get_vertex_identifiers(VT *identifiers) const +void GraphViewBase::get_vertex_identifiers(VT* identifiers) const { cugraph::detail::sequence(number_of_vertices, identifiers); } template -void GraphCompressedSparseBaseView::get_source_indices(VT *src_indices) const +void GraphCompressedSparseBaseView::get_source_indices(VT* src_indices) const { CUGRAPH_EXPECTS(offsets != nullptr, "No graph specified"); cugraph::detail::offsets_to_indices( @@ -73,7 +75,7 @@ void GraphCompressedSparseBaseView::get_source_indices(VT *src_indic } template -void GraphCOOView::degree(ET *degree, DegreeDirection direction) const +void GraphCOOView::degree(ET* degree, DegreeDirection direction) const { // // NOTE: We assume offsets/indices are a CSR. If a CSC is passed @@ -110,7 +112,7 @@ void GraphCOOView::degree(ET *degree, DegreeDirection direction) con } template -void GraphCompressedSparseBaseView::degree(ET *degree, DegreeDirection direction) const +void GraphCompressedSparseBaseView::degree(ET* degree, DegreeDirection direction) const { // // NOTE: We assume offsets/indices are a CSR. If a CSC is passed @@ -118,7 +120,7 @@ void GraphCompressedSparseBaseView::degree(ET *degree, DegreeDirecti // (e.g. if you have a CSC and you want in-degree (x=1) then pass // the offsets/indices and request an out-degree (x=2)) // - cudaStream_t stream{nullptr}; + rmm::cuda_stream_view stream_view; if (direction != DegreeDirection::IN) { if ((GraphViewBase::handle != nullptr) && @@ -127,7 +129,8 @@ void GraphCompressedSparseBaseView::degree(ET *degree, DegreeDirecti // source indexing for // the allreduce to work } - degree_from_offsets(GraphViewBase::number_of_vertices, offsets, degree, stream); + degree_from_offsets( + GraphViewBase::number_of_vertices, offsets, degree, stream_view); } if (direction != DegreeDirection::OUT) { @@ -136,7 +139,7 @@ void GraphCompressedSparseBaseView::degree(ET *degree, DegreeDirecti GraphViewBase::number_of_edges, indices, degree, - stream); + stream_view); } } @@ -147,4 +150,7 @@ template class GraphCOOView; template class GraphCOOView; template class GraphCompressedSparseBaseView; template class GraphCompressedSparseBaseView; +} // namespace legacy } // namespace cugraph + +#include diff --git a/cpp/src/traversal/README.md b/cpp/src/traversal/README.md new file mode 100644 index 00000000000..429b58d441e --- /dev/null +++ b/cpp/src/traversal/README.md @@ -0,0 +1,56 @@ +# Traversal +cuGraph traversal algorithms are contained in this directory + +## SSSP + +The unit test code is the best place to search for examples on calling SSSP. + + * [SG Implementation](../../tests/experimental/sssp_test.cpp) + * MG Implementation - TBD + +## Simple SSSP + +The example assumes that you create an SG or MG graph somehow. The caller must create the distances and predecessors vectors in device memory and pass in the raw pointers to those vectors into the SSSP function. + +```cpp +#include +... +using vertex_t = int32_t; // or int64_t, whichever is appropriate +using weight_t = float; // or double, whichever is appropriate +using result_t = weight_t; // could specify float or double also +raft::handle_t handle; // Must be configured if MG +auto graph_view = graph.view(); // assumes you have created a graph somehow +vertex_t source; // Initialized by user + +rmm::device_uvector distances_v(graph_view.get_number_of_vertices(), handle.get_stream()); +rmm::device_uvector predecessors_v(graph_view.get_number_of_vertices(), handle.get_stream()); + +cugraph::experimental::sssp(handle, graph_view, distances_v.begin(), predecessors_v.begin(), source, std::numeric_limits::max(), false); +``` + +## BFS + +The unit test code is the best place to search for examples on calling BFS. + + * [SG Implementation](../../tests/experimental/bfs_test.cpp) + * MG Implementation - TBD + +## Simple BFS + +The example assumes that you create an SG or MG graph somehow. The caller must create the distances and predecessors vectors in device memory and pass in the raw pointers to those vectors into the BFS function. + +```cpp +#include +... +using vertex_t = int32_t; // or int64_t, whichever is appropriate +using weight_t = float; // or double, whichever is appropriate +using result_t = weight_t; // could specify float or double also +raft::handle_t handle; // Must be configured if MG +auto graph_view = graph.view(); // assumes you have created a graph somehow +vertex_t source; // Initialized by user + +rmm::device_uvector distances_v(graph_view.get_number_of_vertices(), handle.get_stream()); +rmm::device_uvector predecessors_v(graph_view.get_number_of_vertices(), handle.get_stream()); + +cugraph::experimental::bfs(handle, graph_view, d_distances.begin(), d_predecessors.begin(), source, false, std::numeric_limits::max(), false); +``` diff --git a/cpp/src/traversal/bfs.cu b/cpp/src/traversal/bfs.cu index 7c59010cab8..74a94ba0670 100644 --- a/cpp/src/traversal/bfs.cu +++ b/cpp/src/traversal/bfs.cu @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. * * NVIDIA CORPORATION and its licensors retain all intellectual property * and proprietary rights in and to this software, related documentation @@ -14,14 +14,14 @@ #include #include "bfs.cuh" -#include "graph.hpp" +#include -#include +#include +#include #include "bfs_kernels.cuh" #include "mg/bfs.cuh" #include "mg/common_utils.cuh" #include "traversal_common.cuh" -#include "utilities/graph_utils.cuh" namespace cugraph { namespace detail { @@ -96,7 +96,7 @@ void BFS::setup() // Lets use this int* for the next 3 lines // Its dereferenced value is not initialized - so we dont care about what we // put in it - IndexType *d_nisolated = d_new_frontier_cnt; + IndexType* d_nisolated = d_new_frontier_cnt; cudaMemsetAsync(d_nisolated, 0, sizeof(IndexType), stream); // Computing isolated_bmap @@ -114,10 +114,10 @@ void BFS::setup() } template -void BFS::configure(IndexType *_distances, - IndexType *_predecessors, - double *_sp_counters, - int *_edge_mask) +void BFS::configure(IndexType* _distances, + IndexType* _predecessors, + double* _sp_counters, + int* _edge_mask) { distances = _distances; predecessors = _predecessors; @@ -473,11 +473,11 @@ template class BFS; // NOTE: SP counter increase extremely fast on large graph // It can easily reach 1e40~1e70 on GAP-road.mtx template -void bfs(raft::handle_t const &handle, - GraphCSRView const &graph, - VT *distances, - VT *predecessors, - double *sp_counters, +void bfs(raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + VT* distances, + VT* predecessors, + double* sp_counters, const VT start_vertex, bool directed, bool mg_batch) @@ -497,8 +497,8 @@ void bfs(raft::handle_t const &handle, VT number_of_vertices = graph.number_of_vertices; ET number_of_edges = graph.number_of_edges; - const VT *indices_ptr = graph.indices; - const ET *offsets_ptr = graph.offsets; + const VT* indices_ptr = graph.indices; + const ET* offsets_ptr = graph.offsets; int alpha = 15; int beta = 18; @@ -511,63 +511,69 @@ void bfs(raft::handle_t const &handle, } // Explicit Instantiation -template void bfs(raft::handle_t const &handle, - GraphCSRView const &graph, - uint32_t *distances, - uint32_t *predecessors, - double *sp_counters, - const uint32_t source_vertex, - bool directed, - bool mg_batch); +template void bfs( + raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + uint32_t* distances, + uint32_t* predecessors, + double* sp_counters, + const uint32_t source_vertex, + bool directed, + bool mg_batch); // Explicit Instantiation -template void bfs(raft::handle_t const &handle, - GraphCSRView const &graph, - uint32_t *distances, - uint32_t *predecessors, - double *sp_counters, - const uint32_t source_vertex, - bool directed, - bool mg_batch); +template void bfs( + raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + uint32_t* distances, + uint32_t* predecessors, + double* sp_counters, + const uint32_t source_vertex, + bool directed, + bool mg_batch); // Explicit Instantiation -template void bfs(raft::handle_t const &handle, - GraphCSRView const &graph, - int32_t *distances, - int32_t *predecessors, - double *sp_counters, - const int32_t source_vertex, - bool directed, - bool mg_batch); +template void bfs( + raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + int32_t* distances, + int32_t* predecessors, + double* sp_counters, + const int32_t source_vertex, + bool directed, + bool mg_batch); // Explicit Instantiation -template void bfs(raft::handle_t const &handle, - GraphCSRView const &graph, - int32_t *distances, - int32_t *predecessors, - double *sp_counters, - const int32_t source_vertex, - bool directed, - bool mg_batch); +template void bfs( + raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + int32_t* distances, + int32_t* predecessors, + double* sp_counters, + const int32_t source_vertex, + bool directed, + bool mg_batch); // Explicit Instantiation -template void bfs(raft::handle_t const &handle, - GraphCSRView const &graph, - int64_t *distances, - int64_t *predecessors, - double *sp_counters, - const int64_t source_vertex, - bool directed, - bool mg_batch); +template void bfs( + raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + int64_t* distances, + int64_t* predecessors, + double* sp_counters, + const int64_t source_vertex, + bool directed, + bool mg_batch); // Explicit Instantiation -template void bfs(raft::handle_t const &handle, - GraphCSRView const &graph, - int64_t *distances, - int64_t *predecessors, - double *sp_counters, - const int64_t source_vertex, - bool directed, - bool mg_batch); +template void bfs( + raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + int64_t* distances, + int64_t* predecessors, + double* sp_counters, + const int64_t source_vertex, + bool directed, + bool mg_batch); } // namespace cugraph diff --git a/cpp/src/traversal/bfs.cuh b/cpp/src/traversal/bfs.cuh index 6457665ec09..6bf8e0d0197 100644 --- a/cpp/src/traversal/bfs.cuh +++ b/cpp/src/traversal/bfs.cuh @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. * * NVIDIA CORPORATION and its licensors retain all intellectual property * and proprietary rights in and to this software, related documentation @@ -25,8 +25,8 @@ template class BFS { private: IndexType number_of_vertices, number_of_edges; - const IndexType *row_offsets = nullptr; - const IndexType *col_indices = nullptr; + const IndexType* row_offsets = nullptr; + const IndexType* col_indices = nullptr; bool directed; bool deterministic; @@ -36,10 +36,10 @@ class BFS { bool computeDistances; bool computePredecessors; rmm::device_vector distances_vals; - IndexType *distances = nullptr; - IndexType *predecessors = nullptr; - double *sp_counters = nullptr; - int *edge_mask = nullptr; + IndexType* distances = nullptr; + IndexType* predecessors = nullptr; + double* sp_counters = nullptr; + int* edge_mask = nullptr; rmm::device_vector original_frontier; rmm::device_vector visited_bmap; @@ -53,16 +53,16 @@ class BFS { // Working data // For complete description of each, go to bfs.cu IndexType nisolated; - IndexType *frontier = nullptr; - IndexType *new_frontier = nullptr; - IndexType *frontier_vertex_degree = nullptr; - IndexType *exclusive_sum_frontier_vertex_degree = nullptr; - IndexType *unvisited_queue = nullptr; - IndexType *left_unvisited_queue = nullptr; - IndexType *d_new_frontier_cnt = nullptr; - IndexType *d_mu = nullptr; - IndexType *d_unvisited_cnt = nullptr; - IndexType *d_left_unvisited_cnt = nullptr; + IndexType* frontier = nullptr; + IndexType* new_frontier = nullptr; + IndexType* frontier_vertex_degree = nullptr; + IndexType* exclusive_sum_frontier_vertex_degree = nullptr; + IndexType* unvisited_queue = nullptr; + IndexType* left_unvisited_queue = nullptr; + IndexType* d_new_frontier_cnt = nullptr; + IndexType* d_mu = nullptr; + IndexType* d_unvisited_cnt = nullptr; + IndexType* d_left_unvisited_cnt = nullptr; IndexType vertices_bmap_size; @@ -80,8 +80,8 @@ class BFS { BFS(IndexType _number_of_vertices, IndexType _number_of_edges, - const IndexType *_row_offsets, - const IndexType *_col_indices, + const IndexType* _row_offsets, + const IndexType* _col_indices, bool _directed, IndexType _alpha, IndexType _beta, @@ -98,10 +98,10 @@ class BFS { setup(); } - void configure(IndexType *distances, - IndexType *predecessors, - double *sp_counters, - int *edge_mask); + void configure(IndexType* distances, + IndexType* predecessors, + double* sp_counters, + int* edge_mask); void traverse(IndexType source_vertex); }; diff --git a/cpp/src/traversal/bfs_kernels.cuh b/cpp/src/traversal/bfs_kernels.cuh index bf2ec2fc6ee..4e482b446ba 100644 --- a/cpp/src/traversal/bfs_kernels.cuh +++ b/cpp/src/traversal/bfs_kernels.cuh @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, NVIDIA CORPORATION. + * Copyright (c) 2018-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. @@ -18,7 +18,7 @@ #include #include -#include "graph.hpp" +#include #include "traversal_common.cuh" namespace cugraph { @@ -40,11 +40,11 @@ namespace bfs_kernels { // visited_bmap_nints = the visited_bmap is made of that number of ints template -__global__ void fill_unvisited_queue_kernel(int *visited_bmap, +__global__ void fill_unvisited_queue_kernel(int* visited_bmap, IndexType visited_bmap_nints, IndexType n, - IndexType *unvisited, - IndexType *unvisited_cnt) + IndexType* unvisited, + IndexType* unvisited_cnt) { typedef cub::BlockScan BlockScan; __shared__ typename BlockScan::TempStorage scan_temp_storage; @@ -118,8 +118,8 @@ __global__ void fill_unvisited_queue_kernel(int *visited_bmap, vec_v.z = v_idx * INT_SIZE + traversal::getNextZeroBit(thread_visited_int); vec_v.w = v_idx * INT_SIZE + traversal::getNextZeroBit(thread_visited_int); - typename traversal::vec_t::vec4 *unvisited_i4 = - reinterpret_cast::vec4 *>( + typename traversal::vec_t::vec4* unvisited_i4 = + reinterpret_cast::vec4*>( &unvisited[current_unvisited_index]); *unvisited_i4 = vec_v; @@ -131,8 +131,8 @@ __global__ void fill_unvisited_queue_kernel(int *visited_bmap, vec_v.x = v_idx * INT_SIZE + traversal::getNextZeroBit(thread_visited_int); vec_v.y = v_idx * INT_SIZE + traversal::getNextZeroBit(thread_visited_int); - typename traversal::vec_t::vec2 *unvisited_i2 = - reinterpret_cast::vec2 *>( + typename traversal::vec_t::vec2* unvisited_i2 = + reinterpret_cast::vec2*>( &unvisited[current_unvisited_index]); *unvisited_i2 = vec_v; @@ -152,11 +152,11 @@ __global__ void fill_unvisited_queue_kernel(int *visited_bmap, // Wrapper template -void fill_unvisited_queue(int *visited_bmap, +void fill_unvisited_queue(int* visited_bmap, IndexType visited_bmap_nints, IndexType n, - IndexType *unvisited, - IndexType *unvisited_cnt, + IndexType* unvisited, + IndexType* unvisited_cnt, cudaStream_t m_stream, bool deterministic) { @@ -181,11 +181,11 @@ void fill_unvisited_queue(int *visited_bmap, // template -__global__ void count_unvisited_edges_kernel(const IndexType *potentially_unvisited, +__global__ void count_unvisited_edges_kernel(const IndexType* potentially_unvisited, const IndexType potentially_unvisited_size, - const int *visited_bmap, - IndexType *degree_vertices, - IndexType *mu) + const int* visited_bmap, + IndexType* degree_vertices, + IndexType* mu) { typedef cub::BlockReduce BlockReduce; __shared__ typename BlockReduce::TempStorage reduce_temp_storage; @@ -214,11 +214,11 @@ __global__ void count_unvisited_edges_kernel(const IndexType *potentially_unvisi // Wrapper template -void count_unvisited_edges(const IndexType *potentially_unvisited, +void count_unvisited_edges(const IndexType* potentially_unvisited, const IndexType potentially_unvisited_size, - const int *visited_bmap, - IndexType *node_degree, - IndexType *mu, + const int* visited_bmap, + IndexType* node_degree, + IndexType* mu, cudaStream_t m_stream) { dim3 grid, block; @@ -246,19 +246,19 @@ void count_unvisited_edges(const IndexType *potentially_unvisited, // template -__global__ void main_bottomup_kernel(const IndexType *unvisited, +__global__ void main_bottomup_kernel(const IndexType* unvisited, const IndexType unvisited_size, - IndexType *left_unvisited, - IndexType *left_unvisited_cnt, - int *visited_bmap, - const IndexType *row_ptr, - const IndexType *col_ind, + IndexType* left_unvisited, + IndexType* left_unvisited_cnt, + int* visited_bmap, + const IndexType* row_ptr, + const IndexType* col_ind, IndexType lvl, - IndexType *new_frontier, - IndexType *new_frontier_cnt, - IndexType *distances, - IndexType *predecessors, - int *edge_mask) + IndexType* new_frontier, + IndexType* new_frontier_cnt, + IndexType* distances, + IndexType* predecessors, + int* edge_mask) { typedef cub::BlockDiscontinuity BlockDiscontinuity; typedef cub::WarpReduce WarpReduce; @@ -292,7 +292,7 @@ __global__ void main_bottomup_kernel(const IndexType *unvisited, // When this kernel is converted to support different VT and ET, this // will likely split into invalid_vid and invalid_eid // This is equivalent to ~IndexType(0) (i.e., all bits set to 1) - constexpr IndexType invalid_idx = cugraph::invalid_idx::value; + constexpr IndexType invalid_idx = cugraph::legacy::invalid_idx::value; // we will call __syncthreads inside the loop // we need to keep complete block active @@ -487,19 +487,19 @@ __global__ void main_bottomup_kernel(const IndexType *unvisited, } template -void bottom_up_main(IndexType *unvisited, +void bottom_up_main(IndexType* unvisited, IndexType unvisited_size, - IndexType *left_unvisited, - IndexType *d_left_unvisited_idx, - int *visited, - const IndexType *row_ptr, - const IndexType *col_ind, + IndexType* left_unvisited, + IndexType* d_left_unvisited_idx, + int* visited, + const IndexType* row_ptr, + const IndexType* col_ind, IndexType lvl, - IndexType *new_frontier, - IndexType *new_frontier_idx, - IndexType *distances, - IndexType *predecessors, - int *edge_mask, + IndexType* new_frontier, + IndexType* new_frontier_idx, + IndexType* distances, + IndexType* predecessors, + int* edge_mask, cudaStream_t m_stream, bool deterministic) { @@ -531,17 +531,17 @@ void bottom_up_main(IndexType *unvisited, // MAIN_BOTTOMUP_MAX_EDGES && no parent found // template -__global__ void bottom_up_large_degree_kernel(IndexType *left_unvisited, +__global__ void bottom_up_large_degree_kernel(IndexType* left_unvisited, IndexType left_unvisited_size, - int *visited, - const IndexType *row_ptr, - const IndexType *col_ind, + int* visited, + const IndexType* row_ptr, + const IndexType* col_ind, IndexType lvl, - IndexType *new_frontier, - IndexType *new_frontier_cnt, - IndexType *distances, - IndexType *predecessors, - int *edge_mask) + IndexType* new_frontier, + IndexType* new_frontier_cnt, + IndexType* distances, + IndexType* predecessors, + int* edge_mask) { int logical_lane_id = threadIdx.x % BOTTOM_UP_LOGICAL_WARP_SIZE; int logical_warp_id = threadIdx.x / BOTTOM_UP_LOGICAL_WARP_SIZE; @@ -550,7 +550,7 @@ __global__ void bottom_up_large_degree_kernel(IndexType *left_unvisited, // When this kernel is converted to support different VT and ET, this // will likely split into invalid_vid and invalid_eid // This is equivalent to ~IndexType(0) (i.e., all bits set to 1) - constexpr IndexType invalid_idx = cugraph::invalid_idx::value; + constexpr IndexType invalid_idx = cugraph::legacy::invalid_idx::value; // Inactive threads are not a pb for __ballot (known behaviour) for (IndexType idx = logical_warps_per_block * blockIdx.x + logical_warp_id; @@ -610,17 +610,17 @@ __global__ void bottom_up_large_degree_kernel(IndexType *left_unvisited, } template -void bottom_up_large(IndexType *left_unvisited, +void bottom_up_large(IndexType* left_unvisited, IndexType left_unvisited_size, - int *visited, - const IndexType *row_ptr, - const IndexType *col_ind, + int* visited, + const IndexType* row_ptr, + const IndexType* col_ind, IndexType lvl, - IndexType *new_frontier, - IndexType *new_frontier_idx, - IndexType *distances, - IndexType *predecessors, - int *edge_mask, + IndexType* new_frontier, + IndexType* new_frontier_idx, + IndexType* distances, + IndexType* predecessors, + int* edge_mask, cudaStream_t m_stream, bool deterministic) { @@ -680,24 +680,24 @@ void bottom_up_large(IndexType *left_unvisited, template __global__ void topdown_expand_kernel( - const IndexType *row_ptr, - const IndexType *col_ind, - const IndexType *frontier, + const IndexType* row_ptr, + const IndexType* col_ind, + const IndexType* frontier, const IndexType frontier_size, const IndexType totaldegree, const IndexType max_items_per_thread, const IndexType lvl, - IndexType *new_frontier, - IndexType *new_frontier_cnt, - const IndexType *frontier_degrees_exclusive_sum, - const IndexType *frontier_degrees_exclusive_sum_buckets_offsets, - int *previous_bmap, - int *bmap, - IndexType *distances, - IndexType *predecessors, - double *sp_counters, - const int *edge_mask, - const int *isolated_bmap, + IndexType* new_frontier, + IndexType* new_frontier_cnt, + const IndexType* frontier_degrees_exclusive_sum, + const IndexType* frontier_degrees_exclusive_sum_buckets_offsets, + int* previous_bmap, + int* bmap, + IndexType* distances, + IndexType* predecessors, + double* sp_counters, + const int* edge_mask, + const int* isolated_bmap, bool directed) { // BlockScan @@ -728,7 +728,7 @@ __global__ void topdown_expand_kernel( // When this kernel is converted to support different VT and ET, this // will likely split into invalid_vid and invalid_eid // This is equivalent to ~IndexType(0) (i.e., all bits set to 1) - constexpr IndexType invalid_idx = cugraph::invalid_idx::value; + constexpr IndexType invalid_idx = cugraph::legacy::invalid_idx::value; IndexType n_items_per_thread_left = (totaldegree > block_offset) @@ -844,7 +844,7 @@ __global__ void topdown_expand_kernel( IndexType local_buf1[TOP_DOWN_BATCH_SIZE]; IndexType local_buf2[TOP_DOWN_BATCH_SIZE]; - IndexType *vec_frontier_degrees_exclusive_sum_index = &local_buf2[0]; + IndexType* vec_frontier_degrees_exclusive_sum_index = &local_buf2[0]; #pragma unroll for (IndexType iv = 0; iv < TOP_DOWN_BATCH_SIZE; ++iv) { @@ -869,7 +869,7 @@ __global__ void topdown_expand_kernel( } } - IndexType *vec_row_ptr_u = &local_buf1[0]; + IndexType* vec_row_ptr_u = &local_buf1[0]; #pragma unroll for (int iv = 0; iv < TOP_DOWN_BATCH_SIZE; ++iv) { IndexType u = vec_u[iv]; @@ -878,7 +878,7 @@ __global__ void topdown_expand_kernel( } // We won't need row_ptr after that, reusing pointer - IndexType *vec_dest_v = vec_row_ptr_u; + IndexType* vec_dest_v = vec_row_ptr_u; #pragma unroll for (int iv = 0; iv < TOP_DOWN_BATCH_SIZE; ++iv) { @@ -901,7 +901,7 @@ __global__ void topdown_expand_kernel( } // We don't need vec_frontier_degrees_exclusive_sum_index anymore - IndexType *vec_v_visited_bmap = vec_frontier_degrees_exclusive_sum_index; + IndexType* vec_v_visited_bmap = vec_frontier_degrees_exclusive_sum_index; // Visited bmap need to contain information about the previous // frontier if we actually process every edge (shortest path counting) @@ -916,7 +916,7 @@ __global__ void topdown_expand_kernel( // From now on we will consider v as a frontier candidate // If for some reason vec_candidate[iv] should be put in the // new_frontier Then set vec_candidate[iv] = -1 - IndexType *vec_frontier_candidate = vec_dest_v; + IndexType* vec_frontier_candidate = vec_dest_v; #pragma unroll for (int iv = 0; iv < TOP_DOWN_BATCH_SIZE; ++iv) { @@ -943,7 +943,7 @@ __global__ void topdown_expand_kernel( if (directed) { // vec_v_visited_bmap is available - IndexType *vec_is_isolated_bmap = vec_v_visited_bmap; + IndexType* vec_is_isolated_bmap = vec_v_visited_bmap; #pragma unroll for (int iv = 0; iv < TOP_DOWN_BATCH_SIZE; ++iv) { @@ -1021,7 +1021,7 @@ __global__ void topdown_expand_kernel( IndexType naccepted_vertices = 0; // We won't need vec_frontier_candidate after that - IndexType *vec_frontier_accepted_vertex = vec_frontier_candidate; + IndexType* vec_frontier_accepted_vertex = vec_frontier_candidate; #pragma unroll for (int iv = 0; iv < TOP_DOWN_BATCH_SIZE; ++iv) { @@ -1094,23 +1094,23 @@ __global__ void topdown_expand_kernel( } template -void frontier_expand(const IndexType *row_ptr, - const IndexType *col_ind, - const IndexType *frontier, +void frontier_expand(const IndexType* row_ptr, + const IndexType* col_ind, + const IndexType* frontier, const IndexType frontier_size, const IndexType totaldegree, const IndexType lvl, - IndexType *new_frontier, - IndexType *new_frontier_cnt, - const IndexType *frontier_degrees_exclusive_sum, - const IndexType *frontier_degrees_exclusive_sum_buckets_offsets, - int *previous_visited_bmap, - int *visited_bmap, - IndexType *distances, - IndexType *predecessors, - double *sp_counters, - const int *edge_mask, - const int *isolated_bmap, + IndexType* new_frontier, + IndexType* new_frontier_cnt, + const IndexType* frontier_degrees_exclusive_sum, + const IndexType* frontier_degrees_exclusive_sum_buckets_offsets, + int* previous_visited_bmap, + int* visited_bmap, + IndexType* distances, + IndexType* predecessors, + double* sp_counters, + const int* edge_mask, + const int* isolated_bmap, bool directed, cudaStream_t m_stream, bool deterministic) diff --git a/cpp/src/traversal/mg/bfs.cuh b/cpp/src/traversal/mg/bfs.cuh index b053a6ff75a..e6c8c3bf700 100644 --- a/cpp/src/traversal/mg/bfs.cuh +++ b/cpp/src/traversal/mg/bfs.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -28,12 +28,12 @@ namespace mg { namespace detail { template -void bfs_traverse(raft::handle_t const &handle, - cugraph::GraphCSRView const &graph, +void bfs_traverse(raft::handle_t const& handle, + cugraph::legacy::GraphCSRView const& graph, const vertex_t start_vertex, - rmm::device_vector &visited_bmap, - rmm::device_vector &output_frontier_bmap, - operator_t &bfs_op) + rmm::device_vector& visited_bmap, + rmm::device_vector& output_frontier_bmap, + operator_t& bfs_op) { // Frontiers required for BFS rmm::device_vector input_frontier(graph.number_of_vertices); @@ -47,8 +47,8 @@ void bfs_traverse(raft::handle_t const &handle, // Reusing buffers to create isolated bitmap { - rmm::device_vector &local_isolated_ids = input_frontier; - rmm::device_vector &global_isolated_ids = output_frontier; + rmm::device_vector& local_isolated_ids = input_frontier; + rmm::device_vector& global_isolated_ids = output_frontier; detail::create_isolated_bitmap( handle, graph, local_isolated_ids, global_isolated_ids, temp_buffer_len, isolated_bmap); } @@ -110,10 +110,10 @@ void bfs_traverse(raft::handle_t const &handle, } // namespace detail template -void bfs(raft::handle_t const &handle, - cugraph::GraphCSRView const &graph, - vertex_t *distances, - vertex_t *predecessors, +void bfs(raft::handle_t const& handle, + cugraph::legacy::GraphCSRView const& graph, + vertex_t* distances, + vertex_t* predecessors, const vertex_t start_vertex) { CUGRAPH_EXPECTS(handle.comms_initialized(), @@ -132,7 +132,7 @@ void bfs(raft::handle_t const &handle, thrust::fill(rmm::exec_policy(stream)->on(stream), predecessors, predecessors + global_number_of_vertices, - cugraph::invalid_idx::value); + cugraph::legacy::invalid_idx::value); if (distances == nullptr) { detail::BFSStepNoDist bfs_op( diff --git a/cpp/src/traversal/mg/common_utils.cuh b/cpp/src/traversal/mg/common_utils.cuh index 2cda827b471..9a95aba7901 100644 --- a/cpp/src/traversal/mg/common_utils.cuh +++ b/cpp/src/traversal/mg/common_utils.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -16,10 +16,15 @@ #pragma once -#include +#include "../traversal_common.cuh" + #include +#include #include -#include "../traversal_common.cuh" + +#include +#include +#include namespace cugraph { @@ -41,25 +46,25 @@ constexpr inline return_t number_of_words(return_t number_of_bits) template struct isDegreeZero { - edge_t const *offset_; - isDegreeZero(edge_t const *offset) : offset_(offset) {} + edge_t const* offset_; + isDegreeZero(edge_t const* offset) : offset_(offset) {} - __device__ bool operator()(const edge_t &id) const { return (offset_[id + 1] == offset_[id]); } + __device__ bool operator()(const edge_t& id) const { return (offset_[id + 1] == offset_[id]); } }; struct set_nth_bit { - uint32_t *bmap_; - set_nth_bit(uint32_t *bmap) : bmap_(bmap) {} + uint32_t* bmap_; + set_nth_bit(uint32_t* bmap) : bmap_(bmap) {} template - __device__ void operator()(const return_t &id) + __device__ void operator()(const return_t& id) { atomicOr(bmap_ + (id / BitsPWrd), (uint32_t{1} << (id % BitsPWrd))); } }; template -bool is_vertex_isolated(rmm::device_vector &bmap, vertex_t id) +bool is_vertex_isolated(rmm::device_vector& bmap, vertex_t id) { uint32_t word = bmap[id / BitsPWrd]; uint32_t active_bit = static_cast(1) << (id % BitsPWrd); @@ -69,11 +74,11 @@ bool is_vertex_isolated(rmm::device_vector &bmap, vertex_t id) template struct BFSStepNoDist { - uint32_t *output_frontier_; - uint32_t *visited_; - vertex_t *predecessors_; + uint32_t* output_frontier_; + uint32_t* visited_; + vertex_t* predecessors_; - BFSStepNoDist(uint32_t *output_frontier, uint32_t *visited, vertex_t *predecessors) + BFSStepNoDist(uint32_t* output_frontier, uint32_t* visited, vertex_t* predecessors) : output_frontier_(output_frontier), visited_(visited), predecessors_(predecessors) { } @@ -100,13 +105,13 @@ struct BFSStepNoDist { template struct BFSStep { - uint32_t *output_frontier_; - uint32_t *visited_; - vertex_t *predecessors_; - vertex_t *distances_; + uint32_t* output_frontier_; + uint32_t* visited_; + vertex_t* predecessors_; + vertex_t* distances_; vertex_t level_; - BFSStep(uint32_t *output_frontier, uint32_t *visited, vertex_t *predecessors, vertex_t *distances) + BFSStep(uint32_t* output_frontier, uint32_t* visited, vertex_t* predecessors, vertex_t* distances) : output_frontier_(output_frontier), visited_(visited), predecessors_(predecessors), @@ -136,9 +141,10 @@ struct BFSStep { }; template -vertex_t populate_isolated_vertices(raft::handle_t const &handle, - cugraph::GraphCSRView const &graph, - rmm::device_vector &isolated_vertex_ids) +vertex_t populate_isolated_vertices( + raft::handle_t const& handle, + cugraph::legacy::GraphCSRView const& graph, + rmm::device_vector& isolated_vertex_ids) { bool is_mg = (handle.comms_initialized() && (graph.local_vertices != nullptr) && (graph.local_offsets != nullptr)); @@ -164,11 +170,11 @@ vertex_t populate_isolated_vertices(raft::handle_t const &handle, } template -return_t collect_vectors(raft::handle_t const &handle, - rmm::device_vector &buffer_len, - rmm::device_vector &local, +return_t collect_vectors(raft::handle_t const& handle, + rmm::device_vector& buffer_len, + rmm::device_vector& local, return_t local_count, - rmm::device_vector &global) + rmm::device_vector& global) { CHECK_CUDA(handle.get_stream()); buffer_len.resize(handle.get_comms().get_size()); @@ -200,9 +206,9 @@ return_t collect_vectors(raft::handle_t const &handle, } template -void add_to_bitmap(raft::handle_t const &handle, - rmm::device_vector &bmap, - rmm::device_vector &id, +void add_to_bitmap(raft::handle_t const& handle, + rmm::device_vector& bmap, + rmm::device_vector& id, return_t count) { cudaStream_t stream = handle.get_stream(); @@ -216,12 +222,12 @@ void add_to_bitmap(raft::handle_t const &handle, // For all vertex ids i which are isolated (out degree is 0), set // ith bit of isolated_bmap to 1 template -void create_isolated_bitmap(raft::handle_t const &handle, - cugraph::GraphCSRView const &graph, - rmm::device_vector &local_isolated_ids, - rmm::device_vector &global_isolated_ids, - rmm::device_vector &temp_buffer_len, - rmm::device_vector &isolated_bmap) +void create_isolated_bitmap(raft::handle_t const& handle, + cugraph::legacy::GraphCSRView const& graph, + rmm::device_vector& local_isolated_ids, + rmm::device_vector& global_isolated_ids, + rmm::device_vector& temp_buffer_len, + rmm::device_vector& isolated_bmap) { size_t word_count = detail::number_of_words(graph.number_of_vertices); local_isolated_ids.resize(graph.number_of_vertices); @@ -236,8 +242,8 @@ void create_isolated_bitmap(raft::handle_t const &handle, } template -return_t remove_duplicates(raft::handle_t const &handle, - rmm::device_vector &data, +return_t remove_duplicates(raft::handle_t const& handle, + rmm::device_vector& data, return_t data_len) { cudaStream_t stream = handle.get_stream(); @@ -253,13 +259,13 @@ return_t remove_duplicates(raft::handle_t const &handle, // ids. bmap is expected to be of the length // id_end/BitsPWrd and is set to 0 initially template -__global__ void remove_duplicates_kernel(uint32_t *bmap, - return_t *in_id, +__global__ void remove_duplicates_kernel(uint32_t* bmap, + return_t* in_id, return_t id_begin, return_t id_end, return_t count, - return_t *out_id, - return_t *out_count) + return_t* out_id, + return_t* out_count) { return_t tid = blockIdx.x * blockDim.x + threadIdx.x; return_t id; @@ -301,14 +307,14 @@ __global__ void remove_duplicates_kernel(uint32_t *bmap, } template -__global__ void remove_duplicates_kernel(uint32_t *bmap, - uint32_t *isolated_bmap, - return_t *in_id, +__global__ void remove_duplicates_kernel(uint32_t* bmap, + uint32_t* isolated_bmap, + return_t* in_id, return_t id_begin, return_t id_end, return_t count, - return_t *out_id, - return_t *out_count) + return_t* out_id, + return_t* out_count) { return_t tid = blockIdx.x * blockDim.x + threadIdx.x; return_t id; @@ -354,13 +360,13 @@ __global__ void remove_duplicates_kernel(uint32_t *bmap, } template -return_t remove_duplicates(raft::handle_t const &handle, - rmm::device_vector &bmap, - rmm::device_vector &data, +return_t remove_duplicates(raft::handle_t const& handle, + rmm::device_vector& bmap, + rmm::device_vector& data, return_t data_len, return_t data_begin, return_t data_end, - rmm::device_vector &out_data) + rmm::device_vector& out_data) { cudaStream_t stream = handle.get_stream(); @@ -382,13 +388,14 @@ return_t remove_duplicates(raft::handle_t const &handle, } template -vertex_t preprocess_input_frontier(raft::handle_t const &handle, - cugraph::GraphCSRView const &graph, - rmm::device_vector &bmap, - rmm::device_vector &isolated_bmap, - rmm::device_vector &input_frontier, - vertex_t input_frontier_len, - rmm::device_vector &output_frontier) +vertex_t preprocess_input_frontier( + raft::handle_t const& handle, + cugraph::legacy::GraphCSRView const& graph, + rmm::device_vector& bmap, + rmm::device_vector& isolated_bmap, + rmm::device_vector& input_frontier, + vertex_t input_frontier_len, + rmm::device_vector& output_frontier) { cudaStream_t stream = handle.get_stream(); @@ -414,12 +421,13 @@ vertex_t preprocess_input_frontier(raft::handle_t const &handle, } template -vertex_t preprocess_input_frontier(raft::handle_t const &handle, - cugraph::GraphCSRView const &graph, - rmm::device_vector &bmap, - rmm::device_vector &input_frontier, - vertex_t input_frontier_len, - rmm::device_vector &output_frontier) +vertex_t preprocess_input_frontier( + raft::handle_t const& handle, + cugraph::legacy::GraphCSRView const& graph, + rmm::device_vector& bmap, + rmm::device_vector& input_frontier, + vertex_t input_frontier_len, + rmm::device_vector& output_frontier) { cudaStream_t stream = handle.get_stream(); @@ -444,7 +452,7 @@ vertex_t preprocess_input_frontier(raft::handle_t const &handle, } template -__global__ void fill_kernel(vertex_t *distances, vertex_t count, vertex_t start_vertex) +__global__ void fill_kernel(vertex_t* distances, vertex_t count, vertex_t start_vertex) { vertex_t tid = blockIdx.x * blockDim.x + threadIdx.x; if (tid >= count) { return; } @@ -456,11 +464,11 @@ __global__ void fill_kernel(vertex_t *distances, vertex_t count, vertex_t start_ } template -void fill_max_dist(raft::handle_t const &handle, - cugraph::GraphCSRView const &graph, +void fill_max_dist(raft::handle_t const& handle, + cugraph::legacy::GraphCSRView const& graph, vertex_t start_vertex, vertex_t global_number_of_vertices, - vertex_t *distances) + vertex_t* distances) { if (distances == nullptr) { return; } vertex_t array_size = global_number_of_vertices; @@ -470,8 +478,9 @@ void fill_max_dist(raft::handle_t const &handle, } template -vertex_t get_global_vertex_count(raft::handle_t const &handle, - cugraph::GraphCSRView const &graph) +vertex_t get_global_vertex_count( + raft::handle_t const& handle, + cugraph::legacy::GraphCSRView const& graph) { rmm::device_vector id(1); id[0] = *thrust::max_element(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), diff --git a/cpp/src/traversal/mg/frontier_expand.cuh b/cpp/src/traversal/mg/frontier_expand.cuh index 2733c319087..078ab085724 100644 --- a/cpp/src/traversal/mg/frontier_expand.cuh +++ b/cpp/src/traversal/mg/frontier_expand.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -16,7 +16,7 @@ #pragma once -#include +#include #include "frontier_expand_kernels.cuh" #include "vertex_binning.cuh" @@ -28,8 +28,8 @@ namespace detail { template class FrontierExpand { - raft::handle_t const &handle_; - cugraph::GraphCSRView const &graph_; + raft::handle_t const& handle_; + cugraph::legacy::GraphCSRView const& graph_; VertexBinner dist_; rmm::device_vector reorganized_vertices_; edge_t vertex_begin_; @@ -37,8 +37,8 @@ class FrontierExpand { rmm::device_vector output_vertex_count_; public: - FrontierExpand(raft::handle_t const &handle, - cugraph::GraphCSRView const &graph) + FrontierExpand(raft::handle_t const& handle, + cugraph::legacy::GraphCSRView const& graph) : handle_(handle), graph_(graph) { bool is_mg = (handle.comms_initialized() && (graph.local_vertices != nullptr) && @@ -59,9 +59,9 @@ class FrontierExpand { // Return the size of the output_frontier template vertex_t operator()(operator_t op, - rmm::device_vector &input_frontier, + rmm::device_vector& input_frontier, vertex_t input_frontier_len, - rmm::device_vector &output_frontier) + rmm::device_vector& output_frontier) { if (input_frontier_len == 0) { return static_cast(0); } cudaStream_t stream = handle_.get_stream(); diff --git a/cpp/src/traversal/mg/frontier_expand_kernels.cuh b/cpp/src/traversal/mg/frontier_expand_kernels.cuh index 625ec0d956f..32b9310f02f 100644 --- a/cpp/src/traversal/mg/frontier_expand_kernels.cuh +++ b/cpp/src/traversal/mg/frontier_expand_kernels.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -16,7 +16,7 @@ #pragma once -#include +#include #include "vertex_binning.cuh" namespace cugraph { @@ -26,13 +26,13 @@ namespace mg { namespace detail { template -__device__ void write_to_frontier(vertex_t const *thread_frontier, +__device__ void write_to_frontier(vertex_t const* thread_frontier, int thread_frontier_count, - vertex_t *block_frontier, - int *block_frontier_count, - vertex_t *output_frontier, - edge_t *block_write_offset, - edge_t *output_frontier_count) + vertex_t* block_frontier, + int* block_frontier_count, + vertex_t* output_frontier, + edge_t* block_write_offset, + edge_t* output_frontier_count) { // Set frontier count for block to 0 if (threadIdx.x == 0) { *block_frontier_count = 0; } @@ -66,13 +66,13 @@ template -__global__ void block_per_vertex(edge_t const *offsets, - vertex_t const *indices, - vertex_t const *input_frontier, +__global__ void block_per_vertex(edge_t const* offsets, + vertex_t const* indices, + vertex_t const* input_frontier, vertex_t input_frontier_count, vertex_t vertex_begin, - vertex_t *output_frontier, - edge_t *output_frontier_count, + vertex_t* output_frontier, + edge_t* output_frontier_count, operator_t op) { if (blockIdx.x >= input_frontier_count) { return; } @@ -121,13 +121,13 @@ template -__global__ void kernel_per_vertex(edge_t const *offsets, - vertex_t const *indices, - vertex_t const *input_frontier, +__global__ void kernel_per_vertex(edge_t const* offsets, + vertex_t const* indices, + vertex_t const* input_frontier, vertex_t input_frontier_count, vertex_t vertex_begin, - vertex_t *output_frontier, - edge_t *output_frontier_count, + vertex_t* output_frontier, + edge_t* output_frontier_count, operator_t op) { vertex_t current_vertex_index = 0; @@ -171,12 +171,12 @@ __global__ void kernel_per_vertex(edge_t const *offsets, } template -void large_vertex_lb(cugraph::GraphCSRView const &graph, - DegreeBucket &bucket, +void large_vertex_lb(cugraph::legacy::GraphCSRView const& graph, + DegreeBucket& bucket, operator_t op, vertex_t vertex_begin, - vertex_t *output_vertex_ids, - edge_t *output_vertex_ids_offset, + vertex_t* output_vertex_ids, + edge_t* output_vertex_ids_offset, cudaStream_t stream) { if (bucket.numberOfVertices != 0) { @@ -196,12 +196,12 @@ void large_vertex_lb(cugraph::GraphCSRView const &gr } template -void medium_vertex_lb(cugraph::GraphCSRView const &graph, - DegreeBucket &bucket, +void medium_vertex_lb(cugraph::legacy::GraphCSRView const& graph, + DegreeBucket& bucket, operator_t op, vertex_t vertex_begin, - vertex_t *output_vertex_ids, - edge_t *output_vertex_ids_offset, + vertex_t* output_vertex_ids, + edge_t* output_vertex_ids_offset, cudaStream_t stream) { // Vertices with degrees 2^12 <= d < 2^16 are handled by this kernel @@ -223,12 +223,12 @@ void medium_vertex_lb(cugraph::GraphCSRView const &g } template -void small_vertex_lb(cugraph::GraphCSRView const &graph, - DegreeBucket &bucket, +void small_vertex_lb(cugraph::legacy::GraphCSRView const& graph, + DegreeBucket& bucket, operator_t op, vertex_t vertex_begin, - vertex_t *output_vertex_ids, - edge_t *output_vertex_ids_offset, + vertex_t* output_vertex_ids, + edge_t* output_vertex_ids_offset, cudaStream_t stream) { int block_count = bucket.numberOfVertices; diff --git a/cpp/src/traversal/mg/vertex_binning.cuh b/cpp/src/traversal/mg/vertex_binning.cuh index 3d8c963c466..b4ed881a06e 100644 --- a/cpp/src/traversal/mg/vertex_binning.cuh +++ b/cpp/src/traversal/mg/vertex_binning.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -19,6 +19,8 @@ #include "common_utils.cuh" #include "vertex_binning_kernels.cuh" +#include + namespace cugraph { namespace mg { diff --git a/cpp/src/traversal/mg/vertex_binning_kernels.cuh b/cpp/src/traversal/mg/vertex_binning_kernels.cuh index dbb339fea05..57574965a3a 100644 --- a/cpp/src/traversal/mg/vertex_binning_kernels.cuh +++ b/cpp/src/traversal/mg/vertex_binning_kernels.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -40,7 +40,7 @@ __device__ inline typename std::enable_if<(sizeof(degree_t) == 8), int>::type ce } template -__global__ void simple_fill(return_t *bin0, return_t *bin1, return_t count) +__global__ void simple_fill(return_t* bin0, return_t* bin1, return_t count) { for (return_t i = 0; i < count; i++) { bin0[i] = 0; @@ -49,7 +49,7 @@ __global__ void simple_fill(return_t *bin0, return_t *bin1, return_t count) } template -__global__ void exclusive_scan(return_t *data, return_t *out) +__global__ void exclusive_scan(return_t* data, return_t* out) { constexpr int BinCount = NumberBins; return_t lData[BinCount]; @@ -71,9 +71,9 @@ __global__ void exclusive_scan(return_t *data, return_t *out) // In this function, any id in vertex_ids array is only acceptable as long // as its value is between vertex_begin and vertex_end template -__global__ void count_bin_sizes(edge_t *bins, - edge_t const *offsets, - vertex_t const *vertex_ids, +__global__ void count_bin_sizes(edge_t* bins, + edge_t const* offsets, + vertex_t const* vertex_ids, edge_t const vertex_id_count, vertex_t vertex_begin, vertex_t vertex_end) @@ -81,7 +81,9 @@ __global__ void count_bin_sizes(edge_t *bins, using cugraph::detail::traversal::atomicAdd; constexpr int BinCount = NumberBins; __shared__ edge_t lBin[BinCount]; - for (int i = threadIdx.x; i < BinCount; i += blockDim.x) { lBin[i] = 0; } + for (int i = threadIdx.x; i < BinCount; i += blockDim.x) { + lBin[i] = 0; + } __syncthreads(); for (vertex_t i = threadIdx.x + (blockIdx.x * blockDim.x); i < vertex_id_count; @@ -98,16 +100,18 @@ __global__ void count_bin_sizes(edge_t *bins, } __syncthreads(); - for (int i = threadIdx.x; i < BinCount; i += blockDim.x) { atomicAdd(bins + i, lBin[i]); } + for (int i = threadIdx.x; i < BinCount; i += blockDim.x) { + atomicAdd(bins + i, lBin[i]); + } } // Bin vertices to the appropriate bins by taking into account // the starting offsets calculated by count_bin_sizes template -__global__ void create_vertex_bins(vertex_t *out_vertex_ids, - edge_t *bin_offsets, - edge_t const *offsets, - vertex_t *in_vertex_ids, +__global__ void create_vertex_bins(vertex_t* out_vertex_ids, + edge_t* bin_offsets, + edge_t const* offsets, + vertex_t* in_vertex_ids, edge_t const vertex_id_count, vertex_t vertex_begin, vertex_t vertex_end) @@ -149,12 +153,12 @@ __global__ void create_vertex_bins(vertex_t *out_vertex_ids, } template -void bin_vertices(rmm::device_vector &input_vertex_ids, +void bin_vertices(rmm::device_vector& input_vertex_ids, vertex_t input_vertex_ids_len, - rmm::device_vector &reorganized_vertex_ids, - rmm::device_vector &bin_count_offsets, - rmm::device_vector &bin_count, - edge_t *offsets, + rmm::device_vector& reorganized_vertex_ids, + rmm::device_vector& bin_count_offsets, + rmm::device_vector& bin_count, + edge_t* offsets, vertex_t vertex_begin, vertex_t vertex_end, cudaStream_t stream) diff --git a/cpp/src/traversal/sssp.cu b/cpp/src/traversal/sssp.cu index 4018c9d9878..61225dd7fd6 100644 --- a/cpp/src/traversal/sssp.cu +++ b/cpp/src/traversal/sssp.cu @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. @@ -17,9 +17,9 @@ // Author: Prasun Gera pgera@nvidia.com #include -#include +#include -#include "graph.hpp" +#include #include "sssp.cuh" #include "sssp_kernels.cuh" @@ -47,11 +47,11 @@ void SSSP::setup() // Allocate buffer for data that need to be reset every iteration iter_buffer_size = sizeof(int) * (edges_bmap_size + vertices_bmap_size) + sizeof(IndexType); - iter_buffer.resize(iter_buffer_size); + iter_buffer.resize(iter_buffer_size, stream); // ith bit of relaxed_edges_bmap <=> ith edge was relaxed - relaxed_edges_bmap = static_cast(iter_buffer.data()); + relaxed_edges_bmap = static_cast(iter_buffer.data()); // ith bit of next_frontier_bmap <=> vertex is active in the next frontier - next_frontier_bmap = static_cast(iter_buffer.data()) + edges_bmap_size; + next_frontier_bmap = static_cast(iter_buffer.data()) + edges_bmap_size; // num vertices in the next frontier d_new_frontier_cnt = next_frontier_bmap + vertices_bmap_size; @@ -73,7 +73,7 @@ void SSSP::setup() exclusive_sum_frontier_vertex_buckets_offsets.resize(bucket_off_size); // Repurpose d_new_frontier_cnt temporarily - IndexType *d_nisolated = d_new_frontier_cnt; + IndexType* d_nisolated = d_new_frontier_cnt; cudaMemsetAsync(d_nisolated, 0, sizeof(IndexType), stream); // Computing isolated_bmap @@ -89,9 +89,9 @@ void SSSP::setup() } template -void SSSP::configure(DistType *_distances, - IndexType *_predecessors, - int *_edge_mask) +void SSSP::configure(DistType* _distances, + IndexType* _predecessors, + int* _edge_mask) { distances = _distances; predecessors = _predecessors; @@ -242,12 +242,12 @@ void SSSP::clean() * @file sssp.cu * --------------------------------------------------------------------------*/ template -void sssp(GraphCSRView const &graph, - WT *distances, - VT *predecessors, +void sssp(legacy::GraphCSRView const& graph, + WT* distances, + VT* predecessors, const VT source_vertex) { - CUGRAPH_EXPECTS(distances || predecessors, "Invalid API parameter, both outputs are nullptr"); + CUGRAPH_EXPECTS(distances || predecessors, "Invalid input argument, both outputs are nullptr"); if (typeid(VT) != typeid(int)) CUGRAPH_FAIL("Unsupported vertex id data type, please use int"); if (typeid(ET) != typeid(int)) CUGRAPH_FAIL("Unsupported edge id data type, please use int"); @@ -257,9 +257,9 @@ void sssp(GraphCSRView const &graph, int num_vertices = graph.number_of_vertices; int num_edges = graph.number_of_edges; - const ET *offsets_ptr = graph.offsets; - const VT *indices_ptr = graph.indices; - const WT *edge_weights_ptr = nullptr; + const ET* offsets_ptr = graph.offsets; + const VT* indices_ptr = graph.indices; + const WT* edge_weights_ptr = nullptr; // Both if / else branch operate own calls due to // thrust::device_vector lifetime @@ -281,7 +281,7 @@ void sssp(GraphCSRView const &graph, } else { // SSSP is not defined for graphs with negative weight cycles // Warn user about any negative edges - if (graph.prop.has_negative_edges == PropType::PROP_TRUE) + if (graph.prop.has_negative_edges == legacy::PropType::PROP_TRUE) std::cerr << "WARN: The graph has negative weight edges. SSSP will not " "converge if the graph has negative weight cycles\n"; edge_weights_ptr = graph.edge_data; @@ -293,13 +293,13 @@ void sssp(GraphCSRView const &graph, } // explicit instantiation -template void sssp(GraphCSRView const &graph, - float *distances, - int *predecessors, +template void sssp(legacy::GraphCSRView const& graph, + float* distances, + int* predecessors, const int source_vertex); -template void sssp(GraphCSRView const &graph, - double *distances, - int *predecessors, +template void sssp(legacy::GraphCSRView const& graph, + double* distances, + int* predecessors, const int source_vertex); } // namespace cugraph diff --git a/cpp/src/traversal/sssp_kernels.cuh b/cpp/src/traversal/sssp_kernels.cuh index d96540b22b9..d1cf9980773 100644 --- a/cpp/src/traversal/sssp_kernels.cuh +++ b/cpp/src/traversal/sssp_kernels.cuh @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. @@ -19,8 +19,8 @@ #include #include +#include #include "traversal_common.cuh" -#include "utilities/error.hpp" namespace cugraph { namespace detail { namespace sssp_kernels { diff --git a/cpp/src/traversal/traversal_common.cuh b/cpp/src/traversal/traversal_common.cuh index 2802fb94be8..ea77173870e 100644 --- a/cpp/src/traversal/traversal_common.cuh +++ b/cpp/src/traversal/traversal_common.cuh @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. @@ -17,11 +17,11 @@ #pragma once #include -#include "utilities/error.hpp" +#include #define MAXBLOCKS 65535 #define WARP_SIZE 32 -#define INT_SIZE 32 +#define INT_SIZE 32 // // Bottom up macros @@ -31,7 +31,7 @@ #define COUNT_UNVISITED_EDGES_DIMX 256 -#define MAIN_BOTTOMUP_DIMX 256 +#define MAIN_BOTTOMUP_DIMX 256 #define MAIN_BOTTOMUP_NWARPS (MAIN_BOTTOMUP_DIMX / WARP_SIZE) #define LARGE_BOTTOMUP_DIMX 256 diff --git a/cpp/src/traversal/tsp.cu b/cpp/src/traversal/tsp.cu new file mode 100644 index 00000000000..9be4f4f3767 --- /dev/null +++ b/cpp/src/traversal/tsp.cu @@ -0,0 +1,273 @@ +/* + * Copyright (c) 2021, NVIDIA CORPORATION. + * Copyright (c) 2014-2020, Texas State University. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include + +#include + +#include "tsp.hpp" +#include "tsp_solver.hpp" + +namespace cugraph { +namespace detail { + +TSP::TSP(raft::handle_t const& handle, + int const* vtx_ptr, + float const* x_pos, + float const* y_pos, + int nodes, + int restarts, + bool beam_search, + int k, + int nstart, + bool verbose, + int* route) + : handle_(handle), + vtx_ptr_(vtx_ptr), + x_pos_(x_pos), + y_pos_(y_pos), + nodes_(nodes), + restarts_(restarts), + beam_search_(beam_search), + k_(k), + nstart_(nstart), + verbose_(verbose), + route_(route), + stream_(handle_.get_stream()), + max_blocks_(handle_.get_device_properties().maxGridSize[0]), + max_threads_(handle_.get_device_properties().maxThreadsPerBlock), + warp_size_(handle_.get_device_properties().warpSize), + sm_count_(handle_.get_device_properties().multiProcessorCount), + restart_batch_(8192), + mylock_scalar_(stream_), + best_cost_scalar_(stream_), + neighbors_vec_((k_ + 1) * nodes_, stream_), + work_vec_(restart_batch_ * ((4 * nodes_ + 3 + warp_size_ - 1) / warp_size_ * warp_size_), + stream_), + best_x_pos_vec_(1, stream_), + best_y_pos_vec_(1, stream_), + best_route_vec_(1, stream_) +{ + setup(); +} + +void TSP::setup() +{ + mylock_ = mylock_scalar_.data(); + + neighbors_ = neighbors_vec_.data(); + // pre-allocate workspace for climbs, each block needs a separate permutation space and search + // buffer. We allocate a work buffer that will store the computed distances, px, py and the route. + // We align it on the warp size. + work_ = work_vec_.data(); + + results_.best_x_pos = best_x_pos_vec_.data(); + results_.best_y_pos = best_y_pos_vec_.data(); + results_.best_route = best_route_vec_.data(); + results_.best_cost = best_cost_scalar_.data(); +} + +void TSP::reset_batch() +{ + mylock_scalar_.set_value_to_zero_async(stream_); + auto const max{std::numeric_limits::max()}; + best_cost_scalar_.set_value_async(max, stream_); +} + +void TSP::get_initial_solution(int const batch) +{ + if (!beam_search_) { + random_init<<>>( + work_, x_pos_, y_pos_, vtx_ptr_, nstart_, nodes_, batch, restart_batch_); + CHECK_CUDA(stream_); + } else { + knn_init<<>>( + work_, x_pos_, y_pos_, vtx_ptr_, neighbors_, nstart_, nodes_, k_, batch, restart_batch_); + CHECK_CUDA(stream_); + } +} + +float TSP::compute() +{ + float final_cost = 0.f; + int num_restart_batches = (restarts_ + restart_batch_ - 1) / restart_batch_; + int restart_resid = restarts_ - (num_restart_batches - 1) * restart_batch_; + int global_best = std::numeric_limits::max(); + int best = 0; + + std::vector h_x_pos; + std::vector h_y_pos; + std::vector h_route; + h_x_pos.reserve(nodes_ + 1); + h_y_pos.reserve(nodes_ + 1); + h_route.reserve(nodes_); + std::vector addr_best_x_pos(1); + std::vector addr_best_y_pos(1); + std::vector addr_best_route(1); + HighResTimer hr_timer; + auto create_timer = [&hr_timer, this](char const* name) { + return VerboseTimer(name, hr_timer, verbose_); + }; + + if (verbose_) { + std::cout << "Doing " << num_restart_batches << " batches of size " << restart_batch_ + << ", with " << restart_resid << " tail\n"; + std::cout << "configuration: " << nodes_ << " nodes, " << restarts_ << " restart\n"; + std::cout << "optimizing graph with kswap = " << kswaps << "\n"; + } + + // Tell the cache how we want it to behave + cudaFuncSetCacheConfig(search_solution, cudaFuncCachePreferEqual); + best_thread_num_ = best_thread_count(nodes_, max_threads_, sm_count_, warp_size_); + + if (verbose_) std::cout << "Calculated best thread number = " << best_thread_num_ << "\n"; + + if (beam_search_) { + auto timer = create_timer("knn"); + knn(); + } + + for (auto batch = 0; batch < num_restart_batches; ++batch) { + reset_batch(); + if (batch == num_restart_batches - 1) restart_batch_ = restart_resid; + + { + auto timer = create_timer("initial_sol"); + get_initial_solution(batch); + } + + { + auto timer = create_timer("search_sol"); + search_solution<<>>( + results_, mylock_, vtx_ptr_, beam_search_, k_, nodes_, x_pos_, y_pos_, work_, nstart_); + CHECK_CUDA(stream_); + } + + { + auto timer = create_timer("optimal_tour"); + get_optimal_tour<<>>(results_, mylock_, work_, nodes_); + CHECK_CUDA(stream_); + } + + cudaDeviceSynchronize(); + best = best_cost_scalar_.value(stream_); + + if (verbose_) std::cout << "Best reported by kernel = " << best << "\n"; + + if (best < global_best) { + global_best = best; + + raft::update_host(addr_best_x_pos.data(), results_.best_x_pos, 1, stream_); + raft::update_host(addr_best_y_pos.data(), results_.best_y_pos, 1, stream_); + raft::update_host(addr_best_route.data(), results_.best_route, 1, stream_); + CUDA_TRY(cudaStreamSynchronize(stream_)); + + raft::copy(h_x_pos.data(), addr_best_x_pos[0], nodes_ + 1, stream_); + raft::copy(h_y_pos.data(), addr_best_y_pos[0], nodes_ + 1, stream_); + raft::copy(h_route.data(), addr_best_route[0], nodes_, stream_); + raft::copy(route_, addr_best_route[0], nodes_, stream_); + CHECK_CUDA(stream_); + } + } + + for (auto i = 0; i < nodes_; ++i) { + if (verbose_) { std::cout << h_route[i] << ": " << h_x_pos[i] << " " << h_y_pos[i] << "\n"; } + final_cost += euclidean_dist(h_x_pos.data(), h_y_pos.data(), i, i + 1); + } + + if (verbose_) { + hr_timer.display(std::cout); + std::cout << "Optimized tour length = " << global_best << "\n"; + } + + return final_cost; +} + +void TSP::knn() +{ + if (verbose_) std::cout << "Looking at " << k_ << " nearest neighbors\n"; + + int dim = 2; + bool row_major_order = false; + + rmm::device_uvector input(nodes_ * dim, stream_); + float* input_ptr = input.data(); + raft::copy(input_ptr, x_pos_, nodes_, stream_); + raft::copy(input_ptr + nodes_, y_pos_, nodes_, stream_); + + rmm::device_uvector search_data(nodes_ * dim, stream_); + float* search_data_ptr = search_data.data(); + raft::copy(search_data_ptr, input_ptr, nodes_ * dim, stream_); + + rmm::device_uvector distances(nodes_ * (k_ + 1), stream_); + float* distances_ptr = distances.data(); + + std::vector input_vec; + std::vector sizes_vec; + input_vec.push_back(input_ptr); + sizes_vec.push_back(nodes_); + + // k neighbors + 1 is needed because the nearest neighbor of each point is + // the point itself that we don't want to take into account. + + raft::spatial::knn::brute_force_knn(handle_, + input_vec, + sizes_vec, + dim, + search_data_ptr, + nodes_, + neighbors_, + distances_ptr, + k_ + 1, + row_major_order, + row_major_order); +} +} // namespace detail + +float traveling_salesperson(raft::handle_t const& handle, + int const* vtx_ptr, + float const* x_pos, + float const* y_pos, + int nodes, + int restarts, + bool beam_search, + int k, + int nstart, + bool verbose, + int* route) +{ + RAFT_EXPECTS(route != nullptr, "route should equal the number of nodes"); + RAFT_EXPECTS(nodes > 0, "nodes should be strictly positive"); + RAFT_EXPECTS(restarts > 0, "restarts should be strictly positive"); + RAFT_EXPECTS(nstart >= 0 && nstart < nodes, "nstart should be between 0 and nodes - 1"); + RAFT_EXPECTS(k > 0, "k should be strictly positive"); + + cugraph::detail::TSP tsp( + handle, vtx_ptr, x_pos, y_pos, nodes, restarts, beam_search, k, nstart, verbose, route); + return tsp.compute(); +} + +} // namespace cugraph diff --git a/cpp/src/traversal/tsp.hpp b/cpp/src/traversal/tsp.hpp new file mode 100644 index 00000000000..f052462156f --- /dev/null +++ b/cpp/src/traversal/tsp.hpp @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2021, NVIDIA CORPORATION. + * Copyright (c) 2014-2020, Texas State University. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +#include +#include + +#include +#include +#include + +namespace cugraph { +namespace detail { + +struct TSPResults { + float** best_x_pos; + float** best_y_pos; + int** best_route; + int* best_cost; +}; + +class TSP { + public: + TSP(raft::handle_t const& handle, + int const* vtx_ptr, + float const* x_pos, + float const* y_pos, + int nodes, + int restarts, + bool beam_search, + int k, + int nstart, + bool verbose, + int* route); + + void setup(); + void reset_batch(); + void get_initial_solution(int const batch); + float compute(); + void knn(); + ~TSP(){}; + + private: + // Config + raft::handle_t const& handle_; + cudaStream_t stream_; + int max_blocks_; + int max_threads_; + int warp_size_; + int sm_count_; + // how large a grid we want to run, this is fixed + int restart_batch_; + int best_thread_num_; + + // TSP + int const* vtx_ptr_; + int* route_; + float const* x_pos_; + float const* y_pos_; + int nodes_; + int restarts_; + bool beam_search_; + int k_; + int nstart_; + bool verbose_; + + // Scalars + rmm::device_scalar mylock_scalar_; + rmm::device_scalar best_cost_scalar_; + + int* mylock_; + int* best_cost_; + + // Vectors + rmm::device_uvector neighbors_vec_; + rmm::device_uvector work_vec_; + rmm::device_uvector best_x_pos_vec_; + rmm::device_uvector best_y_pos_vec_; + rmm::device_uvector best_route_vec_; + + int64_t* neighbors_; + int* work_; + int* work_route_; + TSPResults results_; +}; + +class VerboseTimer { + public: + VerboseTimer(char const* name, HighResTimer& hr_timer, bool verbose) + : name_(name), hr_timer_(hr_timer), verbose_(verbose) + { + if (verbose_) hr_timer_.start(name_); + } + + ~VerboseTimer() + { + if (verbose_) hr_timer_.stop(); + } + + private: + const char* name_; + HighResTimer& hr_timer_; + bool verbose_; +}; + +} // namespace detail +} // namespace cugraph diff --git a/cpp/src/traversal/tsp_solver.hpp b/cpp/src/traversal/tsp_solver.hpp new file mode 100644 index 00000000000..9d36357046f --- /dev/null +++ b/cpp/src/traversal/tsp_solver.hpp @@ -0,0 +1,388 @@ +/* + * Copyright (c) 2021, NVIDIA CORPORATION. + * Copyright (c) 2014-2020, Texas State University. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +#include "tsp_utils.hpp" + +namespace cugraph { +namespace detail { + +__global__ void random_init(int* work, + float const* posx, + float const* posy, + int const* vtx_ptr, + int const nstart, + int const nodes, + int const batch, + int const restart_batch) +{ + int* buf = &work[blockIdx.x * ((4 * nodes + 3 + 31) / 32 * 32)]; + float* px = (float*)(&buf[nodes]); + float* py = &px[nodes + 1]; + int* path = (int*)(&py[nodes + 1]); + + // Fill values + for (int i = threadIdx.x; i <= nodes; i += blockDim.x) { + px[i] = posx[i]; + py[i] = posy[i]; + path[i] = vtx_ptr[i]; + } + + __syncthreads(); + // serial permutation as starting point + if (threadIdx.x == 0) { + // swap to start at nstart node + raft::swapVals(px[0], px[nstart]); + raft::swapVals(py[0], py[nstart]); + raft::swapVals(path[0], path[nstart]); + + curandState rndstate; + curand_init(blockIdx.x + (restart_batch * batch), 0, 0, &rndstate); + for (int i = 1; i < nodes; i++) { + int j = curand(&rndstate) % (nodes - 1 - i) + i; + if (i == j) continue; + raft::swapVals(px[i], px[j]); + raft::swapVals(py[i], py[j]); + raft::swapVals(path[i], path[j]); + } + // close the loop now, avoid special cases later + px[nodes] = px[0]; + py[nodes] = py[0]; + path[nodes] = path[0]; + } +} + +__global__ void knn_init(int* work, + float const* posx, + float const* posy, + int const* vtx_ptr, + int64_t const* neighbors, + int const nstart, + int const nodes, + int const K, + int const batch, + int const restart_batch) +{ + int* buf = &work[blockIdx.x * ((4 * nodes + 3 + 31) / 32 * 32)]; + float* px = (float*)(&buf[nodes]); + float* py = &px[nodes + 1]; + int* path = (int*)(&py[nodes + 1]); + + for (int i = threadIdx.x; i < nodes; i += blockDim.x) + buf[i] = 0; + + __syncthreads(); + + if (threadIdx.x == 0) { + curandState rndstate; + curand_init(blockIdx.x + (restart_batch * batch), 0, 0, &rndstate); + int progress = 0; + + px[0] = posx[nstart]; + py[0] = posy[nstart]; + path[0] = vtx_ptr[nstart]; + int head = nstart; + int v = 0; + buf[head] = 1; + while (progress < nodes - 1) { // beam search as starting point + for (int i = 1; i <= progress; i++) + buf[i] = 0; + progress = 0; // reset current location in path and visited array + int randjumps = 0; + while (progress < nodes - 1) { + int nj = curand(&rndstate) % K; + int linked = 0; + for (int nh = 0; nh < K; ++nh) { + // offset (idx / K) + 1 filters the points as their own nearest neighbors. + int offset = (K * head + nj) / K + 1; + v = neighbors[K * head + nj + offset]; + if (v < nodes && buf[v] == 0) { + head = v; + progress += 1; + buf[head] = 1; + linked = 1; + break; + } + nj = (nj + 1) % K; + } + if (linked == 0) { + if (randjumps > nodes - 1) + break; // give up on this traversal, we failed to find a next link + randjumps += 1; + int nr = (head + 1) % nodes; // jump to next node + while (buf[nr] == 1) { + nr = (nr + 1) % nodes; + } + head = nr; + progress += 1; + buf[head] = 1; + } + // copy from input into beam-search order, update len + px[progress] = posx[head]; + py[progress] = posy[head]; + path[progress] = vtx_ptr[head]; + } + } + px[nodes] = px[nstart]; + py[nodes] = py[nstart]; + path[nodes] = path[nstart]; + } +} + +__device__ void two_opt_search( + int* buf, float* px, float* py, int* shbuf, int* minchange, int* mini, int* minj, int const nodes) +{ + __shared__ float shmem_x[tilesize]; + __shared__ float shmem_y[tilesize]; + + for (int ii = 0; ii < nodes - 2; ii += blockDim.x) { + int i = ii + threadIdx.x; + float pxi0, pyi0, pxi1, pyi1, pxj1, pyj1; + if (i < nodes - 2) { + minchange[0] -= buf[i]; + pxi0 = px[i]; + pyi0 = py[i]; + pxi1 = px[i + 1]; + pyi1 = py[i + 1]; + pxj1 = px[nodes]; + pyj1 = py[nodes]; + } + for (int jj = nodes - 1; jj >= ii + 2; jj -= tilesize) { + int bound = jj - tilesize + 1; + for (int k = threadIdx.x; k < tilesize; k += blockDim.x) { + if (k + bound >= ii + 2) { + shmem_x[k] = px[k + bound]; + shmem_y[k] = py[k + bound]; + shbuf[k] = buf[k + bound]; + } + } + __syncthreads(); + + int lower = bound; + if (lower < (i + 2)) lower = i + 2; + for (int j = jj; j >= lower; j--) { + int jm = j - bound; + float pxj0 = shmem_x[jm]; + float pyj0 = shmem_y[jm]; + int delta = + shbuf[jm] + + __float2int_rn(sqrtf((pxi0 - pxj0) * (pxi0 - pxj0) + (pyi0 - pyj0) * (pyi0 - pyj0))) + + __float2int_rn(sqrtf((pxi1 - pxj1) * (pxi1 - pxj1) + (pyi1 - pyj1) * (pyi1 - pyj1))); + pxj1 = pxj0; + pyj1 = pyj0; + + if (delta < minchange[0]) { + minchange[0] = delta; + mini[0] = i; + minj[0] = j; + } + } + __syncthreads(); + } + + if (i < nodes - 2) { minchange[0] += buf[i]; } + } +} + +__global__ __launch_bounds__(2048, 2) void search_solution(TSPResults results, + int* mylock, + int const* vtx_ptr, + bool beam_search, + int const K, + int nodes, + float const* posx, + float const* posy, + int* work, + int const nstart) +{ + int* buf = &work[blockIdx.x * ((4 * nodes + 3 + 31) / 32 * 32)]; + float* px = (float*)(&buf[nodes]); + float* py = &px[nodes + 1]; + int* path = (int*)(&py[nodes + 1]); + + __shared__ int shbuf[tilesize]; + __shared__ int best_change[kswaps]; + __shared__ int best_i[kswaps]; + __shared__ int best_j[kswaps]; + + int minchange; + int mini; + int minj; + int kswaps_active = kswaps; + int myswaps = 0; + + // Hill climbing, iteratively improve from the starting guess + do { + if (threadIdx.x == 0) { + for (int k = 0; k < kswaps; k++) { + best_change[k] = 0; + best_i[k] = 0; + best_j[k] = 0; + } + } + __syncthreads(); + for (int i = threadIdx.x; i < nodes; i += blockDim.x) { + buf[i] = -__float2int_rn(euclidean_dist(px, py, i, i + 1)); + } + __syncthreads(); + + // Reset + minchange = 0; + mini = 0; + minj = 0; + + // Find best indices + two_opt_search(buf, px, py, shbuf, &minchange, &mini, &minj, nodes); + __syncthreads(); + + shbuf[threadIdx.x] = minchange; + + int j = blockDim.x; // warp reduction to find best thread results + do { + int k = (j + 1) / 2; + if ((threadIdx.x + k) < j) { + shbuf[threadIdx.x] = min(shbuf[threadIdx.x + k], shbuf[threadIdx.x]); + } + j = k; + __syncthreads(); + } while (j > 1); // thread winner for this k is in shbuf[0] + + if (threadIdx.x == 0) { + best_change[0] = shbuf[0]; // sort best result in shared + } + __syncthreads(); + + if (minchange == shbuf[0]) { // My thread is as good as the winner + shbuf[1] = threadIdx.x; // store thread ID in shbuf[1] + } + __syncthreads(); + + if (threadIdx.x == shbuf[1]) { // move from thread local to shared + best_i[0] = mini; // shared best indices for compatibility checks + best_j[0] = minj; + } + __syncthreads(); + + // look for more compatible swaps + for (int kmin = 1; kmin < kswaps_active; kmin++) { + // disallow swaps that conflict with ones already picked + for (int kchk = kmin - 1; kchk >= 0; --kchk) { + if ((mini < (best_j[kchk] + 1)) && (minj > (best_i[kchk] - 1))) { + minchange = shbuf[threadIdx.x] = 0; + } + __syncthreads(); + } + shbuf[threadIdx.x] = minchange; + + j = blockDim.x; + do { + int k = (j + 1) / 2; + if ((threadIdx.x + k) < j) { + shbuf[threadIdx.x] = min(shbuf[threadIdx.x + k], shbuf[threadIdx.x]); + } + j = k; + __syncthreads(); + } while (j > 1); // thread winner for this k is in shbuf[0] + + if (threadIdx.x == 0) { + best_change[kmin] = shbuf[0]; // store best result in shared + } + __syncthreads(); + + if (minchange == shbuf[0]) { // My thread is as good as the winner + shbuf[1] = threadIdx.x; // store thread ID in shbuf[1] + __threadfence_block(); + } + __syncthreads(); + + if (threadIdx.x == shbuf[1]) { // move from thread local to shared + best_i[kmin] = mini; // store swap targets + best_j[kmin] = minj; + __threadfence_block(); + } + __syncthreads(); + // look for the best compatible move + } // end loop over kmin + minchange = best_change[0]; + myswaps += 1; + for (int kmin = 0; kmin < kswaps_active; kmin++) { + int sum = best_i[kmin] + best_j[kmin] + 1; // = mini + minj +1 + // this is a reversal of all nodes included in the range [ i+1, j ] + for (int i = threadIdx.x; (i + i) < sum; i += blockDim.x) { + if (best_i[kmin] < i) { + int j = sum - i; + raft::swapVals(px[i], px[j]); + raft::swapVals(py[i], py[j]); + raft::swapVals(path[i], path[j]); + } + } + __syncthreads(); + } + } while (minchange < 0 && myswaps < 2 * nodes); +} + +__global__ void get_optimal_tour(TSPResults results, int* mylock, int* work, int const nodes) +{ + extern __shared__ int accumulator[]; + int climber_id = blockIdx.x; + int* buf = &work[climber_id * ((4 * nodes + 3 + 31) / 32 * 32)]; + float* px = (float*)(&buf[nodes]); + float* py = &px[nodes + 1]; + int* path = (int*)(&py[nodes + 1]); + + // Now find actual length of the last tour, result of the climb + int term = 0; + for (int i = threadIdx.x; i < nodes; i += blockDim.x) { + term += __float2int_rn(euclidean_dist(px, py, i, i + 1)); + } + accumulator[threadIdx.x] = term; + __syncthreads(); + + int j = blockDim.x; // block level reduction + do { + int k = (j + 1) / 2; + if ((threadIdx.x + k) < j) { accumulator[threadIdx.x] += accumulator[threadIdx.x + k]; } + j = k; // divide active warp size in half + __syncthreads(); + } while (j > 1); + term = accumulator[0]; + + if (threadIdx.x == 0) { + atomicMin(results.best_cost, term); + while (atomicExch(mylock, 1) != 0) + ; // acquire + if (results.best_cost[0] == term) { + results.best_x_pos[0] = px; + results.best_y_pos[0] = py; + results.best_route[0] = path; + } + *mylock = 0; // release + __threadfence(); + } +} + +} // namespace detail +} // namespace cugraph diff --git a/cpp/src/traversal/tsp_utils.hpp b/cpp/src/traversal/tsp_utils.hpp new file mode 100644 index 00000000000..eab5c09eb2f --- /dev/null +++ b/cpp/src/traversal/tsp_utils.hpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2021, NVIDIA CORPORATION. + * Copyright (c) 2014-2020, Texas State University. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#define tilesize 128 +#define kswaps 4 + +#include +#include +#include + +namespace cugraph { +namespace detail { + +constexpr float euclidean_dist(float* px, float* py, int a, int b) +{ + return sqrtf((px[a] - px[b]) * (px[a] - px[b]) + (py[a] - py[b]) * (py[a] - py[b])); +} + +// Get maximum number of threads we can run on based on number of nodes, +// shared memory usage, max threads per block and SM, max blocks for SM and registers per SM. +int best_thread_count(int nodes, int max_threads, int sm_count, int warp_size) +{ + int smem, blocks, thr, perf; + int const max_threads_sm = 2048; + int max = nodes - 2; + int best = 0; + int bthr = 4; + + if (max > max_threads) max = max_threads; + + for (int threads = 1; threads <= max; ++threads) { + smem = sizeof(int) * threads + 2 * sizeof(float) * tilesize + sizeof(int) * tilesize; + blocks = (16384 * 2) / smem; + if (blocks > sm_count) blocks = sm_count; + thr = (threads + warp_size - 1) / warp_size * warp_size; + while (blocks * thr > max_threads_sm) + blocks--; + perf = threads * blocks; + if (perf > best) { + best = perf; + bthr = threads; + } + } + + return bthr; +} +} // namespace detail +} // namespace cugraph diff --git a/cpp/src/traversal/two_hop_neighbors.cu b/cpp/src/traversal/two_hop_neighbors.cu index fb984dae0ad..e1fce911130 100644 --- a/cpp/src/traversal/two_hop_neighbors.cu +++ b/cpp/src/traversal/two_hop_neighbors.cu @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. @@ -20,9 +20,9 @@ * ---------------------------------------------------------------------------**/ #include -#include -#include -#include +#include +#include +#include #include "two_hop_neighbors.cuh" #include @@ -32,12 +32,13 @@ namespace cugraph { template -std::unique_ptr> get_two_hop_neighbors(GraphCSRView const &graph) +std::unique_ptr> get_two_hop_neighbors( + legacy::GraphCSRView const& graph) { cudaStream_t stream{nullptr}; rmm::device_vector exsum_degree(graph.number_of_edges + 1); - ET *d_exsum_degree = exsum_degree.data().get(); + ET* d_exsum_degree = exsum_degree.data().get(); // Find the degree of the out vertex of each edge degree_iterator deg_it(graph.offsets); @@ -62,14 +63,14 @@ std::unique_ptr> get_two_hop_neighbors(GraphCSRView first_pair(output_size); rmm::device_vector second_pair(output_size); - VT *d_first_pair = first_pair.data().get(); - VT *d_second_pair = second_pair.data().get(); + VT* d_first_pair = first_pair.data().get(); + VT* d_second_pair = second_pair.data().get(); // Figure out number of blocks and allocate memory for block bucket offsets ET num_blocks = (output_size + TWO_HOP_BLOCK_SIZE - 1) / TWO_HOP_BLOCK_SIZE; rmm::device_vector block_bucket_offsets(num_blocks + 1); - ET *d_block_bucket_offsets = block_bucket_offsets.data().get(); + ET* d_block_bucket_offsets = block_bucket_offsets.data().get(); // Compute the block bucket offsets dim3 grid, block; @@ -108,7 +109,8 @@ std::unique_ptr> get_two_hop_neighbors(GraphCSRView>(graph.number_of_vertices, outputSize, false); + auto result = + std::make_unique>(graph.number_of_vertices, outputSize, false); cudaMemcpy(result->src_indices(), d_first_pair, sizeof(VT) * outputSize, cudaMemcpyDefault); cudaMemcpy(result->dst_indices(), d_second_pair, sizeof(VT) * outputSize, cudaMemcpyDefault); @@ -116,10 +118,10 @@ std::unique_ptr> get_two_hop_neighbors(GraphCSRView> get_two_hop_neighbors( - GraphCSRView const &); +template std::unique_ptr> get_two_hop_neighbors( + legacy::GraphCSRView const&); -template std::unique_ptr> get_two_hop_neighbors( - GraphCSRView const &); +template std::unique_ptr> get_two_hop_neighbors( + legacy::GraphCSRView const&); } // namespace cugraph diff --git a/cpp/src/traversal/two_hop_neighbors.cuh b/cpp/src/traversal/two_hop_neighbors.cuh index 87d3b36b861..e830fb4a95f 100644 --- a/cpp/src/traversal/two_hop_neighbors.cuh +++ b/cpp/src/traversal/two_hop_neighbors.cuh @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. @@ -21,13 +21,13 @@ #include -#define MAXBLOCKS 65535 +#define MAXBLOCKS 65535 #define TWO_HOP_BLOCK_SIZE 512 template struct degree_iterator { - edge_t const *offsets; - degree_iterator(edge_t const *_offsets) : offsets(_offsets) {} + edge_t const* offsets; + degree_iterator(edge_t const* _offsets) : offsets(_offsets) {} __host__ __device__ edge_t operator[](edge_t place) { @@ -53,7 +53,7 @@ struct self_loop_flagger { }; template -__device__ edge_t binsearch_maxle(const edge_t *vec, const edge_t val, edge_t low, edge_t high) +__device__ edge_t binsearch_maxle(const edge_t* vec, const edge_t val, edge_t low, edge_t high) { while (true) { if (low == high) return low; // we know it exists @@ -69,8 +69,8 @@ __device__ edge_t binsearch_maxle(const edge_t *vec, const edge_t val, edge_t lo } template -__global__ void compute_bucket_offsets_kernel(const edge_t *frontier_degrees_exclusive_sum, - edge_t *bucket_offsets, +__global__ void compute_bucket_offsets_kernel(const edge_t* frontier_degrees_exclusive_sum, + edge_t* bucket_offsets, const edge_t frontier_size, edge_t total_degree) { @@ -86,15 +86,15 @@ __global__ void compute_bucket_offsets_kernel(const edge_t *frontier_degrees_exc } template -__global__ void scatter_expand_kernel(const edge_t *exsum_degree, - const vertex_t *indices, - const edge_t *offsets, - const edge_t *bucket_offsets, +__global__ void scatter_expand_kernel(const edge_t* exsum_degree, + const vertex_t* indices, + const edge_t* offsets, + const edge_t* bucket_offsets, vertex_t num_verts, edge_t max_item, edge_t max_block, - vertex_t *output_first, - vertex_t *output_second) + vertex_t* output_first, + vertex_t* output_second) { __shared__ edge_t blockRange[2]; for (edge_t bid = blockIdx.x; bid < max_block; bid += gridDim.x) { diff --git a/cpp/src/tree/mst.cu b/cpp/src/tree/mst.cu index cc3bdc64a2d..e6caa629cd1 100644 --- a/cpp/src/tree/mst.cu +++ b/cpp/src/tree/mst.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -20,7 +20,7 @@ * @file mst.cu * ---------------------------------------------------------------------------**/ -#include +#include #include #include @@ -28,8 +28,8 @@ #include #include -#include -#include +#include +#include #include @@ -38,10 +38,10 @@ namespace cugraph { namespace detail { template -std::unique_ptr> mst_impl( - raft::handle_t const &handle, - GraphCSRView const &graph, - rmm::mr::device_memory_resource *mr) +std::unique_ptr> mst_impl( + raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + rmm::mr::device_memory_resource* mr) { auto stream = handle.get_stream(); @@ -55,33 +55,33 @@ std::unique_ptr> mst_impl( colors.data(), stream); - GraphCOOContents coo_contents{ + legacy::GraphCOOContents coo_contents{ graph.number_of_vertices, mst_edges.n_edges, std::make_unique(mst_edges.src.release()), std::make_unique(mst_edges.dst.release()), std::make_unique(mst_edges.weights.release())}; - return std::make_unique>(std::move(coo_contents)); + return std::make_unique>(std::move(coo_contents)); } } // namespace detail template -std::unique_ptr> minimum_spanning_tree( - raft::handle_t const &handle, - GraphCSRView const &graph, - rmm::mr::device_memory_resource *mr) +std::unique_ptr> minimum_spanning_tree( + raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + rmm::mr::device_memory_resource* mr) { return detail::mst_impl(handle, graph, mr); } -template std::unique_ptr> minimum_spanning_tree( - raft::handle_t const &handle, - GraphCSRView const &graph, - rmm::mr::device_memory_resource *mr); -template std::unique_ptr> minimum_spanning_tree( - raft::handle_t const &handle, - GraphCSRView const &graph, - rmm::mr::device_memory_resource *mr); +template std::unique_ptr> minimum_spanning_tree( + raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + rmm::mr::device_memory_resource* mr); +template std::unique_ptr> +minimum_spanning_tree(raft::handle_t const& handle, + legacy::GraphCSRView const& graph, + rmm::mr::device_memory_resource* mr); } // namespace cugraph diff --git a/cpp/src/utilities/cython.cu b/cpp/src/utilities/cython.cu index 6c8ef98e2e2..909e3d5b31f 100644 --- a/cpp/src/utilities/cython.cu +++ b/cpp/src/utilities/cython.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -14,24 +14,112 @@ * limitations under the License. */ -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + #include -#include -#include #include +#include + #include +#include +#include #include +#include +#include +#include +#include + +#include +#include namespace cugraph { namespace cython { namespace detail { -// FIXME: Add description of this function +// workaround for CUDA extended lambda restrictions +template +struct compute_local_partition_id_t { + vertex_t const* lasts{nullptr}; + size_t num_local_partitions{0}; + + __device__ size_t operator()(vertex_t v) + { + for (size_t i = 0; i < num_local_partitions; ++i) { + if (v < lasts[i]) { return i; } + } + return num_local_partitions; + } +}; + +// FIXME: this is unnecessary if edge_counts_ in the major_minor_weights_t object returned by +// call_shuffle() is passed back, better be fixed. this code assumes that the entire set of edges +// for each partition are consecutively stored. +template +std::vector compute_edge_counts(raft::handle_t const& handle, + graph_container_t const& graph_container) +{ + auto num_local_partitions = static_cast(graph_container.col_comm_size); + + std::vector partition_offsets_vector( + reinterpret_cast(graph_container.vertex_partition_offsets), + reinterpret_cast(graph_container.vertex_partition_offsets) + + (graph_container.row_comm_size * graph_container.col_comm_size) + 1); + + std::vector h_lasts(num_local_partitions); + for (size_t i = 0; i < h_lasts.size(); ++i) { + h_lasts[i] = partition_offsets_vector[graph_container.row_comm_size * (i + 1)]; + } + rmm::device_uvector d_lasts(h_lasts.size(), handle.get_stream()); + raft::update_device(d_lasts.data(), h_lasts.data(), h_lasts.size(), handle.get_stream()); + auto major_vertices = transposed + ? reinterpret_cast(graph_container.dst_vertices) + : reinterpret_cast(graph_container.src_vertices); + auto key_first = thrust::make_transform_iterator( + major_vertices, compute_local_partition_id_t{d_lasts.data(), num_local_partitions}); + rmm::device_uvector d_local_partition_ids(num_local_partitions, handle.get_stream()); + rmm::device_uvector d_edge_counts(d_local_partition_ids.size(), handle.get_stream()); + auto it = thrust::reduce_by_key(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + key_first, + key_first + graph_container.num_local_edges, + thrust::make_constant_iterator(edge_t{1}), + d_local_partition_ids.begin(), + d_edge_counts.begin()); + if (static_cast(thrust::distance(d_local_partition_ids.begin(), thrust::get<0>(it))) < + num_local_partitions) { + rmm::device_uvector d_counts(num_local_partitions, handle.get_stream()); + thrust::fill(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + d_counts.begin(), + d_counts.end(), + edge_t{0}); + thrust::scatter(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + d_edge_counts.begin(), + thrust::get<1>(it), + d_local_partition_ids.begin(), + d_counts.begin()); + d_edge_counts = std::move(d_counts); + } + std::vector h_edge_counts(num_local_partitions, 0); + raft::update_host( + h_edge_counts.data(), d_edge_counts.data(), d_edge_counts.size(), handle.get_stream()); + handle.get_stream_view().synchronize(); + + return h_edge_counts; +} + template > create_graph(raft::handle_t const& handle, graph_container_t const& graph_container) { - std::vector> edgelist( - {{reinterpret_cast(graph_container.src_vertices), - reinterpret_cast(graph_container.dst_vertices), - reinterpret_cast(graph_container.weights), - static_cast(graph_container.num_partition_edges)}}); + auto num_local_partitions = static_cast(graph_container.col_comm_size); std::vector partition_offsets_vector( reinterpret_cast(graph_container.vertex_partition_offsets), reinterpret_cast(graph_container.vertex_partition_offsets) + (graph_container.row_comm_size * graph_container.col_comm_size) + 1); + auto edge_counts = compute_edge_counts(handle, graph_container); + + std::vector displacements(edge_counts.size(), 0); + std::partial_sum(edge_counts.begin(), edge_counts.end() - 1, displacements.begin() + 1); + + std::vector> edgelists( + num_local_partitions); + for (size_t i = 0; i < edgelists.size(); ++i) { + edgelists[i] = cugraph::experimental::edgelist_t{ + reinterpret_cast(graph_container.src_vertices) + displacements[i], + reinterpret_cast(graph_container.dst_vertices) + displacements[i], + graph_container.is_weighted + ? std::optional( + {static_cast(graph_container.weights) + displacements[i]}) + : std::nullopt, + edge_counts[i]}; + } + experimental::partition_t partition(partition_offsets_vector, - graph_container.hypergraph_partitioned, graph_container.row_comm_size, graph_container.col_comm_size, graph_container.row_comm_rank, @@ -61,14 +162,17 @@ create_graph(raft::handle_t const& handle, graph_container_t const& graph_contai return std::make_unique>( handle, - edgelist, + edgelists, partition, static_cast(graph_container.num_global_vertices), static_cast(graph_container.num_global_edges), graph_container.graph_props, - // FIXME: This currently fails if sorted_by_degree is true... - // graph_container.sorted_by_degree, - false, + graph_container.segment_offsets != nullptr + ? std::make_optional>( + static_cast(graph_container.segment_offsets), + static_cast(graph_container.segment_offsets) + + graph_container.num_segments + 1) + : std::nullopt, graph_container.do_expensive_check); } @@ -84,15 +188,21 @@ create_graph(raft::handle_t const& handle, graph_container_t const& graph_contai experimental::edgelist_t edgelist{ reinterpret_cast(graph_container.src_vertices), reinterpret_cast(graph_container.dst_vertices), - reinterpret_cast(graph_container.weights), - static_cast(graph_container.num_partition_edges)}; - + graph_container.is_weighted + ? std::optional{reinterpret_cast(graph_container.weights)} + : std::nullopt, + static_cast(graph_container.num_local_edges)}; return std::make_unique>( handle, edgelist, static_cast(graph_container.num_global_vertices), graph_container.graph_props, - graph_container.sorted_by_degree, + graph_container.segment_offsets != nullptr + ? std::make_optional>( + static_cast(graph_container.segment_offsets), + static_cast(graph_container.segment_offsets) + + graph_container.num_segments + 1) + : std::nullopt, graph_container.do_expensive_check); } @@ -107,13 +217,16 @@ void populate_graph_container(graph_container_t& graph_container, void* dst_vertices, void* weights, void* vertex_partition_offsets, + void* segment_offsets, + size_t num_segments, numberTypeEnum vertexType, numberTypeEnum edgeType, numberTypeEnum weightType, - size_t num_partition_edges, + size_t num_local_edges, size_t num_global_vertices, size_t num_global_edges, - bool sorted_by_degree, + bool is_weighted, + bool is_symmetric, bool transposed, bool multi_gpu) { @@ -121,20 +234,28 @@ void populate_graph_container(graph_container_t& graph_container, "populate_graph_container() can only be called on an empty container."); bool do_expensive_check{true}; - bool hypergraph_partitioned{false}; - auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); - auto const row_comm_rank = row_comm.get_rank(); - auto const row_comm_size = row_comm.get_size(); // pcols - auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); - auto const col_comm_rank = col_comm.get_rank(); - auto const col_comm_size = col_comm.get_size(); // prows + if (multi_gpu) { + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_rank = row_comm.get_rank(); + auto const row_comm_size = row_comm.get_size(); // pcols + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_rank = col_comm.get_rank(); + auto const col_comm_size = col_comm.get_size(); // prows + graph_container.row_comm_size = row_comm_size; + graph_container.col_comm_size = col_comm_size; + graph_container.row_comm_rank = row_comm_rank; + graph_container.col_comm_rank = col_comm_rank; + } - graph_container.vertex_partition_offsets = vertex_partition_offsets; graph_container.src_vertices = src_vertices; graph_container.dst_vertices = dst_vertices; graph_container.weights = weights; - graph_container.num_partition_edges = num_partition_edges; + graph_container.is_weighted = is_weighted; + graph_container.vertex_partition_offsets = vertex_partition_offsets; + graph_container.segment_offsets = segment_offsets; + graph_container.num_segments = num_segments; + graph_container.num_local_edges = num_local_edges; graph_container.num_global_vertices = num_global_vertices; graph_container.num_global_edges = num_global_edges; graph_container.vertexType = vertexType; @@ -142,15 +263,10 @@ void populate_graph_container(graph_container_t& graph_container, graph_container.weightType = weightType; graph_container.transposed = transposed; graph_container.is_multi_gpu = multi_gpu; - graph_container.hypergraph_partitioned = hypergraph_partitioned; - graph_container.row_comm_size = row_comm_size; - graph_container.col_comm_size = col_comm_size; - graph_container.row_comm_rank = row_comm_rank; - graph_container.col_comm_rank = col_comm_rank; - graph_container.sorted_by_degree = sorted_by_degree; graph_container.do_expensive_check = do_expensive_check; - experimental::graph_properties_t graph_props{.is_symmetric = false, .is_multigraph = false}; + experimental::graph_properties_t graph_props{.is_symmetric = is_symmetric, + .is_multigraph = false}; graph_container.graph_props = graph_props; graph_container.graph_type = graphTypeEnum::graph_t; @@ -172,7 +288,7 @@ void populate_graph_container_legacy(graph_container_t& graph_container, int* local_offsets) { CUGRAPH_EXPECTS(graph_container.graph_type == graphTypeEnum::null, - "populate_graph_container() can only be called on an empty container."); + "populate_graph_container_legacy() can only be called on an empty container."); // FIXME: This is soon-to-be legacy code left in place until the new graph_t // class is supported everywhere else. Remove everything down to the comment @@ -182,11 +298,11 @@ void populate_graph_container_legacy(graph_container_t& graph_container, switch (legacyType) { case graphTypeEnum::LegacyCSR: { graph_container.graph_ptr_union.GraphCSRViewFloatPtr = - std::make_unique>(reinterpret_cast(offsets), - reinterpret_cast(indices), - reinterpret_cast(weights), - num_global_vertices, - num_global_edges); + std::make_unique>(reinterpret_cast(offsets), + reinterpret_cast(indices), + reinterpret_cast(weights), + num_global_vertices, + num_global_edges); graph_container.graph_type = graphTypeEnum::GraphCSRViewFloat; (graph_container.graph_ptr_union.GraphCSRViewFloatPtr) ->set_local_data(local_vertices, local_edges, local_offsets); @@ -195,11 +311,11 @@ void populate_graph_container_legacy(graph_container_t& graph_container, } break; case graphTypeEnum::LegacyCSC: { graph_container.graph_ptr_union.GraphCSCViewFloatPtr = - std::make_unique>(reinterpret_cast(offsets), - reinterpret_cast(indices), - reinterpret_cast(weights), - num_global_vertices, - num_global_edges); + std::make_unique>(reinterpret_cast(offsets), + reinterpret_cast(indices), + reinterpret_cast(weights), + num_global_vertices, + num_global_edges); graph_container.graph_type = graphTypeEnum::GraphCSCViewFloat; (graph_container.graph_ptr_union.GraphCSCViewFloatPtr) ->set_local_data(local_vertices, local_edges, local_offsets); @@ -208,11 +324,11 @@ void populate_graph_container_legacy(graph_container_t& graph_container, } break; case graphTypeEnum::LegacyCOO: { graph_container.graph_ptr_union.GraphCOOViewFloatPtr = - std::make_unique>(reinterpret_cast(offsets), - reinterpret_cast(indices), - reinterpret_cast(weights), - num_global_vertices, - num_global_edges); + std::make_unique>(reinterpret_cast(offsets), + reinterpret_cast(indices), + reinterpret_cast(weights), + num_global_vertices, + num_global_edges); graph_container.graph_type = graphTypeEnum::GraphCOOViewFloat; (graph_container.graph_ptr_union.GraphCOOViewFloatPtr) ->set_local_data(local_vertices, local_edges, local_offsets); @@ -226,11 +342,12 @@ void populate_graph_container_legacy(graph_container_t& graph_container, switch (legacyType) { case graphTypeEnum::LegacyCSR: { graph_container.graph_ptr_union.GraphCSRViewDoublePtr = - std::make_unique>(reinterpret_cast(offsets), - reinterpret_cast(indices), - reinterpret_cast(weights), - num_global_vertices, - num_global_edges); + std::make_unique>( + reinterpret_cast(offsets), + reinterpret_cast(indices), + reinterpret_cast(weights), + num_global_vertices, + num_global_edges); graph_container.graph_type = graphTypeEnum::GraphCSRViewDouble; (graph_container.graph_ptr_union.GraphCSRViewDoublePtr) ->set_local_data(local_vertices, local_edges, local_offsets); @@ -239,11 +356,12 @@ void populate_graph_container_legacy(graph_container_t& graph_container, } break; case graphTypeEnum::LegacyCSC: { graph_container.graph_ptr_union.GraphCSCViewDoublePtr = - std::make_unique>(reinterpret_cast(offsets), - reinterpret_cast(indices), - reinterpret_cast(weights), - num_global_vertices, - num_global_edges); + std::make_unique>( + reinterpret_cast(offsets), + reinterpret_cast(indices), + reinterpret_cast(weights), + num_global_vertices, + num_global_edges); graph_container.graph_type = graphTypeEnum::GraphCSCViewDouble; (graph_container.graph_ptr_union.GraphCSCViewDoublePtr) ->set_local_data(local_vertices, local_edges, local_offsets); @@ -252,11 +370,12 @@ void populate_graph_container_legacy(graph_container_t& graph_container, } break; case graphTypeEnum::LegacyCOO: { graph_container.graph_ptr_union.GraphCOOViewDoublePtr = - std::make_unique>(reinterpret_cast(offsets), - reinterpret_cast(indices), - reinterpret_cast(weights), - num_global_vertices, - num_global_edges); + std::make_unique>( + reinterpret_cast(offsets), + reinterpret_cast(indices), + reinterpret_cast(weights), + num_global_vertices, + num_global_edges); graph_container.graph_type = graphTypeEnum::GraphCOOViewDouble; (graph_container.graph_ptr_union.GraphCOOViewDoublePtr) ->set_local_data(local_vertices, local_edges, local_offsets); @@ -463,65 +582,96 @@ void call_pagerank(raft::handle_t const& handle, int64_t max_iter, bool has_guess) { - if (graph_container.graph_type == graphTypeEnum::GraphCSCViewFloat) { - pagerank(handle, - *(graph_container.graph_ptr_union.GraphCSCViewFloatPtr), - reinterpret_cast(p_pagerank), - static_cast(personalization_subset_size), - reinterpret_cast(personalization_subset), - reinterpret_cast(personalization_values), - alpha, - tolerance, - max_iter, - has_guess); - graph_container.graph_ptr_union.GraphCSCViewFloatPtr->get_vertex_identifiers( - reinterpret_cast(identifiers)); - } else if (graph_container.graph_type == graphTypeEnum::GraphCSCViewDouble) { - pagerank(handle, - *(graph_container.graph_ptr_union.GraphCSCViewDoublePtr), - reinterpret_cast(p_pagerank), - static_cast(personalization_subset_size), - reinterpret_cast(personalization_subset), - reinterpret_cast(personalization_values), - alpha, - tolerance, - max_iter, - has_guess); - graph_container.graph_ptr_union.GraphCSCViewDoublePtr->get_vertex_identifiers( - reinterpret_cast(identifiers)); - } else if (graph_container.graph_type == graphTypeEnum::graph_t) { + if (graph_container.is_multi_gpu) { + auto& comm = handle.get_comms(); + auto aggregate_personalization_subset_size = cugraph::experimental::host_scalar_allreduce( + comm, personalization_subset_size, handle.get_stream()); + if (graph_container.edgeType == numberTypeEnum::int32Type) { auto graph = detail::create_graph(handle, graph_container); - cugraph::experimental::pagerank(handle, - graph->view(), - static_cast(nullptr), - reinterpret_cast(personalization_subset), - reinterpret_cast(personalization_values), - static_cast(personalization_subset_size), - reinterpret_cast(p_pagerank), - static_cast(alpha), - static_cast(tolerance), - max_iter, - has_guess, - false); + cugraph::experimental::pagerank( + handle, + graph->view(), + std::nullopt, + aggregate_personalization_subset_size > 0 + ? std::optional{reinterpret_cast(personalization_subset)} + : std::nullopt, + aggregate_personalization_subset_size > 0 + ? std::optional{personalization_values} + : std::nullopt, + aggregate_personalization_subset_size > 0 + ? std::optional{static_cast(personalization_subset_size)} + : std::nullopt, + reinterpret_cast(p_pagerank), + static_cast(alpha), + static_cast(tolerance), + max_iter, + has_guess, + true); } else if (graph_container.edgeType == numberTypeEnum::int64Type) { auto graph = detail::create_graph(handle, graph_container); - cugraph::experimental::pagerank(handle, - graph->view(), - static_cast(nullptr), - reinterpret_cast(personalization_subset), - reinterpret_cast(personalization_values), - static_cast(personalization_subset_size), - reinterpret_cast(p_pagerank), - static_cast(alpha), - static_cast(tolerance), - max_iter, - has_guess, - false); - } else { - CUGRAPH_FAIL("vertexType/edgeType combination unsupported"); + cugraph::experimental::pagerank( + handle, + graph->view(), + std::nullopt, + aggregate_personalization_subset_size > 0 + ? std::optional{personalization_subset} + : std::nullopt, + aggregate_personalization_subset_size > 0 + ? std::optional{personalization_values} + : std::nullopt, + aggregate_personalization_subset_size > 0 + ? std::optional{personalization_subset_size} + : std::nullopt, + reinterpret_cast(p_pagerank), + static_cast(alpha), + static_cast(tolerance), + max_iter, + has_guess, + true); + } + } else { + if (graph_container.edgeType == numberTypeEnum::int32Type) { + auto graph = + detail::create_graph(handle, graph_container); + cugraph::experimental::pagerank( + handle, + graph->view(), + std::nullopt, + personalization_subset_size > 0 + ? std::optional{reinterpret_cast(personalization_subset)} + : std::nullopt, + personalization_subset_size > 0 ? std::optional{personalization_values} + : std::nullopt, + personalization_subset_size > 0 ? std::optional{personalization_subset_size} + : std::nullopt, + reinterpret_cast(p_pagerank), + static_cast(alpha), + static_cast(tolerance), + max_iter, + has_guess, + true); + } else if (graph_container.edgeType == numberTypeEnum::int64Type) { + auto graph = + detail::create_graph(handle, graph_container); + cugraph::experimental::pagerank( + handle, + graph->view(), + std::nullopt, + personalization_subset_size > 0 ? std::optional{personalization_subset} + : std::nullopt, + personalization_subset_size > 0 ? std::optional{personalization_values} + : std::nullopt, + personalization_subset_size > 0 ? std::optional{personalization_subset_size} + : std::nullopt, + reinterpret_cast(p_pagerank), + static_cast(alpha), + static_cast(tolerance), + max_iter, + has_guess, + true); } } } @@ -591,31 +741,11 @@ void call_bfs(raft::handle_t const& handle, vertex_t* identifiers, vertex_t* distances, vertex_t* predecessors, - double* sp_counters, + vertex_t depth_limit, const vertex_t start_vertex, - bool directed) + bool direction_optimizing) { - if (graph_container.graph_type == graphTypeEnum::GraphCSRViewFloat) { - graph_container.graph_ptr_union.GraphCSRViewFloatPtr->get_vertex_identifiers( - reinterpret_cast(identifiers)); - bfs(handle, - *(graph_container.graph_ptr_union.GraphCSRViewFloatPtr), - reinterpret_cast(distances), - reinterpret_cast(predecessors), - sp_counters, - static_cast(start_vertex), - directed); - } else if (graph_container.graph_type == graphTypeEnum::GraphCSRViewDouble) { - graph_container.graph_ptr_union.GraphCSRViewDoublePtr->get_vertex_identifiers( - reinterpret_cast(identifiers)); - bfs(handle, - *(graph_container.graph_ptr_union.GraphCSRViewDoublePtr), - reinterpret_cast(distances), - reinterpret_cast(predecessors), - sp_counters, - static_cast(start_vertex), - directed); - } else if (graph_container.graph_type == graphTypeEnum::graph_t) { + if (graph_container.is_multi_gpu) { if (graph_container.edgeType == numberTypeEnum::int32Type) { auto graph = detail::create_graph(handle, graph_container); @@ -623,7 +753,9 @@ void call_bfs(raft::handle_t const& handle, graph->view(), reinterpret_cast(distances), reinterpret_cast(predecessors), - static_cast(start_vertex)); + static_cast(start_vertex), + direction_optimizing, + static_cast(depth_limit)); } else if (graph_container.edgeType == numberTypeEnum::int64Type) { auto graph = detail::create_graph(handle, graph_container); @@ -631,12 +763,253 @@ void call_bfs(raft::handle_t const& handle, graph->view(), reinterpret_cast(distances), reinterpret_cast(predecessors), - static_cast(start_vertex)); - } else { - CUGRAPH_FAIL("vertexType/edgeType combination unsupported"); + static_cast(start_vertex), + direction_optimizing, + static_cast(depth_limit)); } + } else { + if (graph_container.edgeType == numberTypeEnum::int32Type) { + auto graph = + detail::create_graph(handle, graph_container); + cugraph::experimental::bfs(handle, + graph->view(), + reinterpret_cast(distances), + reinterpret_cast(predecessors), + static_cast(start_vertex), + direction_optimizing, + static_cast(depth_limit)); + } else if (graph_container.edgeType == numberTypeEnum::int64Type) { + auto graph = + detail::create_graph(handle, graph_container); + cugraph::experimental::bfs(handle, + graph->view(), + reinterpret_cast(distances), + reinterpret_cast(predecessors), + static_cast(start_vertex), + direction_optimizing, + static_cast(depth_limit)); + } + } +} + +// Wrapper for calling extract_egonet through a graph container +// FIXME : this should not be a legacy COO and it is not clear how to handle C++ api return type as +// is.graph_container Need to figure out how to return edge lists +template +std::unique_ptr call_egonet(raft::handle_t const& handle, + graph_container_t const& graph_container, + vertex_t* source_vertex, + vertex_t n_subgraphs, + vertex_t radius) +{ + if (graph_container.edgeType == numberTypeEnum::int32Type) { + auto graph = + detail::create_graph(handle, graph_container); + auto g = cugraph::experimental::extract_ego(handle, + graph->view(), + reinterpret_cast(source_vertex), + static_cast(n_subgraphs), + static_cast(radius)); + cy_multi_edgelists_t coo_contents{ + 0, // not used + std::get<0>(g).size(), + static_cast(n_subgraphs), + std::make_unique(std::get<0>(g).release()), + std::make_unique(std::get<1>(g).release()), + std::make_unique( + std::get<2>(g) ? (*std::get<2>(g)).release() + : rmm::device_buffer(size_t{0}, handle.get_stream_view())), + std::make_unique(std::get<3>(g).release())}; + return std::make_unique(std::move(coo_contents)); + } else if (graph_container.edgeType == numberTypeEnum::int64Type) { + auto graph = + detail::create_graph(handle, graph_container); + auto g = cugraph::experimental::extract_ego(handle, + graph->view(), + reinterpret_cast(source_vertex), + static_cast(n_subgraphs), + static_cast(radius)); + cy_multi_edgelists_t coo_contents{ + 0, // not used + std::get<0>(g).size(), + static_cast(n_subgraphs), + std::make_unique(std::get<0>(g).release()), + std::make_unique(std::get<1>(g).release()), + std::make_unique( + std::get<2>(g) ? (*std::get<2>(g)).release() + : rmm::device_buffer(size_t{0}, handle.get_stream_view())), + std::make_unique(std::get<3>(g).release())}; + return std::make_unique(std::move(coo_contents)); + } else { + CUGRAPH_FAIL("vertexType/edgeType combination unsupported"); } } +// Wrapper for graph generate_rmat_edgelist() +// to expose the API to cython +// enum class generator_distribution_t { POWER_LAW = 0, UNIFORM }; +template +std::unique_ptr call_generate_rmat_edgelist(raft::handle_t const& handle, + size_t scale, + size_t num_edges, + double a, + double b, + double c, + uint64_t seed, + bool clip_and_flip, + bool scramble_vertex_ids) +{ + auto src_dst_tuple = cugraph::generate_rmat_edgelist( + handle, scale, num_edges, a, b, c, seed, clip_and_flip); + + if (scramble_vertex_ids) { + cugraph::scramble_vertex_ids( + handle, std::get<0>(src_dst_tuple), std::get<1>(src_dst_tuple), vertex_t{0}, seed); + } + + graph_generator_t gg_vals{ + std::make_unique(std::get<0>(src_dst_tuple).release()), + std::make_unique(std::get<1>(src_dst_tuple).release())}; + + return std::make_unique(std::move(gg_vals)); +} + +template +std::vector, std::unique_ptr>> +call_generate_rmat_edgelists(raft::handle_t const& handle, + size_t n_edgelists, + size_t min_scale, + size_t max_scale, + size_t edge_factor, + cugraph::generator_distribution_t size_distribution, + cugraph::generator_distribution_t edge_distribution, + uint64_t seed, + bool clip_and_flip, + bool scramble_vertex_ids) +{ + auto src_dst_vec_tuple = cugraph::generate_rmat_edgelists(handle, + n_edgelists, + min_scale, + max_scale, + edge_factor, + size_distribution, + edge_distribution, + seed, + clip_and_flip); + + if (scramble_vertex_ids) { + std::for_each( + src_dst_vec_tuple.begin(), src_dst_vec_tuple.end(), [&handle, seed](auto& src_dst_tuple) { + cugraph::scramble_vertex_ids( + handle, std::get<0>(src_dst_tuple), std::get<1>(src_dst_tuple), vertex_t{0}, seed); + }); + } + + std::vector, std::unique_ptr>> + gg_vec; + + std::transform( + src_dst_vec_tuple.begin(), + src_dst_vec_tuple.end(), + std::back_inserter(gg_vec), + [](auto& tpl_dev_uvec) { + return std::make_pair( + std::move(std::make_unique(std::get<0>(tpl_dev_uvec).release())), + std::move(std::make_unique(std::get<1>(tpl_dev_uvec).release()))); + }); + + return gg_vec; +} + +// Wrapper for random_walks() through a graph container +// to expose the API to cython. +// +template +std::enable_if_t::value, + std::unique_ptr> +call_random_walks(raft::handle_t const& handle, + graph_container_t const& graph_container, + vertex_t const* ptr_start_set, + edge_t num_paths, + edge_t max_depth, + bool use_padding) +{ + if (graph_container.weightType == numberTypeEnum::floatType) { + using weight_t = float; + + auto graph = + detail::create_graph(handle, graph_container); + + auto triplet = cugraph::experimental::random_walks( + handle, graph->view(), ptr_start_set, num_paths, max_depth, use_padding); + + random_walk_ret_t rw_tri{std::get<0>(triplet).size(), + std::get<1>(triplet).size(), + static_cast(num_paths), + static_cast(max_depth), + std::make_unique(std::get<0>(triplet).release()), + std::make_unique(std::get<1>(triplet).release()), + std::make_unique(std::get<2>(triplet).release())}; + + return std::make_unique(std::move(rw_tri)); + + } else if (graph_container.weightType == numberTypeEnum::doubleType) { + using weight_t = double; + + auto graph = + detail::create_graph(handle, graph_container); + + auto triplet = cugraph::experimental::random_walks( + handle, graph->view(), ptr_start_set, num_paths, max_depth, use_padding); + + random_walk_ret_t rw_tri{std::get<0>(triplet).size(), + std::get<1>(triplet).size(), + static_cast(num_paths), + static_cast(max_depth), + std::make_unique(std::get<0>(triplet).release()), + std::make_unique(std::get<1>(triplet).release()), + std::make_unique(std::get<2>(triplet).release())}; + + return std::make_unique(std::move(rw_tri)); + + } else { + CUGRAPH_FAIL("Unsupported weight type."); + } +} + +template +std::unique_ptr call_rw_paths(raft::handle_t const& handle, + index_t num_paths, + index_t const* vertex_path_sizes) +{ + auto triplet = + cugraph::experimental::query_rw_sizes_offsets(handle, num_paths, vertex_path_sizes); + random_walk_path_t rw_path_tri{ + std::make_unique(std::get<0>(triplet).release()), + std::make_unique(std::get<1>(triplet).release()), + std::make_unique(std::get<2>(triplet).release())}; + return std::make_unique(std::move(rw_path_tri)); +} + +template +std::unique_ptr random_walks_to_coo(raft::handle_t const& handle, + random_walk_ret_t& rw_tri) +{ + auto triplet = cugraph::experimental::convert_paths_to_coo( + handle, + static_cast(rw_tri.coalesced_sz_v_), + static_cast(rw_tri.num_paths_), + std::move(*rw_tri.d_coalesced_v_), + std::move(*rw_tri.d_sizes_)); + + random_walk_coo_t rw_coo{std::get<0>(triplet).size(), + std::get<2>(triplet).size(), + std::make_unique(std::get<0>(triplet).release()), + std::make_unique(std::get<1>(triplet).release()), + std::move(rw_tri.d_coalesced_w_), // pass-through + std::make_unique(std::get<2>(triplet).release())}; + + return std::make_unique(std::move(rw_coo)); +} // Wrapper for calling SSSP through a graph container template @@ -686,6 +1059,187 @@ void call_sssp(raft::handle_t const& handle, } } +// wrapper for weakly connected components: +// +template +void call_wcc(raft::handle_t const& handle, + graph_container_t const& graph_container, + vertex_t* components) +{ + if (graph_container.is_multi_gpu) { + if (graph_container.edgeType == numberTypeEnum::int32Type) { + auto graph = + detail::create_graph(handle, graph_container); + cugraph::experimental::weakly_connected_components( + handle, graph->view(), reinterpret_cast(components), false); + + } else if (graph_container.edgeType == numberTypeEnum::int64Type) { + auto graph = + detail::create_graph(handle, graph_container); + cugraph::experimental::weakly_connected_components( + handle, graph->view(), reinterpret_cast(components), false); + } + } else { + if (graph_container.edgeType == numberTypeEnum::int32Type) { + auto graph = + detail::create_graph(handle, graph_container); + cugraph::experimental::weakly_connected_components( + handle, graph->view(), reinterpret_cast(components), false); + } else if (graph_container.edgeType == numberTypeEnum::int64Type) { + auto graph = + detail::create_graph(handle, graph_container); + cugraph::experimental::weakly_connected_components( + handle, graph->view(), reinterpret_cast(components), false); + } + } +} + +// wrapper for shuffling: +// +template +std::unique_ptr> call_shuffle( + raft::handle_t const& handle, + vertex_t* + edgelist_major_vertices, // [IN / OUT]: groupby_gpuid_and_shuffle_values() sorts in-place + vertex_t* edgelist_minor_vertices, // [IN / OUT] + weight_t* edgelist_weights, // [IN / OUT] + edge_t num_edgelist_edges) +{ + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_size = row_comm.get_size(); + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_size = col_comm.get_size(); + + std::unique_ptr> ptr_ret = + std::make_unique>(handle); + + if (edgelist_weights != nullptr) { + auto zip_edge = thrust::make_zip_iterator( + thrust::make_tuple(edgelist_major_vertices, edgelist_minor_vertices, edgelist_weights)); + + std::forward_as_tuple( + std::tie(ptr_ret->get_major(), ptr_ret->get_minor(), ptr_ret->get_weights()), + std::ignore) = + cugraph::experimental::groupby_gpuid_and_shuffle_values( + comm, // handle.get_comms(), + zip_edge, + zip_edge + num_edgelist_edges, + [key_func = + cugraph::experimental::detail::compute_gpu_id_from_edge_t{ + comm.get_size(), row_comm.get_size(), col_comm.get_size()}] __device__(auto val) { + return key_func(thrust::get<0>(val), thrust::get<1>(val)); + }, + handle.get_stream()); + } else { + auto zip_edge = thrust::make_zip_iterator( + thrust::make_tuple(edgelist_major_vertices, edgelist_minor_vertices)); + + std::forward_as_tuple(std::tie(ptr_ret->get_major(), ptr_ret->get_minor()), + std::ignore) = + cugraph::experimental::groupby_gpuid_and_shuffle_values( + comm, // handle.get_comms(), + zip_edge, + zip_edge + num_edgelist_edges, + [key_func = + cugraph::experimental::detail::compute_gpu_id_from_edge_t{ + comm.get_size(), row_comm.get_size(), col_comm.get_size()}] __device__(auto val) { + return key_func(thrust::get<0>(val), thrust::get<1>(val)); + }, + handle.get_stream()); + } + + auto local_partition_id_op = + [comm_size, + key_func = cugraph::experimental::detail::compute_partition_id_from_edge_t{ + comm_size, row_comm_size, col_comm_size}] __device__(auto pair) { + return key_func(thrust::get<0>(pair), thrust::get<1>(pair)) / + comm_size; // global partition id to local partition id + }; + auto pair_first = thrust::make_zip_iterator( + thrust::make_tuple(ptr_ret->get_major().data(), ptr_ret->get_minor().data())); + + auto edge_counts = + (edgelist_weights != nullptr) + ? cugraph::experimental::groupby_and_count(pair_first, + pair_first + ptr_ret->get_major().size(), + ptr_ret->get_weights().data(), + local_partition_id_op, + col_comm_size, + handle.get_stream()) + : cugraph::experimental::groupby_and_count(pair_first, + pair_first + ptr_ret->get_major().size(), + local_partition_id_op, + col_comm_size, + handle.get_stream()); + + std::vector h_edge_counts(edge_counts.size()); + raft::update_host( + h_edge_counts.data(), edge_counts.data(), edge_counts.size(), handle.get_stream()); + handle.get_stream_view().synchronize(); + + ptr_ret->get_edge_counts().resize(h_edge_counts.size()); + for (size_t i = 0; i < h_edge_counts.size(); ++i) { + ptr_ret->get_edge_counts()[i] = static_cast(h_edge_counts[i]); + } + + return ptr_ret; // RVO-ed +} + +// Wrapper for calling renumber_edeglist() inplace: +// TODO: check if return type needs further handling... +// +template +std::unique_ptr> call_renumber( + raft::handle_t const& handle, + vertex_t* shuffled_edgelist_major_vertices /* [INOUT] */, + vertex_t* shuffled_edgelist_minor_vertices /* [INOUT] */, + std::vector const& edge_counts, + bool do_expensive_check, + bool multi_gpu) // bc. cython cannot take non-type template params +{ + // caveat: return values have different types on the 2 branches below: + // + std::unique_ptr> p_ret = + std::make_unique>(handle); + + if (multi_gpu) { + std::vector displacements(edge_counts.size(), edge_t{0}); + std::partial_sum(edge_counts.begin(), edge_counts.end() - 1, displacements.begin() + 1); + std::vector major_ptrs(edge_counts.size()); + std::vector minor_ptrs(major_ptrs.size()); + for (size_t i = 0; i < edge_counts.size(); ++i) { + major_ptrs[i] = shuffled_edgelist_major_vertices + displacements[i]; + minor_ptrs[i] = shuffled_edgelist_minor_vertices + displacements[i]; + } + + std::tie(p_ret->get_dv(), + p_ret->get_partition(), + p_ret->get_num_vertices(), + p_ret->get_num_edges(), + p_ret->get_segment_offsets()) = + cugraph::experimental::renumber_edgelist( + handle, std::nullopt, major_ptrs, minor_ptrs, edge_counts, do_expensive_check); + } else { + std::tie(p_ret->get_dv(), p_ret->get_segment_offsets()) = + cugraph::experimental::renumber_edgelist( + handle, + std::nullopt, + shuffled_edgelist_major_vertices, + shuffled_edgelist_minor_vertices, + edge_counts[0], + do_expensive_check); + + p_ret->get_partition() = cugraph::experimental::partition_t{}; // dummy + + p_ret->get_num_vertices() = static_cast(p_ret->get_dv().size()); + p_ret->get_num_edges() = edge_counts[0]; + } + + return p_ret; // RVO-ed (copy ellision) +} + // Helper for setting up subcommunicators void init_subcomms(raft::handle_t& handle, size_t row_comm_size) { @@ -806,36 +1360,103 @@ template void call_bfs(raft::handle_t const& handle, int32_t* identifiers, int32_t* distances, int32_t* predecessors, - double* sp_counters, + int32_t depth_limit, const int32_t start_vertex, - bool directed); + bool direction_optimizing); template void call_bfs(raft::handle_t const& handle, graph_container_t const& graph_container, int32_t* identifiers, int32_t* distances, int32_t* predecessors, - double* sp_counters, + int32_t depth_limit, const int32_t start_vertex, - bool directed); + bool direction_optimizing); template void call_bfs(raft::handle_t const& handle, graph_container_t const& graph_container, int64_t* identifiers, int64_t* distances, int64_t* predecessors, - double* sp_counters, + int64_t depth_limit, const int64_t start_vertex, - bool directed); + bool direction_optimizing); template void call_bfs(raft::handle_t const& handle, graph_container_t const& graph_container, int64_t* identifiers, int64_t* distances, int64_t* predecessors, - double* sp_counters, + int64_t depth_limit, const int64_t start_vertex, - bool directed); + bool direction_optimizing); + +template std::unique_ptr call_egonet( + raft::handle_t const& handle, + graph_container_t const& graph_container, + int32_t* source_vertex, + int32_t n_subgraphs, + int32_t radius); + +template std::unique_ptr call_egonet( + raft::handle_t const& handle, + graph_container_t const& graph_container, + int32_t* source_vertex, + int32_t n_subgraphs, + int32_t radius); + +template std::unique_ptr call_egonet( + raft::handle_t const& handle, + graph_container_t const& graph_container, + int64_t* source_vertex, + int64_t n_subgraphs, + int64_t radius); + +template std::unique_ptr call_egonet( + raft::handle_t const& handle, + graph_container_t const& graph_container, + int64_t* source_vertex, + int64_t n_subgraphs, + int64_t radius); + +template std::unique_ptr call_random_walks( + raft::handle_t const& handle, + graph_container_t const& graph_container, + int32_t const* ptr_start_set, + int32_t num_paths, + int32_t max_depth, + bool use_padding); + +template std::unique_ptr call_random_walks( + raft::handle_t const& handle, + graph_container_t const& graph_container, + int32_t const* ptr_start_set, + int64_t num_paths, + int64_t max_depth, + bool use_padding); + +template std::unique_ptr call_random_walks( + raft::handle_t const& handle, + graph_container_t const& graph_container, + int64_t const* ptr_start_set, + int64_t num_paths, + int64_t max_depth, + bool use_padding); + +template std::unique_ptr call_rw_paths( + raft::handle_t const& handle, int32_t num_paths, int32_t const* vertex_path_sizes); + +template std::unique_ptr call_rw_paths( + raft::handle_t const& handle, int64_t num_paths, int64_t const* vertex_path_sizes); + +template std::unique_ptr random_walks_to_coo( + raft::handle_t const& handle, random_walk_ret_t& rw_tri); + +template std::unique_ptr random_walks_to_coo( + raft::handle_t const& handle, random_walk_ret_t& rw_tri); + +template std::unique_ptr random_walks_to_coo( + raft::handle_t const& handle, random_walk_ret_t& rw_tri); template void call_sssp(raft::handle_t const& handle, graph_container_t const& graph_container, @@ -865,5 +1486,137 @@ template void call_sssp(raft::handle_t const& handle, int64_t* predecessors, const int64_t source_vertex); +template void call_wcc(raft::handle_t const& handle, + graph_container_t const& graph_container, + int32_t* components); + +template void call_wcc(raft::handle_t const& handle, + graph_container_t const& graph_container, + int32_t* components); + +template void call_wcc(raft::handle_t const& handle, + graph_container_t const& graph_container, + int64_t* components); + +template void call_wcc(raft::handle_t const& handle, + graph_container_t const& graph_container, + int64_t* components); + +template std::unique_ptr> call_shuffle( + raft::handle_t const& handle, + int32_t* edgelist_major_vertices, + int32_t* edgelist_minor_vertices, + float* edgelist_weights, + int32_t num_edgelist_edges); + +template std::unique_ptr> call_shuffle( + raft::handle_t const& handle, + int32_t* edgelist_major_vertices, + int32_t* edgelist_minor_vertices, + float* edgelist_weights, + int64_t num_edgelist_edges); + +template std::unique_ptr> call_shuffle( + raft::handle_t const& handle, + int32_t* edgelist_major_vertices, + int32_t* edgelist_minor_vertices, + double* edgelist_weights, + int32_t num_edgelist_edges); + +template std::unique_ptr> call_shuffle( + raft::handle_t const& handle, + int32_t* edgelist_major_vertices, + int32_t* edgelist_minor_vertices, + double* edgelist_weights, + int64_t num_edgelist_edges); + +template std::unique_ptr> call_shuffle( + raft::handle_t const& handle, + int64_t* edgelist_major_vertices, + int64_t* edgelist_minor_vertices, + float* edgelist_weights, + int64_t num_edgelist_edges); + +template std::unique_ptr> call_shuffle( + raft::handle_t const& handle, + int64_t* edgelist_major_vertices, + int64_t* edgelist_minor_vertices, + double* edgelist_weights, + int64_t num_edgelist_edges); + +// TODO: add the remaining relevant EIDIr's: +// +template std::unique_ptr> call_renumber( + raft::handle_t const& handle, + int32_t* shuffled_edgelist_major_vertices /* [INOUT] */, + int32_t* shuffled_edgelist_minor_vertices /* [INOUT] */, + std::vector const& edge_counts, + bool do_expensive_check, + bool multi_gpu); + +template std::unique_ptr> call_renumber( + raft::handle_t const& handle, + int32_t* shuffled_edgelist_major_vertices /* [INOUT] */, + int32_t* shuffled_edgelist_minor_vertices /* [INOUT] */, + std::vector const& edge_counts, + bool do_expensive_check, + bool multi_gpu); + +template std::unique_ptr> call_renumber( + raft::handle_t const& handle, + int64_t* shuffled_edgelist_major_vertices /* [INOUT] */, + int64_t* shuffled_edgelist_minor_vertices /* [INOUT] */, + std::vector const& edge_counts, + bool do_expensive_check, + bool multi_gpu); + +template std::unique_ptr call_generate_rmat_edgelist( + raft::handle_t const& handle, + size_t scale, + size_t num_edges, + double a, + double b, + double c, + uint64_t seed, + bool clip_and_flip, + bool scramble_vertex_ids); + +template std::unique_ptr call_generate_rmat_edgelist( + raft::handle_t const& handle, + size_t scale, + size_t num_edges, + double a, + double b, + double c, + uint64_t seed, + bool clip_and_flip, + bool scramble_vertex_ids); + +template std::vector< + std::pair, std::unique_ptr>> +call_generate_rmat_edgelists(raft::handle_t const& handle, + size_t n_edgelists, + size_t min_scale, + size_t max_scale, + size_t edge_factor, + cugraph::generator_distribution_t size_distribution, + cugraph::generator_distribution_t edge_distribution, + uint64_t seed, + bool clip_and_flip, + bool scramble_vertex_ids); + +template std::vector< + std::pair, std::unique_ptr>> +call_generate_rmat_edgelists(raft::handle_t const& handle, + size_t n_edgelists, + size_t min_scale, + size_t max_scale, + size_t edge_factor, + cugraph::generator_distribution_t size_distribution, + cugraph::generator_distribution_t edge_distribution, + uint64_t seed, + bool clip_and_flip, + bool scramble_vertex_ids); + } // namespace cython } // namespace cugraph diff --git a/cpp/src/utilities/graph_bcast.cu b/cpp/src/utilities/graph_bcast.cu new file mode 100644 index 00000000000..e06c1508cf9 --- /dev/null +++ b/cpp/src/utilities/graph_bcast.cu @@ -0,0 +1,45 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// +#include "graph_bcast.cuh" + +namespace cugraph { +namespace broadcast { +using namespace cugraph::experimental; +// Manual template instantiations (EIDir's): +// +template graph_t graph_broadcast( + raft::handle_t const& handle, graph_t* graph_ptr); + +template graph_t graph_broadcast( + raft::handle_t const& handle, graph_t* graph_ptr); + +template graph_t graph_broadcast( + raft::handle_t const& handle, graph_t* graph_ptr); + +template graph_t graph_broadcast( + raft::handle_t const& handle, graph_t* graph_ptr); + +template graph_t graph_broadcast( + raft::handle_t const& handle, graph_t* graph_ptr); + +template graph_t graph_broadcast( + raft::handle_t const& handle, graph_t* graph_ptr); + +} // namespace broadcast +} // namespace cugraph diff --git a/cpp/src/utilities/graph_bcast.cuh b/cpp/src/utilities/graph_bcast.cuh new file mode 100644 index 00000000000..b4007ad20f2 --- /dev/null +++ b/cpp/src/utilities/graph_bcast.cuh @@ -0,0 +1,110 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// +#pragma once + +#include + +#include +#include + +#include + +namespace cugraph { +namespace broadcast { + +/** + * @brief broadcasts graph_t object (only the single GPU version). + * + * @tparam graph_t Type of graph (view). + * @param handle RAFT handle object to encapsulate resources (e.g. CUDA stream, communicator, and + * handles to various CUDA libraries) to run graph algorithms. + * @param graph_ptr pointer to graph object: not `nullptr` on send, `nullptr` (ignored) on receive. + * @return graph_t object that was sent/received + */ +template +graph_t graph_broadcast(raft::handle_t const& handle, graph_t* graph_ptr) +{ + using namespace cugraph::serializer; + using namespace cugraph::experimental; + + using vertex_t = typename graph_t::vertex_type; + using edge_t = typename graph_t::edge_type; + using weight_t = typename graph_t::weight_type; + + if constexpr (!graph_t::is_multi_gpu) { + if (handle.get_comms().get_rank() == 0) { + CUGRAPH_EXPECTS(graph_ptr != nullptr, "Cannot serialize nullptr graph pointer."); + + auto pair = serializer_t::get_device_graph_sz_bytes(*graph_ptr); + thrust::tuple dev_sz_host_sz_bytes = + thrust::make_tuple(pair.first, pair.second); + + auto total_graph_dev_sz = pair.first + pair.second; + + serializer_t ser(handle, total_graph_dev_sz); + serializer_t::graph_meta_t graph_meta{}; + ser.serialize(*graph_ptr, graph_meta); + + int root{0}; + host_scalar_bcast(handle.get_comms(), dev_sz_host_sz_bytes, root, handle.get_stream()); + device_bcast(handle.get_comms(), + ser.get_storage(), + ser.get_storage(), + total_graph_dev_sz, + root, + handle.get_stream()); + + return std::move(*graph_ptr); + } else { + thrust::tuple dev_sz_host_sz_bytes(0, 0); + + int root{0}; + dev_sz_host_sz_bytes = + host_scalar_bcast(handle.get_comms(), dev_sz_host_sz_bytes, root, handle.get_stream()); + // + auto total_graph_dev_sz = + thrust::get<0>(dev_sz_host_sz_bytes) + thrust::get<1>(dev_sz_host_sz_bytes); + + CUGRAPH_EXPECTS(total_graph_dev_sz > 0, "Graph size comm failure."); + + rmm::device_uvector data_buffer(total_graph_dev_sz, + handle.get_stream_view()); + + device_bcast(handle.get_comms(), + data_buffer.data(), + data_buffer.data(), + total_graph_dev_sz, + root, + handle.get_stream()); + + serializer_t ser(handle, data_buffer.data()); + auto graph = ser.unserialize(thrust::get<0>(dev_sz_host_sz_bytes), + thrust::get<1>(dev_sz_host_sz_bytes)); + + return graph; + } + } else { + CUGRAPH_FAIL("Unsupported graph type for broadcasting."); + + return graph_t{handle}; + } +} + +} // namespace broadcast +} // namespace cugraph diff --git a/cpp/src/utilities/graph_utils.cuh b/cpp/src/utilities/graph_utils.cuh index ca0b5831c92..4b7c2baab19 100644 --- a/cpp/src/utilities/graph_utils.cuh +++ b/cpp/src/utilities/graph_utils.cuh @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2018-2021, NVIDIA CORPORATION. All rights reserved. * * NVIDIA CORPORATION and its licensors retain all intellectual property * and proprietary rights in and to this software, related documentation @@ -13,11 +13,12 @@ // Author: Alex Fender afender@nvidia.com #pragma once -#include +#include #include -#include #include +#include +#include #include #include @@ -31,12 +32,12 @@ namespace cugraph { namespace detail { //#define DEBUG 1 -#define CUDA_MAX_BLOCKS 65535 +#define CUDA_MAX_BLOCKS 65535 #define CUDA_MAX_KERNEL_THREADS 256 // kernel will launch at most 256 threads per block #define US template -__inline__ __device__ value_t parallel_prefix_sum(count_t n, index_t const *ind, value_t const *w) +__inline__ __device__ value_t parallel_prefix_sum(count_t n, index_t const* ind, value_t const* w) { count_t i, j, mn; value_t v, last; @@ -80,124 +81,110 @@ __inline__ __device__ value_t parallel_prefix_sum(count_t n, index_t const *ind, return last; } -// dot -template -T dot(size_t n, T *x, T *y) -{ - cudaStream_t stream{nullptr}; - T result = thrust::inner_product(rmm::exec_policy(stream)->on(stream), - thrust::device_pointer_cast(x), - thrust::device_pointer_cast(x + n), - thrust::device_pointer_cast(y), - 0.0f); - CHECK_CUDA(stream); - return result; -} - // axpy template struct axpy_functor : public thrust::binary_function { const T a; axpy_functor(T _a) : a(_a) {} - __host__ __device__ T operator()(const T &x, const T &y) const { return a * x + y; } + __host__ __device__ T operator()(const T& x, const T& y) const { return a * x + y; } }; template -void axpy(size_t n, T a, T *x, T *y) +void axpy(size_t n, T a, T* x, T* y) { - cudaStream_t stream{nullptr}; - thrust::transform(rmm::exec_policy(stream)->on(stream), + rmm::cuda_stream_view stream_view; + thrust::transform(rmm::exec_policy(stream_view), thrust::device_pointer_cast(x), thrust::device_pointer_cast(x + n), thrust::device_pointer_cast(y), thrust::device_pointer_cast(y), axpy_functor(a)); - CHECK_CUDA(stream); + CHECK_CUDA(stream_view.value()); } // norm template struct square { - __host__ __device__ T operator()(const T &x) const { return x * x; } + __host__ __device__ T operator()(const T& x) const { return x * x; } }; template -T nrm2(size_t n, T *x) +T nrm2(size_t n, T* x) { - cudaStream_t stream{nullptr}; + rmm::cuda_stream_view stream_view; T init = 0; - T result = std::sqrt(thrust::transform_reduce(rmm::exec_policy(stream)->on(stream), + T result = std::sqrt(thrust::transform_reduce(rmm::exec_policy(stream_view), thrust::device_pointer_cast(x), thrust::device_pointer_cast(x + n), square(), init, thrust::plus())); - CHECK_CUDA(stream); + CHECK_CUDA(stream_view.value()); return result; } template -T nrm1(size_t n, T *x) +T nrm1(size_t n, T* x) { - cudaStream_t stream{nullptr}; - T result = thrust::reduce(rmm::exec_policy(stream)->on(stream), + rmm::cuda_stream_view stream_view; + T result = thrust::reduce(rmm::exec_policy(stream_view), thrust::device_pointer_cast(x), thrust::device_pointer_cast(x + n)); - CHECK_CUDA(stream); + CHECK_CUDA(stream_view.value()); return result; } template -void scal(size_t n, T val, T *x) +void scal(size_t n, T val, T* x) { - cudaStream_t stream{nullptr}; - thrust::transform(rmm::exec_policy(stream)->on(stream), + rmm::cuda_stream_view stream_view; + thrust::transform(rmm::exec_policy(stream_view), thrust::device_pointer_cast(x), thrust::device_pointer_cast(x + n), thrust::make_constant_iterator(val), thrust::device_pointer_cast(x), thrust::multiplies()); - CHECK_CUDA(stream); + CHECK_CUDA(stream_view.value()); } template -void addv(size_t n, T val, T *x) +void addv(size_t n, T val, T* x) { - cudaStream_t stream{nullptr}; - thrust::transform(rmm::exec_policy(stream)->on(stream), + rmm::cuda_stream_view stream_view; + thrust::transform(rmm::exec_policy(stream_view), thrust::device_pointer_cast(x), thrust::device_pointer_cast(x + n), thrust::make_constant_iterator(val), thrust::device_pointer_cast(x), thrust::plus()); - CHECK_CUDA(stream); + CHECK_CUDA(stream_view.value()); } template -void fill(size_t n, T *x, T value) +void fill(size_t n, T* x, T value) { - cudaStream_t stream{nullptr}; - thrust::fill(rmm::exec_policy(stream)->on(stream), + rmm::cuda_stream_view stream_view; + thrust::fill(rmm::exec_policy(stream_view), thrust::device_pointer_cast(x), thrust::device_pointer_cast(x + n), value); - CHECK_CUDA(stream); + CHECK_CUDA(stream_view.value()); } template -void scatter(size_t n, T *src, T *dst, M *map) +void scatter(size_t n, T* src, T* dst, M* map) { - cudaStream_t stream{nullptr}; - thrust::scatter(rmm::exec_policy(stream)->on(stream), + rmm::cuda_stream_view stream_view; + thrust::scatter(rmm::exec_policy(stream_view), thrust::device_pointer_cast(src), thrust::device_pointer_cast(src + n), thrust::device_pointer_cast(map), thrust::device_pointer_cast(dst)); - CHECK_CUDA(stream); + CHECK_CUDA(stream_view.value()); } template -void printv(size_t n, T *vec, int offset) +void printv(size_t n, T* vec, int offset) { thrust::device_ptr dev_ptr(vec); std::cout.precision(15); @@ -212,13 +199,13 @@ void printv(size_t n, T *vec, int offset) } template -void copy(size_t n, T *x, T *res) +void copy(size_t n, T* x, T* res) { thrust::device_ptr dev_ptr(x); thrust::device_ptr res_ptr(res); - cudaStream_t stream{nullptr}; - thrust::copy_n(rmm::exec_policy(stream)->on(stream), dev_ptr, n, res_ptr); - CHECK_CUDA(stream); + rmm::cuda_stream_view stream_view; + thrust::copy_n(rmm::exec_policy(stream_view), dev_ptr, n, res_ptr); + CHECK_CUDA(stream_view.value()); } template @@ -230,35 +217,35 @@ template struct dangling_functor : public thrust::unary_function { const T val; dangling_functor(T _val) : val(_val) {} - __host__ __device__ T operator()(const T &x) const { return val + x; } + __host__ __device__ T operator()(const T& x) const { return val + x; } }; template -void update_dangling_nodes(size_t n, T *dangling_nodes, T damping_factor) +void update_dangling_nodes(size_t n, T* dangling_nodes, T damping_factor) { - cudaStream_t stream{nullptr}; - thrust::transform_if(rmm::exec_policy(stream)->on(stream), + rmm::cuda_stream_view stream_view; + thrust::transform_if(rmm::exec_policy(stream_view), thrust::device_pointer_cast(dangling_nodes), thrust::device_pointer_cast(dangling_nodes + n), thrust::device_pointer_cast(dangling_nodes), dangling_functor(1.0 - damping_factor), is_zero()); - CHECK_CUDA(stream); + CHECK_CUDA(stream_view.value()); } // google matrix kernels template __global__ void degree_coo(const IndexType n, const IndexType e, - const IndexType *ind, - ValueType *degree) + const IndexType* ind, + ValueType* degree) { for (int i = threadIdx.x + blockIdx.x * blockDim.x; i < e; i += gridDim.x * blockDim.x) atomicAdd(°ree[ind[i]], (ValueType)1.0); } template -__global__ void flag_leafs_kernel(const size_t n, const IndexType *degree, ValueType *bookmark) +__global__ void flag_leafs_kernel(const size_t n, const IndexType* degree, ValueType* bookmark) { for (auto i = threadIdx.x + blockIdx.x * blockDim.x; i < n; i += gridDim.x * blockDim.x) if (degree[i] == 0) bookmark[i] = 1.0; @@ -267,19 +254,19 @@ __global__ void flag_leafs_kernel(const size_t n, const IndexType *degree, Value template __global__ void degree_offsets(const IndexType n, const IndexType e, - const IndexType *ind, - ValueType *degree) + const IndexType* ind, + ValueType* degree) { for (int i = threadIdx.x + blockIdx.x * blockDim.x; i < n; i += gridDim.x * blockDim.x) degree[i] += ind[i + 1] - ind[i]; } template -__global__ void type_convert(FromType *array, int n) +__global__ void type_convert(FromType* array, int n) { for (int i = threadIdx.x + blockIdx.x * blockDim.x; i < n; i += gridDim.x * blockDim.x) { ToType val = array[i]; - ToType *vals = (ToType *)array; + ToType* vals = (ToType*)array; vals[i] = val; } } @@ -287,10 +274,10 @@ __global__ void type_convert(FromType *array, int n) template __global__ void equi_prob3(const IndexType n, const IndexType e, - const IndexType *csrPtr, - const IndexType *csrInd, - ValueType *val, - IndexType *degree) + const IndexType* csrPtr, + const IndexType* csrInd, + ValueType* val, + IndexType* degree) { int j, row, col; for (row = threadIdx.z + blockIdx.z * blockDim.z; row < n; row += gridDim.z * blockDim.z) { @@ -306,10 +293,10 @@ __global__ void equi_prob3(const IndexType n, template __global__ void equi_prob2(const IndexType n, const IndexType e, - const IndexType *csrPtr, - const IndexType *csrInd, - ValueType *val, - IndexType *degree) + const IndexType* csrPtr, + const IndexType* csrInd, + ValueType* val, + IndexType* degree) { int row = blockIdx.x * blockDim.x + threadIdx.x; if (row < n) { @@ -327,13 +314,13 @@ __global__ void equi_prob2(const IndexType n, template void HT_matrix_csc_coo(const IndexType n, const IndexType e, - const IndexType *csrPtr, - const IndexType *csrInd, - ValueType *val, - ValueType *bookmark) + const IndexType* csrPtr, + const IndexType* csrInd, + ValueType* val, + ValueType* bookmark) { - cudaStream_t stream{nullptr}; - rmm::device_vector degree(n, 0); + rmm::cuda_stream_view stream_view; + rmm::device_uvector degree(n, stream_view); dim3 nthreads, nblocks; nthreads.x = min(e, CUDA_MAX_KERNEL_THREADS); @@ -343,8 +330,8 @@ void HT_matrix_csc_coo(const IndexType n, nblocks.y = 1; nblocks.z = 1; degree_coo - <<>>(n, e, csrInd, degree.data().get()); - CHECK_CUDA(stream); + <<>>(n, e, csrInd, degree.data()); + CHECK_CUDA(stream_view.value()); int y = 4; nthreads.x = 32 / y; @@ -354,12 +341,12 @@ void HT_matrix_csc_coo(const IndexType n, nblocks.y = 1; nblocks.z = min((n + nthreads.z - 1) / nthreads.z, CUDA_MAX_BLOCKS); // 1; equi_prob3 - <<>>(n, e, csrPtr, csrInd, val, degree.data().get()); - CHECK_CUDA(stream); + <<>>(n, e, csrPtr, csrInd, val, degree.data()); + CHECK_CUDA(stream_view.value()); ValueType a = 0.0; fill(n, bookmark, a); - CHECK_CUDA(stream); + CHECK_CUDA(stream_view.value()); nthreads.x = min(n, CUDA_MAX_KERNEL_THREADS); nthreads.y = 1; @@ -368,100 +355,12 @@ void HT_matrix_csc_coo(const IndexType n, nblocks.y = 1; nblocks.z = 1; flag_leafs_kernel - <<>>(n, degree.data().get(), bookmark); - CHECK_CUDA(stream); -} - -template -__global__ void permute_vals_kernel(const IndexType e, - IndexType *perm, - ValueType *in, - ValueType *out) -{ - for (int i = threadIdx.x + blockIdx.x * blockDim.x; i < e; i += gridDim.x * blockDim.x) - out[i] = in[perm[i]]; -} - -template -void permute_vals( - const IndexType e, IndexType *perm, ValueType *in, ValueType *out, cudaStream_t stream = nullptr) -{ - int nthreads = min(e, CUDA_MAX_KERNEL_THREADS); - int nblocks = min((e + nthreads - 1) / nthreads, CUDA_MAX_BLOCKS); - permute_vals_kernel<<>>(e, perm, in, out); -} - -// This will remove duplicate along with sorting -// This will sort the COO Matrix, row will be sorted and each column of same row will be sorted. -template -void remove_duplicate( - IndexType *src, IndexType *dest, ValueType *val, SizeT &nnz, cudaStream_t stream = nullptr) -{ - if (val != NULL) { - thrust::stable_sort_by_key(rmm::exec_policy(stream)->on(stream), - thrust::raw_pointer_cast(val), - thrust::raw_pointer_cast(val) + nnz, - thrust::make_zip_iterator(thrust::make_tuple( - thrust::raw_pointer_cast(src), thrust::raw_pointer_cast(dest)))); - thrust::stable_sort_by_key(rmm::exec_policy(stream)->on(stream), - thrust::raw_pointer_cast(dest), - thrust::raw_pointer_cast(dest + nnz), - thrust::make_zip_iterator(thrust::make_tuple( - thrust::raw_pointer_cast(src), thrust::raw_pointer_cast(val)))); - thrust::stable_sort_by_key(rmm::exec_policy(stream)->on(stream), - thrust::raw_pointer_cast(src), - thrust::raw_pointer_cast(src + nnz), - thrust::make_zip_iterator(thrust::make_tuple( - thrust::raw_pointer_cast(dest), thrust::raw_pointer_cast(val)))); - - typedef thrust::tuple IteratorTuple; - typedef thrust::zip_iterator ZipIterator; - typedef thrust::tuple ZipIteratorTuple; - typedef thrust::zip_iterator ZipZipIterator; - - ZipZipIterator newEnd = - thrust::unique(rmm::exec_policy(stream)->on(stream), - thrust::make_zip_iterator(thrust::make_tuple( - thrust::raw_pointer_cast(src), - thrust::make_zip_iterator(thrust::make_tuple( - thrust::raw_pointer_cast(dest), thrust::raw_pointer_cast(val))))), - thrust::make_zip_iterator(thrust::make_tuple( - thrust::raw_pointer_cast(src + nnz), - thrust::make_zip_iterator(thrust::make_tuple(dest + nnz, val + nnz))))); - - ZipIteratorTuple endTuple = newEnd.get_iterator_tuple(); - IndexType *row_end = thrust::get<0>(endTuple); - - nnz = ((size_t)row_end - (size_t)src) / sizeof(IndexType); - } else { - thrust::stable_sort_by_key(rmm::exec_policy(stream)->on(stream), - thrust::raw_pointer_cast(dest), - thrust::raw_pointer_cast(dest + nnz), - thrust::raw_pointer_cast(src)); - thrust::stable_sort_by_key(rmm::exec_policy(stream)->on(stream), - thrust::raw_pointer_cast(src), - thrust::raw_pointer_cast(src + nnz), - thrust::raw_pointer_cast(dest)); - - typedef thrust::tuple IteratorTuple; - typedef thrust::zip_iterator ZipIterator; - - ZipIterator newEnd = - thrust::unique(rmm::exec_policy(stream)->on(stream), - thrust::make_zip_iterator(thrust::make_tuple(thrust::raw_pointer_cast(src), - thrust::raw_pointer_cast(dest))), - thrust::make_zip_iterator(thrust::make_tuple( - thrust::raw_pointer_cast(src + nnz), thrust::raw_pointer_cast(dest + nnz)))); - - IteratorTuple endTuple = newEnd.get_iterator_tuple(); - IndexType *row_end = thrust::get<0>(endTuple); - - nnz = ((size_t)row_end - (size_t)src) / sizeof(IndexType); - } + <<>>(n, degree.data(), bookmark); + CHECK_CUDA(stream_view.value()); } template -__global__ void offsets_to_indices_kernel(const offsets_t *offsets, index_t v, index_t *indices) +__global__ void offsets_to_indices_kernel(const offsets_t* offsets, index_t v, index_t* indices) { auto tid{threadIdx.x}; auto ctaStart{blockIdx.x}; @@ -478,7 +377,7 @@ __global__ void offsets_to_indices_kernel(const offsets_t *offsets, index_t v, i } template -void offsets_to_indices(const offsets_t *offsets, index_t v, index_t *indices) +void offsets_to_indices(const offsets_t* offsets, index_t v, index_t* indices) { cudaStream_t stream{nullptr}; index_t nthreads = min(v, (index_t)CUDA_MAX_KERNEL_THREADS); @@ -488,7 +387,7 @@ void offsets_to_indices(const offsets_t *offsets, index_t v, index_t *indices) } template -void sequence(IndexType n, IndexType *vec, IndexType init = 0) +void sequence(IndexType n, IndexType* vec, IndexType init = 0) { thrust::sequence( thrust::device, thrust::device_pointer_cast(vec), thrust::device_pointer_cast(vec + n), init); @@ -496,16 +395,16 @@ void sequence(IndexType n, IndexType *vec, IndexType init = 0) } template -bool has_negative_val(DistType *arr, size_t n) +bool has_negative_val(DistType* arr, size_t n) { // custom kernel with boolean bitwise reduce may be // faster. - cudaStream_t stream{nullptr}; - DistType result = *thrust::min_element(rmm::exec_policy(stream)->on(stream), + rmm::cuda_stream_view stream_view; + DistType result = *thrust::min_element(rmm::exec_policy(stream_view), thrust::device_pointer_cast(arr), thrust::device_pointer_cast(arr + n)); - CHECK_CUDA(stream); + CHECK_CUDA(stream_view.value()); return (result < 0); } diff --git a/cpp/src/utilities/heap.cuh b/cpp/src/utilities/heap.cuh deleted file mode 100644 index 0747a658324..00000000000 --- a/cpp/src/utilities/heap.cuh +++ /dev/null @@ -1,222 +0,0 @@ -// -*-c++-*- - -/* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Utilities to treat array as a heap -// Author: Chuck Hastings charlesh@nvidia.com - -#ifndef HEAP_H -#define HEAP_H - -namespace cugraph { -namespace detail { - -namespace heap { -/* - * Our goal here is to treat a C-style array indexed - * from 0 to n-1 as a heap. The heap is a binary tress - * structure where the root of each tree is the smallest - * (or largest) value in that subtree. - * - * This is a completely serial implementation. The intention - * from a parallelism perspective would be to use this on - * a block of data assigned to a particular GPU (or CPU) thread. - * - * These functions will allow you to use an existing - * c-style array (host or device side) and manipulate - * it as a heap. - * - * Note, the heap will be represented like this - the - * shape indicates the binary tree structure, the element - * indicates the index of the array that is associated - * with the element. This diagram will help understand - * the parent/child calculations defined below. - * - * 0 - * 1 2 - * 3 4 5 6 - * 7 8 9 10 11 12 13 14 - * - * So element 0 is the root of the tree, element 1 is the - * left child of 0, element 2 is the right child of 0, etc. - */ - -namespace detail { -/** - * @brief Identify the parent index of the specified index. - * NOTE: This function does no bounds checking, so - * the parent of 0 is 0. - * - * See the above documentation for a picture to describe - * the tree. - * - * IndexT is a templated integer type of the index - * - * @param[in] index - the current array index - * @return the index of the parent of the current index - */ -template -inline IndexT __host__ __device__ parent(IndexT index) -{ - static_assert(std::is_integral::value, "Index must be of an integral type"); - - return ((index + 1) / 2) - 1; -} - -/** - * @brief Identify the left child index of the specified index. - * NOTE: This function does no bounds checking, so - * the left child computed might be out of bounds. - * - * See the above documentation for a picture to describe - * the tree. - * - * IndexT is a templated integer type of the index - * - * @param[in] index - the current array index - * @return the index of the left child of the current index - */ -template -inline IndexT __host__ __device__ left_child(IndexT index) -{ - static_assert(std::is_integral::value, "Index must be of an integral type"); - - return ((index + 1) * 2 - 1); -} - -/** - * @brief Identify the right child index of the specified index. - * NOTE: This function does no bounds checking, so - * the right child computed might be out of bounds. - * - * See the above documentation for a picture to describe - * the tree. - * - * IndexT is a templated integer type of the index - * - * @param[in] index - the current array index - * @return the index of the right child of the current index - */ -template -inline IndexT __host__ __device__ right_child(IndexT index) -{ - static_assert(std::is_integral::value, "Index must be of an integral type"); - - return (index + 1) * 2; -} -} // namespace detail - -/** - * @brief Reorder an existing array of elements into a heap - * - * ArrayT is a templated type of the array elements - * IndexT is a templated integer type of the index - * CompareT is a templated compare function - * - * @param[in, out] array - the existing array - * @param[in] size - the number of elements in the existing array - * @param[in] compare - the comparison function to use - * - */ -template -inline void __host__ __device__ heapify(ArrayT *array, IndexT size, CompareT compare) -{ - static_assert(std::is_integral::value, "Index must be of an integral type"); - - // - // We want to order ourselves as a heap. This is accomplished by starting - // at the end and for each element, compare with its parent and - // swap if necessary. We repeat this until there are no more swaps - // (should take no more than log2(size) iterations). - // - IndexT count_swaps = 1; - while (count_swaps > 0) { - count_swaps = 0; - for (IndexT i = size - 1; i > 0; --i) { - IndexT p = detail::parent(i); - - if (compare(array[i], array[p])) { - thrust::swap(array[i], array[p]); - ++count_swaps; - } - } - } -} - -/** - * @brief Pop the top element off of the heap. Note that the caller - * should decrement the size - the last element in the - * array is no longer used. - * - * ArrayT is a templated type of the array elements - * IndexT is a templated integer type of the index - * CompareT is a templated compare function - * - * @return - the top of the heap. - */ -template -inline ArrayT __host__ __device__ heap_pop(ArrayT *array, IndexT size, CompareT compare) -{ - static_assert(std::is_integral::value, "Index must be of an integral type"); - - // - // Swap the top of the array with the last element - // - --size; - thrust::swap(array[0], array[size]); - - // - // Now top element is no longer the smallest (largest), so we need - // to sift it down to the proper location. - // - for (IndexT i = 0; i < size;) { - IndexT lc = detail::left_child(i); - IndexT rc = detail::right_child(i); - IndexT smaller = i; - - // - // We can go out of bounds, let's check the simple cases - // - if (rc < size) { - // - // Both children exist in tree, pick the smaller (lerger) - // one. - // - smaller = (compare(array[lc], array[rc])) ? lc : rc; - } else if (lc < size) { - smaller = lc; - } - - if ((smaller != i) && (compare(array[smaller], array[i]))) { - thrust::swap(array[i], array[smaller]); - i = smaller; - } else { - // - // If we don't swap then we can stop checking, break out of the loop - // - i = size; - } - } - - return array[size]; -} -} // namespace heap - -} // namespace detail -} // namespace cugraph - -#endif diff --git a/cpp/src/utilities/high_res_timer.hpp b/cpp/src/utilities/high_res_timer.hpp index f2d6bc6e13f..cf265991f21 100644 --- a/cpp/src/utilities/high_res_timer.hpp +++ b/cpp/src/utilities/high_res_timer.hpp @@ -1,5 +1,5 @@ /* - * 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. @@ -18,8 +18,12 @@ #include #include #include +#include +#include #include +//#define TIMING + class HighResTimer { public: HighResTimer() : timers() {} @@ -50,10 +54,23 @@ class HighResTimer { it->second.second += stop_time.tv_sec * 1000000000 + stop_time.tv_nsec; } + double get_average_runtime(std::string const& label) + { + auto it = timers.find(label); + if (it != timers.end()) { + return (static_cast(it->second.second) / (1000000.0 * it->second.first)); + } else { + std::stringstream ss; + ss << "ERROR: timing label: " << label << "not found."; + + throw std::runtime_error(ss.str()); + } + } + // // Add display functions... specific label or entire structure // - void display(std::ostream &os) + void display(std::ostream& os) { os << "Timer Results (in ms):" << std::endl; for (auto it = timers.begin(); it != timers.end(); ++it) { @@ -63,7 +80,7 @@ class HighResTimer { } } - void display(std::ostream &os, std::string label) + void display(std::ostream& os, std::string label) { auto it = timers.find(label); os << it->first << " called " << it->second.first @@ -71,7 +88,7 @@ class HighResTimer { << std::endl; } - void display_and_clear(std::ostream &os) + void display_and_clear(std::ostream& os) { os << "Timer Results (in ms):" << std::endl; for (auto it = timers.begin(); it != timers.end(); ++it) { diff --git a/cpp/src/utilities/host_barrier.cpp b/cpp/src/utilities/host_barrier.cpp new file mode 100644 index 00000000000..659e4038c67 --- /dev/null +++ b/cpp/src/utilities/host_barrier.cpp @@ -0,0 +1,106 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include + +#include + +namespace cugraph { +namespace experimental { + +// FIXME: a temporary hack till UCC is integrated into RAFT (so we can use UCC barrier for DASK and +// MPI barrier for MPI) +void host_barrier(raft::comms::comms_t const& comm, rmm::cuda_stream_view stream_view) +{ + stream_view.synchronize(); + + auto const comm_size = comm.get_size(); + auto const comm_rank = comm.get_rank(); + + // k-tree barrier + + int constexpr k = 2; + static_assert(k >= 2); + std::vector requests(k - 1); + std::vector dummies(k - 1); + + // up + + int mod = 1; + while (mod < comm_size) { + if (comm_rank % mod == 0) { + auto level_rank = comm_rank / mod; + if (level_rank % k == 0) { + auto num_irecvs = 0; + ; + for (int i = 1; i < k; ++i) { + auto src_rank = (level_rank + i) * mod; + if (src_rank < comm_size) { + comm.irecv(dummies.data() + (i - 1), + sizeof(std::byte), + src_rank, + int{0} /* tag */, + requests.data() + (i - 1)); + ++num_irecvs; + } + } + comm.waitall(num_irecvs, requests.data()); + } else { + comm.isend(dummies.data(), + sizeof(std::byte), + (level_rank - (level_rank % k)) * mod, + int{0} /* tag */, + requests.data()); + comm.waitall(1, requests.data()); + } + } + mod *= k; + } + + // down + + mod /= k; + while (mod >= 1) { + if (comm_rank % mod == 0) { + auto level_rank = comm_rank / mod; + if (level_rank % k == 0) { + auto num_isends = 0; + for (int i = 1; i < k; ++i) { + auto dst_rank = (level_rank + i) * mod; + if (dst_rank < comm_size) { + comm.isend(dummies.data() + (i - 1), + sizeof(std::byte), + dst_rank, + int{0} /* tag */, + requests.data() + (i - 1)); + ++num_isends; + } + } + comm.waitall(num_isends, requests.data()); + } else { + comm.irecv(dummies.data(), + sizeof(std::byte), + (level_rank - (level_rank % k)) * mod, + int{0} /* tag */, + requests.data()); + comm.waitall(1, requests.data()); + } + } + mod /= k; + } +} + +} // namespace experimental +} // namespace cugraph diff --git a/cpp/src/utilities/path_retrieval.cu b/cpp/src/utilities/path_retrieval.cu new file mode 100644 index 00000000000..765cccc1916 --- /dev/null +++ b/cpp/src/utilities/path_retrieval.cu @@ -0,0 +1,133 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include + +#include +#include + +namespace cugraph { +namespace detail { + +template +__global__ void get_traversed_cost_kernel(vertex_t const* vertices, + vertex_t const* preds, + vertex_t const* vtx_map, + weight_t const* info_weights, + weight_t* out, + vertex_t stop_vertex, + vertex_t num_vertices) +{ + for (vertex_t i = threadIdx.x + blockIdx.x * blockDim.x; i < num_vertices; + i += gridDim.x * blockDim.x) { + weight_t sum = info_weights[i]; + vertex_t pred = preds[i]; + while (pred != stop_vertex) { + vertex_t pos = vtx_map[pred]; + sum += info_weights[pos]; + pred = preds[pos]; + } + out[i] = sum; + } +} + +template +void get_traversed_cost_impl(raft::handle_t const& handle, + vertex_t const* vertices, + vertex_t const* preds, + weight_t const* info_weights, + weight_t* out, + vertex_t stop_vertex, + vertex_t num_vertices) +{ + auto stream = handle.get_stream(); + vertex_t max_blocks = handle.get_device_properties().maxGridSize[0]; + vertex_t max_threads = handle.get_device_properties().maxThreadsPerBlock; + + dim3 nthreads, nblocks; + nthreads.x = std::min(num_vertices, max_threads); + nthreads.y = 1; + nthreads.z = 1; + nblocks.x = std::min((num_vertices + nthreads.x - 1) / nthreads.x, max_blocks); + nblocks.y = 1; + nblocks.z = 1; + + rmm::device_uvector vtx_map_v(num_vertices, stream); + rmm::device_uvector vtx_keys_v(num_vertices, stream); + vertex_t* vtx_map = vtx_map_v.data(); + vertex_t* vtx_keys = vtx_keys_v.data(); + raft::copy(vtx_keys, vertices, num_vertices, stream); + + thrust::sequence(rmm::exec_policy(stream)->on(stream), vtx_map, vtx_map + num_vertices); + + thrust::stable_sort_by_key( + rmm::exec_policy(stream)->on(stream), vtx_keys, vtx_keys + num_vertices, vtx_map); + + get_traversed_cost_kernel<<>>( + vertices, preds, vtx_map, info_weights, out, stop_vertex, num_vertices); +} +} // namespace detail + +template +void get_traversed_cost(raft::handle_t const& handle, + vertex_t const* vertices, + vertex_t const* preds, + weight_t const* info_weights, + weight_t* out, + vertex_t stop_vertex, + vertex_t num_vertices) +{ + CUGRAPH_EXPECTS(num_vertices > 0, "num_vertices should be strictly positive"); + CUGRAPH_EXPECTS(out != nullptr, "out should be of size num_vertices"); + cugraph::detail::get_traversed_cost_impl( + handle, vertices, preds, info_weights, out, stop_vertex, num_vertices); +} + +template void get_traversed_cost(raft::handle_t const& handle, + int32_t const* vertices, + int32_t const* preds, + float const* info_weights, + float* out, + int32_t stop_vertex, + int32_t num_vertices); + +template void get_traversed_cost(raft::handle_t const& handle, + int32_t const* vertices, + int32_t const* preds, + double const* info_weights, + double* out, + int32_t stop_vertex, + int32_t num_vertices); + +template void get_traversed_cost(raft::handle_t const& handle, + int64_t const* vertices, + int64_t const* preds, + float const* info_weights, + float* out, + int64_t stop_vertex, + int64_t num_vertices); + +template void get_traversed_cost(raft::handle_t const& handle, + int64_t const* vertices, + int64_t const* preds, + double const* info_weights, + double* out, + int64_t stop_vertex, + int64_t num_vertices); +} // namespace cugraph diff --git a/cpp/src/utilities/spmv_1D.cu b/cpp/src/utilities/spmv_1D.cu index 8a7378e69d3..b4db219fb89 100644 --- a/cpp/src/utilities/spmv_1D.cu +++ b/cpp/src/utilities/spmv_1D.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -19,13 +19,13 @@ namespace cugraph { namespace mg { template -MGcsrmv::MGcsrmv(raft::handle_t const &handle, - vertex_t *local_vertices, - vertex_t *part_off, - edge_t *off, - vertex_t *ind, - weight_t *val, - weight_t *x) +MGcsrmv::MGcsrmv(raft::handle_t const& handle, + vertex_t* local_vertices, + vertex_t* part_off, + edge_t* off, + vertex_t* ind, + weight_t* val, + weight_t* x) : handle_(handle), local_vertices_(local_vertices), part_off_(part_off), @@ -49,7 +49,7 @@ MGcsrmv::~MGcsrmv() } template -void MGcsrmv::run(weight_t *x) +void MGcsrmv::run(weight_t* x) { using namespace raft::matrix; @@ -72,7 +72,7 @@ void MGcsrmv::run(weight_t *x) auto stream = handle_.get_stream(); - auto const &comm{handle_.get_comms()}; // local + auto const& comm{handle_.get_comms()}; // local std::vector recvbuf(comm.get_size()); std::vector displs(comm.get_size()); diff --git a/cpp/src/utilities/spmv_1D.cuh b/cpp/src/utilities/spmv_1D.cuh index 81466595c19..31af0c75585 100644 --- a/cpp/src/utilities/spmv_1D.cuh +++ b/cpp/src/utilities/spmv_1D.cuh @@ -1,5 +1,5 @@ /* - * 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. @@ -16,8 +16,8 @@ #pragma once #include +#include #include -#include "utilities/error.hpp" namespace cugraph { namespace mg { diff --git a/cpp/src/visitors/bfs_visitor.cpp b/cpp/src/visitors/bfs_visitor.cpp new file mode 100644 index 00000000000..672cc35f00b --- /dev/null +++ b/cpp/src/visitors/bfs_visitor.cpp @@ -0,0 +1,142 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// + +#include +#include + +namespace cugraph { +namespace visitors { + +// +// wrapper code: +// +template +void bfs_visitor::value>>:: + visit_graph(graph_envelope_t::base_graph_t const& graph) +{ + // Note: this must be called only on: + // graph_view_t + // + if constexpr (st == false) { + // unless algorithms only call virtual graph methods + // under the hood, the algos require this conversion: + // + graph_t const* p_g = + static_cast const*>(&graph); + + auto gview = p_g->view(); + + auto const& v_args = ep_.get_args(); + + // unpack bfs() args: + // + assert(v_args.size() == 7); + + // cnstr. args unpacking: + // + raft::handle_t const& handle = *static_cast(v_args[0]); + + vertex_t* p_d_dist = static_cast(v_args[1]); + + vertex_t* p_d_predec = static_cast(v_args[2]); + + vertex_t src_v = *static_cast(v_args[3]); + + bool dir_opt = *static_cast(v_args[4]); + + auto depth_l = *static_cast(v_args[5]); + + bool check = *static_cast(v_args[6]); + + // call algorithm + // (no result; void) + // + bfs(handle, gview, p_d_dist, p_d_predec, src_v, dir_opt, depth_l, check); + } else { + CUGRAPH_FAIL("Unsupported BFS algorithm (store_transposed == true)."); + } +} + +// EIDir's: +// +template class bfs_visitor; +template class bfs_visitor; + +template class bfs_visitor; +template class bfs_visitor; + +template class bfs_visitor; +template class bfs_visitor; + +template class bfs_visitor; +template class bfs_visitor; + +//------ + +template class bfs_visitor; +template class bfs_visitor; + +template class bfs_visitor; +template class bfs_visitor; + +template class bfs_visitor; +template class bfs_visitor; + +template class bfs_visitor; +template class bfs_visitor; + +//------ + +template class bfs_visitor; +template class bfs_visitor; + +template class bfs_visitor; +template class bfs_visitor; + +template class bfs_visitor; +template class bfs_visitor; + +template class bfs_visitor; +template class bfs_visitor; + +} // namespace visitors + +namespace api { +using namespace cugraph::visitors; +// wrapper: +// macro option: MAKE_WRAPPER(bfs) +// +return_t bfs(graph_envelope_t const& g, erased_pack_t& ep) +{ + auto p_visitor = g.factory()->make_bfs_visitor(ep); + + g.apply(*p_visitor); + + return_t ret{p_visitor->get_result()}; + + return ret; // RVO-ed; +} + +} // namespace api +} // namespace cugraph diff --git a/cpp/src/visitors/graph_envelope.cpp b/cpp/src/visitors/graph_envelope.cpp new file mode 100755 index 00000000000..927c5060b1e --- /dev/null +++ b/cpp/src/visitors/graph_envelope.cpp @@ -0,0 +1,52 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// + +#include +#include + +namespace cugraph { +namespace visitors { + +// call cascaded dispatcher with factory and erased_pack_t +// +graph_envelope_t::graph_envelope_t(DTypes vertex_tid, + DTypes edge_tid, + DTypes weight_tid, + bool st, + bool mg, + GTypes graph_tid, + erased_pack_t& ep) + : p_impl_fact_(vertex_dispatcher(vertex_tid, edge_tid, weight_tid, st, mg, graph_tid, ep)) +{ +} + +template class graph_factory_t>; +template class graph_factory_t>; + +template class graph_factory_t>; +template class graph_factory_t>; + +template class graph_factory_t>; +template class graph_factory_t>; + +template class graph_factory_t>; +template class graph_factory_t>; + +} // namespace visitors +} // namespace cugraph diff --git a/cpp/src/visitors/visitors_factory.cpp b/cpp/src/visitors/visitors_factory.cpp new file mode 100644 index 00000000000..c4238166c6a --- /dev/null +++ b/cpp/src/visitors/visitors_factory.cpp @@ -0,0 +1,103 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// + +#include +#include + +namespace cugraph { +namespace visitors { + +template +std::unique_ptr +dependent_factory_t::value>>:: + make_louvain_visitor(erased_pack_t& ep) const +{ + /// return std::unique_ptr( + /// static_cast(new louvain_visitor(ep))); + + return nullptr; // for now... +} + +template +std::unique_ptr +dependent_factory_t::value>>:: + make_bfs_visitor(erased_pack_t& ep) const +{ + // return nullptr; // for now... + return std::make_unique>(ep); +} + +// EIDir's: +// +template class dependent_factory_t; +template class dependent_factory_t; + +template class dependent_factory_t; +template class dependent_factory_t; + +template class dependent_factory_t; +template class dependent_factory_t; + +template class dependent_factory_t; +template class dependent_factory_t; + +//------ + +template class dependent_factory_t; +template class dependent_factory_t; + +template class dependent_factory_t; +template class dependent_factory_t; + +template class dependent_factory_t; +template class dependent_factory_t; + +template class dependent_factory_t; +template class dependent_factory_t; + +//------ + +template class dependent_factory_t; +template class dependent_factory_t; + +template class dependent_factory_t; +template class dependent_factory_t; + +template class dependent_factory_t; +template class dependent_factory_t; + +template class dependent_factory_t; +template class dependent_factory_t; + +// Either use EIDir or specialization, can't have both; +// Prefer specialization when EIdir's are not enough +// because of cascaded-dispatcher exhaustive instantiations +// In this case EIDir above are enough; +} // namespace visitors +} // namespace cugraph diff --git a/cpp/tests/CMakeLists.txt b/cpp/tests/CMakeLists.txt index 593c36359e2..2d13b46ac61 100644 --- a/cpp/tests/CMakeLists.txt +++ b/cpp/tests/CMakeLists.txt @@ -1,6 +1,6 @@ ο»Ώ#============================================================================= # -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. @@ -16,35 +16,155 @@ # #============================================================================= +################################################################################################### +# - common test utils ----------------------------------------------------------------------------- + +add_library(cugraphtestutil STATIC + utilities/matrix_market_file_utilities.cu + utilities/rmat_utilities.cpp + utilities/thrust_wrapper.cu + utilities/misc_utilities.cpp + components/wcc_graphs.cu + ../../thirdparty/mmio/mmio.c) + +target_compile_options(cugraphtestutil + PUBLIC "$<$:${CUGRAPH_CXX_FLAGS}>" + "$:${CUGRAPH_CUDA_FLAGS}>>" +) + +set_property(TARGET cugraphtestutil PROPERTY POSITION_INDEPENDENT_CODE ON) + +target_include_directories(cugraphtestutil + PUBLIC + "${CMAKE_CURRENT_SOURCE_DIR}/../../thirdparty/mmio" + "${CMAKE_CURRENT_SOURCE_DIR}" + "${CUGRAPH_SOURCE_DIR}/src" +) + +target_link_libraries(cugraphtestutil + PUBLIC + cugraph + cuco::cuco + NCCL::NCCL +) + + +add_library(cugraphmgtestutil STATIC + "${CMAKE_CURRENT_SOURCE_DIR}/utilities/device_comm_wrapper.cu") + +set_property(TARGET cugraphmgtestutil PROPERTY POSITION_INDEPENDENT_CODE ON) + +target_include_directories(cugraphmgtestutil + PRIVATE + "${CUB_INCLUDE_DIR}" + "${THRUST_INCLUDE_DIR}" + "${CUCO_INCLUDE_DIR}" + "${LIBCUDACXX_INCLUDE_DIR}" + "${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}" + "${RMM_INCLUDE}" + "${NCCL_INCLUDE_DIRS}" + "${CMAKE_CURRENT_SOURCE_DIR}/../../thirdparty/mmio" + "${CMAKE_CURRENT_SOURCE_DIR}/../include" + "${CMAKE_CURRENT_SOURCE_DIR}" + "${RAFT_DIR}/cpp/include" +) + +target_link_libraries(cugraphmgtestutil cugraph) + ################################################################################################### # - compiler function ----------------------------------------------------------------------------- -function(ConfigureTest CMAKE_TEST_NAME CMAKE_TEST_SRC CMAKE_EXTRA_LIBS) - add_executable(${CMAKE_TEST_NAME} - ${CMAKE_TEST_SRC}) +function(ConfigureTest CMAKE_TEST_NAME) + add_executable(${CMAKE_TEST_NAME} ${ARGN}) - target_include_directories(${CMAKE_TEST_NAME} + target_link_libraries(${CMAKE_TEST_NAME} PRIVATE - "${CUB_INCLUDE_DIR}" - "${THRUST_INCLUDE_DIR}" - "${CUCO_INCLUDE_DIR}" - "${LIBCUDACXX_INCLUDE_DIR}" - "${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}" - "${GTEST_INCLUDE_DIR}" - "${RMM_INCLUDE}" - "${CUDF_INCLUDE}" - "${CUDF_INCLUDE}/libcudf/libcudacxx" - "${NCCL_INCLUDE_DIRS}" - "${CMAKE_SOURCE_DIR}/../thirdparty/mmio" - "${CMAKE_SOURCE_DIR}/include" - "${CMAKE_SOURCE_DIR}/src" - "${CMAKE_CURRENT_SOURCE_DIR}" - "${RAFT_DIR}/cpp/include" + cugraphtestutil + cugraph + GTest::gmock + GTest::gmock_main + GTest::gtest + GTest::gtest_main + NCCL::NCCL + CUDA::cublas + CUDA::cusparse + CUDA::cusolver + CUDA::curand ) + if(OpenMP_CXX_FOUND) + target_link_libraries(${CMAKE_TEST_NAME} PRIVATE +################################################################################################### +### Use ${OpenMP_CXX_LIB_NAMES} instead of OpenMP::OpenMP_CXX to avoid the following warnings. +### +### Cannot generate a safe runtime search path for target TARGET_NAME +### because files in some directories may conflict with libraries in implicit +### directories: +### ... +### +### libgomp.so is included in the conda base environment and copied to every new conda +### environment. If a full file path is provided (e.g ${CUDF_LIBRARY}), cmake +### extracts the directory path and adds the directory path to BUILD_RPATH (if BUILD_RPATH is not +### disabled). +### +### cmake maintains a system specific implicit directories (e.g. /lib, /lib/x86_64-linux-gnu, +### /lib32, /lib32/x86_64-linux-gnu, /lib64, /lib64/x86_64-linux-gnu, /usr/lib, +### /usr/lib/gcc/x86_64-linux-gnu/7, /usr/lib/x86_64-linux-gnu, /usr/lib32, +### /usr/lib32/x86_64-linux-gnu, /usr/lib64, /usr/lib64/x86_64-linux-gnu, +### /usr/local/cuda-10.0/lib64", /usr/local/cuda-10.0/lib64/stubs). +### +### If a full path to libgomp.so is provided (which is the case with OpenMP::OpenMP_CXX), cmake +### checks whether there is any other libgomp.so with the different full path (after resolving +### soft links) in the search paths (implicit directoires + BUILD_RAPTH). There is one in the +### path included in BUILD_RPATH when ${CUDF_LIBRARY} is added; this one can +### potentially hide the one in the provided full path and cmake generates a warning (and RPATH +### is searched before the directories in /etc/ld.so/conf; ld.so.conf does not coincide but +### overlaps with implicit directories). +### +### If we provide just the library names (gomp;pthread), cmake does not generate warnings (we +### did not specify which libgomp.so should be loaded in runtime), and the one first found in +### the search order is loaded (we can change the loaded library by setting LD_LIBRARY_PATH or +### manually editing BUILD_RPATH). +### +### Manually editing BUILD_RPATH: +### set(TARGET_BUILD_RPATH "") +### foreach(TMP_VAR_FULLPATH IN LISTS OpenMP_CXX_LIBRARIES) +### get_filename_component(TMP_VAR_DIR ${TMP_VAR_FULLPATH} DIRECTORY) +### string(APPEND TARGET_BUILD_RPATH "${TMP_VAR_DIR};") +### get_filename_component(TMP_VAR_REALPATH ${TMP_VAR_FULLPATH} REALPATH) +### get_filename_component(TMP_VAR_DIR ${TMP_VAR_REALPATH} DIRECTORY) +### # cmake automatically removes duplicates, so skip checking. +### string(APPEND TARGET_BUILD_RPATH "${TMP_VAR_DIR};") +### endforeach() +### string(APPEND TARGET_BUILD_RPATH "${CONDA_PREFIX}/lib") +### message(STATUS "TARGET_BUILD_RPATH=${TARGET_BUILD_RPATH}") +### set_target_properties(target PROPERTIES +### BUILD_RPATH "${TARGET_BUILD_RPATH}") + ${OpenMP_CXX_LIB_NAMES}) + endif(OpenMP_CXX_FOUND) + + add_test(NAME ${CMAKE_TEST_NAME} COMMAND ${CMAKE_TEST_NAME}) +endfunction() + +function(ConfigureTestMG CMAKE_TEST_NAME) + add_executable(${CMAKE_TEST_NAME} ${ARGN}) + target_link_libraries(${CMAKE_TEST_NAME} PRIVATE - gtest gmock_main gmock cugraph ${CUDF_LIBRARY} ${CMAKE_EXTRA_LIBS} ${NCCL_LIBRARIES} cudart cuda cublas cusparse cusolver curand) + cugraphmgtestutil + cugraphtestutil + cugraph + GTest::gmock + GTest::gmock_main + GTest::gtest + GTest::gtest_main + NCCL::NCCL + CUDA::cublas + CUDA::cusparse + CUDA::cusolver + CUDA::curand + MPI::MPI_CXX + ) if(OpenMP_CXX_FOUND) target_link_libraries(${CMAKE_TEST_NAME} PRIVATE @@ -96,10 +216,14 @@ function(ConfigureTest CMAKE_TEST_NAME CMAKE_TEST_SRC CMAKE_EXTRA_LIBS) ### BUILD_RPATH "${TARGET_BUILD_RPATH}") ${OpenMP_CXX_LIB_NAMES}) endif(OpenMP_CXX_FOUND) - set_target_properties(${CMAKE_TEST_NAME} PROPERTIES - RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/gtests/") - add_test(NAME ${CMAKE_TEST_NAME} COMMAND ${CMAKE_TEST_NAME}) + add_test(NAME ${CMAKE_TEST_NAME} + COMMAND ${MPIEXEC_EXECUTABLE} + ${MPIEXEC_NUMPROC_FLAG} + ${GPU_COUNT} + ${MPIEXEC_PREFLAGS} + ${CMAKE_TEST_NAME} + ${MPIEXEC_POSTFLAGS}) endfunction() ################################################################################################### @@ -117,206 +241,236 @@ endif(RAPIDS_DATASET_ROOT_DIR) ################################################################################################### ################################################################################################### -# - katz centrality tests ------------------------------------------------------------------------- +# - graph generator tests ------------------------------------------------------------------------- -set(KATZ_TEST_SRC - "${CMAKE_SOURCE_DIR}/../thirdparty/mmio/mmio.c" - "${CMAKE_CURRENT_SOURCE_DIR}/centrality/katz_centrality_test.cu") +set(GRAPH_GENERATORS_TEST_SRC + "${CMAKE_CURRENT_SOURCE_DIR}/generators/generators_test.cpp") - ConfigureTest(KATZ_TEST "${KATZ_TEST_SRC}" "") + ConfigureTest(GRAPH_GENERATORS_TEST "${GRAPH_GENERATORS_TEST_SRC}") ################################################################################################### -# - betweenness centrality tests ------------------------------------------------------------------ +# - erdos renyi graph generator tests ------------------------------------------------------------- -set(BETWEENNESS_TEST_SRC - "${CMAKE_SOURCE_DIR}/../thirdparty/mmio/mmio.c" - "${CMAKE_CURRENT_SOURCE_DIR}/centrality/betweenness_centrality_test.cu") +set(ERDOS_RENYI_GENERATOR_TEST_SRC + "${CMAKE_CURRENT_SOURCE_DIR}/generators/erdos_renyi_test.cpp") - ConfigureTest(BETWEENNESS_TEST "${BETWEENNESS_TEST_SRC}" "") - -set(EDGE_BETWEENNESS_TEST_SRC - "${CMAKE_SOURCE_DIR}/../thirdparty/mmio/mmio.c" - "${CMAKE_CURRENT_SOURCE_DIR}/centrality/edge_betweenness_centrality_test.cu") - - ConfigureTest(EDGE_BETWEENNESS_TEST "${EDGE_BETWEENNESS_TEST_SRC}" "") + ConfigureTest(ERDOS_RENYI_GENERATOR_TEST "${ERDOS_RENYI_GENERATOR_TEST_SRC}") ################################################################################################### -# - pagerank tests -------------------------------------------------------------------------------- - -set(PAGERANK_TEST_SRC - "${CMAKE_SOURCE_DIR}/../thirdparty/mmio/mmio.c" - "${CMAKE_CURRENT_SOURCE_DIR}/pagerank/pagerank_test.cpp") +# - katz centrality tests ------------------------------------------------------------------------- +ConfigureTest(KATZ_TEST centrality/katz_centrality_test.cu) -ConfigureTest(PAGERANK_TEST "${PAGERANK_TEST_SRC}" "") +################################################################################################### +# - betweenness centrality tests ------------------------------------------------------------------ +ConfigureTest(BETWEENNESS_TEST centrality/betweenness_centrality_test.cu) +ConfigureTest(EDGE_BETWEENNESS_TEST centrality/edge_betweenness_centrality_test.cu) ################################################################################################### # - SSSP tests ------------------------------------------------------------------------------------ - -set(SSSP_TEST_SRCS - "${CMAKE_SOURCE_DIR}/../thirdparty/mmio/mmio.c" - "${CMAKE_CURRENT_SOURCE_DIR}/traversal/sssp_test.cu") - -ConfigureTest(SSSP_TEST "${SSSP_TEST_SRCS}" "") +ConfigureTest(SSSP_TEST traversal/sssp_test.cu) ################################################################################################### # - BFS tests ------------------------------------------------------------------------------------- - -set(BFS_TEST_SRCS - "${CMAKE_SOURCE_DIR}/../thirdparty/mmio/mmio.c" - "${CMAKE_CURRENT_SOURCE_DIR}/traversal/bfs_test.cu") - -ConfigureTest(BFS_TEST "${BFS_TEST_SRCS}" "") +ConfigureTest(BFS_TEST traversal/bfs_test.cu) ################################################################################################### # - LOUVAIN tests --------------------------------------------------------------------------------- - -set(LOUVAIN_TEST_SRC - "${CMAKE_SOURCE_DIR}/../thirdparty/mmio/mmio.c" - "${CMAKE_CURRENT_SOURCE_DIR}/community/louvain_test.cu") - -ConfigureTest(LOUVAIN_TEST "${LOUVAIN_TEST_SRC}" "") +ConfigureTest(LOUVAIN_TEST community/louvain_test.cpp) ################################################################################################### # - LEIDEN tests --------------------------------------------------------------------------------- - -set(LEIDEN_TEST_SRC - "${CMAKE_SOURCE_DIR}/../thirdparty/mmio/mmio.c" - "${CMAKE_CURRENT_SOURCE_DIR}/community/leiden_test.cpp") - -ConfigureTest(LEIDEN_TEST "${LEIDEN_TEST_SRC}" "") +ConfigureTest(LEIDEN_TEST community/leiden_test.cpp) ################################################################################################### # - ECG tests --------------------------------------------------------------------------------- - -set(ECG_TEST_SRC - "${CMAKE_CURRENT_SOURCE_DIR}/community/ecg_test.cu") - -ConfigureTest(ECG_TEST "${ECG_TEST_SRC}" "") +ConfigureTest(ECG_TEST community/ecg_test.cpp) ################################################################################################### # - Balanced cut clustering tests ----------------------------------------------------------------- - -set(BALANCED_TEST_SRC - "${CMAKE_CURRENT_SOURCE_DIR}/community/balanced_edge_test.cpp") - -ConfigureTest(BALANCED_TEST "${BALANCED_TEST_SRC}" "") +ConfigureTest(BALANCED_TEST community/balanced_edge_test.cpp) ################################################################################################### # - TRIANGLE tests -------------------------------------------------------------------------------- - -set(TRIANGLE_TEST_SRC - "${CMAKE_CURRENT_SOURCE_DIR}/community/triangle_test.cu") - -ConfigureTest(TRIANGLE_TEST "${TRIANGLE_TEST_SRC}" "") +ConfigureTest(TRIANGLE_TEST community/triangle_test.cu) ################################################################################################### -# - RENUMBERING tests ----------------------------------------------------------------------------- - -set(RENUMBERING_TEST_SRC - "${CMAKE_SOURCE_DIR}/../thirdparty/mmio/mmio.c" - "${CMAKE_CURRENT_SOURCE_DIR}/renumber/renumber_test.cu") - -ConfigureTest(RENUMBERING_TEST "${RENUMBERING_TEST_SRC}" "") +# - EGO tests -------------------------------------------------------------------------------- +ConfigureTest(EGO_TEST community/egonet_test.cu) ################################################################################################### # - FORCE ATLAS 2 tests -------------------------------------------------------------------------- +ConfigureTest(FA2_TEST layout/force_atlas2_test.cu) -set(FA2_TEST_SRC - "${CMAKE_SOURCE_DIR}/../thirdparty/mmio/mmio.c" - "${CMAKE_CURRENT_SOURCE_DIR}/layout/force_atlas2_test.cu") - -ConfigureTest(FA2_TEST "${FA2_TEST_SRC}" "") +################################################################################################### +# - TSP tests -------------------------------------------------------------------------- +ConfigureTest(TSP_TEST traversal/tsp_test.cu) ################################################################################################### # - CONNECTED COMPONENTS tests ------------------------------------------------------------------- - -set(CONNECT_TEST_SRC - "${CMAKE_SOURCE_DIR}/../thirdparty/mmio/mmio.c" - "${CMAKE_CURRENT_SOURCE_DIR}/components/con_comp_test.cu") - -ConfigureTest(CONNECT_TEST "${CONNECT_TEST_SRC}" "") +ConfigureTest(CONNECT_TEST components/con_comp_test.cu) ################################################################################################### # - STRONGLY CONNECTED COMPONENTS tests ---------------------------------------------------------- +ConfigureTest(SCC_TEST components/scc_test.cu) -set(SCC_TEST_SRC - "${CMAKE_SOURCE_DIR}/../thirdparty/mmio/mmio.c" - "${CMAKE_CURRENT_SOURCE_DIR}/components/scc_test.cu") +################################################################################################### +# - WEAKLY CONNECTED COMPONENTS tests ---------------------------------------------------------- +ConfigureTest(WCC_TEST components/wcc_test.cpp) -ConfigureTest(SCC_TEST "${SCC_TEST_SRC}" "") +################################################################################################### +#-Hungarian (Linear Assignment Problem) tests ---------------------------------------------------- +ConfigureTest(HUNGARIAN_TEST linear_assignment/hungarian_test.cu) ################################################################################################### -#-Hungarian (Linear Assignment Problem) tests --------------------------------------------------------------------- +# - MST tests ---------------------------------------------------------------------------- +ConfigureTest(MST_TEST tree/mst_test.cu) -set(HUNGARIAN_TEST_SRC - "${CMAKE_CURRENT_SOURCE_DIR}/linear_assignment/hungarian_test.cu") +################################################################################################### +# - Experimental stream tests ----------------------------------------------------- +ConfigureTest(EXPERIMENTAL_STREAM experimental/streams.cu) -ConfigureTest(HUNGARIAN_TEST "${HUNGARIAN_TEST_SRC}" "") +################################################################################################### +# - Experimental R-mat graph generation tests ----------------------------------------------------- +ConfigureTest(EXPERIMENTAL_GENERATE_RMAT_TEST experimental/generate_rmat_test.cpp) ################################################################################################### -# - MST tests ---------------------------------------------------------------------------- +# - Experimental Graph tests ---------------------------------------------------------------------- +ConfigureTest(EXPERIMENTAL_GRAPH_TEST experimental/graph_test.cpp) -set(MST_TEST_SRC - "${CMAKE_SOURCE_DIR}/../thirdparty/mmio/mmio.c" - "${CMAKE_CURRENT_SOURCE_DIR}/tree/mst_test.cu") +################################################################################################### +# - Experimental weight-sum tests ----------------------------------------------------------------- +ConfigureTest(EXPERIMENTAL_WEIGHT_SUM_TEST experimental/weight_sum_test.cpp) -ConfigureTest(MST_TEST "${MST_TEST_SRC}" "") +################################################################################################### +# - Experimental degree tests --------------------------------------------------------------------- +ConfigureTest(EXPERIMENTAL_DEGREE_TEST experimental/degree_test.cpp) +################################################################################################### +# - Experimental coarsening tests ----------------------------------------------------------------- +ConfigureTest(EXPERIMENTAL_COARSEN_GRAPH_TEST experimental/coarsen_graph_test.cpp) ################################################################################################### -# - Experimental Graph tests ---------------------------------------------------------------------- +# - Experimental induced subgraph tests ----------------------------------------------------------- +ConfigureTest(EXPERIMENTAL_INDUCED_SUBGRAPH_TEST experimental/induced_subgraph_test.cpp) -set(EXPERIMENTAL_GRAPH_TEST_SRCS - "${CMAKE_SOURCE_DIR}/../thirdparty/mmio/mmio.c" - "${CMAKE_CURRENT_SOURCE_DIR}/experimental/graph_test.cpp") +################################################################################################### +# - Experimental BFS tests ------------------------------------------------------------------------ +ConfigureTest(EXPERIMENTAL_BFS_TEST experimental/bfs_test.cpp) -ConfigureTest(EXPERIMENTAL_GRAPH_TEST "${EXPERIMENTAL_GRAPH_TEST_SRCS}" "") +################################################################################################### +# - Experimental Multi-source BFS tests ----------------------------------------------------------- +ConfigureTest(EXPERIMENTAL_MSBFS_TEST experimental/ms_bfs_test.cpp) ################################################################################################### -# - Experimental BFS tests ------------------------------------------------------------------------ +# - Experimental SSSP tests ----------------------------------------------------------------------- +ConfigureTest(EXPERIMENTAL_SSSP_TEST experimental/sssp_test.cpp) -set(EXPERIMENTAL_BFS_TEST_SRCS - "${CMAKE_SOURCE_DIR}/../thirdparty/mmio/mmio.c" - "${CMAKE_CURRENT_SOURCE_DIR}/experimental/bfs_test.cpp") +################################################################################################### +# - Experimental PAGERANK tests ------------------------------------------------------------------- +ConfigureTest(EXPERIMENTAL_PAGERANK_TEST experimental/pagerank_test.cpp) -ConfigureTest(EXPERIMENTAL_BFS_TEST "${EXPERIMENTAL_BFS_TEST_SRCS}" "") +################################################################################################### +# - Experimental KATZ_CENTRALITY tests ------------------------------------------------------------ +ConfigureTest(EXPERIMENTAL_KATZ_CENTRALITY_TEST experimental/katz_centrality_test.cpp) ################################################################################################### -# - Experimental SSSP tests ----------------------------------------------------------------------- +# - WEAKLY CONNECTED COMPONENTS tests ------------------------------------------------------------- -set(EXPERIMENTAL_SSSP_TEST_SRCS - "${CMAKE_SOURCE_DIR}/../thirdparty/mmio/mmio.c" - "${CMAKE_CURRENT_SOURCE_DIR}/experimental/sssp_test.cpp") +set(WEAKLY_CONNECTED_COMPONENTS_TEST_SRCS + "${CMAKE_CURRENT_SOURCE_DIR}/components/weakly_connected_components_test.cpp") -ConfigureTest(EXPERIMENTAL_SSSP_TEST "${EXPERIMENTAL_SSSP_TEST_SRCS}" "") +ConfigureTest(WEAKLY_CONNECTED_COMPONENTS_TEST "${WEAKLY_CONNECTED_COMPONENTS_TEST_SRCS}") ################################################################################################### -# - Experimental PAGERANK tests ------------------------------------------------------------------- +# - Experimental RANDOM_WALKS tests --------------------------------------------------------------- +ConfigureTest(RANDOM_WALKS_TEST sampling/random_walks_test.cu) -set(EXPERIMENTAL_PAGERANK_TEST_SRCS - "${CMAKE_SOURCE_DIR}/../thirdparty/mmio/mmio.c" - "${CMAKE_CURRENT_SOURCE_DIR}/experimental/pagerank_test.cpp") +################################################################################################### +ConfigureTest(RANDOM_WALKS_LOW_LEVEL_TEST sampling/rw_low_level_test.cu) -ConfigureTest(EXPERIMENTAL_PAGERANK_TEST "${EXPERIMENTAL_PAGERANK_TEST_SRCS}" "") +################################################################################################### +# FIXME: since this is technically not a test, consider refactoring the the +# ConfigureTest function to share common code with a new ConfigureBenchmark +# function (which would not link gtest, etc.) +ConfigureTest(RANDOM_WALKS_PROFILING sampling/random_walks_profiling.cu) ################################################################################################### -# - Experimental LOUVAIN tests ------------------------------------------------------------------- +# - Serialization tests --------------------------------------------------------------------------- + +set(SERIALIZATION_TEST_SRCS + "${CMAKE_CURRENT_SOURCE_DIR}/serialization/un_serialize_test.cpp") + +ConfigureTest(SERIALIZATION_TEST "${SERIALIZATION_TEST_SRCS}") -# FIXME: Re-enable once failures are fixed -#set(EXPERIMENTAL_LOUVAIN_TEST_SRCS -# "${CMAKE_SOURCE_DIR}/../thirdparty/mmio/mmio.c" -# "${CMAKE_CURRENT_SOURCE_DIR}/experimental/louvain_test.cu") -# -#ConfigureTest(EXPERIMENTAL_LOUVAIN_TEST "${EXPERIMENTAL_LOUVAIN_TEST_SRCS}" "") ################################################################################################### -# - Experimental KATZ_CENTRALITY tests ------------------------------------------------------------ +# - BFS Visitor tests ----------------------------------------------------------------------------- + +set(BFS_VISITOR_TEST_SRCS + "${CMAKE_CURRENT_SOURCE_DIR}/visitors/bfs_test.cpp") -set(EXPERIMENTAL_KATZ_CENTRALITY_TEST_SRCS - "${CMAKE_SOURCE_DIR}/../thirdparty/mmio/mmio.c" - "${CMAKE_CURRENT_SOURCE_DIR}/experimental/katz_centrality_test.cpp") +ConfigureTest(BFS_VISITOR_TEST "${BFS_VISITOR_TEST_SRCS}") -ConfigureTest(EXPERIMENTAL_KATZ_CENTRALITY_TEST "${EXPERIMENTAL_KATZ_CENTRALITY_TEST_SRCS}" "") +################################################################################################### +# - MG tests -------------------------------------------------------------------------------------- + +if(BUILD_CUGRAPH_MG_TESTS) + + ############################################################################################### + # - find MPI - only enabled if MG tests are to be built + find_package(MPI REQUIRED) + + execute_process( + COMMAND nvidia-smi -L + COMMAND wc -l + OUTPUT_VARIABLE GPU_COUNT) + + string(REGEX REPLACE "\n$" "" GPU_COUNT ${GPU_COUNT}) + MESSAGE(STATUS "GPU_COUNT: " ${GPU_COUNT}) + + if(MPI_CXX_FOUND) + ########################################################################################### + # - MG PAGERANK tests --------------------------------------------------------------------- + ConfigureTestMG(MG_PAGERANK_TEST pagerank/mg_pagerank_test.cpp) + + ########################################################################################### + # - MG KATZ CENTRALITY tests -------------------------------------------------------------- + ConfigureTestMG(MG_KATZ_CENTRALITY_TEST experimental/mg_katz_centrality_test.cpp) + + ########################################################################################### + # - MG BFS tests -------------------------------------------------------------------------- + ConfigureTestMG(MG_BFS_TEST experimental/mg_bfs_test.cpp) + + ########################################################################################### + # - MG SSSP tests ------------------------------------------------------------------------- + ConfigureTestMG(MG_SSSP_TEST experimental/mg_sssp_test.cpp) + + ########################################################################################### + # - MG LOUVAIN tests ---------------------------------------------------------------------- + ConfigureTestMG(MG_LOUVAIN_TEST + community/mg_louvain_helper.cu + community/mg_louvain_test.cpp) + + ########################################################################################### + # - MG WEAKLY CONNECTED COMPONENTS tests -------------------------------------------------- + ConfigureTestMG(MG_WEAKLY_CONNECTED_COMPONENTS_TEST + components/mg_weakly_connected_components_test.cpp) + + ########################################################################################### + # - MG GRAPH BROADCAST tests -------------------------------------------------------------- + ConfigureTestMG(MG_GRAPH_BROADCAST_TEST bcast/mg_graph_bcast.cpp) + + ########################################################################################### + # - MG PRIMS COUNT_IF_V tests ------------------------------------------------------------- + ConfigureTestMG(MG_COUNT_IF_V_TEST prims/mg_count_if_v.cu) + + ########################################################################################### + # - MG PRIMS REDUCE_V tests --------------------------------------------------------------- + ConfigureTestMG(MG_REDUCE_V_TEST prims/mg_reduce_v.cu) + else() + message(FATAL_ERROR "OpenMPI NOT found, cannot build MG tests.") + endif() +endif() ################################################################################################### ### enable testing ################################################################################ diff --git a/cpp/tests/README.md b/cpp/tests/README.md new file mode 100644 index 00000000000..b5808822467 --- /dev/null +++ b/cpp/tests/README.md @@ -0,0 +1,31 @@ +# libcugraph C++ tests + +## Prerequisites +### Datasets +``` +/path/to/cuGraph> ./datasets/get_test_data.sh +/path/to/cuGraph> export RAPIDS_DATASET_ROOT_DIR=/path/to/cuGraph/datasets +``` +### System Requirements +* MPI (multi-GPU tests only) + ``` + conda install -c conda-forge openmpi + ``` + +## Building +``` +/path/to/cuGraph> ./build.sh libcugraph +``` +To build the multi-GPU tests: +``` +/path/to/cuGraph> ./build.sh libcugraph cpp-mgtests +``` + +## Running +``` + +``` +To run the multi-GPU tests (example using 2 GPUs): +``` +/path/to/cuGraph> mpirun -n 2 ./cpp/build/gtests/MG_PAGERANK_TEST +``` diff --git a/cpp/tests/bcast/mg_graph_bcast.cpp b/cpp/tests/bcast/mg_graph_bcast.cpp new file mode 100644 index 00000000000..1a0d4c558c9 --- /dev/null +++ b/cpp/tests/bcast/mg_graph_bcast.cpp @@ -0,0 +1,119 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include + +#include + +//////////////////////////////////////////////////////////////////////////////// +// Test param object. This defines the input and expected output for a test, and +// will be instantiated as the parameter to the tests defined below using +// INSTANTIATE_TEST_SUITE_P() +// +struct GraphBcast_Usecase { + std::string graph_file_full_path{}; + + // FIXME: We really should have a Graph_Testparms_Base class or something + // like that which can handle this graph_full_path thing. + // + explicit GraphBcast_Usecase(std::string const& graph_file_path) + { + if ((graph_file_path.length() > 0) && (graph_file_path[0] != '/')) { + graph_file_full_path = cugraph::test::get_rapids_dataset_root_dir() + "/" + graph_file_path; + } else { + graph_file_full_path = graph_file_path; + } + }; +}; + +//////////////////////////////////////////////////////////////////////////////// +// Parameterized test fixture, to be used with TEST_P(). This defines common +// setup and teardown steps as well as common utilities used by each E2E MG +// test. In this case, each test is identical except for the inputs and +// expected outputs, so the entire test is defined in the run_test() method. +// +class GraphBcast_MG_Testfixture : public ::testing::TestWithParam { + public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + + // Run once for each test instance + // + virtual void SetUp() {} + virtual void TearDown() {} + + // Compare the results of broadcasting a graph, + // by comparing the graph that was sent (`sg_graph`) + // with th eone that was received (`graph-copy`): + // + template + void run_test(const GraphBcast_Usecase& param) + { + using namespace cugraph::broadcast; + using sg_graph_t = cugraph::experimental::graph_t; + + raft::handle_t handle; + + raft::comms::initialize_mpi_comms(&handle, MPI_COMM_WORLD); + const auto& comm = handle.get_comms(); + + auto const comm_rank = comm.get_rank(); + + auto [sg_graph, d_renumber_map_labels] = + cugraph::test::read_graph_from_matrix_market_file( + handle, param.graph_file_full_path, true, /*renumber=*/false); + + if (comm_rank == 0) { + graph_broadcast(handle, &sg_graph); + } else { + sg_graph_t* g_ignore{nullptr}; + auto graph_copy = graph_broadcast(handle, g_ignore); + auto [same, str_fail] = cugraph::test::compare_graphs(handle, sg_graph, graph_copy); + + if (!same) std::cerr << "Graph comparison failed on " << str_fail << '\n'; + + ASSERT_TRUE(same); + } + } +}; + +//////////////////////////////////////////////////////////////////////////////// +TEST_P(GraphBcast_MG_Testfixture, CheckInt32Int32Float) +{ + run_test(GetParam()); +} + +INSTANTIATE_TEST_SUITE_P(simple_test, + GraphBcast_MG_Testfixture, + ::testing::Values(GraphBcast_Usecase("test/datasets/karate.mtx") + //,GraphBcast_Usecase("test/datasets/smallworld.mtx") + )); + +CUGRAPH_MG_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/centrality/betweenness_centrality_test.cu b/cpp/tests/centrality/betweenness_centrality_test.cu index d680574e10b..a171b0010d1 100644 --- a/cpp/tests/centrality/betweenness_centrality_test.cu +++ b/cpp/tests/centrality/betweenness_centrality_test.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -18,13 +18,14 @@ #include #include -#include -#include +#include +#include #include #include #include +#include #include @@ -49,68 +50,80 @@ // C++ Reference Implementation // ============================================================================ template -void ref_accumulation(result_t *result, +void ref_accumulation(result_t* result, vertex_t const number_of_vertices, - std::stack &S, - std::vector> &pred, - std::vector &sigmas, - std::vector &deltas, + std::stack& S, + std::vector>& pred, + std::vector& sigmas, + std::vector& deltas, vertex_t source) { - for (vertex_t v = 0; v < number_of_vertices; ++v) { deltas[v] = 0; } + for (vertex_t v = 0; v < number_of_vertices; ++v) { + deltas[v] = 0; + } while (!S.empty()) { vertex_t w = S.top(); S.pop(); - for (vertex_t v : pred[w]) { deltas[v] += (sigmas[v] / sigmas[w]) * (1.0 + deltas[w]); } + for (vertex_t v : pred[w]) { + deltas[v] += (sigmas[v] / sigmas[w]) * (1.0 + deltas[w]); + } if (w != source) { result[w] += deltas[w]; } } } template -void ref_endpoints_accumulation(result_t *result, +void ref_endpoints_accumulation(result_t* result, vertex_t const number_of_vertices, - std::stack &S, - std::vector> &pred, - std::vector &sigmas, - std::vector &deltas, + std::stack& S, + std::vector>& pred, + std::vector& sigmas, + std::vector& deltas, vertex_t source) { result[source] += S.size() - 1; - for (vertex_t v = 0; v < number_of_vertices; ++v) { deltas[v] = 0; } + for (vertex_t v = 0; v < number_of_vertices; ++v) { + deltas[v] = 0; + } while (!S.empty()) { vertex_t w = S.top(); S.pop(); - for (vertex_t v : pred[w]) { deltas[v] += (sigmas[v] / sigmas[w]) * (1.0 + deltas[w]); } + for (vertex_t v : pred[w]) { + deltas[v] += (sigmas[v] / sigmas[w]) * (1.0 + deltas[w]); + } if (w != source) { result[w] += deltas[w] + 1; } } } template -void ref_edge_accumulation(result_t *result, +void ref_edge_accumulation(result_t* result, vertex_t const number_of_vertices, - std::stack &S, - std::vector> &pred, - std::vector &sigmas, - std::vector &deltas, + std::stack& S, + std::vector>& pred, + std::vector& sigmas, + std::vector& deltas, vertex_t source) { - for (vertex_t v = 0; v < number_of_vertices; ++v) { deltas[v] = 0; } + for (vertex_t v = 0; v < number_of_vertices; ++v) { + deltas[v] = 0; + } while (!S.empty()) { vertex_t w = S.top(); S.pop(); - for (vertex_t v : pred[w]) { deltas[v] += (sigmas[v] / sigmas[w]) * (1.0 + deltas[w]); } + for (vertex_t v : pred[w]) { + deltas[v] += (sigmas[v] / sigmas[w]) * (1.0 + deltas[w]); + } if (w != source) { result[w] += deltas[w]; } } } // Algorithm 1: Shortest-path vertex betweenness, (Brandes, 2001) template -void reference_betweenness_centrality_impl(vertex_t *indices, - edge_t *offsets, +void reference_betweenness_centrality_impl(vertex_t* indices, + edge_t* offsets, vertex_t const number_of_vertices, - result_t *result, + result_t* result, bool endpoints, - vertex_t const *sources, + vertex_t const* sources, vertex_t const number_of_sources) { std::queue Q; @@ -158,7 +171,7 @@ void reference_betweenness_centrality_impl(vertex_t *indices, } template -void reference_rescale(result_t *result, +void reference_rescale(result_t* result, bool directed, bool normalize, bool endpoints, @@ -189,25 +202,27 @@ void reference_rescale(result_t *result, rescale_factor *= (casted_number_of_vertices / casted_number_of_sources); } } - for (auto idx = 0; idx < number_of_vertices; ++idx) { result[idx] *= rescale_factor; } + for (auto idx = 0; idx < number_of_vertices; ++idx) { + result[idx] *= rescale_factor; + } } template void reference_betweenness_centrality( - cugraph::GraphCSRView const &graph, - result_t *result, + cugraph::legacy::GraphCSRView const& graph, + result_t* result, bool normalize, bool endpoints, // This is not yet implemented vertex_t const number_of_sources, - vertex_t const *sources) + vertex_t const* sources) { vertex_t number_of_vertices = graph.number_of_vertices; edge_t number_of_edges = graph.number_of_edges; thrust::host_vector h_indices(number_of_edges); thrust::host_vector h_offsets(number_of_vertices + 1); - thrust::device_ptr d_indices((vertex_t *)&graph.indices[0]); - thrust::device_ptr d_offsets((edge_t *)&graph.offsets[0]); + thrust::device_ptr d_indices((vertex_t*)&graph.indices[0]); + thrust::device_ptr d_offsets((edge_t*)&graph.offsets[0]); thrust::copy(d_indices, d_indices + number_of_edges, h_indices.begin()); thrust::copy(d_offsets, d_offsets + (number_of_vertices + 1), h_offsets.begin()); @@ -227,14 +242,14 @@ void reference_betweenness_centrality( // Explicit instantiation /* FIXME!!! template void reference_betweenness_centrality( - cugraph::GraphCSRView const &, + cugraph::legacy::GraphCSRView const &, float *, bool, bool, const int, int const *); template void reference_betweenness_centrality( - cugraph::GraphCSRView const &, + cugraph::legacy::GraphCSRView const &, double *, bool, bool, @@ -248,7 +263,7 @@ template void reference_betweenness_centrality( // Compare while allowing relatie error of epsilon // zero_threshold indicates when we should drop comparison for small numbers template -bool compare_close(const T &a, const T &b, const precision_t epsilon, precision_t zero_threshold) +bool compare_close(const T& a, const T& b, const precision_t epsilon, precision_t zero_threshold) { return ((zero_threshold > a && zero_threshold > b)) || (a >= b * (1.0 - epsilon)) && (a <= b * (1.0 + epsilon)); @@ -264,12 +279,12 @@ typedef struct BC_Usecase_t { std::string config_; // Path to graph file std::string file_path_; // Complete path to graph using dataset_root_dir int number_of_sources_; // Starting point from the traversal - BC_Usecase_t(const std::string &config, int number_of_sources) + BC_Usecase_t(const std::string& config, int number_of_sources) : config_(config), number_of_sources_(number_of_sources) { // assume relative paths are relative to RAPIDS_DATASET_ROOT_DIR // FIXME: Use platform independent stuff from c++14/17 on compiler update - const std::string &rapidsDatasetRootDir = cugraph::test::get_rapids_dataset_root_dir(); + const std::string& rapidsDatasetRootDir = cugraph::test::get_rapids_dataset_root_dir(); if ((config_ != "") && (config_[0] != '/')) { file_path_ = rapidsDatasetRootDir + "/" + config_; } else { @@ -300,15 +315,15 @@ class Tests_BC : public ::testing::TestWithParam { typename result_t, bool normalize, bool endpoints> - void run_current_test(const BC_Usecase &configuration) + void run_current_test(const BC_Usecase& configuration) { // Step 1: Construction of the graph based on configuration bool is_directed = false; auto csr = cugraph::test::generate_graph_csr_from_mm( is_directed, configuration.file_path_); cudaDeviceSynchronize(); - cugraph::GraphCSRView G = csr->view(); - G.prop.directed = is_directed; + cugraph::legacy::GraphCSRView G = csr->view(); + G.prop.directed = is_directed; CUDA_TRY(cudaGetLastError()); std::vector result(G.number_of_vertices, 0); std::vector expected(G.number_of_vertices, 0); @@ -323,7 +338,7 @@ class Tests_BC : public ::testing::TestWithParam { std::vector sources(configuration.number_of_sources_); thrust::sequence(thrust::host, sources.begin(), sources.end(), 0); - vertex_t *sources_ptr = nullptr; + vertex_t* sources_ptr = nullptr; if (configuration.number_of_sources_ > 0) { sources_ptr = sources.data(); } reference_betweenness_centrality( @@ -338,7 +353,7 @@ class Tests_BC : public ::testing::TestWithParam { d_result.data().get(), normalize, endpoints, - static_cast(nullptr), + static_cast(nullptr), configuration.number_of_sources_, sources_ptr); cudaDeviceSynchronize(); @@ -363,6 +378,9 @@ TEST_P(Tests_BC, CheckFP32_NO_NORMALIZE_NO_ENDPOINTS) run_current_test(GetParam()); } +#if 0 +// Temporarily disable some of the test combinations +// Full solution will be explored for issue #1555 TEST_P(Tests_BC, CheckFP64_NO_NORMALIZE_NO_ENDPOINTS) { run_current_test(GetParam()); @@ -372,6 +390,7 @@ TEST_P(Tests_BC, CheckFP32_NO_NORMALIZE_ENDPOINTS) { run_current_test(GetParam()); } +#endif TEST_P(Tests_BC, CheckFP64_NO_NORMALIZE_ENDPOINTS) { @@ -384,6 +403,9 @@ TEST_P(Tests_BC, CheckFP32_NORMALIZE_NO_ENDPOINTS) run_current_test(GetParam()); } +#if 0 +// Temporarily disable some of the test combinations +// Full solution will be explored for issue #1555 TEST_P(Tests_BC, CheckFP64_NORMALIZE_NO_ENDPOINTS) { run_current_test(GetParam()); @@ -393,18 +415,29 @@ TEST_P(Tests_BC, CheckFP32_NORMALIZE_ENDPOINTS) { run_current_test(GetParam()); } +#endif TEST_P(Tests_BC, CheckFP64_NORMALIZE_ENDPOINTS) { run_current_test(GetParam()); } -INSTANTIATE_TEST_CASE_P(simple_test, - Tests_BC, - ::testing::Values(BC_Usecase("test/datasets/karate.mtx", 0), - BC_Usecase("test/datasets/netscience.mtx", 0), - BC_Usecase("test/datasets/netscience.mtx", 4), - BC_Usecase("test/datasets/wiki2003.mtx", 4), - BC_Usecase("test/datasets/wiki-Talk.mtx", 4))); +#if 0 +// Temporarily disable some of the test combinations +// Full solution will be explored for issue #1555 +INSTANTIATE_TEST_SUITE_P(simple_test, + Tests_BC, + ::testing::Values(BC_Usecase("test/datasets/karate.mtx", 0), + BC_Usecase("test/datasets/netscience.mtx", 0), + BC_Usecase("test/datasets/netscience.mtx", 4), + BC_Usecase("test/datasets/wiki2003.mtx", 4), + BC_Usecase("test/datasets/wiki-Talk.mtx", 4))); +#else +INSTANTIATE_TEST_SUITE_P(simple_test, + Tests_BC, + ::testing::Values(BC_Usecase("test/datasets/karate.mtx", 0), + BC_Usecase("test/datasets/netscience.mtx", 0), + BC_Usecase("test/datasets/netscience.mtx", 4))); +#endif CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/centrality/edge_betweenness_centrality_test.cu b/cpp/tests/centrality/edge_betweenness_centrality_test.cu index b6cce8684e8..67fdb22f953 100644 --- a/cpp/tests/centrality/edge_betweenness_centrality_test.cu +++ b/cpp/tests/centrality/edge_betweenness_centrality_test.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -22,11 +22,12 @@ #include #include +#include #include -#include -#include +#include +#include #include #include @@ -52,8 +53,8 @@ template edge_t get_edge_index_from_source_and_destination(vertex_t source_vertex, vertex_t destination_vertex, - vertex_t const *indices, - edge_t const *offsets) + vertex_t const* indices, + edge_t const* offsets) { edge_t index = -1; edge_t first_edge_idx = offsets[source_vertex]; @@ -64,17 +65,19 @@ edge_t get_edge_index_from_source_and_destination(vertex_t source_vertex, } template -void ref_accumulation(result_t *result, - vertex_t const *indices, - edge_t const *offsets, +void ref_accumulation(result_t* result, + vertex_t const* indices, + edge_t const* offsets, vertex_t const number_of_vertices, - std::stack &S, - std::vector> &pred, - std::vector &sigmas, - std::vector &deltas, + std::stack& S, + std::vector>& pred, + std::vector& sigmas, + std::vector& deltas, vertex_t source) { - for (vertex_t v = 0; v < number_of_vertices; ++v) { deltas[v] = 0; } + for (vertex_t v = 0; v < number_of_vertices; ++v) { + deltas[v] = 0; + } while (!S.empty()) { vertex_t w = S.top(); S.pop(); @@ -92,11 +95,11 @@ void ref_accumulation(result_t *result, // Algorithm 1: Shortest-path vertex betweenness, (Brandes, 2001) template -void reference_edge_betweenness_centrality_impl(vertex_t *indices, - edge_t *offsets, +void reference_edge_betweenness_centrality_impl(vertex_t* indices, + edge_t* offsets, vertex_t const number_of_vertices, - result_t *result, - vertex_t const *sources, + result_t* result, + vertex_t const* sources, vertex_t const number_of_sources) { std::queue Q; @@ -134,7 +137,7 @@ void reference_edge_betweenness_centrality_impl(vertex_t *indices, } template -void reference_rescale(result_t *result, +void reference_rescale(result_t* result, bool directed, bool normalize, vertex_t const number_of_vertices, @@ -149,24 +152,26 @@ void reference_rescale(result_t *result, } else { if (!directed) { rescale_factor /= static_cast(2); } } - for (auto idx = 0; idx < number_of_edges; ++idx) { result[idx] *= rescale_factor; } + for (auto idx = 0; idx < number_of_edges; ++idx) { + result[idx] *= rescale_factor; + } } template void reference_edge_betweenness_centrality( - cugraph::GraphCSRView const &graph, - result_t *result, + cugraph::legacy::GraphCSRView const& graph, + result_t* result, bool normalize, vertex_t const number_of_sources, - vertex_t const *sources) + vertex_t const* sources) { vertex_t number_of_vertices = graph.number_of_vertices; edge_t number_of_edges = graph.number_of_edges; thrust::host_vector h_indices(number_of_edges); thrust::host_vector h_offsets(number_of_vertices + 1); - thrust::device_ptr d_indices((vertex_t *)&graph.indices[0]); - thrust::device_ptr d_offsets((edge_t *)&graph.offsets[0]); + thrust::device_ptr d_indices((vertex_t*)&graph.indices[0]); + thrust::device_ptr d_offsets((edge_t*)&graph.offsets[0]); thrust::copy(d_indices, d_indices + number_of_edges, h_indices.begin()); thrust::copy(d_offsets, d_offsets + (number_of_vertices + 1), h_offsets.begin()); @@ -185,7 +190,7 @@ void reference_edge_betweenness_centrality( // Compare while allowing relatie error of epsilon // zero_threshold indicates when we should drop comparison for small numbers template -bool compare_close(const T &a, const T &b, const precision_t epsilon, precision_t zero_threshold) +bool compare_close(const T& a, const T& b, const precision_t epsilon, precision_t zero_threshold) { return ((zero_threshold > a && zero_threshold > b)) || (a >= b * (1.0 - epsilon)) && (a <= b * (1.0 + epsilon)); @@ -201,12 +206,12 @@ typedef struct EdgeBC_Usecase_t { std::string config_; // Path to graph file std::string file_path_; // Complete path to graph using dataset_root_dir int number_of_sources_; // Starting point from the traversal - EdgeBC_Usecase_t(const std::string &config, int number_of_sources) + EdgeBC_Usecase_t(const std::string& config, int number_of_sources) : config_(config), number_of_sources_(number_of_sources) { // assume relative paths are relative to RAPIDS_DATASET_ROOT_DIR // FIXME: Use platform independent stuff from c++14/17 on compiler update - const std::string &rapidsDatasetRootDir = cugraph::test::get_rapids_dataset_root_dir(); + const std::string& rapidsDatasetRootDir = cugraph::test::get_rapids_dataset_root_dir(); if ((config_ != "") && (config_[0] != '/')) { file_path_ = rapidsDatasetRootDir + "/" + config_; } else { @@ -236,15 +241,15 @@ class Tests_EdgeBC : public ::testing::TestWithParam { typename weight_t, typename result_t, bool normalize> - void run_current_test(const EdgeBC_Usecase &configuration) + void run_current_test(const EdgeBC_Usecase& configuration) { // Step 1: Construction of the graph based on configuration bool is_directed = false; auto csr = cugraph::test::generate_graph_csr_from_mm( is_directed, configuration.file_path_); cudaDeviceSynchronize(); - cugraph::GraphCSRView G = csr->view(); - G.prop.directed = is_directed; + cugraph::legacy::GraphCSRView G = csr->view(); + G.prop.directed = is_directed; CUDA_TRY(cudaGetLastError()); std::vector result(G.number_of_edges, 0); std::vector expected(G.number_of_edges, 0); @@ -259,7 +264,7 @@ class Tests_EdgeBC : public ::testing::TestWithParam { std::vector sources(configuration.number_of_sources_); thrust::sequence(thrust::host, sources.begin(), sources.end(), 0); - vertex_t *sources_ptr = nullptr; + vertex_t* sources_ptr = nullptr; if (configuration.number_of_sources_ > 0) { sources_ptr = sources.data(); } reference_edge_betweenness_centrality( @@ -273,7 +278,7 @@ class Tests_EdgeBC : public ::testing::TestWithParam { G, d_result.data().get(), normalize, - static_cast(nullptr), + static_cast(nullptr), configuration.number_of_sources_, sources_ptr); CUDA_TRY(cudaMemcpy(result.data(), @@ -296,6 +301,9 @@ TEST_P(Tests_EdgeBC, CheckFP32_NO_NORMALIZE) run_current_test(GetParam()); } +#if 0 +// Temporarily disable some of the test combinations +// Full solution will be explored for issue #1555 TEST_P(Tests_EdgeBC, CheckFP64_NO_NORMALIZE) { run_current_test(GetParam()); @@ -306,18 +314,29 @@ TEST_P(Tests_EdgeBC, CheckFP32_NORMALIZE) { run_current_test(GetParam()); } +#endif TEST_P(Tests_EdgeBC, CheckFP64_NORMALIZE) { run_current_test(GetParam()); } -INSTANTIATE_TEST_CASE_P(simple_test, - Tests_EdgeBC, - ::testing::Values(EdgeBC_Usecase("test/datasets/karate.mtx", 0), - EdgeBC_Usecase("test/datasets/netscience.mtx", 0), - EdgeBC_Usecase("test/datasets/netscience.mtx", 4), - EdgeBC_Usecase("test/datasets/wiki2003.mtx", 4), - EdgeBC_Usecase("test/datasets/wiki-Talk.mtx", 4))); +#if 0 +// Temporarily disable some of the test combinations +// Full solution will be explored for issue #1555 +INSTANTIATE_TEST_SUITE_P(simple_test, + Tests_EdgeBC, + ::testing::Values(EdgeBC_Usecase("test/datasets/karate.mtx", 0), + EdgeBC_Usecase("test/datasets/netscience.mtx", 0), + EdgeBC_Usecase("test/datasets/netscience.mtx", 4), + EdgeBC_Usecase("test/datasets/wiki2003.mtx", 4), + EdgeBC_Usecase("test/datasets/wiki-Talk.mtx", 4))); +#else +INSTANTIATE_TEST_SUITE_P(simple_test, + Tests_EdgeBC, + ::testing::Values(EdgeBC_Usecase("test/datasets/karate.mtx", 0), + EdgeBC_Usecase("test/datasets/netscience.mtx", 0), + EdgeBC_Usecase("test/datasets/netscience.mtx", 4))); +#endif CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/centrality/katz_centrality_test.cu b/cpp/tests/centrality/katz_centrality_test.cu index c4f17192955..ee2df5347fc 100644 --- a/cpp/tests/centrality/katz_centrality_test.cu +++ b/cpp/tests/centrality/katz_centrality_test.cu @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. @@ -20,8 +20,8 @@ #include -#include -#include +#include +#include #include #include @@ -35,7 +35,9 @@ std::vector getGoldenTopKIds(std::ifstream& fs_result, int k = 10) std::vector vec; int val; int count = 0; - while (fs_result >> val && ((count++) < k)) { vec.push_back(val); } + while (fs_result >> val && ((count++) < k)) { + vec.push_back(val); + } vec.resize(k); return vec; } @@ -56,13 +58,13 @@ std::vector getTopKIds(double* p_katz, int count, int k = 10) } template -int getMaxDegree(cugraph::GraphCSRView const& g) +int getMaxDegree(cugraph::legacy::GraphCSRView const& g) { cudaStream_t stream{nullptr}; rmm::device_vector degree_vector(g.number_of_vertices); ET* p_degree = degree_vector.data().get(); - g.degree(p_degree, cugraph::DegreeDirection::OUT); + g.degree(p_degree, cugraph::legacy::DegreeDirection::OUT); ET max_out_degree = thrust::reduce(rmm::exec_policy(stream)->on(stream), p_degree, p_degree + g.number_of_vertices, @@ -137,9 +139,10 @@ class Tests_Katz : public ::testing::TestWithParam { << "\n"; ASSERT_EQ(fclose(fpin), 0); - cugraph::GraphCOOView cooview(&cooColInd[0], &cooRowInd[0], nullptr, m, nnz); - auto csr = cugraph::coo_to_csr(cooview); - cugraph::GraphCSRView G = csr->view(); + cugraph::legacy::GraphCOOView cooview( + &cooColInd[0], &cooRowInd[0], nullptr, m, nnz); + auto csr = cugraph::coo_to_csr(cooview); + cugraph::legacy::GraphCSRView G = csr->view(); rmm::device_vector katz_vector(m); double* d_katz = thrust::raw_pointer_cast(katz_vector.data()); @@ -156,7 +159,7 @@ class Tests_Katz : public ::testing::TestWithParam { } }; -INSTANTIATE_TEST_CASE_P( +INSTANTIATE_TEST_SUITE_P( simple_test, Tests_Katz, ::testing::Values(Katz_Usecase("test/datasets/karate.mtx", "ref/katz/karate.csv"), diff --git a/cpp/tests/community/balanced_edge_test.cpp b/cpp/tests/community/balanced_edge_test.cpp index 81cee945821..d4c5edf3f35 100644 --- a/cpp/tests/community/balanced_edge_test.cpp +++ b/cpp/tests/community/balanced_edge_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. * * NVIDIA CORPORATION and its licensors retain all intellectual property * and proprietary rights in and to this software, related documentation @@ -10,7 +10,7 @@ */ #include -#include +#include #include @@ -48,7 +48,7 @@ TEST(balanced_edge, success) rmm::device_vector weights_v(w_h); rmm::device_vector result_v(cluster_id); - cugraph::GraphCSRView G( + cugraph::legacy::GraphCSRView G( offsets_v.data().get(), indices_v.data().get(), weights_v.data().get(), num_verts, num_edges); int num_clusters{8}; diff --git a/cpp/tests/community/ecg_test.cu b/cpp/tests/community/ecg_test.cpp similarity index 70% rename from cpp/tests/community/ecg_test.cu rename to cpp/tests/community/ecg_test.cpp index 85b80b1610b..f174d882937 100644 --- a/cpp/tests/community/ecg_test.cu +++ b/cpp/tests/community/ecg_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. * * NVIDIA CORPORATION and its licensors retain all intellectual property * and proprietary rights in and to this software, related documentation @@ -10,8 +10,8 @@ */ #include -#include -#include +#include +#include #include @@ -47,7 +47,7 @@ TEST(ecg, success) rmm::device_vector weights_v(w_h); rmm::device_vector result_v(cluster_id); - cugraph::GraphCSRView graph_csr( + cugraph::legacy::GraphCSRView graph_csr( offsets_v.data().get(), indices_v.data().get(), weights_v.data().get(), num_verts, num_edges); raft::handle_t handle; @@ -77,6 +77,10 @@ TEST(ecg, success) TEST(ecg, dolphin) { + raft::handle_t handle; + + auto stream = handle.get_stream(); + std::vector off_h = {0, 6, 14, 18, 21, 22, 26, 32, 37, 43, 50, 55, 56, 57, 65, 77, 84, 90, 99, 106, 110, 119, 125, 126, 129, 135, 138, 141, 146, 151, 160, 165, 166, 169, 179, 184, 185, 192, 203, @@ -103,38 +107,57 @@ TEST(ecg, dolphin) int num_verts = off_h.size() - 1; int num_edges = ind_h.size(); - thrust::host_vector cluster_id(num_verts, -1); + std::vector cluster_id(num_verts, -1); - rmm::device_vector offsets_v(off_h); - rmm::device_vector indices_v(ind_h); - rmm::device_vector weights_v(w_h); - rmm::device_vector result_v(cluster_id); + rmm::device_uvector offsets_v(num_verts + 1, stream); + rmm::device_uvector indices_v(num_edges, stream); + rmm::device_uvector weights_v(num_edges, stream); + rmm::device_uvector result_v(num_verts, stream); - cugraph::GraphCSRView graph_csr( - offsets_v.data().get(), indices_v.data().get(), weights_v.data().get(), num_verts, num_edges); + raft::update_device(offsets_v.data(), off_h.data(), off_h.size(), stream); + raft::update_device(indices_v.data(), ind_h.data(), ind_h.size(), stream); + raft::update_device(weights_v.data(), w_h.data(), w_h.size(), stream); - raft::handle_t handle; - cugraph::ecg(handle, graph_csr, .05, 16, result_v.data().get()); + cugraph::legacy::GraphCSRView graph_csr( + offsets_v.data(), indices_v.data(), weights_v.data(), num_verts, num_edges); - cluster_id = result_v; - int max = *max_element(cluster_id.begin(), cluster_id.end()); - int min = *min_element(cluster_id.begin(), cluster_id.end()); + // "FIXME": remove this check once we drop support for Pascal + // + // Calling louvain on Pascal will throw an exception, we'll check that + // this is the behavior while we still support Pascal (device_prop.major < 7) + // + if (handle.get_device_properties().major < 7) { + EXPECT_THROW( + (cugraph::ecg(handle, graph_csr, .05, 16, result_v.data())), + cugraph::logic_error); + } else { + cugraph::ecg(handle, graph_csr, .05, 16, result_v.data()); - ASSERT_EQ((min >= 0), 1); + raft::update_host(cluster_id.data(), result_v.data(), num_verts, stream); - std::set cluster_ids; - for (auto c : cluster_id) { cluster_ids.insert(c); } + CUDA_TRY(cudaDeviceSynchronize()); - ASSERT_EQ(cluster_ids.size(), size_t(max + 1)); + int max = *max_element(cluster_id.begin(), cluster_id.end()); + int min = *min_element(cluster_id.begin(), cluster_id.end()); - float modularity{0.0}; + ASSERT_EQ((min >= 0), 1); - cugraph::ext_raft::analyzeClustering_modularity( - graph_csr, max + 1, result_v.data().get(), &modularity); + std::set cluster_ids; + for (auto c : cluster_id) { + cluster_ids.insert(c); + } + + ASSERT_EQ(cluster_ids.size(), size_t(max + 1)); + + float modularity{0.0}; + + cugraph::ext_raft::analyzeClustering_modularity( + graph_csr, max + 1, result_v.data(), &modularity); - float random_modularity{0.95 * 0.4962422251701355}; + float random_modularity{0.95 * 0.4962422251701355}; - ASSERT_GT(modularity, random_modularity); + ASSERT_GT(modularity, random_modularity); + } } CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/community/egonet_test.cu b/cpp/tests/community/egonet_test.cu new file mode 100644 index 00000000000..29be3508de7 --- /dev/null +++ b/cpp/tests/community/egonet_test.cu @@ -0,0 +1,292 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +#include + +typedef struct InducedEgo_Usecase_t { + std::string graph_file_full_path{}; + std::vector ego_sources{}; + int32_t radius; + bool test_weighted{false}; + + InducedEgo_Usecase_t(std::string const& graph_file_path, + std::vector const& ego_sources, + int32_t radius, + bool test_weighted) + : ego_sources(ego_sources), radius(radius), test_weighted(test_weighted) + { + if ((graph_file_path.length() > 0) && (graph_file_path[0] != '/')) { + graph_file_full_path = cugraph::test::get_rapids_dataset_root_dir() + "/" + graph_file_path; + } else { + graph_file_full_path = graph_file_path; + } + }; +} InducedEgo_Usecase; + +class Tests_InducedEgo : public ::testing::TestWithParam { + public: + Tests_InducedEgo() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + + virtual void SetUp() {} + virtual void TearDown() {} + + template + void run_current_test(InducedEgo_Usecase const& configuration) + { + int n_streams = std::min(configuration.ego_sources.size(), static_cast(128)); + raft::handle_t handle(n_streams); + + cugraph::experimental::graph_t graph( + handle); + std::tie(graph, std::ignore) = cugraph::test:: + read_graph_from_matrix_market_file( + handle, configuration.graph_file_full_path, configuration.test_weighted, false); + auto graph_view = graph.view(); + + rmm::device_uvector d_ego_sources(configuration.ego_sources.size(), + handle.get_stream()); + + raft::update_device(d_ego_sources.data(), + configuration.ego_sources.data(), + configuration.ego_sources.size(), + handle.get_stream()); + + HighResTimer hr_timer; + hr_timer.start("egonet"); + cudaProfilerStart(); + auto [d_ego_edgelist_src, d_ego_edgelist_dst, d_ego_edgelist_weights, d_ego_edge_offsets] = + cugraph::experimental::extract_ego(handle, + graph_view, + d_ego_sources.data(), + static_cast(configuration.ego_sources.size()), + configuration.radius); + cudaProfilerStop(); + hr_timer.stop(); + hr_timer.display(std::cout); + std::vector h_cugraph_ego_edge_offsets(d_ego_edge_offsets.size()); + std::vector h_cugraph_ego_edgelist_src(d_ego_edgelist_src.size()); + std::vector h_cugraph_ego_edgelist_dst(d_ego_edgelist_dst.size()); + raft::update_host(h_cugraph_ego_edgelist_src.data(), + d_ego_edgelist_src.data(), + d_ego_edgelist_src.size(), + handle.get_stream()); + raft::update_host(h_cugraph_ego_edgelist_dst.data(), + d_ego_edgelist_dst.data(), + d_ego_edgelist_dst.size(), + handle.get_stream()); + raft::update_host(h_cugraph_ego_edge_offsets.data(), + d_ego_edge_offsets.data(), + d_ego_edge_offsets.size(), + handle.get_stream()); + ASSERT_TRUE(d_ego_edge_offsets.size() == (configuration.ego_sources.size() + 1)); + ASSERT_TRUE(d_ego_edgelist_src.size() == d_ego_edgelist_dst.size()); + if (configuration.test_weighted) + ASSERT_TRUE(d_ego_edgelist_src.size() == (*d_ego_edgelist_weights).size()); + ASSERT_TRUE(h_cugraph_ego_edge_offsets[configuration.ego_sources.size()] == + d_ego_edgelist_src.size()); + for (size_t i = 0; i < configuration.ego_sources.size(); i++) + ASSERT_TRUE(h_cugraph_ego_edge_offsets[i] <= h_cugraph_ego_edge_offsets[i + 1]); + auto n_vertices = graph_view.get_number_of_vertices(); + for (size_t i = 0; i < d_ego_edgelist_src.size(); i++) { + ASSERT_TRUE( + cugraph::experimental::is_valid_vertex(n_vertices, h_cugraph_ego_edgelist_src[i])); + ASSERT_TRUE( + cugraph::experimental::is_valid_vertex(n_vertices, h_cugraph_ego_edgelist_dst[i])); + } + } +}; + +TEST_P(Tests_InducedEgo, CheckInt32Int32FloatUntransposed) +{ + run_current_test(GetParam()); +} + +INSTANTIATE_TEST_SUITE_P( + simple_test, + Tests_InducedEgo, + ::testing::Values( + InducedEgo_Usecase("test/datasets/karate.mtx", std::vector{0}, 1, false), + InducedEgo_Usecase("test/datasets/karate.mtx", std::vector{0}, 2, false), + InducedEgo_Usecase("test/datasets/karate.mtx", std::vector{1}, 3, false), + InducedEgo_Usecase("test/datasets/karate.mtx", std::vector{10, 0, 5}, 2, false), + InducedEgo_Usecase("test/datasets/karate.mtx", std::vector{9, 3, 10}, 2, false), + InducedEgo_Usecase( + "test/datasets/karate.mtx", std::vector{5, 9, 3, 10, 12, 13}, 2, true))); + +// For perf analysis +/* +INSTANTIATE_TEST_SUITE_P( + simple_test, + Tests_InducedEgo, + ::testing::Values( + InducedEgo_Usecase("test/datasets/soc-LiveJournal1.mtx", std::vector{0}, 1, false), + InducedEgo_Usecase("test/datasets/soc-LiveJournal1.mtx", std::vector{0}, 2, false), + InducedEgo_Usecase("test/datasets/soc-LiveJournal1.mtx", std::vector{0}, 3, false), + InducedEgo_Usecase("test/datasets/soc-LiveJournal1.mtx", std::vector{0}, 4, false), + InducedEgo_Usecase("test/datasets/soc-LiveJournal1.mtx", std::vector{0}, 5, false), + InducedEgo_Usecase( + "test/datasets/soc-LiveJournal1.mtx", std::vector{363617}, 2, false), + InducedEgo_Usecase( + "test/datasets/soc-LiveJournal1.mtx", + std::vector{ + 363617, 722214, 2337449, 2510183, 2513389, 225853, 2035807, 3836330, 1865496, 28755}, + 2, + false), + InducedEgo_Usecase( + "test/datasets/soc-LiveJournal1.mtx", + std::vector{ + 363617, 722214, 2337449, 2510183, 2513389, 225853, 2035807, 3836330, 1865496, 28755, + 2536834, 3070144, 3888415, 3131712, 2382526, 1040771, 2631543, 4607218, 4465829, 3341686, + 2772973, 2611175, 4526129, 2624421, 1220593, 2593137, 3270705, 1503899, 1213033, 4840102, + 4529036, 3421116, 4264831, 4089751, 4272322, 3486998, 2830318, 320953, 2388331, 520808, + 3023094, 1600294, 3631119, 1716614, 4829213, 1175844, 960680, 847662, 3277365, 3957318, + 3455123, 2454259, 670953, 4465677, 1027332, 2560721, 89061, 1163406, 3109528, 3221856, + 4714426, 2382774, 37828, 4433616, 3283229, 591911, 4200188, 442522, 872207, 2437601, + 741003, 266241, 914618, 3626195, 2021080, 4679624, 777476, 2527796, 1114017, 640142, + 49259, 4069879, 3869098, 1105040, 4707804, 3208582, 3325885, 1450601, 4072548, 2037062, + 2029646, 4575891, 1488598, 79105, 4827273, 3795434, 4647518, 4733397, 3980718, 1184627}, + 2, + false), + InducedEgo_Usecase( + "test/datasets/soc-LiveJournal1.mtx", + std::vector{ + 363617, 722214, 2337449, 2510183, 2513389, 225853, 2035807, 3836330, 1865496, 28755, + 2536834, 3070144, 3888415, 3131712, 2382526, 1040771, 2631543, 4607218, 4465829, 3341686, + 2772973, 2611175, 4526129, 2624421, 1220593, 2593137, 3270705, 1503899, 1213033, 4840102, + 4529036, 3421116, 4264831, 4089751, 4272322, 3486998, 2830318, 320953, 2388331, 520808, + 3023094, 1600294, 3631119, 1716614, 4829213, 1175844, 960680, 847662, 3277365, 3957318, + 3455123, 2454259, 670953, 4465677, 1027332, 2560721, 89061, 1163406, 3109528, 3221856, + 4714426, 2382774, 37828, 4433616, 3283229, 591911, 4200188, 442522, 872207, 2437601, + 741003, 266241, 914618, 3626195, 2021080, 4679624, 777476, 2527796, 1114017, 640142, + 49259, 4069879, 3869098, 1105040, 4707804, 3208582, 3325885, 1450601, 4072548, 2037062, + 2029646, 4575891, 1488598, 79105, 4827273, 3795434, 4647518, 4733397, 3980718, 1184627, + 984983, 3114832, 1967741, 1599818, 144593, 2698770, 2889449, 2495550, 1053813, 1193622, + 686026, 3989015, 2040719, 4693428, 3190376, 2926728, 3399030, 1664419, 662429, 4526841, + 2186957, 3752558, 2440046, 2930226, 3633006, 4058166, 3137060, 3499296, 2126343, 148971, + 2199672, 275811, 2813976, 2274536, 1189239, 1335942, 2465624, 2596042, 829684, 193400, + 2682845, 3691697, 4022437, 4051170, 4195175, 2876420, 3984220, 2174475, 326134, 2606530, + 2493046, 4706121, 1498980, 4576225, 1271339, 44832, 1875673, 4664940, 134931, 736397, + 4333554, 2751031, 2163610, 2879676, 3174153, 3317403, 2052464, 1881883, 4757859, 3596257, + 2358088, 2578758, 447504, 590720, 1717038, 1869795, 1133885, 3027521, 840312, 2818881, + 3654321, 2730947, 353585, 1134903, 2223378, 1508824, 3662521, 1363776, 2712071, 288441, + 1204581, 3502242, 4645567, 2767267, 1514366, 3956099, 1422145, 1216608, 2253360, 189132, + 4238225, 1345783, 451571, 1599442, 3237284, 4711405, 929446, 1857675, 150759, 1277633, + 761210, 138628, 1026833, 2599544, 2464737, 989203, 3399615, 2144292, 216142, 637312, + 2044964, 716256, 1660632, 1762919, 4784357, 2213415, 2764769, 291806, 609772, 3264819, + 1870953, 1516385, 235647, 1045474, 2664957, 819095, 1824119, 4045271, 4448109, 1676788, + 4285177, 1580502, 3546548, 2771971, 3927086, 1339779, 3156204, 1730998, 1172522, 2433024, + 4533449, 479930, 2010695, 672994, 3542039, 3176455, 26352, 2137735, 866910, 4410835, + 2623982, 3603159, 2555625, 2765653, 267865, 2015523, 1009052, 4713994, 1600667, 2176195, + 3179631, 4570390, 2018424, 3356384, 1784287, 894861, 3622099, 1647273, 3044136, 950354, + 1491760, 3416929, 3757300, 2244912, 4129215, 1600848, 3867343, 72329, 919189, 992521, + 3445975, 4712557, 4680974, 188419, 2612093, 1991268, 3566207, 2281468, 3859078, 2492806, + 3398628, 763441, 2679107, 2554420, 2130132, 4664374, 1182901, 3890770, 4714667, 4209303, + 4013060, 3617653, 2040022, 3296519, 4190671, 1693353, 2678411, 3788834, 2781815, 191965, + 1083926, 503974, 3529226, 1650522, 1900976, 542080, 3423929, 3418905, 878165, 4701703, + 3022790, 4316365, 76365, 4053672, 1358185, 3830478, 4445661, 3210024, 1895915, 4541133, + 2938808, 562788, 3920065, 1458776, 4052046, 2967475, 1092809, 3203538, 159626, 3399464, + 214467, 3343982, 1811854, 3189045, 4272117, 4701563, 424807, 4341116, 760545, 4674683, + 1538018, 386762, 194237, 2162719, 1694433, 943728, 2389036, 2196653, 3085571, 1513424, + 3689413, 3278747, 4197291, 3324063, 3651090, 1737936, 2768803, 2768889, 3108096, 4311775, + 3569480, 886705, 733256, 2477493, 1735412, 2960895, 1983781, 1861797, 3566460, 4537673, + 1164093, 3499764, 4553071, 3518985, 847658, 918948, 2922351, 1056144, 652895, 1013195, + 780505, 1702928, 3562838, 1432719, 2405207, 1054920, 641647, 2240939, 3617702, 383165, + 652641, 879593, 1810739, 2096385, 4497865, 4768530, 1743968, 3582014, 1025009, 3002122, + 2422190, 527647, 1251821, 2571153, 4095874, 3705333, 3637407, 1385567, 4043855, 4041930, + 2433139, 1710383, 1127734, 4362316, 711588, 817839, 3214775, 910077, 1313768, 2382229, + 16864, 2081770, 3095420, 3195272, 548711, 2259860, 1167323, 2435974, 425238, 2085179, + 2630042, 2632881, 2867923, 3703565, 1037695, 226617, 4379130, 1541468, 3581937, 605965, + 1137674, 4655221, 4769963, 1394370, 4425315, 2990132, 2364485, 1561137, 2713384, 481509, + 2900382, 934766, 2986774, 1767669, 298593, 2502539, 139296, 3794229, 4002180, 4718138, + 2909238, 423691, 3023810, 2784924, 2760160, 1971980, 316683, 3828090, 3253691, 4839313, + 1203624, 584938, 3901482, 1747543, 1572737, 3533226, 774708, 1691195, 1037110, 1557763, + 225120, 4424243, 3524086, 1717663, 4332507, 3513592, 4274932, 1232118, 873498, 1416042, + 2488925, 111391, 4704545, 4492545, 445317, 1584812, 2187737, 2471948, 3731678, 219255, + 2282627, 2589971, 2372185, 4609096, 3673961, 2524410, 12823, 2437155, 3015974, 4188352, + 3184084, 3690756, 1222341, 1278376, 3652030, 4162647, 326548, 3930062, 3926100, 1551222, + 2722165, 4526695, 3997534, 4815513, 3139056, 2547644, 3028915, 4149092, 3656554, 2691582, + 2676699, 1878842, 260174, 3129900, 4379993, 182347, 2189338, 3783616, 2616666, 2596952, + 243007, 4179282, 2730, 1939894, 2332032, 3335636, 182332, 3112260, 2174584, 587481, + 4527368, 3154106, 3403059, 673206, 2150292, 446521, 1600204, 4819428, 2591357, 48490, + 2917012, 2285923, 1072926, 2824281, 4364250, 956033, 311938, 37251, 3729300, 2726300, + 644966, 1623020, 1419070, 4646747, 2417222, 2680238, 2561083, 1793801, 2349366, 339747, + 611366, 4684147, 4356907, 1277161, 4510381, 3218352, 4161658, 3200733, 1172372, 3997786, + 3169266, 3353418, 2248955, 2875885, 2365369, 498208, 2968066, 2681505, 2059048, 2097106, + 3607540, 1121504, 2016789, 1762605, 3138431, 866081, 3705757, 3833066, 2599788, 760816, + 4046672, 1544367, 2983906, 4842911, 209599, 1250954, 3333704, 561212, 4674336, 2831841, + 3690724, 2929360, 4830834, 1177524, 2487687, 3525137, 875283, 651241, 2110742, 1296646, + 1543739, 4349417, 2384725, 1931751, 1519208, 1520034, 3385008, 3219962, 734912, 170230, + 1741419, 729913, 2860117, 2362381, 1199807, 2424230, 177824, 125948, 2722701, 4687548, + 1140771, 3232742, 4522020, 4376360, 1125603, 590312, 2481884, 138951, 4086775, 615155, + 3395781, 4587272, 283209, 568470, 4296185, 4344150, 2454321, 2672602, 838828, 4051647, + 1709120, 3074610, 693235, 4356087, 3018806, 239410, 2431497, 691186, 766276, 4462126, + 859155, 2370304, 1571808, 1938673, 1694955, 3871296, 4245059, 3987376, 301524, 2512461, + 3410437, 3300380, 684922, 4581995, 3599557, 683515, 1850634, 3704678, 1937490, 2035591, + 3718533, 2065879, 3160765, 1467884, 1912241, 2501509, 3668572, 3390469, 2501150, 612319, + 713633, 1976262, 135946, 3641535, 632083, 13414, 4217765, 4137712, 2550250, 3281035, + 4179598, 961045, 2020694, 4380006, 1345936, 289162, 1359035, 770872, 4509911, 3947317, + 4719693, 248568, 2625660, 1237232, 2153208, 4814282, 1259954, 3677369, 861222, 2883506, + 3339149, 3998335, 491017, 1609022, 2648112, 742132, 649609, 4206953, 3131106, 3504814, + 3344486, 611721, 3215620, 2856233, 4447505, 1949222, 1868345, 712710, 6966, 4730666, + 3181872, 2972889, 3038521, 3525444, 4385208, 1845613, 1124187, 2030476, 4468651, 2478792, + 3473580, 3783357, 1852991, 1648485, 871319, 1670723, 4458328, 3218600, 1811100, 3443356, + 2233873, 3035207, 2548692, 3337891, 3773674, 1552957, 4782811, 3144712, 3523466, 1491315, + 3955852, 1838410, 3164028, 1092543, 776459, 2959379, 2541744, 4064418, 3908320, 2854145, + 3960709, 1348188, 977678, 853619, 1304291, 2848702, 1657913, 1319826, 3322665, 788037, + 2913686, 4471279, 1766285, 348304, 56570, 1892118, 4017244, 401006, 3524539, 4310134, + 1624693, 4081113, 957511, 849400, 129975, 2616130, 378537, 1556787, 3916162, 1039980, + 4407778, 2027690, 4213675, 839863, 683134, 75805, 2493150, 4215796, 81587, 751845, + 1255588, 1947964, 1950470, 859401, 3077088, 3931110, 2316256, 1523761, 4527477, 4237511, + 1123513, 4209796, 3584772, 4250563, 2091754, 1618766, 2139944, 4525352, 382159, 2955887, + 41760, 2313998, 496912, 3791570, 3904792, 3613654, 873959, 127076, 2537797, 2458107, + 4543265, 3661909, 26828, 271816, 17854, 2461269, 1776042, 1573899, 3409957, 4335712, + 4534313, 3392751, 1230124, 2159031, 4444015, 3373087, 3848014, 2026600, 1382747, 3537242, + 4536743, 4714155, 3788371, 3570849, 173741, 211962, 4377778, 119369, 2856973, 2945854, + 1508054, 4503932, 3141566, 1842177, 3448683, 3384614, 2886508, 1573965, 990618, 3053734, + 2918742, 4508753, 1032149, 60943, 4291620, 722607, 2883224, 169359, 4356585, 3725543, + 3678729, 341673, 3592828, 4077251, 3382936, 3885685, 4630994, 1286698, 4449616, 1138430, + 3113385, 4660578, 2539973, 4562286, 4085089, 494737, 3967610, 2130702, 1823755, 1369324, + 3796951, 956299, 141730, 935144, 4381893, 4412545, 1382250, 3024476, 2364546, 3396164, + 3573511, 314081, 577688, 4154135, 1567018, 4047761, 2446220, 1148833, 4842497, 3967186, + 1175290, 3749667, 1209593, 3295627, 3169065, 2460328, 1838486, 1436923, 2843887, 3676426, + 2079145, 2975635, 535071, 4287509, 3281107, 39606, 3115500, 3204573, 722131, 3124073}, + 2, + false))); +*/ +CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/community/leiden_test.cpp b/cpp/tests/community/leiden_test.cpp index 764ab8bf6cb..13e139666f6 100644 --- a/cpp/tests/community/leiden_test.cpp +++ b/cpp/tests/community/leiden_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. * * NVIDIA CORPORATION and its licensors retain all intellectual property * and proprietary rights in and to this software, related documentation @@ -10,8 +10,8 @@ */ #include -#include -#include +#include +#include #include @@ -19,6 +19,10 @@ TEST(leiden_karate, success) { + raft::handle_t handle; + + auto stream = handle.get_stream(); + std::vector off_h = {0, 16, 25, 35, 41, 44, 48, 52, 56, 61, 63, 66, 67, 69, 74, 76, 78, 80, 82, 84, 87, 89, 91, 93, 98, 101, 104, 106, 110, 113, 117, 121, 127, 139, 156}; @@ -46,27 +50,38 @@ TEST(leiden_karate, success) std::vector cluster_id(num_verts, -1); - rmm::device_vector offsets_v(off_h); - rmm::device_vector indices_v(ind_h); - rmm::device_vector weights_v(w_h); - rmm::device_vector result_v(cluster_id); + rmm::device_uvector offsets_v(num_verts + 1, stream); + rmm::device_uvector indices_v(num_edges, stream); + rmm::device_uvector weights_v(num_edges, stream); + rmm::device_uvector result_v(num_verts, stream); - cugraph::GraphCSRView G( - offsets_v.data().get(), indices_v.data().get(), weights_v.data().get(), num_verts, num_edges); + raft::update_device(offsets_v.data(), off_h.data(), off_h.size(), stream); + raft::update_device(indices_v.data(), ind_h.data(), ind_h.size(), stream); + raft::update_device(weights_v.data(), w_h.data(), w_h.size(), stream); + + cugraph::legacy::GraphCSRView G( + offsets_v.data(), indices_v.data(), weights_v.data(), num_verts, num_edges); float modularity{0.0}; size_t num_level = 40; - raft::handle_t handle; - std::tie(num_level, modularity) = cugraph::leiden(handle, G, result_v.data().get()); + // "FIXME": remove this check once we drop support for Pascal + // + // Calling louvain on Pascal will throw an exception, we'll check that + // this is the behavior while we still support Pascal (device_prop.major < 7) + // + if (handle.get_device_properties().major < 7) { + EXPECT_THROW(cugraph::leiden(handle, G, result_v.data()), cugraph::logic_error); + } else { + std::tie(num_level, modularity) = cugraph::leiden(handle, G, result_v.data()); + + raft::update_host(cluster_id.data(), result_v.data(), num_verts, stream); - cudaMemcpy((void*)&(cluster_id[0]), - result_v.data().get(), - sizeof(int) * num_verts, - cudaMemcpyDeviceToHost); + CUDA_TRY(cudaDeviceSynchronize()); - int min = *min_element(cluster_id.begin(), cluster_id.end()); + int min = *min_element(cluster_id.begin(), cluster_id.end()); - ASSERT_GE(min, 0); - ASSERT_GE(modularity, 0.41116042 * 0.99); + ASSERT_GE(min, 0); + ASSERT_GE(modularity, 0.41116042 * 0.99); + } } diff --git a/cpp/tests/community/louvain_test.cpp b/cpp/tests/community/louvain_test.cpp new file mode 100644 index 00000000000..4dca720483e --- /dev/null +++ b/cpp/tests/community/louvain_test.cpp @@ -0,0 +1,326 @@ +/* + * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. + * + * NVIDIA CORPORATION and its licensors retain all intellectual property + * and proprietary rights in and to this software, related documentation + * and any modifications thereto. Any use, reproduction, disclosure or + * distribution of this software and related documentation without an express + * license agreement from NVIDIA CORPORATION is strictly prohibited. + * + */ +#include +#include + +#include +#include +#include +#include + +#include + +#include + +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +struct Louvain_Usecase { + std::string graph_file_full_path_{}; + bool test_weighted_{false}; + int expected_level_{0}; + float expected_modularity_{0}; + + Louvain_Usecase(std::string const& graph_file_path, + bool test_weighted, + int expected_level, + float expected_modularity) + : test_weighted_(test_weighted), + expected_level_(expected_level), + expected_modularity_(expected_modularity) + { + if ((graph_file_path.length() > 0) && (graph_file_path[0] != '/')) { + graph_file_full_path_ = cugraph::test::get_rapids_dataset_root_dir() + "/" + graph_file_path; + } else { + graph_file_full_path_ = graph_file_path; + } + }; +}; + +class Tests_Louvain : public ::testing::TestWithParam { + public: + Tests_Louvain() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + + virtual void SetUp() {} + virtual void TearDown() {} + + template + void run_legacy_test(Louvain_Usecase const& configuration) + { + raft::handle_t handle{}; + + bool directed{false}; + + auto graph = cugraph::test::generate_graph_csr_from_mm( + directed, configuration.graph_file_full_path_); + auto graph_view = graph->view(); + + // "FIXME": remove this check once we drop support for Pascal + // + // Calling louvain on Pascal will throw an exception, we'll check that + // this is the behavior while we still support Pascal (device_prop.major < 7) + // + cudaDeviceProp device_prop; + CUDA_CHECK(cudaGetDeviceProperties(&device_prop, 0)); + + if (device_prop.major < 7) { + EXPECT_THROW(louvain(graph_view, + graph_view.get_number_of_vertices(), + configuration.expected_level_, + configuration.expected_modularity_), + cugraph::logic_error); + } else { + louvain(graph_view, + graph_view.get_number_of_vertices(), + configuration.expected_level_, + configuration.expected_modularity_); + } + } + + template + void run_current_test(Louvain_Usecase const& configuration) + { + raft::handle_t handle{}; + + cugraph::experimental::graph_t graph(handle); + std::tie(graph, std::ignore) = + cugraph::test::read_graph_from_matrix_market_file( + handle, configuration.graph_file_full_path_, configuration.test_weighted_, false); + + auto graph_view = graph.view(); + + // "FIXME": remove this check once we drop support for Pascal + // + // Calling louvain on Pascal will throw an exception, we'll check that + // this is the behavior while we still support Pascal (device_prop.major < 7) + // + cudaDeviceProp device_prop; + CUDA_CHECK(cudaGetDeviceProperties(&device_prop, 0)); + + if (device_prop.major < 7) { + EXPECT_THROW(louvain(graph_view, + graph_view.get_number_of_local_vertices(), + configuration.expected_level_, + configuration.expected_modularity_), + cugraph::logic_error); + } else { + louvain(graph_view, + graph_view.get_number_of_local_vertices(), + configuration.expected_level_, + configuration.expected_modularity_); + } + } + + template + void louvain(graph_t const& graph_view, + typename graph_t::vertex_type num_vertices, + int expected_level, + float expected_modularity) + { + using vertex_t = typename graph_t::vertex_type; + using weight_t = typename graph_t::weight_type; + + raft::handle_t handle{}; + + rmm::device_uvector clustering_v(num_vertices, handle.get_stream()); + size_t level; + weight_t modularity; + + std::tie(level, modularity) = + cugraph::louvain(handle, graph_view, clustering_v.data(), size_t{100}, weight_t{1}); + + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + + float compare_modularity = static_cast(modularity); + + ASSERT_FLOAT_EQ(compare_modularity, expected_modularity); + ASSERT_EQ(level, expected_level); + } +}; + +// FIXME: add tests for type combinations + +TEST(louvain_legacy, success) +{ + raft::handle_t handle; + + auto stream = handle.get_stream(); + + std::vector off_h = {0, 16, 25, 35, 41, 44, 48, 52, 56, 61, 63, 66, + 67, 69, 74, 76, 78, 80, 82, 84, 87, 89, 91, 93, + 98, 101, 104, 106, 110, 113, 117, 121, 127, 139, 156}; + std::vector ind_h = { + 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 17, 19, 21, 31, 0, 2, 3, 7, 13, 17, 19, + 21, 30, 0, 1, 3, 7, 8, 9, 13, 27, 28, 32, 0, 1, 2, 7, 12, 13, 0, 6, 10, 0, 6, + 10, 16, 0, 4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.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, + 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; + + std::vector result_h = {1, 1, 1, 1, 0, 0, 0, 1, 3, 1, 0, 1, 1, 1, 3, 3, 0, + 1, 3, 1, 3, 1, 3, 2, 2, 2, 3, 2, 1, 3, 3, 2, 3, 3}; + + int num_verts = off_h.size() - 1; + int num_edges = ind_h.size(); + + std::vector cluster_id(num_verts, -1); + + rmm::device_uvector offsets_v(num_verts + 1, stream); + rmm::device_uvector indices_v(num_edges, stream); + rmm::device_uvector weights_v(num_edges, stream); + rmm::device_uvector result_v(num_verts, stream); + + raft::update_device(offsets_v.data(), off_h.data(), off_h.size(), stream); + raft::update_device(indices_v.data(), ind_h.data(), ind_h.size(), stream); + raft::update_device(weights_v.data(), w_h.data(), w_h.size(), stream); + + cugraph::legacy::GraphCSRView G( + offsets_v.data(), indices_v.data(), weights_v.data(), num_verts, num_edges); + + float modularity{0.0}; + size_t num_level = 40; + + // "FIXME": remove this check once we drop support for Pascal + // + // Calling louvain on Pascal will throw an exception, we'll check that + // this is the behavior while we still support Pascal (device_prop.major < 7) + // + if (handle.get_device_properties().major < 7) { + EXPECT_THROW(cugraph::louvain(handle, G, result_v.data()), cugraph::logic_error); + } else { + std::tie(num_level, modularity) = cugraph::louvain(handle, G, result_v.data()); + + raft::update_host(cluster_id.data(), result_v.data(), num_verts, stream); + + CUDA_TRY(cudaDeviceSynchronize()); + + int min = *min_element(cluster_id.begin(), cluster_id.end()); + + ASSERT_GE(min, 0); + ASSERT_FLOAT_EQ(modularity, 0.408695); + ASSERT_EQ(cluster_id, result_h); + } +} + +TEST(louvain_legacy_renumbered, success) +{ + raft::handle_t handle; + + auto stream = handle.get_stream(); + + std::vector off_h = {0, 16, 25, 30, 34, 38, 42, 44, 46, 48, 50, 52, + 54, 56, 73, 85, 95, 101, 107, 112, 117, 121, 125, 129, + 132, 135, 138, 141, 144, 147, 149, 151, 153, 155, 156}; + std::vector ind_h = { + 1, 3, 7, 11, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 30, 33, 0, 5, 11, 15, 16, 19, 21, + 25, 30, 4, 13, 14, 22, 27, 0, 9, 20, 24, 2, 13, 15, 26, 1, 13, 14, 18, 13, 15, 0, 16, + 13, 14, 3, 20, 13, 14, 0, 1, 13, 22, 2, 4, 5, 6, 8, 10, 12, 14, 17, 18, 19, 22, 25, + 28, 29, 31, 32, 2, 5, 8, 10, 13, 15, 17, 18, 22, 29, 31, 32, 0, 1, 4, 6, 14, 16, 18, + 19, 21, 28, 0, 1, 7, 15, 19, 21, 0, 13, 14, 26, 27, 28, 0, 5, 13, 14, 15, 0, 1, 13, + 16, 16, 0, 3, 9, 23, 0, 1, 15, 16, 2, 12, 13, 14, 0, 20, 24, 0, 3, 23, 0, 1, 13, + 4, 17, 27, 2, 17, 26, 13, 15, 17, 13, 14, 0, 1, 13, 14, 13, 14, 0}; + + std::vector w_h = { + 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, + 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, + 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, + 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, + 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, + 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, + 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, + 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, + 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; + + int num_verts = off_h.size() - 1; + int num_edges = ind_h.size(); + + std::vector cluster_id(num_verts, -1); + + rmm::device_uvector offsets_v(num_verts + 1, stream); + rmm::device_uvector indices_v(num_edges, stream); + rmm::device_uvector weights_v(num_edges, stream); + rmm::device_uvector result_v(num_verts, stream); + + raft::update_device(offsets_v.data(), off_h.data(), off_h.size(), stream); + raft::update_device(indices_v.data(), ind_h.data(), ind_h.size(), stream); + raft::update_device(weights_v.data(), w_h.data(), w_h.size(), stream); + + cugraph::legacy::GraphCSRView G( + offsets_v.data(), indices_v.data(), weights_v.data(), num_verts, num_edges); + + float modularity{0.0}; + size_t num_level = 40; + + // "FIXME": remove this check once we drop support for Pascal + // + // Calling louvain on Pascal will throw an exception, we'll check that + // this is the behavior while we still support Pascal (device_prop.major < 7) + // + if (handle.get_device_properties().major < 7) { + EXPECT_THROW(cugraph::louvain(handle, G, result_v.data()), cugraph::logic_error); + } else { + std::tie(num_level, modularity) = cugraph::louvain(handle, G, result_v.data()); + + raft::update_host(cluster_id.data(), result_v.data(), num_verts, stream); + + CUDA_TRY(cudaDeviceSynchronize()); + + int min = *min_element(cluster_id.begin(), cluster_id.end()); + + ASSERT_GE(min, 0); + ASSERT_FLOAT_EQ(modularity, 0.41880345); + } +} + +TEST_P(Tests_Louvain, CheckInt32Int32FloatFloatLegacy) +{ + run_legacy_test(GetParam()); +} + +TEST_P(Tests_Louvain, CheckInt32Int32FloatFloat) +{ + run_current_test(GetParam()); +} + +TEST_P(Tests_Louvain, CheckInt64Int64FloatFloat) +{ + run_current_test(GetParam()); +} + +// FIXME: Expand testing once we evaluate RMM memory use +INSTANTIATE_TEST_SUITE_P( + simple_test, + Tests_Louvain, + ::testing::Values(Louvain_Usecase("test/datasets/karate.mtx", true, 3, 0.408695))); + +CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/community/louvain_test.cu b/cpp/tests/community/louvain_test.cu deleted file mode 100644 index 2bac0097212..00000000000 --- a/cpp/tests/community/louvain_test.cu +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved. - * - * NVIDIA CORPORATION and its licensors retain all intellectual property - * and proprietary rights in and to this software, related documentation - * and any modifications thereto. Any use, reproduction, disclosure or - * distribution of this software and related documentation without an express - * license agreement from NVIDIA CORPORATION is strictly prohibited. - * - */ -#include - -#include -#include - -#include - -#include - -TEST(louvain, success) -{ - std::vector off_h = {0, 16, 25, 35, 41, 44, 48, 52, 56, 61, 63, 66, - 67, 69, 74, 76, 78, 80, 82, 84, 87, 89, 91, 93, - 98, 101, 104, 106, 110, 113, 117, 121, 127, 139, 156}; - std::vector ind_h = { - 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 17, 19, 21, 31, 0, 2, 3, 7, 13, 17, 19, - 21, 30, 0, 1, 3, 7, 8, 9, 13, 27, 28, 32, 0, 1, 2, 7, 12, 13, 0, 6, 10, 0, 6, - 10, 16, 0, 4, 5, 16, 0, 1, 2, 3, 0, 2, 30, 32, 33, 2, 33, 0, 4, 5, 0, 0, 3, - 0, 1, 2, 3, 33, 32, 33, 32, 33, 5, 6, 0, 1, 32, 33, 0, 1, 33, 32, 33, 0, 1, 32, - 33, 25, 27, 29, 32, 33, 25, 27, 31, 23, 24, 31, 29, 33, 2, 23, 24, 33, 2, 31, 33, 23, 26, - 32, 33, 1, 8, 32, 33, 0, 24, 25, 28, 32, 33, 2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ges = ind_h.size(); - - std::vector cluster_id(num_verts, -1); - - rmm::device_vector offsets_v(off_h); - rmm::device_vector indices_v(ind_h); - rmm::device_vector weights_v(w_h); - rmm::device_vector result_v(cluster_id); - - cugraph::GraphCSRView G( - offsets_v.data().get(), indices_v.data().get(), weights_v.data().get(), num_verts, num_edges); - - float modularity{0.0}; - size_t num_level = 40; - - raft::handle_t handle; - - std::tie(num_level, modularity) = cugraph::louvain(handle, G, result_v.data().get()); - - cudaMemcpy((void*)&(cluster_id[0]), - result_v.data().get(), - sizeof(int) * num_verts, - cudaMemcpyDeviceToHost); - - int min = *min_element(cluster_id.begin(), cluster_id.end()); - - std::cout << "modularity = " << modularity << std::endl; - - ASSERT_GE(min, 0); - ASSERT_GE(modularity, 0.402777 * 0.95); - ASSERT_EQ(result_v, result_h); -} - -TEST(louvain_renumbered, success) -{ - std::vector off_h = {0, 16, 25, 30, 34, 38, 42, 44, 46, 48, 50, 52, - 54, 56, 73, 85, 95, 101, 107, 112, 117, 121, 125, 129, - 132, 135, 138, 141, 144, 147, 149, 151, 153, 155, 156 - - }; - std::vector ind_h = { - 1, 3, 7, 11, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 30, 33, 0, 5, 11, 15, 16, 19, 21, - 25, 30, 4, 13, 14, 22, 27, 0, 9, 20, 24, 2, 13, 15, 26, 1, 13, 14, 18, 13, 15, 0, 16, - 13, 14, 3, 20, 13, 14, 0, 1, 13, 22, 2, 4, 5, 6, 8, 10, 12, 14, 17, 18, 19, 22, 25, - 28, 29, 31, 32, 2, 5, 8, 10, 13, 15, 17, 18, 22, 29, 31, 32, 0, 1, 4, 6, 14, 16, 18, - 19, 21, 28, 0, 1, 7, 15, 19, 21, 0, 13, 14, 26, 27, 28, 0, 5, 13, 14, 15, 0, 1, 13, - 16, 16, 0, 3, 9, 23, 0, 1, 15, 16, 2, 12, 13, 14, 0, 20, 24, 0, 3, 23, 0, 1, 13, - 4, 17, 27, 2, 17, 26, 13, 15, 17, 13, 14, 0, 1, 13, 14, 13, 14, 0}; - - std::vector w_h = { - 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, - 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, - 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, - 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, - 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, - 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, - 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, - 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, - 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; - - int num_verts = off_h.size() - 1; - int num_edges = ind_h.size(); - - std::vector cluster_id(num_verts, -1); - - rmm::device_vector offsets_v(off_h); - rmm::device_vector indices_v(ind_h); - rmm::device_vector weights_v(w_h); - rmm::device_vector result_v(cluster_id); - - cugraph::GraphCSRView G( - offsets_v.data().get(), indices_v.data().get(), weights_v.data().get(), num_verts, num_edges); - - float modularity{0.0}; - size_t num_level = 40; - - raft::handle_t handle; - - std::tie(num_level, modularity) = cugraph::louvain(handle, G, result_v.data().get()); - - cudaMemcpy((void*)&(cluster_id[0]), - result_v.data().get(), - sizeof(int) * num_verts, - cudaMemcpyDeviceToHost); - - int min = *min_element(cluster_id.begin(), cluster_id.end()); - - std::cout << "modularity = " << modularity << std::endl; - - ASSERT_GE(min, 0); - ASSERT_GE(modularity, 0.402777 * 0.95); -} - -CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/community/mg_louvain_helper.cu b/cpp/tests/community/mg_louvain_helper.cu new file mode 100644 index 00000000000..b5347778b4c --- /dev/null +++ b/cpp/tests/community/mg_louvain_helper.cu @@ -0,0 +1,281 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mg_louvain_helper.hpp" + +#include + +#include +#include +#include + +#include + +#include +#include +#include + +namespace cugraph { +namespace test { + +template +void single_gpu_renumber_edgelist_given_number_map(raft::handle_t const& handle, + rmm::device_uvector& edgelist_rows_v, + rmm::device_uvector& edgelist_cols_v, + rmm::device_uvector& renumber_map_gathered_v) +{ + rmm::device_uvector index_v(renumber_map_gathered_v.size(), handle.get_stream()); + + thrust::for_each( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + thrust::make_counting_iterator(0), + thrust::make_counting_iterator(renumber_map_gathered_v.size()), + [d_renumber_map_gathered = renumber_map_gathered_v.data(), d_index = index_v.data()] __device__( + auto idx) { d_index[d_renumber_map_gathered[idx]] = idx; }); + + thrust::transform(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + edgelist_rows_v.begin(), + edgelist_rows_v.end(), + edgelist_rows_v.begin(), + [d_index = index_v.data()] __device__(auto v) { return d_index[v]; }); + + thrust::transform(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + edgelist_cols_v.begin(), + edgelist_cols_v.end(), + edgelist_cols_v.begin(), + [d_index = index_v.data()] __device__(auto v) { return d_index[v]; }); +} + +template +std::tuple, + rmm::device_uvector, + std::optional>> +compressed_sparse_to_edgelist(edge_t const* compressed_sparse_offsets, + vertex_t const* compressed_sparse_indices, + std::optional compressed_sparse_weights, + vertex_t major_first, + vertex_t major_last, + cudaStream_t stream) +{ + edge_t number_of_edges{0}; + raft::update_host( + &number_of_edges, compressed_sparse_offsets + (major_last - major_first), 1, stream); + CUDA_TRY(cudaStreamSynchronize(stream)); + rmm::device_uvector edgelist_major_vertices(number_of_edges, stream); + rmm::device_uvector edgelist_minor_vertices(number_of_edges, stream); + auto edgelist_weights = + compressed_sparse_weights + ? std::make_optional>(number_of_edges, stream) + : std::nullopt; + + // FIXME: this is highly inefficient for very high-degree vertices, for better performance, we can + // fill high-degree vertices using one CUDA block per vertex, mid-degree vertices using one CUDA + // warp per vertex, and low-degree vertices using one CUDA thread per block + thrust::for_each(rmm::exec_policy(stream)->on(stream), + thrust::make_counting_iterator(major_first), + thrust::make_counting_iterator(major_last), + [compressed_sparse_offsets, + major_first, + p_majors = edgelist_major_vertices.begin()] __device__(auto v) { + auto first = compressed_sparse_offsets[v - major_first]; + auto last = compressed_sparse_offsets[v - major_first + 1]; + thrust::fill(thrust::seq, p_majors + first, p_majors + last, v); + }); + thrust::copy(rmm::exec_policy(stream)->on(stream), + compressed_sparse_indices, + compressed_sparse_indices + number_of_edges, + edgelist_minor_vertices.begin()); + if (compressed_sparse_weights) { + thrust::copy(rmm::exec_policy(stream)->on(stream), + (*compressed_sparse_weights), + (*compressed_sparse_weights) + number_of_edges, + (*edgelist_weights).data()); + } + + return std::make_tuple(std::move(edgelist_major_vertices), + std::move(edgelist_minor_vertices), + std::move(edgelist_weights)); +} + +template +void sort_and_coarsen_edgelist( + rmm::device_uvector& edgelist_major_vertices /* [INOUT] */, + rmm::device_uvector& edgelist_minor_vertices /* [INOUT] */, + std::optional>& edgelist_weights /* [INOUT] */, + cudaStream_t stream) +{ + auto pair_first = thrust::make_zip_iterator( + thrust::make_tuple(edgelist_major_vertices.begin(), edgelist_minor_vertices.begin())); + + size_t number_of_edges{0}; + if (edgelist_weights) { + thrust::sort_by_key(rmm::exec_policy(stream)->on(stream), + pair_first, + pair_first + edgelist_major_vertices.size(), + (*edgelist_weights).begin()); + + rmm::device_uvector tmp_edgelist_major_vertices(edgelist_major_vertices.size(), + stream); + rmm::device_uvector tmp_edgelist_minor_vertices(tmp_edgelist_major_vertices.size(), + stream); + rmm::device_uvector tmp_edgelist_weights(tmp_edgelist_major_vertices.size(), stream); + auto it = thrust::reduce_by_key( + rmm::exec_policy(stream)->on(stream), + pair_first, + pair_first + edgelist_major_vertices.size(), + (*edgelist_weights).begin(), + thrust::make_zip_iterator(thrust::make_tuple(tmp_edgelist_major_vertices.begin(), + tmp_edgelist_minor_vertices.begin())), + tmp_edgelist_weights.begin()); + number_of_edges = thrust::distance(tmp_edgelist_weights.begin(), thrust::get<1>(it)); + + edgelist_major_vertices = std::move(tmp_edgelist_major_vertices); + edgelist_minor_vertices = std::move(tmp_edgelist_minor_vertices); + (*edgelist_weights) = std::move(tmp_edgelist_weights); + } else { + thrust::sort(rmm::exec_policy(stream)->on(stream), + pair_first, + pair_first + edgelist_major_vertices.size()); + auto it = thrust::unique(rmm::exec_policy(stream)->on(stream), + pair_first, + pair_first + edgelist_major_vertices.size()); + number_of_edges = thrust::distance(pair_first, it); + } + + edgelist_major_vertices.resize(number_of_edges, stream); + edgelist_minor_vertices.resize(number_of_edges, stream); + edgelist_major_vertices.shrink_to_fit(stream); + edgelist_minor_vertices.shrink_to_fit(stream); + if (edgelist_weights) { + (*edgelist_weights).resize(number_of_edges, stream); + (*edgelist_weights).shrink_to_fit(stream); + } +} + +template +std::tuple, + rmm::device_uvector, + std::optional>> +compressed_sparse_to_relabeled_and_sorted_and_coarsened_edgelist( + edge_t const* compressed_sparse_offsets, + vertex_t const* compressed_sparse_indices, + std::optional compressed_sparse_weights, + vertex_t const* p_major_labels, + vertex_t const* p_minor_labels, + vertex_t major_first, + vertex_t major_last, + vertex_t minor_first, + vertex_t minor_last, + cudaStream_t stream) +{ + // FIXME: it might be possible to directly create relabled & coarsened edgelist from the + // compressed sparse format to save memory + + auto [edgelist_major_vertices, edgelist_minor_vertices, edgelist_weights] = + compressed_sparse_to_edgelist(compressed_sparse_offsets, + compressed_sparse_indices, + compressed_sparse_weights, + major_first, + major_last, + stream); + + auto pair_first = thrust::make_zip_iterator( + thrust::make_tuple(edgelist_major_vertices.begin(), edgelist_minor_vertices.begin())); + thrust::transform( + rmm::exec_policy(stream)->on(stream), + pair_first, + pair_first + edgelist_major_vertices.size(), + pair_first, + [p_major_labels, p_minor_labels, major_first, minor_first] __device__(auto val) { + return thrust::make_tuple(p_major_labels[thrust::get<0>(val) - major_first], + p_minor_labels[thrust::get<1>(val) - minor_first]); + }); + + sort_and_coarsen_edgelist( + edgelist_major_vertices, edgelist_minor_vertices, edgelist_weights, stream); + + return std::make_tuple(std::move(edgelist_major_vertices), + std::move(edgelist_minor_vertices), + std::move(edgelist_weights)); +} + +// FIXME: better add "bool renumber" (which must be false in MG) to the coarsen_grpah function +// instead of replicating the code here. single-GPU version +template +std::unique_ptr> +coarsen_graph( + raft::handle_t const& handle, + cugraph::experimental::graph_view_t const& + graph_view, + vertex_t const* labels) +{ + auto [coarsened_edgelist_major_vertices, + coarsened_edgelist_minor_vertices, + coarsened_edgelist_weights] = + compressed_sparse_to_relabeled_and_sorted_and_coarsened_edgelist( + graph_view.get_matrix_partition_view().get_offsets(), + graph_view.get_matrix_partition_view().get_indices(), + graph_view.get_matrix_partition_view().get_weights(), + labels, + labels, + vertex_t{0}, + graph_view.get_number_of_vertices(), + vertex_t{0}, + graph_view.get_number_of_vertices(), + handle.get_stream()); + + cugraph::experimental::edgelist_t edgelist{}; + edgelist.p_src_vertices = store_transposed ? coarsened_edgelist_minor_vertices.data() + : coarsened_edgelist_major_vertices.data(); + edgelist.p_dst_vertices = store_transposed ? coarsened_edgelist_major_vertices.data() + : coarsened_edgelist_minor_vertices.data(); + edgelist.p_edge_weights = coarsened_edgelist_weights + ? std::optional{(*coarsened_edgelist_weights).data()} + : std::nullopt; + edgelist.number_of_edges = static_cast(coarsened_edgelist_major_vertices.size()); + + vertex_t new_number_of_vertices = + 1 + thrust::reduce(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + labels, + labels + graph_view.get_number_of_vertices(), + vertex_t{0}, + thrust::maximum()); + + return std::make_unique< + cugraph::experimental::graph_t>( + handle, + edgelist, + new_number_of_vertices, + cugraph::experimental::graph_properties_t{graph_view.is_symmetric(), false}, + std::nullopt); +} + +// explicit instantiation + +template void single_gpu_renumber_edgelist_given_number_map( + raft::handle_t const& handle, + rmm::device_uvector& d_edgelist_rows, + rmm::device_uvector& d_edgelist_cols, + rmm::device_uvector& d_renumber_map_gathered_v); + +template std::unique_ptr> +coarsen_graph( + raft::handle_t const& handle, + cugraph::experimental::graph_view_t const& graph_view, + int32_t const* labels); + +} // namespace test +} // namespace cugraph diff --git a/cpp/tests/community/mg_louvain_helper.hpp b/cpp/tests/community/mg_louvain_helper.hpp new file mode 100644 index 00000000000..7e03f435724 --- /dev/null +++ b/cpp/tests/community/mg_louvain_helper.hpp @@ -0,0 +1,53 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +#include +#include + +namespace cugraph { +namespace test { + +template +bool compare_renumbered_vectors(raft::handle_t const& handle, + std::vector const& v1, + std::vector const& v2); + +template +bool compare_renumbered_vectors(raft::handle_t const& handle, + rmm::device_uvector const& v1, + rmm::device_uvector const& v2); + +template +void single_gpu_renumber_edgelist_given_number_map( + raft::handle_t const& handle, + rmm::device_uvector& d_edgelist_rows, + rmm::device_uvector& d_edgelist_cols, + rmm::device_uvector& d_renumber_map_gathered_v); + +template +std::unique_ptr> +coarsen_graph( + raft::handle_t const& handle, + cugraph::experimental::graph_view_t const& + graph_view, + vertex_t const* labels); + +} // namespace test +} // namespace cugraph diff --git a/cpp/tests/community/mg_louvain_test.cpp b/cpp/tests/community/mg_louvain_test.cpp new file mode 100644 index 00000000000..1f7276fa116 --- /dev/null +++ b/cpp/tests/community/mg_louvain_test.cpp @@ -0,0 +1,234 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mg_louvain_helper.hpp" + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include + +void compare(float mg_modularity, float sg_modularity) +{ + ASSERT_FLOAT_EQ(mg_modularity, sg_modularity); +} +void compare(double mg_modularity, double sg_modularity) +{ + ASSERT_DOUBLE_EQ(mg_modularity, sg_modularity); +} + +//////////////////////////////////////////////////////////////////////////////// +// Test param object. This defines the input and expected output for a test, and +// will be instantiated as the parameter to the tests defined below using +// INSTANTIATE_TEST_SUITE_P() +// +struct Louvain_Usecase { + std::string graph_file_full_path{}; + bool weighted{false}; + size_t max_level; + double resolution; + + // FIXME: We really should have a Graph_Testparms_Base class or something + // like that which can handle this graph_full_path thing. + // + Louvain_Usecase(std::string const& graph_file_path, + bool weighted, + size_t max_level, + double resolution) + : weighted(weighted), max_level(max_level), resolution(resolution) + { + if ((graph_file_path.length() > 0) && (graph_file_path[0] != '/')) { + graph_file_full_path = cugraph::test::get_rapids_dataset_root_dir() + "/" + graph_file_path; + } else { + graph_file_full_path = graph_file_path; + } + }; +}; + +//////////////////////////////////////////////////////////////////////////////// +// Parameterized test fixture, to be used with TEST_P(). This defines common +// setup and teardown steps as well as common utilities used by each E2E MG +// test. In this case, each test is identical except for the inputs and +// expected outputs, so the entire test is defined in the run_test() method. +// +class Louvain_MG_Testfixture : public ::testing::TestWithParam { + public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + + // Run once for each test instance + virtual void SetUp() {} + virtual void TearDown() {} + + // Compare the results of MNMG Louvain with the results of running + // each step of SG Louvain, renumbering the coarsened graphs based + // on the MNMG renumbering. + template + void compare_sg_results(raft::handle_t const& handle, + std::string const& graph_filename, + rmm::device_uvector& d_renumber_map_gathered_v, + cugraph::Dendrogram const& dendrogram, + weight_t resolution, + int rank, + weight_t mg_modularity) + { + auto sg_graph = + std::make_unique>( + handle); + rmm::device_uvector d_clustering_v(0, handle.get_stream()); + weight_t sg_modularity{-1.0}; + + if (rank == 0) { + // Create initial SG graph, renumbered according to the MNMG renumber map + + auto [d_edgelist_rows, + d_edgelist_cols, + d_edgelist_weights, + number_of_vertices, + is_symmetric] = + cugraph::test::read_edgelist_from_matrix_market_file( + handle, graph_filename, true); + + rmm::device_uvector d_vertices(number_of_vertices, handle.get_stream()); + std::vector h_vertices(number_of_vertices); + + d_clustering_v.resize(d_vertices.size(), handle.get_stream()); + + thrust::sequence(thrust::host, h_vertices.begin(), h_vertices.end(), vertex_t{0}); + raft::update_device( + d_vertices.data(), h_vertices.data(), d_vertices.size(), handle.get_stream()); + + // renumber using d_renumber_map_gathered_v + cugraph::test::single_gpu_renumber_edgelist_given_number_map( + handle, d_edgelist_rows, d_edgelist_cols, d_renumber_map_gathered_v); + + std::tie(*sg_graph, std::ignore) = + cugraph::experimental::create_graph_from_edgelist( + handle, + std::optional>{ + std::make_tuple(d_vertices.data(), static_cast(d_vertices.size()))}, + std::move(d_edgelist_rows), + std::move(d_edgelist_cols), + std::move(d_edgelist_weights), + cugraph::experimental::graph_properties_t{is_symmetric, false}, + false); + } + + std::for_each( + thrust::make_counting_iterator(0), + thrust::make_counting_iterator(dendrogram.num_levels()), + [&dendrogram, &sg_graph, &d_clustering_v, &sg_modularity, &handle, resolution, rank]( + size_t i) { + auto d_dendrogram_gathered_v = cugraph::test::device_gatherv( + handle, dendrogram.get_level_ptr_nocheck(i), dendrogram.get_level_size_nocheck(i)); + + if (rank == 0) { + auto graph_view = sg_graph->view(); + + d_clustering_v.resize(graph_view.get_number_of_vertices(), handle.get_stream()); + + std::tie(std::ignore, sg_modularity) = + cugraph::louvain(handle, graph_view, d_clustering_v.data(), size_t{1}, resolution); + + EXPECT_TRUE(cugraph::test::renumbered_vectors_same( + handle, d_clustering_v, d_dendrogram_gathered_v)); + + sg_graph = + cugraph::test::coarsen_graph(handle, graph_view, d_dendrogram_gathered_v.data()); + } + }); + + if (rank == 0) compare(mg_modularity, sg_modularity); + } + + // Compare the results of running louvain on multiple GPUs to that of a + // single-GPU run for the configuration in param. Note that MNMG Louvain + // and single GPU Louvain are ONLY deterministic through a single + // iteration of the outer loop. Renumbering of the partitions when coarsening + // the graph is a function of the number of GPUs in the GPU cluster. + template + void run_test(const Louvain_Usecase& param) + { + raft::handle_t handle; + + raft::comms::initialize_mpi_comms(&handle, MPI_COMM_WORLD); + const auto& comm = handle.get_comms(); + + auto const comm_size = comm.get_size(); + auto const comm_rank = comm.get_rank(); + + auto row_comm_size = static_cast(sqrt(static_cast(comm_size))); + while (comm_size % row_comm_size != 0) { + --row_comm_size; + } + cugraph::partition_2d::subcomm_factory_t + subcomm_factory(handle, row_comm_size); + + cudaStream_t stream = handle.get_stream(); + + auto [mg_graph, d_renumber_map_labels] = + cugraph::test::read_graph_from_matrix_market_file( + handle, param.graph_file_full_path, true, true); + + auto mg_graph_view = mg_graph.view(); + + std::unique_ptr> dendrogram; + weight_t mg_modularity; + + std::tie(dendrogram, mg_modularity) = + cugraph::louvain(handle, mg_graph_view, param.max_level, param.resolution); + + SCOPED_TRACE("compare modularity input: " + param.graph_file_full_path); + + auto d_renumber_map_gathered_v = cugraph::test::device_gatherv( + handle, (*d_renumber_map_labels).data(), (*d_renumber_map_labels).size()); + + compare_sg_results(handle, + param.graph_file_full_path, + d_renumber_map_gathered_v, + *dendrogram, + param.resolution, + comm_rank, + mg_modularity); + } +}; + +//////////////////////////////////////////////////////////////////////////////// +TEST_P(Louvain_MG_Testfixture, CheckInt32Int32Float) +{ + run_test(GetParam()); +} + +INSTANTIATE_TEST_SUITE_P( + simple_test, + Louvain_MG_Testfixture, + ::testing::Values(Louvain_Usecase("test/datasets/karate.mtx", true, 100, 1) + //,Louvain_Usecase("test/datasets/smallworld.mtx", true, 100, 1) + )); + +CUGRAPH_MG_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/community/triangle_test.cu b/cpp/tests/community/triangle_test.cu index 1c5c99261d2..4c51e15b111 100644 --- a/cpp/tests/community/triangle_test.cu +++ b/cpp/tests/community/triangle_test.cu @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. * * NVIDIA CORPORATION and its licensors retain all intellectual property * and proprietary rights in and to this software, related documentation @@ -10,8 +10,8 @@ */ #include -#include -#include +#include +#include #include @@ -49,7 +49,7 @@ TEST(triangle, dolphin) rmm::device_vector indices_v(ind_h); rmm::device_vector weights_v(w_h); - cugraph::GraphCSRView graph_csr( + cugraph::legacy::GraphCSRView graph_csr( offsets_v.data().get(), indices_v.data().get(), weights_v.data().get(), num_verts, num_edges); uint64_t count{0}; @@ -63,4 +63,47 @@ TEST(triangle, dolphin) ASSERT_EQ(count, expected); } +TEST(triangle, karate) +{ + using vertex_t = int32_t; + using edge_t = int32_t; + using weight_t = float; + + std::vector off_h = {0, 16, 25, 35, 41, 44, 48, 52, 56, 61, 63, 66, + 67, 69, 74, 76, 78, 80, 82, 84, 87, 89, 91, 93, + 98, 101, 104, 106, 110, 113, 117, 121, 127, 139, 156}; + std::vector ind_h = { + 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 17, 19, 21, 31, 0, 2, 3, 7, 13, 17, 19, + 21, 30, 0, 1, 3, 7, 8, 9, 13, 27, 28, 32, 0, 1, 2, 7, 12, 13, 0, 6, 10, 0, 6, + 10, 16, 0, 4, 5, 16, 0, 1, 2, 3, 0, 2, 30, 32, 33, 2, 33, 0, 4, 5, 0, 0, 3, + 0, 1, 2, 3, 33, 32, 33, 32, 33, 5, 6, 0, 1, 32, 33, 0, 1, 33, 32, 33, 0, 1, 32, + 33, 25, 27, 29, 32, 33, 25, 27, 31, 23, 24, 31, 29, 33, 2, 23, 24, 33, 2, 31, 33, 23, 26, + 32, 33, 1, 8, 32, 33, 0, 24, 25, 28, 32, 33, 2, 8, 14, 15, 18, 20, 22, 23, 29, 30, 31, + 33, 8, 9, 13, 14, 15, 18, 19, 20, 22, 23, 26, 27, 28, 29, 30, 31, 32}; + + std::vector w_h(ind_h.size(), weight_t{1.0}); + + vertex_t num_verts = off_h.size() - 1; + int num_edges = ind_h.size(); + + uint64_t expected{135}; + + rmm::device_vector offsets_v(off_h); + rmm::device_vector indices_v(ind_h); + rmm::device_vector weights_v(w_h); + + cugraph::legacy::GraphCSRView graph_csr( + offsets_v.data().get(), indices_v.data().get(), weights_v.data().get(), num_verts, num_edges); + + uint64_t count{0}; + + try { + count = cugraph::triangle::triangle_count(graph_csr); + } catch (std::exception& e) { + std::cout << "Exception: " << e.what() << std::endl; + } + + ASSERT_EQ(count, expected); +} + CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/components/con_comp_test.cu b/cpp/tests/components/con_comp_test.cu index 15d60867753..2db7235c299 100644 --- a/cpp/tests/components/con_comp_test.cu +++ b/cpp/tests/components/con_comp_test.cu @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. * * NVIDIA CORPORATION and its licensors retain all intellectual property * and proprietary rights in and to this software, related documentation @@ -18,9 +18,9 @@ #include -#include #include -#include +#include +#include #include #include @@ -113,9 +113,10 @@ struct Tests_Weakly_CC : ::testing::TestWithParam { << "\n"; ASSERT_EQ(fclose(fpin), 0); - cugraph::GraphCOOView G_coo(&cooRowInd[0], &cooColInd[0], nullptr, m, nnz); - auto G_unique = cugraph::coo_to_csr(G_coo); - cugraph::GraphCSRView G = G_unique->view(); + cugraph::legacy::GraphCOOView G_coo( + &cooRowInd[0], &cooColInd[0], nullptr, m, nnz); + auto G_unique = cugraph::coo_to_csr(G_coo); + cugraph::legacy::GraphCSRView G = G_unique->view(); rmm::device_vector d_labels(m); @@ -141,11 +142,11 @@ std::vector Tests_Weakly_CC::weakly_cc_time; TEST_P(Tests_Weakly_CC, Weakly_CC) { run_current_test(GetParam()); } // --gtest_filter=*simple_test* -INSTANTIATE_TEST_CASE_P(simple_test, - Tests_Weakly_CC, - ::testing::Values(Usecase("test/datasets/dolphins.mtx"), - Usecase("test/datasets/coPapersDBLP.mtx"), - Usecase("test/datasets/coPapersCiteseer.mtx"), - Usecase("test/datasets/hollywood.mtx"))); +INSTANTIATE_TEST_SUITE_P(simple_test, + Tests_Weakly_CC, + ::testing::Values(Usecase("test/datasets/dolphins.mtx"), + Usecase("test/datasets/coPapersDBLP.mtx"), + Usecase("test/datasets/coPapersCiteseer.mtx"), + Usecase("test/datasets/hollywood.mtx"))); CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/components/mg_weakly_connected_components_test.cpp b/cpp/tests/components/mg_weakly_connected_components_test.cpp new file mode 100644 index 00000000000..11e3f803b38 --- /dev/null +++ b/cpp/tests/components/mg_weakly_connected_components_test.cpp @@ -0,0 +1,243 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +// do the perf measurements +// enabled by command line parameter s'--perf' +// +static int PERF = 0; + +struct WeaklyConnectedComponents_Usecase { + bool check_correctness{true}; +}; + +template +class Tests_MGWeaklyConnectedComponents + : public ::testing::TestWithParam< + std::tuple> { + public: + Tests_MGWeaklyConnectedComponents() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + + virtual void SetUp() {} + virtual void TearDown() {} + + // Compare the results of running weakly connected components on multiple GPUs to that of a + // single-GPU run + template + void run_current_test( + WeaklyConnectedComponents_Usecase const& weakly_connected_components_usecase, + input_usecase_t const& input_usecase) + { + using weight_t = float; + + // 1. initialize handle + + raft::handle_t handle{}; + HighResClock hr_clock{}; + + raft::comms::initialize_mpi_comms(&handle, MPI_COMM_WORLD); + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + auto const comm_rank = comm.get_rank(); + + auto row_comm_size = static_cast(sqrt(static_cast(comm_size))); + while (comm_size % row_comm_size != 0) { + --row_comm_size; + } + cugraph::partition_2d::subcomm_factory_t + subcomm_factory(handle, row_comm_size); + + // 2. create MG graph + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + hr_clock.start(); + } + + auto [mg_graph, d_mg_renumber_map_labels] = + input_usecase.template construct_graph( + handle, false, true); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "MG construct_graph took " << elapsed_time * 1e-6 << " s.\n"; + } + + auto mg_graph_view = mg_graph.view(); + + // 3. run MG weakly connected components + + rmm::device_uvector d_mg_components(mg_graph_view.get_number_of_local_vertices(), + handle.get_stream()); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + hr_clock.start(); + } + + cugraph::experimental::weakly_connected_components( + handle, mg_graph_view, d_mg_components.data()); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "MG weakly_connected_components took " << elapsed_time * 1e-6 << " s.\n"; + } + + // 4. compare SG & MG results + + if (weakly_connected_components_usecase.check_correctness) { + // 4-1. aggregate MG results + + auto d_mg_aggregate_renumber_map_labels = cugraph::test::device_gatherv( + handle, (*d_mg_renumber_map_labels).data(), (*d_mg_renumber_map_labels).size()); + auto d_mg_aggregate_components = + cugraph::test::device_gatherv(handle, d_mg_components.data(), d_mg_components.size()); + + if (handle.get_comms().get_rank() == int{0}) { + // 4-2. unrenumbr MG results + + std::tie(std::ignore, d_mg_aggregate_components) = + cugraph::test::sort_by_key(handle, + d_mg_aggregate_renumber_map_labels.data(), + d_mg_aggregate_components.data(), + d_mg_aggregate_renumber_map_labels.size()); + + // 4-3. create SG graph + + cugraph::experimental::graph_t sg_graph(handle); + std::tie(sg_graph, std::ignore) = + input_usecase.template construct_graph( + handle, false, false); + + auto sg_graph_view = sg_graph.view(); + + ASSERT_TRUE(mg_graph_view.get_number_of_vertices() == + sg_graph_view.get_number_of_vertices()); + + // 4-4. run SG weakly connected components + + rmm::device_uvector d_sg_components(sg_graph_view.get_number_of_vertices(), + handle.get_stream()); + + cugraph::experimental::weakly_connected_components( + handle, sg_graph_view, d_sg_components.data()); + + // 4-5. compare + + std::vector h_mg_aggregate_components(mg_graph_view.get_number_of_vertices()); + raft::update_host(h_mg_aggregate_components.data(), + d_mg_aggregate_components.data(), + d_mg_aggregate_components.size(), + handle.get_stream()); + + std::vector h_sg_components(sg_graph_view.get_number_of_vertices()); + raft::update_host(h_sg_components.data(), + d_sg_components.data(), + d_sg_components.size(), + handle.get_stream()); + + handle.get_stream_view().synchronize(); + + std::unordered_map mg_to_sg_map{}; + for (size_t i = 0; i < h_sg_components.size(); ++i) { + mg_to_sg_map.insert({h_mg_aggregate_components[i], h_sg_components[i]}); + } + std::transform(h_mg_aggregate_components.begin(), + h_mg_aggregate_components.end(), + h_mg_aggregate_components.begin(), + [&mg_to_sg_map](auto mg_c) { return mg_to_sg_map[mg_c]; }); + + ASSERT_TRUE(std::equal( + h_sg_components.begin(), h_sg_components.end(), h_mg_aggregate_components.begin())) + << "components do not match with the SG values."; + } + } + } +}; + +using Tests_MGWeaklyConnectedComponents_File = + Tests_MGWeaklyConnectedComponents; +using Tests_MGWeaklyConnectedComponents_Rmat = + Tests_MGWeaklyConnectedComponents; + +TEST_P(Tests_MGWeaklyConnectedComponents_File, CheckInt32Int32) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +TEST_P(Tests_MGWeaklyConnectedComponents_Rmat, CheckInt32Int32) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +INSTANTIATE_TEST_SUITE_P( + file_test, + Tests_MGWeaklyConnectedComponents_File, + ::testing::Combine( + // enable correctness checks + ::testing::Values(WeaklyConnectedComponents_Usecase{0}), + ::testing::Values(cugraph::test::File_Usecase("test/datasets/karate.mtx"), + cugraph::test::File_Usecase("test/datasets/polbooks.mtx"), + cugraph::test::File_Usecase("test/datasets/netscience.mtx")))); + +INSTANTIATE_TEST_SUITE_P(rmat_small_test, + Tests_MGWeaklyConnectedComponents_Rmat, + ::testing::Values( + // enable correctness checks + std::make_tuple(WeaklyConnectedComponents_Usecase{}, + cugraph::test::Rmat_Usecase( + 10, 16, 0.57, 0.19, 0.19, 0, true, false, 0, true)))); + +INSTANTIATE_TEST_SUITE_P(rmat_large_test, + Tests_MGWeaklyConnectedComponents_Rmat, + ::testing::Values( + // disable correctness checks + std::make_tuple(WeaklyConnectedComponents_Usecase{false}, + cugraph::test::Rmat_Usecase( + 20, 16, 0.57, 0.19, 0.19, 0, true, false, 0, true)))); + +CUGRAPH_MG_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/components/scc_test.cu b/cpp/tests/components/scc_test.cu index 9d5b55f34c6..eaf6daf2d29 100644 --- a/cpp/tests/components/scc_test.cu +++ b/cpp/tests/components/scc_test.cu @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. * * NVIDIA CORPORATION and its licensors retain all intellectual property * and proprietary rights in and to this software, related documentation @@ -16,14 +16,17 @@ #include #include -#include #include #include -#include +#include +#include #include #include +#include +#include +#include #include #include @@ -57,41 +60,48 @@ struct Usecase { std::string matrix_file; }; -// checker of counts of labels for each component -// expensive, for testing purposes only; +// counts number of vertices in each component; +// (of same label); +// potentially expensive, for testing purposes only; // // params: -// p_d_labels: device array of labels of size nrows; -// nrows: |V| for graph G(V, E); -// d_v_counts: #labels for each component; (_not_ pre-allocated!) +// in: p_d_labels: device array of labels of size nrows; +// in: nrows: |V| for graph G(V, E); +// out: d_v_counts: #labels for each component; (_not_ pre-allocated!) +// return: number of components; // template -size_t get_component_sizes(const IndexT* p_d_labels, size_t nrows, DVector& d_v_counts) +size_t get_component_sizes(const IndexT* p_d_labels, + size_t nrows, + DVector& d_num_vs_per_component) { DVector d_sorted_l(p_d_labels, p_d_labels + nrows); thrust::sort(d_sorted_l.begin(), d_sorted_l.end()); - size_t counts = - thrust::distance(d_sorted_l.begin(), thrust::unique(d_sorted_l.begin(), d_sorted_l.end())); + auto pair_it = thrust::reduce_by_key(d_sorted_l.begin(), + d_sorted_l.end(), + thrust::make_constant_iterator(1), + thrust::make_discard_iterator(), // ignore... + d_num_vs_per_component.begin()); - IndexT* p_d_srt_l = d_sorted_l.data().get(); - - d_v_counts.resize(counts); - thrust::transform( - thrust::device, - d_sorted_l.begin(), - d_sorted_l.begin() + counts, - d_v_counts.begin(), - [p_d_srt_l, counts] __device__(IndexT indx) { - return thrust::count_if( - thrust::seq, p_d_srt_l, p_d_srt_l + counts, [indx](IndexT label) { return label == indx; }); - }); - - // sort the counts: - thrust::sort(d_v_counts.begin(), d_v_counts.end()); + size_t counts = thrust::distance(d_num_vs_per_component.begin(), pair_it.second); + d_num_vs_per_component.resize(counts); return counts; } + +template +DVector byte_matrix_to_int(const DVector& d_adj_byte_matrix) +{ + auto n2 = d_adj_byte_matrix.size(); + thrust::device_vector d_vec_matrix(n2, 0); + thrust::transform(d_adj_byte_matrix.begin(), + d_adj_byte_matrix.end(), + d_vec_matrix.begin(), + [] __device__(auto byte_v) { return static_cast(byte_v); }); + return d_vec_matrix; +} + } // namespace struct Tests_Strongly_CC : ::testing::TestWithParam { @@ -105,7 +115,8 @@ struct Tests_Strongly_CC : ::testing::TestWithParam { } std::cout << "#iterations:\n"; - for (auto&& count : strongly_cc_counts) std::cout << count << std::endl; + for (auto&& count : strongly_cc_counts) + std::cout << count << std::endl; } } virtual void SetUp() {} @@ -154,8 +165,8 @@ struct Tests_Strongly_CC : ::testing::TestWithParam { // Allocate memory on host std::vector cooRowInd(nnz); std::vector cooColInd(nnz); - std::vector labels(m); // for G(V, E), m := |V| - std::vector verts(m); + std::vector labels(nrows); // for G(V, E), m := |V| + std::vector verts(nrows); // Read: COO Format // @@ -166,11 +177,12 @@ struct Tests_Strongly_CC : ::testing::TestWithParam { << "\n"; ASSERT_EQ(fclose(fpin), 0); - cugraph::GraphCOOView G_coo(&cooRowInd[0], &cooColInd[0], nullptr, m, nnz); - auto G_unique = cugraph::coo_to_csr(G_coo); - cugraph::GraphCSRView G = G_unique->view(); + cugraph::legacy::GraphCOOView G_coo( + &cooRowInd[0], &cooColInd[0], nullptr, nrows, nnz); + auto G_unique = cugraph::coo_to_csr(G_coo); + cugraph::legacy::GraphCSRView G = G_unique->view(); - rmm::device_vector d_labels(m); + rmm::device_vector d_labels(nrows); size_t count = 0; @@ -190,7 +202,7 @@ struct Tests_Strongly_CC : ::testing::TestWithParam { } strongly_cc_counts.push_back(count); - DVector d_counts; + DVector d_counts(nrows); auto count_labels = get_component_sizes(d_labels.data().get(), nrows, d_counts); } }; @@ -201,11 +213,220 @@ std::vector Tests_Strongly_CC::strongly_cc_counts; TEST_P(Tests_Strongly_CC, Strongly_CC) { run_current_test(GetParam()); } // --gtest_filter=*simple_test* -INSTANTIATE_TEST_CASE_P( +INSTANTIATE_TEST_SUITE_P( simple_test, Tests_Strongly_CC, ::testing::Values( Usecase("test/datasets/cage6.mtx") // DG "small" enough to meet SCC GPU memory requirements )); +struct SCCSmallTest : public ::testing::Test { +}; + +// FIXME: we should take advantage of gtest parameterization over copy-and-paste reuse. +// +TEST_F(SCCSmallTest, CustomGraphSimpleLoops) +{ + using IndexT = int; + + size_t nrows = 5; + size_t n2 = 2 * nrows * nrows; + + cudaDeviceProp prop; + int device = 0; + cudaGetDeviceProperties(&prop, device); + + ASSERT_TRUE(n2 < prop.totalGlobalMem); + + // Allocate memory on host + std::vector cooRowInd{0, 1, 2, 3, 3, 4}; + std::vector cooColInd{1, 0, 0, 1, 4, 3}; + std::vector labels(nrows); + std::vector verts(nrows); + + size_t nnz = cooRowInd.size(); + + EXPECT_EQ(nnz, cooColInd.size()); + + cugraph::legacy::GraphCOOView G_coo( + &cooRowInd[0], &cooColInd[0], nullptr, nrows, nnz); + auto G_unique = cugraph::coo_to_csr(G_coo); + cugraph::legacy::GraphCSRView G = G_unique->view(); + + rmm::device_vector d_labels(nrows); + + cugraph::connected_components(G, cugraph::cugraph_cc_t::CUGRAPH_STRONG, d_labels.data().get()); + + DVector d_counts(nrows); + auto count_components = get_component_sizes(d_labels.data().get(), nrows, d_counts); + + EXPECT_EQ(count_components, static_cast(3)); + + std::vector v_counts(d_counts.size()); + + cudaMemcpy(v_counts.data(), + d_counts.data().get(), + sizeof(size_t) * v_counts.size(), + cudaMemcpyDeviceToHost); + + cudaDeviceSynchronize(); + + std::vector v_counts_exp{2, 1, 2}; + + EXPECT_EQ(v_counts, v_counts_exp); +} + +TEST_F(SCCSmallTest, /*DISABLED_*/ CustomGraphWithSelfLoops) +{ + using IndexT = int; + + size_t nrows = 5; + size_t n2 = 2 * nrows * nrows; + + cudaDeviceProp prop; + int device = 0; + cudaGetDeviceProperties(&prop, device); + + ASSERT_TRUE(n2 < prop.totalGlobalMem); + + // Allocate memory on host + std::vector cooRowInd{0, 0, 1, 1, 2, 2, 3, 3, 4}; + std::vector cooColInd{0, 1, 0, 1, 0, 2, 1, 3, 4}; + std::vector labels(nrows); + std::vector verts(nrows); + + size_t nnz = cooRowInd.size(); + + EXPECT_EQ(nnz, cooColInd.size()); + + cugraph::legacy::GraphCOOView G_coo( + &cooRowInd[0], &cooColInd[0], nullptr, nrows, nnz); + auto G_unique = cugraph::coo_to_csr(G_coo); + cugraph::legacy::GraphCSRView G = G_unique->view(); + + rmm::device_vector d_labels(nrows); + + cugraph::connected_components(G, cugraph::cugraph_cc_t::CUGRAPH_STRONG, d_labels.data().get()); + + DVector d_counts(nrows); + auto count_components = get_component_sizes(d_labels.data().get(), nrows, d_counts); + + EXPECT_EQ(count_components, static_cast(4)); + + std::vector v_counts(d_counts.size()); + + cudaMemcpy(v_counts.data(), + d_counts.data().get(), + sizeof(size_t) * v_counts.size(), + cudaMemcpyDeviceToHost); + + cudaDeviceSynchronize(); + + std::vector v_counts_exp{2, 1, 1, 1}; + + EXPECT_EQ(v_counts, v_counts_exp); +} + +TEST_F(SCCSmallTest, SmallGraphWithSelfLoops1) +{ + using IndexT = int; + + size_t nrows = 3; + + std::vector cooRowInd{0, 0, 1, 2}; + std::vector cooColInd{0, 1, 0, 0}; + + std::vector v_counts_exp{2, 1}; + + std::vector labels(nrows); + std::vector verts(nrows); + + size_t nnz = cooRowInd.size(); + + EXPECT_EQ(nnz, cooColInd.size()); + + cugraph::legacy::GraphCOOView G_coo( + &cooRowInd[0], &cooColInd[0], nullptr, nrows, nnz); + auto G_unique = cugraph::coo_to_csr(G_coo); + cugraph::legacy::GraphCSRView G = G_unique->view(); + + rmm::device_vector d_labels(nrows); + + cugraph::connected_components(G, cugraph::cugraph_cc_t::CUGRAPH_STRONG, d_labels.data().get()); + + DVector d_counts(nrows); + auto count_components = get_component_sizes(d_labels.data().get(), nrows, d_counts); + + // std::cout << "vertex labels:\n"; + // print_v(d_labels, std::cout); + + decltype(count_components) num_components_exp = 2; + + EXPECT_EQ(count_components, num_components_exp); +} + +TEST_F(SCCSmallTest, SmallGraphWithIsolated) +{ + using IndexT = int; + + size_t nrows = 3; + + std::vector cooRowInd{0, 0, 1}; + std::vector cooColInd{0, 1, 0}; + + std::vector v_counts_exp{2, 1}; + + std::vector labels(nrows); + std::vector verts(nrows); + + size_t nnz = cooRowInd.size(); + + EXPECT_EQ(nnz, cooColInd.size()); + + // Note: there seems to be a BUG in coo_to_csr() or view() + // COO format doesn't account for isolated vertices; + // + // cugraph::legacy::GraphCOOView G_coo(&cooRowInd[0], &cooColInd[0], nullptr, + // nrows, nnz); auto G_unique = cugraph::coo_to_csr(G_coo); + // cugraph::legacy::GraphCSRView G = G_unique->view(); + // + // + // size_t num_vertices = G.number_of_vertices; + // size_t num_edges = G.number_of_edges; + // + // EXPECT_EQ(num_vertices, nrows); //fails when G was constructed from COO + // EXPECT_EQ(num_edges, nnz); + + std::vector ro{0, 2, 3, 3}; + std::vector ci{0, 1, 0}; + + nnz = ci.size(); + + thrust::device_vector d_ro(ro); + thrust::device_vector d_ci(ci); + + cugraph::legacy::GraphCSRView G{ + d_ro.data().get(), d_ci.data().get(), nullptr, static_cast(nrows), static_cast(nnz)}; + + size_t num_vertices = G.number_of_vertices; + size_t num_edges = G.number_of_edges; + + EXPECT_EQ(num_vertices, nrows); + EXPECT_EQ(num_edges, nnz); + + rmm::device_vector d_labels(nrows); + + cugraph::connected_components(G, cugraph::cugraph_cc_t::CUGRAPH_STRONG, d_labels.data().get()); + + DVector d_counts(nrows); + auto count_components = get_component_sizes(d_labels.data().get(), nrows, d_counts); + + // std::cout << "vertex labels:\n"; + // print_v(d_labels, std::cout); + + decltype(count_components) num_components_exp = 2; + + EXPECT_EQ(count_components, num_components_exp); +} + CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/components/wcc_graphs.cu b/cpp/tests/components/wcc_graphs.cu new file mode 100644 index 00000000000..3ceebfd46a2 --- /dev/null +++ b/cpp/tests/components/wcc_graphs.cu @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved. + * + * NVIDIA CORPORATION and its licensors retain all intellectual property + * and proprietary rights in and to this software, related documentation + * and any modifications thereto. Any use, reproduction, disclosure or + * distribution of this software and related documentation without an express + * license agreement from NVIDIA CORPORATION is strictly prohibited. + * + */ + +#include +#include + +#include +#include + +#include + +#include + +namespace cugraph { +namespace test { + +template +std::tuple, + std::optional>> +LineGraph_Usecase::construct_graph(raft::handle_t const& handle, + bool test_weighted, + bool renumber) const +{ + uint64_t seed{0}; + + edge_t num_edges = 2 * (num_vertices_ - 1); + + rmm::device_uvector vertices_v(num_vertices_, handle.get_stream()); + rmm::device_uvector src_v(num_edges, handle.get_stream()); + rmm::device_uvector dst_v(num_edges, handle.get_stream()); + rmm::device_uvector order_v(num_vertices_, handle.get_stream()); + + thrust::sequence( + rmm::exec_policy(handle.get_stream()), vertices_v.begin(), vertices_v.end(), vertex_t{0}); + + cugraph::detail::uniform_random_fill( + handle.get_stream_view(), order_v.data(), num_vertices_, double{0.0}, double{1.0}, seed); + + thrust::sort_by_key( + rmm::exec_policy(handle.get_stream()), order_v.begin(), order_v.end(), vertices_v.begin()); + + raft::copy(src_v.begin(), vertices_v.begin(), (num_vertices_ - 1), handle.get_stream()); + raft::copy(dst_v.begin(), vertices_v.begin() + 1, (num_vertices_ - 1), handle.get_stream()); + + raft::copy(src_v.begin() + (num_vertices_ - 1), + vertices_v.begin() + 1, + (num_vertices_ - 1), + handle.get_stream()); + raft::copy(dst_v.begin() + (num_vertices_ - 1), + vertices_v.begin(), + (num_vertices_ - 1), + handle.get_stream()); + + thrust::sequence( + rmm::exec_policy(handle.get_stream()), vertices_v.begin(), vertices_v.end(), vertex_t{0}); + + handle.get_stream_view().synchronize(); + + return cugraph::experimental:: + create_graph_from_edgelist( + handle, + std::optional>{ + std::make_tuple(vertices_v.data(), static_cast(vertices_v.size()))}, + std::move(src_v), + std::move(dst_v), + std::nullopt, + cugraph::experimental::graph_properties_t{true, false}, + false); +} + +template std::tuple, + std::optional>> +LineGraph_Usecase::construct_graph(raft::handle_t const&, bool, bool) const; + +} // namespace test +} // namespace cugraph diff --git a/cpp/tests/components/wcc_graphs.hpp b/cpp/tests/components/wcc_graphs.hpp new file mode 100644 index 00000000000..cea82cb95bc --- /dev/null +++ b/cpp/tests/components/wcc_graphs.hpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved. + * + * NVIDIA CORPORATION and its licensors retain all intellectual property + * and proprietary rights in and to this software, related documentation + * and any modifications thereto. Any use, reproduction, disclosure or + * distribution of this software and related documentation without an express + * license agreement from NVIDIA CORPORATION is strictly prohibited. + * + */ + +#include + +#include + +namespace cugraph { +namespace test { + +class LineGraph_Usecase { + public: + LineGraph_Usecase() = delete; + + LineGraph_Usecase(size_t num_vertices) : num_vertices_(num_vertices) {} + + template + std::tuple< + cugraph::experimental::graph_t, + std::optional>> + construct_graph(raft::handle_t const& handle, bool test_weighted, bool renumber = true) const; + + private: + size_t num_vertices_{0}; +}; + +} // namespace test +} // namespace cugraph diff --git a/cpp/tests/components/wcc_test.cpp b/cpp/tests/components/wcc_test.cpp new file mode 100644 index 00000000000..9f6254d445f --- /dev/null +++ b/cpp/tests/components/wcc_test.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved. + * + * NVIDIA CORPORATION and its licensors retain all intellectual property + * and proprietary rights in and to this software, related documentation + * and any modifications thereto. Any use, reproduction, disclosure or + * distribution of this software and related documentation without an express + * license agreement from NVIDIA CORPORATION is strictly prohibited. + * + */ + +#include +#include +#include + +#include +#include + +#include +#include + +struct WCC_Usecase { + bool validate_results{true}; +}; + +template +class Tests_WCC : public ::testing::TestWithParam> { + public: + Tests_WCC() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + + virtual void SetUp() {} + virtual void TearDown() {} + + static std::vector weakly_cc_time; + + template + void run_current_test(WCC_Usecase const& param, input_usecase_t const& input_usecase) + { + raft::handle_t handle{}; + + cugraph::experimental::graph_t graph(handle); + + std::cout << "calling construct_graph" << std::endl; + + std::tie(graph, std::ignore) = + cugraph::test::construct_graph( + handle, input_usecase, false, false); + + std::cout << "back from construct_graph" << std::endl; + + auto graph_view = graph.view(); + + rmm::device_uvector component_labels_v(graph_view.get_number_of_vertices(), + handle.get_stream()); + + // cugraph::weakly_connected_components(handle, graph_view, component_labels_v.begin()); + + // TODO: validate result + } +}; + +using Tests_WCC_File = Tests_WCC; +using Tests_WCC_Rmat = Tests_WCC; +using Tests_WCC_PathGraph = Tests_WCC; + +TEST_P(Tests_WCC_File, WCC) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} +TEST_P(Tests_WCC_Rmat, WCC) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} +TEST_P(Tests_WCC_PathGraph, WCC) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +// --gtest_filter=*simple_test* +INSTANTIATE_TEST_SUITE_P( + file_test, + Tests_WCC_File, + ::testing::Values( + std::make_tuple(WCC_Usecase{}, cugraph::test::File_Usecase("test/datasets/dolphins.mtx")), + std::make_tuple(WCC_Usecase{}, cugraph::test::File_Usecase("test/datasets/coPapersDBLP.mtx")), + std::make_tuple(WCC_Usecase{}, + cugraph::test::File_Usecase("test/datasets/coPapersCiteseer.mtx")), + std::make_tuple(WCC_Usecase{}, cugraph::test::File_Usecase("test/datasets/hollywood.mtx")))); + +INSTANTIATE_TEST_SUITE_P( + path_graph_test, + Tests_WCC_PathGraph, + ::testing::Values(std::make_tuple(WCC_Usecase{}, + cugraph::test::PathGraph_Usecase( + std::vector>({{1000, 0}}))), + std::make_tuple(WCC_Usecase{}, + cugraph::test::PathGraph_Usecase( + std::vector>({{100000, 0}}))))); + +CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/components/weakly_connected_components_test.cpp b/cpp/tests/components/weakly_connected_components_test.cpp new file mode 100644 index 00000000000..fe87e806687 --- /dev/null +++ b/cpp/tests/components/weakly_connected_components_test.cpp @@ -0,0 +1,268 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governin_from_mtxg permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +// do the perf measurements +// enabled by command line parameter s'--perf' +// +static int PERF = 0; + +template +void weakly_connected_components_reference(edge_t const* offsets, + vertex_t const* indices, + vertex_t* components, + vertex_t num_vertices) +{ + vertex_t depth{0}; + + std::fill(components, + components + num_vertices, + cugraph::experimental::invalid_component_id::value); + + vertex_t num_scanned{0}; + while (true) { + auto it = std::find(components + num_scanned, + components + num_vertices, + cugraph::experimental::invalid_component_id::value); + if (it == components + num_vertices) { break; } + num_scanned += static_cast(std::distance(components + num_scanned, it)); + auto source = num_scanned; + *(components + source) = source; + std::vector cur_frontier_rows{source}; + std::vector new_frontier_rows{}; + + while (cur_frontier_rows.size() > 0) { + for (auto const row : cur_frontier_rows) { + auto nbr_offset_first = *(offsets + row); + auto nbr_offset_last = *(offsets + row + 1); + for (auto nbr_offset = nbr_offset_first; nbr_offset != nbr_offset_last; ++nbr_offset) { + auto nbr = *(indices + nbr_offset); + if (*(components + nbr) == cugraph::experimental::invalid_component_id::value) { + *(components + nbr) = source; + new_frontier_rows.push_back(nbr); + } + } + } + std::swap(cur_frontier_rows, new_frontier_rows); + new_frontier_rows.clear(); + } + } + + return; +} + +struct WeaklyConnectedComponents_Usecase { + bool check_correctness{true}; +}; + +template +class Tests_WeaklyConnectedComponent + : public ::testing::TestWithParam< + std::tuple> { + public: + Tests_WeaklyConnectedComponent() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + + virtual void SetUp() {} + virtual void TearDown() {} + + template + void run_current_test( + WeaklyConnectedComponents_Usecase const& weakly_connected_components_usecase, + input_usecase_t const& input_usecase) + { + constexpr bool renumber = true; + + using weight_t = float; + + raft::handle_t handle{}; + HighResClock hr_clock{}; + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + hr_clock.start(); + } + + auto [graph, d_renumber_map_labels] = + input_usecase.template construct_graph( + handle, false, renumber); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "construct_graph took " << elapsed_time * 1e-6 << " s.\n"; + } + + auto graph_view = graph.view(); + ASSERT_TRUE(graph_view.is_symmetric()) + << "Weakly connected components works only on undirected (symmetric) graphs."; + + rmm::device_uvector d_components(graph_view.get_number_of_vertices(), + handle.get_stream()); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + hr_clock.start(); + } + + cugraph::experimental::weakly_connected_components(handle, graph_view, d_components.data()); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "weakly_connected_components took " << elapsed_time * 1e-6 << " s.\n"; + } + + if (weakly_connected_components_usecase.check_correctness) { + cugraph::experimental::graph_t unrenumbered_graph( + handle); + if (renumber) { + std::tie(unrenumbered_graph, std::ignore) = + input_usecase.template construct_graph( + handle, false, false); + } + auto unrenumbered_graph_view = renumber ? unrenumbered_graph.view() : graph_view; + + std::vector h_offsets(unrenumbered_graph_view.get_number_of_vertices() + 1); + std::vector h_indices(unrenumbered_graph_view.get_number_of_edges()); + raft::update_host(h_offsets.data(), + unrenumbered_graph_view.get_matrix_partition_view().get_offsets(), + unrenumbered_graph_view.get_number_of_vertices() + 1, + handle.get_stream()); + raft::update_host(h_indices.data(), + unrenumbered_graph_view.get_matrix_partition_view().get_indices(), + unrenumbered_graph_view.get_number_of_edges(), + handle.get_stream()); + + handle.get_stream_view().synchronize(); + + std::vector h_reference_components( + unrenumbered_graph_view.get_number_of_vertices()); + + weakly_connected_components_reference(h_offsets.data(), + h_indices.data(), + h_reference_components.data(), + unrenumbered_graph_view.get_number_of_vertices()); + + std::vector h_cugraph_components(graph_view.get_number_of_vertices()); + if (renumber) { + rmm::device_uvector d_unrenumbered_components(size_t{0}, + handle.get_stream_view()); + std::tie(std::ignore, d_unrenumbered_components) = + cugraph::test::sort_by_key(handle, + (*d_renumber_map_labels).data(), + d_components.data(), + (*d_renumber_map_labels).size()); + raft::update_host(h_cugraph_components.data(), + d_unrenumbered_components.data(), + d_unrenumbered_components.size(), + handle.get_stream()); + } else { + raft::update_host(h_cugraph_components.data(), + d_components.data(), + d_components.size(), + handle.get_stream()); + } + handle.get_stream_view().synchronize(); + + std::unordered_map cuda_to_reference_map{}; + for (size_t i = 0; i < h_reference_components.size(); ++i) { + cuda_to_reference_map.insert({h_cugraph_components[i], h_reference_components[i]}); + } + std::transform( + h_cugraph_components.begin(), + h_cugraph_components.end(), + h_cugraph_components.begin(), + [&cuda_to_reference_map](auto cugraph_c) { return cuda_to_reference_map[cugraph_c]; }); + + ASSERT_TRUE(std::equal( + h_reference_components.begin(), h_reference_components.end(), h_cugraph_components.begin())) + << "components do not match with the reference values."; + } + } +}; + +using Tests_WeaklyConnectedComponents_File = + Tests_WeaklyConnectedComponent; +using Tests_WeaklyConnectedComponents_Rmat = + Tests_WeaklyConnectedComponent; + +// FIXME: add tests for type combinations +TEST_P(Tests_WeaklyConnectedComponents_File, CheckInt32Int32) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +TEST_P(Tests_WeaklyConnectedComponents_Rmat, CheckInt32Int32) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +INSTANTIATE_TEST_SUITE_P( + file_test, + Tests_WeaklyConnectedComponents_File, + ::testing::Values( + // enable correctness checks + std::make_tuple(WeaklyConnectedComponents_Usecase{}, + cugraph::test::File_Usecase("test/datasets/karate.mtx")), + std::make_tuple(WeaklyConnectedComponents_Usecase{}, + cugraph::test::File_Usecase("test/datasets/polbooks.mtx")), + std::make_tuple(WeaklyConnectedComponents_Usecase{}, + cugraph::test::File_Usecase("test/datasets/netscience.mtx")))); + +INSTANTIATE_TEST_SUITE_P( + rmat_small_test, + Tests_WeaklyConnectedComponents_Rmat, + ::testing::Values( + // enable correctness checks + std::make_tuple(WeaklyConnectedComponents_Usecase{}, + cugraph::test::Rmat_Usecase(10, 16, 0.57, 0.19, 0.19, 0, true, false)))); + +INSTANTIATE_TEST_SUITE_P( + rmat_large_test, + Tests_WeaklyConnectedComponents_Rmat, + ::testing::Values( + // disable correctness checks + std::make_tuple(WeaklyConnectedComponents_Usecase{false}, + cugraph::test::Rmat_Usecase(20, 16, 0.57, 0.19, 0.19, 0, true, false)))); + +CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/experimental/bfs_test.cpp b/cpp/tests/experimental/bfs_test.cpp index 2498ca4f3f5..8510d6698ca 100644 --- a/cpp/tests/experimental/bfs_test.cpp +++ b/cpp/tests/experimental/bfs_test.cpp @@ -1,5 +1,5 @@ /* - * 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. @@ -14,12 +14,16 @@ * limitations under the License. */ +#include #include +#include #include +#include -#include -#include -#include +#include +#include +#include +#include #include #include @@ -28,13 +32,19 @@ #include +#include #include #include #include +// do the perf measurements +// enabled by command line parameter s'--perf' +// +static int PERF = 0; + template -void bfs_reference(edge_t* offsets, - vertex_t* indices, +void bfs_reference(edge_t const* offsets, + vertex_t const* indices, vertex_t* distances, vertex_t* predecessors, vertex_t num_vertices, @@ -44,7 +54,9 @@ void bfs_reference(edge_t* offsets, vertex_t depth{0}; std::fill(distances, distances + num_vertices, std::numeric_limits::max()); - std::fill(predecessors, predecessors + num_vertices, cugraph::invalid_vertex_id::value); + std::fill(predecessors, + predecessors + num_vertices, + cugraph::experimental::invalid_vertex_id::value); *(distances + source) = depth; std::vector cur_frontier_rows{source}; @@ -72,21 +84,13 @@ void bfs_reference(edge_t* offsets, return; } -typedef struct BFS_Usecase_t { - std::string graph_file_full_path{}; - size_t source{false}; - - BFS_Usecase_t(std::string const& graph_file_path, size_t source) : source(source) - { - if ((graph_file_path.length() > 0) && (graph_file_path[0] != '/')) { - graph_file_full_path = cugraph::test::get_rapids_dataset_root_dir() + "/" + graph_file_path; - } else { - graph_file_full_path = graph_file_path; - } - }; -} BFS_Usecase; +struct BFS_Usecase { + size_t source{0}; + bool check_correctness{true}; +}; -class Tests_BFS : public ::testing::TestWithParam { +template +class Tests_BFS : public ::testing::TestWithParam> { public: Tests_BFS() {} static void SetupTestCase() {} @@ -96,109 +100,222 @@ class Tests_BFS : public ::testing::TestWithParam { virtual void TearDown() {} template - void run_current_test(BFS_Usecase const& configuration) + void run_current_test(BFS_Usecase const& bfs_usecase, input_usecase_t const& input_usecase) { + constexpr bool renumber = true; + using weight_t = float; raft::handle_t handle{}; + HighResClock hr_clock{}; - auto graph = - cugraph::test::read_graph_from_matrix_market_file( - handle, configuration.graph_file_full_path, false); + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + hr_clock.start(); + } + + auto [graph, d_renumber_map_labels] = + input_usecase.template construct_graph( + handle, true, renumber); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "construct_graph took " << elapsed_time * 1e-6 << " s.\n"; + } auto graph_view = graph.view(); - std::vector h_offsets(graph_view.get_number_of_vertices() + 1); - std::vector h_indices(graph_view.get_number_of_edges()); - raft::update_host(h_offsets.data(), - graph_view.offsets(), - graph_view.get_number_of_vertices() + 1, - handle.get_stream()); - raft::update_host(h_indices.data(), - graph_view.indices(), - graph_view.get_number_of_edges(), - handle.get_stream()); - CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); - - ASSERT_TRUE(configuration.source >= 0 && - configuration.source <= graph_view.get_number_of_vertices()) - << "Starting sources should be >= 0 and" - << " less than the number of vertices in the graph."; - - std::vector h_reference_distances(graph_view.get_number_of_vertices()); - std::vector h_reference_predecessors(graph_view.get_number_of_vertices()); - - bfs_reference(h_offsets.data(), - h_indices.data(), - h_reference_distances.data(), - h_reference_predecessors.data(), - graph_view.get_number_of_vertices(), - static_cast(configuration.source), - std::numeric_limits::max()); + ASSERT_TRUE(static_cast(bfs_usecase.source) >= 0 && + static_cast(bfs_usecase.source) < graph_view.get_number_of_vertices()) + << "Invalid starting source."; rmm::device_uvector d_distances(graph_view.get_number_of_vertices(), handle.get_stream()); rmm::device_uvector d_predecessors(graph_view.get_number_of_vertices(), handle.get_stream()); - CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + hr_clock.start(); + } cugraph::experimental::bfs(handle, graph_view, - d_distances.begin(), - d_predecessors.begin(), - static_cast(configuration.source), + d_distances.data(), + d_predecessors.data(), + static_cast(bfs_usecase.source), false, - std::numeric_limits::max(), - false); - - CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement - - std::vector h_cugraph_distances(graph_view.get_number_of_vertices()); - std::vector h_cugraph_predecessors(graph_view.get_number_of_vertices()); - - raft::update_host( - h_cugraph_distances.data(), d_distances.data(), d_distances.size(), handle.get_stream()); - raft::update_host(h_cugraph_predecessors.data(), - d_predecessors.data(), - d_predecessors.size(), - handle.get_stream()); - CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); - - ASSERT_TRUE(std::equal( - h_reference_distances.begin(), h_reference_distances.end(), h_cugraph_distances.begin())) - << "distances do not match with the reference values."; - - for (auto it = h_cugraph_predecessors.begin(); it != h_cugraph_predecessors.end(); ++it) { - auto i = std::distance(h_cugraph_predecessors.begin(), it); - if (*it == cugraph::invalid_vertex_id::value) { - ASSERT_TRUE(h_reference_predecessors[i] == *it) - << "vertex reachability do not match with the reference."; + std::numeric_limits::max()); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "BFS took " << elapsed_time * 1e-6 << " s.\n"; + } + + if (bfs_usecase.check_correctness) { + cugraph::experimental::graph_t unrenumbered_graph( + handle); + if (renumber) { + std::tie(unrenumbered_graph, std::ignore) = + input_usecase.template construct_graph( + handle, true, false); + } + auto unrenumbered_graph_view = renumber ? unrenumbered_graph.view() : graph_view; + + std::vector h_offsets(unrenumbered_graph_view.get_number_of_vertices() + 1); + std::vector h_indices(unrenumbered_graph_view.get_number_of_edges()); + raft::update_host(h_offsets.data(), + unrenumbered_graph_view.get_matrix_partition_view().get_offsets(), + unrenumbered_graph_view.get_number_of_vertices() + 1, + handle.get_stream()); + raft::update_host(h_indices.data(), + unrenumbered_graph_view.get_matrix_partition_view().get_indices(), + unrenumbered_graph_view.get_number_of_edges(), + handle.get_stream()); + + handle.get_stream_view().synchronize(); + + auto unrenumbered_source = static_cast(bfs_usecase.source); + if (renumber) { + std::vector h_renumber_map_labels((*d_renumber_map_labels).size()); + raft::update_host(h_renumber_map_labels.data(), + (*d_renumber_map_labels).data(), + (*d_renumber_map_labels).size(), + handle.get_stream()); + + handle.get_stream_view().synchronize(); + + unrenumbered_source = h_renumber_map_labels[bfs_usecase.source]; + } + + std::vector h_reference_distances(unrenumbered_graph_view.get_number_of_vertices()); + std::vector h_reference_predecessors( + unrenumbered_graph_view.get_number_of_vertices()); + + bfs_reference(h_offsets.data(), + h_indices.data(), + h_reference_distances.data(), + h_reference_predecessors.data(), + unrenumbered_graph_view.get_number_of_vertices(), + unrenumbered_source, + std::numeric_limits::max()); + + std::vector h_cugraph_distances(graph_view.get_number_of_vertices()); + std::vector h_cugraph_predecessors(graph_view.get_number_of_vertices()); + if (renumber) { + cugraph::experimental::unrenumber_local_int_vertices(handle, + d_predecessors.data(), + d_predecessors.size(), + (*d_renumber_map_labels).data(), + vertex_t{0}, + graph_view.get_number_of_vertices(), + true); + + rmm::device_uvector d_unrenumbered_distances(size_t{0}, handle.get_stream()); + std::tie(std::ignore, d_unrenumbered_distances) = + cugraph::test::sort_by_key(handle, + (*d_renumber_map_labels).data(), + d_distances.data(), + (*d_renumber_map_labels).size()); + rmm::device_uvector d_unrenumbered_predecessors(size_t{0}, handle.get_stream()); + std::tie(std::ignore, d_unrenumbered_predecessors) = + cugraph::test::sort_by_key(handle, + (*d_renumber_map_labels).data(), + d_predecessors.data(), + (*d_renumber_map_labels).size()); + raft::update_host(h_cugraph_distances.data(), + d_unrenumbered_distances.data(), + d_unrenumbered_distances.size(), + handle.get_stream()); + raft::update_host(h_cugraph_predecessors.data(), + d_unrenumbered_predecessors.data(), + d_unrenumbered_predecessors.size(), + handle.get_stream()); + + handle.get_stream_view().synchronize(); } else { - ASSERT_TRUE(h_reference_distances[*it] + 1 == h_reference_distances[i]) - << "distance to this vertex != distance to the predecessor vertex + 1."; - bool found{false}; - for (auto j = h_offsets[*it]; j < h_offsets[*it + 1]; ++j) { - if (h_indices[j] == i) { - found = true; - break; + raft::update_host( + h_cugraph_distances.data(), d_distances.data(), d_distances.size(), handle.get_stream()); + raft::update_host(h_cugraph_predecessors.data(), + d_predecessors.data(), + d_predecessors.size(), + handle.get_stream()); + + handle.get_stream_view().synchronize(); + } + + ASSERT_TRUE(std::equal( + h_reference_distances.begin(), h_reference_distances.end(), h_cugraph_distances.begin())) + << "distances do not match with the reference values."; + + for (auto it = h_cugraph_predecessors.begin(); it != h_cugraph_predecessors.end(); ++it) { + auto i = std::distance(h_cugraph_predecessors.begin(), it); + if (*it == cugraph::experimental::invalid_vertex_id::value) { + ASSERT_TRUE(h_reference_predecessors[i] == *it) + << "vertex reachability does not match with the reference."; + } else { + ASSERT_TRUE(h_reference_distances[*it] + 1 == h_reference_distances[i]) + << "distance to this vertex != distance to the predecessor vertex + 1."; + bool found{false}; + for (auto j = h_offsets[*it]; j < h_offsets[*it + 1]; ++j) { + if (h_indices[j] == i) { + found = true; + break; + } } + ASSERT_TRUE(found) << "no edge from the predecessor vertex to this vertex."; } - ASSERT_TRUE(found) << "no edge from the predecessor vertex to this vertex."; } } } }; +using Tests_BFS_File = Tests_BFS; +using Tests_BFS_Rmat = Tests_BFS; + // FIXME: add tests for type combinations -TEST_P(Tests_BFS, CheckInt32Int32) { run_current_test(GetParam()); } - -INSTANTIATE_TEST_CASE_P(simple_test, - Tests_BFS, - ::testing::Values(BFS_Usecase("test/datasets/karate.mtx", 0), - BFS_Usecase("test/datasets/polbooks.mtx", 0), - BFS_Usecase("test/datasets/netscience.mtx", 0), - BFS_Usecase("test/datasets/netscience.mtx", 100), - BFS_Usecase("test/datasets/wiki2003.mtx", 1000), - BFS_Usecase("test/datasets/wiki-Talk.mtx", 1000))); +TEST_P(Tests_BFS_File, CheckInt32Int32) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +TEST_P(Tests_BFS_Rmat, CheckInt32Int32) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +INSTANTIATE_TEST_SUITE_P( + file_test, + Tests_BFS_File, + ::testing::Values( + // enable correctness checks + std::make_tuple(BFS_Usecase{0}, cugraph::test::File_Usecase("test/datasets/karate.mtx")), + std::make_tuple(BFS_Usecase{0}, cugraph::test::File_Usecase("test/datasets/polbooks.mtx")), + std::make_tuple(BFS_Usecase{0}, cugraph::test::File_Usecase("test/datasets/netscience.mtx")), + std::make_tuple(BFS_Usecase{100}, cugraph::test::File_Usecase("test/datasets/netscience.mtx")), + std::make_tuple(BFS_Usecase{1000}, cugraph::test::File_Usecase("test/datasets/wiki2003.mtx")), + std::make_tuple(BFS_Usecase{1000}, + cugraph::test::File_Usecase("test/datasets/wiki-Talk.mtx")))); + +INSTANTIATE_TEST_SUITE_P( + rmat_small_test, + Tests_BFS_Rmat, + ::testing::Values( + // enable correctness checks + std::make_tuple(BFS_Usecase{0}, + cugraph::test::Rmat_Usecase(10, 16, 0.57, 0.19, 0.19, 0, false, false)))); + +INSTANTIATE_TEST_SUITE_P( + rmat_large_test, + Tests_BFS_Rmat, + ::testing::Values( + // disable correctness checks for large graphs + std::make_pair(BFS_Usecase{0, false}, + cugraph::test::Rmat_Usecase(20, 32, 0.57, 0.19, 0.19, 0, false, false)))); CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/experimental/coarsen_graph_test.cpp b/cpp/tests/experimental/coarsen_graph_test.cpp new file mode 100644 index 00000000000..aaee198a4b0 --- /dev/null +++ b/cpp/tests/experimental/coarsen_graph_test.cpp @@ -0,0 +1,385 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governin_from_mtxg permissions and + * limitations under the License. + */ + +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include + +template +void check_coarsened_graph_results(edge_t* org_offsets, + vertex_t* org_indices, + weight_t* org_weights, + vertex_t* org_labels, + edge_t* coarse_offsets, + vertex_t* coarse_indices, + weight_t* coarse_weights, + vertex_t* coarse_vertex_labels, + vertex_t num_org_vertices, + vertex_t num_coarse_vertices) +{ + ASSERT_TRUE(((org_weights == nullptr) && (coarse_weights == nullptr)) || + ((org_weights != nullptr) && (coarse_weights != nullptr))); + ASSERT_TRUE(std::is_sorted(org_offsets, org_offsets + num_org_vertices)); + ASSERT_TRUE(std::count_if(org_indices, + org_indices + org_offsets[num_org_vertices], + [num_org_vertices](auto nbr) { + return !cugraph::experimental::is_valid_vertex(num_org_vertices, nbr); + }) == 0); + ASSERT_TRUE(std::is_sorted(coarse_offsets, coarse_offsets + num_coarse_vertices)); + ASSERT_TRUE(std::count_if(coarse_indices, + coarse_indices + coarse_offsets[num_coarse_vertices], + [num_coarse_vertices](auto nbr) { + return !cugraph::experimental::is_valid_vertex(num_coarse_vertices, + nbr); + }) == 0); + ASSERT_TRUE(num_coarse_vertices <= num_org_vertices); + + std::vector org_unique_labels(num_org_vertices); + std::iota(org_unique_labels.begin(), org_unique_labels.end(), vertex_t{0}); + std::transform(org_unique_labels.begin(), + org_unique_labels.end(), + org_unique_labels.begin(), + [org_labels](auto v) { return org_labels[v]; }); + std::sort(org_unique_labels.begin(), org_unique_labels.end()); + org_unique_labels.resize(std::distance( + org_unique_labels.begin(), std::unique(org_unique_labels.begin(), org_unique_labels.end()))); + + ASSERT_TRUE(org_unique_labels.size() == static_cast(num_coarse_vertices)); + + { + std::vector tmp_coarse_vertex_labels(coarse_vertex_labels, + coarse_vertex_labels + num_coarse_vertices); + std::sort(tmp_coarse_vertex_labels.begin(), tmp_coarse_vertex_labels.end()); + ASSERT_TRUE(std::unique(tmp_coarse_vertex_labels.begin(), tmp_coarse_vertex_labels.end()) == + tmp_coarse_vertex_labels.end()); + ASSERT_TRUE(std::equal( + org_unique_labels.begin(), org_unique_labels.end(), tmp_coarse_vertex_labels.begin())); + } + + std::vector> label_org_vertex_pairs(num_org_vertices); + for (vertex_t i = 0; i < num_org_vertices; ++i) { + label_org_vertex_pairs[i] = std::make_tuple(org_labels[i], i); + } + std::sort(label_org_vertex_pairs.begin(), label_org_vertex_pairs.end()); + + std::map label_to_coarse_vertex_map{}; + for (vertex_t i = 0; i < num_coarse_vertices; ++i) { + label_to_coarse_vertex_map[coarse_vertex_labels[i]] = i; + } + + auto threshold_ratio = (org_weights == nullptr) ? weight_t{1.0} /* irrelevant */ : weight_t{1e-4}; + auto threshold_magnitude = + (org_weights == nullptr) + ? weight_t{1.0} /* irrelevant */ + : (std::accumulate( + coarse_weights, coarse_weights + coarse_offsets[num_coarse_vertices], weight_t{0.0}) / + static_cast(coarse_offsets[num_coarse_vertices])) * + threshold_ratio; + + for (size_t i = 0; i < org_unique_labels.size(); ++i) { // for each vertex in the coarse graph + auto lb = std::lower_bound( + label_org_vertex_pairs.begin(), + label_org_vertex_pairs.end(), + std::make_tuple(org_unique_labels[i], + cugraph::experimental::invalid_vertex_id::value /* dummy */), + [](auto lhs, auto rhs) { return std::get<0>(lhs) < std::get<0>(rhs); }); + auto ub = std::upper_bound( + label_org_vertex_pairs.begin(), + label_org_vertex_pairs.end(), + std::make_tuple(org_unique_labels[i], + cugraph::experimental::invalid_vertex_id::value /* dummy */), + [](auto lhs, auto rhs) { return std::get<0>(lhs) < std::get<0>(rhs); }); + auto count = std::distance(lb, ub); + auto offset = std::distance(label_org_vertex_pairs.begin(), lb); + if (org_weights == nullptr) { + std::vector coarse_nbrs0{}; + std::for_each( + lb, + ub, + [org_offsets, org_indices, org_labels, &label_to_coarse_vertex_map, &coarse_nbrs0](auto t) { + auto org_vertex = std::get<1>(t); + std::vector tmp_nbrs(org_offsets[org_vertex + 1] - org_offsets[org_vertex]); + std::transform(org_indices + org_offsets[org_vertex], + org_indices + org_offsets[org_vertex + 1], + tmp_nbrs.begin(), + [org_labels, &label_to_coarse_vertex_map](auto nbr) { + return label_to_coarse_vertex_map[org_labels[nbr]]; + }); + coarse_nbrs0.insert(coarse_nbrs0.end(), tmp_nbrs.begin(), tmp_nbrs.end()); + }); + std::sort(coarse_nbrs0.begin(), coarse_nbrs0.end()); + coarse_nbrs0.resize( + std::distance(coarse_nbrs0.begin(), std::unique(coarse_nbrs0.begin(), coarse_nbrs0.end()))); + + auto coarse_vertex = label_to_coarse_vertex_map[org_unique_labels[i]]; + auto coarse_offset = coarse_offsets[coarse_vertex]; + auto coarse_count = coarse_offsets[coarse_vertex + 1] - coarse_offset; + std::vector coarse_nbrs1(coarse_indices + coarse_offset, + coarse_indices + coarse_offset + coarse_count); + std::sort(coarse_nbrs1.begin(), coarse_nbrs1.end()); + + ASSERT_TRUE(coarse_nbrs0.size() == coarse_nbrs1.size()); + ASSERT_TRUE(std::equal(coarse_nbrs0.begin(), coarse_nbrs0.end(), coarse_nbrs1.begin())); + } else { + std::vector> coarse_nbr_weight_pairs0{}; + std::for_each(lb, + ub, + [org_offsets, + org_indices, + org_weights, + org_labels, + &label_to_coarse_vertex_map, + &coarse_nbr_weight_pairs0](auto t) { + auto org_vertex = std::get<1>(t); + std::vector> tmp_pairs( + org_offsets[org_vertex + 1] - org_offsets[org_vertex]); + for (auto j = org_offsets[org_vertex]; j < org_offsets[org_vertex + 1]; ++j) { + tmp_pairs[j - org_offsets[org_vertex]] = std::make_tuple( + label_to_coarse_vertex_map[org_labels[org_indices[j]]], org_weights[j]); + } + coarse_nbr_weight_pairs0.insert( + coarse_nbr_weight_pairs0.end(), tmp_pairs.begin(), tmp_pairs.end()); + }); + std::sort(coarse_nbr_weight_pairs0.begin(), coarse_nbr_weight_pairs0.end()); + // reduce by key + { + size_t run_start_idx = 0; + for (size_t j = 1; j < coarse_nbr_weight_pairs0.size(); ++j) { + auto& start = coarse_nbr_weight_pairs0[run_start_idx]; + auto& cur = coarse_nbr_weight_pairs0[j]; + if (std::get<0>(start) == std::get<0>(cur)) { + std::get<1>(start) += std::get<1>(cur); + std::get<0>(cur) = cugraph::experimental::invalid_vertex_id::value; + } else { + run_start_idx = j; + } + } + coarse_nbr_weight_pairs0.erase( + std::remove_if(coarse_nbr_weight_pairs0.begin(), + coarse_nbr_weight_pairs0.end(), + [](auto t) { + return std::get<0>(t) == + cugraph::experimental::invalid_vertex_id::value; + }), + coarse_nbr_weight_pairs0.end()); + } + + auto coarse_vertex = label_to_coarse_vertex_map[org_unique_labels[i]]; + std::vector> coarse_nbr_weight_pairs1( + coarse_offsets[coarse_vertex + 1] - coarse_offsets[coarse_vertex]); + for (auto j = coarse_offsets[coarse_vertex]; j < coarse_offsets[coarse_vertex + 1]; ++j) { + coarse_nbr_weight_pairs1[j - coarse_offsets[coarse_vertex]] = + std::make_tuple(coarse_indices[j], coarse_weights[j]); + } + std::sort(coarse_nbr_weight_pairs1.begin(), coarse_nbr_weight_pairs1.end()); + + ASSERT_TRUE(coarse_nbr_weight_pairs0.size() == coarse_nbr_weight_pairs1.size()); + ASSERT_TRUE(std::equal( + coarse_nbr_weight_pairs0.begin(), + coarse_nbr_weight_pairs0.end(), + coarse_nbr_weight_pairs1.begin(), + [threshold_ratio, threshold_magnitude](auto lhs, auto rhs) { + return std::get<0>(lhs) == std::get<0>(rhs) + ? (std::abs(std::get<1>(lhs) - std::get<1>(rhs)) <= + std::max(std::max(std::abs(std::get<1>(lhs)), std::abs(std::get<1>(rhs))) * + threshold_ratio, + threshold_magnitude)) + : false; + })); + } + } + + return; +} + +typedef struct CoarsenGraph_Usecase_t { + std::string graph_file_full_path{}; + double coarsen_ratio{0.0}; + bool test_weighted{false}; + + CoarsenGraph_Usecase_t(std::string const& graph_file_path, + double coarsen_ratio, + bool test_weighted) + : coarsen_ratio(coarsen_ratio), test_weighted(test_weighted) + { + if ((graph_file_path.length() > 0) && (graph_file_path[0] != '/')) { + graph_file_full_path = cugraph::test::get_rapids_dataset_root_dir() + "/" + graph_file_path; + } else { + graph_file_full_path = graph_file_path; + } + }; +} CoarsenGraph_Usecase; + +class Tests_CoarsenGraph : public ::testing::TestWithParam { + public: + Tests_CoarsenGraph() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + + virtual void SetUp() {} + virtual void TearDown() {} + + template + void run_current_test(CoarsenGraph_Usecase const& configuration) + { + raft::handle_t handle{}; + + // FIXME: remove this once we drop Pascal support + if (handle.get_device_properties().major < 7) { // Pascal is not supported, skip testing + return; + } + + cugraph::experimental::graph_t graph( + handle); + std::tie(graph, std::ignore) = cugraph::test:: + read_graph_from_matrix_market_file( + handle, configuration.graph_file_full_path, configuration.test_weighted, false); + auto graph_view = graph.view(); + + if (graph_view.get_number_of_vertices() == 0) { return; } + + std::vector h_labels(graph_view.get_number_of_vertices()); + auto num_labels = + std::max(static_cast(h_labels.size() * configuration.coarsen_ratio), vertex_t{1}); + + std::default_random_engine generator{}; + std::uniform_int_distribution distribution{0, num_labels - 1}; + + std::for_each(h_labels.begin(), h_labels.end(), [&distribution, &generator](auto& label) { + label = distribution(generator); + }); + + rmm::device_uvector d_labels(h_labels.size(), handle.get_stream()); + raft::update_device(d_labels.data(), h_labels.data(), h_labels.size(), handle.get_stream()); + + CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); + + std::unique_ptr< + cugraph::experimental::graph_t> + coarse_graph{}; + rmm::device_uvector coarse_vertices_to_labels(0, handle.get_stream()); + + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + + std::tie(coarse_graph, coarse_vertices_to_labels) = + cugraph::experimental::coarsen_graph(handle, graph_view, d_labels.begin()); + + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + + std::vector h_org_offsets(graph_view.get_number_of_vertices() + 1); + std::vector h_org_indices(graph_view.get_number_of_edges()); + std::vector h_org_weights{}; + raft::update_host(h_org_offsets.data(), + graph_view.get_matrix_partition_view().get_offsets(), + graph_view.get_number_of_vertices() + 1, + handle.get_stream()); + raft::update_host(h_org_indices.data(), + graph_view.get_matrix_partition_view().get_indices(), + graph_view.get_number_of_edges(), + handle.get_stream()); + if (graph_view.is_weighted()) { + h_org_weights.assign(graph_view.get_number_of_edges(), weight_t{0.0}); + raft::update_host(h_org_weights.data(), + *(graph_view.get_matrix_partition_view().get_weights()), + graph_view.get_number_of_edges(), + handle.get_stream()); + } + + auto coarse_graph_view = coarse_graph->view(); + + std::vector h_coarse_offsets(coarse_graph_view.get_number_of_vertices() + 1); + std::vector h_coarse_indices(coarse_graph_view.get_number_of_edges()); + std::vector h_coarse_weights{}; + raft::update_host(h_coarse_offsets.data(), + coarse_graph_view.get_matrix_partition_view().get_offsets(), + coarse_graph_view.get_number_of_vertices() + 1, + handle.get_stream()); + raft::update_host(h_coarse_indices.data(), + coarse_graph_view.get_matrix_partition_view().get_indices(), + coarse_graph_view.get_number_of_edges(), + handle.get_stream()); + if (graph_view.is_weighted()) { + h_coarse_weights.resize(coarse_graph_view.get_number_of_edges()); + raft::update_host(h_coarse_weights.data(), + *(coarse_graph_view.get_matrix_partition_view().get_weights()), + coarse_graph_view.get_number_of_edges(), + handle.get_stream()); + } + + std::vector h_coarse_vertices_to_labels(coarse_vertices_to_labels.size()); + raft::update_host(h_coarse_vertices_to_labels.data(), + coarse_vertices_to_labels.data(), + coarse_vertices_to_labels.size(), + handle.get_stream()); + + CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); + + check_coarsened_graph_results(h_org_offsets.data(), + h_org_indices.data(), + h_org_weights.data(), + h_labels.data(), + h_coarse_offsets.data(), + h_coarse_indices.data(), + h_coarse_weights.data(), + h_coarse_vertices_to_labels.data(), + graph_view.get_number_of_vertices(), + coarse_graph_view.get_number_of_vertices()); + } +}; + +// FIXME: add tests for type combinations + +TEST_P(Tests_CoarsenGraph, CheckInt32Int32FloatTransposed) +{ + run_current_test(GetParam()); +} + +TEST_P(Tests_CoarsenGraph, CheckInt32Int32FloatUntransposed) +{ + run_current_test(GetParam()); +} + +INSTANTIATE_TEST_SUITE_P( + simple_test, + Tests_CoarsenGraph, + ::testing::Values(CoarsenGraph_Usecase("test/datasets/karate.mtx", 0.2, false), + CoarsenGraph_Usecase("test/datasets/karate.mtx", 0.2, true), + CoarsenGraph_Usecase("test/datasets/web-Google.mtx", 0.1, false), + CoarsenGraph_Usecase("test/datasets/web-Google.mtx", 0.1, true), + CoarsenGraph_Usecase("test/datasets/ljournal-2008.mtx", 0.1, false), + CoarsenGraph_Usecase("test/datasets/ljournal-2008.mtx", 0.1, true), + CoarsenGraph_Usecase("test/datasets/webbase-1M.mtx", 0.1, false), + CoarsenGraph_Usecase("test/datasets/webbase-1M.mtx", 0.1, true))); + +CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/experimental/degree_test.cpp b/cpp/tests/experimental/degree_test.cpp new file mode 100644 index 00000000000..94134e3426f --- /dev/null +++ b/cpp/tests/experimental/degree_test.cpp @@ -0,0 +1,167 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governin_from_mtxg permissions and + * limitations under the License. + */ + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +template +void degree_reference(edge_t const* offsets, + vertex_t const* indices, + edge_t* degrees, + vertex_t num_vertices, + bool major) +{ + if (major) { + std::adjacent_difference(offsets + 1, offsets + num_vertices + 1, degrees); + } else { + std::fill(degrees, degrees + num_vertices, edge_t{0}); + for (vertex_t i = 0; i < num_vertices; ++i) { + for (auto j = offsets[i]; j < offsets[i + 1]; ++j) { + auto nbr = indices[j]; + ++degrees[nbr]; + } + } + } + + return; +} + +typedef struct Degree_Usecase_t { + std::string graph_file_full_path{}; + + Degree_Usecase_t(std::string const& graph_file_path) + { + if ((graph_file_path.length() > 0) && (graph_file_path[0] != '/')) { + graph_file_full_path = cugraph::test::get_rapids_dataset_root_dir() + "/" + graph_file_path; + } else { + graph_file_full_path = graph_file_path; + } + }; +} Degree_Usecase; + +class Tests_Degree : public ::testing::TestWithParam { + public: + Tests_Degree() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + + virtual void SetUp() {} + virtual void TearDown() {} + + template + void run_current_test(Degree_Usecase const& configuration) + { + raft::handle_t handle{}; + + cugraph::experimental::graph_t graph( + handle); + std::tie(graph, std::ignore) = cugraph::test:: + read_graph_from_matrix_market_file( + handle, configuration.graph_file_full_path, false, false); + auto graph_view = graph.view(); + + std::vector h_offsets(graph_view.get_number_of_vertices() + 1); + std::vector h_indices(graph_view.get_number_of_edges()); + raft::update_host(h_offsets.data(), + graph_view.get_matrix_partition_view().get_offsets(), + graph_view.get_number_of_vertices() + 1, + handle.get_stream()); + raft::update_host(h_indices.data(), + graph_view.get_matrix_partition_view().get_indices(), + graph_view.get_number_of_edges(), + handle.get_stream()); + CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); + + std::vector h_reference_in_degrees(graph_view.get_number_of_vertices()); + std::vector h_reference_out_degrees(graph_view.get_number_of_vertices()); + + degree_reference(h_offsets.data(), + h_indices.data(), + h_reference_in_degrees.data(), + graph_view.get_number_of_vertices(), + store_transposed); + + degree_reference(h_offsets.data(), + h_indices.data(), + h_reference_out_degrees.data(), + graph_view.get_number_of_vertices(), + !store_transposed); + + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + + auto d_in_degrees = graph_view.compute_in_degrees(handle); + auto d_out_degrees = graph_view.compute_out_degrees(handle); + + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + + std::vector h_cugraph_in_degrees(graph_view.get_number_of_vertices()); + std::vector h_cugraph_out_degrees(graph_view.get_number_of_vertices()); + + raft::update_host( + h_cugraph_in_degrees.data(), d_in_degrees.data(), d_in_degrees.size(), handle.get_stream()); + raft::update_host(h_cugraph_out_degrees.data(), + d_out_degrees.data(), + d_out_degrees.size(), + handle.get_stream()); + CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); + + ASSERT_TRUE(std::equal( + h_reference_in_degrees.begin(), h_reference_in_degrees.end(), h_cugraph_in_degrees.begin())) + << "In-degree values do not match with the reference values."; + ASSERT_TRUE(std::equal(h_reference_out_degrees.begin(), + h_reference_out_degrees.end(), + h_cugraph_out_degrees.begin())) + << "Out-degree values do not match with the reference values."; + } +}; + +// FIXME: add tests for type combinations + +TEST_P(Tests_Degree, CheckInt32Int32FloatTransposed) +{ + run_current_test(GetParam()); +} + +TEST_P(Tests_Degree, CheckInt32Int32FloatUntransposed) +{ + run_current_test(GetParam()); +} + +INSTANTIATE_TEST_SUITE_P(simple_test, + Tests_Degree, + ::testing::Values(Degree_Usecase("test/datasets/karate.mtx"), + Degree_Usecase("test/datasets/web-Google.mtx"), + Degree_Usecase("test/datasets/ljournal-2008.mtx"), + Degree_Usecase("test/datasets/webbase-1M.mtx"))); + +CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/experimental/generate_rmat_test.cpp b/cpp/tests/experimental/generate_rmat_test.cpp new file mode 100644 index 00000000000..7c2dbb3911a --- /dev/null +++ b/cpp/tests/experimental/generate_rmat_test.cpp @@ -0,0 +1,372 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governin_from_mtxg permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include + +#include +#include +#include + +// this function assumes that vertex IDs are not scrambled +template +void validate_rmat_distribution( + std::tuple* edges, + size_t num_edges, + vertex_t src_first, + vertex_t src_last, + vertex_t dst_first, + vertex_t dst_last, + double a, + double b, + double c, + bool clip_and_flip, + size_t min_edges /* stop recursion if # edges < min_edges */, + double error_tolerance /* (computed a|b|c - input a|b|c) shoud be smaller than error_tolerance*/) +{ + // we cannot expect the ratios of the edges in the four quadrants of the graph adjacency matrix to + // converge close to a, b, c, d if num_edges is not large enough. + if (num_edges < min_edges) { return; } + + auto src_threshold = (src_first + src_last) / 2; + auto dst_threshold = (dst_first + dst_last) / 2; + + auto a_plus_b_last = std::partition(edges, edges + num_edges, [src_threshold](auto edge) { + return std::get<0>(edge) < src_threshold; + }); + auto a_last = std::partition( + edges, a_plus_b_last, [dst_threshold](auto edge) { return std::get<1>(edge) < dst_threshold; }); + auto c_last = std::partition(a_plus_b_last, edges + num_edges, [dst_threshold](auto edge) { + return std::get<1>(edge) < dst_threshold; + }); + + ASSERT_TRUE(std::abs((double)std::distance(edges, a_last) / num_edges - a) < error_tolerance) + << "# edges=" << num_edges << " computed a=" << (double)std::distance(edges, a_last) / num_edges + << " iput a=" << a << " error tolerance=" << error_tolerance << "."; + if (clip_and_flip && (src_first == dst_first) && + (src_last == dst_last)) { // if clip_and_flip and in the diagonal + ASSERT_TRUE(std::distance(a_last, a_plus_b_last) == 0); + ASSERT_TRUE(std::abs((double)std::distance(a_plus_b_last, c_last) / num_edges - (b + c)) < + error_tolerance) + << "# edges=" << num_edges + << " computed c=" << (double)std::distance(a_plus_b_last, c_last) / num_edges + << " iput (b + c)=" << (b + c) << " error tolerance=" << error_tolerance << "."; + } else { + ASSERT_TRUE(std::abs((double)std::distance(a_last, a_plus_b_last) / num_edges - b) < + error_tolerance) + << "# edges=" << num_edges + << " computed b=" << (double)std::distance(a_last, a_plus_b_last) / num_edges + << " iput b=" << b << " error tolerance=" << error_tolerance << "."; + ASSERT_TRUE(std::abs((double)std::distance(a_plus_b_last, c_last) / num_edges - c) < + error_tolerance) + << "# edges=" << num_edges + << " computed c=" << (double)std::distance(a_plus_b_last, c_last) / num_edges + << " iput c=" << c << " error tolerance=" << error_tolerance << "."; + } + + validate_rmat_distribution(edges, + std::distance(edges, a_last), + src_first, + src_threshold, + dst_first, + dst_threshold, + a, + b, + c, + clip_and_flip, + min_edges, + error_tolerance); + validate_rmat_distribution(a_last, + std::distance(a_last, a_plus_b_last), + src_first, + (src_first + src_last) / 2, + dst_threshold, + dst_last, + a, + b, + c, + clip_and_flip, + min_edges, + error_tolerance); + validate_rmat_distribution(a_plus_b_last, + std::distance(a_plus_b_last, c_last), + src_threshold, + src_last, + dst_first, + dst_threshold, + a, + b, + c, + clip_and_flip, + min_edges, + error_tolerance); + validate_rmat_distribution(c_last, + std::distance(c_last, edges + num_edges), + src_threshold, + src_last, + dst_threshold, + dst_last, + a, + b, + c, + clip_and_flip, + min_edges, + error_tolerance); + + return; +} + +typedef struct GenerateRmat_Usecase_t { + size_t scale{0}; + size_t edge_factor{0}; + double a{0.0}; + double b{0.0}; + double c{0.0}; + bool clip_and_flip{false}; + + GenerateRmat_Usecase_t( + size_t scale, size_t edge_factor, double a, double b, double c, bool clip_and_flip) + : scale(scale), edge_factor(edge_factor), a(a), b(b), c(c), clip_and_flip(clip_and_flip){}; +} GenerateRmat_Usecase; + +class Tests_GenerateRmat : public ::testing::TestWithParam { + public: + Tests_GenerateRmat() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + + virtual void SetUp() {} + virtual void TearDown() {} + + template + void run_current_test(GenerateRmat_Usecase const& configuration) + { + raft::handle_t handle{}; + + auto num_vertices = static_cast(size_t{1} << configuration.scale); + std::vector no_scramble_out_degrees(num_vertices, 0); + std::vector no_scramble_in_degrees(num_vertices, 0); + std::vector scramble_out_degrees(num_vertices, 0); + std::vector scramble_in_degrees(num_vertices, 0); + for (size_t scramble = 0; scramble < 2; ++scramble) { + rmm::device_uvector d_srcs(0, handle.get_stream()); + rmm::device_uvector d_dsts(0, handle.get_stream()); + + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + + std::tie(d_srcs, d_dsts) = cugraph::generate_rmat_edgelist( + handle, + configuration.scale, + (size_t{1} << configuration.scale) * configuration.edge_factor, + configuration.a, + configuration.b, + configuration.c, + uint64_t{0}, + configuration.clip_and_flip); + // static_cast(scramble)); + + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + + std::vector h_cugraph_srcs(d_srcs.size()); + std::vector h_cugraph_dsts(d_dsts.size()); + + raft::update_host(h_cugraph_srcs.data(), d_srcs.data(), d_srcs.size(), handle.get_stream()); + raft::update_host(h_cugraph_dsts.data(), d_dsts.data(), d_dsts.size(), handle.get_stream()); + CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); + + ASSERT_TRUE( + (h_cugraph_srcs.size() == (size_t{1} << configuration.scale) * configuration.edge_factor) && + (h_cugraph_dsts.size() == (size_t{1} << configuration.scale) * configuration.edge_factor)) + << "Returned an invalid number of R-mat graph edges."; + ASSERT_TRUE(std::count_if(h_cugraph_srcs.begin(), + h_cugraph_srcs.end(), + [num_vertices = static_cast( + size_t{1} << configuration.scale)](auto v) { + return !cugraph::experimental::is_valid_vertex(num_vertices, v); + }) == 0) + << "Returned R-mat graph edges have invalid source vertex IDs."; + ASSERT_TRUE(std::count_if(h_cugraph_dsts.begin(), + h_cugraph_dsts.end(), + [num_vertices = static_cast( + size_t{1} << configuration.scale)](auto v) { + return !cugraph::experimental::is_valid_vertex(num_vertices, v); + }) == 0) + << "Returned R-mat graph edges have invalid destination vertex IDs."; + + if (!scramble) { + if (configuration.clip_and_flip) { + for (size_t i = 0; i < h_cugraph_srcs.size(); ++i) { + ASSERT_TRUE(h_cugraph_srcs[i] >= h_cugraph_dsts[i]); + } + } + + std::vector> h_cugraph_edges(h_cugraph_srcs.size()); + for (size_t i = 0; i < h_cugraph_srcs.size(); ++i) { + h_cugraph_edges[i] = std::make_tuple(h_cugraph_srcs[i], h_cugraph_dsts[i]); + } + + validate_rmat_distribution(h_cugraph_edges.data(), + h_cugraph_edges.size(), + vertex_t{0}, + num_vertices, + vertex_t{0}, + num_vertices, + configuration.a, + configuration.b, + configuration.c, + configuration.clip_and_flip, + size_t{100000}, + 0.01); + } + + if (scramble) { + std::for_each(h_cugraph_srcs.begin(), + h_cugraph_srcs.end(), + [&scramble_out_degrees](auto src) { scramble_out_degrees[src]++; }); + std::for_each(h_cugraph_dsts.begin(), + h_cugraph_dsts.end(), + [&scramble_in_degrees](auto dst) { scramble_in_degrees[dst]++; }); + std::sort(scramble_out_degrees.begin(), scramble_out_degrees.end()); + std::sort(scramble_in_degrees.begin(), scramble_in_degrees.end()); + } else { + std::for_each(h_cugraph_srcs.begin(), + h_cugraph_srcs.end(), + [&no_scramble_out_degrees](auto src) { no_scramble_out_degrees[src]++; }); + std::for_each(h_cugraph_dsts.begin(), + h_cugraph_dsts.end(), + [&no_scramble_in_degrees](auto dst) { no_scramble_in_degrees[dst]++; }); + std::sort(no_scramble_out_degrees.begin(), no_scramble_out_degrees.end()); + std::sort(no_scramble_in_degrees.begin(), no_scramble_in_degrees.end()); + } + } + + // this relies on the fact that the edge generator is deterministic. + // ideally, we should test that the two graphs are isomorphic, but this is NP hard; insted, we + // just check out-degree & in-degree distributions + ASSERT_TRUE(std::equal(no_scramble_out_degrees.begin(), + no_scramble_out_degrees.end(), + scramble_out_degrees.begin())); + ASSERT_TRUE(std::equal( + no_scramble_in_degrees.begin(), no_scramble_in_degrees.end(), scramble_in_degrees.begin())); + } +}; + +// FIXME: add tests for type combinations + +TEST_P(Tests_GenerateRmat, CheckInt32) { run_current_test(GetParam()); } + +INSTANTIATE_TEST_SUITE_P(simple_test, + Tests_GenerateRmat, + ::testing::Values(GenerateRmat_Usecase(20, 16, 0.57, 0.19, 0.19, true), + GenerateRmat_Usecase(20, 16, 0.57, 0.19, 0.19, false), + GenerateRmat_Usecase(20, 16, 0.45, 0.22, 0.22, true), + GenerateRmat_Usecase(20, 16, 0.45, 0.22, 0.22, false))); +typedef struct GenerateRmats_Usecase_t { + size_t n_edgelists{0}; + size_t min_scale{0}; + size_t max_scale{0}; + size_t edge_factor{0}; + cugraph::generator_distribution_t component_distribution; + cugraph::generator_distribution_t edge_distribution; + + GenerateRmats_Usecase_t(size_t n_edgelists, + size_t min_scale, + size_t max_scale, + size_t edge_factor, + cugraph::generator_distribution_t component_distribution, + cugraph::generator_distribution_t edge_distribution) + : n_edgelists(n_edgelists), + min_scale(min_scale), + max_scale(max_scale), + component_distribution(component_distribution), + edge_distribution(edge_distribution), + edge_factor(edge_factor){}; +} GenerateRmats_Usecase; +class Tests_GenerateRmats : public ::testing::TestWithParam { + public: + Tests_GenerateRmats() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + + virtual void SetUp() {} + virtual void TearDown() {} + + template + void run_current_test(GenerateRmats_Usecase const& configuration) + { + raft::handle_t handle{}; + + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + + auto outputs = cugraph::generate_rmat_edgelists(handle, + configuration.n_edgelists, + configuration.min_scale, + configuration.max_scale, + configuration.edge_factor, + configuration.component_distribution, + configuration.edge_distribution, + uint64_t{0}); + + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + ASSERT_EQ(configuration.n_edgelists, outputs.size()); + for (auto i = outputs.begin(); i != outputs.end(); ++i) { + ASSERT_EQ(std::get<0>(*i).size(), std::get<1>(*i).size()); + ASSERT_TRUE((configuration.min_scale * configuration.edge_factor) <= std::get<0>(*i).size()); + ASSERT_TRUE((configuration.max_scale * configuration.edge_factor) >= std::get<0>(*i).size()); + } + } +}; +TEST_P(Tests_GenerateRmats, CheckInt32) { run_current_test(GetParam()); } + +INSTANTIATE_TEST_SUITE_P( + simple_test, + Tests_GenerateRmats, + ::testing::Values(GenerateRmats_Usecase(8, + 1, + 16, + 32, + cugraph::generator_distribution_t::UNIFORM, + cugraph::generator_distribution_t::UNIFORM), + GenerateRmats_Usecase(8, + 1, + 16, + 32, + cugraph::generator_distribution_t::UNIFORM, + cugraph::generator_distribution_t::POWER_LAW), + GenerateRmats_Usecase(8, + 3, + 16, + 32, + cugraph::generator_distribution_t::POWER_LAW, + cugraph::generator_distribution_t::UNIFORM), + GenerateRmats_Usecase(8, + 3, + 16, + 32, + cugraph::generator_distribution_t::POWER_LAW, + cugraph::generator_distribution_t::POWER_LAW))); +CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/experimental/graph_test.cpp b/cpp/tests/experimental/graph_test.cpp index b80de68f95c..dff841a5b73 100644 --- a/cpp/tests/experimental/graph_test.cpp +++ b/cpp/tests/experimental/graph_test.cpp @@ -1,5 +1,5 @@ /* - * 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. @@ -17,8 +17,8 @@ #include #include -#include -#include +#include +#include #include #include @@ -34,16 +34,18 @@ #include template -std::tuple, std::vector, std::vector> graph_reference( - vertex_t const* p_src_vertices, - vertex_t const* p_dst_vertices, - weight_t const* p_edge_weights, - vertex_t number_of_vertices, - edge_t number_of_edges) +std::tuple, std::vector, std::optional>> +graph_reference(vertex_t const* p_src_vertices, + vertex_t const* p_dst_vertices, + std::optional p_edge_weights, + vertex_t number_of_vertices, + edge_t number_of_edges) { std::vector offsets(number_of_vertices + 1, edge_t{0}); std::vector indices(number_of_edges, vertex_t{0}); - std::vector weights(p_edge_weights != nullptr ? number_of_edges : 0, weight_t{0.0}); + auto weights = p_edge_weights + ? std::make_optional>(number_of_edges, weight_t{0.0}) + : std::nullopt; for (size_t i = 0; i < number_of_edges; ++i) { auto major = store_transposed ? p_dst_vertices[i] : p_src_vertices[i]; @@ -58,7 +60,7 @@ std::tuple, std::vector, std::vector> gr auto degree = offsets[major + 1] - start; auto idx = indices[start + degree - 1]++; indices[start + idx] = minor; - if (p_edge_weights != nullptr) { weights[start + idx] = p_edge_weights[i]; } + if (p_edge_weights) { (*weights)[start + idx] = (*p_edge_weights)[i]; } } return std::make_tuple(std::move(offsets), std::move(indices), std::move(weights)); @@ -91,43 +93,38 @@ class Tests_Graph : public ::testing::TestWithParam { template void run_current_test(Graph_Usecase const& configuration) { - auto mm_graph = - cugraph::test::read_edgelist_from_matrix_market_file( - configuration.graph_file_full_path); - edge_t number_of_edges = static_cast(mm_graph.h_rows.size()); + raft::handle_t handle{}; - std::vector h_reference_offsets{}; - std::vector h_reference_indices{}; - std::vector h_reference_weights{}; + auto [d_rows, d_cols, d_weights, number_of_vertices, is_symmetric] = + cugraph::test::read_edgelist_from_matrix_market_file( + handle, configuration.graph_file_full_path, configuration.test_weighted); + edge_t number_of_edges = static_cast(d_rows.size()); + + std::vector h_rows(number_of_edges); + std::vector h_cols(number_of_edges); + auto h_weights = + d_weights ? std::make_optional>(number_of_edges) : std::nullopt; + + raft::update_host(h_rows.data(), d_rows.data(), number_of_edges, handle.get_stream()); + raft::update_host(h_cols.data(), d_cols.data(), number_of_edges, handle.get_stream()); + if (h_weights) { + raft::update_host( + (*h_weights).data(), (*d_weights).data(), number_of_edges, handle.get_stream()); + } + CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); - std::tie(h_reference_offsets, h_reference_indices, h_reference_weights) = + auto [h_reference_offsets, h_reference_indices, h_reference_weights] = graph_reference( - mm_graph.h_rows.data(), - mm_graph.h_cols.data(), - configuration.test_weighted ? mm_graph.h_weights.data() : nullptr, - mm_graph.number_of_vertices, + h_rows.data(), + h_cols.data(), + h_weights ? std::optional{(*h_weights).data()} : std::nullopt, + number_of_vertices, number_of_edges); - raft::handle_t handle{}; - - rmm::device_uvector d_rows(number_of_edges, handle.get_stream()); - rmm::device_uvector d_cols(number_of_edges, handle.get_stream()); - rmm::device_uvector d_weights(configuration.test_weighted ? number_of_edges : 0, - handle.get_stream()); - - raft::update_device( - d_rows.data(), mm_graph.h_rows.data(), number_of_edges, handle.get_stream()); - raft::update_device( - d_cols.data(), mm_graph.h_cols.data(), number_of_edges, handle.get_stream()); - if (configuration.test_weighted) { - raft::update_device( - d_weights.data(), mm_graph.h_weights.data(), number_of_edges, handle.get_stream()); - } - cugraph::experimental::edgelist_t edgelist{ d_rows.data(), d_cols.data(), - configuration.test_weighted ? d_weights.data() : nullptr, + d_weights ? std::optional{(*d_weights).data()} : std::nullopt, number_of_edges}; CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement @@ -136,34 +133,35 @@ class Tests_Graph : public ::testing::TestWithParam { cugraph::experimental::graph_t( handle, edgelist, - mm_graph.number_of_vertices, - cugraph::experimental::graph_properties_t{mm_graph.is_symmetric, false}, - false, + number_of_vertices, + cugraph::experimental::graph_properties_t{is_symmetric, false}, + std::nullopt, true); auto graph_view = graph.view(); CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement - ASSERT_EQ(graph_view.get_number_of_vertices(), mm_graph.number_of_vertices); + ASSERT_EQ(graph_view.get_number_of_vertices(), number_of_vertices); ASSERT_EQ(graph_view.get_number_of_edges(), number_of_edges); std::vector h_cugraph_offsets(graph_view.get_number_of_vertices() + 1); std::vector h_cugraph_indices(graph_view.get_number_of_edges()); - std::vector h_cugraph_weights( - configuration.test_weighted ? graph_view.get_number_of_edges() : 0); + auto h_cugraph_weights = + graph.is_weighted() ? std::optional>(graph_view.get_number_of_edges()) + : std::nullopt; raft::update_host(h_cugraph_offsets.data(), - graph_view.offsets(), + graph_view.get_matrix_partition_view().get_offsets(), graph_view.get_number_of_vertices() + 1, handle.get_stream()); raft::update_host(h_cugraph_indices.data(), - graph_view.indices(), + graph_view.get_matrix_partition_view().get_indices(), graph_view.get_number_of_edges(), handle.get_stream()); - if (configuration.test_weighted) { - raft::update_host(h_cugraph_weights.data(), - graph_view.weights(), + if (h_cugraph_weights) { + raft::update_host((*h_cugraph_weights).data(), + *(graph_view.get_matrix_partition_view().get_weights()), graph_view.get_number_of_edges(), handle.get_stream()); } @@ -173,8 +171,11 @@ class Tests_Graph : public ::testing::TestWithParam { ASSERT_TRUE( std::equal(h_reference_offsets.begin(), h_reference_offsets.end(), h_cugraph_offsets.begin())) << "Graph compressed sparse format offsets do not match with the reference values."; - ASSERT_EQ(h_reference_weights.size(), h_cugraph_weights.size()); - for (vertex_t i = 0; i < mm_graph.number_of_vertices; ++i) { + ASSERT_EQ(h_reference_weights.has_value(), h_cugraph_weights.has_value()); + if (h_reference_weights) { + ASSERT_EQ((*h_reference_weights).size(), (*h_cugraph_weights).size()); + } + for (vertex_t i = 0; i < number_of_vertices; ++i) { auto start = h_reference_offsets[i]; auto degree = h_reference_offsets[i + 1] - start; if (configuration.test_weighted) { @@ -182,9 +183,9 @@ class Tests_Graph : public ::testing::TestWithParam { std::vector> cugraph_pairs(degree); for (edge_t j = 0; j < degree; ++j) { reference_pairs[j] = - std::make_tuple(h_reference_indices[start + j], h_reference_weights[start + j]); + std::make_tuple(h_reference_indices[start + j], (*h_reference_weights)[start + j]); cugraph_pairs[j] = - std::make_tuple(h_cugraph_indices[start + j], h_cugraph_weights[start + j]); + std::make_tuple(h_cugraph_indices[start + j], (*h_cugraph_weights)[start + j]); } std::sort(reference_pairs.begin(), reference_pairs.end()); std::sort(cugraph_pairs.begin(), cugraph_pairs.end()); @@ -228,15 +229,15 @@ TEST_P(Tests_Graph, CheckStoreTransposedTrue) run_current_test(GetParam()); } -INSTANTIATE_TEST_CASE_P(simple_test, - Tests_Graph, - ::testing::Values(Graph_Usecase("test/datasets/karate.mtx", false), - Graph_Usecase("test/datasets/karate.mtx", true), - Graph_Usecase("test/datasets/web-Google.mtx", false), - Graph_Usecase("test/datasets/web-Google.mtx", true), - Graph_Usecase("test/datasets/ljournal-2008.mtx", false), - Graph_Usecase("test/datasets/ljournal-2008.mtx", true), - Graph_Usecase("test/datasets/webbase-1M.mtx", false), - Graph_Usecase("test/datasets/webbase-1M.mtx", true))); +INSTANTIATE_TEST_SUITE_P(simple_test, + Tests_Graph, + ::testing::Values(Graph_Usecase("test/datasets/karate.mtx", false), + Graph_Usecase("test/datasets/karate.mtx", true), + Graph_Usecase("test/datasets/web-Google.mtx", false), + Graph_Usecase("test/datasets/web-Google.mtx", true), + Graph_Usecase("test/datasets/ljournal-2008.mtx", false), + Graph_Usecase("test/datasets/ljournal-2008.mtx", true), + Graph_Usecase("test/datasets/webbase-1M.mtx", false), + Graph_Usecase("test/datasets/webbase-1M.mtx", true))); CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/experimental/induced_subgraph_test.cpp b/cpp/tests/experimental/induced_subgraph_test.cpp new file mode 100644 index 00000000000..3e2dbf4fe3c --- /dev/null +++ b/cpp/tests/experimental/induced_subgraph_test.cpp @@ -0,0 +1,326 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governin_from_mtxg permissions and + * limitations under the License. + */ + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +template +std::tuple, + std::vector, + std::optional>, + std::vector> +extract_induced_subgraph_reference(edge_t const* offsets, + vertex_t const* indices, + std::optional weights, + size_t const* subgraph_offsets, + vertex_t const* subgraph_vertices, + vertex_t num_vertices, + size_t num_subgraphs) +{ + std::vector edgelist_majors{}; + std::vector edgelist_minors{}; + auto edgelist_weights = weights ? std::make_optional>(0) : std::nullopt; + std::vector subgraph_edge_offsets{0}; + + for (size_t i = 0; i < num_subgraphs; ++i) { + std::for_each(subgraph_vertices + subgraph_offsets[i], + subgraph_vertices + subgraph_offsets[i + 1], + [offsets, + indices, + weights, + subgraph_vertices, + subgraph_offsets, + &edgelist_majors, + &edgelist_minors, + &edgelist_weights, + i](auto v) { + auto first = offsets[v]; + auto last = offsets[v + 1]; + for (auto j = first; j < last; ++j) { + if (std::binary_search(subgraph_vertices + subgraph_offsets[i], + subgraph_vertices + subgraph_offsets[i + 1], + indices[j])) { + edgelist_majors.push_back(v); + edgelist_minors.push_back(indices[j]); + if (weights) { (*edgelist_weights).push_back((*weights)[j]); } + } + } + }); + subgraph_edge_offsets.push_back(edgelist_majors.size()); + } + + return std::make_tuple(edgelist_majors, edgelist_minors, edgelist_weights, subgraph_edge_offsets); +} + +typedef struct InducedSubgraph_Usecase_t { + std::string graph_file_full_path{}; + std::vector subgraph_sizes{}; + bool test_weighted{false}; + + InducedSubgraph_Usecase_t(std::string const& graph_file_path, + std::vector const& subgraph_sizes, + bool test_weighted) + : subgraph_sizes(subgraph_sizes), test_weighted(test_weighted) + { + if ((graph_file_path.length() > 0) && (graph_file_path[0] != '/')) { + graph_file_full_path = cugraph::test::get_rapids_dataset_root_dir() + "/" + graph_file_path; + } else { + graph_file_full_path = graph_file_path; + } + }; +} InducedSubgraph_Usecase; + +class Tests_InducedSubgraph : public ::testing::TestWithParam { + public: + Tests_InducedSubgraph() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + + virtual void SetUp() {} + virtual void TearDown() {} + + template + void run_current_test(InducedSubgraph_Usecase const& configuration) + { + raft::handle_t handle{}; + + cugraph::experimental::graph_t graph( + handle); + std::tie(graph, std::ignore) = cugraph::test:: + read_graph_from_matrix_market_file( + handle, configuration.graph_file_full_path, configuration.test_weighted, false); + auto graph_view = graph.view(); + + std::vector h_offsets(graph_view.get_number_of_vertices() + 1); + std::vector h_indices(graph_view.get_number_of_edges()); + auto h_weights = graph_view.is_weighted() ? std::make_optional>( + graph_view.get_number_of_edges(), weight_t{0.0}) + : std::nullopt; + raft::update_host(h_offsets.data(), + graph_view.get_matrix_partition_view().get_offsets(), + graph_view.get_number_of_vertices() + 1, + handle.get_stream()); + raft::update_host(h_indices.data(), + graph_view.get_matrix_partition_view().get_indices(), + graph_view.get_number_of_edges(), + handle.get_stream()); + if (h_weights) { + raft::update_host((*h_weights).data(), + *(graph_view.get_matrix_partition_view().get_weights()), + graph_view.get_number_of_edges(), + handle.get_stream()); + } + CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); + + std::vector h_subgraph_offsets(configuration.subgraph_sizes.size() + 1, 0); + std::partial_sum(configuration.subgraph_sizes.begin(), + configuration.subgraph_sizes.end(), + h_subgraph_offsets.begin() + 1); + std::vector h_subgraph_vertices( + h_subgraph_offsets.back(), cugraph::experimental::invalid_vertex_id::value); + std::default_random_engine generator{}; + std::uniform_int_distribution distribution{0, + graph_view.get_number_of_vertices() - 1}; + + for (size_t i = 0; i < configuration.subgraph_sizes.size(); ++i) { + auto start = h_subgraph_offsets[i]; + auto last = h_subgraph_offsets[i + 1]; + ASSERT_TRUE(last - start <= graph_view.get_number_of_vertices()) << "Invalid subgraph size."; + // this is inefficient if last - start << graph_view.get_number_of_vertices() but this is for + // the test puspose only and the time & memory cost is only linear to + // graph_view.get_number_of_vertices(), so this may not matter. + std::vector vertices(graph_view.get_number_of_vertices()); + std::iota(vertices.begin(), vertices.end(), vertex_t{0}); + std::random_shuffle(vertices.begin(), vertices.end()); + std::copy( + vertices.begin(), vertices.begin() + (last - start), h_subgraph_vertices.begin() + start); + std::sort(h_subgraph_vertices.begin() + start, h_subgraph_vertices.begin() + last); + } + + rmm::device_uvector d_subgraph_offsets(h_subgraph_offsets.size(), handle.get_stream()); + rmm::device_uvector d_subgraph_vertices(h_subgraph_vertices.size(), + handle.get_stream()); + raft::update_device(d_subgraph_offsets.data(), + h_subgraph_offsets.data(), + h_subgraph_offsets.size(), + handle.get_stream()); + raft::update_device(d_subgraph_vertices.data(), + h_subgraph_vertices.data(), + h_subgraph_vertices.size(), + handle.get_stream()); + + auto [h_reference_subgraph_edgelist_majors, + h_reference_subgraph_edgelist_minors, + h_reference_subgraph_edgelist_weights, + h_reference_subgraph_edge_offsets] = + extract_induced_subgraph_reference( + h_offsets.data(), + h_indices.data(), + h_weights ? std::optional{(*h_weights).data()} : std::nullopt, + h_subgraph_offsets.data(), + h_subgraph_vertices.data(), + graph_view.get_number_of_vertices(), + configuration.subgraph_sizes.size()); + + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + + // FIXME: turn-off do_expensive_check once verified. + auto [d_subgraph_edgelist_majors, + d_subgraph_edgelist_minors, + d_subgraph_edgelist_weights, + d_subgraph_edge_offsets] = + cugraph::experimental::extract_induced_subgraphs(handle, + graph_view, + d_subgraph_offsets.data(), + d_subgraph_vertices.data(), + configuration.subgraph_sizes.size(), + true); + + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + + std::vector h_cugraph_subgraph_edgelist_majors(d_subgraph_edgelist_majors.size()); + std::vector h_cugraph_subgraph_edgelist_minors(d_subgraph_edgelist_minors.size()); + auto h_cugraph_subgraph_edgelist_weights = + d_subgraph_edgelist_weights + ? std::make_optional>((*d_subgraph_edgelist_weights).size()) + : std::nullopt; + std::vector h_cugraph_subgraph_edge_offsets(d_subgraph_edge_offsets.size()); + + raft::update_host(h_cugraph_subgraph_edgelist_majors.data(), + d_subgraph_edgelist_majors.data(), + d_subgraph_edgelist_majors.size(), + handle.get_stream()); + raft::update_host(h_cugraph_subgraph_edgelist_minors.data(), + d_subgraph_edgelist_minors.data(), + d_subgraph_edgelist_minors.size(), + handle.get_stream()); + if (d_subgraph_edgelist_weights) { + raft::update_host((*h_cugraph_subgraph_edgelist_weights).data(), + (*d_subgraph_edgelist_weights).data(), + (*d_subgraph_edgelist_weights).size(), + handle.get_stream()); + } + raft::update_host(h_cugraph_subgraph_edge_offsets.data(), + d_subgraph_edge_offsets.data(), + d_subgraph_edge_offsets.size(), + handle.get_stream()); + CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); + + ASSERT_TRUE(h_reference_subgraph_edge_offsets.size() == h_cugraph_subgraph_edge_offsets.size()) + << "Returned subgraph edge offset vector has an invalid size."; + ASSERT_TRUE(std::equal(h_reference_subgraph_edge_offsets.begin(), + h_reference_subgraph_edge_offsets.end(), + h_cugraph_subgraph_edge_offsets.begin())) + << "Returned subgraph edge offset values do not match with the reference values."; + ASSERT_TRUE(h_reference_subgraph_edgelist_weights.has_value() == configuration.test_weighted); + ASSERT_TRUE(h_cugraph_subgraph_edgelist_weights.has_value() == + h_reference_subgraph_edgelist_weights.has_value()); + + for (size_t i = 0; i < configuration.subgraph_sizes.size(); ++i) { + auto start = h_reference_subgraph_edge_offsets[i]; + auto last = h_reference_subgraph_edge_offsets[i + 1]; + if (configuration.test_weighted) { + std::vector> reference_tuples(last - start); + std::vector> cugraph_tuples(last - start); + for (auto j = start; j < last; ++j) { + reference_tuples[j - start] = + std::make_tuple(h_reference_subgraph_edgelist_majors[j], + h_reference_subgraph_edgelist_minors[j], + (*h_reference_subgraph_edgelist_weights)[j]); + cugraph_tuples[j - start] = std::make_tuple(h_cugraph_subgraph_edgelist_majors[j], + h_cugraph_subgraph_edgelist_minors[j], + (*h_cugraph_subgraph_edgelist_weights)[j]); + } + ASSERT_TRUE( + std::equal(reference_tuples.begin(), reference_tuples.end(), cugraph_tuples.begin())) + << "Extracted subgraph edges do not match with the edges extracted by the reference " + "implementation."; + } else { + std::vector> reference_tuples(last - start); + std::vector> cugraph_tuples(last - start); + for (auto j = start; j < last; ++j) { + reference_tuples[j - start] = std::make_tuple(h_reference_subgraph_edgelist_majors[j], + h_reference_subgraph_edgelist_minors[j]); + cugraph_tuples[j - start] = std::make_tuple(h_cugraph_subgraph_edgelist_majors[j], + h_cugraph_subgraph_edgelist_minors[j]); + } + ASSERT_TRUE( + std::equal(reference_tuples.begin(), reference_tuples.end(), cugraph_tuples.begin())) + << "Extracted subgraph edges do not match with the edges extracted by the reference " + "implementation."; + } + } + } +}; + +// FIXME: add tests for type combinations + +TEST_P(Tests_InducedSubgraph, CheckInt32Int32FloatTransposed) +{ + run_current_test(GetParam()); +} + +TEST_P(Tests_InducedSubgraph, CheckInt32Int32FloatUntransposed) +{ + run_current_test(GetParam()); +} + +INSTANTIATE_TEST_SUITE_P( + simple_test, + Tests_InducedSubgraph, + ::testing::Values( + InducedSubgraph_Usecase("test/datasets/karate.mtx", std::vector{0}, false), + InducedSubgraph_Usecase("test/datasets/karate.mtx", std::vector{1}, false), + InducedSubgraph_Usecase("test/datasets/karate.mtx", std::vector{10}, false), + InducedSubgraph_Usecase("test/datasets/karate.mtx", std::vector{34}, false), + InducedSubgraph_Usecase("test/datasets/karate.mtx", std::vector{10, 0, 5}, false), + InducedSubgraph_Usecase("test/datasets/karate.mtx", std::vector{9, 3, 10}, false), + InducedSubgraph_Usecase("test/datasets/karate.mtx", std::vector{5, 12, 13}, true), + InducedSubgraph_Usecase("test/datasets/web-Google.mtx", + std::vector{250, 130, 15}, + false), + InducedSubgraph_Usecase("test/datasets/web-Google.mtx", + std::vector{125, 300, 70}, + true), + InducedSubgraph_Usecase("test/datasets/ljournal-2008.mtx", + std::vector{300, 20, 400}, + false), + InducedSubgraph_Usecase("test/datasets/ljournal-2008.mtx", + std::vector{9130, 1200, 300}, + true), + InducedSubgraph_Usecase("test/datasets/webbase-1M.mtx", std::vector{700}, false), + InducedSubgraph_Usecase("test/datasets/webbase-1M.mtx", std::vector{500}, true))); + +CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/experimental/katz_centrality_test.cpp b/cpp/tests/experimental/katz_centrality_test.cpp index c2ac4340319..0a4fba9acd1 100644 --- a/cpp/tests/experimental/katz_centrality_test.cpp +++ b/cpp/tests/experimental/katz_centrality_test.cpp @@ -1,5 +1,5 @@ /* - * 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. @@ -14,12 +14,16 @@ * limitations under the License. */ +#include #include +#include #include +#include -#include -#include -#include +#include +#include +#include +#include #include #include @@ -34,11 +38,16 @@ #include #include +// do the perf measurements +// enabled by command line parameter s'--perf' +// +static int PERF = 0; + template -void katz_centrality_reference(edge_t* offsets, - vertex_t* indices, - weight_t* weights, - result_t* betas, +void katz_centrality_reference(edge_t const* offsets, + vertex_t const* indices, + std::optional weights, + result_t const* betas, result_t* katz_centralities, vertex_t num_vertices, result_t alpha, @@ -62,7 +71,7 @@ void katz_centrality_reference(edge_t* offsets, katz_centralities[i] = betas != nullptr ? betas[i] : beta; for (auto j = *(offsets + i); j < *(offsets + i + 1); ++j) { auto nbr = indices[j]; - auto w = weights != nullptr ? weights[j] : result_t{1.0}; + auto w = weights ? (*weights)[j] : result_t{1.0}; katz_centralities[i] += alpha * old_katz_centralities[nbr] * w; } } @@ -88,22 +97,14 @@ void katz_centrality_reference(edge_t* offsets, return; } -typedef struct KatzCentrality_Usecase_t { - std::string graph_file_full_path{}; +struct KatzCentrality_Usecase { bool test_weighted{false}; + bool check_correctness{true}; +}; - KatzCentrality_Usecase_t(std::string const& graph_file_path, bool test_weighted) - : test_weighted(test_weighted) - { - if ((graph_file_path.length() > 0) && (graph_file_path[0] != '/')) { - graph_file_full_path = cugraph::test::get_rapids_dataset_root_dir() + "/" + graph_file_path; - } else { - graph_file_full_path = graph_file_path; - } - }; -} KatzCentrality_Usecase; - -class Tests_KatzCentrality : public ::testing::TestWithParam { +template +class Tests_KatzCentrality + : public ::testing::TestWithParam> { public: Tests_KatzCentrality() {} static void SetupTestCase() {} @@ -113,118 +114,199 @@ class Tests_KatzCentrality : public ::testing::TestWithParam - void run_current_test(KatzCentrality_Usecase const& configuration) + void run_current_test(KatzCentrality_Usecase const& katz_usecase, + input_usecase_t const& input_usecase) { + constexpr bool renumber = true; + raft::handle_t handle{}; + HighResClock hr_clock{}; - auto graph = - cugraph::test::read_graph_from_matrix_market_file( - handle, configuration.graph_file_full_path, configuration.test_weighted); - auto graph_view = graph.view(); + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + hr_clock.start(); + } - std::vector h_offsets(graph_view.get_number_of_vertices() + 1); - std::vector h_indices(graph_view.get_number_of_edges()); - std::vector h_weights{}; - raft::update_host(h_offsets.data(), - graph_view.offsets(), - graph_view.get_number_of_vertices() + 1, - handle.get_stream()); - raft::update_host(h_indices.data(), - graph_view.indices(), - graph_view.get_number_of_edges(), - handle.get_stream()); - if (graph_view.is_weighted()) { - h_weights.assign(graph_view.get_number_of_edges(), weight_t{0.0}); - raft::update_host(h_weights.data(), - graph_view.weights(), - graph_view.get_number_of_edges(), - handle.get_stream()); + auto [graph, d_renumber_map_labels] = + input_usecase.template construct_graph( + handle, katz_usecase.test_weighted, renumber); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "construct_graph took " << elapsed_time * 1e-6 << " s.\n"; } - CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); - std::vector h_reference_katz_centralities(graph_view.get_number_of_vertices()); + auto graph_view = graph.view(); - std::vector tmps(h_offsets.size()); - std::adjacent_difference(h_offsets.begin(), h_offsets.end(), tmps.begin()); - auto max_it = std::max_element(tmps.begin(), tmps.end()); + auto degrees = graph_view.compute_in_degrees(handle); + std::vector h_degrees(degrees.size()); + raft::update_host(h_degrees.data(), degrees.data(), degrees.size(), handle.get_stream()); + handle.get_stream_view().synchronize(); + auto max_it = std::max_element(h_degrees.begin(), h_degrees.end()); result_t const alpha = result_t{1.0} / static_cast(*max_it + 1); result_t constexpr beta{1.0}; result_t constexpr epsilon{1e-6}; - katz_centrality_reference( - h_offsets.data(), - h_indices.data(), - h_weights.size() > 0 ? h_weights.data() : static_cast(nullptr), - static_cast(nullptr), - h_reference_katz_centralities.data(), - graph_view.get_number_of_vertices(), - alpha, - beta, - epsilon, - std::numeric_limits::max(), - false, - true); - rmm::device_uvector d_katz_centralities(graph_view.get_number_of_vertices(), handle.get_stream()); - CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + hr_clock.start(); + } cugraph::experimental::katz_centrality(handle, graph_view, static_cast(nullptr), - d_katz_centralities.begin(), + d_katz_centralities.data(), alpha, beta, epsilon, std::numeric_limits::max(), false, - true, - false); - - CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement - - std::vector h_cugraph_katz_centralities(graph_view.get_number_of_vertices()); - - raft::update_host(h_cugraph_katz_centralities.data(), - d_katz_centralities.data(), - d_katz_centralities.size(), - handle.get_stream()); - CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); - - auto threshold_ratio = 1e-3; - auto threshold_magnitude = - (epsilon / static_cast(graph_view.get_number_of_vertices())) * threshold_ratio; - auto nearly_equal = [threshold_ratio, threshold_magnitude](auto lhs, auto rhs) { - auto diff = std::abs(lhs - rhs); - return (diff < std::max(lhs, rhs) * threshold_ratio) || (diff < threshold_magnitude); - }; - - ASSERT_TRUE(std::equal(h_reference_katz_centralities.begin(), - h_reference_katz_centralities.end(), - h_cugraph_katz_centralities.begin(), - nearly_equal)) - << "Katz centrality values do not match with the reference values."; + true); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "Katz Centrality took " << elapsed_time * 1e-6 << " s.\n"; + } + + if (katz_usecase.check_correctness) { + cugraph::experimental::graph_t unrenumbered_graph( + handle); + if (renumber) { + std::tie(unrenumbered_graph, std::ignore) = + input_usecase.template construct_graph( + handle, katz_usecase.test_weighted, false); + } + auto unrenumbered_graph_view = renumber ? unrenumbered_graph.view() : graph_view; + + std::vector h_offsets(unrenumbered_graph_view.get_number_of_vertices() + 1); + std::vector h_indices(unrenumbered_graph_view.get_number_of_edges()); + auto h_weights = unrenumbered_graph_view.is_weighted() + ? std::make_optional>( + unrenumbered_graph_view.get_number_of_edges(), weight_t{0.0}) + : std::nullopt; + raft::update_host(h_offsets.data(), + unrenumbered_graph_view.get_matrix_partition_view().get_offsets(), + unrenumbered_graph_view.get_number_of_vertices() + 1, + handle.get_stream()); + raft::update_host(h_indices.data(), + unrenumbered_graph_view.get_matrix_partition_view().get_indices(), + unrenumbered_graph_view.get_number_of_edges(), + handle.get_stream()); + if (h_weights) { + raft::update_host((*h_weights).data(), + *(unrenumbered_graph_view.get_matrix_partition_view().get_weights()), + unrenumbered_graph_view.get_number_of_edges(), + handle.get_stream()); + } + + handle.get_stream_view().synchronize(); + + std::vector h_reference_katz_centralities( + unrenumbered_graph_view.get_number_of_vertices()); + + katz_centrality_reference( + h_offsets.data(), + h_indices.data(), + h_weights ? std::optional{(*h_weights).data()} : std::nullopt, + static_cast(nullptr), + h_reference_katz_centralities.data(), + unrenumbered_graph_view.get_number_of_vertices(), + alpha, + beta, + epsilon, + std::numeric_limits::max(), + false, + true); + + std::vector h_cugraph_katz_centralities(graph_view.get_number_of_vertices()); + if (renumber) { + rmm::device_uvector d_unrenumbered_katz_centralities(size_t{0}, + handle.get_stream()); + std::tie(std::ignore, d_unrenumbered_katz_centralities) = + cugraph::test::sort_by_key(handle, + (*d_renumber_map_labels).data(), + d_katz_centralities.data(), + (*d_renumber_map_labels).size()); + raft::update_host(h_cugraph_katz_centralities.data(), + d_unrenumbered_katz_centralities.data(), + d_unrenumbered_katz_centralities.size(), + handle.get_stream()); + } else { + raft::update_host(h_cugraph_katz_centralities.data(), + d_katz_centralities.data(), + d_katz_centralities.size(), + handle.get_stream()); + } + + handle.get_stream_view().synchronize(); + + auto threshold_ratio = 1e-3; + auto threshold_magnitude = + (1.0 / static_cast(graph_view.get_number_of_vertices())) * + threshold_ratio; // skip comparison for low Katz Centrality verties (lowly ranked vertices) + auto nearly_equal = [threshold_ratio, threshold_magnitude](auto lhs, auto rhs) { + return std::abs(lhs - rhs) < + std::max(std::max(lhs, rhs) * threshold_ratio, threshold_magnitude); + }; + + ASSERT_TRUE(std::equal(h_reference_katz_centralities.begin(), + h_reference_katz_centralities.end(), + h_cugraph_katz_centralities.begin(), + nearly_equal)) + << "Katz centrality values do not match with the reference values."; + } } }; +using Tests_KatzCentrality_File = Tests_KatzCentrality; +using Tests_KatzCentrality_Rmat = Tests_KatzCentrality; + // FIXME: add tests for type combinations -TEST_P(Tests_KatzCentrality, CheckInt32Int32FloatFloat) +TEST_P(Tests_KatzCentrality_File, CheckInt32Int32FloatFloat) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +TEST_P(Tests_KatzCentrality_Rmat, CheckInt32Int32FloatFloat) { - run_current_test(GetParam()); + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); } -INSTANTIATE_TEST_CASE_P( - simple_test, - Tests_KatzCentrality, - ::testing::Values(KatzCentrality_Usecase("test/datasets/karate.mtx", false), - KatzCentrality_Usecase("test/datasets/karate.mtx", true), - KatzCentrality_Usecase("test/datasets/web-Google.mtx", false), - KatzCentrality_Usecase("test/datasets/web-Google.mtx", true), - KatzCentrality_Usecase("test/datasets/ljournal-2008.mtx", false), - KatzCentrality_Usecase("test/datasets/ljournal-2008.mtx", true), - KatzCentrality_Usecase("test/datasets/webbase-1M.mtx", false), - KatzCentrality_Usecase("test/datasets/webbase-1M.mtx", true))); +INSTANTIATE_TEST_SUITE_P( + file_test, + Tests_KatzCentrality_File, + ::testing::Combine( + // enable correctness checks + ::testing::Values(KatzCentrality_Usecase{false}, KatzCentrality_Usecase{true}), + ::testing::Values(cugraph::test::File_Usecase("test/datasets/karate.mtx"), + cugraph::test::File_Usecase("test/datasets/web-Google.mtx"), + cugraph::test::File_Usecase("test/datasets/ljournal-2008.mtx"), + cugraph::test::File_Usecase("test/datasets/webbase-1M.mtx")))); + +INSTANTIATE_TEST_SUITE_P(rmat_small_test, + Tests_KatzCentrality_Rmat, + // enable correctness checks + ::testing::Combine(::testing::Values(KatzCentrality_Usecase{false}, + KatzCentrality_Usecase{true}), + ::testing::Values(cugraph::test::Rmat_Usecase( + 10, 16, 0.57, 0.19, 0.19, 0, false, false)))); + +INSTANTIATE_TEST_SUITE_P(rmat_large_test, + Tests_KatzCentrality_Rmat, + // disable correctness checks for large graphs + ::testing::Combine(::testing::Values(KatzCentrality_Usecase{false, false}, + KatzCentrality_Usecase{true, false}), + ::testing::Values(cugraph::test::Rmat_Usecase( + 20, 32, 0.57, 0.19, 0.19, 0, false, false)))); CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/experimental/louvain_test.cu b/cpp/tests/experimental/louvain_test.cu deleted file mode 100644 index ce8fb55b1d8..00000000000 --- a/cpp/tests/experimental/louvain_test.cu +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright (c) 2020, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governin_from_mtxg permissions and - * limitations under the License. - */ - -#include -#include - -#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 700 -#include -#else -#include -#endif - -#include - -#include -#include -#include - -#include - -#include -#include -#include -#include -#include - -typedef struct Louvain_Usecase_t { - std::string graph_file_full_path{}; - bool test_weighted{false}; - - Louvain_Usecase_t(std::string const& graph_file_path, bool test_weighted) - : test_weighted(test_weighted) - { - if ((graph_file_path.length() > 0) && (graph_file_path[0] != '/')) { - graph_file_full_path = cugraph::test::get_rapids_dataset_root_dir() + "/" + graph_file_path; - } else { - graph_file_full_path = graph_file_path; - } - }; -} Louvain_Usecase; - -class Tests_Louvain : public ::testing::TestWithParam { - public: - Tests_Louvain() {} - static void SetupTestCase() {} - static void TearDownTestCase() {} - - virtual void SetUp() {} - virtual void TearDown() {} - - template - void run_current_test(Louvain_Usecase const& configuration) - { -#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 700 - CUGRAPH_FAIL("Louvain not supported on Pascal and older architectures"); -#else - raft::handle_t handle{}; - - std::cout << "read graph file: " << configuration.graph_file_full_path << std::endl; - - auto graph = - cugraph::test::read_graph_from_matrix_market_file( - handle, configuration.graph_file_full_path, configuration.test_weighted); - - auto graph_view = graph.view(); - - louvain(graph_view); -#endif - } - - template - void louvain(graph_t const& graph_view) - { - using vertex_t = typename graph_t::vertex_type; - using weight_t = typename graph_t::weight_type; - - raft::handle_t handle{}; - - rmm::device_vector clustering_v(graph_view.get_number_of_local_vertices()); - size_t level; - weight_t modularity; - - std::tie(level, modularity) = - cugraph::louvain(handle, graph_view, clustering_v.data().get(), size_t{100}, weight_t{1}); - - CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement - - std::cout << "level = " << level << std::endl; - std::cout << "modularity = " << modularity << std::endl; - } -}; - -// FIXME: add tests for type combinations -TEST_P(Tests_Louvain, CheckInt32Int32FloatFloat) -{ - run_current_test(GetParam()); -} - -INSTANTIATE_TEST_CASE_P(simple_test, - Tests_Louvain, - ::testing::Values(Louvain_Usecase("test/datasets/karate.mtx", true) -#if 0 - , - Louvain_Usecase("test/datasets/web-Google.mtx", true), - Louvain_Usecase("test/datasets/ljournal-2008.mtx", true), - Louvain_Usecase("test/datasets/webbase-1M.mtx", true) -#endif - )); - -CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/experimental/mg_bfs_test.cpp b/cpp/tests/experimental/mg_bfs_test.cpp new file mode 100644 index 00000000000..5257c728ce4 --- /dev/null +++ b/cpp/tests/experimental/mg_bfs_test.cpp @@ -0,0 +1,312 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include + +// do the perf measurements +// enabled by command line parameter s'--perf' +// +static int PERF = 0; + +struct BFS_Usecase { + size_t source{0}; + bool check_correctness{true}; +}; + +template +class Tests_MGBFS : public ::testing::TestWithParam> { + public: + Tests_MGBFS() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + + virtual void SetUp() {} + virtual void TearDown() {} + + // Compare the results of running BFS on multiple GPUs to that of a single-GPU run + template + void run_current_test(BFS_Usecase const& bfs_usecase, input_usecase_t const& input_usecase) + { + using weight_t = float; + + // 1. initialize handle + + raft::handle_t handle{}; + HighResClock hr_clock{}; + + raft::comms::initialize_mpi_comms(&handle, MPI_COMM_WORLD); + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + auto const comm_rank = comm.get_rank(); + + auto row_comm_size = static_cast(sqrt(static_cast(comm_size))); + while (comm_size % row_comm_size != 0) { + --row_comm_size; + } + cugraph::partition_2d::subcomm_factory_t + subcomm_factory(handle, row_comm_size); + + // 2. create MG graph + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + hr_clock.start(); + } + + auto [mg_graph, d_mg_renumber_map_labels] = + input_usecase.template construct_graph( + handle, false, true); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "MG construct_graph took " << elapsed_time * 1e-6 << " s.\n"; + } + + auto mg_graph_view = mg_graph.view(); + + ASSERT_TRUE(static_cast(bfs_usecase.source) >= 0 && + static_cast(bfs_usecase.source) < mg_graph_view.get_number_of_vertices()) + << "Invalid starting source."; + + // 3. run MG BFS + + rmm::device_uvector d_mg_distances(mg_graph_view.get_number_of_local_vertices(), + handle.get_stream()); + rmm::device_uvector d_mg_predecessors(mg_graph_view.get_number_of_local_vertices(), + handle.get_stream()); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + hr_clock.start(); + } + + cugraph::experimental::bfs(handle, + mg_graph_view, + d_mg_distances.data(), + d_mg_predecessors.data(), + static_cast(bfs_usecase.source), + false, + std::numeric_limits::max()); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "MG BFS took " << elapsed_time * 1e-6 << " s.\n"; + } + + // 4. compare SG & MG results + + if (bfs_usecase.check_correctness) { + // 4-1. aggregate MG results + + auto d_mg_aggregate_renumber_map_labels = cugraph::test::device_gatherv( + handle, (*d_mg_renumber_map_labels).data(), (*d_mg_renumber_map_labels).size()); + auto d_mg_aggregate_distances = + cugraph::test::device_gatherv(handle, d_mg_distances.data(), d_mg_distances.size()); + auto d_mg_aggregate_predecessors = + cugraph::test::device_gatherv(handle, d_mg_predecessors.data(), d_mg_predecessors.size()); + + if (handle.get_comms().get_rank() == int{0}) { + // 4-2. unrenumbr MG results + + cugraph::experimental::unrenumber_int_vertices( + handle, + d_mg_aggregate_predecessors.data(), + d_mg_aggregate_predecessors.size(), + d_mg_aggregate_renumber_map_labels.data(), + vertex_t{0}, + mg_graph_view.get_number_of_vertices(), + std::vector{mg_graph_view.get_number_of_vertices()}); + + std::tie(std::ignore, d_mg_aggregate_distances) = + cugraph::test::sort_by_key(handle, + d_mg_aggregate_renumber_map_labels.data(), + d_mg_aggregate_distances.data(), + d_mg_aggregate_renumber_map_labels.size()); + std::tie(std::ignore, d_mg_aggregate_predecessors) = + cugraph::test::sort_by_key(handle, + d_mg_aggregate_renumber_map_labels.data(), + d_mg_aggregate_predecessors.data(), + d_mg_aggregate_renumber_map_labels.size()); + + // 4-3. create SG graph + + cugraph::experimental::graph_t sg_graph(handle); + std::tie(sg_graph, std::ignore) = + input_usecase.template construct_graph( + handle, false, false); + + auto sg_graph_view = sg_graph.view(); + + ASSERT_TRUE(mg_graph_view.get_number_of_vertices() == + sg_graph_view.get_number_of_vertices()); + + // 4-4. run SG BFS + + rmm::device_uvector d_sg_distances(sg_graph_view.get_number_of_vertices(), + handle.get_stream()); + rmm::device_uvector d_sg_predecessors( + sg_graph_view.get_number_of_local_vertices(), handle.get_stream()); + + vertex_t unrenumbered_source{}; + raft::update_host(&unrenumbered_source, + d_mg_aggregate_renumber_map_labels.data() + bfs_usecase.source, + size_t{1}, + handle.get_stream()); + handle.get_stream_view().synchronize(); + + cugraph::experimental::bfs(handle, + sg_graph_view, + d_sg_distances.data(), + d_sg_predecessors.data(), + unrenumbered_source, + false, + std::numeric_limits::max()); + // 4-5. compare + + std::vector h_sg_offsets(sg_graph_view.get_number_of_vertices() + 1); + std::vector h_sg_indices(sg_graph_view.get_number_of_edges()); + raft::update_host(h_sg_offsets.data(), + sg_graph_view.get_matrix_partition_view().get_offsets(), + sg_graph_view.get_number_of_vertices() + 1, + handle.get_stream()); + raft::update_host(h_sg_indices.data(), + sg_graph_view.get_matrix_partition_view().get_indices(), + sg_graph_view.get_number_of_edges(), + handle.get_stream()); + + std::vector h_mg_aggregate_distances(mg_graph_view.get_number_of_vertices()); + std::vector h_mg_aggregate_predecessors(mg_graph_view.get_number_of_vertices()); + + raft::update_host(h_mg_aggregate_distances.data(), + d_mg_aggregate_distances.data(), + d_mg_aggregate_distances.size(), + handle.get_stream()); + raft::update_host(h_mg_aggregate_predecessors.data(), + d_mg_aggregate_predecessors.data(), + d_mg_aggregate_predecessors.size(), + handle.get_stream()); + + std::vector h_sg_distances(sg_graph_view.get_number_of_vertices()); + std::vector h_sg_predecessors(sg_graph_view.get_number_of_vertices()); + + raft::update_host( + h_sg_distances.data(), d_sg_distances.data(), d_sg_distances.size(), handle.get_stream()); + raft::update_host(h_sg_predecessors.data(), + d_sg_predecessors.data(), + d_sg_predecessors.size(), + handle.get_stream()); + handle.get_stream_view().synchronize(); + + ASSERT_TRUE(std::equal(h_mg_aggregate_distances.begin(), + h_mg_aggregate_distances.end(), + h_sg_distances.begin())); + for (size_t i = 0; i < h_mg_aggregate_predecessors.size(); ++i) { + if (h_mg_aggregate_predecessors[i] == + cugraph::experimental::invalid_vertex_id::value) { + ASSERT_TRUE(h_sg_predecessors[i] == h_mg_aggregate_predecessors[i]) + << "vertex reachability does not match with the SG result."; + } else { + ASSERT_TRUE(h_sg_distances[h_mg_aggregate_predecessors[i]] + 1 == h_sg_distances[i]) + << "distances to this vertex != distances to the predecessor vertex + 1."; + bool found{false}; + for (auto j = h_sg_offsets[h_mg_aggregate_predecessors[i]]; + j < h_sg_offsets[h_mg_aggregate_predecessors[i] + 1]; + ++j) { + if (h_sg_indices[j] == i) { + found = true; + break; + } + } + ASSERT_TRUE(found) << "no edge from the predecessor vertex to this vertex."; + } + } + } + } + } +}; + +using Tests_MGBFS_File = Tests_MGBFS; +using Tests_MGBFS_Rmat = Tests_MGBFS; + +TEST_P(Tests_MGBFS_File, CheckInt32Int32) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +TEST_P(Tests_MGBFS_Rmat, CheckInt32Int32) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +INSTANTIATE_TEST_SUITE_P( + file_test, + Tests_MGBFS_File, + ::testing::Combine( + // enable correctness checks + ::testing::Values(BFS_Usecase{0}), + ::testing::Values(cugraph::test::File_Usecase("test/datasets/karate.mtx"), + cugraph::test::File_Usecase("test/datasets/web-Google.mtx"), + cugraph::test::File_Usecase("test/datasets/ljournal-2008.mtx"), + cugraph::test::File_Usecase("test/datasets/webbase-1M.mtx")))); + +INSTANTIATE_TEST_SUITE_P(rmat_small_test, + Tests_MGBFS_Rmat, + ::testing::Values( + // enable correctness checks + std::make_tuple(BFS_Usecase{0}, + cugraph::test::Rmat_Usecase( + 10, 16, 0.57, 0.19, 0.19, 0, false, false, 0, true)))); + +INSTANTIATE_TEST_SUITE_P(rmat_large_test, + Tests_MGBFS_Rmat, + ::testing::Values( + // disable correctness checks for large graphs + std::make_tuple(BFS_Usecase{0, false}, + cugraph::test::Rmat_Usecase( + 20, 32, 0.57, 0.19, 0.19, 0, false, false, 0, true)))); + +CUGRAPH_MG_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/experimental/mg_katz_centrality_test.cpp b/cpp/tests/experimental/mg_katz_centrality_test.cpp new file mode 100644 index 00000000000..9071701324e --- /dev/null +++ b/cpp/tests/experimental/mg_katz_centrality_test.cpp @@ -0,0 +1,266 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include + +// do the perf measurements +// enabled by command line parameter s'--perf' +// +static int PERF = 0; + +struct KatzCentrality_Usecase { + bool test_weighted{false}; + bool check_correctness{true}; +}; + +template +class Tests_MGKatzCentrality + : public ::testing::TestWithParam> { + public: + Tests_MGKatzCentrality() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + + virtual void SetUp() {} + virtual void TearDown() {} + + // Compare the results of running Katz Centrality on multiple GPUs to that of a single-GPU run + template + void run_current_test(KatzCentrality_Usecase const& katz_usecase, + input_usecase_t const& input_usecase) + { + // 1. initialize handle + + raft::handle_t handle{}; + HighResClock hr_clock{}; + + raft::comms::initialize_mpi_comms(&handle, MPI_COMM_WORLD); + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + auto const comm_rank = comm.get_rank(); + + auto row_comm_size = static_cast(sqrt(static_cast(comm_size))); + while (comm_size % row_comm_size != 0) { + --row_comm_size; + } + cugraph::partition_2d::subcomm_factory_t + subcomm_factory(handle, row_comm_size); + + // 2. create MG graph + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + hr_clock.start(); + } + + auto [mg_graph, d_mg_renumber_map_labels] = + input_usecase.template construct_graph( + handle, katz_usecase.test_weighted, true); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "MG construct_graph took " << elapsed_time * 1e-6 << " s.\n"; + } + + auto mg_graph_view = mg_graph.view(); + + // 3. compute max in-degree + + auto max_in_degree = mg_graph_view.compute_max_in_degree(handle); + + // 4. run MG Katz Centrality + + result_t const alpha = result_t{1.0} / static_cast(max_in_degree + 1); + result_t constexpr beta{1.0}; + result_t constexpr epsilon{1e-6}; + + rmm::device_uvector d_mg_katz_centralities( + mg_graph_view.get_number_of_local_vertices(), handle.get_stream()); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + hr_clock.start(); + } + + cugraph::experimental::katz_centrality(handle, + mg_graph_view, + static_cast(nullptr), + d_mg_katz_centralities.data(), + alpha, + beta, + epsilon, + std::numeric_limits::max(), + false); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "MG Katz Centrality took " << elapsed_time * 1e-6 << " s.\n"; + } + + // 5. copmare SG & MG results + + if (katz_usecase.check_correctness) { + // 5-1. aggregate MG results + + auto d_mg_aggregate_renumber_map_labels = cugraph::test::device_gatherv( + handle, (*d_mg_renumber_map_labels).data(), (*d_mg_renumber_map_labels).size()); + auto d_mg_aggregate_katz_centralities = cugraph::test::device_gatherv( + handle, d_mg_katz_centralities.data(), d_mg_katz_centralities.size()); + + if (handle.get_comms().get_rank() == int{0}) { + // 5-2. unrenumbr MG results + + std::tie(std::ignore, d_mg_aggregate_katz_centralities) = + cugraph::test::sort_by_key(handle, + d_mg_aggregate_renumber_map_labels.data(), + d_mg_aggregate_katz_centralities.data(), + d_mg_aggregate_renumber_map_labels.size()); + + // 5-3. create SG graph + + cugraph::experimental::graph_t sg_graph(handle); + std::tie(sg_graph, std::ignore) = + input_usecase.template construct_graph( + handle, katz_usecase.test_weighted, false); + + auto sg_graph_view = sg_graph.view(); + + ASSERT_TRUE(mg_graph_view.get_number_of_vertices() == + sg_graph_view.get_number_of_vertices()); + + // 5-4. run SG Katz Centrality + + rmm::device_uvector d_sg_katz_centralities(sg_graph_view.get_number_of_vertices(), + handle.get_stream()); + + cugraph::experimental::katz_centrality( + handle, + sg_graph_view, + static_cast(nullptr), + d_sg_katz_centralities.data(), + alpha, + beta, + epsilon, + std::numeric_limits::max(), // max_iterations + false); + + // 5-5. compare + + std::vector h_mg_aggregate_katz_centralities( + mg_graph_view.get_number_of_vertices()); + raft::update_host(h_mg_aggregate_katz_centralities.data(), + d_mg_aggregate_katz_centralities.data(), + d_mg_aggregate_katz_centralities.size(), + handle.get_stream()); + + std::vector h_sg_katz_centralities(sg_graph_view.get_number_of_vertices()); + raft::update_host(h_sg_katz_centralities.data(), + d_sg_katz_centralities.data(), + d_sg_katz_centralities.size(), + handle.get_stream()); + + handle.get_stream_view().synchronize(); + + auto threshold_ratio = 1e-3; + auto threshold_magnitude = + (1.0 / static_cast(mg_graph_view.get_number_of_vertices())) * + threshold_ratio; // skip comparison for low KatzCentrality verties (lowly ranked + // vertices) + auto nearly_equal = [threshold_ratio, threshold_magnitude](auto lhs, auto rhs) { + return std::abs(lhs - rhs) < + std::max(std::max(lhs, rhs) * threshold_ratio, threshold_magnitude); + }; + + ASSERT_TRUE(std::equal(h_mg_aggregate_katz_centralities.begin(), + h_mg_aggregate_katz_centralities.end(), + h_sg_katz_centralities.begin(), + nearly_equal)); + } + } + } +}; + +using Tests_MGKatzCentrality_File = Tests_MGKatzCentrality; +using Tests_MGKatzCentrality_Rmat = Tests_MGKatzCentrality; + +TEST_P(Tests_MGKatzCentrality_File, CheckInt32Int32FloatFloat) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +TEST_P(Tests_MGKatzCentrality_Rmat, CheckInt32Int32FloatFloat) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +INSTANTIATE_TEST_SUITE_P( + file_test, + Tests_MGKatzCentrality_File, + ::testing::Combine( + // enable correctness checks + ::testing::Values(KatzCentrality_Usecase{false}, KatzCentrality_Usecase{true}), + ::testing::Values(cugraph::test::File_Usecase("test/datasets/karate.mtx"), + cugraph::test::File_Usecase("test/datasets/web-Google.mtx"), + cugraph::test::File_Usecase("test/datasets/ljournal-2008.mtx"), + cugraph::test::File_Usecase("test/datasets/webbase-1M.mtx")))); + +INSTANTIATE_TEST_SUITE_P(rmat_small_test, + Tests_MGKatzCentrality_Rmat, + ::testing::Combine( + // enable correctness checks + ::testing::Values(KatzCentrality_Usecase{false}, + KatzCentrality_Usecase{true}), + ::testing::Values(cugraph::test::Rmat_Usecase( + 10, 16, 0.57, 0.19, 0.19, 0, false, false, 0, true)))); + +INSTANTIATE_TEST_SUITE_P(rmat_large_test, + Tests_MGKatzCentrality_Rmat, + ::testing::Combine( + // disable correctness checks for large graphs + ::testing::Values(KatzCentrality_Usecase{false, false}, + KatzCentrality_Usecase{true, false}), + ::testing::Values(cugraph::test::Rmat_Usecase( + 20, 32, 0.57, 0.19, 0.19, 0, false, false, 0, true)))); + +CUGRAPH_MG_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/experimental/mg_sssp_test.cpp b/cpp/tests/experimental/mg_sssp_test.cpp new file mode 100644 index 00000000000..ce2556bed00 --- /dev/null +++ b/cpp/tests/experimental/mg_sssp_test.cpp @@ -0,0 +1,320 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include + +// do the perf measurements +// enabled by command line parameter s'--perf' +// +static int PERF = 0; + +struct SSSP_Usecase { + size_t source{0}; + bool check_correctness{true}; +}; + +template +class Tests_MGSSSP : public ::testing::TestWithParam> { + public: + Tests_MGSSSP() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + + virtual void SetUp() {} + virtual void TearDown() {} + + // Compare the results of running SSSP on multiple GPUs to that of a single-GPU run + template + void run_current_test(SSSP_Usecase const& sssp_usecase, input_usecase_t const& input_usecase) + { + // 1. initialize handle + raft::handle_t handle{}; + HighResClock hr_clock{}; + + raft::comms::initialize_mpi_comms(&handle, MPI_COMM_WORLD); + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + auto const comm_rank = comm.get_rank(); + + auto row_comm_size = static_cast(sqrt(static_cast(comm_size))); + while (comm_size % row_comm_size != 0) { + --row_comm_size; + } + cugraph::partition_2d::subcomm_factory_t + subcomm_factory(handle, row_comm_size); + + // 2. create MG graph + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + hr_clock.start(); + } + + auto [mg_graph, d_mg_renumber_map_labels] = + input_usecase.template construct_graph( + handle, true, true); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "MG construct_graph took " << elapsed_time * 1e-6 << " s.\n"; + } + + auto mg_graph_view = mg_graph.view(); + + ASSERT_TRUE(static_cast(sssp_usecase.source) >= 0 && + static_cast(sssp_usecase.source) < mg_graph_view.get_number_of_vertices()) + << "Invalid starting source."; + + // 3. run MG SSSP + + rmm::device_uvector d_mg_distances(mg_graph_view.get_number_of_local_vertices(), + handle.get_stream()); + rmm::device_uvector d_mg_predecessors(mg_graph_view.get_number_of_local_vertices(), + handle.get_stream()); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + hr_clock.start(); + } + + cugraph::experimental::sssp(handle, + mg_graph_view, + d_mg_distances.data(), + d_mg_predecessors.data(), + static_cast(sssp_usecase.source), + std::numeric_limits::max()); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "MG SSSP took " << elapsed_time * 1e-6 << " s.\n"; + } + + // 4. copmare SG & MG results + + if (sssp_usecase.check_correctness) { + // 4-1. aggregate MG results + + auto d_mg_aggregate_renumber_map_labels = cugraph::test::device_gatherv( + handle, (*d_mg_renumber_map_labels).data(), (*d_mg_renumber_map_labels).size()); + auto d_mg_aggregate_distances = + cugraph::test::device_gatherv(handle, d_mg_distances.data(), d_mg_distances.size()); + auto d_mg_aggregate_predecessors = + cugraph::test::device_gatherv(handle, d_mg_predecessors.data(), d_mg_predecessors.size()); + + if (handle.get_comms().get_rank() == int{0}) { + // 4-2. unrenumber MG results + + cugraph::experimental::unrenumber_int_vertices( + handle, + d_mg_aggregate_predecessors.data(), + d_mg_aggregate_predecessors.size(), + d_mg_aggregate_renumber_map_labels.data(), + vertex_t{0}, + mg_graph_view.get_number_of_vertices(), + std::vector{mg_graph_view.get_number_of_vertices()}); + + std::tie(std::ignore, d_mg_aggregate_distances) = + cugraph::test::sort_by_key(handle, + d_mg_aggregate_renumber_map_labels.data(), + d_mg_aggregate_distances.data(), + d_mg_aggregate_renumber_map_labels.size()); + std::tie(std::ignore, d_mg_aggregate_predecessors) = + cugraph::test::sort_by_key(handle, + d_mg_aggregate_renumber_map_labels.data(), + d_mg_aggregate_predecessors.data(), + d_mg_aggregate_renumber_map_labels.size()); + + // 4-3. create SG graph + + cugraph::experimental::graph_t sg_graph(handle); + std::tie(sg_graph, std::ignore) = + input_usecase.template construct_graph( + handle, true, false); + + auto sg_graph_view = sg_graph.view(); + + ASSERT_TRUE(mg_graph_view.get_number_of_vertices() == + sg_graph_view.get_number_of_vertices()); + + // 4-4. run SG SSSP + + rmm::device_uvector d_sg_distances(sg_graph_view.get_number_of_local_vertices(), + handle.get_stream()); + rmm::device_uvector d_sg_predecessors( + sg_graph_view.get_number_of_local_vertices(), handle.get_stream()); + vertex_t unrenumbered_source{}; + raft::update_host(&unrenumbered_source, + d_mg_aggregate_renumber_map_labels.data() + sssp_usecase.source, + size_t{1}, + handle.get_stream()); + handle.get_stream_view().synchronize(); + + cugraph::experimental::sssp(handle, + sg_graph_view, + d_sg_distances.data(), + d_sg_predecessors.data(), + unrenumbered_source, + std::numeric_limits::max()); + + // 4-5. compare + + std::vector h_sg_offsets(sg_graph_view.get_number_of_vertices() + 1); + std::vector h_sg_indices(sg_graph_view.get_number_of_edges()); + std::vector h_sg_weights(sg_graph_view.get_number_of_edges()); + raft::update_host(h_sg_offsets.data(), + sg_graph_view.get_matrix_partition_view().get_offsets(), + sg_graph_view.get_number_of_vertices() + 1, + handle.get_stream()); + raft::update_host(h_sg_indices.data(), + sg_graph_view.get_matrix_partition_view().get_indices(), + sg_graph_view.get_number_of_edges(), + handle.get_stream()); + raft::update_host(h_sg_weights.data(), + *(sg_graph_view.get_matrix_partition_view().get_weights()), + sg_graph_view.get_number_of_edges(), + handle.get_stream()); + + std::vector h_mg_aggregate_distances(mg_graph_view.get_number_of_vertices()); + std::vector h_mg_aggregate_predecessors(mg_graph_view.get_number_of_vertices()); + raft::update_host(h_mg_aggregate_distances.data(), + d_mg_aggregate_distances.data(), + d_mg_aggregate_distances.size(), + handle.get_stream()); + raft::update_host(h_mg_aggregate_predecessors.data(), + d_mg_aggregate_predecessors.data(), + d_mg_aggregate_predecessors.size(), + handle.get_stream()); + + std::vector h_sg_distances(sg_graph_view.get_number_of_vertices()); + std::vector h_sg_predecessors(sg_graph_view.get_number_of_vertices()); + raft::update_host( + h_sg_distances.data(), d_sg_distances.data(), d_sg_distances.size(), handle.get_stream()); + raft::update_host(h_sg_predecessors.data(), + d_sg_predecessors.data(), + d_sg_predecessors.size(), + handle.get_stream()); + + handle.get_stream_view().synchronize(); + + auto max_weight_element = std::max_element(h_sg_weights.begin(), h_sg_weights.end()); + auto epsilon = *max_weight_element * weight_t{1e-6}; + auto nearly_equal = [epsilon](auto lhs, auto rhs) { + return std::fabs(lhs - rhs) < epsilon; + }; + + ASSERT_TRUE(std::equal(h_mg_aggregate_distances.begin(), + h_mg_aggregate_distances.end(), + h_sg_distances.begin(), + nearly_equal)); + + for (size_t i = 0; i < h_mg_aggregate_predecessors.size(); ++i) { + if (h_mg_aggregate_predecessors[i] == + cugraph::experimental::invalid_vertex_id::value) { + ASSERT_TRUE(h_sg_predecessors[i] == h_mg_aggregate_predecessors[i]) + << "vertex reachability does not match with the SG result."; + } else { + auto pred_distance = h_sg_distances[h_mg_aggregate_predecessors[i]]; + bool found{false}; + for (auto j = h_sg_offsets[h_mg_aggregate_predecessors[i]]; + j < h_sg_offsets[h_mg_aggregate_predecessors[i] + 1]; + ++j) { + if (h_sg_indices[j] == i) { + if (nearly_equal(pred_distance + h_sg_weights[j], h_sg_distances[i])) { + found = true; + break; + } + } + } + ASSERT_TRUE(found) + << "no edge from the predecessor vertex to this vertex with the matching weight."; + } + } + } + } + } +}; + +using Tests_MGSSSP_File = Tests_MGSSSP; +using Tests_MGSSSP_Rmat = Tests_MGSSSP; + +TEST_P(Tests_MGSSSP_File, CheckInt32Int32Float) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +TEST_P(Tests_MGSSSP_Rmat, CheckInt32Int32Float) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +INSTANTIATE_TEST_SUITE_P( + file_test, + Tests_MGSSSP_File, + ::testing::Values( + // enable correctness checks + std::make_tuple(SSSP_Usecase{0}, cugraph::test::File_Usecase("test/datasets/karate.mtx")), + std::make_tuple(SSSP_Usecase{0}, cugraph::test::File_Usecase("test/datasets/dblp.mtx")), + std::make_tuple(SSSP_Usecase{1000}, + cugraph::test::File_Usecase("test/datasets/wiki2003.mtx")))); + +INSTANTIATE_TEST_SUITE_P(rmat_small_test, + Tests_MGSSSP_Rmat, + ::testing::Values( + // enable correctness checks + std::make_tuple(SSSP_Usecase{0}, + cugraph::test::Rmat_Usecase( + 10, 16, 0.57, 0.19, 0.19, 0, false, false, 0, true)))); + +INSTANTIATE_TEST_SUITE_P(rmat_large_test, + Tests_MGSSSP_Rmat, + ::testing::Values( + // disable correctness checks for large graphs + std::make_tuple(SSSP_Usecase{0, false}, + cugraph::test::Rmat_Usecase( + 20, 32, 0.57, 0.19, 0.19, 0, false, false, 0, true)))); + +CUGRAPH_MG_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/experimental/ms_bfs_test.cpp b/cpp/tests/experimental/ms_bfs_test.cpp new file mode 100644 index 00000000000..a6fb306f1d8 --- /dev/null +++ b/cpp/tests/experimental/ms_bfs_test.cpp @@ -0,0 +1,300 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +typedef struct MsBfs_Usecase_t { + std::string graph_file_full_path{}; + std::vector sources{}; + int32_t radius; + bool test_weighted{false}; + + MsBfs_Usecase_t(std::string const& graph_file_path, + std::vector const& sources, + int32_t radius, + bool test_weighted) + : sources(sources), radius(radius), test_weighted(test_weighted) + { + if ((graph_file_path.length() > 0) && (graph_file_path[0] != '/')) { + graph_file_full_path = cugraph::test::get_rapids_dataset_root_dir() + "/" + graph_file_path; + } else { + graph_file_full_path = graph_file_path; + } + }; +} MsBfs_Usecase; + +class Tests_MsBfs : public ::testing::TestWithParam { + public: + Tests_MsBfs() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + + virtual void SetUp() {} + virtual void TearDown() {} + + template + void run_current_test(MsBfs_Usecase const& configuration) + { + auto n_seeds = configuration.sources.size(); + int n_streams = std::min(n_seeds, static_cast(128)); + raft::handle_t handle(n_streams); + + cugraph::experimental::graph_t graph( + handle); + std::tie(graph, std::ignore) = cugraph::test:: + read_graph_from_matrix_market_file( + handle, configuration.graph_file_full_path, configuration.test_weighted, false); + auto graph_view = graph.view(); + // Streams will allocate concurrently later + std::vector> d_distances{}; + std::vector> d_predecessors{}; + + d_distances.reserve(n_seeds); + d_predecessors.reserve(n_seeds); + for (vertex_t i = 0; i < n_seeds; i++) { + // Allocations and operations are attached to the worker stream + rmm::device_uvector tmp_distances(graph_view.get_number_of_vertices(), + handle.get_internal_stream_view(i)); + rmm::device_uvector tmp_predecessors(graph_view.get_number_of_vertices(), + handle.get_internal_stream_view(i)); + + d_distances.push_back(std::move(tmp_distances)); + d_predecessors.push_back(std::move(tmp_predecessors)); + } + + std::vector radius(n_seeds); + std::generate(radius.begin(), radius.end(), [n = 0]() mutable { return (n++ % 12 + 1); }); + + // warm up + cugraph::experimental::bfs(handle, + graph_view, + d_distances[0].begin(), + d_predecessors[0].begin(), + static_cast(configuration.sources[0]), + false, + radius[0]); + + // one by one + HighResTimer hr_timer; + hr_timer.start("bfs"); + cudaProfilerStart(); + for (vertex_t i = 0; i < n_seeds; i++) { + cugraph::experimental::bfs(handle, + graph_view, + d_distances[i].begin(), + d_predecessors[i].begin(), + static_cast(configuration.sources[i]), + false, + radius[i]); + } + cudaProfilerStop(); + hr_timer.stop(); + hr_timer.display(std::cout); + + // concurrent + hr_timer.start("bfs"); + cudaProfilerStart(); +#pragma omp parallel for + for (vertex_t i = 0; i < n_seeds; i++) { + raft::handle_t light_handle(handle, i); + auto worker_stream_view = light_handle.get_stream_view(); + cugraph::experimental::bfs(light_handle, + graph_view, + d_distances[i].begin(), + d_predecessors[i].begin(), + static_cast(configuration.sources[i]), + false, + radius[i]); + } + + cudaProfilerStop(); + hr_timer.stop(); + hr_timer.display(std::cout); + } +}; + +TEST_P(Tests_MsBfs, DISABLED_CheckInt32Int32FloatUntransposed) +{ + run_current_test(GetParam()); +} +/* +INSTANTIATE_TEST_SUITE_P( + simple_test, + Tests_MsBfs, + ::testing::Values( + MsBfs_Usecase("test/datasets/karate.mtx", std::vector{0}, 1, false), + MsBfs_Usecase("test/datasets/karate.mtx", std::vector{0}, 2, false), + MsBfs_Usecase("test/datasets/karate.mtx", std::vector{1}, 3, false), + MsBfs_Usecase("test/datasets/karate.mtx", std::vector{10, 0, 5}, 2, false), + MsBfs_Usecase("test/datasets/karate.mtx", std::vector{9, 3, 10}, 2, false), + MsBfs_Usecase( + "test/datasets/karate.mtx", std::vector{5, 9, 3, 10, 12, 13}, 2, true))); +*/ +// For perf analysis + +INSTANTIATE_TEST_SUITE_P( + simple_test, + Tests_MsBfs, + ::testing::Values( + MsBfs_Usecase("test/datasets/soc-LiveJournal1.mtx", std::vector{363617}, 2, false), + MsBfs_Usecase( + "test/datasets/soc-LiveJournal1.mtx", + std::vector{ + 363617, 722214, 2337449, 2510183, 2513389, 225853, 2035807, 3836330, 1865496, 28755}, + 2, + false), + MsBfs_Usecase( + "test/datasets/soc-LiveJournal1.mtx", + std::vector{ + 363617, 722214, 2337449, 2510183, 2513389, 225853, 2035807, 3836330, 1865496, 28755, + 2536834, 3070144, 3888415, 3131712, 2382526, 1040771, 2631543, 4607218, 4465829, 3341686, + 2772973, 2611175, 4526129, 2624421, 1220593, 2593137, 3270705, 1503899, 1213033, 4840102, + 4529036, 3421116, 4264831, 4089751, 4272322, 3486998, 2830318, 320953, 2388331, 520808, + 3023094, 1600294, 3631119, 1716614, 4829213, 1175844, 960680, 847662, 3277365, 3957318, + 3455123, 2454259, 670953, 4465677, 1027332, 2560721, 89061, 1163406, 3109528, 3221856, + 4714426, 2382774, 37828, 4433616, 3283229, 591911, 4200188, 442522, 872207, 2437601, + 741003, 266241, 914618, 3626195, 2021080, 4679624, 777476, 2527796, 1114017, 640142, + 49259, 4069879, 3869098, 1105040, 4707804, 3208582, 3325885, 1450601, 4072548, 2037062, + 2029646, 4575891, 1488598, 79105, 4827273, 3795434, 4647518, 4733397, 3980718, 1184627}, + 2, + false), + MsBfs_Usecase( + "test/datasets/soc-LiveJournal1.mtx", + std::vector{ + 363617, 722214, 2337449, 2510183, 2513389, 225853, 2035807, 3836330, 1865496, 28755, + 2536834, 3070144, 3888415, 3131712, 2382526, 1040771, 2631543, 4607218, 4465829, 3341686, + 2772973, 2611175, 4526129, 2624421, 1220593, 2593137, 3270705, 1503899, 1213033, 4840102, + 4529036, 3421116, 4264831, 4089751, 4272322, 3486998, 2830318, 320953, 2388331, 520808, + 3023094, 1600294, 3631119, 1716614, 4829213, 1175844, 960680, 847662, 3277365, 3957318, + 3455123, 2454259, 670953, 4465677, 1027332, 2560721, 89061, 1163406, 3109528, 3221856, + 4714426, 2382774, 37828, 4433616, 3283229, 591911, 4200188, 442522, 872207, 2437601, + 741003, 266241, 914618, 3626195, 2021080, 4679624, 777476, 2527796, 1114017, 640142, + 49259, 4069879, 3869098, 1105040, 4707804, 3208582, 3325885, 1450601, 4072548, 2037062, + 2029646, 4575891, 1488598, 79105, 4827273, 3795434, 4647518, 4733397, 3980718, 1184627, + 984983, 3114832, 1967741, 1599818, 144593, 2698770, 2889449, 2495550, 1053813, 1193622, + 686026, 3989015, 2040719, 4693428, 3190376, 2926728, 3399030, 1664419, 662429, 4526841, + 2186957, 3752558, 2440046, 2930226, 3633006, 4058166, 3137060, 3499296, 2126343, 148971, + 2199672, 275811, 2813976, 2274536, 1189239, 1335942, 2465624, 2596042, 829684, 193400, + 2682845, 3691697, 4022437, 4051170, 4195175, 2876420, 3984220, 2174475, 326134, 2606530, + 2493046, 4706121, 1498980, 4576225, 1271339, 44832, 1875673, 4664940, 134931, 736397, + 4333554, 2751031, 2163610, 2879676, 3174153, 3317403, 2052464, 1881883, 4757859, 3596257, + 2358088, 2578758, 447504, 590720, 1717038, 1869795, 1133885, 3027521, 840312, 2818881, + 3654321, 2730947, 353585, 1134903, 2223378, 1508824, 3662521, 1363776, 2712071, 288441, + 1204581, 3502242, 4645567, 2767267, 1514366, 3956099, 1422145, 1216608, 2253360, 189132, + 4238225, 1345783, 451571, 1599442, 3237284, 4711405, 929446, 1857675, 150759, 1277633, + 761210, 138628, 1026833, 2599544, 2464737, 989203, 3399615, 2144292, 216142, 637312, + 2044964, 716256, 1660632, 1762919, 4784357, 2213415, 2764769, 291806, 609772, 3264819, + 1870953, 1516385, 235647, 1045474, 2664957, 819095, 1824119, 4045271, 4448109, 1676788, + 4285177, 1580502, 3546548, 2771971, 3927086, 1339779, 3156204, 1730998, 1172522, 2433024, + 4533449, 479930, 2010695, 672994, 3542039, 3176455, 26352, 2137735, 866910, 4410835, + 2623982, 3603159, 2555625, 2765653, 267865, 2015523, 1009052, 4713994, 1600667, 2176195, + 3179631, 4570390, 2018424, 3356384, 1784287, 894861, 3622099, 1647273, 3044136, 950354, + 1491760, 3416929, 3757300, 2244912, 4129215, 1600848, 3867343, 72329, 919189, 992521, + 3445975, 4712557, 4680974, 188419, 2612093, 1991268, 3566207, 2281468, 3859078, 2492806, + 3398628, 763441, 2679107, 2554420, 2130132, 4664374, 1182901, 3890770, 4714667, 4209303, + 4013060, 3617653, 2040022, 3296519, 4190671, 1693353, 2678411, 3788834, 2781815, 191965, + 1083926, 503974, 3529226, 1650522, 1900976, 542080, 3423929, 3418905, 878165, 4701703, + 3022790, 4316365, 76365, 4053672, 1358185, 3830478, 4445661, 3210024, 1895915, 4541133, + 2938808, 562788, 3920065, 1458776, 4052046, 2967475, 1092809, 3203538, 159626, 3399464, + 214467, 3343982, 1811854, 3189045, 4272117, 4701563, 424807, 4341116, 760545, 4674683, + 1538018, 386762, 194237, 2162719, 1694433, 943728, 2389036, 2196653, 3085571, 1513424, + 3689413, 3278747, 4197291, 3324063, 3651090, 1737936, 2768803, 2768889, 3108096, 4311775, + 3569480, 886705, 733256, 2477493, 1735412, 2960895, 1983781, 1861797, 3566460, 4537673, + 1164093, 3499764, 4553071, 3518985, 847658, 918948, 2922351, 1056144, 652895, 1013195, + 780505, 1702928, 3562838, 1432719, 2405207, 1054920, 641647, 2240939, 3617702, 383165, + 652641, 879593, 1810739, 2096385, 4497865, 4768530, 1743968, 3582014, 1025009, 3002122, + 2422190, 527647, 1251821, 2571153, 4095874, 3705333, 3637407, 1385567, 4043855, 4041930, + 2433139, 1710383, 1127734, 4362316, 711588, 817839, 3214775, 910077, 1313768, 2382229, + 16864, 2081770, 3095420, 3195272, 548711, 2259860, 1167323, 2435974, 425238, 2085179, + 2630042, 2632881, 2867923, 3703565, 1037695, 226617, 4379130, 1541468, 3581937, 605965, + 1137674, 4655221, 4769963, 1394370, 4425315, 2990132, 2364485, 1561137, 2713384, 481509, + 2900382, 934766, 2986774, 1767669, 298593, 2502539, 139296, 3794229, 4002180, 4718138, + 2909238, 423691, 3023810, 2784924, 2760160, 1971980, 316683, 3828090, 3253691, 4839313, + 1203624, 584938, 3901482, 1747543, 1572737, 3533226, 774708, 1691195, 1037110, 1557763, + 225120, 4424243, 3524086, 1717663, 4332507, 3513592, 4274932, 1232118, 873498, 1416042, + 2488925, 111391, 4704545, 4492545, 445317, 1584812, 2187737, 2471948, 3731678, 219255, + 2282627, 2589971, 2372185, 4609096, 3673961, 2524410, 12823, 2437155, 3015974, 4188352, + 3184084, 3690756, 1222341, 1278376, 3652030, 4162647, 326548, 3930062, 3926100, 1551222, + 2722165, 4526695, 3997534, 4815513, 3139056, 2547644, 3028915, 4149092, 3656554, 2691582, + 2676699, 1878842, 260174, 3129900, 4379993, 182347, 2189338, 3783616, 2616666, 2596952, + 243007, 4179282, 2730, 1939894, 2332032, 3335636, 182332, 3112260, 2174584, 587481, + 4527368, 3154106, 3403059, 673206, 2150292, 446521, 1600204, 4819428, 2591357, 48490, + 2917012, 2285923, 1072926, 2824281, 4364250, 956033, 311938, 37251, 3729300, 2726300, + 644966, 1623020, 1419070, 4646747, 2417222, 2680238, 2561083, 1793801, 2349366, 339747, + 611366, 4684147, 4356907, 1277161, 4510381, 3218352, 4161658, 3200733, 1172372, 3997786, + 3169266, 3353418, 2248955, 2875885, 2365369, 498208, 2968066, 2681505, 2059048, 2097106, + 3607540, 1121504, 2016789, 1762605, 3138431, 866081, 3705757, 3833066, 2599788, 760816, + 4046672, 1544367, 2983906, 4842911, 209599, 1250954, 3333704, 561212, 4674336, 2831841, + 3690724, 2929360, 4830834, 1177524, 2487687, 3525137, 875283, 651241, 2110742, 1296646, + 1543739, 4349417, 2384725, 1931751, 1519208, 1520034, 3385008, 3219962, 734912, 170230, + 1741419, 729913, 2860117, 2362381, 1199807, 2424230, 177824, 125948, 2722701, 4687548, + 1140771, 3232742, 4522020, 4376360, 1125603, 590312, 2481884, 138951, 4086775, 615155, + 3395781, 4587272, 283209, 568470, 4296185, 4344150, 2454321, 2672602, 838828, 4051647, + 1709120, 3074610, 693235, 4356087, 3018806, 239410, 2431497, 691186, 766276, 4462126, + 859155, 2370304, 1571808, 1938673, 1694955, 3871296, 4245059, 3987376, 301524, 2512461, + 3410437, 3300380, 684922, 4581995, 3599557, 683515, 1850634, 3704678, 1937490, 2035591, + 3718533, 2065879, 3160765, 1467884, 1912241, 2501509, 3668572, 3390469, 2501150, 612319, + 713633, 1976262, 135946, 3641535, 632083, 13414, 4217765, 4137712, 2550250, 3281035, + 4179598, 961045, 2020694, 4380006, 1345936, 289162, 1359035, 770872, 4509911, 3947317, + 4719693, 248568, 2625660, 1237232, 2153208, 4814282, 1259954, 3677369, 861222, 2883506, + 3339149, 3998335, 491017, 1609022, 2648112, 742132, 649609, 4206953, 3131106, 3504814, + 3344486, 611721, 3215620, 2856233, 4447505, 1949222, 1868345, 712710, 6966, 4730666, + 3181872, 2972889, 3038521, 3525444, 4385208, 1845613, 1124187, 2030476, 4468651, 2478792, + 3473580, 3783357, 1852991, 1648485, 871319, 1670723, 4458328, 3218600, 1811100, 3443356, + 2233873, 3035207, 2548692, 3337891, 3773674, 1552957, 4782811, 3144712, 3523466, 1491315, + 3955852, 1838410, 3164028, 1092543, 776459, 2959379, 2541744, 4064418, 3908320, 2854145, + 3960709, 1348188, 977678, 853619, 1304291, 2848702, 1657913, 1319826, 3322665, 788037, + 2913686, 4471279, 1766285, 348304, 56570, 1892118, 4017244, 401006, 3524539, 4310134, + 1624693, 4081113, 957511, 849400, 129975, 2616130, 378537, 1556787, 3916162, 1039980, + 4407778, 2027690, 4213675, 839863, 683134, 75805, 2493150, 4215796, 81587, 751845, + 1255588, 1947964, 1950470, 859401, 3077088, 3931110, 2316256, 1523761, 4527477, 4237511, + 1123513, 4209796, 3584772, 4250563, 2091754, 1618766, 2139944, 4525352, 382159, 2955887, + 41760, 2313998, 496912, 3791570, 3904792, 3613654, 873959, 127076, 2537797, 2458107, + 4543265, 3661909, 26828, 271816, 17854, 2461269, 1776042, 1573899, 3409957, 4335712, + 4534313, 3392751, 1230124, 2159031, 4444015, 3373087, 3848014, 2026600, 1382747, 3537242, + 4536743, 4714155, 3788371, 3570849, 173741, 211962, 4377778, 119369, 2856973, 2945854, + 1508054, 4503932, 3141566, 1842177, 3448683, 3384614, 2886508, 1573965, 990618, 3053734, + 2918742, 4508753, 1032149, 60943, 4291620, 722607, 2883224, 169359, 4356585, 3725543, + 3678729, 341673, 3592828, 4077251, 3382936, 3885685, 4630994, 1286698, 4449616, 1138430, + 3113385, 4660578, 2539973, 4562286, 4085089, 494737, 3967610, 2130702, 1823755, 1369324, + 3796951, 956299, 141730, 935144, 4381893, 4412545, 1382250, 3024476, 2364546, 3396164, + 3573511, 314081, 577688, 4154135, 1567018, 4047761, 2446220, 1148833, 4842497, 3967186, + 1175290, 3749667, 1209593, 3295627, 3169065, 2460328, 1838486, 1436923, 2843887, 3676426, + 2079145, 2975635, 535071, 4287509, 3281107, 39606, 3115500, 3204573, 722131, 3124073}, + 2, + false))); +CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/experimental/pagerank_test.cpp b/cpp/tests/experimental/pagerank_test.cpp index 4763249aa9e..105cf38acef 100644 --- a/cpp/tests/experimental/pagerank_test.cpp +++ b/cpp/tests/experimental/pagerank_test.cpp @@ -1,5 +1,5 @@ /* - * 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. @@ -14,12 +14,16 @@ * limitations under the License. */ +#include #include +#include #include +#include -#include -#include -#include +#include +#include +#include +#include #include #include @@ -35,15 +39,20 @@ #include #include +// do the perf measurements +// enabled by command line parameter s'--perf' +// +static int PERF = 0; + template -void pagerank_reference(edge_t* offsets, - vertex_t* indices, - weight_t* weights, - vertex_t* personalization_vertices, - result_t* personalization_values, +void pagerank_reference(edge_t const* offsets, + vertex_t const* indices, + std::optional weights, + std::optional personalization_vertices, + std::optional personalization_values, + std::optional personalization_vector_size, result_t* pageranks, vertex_t num_vertices, - vertex_t personalization_vector_size, result_t alpha, result_t epsilon, size_t max_iterations, @@ -52,7 +61,11 @@ void pagerank_reference(edge_t* offsets, if (num_vertices == 0) { return; } if (has_initial_guess) { - auto sum = std::accumulate(pageranks, pageranks + num_vertices, result_t{0.0}); + // use a double type counter (instead of result_t) to accumulate as std::accumulate is + // inaccurate in adding a large number of comparably sized numbers. In C++17 or later, + // std::reduce may be a better option. + auto sum = + static_cast(std::accumulate(pageranks, pageranks + num_vertices, double{0.0})); ASSERT_TRUE(sum > 0.0); std::for_each(pageranks, pageranks + num_vertices, [sum](auto& val) { val /= sum; }); } else { @@ -61,20 +74,23 @@ void pagerank_reference(edge_t* offsets, }); } - if (personalization_vertices != nullptr) { - auto sum = std::accumulate( - personalization_values, personalization_values + personalization_vector_size, result_t{0.0}); - ASSERT_TRUE(sum > 0.0); - std::for_each(personalization_values, - personalization_values + personalization_vector_size, - [sum](auto& val) { val /= sum; }); + result_t personalization_sum{0.0}; + if (personalization_vertices) { + // use a double type counter (instead of result_t) to accumulate as std::accumulate is + // inaccurate in adding a large number of comparably sized numbers. In C++17 or later, + // std::reduce may be a better option. + personalization_sum = + static_cast(std::accumulate(*personalization_values, + *personalization_values + *personalization_vector_size, + double{0.0})); + ASSERT_TRUE(personalization_sum > 0.0); } std::vector out_weight_sums(num_vertices, result_t{0.0}); for (vertex_t i = 0; i < num_vertices; ++i) { for (auto j = *(offsets + i); j < *(offsets + i + 1); ++j) { auto nbr = indices[j]; - auto w = weights != nullptr ? weights[j] : 1.0; + auto w = weights ? (*weights)[j] : weight_t{1.0}; out_weight_sums[nbr] += w; } } @@ -91,18 +107,19 @@ void pagerank_reference(edge_t* offsets, pageranks[i] = result_t{0.0}; for (auto j = *(offsets + i); j < *(offsets + i + 1); ++j) { auto nbr = indices[j]; - auto w = weights != nullptr ? weights[j] : result_t{1.0}; + auto w = weights ? (*weights)[j] : result_t{1.0}; pageranks[i] += alpha * old_pageranks[nbr] * (w / out_weight_sums[nbr]); } - if (personalization_vertices == nullptr) { + if (!personalization_vertices) { pageranks[i] += (dangling_sum * alpha + (1.0 - alpha)) / static_cast(num_vertices); } } - if (personalization_vertices != nullptr) { - for (vertex_t i = 0; i < personalization_vector_size; ++i) { - auto v = personalization_vertices[i]; - pageranks[v] += (dangling_sum * alpha + (1.0 - alpha)) * personalization_values[i]; + if (personalization_vertices) { + for (vertex_t i = 0; i < *personalization_vector_size; ++i) { + auto v = (*personalization_vertices)[i]; + pageranks[v] += (dangling_sum * alpha + (1.0 - alpha)) * + ((*personalization_values)[i] / personalization_sum); } } result_t diff_sum{0.0}; @@ -117,25 +134,15 @@ void pagerank_reference(edge_t* offsets, return; } -typedef struct PageRank_Usecase_t { - std::string graph_file_full_path{}; +struct PageRank_Usecase { double personalization_ratio{0.0}; bool test_weighted{false}; + bool check_correctness{true}; +}; - PageRank_Usecase_t(std::string const& graph_file_path, - double personalization_ratio, - bool test_weighted) - : personalization_ratio(personalization_ratio), test_weighted(test_weighted) - { - if ((graph_file_path.length() > 0) && (graph_file_path[0] != '/')) { - graph_file_full_path = cugraph::test::get_rapids_dataset_root_dir() + "/" + graph_file_path; - } else { - graph_file_full_path = graph_file_path; - } - }; -} PageRank_Usecase; - -class Tests_PageRank : public ::testing::TestWithParam { +template +class Tests_PageRank + : public ::testing::TestWithParam> { public: Tests_PageRank() {} static void SetupTestCase() {} @@ -145,163 +152,317 @@ class Tests_PageRank : public ::testing::TestWithParam { virtual void TearDown() {} template - void run_current_test(PageRank_Usecase const& configuration) + void run_current_test(PageRank_Usecase const& pagerank_usecase, + input_usecase_t const& input_usecase) { + constexpr bool renumber = true; + raft::handle_t handle{}; + HighResClock hr_clock{}; - auto graph = - cugraph::test::read_graph_from_matrix_market_file( - handle, configuration.graph_file_full_path, configuration.test_weighted); - auto graph_view = graph.view(); + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + hr_clock.start(); + } - std::vector h_offsets(graph_view.get_number_of_vertices() + 1); - std::vector h_indices(graph_view.get_number_of_edges()); - std::vector h_weights{}; - raft::update_host(h_offsets.data(), - graph_view.offsets(), - graph_view.get_number_of_vertices() + 1, - handle.get_stream()); - raft::update_host(h_indices.data(), - graph_view.indices(), - graph_view.get_number_of_edges(), - handle.get_stream()); - if (graph_view.is_weighted()) { - h_weights.assign(graph_view.get_number_of_edges(), weight_t{0.0}); - raft::update_host(h_weights.data(), - graph_view.weights(), - graph_view.get_number_of_edges(), - handle.get_stream()); + auto [graph, d_renumber_map_labels] = + input_usecase.template construct_graph( + handle, pagerank_usecase.test_weighted, renumber); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "construct_graph took " << elapsed_time * 1e-6 << " s.\n"; } - CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); - std::vector h_personalization_vertices{}; - std::vector h_personalization_values{}; - if (configuration.personalization_ratio > 0.0) { - std::random_device r{}; - std::default_random_engine generator{r()}; + auto graph_view = graph.view(); + + std::optional> h_personalization_vertices{std::nullopt}; + std::optional> h_personalization_values{std::nullopt}; + if (pagerank_usecase.personalization_ratio > 0.0) { + std::default_random_engine generator{}; std::uniform_real_distribution distribution{0.0, 1.0}; - h_personalization_vertices.resize(graph_view.get_number_of_local_vertices()); - std::iota(h_personalization_vertices.begin(), - h_personalization_vertices.end(), + h_personalization_vertices = std::vector(graph_view.get_number_of_local_vertices()); + std::iota((*h_personalization_vertices).begin(), + (*h_personalization_vertices).end(), graph_view.get_local_vertex_first()); - h_personalization_vertices.erase( - std::remove_if(h_personalization_vertices.begin(), - h_personalization_vertices.end(), - [&generator, &distribution, configuration](auto v) { - return distribution(generator) >= configuration.personalization_ratio; - }), - h_personalization_vertices.end()); - h_personalization_values.resize(h_personalization_vertices.size()); - std::for_each(h_personalization_values.begin(), - h_personalization_values.end(), + (*h_personalization_vertices) + .erase(std::remove_if((*h_personalization_vertices).begin(), + (*h_personalization_vertices).end(), + [&generator, &distribution, pagerank_usecase](auto v) { + return distribution(generator) >= + pagerank_usecase.personalization_ratio; + }), + (*h_personalization_vertices).end()); + h_personalization_values = std::vector((*h_personalization_vertices).size()); + std::for_each((*h_personalization_values).begin(), + (*h_personalization_values).end(), [&distribution, &generator](auto& val) { val = distribution(generator); }); - auto sum = std::accumulate( - h_personalization_values.begin(), h_personalization_values.end(), result_t{0.0}); - std::for_each(h_personalization_values.begin(), - h_personalization_values.end(), + // use a double type counter (instead of result_t) to accumulate as std::accumulate is + // inaccurate in adding a large number of comparably sized numbers. In C++17 or later, + // std::reduce may be a better option. + auto sum = static_cast(std::accumulate( + (*h_personalization_values).begin(), (*h_personalization_values).end(), double{0.0})); + std::for_each((*h_personalization_values).begin(), + (*h_personalization_values).end(), [sum](auto& val) { val /= sum; }); } - rmm::device_uvector d_personalization_vertices(h_personalization_vertices.size(), - handle.get_stream()); - rmm::device_uvector d_personalization_values(d_personalization_vertices.size(), - handle.get_stream()); - if (d_personalization_vertices.size() > 0) { - raft::update_device(d_personalization_vertices.data(), - h_personalization_vertices.data(), - h_personalization_vertices.size(), + auto d_personalization_vertices = + h_personalization_vertices ? std::make_optional>( + (*h_personalization_vertices).size(), handle.get_stream()) + : std::nullopt; + auto d_personalization_values = h_personalization_values + ? std::make_optional>( + (*d_personalization_vertices).size(), handle.get_stream()) + : std::nullopt; + if (d_personalization_vertices) { + raft::update_device((*d_personalization_vertices).data(), + (*h_personalization_vertices).data(), + (*h_personalization_vertices).size(), handle.get_stream()); - raft::update_device(d_personalization_values.data(), - h_personalization_values.data(), - h_personalization_values.size(), + raft::update_device((*d_personalization_values).data(), + (*h_personalization_values).data(), + (*h_personalization_values).size(), handle.get_stream()); } - std::vector h_reference_pageranks(graph_view.get_number_of_vertices()); - result_t constexpr alpha{0.85}; result_t constexpr epsilon{1e-6}; - pagerank_reference(h_offsets.data(), - h_indices.data(), - h_weights.size() > 0 ? h_weights.data() : static_cast(nullptr), - h_personalization_vertices.data(), - h_personalization_values.data(), - h_reference_pageranks.data(), - graph_view.get_number_of_vertices(), - static_cast(h_personalization_vertices.size()), - alpha, - epsilon, - std::numeric_limits::max(), - false); - rmm::device_uvector d_pageranks(graph_view.get_number_of_vertices(), handle.get_stream()); - CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement - - cugraph::experimental::pagerank(handle, - graph_view, - static_cast(nullptr), - d_personalization_vertices.data(), - d_personalization_values.data(), - static_cast(d_personalization_vertices.size()), - d_pageranks.begin(), - alpha, - epsilon, - std::numeric_limits::max(), - false, - false); - - CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement - - std::vector h_cugraph_pageranks(graph_view.get_number_of_vertices()); - - raft::update_host( - h_cugraph_pageranks.data(), d_pageranks.data(), d_pageranks.size(), handle.get_stream()); - CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); - - auto threshold_ratio = 1e-3; - auto threshold_magnitude = - (epsilon / static_cast(graph_view.get_number_of_vertices())) * threshold_ratio; - auto nearly_equal = [threshold_ratio, threshold_magnitude](auto lhs, auto rhs) { - auto diff = std::abs(lhs - rhs); - return (diff < std::max(lhs, rhs) * threshold_ratio) || (diff < threshold_magnitude); - }; - - ASSERT_TRUE(std::equal(h_reference_pageranks.begin(), - h_reference_pageranks.end(), - h_cugraph_pageranks.begin(), - nearly_equal)) - << "PageRank values do not match with the reference values."; + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + hr_clock.start(); + } + + cugraph::experimental::pagerank( + handle, + graph_view, + std::nullopt, + d_personalization_vertices + ? std::optional{(*d_personalization_vertices).data()} + : std::nullopt, + d_personalization_values ? std::optional{(*d_personalization_values).data()} + : std::nullopt, + d_personalization_vertices ? std::optional{(*d_personalization_vertices).size()} + : std::nullopt, + d_pageranks.data(), + alpha, + epsilon, + std::numeric_limits::max(), + false, + false); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "PageRank took " << elapsed_time * 1e-6 << " s.\n"; + } + + if (pagerank_usecase.check_correctness) { + cugraph::experimental::graph_t unrenumbered_graph( + handle); + if (renumber) { + std::tie(unrenumbered_graph, std::ignore) = + input_usecase.template construct_graph( + handle, pagerank_usecase.test_weighted, false); + } + auto unrenumbered_graph_view = renumber ? unrenumbered_graph.view() : graph_view; + + std::vector h_offsets(unrenumbered_graph_view.get_number_of_vertices() + 1); + std::vector h_indices(unrenumbered_graph_view.get_number_of_edges()); + auto h_weights = unrenumbered_graph_view.is_weighted() + ? std::make_optional>( + unrenumbered_graph_view.get_number_of_edges(), weight_t{0.0}) + : std::nullopt; + raft::update_host(h_offsets.data(), + unrenumbered_graph_view.get_matrix_partition_view().get_offsets(), + unrenumbered_graph_view.get_number_of_vertices() + 1, + handle.get_stream()); + raft::update_host(h_indices.data(), + unrenumbered_graph_view.get_matrix_partition_view().get_indices(), + unrenumbered_graph_view.get_number_of_edges(), + handle.get_stream()); + if (h_weights) { + raft::update_host((*h_weights).data(), + *(unrenumbered_graph_view.get_matrix_partition_view().get_weights()), + unrenumbered_graph_view.get_number_of_edges(), + handle.get_stream()); + } + + auto h_unrenumbered_personalization_vertices = + d_personalization_vertices + ? std::make_optional>((*d_personalization_vertices).size()) + : std::nullopt; + auto h_unrenumbered_personalization_values = + d_personalization_vertices + ? std::make_optional>((*d_personalization_vertices).size()) + : std::nullopt; + if (h_unrenumbered_personalization_vertices) { + if (renumber) { + rmm::device_uvector d_unrenumbered_personalization_vertices( + (*d_personalization_vertices).size(), handle.get_stream()); + rmm::device_uvector d_unrenumbered_personalization_values( + d_unrenumbered_personalization_vertices.size(), handle.get_stream()); + raft::copy_async(d_unrenumbered_personalization_vertices.data(), + (*d_personalization_vertices).data(), + (*d_personalization_vertices).size(), + handle.get_stream()); + raft::copy_async(d_unrenumbered_personalization_values.data(), + (*d_personalization_values).data(), + (*d_personalization_values).size(), + handle.get_stream()); + cugraph::experimental::unrenumber_local_int_vertices( + handle, + d_unrenumbered_personalization_vertices.data(), + d_unrenumbered_personalization_vertices.size(), + (*d_renumber_map_labels).data(), + vertex_t{0}, + graph_view.get_number_of_vertices()); + std::tie(d_unrenumbered_personalization_vertices, d_unrenumbered_personalization_values) = + cugraph::test::sort_by_key(handle, + d_unrenumbered_personalization_vertices.data(), + d_unrenumbered_personalization_values.data(), + d_unrenumbered_personalization_vertices.size()); + + raft::update_host((*h_unrenumbered_personalization_vertices).data(), + d_unrenumbered_personalization_vertices.data(), + d_unrenumbered_personalization_vertices.size(), + handle.get_stream()); + raft::update_host((*h_unrenumbered_personalization_values).data(), + d_unrenumbered_personalization_values.data(), + d_unrenumbered_personalization_values.size(), + handle.get_stream()); + } else { + raft::update_host((*h_unrenumbered_personalization_vertices).data(), + (*d_personalization_vertices).data(), + (*d_personalization_vertices).size(), + handle.get_stream()); + raft::update_host((*h_unrenumbered_personalization_values).data(), + (*d_personalization_values).data(), + (*d_personalization_values).size(), + handle.get_stream()); + } + } + + handle.get_stream_view().synchronize(); + + std::vector h_reference_pageranks(unrenumbered_graph_view.get_number_of_vertices()); + + pagerank_reference( + h_offsets.data(), + h_indices.data(), + h_weights ? std::optional{(*h_weights).data()} : std::nullopt, + h_unrenumbered_personalization_vertices + ? std::optional{(*h_unrenumbered_personalization_vertices).data()} + : std::nullopt, + h_unrenumbered_personalization_values + ? std::optional{(*h_unrenumbered_personalization_values).data()} + : std::nullopt, + h_unrenumbered_personalization_vertices + ? std::optional{static_cast( + (*h_unrenumbered_personalization_vertices).size())} + : std::nullopt, + h_reference_pageranks.data(), + unrenumbered_graph_view.get_number_of_vertices(), + alpha, + epsilon, + std::numeric_limits::max(), + false); + + std::vector h_cugraph_pageranks(graph_view.get_number_of_vertices()); + if (renumber) { + rmm::device_uvector d_unrenumbered_pageranks(size_t{0}, handle.get_stream()); + std::tie(std::ignore, d_unrenumbered_pageranks) = + cugraph::test::sort_by_key(handle, + (*d_renumber_map_labels).data(), + d_pageranks.data(), + (*d_renumber_map_labels).size()); + raft::update_host(h_cugraph_pageranks.data(), + d_unrenumbered_pageranks.data(), + d_unrenumbered_pageranks.size(), + handle.get_stream()); + } else { + raft::update_host( + h_cugraph_pageranks.data(), d_pageranks.data(), d_pageranks.size(), handle.get_stream()); + } + + handle.get_stream_view().synchronize(); + + auto threshold_ratio = 1e-3; + auto threshold_magnitude = + (1.0 / static_cast(graph_view.get_number_of_vertices())) * + threshold_ratio; // skip comparison for low PageRank verties (lowly ranked vertices) + auto nearly_equal = [threshold_ratio, threshold_magnitude](auto lhs, auto rhs) { + return std::abs(lhs - rhs) < + std::max(std::max(lhs, rhs) * threshold_ratio, threshold_magnitude); + }; + + ASSERT_TRUE(std::equal(h_reference_pageranks.begin(), + h_reference_pageranks.end(), + h_cugraph_pageranks.begin(), + nearly_equal)) + << "PageRank values do not match with the reference values."; + } } }; +using Tests_PageRank_File = Tests_PageRank; +using Tests_PageRank_Rmat = Tests_PageRank; + +// FIXME: add tests for type combinations +TEST_P(Tests_PageRank_File, CheckInt32Int32FloatFloat) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + // FIXME: add tests for type combinations -TEST_P(Tests_PageRank, CheckInt32Int32FloatFloat) +TEST_P(Tests_PageRank_Rmat, CheckInt32Int32FloatFloat) { - run_current_test(GetParam()); + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); } -INSTANTIATE_TEST_CASE_P( - simple_test, - Tests_PageRank, - ::testing::Values(PageRank_Usecase("test/datasets/karate.mtx", 0.0, false), - PageRank_Usecase("test/datasets/karate.mtx", 0.5, false), - PageRank_Usecase("test/datasets/karate.mtx", 0.0, true), - PageRank_Usecase("test/datasets/karate.mtx", 0.5, true), - PageRank_Usecase("test/datasets/web-Google.mtx", 0.0, false), - PageRank_Usecase("test/datasets/web-Google.mtx", 0.5, false), - PageRank_Usecase("test/datasets/web-Google.mtx", 0.0, true), - PageRank_Usecase("test/datasets/web-Google.mtx", 0.5, true), - PageRank_Usecase("test/datasets/ljournal-2008.mtx", 0.0, false), - PageRank_Usecase("test/datasets/ljournal-2008.mtx", 0.5, false), - PageRank_Usecase("test/datasets/ljournal-2008.mtx", 0.0, true), - PageRank_Usecase("test/datasets/ljournal-2008.mtx", 0.5, true), - PageRank_Usecase("test/datasets/webbase-1M.mtx", 0.0, false), - // FIXME: Re-enable test after failures are addressed - // PageRank_Usecase("test/datasets/webbase-1M.mtx", 0.5, false), - PageRank_Usecase("test/datasets/webbase-1M.mtx", 0.0, true), - PageRank_Usecase("test/datasets/webbase-1M.mtx", 0.5, true))); +INSTANTIATE_TEST_SUITE_P( + file_test, + Tests_PageRank_File, + ::testing::Combine( + // enable correctness checks + ::testing::Values(PageRank_Usecase{0.0, false}, + PageRank_Usecase{0.5, false}, + PageRank_Usecase{0.0, true}, + PageRank_Usecase{0.5, true}), + ::testing::Values(cugraph::test::File_Usecase("test/datasets/karate.mtx"), + cugraph::test::File_Usecase("test/datasets/web-Google.mtx"), + cugraph::test::File_Usecase("test/datasets/ljournal-2008.mtx"), + cugraph::test::File_Usecase("test/datasets/webbase-1M.mtx")))); + +INSTANTIATE_TEST_SUITE_P( + rmat_small_tests, + Tests_PageRank_Rmat, + ::testing::Combine( + // enable correctness checks + ::testing::Values(PageRank_Usecase{0.0, false}, + PageRank_Usecase{0.5, false}, + PageRank_Usecase{0.0, true}, + PageRank_Usecase{0.5, true}), + ::testing::Values(cugraph::test::Rmat_Usecase(10, 16, 0.57, 0.19, 0.19, 0, false, false)))); + +INSTANTIATE_TEST_SUITE_P( + rmat_large_tests, + Tests_PageRank_Rmat, + ::testing::Combine( + // disable correctness checks for large graphs + ::testing::Values(PageRank_Usecase{0.0, false, false}, + PageRank_Usecase{0.5, false, false}, + PageRank_Usecase{0.0, true, false}, + PageRank_Usecase{0.5, true, false}), + ::testing::Values(cugraph::test::Rmat_Usecase(20, 32, 0.57, 0.19, 0.19, 0, false, false)))); CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/experimental/sssp_test.cpp b/cpp/tests/experimental/sssp_test.cpp index 49eaca56f56..07947a7a059 100644 --- a/cpp/tests/experimental/sssp_test.cpp +++ b/cpp/tests/experimental/sssp_test.cpp @@ -1,5 +1,5 @@ /* - * 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. @@ -14,12 +14,16 @@ * limitations under the License. */ +#include #include +#include #include +#include -#include -#include -#include +#include +#include +#include +#include #include #include @@ -28,31 +32,38 @@ #include +#include #include #include #include #include #include +// do the perf measurements +// enabled by command line parameter s'--perf' +// +static int PERF = 0; + // Dijkstra's algorithm template -void sssp_reference(edge_t* offsets, - vertex_t* indices, - weight_t* weights, +void sssp_reference(edge_t const* offsets, + vertex_t const* indices, + weight_t const* weights, weight_t* distances, vertex_t* predecessors, vertex_t num_vertices, vertex_t source, weight_t cutoff = std::numeric_limits::max()) { - using queue_iterm_t = std::tuple; + using queue_item_t = std::tuple; std::fill(distances, distances + num_vertices, std::numeric_limits::max()); - std::fill(predecessors, predecessors + num_vertices, cugraph::invalid_vertex_id::value); + std::fill(predecessors, + predecessors + num_vertices, + cugraph::experimental::invalid_vertex_id::value); *(distances + source) = weight_t{0.0}; - std::priority_queue, std::greater> - queue{}; + std::priority_queue, std::greater> queue{}; queue.push(std::make_tuple(weight_t{0.0}, source)); while (queue.size() > 0) { @@ -78,21 +89,13 @@ void sssp_reference(edge_t* offsets, return; } -typedef struct SSSP_Usecase_t { - std::string graph_file_full_path{}; - size_t source{false}; - - SSSP_Usecase_t(std::string const& graph_file_path, size_t source) : source(source) - { - if ((graph_file_path.length() > 0) && (graph_file_path[0] != '/')) { - graph_file_full_path = cugraph::test::get_rapids_dataset_root_dir() + "/" + graph_file_path; - } else { - graph_file_full_path = graph_file_path; - } - }; -} SSSP_Usecase; +struct SSSP_Usecase { + size_t source{0}; + bool check_correctness{true}; +}; -class Tests_SSSP : public ::testing::TestWithParam { +template +class Tests_SSSP : public ::testing::TestWithParam> { public: Tests_SSSP() {} static void SetupTestCase() {} @@ -102,122 +105,230 @@ class Tests_SSSP : public ::testing::TestWithParam { virtual void TearDown() {} template - void run_current_test(SSSP_Usecase const& configuration) + void run_current_test(SSSP_Usecase const& sssp_usecase, input_usecase_t const& input_usecase) { + constexpr bool renumber = true; + raft::handle_t handle{}; + HighResClock hr_clock{}; + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + hr_clock.start(); + } + + auto [graph, d_renumber_map_labels] = + input_usecase.template construct_graph( + handle, true, renumber); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "construct_graph took " << elapsed_time * 1e-6 << " s.\n"; + } - auto graph = - cugraph::test::read_graph_from_matrix_market_file( - handle, configuration.graph_file_full_path, true); auto graph_view = graph.view(); - std::vector h_offsets(graph_view.get_number_of_vertices() + 1); - std::vector h_indices(graph_view.get_number_of_edges()); - std::vector h_weights(graph_view.get_number_of_edges()); - raft::update_host(h_offsets.data(), - graph_view.offsets(), - graph_view.get_number_of_vertices() + 1, - handle.get_stream()); - raft::update_host(h_indices.data(), - graph_view.indices(), - graph_view.get_number_of_edges(), - handle.get_stream()); - raft::update_host(h_weights.data(), - graph_view.weights(), - graph_view.get_number_of_edges(), - handle.get_stream()); - CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); - - ASSERT_TRUE(configuration.source >= 0 && - configuration.source <= graph_view.get_number_of_vertices()) - << "Starting sources should be >= 0 and" - << " less than the number of vertices in the graph."; - - std::vector h_reference_distances(graph_view.get_number_of_vertices()); - std::vector h_reference_predecessors(graph_view.get_number_of_vertices()); - - sssp_reference(h_offsets.data(), - h_indices.data(), - h_weights.data(), - h_reference_distances.data(), - h_reference_predecessors.data(), - graph_view.get_number_of_vertices(), - static_cast(configuration.source)); + ASSERT_TRUE(static_cast(sssp_usecase.source) >= 0 && + static_cast(sssp_usecase.source) < graph_view.get_number_of_vertices()); rmm::device_uvector d_distances(graph_view.get_number_of_vertices(), handle.get_stream()); rmm::device_uvector d_predecessors(graph_view.get_number_of_vertices(), handle.get_stream()); - CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + hr_clock.start(); + } cugraph::experimental::sssp(handle, graph_view, - d_distances.begin(), - d_predecessors.begin(), - static_cast(configuration.source), + d_distances.data(), + d_predecessors.data(), + static_cast(sssp_usecase.source), std::numeric_limits::max(), false); - CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement - - std::vector h_cugraph_distances(graph_view.get_number_of_vertices()); - std::vector h_cugraph_predecessors(graph_view.get_number_of_vertices()); - - raft::update_host( - h_cugraph_distances.data(), d_distances.data(), d_distances.size(), handle.get_stream()); - raft::update_host(h_cugraph_predecessors.data(), - d_predecessors.data(), - d_predecessors.size(), - handle.get_stream()); - CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); - - auto max_weight_element = std::max_element(h_weights.begin(), h_weights.end()); - auto epsilon = *max_weight_element * weight_t{1e-6}; - auto nearly_equal = [epsilon](auto lhs, auto rhs) { return std::fabs(lhs - rhs) < epsilon; }; - - ASSERT_TRUE(std::equal(h_reference_distances.begin(), - h_reference_distances.end(), - h_cugraph_distances.begin(), - nearly_equal)) - << "distances do not match with the reference values."; - - for (auto it = h_cugraph_predecessors.begin(); it != h_cugraph_predecessors.end(); ++it) { - auto i = std::distance(h_cugraph_predecessors.begin(), it); - if (*it == cugraph::invalid_vertex_id::value) { - ASSERT_TRUE(h_reference_predecessors[i] == *it) - << "vertex reachability do not match with the reference."; + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "SSSP took " << elapsed_time * 1e-6 << " s.\n"; + } + + if (sssp_usecase.check_correctness) { + cugraph::experimental::graph_t unrenumbered_graph( + handle); + if (renumber) { + std::tie(unrenumbered_graph, std::ignore) = + input_usecase.template construct_graph( + handle, true, false); + } + auto unrenumbered_graph_view = renumber ? unrenumbered_graph.view() : graph_view; + + std::vector h_offsets(unrenumbered_graph_view.get_number_of_vertices() + 1); + std::vector h_indices(unrenumbered_graph_view.get_number_of_edges()); + std::vector h_weights(unrenumbered_graph_view.get_number_of_edges()); + raft::update_host(h_offsets.data(), + unrenumbered_graph_view.get_matrix_partition_view().get_offsets(), + unrenumbered_graph_view.get_number_of_vertices() + 1, + handle.get_stream()); + raft::update_host(h_indices.data(), + unrenumbered_graph_view.get_matrix_partition_view().get_indices(), + unrenumbered_graph_view.get_number_of_edges(), + handle.get_stream()); + raft::update_host(h_weights.data(), + *(unrenumbered_graph_view.get_matrix_partition_view().get_weights()), + unrenumbered_graph_view.get_number_of_edges(), + handle.get_stream()); + + handle.get_stream_view().synchronize(); + + auto unrenumbered_source = static_cast(sssp_usecase.source); + if (renumber) { + std::vector h_renumber_map_labels((*d_renumber_map_labels).size()); + raft::update_host(h_renumber_map_labels.data(), + (*d_renumber_map_labels).data(), + (*d_renumber_map_labels).size(), + handle.get_stream()); + + handle.get_stream_view().synchronize(); + + unrenumbered_source = h_renumber_map_labels[sssp_usecase.source]; + } + + std::vector h_reference_distances(unrenumbered_graph_view.get_number_of_vertices()); + std::vector h_reference_predecessors( + unrenumbered_graph_view.get_number_of_vertices()); + + sssp_reference(h_offsets.data(), + h_indices.data(), + h_weights.data(), + h_reference_distances.data(), + h_reference_predecessors.data(), + unrenumbered_graph_view.get_number_of_vertices(), + unrenumbered_source, + std::numeric_limits::max()); + + std::vector h_cugraph_distances(graph_view.get_number_of_vertices()); + std::vector h_cugraph_predecessors(graph_view.get_number_of_vertices()); + if (renumber) { + cugraph::experimental::unrenumber_local_int_vertices(handle, + d_predecessors.data(), + d_predecessors.size(), + (*d_renumber_map_labels).data(), + vertex_t{0}, + graph_view.get_number_of_vertices(), + true); + + rmm::device_uvector d_unrenumbered_distances(size_t{0}, handle.get_stream()); + std::tie(std::ignore, d_unrenumbered_distances) = + cugraph::test::sort_by_key(handle, + (*d_renumber_map_labels).data(), + d_distances.data(), + (*d_renumber_map_labels).size()); + rmm::device_uvector d_unrenumbered_predecessors(size_t{0}, handle.get_stream()); + std::tie(std::ignore, d_unrenumbered_predecessors) = + cugraph::test::sort_by_key(handle, + (*d_renumber_map_labels).data(), + d_predecessors.data(), + (*d_renumber_map_labels).size()); + + raft::update_host(h_cugraph_distances.data(), + d_unrenumbered_distances.data(), + d_unrenumbered_distances.size(), + handle.get_stream()); + raft::update_host(h_cugraph_predecessors.data(), + d_unrenumbered_predecessors.data(), + d_unrenumbered_predecessors.size(), + handle.get_stream()); + + handle.get_stream_view().synchronize(); } else { - auto pred_distance = h_reference_distances[*it]; - bool found{false}; - for (auto j = h_offsets[*it]; j < h_offsets[*it + 1]; ++j) { - if (h_indices[j] == i) { - if (nearly_equal(pred_distance + h_weights[j], h_reference_distances[i])) { - found = true; - break; + raft::update_host( + h_cugraph_distances.data(), d_distances.data(), d_distances.size(), handle.get_stream()); + raft::update_host(h_cugraph_predecessors.data(), + d_predecessors.data(), + d_predecessors.size(), + handle.get_stream()); + + handle.get_stream_view().synchronize(); + } + + auto max_weight_element = std::max_element(h_weights.begin(), h_weights.end()); + auto epsilon = *max_weight_element * weight_t{1e-6}; + auto nearly_equal = [epsilon](auto lhs, auto rhs) { return std::fabs(lhs - rhs) < epsilon; }; + + ASSERT_TRUE(std::equal(h_reference_distances.begin(), + h_reference_distances.end(), + h_cugraph_distances.begin(), + nearly_equal)) + << "distances do not match with the reference values."; + + for (auto it = h_cugraph_predecessors.begin(); it != h_cugraph_predecessors.end(); ++it) { + auto i = std::distance(h_cugraph_predecessors.begin(), it); + if (*it == cugraph::experimental::invalid_vertex_id::value) { + ASSERT_TRUE(h_reference_predecessors[i] == *it) + << "vertex reachability do not match with the reference."; + } else { + auto pred_distance = h_reference_distances[*it]; + bool found{false}; + for (auto j = h_offsets[*it]; j < h_offsets[*it + 1]; ++j) { + if (h_indices[j] == i) { + if (nearly_equal(pred_distance + h_weights[j], h_reference_distances[i])) { + found = true; + break; + } } } + ASSERT_TRUE(found) + << "no edge from the predecessor vertex to this vertex with the matching weight."; } - ASSERT_TRUE(found) - << "no edge from the predecessor vertex to this vertex with the matching weight."; } } } }; +using Tests_SSSP_File = Tests_SSSP; +using Tests_SSSP_Rmat = Tests_SSSP; + // FIXME: add tests for type combinations -TEST_P(Tests_SSSP, CheckInt32Int32Float) { run_current_test(GetParam()); } - -#if 0 -INSTANTIATE_TEST_CASE_P(simple_test, - Tests_SSSP, - ::testing::Values(SSSP_Usecase("test/datasets/karate.mtx", 0))); -#else -INSTANTIATE_TEST_CASE_P(simple_test, - Tests_SSSP, - ::testing::Values(SSSP_Usecase("test/datasets/karate.mtx", 0), - SSSP_Usecase("test/datasets/dblp.mtx", 0), - SSSP_Usecase("test/datasets/wiki2003.mtx", 1000))); -#endif +TEST_P(Tests_SSSP_File, CheckInt32Int32Float) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} +TEST_P(Tests_SSSP_Rmat, CheckInt32Int32Float) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +INSTANTIATE_TEST_SUITE_P( + file_test, + Tests_SSSP_File, + // enable correctness checks + ::testing::Values( + std::make_tuple(SSSP_Usecase{0}, cugraph::test::File_Usecase("test/datasets/karate.mtx")), + std::make_tuple(SSSP_Usecase{0}, cugraph::test::File_Usecase("test/datasets/dblp.mtx")), + std::make_tuple(SSSP_Usecase{1000}, + cugraph::test::File_Usecase("test/datasets/wiki2003.mtx")))); + +INSTANTIATE_TEST_SUITE_P( + rmat_small_test, + Tests_SSSP_Rmat, + // enable correctness checks + ::testing::Values(std::make_tuple( + SSSP_Usecase{0}, cugraph::test::Rmat_Usecase(10, 16, 0.57, 0.19, 0.19, 0, false, false)))); + +INSTANTIATE_TEST_SUITE_P( + rmat_large_test, + Tests_SSSP_Rmat, + // disable correctness checks for large graphs + ::testing::Values( + std::make_tuple(SSSP_Usecase{0, false}, + cugraph::test::Rmat_Usecase(20, 32, 0.57, 0.19, 0.19, 0, false, false)))); CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/experimental/streams.cu b/cpp/tests/experimental/streams.cu new file mode 100644 index 00000000000..c89ffe1e532 --- /dev/null +++ b/cpp/tests/experimental/streams.cu @@ -0,0 +1,44 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governin_from_mtxg permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include "gtest/gtest.h" +struct StreamTest : public ::testing::Test { +}; +TEST_F(StreamTest, basic_test) +{ + int n_streams = 4; + raft::handle_t handle(n_streams); + + const size_t intput_size = 4096; + +#pragma omp parallel for + for (int i = 0; i < n_streams; i++) { + rmm::device_uvector u(intput_size, handle.get_internal_stream_view(i)), + v(intput_size, handle.get_internal_stream_view(i)); + thrust::transform(rmm::exec_policy(handle.get_internal_stream_view(i)), + u.begin(), + u.end(), + v.begin(), + v.begin(), + 2 * thrust::placeholders::_1 + thrust::placeholders::_2); + } +} \ No newline at end of file diff --git a/cpp/tests/experimental/weight_sum_test.cpp b/cpp/tests/experimental/weight_sum_test.cpp new file mode 100644 index 00000000000..70c42da6136 --- /dev/null +++ b/cpp/tests/experimental/weight_sum_test.cpp @@ -0,0 +1,188 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governin_from_mtxg permissions and + * limitations under the License. + */ + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +template +void weight_sum_reference(edge_t const* offsets, + vertex_t const* indices, + weight_t const* weights, + weight_t* weight_sums, + vertex_t num_vertices, + bool major) +{ + if (!major) { std::fill(weight_sums, weight_sums + num_vertices, weight_t{0.0}); } + for (vertex_t i = 0; i < num_vertices; ++i) { + if (major) { + weight_sums[i] = + std::accumulate(weights + offsets[i], weights + offsets[i + 1], weight_t{0.0}); + } else { + for (auto j = offsets[i]; j < offsets[i + 1]; ++j) { + auto nbr = indices[j]; + weight_sums[nbr] += weights[j]; + } + } + } + + return; +} + +typedef struct WeightSum_Usecase_t { + std::string graph_file_full_path{}; + + WeightSum_Usecase_t(std::string const& graph_file_path) + { + if ((graph_file_path.length() > 0) && (graph_file_path[0] != '/')) { + graph_file_full_path = cugraph::test::get_rapids_dataset_root_dir() + "/" + graph_file_path; + } else { + graph_file_full_path = graph_file_path; + } + }; +} WeightSum_Usecase; + +class Tests_WeightSum : public ::testing::TestWithParam { + public: + Tests_WeightSum() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + + virtual void SetUp() {} + virtual void TearDown() {} + + template + void run_current_test(WeightSum_Usecase const& configuration) + { + raft::handle_t handle{}; + + cugraph::experimental::graph_t graph( + handle); + std::tie(graph, std::ignore) = cugraph::test:: + read_graph_from_matrix_market_file( + handle, configuration.graph_file_full_path, true, false); + auto graph_view = graph.view(); + + std::vector h_offsets(graph_view.get_number_of_vertices() + 1); + std::vector h_indices(graph_view.get_number_of_edges()); + std::vector h_weights(graph_view.get_number_of_edges()); + raft::update_host(h_offsets.data(), + graph_view.get_matrix_partition_view().get_offsets(), + graph_view.get_number_of_vertices() + 1, + handle.get_stream()); + raft::update_host(h_indices.data(), + graph_view.get_matrix_partition_view().get_indices(), + graph_view.get_number_of_edges(), + handle.get_stream()); + raft::update_host(h_weights.data(), + *(graph_view.get_matrix_partition_view().get_weights()), + graph_view.get_number_of_edges(), + handle.get_stream()); + CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); + + std::vector h_reference_in_weight_sums(graph_view.get_number_of_vertices()); + std::vector h_reference_out_weight_sums(graph_view.get_number_of_vertices()); + + weight_sum_reference(h_offsets.data(), + h_indices.data(), + h_weights.data(), + h_reference_in_weight_sums.data(), + graph_view.get_number_of_vertices(), + store_transposed); + + weight_sum_reference(h_offsets.data(), + h_indices.data(), + h_weights.data(), + h_reference_out_weight_sums.data(), + graph_view.get_number_of_vertices(), + !store_transposed); + + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + + auto d_in_weight_sums = graph_view.compute_in_weight_sums(handle); + auto d_out_weight_sums = graph_view.compute_out_weight_sums(handle); + + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + + std::vector h_cugraph_in_weight_sums(graph_view.get_number_of_vertices()); + std::vector h_cugraph_out_weight_sums(graph_view.get_number_of_vertices()); + + raft::update_host(h_cugraph_in_weight_sums.data(), + d_in_weight_sums.data(), + d_in_weight_sums.size(), + handle.get_stream()); + raft::update_host(h_cugraph_out_weight_sums.data(), + d_out_weight_sums.data(), + d_out_weight_sums.size(), + handle.get_stream()); + CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); + + auto threshold_ratio = weight_t{1e-4}; + auto threshold_magnitude = std::numeric_limits::min(); + auto nearly_equal = [threshold_ratio, threshold_magnitude](auto lhs, auto rhs) { + return std::abs(lhs - rhs) < + std::max(std::max(lhs, rhs) * threshold_ratio, threshold_magnitude); + }; + + ASSERT_TRUE(std::equal(h_reference_in_weight_sums.begin(), + h_reference_in_weight_sums.end(), + h_cugraph_in_weight_sums.begin(), + nearly_equal)) + << "In-weight-sum values do not match with the reference values."; + ASSERT_TRUE(std::equal(h_reference_out_weight_sums.begin(), + h_reference_out_weight_sums.end(), + h_cugraph_out_weight_sums.begin(), + nearly_equal)) + << "Out-weight-sum values do not match with the reference values."; + } +}; + +// FIXME: add tests for type combinations + +TEST_P(Tests_WeightSum, CheckInt32Int32FloatTransposed) +{ + run_current_test(GetParam()); +} + +TEST_P(Tests_WeightSum, CheckInt32Int32FloatUntransposed) +{ + run_current_test(GetParam()); +} + +INSTANTIATE_TEST_SUITE_P(simple_test, + Tests_WeightSum, + ::testing::Values(WeightSum_Usecase("test/datasets/karate.mtx"), + WeightSum_Usecase("test/datasets/web-Google.mtx"), + WeightSum_Usecase("test/datasets/ljournal-2008.mtx"), + WeightSum_Usecase("test/datasets/webbase-1M.mtx"))); + +CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/generators/erdos_renyi_test.cpp b/cpp/tests/generators/erdos_renyi_test.cpp new file mode 100644 index 00000000000..3606ce2ddef --- /dev/null +++ b/cpp/tests/generators/erdos_renyi_test.cpp @@ -0,0 +1,98 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governin_from_mtxg permissions and + * limitations under the License. + */ + +#include +#include + +#include +#include + +#include + +#include + +struct GenerateErdosRenyiTest : public ::testing::Test { +}; + +template +void test_symmetric(std::vector& h_src_v, std::vector& h_dst_v) +{ + std::vector reverse_src_v(h_src_v.size()); + std::vector reverse_dst_v(h_dst_v.size()); + + std::copy(h_src_v.begin(), h_src_v.end(), reverse_dst_v.begin()); + std::copy(h_dst_v.begin(), h_dst_v.end(), reverse_src_v.begin()); + + thrust::sort(thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(h_src_v.begin(), h_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(h_src_v.end(), h_dst_v.end()))); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(reverse_src_v.begin(), reverse_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(reverse_src_v.end(), reverse_dst_v.end()))); + + EXPECT_EQ(reverse_src_v, h_src_v); + EXPECT_EQ(reverse_dst_v, h_dst_v); +} + +template +void er_test(size_t num_vertices, float p) +{ + raft::handle_t handle; + rmm::device_uvector d_src_v(0, handle.get_stream()); + rmm::device_uvector d_dst_v(0, handle.get_stream()); + + std::tie(d_src_v, d_dst_v) = + cugraph::generate_erdos_renyi_graph_edgelist_gnp(handle, num_vertices, p, 0); + + handle.get_stream_view().synchronize(); + + std::vector h_src_v(d_src_v.size()); + std::vector h_dst_v(d_dst_v.size()); + + raft::update_host(h_src_v.data(), d_src_v.data(), d_src_v.size(), handle.get_stream()); + raft::update_host(h_dst_v.data(), d_dst_v.data(), d_dst_v.size(), handle.get_stream()); + + handle.get_stream_view().synchronize(); + + float expected_edge_count = p * num_vertices * num_vertices; + + ASSERT_GE(h_src_v.size(), static_cast(expected_edge_count * 0.8)); + ASSERT_LE(h_src_v.size(), static_cast(expected_edge_count * 1.2)); + ASSERT_EQ(std::count_if(h_src_v.begin(), + h_src_v.end(), + [n = static_cast(num_vertices)](auto v) { + return !cugraph::experimental::is_valid_vertex(n, v); + }), + 0); + ASSERT_EQ(std::count_if(h_dst_v.begin(), + h_dst_v.end(), + [n = static_cast(num_vertices)](auto v) { + return !cugraph::experimental::is_valid_vertex(n, v); + }), + 0); +} + +TEST_F(GenerateErdosRenyiTest, ERTest) +{ + er_test(size_t{10}, float{0.1}); + er_test(size_t{20}, float{0.1}); + er_test(size_t{50}, float{0.1}); + er_test(size_t{10000}, float{0.1}); +} + +CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/generators/generators_test.cpp b/cpp/tests/generators/generators_test.cpp new file mode 100644 index 00000000000..11e63d81f36 --- /dev/null +++ b/cpp/tests/generators/generators_test.cpp @@ -0,0 +1,689 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include + +#include +#include +#include + +#include + +struct GeneratorsTest : public ::testing::Test { +}; + +TEST_F(GeneratorsTest, PathGraphTest) +{ + using vertex_t = int32_t; + + std::vector expected_src_v({0, 1, 2, 3}); + std::vector expected_dst_v({1, 2, 3, 4}); + std::vector actual_src_v; + std::vector actual_dst_v; + + std::vector> parameters({{5, 0}}); + + raft::handle_t handle; + + rmm::device_uvector src_v(0, handle.get_stream()); + rmm::device_uvector dst_v(0, handle.get_stream()); + + std::tie(src_v, dst_v) = cugraph::generate_path_graph_edgelist(handle, parameters); + + actual_src_v.resize(src_v.size()); + actual_dst_v.resize(src_v.size()); + + raft::update_host(actual_src_v.data(), src_v.data(), src_v.size(), handle.get_stream()); + raft::update_host(actual_dst_v.data(), dst_v.data(), dst_v.size(), handle.get_stream()); + + EXPECT_EQ(expected_src_v, actual_src_v); + EXPECT_EQ(expected_dst_v, actual_dst_v); +} + +TEST_F(GeneratorsTest, Mesh2DGraphTest) +{ + using vertex_t = int32_t; + + std::vector expected_src_v({0, 1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14, 16, 17, 18, + 20, 21, 22, 0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19}); + std::vector expected_dst_v({1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15, 17, 18, 19, + 21, 22, 23, 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23}); + std::vector actual_src_v; + std::vector actual_dst_v; + + raft::handle_t handle; + + rmm::device_uvector src_v(0, handle.get_stream()); + rmm::device_uvector dst_v(0, handle.get_stream()); + + std::vector> parameters( + {{4, 2, 0}, {4, 2, 8}, {4, 2, 16}}); + + std::tie(src_v, dst_v) = cugraph::generate_2d_mesh_graph_edgelist(handle, parameters); + + actual_src_v.resize(src_v.size()); + actual_dst_v.resize(src_v.size()); + + raft::update_host(actual_src_v.data(), src_v.data(), src_v.size(), handle.get_stream()); + raft::update_host(actual_dst_v.data(), dst_v.data(), dst_v.size(), handle.get_stream()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())) + + expected_src_v.size()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())) + + actual_src_v.size()); + + EXPECT_EQ(expected_src_v, actual_src_v); + EXPECT_EQ(expected_dst_v, actual_dst_v); +} + +TEST_F(GeneratorsTest, Mesh3DGraphTest) +{ + using vertex_t = int32_t; + + std::vector expected_src_v( + {0, 1, 3, 4, 6, 7, 9, 10, 12, 13, 15, 16, 18, 19, 21, 22, 24, 25, 27, 28, 30, 31, 33, 34, + 36, 37, 39, 40, 42, 43, 45, 46, 48, 49, 51, 52, 54, 55, 57, 58, 60, 61, 63, 64, 66, 67, 69, 70, + 72, 73, 75, 76, 78, 79, 0, 1, 2, 3, 4, 5, 9, 10, 11, 12, 13, 14, 18, 19, 20, 21, 22, 23, + 27, 28, 29, 30, 31, 32, 36, 37, 38, 39, 40, 41, 45, 46, 47, 48, 49, 50, 54, 55, 56, 57, 58, 59, + 63, 64, 65, 66, 67, 68, 72, 73, 74, 75, 76, 77, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, + 12, 13, 14, 15, 16, 17, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, + 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71}); + + std::vector expected_dst_v( + {1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, + 37, 38, 40, 41, 43, 44, 46, 47, 49, 50, 52, 53, 55, 56, 58, 59, 61, 62, 64, 65, 67, 68, 70, 71, + 73, 74, 76, 77, 79, 80, 3, 4, 5, 6, 7, 8, 12, 13, 14, 15, 16, 17, 21, 22, 23, 24, 25, 26, + 30, 31, 32, 33, 34, 35, 39, 40, 41, 42, 43, 44, 48, 49, 50, 51, 52, 53, 57, 58, 59, 60, 61, 62, + 66, 67, 68, 69, 70, 71, 75, 76, 77, 78, 79, 80, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, + 21, 22, 23, 24, 25, 26, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, + 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80}); + + std::vector actual_src_v; + std::vector actual_dst_v; + + raft::handle_t handle; + + rmm::device_uvector src_v(0, handle.get_stream()); + rmm::device_uvector dst_v(0, handle.get_stream()); + + std::vector> parameters( + {{3, 3, 3, 0}, {3, 3, 3, 27}, {3, 3, 3, 54}}); + + std::tie(src_v, dst_v) = cugraph::generate_3d_mesh_graph_edgelist(handle, parameters); + + actual_src_v.resize(src_v.size()); + actual_dst_v.resize(src_v.size()); + + raft::update_host(actual_src_v.data(), src_v.data(), src_v.size(), handle.get_stream()); + raft::update_host(actual_dst_v.data(), dst_v.data(), dst_v.size(), handle.get_stream()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())) + + expected_src_v.size()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())) + + actual_src_v.size()); + + EXPECT_EQ(expected_src_v, actual_src_v); + EXPECT_EQ(expected_dst_v, actual_dst_v); +} + +TEST_F(GeneratorsTest, CompleteGraphTestTriangles) +{ + using vertex_t = int32_t; + + std::vector expected_src_v({0, 0, 1, 3, 3, 4, 6, 6, 7}); + std::vector expected_dst_v({1, 2, 2, 4, 5, 5, 7, 8, 8}); + std::vector actual_src_v; + std::vector actual_dst_v; + + raft::handle_t handle; + + rmm::device_uvector src_v(0, handle.get_stream()); + rmm::device_uvector dst_v(0, handle.get_stream()); + + std::vector> parameters({{3, 0}, {3, 3}, {3, 6}}); + + std::tie(src_v, dst_v) = cugraph::generate_complete_graph_edgelist(handle, parameters); + + actual_src_v.resize(src_v.size()); + actual_dst_v.resize(src_v.size()); + + raft::update_host(actual_src_v.data(), src_v.data(), src_v.size(), handle.get_stream()); + raft::update_host(actual_dst_v.data(), dst_v.data(), dst_v.size(), handle.get_stream()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())) + + expected_src_v.size()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())) + + actual_src_v.size()); + + EXPECT_EQ(expected_src_v, actual_src_v); + EXPECT_EQ(expected_dst_v, actual_dst_v); +} + +TEST_F(GeneratorsTest, CompleteGraphTest5) +{ + using vertex_t = int32_t; + + size_t num_vertices{5}; + size_t num_graphs{3}; + + std::vector expected_src_v({0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 5, 5, 5, 5, 6, + 6, 6, 7, 7, 8, 10, 10, 10, 10, 11, 11, 11, 12, 12, 13}); + std::vector expected_dst_v({1, 2, 3, 4, 2, 3, 4, 3, 4, 4, 6, 7, 8, 9, 7, + 8, 9, 8, 9, 9, 11, 12, 13, 14, 12, 13, 14, 13, 14, 14}); + std::vector actual_src_v; + std::vector actual_dst_v; + + raft::handle_t handle; + + rmm::device_uvector src_v(0, handle.get_stream()); + rmm::device_uvector dst_v(0, handle.get_stream()); + + std::vector> parameters({{5, 0}, {5, 5}, {5, 10}}); + + std::tie(src_v, dst_v) = cugraph::generate_complete_graph_edgelist(handle, parameters); + + actual_src_v.resize(src_v.size()); + actual_dst_v.resize(src_v.size()); + + raft::update_host(actual_src_v.data(), src_v.data(), src_v.size(), handle.get_stream()); + raft::update_host(actual_dst_v.data(), dst_v.data(), dst_v.size(), handle.get_stream()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())) + + expected_src_v.size()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())) + + actual_src_v.size()); + + EXPECT_EQ(expected_src_v, actual_src_v); + EXPECT_EQ(expected_dst_v, actual_dst_v); +} + +TEST_F(GeneratorsTest, LineGraphTestSymmetric) +{ + using vertex_t = int32_t; + + size_t num_vertices{5}; + std::vector expected_src_v({0, 1, 2, 3, 1, 2, 3, 4}); + std::vector expected_dst_v({1, 2, 3, 4, 0, 1, 2, 3}); + std::vector actual_src_v; + std::vector actual_dst_v; + + raft::handle_t handle; + + rmm::device_uvector src_v(0, handle.get_stream()); + rmm::device_uvector dst_v(0, handle.get_stream()); + + std::vector> parameters({{5, 0}}); + + std::tie(src_v, dst_v) = cugraph::generate_path_graph_edgelist(handle, parameters); + std::tie(src_v, dst_v, std::ignore) = cugraph::symmetrize_edgelist( + handle, std::move(src_v), std::move(dst_v), std::nullopt); + + actual_src_v.resize(src_v.size()); + actual_dst_v.resize(src_v.size()); + + raft::update_host(actual_src_v.data(), src_v.data(), src_v.size(), handle.get_stream()); + raft::update_host(actual_dst_v.data(), dst_v.data(), dst_v.size(), handle.get_stream()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())) + + expected_src_v.size()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())) + + actual_src_v.size()); + + EXPECT_EQ(expected_src_v, actual_src_v); + EXPECT_EQ(expected_dst_v, actual_dst_v); +} + +TEST_F(GeneratorsTest, Mesh2DGraphTestSymmetric) +{ + using vertex_t = int32_t; + + size_t x{4}; + size_t y{2}; + size_t num_graphs{3}; + + std::vector expected_src_v({0, 1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14, 16, 17, 18, + 20, 21, 22, 0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19, + 1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15, 17, 18, 19, + 21, 22, 23, 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23}); + std::vector expected_dst_v({1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15, 17, 18, 19, + 21, 22, 23, 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, + 0, 1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14, 16, 17, 18, + 20, 21, 22, 0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19}); + std::vector actual_src_v; + std::vector actual_dst_v; + + raft::handle_t handle; + + rmm::device_uvector src_v(0, handle.get_stream()); + rmm::device_uvector dst_v(0, handle.get_stream()); + + std::vector> parameters( + {{4, 2, 0}, {4, 2, 8}, {4, 2, 16}}); + + std::tie(src_v, dst_v) = cugraph::generate_2d_mesh_graph_edgelist(handle, parameters); + std::tie(src_v, dst_v, std::ignore) = cugraph::symmetrize_edgelist( + handle, std::move(src_v), std::move(dst_v), std::nullopt); + + actual_src_v.resize(src_v.size()); + actual_dst_v.resize(src_v.size()); + + raft::update_host(actual_src_v.data(), src_v.data(), src_v.size(), handle.get_stream()); + raft::update_host(actual_dst_v.data(), dst_v.data(), dst_v.size(), handle.get_stream()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())) + + expected_src_v.size()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())) + + actual_src_v.size()); + + EXPECT_EQ(expected_src_v, actual_src_v); + EXPECT_EQ(expected_dst_v, actual_dst_v); +} + +TEST_F(GeneratorsTest, Mesh3DGraphTestSymmetric) +{ + using vertex_t = int32_t; + + size_t x{3}; + size_t y{3}; + size_t z{3}; + size_t num_graphs{3}; + + std::vector expected_src_v( + {0, 1, 3, 4, 6, 7, 9, 10, 12, 13, 15, 16, 18, 19, 21, 22, 24, 25, 27, 28, 30, 31, 33, 34, + 36, 37, 39, 40, 42, 43, 45, 46, 48, 49, 51, 52, 54, 55, 57, 58, 60, 61, 63, 64, 66, 67, 69, 70, + 72, 73, 75, 76, 78, 79, 0, 1, 2, 3, 4, 5, 9, 10, 11, 12, 13, 14, 18, 19, 20, 21, 22, 23, + 27, 28, 29, 30, 31, 32, 36, 37, 38, 39, 40, 41, 45, 46, 47, 48, 49, 50, 54, 55, 56, 57, 58, 59, + 63, 64, 65, 66, 67, 68, 72, 73, 74, 75, 76, 77, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, + 12, 13, 14, 15, 16, 17, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, + 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 1, 2, 4, 5, 7, 8, + 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 37, 38, 40, 41, 43, 44, + 46, 47, 49, 50, 52, 53, 55, 56, 58, 59, 61, 62, 64, 65, 67, 68, 70, 71, 73, 74, 76, 77, 79, 80, + 3, 4, 5, 6, 7, 8, 12, 13, 14, 15, 16, 17, 21, 22, 23, 24, 25, 26, 30, 31, 32, 33, 34, 35, + 39, 40, 41, 42, 43, 44, 48, 49, 50, 51, 52, 53, 57, 58, 59, 60, 61, 62, 66, 67, 68, 69, 70, 71, + 75, 76, 77, 78, 79, 80, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, + 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 63, 64, 65, 66, 67, 68, + 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80}); + + std::vector expected_dst_v( + {1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, + 37, 38, 40, 41, 43, 44, 46, 47, 49, 50, 52, 53, 55, 56, 58, 59, 61, 62, 64, 65, 67, 68, 70, 71, + 73, 74, 76, 77, 79, 80, 3, 4, 5, 6, 7, 8, 12, 13, 14, 15, 16, 17, 21, 22, 23, 24, 25, 26, + 30, 31, 32, 33, 34, 35, 39, 40, 41, 42, 43, 44, 48, 49, 50, 51, 52, 53, 57, 58, 59, 60, 61, 62, + 66, 67, 68, 69, 70, 71, 75, 76, 77, 78, 79, 80, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, + 21, 22, 23, 24, 25, 26, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, + 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 0, 1, 3, 4, 6, 7, + 9, 10, 12, 13, 15, 16, 18, 19, 21, 22, 24, 25, 27, 28, 30, 31, 33, 34, 36, 37, 39, 40, 42, 43, + 45, 46, 48, 49, 51, 52, 54, 55, 57, 58, 60, 61, 63, 64, 66, 67, 69, 70, 72, 73, 75, 76, 78, 79, + 0, 1, 2, 3, 4, 5, 9, 10, 11, 12, 13, 14, 18, 19, 20, 21, 22, 23, 27, 28, 29, 30, 31, 32, + 36, 37, 38, 39, 40, 41, 45, 46, 47, 48, 49, 50, 54, 55, 56, 57, 58, 59, 63, 64, 65, 66, 67, 68, + 72, 73, 74, 75, 76, 77, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, + 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 54, 55, 56, 57, 58, 59, + 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71}); + + std::vector actual_src_v; + std::vector actual_dst_v; + + raft::handle_t handle; + + rmm::device_uvector src_v(0, handle.get_stream()); + rmm::device_uvector dst_v(0, handle.get_stream()); + + std::vector> parameters( + {{3, 3, 3, 0}, {3, 3, 3, 27}, {3, 3, 3, 54}}); + + std::tie(src_v, dst_v) = cugraph::generate_3d_mesh_graph_edgelist(handle, parameters); + std::tie(src_v, dst_v, std::ignore) = cugraph::symmetrize_edgelist( + handle, std::move(src_v), std::move(dst_v), std::nullopt); + + actual_src_v.resize(src_v.size()); + actual_dst_v.resize(src_v.size()); + + raft::update_host(actual_src_v.data(), src_v.data(), src_v.size(), handle.get_stream()); + raft::update_host(actual_dst_v.data(), dst_v.data(), dst_v.size(), handle.get_stream()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())) + + expected_src_v.size()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())) + + actual_src_v.size()); + + EXPECT_EQ(expected_src_v, actual_src_v); + EXPECT_EQ(expected_dst_v, actual_dst_v); +} + +TEST_F(GeneratorsTest, CompleteGraphTestTrianglesSymmetric) +{ + using vertex_t = int32_t; + + size_t num_vertices{3}; + size_t num_graphs{3}; + + std::vector expected_src_v({0, 0, 1, 3, 3, 4, 6, 6, 7, 1, 2, 2, 4, 5, 5, 7, 8, 8}); + std::vector expected_dst_v({1, 2, 2, 4, 5, 5, 7, 8, 8, 0, 0, 1, 3, 3, 4, 6, 6, 7}); + std::vector actual_src_v; + std::vector actual_dst_v; + + raft::handle_t handle; + + rmm::device_uvector src_v(0, handle.get_stream()); + rmm::device_uvector dst_v(0, handle.get_stream()); + + std::vector> parameters({{3, 0}, {3, 3}, {3, 6}}); + + std::tie(src_v, dst_v) = cugraph::generate_complete_graph_edgelist(handle, parameters); + std::tie(src_v, dst_v, std::ignore) = cugraph::symmetrize_edgelist( + handle, std::move(src_v), std::move(dst_v), std::nullopt); + + actual_src_v.resize(src_v.size()); + actual_dst_v.resize(src_v.size()); + + raft::update_host(actual_src_v.data(), src_v.data(), src_v.size(), handle.get_stream()); + raft::update_host(actual_dst_v.data(), dst_v.data(), dst_v.size(), handle.get_stream()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())) + + expected_src_v.size()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())) + + actual_src_v.size()); + + EXPECT_EQ(expected_src_v, actual_src_v); + EXPECT_EQ(expected_dst_v, actual_dst_v); +} + +TEST_F(GeneratorsTest, CompleteGraphTest5Symmetric) +{ + using vertex_t = int32_t; + + size_t num_vertices{5}; + size_t num_graphs{3}; + + std::vector expected_src_v({0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 5, 5, 5, 5, 6, + 6, 6, 7, 7, 8, 10, 10, 10, 10, 11, 11, 11, 12, 12, 13, + 1, 2, 3, 4, 2, 3, 4, 3, 4, 4, 6, 7, 8, 9, 7, + 8, 9, 8, 9, 9, 11, 12, 13, 14, 12, 13, 14, 13, 14, 14}); + std::vector expected_dst_v({1, 2, 3, 4, 2, 3, 4, 3, 4, 4, 6, 7, 8, 9, 7, + 8, 9, 8, 9, 9, 11, 12, 13, 14, 12, 13, 14, 13, 14, 14, + 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 5, 5, 5, 5, 6, + 6, 6, 7, 7, 8, 10, 10, 10, 10, 11, 11, 11, 12, 12, 13}); + std::vector actual_src_v; + std::vector actual_dst_v; + + raft::handle_t handle; + + rmm::device_uvector src_v(0, handle.get_stream()); + rmm::device_uvector dst_v(0, handle.get_stream()); + + std::vector> parameters({{5, 0}, {5, 5}, {5, 10}}); + + std::tie(src_v, dst_v) = cugraph::generate_complete_graph_edgelist(handle, parameters); + std::tie(src_v, dst_v, std::ignore) = cugraph::symmetrize_edgelist( + handle, std::move(src_v), std::move(dst_v), std::nullopt); + + actual_src_v.resize(src_v.size()); + actual_dst_v.resize(src_v.size()); + + raft::update_host(actual_src_v.data(), src_v.data(), src_v.size(), handle.get_stream()); + raft::update_host(actual_dst_v.data(), dst_v.data(), dst_v.size(), handle.get_stream()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())) + + expected_src_v.size()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())) + + actual_src_v.size()); + + EXPECT_EQ(expected_src_v, actual_src_v); + EXPECT_EQ(expected_dst_v, actual_dst_v); +} + +TEST_F(GeneratorsTest, CombineGraphsTest) +{ + using vertex_t = int32_t; + using weight_t = float; + + raft::handle_t handle; + + size_t num_vertices{8}; + + std::vector expected_src_v({0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 13, 14, 16, 17, 18, + 20, 21, 22, 0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19}); + std::vector expected_dst_v({1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 13, 14, 15, 17, 18, 19, + 21, 22, 23, 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23}); + + rmm::device_uvector src_graph_1_v(0, handle.get_stream()); + rmm::device_uvector dst_graph_1_v(0, handle.get_stream()); + rmm::device_uvector src_graph_2_v(0, handle.get_stream()); + rmm::device_uvector dst_graph_2_v(0, handle.get_stream()); + rmm::device_uvector src_v(0, handle.get_stream()); + rmm::device_uvector dst_v(0, handle.get_stream()); + + std::vector> parameters1({{num_vertices, 0}}); + std::vector> parameters2( + {{4, 2, 0}, {4, 2, 8}, {4, 2, 16}}); + + std::tie(src_graph_1_v, dst_graph_1_v) = + cugraph::generate_path_graph_edgelist(handle, parameters1); + std::tie(src_graph_2_v, dst_graph_2_v) = + cugraph::generate_2d_mesh_graph_edgelist(handle, parameters2); + + std::vector> sources; + sources.push_back(std::move(src_graph_1_v)); + sources.push_back(std::move(src_graph_2_v)); + + std::vector> dests; + dests.push_back(std::move(dst_graph_1_v)); + dests.push_back(std::move(dst_graph_2_v)); + + std::tie(src_v, dst_v, std::ignore) = cugraph::combine_edgelists( + handle, std::move(sources), std::move(dests), std::nullopt); + + std::vector actual_src_v; + std::vector actual_dst_v; + + actual_src_v.resize(src_v.size()); + actual_dst_v.resize(dst_v.size()); + + raft::update_host(actual_src_v.data(), src_v.data(), src_v.size(), handle.get_stream()); + raft::update_host(actual_dst_v.data(), dst_v.data(), dst_v.size(), handle.get_stream()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())) + + expected_src_v.size()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())) + + actual_src_v.size()); + + EXPECT_EQ(expected_src_v, actual_src_v); + EXPECT_EQ(expected_dst_v, actual_dst_v); +} + +TEST_F(GeneratorsTest, CombineGraphsOffsetsTest) +{ + using vertex_t = int32_t; + using weight_t = float; + + raft::handle_t handle; + + size_t num_vertices{8}; + vertex_t offset{10}; + + std::vector expected_src_v({0, 1, 2, 3, 4, 5, 6, 10, 11, 12, 14, 15, 16, + 18, 19, 20, 22, 23, 24, 26, 27, 28, 30, 31, 32, 10, + 11, 12, 13, 18, 19, 20, 21, 26, 27, 28, 29}); + std::vector expected_dst_v({1, 2, 3, 4, 5, 6, 7, 11, 12, 13, 15, 16, 17, + 19, 20, 21, 23, 24, 25, 27, 28, 29, 31, 32, 33, 14, + 15, 16, 17, 22, 23, 24, 25, 30, 31, 32, 33}); + + rmm::device_uvector src_graph_1_v(0, handle.get_stream()); + rmm::device_uvector dst_graph_1_v(0, handle.get_stream()); + rmm::device_uvector src_graph_2_v(0, handle.get_stream()); + rmm::device_uvector dst_graph_2_v(0, handle.get_stream()); + rmm::device_uvector src_v(0, handle.get_stream()); + rmm::device_uvector dst_v(0, handle.get_stream()); + + std::vector> parameters1({{num_vertices, 0}}); + std::vector> parameters2( + {{4, 2, 10}, {4, 2, 18}, {4, 2, 26}}); + + std::tie(src_graph_1_v, dst_graph_1_v) = + cugraph::generate_path_graph_edgelist(handle, parameters1); + std::tie(src_graph_2_v, dst_graph_2_v) = + cugraph::generate_2d_mesh_graph_edgelist(handle, parameters2); + + std::vector> sources; + sources.push_back(std::move(src_graph_1_v)); + sources.push_back(std::move(src_graph_2_v)); + + std::vector> dests; + dests.push_back(std::move(dst_graph_1_v)); + dests.push_back(std::move(dst_graph_2_v)); + + std::tie(src_v, dst_v, std::ignore) = cugraph::combine_edgelists( + handle, std::move(sources), std::move(dests), std::nullopt); + + std::vector actual_src_v; + std::vector actual_dst_v; + + actual_src_v.resize(src_v.size()); + actual_dst_v.resize(dst_v.size()); + + raft::update_host(actual_src_v.data(), src_v.data(), src_v.size(), handle.get_stream()); + raft::update_host(actual_dst_v.data(), dst_v.data(), dst_v.size(), handle.get_stream()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(expected_src_v.begin(), expected_dst_v.begin())) + + expected_src_v.size()); + + thrust::sort( + thrust::host, + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())), + thrust::make_zip_iterator(thrust::make_tuple(actual_src_v.begin(), actual_dst_v.begin())) + + actual_src_v.size()); + + EXPECT_EQ(expected_src_v, actual_src_v); + EXPECT_EQ(expected_dst_v, actual_dst_v); +} + +TEST_F(GeneratorsTest, ScrambleTest) +{ + using vertex_t = int32_t; + using edge_t = int32_t; + + edge_t num_vertices{30}; + edge_t num_edges{100}; + + raft::handle_t handle; + + std::vector input_src_v(num_edges); + std::vector input_dst_v(num_edges); + + std::default_random_engine generator{}; + std::uniform_int_distribution distribution{0, num_vertices - 1}; + + std::generate(input_src_v.begin(), input_src_v.end(), [&distribution, &generator]() { + return distribution(generator); + }); + std::generate(input_dst_v.begin(), input_dst_v.end(), [&distribution, &generator]() { + return distribution(generator); + }); + + rmm::device_uvector d_src_v(input_src_v.size(), handle.get_stream()); + rmm::device_uvector d_dst_v(input_src_v.size(), handle.get_stream()); + std::vector output_src_v(input_src_v.size()); + std::vector output_dst_v(input_src_v.size()); + + raft::update_device(d_src_v.data(), input_src_v.data(), input_src_v.size(), handle.get_stream()); + raft::update_device(d_dst_v.data(), input_dst_v.data(), input_dst_v.size(), handle.get_stream()); + + cugraph::scramble_vertex_ids(handle, d_src_v, d_dst_v, 5, 0); + + raft::update_host(output_src_v.data(), d_src_v.data(), d_src_v.size(), handle.get_stream()); + raft::update_host(output_dst_v.data(), d_dst_v.data(), d_dst_v.size(), handle.get_stream()); + + EXPECT_TRUE(cugraph::test::renumbered_vectors_same(handle, input_src_v, output_src_v)); + EXPECT_TRUE(cugraph::test::renumbered_vectors_same(handle, input_dst_v, output_dst_v)); +} + +CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/layout/force_atlas2_test.cu b/cpp/tests/layout/force_atlas2_test.cu index d564765d0df..e736f2d2db5 100644 --- a/cpp/tests/layout/force_atlas2_test.cu +++ b/cpp/tests/layout/force_atlas2_test.cu @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2020-2021, NVIDIA CORPORATION. All rights reserved. * * NVIDIA CORPORATION and its licensors retain all intellectual property * and proprietary rights in and to this software, related documentation @@ -17,8 +17,8 @@ #include #include -#include -#include +#include +#include #include #include @@ -111,9 +111,10 @@ class Tests_Force_Atlas2 : public ::testing::TestWithParam std::vector> adj_matrix(m, std::vector(m)); std::vector force_atlas2(m * 2); + raft::handle_t const handle; + auto stream = handle.get_stream(); // device alloc - rmm::device_vector force_atlas2_vector(m * 2); - float* d_force_atlas2 = force_atlas2_vector.data().get(); + rmm::device_uvector pos(m * 2, stream); // Read ASSERT_EQ((cugraph::test::mm_to_coo( @@ -131,19 +132,19 @@ class Tests_Force_Atlas2 : public ::testing::TestWithParam } // Allocate COO on device - rmm::device_vector srcs_v(nnz); - rmm::device_vector dests_v(nnz); - rmm::device_vector weights_v(nnz); + rmm::device_uvector srcs_v(nnz, stream); + rmm::device_uvector dests_v(nnz, stream); + rmm::device_uvector weights_v(nnz, stream); - int* srcs = srcs_v.data().get(); - int* dests = dests_v.data().get(); - T* weights = weights_v.data().get(); + int* srcs = srcs_v.data(); + int* dests = dests_v.data(); + T* weights = weights_v.data(); // FIXME: RAFT error handling mechanism should be used instead CUDA_TRY(cudaMemcpy(srcs, &cooRowInd[0], sizeof(int) * nnz, cudaMemcpyDefault)); CUDA_TRY(cudaMemcpy(dests, &cooColInd[0], sizeof(int) * nnz, cudaMemcpyDefault)); CUDA_TRY(cudaMemcpy(weights, &cooVal[0], sizeof(T) * nnz, cudaMemcpyDefault)); - cugraph::GraphCOOView G(srcs, dests, weights, m, nnz); + cugraph::legacy::GraphCOOView G(srcs, dests, weights, m, nnz); const int max_iter = 500; float* x_start = nullptr; @@ -163,8 +164,9 @@ class Tests_Force_Atlas2 : public ::testing::TestWithParam if (PERF) { hr_clock.start(); for (int i = 0; i < PERF_MULTIPLIER; ++i) { - cugraph::force_atlas2(G, - d_force_atlas2, + cugraph::force_atlas2(handle, + G, + pos.data(), max_iter, x_start, y_start, @@ -185,8 +187,9 @@ class Tests_Force_Atlas2 : public ::testing::TestWithParam force_atlas2_time.push_back(time_tmp); } else { cudaProfilerStart(); - cugraph::force_atlas2(G, - d_force_atlas2, + cugraph::force_atlas2(handle, + G, + pos.data(), max_iter, x_start, y_start, @@ -207,12 +210,13 @@ class Tests_Force_Atlas2 : public ::testing::TestWithParam // Copy pos to host std::vector h_pos(m * 2); - CUDA_TRY(cudaMemcpy(&h_pos[0], d_force_atlas2, sizeof(float) * m * 2, cudaMemcpyDeviceToHost)); + CUDA_TRY(cudaMemcpy(&h_pos[0], pos.data(), sizeof(float) * m * 2, cudaMemcpyDeviceToHost)); // Transpose the data std::vector> C_contiguous_embedding(m, std::vector(2)); for (int i = 0; i < m; i++) { - for (int j = 0; j < 2; j++) C_contiguous_embedding[i][j] = h_pos[j * m + i]; + for (int j = 0; j < 2; j++) + C_contiguous_embedding[i][j] = h_pos[j * m + i]; } // Test trustworthiness @@ -229,12 +233,12 @@ TEST_P(Tests_Force_Atlas2, CheckFP32_T) { run_current_test(GetParam()); } TEST_P(Tests_Force_Atlas2, CheckFP64_T) { run_current_test(GetParam()); } // --gtest_filter=*simple_test* -INSTANTIATE_TEST_CASE_P(simple_test, - Tests_Force_Atlas2, - ::testing::Values(Force_Atlas2_Usecase("test/datasets/karate.mtx", 0.73), - Force_Atlas2_Usecase("test/datasets/dolphins.mtx", 0.69), - Force_Atlas2_Usecase("test/datasets/polbooks.mtx", 0.76), - Force_Atlas2_Usecase("test/datasets/netscience.mtx", - 0.80))); +INSTANTIATE_TEST_SUITE_P(simple_test, + Tests_Force_Atlas2, + ::testing::Values(Force_Atlas2_Usecase("test/datasets/karate.mtx", 0.73), + Force_Atlas2_Usecase("test/datasets/dolphins.mtx", 0.69), + Force_Atlas2_Usecase("test/datasets/polbooks.mtx", 0.76), + Force_Atlas2_Usecase("test/datasets/netscience.mtx", + 0.80))); CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/layout/knn.h b/cpp/tests/layout/knn.h index 07d07528769..26666794896 100644 --- a/cpp/tests/layout/knn.h +++ b/cpp/tests/layout/knn.h @@ -1,5 +1,5 @@ /* - * 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. @@ -44,7 +44,8 @@ double sq_euclid_dist(const point& x, const point& y) double total = 0; auto i = x.attributes.begin(); auto j = y.attributes.begin(); - for (; i != x.attributes.end() && j != y.attributes.end(); ++i, ++j) total += pow(*i - *j, 2); + for (; i != x.attributes.end() && j != y.attributes.end(); ++i, ++j) + total += pow(*i - *j, 2); return total; } @@ -63,6 +64,7 @@ std::vector knn_classify(std::list& dataframe, const point& c, const auto count = 0; auto j = distances.begin(); ++j; - for (; j != distances.end() && count < k; ++j, ++count) res.push_back(j->index); + for (; j != distances.end() && count < k; ++j, ++count) + res.push_back(j->index); return res; } diff --git a/cpp/tests/layout/trust_worthiness.h b/cpp/tests/layout/trust_worthiness.h index 40c9782a76e..5a112ea3c6b 100644 --- a/cpp/tests/layout/trust_worthiness.h +++ b/cpp/tests/layout/trust_worthiness.h @@ -1,5 +1,5 @@ /* - * 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. @@ -25,7 +25,8 @@ double euclidian_dist(const std::vector& x, const std::vector& y) double total = 0; auto i = x.begin(); auto j = y.begin(); - for (; i != x.end() && j != y.end(); ++i, ++j) total += pow(*i, 2) - 2 * *i * *j + pow(*j, 2); + for (; i != x.end() && j != y.end(); ++i, ++j) + total += pow(*i, 2) - 2 * *i * *j + pow(*j, 2); return sqrt(total); } @@ -62,7 +63,8 @@ std::vector argsort(Iter begin, Iter end, Compare comp) return comp(*prev.second, *next.second); }); - for (auto i : pairList) ret.push_back(i.first); + for (auto i : pairList) + ret.push_back(i.first); return ret; } @@ -109,7 +111,8 @@ double compute_rank(const std::vector>& ind_X, ranks[j] = idx; } } - for (auto& val : ranks) val -= k; + for (auto& val : ranks) + val -= k; for (const auto& val : ranks) if (val > 0) rank += val; @@ -122,7 +125,9 @@ void print_matrix(const std::vector>& matrix) { for (size_t i = 0; i < matrix.size(); ++i) { std::cout << "[ "; - for (size_t j = 0; j < matrix[i].size(); ++j) { std::cout << matrix[i][j] << ' '; } + for (size_t j = 0; j < matrix[i].size(); ++j) { + std::cout << matrix[i][j] << ' '; + } std::cout << "]\n"; } } diff --git a/cpp/tests/linear_assignment/hungarian_test.cu b/cpp/tests/linear_assignment/hungarian_test.cu index 656957a85eb..f806a217a8f 100644 --- a/cpp/tests/linear_assignment/hungarian_test.cu +++ b/cpp/tests/linear_assignment/hungarian_test.cu @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2020-2021, NVIDIA CORPORATION. All rights reserved. * * NVIDIA CORPORATION and its licensors retain all intellectual property * and proprietary rights in and to this software, related documentation @@ -9,29 +9,31 @@ * */ -#include "cuda_profiler_api.h" -#include "gtest/gtest.h" - -#include -#include +#include -#include -#include +#include +#include #include -#include - #include -__global__ void setup_generator(curandState *state) +#include + +#include "cuda_profiler_api.h" +#include "gtest/gtest.h" + +#include +#include + +__global__ void setup_generator(curandState* state) { int id = threadIdx.x + blockIdx.x * blockDim.x; curand_init(43, id, 0, &state[id]); } template -__global__ void generate_random(curandState *state, int n, T *data, int32_t upper_bound) +__global__ void generate_random(curandState* state, int n, T* data, int32_t upper_bound) { int first = threadIdx.x + blockIdx.x * blockDim.x; int stride = blockDim.x * gridDim.x; @@ -64,29 +66,35 @@ TEST_F(HungarianTest, Bipartite4x4) int32_t workers[] = {0, 1, 2, 3}; - float min_cost = 18.0; - int32_t expected[] = {6, 7, 5, 4}; + float min_cost = 18.0; + std::vector expected({6, 7, 5, 4}); + std::vector assignment({0, 0, 0, 0}); int32_t length = sizeof(src_data) / sizeof(src_data[0]); int32_t length_workers = sizeof(workers) / sizeof(workers[0]); int32_t num_vertices = 1 + std::max(*std::max_element(src_data, src_data + length), *std::max_element(dst_data, dst_data + length)); - rmm::device_vector src_v(src_data, src_data + length); - rmm::device_vector dst_v(dst_data, dst_data + length); - rmm::device_vector cost_v(cost, cost + length); - rmm::device_vector workers_v(workers, workers + length_workers); - rmm::device_vector expected_v(expected, expected + length_workers); - rmm::device_vector assignment_v(length_workers); + rmm::device_uvector src_v(length, handle.get_stream_view()); + rmm::device_uvector dst_v(length, handle.get_stream_view()); + rmm::device_uvector cost_v(length, handle.get_stream_view()); + rmm::device_uvector workers_v(length_workers, handle.get_stream_view()); + rmm::device_uvector assignment_v(length_workers, handle.get_stream_view()); + + raft::update_device(src_v.begin(), src_data, length, handle.get_stream()); + raft::update_device(dst_v.begin(), dst_data, length, handle.get_stream()); + raft::update_device(cost_v.begin(), cost, length, handle.get_stream()); + raft::update_device(workers_v.begin(), workers, length_workers, handle.get_stream()); + + cugraph::legacy::GraphCOOView g( + src_v.data(), dst_v.data(), cost_v.data(), num_vertices, length); - cugraph::GraphCOOView g( - src_v.data().get(), dst_v.data().get(), cost_v.data().get(), num_vertices, length); + float r = cugraph::hungarian(handle, g, length_workers, workers_v.data(), assignment_v.data()); - float r = cugraph::hungarian( - handle, g, length_workers, workers_v.data().get(), assignment_v.data().get()); + raft::update_host(assignment.data(), assignment_v.begin(), length_workers, handle.get_stream()); EXPECT_EQ(min_cost, r); - EXPECT_EQ(expected_v, assignment_v); + EXPECT_EQ(assignment, expected); } TEST_F(HungarianTest, Bipartite5x5) @@ -100,29 +108,36 @@ TEST_F(HungarianTest, Bipartite5x5) int32_t workers[] = {0, 1, 2, 3, 4}; - float min_cost = 51.0; - int32_t expected[] = {5, 7, 8, 6, 9}; + float min_cost = 51.0; + std::vector expected({5, 7, 8, 6, 9}); + std::vector assignment({0, 0, 0, 0, 0}); int32_t length = sizeof(src_data) / sizeof(src_data[0]); int32_t length_workers = sizeof(workers) / sizeof(workers[0]); int32_t num_vertices = 1 + std::max(*std::max_element(src_data, src_data + length), *std::max_element(dst_data, dst_data + length)); - rmm::device_vector src_v(src_data, src_data + length); - rmm::device_vector dst_v(dst_data, dst_data + length); - rmm::device_vector cost_v(cost, cost + length); - rmm::device_vector workers_v(workers, workers + length_workers); - rmm::device_vector expected_v(expected, expected + length_workers); - rmm::device_vector assignment_v(length_workers); + rmm::device_uvector src_v(length, handle.get_stream_view()); + rmm::device_uvector dst_v(length, handle.get_stream_view()); + rmm::device_uvector cost_v(length, handle.get_stream_view()); + rmm::device_uvector workers_v(length_workers, handle.get_stream_view()); + rmm::device_uvector assignment_v(length_workers, handle.get_stream_view()); - cugraph::GraphCOOView g( - src_v.data().get(), dst_v.data().get(), cost_v.data().get(), num_vertices, length); + raft::update_device(src_v.begin(), src_data, length, handle.get_stream()); + raft::update_device(dst_v.begin(), dst_data, length, handle.get_stream()); + raft::update_device(cost_v.begin(), cost, length, handle.get_stream()); + raft::update_device(workers_v.begin(), workers, length_workers, handle.get_stream()); - float r = cugraph::hungarian( - handle, g, length_workers, workers_v.data().get(), assignment_v.data().get()); + cugraph::legacy::GraphCOOView g( + src_v.data(), dst_v.data(), cost_v.data(), num_vertices, length); + + float r = cugraph::hungarian(handle, g, length_workers, workers_v.data(), assignment_v.data()); + + raft::update_host( + assignment.data(), assignment_v.begin(), assignment_v.size(), handle.get_stream()); EXPECT_EQ(min_cost, r); - EXPECT_EQ(expected_v, assignment_v); + EXPECT_EQ(assignment, expected); } TEST_F(HungarianTest, Bipartite4x4_multiple_answers) @@ -135,40 +150,44 @@ TEST_F(HungarianTest, Bipartite4x4_multiple_answers) int32_t workers[] = {0, 1, 2, 3}; - float min_cost = 13.0; - int32_t expected1[] = {7, 6, 5, 4}; - int32_t expected2[] = {6, 7, 5, 4}; - int32_t expected3[] = {7, 6, 4, 5}; - int32_t expected4[] = {6, 7, 4, 5}; + float min_cost = 13.0; + + std::vector expected1({7, 6, 5, 4}); + std::vector expected2({6, 7, 5, 4}); + std::vector expected3({7, 6, 4, 5}); + std::vector expected4({6, 7, 4, 5}); + std::vector assignment({0, 0, 0, 0}); int32_t length = sizeof(src_data) / sizeof(src_data[0]); int32_t length_workers = sizeof(workers) / sizeof(workers[0]); int32_t num_vertices = 1 + std::max(*std::max_element(src_data, src_data + length), *std::max_element(dst_data, dst_data + length)); - rmm::device_vector src_v(src_data, src_data + length); - rmm::device_vector dst_v(dst_data, dst_data + length); - rmm::device_vector cost_v(cost, cost + length); - rmm::device_vector workers_v(workers, workers + length_workers); - rmm::device_vector assignment_v(length_workers); + rmm::device_uvector src_v(length, handle.get_stream_view()); + rmm::device_uvector dst_v(length, handle.get_stream_view()); + rmm::device_uvector cost_v(length, handle.get_stream_view()); + rmm::device_uvector workers_v(length_workers, handle.get_stream_view()); + rmm::device_uvector assignment_v(length_workers, handle.get_stream_view()); - rmm::device_vector expected1_v(expected1, expected1 + length_workers); - rmm::device_vector expected2_v(expected2, expected2 + length_workers); - rmm::device_vector expected3_v(expected3, expected3 + length_workers); - rmm::device_vector expected4_v(expected4, expected4 + length_workers); + raft::update_device(src_v.begin(), src_data, length, handle.get_stream()); + raft::update_device(dst_v.begin(), dst_data, length, handle.get_stream()); + raft::update_device(cost_v.begin(), cost, length, handle.get_stream()); + raft::update_device(workers_v.begin(), workers, length_workers, handle.get_stream()); - cugraph::GraphCOOView g( - src_v.data().get(), dst_v.data().get(), cost_v.data().get(), num_vertices, length); + cugraph::legacy::GraphCOOView g( + src_v.data(), dst_v.data(), cost_v.data(), num_vertices, length); - float r = cugraph::hungarian( - handle, g, length_workers, workers_v.data().get(), assignment_v.data().get()); + float r = cugraph::hungarian(handle, g, length_workers, workers_v.data(), assignment_v.data()); EXPECT_EQ(min_cost, r); - EXPECT_TRUE(thrust::equal(assignment_v.begin(), assignment_v.end(), expected1_v.begin()) || - thrust::equal(assignment_v.begin(), assignment_v.end(), expected2_v.begin()) || - thrust::equal(assignment_v.begin(), assignment_v.end(), expected3_v.begin()) || - thrust::equal(assignment_v.begin(), assignment_v.end(), expected4_v.begin())); + raft::update_host( + assignment.data(), assignment_v.data(), assignment_v.size(), handle.get_stream()); + + EXPECT_TRUE(std::equal(assignment.begin(), assignment.end(), expected1.begin()) || + std::equal(assignment.begin(), assignment.end(), expected2.begin()) || + std::equal(assignment.begin(), assignment.end(), expected3.begin()) || + std::equal(assignment.begin(), assignment.end(), expected4.begin())); } TEST_F(HungarianTest, May29InfLoop) @@ -181,13 +200,82 @@ TEST_F(HungarianTest, May29InfLoop) float min_cost = 2; - rmm::device_vector cost_v(cost, cost + num_rows * num_cols); - rmm::device_vector assignment_v(num_rows); + std::vector expected({3, 2, 1, 0}); + std::vector assignment({0, 0, 0, 0}); + + rmm::device_uvector cost_v(num_rows * num_cols, handle.get_stream_view()); + rmm::device_uvector assignment_v(num_rows, handle.get_stream_view()); + + raft::update_device(cost_v.begin(), cost, num_rows * num_cols, handle.get_stream()); + + float r = + cugraph::dense::hungarian(handle, cost_v.data(), num_rows, num_cols, assignment_v.data()); + + raft::update_host( + assignment.data(), assignment_v.data(), assignment_v.size(), handle.get_stream()); + + EXPECT_EQ(min_cost, r); + EXPECT_EQ(assignment, expected); +} + +TEST_F(HungarianTest, Dense4x6) +{ + raft::handle_t handle{}; + + int32_t num_rows = 4; + int32_t num_cols = 6; + float cost[] = {0, 16, 1, 0, 90, 100, 33, 45, 0, 4, 90, 100, + 22, 0, 1000, 2000, 90, 100, 2, 0, 3000, 4000, 90, 100}; + + float min_cost = 2; + + std::vector expected({3, 2, 1, 0}); + std::vector assignment({0, 0, 0, 0}); + + rmm::device_uvector cost_v(num_rows * num_cols, handle.get_stream_view()); + rmm::device_uvector assignment_v(num_rows, handle.get_stream_view()); + + raft::update_device(cost_v.begin(), cost, num_rows * num_cols, handle.get_stream()); + + float r = + cugraph::dense::hungarian(handle, cost_v.data(), num_rows, num_cols, assignment_v.data()); - float r = cugraph::dense::hungarian( - handle, cost_v.data().get(), num_rows, num_cols, assignment_v.data().get()); + raft::update_host( + assignment.data(), assignment_v.data(), assignment_v.size(), handle.get_stream()); EXPECT_EQ(min_cost, r); + EXPECT_EQ(assignment, expected); +} + +TEST_F(HungarianTest, Dense6x4) +{ + raft::handle_t handle{}; + + int32_t num_rows = 6; + int32_t num_cols = 4; + float cost[] = {0, 16, 1, 0, 33, 45, 0, 4, 90, 100, 110, 120, + 22, 0, 1000, 2000, 90, 100, 110, 120, 2, 0, 3000, 4000}; + + float min_cost = 2; + + std::vector expected1({3, 2, 4, 1, 5, 0}); + std::vector expected2({3, 2, 5, 1, 4, 0}); + std::vector assignment({0, 0, 0, 0, 0, 0}); + + rmm::device_uvector cost_v(num_rows * num_cols, handle.get_stream_view()); + rmm::device_uvector assignment_v(num_rows, handle.get_stream_view()); + + raft::update_device(cost_v.begin(), cost, num_rows * num_cols, handle.get_stream()); + + float r = + cugraph::dense::hungarian(handle, cost_v.data(), num_rows, num_cols, assignment_v.data()); + + raft::update_host( + assignment.data(), assignment_v.data(), assignment_v.size(), handle.get_stream()); + + EXPECT_EQ(min_cost, r); + EXPECT_TRUE(std::equal(assignment.begin(), assignment.end(), expected1.begin()) || + std::equal(assignment.begin(), assignment.end(), expected2.begin())); } TEST_F(HungarianTest, PythonTestFailure) @@ -229,13 +317,22 @@ TEST_F(HungarianTest, PythonTestFailure) float min_cost = 16; - rmm::device_vector cost_v(cost, cost + num_rows * num_cols); - rmm::device_vector assignment_v(num_rows); + std::vector expected({0, 2, 1, 4, 3}); + std::vector assignment({0, 0, 0, 0, 0}); + + rmm::device_uvector cost_v(num_rows * num_cols, handle.get_stream_view()); + rmm::device_uvector assignment_v(num_rows, handle.get_stream_view()); + + raft::update_device(cost_v.begin(), cost, num_rows * num_cols, handle.get_stream()); + + float r = + cugraph::dense::hungarian(handle, cost_v.data(), num_rows, num_cols, assignment_v.data()); - float r = cugraph::dense::hungarian( - handle, cost_v.data().get(), num_rows, num_cols, assignment_v.data().get()); + raft::update_host( + assignment.data(), assignment_v.data(), assignment_v.size(), handle.get_stream()); EXPECT_EQ(min_cost, r); + EXPECT_EQ(assignment, expected); } // FIXME: Need to have tests with nxm (e.g. 4x5 and 5x4) to test those conditions @@ -249,19 +346,19 @@ void random_test(int32_t num_rows, int32_t num_cols, int32_t upper_bound, int re HighResTimer hr_timer; - rmm::device_vector data_v(num_rows * num_cols); - rmm::device_vector state_vals_v(num_threads); - rmm::device_vector assignment_v(num_rows); + rmm::device_uvector data_v(num_rows * num_cols, handle.get_stream_view()); + rmm::device_uvector state_vals_v(num_threads, handle.get_stream_view()); + rmm::device_uvector assignment_v(num_rows, handle.get_stream_view()); std::vector validate(num_cols); hr_timer.start("initialization"); cudaStream_t stream{0}; - int32_t *d_data = data_v.data().get(); + int32_t *d_data = data_v.data(); //int64_t seed{85}; int64_t seed{time(nullptr)}; - + thrust::for_each(rmm::exec_policy(stream)->on(stream), thrust::make_counting_iterator(0), thrust::make_counting_iterator(num_rows * num_cols), @@ -280,15 +377,15 @@ void random_test(int32_t num_rows, int32_t num_cols, int32_t upper_bound, int re for (int i = 0 ; i < repetitions ; ++i) { hr_timer.start("hungarian"); - r = cugraph::hungarian_dense(cost_v.data().get(), num_rows, num_cols, assignment_v.data().get()); + r = cugraph::hungarian_dense(cost_v.data(), num_rows, num_cols, assignment_v.data()); hr_timer.stop(); } std::cout << "cost = " << r << std::endl; hr_timer.display(std::cout); - - for (int i = 0 ; i < num_cols ; ++i) + + for (int i = 0 ; i < num_cols ; ++i) validate[i] = 0; int32_t assignment_out_of_range{0}; @@ -303,8 +400,8 @@ void random_test(int32_t num_rows, int32_t num_cols, int32_t upper_bound, int re EXPECT_EQ(assignment_out_of_range, 0); - int32_t assignment_missed = 0; - + int32_t assignment_missed = 0; + for (int32_t i = 0 ; i < num_cols ; ++i) { if (validate[i] != 1) { ++assignment_missed; diff --git a/cpp/tests/pagerank/mg_pagerank_test.cpp b/cpp/tests/pagerank/mg_pagerank_test.cpp new file mode 100644 index 00000000000..fca889c3299 --- /dev/null +++ b/cpp/tests/pagerank/mg_pagerank_test.cpp @@ -0,0 +1,356 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include + +// do the perf measurements +// enabled by command line parameter s'--perf' +// +static int PERF = 0; + +struct PageRank_Usecase { + double personalization_ratio{0.0}; + bool test_weighted{false}; + bool check_correctness{true}; +}; + +template +class Tests_MGPageRank + : public ::testing::TestWithParam> { + public: + Tests_MGPageRank() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + + virtual void SetUp() {} + virtual void TearDown() {} + + // Compare the results of running PageRank on multiple GPUs to that of a single-GPU run + template + void run_current_test(PageRank_Usecase const& pagerank_usecase, + input_usecase_t const& input_usecase) + { + // 1. initialize handle + raft::handle_t handle{}; + HighResClock hr_clock{}; + + raft::comms::initialize_mpi_comms(&handle, MPI_COMM_WORLD); + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + auto const comm_rank = comm.get_rank(); + + auto row_comm_size = static_cast(sqrt(static_cast(comm_size))); + while (comm_size % row_comm_size != 0) { + --row_comm_size; + } + cugraph::partition_2d::subcomm_factory_t + subcomm_factory(handle, row_comm_size); + + // 2. create MG graph + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + hr_clock.start(); + } + + auto [mg_graph, d_mg_renumber_map_labels] = + input_usecase.template construct_graph( + handle, pagerank_usecase.test_weighted, true); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "MG construct_graph took " << elapsed_time * 1e-6 << " s.\n"; + } + + auto mg_graph_view = mg_graph.view(); + + // 3. generate personalization vertex/value pairs + + std::optional> h_mg_personalization_vertices{std::nullopt}; + std::optional> h_mg_personalization_values{std::nullopt}; + if (pagerank_usecase.personalization_ratio > 0.0) { + std::default_random_engine generator{ + static_cast(comm.get_rank()) /* seed */}; + std::uniform_real_distribution distribution{0.0, 1.0}; + h_mg_personalization_vertices = + std::vector(mg_graph_view.get_number_of_local_vertices()); + std::iota((*h_mg_personalization_vertices).begin(), + (*h_mg_personalization_vertices).end(), + mg_graph_view.get_local_vertex_first()); + (*h_mg_personalization_vertices) + .erase(std::remove_if((*h_mg_personalization_vertices).begin(), + (*h_mg_personalization_vertices).end(), + [&generator, &distribution, pagerank_usecase](auto v) { + return distribution(generator) >= + pagerank_usecase.personalization_ratio; + }), + (*h_mg_personalization_vertices).end()); + h_mg_personalization_values = std::vector((*h_mg_personalization_vertices).size()); + std::for_each((*h_mg_personalization_values).begin(), + (*h_mg_personalization_values).end(), + [&distribution, &generator](auto& val) { val = distribution(generator); }); + } + + auto d_mg_personalization_vertices = + h_mg_personalization_vertices + ? std::make_optional>((*h_mg_personalization_vertices).size(), + handle.get_stream()) + : std::nullopt; + auto d_mg_personalization_values = + h_mg_personalization_values ? std::make_optional>( + (*d_mg_personalization_vertices).size(), handle.get_stream()) + : std::nullopt; + if (d_mg_personalization_vertices) { + raft::update_device((*d_mg_personalization_vertices).data(), + (*h_mg_personalization_vertices).data(), + (*h_mg_personalization_vertices).size(), + handle.get_stream()); + raft::update_device((*d_mg_personalization_values).data(), + (*h_mg_personalization_values).data(), + (*h_mg_personalization_values).size(), + handle.get_stream()); + } + + // 4. run MG PageRank + + result_t constexpr alpha{0.85}; + result_t constexpr epsilon{1e-6}; + + rmm::device_uvector d_mg_pageranks(mg_graph_view.get_number_of_local_vertices(), + handle.get_stream()); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + hr_clock.start(); + } + + cugraph::experimental::pagerank( + handle, + mg_graph_view, + std::nullopt, + d_mg_personalization_vertices + ? std::optional{(*d_mg_personalization_vertices).data()} + : std::nullopt, + d_mg_personalization_values + ? std::optional{(*d_mg_personalization_values).data()} + : std::nullopt, + d_mg_personalization_vertices + ? std::optional{static_cast((*d_mg_personalization_vertices).size())} + : std::nullopt, + d_mg_pageranks.data(), + alpha, + epsilon, + std::numeric_limits::max(), + false); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "MG PageRank took " << elapsed_time * 1e-6 << " s.\n"; + } + + // 5. copmare SG & MG results + + if (pagerank_usecase.check_correctness) { + // 5-1. aggregate MG results + + auto d_mg_aggregate_renumber_map_labels = cugraph::test::device_gatherv( + handle, (*d_mg_renumber_map_labels).data(), (*d_mg_renumber_map_labels).size()); + auto d_mg_aggregate_personalization_vertices = + d_mg_personalization_vertices + ? std::optional>{cugraph::test::device_gatherv( + handle, + (*d_mg_personalization_vertices).data(), + (*d_mg_personalization_vertices).size())} + : std::nullopt; + auto d_mg_aggregate_personalization_values = + d_mg_personalization_values + ? std::optional>{cugraph::test::device_gatherv( + handle, (*d_mg_personalization_values).data(), (*d_mg_personalization_values).size())} + : std::nullopt; + auto d_mg_aggregate_pageranks = + cugraph::test::device_gatherv(handle, d_mg_pageranks.data(), d_mg_pageranks.size()); + + if (handle.get_comms().get_rank() == int{0}) { + // 5-2. unrenumbr MG results + + if (d_mg_aggregate_personalization_vertices) { + cugraph::experimental::unrenumber_int_vertices( + handle, + (*d_mg_aggregate_personalization_vertices).data(), + (*d_mg_aggregate_personalization_vertices).size(), + d_mg_aggregate_renumber_map_labels.data(), + vertex_t{0}, + mg_graph_view.get_number_of_vertices(), + std::vector{mg_graph_view.get_number_of_vertices()}); + std::tie(d_mg_aggregate_personalization_vertices, d_mg_aggregate_personalization_values) = + cugraph::test::sort_by_key(handle, + (*d_mg_aggregate_personalization_vertices).data(), + (*d_mg_aggregate_personalization_values).data(), + (*d_mg_aggregate_personalization_vertices).size()); + } + std::tie(std::ignore, d_mg_aggregate_pageranks) = + cugraph::test::sort_by_key(handle, + d_mg_aggregate_renumber_map_labels.data(), + d_mg_aggregate_pageranks.data(), + d_mg_aggregate_renumber_map_labels.size()); + + // 5-3. create SG graph + + cugraph::experimental::graph_t sg_graph(handle); + std::tie(sg_graph, std::ignore) = + input_usecase.template construct_graph( + handle, pagerank_usecase.test_weighted, false); + + auto sg_graph_view = sg_graph.view(); + + ASSERT_EQ(mg_graph_view.get_number_of_vertices(), sg_graph_view.get_number_of_vertices()); + + // 5-4. run SG PageRank + + rmm::device_uvector d_sg_pageranks(sg_graph_view.get_number_of_vertices(), + handle.get_stream()); + + cugraph::experimental::pagerank( + handle, + sg_graph_view, + std::nullopt, + d_mg_aggregate_personalization_vertices + ? std::optional{(*d_mg_aggregate_personalization_vertices).data()} + : std::nullopt, + d_mg_aggregate_personalization_values + ? std::optional{(*d_mg_aggregate_personalization_values).data()} + : std::nullopt, + d_mg_aggregate_personalization_vertices + ? std::optional{static_cast( + (*d_mg_aggregate_personalization_vertices).size())} + : std::nullopt, + d_sg_pageranks.data(), + alpha, + epsilon, + std::numeric_limits::max(), // max_iterations + false); + + // 5-4. compare + + std::vector h_mg_aggregate_pageranks(mg_graph_view.get_number_of_vertices()); + raft::update_host(h_mg_aggregate_pageranks.data(), + d_mg_aggregate_pageranks.data(), + d_mg_aggregate_pageranks.size(), + handle.get_stream()); + + std::vector h_sg_pageranks(sg_graph_view.get_number_of_vertices()); + raft::update_host( + h_sg_pageranks.data(), d_sg_pageranks.data(), d_sg_pageranks.size(), handle.get_stream()); + + handle.get_stream_view().synchronize(); + + auto threshold_ratio = 1e-3; + auto threshold_magnitude = + (1.0 / static_cast(mg_graph_view.get_number_of_vertices())) * + threshold_ratio; // skip comparison for low PageRank verties (lowly ranked vertices) + auto nearly_equal = [threshold_ratio, threshold_magnitude](auto lhs, auto rhs) { + return std::abs(lhs - rhs) < + std::max(std::max(lhs, rhs) * threshold_ratio, threshold_magnitude); + }; + + ASSERT_TRUE(std::equal(h_mg_aggregate_pageranks.begin(), + h_mg_aggregate_pageranks.end(), + h_sg_pageranks.begin(), + nearly_equal)); + } + } + } +}; + +using Tests_MGPageRank_File = Tests_MGPageRank; +using Tests_MGPageRank_Rmat = Tests_MGPageRank; + +TEST_P(Tests_MGPageRank_File, CheckInt32Int32FloatFloat) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +TEST_P(Tests_MGPageRank_Rmat, CheckInt32Int32FloatFloat) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +INSTANTIATE_TEST_SUITE_P( + file_tests, + Tests_MGPageRank_File, + ::testing::Combine( + // enable correctness checks + ::testing::Values(PageRank_Usecase{0.0, false}, + PageRank_Usecase{0.5, false}, + PageRank_Usecase{0.0, true}, + PageRank_Usecase{0.5, true}), + ::testing::Values(cugraph::test::File_Usecase("test/datasets/karate.mtx"), + cugraph::test::File_Usecase("test/datasets/web-Google.mtx"), + cugraph::test::File_Usecase("test/datasets/ljournal-2008.mtx"), + cugraph::test::File_Usecase("test/datasets/webbase-1M.mtx")))); + +INSTANTIATE_TEST_SUITE_P( + rmat_small_tests, + Tests_MGPageRank_Rmat, + ::testing::Combine(::testing::Values(PageRank_Usecase{0.0, false}, + PageRank_Usecase{0.5, false}, + PageRank_Usecase{0.0, true}, + PageRank_Usecase{0.5, true}), + ::testing::Values(cugraph::test::Rmat_Usecase( + 10, 16, 0.57, 0.19, 0.19, 0, false, false, 0, true)))); + +INSTANTIATE_TEST_SUITE_P( + rmat_large_tests, + Tests_MGPageRank_Rmat, + ::testing::Combine(::testing::Values(PageRank_Usecase{0.0, false, false}, + PageRank_Usecase{0.5, false, false}, + PageRank_Usecase{0.0, true, false}, + PageRank_Usecase{0.5, true, false}), + ::testing::Values(cugraph::test::Rmat_Usecase( + 20, 32, 0.57, 0.19, 0.19, 0, false, false, 0, true)))); + +CUGRAPH_MG_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/pagerank/pagerank_test.cpp b/cpp/tests/pagerank/pagerank_test.cpp deleted file mode 100644 index 48705f7f324..00000000000 --- a/cpp/tests/pagerank/pagerank_test.cpp +++ /dev/null @@ -1,206 +0,0 @@ -/* - * Copyright (c) 2018-2020, NVIDIA CORPORATION. All rights reserved. - * - * NVIDIA CORPORATION and its licensors retain all intellectual property - * and proprietary rights in and to this software, related documentation - * and any modifications thereto. Any use, reproduction, disclosure or - * distribution of this software and related documentation without an express - * license agreement from NVIDIA CORPORATION is strictly prohibited. - * - */ - -// Pagerank solver tests -// Author: Alex Fender afender@nvidia.com - -#include -#include -#include - -#include -#include - -#include -#include - -#include - -#include - -#include - -// do the perf measurements -// enabled by command line parameter s'--perf' -static int PERF = 0; - -// iterations for perf tests -// enabled by command line parameter '--perf-iters" -static int PERF_MULTIPLIER = 5; - -typedef struct Pagerank_Usecase_t { - std::string matrix_file; - std::string result_file; - Pagerank_Usecase_t(const std::string& a, const std::string& b) - { - // assume relative paths are relative to RAPIDS_DATASET_ROOT_DIR - const std::string& rapidsDatasetRootDir = cugraph::test::get_rapids_dataset_root_dir(); - if ((a != "") && (a[0] != '/')) { - matrix_file = rapidsDatasetRootDir + "/" + a; - } else { - matrix_file = a; - } - if ((b != "") && (b[0] != '/')) { - result_file = rapidsDatasetRootDir + "/" + b; - } else { - result_file = b; - } - } - Pagerank_Usecase_t& operator=(const Pagerank_Usecase_t& rhs) - { - matrix_file = rhs.matrix_file; - result_file = rhs.result_file; - return *this; - } -} Pagerank_Usecase; - -class Tests_Pagerank : public ::testing::TestWithParam { - public: - Tests_Pagerank() {} - static void SetupTestCase() {} - static void TearDownTestCase() - { - if (PERF) { - for (unsigned int i = 0; i < pagerank_time.size(); ++i) { - std::cout << pagerank_time[i] / PERF_MULTIPLIER << std::endl; - } - } - } - virtual void SetUp() {} - virtual void TearDown() {} - - static std::vector pagerank_time; - - template - void run_current_test(const Pagerank_Usecase& param) - { - const ::testing::TestInfo* const test_info = - ::testing::UnitTest::GetInstance()->current_test_info(); - std::stringstream ss; - std::string test_id = std::string(test_info->test_case_name()) + std::string(".") + - std::string(test_info->name()) + std::string("_") + - cugraph::test::getFileName(param.matrix_file) + std::string("_") + - ss.str().c_str(); - - int m, k, nnz; - MM_typecode mc; - - float tol = 1E-5f; - - // Default parameters - /* - float alpha = 0.85; - int max_iter = 500; - bool has_guess = false; - */ - - HighResClock hr_clock; - double time_tmp; - - FILE* fpin = fopen(param.matrix_file.c_str(), "r"); - ASSERT_NE(fpin, nullptr) << "fopen (" << param.matrix_file << ") failure."; - - ASSERT_EQ(cugraph::test::mm_properties(fpin, 1, &mc, &m, &k, &nnz), 0) - << "could not read Matrix Market file properties" - << "\n"; - ASSERT_TRUE(mm_is_matrix(mc)); - ASSERT_TRUE(mm_is_coordinate(mc)); - ASSERT_FALSE(mm_is_complex(mc)); - ASSERT_FALSE(mm_is_skew(mc)); - - // Allocate memory on host - std::vector cooRowInd(nnz), cooColInd(nnz); - std::vector cooVal(nnz), pagerank(m); - - // device alloc - rmm::device_uvector pagerank_vector(static_cast(m), nullptr); - T* d_pagerank = pagerank_vector.data(); - - // Read - ASSERT_EQ((cugraph::test::mm_to_coo( - fpin, 1, nnz, &cooRowInd[0], &cooColInd[0], &cooVal[0], NULL)), - 0) - << "could not read matrix data" - << "\n"; - ASSERT_EQ(fclose(fpin), 0); - - // Pagerank runs on CSC, so feed COOtoCSR the row/col backwards. - raft::handle_t handle; - cugraph::GraphCOOView G_coo(&cooColInd[0], &cooRowInd[0], &cooVal[0], m, nnz); - auto G_unique = cugraph::coo_to_csr(G_coo); - cugraph::GraphCSCView G(G_unique->view().offsets, - G_unique->view().indices, - G_unique->view().edge_data, - G_unique->view().number_of_vertices, - G_unique->view().number_of_edges); - - cudaDeviceSynchronize(); - if (PERF) { - hr_clock.start(); - for (int i = 0; i < PERF_MULTIPLIER; ++i) { - cugraph::pagerank(handle, G, d_pagerank); - cudaDeviceSynchronize(); - } - hr_clock.stop(&time_tmp); - pagerank_time.push_back(time_tmp); - } else { - cudaProfilerStart(); - cugraph::pagerank(handle, G, d_pagerank); - cudaProfilerStop(); - cudaDeviceSynchronize(); - } - - // Check vs golden data - if (param.result_file.length() > 0) { - std::vector calculated_res(m); - - CUDA_TRY(cudaMemcpy(&calculated_res[0], d_pagerank, sizeof(T) * m, cudaMemcpyDeviceToHost)); - std::sort(calculated_res.begin(), calculated_res.end()); - fpin = fopen(param.result_file.c_str(), "rb"); - ASSERT_TRUE(fpin != NULL) << " Cannot read file with reference data: " << param.result_file - << std::endl; - std::vector expected_res(m); - ASSERT_EQ(cugraph::test::read_binary_vector(fpin, m, expected_res), 0); - fclose(fpin); - T err; - int n_err = 0; - for (int i = 0; i < m; i++) { - err = fabs(expected_res[i] - calculated_res[i]); - if (err > tol * 1.1) { - n_err++; // count the number of mismatches - } - } - if (n_err) { - EXPECT_LE(n_err, 0.001 * m); // we tolerate 0.1% of values with a litte difference - } - } - } -}; - -std::vector Tests_Pagerank::pagerank_time; - -TEST_P(Tests_Pagerank, CheckFP32_T) { run_current_test(GetParam()); } - -TEST_P(Tests_Pagerank, CheckFP64_T) { run_current_test(GetParam()); } - -// --gtest_filter=*simple_test* -INSTANTIATE_TEST_CASE_P( - simple_test, - Tests_Pagerank, - ::testing::Values(Pagerank_Usecase("test/datasets/karate.mtx", ""), - Pagerank_Usecase("test/datasets/web-Google.mtx", - "test/ref/pagerank/web-Google.pagerank_val_0.85.bin"), - Pagerank_Usecase("test/datasets/ljournal-2008.mtx", - "test/ref/pagerank/ljournal-2008.pagerank_val_0.85.bin"), - Pagerank_Usecase("test/datasets/webbase-1M.mtx", - "test/ref/pagerank/webbase-1M.pagerank_val_0.85.bin"))); - -CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/prims/mg_count_if_v.cu b/cpp/tests/prims/mg_count_if_v.cu new file mode 100644 index 00000000000..c956067cae8 --- /dev/null +++ b/cpp/tests/prims/mg_count_if_v.cu @@ -0,0 +1,207 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include + +// do the perf measurements +// enabled by command line parameter s'--perf' +// +static int PERF = 0; + +template +struct test_predicate { + int mod{}; + test_predicate(int mod_count) : mod(mod_count) {} + __device__ bool operator()(const vertex_t& val) + { + cuco::detail::MurmurHash3_32 hash_func{}; + return (0 == (hash_func(val) % mod)); + } +}; + +struct Prims_Usecase { + bool check_correctness{true}; +}; + +template +class Tests_MG_CountIfV + : public ::testing::TestWithParam> { + public: + Tests_MG_CountIfV() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + + virtual void SetUp() {} + virtual void TearDown() {} + + // Compare the results of count_if_v primitive and thrust count_if on a single GPU + template + void run_current_test(Prims_Usecase const& prims_usecase, input_usecase_t const& input_usecase) + { + // 1. initialize handle + + raft::handle_t handle{}; + HighResClock hr_clock{}; + + raft::comms::initialize_mpi_comms(&handle, MPI_COMM_WORLD); + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + auto const comm_rank = comm.get_rank(); + + auto row_comm_size = static_cast(sqrt(static_cast(comm_size))); + while (comm_size % row_comm_size != 0) { + --row_comm_size; + } + cugraph::partition_2d::subcomm_factory_t + subcomm_factory(handle, row_comm_size); + + // 2. create MG graph + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + hr_clock.start(); + } + auto [mg_graph, d_mg_renumber_map_labels] = + input_usecase.template construct_graph( + handle, true, true); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "MG construct_graph took " << elapsed_time * 1e-6 << " s.\n"; + } + + auto mg_graph_view = mg_graph.view(); + + const int hash_bin_count = 5; + + // 3. run MG count if + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + hr_clock.start(); + } + + vertex_t const* data = (*d_mg_renumber_map_labels).data(); + auto vertex_count = + count_if_v(handle, mg_graph_view, data, test_predicate(hash_bin_count)); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "MG count if took " << elapsed_time * 1e-6 << " s.\n"; + } + + // 4. compare SG & MG results + + if (prims_usecase.check_correctness) { + cugraph::experimental::graph_t sg_graph( + handle); + std::tie(sg_graph, std::ignore) = + input_usecase.template construct_graph( + handle, true, false); + auto sg_graph_view = sg_graph.view(); + auto expected_vertex_count = + thrust::count_if(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + thrust::make_counting_iterator(sg_graph_view.get_local_vertex_first()), + thrust::make_counting_iterator(sg_graph_view.get_local_vertex_last()), + test_predicate(hash_bin_count)); + ASSERT_TRUE(expected_vertex_count == vertex_count); + } + } +}; + +using Tests_MG_CountIfV_File = Tests_MG_CountIfV; +using Tests_MG_CountIfV_Rmat = Tests_MG_CountIfV; + +TEST_P(Tests_MG_CountIfV_File, CheckInt32Int32FloatTransposeFalse) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +TEST_P(Tests_MG_CountIfV_Rmat, CheckInt32Int32FloatTransposeFalse) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +TEST_P(Tests_MG_CountIfV_File, CheckInt32Int32FloatTransposeTrue) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +TEST_P(Tests_MG_CountIfV_Rmat, CheckInt32Int32FloatTransposeTrue) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +INSTANTIATE_TEST_SUITE_P( + file_test, + Tests_MG_CountIfV_File, + ::testing::Combine( + ::testing::Values(Prims_Usecase{true}), + ::testing::Values(cugraph::test::File_Usecase("test/datasets/karate.mtx"), + cugraph::test::File_Usecase("test/datasets/web-Google.mtx"), + cugraph::test::File_Usecase("test/datasets/ljournal-2008.mtx"), + cugraph::test::File_Usecase("test/datasets/webbase-1M.mtx")))); + +INSTANTIATE_TEST_SUITE_P( + rmat_small_test, + Tests_MG_CountIfV_Rmat, + ::testing::Combine(::testing::Values(Prims_Usecase{true}), + ::testing::Values(cugraph::test::Rmat_Usecase( + 10, 16, 0.57, 0.19, 0.19, 0, false, false, 0, true)))); + +INSTANTIATE_TEST_SUITE_P( + rmat_large_test, + Tests_MG_CountIfV_Rmat, + ::testing::Combine(::testing::Values(Prims_Usecase{false}), + ::testing::Values(cugraph::test::Rmat_Usecase( + 20, 32, 0.57, 0.19, 0.19, 0, false, false, 0, true)))); + +CUGRAPH_MG_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/prims/mg_reduce_v.cu b/cpp/tests/prims/mg_reduce_v.cu new file mode 100644 index 00000000000..539e4e35ded --- /dev/null +++ b/cpp/tests/prims/mg_reduce_v.cu @@ -0,0 +1,365 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include + +// do the perf measurements +// enabled by command line parameter s'--perf' +// +static int PERF = 0; + +template +struct property_transform : public thrust::unary_function> { + int mod{}; + property_transform(int mod_count) : mod(mod_count) {} + __device__ auto operator()(const vertex_t& val) + { + cuco::detail::MurmurHash3_32 hash_func{}; + auto value = hash_func(val) % mod; + return thrust::make_tuple(static_cast(value)...); + } +}; +template typename Tuple, typename... T> +struct property_transform> : public property_transform { +}; + +template +auto make_iterator_tuple(Tuple& data, std::index_sequence) +{ + return thrust::make_tuple((std::get(data).begin())...); +} + +template +auto get_zip_iterator(std::tuple& data) +{ + return thrust::make_zip_iterator(make_iterator_tuple( + data, std::make_index_sequence>::value>())); +} + +template +auto get_property_iterator(std::tuple& data) +{ + return (std::get<0>(data)).begin(); +} + +template +auto get_property_iterator(std::tuple& data) +{ + return get_zip_iterator(data); +} + +template +struct generate_impl { + static thrust::tuple initial_value(int init) + { + return thrust::make_tuple(static_cast(init)...); + } + template + static std::tuple...> property(rmm::device_uvector& labels, + int hash_bin_count, + raft::handle_t const& handle) + { + auto data = std::make_tuple(rmm::device_uvector(labels.size(), handle.get_stream())...); + auto zip = get_zip_iterator(data); + thrust::transform(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + labels.begin(), + labels.end(), + zip, + property_transform(hash_bin_count)); + return data; + } + template + static std::tuple...> property(thrust::counting_iterator begin, + thrust::counting_iterator end, + int hash_bin_count, + raft::handle_t const& handle) + { + auto length = thrust::distance(begin, end); + auto data = std::make_tuple(rmm::device_uvector(length, handle.get_stream())...); + auto zip = get_zip_iterator(data); + thrust::transform(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + begin, + end, + zip, + property_transform(hash_bin_count)); + return data; + } +}; + +template +struct result_compare { + constexpr auto operator()(const T& t1, const T& t2) { return (t1 == t2); } +}; + +template +struct result_compare> { + static constexpr double threshold_ratio{1e-3}; + + private: + template + bool equal(T t1, T t2) + { + if constexpr (std::is_floating_point_v) { + return std::abs(t1 - t2) < (std::max(t1, t2) * threshold_ratio); + } + return t1 == t2; + } + template + constexpr auto equality_impl(T& t1, T& t2, std::index_sequence) + { + return (... && (equal(thrust::get(t1), thrust::get(t2)))); + } + + public: + using Type = thrust::tuple; + constexpr auto operator()(const Type& t1, const Type& t2) + { + return equality_impl(t1, t2, std::make_index_sequence::value>()); + } +}; + +template +struct generate : public generate_impl { + static T initial_value(int init) { return static_cast(init); } +}; +template +struct generate> : public generate_impl { +}; + +struct Prims_Usecase { + bool check_correctness{true}; +}; + +template +class Tests_MG_ReduceIfV + : public ::testing::TestWithParam> { + public: + Tests_MG_ReduceIfV() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + + virtual void SetUp() {} + virtual void TearDown() {} + + // Compare the results of reduce_if_v primitive and thrust reduce on a single GPU + template + void run_current_test(Prims_Usecase const& prims_usecase, input_usecase_t const& input_usecase) + { + // 1. initialize handle + + raft::handle_t handle{}; + HighResClock hr_clock{}; + + raft::comms::initialize_mpi_comms(&handle, MPI_COMM_WORLD); + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + auto const comm_rank = comm.get_rank(); + + auto row_comm_size = static_cast(sqrt(static_cast(comm_size))); + while (comm_size % row_comm_size != 0) { + --row_comm_size; + } + cugraph::partition_2d::subcomm_factory_t + subcomm_factory(handle, row_comm_size); + + // 2. create MG graph + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + hr_clock.start(); + } + auto [mg_graph, d_mg_renumber_map_labels] = + input_usecase.template construct_graph( + handle, true, true); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "MG construct_graph took " << elapsed_time * 1e-6 << " s.\n"; + } + + auto mg_graph_view = mg_graph.view(); + + // 3. run MG count if + + const int hash_bin_count = 5; + const int initial_value = 10; + + auto property_initial_value = generate::initial_value(initial_value); + auto property_data = + generate::property((*d_mg_renumber_map_labels), hash_bin_count, handle); + auto property_iter = get_property_iterator(property_data); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + hr_clock.start(); + } + + auto result = reduce_v(handle, + mg_graph_view, + property_iter, + property_iter + (*d_mg_renumber_map_labels).size(), + property_initial_value); + + if (PERF) { + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + handle.get_comms().barrier(); + double elapsed_time{0.0}; + hr_clock.stop(&elapsed_time); + std::cout << "MG count if took " << elapsed_time * 1e-6 << " s.\n"; + } + + //// 4. compare SG & MG results + + if (prims_usecase.check_correctness) { + cugraph::experimental::graph_t sg_graph( + handle); + std::tie(sg_graph, std::ignore) = + input_usecase.template construct_graph( + handle, true, false); + auto sg_graph_view = sg_graph.view(); + + auto sg_property_data = generate::property( + thrust::make_counting_iterator(sg_graph_view.get_local_vertex_first()), + thrust::make_counting_iterator(sg_graph_view.get_local_vertex_last()), + hash_bin_count, + handle); + auto sg_property_iter = get_property_iterator(sg_property_data); + using property_t = decltype(property_initial_value); + + auto expected_result = + thrust::reduce(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + sg_property_iter, + sg_property_iter + sg_graph_view.get_number_of_local_vertices(), + property_initial_value, + cugraph::experimental::property_add()); + result_compare compare; + ASSERT_TRUE(compare(expected_result, result)); + } + } +}; + +using Tests_MG_ReduceIfV_File = Tests_MG_ReduceIfV; +using Tests_MG_ReduceIfV_Rmat = Tests_MG_ReduceIfV; + +TEST_P(Tests_MG_ReduceIfV_File, CheckInt32Int32FloatTupleIntFloatTransposeFalse) +{ + auto param = GetParam(); + run_current_test, false>(std::get<0>(param), + std::get<1>(param)); +} + +TEST_P(Tests_MG_ReduceIfV_Rmat, CheckInt32Int32FloatTupleIntFloatTransposeFalse) +{ + auto param = GetParam(); + run_current_test, false>(std::get<0>(param), + std::get<1>(param)); +} + +TEST_P(Tests_MG_ReduceIfV_File, CheckInt32Int32FloatTupleIntFloatTransposeTrue) +{ + auto param = GetParam(); + run_current_test, true>(std::get<0>(param), + std::get<1>(param)); +} + +TEST_P(Tests_MG_ReduceIfV_Rmat, CheckInt32Int32FloatTupleIntFloatTransposeTrue) +{ + auto param = GetParam(); + run_current_test, true>(std::get<0>(param), + std::get<1>(param)); +} + +TEST_P(Tests_MG_ReduceIfV_File, CheckInt32Int32FloatTransposeFalse) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +TEST_P(Tests_MG_ReduceIfV_Rmat, CheckInt32Int32FloatTransposeFalse) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +TEST_P(Tests_MG_ReduceIfV_File, CheckInt32Int32FloatTransposeTrue) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +TEST_P(Tests_MG_ReduceIfV_Rmat, CheckInt32Int32FloatTransposeTrue) +{ + auto param = GetParam(); + run_current_test(std::get<0>(param), std::get<1>(param)); +} + +INSTANTIATE_TEST_SUITE_P( + file_test, + Tests_MG_ReduceIfV_File, + ::testing::Combine( + ::testing::Values(Prims_Usecase{true}), + ::testing::Values(cugraph::test::File_Usecase("test/datasets/karate.mtx"), + cugraph::test::File_Usecase("test/datasets/web-Google.mtx"), + cugraph::test::File_Usecase("test/datasets/ljournal-2008.mtx"), + cugraph::test::File_Usecase("test/datasets/webbase-1M.mtx")))); + +INSTANTIATE_TEST_SUITE_P( + rmat_small_test, + Tests_MG_ReduceIfV_Rmat, + ::testing::Combine(::testing::Values(Prims_Usecase{true}), + ::testing::Values(cugraph::test::Rmat_Usecase( + 10, 16, 0.57, 0.19, 0.19, 0, false, false, 0, true)))); + +INSTANTIATE_TEST_SUITE_P( + rmat_large_test, + Tests_MG_ReduceIfV_Rmat, + ::testing::Combine(::testing::Values(Prims_Usecase{false}), + ::testing::Values(cugraph::test::Rmat_Usecase( + 20, 32, 0.57, 0.19, 0.19, 0, false, false, 0, true)))); + +CUGRAPH_MG_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/renumber/renumber_test.cu b/cpp/tests/renumber/renumber_test.cu deleted file mode 100644 index a7102402acf..00000000000 --- a/cpp/tests/renumber/renumber_test.cu +++ /dev/null @@ -1,579 +0,0 @@ -// -*-c++-*- - -/* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -//#include "gmock/gmock.h" - -#include - -#include - -#include - -#include -#include - -#include - -struct RenumberingTest : public ::testing::Test { -}; - -__global__ void display_list(const char *label, uint32_t *verts, size_t length) -{ - printf("%s\n", label); - - for (size_t i = 0; i < length; ++i) { printf(" %u\n", verts[i]); } -} - -__global__ void setup_generator(curandState *state) -{ - int id = threadIdx.x + blockIdx.x * blockDim.x; - curand_init(43, id, 0, &state[id]); -} - -__global__ void generate_sources(curandState *state, int n, uint32_t *verts) -{ - int first = threadIdx.x + blockIdx.x * blockDim.x; - int stride = blockDim.x * gridDim.x; - - curandState local_state = state[first]; - for (int id = first; id < n; id += stride) { verts[id] = curand(&local_state); } - - state[first] = local_state; -} - -__global__ void generate_destinations(curandState *state, - int n, - const uint32_t *sources, - uint32_t *destinations) -{ - int first = threadIdx.x + blockIdx.x * blockDim.x; - int stride = blockDim.x * gridDim.x; - - curandState local_state = state[first]; - for (int id = first; id < n; id += stride) { - destinations[id] = sources[curand(&local_state) % n]; - } - - state[first] = local_state; -} - -TEST_F(RenumberingTest, SmallFixedVertexList) -{ - uint32_t src_data[] = {4U, 6U, 8U, 20U, 1U}; - uint32_t dst_data[] = {1U, 29U, 35U, 0U, 77U}; - - uint32_t src_expected[] = {2U, 3U, 4U, 5U, 1U}; - uint32_t dst_expected[] = {1U, 6U, 7U, 0U, 8U}; - - size_t length = sizeof(src_data) / sizeof(src_data[0]); - - uint32_t *src_d; - uint32_t *dst_d; - - uint32_t tmp_results[length]; - uint32_t tmp_map[2 * length]; - - rmm::device_vector src(length); - rmm::device_vector dst(length); - src_d = src.data().get(); - dst_d = dst.data().get(); - - EXPECT_EQ(cudaMemcpy(src_d, src_data, sizeof(uint32_t) * length, cudaMemcpyHostToDevice), - cudaSuccess); - EXPECT_EQ(cudaMemcpy(dst_d, dst_data, sizeof(uint32_t) * length, cudaMemcpyHostToDevice), - cudaSuccess); - - size_t unique_verts = 0; - - auto number_map = cugraph::detail::renumber_vertices(length, - src_d, - dst_d, - src_d, - dst_d, - &unique_verts, - cugraph::detail::HashFunctionObjectInt(511), - thrust::less(), - rmm::mr::get_current_device_resource()); - - EXPECT_EQ(cudaMemcpy( - tmp_map, number_map->data(), sizeof(uint32_t) * unique_verts, cudaMemcpyDeviceToHost), - cudaSuccess); - EXPECT_EQ(cudaMemcpy(tmp_results, src_d, sizeof(uint32_t) * length, cudaMemcpyDeviceToHost), - cudaSuccess); - - for (size_t i = 0; i < length; ++i) { - EXPECT_EQ(tmp_results[i], src_expected[i]); - EXPECT_EQ(tmp_map[tmp_results[i]], src_data[i]); - } - - EXPECT_EQ(cudaMemcpy(tmp_results, dst_d, sizeof(uint32_t) * length, cudaMemcpyDeviceToHost), - cudaSuccess); - for (size_t i = 0; i < length; ++i) { - EXPECT_EQ(tmp_results[i], dst_expected[i]); - EXPECT_EQ(tmp_map[tmp_results[i]], dst_data[i]); - } -} - -TEST_F(RenumberingTest, SmallFixedVertexListNegative) -{ - int64_t src_data[] = {4, 6, 8, -20, 1}; - int64_t dst_data[] = {1, 29, 35, 0, 77}; - - int64_t src_expected[] = {2, 3, 4, 8, 1}; - int64_t dst_expected[] = {1, 5, 6, 0, 7}; - - size_t length = sizeof(src_data) / sizeof(src_data[0]); - - int64_t *src_d; - int64_t *dst_d; - - int64_t tmp_results[length]; - int64_t tmp_map[2 * length]; - - rmm::device_vector src(length); - rmm::device_vector dst(length); - src_d = src.data().get(); - dst_d = dst.data().get(); - - EXPECT_EQ(cudaMemcpy(src_d, src_data, sizeof(int64_t) * length, cudaMemcpyHostToDevice), - cudaSuccess); - EXPECT_EQ(cudaMemcpy(dst_d, dst_data, sizeof(int64_t) * length, cudaMemcpyHostToDevice), - cudaSuccess); - - size_t unique_verts = 0; - - auto number_map = cugraph::detail::renumber_vertices(length, - src_d, - dst_d, - src_d, - dst_d, - &unique_verts, - cugraph::detail::HashFunctionObjectInt(511), - thrust::less(), - rmm::mr::get_current_device_resource()); - - EXPECT_EQ( - cudaMemcpy(tmp_map, number_map->data(), sizeof(int64_t) * unique_verts, cudaMemcpyDeviceToHost), - cudaSuccess); - EXPECT_EQ(cudaMemcpy(tmp_results, src_d, sizeof(int64_t) * length, cudaMemcpyDeviceToHost), - cudaSuccess); - - for (size_t i = 0; i < length; ++i) { - EXPECT_EQ(tmp_results[i], src_expected[i]); - EXPECT_EQ(tmp_map[tmp_results[i]], src_data[i]); - } - - EXPECT_EQ(cudaMemcpy(tmp_results, dst_d, sizeof(int64_t) * length, cudaMemcpyDeviceToHost), - cudaSuccess); - for (size_t i = 0; i < length; ++i) { - EXPECT_EQ(tmp_results[i], dst_expected[i]); - EXPECT_EQ(tmp_map[tmp_results[i]], dst_data[i]); - } -} - -TEST_F(RenumberingTest, SmallFixedVertexList64Bit) -{ - uint64_t src_data[] = {4U, 6U, 8U, 20U, 1U}; - uint64_t dst_data[] = {1U, 29U, 35U, 0U, 77U}; - - uint64_t src_expected[] = {2U, 3U, 4U, 5U, 1U}; - uint64_t dst_expected[] = {1U, 6U, 7U, 0U, 8U}; - - size_t length = sizeof(src_data) / sizeof(src_data[0]); - - uint64_t *src_d; - uint64_t *dst_d; - - uint64_t tmp_results[length]; - uint64_t tmp_map[2 * length]; - - rmm::device_vector src(length); - rmm::device_vector dst(length); - src_d = src.data().get(); - dst_d = dst.data().get(); - - EXPECT_EQ(cudaMemcpy(src_d, src_data, sizeof(uint64_t) * length, cudaMemcpyHostToDevice), - cudaSuccess); - EXPECT_EQ(cudaMemcpy(dst_d, dst_data, sizeof(uint64_t) * length, cudaMemcpyHostToDevice), - cudaSuccess); - - size_t unique_verts = 0; - - auto number_map = cugraph::detail::renumber_vertices(length, - src_d, - dst_d, - src_d, - dst_d, - &unique_verts, - cugraph::detail::HashFunctionObjectInt(511), - thrust::less(), - rmm::mr::get_current_device_resource()); - - EXPECT_EQ(cudaMemcpy( - tmp_map, number_map->data(), sizeof(uint64_t) * unique_verts, cudaMemcpyDeviceToHost), - cudaSuccess); - EXPECT_EQ(cudaMemcpy(tmp_results, src_d, sizeof(uint64_t) * length, cudaMemcpyDeviceToHost), - cudaSuccess); - - for (size_t i = 0; i < length; ++i) { - EXPECT_EQ(tmp_results[i], src_expected[i]); - EXPECT_EQ(tmp_map[tmp_results[i]], src_data[i]); - } - - EXPECT_EQ(cudaMemcpy(tmp_results, dst_d, sizeof(uint64_t) * length, cudaMemcpyDeviceToHost), - cudaSuccess); - for (size_t i = 0; i < length; ++i) { - EXPECT_EQ(tmp_results[i], dst_expected[i]); - EXPECT_EQ(tmp_map[tmp_results[i]], dst_data[i]); - } -} - -TEST_F(RenumberingTest, SmallFixedVertexList64BitTo32Bit) -{ - uint64_t src_data[] = {4U, 6U, 8U, 20U, 1U}; - uint64_t dst_data[] = {1U, 29U, 35U, 0U, 77U}; - - uint32_t src_expected[] = {2U, 3U, 4U, 5U, 1U}; - uint32_t dst_expected[] = {1U, 6U, 7U, 0U, 8U}; - - size_t length = sizeof(src_data) / sizeof(src_data[0]); - - uint64_t *src_d; - uint64_t *dst_d; - uint32_t *src_renumbered_d; - uint32_t *dst_renumbered_d; - - uint32_t tmp_results[length]; - uint64_t tmp_map[2 * length]; - - rmm::device_vector src(length); - rmm::device_vector dst(length); - src_d = src.data().get(); - dst_d = dst.data().get(); - rmm::device_vector src_renumbered(length); - rmm::device_vector dst_renumbered(length); - src_renumbered_d = src_renumbered.data().get(); - dst_renumbered_d = dst_renumbered.data().get(); - - EXPECT_EQ(cudaMemcpy(src_d, src_data, sizeof(uint64_t) * length, cudaMemcpyHostToDevice), - cudaSuccess); - EXPECT_EQ(cudaMemcpy(dst_d, dst_data, sizeof(uint64_t) * length, cudaMemcpyHostToDevice), - cudaSuccess); - - size_t unique_verts = 0; - - auto number_map = cugraph::detail::renumber_vertices(length, - src_d, - dst_d, - src_renumbered_d, - dst_renumbered_d, - &unique_verts, - cugraph::detail::HashFunctionObjectInt(511), - thrust::less(), - rmm::mr::get_current_device_resource()); - - EXPECT_EQ(cudaMemcpy( - tmp_map, number_map->data(), sizeof(uint64_t) * unique_verts, cudaMemcpyDeviceToHost), - cudaSuccess); - EXPECT_EQ( - cudaMemcpy(tmp_results, src_renumbered_d, sizeof(uint32_t) * length, cudaMemcpyDeviceToHost), - cudaSuccess); - - for (size_t i = 0; i < length; ++i) { - EXPECT_EQ(tmp_results[i], src_expected[i]); - EXPECT_EQ(tmp_map[tmp_results[i]], src_data[i]); - } - - EXPECT_EQ( - cudaMemcpy(tmp_results, dst_renumbered_d, sizeof(uint32_t) * length, cudaMemcpyDeviceToHost), - cudaSuccess); - for (size_t i = 0; i < length; ++i) { - EXPECT_EQ(tmp_results[i], dst_expected[i]); - EXPECT_EQ(tmp_map[tmp_results[i]], dst_data[i]); - } -} - -TEST_F(RenumberingTest, Random100KVertexSet) -{ - const int num_verts = 100000; - - uint64_t *src_d; - uint64_t *dst_d; - - std::vector src_data_vec(num_verts); - std::vector dst_data_vec(num_verts); - std::vector tmp_results_vec(num_verts); - std::vector tmp_map_vec(2 * num_verts); - - uint64_t *src_data = src_data_vec.data(); - uint64_t *dst_data = dst_data_vec.data(); - uint64_t *tmp_results = tmp_results_vec.data(); - uint64_t *tmp_map = tmp_map_vec.data(); - rmm::device_vector src(num_verts); - rmm::device_vector dst(num_verts); - src_d = src.data().get(); - dst_d = dst.data().get(); - - // - // Generate random source and vertex values - // - srand(43); - - for (int i = 0; i < num_verts; ++i) { src_data[i] = (uint64_t)rand(); } - - for (int i = 0; i < num_verts; ++i) { dst_data[i] = (uint64_t)rand(); } - - EXPECT_EQ(cudaMemcpy(src_d, src_data, sizeof(uint64_t) * num_verts, cudaMemcpyHostToDevice), - cudaSuccess); - EXPECT_EQ(cudaMemcpy(dst_d, dst_data, sizeof(uint64_t) * num_verts, cudaMemcpyHostToDevice), - cudaSuccess); - - // - // Renumber everything - // - size_t unique_verts = 0; - size_t n_verts{num_verts}; - - auto start = std::chrono::system_clock::now(); - - auto number_map = cugraph::detail::renumber_vertices(n_verts, - src_d, - dst_d, - src_d, - dst_d, - &unique_verts, - cugraph::detail::HashFunctionObjectInt(511), - thrust::less(), - rmm::mr::get_current_device_resource()); - - auto end = std::chrono::system_clock::now(); - std::chrono::duration elapsed_seconds = end - start; - - std::cout << "Renumber kernel elapsed time (ms): " << elapsed_seconds.count() * 1000 << std::endl; - - EXPECT_EQ(cudaMemcpy( - tmp_map, number_map->data(), sizeof(uint64_t) * unique_verts, cudaMemcpyDeviceToHost), - cudaSuccess); - EXPECT_EQ(cudaMemcpy(tmp_results, src_d, sizeof(uint64_t) * num_verts, cudaMemcpyDeviceToHost), - cudaSuccess); - - size_t min_id = unique_verts; - size_t max_id = 0; - - size_t cnt = 0; - for (size_t i = 0; i < num_verts; ++i) { - min_id = min(min_id, tmp_results[i]); - max_id = max(max_id, tmp_results[i]); - if (tmp_map[tmp_results[i]] != src_data[i]) ++cnt; - - if (cnt < 20) EXPECT_EQ(tmp_map[tmp_results[i]], src_data[i]); - } - - if (cnt > 0) printf(" src error count = %ld out of %d\n", cnt, num_verts); - - EXPECT_EQ(cudaMemcpy(tmp_results, dst_d, sizeof(uint64_t) * num_verts, cudaMemcpyDeviceToHost), - cudaSuccess); - for (size_t i = 0; i < num_verts; ++i) { - min_id = min(min_id, tmp_results[i]); - max_id = max(max_id, tmp_results[i]); - if (tmp_map[tmp_results[i]] != dst_data[i]) ++cnt; - - if (cnt < 20) EXPECT_EQ(tmp_map[tmp_results[i]], dst_data[i]); - } - - if (cnt > 0) printf(" src error count = %ld out of %d\n", cnt, num_verts); - - EXPECT_EQ(min_id, 0); - EXPECT_EQ(max_id, (unique_verts - 1)); -} - -TEST_F(RenumberingTest, Random10MVertexSet) -{ - const int num_verts = 10000000; - - // A sampling of performance on single Quadro GV100 - // const int hash_size = 32767; // 238 ms - // const int hash_size = 8191; // 224 ms - const int hash_size = 511; // 224 ms - - uint32_t *src_d; - uint32_t *dst_d; - - rmm::device_vector src(num_verts); - rmm::device_vector dst(num_verts); - src_d = src.data().get(); - dst_d = dst.data().get(); - - // - // Init the random number generate - // - const int num_threads{64}; - curandState *state; - - rmm::device_vector state_vals(num_threads); - state = state_vals.data().get(); - setup_generator<<>>(state); - generate_sources<<>>(state, num_verts, src_d); - generate_destinations<<>>(state, num_verts, src_d, dst_d); - - std::cout << "done with initialization" << std::endl; - - // - // Renumber everything - // - size_t unique_verts = 0; - size_t n_verts{num_verts}; - - auto start = std::chrono::system_clock::now(); - auto number_map = - cugraph::detail::renumber_vertices(n_verts, - src_d, - dst_d, - src_d, - dst_d, - &unique_verts, - cugraph::detail::HashFunctionObjectInt(hash_size), - thrust::less(), - rmm::mr::get_current_device_resource()); - auto end = std::chrono::system_clock::now(); - std::chrono::duration elapsed_seconds = end - start; - - std::cout << "Renumber kernel elapsed time (ms): " << elapsed_seconds.count() * 1000 << std::endl; - std::cout << " unique verts = " << unique_verts << std::endl; - std::cout << " hash size = " << hash_size << std::endl; -} - -TEST_F(RenumberingTest, Random100MVertexSet) -{ - const int num_verts = 100000000; - - // A sampling of performance on single Quadro GV100 - // const int hash_size = 8192; // 1811 ms - // const int hash_size = 16384; // 1746 ms - // const int hash_size = 32768; // 1662 ms - // const int hash_size = 65536; // 1569 ms - // const int hash_size = 16777216; // 1328 ms - const int hash_size = 511; - - uint32_t *src_d; - uint32_t *dst_d; - - rmm::device_vector src(num_verts); - rmm::device_vector dst(num_verts); - src_d = src.data().get(); - dst_d = dst.data().get(); - - // - // Init the random number generate - // - const int num_threads{64}; - curandState *state; - - rmm::device_vector state_vals(num_threads); - state = state_vals.data().get(); - setup_generator<<>>(state); - generate_sources<<>>(state, num_verts, src_d); - generate_destinations<<>>(state, num_verts, src_d, dst_d); - - std::cout << "done with initialization" << std::endl; - - // - // Renumber everything - // - size_t unique_verts = 0; - size_t n_verts{num_verts}; - - auto start = std::chrono::system_clock::now(); - auto number_map = - cugraph::detail::renumber_vertices(n_verts, - src_d, - dst_d, - src_d, - dst_d, - &unique_verts, - cugraph::detail::HashFunctionObjectInt(hash_size), - thrust::less(), - rmm::mr::get_current_device_resource()); - auto end = std::chrono::system_clock::now(); - std::chrono::duration elapsed_seconds = end - start; - - std::cout << "Renumber kernel elapsed time (ms): " << elapsed_seconds.count() * 1000 << std::endl; - std::cout << " unique verts = " << unique_verts << std::endl; - std::cout << " hash size = " << hash_size << std::endl; -} - -TEST_F(RenumberingTest, Random500MVertexSet) -{ - const int num_verts = 500000000; - - // A sampling of performance on single Quadro GV100 - // const int hash_size = 8192; // 9918 ms - // const int hash_size = 16384; // 9550 ms - // const int hash_size = 32768; // 9146 ms - // const int hash_size = 131072; // 8537 ms - const int hash_size = 1048576; // 7335 ms - // const int hash_size = 511; // 7335 ms - - uint32_t *src_d; - uint32_t *dst_d; - - rmm::device_vector src(num_verts); - rmm::device_vector dst(num_verts); - src_d = src.data().get(); - dst_d = dst.data().get(); - - // - // Init the random number generate - // - const int num_threads{64}; - curandState *state; - - rmm::device_vector state_vals(num_threads); - state = state_vals.data().get(); - setup_generator<<>>(state); - generate_sources<<>>(state, num_verts, src_d); - generate_destinations<<>>(state, num_verts, src_d, dst_d); - - std::cout << "done with initialization" << std::endl; - - // - // Renumber everything - // - size_t unique_verts = 0; - size_t n_verts{num_verts}; - - auto start = std::chrono::system_clock::now(); - auto number_map = - cugraph::detail::renumber_vertices(n_verts, - src_d, - dst_d, - src_d, - dst_d, - &unique_verts, - cugraph::detail::HashFunctionObjectInt(hash_size), - thrust::less(), - rmm::mr::get_current_device_resource()); - auto end = std::chrono::system_clock::now(); - std::chrono::duration elapsed_seconds = end - start; - - std::cout << "Renumber kernel elapsed time (ms): " << elapsed_seconds.count() * 1000 << std::endl; - std::cout << " unique verts = " << unique_verts << std::endl; - std::cout << " hash size = " << hash_size << std::endl; -} - -CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/sampling/random_walks_profiling.cu b/cpp/tests/sampling/random_walks_profiling.cu new file mode 100644 index 00000000000..595086a570e --- /dev/null +++ b/cpp/tests/sampling/random_walks_profiling.cu @@ -0,0 +1,241 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include // cugraph::test::create_memory_resource() +#include +#include + +#include +#include + +#include +#include + +#include + +#include +#include + +#include +#include +#include +#include +#include + +/** + * @internal + * @brief Populates the device vector d_start with the starting vertex indices + * to be used for each RW path specified. + */ +template +void fill_start(raft::handle_t const& handle, + rmm::device_uvector& d_start, + index_t num_vertices) +{ + index_t num_paths = d_start.size(); + + thrust::transform(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + thrust::make_counting_iterator(0), + thrust::make_counting_iterator(num_paths), + + d_start.begin(), + [num_vertices] __device__(auto indx) { return indx % num_vertices; }); +} + +namespace impl_details = cugraph::experimental::detail; + +enum class traversal_id_t : int { HORIZONTAL = 0, VERTICAL }; + +/** + * @internal + * @brief Calls the random_walks algorithm with specified traversal strategy and displays the time + * metrics (total time for all requested paths, average time for each path). + */ +template +void output_random_walks_time(graph_vt const& graph_view, + typename graph_vt::edge_type num_paths, + traversal_id_t trv_id) +{ + using vertex_t = typename graph_vt::vertex_type; + using edge_t = typename graph_vt::edge_type; + using weight_t = typename graph_vt::weight_type; + + raft::handle_t handle{}; + rmm::device_uvector d_start(num_paths, handle.get_stream()); + + vertex_t num_vertices = graph_view.get_number_of_vertices(); + fill_start(handle, d_start, num_vertices); + + // 0-copy const device view: + // + impl_details::device_const_vector_view d_start_view{d_start.data(), num_paths}; + + edge_t max_depth{10}; + + HighResTimer hr_timer; + std::string label{}; + + if (trv_id == traversal_id_t::HORIZONTAL) { + label = std::string("RandomWalks; Horizontal traversal"); + hr_timer.start(label); + cudaProfilerStart(); + auto ret_tuple = + impl_details::random_walks_impl( + handle, graph_view, d_start_view, max_depth); + cudaProfilerStop(); + hr_timer.stop(); + } else { + label = std::string("RandomWalks; Vertical traversal"); + hr_timer.start(label); + cudaProfilerStart(); + auto ret_tuple = impl_details::random_walks_impl( + handle, graph_view, d_start_view, max_depth); + cudaProfilerStop(); + hr_timer.stop(); + } + try { + auto runtime = hr_timer.get_average_runtime(label); + + std::cout << "RW for num_paths: " << num_paths + << ", runtime [ms] / path: " << runtime / num_paths << ":\n"; + + } catch (std::exception const& ex) { + std::cerr << ex.what() << '\n'; + return; + + } catch (...) { + std::cerr << "ERROR: Unknown exception on timer label search." << '\n'; + return; + } + hr_timer.display(std::cout); +} + +/** + * @struct RandomWalks_Usecase + * @brief Used to specify input to a random_walks benchmark/profile run + * + * @var RandomWalks_Usecase::graph_file_full_path Computed during construction + * to be an absolute path consisting of the value of the RAPIDS_DATASET_ROOT_DIR + * env var and the graph_file_path constructor arg. This is initialized to an + * empty string. + * + * @var RandomWalks_Usecase::test_weighted Bool representing if the specified + * graph is weighted or not. This is initialized to false (unweighted). + */ +struct RandomWalks_Usecase { + std::string graph_file_full_path{}; + bool test_weighted{false}; + + RandomWalks_Usecase(std::string const& graph_file_path, bool test_weighted) + : test_weighted(test_weighted) + { + if ((graph_file_path.length() > 0) && (graph_file_path[0] != '/')) { + graph_file_full_path = cugraph::test::get_rapids_dataset_root_dir() + "/" + graph_file_path; + } else { + graph_file_full_path = graph_file_path; + } + }; +}; + +/** + * @brief Runs random_walks on a specified input and outputs time metrics + * + * Creates a graph_t instance from the configuration specified in the + * RandomWalks_Usecase instance passed in (currently by reading a dataset to + * populate the graph_t), then runs random_walks to generate 1, 10, and 100 + * random paths and output statistics for each. + * + * @tparam vertex_t Type of vertex identifiers. + * @tparam edge_t Type of edge identifiers. + * @tparam weight_t Type of weight identifiers. + * + * @param[in] configuration RandomWalks_Usecase instance containing the input + * file to read for constructing the graph_t. + * @param[in] trv_id traversal strategy. + */ +template +void run(RandomWalks_Usecase const& configuration, traversal_id_t trv_id) +{ + raft::handle_t handle{}; + + cugraph::experimental::graph_t graph(handle); + std::tie(graph, std::ignore) = + cugraph::test::read_graph_from_matrix_market_file( + handle, configuration.graph_file_full_path, configuration.test_weighted, false); + + auto graph_view = graph.view(); + + // FIXME: the num_paths vector might be better specified via the + // configuration input instead of hardcoding here. + std::vector v_np{1, 10, 100}; + for (auto&& num_paths : v_np) { + output_random_walks_time(graph_view, num_paths, trv_id); + } +} + +/** + * @brief Performs the random_walks benchmark/profiling run + * + * main function for performing the random_walks benchmark/profiling run. The + * resulting executable takes the following options: "rmm_mode" which can be one + * of "binning", "cuda", "pool", or "managed. "dataset" which is a path + * relative to the env var RAPIDS_DATASET_ROOT_DIR to a input .mtx file to use + * to populate the graph_t instance. + * + * To use the default values of rmm_mode=pool and + * dataset=test/datasets/karate.mtx: + * @code + * RANDOM_WALKS_PROFILING + * @endcode + * + * To specify managed memory and the netscience.mtx dataset (relative to a + * particular RAPIDS_DATASET_ROOT_DIR setting): + * @code + * RANDOM_WALKS_PROFILING --rmm_mode=managed --dataset=test/datasets/netscience.mtx + * @endcode + * + * @return An int representing a successful run. 0 indicates success. + */ +int main(int argc, char** argv) +{ + // Add command-line processing, provide defaults + cxxopts::Options options(argv[0], " - Random Walks benchmark command line options"); + options.add_options()( + "rmm_mode", "RMM allocation mode", cxxopts::value()->default_value("pool")); + options.add_options()( + "dataset", "dataset", cxxopts::value()->default_value("test/datasets/karate.mtx")); + auto const cmd_options = options.parse(argc, argv); + auto const rmm_mode = cmd_options["rmm_mode"].as(); + auto const dataset = cmd_options["dataset"].as(); + + // Configure RMM + auto resource = cugraph::test::create_memory_resource(rmm_mode); + rmm::mr::set_current_device_resource(resource.get()); + + // Run benchmarks + std::cout << "Using dataset: " << dataset << std::endl; + + std::cout << "Horizontal traversal strategy:\n"; + run(RandomWalks_Usecase(dataset, true), traversal_id_t::HORIZONTAL); + + std::cout << "Vertical traversal strategy:\n"; + run(RandomWalks_Usecase(dataset, true), traversal_id_t::VERTICAL); + + // FIXME: consider returning non-zero for situations that warrant it (eg. if + // the algo ran but the results are invalid, if a benchmark threshold is + // exceeded, etc.) + return 0; +} diff --git a/cpp/tests/sampling/random_walks_test.cu b/cpp/tests/sampling/random_walks_test.cu new file mode 100644 index 00000000000..dc73c474356 --- /dev/null +++ b/cpp/tests/sampling/random_walks_test.cu @@ -0,0 +1,177 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cuda_profiler_api.h" +#include "gtest/gtest.h" + +#include +#include + +#include +#include + +#include +#include + +#include +#include + +#include "random_walks_utils.cuh" + +#include +#include +#include +#include +#include +#include +#include + +namespace { // anonym. +template +void fill_start(raft::handle_t const& handle, + rmm::device_uvector& d_start, + index_t num_vertices) +{ + index_t num_paths = d_start.size(); + + thrust::transform(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + thrust::make_counting_iterator(0), + thrust::make_counting_iterator(num_paths), + + d_start.begin(), + [num_vertices] __device__(auto indx) { return indx % num_vertices; }); +} +} // namespace + +namespace impl_details = cugraph::experimental::detail; + +enum class traversal_id_t : int { HORIZONTAL = 0, VERTICAL }; + +struct RandomWalks_Usecase { + std::string graph_file_full_path{}; + bool test_weighted{false}; + + RandomWalks_Usecase(std::string const& graph_file_path, bool test_weighted) + : test_weighted(test_weighted) + { + if ((graph_file_path.length() > 0) && (graph_file_path[0] != '/')) { + graph_file_full_path = cugraph::test::get_rapids_dataset_root_dir() + "/" + graph_file_path; + } else { + graph_file_full_path = graph_file_path; + } + }; +}; + +class Tests_RandomWalks + : public ::testing::TestWithParam> { + public: + Tests_RandomWalks() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + + virtual void SetUp() {} + virtual void TearDown() {} + + template + void run_current_test(std::tuple const& configuration) + { + raft::handle_t handle{}; + + // debuf info: + // + // std::cout << "read graph file: " << configuration.graph_file_full_path << std::endl; + + traversal_id_t trv_id = std::get<0>(configuration); + auto const& target = std::get<1>(configuration); + cugraph::experimental::graph_t graph(handle); + std::tie(graph, std::ignore) = + cugraph::test::read_graph_from_matrix_market_file( + handle, target.graph_file_full_path, target.test_weighted, false); + + auto graph_view = graph.view(); + + // call random_walks: + start_random_walks(graph_view, trv_id); + } + + template + void start_random_walks(graph_vt const& graph_view, traversal_id_t trv_id) + { + using vertex_t = typename graph_vt::vertex_type; + using edge_t = typename graph_vt::edge_type; + using weight_t = typename graph_vt::weight_type; + + raft::handle_t handle{}; + edge_t num_paths = 10; + rmm::device_uvector d_start(num_paths, handle.get_stream()); + + vertex_t num_vertices = graph_view.get_number_of_vertices(); + fill_start(handle, d_start, num_vertices); + + // 0-copy const device view: + // + impl_details::device_const_vector_view d_start_view{d_start.data(), + num_paths}; + + edge_t max_depth{10}; + + if (trv_id == traversal_id_t::HORIZONTAL) { + auto ret_tuple = + impl_details::random_walks_impl( + handle, graph_view, d_start_view, max_depth); + + // check results: + // + bool test_all_paths = cugraph::test::host_check_rw_paths( + handle, graph_view, std::get<0>(ret_tuple), std::get<1>(ret_tuple), std::get<2>(ret_tuple)); + + if (!test_all_paths) + std::cout << "starting seed on failure: " << std::get<3>(ret_tuple) << '\n'; + + ASSERT_TRUE(test_all_paths); + } else { // VERTICAL + auto ret_tuple = + impl_details::random_walks_impl( + handle, graph_view, d_start_view, max_depth); + + // check results: + // + bool test_all_paths = cugraph::test::host_check_rw_paths( + handle, graph_view, std::get<0>(ret_tuple), std::get<1>(ret_tuple), std::get<2>(ret_tuple)); + + if (!test_all_paths) + std::cout << "starting seed on failure: " << std::get<3>(ret_tuple) << '\n'; + + ASSERT_TRUE(test_all_paths); + } + } +}; + +TEST_P(Tests_RandomWalks, Initialize_i32_i32_f) +{ + run_current_test(GetParam()); +} + +INSTANTIATE_TEST_SUITE_P( + simple_test, + Tests_RandomWalks, + ::testing::Combine(::testing::Values(traversal_id_t::HORIZONTAL, traversal_id_t::VERTICAL), + ::testing::Values(RandomWalks_Usecase("test/datasets/karate.mtx", true), + RandomWalks_Usecase("test/datasets/web-Google.mtx", true), + RandomWalks_Usecase("test/datasets/ljournal-2008.mtx", true), + RandomWalks_Usecase("test/datasets/webbase-1M.mtx", true)))); + +CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/sampling/random_walks_utils.cuh b/cpp/tests/sampling/random_walks_utils.cuh new file mode 100644 index 00000000000..df42242e6fe --- /dev/null +++ b/cpp/tests/sampling/random_walks_utils.cuh @@ -0,0 +1,274 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include + +#include + +#include +#include +#include +#include +#include +#include + +// utilities for testing / verification of Random Walks functionality: +// +namespace cugraph { +namespace test { + +template +using vector_test_t = cugraph::experimental::detail::device_vec_t; // for debug purposes + +// host side utility to check a if a sequence of vertices is connected: +// +template +bool host_check_path(std::vector const& row_offsets, + std::vector const& col_inds, + std::vector const& values, + typename std::vector::const_iterator v_path_begin, + typename std::vector::const_iterator v_path_end, + typename std::vector::const_iterator w_path_begin) +{ + bool assert1 = (row_offsets.size() > 0); + bool assert2 = (col_inds.size() == values.size()); + + vertex_t num_rows = row_offsets.size() - 1; + edge_t nnz = row_offsets.back(); + + bool assert3 = (nnz == static_cast(col_inds.size())); + if (assert1 == false || assert2 == false || assert3 == false) { + std::cerr << "CSR inconsistency\n"; + return false; + } + + auto it_w = w_path_begin; + for (auto it_v = v_path_begin; it_v != v_path_end - 1; ++it_v, ++it_w) { + auto crt_vertex = *it_v; + auto next_vertex = *(it_v + 1); + + auto begin = col_inds.begin() + row_offsets[crt_vertex]; + auto end = col_inds.begin() + row_offsets[crt_vertex + 1]; + auto found_next = std::find_if( + begin, end, [next_vertex](auto dst_vertex) { return dst_vertex == next_vertex; }); + if (found_next == end) { + std::cerr << "vertex not found: " << next_vertex << " as neighbor of " << crt_vertex << '\n'; + return false; + } + + auto delta = row_offsets[crt_vertex] + std::distance(begin, found_next); + + // std::cerr << "delta in ci: " << delta << '\n'; + auto found_edge = values.begin() + delta; + if (*found_edge != *it_w) { + std::cerr << "weight not found: " << *found_edge << " between " << crt_vertex << " and " + << next_vertex << '\n'; + return false; + } + } + return true; +} + +template +bool host_check_rw_paths( + raft::handle_t const& handle, + cugraph::experimental::graph_view_t const& graph_view, + vector_test_t const& d_coalesced_v, + vector_test_t const& d_coalesced_w, + vector_test_t const& d_sizes, + index_t num_paths = 0) // only relevant for the padded case (in which case it must be non-zero) +{ + edge_t num_edges = graph_view.get_number_of_edges(); + vertex_t num_vertices = graph_view.get_number_of_vertices(); + + auto offsets = graph_view.get_matrix_partition_view().get_offsets(); + auto indices = graph_view.get_matrix_partition_view().get_indices(); + auto values = graph_view.get_matrix_partition_view().get_weights(); + + std::vector v_ro(num_vertices + 1); + std::vector v_ci(num_edges); + std::vector v_vals( + num_edges, 1); // account for unweighted graph, for which RW provides default weights{1} + + raft::update_host(v_ro.data(), offsets, v_ro.size(), handle.get_stream()); + raft::update_host(v_ci.data(), indices, v_ci.size(), handle.get_stream()); + + if (values) { raft::update_host(v_vals.data(), *values, v_vals.size(), handle.get_stream()); } + + std::vector v_coalesced(d_coalesced_v.size()); + std::vector w_coalesced(d_coalesced_w.size()); + std::vector v_sizes(d_sizes.size()); + + raft::update_host(v_coalesced.data(), + cugraph::experimental::detail::raw_const_ptr(d_coalesced_v), + d_coalesced_v.size(), + handle.get_stream()); + raft::update_host(w_coalesced.data(), + cugraph::experimental::detail::raw_const_ptr(d_coalesced_w), + d_coalesced_w.size(), + handle.get_stream()); + + if (v_sizes.size() > 0) { // coalesced case + raft::update_host(v_sizes.data(), + cugraph::experimental::detail::raw_const_ptr(d_sizes), + d_sizes.size(), + handle.get_stream()); + } else { // padded case + if (num_paths == 0) { + std::cerr << "ERROR: padded case requires `num_paths` info.\n"; + return false; + } + + // extract sizes from v_coalesced (which now contains padded info) + // + auto max_depth = v_coalesced.size() / num_paths; + auto it_start_path = v_coalesced.begin(); + for (index_t row_index = 0; row_index < num_paths; ++row_index) { + auto it_end_path = it_start_path + max_depth; + auto it_padding_found = std::find(it_start_path, it_end_path, num_vertices); + + v_sizes.push_back(std::distance(it_start_path, it_padding_found)); + + it_start_path = it_end_path; + } + + // truncate padded vectors v_coalesced, w_coalesced: + // + v_coalesced.erase(std::remove(v_coalesced.begin(), v_coalesced.end(), num_vertices), + v_coalesced.end()); + + w_coalesced.erase(std::remove(w_coalesced.begin(), w_coalesced.end(), weight_t{0}), + w_coalesced.end()); + } + + auto it_v_begin = v_coalesced.begin(); + auto it_w_begin = w_coalesced.begin(); + for (auto&& crt_sz : v_sizes) { + auto it_v_end = it_v_begin + crt_sz; + + bool test_path = host_check_path(v_ro, v_ci, v_vals, it_v_begin, it_v_end, it_w_begin); + + it_v_begin = it_v_end; + it_w_begin += crt_sz - 1; + + if (!test_path) { // something went wrong; print to debug (since it's random) + raft::print_host_vector("sizes", v_sizes.data(), v_sizes.size(), std::cerr); + + raft::print_host_vector("coalesced v", v_coalesced.data(), v_coalesced.size(), std::cerr); + + raft::print_host_vector("coalesced w", w_coalesced.data(), w_coalesced.size(), std::cerr); + + return false; + } + } + return true; +} + +template +bool host_check_query_rw(raft::handle_t const& handle, + vector_test_t const& d_v_sizes, + vector_test_t const& d_v_offsets, + vector_test_t const& d_w_sizes, + vector_test_t const& d_w_offsets) +{ + index_t num_paths = d_v_sizes.size(); + + if (num_paths == 0) return false; + + std::vector v_sizes(num_paths); + std::vector v_offsets(num_paths); + std::vector w_sizes(num_paths); + std::vector w_offsets(num_paths); + + raft::update_host(v_sizes.data(), + cugraph::experimental::detail::raw_const_ptr(d_v_sizes), + num_paths, + handle.get_stream()); + + raft::update_host(v_offsets.data(), + cugraph::experimental::detail::raw_const_ptr(d_v_offsets), + num_paths, + handle.get_stream()); + + raft::update_host(w_sizes.data(), + cugraph::experimental::detail::raw_const_ptr(d_w_sizes), + num_paths, + handle.get_stream()); + + raft::update_host(w_offsets.data(), + cugraph::experimental::detail::raw_const_ptr(d_w_offsets), + num_paths, + handle.get_stream()); + + index_t crt_v_offset = 0; + index_t crt_w_offset = 0; + auto it_v_sz = v_sizes.begin(); + auto it_w_sz = w_sizes.begin(); + auto it_v_offset = v_offsets.begin(); + auto it_w_offset = w_offsets.begin(); + + bool flag_passed{true}; + + for (; it_v_sz != v_sizes.end(); ++it_v_sz, ++it_w_sz, ++it_v_offset, ++it_w_offset) { + if (*it_w_sz != (*it_v_sz) - 1) { + std::cerr << "ERROR: Incorrect weight path size: " << *it_w_sz << ", " << *it_v_sz << '\n'; + flag_passed = false; + break; + } + + if (*it_v_offset != crt_v_offset) { + std::cerr << "ERROR: Incorrect vertex path offset: " << *it_v_offset << ", " << crt_v_offset + << '\n'; + flag_passed = false; + break; + } + + if (*it_w_offset != crt_w_offset) { + std::cerr << "ERROR: Incorrect weight path offset: " << *it_w_offset << ", " << crt_w_offset + << '\n'; + flag_passed = false; + break; + } + + crt_v_offset += *it_v_sz; + crt_w_offset += *it_w_sz; + } + + if (!flag_passed) { + std::cerr << "v sizes:"; + std::copy(v_sizes.begin(), v_sizes.end(), std::ostream_iterator(std::cerr, ", ")); + std::cerr << '\n'; + + std::cerr << "v offsets:"; + std::copy(v_offsets.begin(), v_offsets.end(), std::ostream_iterator(std::cerr, ", ")); + std::cerr << '\n'; + + std::cerr << "w sizes:"; + std::copy(w_sizes.begin(), w_sizes.end(), std::ostream_iterator(std::cerr, ", ")); + std::cerr << '\n'; + + std::cerr << "w offsets:"; + std::copy(w_offsets.begin(), w_offsets.end(), std::ostream_iterator(std::cerr, ", ")); + std::cerr << '\n'; + } + + return flag_passed; +} + +} // namespace test +} // namespace cugraph diff --git a/cpp/tests/sampling/rw_low_level_test.cu b/cpp/tests/sampling/rw_low_level_test.cu new file mode 100644 index 00000000000..3b2779a5814 --- /dev/null +++ b/cpp/tests/sampling/rw_low_level_test.cu @@ -0,0 +1,1062 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "cuda_profiler_api.h" + +#include +#include + +#include +#include + +#include +#include + +#include +#include + +#include "random_walks_utils.cuh" + +#include +#include +#include +#include +#include +#include +#include + +using namespace cugraph::experimental; + +template +using vector_test_t = detail::device_vec_t; // for debug purposes + +namespace { // anonym. + +template +bool check_col_indices(raft::handle_t const& handle, + vector_test_t const& d_crt_out_degs, + vector_test_t const& d_col_indx, + index_t num_paths) +{ + bool all_indices_within_degs = thrust::all_of( + rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + thrust::make_counting_iterator(0), + thrust::make_counting_iterator(num_paths), + [p_d_col_indx = detail::raw_const_ptr(d_col_indx), + p_d_crt_out_degs = detail::raw_const_ptr(d_crt_out_degs)] __device__(auto indx) { + if (p_d_crt_out_degs[indx] > 0) + return ((p_d_col_indx[indx] >= 0) && (p_d_col_indx[indx] < p_d_crt_out_degs[indx])); + else + return true; + }); + return all_indices_within_degs; +} + +} // namespace + +// FIXME (per rlratzel request): +// This test may be considered an e2e test +// which could be moved to a different test suite: +// +struct RandomWalksPrimsTest : public ::testing::Test { +}; + +TEST_F(RandomWalksPrimsTest, SimpleGraphRWStart) +{ + using namespace cugraph::experimental::detail; + + using vertex_t = int32_t; + using edge_t = vertex_t; + using weight_t = float; + using index_t = vertex_t; + + raft::handle_t handle{}; + + edge_t num_edges = 8; + vertex_t num_vertices = 6; + + std::vector v_src{0, 1, 1, 2, 2, 2, 3, 4}; + std::vector v_dst{1, 3, 4, 0, 1, 3, 5, 5}; + std::vector v_w{0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1}; + + auto graph = cugraph::test::make_graph( + handle, v_src, v_dst, std::optional>{v_w}, num_vertices, num_edges); + + auto graph_view = graph.view(); + + edge_t const* offsets = graph_view.get_matrix_partition_view().get_offsets(); + vertex_t const* indices = graph_view.get_matrix_partition_view().get_indices(); + weight_t const* values = *(graph_view.get_matrix_partition_view().get_weights()); + + std::vector v_ro(num_vertices + 1); + std::vector v_ci(num_edges); + std::vector v_vs(num_edges); + + raft::update_host(v_ro.data(), offsets, num_vertices + 1, handle.get_stream()); + raft::update_host(v_ci.data(), indices, num_edges, handle.get_stream()); + raft::update_host(v_vs.data(), values, num_edges, handle.get_stream()); + + std::vector v_ro_expected{0, 1, 3, 6, 7, 8, 8}; + std::vector v_ci_expected{1, 3, 4, 0, 1, 3, 5, 5}; + std::vector v_vs_expected{0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1}; + + EXPECT_EQ(v_ro, v_ro_expected); + EXPECT_EQ(v_ci, v_ci_expected); + EXPECT_EQ(v_vs, v_vs_expected); + + index_t num_paths = 4; + index_t max_depth = 3; + index_t total_sz = num_paths * max_depth; + + std::vector v_coalesced(total_sz, -1); + std::vector w_coalesced(total_sz - num_paths, -1); + + vector_test_t d_coalesced_v(total_sz, handle.get_stream()); + vector_test_t d_coalesced_w(total_sz - num_paths, handle.get_stream()); + + raft::update_device( + d_coalesced_v.data(), v_coalesced.data(), d_coalesced_v.size(), handle.get_stream()); + raft::update_device( + d_coalesced_w.data(), w_coalesced.data(), d_coalesced_w.size(), handle.get_stream()); + + std::vector v_start{1, 0, 4, 2}; + vector_test_t d_start(num_paths, handle.get_stream()); + + raft::update_device(d_start.data(), v_start.data(), d_start.size(), handle.get_stream()); + + vector_test_t d_sizes(num_paths, handle.get_stream()); + + random_walker_t rand_walker{handle, graph_view, num_paths, max_depth}; + + rand_walker.start(d_start, d_coalesced_v, d_sizes); + + std::vector v_coalesced_exp{1, -1, -1, 0, -1, -1, 4, -1, -1, 2, -1, -1}; + raft::update_host( + v_coalesced.data(), raw_const_ptr(d_coalesced_v), total_sz, handle.get_stream()); + EXPECT_EQ(v_coalesced, v_coalesced_exp); + + std::vector v_sizes{1, 1, 1, 1}; + std::vector v_sz_exp(num_paths); + raft::update_host(v_sz_exp.data(), raw_const_ptr(d_sizes), num_paths, handle.get_stream()); + + EXPECT_EQ(v_sizes, v_sz_exp); +} + +TEST_F(RandomWalksPrimsTest, SimpleGraphCoalesceExperiments) +{ + using namespace cugraph::experimental::detail; + + using vertex_t = int32_t; + using edge_t = vertex_t; + using weight_t = float; + using index_t = vertex_t; + + raft::handle_t handle{}; + + edge_t num_edges = 8; + vertex_t num_vertices = 6; + + std::vector v_src{0, 1, 1, 2, 2, 2, 3, 4}; + std::vector v_dst{1, 3, 4, 0, 1, 3, 5, 5}; + std::vector v_w{0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1}; + + auto graph = cugraph::test::make_graph( + handle, v_src, v_dst, std::optional>{v_w}, num_vertices, num_edges); + + auto graph_view = graph.view(); + + edge_t const* offsets = graph_view.get_matrix_partition_view().get_offsets(); + vertex_t const* indices = graph_view.get_matrix_partition_view().get_indices(); + weight_t const* values = *(graph_view.get_matrix_partition_view().get_weights()); + + index_t num_paths = 4; + index_t max_depth = 3; + index_t total_sz = num_paths * max_depth; + + std::vector v_coalesced(total_sz, -1); + std::vector w_coalesced(total_sz - num_paths, -1); + + vector_test_t d_coalesced_v(total_sz, handle.get_stream()); + vector_test_t d_coalesced_w(total_sz - num_paths, handle.get_stream()); + + raft::update_device( + d_coalesced_v.data(), v_coalesced.data(), d_coalesced_v.size(), handle.get_stream()); + raft::update_device( + d_coalesced_w.data(), w_coalesced.data(), d_coalesced_w.size(), handle.get_stream()); + + std::vector v_start{1, 0, 4, 2}; + vector_test_t d_start(num_paths, handle.get_stream()); + + raft::update_device(d_start.data(), v_start.data(), d_start.size(), handle.get_stream()); + + vector_test_t d_sizes(num_paths, handle.get_stream()); + + random_walker_t rand_walker{handle, graph_view, num_paths, max_depth}; + + auto const& d_out_degs = rand_walker.get_out_degs(); + EXPECT_EQ(static_cast(num_vertices), d_out_degs.size()); + + std::vector v_out_degs(num_vertices); + raft::update_host( + v_out_degs.data(), raw_const_ptr(d_out_degs), num_vertices, handle.get_stream()); + + std::vector v_out_degs_exp{1, 2, 3, 1, 1, 0}; + EXPECT_EQ(v_out_degs, v_out_degs_exp); + + rand_walker.start(d_start, d_coalesced_v, d_sizes); + + // update crt_out_degs: + // + vector_test_t d_crt_out_degs(num_paths, handle.get_stream()); + rand_walker.gather_from_coalesced( + d_coalesced_v, d_out_degs, d_sizes, d_crt_out_degs, max_depth, num_paths); + + std::vector v_crt_out_degs(num_paths); + raft::update_host( + v_crt_out_degs.data(), raw_const_ptr(d_crt_out_degs), num_paths, handle.get_stream()); + + std::vector v_crt_out_degs_exp{2, 1, 1, 3}; + EXPECT_EQ(v_crt_out_degs, v_crt_out_degs_exp); +} + +TEST_F(RandomWalksPrimsTest, SimpleGraphColExtraction) +{ + using namespace cugraph::experimental::detail; + + using vertex_t = int32_t; + using edge_t = vertex_t; + using weight_t = float; + using index_t = vertex_t; + + raft::handle_t handle{}; + + edge_t num_edges = 8; + vertex_t num_vertices = 6; + + std::vector v_src{0, 1, 1, 2, 2, 2, 3, 4}; + std::vector v_dst{1, 3, 4, 0, 1, 3, 5, 5}; + std::vector v_w{0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1}; + + auto graph = cugraph::test::make_graph( + handle, v_src, v_dst, std::optional>{v_w}, num_vertices, num_edges); + + auto graph_view = graph.view(); + + edge_t const* offsets = graph_view.get_matrix_partition_view().get_offsets(); + vertex_t const* indices = graph_view.get_matrix_partition_view().get_indices(); + weight_t const* values = *(graph_view.get_matrix_partition_view().get_weights()); + + index_t num_paths = 4; + index_t max_depth = 3; + index_t total_sz = num_paths * max_depth; + + std::vector v_coalesced(total_sz, -1); + std::vector w_coalesced(total_sz - num_paths, -1); + + vector_test_t d_coalesced_v(total_sz, handle.get_stream()); + vector_test_t d_coalesced_w(total_sz - num_paths, handle.get_stream()); + + raft::update_device( + d_coalesced_v.data(), v_coalesced.data(), d_coalesced_v.size(), handle.get_stream()); + raft::update_device( + d_coalesced_w.data(), w_coalesced.data(), d_coalesced_w.size(), handle.get_stream()); + + std::vector v_start{1, 0, 4, 2}; + vector_test_t d_start(num_paths, handle.get_stream()); + + raft::update_device(d_start.data(), v_start.data(), d_start.size(), handle.get_stream()); + + vector_test_t d_sizes(num_paths, handle.get_stream()); + + random_walker_t rand_walker{handle, graph_view, num_paths, max_depth}; + + auto const& d_out_degs = rand_walker.get_out_degs(); + + rand_walker.start(d_start, d_coalesced_v, d_sizes); + + // update crt_out_degs: + // + vector_test_t d_crt_out_degs(num_paths, handle.get_stream()); + rand_walker.gather_from_coalesced( + d_coalesced_v, d_out_degs, d_sizes, d_crt_out_degs, max_depth, num_paths); + + col_indx_extract_t col_extractor{handle, + graph_view, + raw_const_ptr(d_crt_out_degs), + raw_const_ptr(d_sizes), + num_paths, + max_depth}; + + // typically given by random engine: + // + std::vector v_col_indx{1, 0, 0, 2}; + vector_test_t d_col_indx(num_paths, handle.get_stream()); + + raft::update_device(d_col_indx.data(), v_col_indx.data(), d_col_indx.size(), handle.get_stream()); + + vector_test_t d_next_v(num_paths, handle.get_stream()); + vector_test_t d_next_w(num_paths, handle.get_stream()); + + col_extractor(d_coalesced_v, d_col_indx, d_next_v, d_next_w); + + std::vector v_next_v(num_paths); + std::vector v_next_w(num_paths); + + raft::update_host(v_next_v.data(), raw_const_ptr(d_next_v), num_paths, handle.get_stream()); + raft::update_host(v_next_w.data(), raw_const_ptr(d_next_w), num_paths, handle.get_stream()); + + std::vector v_next_v_exp{4, 1, 5, 3}; + std::vector v_next_w_exp{2.1f, 0.1f, 7.1f, 5.1f}; + + EXPECT_EQ(v_next_v, v_next_v_exp); + EXPECT_EQ(v_next_w, v_next_w_exp); +} + +TEST_F(RandomWalksPrimsTest, SimpleGraphRndGenColIndx) +{ + using namespace cugraph::experimental::detail; + + using vertex_t = int32_t; + using edge_t = vertex_t; + using weight_t = float; + using index_t = vertex_t; + using real_t = float; + using seed_t = long; + + using random_engine_t = rrandom_gen_t; + + raft::handle_t handle{}; + + edge_t num_edges = 8; + vertex_t num_vertices = 6; + + std::vector v_src{0, 1, 1, 2, 2, 2, 3, 4}; + std::vector v_dst{1, 3, 4, 0, 1, 3, 5, 5}; + std::vector v_w{0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1}; + + auto graph = cugraph::test::make_graph( + handle, v_src, v_dst, std::optional>{v_w}, num_vertices, num_edges); + + auto graph_view = graph.view(); + + edge_t const* offsets = graph_view.get_matrix_partition_view().get_offsets(); + vertex_t const* indices = graph_view.get_matrix_partition_view().get_indices(); + weight_t const* values = *(graph_view.get_matrix_partition_view().get_weights()); + + index_t num_paths = 4; + index_t max_depth = 3; + index_t total_sz = num_paths * max_depth; + + std::vector v_coalesced(total_sz, -1); + std::vector w_coalesced(total_sz - num_paths, -1); + + vector_test_t d_coalesced_v(total_sz, handle.get_stream()); + vector_test_t d_coalesced_w(total_sz - num_paths, handle.get_stream()); + + raft::update_device( + d_coalesced_v.data(), v_coalesced.data(), d_coalesced_v.size(), handle.get_stream()); + raft::update_device( + d_coalesced_w.data(), w_coalesced.data(), d_coalesced_w.size(), handle.get_stream()); + + std::vector v_start{1, 0, 4, 2}; + vector_test_t d_start(num_paths, handle.get_stream()); + + raft::update_device(d_start.data(), v_start.data(), d_start.size(), handle.get_stream()); + + vector_test_t d_sizes(num_paths, handle.get_stream()); + + random_walker_t rand_walker{handle, graph_view, num_paths, max_depth}; + + auto const& d_out_degs = rand_walker.get_out_degs(); + + rand_walker.start(d_start, d_coalesced_v, d_sizes); + + // update crt_out_degs: + // + vector_test_t d_crt_out_degs(num_paths, handle.get_stream()); + rand_walker.gather_from_coalesced( + d_coalesced_v, d_out_degs, d_sizes, d_crt_out_degs, max_depth, num_paths); + + // random engine generated: + // + vector_test_t d_col_indx(num_paths, handle.get_stream()); + vector_test_t d_random(num_paths, handle.get_stream()); + + seed_t seed = static_cast(std::time(nullptr)); + random_engine_t rgen(handle, num_paths, d_random, d_crt_out_degs, seed); + rgen.generate_col_indices(d_col_indx); + + bool all_indices_within_degs = check_col_indices(handle, d_crt_out_degs, d_col_indx, num_paths); + + ASSERT_TRUE(all_indices_within_degs); +} + +TEST_F(RandomWalksPrimsTest, SimpleGraphUpdatePathSizes) +{ + using namespace cugraph::experimental::detail; + + using vertex_t = int32_t; + using edge_t = vertex_t; + using weight_t = float; + using index_t = vertex_t; + using real_t = float; + using seed_t = long; + + using random_engine_t = rrandom_gen_t; + + raft::handle_t handle{}; + + edge_t num_edges = 8; + vertex_t num_vertices = 6; + + std::vector v_src{0, 1, 1, 2, 2, 2, 3, 4}; + std::vector v_dst{1, 3, 4, 0, 1, 3, 5, 5}; + std::vector v_w{0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1}; + + auto graph = cugraph::test::make_graph( + handle, v_src, v_dst, std::optional>{v_w}, num_vertices, num_edges); + + auto graph_view = graph.view(); + + edge_t const* offsets = graph_view.get_matrix_partition_view().get_offsets(); + vertex_t const* indices = graph_view.get_matrix_partition_view().get_indices(); + weight_t const* values = *(graph_view.get_matrix_partition_view().get_weights()); + + index_t num_paths = 4; + index_t max_depth = 3; + index_t total_sz = num_paths * max_depth; + + std::vector v_coalesced(total_sz, -1); + std::vector w_coalesced(total_sz - num_paths, -1); + + vector_test_t d_coalesced_v(total_sz, handle.get_stream()); + vector_test_t d_coalesced_w(total_sz - num_paths, handle.get_stream()); + + raft::update_device( + d_coalesced_v.data(), v_coalesced.data(), d_coalesced_v.size(), handle.get_stream()); + raft::update_device( + d_coalesced_w.data(), w_coalesced.data(), d_coalesced_w.size(), handle.get_stream()); + + std::vector v_start{1, 0, 4, 2}; + vector_test_t d_start(num_paths, handle.get_stream()); + + raft::update_device(d_start.data(), v_start.data(), d_start.size(), handle.get_stream()); + + vector_test_t d_sizes(num_paths, handle.get_stream()); + + random_walker_t rand_walker{handle, graph_view, num_paths, max_depth}; + + auto const& d_out_degs = rand_walker.get_out_degs(); + + rand_walker.start(d_start, d_coalesced_v, d_sizes); + + // Fixed set of out-degs, as opposed to have them generated by the algorithm. + // That's because I want to test a certain functionality in isolation + // + std::vector v_crt_out_degs{2, 0, 1, 0}; + vector_test_t d_crt_out_degs(num_paths, handle.get_stream()); + raft::update_device( + d_crt_out_degs.data(), v_crt_out_degs.data(), d_crt_out_degs.size(), handle.get_stream()); + + rand_walker.update_path_sizes(d_crt_out_degs, d_sizes); + + std::vector v_sizes(num_paths); + raft::update_host(v_sizes.data(), raw_const_ptr(d_sizes), num_paths, handle.get_stream()); + std::vector v_sizes_exp{2, 1, 2, 1}; + // i.e., corresponding 0-entries in crt-out-degs, don't get updated; + + EXPECT_EQ(v_sizes, v_sizes_exp); +} + +TEST_F(RandomWalksPrimsTest, SimpleGraphScatterUpdate) +{ + using namespace cugraph::experimental::detail; + + using vertex_t = int32_t; + using edge_t = vertex_t; + using weight_t = float; + using index_t = vertex_t; + + raft::handle_t handle{}; + + edge_t num_edges = 8; + vertex_t num_vertices = 6; + + std::vector v_src{0, 1, 1, 2, 2, 2, 3, 4}; + std::vector v_dst{1, 3, 4, 0, 1, 3, 5, 5}; + std::vector v_w{0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1}; + + auto graph = cugraph::test::make_graph( + handle, v_src, v_dst, std::optional>{v_w}, num_vertices, num_edges); + + auto graph_view = graph.view(); + + edge_t const* offsets = graph_view.get_matrix_partition_view().get_offsets(); + vertex_t const* indices = graph_view.get_matrix_partition_view().get_indices(); + weight_t const* values = *(graph_view.get_matrix_partition_view().get_weights()); + + index_t num_paths = 4; + index_t max_depth = 3; + index_t total_sz = num_paths * max_depth; + + std::vector v_coalesced(total_sz, -1); + std::vector w_coalesced(total_sz - num_paths, -1); + + vector_test_t d_coalesced_v(total_sz, handle.get_stream()); + vector_test_t d_coalesced_w(total_sz - num_paths, handle.get_stream()); + + raft::update_device( + d_coalesced_v.data(), v_coalesced.data(), d_coalesced_v.size(), handle.get_stream()); + raft::update_device( + d_coalesced_w.data(), w_coalesced.data(), d_coalesced_w.size(), handle.get_stream()); + + std::vector v_start{1, 0, 4, 2}; + vector_test_t d_start(num_paths, handle.get_stream()); + + raft::update_device(d_start.data(), v_start.data(), d_start.size(), handle.get_stream()); + + vector_test_t d_sizes(num_paths, handle.get_stream()); + + random_walker_t rand_walker{handle, graph_view, num_paths, max_depth}; + + auto const& d_out_degs = rand_walker.get_out_degs(); + + rand_walker.start(d_start, d_coalesced_v, d_sizes); + + // update crt_out_degs: + // + vector_test_t d_crt_out_degs(num_paths, handle.get_stream()); + rand_walker.gather_from_coalesced( + d_coalesced_v, d_out_degs, d_sizes, d_crt_out_degs, max_depth, num_paths); + + col_indx_extract_t col_extractor{handle, + graph_view, + raw_const_ptr(d_crt_out_degs), + raw_const_ptr(d_sizes), + num_paths, + max_depth}; + + // typically given by random engine: + // + std::vector v_col_indx{1, 0, 0, 2}; + vector_test_t d_col_indx(num_paths, handle.get_stream()); + + raft::update_device(d_col_indx.data(), v_col_indx.data(), d_col_indx.size(), handle.get_stream()); + + vector_test_t d_next_v(num_paths, handle.get_stream()); + vector_test_t d_next_w(num_paths, handle.get_stream()); + + col_extractor(d_coalesced_v, d_col_indx, d_next_v, d_next_w); + + rand_walker.update_path_sizes(d_crt_out_degs, d_sizes); + + // check start(): + // + { + std::vector v_coalesced_exp{1, -1, -1, 0, -1, -1, 4, -1, -1, 2, -1, -1}; + raft::update_host( + v_coalesced.data(), raw_const_ptr(d_coalesced_v), total_sz, handle.get_stream()); + EXPECT_EQ(v_coalesced, v_coalesced_exp); + } + + // check crt_out_degs: + // + { + std::vector v_crt_out_degs(num_paths); + raft::update_host( + v_crt_out_degs.data(), raw_const_ptr(d_crt_out_degs), num_paths, handle.get_stream()); + std::vector v_crt_out_degs_exp{2, 1, 1, 3}; + EXPECT_EQ(v_crt_out_degs, v_crt_out_degs_exp); + } + + // check paths sizes update: + // + { + std::vector v_sizes(num_paths); + raft::update_host(v_sizes.data(), raw_const_ptr(d_sizes), num_paths, handle.get_stream()); + std::vector v_sizes_exp{2, 2, 2, 2}; + // i.e., corresponding 0-entries in crt-out-degs, don't get updated; + EXPECT_EQ(v_sizes, v_sizes_exp); + } + + // check next step: + // + { + std::vector v_next_v(num_paths); + std::vector v_next_w(num_paths); + + raft::update_host(v_next_v.data(), raw_const_ptr(d_next_v), num_paths, handle.get_stream()); + raft::update_host(v_next_w.data(), raw_const_ptr(d_next_w), num_paths, handle.get_stream()); + + std::vector v_next_v_exp{4, 1, 5, 3}; + std::vector v_next_w_exp{2.1f, 0.1f, 7.1f, 5.1f}; + + EXPECT_EQ(v_next_v, v_next_v_exp); + EXPECT_EQ(v_next_w, v_next_w_exp); + } + + rand_walker.scatter_vertices(d_next_v, d_coalesced_v, d_crt_out_degs, d_sizes); + rand_walker.scatter_weights(d_next_w, d_coalesced_w, d_crt_out_degs, d_sizes); + + // check vertex/weight scatter: + // + { + raft::update_host( + v_coalesced.data(), raw_const_ptr(d_coalesced_v), total_sz, handle.get_stream()); + raft::update_host( + w_coalesced.data(), raw_const_ptr(d_coalesced_w), total_sz - num_paths, handle.get_stream()); + + std::vector v_coalesced_exp{1, 4, -1, 0, 1, -1, 4, 5, -1, 2, 3, -1}; + std::vector w_coalesced_exp{2.1, -1, 0.1, -1, 7.1, -1, 5.1, -1}; + + EXPECT_EQ(v_coalesced, v_coalesced_exp); + EXPECT_EQ(w_coalesced, w_coalesced_exp); + } +} + +TEST_F(RandomWalksPrimsTest, SimpleGraphCoalesceDefragment) +{ + using namespace cugraph::experimental::detail; + + using vertex_t = int32_t; + using edge_t = vertex_t; + using weight_t = float; + using index_t = vertex_t; + + raft::handle_t handle{}; + + edge_t num_edges = 8; + vertex_t num_vertices = 6; + + std::vector v_src{0, 1, 1, 2, 2, 2, 3, 4}; + std::vector v_dst{1, 3, 4, 0, 1, 3, 5, 5}; + std::vector v_w{0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1}; + + auto graph = cugraph::test::make_graph( + handle, v_src, v_dst, std::optional>{v_w}, num_vertices, num_edges); + + auto graph_view = graph.view(); + + edge_t const* offsets = graph_view.get_matrix_partition_view().get_offsets(); + vertex_t const* indices = graph_view.get_matrix_partition_view().get_indices(); + weight_t const* values = *(graph_view.get_matrix_partition_view().get_weights()); + + index_t num_paths = 4; + index_t max_depth = 3; + index_t total_sz = num_paths * max_depth; + + std::vector v_sizes{1, 2, 2, 1}; + vector_test_t d_sizes(num_paths, handle.get_stream()); + raft::update_device(d_sizes.data(), v_sizes.data(), d_sizes.size(), handle.get_stream()); + + std::vector v_coalesced(total_sz, -1); + v_coalesced[0] = 3; + v_coalesced[max_depth] = 5; + v_coalesced[max_depth + 1] = 2; + v_coalesced[2 * max_depth] = 4; + v_coalesced[2 * max_depth + 1] = 0; + v_coalesced[3 * max_depth] = 1; + + std::vector w_coalesced(total_sz - num_paths, -1); + w_coalesced[max_depth - 1] = 10.1; + w_coalesced[2 * max_depth - 2] = 11.2; + + vector_test_t d_coalesced_v(total_sz, handle.get_stream()); + vector_test_t d_coalesced_w(total_sz - num_paths, handle.get_stream()); + + raft::update_device( + d_coalesced_v.data(), v_coalesced.data(), d_coalesced_v.size(), handle.get_stream()); + raft::update_device( + d_coalesced_w.data(), w_coalesced.data(), d_coalesced_w.size(), handle.get_stream()); + + random_walker_t rand_walker{handle, graph_view, num_paths, max_depth}; + + rand_walker.stop(d_coalesced_v, d_coalesced_w, d_sizes); + + // check vertex/weight defragment: + // + { + v_coalesced.resize(d_coalesced_v.size()); + w_coalesced.resize(d_coalesced_w.size()); + + raft::update_host( + v_coalesced.data(), raw_const_ptr(d_coalesced_v), d_coalesced_v.size(), handle.get_stream()); + raft::update_host( + w_coalesced.data(), raw_const_ptr(d_coalesced_w), d_coalesced_w.size(), handle.get_stream()); + + std::vector v_coalesced_exp{3, 5, 2, 4, 0, 1}; + std::vector w_coalesced_exp{10.1, 11.2}; + + EXPECT_EQ(v_coalesced, v_coalesced_exp); + EXPECT_EQ(w_coalesced, w_coalesced_exp); + } +} + +TEST_F(RandomWalksPrimsTest, SimpleGraphRandomWalk) +{ + using vertex_t = int32_t; + using edge_t = vertex_t; + using weight_t = float; + using index_t = vertex_t; + + raft::handle_t handle{}; + + edge_t num_edges = 8; + vertex_t num_vertices = 6; + + std::vector v_src{0, 1, 1, 2, 2, 2, 3, 4}; + std::vector v_dst{1, 3, 4, 0, 1, 3, 5, 5}; + std::vector v_w{0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1}; + + auto graph = cugraph::test::make_graph( + handle, v_src, v_dst, std::optional>{v_w}, num_vertices, num_edges); + + auto graph_view = graph.view(); + + edge_t const* offsets = graph_view.get_matrix_partition_view().get_offsets(); + vertex_t const* indices = graph_view.get_matrix_partition_view().get_indices(); + weight_t const* values = *(graph_view.get_matrix_partition_view().get_weights()); + + std::vector v_ro(num_vertices + 1); + std::vector v_ci(num_edges); + std::vector v_vals(num_edges); + + raft::update_host(v_ro.data(), offsets, v_ro.size(), handle.get_stream()); + raft::update_host(v_ci.data(), indices, v_ci.size(), handle.get_stream()); + raft::update_host(v_vals.data(), values, v_vals.size(), handle.get_stream()); + + std::vector v_start{1, 0, 4, 2}; + vector_test_t d_v_start(v_start.size(), handle.get_stream()); + raft::update_device(d_v_start.data(), v_start.data(), d_v_start.size(), handle.get_stream()); + + index_t num_paths = v_start.size(); + index_t max_depth = 5; + + // 0-copy const device view: + // + detail::device_const_vector_view d_start_view{d_v_start.data(), num_paths}; + auto quad = detail::random_walks_impl(handle, graph_view, d_start_view, max_depth); + + auto& d_coalesced_v = std::get<0>(quad); + auto& d_coalesced_w = std::get<1>(quad); + auto& d_sizes = std::get<2>(quad); + auto seed0 = std::get<3>(quad); + + bool test_all_paths = + cugraph::test::host_check_rw_paths(handle, graph_view, d_coalesced_v, d_coalesced_w, d_sizes); + + if (!test_all_paths) std::cout << "starting seed on failure: " << seed0 << '\n'; + + ASSERT_TRUE(test_all_paths); +} + +TEST(RandomWalksQuery, GraphRWQueryOffsets) +{ + using vertex_t = int32_t; + using edge_t = vertex_t; + using weight_t = float; + using index_t = vertex_t; + + raft::handle_t handle{}; + + edge_t num_edges = 8; + vertex_t num_vertices = 6; + + std::vector v_src{0, 1, 1, 2, 2, 2, 3, 4}; + std::vector v_dst{1, 3, 4, 0, 1, 3, 5, 5}; + std::vector v_w{0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1}; + + auto graph = cugraph::test::make_graph( + handle, v_src, v_dst, std::optional>{v_w}, num_vertices, num_edges); + + auto graph_view = graph.view(); + + edge_t const* offsets = graph_view.get_matrix_partition_view().get_offsets(); + vertex_t const* indices = graph_view.get_matrix_partition_view().get_indices(); + weight_t const* values = *(graph_view.get_matrix_partition_view().get_weights()); + + std::vector v_ro(num_vertices + 1); + std::vector v_ci(num_edges); + std::vector v_vals(num_edges); + + raft::update_host(v_ro.data(), offsets, v_ro.size(), handle.get_stream()); + raft::update_host(v_ci.data(), indices, v_ci.size(), handle.get_stream()); + raft::update_host(v_vals.data(), values, v_vals.size(), handle.get_stream()); + + std::vector v_start{1, 0, 4, 2}; + vector_test_t d_v_start(v_start.size(), handle.get_stream()); + raft::update_device(d_v_start.data(), v_start.data(), d_v_start.size(), handle.get_stream()); + + index_t num_paths = v_start.size(); + index_t max_depth = 5; + + // 0-copy const device view: + // + detail::device_const_vector_view d_start_view{d_v_start.data(), num_paths}; + auto quad = detail::random_walks_impl(handle, graph_view, d_start_view, max_depth); + + auto& d_v_sizes = std::get<2>(quad); + auto seed0 = std::get<3>(quad); + + auto triplet = query_rw_sizes_offsets(handle, num_paths, detail::raw_const_ptr(d_v_sizes)); + + auto& d_v_offsets = std::get<0>(triplet); + auto& d_w_sizes = std::get<1>(triplet); + auto& d_w_offsets = std::get<2>(triplet); + + bool test_paths_sz = + cugraph::test::host_check_query_rw(handle, d_v_sizes, d_v_offsets, d_w_sizes, d_w_offsets); + + if (!test_paths_sz) std::cout << "starting seed on failure: " << seed0 << '\n'; + + ASSERT_TRUE(test_paths_sz); +} + +TEST(RandomWalksSpecialCase, SingleRandomWalk) +{ + using vertex_t = int32_t; + using edge_t = vertex_t; + using weight_t = float; + using index_t = vertex_t; + + raft::handle_t handle{}; + + edge_t num_edges = 8; + vertex_t num_vertices = 6; + + std::vector v_src{0, 1, 1, 2, 2, 2, 3, 4}; + std::vector v_dst{1, 3, 4, 0, 1, 3, 5, 5}; + std::vector v_w{0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1}; + + auto graph = cugraph::test::make_graph( + handle, v_src, v_dst, std::optional>{v_w}, num_vertices, num_edges); + + auto graph_view = graph.view(); + + edge_t const* offsets = graph_view.get_matrix_partition_view().get_offsets(); + vertex_t const* indices = graph_view.get_matrix_partition_view().get_indices(); + weight_t const* values = *(graph_view.get_matrix_partition_view().get_weights()); + + std::vector v_ro(num_vertices + 1); + std::vector v_ci(num_edges); + std::vector v_vals(num_edges); + + raft::update_host(v_ro.data(), offsets, v_ro.size(), handle.get_stream()); + raft::update_host(v_ci.data(), indices, v_ci.size(), handle.get_stream()); + raft::update_host(v_vals.data(), values, v_vals.size(), handle.get_stream()); + + std::vector v_start{2}; + vector_test_t d_v_start(v_start.size(), handle.get_stream()); + raft::update_device(d_v_start.data(), v_start.data(), d_v_start.size(), handle.get_stream()); + + index_t num_paths = v_start.size(); + index_t max_depth = 5; + + // 0-copy const device view: + // + detail::device_const_vector_view d_start_view{d_v_start.data(), num_paths}; + auto quad = detail::random_walks_impl(handle, graph_view, d_start_view, max_depth); + + auto& d_coalesced_v = std::get<0>(quad); + auto& d_coalesced_w = std::get<1>(quad); + auto& d_sizes = std::get<2>(quad); + auto seed0 = std::get<3>(quad); + + bool test_all_paths = + cugraph::test::host_check_rw_paths(handle, graph_view, d_coalesced_v, d_coalesced_w, d_sizes); + + if (!test_all_paths) std::cout << "starting seed on failure: " << seed0 << '\n'; + + ASSERT_TRUE(test_all_paths); +} + +TEST(RandomWalksSpecialCase, UnweightedGraph) +{ + using vertex_t = int32_t; + using edge_t = vertex_t; + using weight_t = float; + using index_t = vertex_t; + + raft::handle_t handle{}; + + edge_t num_edges = 8; + vertex_t num_vertices = 6; + + std::vector v_src{0, 1, 1, 2, 2, 2, 3, 4}; + std::vector v_dst{1, 3, 4, 0, 1, 3, 5, 5}; + + auto graph = cugraph::test::make_graph( + handle, v_src, v_dst, std::nullopt, num_vertices, num_edges); // un-weighted + + auto graph_view = graph.view(); + + edge_t const* offsets = graph_view.get_matrix_partition_view().get_offsets(); + vertex_t const* indices = graph_view.get_matrix_partition_view().get_indices(); + ASSERT_TRUE(graph_view.get_matrix_partition_view().get_weights().has_value() == false); + + std::vector v_ro(num_vertices + 1); + std::vector v_ci(num_edges); + + raft::update_host(v_ro.data(), offsets, v_ro.size(), handle.get_stream()); + raft::update_host(v_ci.data(), indices, v_ci.size(), handle.get_stream()); + + std::vector v_start{2}; + vector_test_t d_v_start(v_start.size(), handle.get_stream()); + raft::update_device(d_v_start.data(), v_start.data(), d_v_start.size(), handle.get_stream()); + + index_t num_paths = v_start.size(); + index_t max_depth = 5; + + // 0-copy const device view: + // + detail::device_const_vector_view d_start_view{d_v_start.data(), num_paths}; + auto quad = detail::random_walks_impl(handle, graph_view, d_start_view, max_depth); + + auto& d_coalesced_v = std::get<0>(quad); + auto& d_coalesced_w = std::get<1>(quad); + auto& d_sizes = std::get<2>(quad); + auto seed0 = std::get<3>(quad); + + bool test_all_paths = + cugraph::test::host_check_rw_paths(handle, graph_view, d_coalesced_v, d_coalesced_w, d_sizes); + + if (!test_all_paths) std::cout << "starting seed on failure: " << seed0 << '\n'; + + ASSERT_TRUE(test_all_paths); +} + +TEST(RandomWalksPadded, SimpleGraph) +{ + using vertex_t = int32_t; + using edge_t = vertex_t; + using weight_t = float; + using index_t = vertex_t; + + raft::handle_t handle{}; + + edge_t num_edges = 8; + vertex_t num_vertices = 6; + + std::vector v_src{0, 1, 1, 2, 2, 2, 3, 4}; + std::vector v_dst{1, 3, 4, 0, 1, 3, 5, 5}; + std::vector v_w{0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1}; + + auto graph = cugraph::test::make_graph( + handle, v_src, v_dst, std::optional>{v_w}, num_vertices, num_edges); + + auto graph_view = graph.view(); + + edge_t const* offsets = graph_view.get_matrix_partition_view().get_offsets(); + vertex_t const* indices = graph_view.get_matrix_partition_view().get_indices(); + weight_t const* values = *(graph_view.get_matrix_partition_view().get_weights()); + + std::vector v_ro(num_vertices + 1); + std::vector v_ci(num_edges); + std::vector v_vals(num_edges); + + raft::update_host(v_ro.data(), offsets, v_ro.size(), handle.get_stream()); + raft::update_host(v_ci.data(), indices, v_ci.size(), handle.get_stream()); + raft::update_host(v_vals.data(), values, v_vals.size(), handle.get_stream()); + + std::vector v_start{2}; + vector_test_t d_v_start(v_start.size(), handle.get_stream()); + raft::update_device(d_v_start.data(), v_start.data(), d_v_start.size(), handle.get_stream()); + + index_t num_paths = v_start.size(); + index_t max_depth = 5; + + // 0-copy const device view: + // + detail::device_const_vector_view d_start_view{d_v_start.data(), num_paths}; + bool use_padding{true}; + auto quad = detail::random_walks_impl(handle, graph_view, d_start_view, max_depth, use_padding); + + auto& d_coalesced_v = std::get<0>(quad); + auto& d_coalesced_w = std::get<1>(quad); + auto& d_sizes = std::get<2>(quad); + auto seed0 = std::get<3>(quad); + + ASSERT_TRUE(d_sizes.size() == 0); + + bool test_all_paths = cugraph::test::host_check_rw_paths( + handle, graph_view, d_coalesced_v, d_coalesced_w, d_sizes, num_paths); + + if (!test_all_paths) std::cout << "starting seed on failure: " << seed0 << '\n'; + + ASSERT_TRUE(test_all_paths); +} + +TEST(RandomWalksUtility, PathsToCOO) +{ + using namespace cugraph::experimental::detail; + + using vertex_t = int32_t; + using edge_t = vertex_t; + using weight_t = float; + using index_t = vertex_t; + + raft::handle_t handle{}; + + std::vector v_sizes{2, 1, 3, 5, 1}; + std::vector v_coalesced{5, 3, 4, 9, 0, 1, 6, 2, 7, 3, 2, 5}; + std::vector w_coalesced{0.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1}; + + auto num_paths = v_sizes.size(); + auto total_sz = v_coalesced.size(); + auto num_edges = w_coalesced.size(); + + ASSERT_TRUE(num_edges == total_sz - num_paths); + + vector_test_t d_coalesced_v(total_sz, handle.get_stream()); + vector_test_t d_sizes(num_paths, handle.get_stream()); + + raft::update_device( + d_coalesced_v.data(), v_coalesced.data(), d_coalesced_v.size(), handle.get_stream()); + raft::update_device(d_sizes.data(), v_sizes.data(), d_sizes.size(), handle.get_stream()); + + index_t coalesced_v_sz = d_coalesced_v.size(); + + auto tpl_coo_offsets = convert_paths_to_coo(handle, + coalesced_v_sz, + static_cast(num_paths), + d_coalesced_v.release(), + d_sizes.release()); + + auto&& d_src = std::move(std::get<0>(tpl_coo_offsets)); + auto&& d_dst = std::move(std::get<1>(tpl_coo_offsets)); + auto&& d_offsets = std::move(std::get<2>(tpl_coo_offsets)); + + ASSERT_TRUE(d_src.size() == num_edges); + ASSERT_TRUE(d_dst.size() == num_edges); + + std::vector v_src(num_edges, 0); + std::vector v_dst(num_edges, 0); + std::vector v_offsets(d_offsets.size(), 0); + + raft::update_host(v_src.data(), raw_const_ptr(d_src), d_src.size(), handle.get_stream()); + raft::update_host(v_dst.data(), raw_const_ptr(d_dst), d_dst.size(), handle.get_stream()); + raft::update_host( + v_offsets.data(), raw_const_ptr(d_offsets), d_offsets.size(), handle.get_stream()); + + std::vector v_src_exp{5, 9, 0, 6, 2, 7, 3}; + std::vector v_dst_exp{3, 0, 1, 2, 7, 3, 2}; + std::vector v_offsets_exp{0, 1, 3}; + + EXPECT_EQ(v_src, v_src_exp); + EXPECT_EQ(v_dst, v_dst_exp); + EXPECT_EQ(v_offsets, v_offsets_exp); +} diff --git a/cpp/tests/serialization/un_serialize_test.cpp b/cpp/tests/serialization/un_serialize_test.cpp new file mode 100644 index 00000000000..e65d37fd77a --- /dev/null +++ b/cpp/tests/serialization/un_serialize_test.cpp @@ -0,0 +1,179 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include "cuda_profiler_api.h" + +#include +#include + +#include +#include + +#include + +TEST(SerializationTest, GraphSerUnser) +{ + using namespace cugraph::serializer; + + using vertex_t = int32_t; + using edge_t = vertex_t; + using weight_t = float; + using index_t = vertex_t; + + raft::handle_t handle{}; + + edge_t num_edges = 8; + vertex_t num_vertices = 6; + + std::vector v_src{0, 1, 1, 2, 2, 2, 3, 4}; + std::vector v_dst{1, 3, 4, 0, 1, 3, 5, 5}; + std::vector v_w{0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1}; + + auto graph = cugraph::test::make_graph( + handle, v_src, v_dst, std::optional>{v_w}, num_vertices, num_edges); + + auto pair_sz = serializer_t::get_device_graph_sz_bytes(graph); + auto total_ser_sz = pair_sz.first + pair_sz.second; + + serializer_t ser(handle, total_ser_sz); + serializer_t::graph_meta_t graph_meta{}; + ser.serialize(graph, graph_meta); + + pair_sz = serializer_t::get_device_graph_sz_bytes(graph_meta); + auto post_ser_sz = pair_sz.first + pair_sz.second; + + EXPECT_EQ(total_ser_sz, post_ser_sz); + + auto graph_copy = ser.unserialize(pair_sz.first, pair_sz.second); + + auto pair = cugraph::test::compare_graphs(handle, graph, graph_copy); + if (pair.first == false) std::cerr << "Test failed with " << pair.second << ".\n"; + + ASSERT_TRUE(pair.first); +} + +TEST(SerializationTest, GraphDecoupledSerUnser) +{ + using namespace cugraph::serializer; + + using vertex_t = int32_t; + using edge_t = vertex_t; + using weight_t = double; + using index_t = vertex_t; + + raft::handle_t handle{}; + + edge_t num_edges = 8; + vertex_t num_vertices = 6; + + std::vector v_src{0, 1, 1, 2, 2, 2, 3, 4}; + std::vector v_dst{1, 3, 4, 0, 1, 3, 5, 5}; + std::vector v_w{0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1}; + + auto graph = cugraph::test::make_graph( + handle, v_src, v_dst, std::optional>{v_w}, num_vertices, num_edges); + + auto pair_sz = serializer_t::get_device_graph_sz_bytes(graph); + auto total_ser_sz = pair_sz.first + pair_sz.second; + + // use the following buffer to simulate communication between + // sender and reciever of the serialization: + // + rmm::device_uvector d_storage_comm(0, handle.get_stream()); + + { + serializer_t ser(handle, total_ser_sz); + serializer_t::graph_meta_t graph_meta{}; + ser.serialize(graph, graph_meta); + + pair_sz = serializer_t::get_device_graph_sz_bytes(graph_meta); + auto post_ser_sz = pair_sz.first + pair_sz.second; + + EXPECT_EQ(total_ser_sz, post_ser_sz); + + d_storage_comm.resize(total_ser_sz, handle.get_stream()); + raft::copy(d_storage_comm.data(), ser.get_storage(), total_ser_sz, handle.get_stream()); + } + + { + serializer_t ser(handle, d_storage_comm.data()); + + auto graph_copy = ser.unserialize(pair_sz.first, pair_sz.second); + + auto pair = cugraph::test::compare_graphs(handle, graph, graph_copy); + if (pair.first == false) std::cerr << "Test failed with " << pair.second << ".\n"; + + ASSERT_TRUE(pair.first); + } +} + +TEST(SerializationTest, UnweightedGraphDecoupledSerUnser) +{ + using namespace cugraph::serializer; + + using vertex_t = int32_t; + using edge_t = vertex_t; + using weight_t = double; + using index_t = vertex_t; + + raft::handle_t handle{}; + + edge_t num_edges = 8; + vertex_t num_vertices = 6; + + std::vector v_src{0, 1, 1, 2, 2, 2, 3, 4}; + std::vector v_dst{1, 3, 4, 0, 1, 3, 5, 5}; + + auto graph = cugraph::test::make_graph( + handle, v_src, v_dst, std::nullopt, num_vertices, num_edges); + + ASSERT_TRUE(graph.view().get_matrix_partition_view().get_weights().has_value() == false); + + auto pair_sz = serializer_t::get_device_graph_sz_bytes(graph); + auto total_ser_sz = pair_sz.first + pair_sz.second; + + // use the following buffer to simulate communication between + // sender and reciever of the serialization: + // + rmm::device_uvector d_storage_comm(0, handle.get_stream()); + + { + serializer_t ser(handle, total_ser_sz); + serializer_t::graph_meta_t graph_meta{}; + ser.serialize(graph, graph_meta); + + pair_sz = serializer_t::get_device_graph_sz_bytes(graph_meta); + auto post_ser_sz = pair_sz.first + pair_sz.second; + + EXPECT_EQ(total_ser_sz, post_ser_sz); + + d_storage_comm.resize(total_ser_sz, handle.get_stream()); + raft::copy(d_storage_comm.data(), ser.get_storage(), total_ser_sz, handle.get_stream()); + } + + { + serializer_t ser(handle, d_storage_comm.data()); + + auto graph_copy = ser.unserialize(pair_sz.first, pair_sz.second); + + ASSERT_TRUE(graph_copy.view().get_matrix_partition_view().get_weights().has_value() == false); + + auto pair = cugraph::test::compare_graphs(handle, graph, graph_copy); + if (pair.first == false) std::cerr << "Test failed with " << pair.second << ".\n"; + + ASSERT_TRUE(pair.first); + } +} diff --git a/cpp/tests/traversal/bfs_ref.h b/cpp/tests/traversal/bfs_ref.h index a32b2f99787..5efdce818e7 100644 --- a/cpp/tests/traversal/bfs_ref.h +++ b/cpp/tests/traversal/bfs_ref.h @@ -1,5 +1,5 @@ /* - * 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. @@ -21,7 +21,7 @@ #include template -void populate_neighbors(VT *indices, ET *offsets, VT w, std::vector &neighbors) +void populate_neighbors(VT* indices, ET* offsets, VT w, std::vector& neighbors) { ET edge_start = offsets[w]; ET edge_end = offsets[w + 1]; @@ -31,14 +31,14 @@ void populate_neighbors(VT *indices, ET *offsets, VT w, std::vector &neighbo // This implements the BFS based on (Brandes, 2001) for shortest path counting template -void ref_bfs(VT *indices, - ET *offsets, +void ref_bfs(VT* indices, + ET* offsets, VT const number_of_vertices, - std::queue &Q, - std::stack &S, - std::vector &dist, - std::vector> &pred, - std::vector &sigmas, + std::queue& Q, + std::stack& S, + std::vector& dist, + std::vector>& pred, + std::vector& sigmas, VT source) { std::vector neighbors; diff --git a/cpp/tests/traversal/bfs_test.cu b/cpp/tests/traversal/bfs_test.cu index d90da4367a0..b0da605a0a0 100644 --- a/cpp/tests/traversal/bfs_test.cu +++ b/cpp/tests/traversal/bfs_test.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -19,7 +19,7 @@ #include #include -#include +#include #include @@ -46,7 +46,7 @@ // C++ Reference Implementation // ============================================================================ template -bool compare_close(const T &a, const T &b, const precision_t epsilon, precision_t zero_threshold) +bool compare_close(const T& a, const T& b, const precision_t epsilon, precision_t zero_threshold) { return ((zero_threshold > a && zero_threshold > b)) || (a >= b * (1.0 - epsilon)) && (a <= b * (1.0 + epsilon)); @@ -59,9 +59,9 @@ typedef struct BFS_Usecase_t { std::string config_; // Path to graph file std::string file_path_; // Complete path to graph using dataset_root_dir int source_; // Starting point from the traversal - BFS_Usecase_t(const std::string &config, int source) : config_(config), source_(source) + BFS_Usecase_t(const std::string& config, int source) : config_(config), source_(source) { - const std::string &rapidsDatasetRootDir = cugraph::test::get_rapids_dataset_root_dir(); + const std::string& rapidsDatasetRootDir = cugraph::test::get_rapids_dataset_root_dir(); if ((config_ != "") && (config_[0] != '/')) { file_path_ = rapidsDatasetRootDir + "/" + config_; } else { @@ -86,7 +86,7 @@ class Tests_BFS : public ::testing::TestWithParam { // WT edge weight data type // return_sp_counter should BFS return shortest path countner template - void run_current_test(const BFS_Usecase &configuration) + void run_current_test(const BFS_Usecase& configuration) { // Step 1: Construction of the graph based on configuration VT number_of_vertices; @@ -95,8 +95,8 @@ class Tests_BFS : public ::testing::TestWithParam { auto csr = cugraph::test::generate_graph_csr_from_mm(directed, configuration.file_path_); cudaDeviceSynchronize(); - cugraph::GraphCSRView G = csr->view(); - G.prop.directed = directed; + cugraph::legacy::GraphCSRView G = csr->view(); + G.prop.directed = directed; ASSERT_TRUE(configuration.source_ >= 0 && (VT)configuration.source_ < G.number_of_vertices) << "Starting sources should be >= 0 and" @@ -174,7 +174,7 @@ class Tests_BFS : public ::testing::TestWithParam { // that the predecessor obtained with the GPU implementation is one of the // predecessors obtained during the C++ BFS traversal VT pred = cugraph_pred[i]; // It could be equal to -1 if the node is never reached - constexpr VT invalid_vid = cugraph::invalid_vertex_id::value; + constexpr VT invalid_vid = cugraph::legacy::invalid_vertex_id::value; if (pred == invalid_vid) { EXPECT_TRUE(ref_bfs_pred[i].empty()) << "[MISMATCH][PREDECESSOR] vaid = " << i << " cugraph had not predecessor," @@ -224,13 +224,13 @@ TEST_P(Tests_BFS, CheckInt64_SP_COUNTER) run_current_test(GetParam()); } -INSTANTIATE_TEST_CASE_P(simple_test, - Tests_BFS, - ::testing::Values(BFS_Usecase("test/datasets/karate.mtx", 0), - BFS_Usecase("test/datasets/polbooks.mtx", 0), - BFS_Usecase("test/datasets/netscience.mtx", 0), - BFS_Usecase("test/datasets/netscience.mtx", 100), - BFS_Usecase("test/datasets/wiki2003.mtx", 1000), - BFS_Usecase("test/datasets/wiki-Talk.mtx", 1000))); +INSTANTIATE_TEST_SUITE_P(simple_test, + Tests_BFS, + ::testing::Values(BFS_Usecase("test/datasets/karate.mtx", 0), + BFS_Usecase("test/datasets/polbooks.mtx", 0), + BFS_Usecase("test/datasets/netscience.mtx", 0), + BFS_Usecase("test/datasets/netscience.mtx", 100), + BFS_Usecase("test/datasets/wiki2003.mtx", 1000), + BFS_Usecase("test/datasets/wiki-Talk.mtx", 1000))); CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/traversal/sssp_test.cu b/cpp/tests/traversal/sssp_test.cu index ea56d1d79cb..e221e7e3445 100644 --- a/cpp/tests/traversal/sssp_test.cu +++ b/cpp/tests/traversal/sssp_test.cu @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. * * NVIDIA CORPORATION and its licensors retain all intellectual property * and proprietary rights in and to this software, related documentation @@ -13,9 +13,9 @@ #include #include -#include #include -#include +#include +#include #include @@ -255,14 +255,14 @@ class Tests_SSSP : public ::testing::TestWithParam { ASSERT_TRUE(0); } - cugraph::GraphCOOView G_coo( + cugraph::legacy::GraphCOOView G_coo( &cooRowInd[0], &cooColInd[0], (DoRandomWeights ? &cooVal[0] : nullptr), num_vertices, num_edges); - auto G_unique = cugraph::coo_to_csr(G_coo); - cugraph::GraphCSRView G = G_unique->view(); + auto G_unique = cugraph::coo_to_csr(G_coo); + cugraph::legacy::GraphCSRView G = G_unique->view(); cudaDeviceSynchronize(); std::vector dist_vec; @@ -425,10 +425,10 @@ TEST_P(Tests_SSSP, CheckFP64_RANDOM_DIST_PREDS) // --gtest_filter=*simple_test* -INSTANTIATE_TEST_CASE_P(simple_test, - Tests_SSSP, - ::testing::Values(SSSP_Usecase(MTX, "test/datasets/dblp.mtx", 100), - SSSP_Usecase(MTX, "test/datasets/wiki2003.mtx", 100000), - SSSP_Usecase(MTX, "test/datasets/karate.mtx", 1))); +INSTANTIATE_TEST_SUITE_P(simple_test, + Tests_SSSP, + ::testing::Values(SSSP_Usecase(MTX, "test/datasets/dblp.mtx", 100), + SSSP_Usecase(MTX, "test/datasets/wiki2003.mtx", 100000), + SSSP_Usecase(MTX, "test/datasets/karate.mtx", 1))); CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/traversal/tsp_test.cu b/cpp/tests/traversal/tsp_test.cu new file mode 100644 index 00000000000..e00f2949af5 --- /dev/null +++ b/cpp/tests/traversal/tsp_test.cu @@ -0,0 +1,246 @@ +/* + * Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved. + * + * NVIDIA CORPORATION and its licensors retain all intellectual property + * and proprietary rights in and to this software, related documentation + * and any modifications thereto. Any use, reproduction, disclosure or + * distribution of this software and related documentation without an express + * license agreement from NVIDIA CORPORATION is strictly prohibited. + * + */ + +// TSP solver tests +// Author: Hugo Linsenmaier hlinsenmaier@nvidia.com + +/* + * Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved. + * + * NVIDIA CORPORATION and its licensors retain all intellectual property + * and proprietary rights in and to this software, related documentation + * and any modifications thereto. Any use, reproduction, disclosure or + * distribution of this software and related documentation without an express + * license agreement from NVIDIA CORPORATION is strictly prohibited. + * + */ + +// TSP solver tests +// Author: Hugo Linsenmaier hlinsenmaier@nvidia.com + +#include +#include +#include + +#include +#include + +#include + +#include +#include + +#include + +#include +#include +#include + +typedef struct Tsp_Usecase_t { + std::string tsp_file; + float ref_cost; + Tsp_Usecase_t(const std::string& a, const float c) + { + // assume relative paths are relative to RAPIDS_DATASET_ROOT_DIR + const std::string& rapidsDatasetRootDir = cugraph::test::get_rapids_dataset_root_dir(); + if ((a != "") && (a[0] != '/')) { + tsp_file = rapidsDatasetRootDir + "/" + a; + } else { + tsp_file = a; + } + ref_cost = c; + } + Tsp_Usecase_t& operator=(const Tsp_Usecase_t& rhs) + { + tsp_file = rhs.tsp_file; + ref_cost = rhs.ref_cost; + return *this; + } +} Tsp_Usecase; + +static std::vector euc_2d{ + {"tsplib/datasets/a280.tsp", 2579}, {"tsplib/datasets/berlin52.tsp", 7542}, + {"tsplib/datasets/bier127.tsp", 118282}, {"tsplib/datasets/ch130.tsp", 6110}, + {"tsplib/datasets/ch150.tsp", 6528}, {"tsplib/datasets/d1291.tsp", 50801}, + {"tsplib/datasets/d1655.tsp", 62128}, {"tsplib/datasets/d198.tsp", 15780}, + {"tsplib/datasets/d2103.tsp", 80450}, {"tsplib/datasets/d493.tsp", 35002}, + {"tsplib/datasets/d657.tsp", 48912}, {"tsplib/datasets/eil101.tsp", 629}, + {"tsplib/datasets/eil51.tsp", 426}, {"tsplib/datasets/eil76.tsp", 538}, + {"tsplib/datasets/fl1400.tsp", 20127}, {"tsplib/datasets/fl1577.tsp", 22249}, + {"tsplib/datasets/fl417.tsp", 11861}, {"tsplib/datasets/gil262.tsp", 2378}, + {"tsplib/datasets/kroA100.tsp", 21282}, {"tsplib/datasets/kroA150.tsp", 26524}, + {"tsplib/datasets/kroA200.tsp", 29368}, {"tsplib/datasets/kroB100.tsp", 22141}, + {"tsplib/datasets/kroB150.tsp", 26130}, {"tsplib/datasets/kroB200.tsp", 29437}, + {"tsplib/datasets/kroC100.tsp", 20749}, {"tsplib/datasets/kroD100.tsp", 21294}, + {"tsplib/datasets/kroE100.tsp", 22068}, {"tsplib/datasets/lin105.tsp", 14379}, + {"tsplib/datasets/lin318.tsp", 42029}, {"tsplib/datasets/nrw1379.tsp", 56638}, + {"tsplib/datasets/p654.tsp", 34643}, {"tsplib/datasets/pcb1173.tsp", 56892}, + {"tsplib/datasets/pcb442.tsp", 50778}, {"tsplib/datasets/pr1002.tsp", 259045}, + {"tsplib/datasets/pr107.tsp", 44303}, {"tsplib/datasets/pr136.tsp", 96772}, + {"tsplib/datasets/pr144.tsp", 58537}, {"tsplib/datasets/pr152.tsp", 73682}, + {"tsplib/datasets/pr226.tsp", 80369}, {"tsplib/datasets/pr264.tsp", 49135}, + {"tsplib/datasets/pr299.tsp", 48191}, {"tsplib/datasets/pr439.tsp", 107217}, + {"tsplib/datasets/pr76.tsp", 108159}, {"tsplib/datasets/rat195.tsp", 2323}, + {"tsplib/datasets/rat575.tsp", 6773}, {"tsplib/datasets/rat783.tsp", 8806}, + {"tsplib/datasets/rat99.tsp", 1211}, {"tsplib/datasets/rd100.tsp", 7910}, + {"tsplib/datasets/rd400.tsp", 15281}, {"tsplib/datasets/rl1323.tsp", 270199}, + {"tsplib/datasets/st70.tsp", 675}, {"tsplib/datasets/ts225.tsp", 126643}, + {"tsplib/datasets/tsp225.tsp", 3916}, {"tsplib/datasets/u1060.tsp", 224094}, + {"tsplib/datasets/u1432.tsp", 152970}, {"tsplib/datasets/u159.tsp", 42080}, + {"tsplib/datasets/u574.tsp", 36905}, {"tsplib/datasets/u724.tsp", 41910}, + {"tsplib/datasets/vm1084.tsp", 239297}, +}; + +struct Route { + std::vector cities; + std::vector x_pos; + std::vector y_pos; +}; + +class Tests_Tsp : public ::testing::TestWithParam { + public: + Tests_Tsp() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + virtual void SetUp() {} + virtual void TearDown() {} + + void run_current_test(const Tsp_Usecase& param) + { + const ::testing::TestInfo* const test_info = + ::testing::UnitTest::GetInstance()->current_test_info(); + std::stringstream ss; + std::string test_id = std::string(test_info->test_case_name()) + std::string(".") + + std::string(test_info->name()) + std::string("_") + + cugraph::test::getFileName(param.tsp_file) + std::string("_") + + ss.str().c_str(); + + float tol = 1E-1f; + HighResClock hr_clock; + double time_tmp; + Route input; + + std::cout << "File: " << param.tsp_file.c_str() << "\n"; + int nodes = load_tsp(param.tsp_file.c_str(), &input); + + // Device alloc + raft::handle_t const handle; + auto stream = handle.get_stream(); + rmm::device_uvector vertices(static_cast(nodes), stream); + rmm::device_uvector route(static_cast(nodes), stream); + rmm::device_uvector x_pos(static_cast(nodes), stream); + rmm::device_uvector y_pos(static_cast(nodes), stream); + + int* vtx_ptr = vertices.data(); + int* d_route = route.data(); + float* d_x_pos = x_pos.data(); + float* d_y_pos = y_pos.data(); + + CUDA_TRY(cudaMemcpy(vtx_ptr, input.cities.data(), sizeof(int) * nodes, cudaMemcpyHostToDevice)); + CUDA_TRY( + cudaMemcpy(d_x_pos, input.x_pos.data(), sizeof(float) * nodes, cudaMemcpyHostToDevice)); + CUDA_TRY( + cudaMemcpy(d_y_pos, input.y_pos.data(), sizeof(float) * nodes, cudaMemcpyHostToDevice)); + + // Default parameters + int restarts = 4096; + bool beam_search = true; + int k = 4; + int nstart = 0; + bool verbose = false; + + hr_clock.start(); + cudaDeviceSynchronize(); + cudaProfilerStart(); + + float final_cost = cugraph::traveling_salesperson( + handle, vtx_ptr, d_x_pos, d_y_pos, nodes, restarts, beam_search, k, nstart, verbose, d_route); + cudaProfilerStop(); + cudaDeviceSynchronize(); + hr_clock.stop(&time_tmp); + + std::vector h_route; + h_route.resize(nodes); + std::vector h_vertices; + h_vertices.resize(nodes); + CUDA_TRY(cudaMemcpy(h_route.data(), d_route, sizeof(int) * nodes, cudaMemcpyDeviceToHost)); + cudaDeviceSynchronize(); + CUDA_TRY(cudaMemcpy(h_vertices.data(), vtx_ptr, sizeof(int) * nodes, cudaMemcpyDeviceToHost)); + cudaDeviceSynchronize(); + + std::cout << "tsp_time: " << time_tmp << " us" << std::endl; + std::cout << "Ref cost is: " << param.ref_cost << "\n"; + std::cout << "Final cost is: " << final_cost << "\n"; + float err = fabs(final_cost - param.ref_cost); + err /= param.ref_cost; + std::cout << "Approximation error is: " << err * 100 << "%\n"; + EXPECT_LE(err, tol); + + // Check route goes through each vertex once + size_t u_nodes = nodes; + std::set node_set(h_route.begin(), h_route.end()); + ASSERT_EQ(node_set.size(), u_nodes); + + // Bound check + int max = *std::max_element(h_vertices.begin(), h_vertices.end()); + int min = *std::min_element(h_vertices.begin(), h_vertices.end()); + EXPECT_GE(*node_set.begin(), min); + EXPECT_LE(*node_set.rbegin(), max); + } + + private: + std::vector split(const std::string& s, char delimiter) + { + std::vector tokens; + std::string token; + std::istringstream tokenStream(s); + while (std::getline(tokenStream, token, delimiter)) { + if (token.size() == 0) continue; + tokens.push_back(token); + } + return tokens; + } + + // FIXME: At the moment TSP does not accept a graph_t as input and therefore + // deviates from the standard testing I/O pattern. Once other input types + // are supported we want to reconcile TSP testing with the rest of cugraph. + int load_tsp(const char* fname, Route* input) + { + std::fstream fs; + fs.open(fname); + std::string line; + std::vector tokens; + int nodes = 0; + while (std::getline(fs, line) && line.find(':') != std::string::npos) { + tokens = split(line, ':'); + auto strip_token = split(tokens[0], ' ')[0]; + if (strip_token == "DIMENSION") nodes = std::stof(tokens[1]); + } + + while (std::getline(fs, line) && line.find(' ') != std::string::npos) { + tokens = split(line, ' '); + auto city_id = std::stof(tokens[0]); + auto x = std::stof(tokens[1]); + auto y = std::stof(tokens[2]); + input->cities.push_back(city_id); + input->x_pos.push_back(x); + input->y_pos.push_back(y); + } + fs.close(); + assert(nodes == input->cities.size()); + return nodes; + } +}; + +TEST_P(Tests_Tsp, CheckFP32_T) { run_current_test(GetParam()); } + +INSTANTIATE_TEST_SUITE_P(simple_test, Tests_Tsp, ::testing::ValuesIn(euc_2d)); +CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/tree/mst_test.cu b/cpp/tests/tree/mst_test.cu index 949d6bae59b..ffbddd96eb0 100644 --- a/cpp/tests/tree/mst_test.cu +++ b/cpp/tests/tree/mst_test.cu @@ -1,5 +1,5 @@ /* - * 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. @@ -21,8 +21,8 @@ #include #include -#include -#include +#include +#include #include #include @@ -105,13 +105,14 @@ class Tests_Mst : public ::testing::TestWithParam { raft::handle_t handle; std::cout << std::endl; - cugraph::GraphCOOView G_coo(&cooRowInd[0], &cooColInd[0], &cooVal[0], m, nnz); + cugraph::legacy::GraphCOOView G_coo( + &cooRowInd[0], &cooColInd[0], &cooVal[0], m, nnz); auto G_unique = cugraph::coo_to_csr(G_coo); - cugraph::GraphCSRView G(G_unique->view().offsets, - G_unique->view().indices, - G_unique->view().edge_data, - G_unique->view().number_of_vertices, - G_unique->view().number_of_edges); + cugraph::legacy::GraphCSRView G(G_unique->view().offsets, + G_unique->view().indices, + G_unique->view().edge_data, + G_unique->view().number_of_vertices, + G_unique->view().number_of_edges); cudaDeviceSynchronize(); @@ -144,8 +145,8 @@ TEST_P(Tests_Mst, CheckFP32_T) { run_current_test(GetParam()); } TEST_P(Tests_Mst, CheckFP64_T) { run_current_test(GetParam()); } -INSTANTIATE_TEST_CASE_P(simple_test, - Tests_Mst, - ::testing::Values(Mst_Usecase("test/datasets/netscience.mtx"))); +INSTANTIATE_TEST_SUITE_P(simple_test, + Tests_Mst, + ::testing::Values(Mst_Usecase("test/datasets/netscience.mtx"))); CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/cpp/tests/utilities/base_fixture.hpp b/cpp/tests/utilities/base_fixture.hpp index 535b4b9c79e..75570b2c467 100644 --- a/cpp/tests/utilities/base_fixture.hpp +++ b/cpp/tests/utilities/base_fixture.hpp @@ -1,5 +1,5 @@ /* - * 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. @@ -16,8 +16,8 @@ #pragma once +#include #include -#include #include @@ -32,23 +32,28 @@ namespace cugraph { namespace test { +// FIXME: The BaseFixture class is not used in any tests. This file is only needed for the +// CUGRAPH_TEST_PROGRAM_MAIN macro and the code that it calls, so consider removing the BaseFixture +// class and renaming this file, or moving CUGRAPH_TEST_PROGRAM_MAIN to the test_utilities.hpp file +// and removing this file completely. + /** - * @brief Base test fixture class from which all libcudf tests should inherit. + * @brief Base test fixture class from which all libcugraph tests should inherit. * * Example: * ``` - * class MyTestFixture : public cudf::test::BaseFixture {}; + * class MyTestFixture : public cugraph::test::BaseFixture {}; * ``` **/ class BaseFixture : public ::testing::Test { - rmm::mr::device_memory_resource *_mr{rmm::mr::get_current_device_resource()}; + rmm::mr::device_memory_resource* _mr{rmm::mr::get_current_device_resource()}; public: /** - * @brief Returns pointer to `device_memory_resource` that should be used for - * all tests inheriting from this fixture + * @brief Returns pointer to `device_memory_resource` that should be used for all tests inheriting + *from this fixture **/ - rmm::mr::device_memory_resource *mr() { return _mr; } + rmm::mr::device_memory_resource* mr() { return _mr; } }; /// MR factory functions @@ -71,27 +76,26 @@ inline auto make_binning() } /** - * @brief Creates a memory resource for the unit test environment - * given the name of the allocation mode. + * @brief Creates a memory resource for the unit test environment given the name of the allocation + * mode. * - * The returned resource instance must be kept alive for the duration of - * the tests. Attaching the resource to a TestEnvironment causes - * issues since the environment objects are not destroyed until + * The returned resource instance must be kept alive for the duration of the tests. Attaching the + * resource to a TestEnvironment causes issues since the environment objects are not destroyed until * after the runtime is shutdown. * - * @throw cudf::logic_error if the `allocation_mode` is unsupported. + * @throw cugraph::logic_error if the `allocation_mode` is unsupported. * * @param allocation_mode String identifies which resource type. * Accepted types are "pool", "cuda", and "managed" only. * @return Memory resource instance */ inline std::shared_ptr create_memory_resource( - std::string const &allocation_mode) + std::string const& allocation_mode) { if (allocation_mode == "binning") return make_binning(); if (allocation_mode == "cuda") return make_cuda(); if (allocation_mode == "pool") return make_pool(); - if (allocation_mode == "managed") make_managed(); + if (allocation_mode == "managed") return make_managed(); CUGRAPH_FAIL("Invalid RMM allocation mode"); } @@ -99,22 +103,22 @@ inline std::shared_ptr create_memory_resource( } // namespace cugraph /** - * @brief Parses the cuDF test command line options. + * @brief Parses the cuGraph test command line options. * - * Currently only supports 'rmm_mode' string paramater, which set the rmm - * allocation mode. The default value of the parameter is 'pool'. + * Currently only supports 'rmm_mode' string paramater, which set the rmm allocation mode. The + * default value of the parameter is 'pool'. * * @return Parsing results in the form of cxxopts::ParseResult */ -inline auto parse_test_options(int argc, char **argv) +inline auto parse_test_options(int argc, char** argv) { try { - cxxopts::Options options(argv[0], " - cuDF tests command line options"); + cxxopts::Options options(argv[0], " - cuGraph tests command line options"); options.allow_unrecognised_options().add_options()( "rmm_mode", "RMM allocation mode", cxxopts::value()->default_value("pool")); return options.parse(argc, argv); - } catch (const cxxopts::OptionException &e) { + } catch (const cxxopts::OptionException& e) { CUGRAPH_FAIL("Error parsing command line options"); } } @@ -122,16 +126,14 @@ inline auto parse_test_options(int argc, char **argv) /** * @brief Macro that defines main function for gtest programs that use rmm * - * Should be included in every test program that uses rmm allocators since - * it maintains the lifespan of the rmm default memory resource. - * This `main` function is a wrapper around the google test generated `main`, - * maintaining the original functionality. In addition, this custom `main` - * function parses the command line to customize test behavior, like the - * allocation mode used for creating the default memory resource. - * + * Should be included in every test program that uses rmm allocators since it maintains the lifespan + * of the rmm default memory resource. This `main` function is a wrapper around the google test + * generated `main`, maintaining the original functionality. In addition, this custom `main` + * function parses the command line to customize test behavior, like the allocation mode used for + * creating the default memory resource. */ #define CUGRAPH_TEST_PROGRAM_MAIN() \ - int main(int argc, char **argv) \ + int main(int argc, char** argv) \ { \ ::testing::InitGoogleTest(&argc, argv); \ auto const cmd_opts = parse_test_options(argc, argv); \ @@ -140,3 +142,26 @@ inline auto parse_test_options(int argc, char **argv) rmm::mr::set_current_device_resource(resource.get()); \ return RUN_ALL_TESTS(); \ } + +#define CUGRAPH_MG_TEST_PROGRAM_MAIN() \ + int main(int argc, char** argv) \ + { \ + MPI_TRY(MPI_Init(&argc, &argv)); \ + int comm_rank{}; \ + int comm_size{}; \ + MPI_TRY(MPI_Comm_rank(MPI_COMM_WORLD, &comm_rank)); \ + MPI_TRY(MPI_Comm_size(MPI_COMM_WORLD, &comm_size)); \ + int num_gpus{}; \ + CUDA_TRY(cudaGetDeviceCount(&num_gpus)); \ + CUGRAPH_EXPECTS( \ + comm_size <= num_gpus, "# MPI ranks (%d) > # GPUs (%d).", comm_size, num_gpus); \ + CUDA_TRY(cudaSetDevice(comm_rank)); \ + ::testing::InitGoogleTest(&argc, argv); \ + auto const cmd_opts = parse_test_options(argc, argv); \ + auto const rmm_mode = cmd_opts["rmm_mode"].as(); \ + auto resource = cugraph::test::create_memory_resource(rmm_mode); \ + rmm::mr::set_current_device_resource(resource.get()); \ + auto ret = RUN_ALL_TESTS(); \ + MPI_TRY(MPI_Finalize()); \ + return ret; \ + } diff --git a/cpp/tests/utilities/cxxopts.hpp b/cpp/tests/utilities/cxxopts.hpp index 9a0b6e500d6..5aa77723a1f 100644 --- a/cpp/tests/utilities/cxxopts.hpp +++ b/cpp/tests/utilities/cxxopts.hpp @@ -17,6 +17,22 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #ifndef CXXOPTS_HPP_INCLUDED #define CXXOPTS_HPP_INCLUDED @@ -89,7 +105,9 @@ inline String& stringAppend(String& s, String a) { return s.append(std::move(a)) inline String& stringAppend(String& s, int n, UChar32 c) { - for (int i = 0; i != n; ++i) { s.append(c); } + for (int i = 0; i != n; ++i) { + s.append(c); + } return s; } @@ -1449,7 +1467,9 @@ inline void Options::generate_all_groups_help(String& result) const std::vector all_groups; all_groups.reserve(m_help.size()); - for (auto& group : m_help) { all_groups.push_back(group.first); } + for (auto& group : m_help) { + all_groups.push_back(group.first); + } generate_group_help(result, all_groups); } @@ -1494,4 +1514,4 @@ inline const HelpGroupDetails& Options::group_help(const std::string& group) con } // namespace cxxopts -#endif // CXXOPTS_HPP_INCLUDED \ No newline at end of file +#endif // CXXOPTS_HPP_INCLUDED diff --git a/cpp/tests/utilities/device_comm_wrapper.cu b/cpp/tests/utilities/device_comm_wrapper.cu new file mode 100644 index 00000000000..2fee7719e36 --- /dev/null +++ b/cpp/tests/utilities/device_comm_wrapper.cu @@ -0,0 +1,72 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "device_comm_wrapper.hpp" + +#include +#include + +#include +#include + +namespace cugraph { +namespace test { + +template +rmm::device_uvector device_gatherv(raft::handle_t const& handle, T const* d_input, size_t size) +{ + bool is_root = handle.get_comms().get_rank() == int{0}; + auto rx_sizes = cugraph::experimental::host_scalar_gather( + handle.get_comms(), size, int{0}, handle.get_stream()); + std::vector rx_displs(is_root ? static_cast(handle.get_comms().get_size()) + : size_t{0}); + if (is_root) { std::partial_sum(rx_sizes.begin(), rx_sizes.end() - 1, rx_displs.begin() + 1); } + + rmm::device_uvector gathered_v( + is_root ? std::reduce(rx_sizes.begin(), rx_sizes.end()) : size_t{0}, handle.get_stream()); + + cugraph::experimental::device_gatherv(handle.get_comms(), + d_input, + gathered_v.data(), + size, + rx_sizes, + rx_displs, + int{0}, + handle.get_stream()); + + return gathered_v; +} + +// explicit instantiation + +template rmm::device_uvector device_gatherv(raft::handle_t const& handle, + int32_t const* d_input, + size_t size); + +template rmm::device_uvector device_gatherv(raft::handle_t const& handle, + int64_t const* d_input, + size_t size); + +template rmm::device_uvector device_gatherv(raft::handle_t const& handle, + float const* d_input, + size_t size); + +template rmm::device_uvector device_gatherv(raft::handle_t const& handle, + double const* d_input, + size_t size); + +} // namespace test +} // namespace cugraph diff --git a/cpp/tests/utilities/device_comm_wrapper.hpp b/cpp/tests/utilities/device_comm_wrapper.hpp new file mode 100644 index 00000000000..55145edd71b --- /dev/null +++ b/cpp/tests/utilities/device_comm_wrapper.hpp @@ -0,0 +1,29 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include + +namespace cugraph { +namespace test { + +template +rmm::device_uvector device_gatherv(raft::handle_t const& handle, T const* d_input, size_t size); + +} // namespace test +} // namespace cugraph diff --git a/cpp/tests/utilities/matrix_market_file_utilities.cu b/cpp/tests/utilities/matrix_market_file_utilities.cu new file mode 100644 index 00000000000..711f332ae2f --- /dev/null +++ b/cpp/tests/utilities/matrix_market_file_utilities.cu @@ -0,0 +1,652 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include + +namespace cugraph { +namespace test { + +/// Read matrix properties from Matrix Market file +/** Matrix Market file is assumed to be a sparse matrix in coordinate + * format. + * + * @param f File stream for Matrix Market file. + * @param tg Boolean indicating whether to convert matrix to general + * format (from symmetric, Hermitian, or skew symmetric format). + * @param t (Output) MM_typecode with matrix properties. + * @param m (Output) Number of matrix rows. + * @param n (Output) Number of matrix columns. + * @param nnz (Output) Number of non-zero matrix entries. + * @return Zero if properties were read successfully. Otherwise + * non-zero. + */ +template +int mm_properties(FILE* f, int tg, MM_typecode* t, IndexType_* m, IndexType_* n, IndexType_* nnz) +{ + // Read matrix properties from file + int mint, nint, nnzint; + if (fseek(f, 0, SEEK_SET)) { + fprintf(stderr, "Error: could not set position in file\n"); + return -1; + } + if (mm_read_banner(f, t)) { + fprintf(stderr, "Error: could not read Matrix Market file banner\n"); + return -1; + } + if (!mm_is_matrix(*t) || !mm_is_coordinate(*t)) { + fprintf(stderr, "Error: file does not contain matrix in coordinate format\n"); + return -1; + } + if (mm_read_mtx_crd_size(f, &mint, &nint, &nnzint)) { + fprintf(stderr, "Error: could not read matrix dimensions\n"); + return -1; + } + if (!mm_is_pattern(*t) && !mm_is_real(*t) && !mm_is_integer(*t) && !mm_is_complex(*t)) { + fprintf(stderr, "Error: matrix entries are not valid type\n"); + return -1; + } + *m = mint; + *n = nint; + *nnz = nnzint; + + // Find total number of non-zero entries + if (tg && !mm_is_general(*t)) { + // Non-diagonal entries should be counted twice + *nnz *= 2; + + // Diagonal entries should not be double-counted + int st; + for (int i = 0; i < nnzint; ++i) { + // Read matrix entry + // MTX only supports int for row and col idx + int row, col; + double rval, ival; + if (mm_is_pattern(*t)) + st = fscanf(f, "%d %d\n", &row, &col); + else if (mm_is_real(*t) || mm_is_integer(*t)) + st = fscanf(f, "%d %d %lg\n", &row, &col, &rval); + else // Complex matrix + st = fscanf(f, "%d %d %lg %lg\n", &row, &col, &rval, &ival); + if (ferror(f) || (st == EOF)) { + fprintf(stderr, "Error: error %d reading Matrix Market file (entry %d)\n", st, i + 1); + return -1; + } + + // Check if entry is diagonal + if (row == col) --(*nnz); + } + } + + return 0; +} + +/// Read Matrix Market file and convert to COO format matrix +/** Matrix Market file is assumed to be a sparse matrix in coordinate + * format. + * + * @param f File stream for Matrix Market file. + * @param tg Boolean indicating whether to convert matrix to general + * format (from symmetric, Hermitian, or skew symmetric format). + * @param nnz Number of non-zero matrix entries. + * @param cooRowInd (Output) Row indices for COO matrix. Should have + * at least nnz entries. + * @param cooColInd (Output) Column indices for COO matrix. Should + * have at least nnz entries. + * @param cooRVal (Output) Real component of COO matrix + * entries. Should have at least nnz entries. Ignored if null + * pointer. + * @param cooIVal (Output) Imaginary component of COO matrix + * entries. Should have at least nnz entries. Ignored if null + * pointer. + * @return Zero if matrix was read successfully. Otherwise non-zero. + */ +template +int mm_to_coo(FILE* f, + int tg, + IndexType_ nnz, + IndexType_* cooRowInd, + IndexType_* cooColInd, + ValueType_* cooRVal, + ValueType_* cooIVal) +{ + // Read matrix properties from file + MM_typecode t; + int m, n, nnzOld; + if (fseek(f, 0, SEEK_SET)) { + fprintf(stderr, "Error: could not set position in file\n"); + return -1; + } + if (mm_read_banner(f, &t)) { + fprintf(stderr, "Error: could not read Matrix Market file banner\n"); + return -1; + } + if (!mm_is_matrix(t) || !mm_is_coordinate(t)) { + fprintf(stderr, "Error: file does not contain matrix in coordinate format\n"); + return -1; + } + if (mm_read_mtx_crd_size(f, &m, &n, &nnzOld)) { + fprintf(stderr, "Error: could not read matrix dimensions\n"); + return -1; + } + if (!mm_is_pattern(t) && !mm_is_real(t) && !mm_is_integer(t) && !mm_is_complex(t)) { + fprintf(stderr, "Error: matrix entries are not valid type\n"); + return -1; + } + + // Add each matrix entry in file to COO format matrix + int i; // Entry index in Matrix Market file; can only be int in the MTX format + int j = 0; // Entry index in COO format matrix; can only be int in the MTX format + for (i = 0; i < nnzOld; ++i) { + // Read entry from file + int row, col; + double rval, ival; + int st; + if (mm_is_pattern(t)) { + st = fscanf(f, "%d %d\n", &row, &col); + rval = 1.0; + ival = 0.0; + } else if (mm_is_real(t) || mm_is_integer(t)) { + st = fscanf(f, "%d %d %lg\n", &row, &col, &rval); + ival = 0.0; + } else // Complex matrix + st = fscanf(f, "%d %d %lg %lg\n", &row, &col, &rval, &ival); + if (ferror(f) || (st == EOF)) { + fprintf(stderr, "Error: error %d reading Matrix Market file (entry %d)\n", st, i + 1); + return -1; + } + + // Switch to 0-based indexing + --row; + --col; + + // Record entry + cooRowInd[j] = row; + cooColInd[j] = col; + if (cooRVal != NULL) cooRVal[j] = rval; + if (cooIVal != NULL) cooIVal[j] = ival; + ++j; + + // Add symmetric complement of non-diagonal entries + if (tg && !mm_is_general(t) && (row != col)) { + // Modify entry value if matrix is skew symmetric or Hermitian + if (mm_is_skew(t)) { + rval = -rval; + ival = -ival; + } else if (mm_is_hermitian(t)) { + ival = -ival; + } + + // Record entry + cooRowInd[j] = col; + cooColInd[j] = row; + if (cooRVal != NULL) cooRVal[j] = rval; + if (cooIVal != NULL) cooIVal[j] = ival; + ++j; + } + } + return 0; +} + +// FIXME: A similar function could be useful for CSC format +// There are functions above that operate coo -> csr and coo->csc +/** + * @tparam + */ +template +std::unique_ptr> generate_graph_csr_from_mm( + bool& directed, std::string mm_file) +{ + vertex_t number_of_vertices; + edge_t number_of_edges; + + FILE* fpin = fopen(mm_file.c_str(), "r"); + CUGRAPH_EXPECTS(fpin != nullptr, "fopen (%s) failure.", mm_file.c_str()); + + vertex_t number_of_columns = 0; + MM_typecode mm_typecode{0}; + CUGRAPH_EXPECTS( + mm_properties( + fpin, 1, &mm_typecode, &number_of_vertices, &number_of_columns, &number_of_edges) == 0, + "mm_properties query failure."); + CUGRAPH_EXPECTS(mm_is_matrix(mm_typecode), "Invalid input file."); + CUGRAPH_EXPECTS(mm_is_coordinate(mm_typecode), "Invalid input file."); + CUGRAPH_EXPECTS(!mm_is_complex(mm_typecode), "Invalid input file."); + CUGRAPH_EXPECTS(!mm_is_skew(mm_typecode), "Invalid input file."); + + directed = !mm_is_symmetric(mm_typecode); + + // Allocate memory on host + std::vector coo_row_ind(number_of_edges); + std::vector coo_col_ind(number_of_edges); + std::vector coo_val(number_of_edges); + + // Read + CUGRAPH_EXPECTS( + (mm_to_coo( + fpin, 1, number_of_edges, &coo_row_ind[0], &coo_col_ind[0], &coo_val[0], NULL)) == 0, + "file read failure."); + CUGRAPH_EXPECTS(fclose(fpin) == 0, "fclose failure."); + + cugraph::legacy::GraphCOOView cooview( + &coo_row_ind[0], &coo_col_ind[0], &coo_val[0], number_of_vertices, number_of_edges); + + return cugraph::coo_to_csr(cooview); +} + +template +std::tuple, + rmm::device_uvector, + std::optional>, + vertex_t, + bool> +read_edgelist_from_matrix_market_file(raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted) +{ + MM_typecode mc{}; + vertex_t m{}; + size_t nnz{}; + + FILE* file = fopen(graph_file_full_path.c_str(), "r"); + CUGRAPH_EXPECTS(file != nullptr, "fopen (%s) failure.", graph_file_full_path.c_str()); + + size_t tmp_m{}; + size_t tmp_k{}; + auto mm_ret = cugraph::test::mm_properties(file, 1, &mc, &tmp_m, &tmp_k, &nnz); + CUGRAPH_EXPECTS(mm_ret == 0, "could not read Matrix Market file properties."); + m = static_cast(tmp_m); + CUGRAPH_EXPECTS(mm_is_matrix(mc) && mm_is_coordinate(mc) && !mm_is_complex(mc) && !mm_is_skew(mc), + "invalid Matrix Market file properties."); + + vertex_t number_of_vertices = m; + bool is_symmetric = mm_is_symmetric(mc); + + std::vector h_rows(nnz); + std::vector h_cols(nnz); + std::vector h_weights(nnz); + + mm_ret = cugraph::test::mm_to_coo( + file, 1, nnz, h_rows.data(), h_cols.data(), h_weights.data(), static_cast(nullptr)); + CUGRAPH_EXPECTS(mm_ret == 0, "could not read matrix data"); + + auto file_ret = fclose(file); + CUGRAPH_EXPECTS(file_ret == 0, "fclose failure."); + + rmm::device_uvector d_edgelist_rows(h_rows.size(), handle.get_stream()); + rmm::device_uvector d_edgelist_cols(h_cols.size(), handle.get_stream()); + auto d_edgelist_weights = test_weighted ? std::make_optional>( + h_weights.size(), handle.get_stream()) + : std::nullopt; + + raft::update_device(d_edgelist_rows.data(), h_rows.data(), h_rows.size(), handle.get_stream()); + raft::update_device(d_edgelist_cols.data(), h_cols.data(), h_cols.size(), handle.get_stream()); + if (d_edgelist_weights) { + raft::update_device( + (*d_edgelist_weights).data(), h_weights.data(), h_weights.size(), handle.get_stream()); + } + + return std::make_tuple(std::move(d_edgelist_rows), + std::move(d_edgelist_cols), + std::move(d_edgelist_weights), + number_of_vertices, + is_symmetric); +} + +template +std::tuple, + std::optional>> +read_graph_from_matrix_market_file(raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber) +{ + auto [d_edgelist_rows, d_edgelist_cols, d_edgelist_weights, number_of_vertices, is_symmetric] = + read_edgelist_from_matrix_market_file( + handle, graph_file_full_path, test_weighted); + + rmm::device_uvector d_vertices(number_of_vertices, handle.get_stream()); + thrust::sequence(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + d_vertices.begin(), + d_vertices.end(), + vertex_t{0}); + handle.get_stream_view().synchronize(); + + if (multi_gpu) { + auto& comm = handle.get_comms(); + auto const comm_size = comm.get_size(); + auto const comm_rank = comm.get_rank(); + auto& row_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().row_name()); + auto const row_comm_size = row_comm.get_size(); + auto& col_comm = handle.get_subcomm(cugraph::partition_2d::key_naming_t().col_name()); + auto const col_comm_size = col_comm.get_size(); + + auto vertex_key_func = + cugraph::experimental::detail::compute_gpu_id_from_vertex_t{comm_size}; + d_vertices.resize( + thrust::distance( + d_vertices.begin(), + thrust::remove_if(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + d_vertices.begin(), + d_vertices.end(), + [comm_rank, key_func = vertex_key_func] __device__(auto val) { + return key_func(val) != comm_rank; + })), + handle.get_stream()); + d_vertices.shrink_to_fit(handle.get_stream()); + + auto edge_key_func = cugraph::experimental::detail::compute_gpu_id_from_edge_t{ + comm_size, row_comm_size, col_comm_size}; + size_t number_of_local_edges{}; + if (d_edgelist_weights) { + auto edge_first = thrust::make_zip_iterator(thrust::make_tuple( + d_edgelist_rows.begin(), d_edgelist_cols.begin(), (*d_edgelist_weights).begin())); + number_of_local_edges = thrust::distance( + edge_first, + thrust::remove_if(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + edge_first, + edge_first + d_edgelist_rows.size(), + [comm_rank, key_func = edge_key_func] __device__(auto e) { + auto major = store_transposed ? thrust::get<1>(e) : thrust::get<0>(e); + auto minor = store_transposed ? thrust::get<0>(e) : thrust::get<1>(e); + return key_func(major, minor) != comm_rank; + })); + } else { + auto edge_first = thrust::make_zip_iterator( + thrust::make_tuple(d_edgelist_rows.begin(), d_edgelist_cols.begin())); + number_of_local_edges = thrust::distance( + edge_first, + thrust::remove_if(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()), + edge_first, + edge_first + d_edgelist_rows.size(), + [comm_rank, key_func = edge_key_func] __device__(auto e) { + auto major = store_transposed ? thrust::get<1>(e) : thrust::get<0>(e); + auto minor = store_transposed ? thrust::get<0>(e) : thrust::get<1>(e); + return key_func(major, minor) != comm_rank; + })); + } + + d_edgelist_rows.resize(number_of_local_edges, handle.get_stream()); + d_edgelist_rows.shrink_to_fit(handle.get_stream()); + d_edgelist_cols.resize(number_of_local_edges, handle.get_stream()); + d_edgelist_cols.shrink_to_fit(handle.get_stream()); + if (d_edgelist_weights) { + (*d_edgelist_weights).resize(number_of_local_edges, handle.get_stream()); + (*d_edgelist_weights).shrink_to_fit(handle.get_stream()); + } + } + + handle.get_stream_view().synchronize(); + return cugraph::experimental:: + create_graph_from_edgelist( + handle, + std::optional>{ + std::make_tuple(d_vertices.data(), static_cast(d_vertices.size()))}, + std::move(d_edgelist_rows), + std::move(d_edgelist_cols), + std::move(d_edgelist_weights), + cugraph::experimental::graph_properties_t{is_symmetric, false}, + renumber); +} + +// explicit instantiations + +template int32_t mm_to_coo(FILE* f, + int32_t tg, + int32_t nnz, + int32_t* cooRowInd, + int32_t* cooColInd, + int32_t* cooRVal, + int32_t* cooIVal); + +template int32_t mm_to_coo(FILE* f, + int32_t tg, + int32_t nnz, + int32_t* cooRowInd, + int32_t* cooColInd, + double* cooRVal, + double* cooIVal); + +template int32_t mm_to_coo(FILE* f, + int32_t tg, + int32_t nnz, + int32_t* cooRowInd, + int32_t* cooColInd, + float* cooRVal, + float* cooIVal); + +template std::unique_ptr> +generate_graph_csr_from_mm(bool& directed, std::string mm_file); + +template std::unique_ptr> +generate_graph_csr_from_mm(bool& directed, std::string mm_file); + +template std::unique_ptr> +generate_graph_csr_from_mm(bool& directed, std::string mm_file); + +template std::unique_ptr> +generate_graph_csr_from_mm(bool& directed, std::string mm_file); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template std::tuple, + std::optional>> +read_graph_from_matrix_market_file( + raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +} // namespace test +} // namespace cugraph diff --git a/cpp/tests/utilities/misc_utilities.cpp b/cpp/tests/utilities/misc_utilities.cpp new file mode 100644 index 00000000000..14f0df2f35d --- /dev/null +++ b/cpp/tests/utilities/misc_utilities.cpp @@ -0,0 +1,33 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include + +namespace cugraph { +namespace test { + +std::string getFileName(const std::string& s) +{ + char sep = '/'; +#ifdef _WIN32 + sep = '\\'; +#endif + size_t i = s.rfind(sep, s.length()); + if (i != std::string::npos) { return (s.substr(i + 1, s.length() - i)); } + return (""); +} + +} // namespace test +} // namespace cugraph diff --git a/cpp/tests/utilities/rmat_utilities.cpp b/cpp/tests/utilities/rmat_utilities.cpp new file mode 100644 index 00000000000..8502fe1b1c9 --- /dev/null +++ b/cpp/tests/utilities/rmat_utilities.cpp @@ -0,0 +1,600 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include +#include +#include +#include + +#include + +namespace cugraph { +namespace test { + +template +std::tuple, + std::optional>> +generate_graph_from_rmat_params(raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t base_seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions) +{ + CUGRAPH_EXPECTS(!multi_gpu || renumber, "renumber should be true if multi_gpu is true."); + CUGRAPH_EXPECTS(size_t{1} << scale <= static_cast(std::numeric_limits::max()), + "vertex_t overflow."); + CUGRAPH_EXPECTS( + (size_t{1} << scale) * edge_factor <= static_cast(std::numeric_limits::max()), + " edge_t overflow."); + + vertex_t number_of_vertices = static_cast(size_t{1} << scale); + edge_t number_of_edges = + static_cast(static_cast(number_of_vertices) * edge_factor); + + std::vector partition_edge_counts(partition_ids.size()); + std::vector partition_vertex_firsts(partition_ids.size()); + std::vector partition_vertex_lasts(partition_ids.size()); + for (size_t i = 0; i < partition_ids.size(); ++i) { + auto id = partition_ids[i]; + + partition_edge_counts[i] = number_of_edges / num_partitions + + (id < number_of_edges % num_partitions ? edge_t{1} : edge_t{0}); + + partition_vertex_firsts[i] = (number_of_vertices / num_partitions) * id; + partition_vertex_lasts[i] = (number_of_vertices / num_partitions) * (id + 1); + if (id < number_of_vertices % num_partitions) { + partition_vertex_firsts[i] += id; + partition_vertex_lasts[i] += id + 1; + } else { + partition_vertex_firsts[i] += number_of_vertices % num_partitions; + partition_vertex_lasts[i] += number_of_vertices % num_partitions; + } + } + + rmm::device_uvector d_edgelist_rows(0, handle.get_stream()); + rmm::device_uvector d_edgelist_cols(0, handle.get_stream()); + auto d_edgelist_weights = + test_weighted ? std::make_optional>(0, handle.get_stream()) + : std::nullopt; + for (size_t i = 0; i < partition_ids.size(); ++i) { + auto id = partition_ids[i]; + + rmm::device_uvector d_tmp_rows(0, handle.get_stream()); + rmm::device_uvector d_tmp_cols(0, handle.get_stream()); + std::tie(i == 0 ? d_edgelist_rows : d_tmp_rows, i == 0 ? d_edgelist_cols : d_tmp_cols) = + cugraph::generate_rmat_edgelist(handle, + scale, + partition_edge_counts[i], + a, + b, + c, + base_seed + id, + undirected ? true : false); + + std::optional> d_tmp_weights{std::nullopt}; + if (d_edgelist_weights) { + if (i == 0) { + (*d_edgelist_weights).resize(d_edgelist_rows.size(), handle.get_stream()); + } else { + d_tmp_weights = + std::make_optional>(d_tmp_rows.size(), handle.get_stream()); + } + + cugraph::detail::uniform_random_fill( + handle.get_stream_view(), + i == 0 ? (*d_edgelist_weights).data() : (*d_tmp_weights).data(), + i == 0 ? (*d_edgelist_weights).size() : (*d_tmp_weights).size(), + weight_t{0.0}, + weight_t{1.0}, + base_seed + num_partitions + id); + } + + if (i > 0) { + auto start_offset = d_edgelist_rows.size(); + d_edgelist_rows.resize(start_offset + d_tmp_rows.size(), handle.get_stream()); + d_edgelist_cols.resize(d_edgelist_rows.size(), handle.get_stream()); + raft::copy(d_edgelist_rows.begin() + start_offset, + d_tmp_rows.begin(), + d_tmp_rows.size(), + handle.get_stream()); + raft::copy(d_edgelist_cols.begin() + start_offset, + d_tmp_cols.begin(), + d_tmp_cols.size(), + handle.get_stream()); + if (d_edgelist_weights) { + (*d_edgelist_weights).resize(d_edgelist_rows.size(), handle.get_stream()); + raft::copy(d_edgelist_weights->begin() + start_offset, + d_tmp_weights->begin(), + d_tmp_weights->size(), + handle.get_stream()); + } + } + } + + if (undirected) { +// FIXME: may need to undo this and handle symmetrization elsewhere once the new test graph +// generation API gets integrated +#if 1 + std::tie(d_edgelist_rows, d_edgelist_cols, d_edgelist_weights) = + cugraph::symmetrize_edgelist( + handle, + std::move(d_edgelist_rows), + std::move(d_edgelist_cols), + test_weighted ? std::optional>(std::move(d_edgelist_weights)) + : std::nullopt); +#endif + } + + if (multi_gpu) { + std::tie(d_edgelist_rows, d_edgelist_cols, d_edgelist_weights) = + cugraph::detail::shuffle_edgelist_by_edge( + handle, d_edgelist_rows, d_edgelist_cols, d_edgelist_weights, store_transposed); + } + + rmm::device_uvector d_vertices(0, handle.get_stream()); + for (size_t i = 0; i < partition_ids.size(); ++i) { + auto id = partition_ids[i]; + + auto start_offset = d_vertices.size(); + d_vertices.resize(start_offset + (partition_vertex_lasts[i] - partition_vertex_firsts[i]), + handle.get_stream()); + cugraph::detail::sequence_fill(handle.get_stream_view(), + d_vertices.begin() + start_offset, + d_vertices.size() - start_offset, + partition_vertex_firsts[i]); + } + + if (multi_gpu) { d_vertices = cugraph::detail::shuffle_vertices(handle, d_vertices); } + + return cugraph::experimental:: + create_graph_from_edgelist( + handle, + std::optional>{ + std::make_tuple(d_vertices.data(), static_cast(d_vertices.size()))}, + std::move(d_edgelist_rows), + std::move(d_edgelist_cols), + std::move(d_edgelist_weights), + cugraph::experimental::graph_properties_t{undirected, true}, + renumber); +} // namespace test + +// explicit instantiations + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +template std::tuple, + std::optional>> +generate_graph_from_rmat_params( + raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +} // namespace test +} // namespace cugraph diff --git a/cpp/tests/utilities/test_graphs.hpp b/cpp/tests/utilities/test_graphs.hpp new file mode 100644 index 00000000000..150f2790277 --- /dev/null +++ b/cpp/tests/utilities/test_graphs.hpp @@ -0,0 +1,534 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include + +#include +#include + +namespace cugraph { +namespace test { + +namespace detail { + +class TranslateGraph_Usecase { + public: + TranslateGraph_Usecase() = delete; + TranslateGraph_Usecase(size_t base_vertex_id = 0) : base_vertex_id_(base_vertex_id) {} + + template + void translate(raft::handle_t const& handle, + rmm::device_uvector& d_src, + rmm::device_uvector& d_dst) const + { + if (base_vertex_id_ > 0) + cugraph::test::translate_vertex_ids( + handle, d_src, d_dst, static_cast(base_vertex_id_)); + } + + size_t base_vertex_id_{}; +}; + +} // namespace detail + +class File_Usecase : public detail::TranslateGraph_Usecase { + public: + File_Usecase() = delete; + + File_Usecase(std::string const& graph_file_path, size_t base_vertex_id = 0) + : detail::TranslateGraph_Usecase(base_vertex_id) + { + if ((graph_file_path.length() > 0) && (graph_file_path[0] != '/')) { + graph_file_full_path_ = cugraph::test::get_rapids_dataset_root_dir() + "/" + graph_file_path; + } else { + graph_file_full_path_ = graph_file_path; + } + } + + template + std::tuple, + rmm::device_uvector, + std::optional>, + vertex_t, + bool> + construct_edgelist(raft::handle_t const& handle, bool test_weighted) const + { + auto [d_src_v, d_dst_v, d_weights_v, num_vertices, is_symmetric] = + read_edgelist_from_matrix_market_file( + handle, graph_file_full_path_, test_weighted); + + translate(handle, d_src_v, d_dst_v); + +#if 0 + if (multi_gpu) { + std::tie(d_src_v, d_dst_v) = filter_edgelist_by_gpu(handle, d_src_v, d_dst_v); + } +#endif + + return std::make_tuple( + std::move(d_src_v), + std::move(d_dst_v), + std::move(d_weights_v), + static_cast(detail::TranslateGraph_Usecase::base_vertex_id_) + num_vertices, + is_symmetric); + } + + template + std::tuple< + cugraph::experimental::graph_t, + std::optional>> + construct_graph(raft::handle_t const& handle, bool test_weighted, bool renumber = true) const + { + auto [d_src_v, d_dst_v, d_weights_v, num_vertices, is_symmetric] = + this->template construct_edgelist( + handle, test_weighted); + + // TODO: Consider calling construct_edgelist and creating + // a generic test function to take the edgelist and + // do the graph construction. + // + // Would be more reusable across tests + // + return read_graph_from_matrix_market_file( + handle, graph_file_full_path_, test_weighted, renumber); + } + + private: + std::string graph_file_full_path_{}; +}; + +class Rmat_Usecase : public detail::TranslateGraph_Usecase { + public: + Rmat_Usecase() = delete; + + Rmat_Usecase(size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + size_t base_vertex_id = 0, + bool multi_gpu_usecase = false) + : detail::TranslateGraph_Usecase(base_vertex_id), + scale_(scale), + edge_factor_(edge_factor), + a_(a), + b_(b), + c_(c), + seed_(seed), + undirected_(undirected), + scramble_vertex_ids_(scramble_vertex_ids), + multi_gpu_usecase_(multi_gpu_usecase) + { + } + + template + std::tuple, + rmm::device_uvector, + std::optional>, + vertex_t, + bool> + construct_edgelist(raft::handle_t const& handle, bool test_weighted) const + { + // TODO: Tease through generate_graph_from_rmat_params + // to extract the edgelist part + // Call cugraph::translate_vertex_ids(handle, d_src_v, d_dst_v, base_vertex_id_); + + CUGRAPH_FAIL("Not implemented"); + } + + template + std::tuple< + cugraph::experimental::graph_t, + std::optional>> + construct_graph(raft::handle_t const& handle, bool test_weighted, bool renumber = true) const + { + std::vector partition_ids(1); + size_t comm_size; + + if (multi_gpu_usecase_) { + auto& comm = handle.get_comms(); + comm_size = comm.get_size(); + auto const comm_rank = comm.get_rank(); + + partition_ids.resize(multi_gpu ? size_t{1} : static_cast(comm_size)); + + std::iota(partition_ids.begin(), + partition_ids.end(), + multi_gpu ? static_cast(comm_rank) : size_t{0}); + } else { + comm_size = 1; + partition_ids[0] = size_t{0}; + } + + // TODO: Need to offset by base_vertex_id_ + // static_cast(base_vertex_id_)); + // Consider using construct_edgelist like other options + return generate_graph_from_rmat_params( + handle, + scale_, + edge_factor_, + a_, + b_, + c_, + seed_, + undirected_, + scramble_vertex_ids_, + test_weighted, + renumber, + partition_ids, + comm_size); + } + + private: + size_t scale_{}; + size_t edge_factor_{}; + double a_{}; + double b_{}; + double c_{}; + uint64_t seed_{}; + bool undirected_{}; + bool scramble_vertex_ids_{}; + bool multi_gpu_usecase_{}; +}; + +class PathGraph_Usecase { + public: + PathGraph_Usecase() = delete; + + PathGraph_Usecase(std::vector> parms, + bool weighted = false, + bool scramble = false) + : parms_(parms), weighted_(weighted) + { + } + + template + std::tuple, + rmm::device_uvector, + std::optional>, + vertex_t, + bool> + construct_edgelist(raft::handle_t const& handle, bool test_weighted) const + { + constexpr bool symmetric{true}; + + std::vector> converted_parms(parms_.size()); + + std::transform(parms_.begin(), parms_.end(), converted_parms.begin(), [](auto p) { + return std::make_tuple(static_cast(std::get<0>(p)), + static_cast(std::get<1>(p))); + }); + + auto [src_v, dst_v] = cugraph::generate_path_graph_edgelist(handle, converted_parms); + std::tie(src_v, dst_v, std::ignore) = cugraph::symmetrize_edgelist( + handle, std::move(src_v), std::move(dst_v), std::nullopt); + + return std::make_tuple(std::move(src_v), + std::move(dst_v), + test_weighted ? std::make_optional>( + src_v.size(), handle.get_stream()) + : std::nullopt, + num_vertices_, + symmetric); + } + + template + std::tuple< + cugraph::experimental::graph_t, + std::optional>> + construct_graph(raft::handle_t const& handle, bool test_weighted, bool renumber = true) const + { + CUGRAPH_FAIL("not implemented"); + } + + private: + std::vector> parms_{}; + size_t num_vertices_{0}; + bool weighted_{false}; +}; + +class Mesh2DGraph_Usecase { + public: + Mesh2DGraph_Usecase() = delete; + + Mesh2DGraph_Usecase(std::vector> const& parms, bool weighted) + : parms_(parms), weighted_(weighted) + { + } + + template + std::tuple, + rmm::device_uvector, + std::optional>, + vertex_t, + bool> + construct_edgelist(raft::handle_t const& handle, bool test_weighted) const + { + } + + template + std::tuple< + cugraph::experimental::graph_t, + std::optional>> + construct_graph(raft::handle_t const& handle, bool test_weighted, bool renumber = true) const; + + private: + std::vector> parms_{}; + bool weighted_{false}; +}; + +class Mesh3DGraph_Usecase { + public: + Mesh3DGraph_Usecase() = delete; + + Mesh3DGraph_Usecase(std::vector> const& parms, + bool weighted) + : parms_(parms), weighted_(weighted) + { + } + + template + std::tuple, + rmm::device_uvector, + std::optional>, + vertex_t, + bool> + construct_edgelist(raft::handle_t const& handle, bool test_weighted) const; + + template + std::tuple< + cugraph::experimental::graph_t, + std::optional>> + construct_graph(raft::handle_t const& handle, bool test_weighted, bool renumber = true) const; + + private: + std::vector> parms_{}; + bool weighted_{false}; +}; + +class CompleteGraph_Usecase { + public: + CompleteGraph_Usecase() = delete; + + CompleteGraph_Usecase(std::vector> const& parms, bool weighted) + : parms_(parms), weighted_(weighted) + { + } + + template + std::tuple, + rmm::device_uvector, + std::optional>, + vertex_t, + bool> + construct_edgelist(raft::handle_t const& handle, bool test_weighted) const; + + template + std::tuple< + cugraph::experimental::graph_t, + std::optional>> + construct_graph(raft::handle_t const& handle, bool test_weighted, bool renumber = true) const; + + private: + std::vector> parms_{}; + bool weighted_{false}; +}; + +namespace detail { + +template +struct combined_construct_graph_tuple_impl { + template + std::vector, + rmm::device_uvector, + rmm::device_uvector, + vertex_t, + bool>> + construct_edges(raft::handle_t const& handle, + bool test_weighted, + generator_tuple_t const& generator_tuple) const + { + return combined_construct_graph_tuple_impl() + .construct_edges(generator_tuple) + .push_back(std::get(generator_tuple).construct_edges(handle, test_weighted)); + } +}; + +template +struct combined_construct_graph_tuple_impl { + template + std::vector, + rmm::device_uvector, + rmm::device_uvector, + vertex_t, + bool>> + construct_edges(raft::handle_t const& handle, + bool test_weighted, + generator_tuple_t const& generator_tuple) const + { + return std::vector, + rmm::device_uvector, + rmm::device_uvector, + vertex_t, + bool>>(); + } +}; + +} // namespace detail + +template +class CombinedGenerator_Usecase { + CombinedGenerator_Usecase() = delete; + + CombinedGenerator_Usecase(generator_tuple_t const& tuple) : generator_tuple_(tuple) {} + + template + std::tuple, + rmm::device_uvector, + std::optional>, + vertex_t, + bool> + construct_edgelist(raft::handle_t const& handle, bool test_weighted) const + { + size_t constexpr tuple_size{std::tuple_size::value}; + + auto edge_tuple_vector = + detail::combined_construct_graph_tuple_impl() + .construct_edges(handle, test_weighted, generator_tuple_); + + // Need to combine + CUGRAPH_FAIL("not implemented"); + } + + template + std::tuple< + cugraph::experimental::graph_t, + std::optional>> + construct_graph(raft::handle_t const& handle, bool test_weighted, bool renumber = true) const + { + // Call construct_edgelist to get tuple of edge lists + // return generate_graph_from_edgelist<...>(...) + CUGRAPH_FAIL("not implemented"); + } + + private: + generator_tuple_t const& generator_tuple_; +}; + +template +std::tuple, + std::optional>> +construct_graph(raft::handle_t const& handle, + input_usecase_t const& input_usecase, + bool test_weighted, + bool renumber = true) +{ + auto [d_src_v, d_dst_v, d_weights_v, num_vertices, is_symmetric] = + input_usecase + .template construct_edgelist( + handle, test_weighted); + + return cugraph::experimental:: + create_graph_from_edgelist( + handle, + std::nullopt, + std::move(d_src_v), + std::move(d_dst_v), + std::move(d_weights_v), + cugraph::experimental::graph_properties_t{is_symmetric, false}, + renumber); +} + +} // namespace test +} // namespace cugraph diff --git a/cpp/tests/utilities/test_utilities.hpp b/cpp/tests/utilities/test_utilities.hpp index 518e7c2860e..b452ff9a95f 100644 --- a/cpp/tests/utilities/test_utilities.hpp +++ b/cpp/tests/utilities/test_utilities.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-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. @@ -15,38 +15,29 @@ */ #pragma once -#include -#include -#include +#include +#include +#include -#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include extern "C" { #include "mmio.h" } -#include - -#include -#include -#include -#include - namespace cugraph { namespace test { -std::string getFileName(const std::string& s) -{ - char sep = '/'; - -#ifdef _WIN32 - sep = '\\'; -#endif - - size_t i = s.rfind(sep, s.length()); - if (i != std::string::npos) { return (s.substr(i + 1, s.length() - i)); } - return (""); -} +std::string getFileName(const std::string& s); /// Read matrix properties from Matrix Market file /** Matrix Market file is assumed to be a sparse matrix in coordinate @@ -63,64 +54,7 @@ std::string getFileName(const std::string& s) * non-zero. */ template -int mm_properties(FILE* f, int tg, MM_typecode* t, IndexType_* m, IndexType_* n, IndexType_* nnz) -{ - // Read matrix properties from file - int mint, nint, nnzint; - if (fseek(f, 0, SEEK_SET)) { - fprintf(stderr, "Error: could not set position in file\n"); - return -1; - } - if (mm_read_banner(f, t)) { - fprintf(stderr, "Error: could not read Matrix Market file banner\n"); - return -1; - } - if (!mm_is_matrix(*t) || !mm_is_coordinate(*t)) { - fprintf(stderr, "Error: file does not contain matrix in coordinate format\n"); - return -1; - } - if (mm_read_mtx_crd_size(f, &mint, &nint, &nnzint)) { - fprintf(stderr, "Error: could not read matrix dimensions\n"); - return -1; - } - if (!mm_is_pattern(*t) && !mm_is_real(*t) && !mm_is_integer(*t) && !mm_is_complex(*t)) { - fprintf(stderr, "Error: matrix entries are not valid type\n"); - return -1; - } - *m = mint; - *n = nint; - *nnz = nnzint; - - // Find total number of non-zero entries - if (tg && !mm_is_general(*t)) { - // Non-diagonal entries should be counted twice - *nnz *= 2; - - // Diagonal entries should not be double-counted - int st; - for (int i = 0; i < nnzint; ++i) { - // Read matrix entry - // MTX only supports int for row and col idx - int row, col; - double rval, ival; - if (mm_is_pattern(*t)) - st = fscanf(f, "%d %d\n", &row, &col); - else if (mm_is_real(*t) || mm_is_integer(*t)) - st = fscanf(f, "%d %d %lg\n", &row, &col, &rval); - else // Complex matrix - st = fscanf(f, "%d %d %lg %lg\n", &row, &col, &rval, &ival); - if (ferror(f) || (st == EOF)) { - fprintf(stderr, "Error: error %d reading Matrix Market file (entry %d)\n", st, i + 1); - return -1; - } - - // Check if entry is diagonal - if (row == col) --(*nnz); - } - } - - return 0; -} +int mm_properties(FILE* f, int tg, MM_typecode* t, IndexType_* m, IndexType_* n, IndexType_* nnz); /// Read Matrix Market file and convert to COO format matrix /** Matrix Market file is assumed to be a sparse matrix in coordinate @@ -149,169 +83,16 @@ int mm_to_coo(FILE* f, IndexType_* cooRowInd, IndexType_* cooColInd, ValueType_* cooRVal, - ValueType_* cooIVal) -{ - // Read matrix properties from file - MM_typecode t; - int m, n, nnzOld; - if (fseek(f, 0, SEEK_SET)) { - fprintf(stderr, "Error: could not set position in file\n"); - return -1; - } - if (mm_read_banner(f, &t)) { - fprintf(stderr, "Error: could not read Matrix Market file banner\n"); - return -1; - } - if (!mm_is_matrix(t) || !mm_is_coordinate(t)) { - fprintf(stderr, "Error: file does not contain matrix in coordinate format\n"); - return -1; - } - if (mm_read_mtx_crd_size(f, &m, &n, &nnzOld)) { - fprintf(stderr, "Error: could not read matrix dimensions\n"); - return -1; - } - if (!mm_is_pattern(t) && !mm_is_real(t) && !mm_is_integer(t) && !mm_is_complex(t)) { - fprintf(stderr, "Error: matrix entries are not valid type\n"); - return -1; - } - - // Add each matrix entry in file to COO format matrix - int i; // Entry index in Matrix Market file; can only be int in the MTX format - int j = 0; // Entry index in COO format matrix; can only be int in the MTX format - for (i = 0; i < nnzOld; ++i) { - // Read entry from file - int row, col; - double rval, ival; - int st; - if (mm_is_pattern(t)) { - st = fscanf(f, "%d %d\n", &row, &col); - rval = 1.0; - ival = 0.0; - } else if (mm_is_real(t) || mm_is_integer(t)) { - st = fscanf(f, "%d %d %lg\n", &row, &col, &rval); - ival = 0.0; - } else // Complex matrix - st = fscanf(f, "%d %d %lg %lg\n", &row, &col, &rval, &ival); - if (ferror(f) || (st == EOF)) { - fprintf(stderr, "Error: error %d reading Matrix Market file (entry %d)\n", st, i + 1); - return -1; - } - - // Switch to 0-based indexing - --row; - --col; - - // Record entry - cooRowInd[j] = row; - cooColInd[j] = col; - if (cooRVal != NULL) cooRVal[j] = rval; - if (cooIVal != NULL) cooIVal[j] = ival; - ++j; - - // Add symmetric complement of non-diagonal entries - if (tg && !mm_is_general(t) && (row != col)) { - // Modify entry value if matrix is skew symmetric or Hermitian - if (mm_is_skew(t)) { - rval = -rval; - ival = -ival; - } else if (mm_is_hermitian(t)) { - ival = -ival; - } - - // Record entry - cooRowInd[j] = col; - cooColInd[j] = row; - if (cooRVal != NULL) cooRVal[j] = rval; - if (cooIVal != NULL) cooIVal[j] = ival; - ++j; - } - } - return 0; -} - -int read_binary_vector(FILE* fpin, int n, std::vector& val) -{ - size_t is_read1; - - double* t_storage = new double[n]; - is_read1 = fread(t_storage, sizeof(double), n, fpin); - for (int i = 0; i < n; i++) { - if (t_storage[i] == DBL_MAX) - val[i] = FLT_MAX; - else if (t_storage[i] == -DBL_MAX) - val[i] = -FLT_MAX; - else - val[i] = static_cast(t_storage[i]); - } - delete[] t_storage; - - if (is_read1 != (size_t)n) { - printf("%s", "I/O fail\n"); - return 1; - } - return 0; -} - -int read_binary_vector(FILE* fpin, int n, std::vector& val) -{ - size_t is_read1; - - is_read1 = fread(&val[0], sizeof(double), n, fpin); - - if (is_read1 != (size_t)n) { - printf("%s", "I/O fail\n"); - return 1; - } - return 0; -} + ValueType_* cooIVal); // FIXME: A similar function could be useful for CSC format // There are functions above that operate coo -> csr and coo->csc /** * @tparam */ -template -std::unique_ptr> generate_graph_csr_from_mm(bool& directed, - std::string mm_file) -{ - VT number_of_vertices; - ET number_of_edges; - - FILE* fpin = fopen(mm_file.c_str(), "r"); - EXPECT_NE(fpin, nullptr); - - VT number_of_columns = 0; - MM_typecode mm_typecode{0}; - EXPECT_EQ(mm_properties( - fpin, 1, &mm_typecode, &number_of_vertices, &number_of_columns, &number_of_edges), - 0); - EXPECT_TRUE(mm_is_matrix(mm_typecode)); - EXPECT_TRUE(mm_is_coordinate(mm_typecode)); - EXPECT_FALSE(mm_is_complex(mm_typecode)); - EXPECT_FALSE(mm_is_skew(mm_typecode)); - - directed = !mm_is_symmetric(mm_typecode); - - // Allocate memory on host - std::vector coo_row_ind(number_of_edges); - std::vector coo_col_ind(number_of_edges); - std::vector coo_val(number_of_edges); - - // Read - EXPECT_EQ((mm_to_coo( - fpin, 1, number_of_edges, &coo_row_ind[0], &coo_col_ind[0], &coo_val[0], NULL)), - 0); - EXPECT_EQ(fclose(fpin), 0); - - cugraph::GraphCOOView cooview( - &coo_row_ind[0], &coo_col_ind[0], &coo_val[0], number_of_vertices, number_of_edges); - - return cugraph::coo_to_csr(cooview); -} - -//////////////////////////////////////////////////////////////////////////////// -// FIXME: move this code to rapids-core -//////////////////////////////////////////////////////////////////////////////// +template +std::unique_ptr> generate_graph_csr_from_mm( + bool& directed, std::string mm_file); // Define RAPIDS_DATASET_ROOT_DIR using a preprocessor variable to // allow for a build to override the default. This is useful for @@ -331,89 +112,260 @@ static const std::string& get_rapids_dataset_root_dir() return rdrd; } +// returns a tuple of (rows, columns, weights, number_of_vertices, is_symmetric) template -struct edgelist_from_market_matrix_file_t { - std::vector h_rows{}; - std::vector h_cols{}; - std::vector h_weights{}; - vertex_t number_of_vertices{}; - bool is_symmetric{}; -}; +std::tuple, + rmm::device_uvector, + std::optional>, + vertex_t, + bool> +read_edgelist_from_matrix_market_file(raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted); + +// renumber must be true if multi_gpu is true +template +std::tuple, + std::optional>> +read_graph_from_matrix_market_file(raft::handle_t const& handle, + std::string const& graph_file_full_path, + bool test_weighted, + bool renumber); + +template +std::tuple, + std::optional>> +generate_graph_from_rmat_params(raft::handle_t const& handle, + size_t scale, + size_t edge_factor, + double a, + double b, + double c, + uint64_t seed, + bool undirected, + bool scramble_vertex_ids, + bool test_weighted, + bool renumber, + std::vector const& partition_ids, + size_t num_partitions); + +// alias for easy customization for debug purposes: +// +template +using vector_test_t = rmm::device_uvector; template -edgelist_from_market_matrix_file_t read_edgelist_from_matrix_market_file( - std::string const& graph_file_full_path) +decltype(auto) make_graph(raft::handle_t const& handle, + std::vector const& v_src, + std::vector const& v_dst, + std::optional> const& v_w, + vertex_t num_vertices, + edge_t num_edges) +{ + using namespace cugraph::experimental; + + vector_test_t d_src(num_edges, handle.get_stream()); + vector_test_t d_dst(num_edges, handle.get_stream()); + auto d_w = v_w ? std::make_optional>(num_edges, handle.get_stream()) + : std::nullopt; + + raft::update_device(d_src.data(), v_src.data(), d_src.size(), handle.get_stream()); + raft::update_device(d_dst.data(), v_dst.data(), d_dst.size(), handle.get_stream()); + if (d_w) { + raft::update_device((*d_w).data(), (*v_w).data(), (*d_w).size(), handle.get_stream()); + } + + cugraph::experimental::graph_t graph(handle); + std::tie(graph, std::ignore) = + cugraph::experimental::create_graph_from_edgelist( + handle, + std::nullopt, + std::move(d_src), + std::move(d_dst), + std::move(d_w), + cugraph::experimental::graph_properties_t{false, false}, + false); + + return graph; +} + +// compares single GPU CSR graph data: +// (for testing / debugging); +// on first == false, second == brief description of what is different; +// +template +std::pair compare_graphs(raft::handle_t const& handle, + left_graph_t const& lgraph, + right_graph_t const& rgraph) +{ + if constexpr (left_graph_t::is_multi_gpu && right_graph_t::is_multi_gpu) { + // no support for comparing distributed graphs, yet: + // + CUGRAPH_FAIL("Unsupported graph type for comparison."); + return std::make_pair(false, std::string("unsupported")); + } else if constexpr (!std::is_same_v) { + return std::make_pair(false, std::string("type")); + } else { + // both graphs are single GPU: + // + using graph_t = left_graph_t; + + using vertex_t = typename graph_t::vertex_type; + using edge_t = typename graph_t::edge_type; + using weight_t = typename graph_t::weight_type; + + size_t num_vertices = lgraph.get_number_of_vertices(); + size_t num_edges = lgraph.get_number_of_edges(); + + { + size_t r_num_vertices = rgraph.get_number_of_vertices(); + size_t r_num_edges = rgraph.get_number_of_edges(); + + if (num_vertices != r_num_vertices) return std::make_pair(false, std::string("num_vertices")); + + if (num_edges != r_num_edges) return std::make_pair(false, std::string("num_edges")); + } + + if (lgraph.is_symmetric() != rgraph.is_symmetric()) + return std::make_pair(false, std::string("symmetric")); + + if (lgraph.is_multigraph() != rgraph.is_multigraph()) + return std::make_pair(false, std::string("multigraph")); + + bool is_weighted = lgraph.is_weighted(); + if (is_weighted != rgraph.is_weighted()) return std::make_pair(false, std::string("weighted")); + + auto lgraph_view = lgraph.view(); + auto rgraph_view = rgraph.view(); + + std::vector lv_ro(num_vertices + 1); + std::vector lv_ci(num_edges); + + raft::update_host(lv_ro.data(), + lgraph_view.get_matrix_partition_view().get_offsets(), + num_vertices + 1, + handle.get_stream()); + raft::update_host(lv_ci.data(), + lgraph_view.get_matrix_partition_view().get_indices(), + num_edges, + handle.get_stream()); + + std::vector rv_ro(num_vertices + 1); + std::vector rv_ci(num_edges); + + raft::update_host(rv_ro.data(), + rgraph_view.get_matrix_partition_view().get_offsets(), + num_vertices + 1, + handle.get_stream()); + raft::update_host(rv_ci.data(), + rgraph_view.get_matrix_partition_view().get_indices(), + num_edges, + handle.get_stream()); + + auto lv_vs = is_weighted ? std::make_optional>(num_edges) : std::nullopt; + auto rv_vs = is_weighted ? std::make_optional>(num_edges) : std::nullopt; + if (is_weighted) { + raft::update_host((*lv_vs).data(), + *(lgraph_view.get_matrix_partition_view().get_weights()), + num_edges, + handle.get_stream()); + + raft::update_host((*rv_vs).data(), + *(rgraph_view.get_matrix_partition_view().get_weights()), + num_edges, + handle.get_stream()); + } + + handle.get_stream_view().synchronize(); + + if (lv_ro != rv_ro) return std::make_pair(false, std::string("offsets")); + + for (size_t i = 0; i < num_vertices; ++i) { + auto first = lv_ro[i]; + auto last = lv_ro[i + 1]; + if (is_weighted) { + std::vector> lv_pairs(last - first); + std::vector> rv_pairs(last - first); + for (edge_t j = first; j < last; ++j) { + lv_pairs[j - first] = std::make_tuple(lv_ci[j], (*lv_vs)[j]); + rv_pairs[j - first] = std::make_tuple(rv_ci[j], (*rv_vs)[j]); + } + std::sort(lv_pairs.begin(), lv_pairs.end()); + std::sort(rv_pairs.begin(), rv_pairs.end()); + if (!std::equal(lv_pairs.begin(), lv_pairs.end(), rv_pairs.begin(), [](auto lhs, auto rhs) { + return std::get<0>(lhs) == std::get<0>(rhs); + })) + return std::make_pair(false, std::string("indices")); + if (!std::equal(lv_pairs.begin(), lv_pairs.end(), rv_pairs.begin(), [](auto lhs, auto rhs) { + return std::get<1>(lhs) == std::get<1>(rhs); + })) + return std::make_pair(false, std::string("values")); + } else { + std::sort(lv_ci.begin() + first, lv_ci.begin() + last); + std::sort(rv_ci.begin() + first, rv_ci.begin() + last); + if (!std::equal(lv_ci.begin() + first, lv_ci.begin() + last, rv_ci.begin() + first)) + return std::make_pair(false, std::string("indices")); + } + } + + if (lgraph_view.get_local_adj_matrix_partition_segment_offsets(0) != + rgraph_view.get_local_adj_matrix_partition_segment_offsets(0)) + return std::make_pair(false, std::string("segment offsets")); + + return std::make_pair(true, std::string{}); + } +} + +template +bool renumbered_vectors_same(raft::handle_t const& handle, + std::vector const& v1, + std::vector const& v2) { - edgelist_from_market_matrix_file_t ret{}; + if (v1.size() != v2.size()) return false; - MM_typecode mc{}; - vertex_t m{}; - edge_t nnz{}; + std::map map; - FILE* file = fopen(graph_file_full_path.c_str(), "r"); - CUGRAPH_EXPECTS(file != nullptr, "fopen failure."); + auto iter = thrust::make_zip_iterator(thrust::make_tuple(v1.begin(), v2.begin())); - edge_t tmp_m{}; - edge_t tmp_k{}; - auto mm_ret = cugraph::test::mm_properties(file, 1, &mc, &tmp_m, &tmp_k, &nnz); - CUGRAPH_EXPECTS(mm_ret == 0, "could not read Matrix Market file properties."); - m = static_cast(tmp_m); - CUGRAPH_EXPECTS(mm_is_matrix(mc) && mm_is_coordinate(mc) && !mm_is_complex(mc) && !mm_is_skew(mc), - "invalid Matrix Market file properties."); + std::for_each(iter, iter + v1.size(), [&map](auto pair) { + vertex_t e1 = thrust::get<0>(pair); + vertex_t e2 = thrust::get<1>(pair); - ret.h_rows.assign(nnz, vertex_t{0}); - ret.h_cols.assign(nnz, vertex_t{0}); - ret.h_weights.assign(nnz, weight_t{0.0}); - ret.number_of_vertices = m; - ret.is_symmetric = mm_is_symmetric(mc); + map[e1] = e2; + }); - mm_ret = cugraph::test::mm_to_coo( - file, 1, nnz, ret.h_rows.data(), ret.h_cols.data(), ret.h_weights.data(), nullptr); - CUGRAPH_EXPECTS(mm_ret == 0, "could not read matrix data"); + auto error_count = std::count_if(iter, iter + v1.size(), [&map](auto pair) { + vertex_t e1 = thrust::get<0>(pair); + vertex_t e2 = thrust::get<1>(pair); - auto file_ret = fclose(file); - CUGRAPH_EXPECTS(file_ret == 0, "fclose failure."); + return (map[e1] != e2); + }); - return std::move(ret); + return (error_count == 0); } -template -cugraph::experimental::graph_t -read_graph_from_matrix_market_file(raft::handle_t const& handle, - std::string const& graph_file_full_path, - bool test_weighted) +template +bool renumbered_vectors_same(raft::handle_t const& handle, + rmm::device_uvector const& v1, + rmm::device_uvector const& v2) { - auto mm_graph = - read_edgelist_from_matrix_market_file(graph_file_full_path); - edge_t number_of_edges = static_cast(mm_graph.h_rows.size()); - - rmm::device_uvector d_edgelist_rows(number_of_edges, handle.get_stream()); - rmm::device_uvector d_edgelist_cols(number_of_edges, handle.get_stream()); - rmm::device_uvector d_edgelist_weights(test_weighted ? number_of_edges : 0, - handle.get_stream()); - - raft::update_device( - d_edgelist_rows.data(), mm_graph.h_rows.data(), number_of_edges, handle.get_stream()); - raft::update_device( - d_edgelist_cols.data(), mm_graph.h_cols.data(), number_of_edges, handle.get_stream()); - if (test_weighted) { - raft::update_device( - d_edgelist_weights.data(), mm_graph.h_weights.data(), number_of_edges, handle.get_stream()); - } + if (v1.size() != v2.size()) return false; + + std::vector h_v1(v1.size()); + std::vector h_v2(v1.size()); + + raft::update_host(h_v1.data(), v1.data(), v1.size(), handle.get_stream()); + raft::update_host(h_v2.data(), v2.data(), v2.size(), handle.get_stream()); - cugraph::experimental::edgelist_t edgelist{ - d_edgelist_rows.data(), - d_edgelist_cols.data(), - test_weighted ? d_edgelist_weights.data() : nullptr, - number_of_edges}; - - return cugraph::experimental::graph_t( - handle, - edgelist, - mm_graph.number_of_vertices, - cugraph::experimental::graph_properties_t{mm_graph.is_symmetric, false}, - false, - true); + return renumbered_vectors_same(handle, h_v1, h_v2); } } // namespace test diff --git a/cpp/tests/utilities/thrust_wrapper.cu b/cpp/tests/utilities/thrust_wrapper.cu new file mode 100644 index 00000000000..ae36582d18d --- /dev/null +++ b/cpp/tests/utilities/thrust_wrapper.cu @@ -0,0 +1,133 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include + +#include +#include + +namespace cugraph { +namespace test { + +template +std::tuple, rmm::device_uvector> sort_by_key( + raft::handle_t const& handle, vertex_t const* keys, value_t const* values, size_t num_pairs) +{ + rmm::device_uvector sorted_keys(num_pairs, handle.get_stream_view()); + rmm::device_uvector sorted_values(num_pairs, handle.get_stream_view()); + + thrust::copy( + rmm::exec_policy(handle.get_stream_view()), keys, keys + num_pairs, sorted_keys.begin()); + thrust::copy( + rmm::exec_policy(handle.get_stream_view()), values, values + num_pairs, sorted_values.begin()); + + thrust::sort_by_key(rmm::exec_policy(handle.get_stream_view()), + sorted_keys.begin(), + sorted_keys.end(), + sorted_values.begin()); + + return std::make_tuple(std::move(sorted_keys), std::move(sorted_values)); +} + +template std::tuple, rmm::device_uvector> +sort_by_key(raft::handle_t const& handle, + int32_t const* keys, + float const* values, + size_t num_pairs); + +template std::tuple, rmm::device_uvector> +sort_by_key(raft::handle_t const& handle, + int32_t const* keys, + double const* values, + size_t num_pairs); + +template std::tuple, rmm::device_uvector> +sort_by_key(raft::handle_t const& handle, + int32_t const* keys, + int32_t const* values, + size_t num_pairs); + +template std::tuple, rmm::device_uvector> +sort_by_key(raft::handle_t const& handle, + int64_t const* keys, + float const* values, + size_t num_pairs); + +template std::tuple, rmm::device_uvector> +sort_by_key(raft::handle_t const& handle, + int64_t const* keys, + double const* values, + size_t num_pairs); + +template std::tuple, rmm::device_uvector> +sort_by_key(raft::handle_t const& handle, + int64_t const* keys, + int64_t const* values, + size_t num_pairs); + +template +void translate_vertex_ids(raft::handle_t const& handle, + rmm::device_uvector& d_src_v, + rmm::device_uvector& d_dst_v, + vertex_t vertex_id_offset) +{ + thrust::transform(rmm::exec_policy(handle.get_stream()), + d_src_v.begin(), + d_src_v.end(), + d_src_v.begin(), + [offset = vertex_id_offset] __device__(vertex_t v) { return offset + v; }); + + thrust::transform(rmm::exec_policy(handle.get_stream()), + d_dst_v.begin(), + d_dst_v.end(), + d_dst_v.begin(), + [offset = vertex_id_offset] __device__(vertex_t v) { return offset + v; }); +} + +template +void populate_vertex_ids(raft::handle_t const& handle, + rmm::device_uvector& d_vertices_v, + vertex_t vertex_id_offset) +{ + thrust::sequence(rmm::exec_policy(handle.get_stream()), + d_vertices_v.begin(), + d_vertices_v.end(), + vertex_id_offset); +} + +template void translate_vertex_ids(raft::handle_t const& handle, + rmm::device_uvector& d_src_v, + rmm::device_uvector& d_dst_v, + int32_t vertex_id_offset); + +template void translate_vertex_ids(raft::handle_t const& handle, + rmm::device_uvector& d_src_v, + rmm::device_uvector& d_dst_v, + int64_t vertex_id_offset); + +template void populate_vertex_ids(raft::handle_t const& handle, + rmm::device_uvector& d_vertices_v, + int32_t vertex_id_offset); + +template void populate_vertex_ids(raft::handle_t const& handle, + rmm::device_uvector& d_vertices_v, + int64_t vertex_id_offset); + +} // namespace test +} // namespace cugraph diff --git a/cpp/tests/utilities/thrust_wrapper.hpp b/cpp/tests/utilities/thrust_wrapper.hpp new file mode 100644 index 00000000000..45208a6b921 --- /dev/null +++ b/cpp/tests/utilities/thrust_wrapper.hpp @@ -0,0 +1,41 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include + +namespace cugraph { +namespace test { + +template +std::tuple, rmm::device_uvector> sort_by_key( + raft::handle_t const& handle, vertex_t const* keys, value_t const* values, size_t num_pairs); + +template +void translate_vertex_ids(raft::handle_t const& handle, + rmm::device_uvector& d_src_v, + rmm::device_uvector& d_dst_v, + vertex_t vertex_id_offset); + +template +void populate_vertex_ids(raft::handle_t const& handle, + rmm::device_uvector& d_vertices_v, + vertex_t vertex_id_offset); + +} // namespace test +} // namespace cugraph diff --git a/cpp/tests/visitors/bfs_test.cpp b/cpp/tests/visitors/bfs_test.cpp new file mode 100644 index 00000000000..0e216683b0c --- /dev/null +++ b/cpp/tests/visitors/bfs_test.cpp @@ -0,0 +1,290 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Andrei Schaffer, aschaffer@nvidia.com +// + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +// visitor artifacts: +// +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +template +void bfs_reference(edge_t* offsets, + vertex_t* indices, + vertex_t* distances, + vertex_t* predecessors, + vertex_t num_vertices, + vertex_t source, + vertex_t depth_limit = std::numeric_limits::max()) +{ + vertex_t depth{0}; + + std::fill(distances, distances + num_vertices, std::numeric_limits::max()); + std::fill(predecessors, + predecessors + num_vertices, + cugraph::experimental::invalid_vertex_id::value); + + *(distances + source) = depth; + std::vector cur_frontier_rows{source}; + std::vector new_frontier_rows{}; + + while (cur_frontier_rows.size() > 0) { + for (auto const row : cur_frontier_rows) { + auto nbr_offset_first = *(offsets + row); + auto nbr_offset_last = *(offsets + row + 1); + for (auto nbr_offset = nbr_offset_first; nbr_offset != nbr_offset_last; ++nbr_offset) { + auto nbr = *(indices + nbr_offset); + if (*(distances + nbr) == std::numeric_limits::max()) { + *(distances + nbr) = depth + 1; + *(predecessors + nbr) = row; + new_frontier_rows.push_back(nbr); + } + } + } + std::swap(cur_frontier_rows, new_frontier_rows); + new_frontier_rows.clear(); + ++depth; + if (depth >= depth_limit) { break; } + } + + return; +} + +typedef struct BFS_Usecase_t { + std::string graph_file_full_path{}; + size_t source{false}; + + BFS_Usecase_t(std::string const& graph_file_path, size_t source) : source(source) + { + if ((graph_file_path.length() > 0) && (graph_file_path[0] != '/')) { + graph_file_full_path = cugraph::test::get_rapids_dataset_root_dir() + "/" + graph_file_path; + } else { + graph_file_full_path = graph_file_path; + } + }; +} BFS_Usecase; + +class Tests_BFS : public ::testing::TestWithParam { + public: + Tests_BFS() {} + static void SetupTestCase() {} + static void TearDownTestCase() {} + + virtual void SetUp() {} + virtual void TearDown() {} + + template + void run_current_test(BFS_Usecase const& configuration) + { + using namespace cugraph::experimental; + using namespace cugraph::visitors; + + using weight_t = float; + + raft::handle_t handle{}; + + bool test_weighted = false; + + // extract graph data from graph matrix file: + // + auto&& [d_src, d_dst, opt_d_w, num_vertices, is_sym] = + cugraph::test::read_edgelist_from_matrix_market_file( + handle, configuration.graph_file_full_path, test_weighted); + + graph_properties_t graph_props{is_sym, false}; + edge_t num_edges = d_dst.size(); + + std::optional opt_ptr_w; + if (opt_d_w.has_value()) { opt_ptr_w = opt_d_w->data(); } + + // to be filled: + // + cugraph::experimental::edgelist_t edgelist{ + d_src.data(), d_dst.data(), opt_ptr_w, num_edges}; + bool sorted{false}; + bool check{false}; + + erased_pack_t ep_graph{&handle, &edgelist, &num_vertices, &graph_props, &sorted, &check}; + + DTypes vertex_tid = reverse_dmap_t::type_id; + DTypes edge_tid = reverse_dmap_t::type_id; + DTypes weight_tid = reverse_dmap_t::type_id; + bool st = false; + bool mg = false; + GTypes graph_tid = GTypes::GRAPH_T; + + graph_envelope_t graph_envelope{vertex_tid, edge_tid, weight_tid, st, mg, graph_tid, ep_graph}; + + auto const* p_graph = dynamic_cast const*>( + graph_envelope.graph().get()); + + auto graph_view = p_graph->view(); + + std::vector h_offsets(graph_view.get_number_of_vertices() + 1); + std::vector h_indices(graph_view.get_number_of_edges()); + raft::update_host(h_offsets.data(), + graph_view.get_matrix_partition_view().get_offsets(), + graph_view.get_number_of_vertices() + 1, + handle.get_stream()); + raft::update_host(h_indices.data(), + graph_view.get_matrix_partition_view().get_indices(), + graph_view.get_number_of_edges(), + handle.get_stream()); + CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); + + ASSERT_TRUE(configuration.source >= 0 && + configuration.source <= graph_view.get_number_of_vertices()) + << "Starting sources should be >= 0 and" + << " less than the number of vertices in the graph."; + + std::vector h_reference_distances(graph_view.get_number_of_vertices()); + std::vector h_reference_predecessors(graph_view.get_number_of_vertices()); + + bfs_reference(h_offsets.data(), + h_indices.data(), + h_reference_distances.data(), + h_reference_predecessors.data(), + graph_view.get_number_of_vertices(), + static_cast(configuration.source), + std::numeric_limits::max()); + + rmm::device_uvector d_distances(graph_view.get_number_of_vertices(), + handle.get_stream()); + rmm::device_uvector d_predecessors(graph_view.get_number_of_vertices(), + handle.get_stream()); + + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + { + // visitors version: + // + // using namespace cugraph::experimental; + + // in a context where dependent types are known, + // type-erasing the graph is not necessary, + // hence the `_wrapper()` is not necessary; + // + + // packing visitor arguments = bfs algorithm arguments + // + vertex_t* p_d_dist = d_distances.begin(); + vertex_t* p_d_predec = d_predecessors.begin(); + auto src = static_cast(configuration.source); + bool dir_opt{false}; + auto depth_l = std::numeric_limits::max(); + bool check{false}; + erased_pack_t ep{ + &handle, p_d_dist, p_d_predec, &src, &dir_opt, &depth_l, &check}; // args for bfs() + + // several options to run the BFS algorithm: + // + // (1.) if a graph object already exists, + // we can use it to make the appropriate + // visitor: + // + // auto v_uniq_ptr = make_visitor( + // *p_graph, + // [](graph_envelope_t::visitor_factory_t const& vfact, erased_pack_t& parg) { + // return vfact.make_bfs_visitor(parg); + // }, + // ep); + // p_graph->apply(*v_uniq_ptr); + + // (2.) if a graph object already exists, alternatively we can + // explicitly instantiate the factory and call its make method: + // + // dependent_factory_t visitor_factory{}; // okay + // auto v_uniq_ptr = visitor_factory.make_bfs_visitor(ep); // okay + // p_graph->apply(*v_uniq_ptr); + + // (3.) if only the `graph_envelope_t` object exists, + // we can invoke the algorithm via the wrapper: + // + return_t ret = cugraph::api::bfs(graph_envelope, ep); + } + + CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement + + std::vector h_cugraph_distances(graph_view.get_number_of_vertices()); + std::vector h_cugraph_predecessors(graph_view.get_number_of_vertices()); + + raft::update_host( + h_cugraph_distances.data(), d_distances.data(), d_distances.size(), handle.get_stream()); + raft::update_host(h_cugraph_predecessors.data(), + d_predecessors.data(), + d_predecessors.size(), + handle.get_stream()); + CUDA_TRY(cudaStreamSynchronize(handle.get_stream())); + + ASSERT_TRUE(std::equal( + h_reference_distances.begin(), h_reference_distances.end(), h_cugraph_distances.begin())) + << "distances do not match with the reference values."; + + for (auto it = h_cugraph_predecessors.begin(); it != h_cugraph_predecessors.end(); ++it) { + auto i = std::distance(h_cugraph_predecessors.begin(), it); + if (*it == cugraph::experimental::invalid_vertex_id::value) { + ASSERT_TRUE(h_reference_predecessors[i] == *it) + << "vertex reachability do not match with the reference."; + } else { + ASSERT_TRUE(h_reference_distances[*it] + 1 == h_reference_distances[i]) + << "distance to this vertex != distance to the predecessor vertex + 1."; + bool found{false}; + for (auto j = h_offsets[*it]; j < h_offsets[*it + 1]; ++j) { + if (h_indices[j] == i) { + found = true; + break; + } + } + ASSERT_TRUE(found) << "no edge from the predecessor vertex to this vertex."; + } + } + } +}; + +// FIXME: add tests for type combinations +TEST_P(Tests_BFS, CheckInt32Int32) { run_current_test(GetParam()); } + +INSTANTIATE_TEST_CASE_P(simple_test, + Tests_BFS, + ::testing::Values(BFS_Usecase("test/datasets/karate.mtx", 0), + BFS_Usecase("test/datasets/polbooks.mtx", 0), + BFS_Usecase("test/datasets/netscience.mtx", 0), + BFS_Usecase("test/datasets/netscience.mtx", 100), + BFS_Usecase("test/datasets/wiki2003.mtx", 1000), + BFS_Usecase("test/datasets/wiki-Talk.mtx", 1000))); + +CUGRAPH_TEST_PROGRAM_MAIN() diff --git a/datasets/README.md b/datasets/README.md index c7f76a91dfe..e42413fc996 100644 --- a/datasets/README.md +++ b/datasets/README.md @@ -1,67 +1,132 @@ -# Cugraph test and benchmark data - -## Python - -This directory contains small public datasets in `mtx` and `csv` format used by cuGraph's python tests. Graph details: - -| Graph | V | E | Directed | Weighted | -| ------------- | ----- | ----- | -------- | -------- | -| karate | 34 | 156 | No | No | -| dolphin | 62 | 318 | No | No | -| netscience | 1,589 | 5,484 | No | Yes | - -**karate** : The graph "karate" contains the network of friendships between the 34 members of a karate club at a US university, as described by Wayne Zachary in 1977. - -**dolphin** : The graph dolphins contains an undirected social network of frequent associations between 62 dolphins in a community living off Doubtful Sound, New Zealand, as compiled by Lusseau et al. (2003). - -**netscience** : The graph netscience contains a coauthorship network of scientists working on network theory and experiment, as compiled by M. Newman in May 2006. - -## C++ -Cugraph's C++ analytics tests need larger datasets (>5GB uncompressed) and reference results (>125MB uncompressed). They can be downloaded by running the provided script from the `datasets` directory. -``` -cd /datasets -./get_test_data.sh -``` -You may run this script from elsewhere and store C++ test input to another location. - -Before running the tests, you should let cuGraph know where to find the test input by using: -``` -export RAPIDS_DATASET_ROOT_DIR= -``` - -## Benchmarks -Cugraph benchmarks (which can be found [here](../benchmarks)) also use datasets installed to this folder. Because the datasets used for benchmarking are also quite large (~14GB uncompressed), they are not installed by default. To install datasets for benchmarks, run the same script shown above from the `datasets` directory using the `--benchmark` option: -``` -cd /datasets -./get_test_data.sh --benchmark -``` -The datasets installed for benchmarks currently include CSV files for use in creating both directed and undirected graphs: -``` -/datasets/csv - |- directed - |--- cit-Patents.csv (250M) - |--- soc-LiveJournal1.csv (965M) - |- undirected - |--- europe_osm.csv (1.8G) - |--- hollywood.csv (1.5G) - |--- soc-twitter-2010.csv (8.8G) -``` -The benchmark datasets are described below: -| Graph | V | E | Directed | Weighted | -| ----------------- | ---------- | ------------- | -------- | -------- | -| cit-Patents | 3,774,768 | 16,518,948 | Yes | No | -| soc-LiveJournal1 | 4,847,571 | 43,369,619 | Yes | No | -| europe_osm | 50,912,018 | 54,054,660 | No | No | -| hollywood | 1,139,905 | 57,515,616 | No | No | -| soc-twitter-2010 | 21,297,772 | 265,025,809 | No | No | - -**cit-Patents** : A citation graph that includes all citations made by patents granted between 1975 and 1999, totaling 16,522,438 citations. -**soc-LiveJournal** : A graph of the LiveJournal social network. -**europe_osm** : A graph of OpenStreetMap data for Europe. -**hollywood** : A graph of movie actors where vertices are actors, and two actors are joined by an edge whenever they appeared in a movie together. -**soc-twitter-2010** : A network of follower relationships from a snapshot of Twitter in 2010, where an edge from i to j indicates that j is a follower of i. - -_NOTE: the benchmark datasets were converted to a CSV format from their original format described in the reference URL below, and in doing so had edge weights and isolated vertices discarded._ - -## Reference -The SuiteSparse Matrix Collection (formerly the University of Florida Sparse Matrix Collection) : https://sparse.tamu.edu/ +# Cugraph test and benchmark data + +## Python + +This directory contains small public datasets in `mtx` and `csv` format used by cuGraph's python tests. Graph details: + +| Graph | V | E | Directed | Weighted | +| ------------- | ----- | ----- | -------- | -------- | +| karate | 34 | 156 | No | No | +| dolphin | 62 | 318 | No | No | +| netscience | 1,589 | 5,484 | No | Yes | + +**karate** : The graph "karate" contains the network of friendships between the 34 members of a karate club at a US university, as described by Wayne Zachary in 1977. + +**dolphin** : The graph dolphins contains an undirected social network of frequent associations between 62 dolphins in a community living off Doubtful Sound, New Zealand, as compiled by Lusseau et al. (2003). + +**netscience** : The graph netscience contains a coauthorship network of scientists working on network theory and experiment, as compiled by M. Newman in May 2006. + + + +### Modified datasets + +The datasets below were added to provide input that contains self-loops, string vertex IDs, isolated vertices, and multiple edges. + +| Graph | V | E | Directed | Weighted | self-loops | Isolated V | String V IDs | Multi-edges | +| ------------------- | ------- | ---------- | -------- | --------- | ---------- | ---------- | ------------ | ----------- | +| karate_multi_edge | 34 | 160 | No | Yes | No | No | No | Yes | +| dolphins_multi_edge | 62 | 325 | No | Yes | No | No | No | Yes | +| karate_s_loop | 34 | 160 | No | Yes | Yes | No | No | No | +| dolphins_s_loop | 62 | 321 | No | Yes | Yes | No | No | No | +| karate_mod | 37 | 156 | No | No | No | Yes | No | No | +| karate_str | 34 | 156 | No | Yes | No | No | Yes | No | + +**karate_multi_edge** : The graph "karate_multi_edge" is a modified version of the "karate" graph where multi-edges were added + +**dolphins_multi_edge** : The graph "dolphins_multi_edge" is a modified version of the "dolphin" graph where multi-edges were added + +**karate_s_loop** : The graph "karate_s_loop" is a modified version of the "karate" graph where self-loops were added + +**dolphins_s_loop** : The graph "dolphins_s_loop" is a modified version of the "dolphin" graph where self-loops were added + +**karate_mod** : The graph "karate_mod" is a modified version of the "karate" graph where vertices and edges were added + +**karate_str** : The graph "karate_str" contains the network of friendships between the 34 members of a karate club at a US university, as described by Wayne Zachary in 1977. The integer vertices were replaced by strings + + +### Additional datasets + +Larger datasets containing self-loops can be downloaded by running the provided script from the `datasets` directory using the `--self_loops` +option: +``` +cd /datasets +./get_test_data.sh --self_loops +``` +``` +/datasets/self_loops + |-ca-AstroPh (5.3M) + |-ca-CondMat (2.8M) + |-ca-GrQc (348K) + |-ca-HepTh (763K) +``` +These datasets are not currently used by any tests or benchmarks + +| Graph | V | E | Directed | Weighted | self-loops | Isolated V | String V IDs | Multi-edges | +| ------------- | ------- | -------- | -------- | -------- | ---------- | ---------- | ------------ | ----------- | +| ca-AstroPh | 18,772 | 198,110 | No | No | Yes | No | No | No | +| ca-CondMat | 23,133 | 93,497 | No | Yes | Yes | No | No | No | +| ca-GrQc | 5,242 | 14,387 | No | No | Yes | No | No | No | +| ca-HepTh | 9,877 | 25,998 | No | Yes | Yes | No | No | No | + +**ca-AstroPh** : The graph "ca-AstroPh" covers scientific collaborations between authors papers submitted to Astro Physics category in the period from January 1993 to April 2003 (124 months), as described by J. Leskovec, J. Kleinberg and C. Faloutsos in 2007. + +**ca-CondMat** : The graph "ca-CondMat" covers scientific collaborations between authors papers submitted to Condense Matter category in the period from January 1993 to April 2003 (124 months), as described by J. Leskovec, J. Kleinberg and C. Faloutsos in 2007. + +**ca-GrQc** : The graph "ca-GrQc" covers scientific collaborations between authors papers submitted to General Relativity and Quantum Cosmology category in the period from January 1993 to April 2003 (124 months), as described by J. Leskovec, J. Kleinberg and C. Faloutsos in 2007. + +**ca-HepTh** : The graph "ca-HepTh" covers scientific collaborations between authors papers submitted to High Energy Physics - Theory category in the period from January 1993 to April 2003 (124 months), as described by J. Leskovec, J. Kleinberg and C. Faloutsos in 2007. + + +## Custom path to larger datasets directory + +Cugraph's C++ and Python analytics tests need larger datasets (>5GB uncompressed) and reference results (>125MB uncompressed). They can be downloaded by running the provided script from the `datasets` directory. +``` +cd /datasets +./get_test_data.sh +``` +You may run this script from elsewhere and store C++ or Python test input to another location. + +Before running the tests, you should let cuGraph know where to find the test input by using: +``` +export RAPIDS_DATASET_ROOT_DIR= +``` + + +## Benchmarks + +Cugraph benchmarks (which can be found [here](../benchmarks)) also use datasets installed to this folder. Because the datasets used for benchmarking are also quite large (~14GB uncompressed), they are not installed by default. To install datasets for benchmarks, run the same script shown above from the `datasets` directory using the `--benchmark` option: +``` +cd /datasets +./get_test_data.sh --benchmark +``` +The datasets installed for benchmarks currently include CSV files for use in creating both directed and undirected graphs: +``` +/datasets/csv + |- directed + |--- cit-Patents.csv (250M) + |--- soc-LiveJournal1.csv (965M) + |- undirected + |--- europe_osm.csv (1.8G) + |--- hollywood.csv (1.5G) + |--- soc-twitter-2010.csv (8.8G) +``` +The benchmark datasets are described below: +| Graph | V | E | Directed | Weighted | +| ----------------- | ---------- | ------------- | -------- | -------- | +| cit-Patents | 3,774,768 | 16,518,948 | Yes | No | +| soc-LiveJournal1 | 4,847,571 | 43,369,619 | Yes | No | +| europe_osm | 50,912,018 | 54,054,660 | No | No | +| hollywood | 1,139,905 | 57,515,616 | No | No | +| soc-twitter-2010 | 21,297,772 | 265,025,809 | No | No | + +**cit-Patents** : A citation graph that includes all citations made by patents granted between 1975 and 1999, totaling 16,522,438 citations. +**soc-LiveJournal** : A graph of the LiveJournal social network. +**europe_osm** : A graph of OpenStreetMap data for Europe. +**hollywood** : A graph of movie actors where vertices are actors, and two actors are joined by an edge whenever they appeared in a movie together. +**soc-twitter-2010** : A network of follower relationships from a snapshot of Twitter in 2010, where an edge from i to j indicates that j is a follower of i. + +_NOTE: the benchmark datasets were converted to a CSV format from their original format described in the reference URL below, and in doing so had edge weights and isolated vertices discarded._ + +## Reference +The SuiteSparse Matrix Collection (formerly the University of Florida Sparse Matrix Collection) : https://sparse.tamu.edu/ +The Stanford Network Analysis Platform (SNAP) diff --git a/datasets/dolphins_multi_edge.csv b/datasets/dolphins_multi_edge.csv new file mode 100644 index 00000000000..cf6bc70918e --- /dev/null +++ b/datasets/dolphins_multi_edge.csv @@ -0,0 +1,325 @@ +10 0 1.0 +14 0 1.0 +15 0 1.0 +40 0 1.0 +42 0 1.0 +47 0 1.0 +17 1 1.0 +19 1 1.0 +26 1 1.0 +27 1 1.0 +28 1 1.0 +36 1 1.0 +41 1 1.0 +54 1 1.0 +10 2 1.0 +42 2 1.0 +44 2 1.0 +61 2 1.0 +8 3 1.0 +14 3 1.0 +59 3 1.0 +51 4 1.0 +9 5 1.0 +13 5 1.0 +56 5 1.0 +57 5 1.0 +9 6 1.0 +13 6 1.0 +17 6 1.0 +54 6 1.0 +56 6 1.0 +57 6 1.0 +19 7 1.0 +27 7 1.0 +30 7 1.0 +40 7 1.0 +54 7 1.0 +20 8 1.0 +28 8 1.0 +37 8 1.0 +45 8 1.0 +59 8 1.0 +13 9 1.0 +17 9 1.0 +32 9 1.0 +41 9 1.0 +57 9 1.0 +29 10 1.0 +42 10 1.0 +47 10 1.0 +51 11 1.0 +33 12 1.0 +17 13 1.0 +32 13 1.0 +41 13 1.0 +54 13 1.0 +57 13 1.0 +16 14 1.0 +24 14 1.0 +33 14 1.0 +34 14 1.0 +37 14 1.0 +38 14 1.0 +40 14 1.0 +43 14 1.0 +50 14 1.0 +52 14 1.0 +18 15 1.0 +24 15 1.0 +40 15 1.0 +45 15 1.0 +55 15 1.0 +59 15 1.0 +20 16 1.0 +33 16 1.0 +37 16 1.0 +38 16 1.0 +50 16 1.0 +22 17 1.0 +25 17 1.0 +27 17 1.0 +31 17 1.0 +57 17 1.0 +20 18 1.0 +21 18 1.0 +24 18 1.0 +29 18 1.0 +45 18 1.0 +51 18 1.0 +30 19 1.0 +54 19 1.0 +28 20 1.0 +36 20 1.0 +38 20 1.0 +44 20 1.0 +47 20 1.0 +50 20 1.0 +29 21 1.0 +33 21 1.0 +37 21 1.0 +45 21 1.0 +51 21 1.0 +36 23 1.0 +45 23 1.0 +51 23 1.0 +29 24 1.0 +45 24 1.0 +51 24 1.0 +26 25 1.0 +27 25 1.0 +27 26 1.0 +30 28 1.0 +47 28 1.0 +35 29 1.0 +43 29 1.0 +45 29 1.0 +51 29 1.0 +52 29 1.0 +42 30 1.0 +47 30 1.0 +60 32 1.0 +34 33 1.0 +37 33 1.0 +38 33 1.0 +40 33 1.0 +43 33 1.0 +50 33 1.0 +37 34 1.0 +44 34 1.0 +49 34 1.0 +37 36 1.0 +39 36 1.0 +40 36 1.0 +59 36 1.0 +40 37 1.0 +43 37 1.0 +45 37 1.0 +61 37 1.0 +43 38 1.0 +44 38 1.0 +52 38 1.0 +58 38 1.0 +57 39 1.0 +52 40 1.0 +54 41 1.0 +54 41 1.0 +57 41 1.0 +47 42 1.0 +50 42 1.0 +50 42 1.0 +46 43 1.0 +53 43 1.0 +50 45 1.0 +51 45 1.0 +59 45 1.0 +59 45 1.0 +49 46 1.0 +57 48 1.0 +51 50 1.0 +55 51 1.0 +61 53 1.0 +57 54 1.0 +0 10 1.0 +0 14 1.0 +0 15 1.0 +59 45 1.0 +0 40 1.0 +0 42 1.0 +0 47 1.0 +1 17 1.0 +1 19 1.0 +1 26 1.0 +1 27 1.0 +1 28 1.0 +1 36 1.0 +1 41 1.0 +1 54 1.0 +2 10 1.0 +2 42 1.0 +2 44 1.0 +2 61 1.0 +54 41 1.0 +3 8 1.0 +3 14 1.0 +3 59 1.0 +4 51 1.0 +56 6 1.0 +5 9 1.0 +5 13 1.0 +5 56 1.0 +5 57 1.0 +6 9 1.0 +6 13 1.0 +6 17 1.0 +6 54 1.0 +6 56 1.0 +6 57 1.0 +7 19 1.0 +7 27 1.0 +7 30 1.0 +7 40 1.0 +7 54 1.0 +8 20 1.0 +8 28 1.0 +8 37 1.0 +8 45 1.0 +2 61 1.0 +8 59 1.0 +9 13 1.0 +9 17 1.0 +9 32 1.0 +9 41 1.0 +9 57 1.0 +10 29 1.0 +10 42 1.0 +10 47 1.0 +11 51 1.0 +12 33 1.0 +13 17 1.0 +13 32 1.0 +13 41 1.0 +13 54 1.0 +13 57 1.0 +14 16 1.0 +14 24 1.0 +14 33 1.0 +14 34 1.0 +14 37 1.0 +14 38 1.0 +14 40 1.0 +14 43 1.0 +14 50 1.0 +14 52 1.0 +15 18 1.0 +15 24 1.0 +15 40 1.0 +15 45 1.0 +15 55 1.0 +15 59 1.0 +16 20 1.0 +16 33 1.0 +16 37 1.0 +16 38 1.0 +16 50 1.0 +17 22 1.0 +17 25 1.0 +17 27 1.0 +17 31 1.0 +17 57 1.0 +18 20 1.0 +18 21 1.0 +18 24 1.0 +18 29 1.0 +18 45 1.0 +18 51 1.0 +19 30 1.0 +19 54 1.0 +20 28 1.0 +20 36 1.0 +20 38 1.0 +20 44 1.0 +20 47 1.0 +20 50 1.0 +21 29 1.0 +21 33 1.0 +21 37 1.0 +21 45 1.0 +21 51 1.0 +23 36 1.0 +23 45 1.0 +23 51 1.0 +24 29 1.0 +24 45 1.0 +24 51 1.0 +25 26 1.0 +25 27 1.0 +26 27 1.0 +28 30 1.0 +28 47 1.0 +29 35 1.0 +29 43 1.0 +29 45 1.0 +29 51 1.0 +29 52 1.0 +30 42 1.0 +30 47 1.0 +32 60 1.0 +33 34 1.0 +33 37 1.0 +33 38 1.0 +33 40 1.0 +33 43 1.0 +33 50 1.0 +34 37 1.0 +34 44 1.0 +34 49 1.0 +36 37 1.0 +36 39 1.0 +36 40 1.0 +36 59 1.0 +37 40 1.0 +37 43 1.0 +37 45 1.0 +37 61 1.0 +38 43 1.0 +38 44 1.0 +38 52 1.0 +38 58 1.0 +39 57 1.0 +40 52 1.0 +41 54 1.0 +41 57 1.0 +42 47 1.0 +42 50 1.0 +43 46 1.0 +43 53 1.0 +45 50 1.0 +45 51 1.0 +45 59 1.0 +46 49 1.0 +48 57 1.0 +50 51 1.0 +51 55 1.0 +53 61 1.0 +54 57 1.0 diff --git a/datasets/dolphins_s_loop.csv b/datasets/dolphins_s_loop.csv new file mode 100644 index 00000000000..703b8440afa --- /dev/null +++ b/datasets/dolphins_s_loop.csv @@ -0,0 +1,321 @@ +10 0 1.0 +14 0 1.0 +15 0 1.0 +40 0 1.0 +42 0 1.0 +47 0 1.0 +17 1 1.0 +19 1 1.0 +26 1 1.0 +27 1 1.0 +28 1 1.0 +36 1 1.0 +41 1 1.0 +54 1 1.0 +10 2 1.0 +42 2 1.0 +44 2 1.0 +61 2 1.0 +8 3 1.0 +14 3 1.0 +59 3 1.0 +51 4 1.0 +9 5 1.0 +13 5 1.0 +56 5 1.0 +57 5 1.0 +9 6 1.0 +13 6 1.0 +17 6 1.0 +54 6 1.0 +56 6 1.0 +57 6 1.0 +19 7 1.0 +27 7 1.0 +30 7 1.0 +40 7 1.0 +54 7 1.0 +20 8 1.0 +28 8 1.0 +37 8 1.0 +45 8 1.0 +59 8 1.0 +13 9 1.0 +17 9 1.0 +32 9 1.0 +41 9 1.0 +57 9 1.0 +29 10 1.0 +42 10 1.0 +47 10 1.0 +51 11 1.0 +33 12 1.0 +17 13 1.0 +32 13 1.0 +41 13 1.0 +54 13 1.0 +57 13 1.0 +16 14 1.0 +24 14 1.0 +33 14 1.0 +34 14 1.0 +37 14 1.0 +38 14 1.0 +40 14 1.0 +43 14 1.0 +50 14 1.0 +52 14 1.0 +18 15 1.0 +24 15 1.0 +40 15 1.0 +45 15 1.0 +55 15 1.0 +59 15 1.0 +20 16 1.0 +33 16 1.0 +37 16 1.0 +38 16 1.0 +50 16 1.0 +22 17 1.0 +25 17 1.0 +27 17 1.0 +31 17 1.0 +57 17 1.0 +20 18 1.0 +21 18 1.0 +24 18 1.0 +29 18 1.0 +45 18 1.0 +51 18 1.0 +30 19 1.0 +54 19 1.0 +28 20 1.0 +36 20 1.0 +38 20 1.0 +44 20 1.0 +47 20 1.0 +50 20 1.0 +29 21 1.0 +33 21 1.0 +37 21 1.0 +45 21 1.0 +51 21 1.0 +36 23 1.0 +45 23 1.0 +51 23 1.0 +29 24 1.0 +45 24 1.0 +51 24 1.0 +26 25 1.0 +27 25 1.0 +27 26 1.0 +30 28 1.0 +47 28 1.0 +35 29 1.0 +43 29 1.0 +45 29 1.0 +51 29 1.0 +52 29 1.0 +42 30 1.0 +47 30 1.0 +60 32 1.0 +34 33 1.0 +37 33 1.0 +38 33 1.0 +40 33 1.0 +43 33 1.0 +50 33 1.0 +37 34 1.0 +44 34 1.0 +49 34 1.0 +37 36 1.0 +39 36 1.0 +40 36 1.0 +59 36 1.0 +40 37 1.0 +43 37 1.0 +43 43 1.0 +45 37 1.0 +61 37 1.0 +43 38 1.0 +44 38 1.0 +52 38 1.0 +58 38 1.0 +57 39 1.0 +52 40 1.0 +52 52 1.0 +54 41 1.0 +57 41 1.0 +47 42 1.0 +50 42 1.0 +46 43 1.0 +53 43 1.0 +50 45 1.0 +51 45 1.0 +59 45 1.0 +49 46 1.0 +57 48 1.0 +51 50 1.0 +55 51 1.0 +61 53 1.0 +57 54 1.0 +0 10 1.0 +0 14 1.0 +0 15 1.0 +0 40 1.0 +0 42 1.0 +0 47 1.0 +1 17 1.0 +1 19 1.0 +1 26 1.0 +1 1 1.0 +1 27 1.0 +1 28 1.0 +1 36 1.0 +1 41 1.0 +1 54 1.0 +2 10 1.0 +2 42 1.0 +2 44 1.0 +2 61 1.0 +3 8 1.0 +3 14 1.0 +3 59 1.0 +4 51 1.0 +5 9 1.0 +5 13 1.0 +5 56 1.0 +5 57 1.0 +6 9 1.0 +6 13 1.0 +6 17 1.0 +6 54 1.0 +6 56 1.0 +6 57 1.0 +7 19 1.0 +7 27 1.0 +7 30 1.0 +7 40 1.0 +7 54 1.0 +8 20 1.0 +8 28 1.0 +8 37 1.0 +8 45 1.0 +8 59 1.0 +9 13 1.0 +9 17 1.0 +9 32 1.0 +9 41 1.0 +9 57 1.0 +10 29 1.0 +10 42 1.0 +10 47 1.0 +11 51 1.0 +12 33 1.0 +13 17 1.0 +13 32 1.0 +13 41 1.0 +13 54 1.0 +13 57 1.0 +14 16 1.0 +14 24 1.0 +14 33 1.0 +14 34 1.0 +14 37 1.0 +14 38 1.0 +14 40 1.0 +14 43 1.0 +14 50 1.0 +14 52 1.0 +15 18 1.0 +15 24 1.0 +15 40 1.0 +15 45 1.0 +15 55 1.0 +15 59 1.0 +16 20 1.0 +16 33 1.0 +16 37 1.0 +16 38 1.0 +16 50 1.0 +17 22 1.0 +17 25 1.0 +17 27 1.0 +17 31 1.0 +17 57 1.0 +18 20 1.0 +18 21 1.0 +18 24 1.0 +18 29 1.0 +18 45 1.0 +18 51 1.0 +19 30 1.0 +19 54 1.0 +20 28 1.0 +20 36 1.0 +20 38 1.0 +20 44 1.0 +20 47 1.0 +20 50 1.0 +21 29 1.0 +21 33 1.0 +21 37 1.0 +21 45 1.0 +21 51 1.0 +23 36 1.0 +23 45 1.0 +23 51 1.0 +24 29 1.0 +24 45 1.0 +24 51 1.0 +25 26 1.0 +25 27 1.0 +26 27 1.0 +28 30 1.0 +28 47 1.0 +29 35 1.0 +29 43 1.0 +29 45 1.0 +29 51 1.0 +29 52 1.0 +30 42 1.0 +30 47 1.0 +32 60 1.0 +33 34 1.0 +33 37 1.0 +33 38 1.0 +33 40 1.0 +33 43 1.0 +33 50 1.0 +34 37 1.0 +34 44 1.0 +34 49 1.0 +36 37 1.0 +36 39 1.0 +36 40 1.0 +36 59 1.0 +37 40 1.0 +37 43 1.0 +37 45 1.0 +37 61 1.0 +38 43 1.0 +38 44 1.0 +38 52 1.0 +38 58 1.0 +39 57 1.0 +40 52 1.0 +41 54 1.0 +41 57 1.0 +42 47 1.0 +42 50 1.0 +43 46 1.0 +43 53 1.0 +45 50 1.0 +45 51 1.0 +45 59 1.0 +46 49 1.0 +48 57 1.0 +50 51 1.0 +51 55 1.0 +53 61 1.0 +54 57 1.0 diff --git a/datasets/eil51.tsp b/datasets/eil51.tsp new file mode 100644 index 00000000000..543d1013c14 --- /dev/null +++ b/datasets/eil51.tsp @@ -0,0 +1,58 @@ +NAME : eil51 +COMMENT : 51-city problem (Christofides/Eilon) +TYPE : TSP +DIMENSION : 51 +EDGE_WEIGHT_TYPE : EUC_2D +NODE_COORD_SECTION +1 37 52 +2 49 49 +3 52 64 +4 20 26 +5 40 30 +6 21 47 +7 17 63 +8 31 62 +9 52 33 +10 51 21 +11 42 41 +12 31 32 +13 5 25 +14 12 42 +15 36 16 +16 52 41 +17 27 23 +18 17 33 +19 13 13 +20 57 58 +21 62 42 +22 42 57 +23 16 57 +24 8 52 +25 7 38 +26 27 68 +27 30 48 +28 43 67 +29 58 48 +30 58 27 +31 37 69 +32 38 46 +33 46 10 +34 61 33 +35 62 63 +36 63 69 +37 32 22 +38 45 35 +39 59 15 +40 5 6 +41 10 17 +42 21 10 +43 5 64 +44 30 15 +45 39 10 +46 32 39 +47 25 32 +48 25 55 +49 48 28 +50 56 37 +51 30 40 +EOF diff --git a/datasets/get_test_data.sh b/datasets/get_test_data.sh index 071a4b8dea3..0bd97b55cb5 100755 --- a/datasets/get_test_data.sh +++ b/datasets/get_test_data.sh @@ -1,3 +1,16 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + #!/bin/bash set -e set -o pipefail @@ -31,6 +44,10 @@ benchmark # ~1s download https://s3.us-east-2.amazonaws.com/rapidsai-data/cugraph/benchmark/hibench/hibench_1_small.tgz benchmark + +# ~0.6s download +https://rapidsai-data.s3.us-east-2.amazonaws.com/cugraph/test/tsplib/datasets.tar.gz +tsplib " EXTENDED_DATASET_DATA=" @@ -44,6 +61,12 @@ BENCHMARK_DATASET_DATA=" https://rapidsai-data.s3.us-east-2.amazonaws.com/cugraph/benchmark/benchmark_csv_data.tgz csv " + +SELF_LOOPS_DATASET_DATA=" +# ~1s download +https://rapidsai-data.s3.us-east-2.amazonaws.com/cugraph/benchmark/benchmark_csv_data_self_loops.tgz +self_loops +" ################################################################################ # Do not change the script below this line if only adding/updating a dataset @@ -54,7 +77,7 @@ function hasArg { } if hasArg -h || hasArg --help; then - echo "$0 [--subset | --benchmark]" + echo "$0 [--subset | --benchmark | --self_loops]" exit 0 fi @@ -63,6 +86,8 @@ if hasArg "--benchmark"; then DATASET_DATA="${BENCHMARK_DATASET_DATA}" elif hasArg "--subset"; then DATASET_DATA="${BASE_DATASET_DATA}" +elif hasArg "--self_loops"; then + DATASET_DATA="${SELF_LOOPS_DATASET_DATA}" # Do not include benchmark datasets by default - too big else DATASET_DATA="${BASE_DATASET_DATA} ${EXTENDED_DATASET_DATA}" diff --git a/datasets/gil262.tsp b/datasets/gil262.tsp new file mode 100755 index 00000000000..cfcb15c3b78 --- /dev/null +++ b/datasets/gil262.tsp @@ -0,0 +1,269 @@ +NAME : gil262 +COMMENT : 262-city problem (Gillet/Johnson) +TYPE : TSP +DIMENSION : 262 +EDGE_WEIGHT_TYPE : EUC_2D +NODE_COORD_SECTION +1 -99 -97 +2 -59 50 +3 0 14 +4 -17 -66 +5 -69 -19 +6 31 12 +7 5 -41 +8 -12 10 +9 -64 70 +10 -12 85 +11 -18 64 +12 -77 -16 +13 -53 88 +14 83 -24 +15 24 41 +16 17 21 +17 42 96 +18 -65 0 +19 -47 -26 +20 85 36 +21 -35 -54 +22 54 -21 +23 64 -17 +24 55 89 +25 17 -25 +26 -61 66 +27 -61 26 +28 17 -72 +29 79 38 +30 -62 -2 +31 -90 -68 +32 52 66 +33 -54 -50 +34 8 -84 +35 37 -90 +36 -83 49 +37 35 -1 +38 7 59 +39 12 48 +40 57 95 +41 92 28 +42 -3 97 +43 -7 52 +44 42 -15 +45 77 -43 +46 59 -49 +47 25 91 +48 69 -19 +49 -82 -14 +50 74 -70 +51 69 59 +52 29 33 +53 -97 9 +54 -58 9 +55 28 93 +56 7 73 +57 -28 73 +58 -76 55 +59 41 42 +60 92 40 +61 -84 -29 +62 -12 42 +63 51 -45 +64 -37 46 +65 -97 35 +66 14 89 +67 60 58 +68 -63 -75 +69 -18 34 +70 -46 -82 +71 -86 -79 +72 -43 -30 +73 -44 7 +74 -3 -20 +75 36 41 +76 -30 -94 +77 79 -62 +78 51 70 +79 -61 -26 +80 6 94 +81 -19 -62 +82 -20 51 +83 -81 37 +84 7 31 +85 52 12 +86 83 -91 +87 -7 -92 +88 82 -74 +89 -70 85 +90 -83 -30 +91 71 -61 +92 85 11 +93 66 -48 +94 78 -87 +95 9 -79 +96 -36 4 +97 66 39 +98 92 -17 +99 -46 -79 +100 -30 -63 +101 -42 63 +102 20 42 +103 15 98 +104 1 -17 +105 64 20 +106 -96 85 +107 93 -29 +108 -40 -84 +109 86 35 +110 91 36 +111 62 -8 +112 -24 4 +113 11 96 +114 -53 62 +115 -28 -71 +116 7 -4 +117 95 -9 +118 -3 17 +119 53 -90 +120 58 -19 +121 -83 84 +122 -1 49 +123 -4 17 +124 -82 -3 +125 -43 47 +126 6 -6 +127 70 99 +128 68 -29 +129 -94 -30 +130 -94 -20 +131 -21 77 +132 64 37 +133 -70 -19 +134 88 65 +135 2 29 +136 33 57 +137 -70 6 +138 -38 -56 +139 -80 -95 +140 -5 -39 +141 8 -22 +142 -61 -76 +143 76 -22 +144 49 -71 +145 -30 -68 +146 1 34 +147 77 79 +148 -58 64 +149 82 -97 +150 -80 55 +151 81 -86 +152 39 -49 +153 -67 72 +154 -25 -89 +155 -44 -95 +156 32 -68 +157 -17 49 +158 93 49 +159 99 81 +160 10 -49 +161 63 -41 +162 38 39 +163 -28 39 +164 -2 -47 +165 38 8 +166 -42 -6 +167 -67 88 +168 19 93 +169 40 27 +170 -61 56 +171 43 33 +172 -18 -39 +173 -69 19 +174 75 -18 +175 31 85 +176 25 58 +177 -16 36 +178 91 15 +179 60 -39 +180 49 -47 +181 42 33 +182 16 -81 +183 -78 53 +184 53 -80 +185 -46 -26 +186 -25 -54 +187 69 -46 +188 0 -78 +189 -84 74 +190 -16 16 +191 -63 -14 +192 51 -77 +193 -39 61 +194 5 97 +195 -55 39 +196 70 -14 +197 0 95 +198 -45 7 +199 38 -24 +200 50 -37 +201 59 71 +202 -73 -96 +203 -29 72 +204 -47 12 +205 -88 -61 +206 -88 36 +207 -46 -3 +208 26 -37 +209 -39 -67 +210 92 27 +211 -80 -31 +212 93 -50 +213 -20 -5 +214 -22 73 +215 -4 -7 +216 54 -48 +217 -70 39 +218 54 -82 +219 29 41 +220 -87 51 +221 -96 -36 +222 49 8 +223 -5 54 +224 -26 43 +225 -11 60 +226 40 61 +227 82 35 +228 -92 12 +229 -93 -86 +230 -66 63 +231 -72 -87 +232 -57 -84 +233 23 52 +234 -56 -62 +235 -19 59 +236 63 -14 +237 -13 38 +238 -19 87 +239 44 -84 +240 98 -17 +241 -16 62 +242 3 66 +243 26 22 +244 -38 -81 +245 70 80 +246 17 -35 +247 96 -83 +248 -77 80 +249 -14 44 +250 -33 33 +251 33 -33 +252 70 0 +253 -50 60 +254 -50 -60 +255 75 0 +256 0 75 +257 -75 0 +258 0 -75 +259 40 80 +260 40 -80 +261 -60 20 +262 -60 -20 +EOF diff --git a/datasets/karate_mod.mtx b/datasets/karate_mod.mtx new file mode 100644 index 00000000000..3a562406800 --- /dev/null +++ b/datasets/karate_mod.mtx @@ -0,0 +1,81 @@ +2 1 +3 1 +4 1 +5 1 +6 1 +7 1 +8 1 +9 1 +11 1 +12 1 +13 1 +14 1 +18 1 +20 1 +22 1 +32 1 +3 2 +4 2 +8 2 +14 2 +18 2 +20 2 +22 2 +31 2 +4 3 +8 3 +9 3 +10 3 +14 3 +28 3 +29 3 +33 3 +8 4 +13 4 +14 4 +7 5 +11 5 +7 6 +11 6 +17 6 +17 7 +31 9 +33 9 +34 9 +34 10 +34 14 +33 15 +34 15 +33 16 +34 16 +33 19 +34 19 +34 20 +33 21 +34 21 +33 23 +34 23 +26 24 +28 24 +30 24 +33 24 +34 24 +26 25 +28 25 +32 25 +32 26 +30 27 +34 27 +34 28 +32 29 +34 29 +33 30 +34 30 +33 31 +34 31 +33 32 +34 32 +34 33 +35 +36 +37 diff --git a/datasets/karate_multi_edge.csv b/datasets/karate_multi_edge.csv new file mode 100644 index 00000000000..6f331b77a59 --- /dev/null +++ b/datasets/karate_multi_edge.csv @@ -0,0 +1,160 @@ +1 0 1.0 +2 0 1.0 +3 0 1.0 +4 0 1.0 +5 0 1.0 +6 0 1.0 +7 0 1.0 +8 0 1.0 +10 0 1.0 +11 0 1.0 +12 0 1.0 +13 0 1.0 +17 0 1.0 +19 0 1.0 +21 0 1.0 +31 0 1.0 +2 1 1.0 +3 1 1.0 +7 1 1.0 +13 1 1.0 +7 0 1.0 +17 1 1.0 +19 1 1.0 +21 1 1.0 +30 1 1.0 +3 2 1.0 +7 2 1.0 +8 2 1.0 +9 2 1.0 +13 2 1.0 +27 2 1.0 +28 2 1.0 +32 2 1.0 +7 3 1.0 +12 3 1.0 +13 3 1.0 +6 4 1.0 +10 4 1.0 +6 5 1.0 +10 5 1.0 +16 5 1.0 +16 6 1.0 +30 8 1.0 +32 8 1.0 +33 8 1.0 +28 2 1.0 +33 9 1.0 +33 13 1.0 +32 14 1.0 +33 14 1.0 +32 15 1.0 +33 15 1.0 +32 18 1.0 +33 18 1.0 +33 19 1.0 +32 20 1.0 +33 20 1.0 +32 22 1.0 +33 22 1.0 +25 23 1.0 +27 23 1.0 +29 23 1.0 +32 23 1.0 +33 23 1.0 +25 24 1.0 +27 24 1.0 +31 24 1.0 +31 25 1.0 +29 26 1.0 +33 26 1.0 +33 27 1.0 +31 28 1.0 +33 28 1.0 +32 29 1.0 +33 29 1.0 +32 22 1.0 +32 30 1.0 +33 30 1.0 +32 31 1.0 +33 31 1.0 +33 32 1.0 +0 1 1.0 +0 2 1.0 +0 3 1.0 +0 4 1.0 +0 5 1.0 +0 6 1.0 +0 7 1.0 +0 8 1.0 +0 10 1.0 +0 11 1.0 +0 12 1.0 +0 6 1.0 +0 13 1.0 +0 17 1.0 +0 19 1.0 +0 21 1.0 +0 31 1.0 +1 2 1.0 +1 3 1.0 +1 7 1.0 +1 13 1.0 +1 17 1.0 +1 19 1.0 +1 21 1.0 +1 30 1.0 +2 3 1.0 +2 7 1.0 +2 8 1.0 +2 9 1.0 +2 13 1.0 +2 27 1.0 +2 28 1.0 +2 32 1.0 +3 7 1.0 +3 12 1.0 +3 13 1.0 +4 6 1.0 +4 10 1.0 +5 6 1.0 +5 10 1.0 +5 16 1.0 +6 16 1.0 +8 30 1.0 +8 32 1.0 +8 33 1.0 +9 33 1.0 +13 33 1.0 +14 32 1.0 +14 33 1.0 +15 32 1.0 +15 33 1.0 +18 32 1.0 +18 33 1.0 +19 33 1.0 +20 32 1.0 +20 33 1.0 +22 32 1.0 +22 33 1.0 +23 25 1.0 +23 27 1.0 +23 29 1.0 +23 32 1.0 +23 33 1.0 +24 25 1.0 +24 27 1.0 +24 31 1.0 +25 31 1.0 +26 29 1.0 +26 33 1.0 +27 33 1.0 +28 31 1.0 +28 33 1.0 +29 32 1.0 +29 33 1.0 +30 32 1.0 +30 33 1.0 +31 32 1.0 +31 33 1.0 +32 33 1.0 diff --git a/datasets/karate_s_loop.csv b/datasets/karate_s_loop.csv new file mode 100644 index 00000000000..3959e5f98b3 --- /dev/null +++ b/datasets/karate_s_loop.csv @@ -0,0 +1,160 @@ +1 0 1.0 +2 0 1.0 +3 0 1.0 +4 0 1.0 +5 0 1.0 +6 0 1.0 +7 0 1.0 +8 0 1.0 +10 0 1.0 +11 0 1.0 +12 0 1.0 +13 0 1.0 +17 0 1.0 +19 0 1.0 +21 0 1.0 +31 0 1.0 +2 1 1.0 +3 1 1.0 +7 1 1.0 +13 1 1.0 +17 1 1.0 +19 1 1.0 +21 1 1.0 +30 1 1.0 +3 2 1.0 +7 2 1.0 +8 2 1.0 +9 2 1.0 +13 2 1.0 +27 2 1.0 +28 2 1.0 +32 2 1.0 +7 3 1.0 +12 3 1.0 +13 3 1.0 +6 4 1.0 +10 4 1.0 +6 5 1.0 +10 5 1.0 +10 10 1.0 +16 5 1.0 +16 6 1.0 +30 8 1.0 +32 8 1.0 +33 8 1.0 +33 9 1.0 +33 13 1.0 +32 14 1.0 +33 14 1.0 +32 15 1.0 +33 15 1.0 +32 18 1.0 +33 18 1.0 +33 19 1.0 +32 20 1.0 +33 20 1.0 +32 22 1.0 +33 22 1.0 +25 23 1.0 +27 23 1.0 +29 23 1.0 +32 23 1.0 +33 23 1.0 +25 24 1.0 +27 24 1.0 +31 24 1.0 +31 25 1.0 +29 26 1.0 +33 26 1.0 +33 27 1.0 +31 28 1.0 +33 28 1.0 +32 29 1.0 +33 29 1.0 +32 30 1.0 +33 30 1.0 +32 31 1.0 +33 31 1.0 +33 32 1.0 +0 1 1.0 +0 2 1.0 +0 3 1.0 +0 4 1.0 +0 5 1.0 +0 6 1.0 +0 7 1.0 +0 8 1.0 +0 10 1.0 +0 11 1.0 +0 12 1.0 +0 13 1.0 +0 17 1.0 +0 19 1.0 +0 21 1.0 +0 31 1.0 +1 2 1.0 +1 3 1.0 +1 7 1.0 +1 13 1.0 +1 1 1.0 +1 17 1.0 +1 19 1.0 +1 21 1.0 +1 30 1.0 +2 3 1.0 +2 7 1.0 +2 8 1.0 +2 9 1.0 +2 13 1.0 +2 27 1.0 +2 28 1.0 +2 32 1.0 +3 7 1.0 +3 12 1.0 +3 13 1.0 +4 6 1.0 +4 10 1.0 +5 6 1.0 +5 10 1.0 +5 16 1.0 +6 16 1.0 +8 30 1.0 +8 32 1.0 +8 33 1.0 +9 33 1.0 +13 33 1.0 +13 13 1.0 +14 32 1.0 +14 33 1.0 +15 32 1.0 +15 33 1.0 +18 32 1.0 +18 33 1.0 +19 33 1.0 +20 32 1.0 +20 33 1.0 +22 32 1.0 +22 33 1.0 +23 25 1.0 +23 27 1.0 +23 29 1.0 +23 32 1.0 +23 33 1.0 +24 25 1.0 +24 27 1.0 +24 31 1.0 +25 31 1.0 +26 29 1.0 +26 33 1.0 +27 33 1.0 +28 31 1.0 +28 33 1.0 +29 32 1.0 +29 33 1.0 +30 32 1.0 +30 33 1.0 +31 32 1.0 +31 31 1.0 +31 33 1.0 +32 33 1.0 diff --git a/datasets/karate_str.mtx b/datasets/karate_str.mtx new file mode 100644 index 00000000000..0564d30f91d --- /dev/null +++ b/datasets/karate_str.mtx @@ -0,0 +1,78 @@ +9q a9 1 +ts a9 1 +kt a9 1 +j7 a9 1 +wr a9 1 +n3 a9 1 +2w a9 1 +8a a9 1 +ci a9 1 +cq a9 1 +ca a9 1 +gd a9 1 +y4 a9 1 +kx a9 1 +u3 a9 1 +id a9 1 +ts 9q 1 +kt 9q 1 +2w 9q 1 +gd 9q 1 +y4 9q 1 +kx 9q 1 +u3 9q 1 +7p 9q 1 +kt ts 1 +2w ts 1 +8a ts 1 +ax ts 1 +gd ts 1 +84 ts 1 +ar ts 1 +05 ts 1 +2w kt 1 +ca kt 1 +gd kt 1 +n3 j7 1 +ci j7 1 +n3 wr 1 +ci wr 1 +27 wr 1 +27 n3 1 +7p 8a 1 +05 8a 1 +ux 8a 1 +ux ax 1 +ux gd 1 +05 r9 1 +ux r9 1 +05 44 1 +ux 44 1 +05 a6 1 +ux a6 1 +ux kx 1 +05 d5 1 +ux d5 1 +05 gk 1 +ux gk 1 +fo em 1 +84 em 1 +wc em 1 +05 em 1 +ux em 1 +fo 1j 1 +84 1j 1 +id 1j 1 +id fo 1 +wc nm 1 +ux nm 1 +ux 84 1 +id ar 1 +ux ar 1 +05 wc 1 +ux wc 1 +05 7p 1 +ux 7p 1 +05 id 1 +ux id 1 +ux 05 1 diff --git a/datasets/kroA100.tsp b/datasets/kroA100.tsp new file mode 100644 index 00000000000..05ebae994ac --- /dev/null +++ b/datasets/kroA100.tsp @@ -0,0 +1,107 @@ +NAME: kroA100 +TYPE: TSP +COMMENT: 100-city problem A (Krolak/Felts/Nelson) +DIMENSION: 100 +EDGE_WEIGHT_TYPE : EUC_2D +NODE_COORD_SECTION +1 1380 939 +2 2848 96 +3 3510 1671 +4 457 334 +5 3888 666 +6 984 965 +7 2721 1482 +8 1286 525 +9 2716 1432 +10 738 1325 +11 1251 1832 +12 2728 1698 +13 3815 169 +14 3683 1533 +15 1247 1945 +16 123 862 +17 1234 1946 +18 252 1240 +19 611 673 +20 2576 1676 +21 928 1700 +22 53 857 +23 1807 1711 +24 274 1420 +25 2574 946 +26 178 24 +27 2678 1825 +28 1795 962 +29 3384 1498 +30 3520 1079 +31 1256 61 +32 1424 1728 +33 3913 192 +34 3085 1528 +35 2573 1969 +36 463 1670 +37 3875 598 +38 298 1513 +39 3479 821 +40 2542 236 +41 3955 1743 +42 1323 280 +43 3447 1830 +44 2936 337 +45 1621 1830 +46 3373 1646 +47 1393 1368 +48 3874 1318 +49 938 955 +50 3022 474 +51 2482 1183 +52 3854 923 +53 376 825 +54 2519 135 +55 2945 1622 +56 953 268 +57 2628 1479 +58 2097 981 +59 890 1846 +60 2139 1806 +61 2421 1007 +62 2290 1810 +63 1115 1052 +64 2588 302 +65 327 265 +66 241 341 +67 1917 687 +68 2991 792 +69 2573 599 +70 19 674 +71 3911 1673 +72 872 1559 +73 2863 558 +74 929 1766 +75 839 620 +76 3893 102 +77 2178 1619 +78 3822 899 +79 378 1048 +80 1178 100 +81 2599 901 +82 3416 143 +83 2961 1605 +84 611 1384 +85 3113 885 +86 2597 1830 +87 2586 1286 +88 161 906 +89 1429 134 +90 742 1025 +91 1625 1651 +92 1187 706 +93 1787 1009 +94 22 987 +95 3640 43 +96 3756 882 +97 776 392 +98 1724 1642 +99 198 1810 +100 3950 1558 +EOF diff --git a/datasets/tsp225.tsp b/datasets/tsp225.tsp new file mode 100644 index 00000000000..ac9e06cecc1 --- /dev/null +++ b/datasets/tsp225.tsp @@ -0,0 +1,232 @@ +NAME : tsp225 +COMMENT : A TSP problem (Reinelt) +TYPE : TSP +DIMENSION : 225 +EDGE_WEIGHT_TYPE : EUC_2D +NODE_COORD_SECTION +1 155.42 150.65 +2 375.92 164.65 +3 183.92 150.65 +4 205.42 150.65 +5 205.42 171.65 +6 226.42 171.65 +7 226.42 186.15 +8 226.42 207.15 +9 226.42 235.65 +10 226.42 264.15 +11 226.42 292.65 +12 226.42 314.15 +13 226.42 335.65 +14 205.42 335.65 +15 190.92 335.65 +16 190.92 328.15 +17 176.92 328.15 +18 176.92 299.65 +19 155.42 299.65 +20 155.42 328.15 +21 155.42 356.65 +22 183.92 356.65 +23 219.42 356.65 +24 240.92 356.65 +25 269.42 356.65 +26 290.42 356.65 +27 387.42 136.15 +28 318.92 356.65 +29 318.92 335.65 +30 318.92 328.15 +31 318.92 299.65 +32 297.92 299.65 +33 290.42 328.15 +34 290.42 335.65 +35 297.92 328.15 +36 254.92 335.65 +37 254.92 314.15 +38 254.92 292.65 +39 254.92 271.65 +40 254.92 243.15 +41 254.92 221.65 +42 254.92 193.15 +43 254.92 171.65 +44 276.42 171.65 +45 296.42 150.65 +46 276.42 150.65 +47 375.92 150.65 +48 308.92 150.65 +49 354.92 164.65 +50 338.42 174.65 +51 354.92 174.65 +52 338.42 200.15 +53 338.42 221.65 +54 354.92 221.65 +55 354.92 200.15 +56 361.92 200.15 +57 361.92 186.15 +58 383.42 186.15 +59 383.42 179.15 +60 404.42 179.15 +61 404.42 186.15 +62 418.92 186.15 +63 418.92 200.15 +64 432.92 200.15 +65 432.92 221.65 +66 418.92 221.65 +67 418.92 235.65 +68 397.42 235.65 +69 397.42 243.15 +70 375.92 243.15 +71 375.92 257.15 +72 368.92 257.15 +73 368.92 264.15 +74 347.42 264.15 +75 347.42 278.65 +76 336.42 278.65 +77 336.42 328.15 +78 347.42 328.15 +79 347.42 342.65 +80 368.92 342.65 +81 368.92 353.65 +82 418.92 353.65 +83 418.92 342.65 +84 432.92 342.65 +85 432.92 356.65 +86 447.42 356.65 +87 447.42 321.15 +88 447.42 292.65 +89 432.92 292.65 +90 432.92 314.15 +91 418.92 314.15 +92 418.92 321.15 +93 397.42 321.15 +94 397.42 333.65 +95 375.92 333.65 +96 375.92 321.15 +97 361.92 321.15 +98 361.92 299.65 +99 375.92 299.65 +100 375.92 285.65 +101 397.42 285.65 +102 397.42 271.65 +103 418.92 271.65 +104 418.92 264.15 +105 439.92 264.15 +106 439.92 250.15 +107 454.42 250.15 +108 454.42 243.15 +109 461.42 243.15 +110 461.42 214.65 +111 461.42 193.15 +112 447.42 193.15 +113 447.42 179.15 +114 439.92 179.15 +115 439.92 167.65 +116 419.92 167.65 +117 419.92 150.65 +118 439.92 150.65 +119 454.42 150.65 +120 475.92 150.65 +121 475.92 171.65 +122 496.92 171.65 +123 496.92 193.15 +124 496.92 214.65 +125 496.92 243.15 +126 496.92 271.65 +127 496.92 292.65 +128 496.92 317.15 +129 496.92 335.65 +130 470.42 335.65 +131 470.42 356.65 +132 496.92 356.65 +133 347.42 150.65 +134 539.92 356.65 +135 560.92 356.65 +136 589.42 356.65 +137 589.42 342.65 +138 603.92 342.65 +139 610.92 342.65 +140 610.92 335.65 +141 610.92 321.15 +142 624.92 321.15 +143 624.92 278.65 +144 610.92 278.65 +145 610.92 257.15 +146 589.42 257.15 +147 589.42 250.15 +148 575.42 250.15 +149 560.92 250.15 +150 542.92 250.15 +151 542.92 264.15 +152 560.92 264.15 +153 575.42 264.15 +154 575.42 271.65 +155 582.42 271.65 +156 582.42 285.65 +157 596.42 285.65 +158 560.92 335.65 +159 596.42 314.15 +160 582.42 314.15 +161 582.42 321.15 +162 575.42 321.15 +163 575.42 335.65 +164 525.42 335.65 +165 525.42 314.15 +166 525.42 299.65 +167 525.42 281.65 +168 525.42 233.15 +169 525.42 214.65 +170 525.42 193.15 +171 525.42 171.65 +172 546.92 171.65 +173 546.92 150.65 +174 568.42 150.65 +175 475.92 160.65 +176 603.92 150.65 +177 624.92 150.65 +178 624.92 136.15 +179 596.42 136.15 +180 575.42 136.15 +181 553.92 136.15 +182 532.42 136.15 +183 575.42 356.65 +184 489.92 136.15 +185 468.42 136.15 +186 447.42 136.15 +187 425.92 136.15 +188 404.42 136.15 +189 370.42 136.15 +190 361.92 150.65 +191 340.42 136.15 +192 326.42 136.15 +193 301.92 136.15 +194 276.42 136.15 +195 254.92 136.15 +196 315.92 136.15 +197 212.42 136.15 +198 190.92 136.15 +199 338.92 150.65 +200 155.42 136.15 +201 624.92 299.65 +202 318.92 321.65 +203 155.42 314.15 +204 311.92 356.65 +205 355.42 136.15 +206 318.92 314.15 +207 362.92 164.65 +208 254.92 356.65 +209 383.42 333.65 +210 447.42 335.65 +211 470.42 345.65 +212 525.42 250.15 +213 546.92 335.65 +214 525.42 261.15 +215 525.42 356.65 +216 336.42 298.65 +217 336.42 313.15 +218 293.42 136.15 +219 336.42 306.15 +220 425.92 264.15 +221 391.42 353.65 +222 482.92 335.65 +223 429.92 167.65 +224 330.92 150.65 +225 368.42 150.65 +EOF diff --git a/docs/Makefile b/docs/cugraph/Makefile similarity index 100% rename from docs/Makefile rename to docs/cugraph/Makefile diff --git a/docs/README.md b/docs/cugraph/README.md similarity index 100% rename from docs/README.md rename to docs/cugraph/README.md diff --git a/docs/make.bat b/docs/cugraph/make.bat similarity index 100% rename from docs/make.bat rename to docs/cugraph/make.bat diff --git a/docs/requirement.txt b/docs/cugraph/requirement.txt similarity index 100% rename from docs/requirement.txt rename to docs/cugraph/requirement.txt diff --git a/docs/source/_static/EMPTY b/docs/cugraph/source/_static/EMPTY similarity index 100% rename from docs/source/_static/EMPTY rename to docs/cugraph/source/_static/EMPTY diff --git a/docs/source/_static/copybutton.css b/docs/cugraph/source/_static/copybutton.css similarity index 100% rename from docs/source/_static/copybutton.css rename to docs/cugraph/source/_static/copybutton.css diff --git a/docs/cugraph/source/_static/copybutton_pydocs.js b/docs/cugraph/source/_static/copybutton_pydocs.js new file mode 100644 index 00000000000..cec05777e6b --- /dev/null +++ b/docs/cugraph/source/_static/copybutton_pydocs.js @@ -0,0 +1,65 @@ +$(document).ready(function() { + /* Add a [>>>] button on the top-right corner of code samples to hide + * the >>> and ... prompts and the output and thus make the code + * copyable. */ + var div = $('.highlight-python .highlight,' + + '.highlight-python3 .highlight,' + + '.highlight-pycon .highlight,' + + '.highlight-default .highlight'); + var pre = div.find('pre'); + + // get the styles from the current theme + pre.parent().parent().css('position', 'relative'); + var hide_text = 'Hide the prompts and output'; + var show_text = 'Show the prompts and output'; + var border_width = pre.css('border-top-width'); + var border_style = pre.css('border-top-style'); + var border_color = pre.css('border-top-color'); + var button_styles = { + 'cursor':'pointer', 'position': 'absolute', 'top': '0', 'right': '0', + 'border-color': border_color, 'border-style': border_style, + 'border-width': border_width, 'text-size': '75%', + 'font-family': 'monospace', 'padding-left': '0.2em', 'padding-right': '1.5em', + 'border-radius': '0 3px 0 0', + 'transition': "0.5s" + } + + // create and add the button to all the code blocks that contain >>> + div.each(function(index) { + var jthis = $(this); + if (jthis.find('.gp').length > 0) { + var button = $('>>>'); + button.css(button_styles) + button.attr('title', hide_text); + button.data('hidden', 'false'); + jthis.prepend(button); + } + // tracebacks (.gt) contain bare text elements that need to be + // wrapped in a span to work with .nextUntil() (see later) + jthis.find('pre:has(.gt)').contents().filter(function() { + return ((this.nodeType == 3) && (this.data.trim().length > 0)); + }).wrap(''); + }); + + // define the behavior of the button when it's clicked + $('.copybutton').click(function(e){ + e.preventDefault(); + var button = $(this); + if (button.data('hidden') === 'false') { + // hide the code output + button.parent().find('.go, .gp, .gt').hide(); + button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'hidden'); + button.css('text-decoration', 'line-through'); + button.attr('title', show_text); + button.data('hidden', 'true'); + } else { + // show the code output + button.parent().find('.go, .gp, .gt').show(); + button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'visible'); + button.css('text-decoration', 'none'); + button.attr('title', hide_text); + button.data('hidden', 'false'); + } + }); +}); + diff --git a/docs/source/_static/example_mod.js b/docs/cugraph/source/_static/example_mod.js similarity index 100% rename from docs/source/_static/example_mod.js rename to docs/cugraph/source/_static/example_mod.js diff --git a/docs/source/_static/params.css b/docs/cugraph/source/_static/params.css similarity index 100% rename from docs/source/_static/params.css rename to docs/cugraph/source/_static/params.css diff --git a/docs/source/_static/references.css b/docs/cugraph/source/_static/references.css similarity index 100% rename from docs/source/_static/references.css rename to docs/cugraph/source/_static/references.css diff --git a/docs/source/api.rst b/docs/cugraph/source/api.rst similarity index 63% rename from docs/source/api.rst rename to docs/cugraph/source/api.rst index 459e5fbf4f1..81c5e3e4ee2 100644 --- a/docs/source/api.rst +++ b/docs/cugraph/source/api.rst @@ -10,7 +10,7 @@ Structure Graph ----- -.. autoclass:: cugraph.structure.graph.Graph +.. autoclass:: cugraph.structure.graph_classes.Graph :members: :undoc-members: @@ -40,13 +40,6 @@ Betweenness Centrality :members: :undoc-members: -Edge Betweenness Centrality ---------------------------- - -.. automodule:: cugraph.centrality.edge_betweenness_centrality - :members: - :undoc-members: - Katz Centrality --------------- @@ -55,9 +48,23 @@ Katz Centrality :undoc-members: +Katz Centrality (MG) +-------------------- + +.. automodule:: cugraph.dask.centrality.katz_centrality + :members: + :undoc-members: + Community ========= +EgoNet +------------------------------------ + +.. automodule:: cugraph.community.egonet + :members: + :undoc-members: + Ensemble clustering for graphs (ECG) ------------------------------------ @@ -86,6 +93,14 @@ Louvain :members: :undoc-members: +Louvain (MG) +------------ + +.. automodule:: cugraph.dask.community.louvain + :members: + :undoc-members: + + Spectral Clustering ------------------- @@ -118,6 +133,12 @@ Connected Components :members: :undoc-members: +Connected Components (MG) +-------------------- + +.. automodule:: cugraph.dask.components.connectivity + :members: + :undoc-members: Cores ===== @@ -128,7 +149,7 @@ Core Number .. automodule:: cugraph.cores.core_number :members: :undoc-members: - + K-Core ------ @@ -148,6 +169,17 @@ Force Atlas 2 :undoc-members: +Linear Assignment +================= + +Hungarian +------------- + +.. automodule:: cugraph.linear_assignment.hungarian + :members: + :undoc-members: + + Link Analysis ============= @@ -165,6 +197,13 @@ Pagerank :members: :undoc-members: +Pagerank (MG) +------------- + +.. automodule:: cugraph.dask.link_analysis.pagerank + :members: pagerank + :undoc-members: + Link Prediction =============== @@ -192,6 +231,17 @@ Overlap Coefficient :undoc-members: +Sampling +======== + +Random Walks +------------ + +.. automodule:: cugraph.sampling.random_walks + :members: + :undoc-members: + + Traversal ========= @@ -202,6 +252,13 @@ Breadth-first-search :members: :undoc-members: +Breadth-first-search (MG) +------------------------- + +.. automodule:: cugraph.dask.traversal.bfs + :members: + :undoc-members: + Single-source-shortest-path --------------------------- @@ -209,6 +266,20 @@ Single-source-shortest-path :members: :undoc-members: +Single-source-shortest-path (MG) +-------------------------------- + +.. automodule:: cugraph.dask.traversal.sssp + :members: + :undoc-members: + +Traveling-salesperson-problem +----------------------------- + +.. automodule:: cugraph.traversal.traveling_salesperson + :members: + :undoc-members: + Tree ========= @@ -217,13 +288,37 @@ Minimum Spanning Tree --------------------- .. automodule:: cugraph.tree.minimum_spanning_tree - :members: + :members: minimum_spanning_tree :undoc-members: Maximum Spanning Tree --------------------- -.. automodule:: cugraph.tree.maximum_spanning_tree - :members: +.. automodule:: cugraph.tree.minimum_spanning_tree + :members: maximum_spanning_tree :undoc-members: + :noindex: + +Generator +========= + +RMAT +--------------------- + +.. automodule:: cugraph.generators + :members: rmat + :undoc-members: + + +DASK MG Helper functions +=========================== + +.. automodule:: cugraph.comms.comms + :members: initialize, destroy + :undoc-members: + :member-order: bysource + +.. automodule:: cugraph.dask.common.read_utils + :members: get_chunksize + :undoc-members: diff --git a/docs/source/conf.py b/docs/cugraph/source/conf.py similarity index 97% rename from docs/source/conf.py rename to docs/cugraph/source/conf.py index adec59a2f6c..a4633d04f8d 100644 --- a/docs/source/conf.py +++ b/docs/cugraph/source/conf.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # -# Copyright (c) 2018-2020 NVIDIA CORPORATION. +# Copyright (c) 2018-2021, NVIDIA CORPORATION. # # pygdf documentation build configuration file, created by # sphinx-quickstart on Wed May 3 10:59:22 2017. @@ -42,17 +42,17 @@ # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ - 'numpydoc', - 'sphinx.ext.autodoc', - 'sphinx.ext.autosummary', + "sphinx.ext.intersphinx", + "sphinx.ext.autodoc", + "sphinx.ext.autosummary", + "numpydoc", + "sphinx_markdown_tables", 'sphinx.ext.doctest', - 'sphinx.ext.intersphinx', 'sphinx.ext.linkcode', "IPython.sphinxext.ipython_console_highlighting", "IPython.sphinxext.ipython_directive", "nbsphinx", "recommonmark", - "sphinx_markdown_tables", ] @@ -80,9 +80,9 @@ # built documents. # # The short X.Y version. -version = '0.17' +version = '0.20' # The full version, including alpha/beta/rc tags. -release = '0.17.0' +release = '0.20.0' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/docs/source/cugraph_blogs.rst b/docs/cugraph/source/cugraph_blogs.rst similarity index 93% rename from docs/source/cugraph_blogs.rst rename to docs/cugraph/source/cugraph_blogs.rst index 84e31d40a19..cbbc93a1b14 100644 --- a/docs/source/cugraph_blogs.rst +++ b/docs/cugraph/source/cugraph_blogs.rst @@ -23,6 +23,12 @@ BLOGS * `Status of RAPIDS cuGraphβ€Šβ€”β€ŠRefactoring Code And Rethinking Graphs `_ * `Tackling Large Graphs with RAPIDS cuGraph and CUDA Unified Memory on GPUs `_ * `RAPIDS cuGraph adds NetworkX and DiGraph Compatibility `_ + * `Large Graph Visualization with RAPIDS cuGraph `_ + +2021 +------ + * + Media diff --git a/docs/cugraph/source/cugraph_intro.md b/docs/cugraph/source/cugraph_intro.md new file mode 100644 index 00000000000..142395fb719 --- /dev/null +++ b/docs/cugraph/source/cugraph_intro.md @@ -0,0 +1,69 @@ + +# cuGraph Introduction +The Data Scientist has a collection of techniques within their +proverbial toolbox. Data engineering, statistical analysis, and +machine learning are among the most commonly known. However, there +are numerous cases where the focus of the analysis is on the +relationship between data elements. In those cases, the data is best +represented as a graph. Graph analysis, also called network analysis, +is a collection of algorithms for answering questions posed against +graph data. Graph analysis is not new. + +The first graph problem was posed by Euler in 1736, the [Seven Bridges of +Konigsberg](https://en.wikipedia.org/wiki/Seven_Bridges_of_K%C3%B6nigsberg), +and laid the foundation for the mathematical field of graph theory. +The application of graph analysis covers a wide variety of fields, including +marketing, biology, physics, computer science, sociology, and cyber to name a few. + +RAPIDS cuGraph is a library of graph algorithms that seamlessly integrates +into the RAPIDS data science ecosystem and allows the data scientist to easily +call graph algorithms using data stored in a GPU DataFrame, NetworkX Graphs, or even +CuPy or SciPy sparse Matrix. + + +# Vision +The vision of RAPIDS cuGraph is to ___make graph analysis ubiquitous to the +point that users just think in terms of analysis and not technologies or +frameworks___. This is a goal that many of us on the cuGraph team have been +working on for almost twenty years. Many of the early attempts focused on +solving one problem or using one technique. Those early attempts worked for +the initial goal but tended to break as the scope changed (e.g., shifting +to solving a dynamic graph problem with a static graph solution). The limiting +factors usually came down to compute power, ease-of-use, or choosing a data +structure that was not suited for all problems. NVIDIA GPUs, CUDA, and RAPIDS +have totally changed the paradigm and the goal of an accelerated unified graph +analytic library is now possible. + +The compute power of the latest NVIDIA GPUs (RAPIDS supports Pascal and later +GPU architectures) make graph analytics 1000x faster on average over NetworkX. +Moreover, the internal memory speed within a GPU allows cuGraph to rapidly +switch the data structure to best suit the needs of the analytic rather than +being restricted to a single data structure. cuGraph is working with several +frameworks for both static and dynamic graph data structures so that we always +have a solution to any graph problem. Since Python has emerged as the de facto +language for data science, allowing interactivity and the ability to run graph +analytics in Python makes cuGraph familiar and approachable. RAPIDS wraps all +the graph analytic goodness mentioned above with the ability to perform +high-speed ETL, statistics, and machine learning. To make things even better, +RAPIDS and DASK allows cuGraph to scale to multiple GPUs to support +multi-billion edge graphs. + + +# Terminology + +cuGraph is a collection of GPU accelerated graph algorithms and graph utility +functions. The application of graph analysis covers a lot of areas. +For Example: +* [Network Science](https://en.wikipedia.org/wiki/Network_science) +* [Complex Network](https://en.wikipedia.org/wiki/Complex_network) +* [Graph Theory](https://en.wikipedia.org/wiki/Graph_theory) +* [Social Network Analysis](https://en.wikipedia.org/wiki/Social_network_analysis) + +cuGraph does not favor one field over another. Our developers span the +breadth of fields with the focus being to produce the best graph library +possible. However, each field has its own argot (jargon) for describing the +graph (or network). In our documentation, we try to be consistent. In Python +documentation we will mostly use the terms __Node__ and __Edge__ to better +match NetworkX preferred term use, as well as other Python-based tools. At +the CUDA/C layer, we favor the mathematical terms of __Vertex__ and __Edge__. + diff --git a/docs/source/cugraph_ref.rst b/docs/cugraph/source/cugraph_ref.rst similarity index 61% rename from docs/source/cugraph_ref.rst rename to docs/cugraph/source/cugraph_ref.rst index 591619fb338..e0f113eaba4 100644 --- a/docs/source/cugraph_ref.rst +++ b/docs/cugraph/source/cugraph_ref.rst @@ -2,22 +2,35 @@ References ########## +************ +Architecture +************ + +2-D Data Partitioning + +- Kang, S., Fender, A., Eaton, J., & Rees, B. (2020, September) *Computing PageRank Scores of Web Crawl Data Using DGX A100 Clusters*. In 2020 IEEE High Performance Extreme Computing Conference (HPEC) (pp. 1-4). IEEE. + + +| + +| + ********** Algorithms ********** Betweenness Centrality -- Brandes, U. (2001). A faster algorithm for betweenness centrality. Journal of mathematical sociology, 25(2), 163-177. -- Brandes, U. (2008). On variants of shortest-path betweenness centrality and their generic computation. Social Networks, 30(2), 136-145. -- McLaughlin, A., & Bader, D. A. (2018). Accelerating GPU betweenness centrality. Communications of the ACM, 61(8), 85-92. +- Brandes, U. (2001). *A faster algorithm for betweenness centrality*. Journal of mathematical sociology, 25(2), 163-177. +- Brandes, U. (2008). *On variants of shortest-path betweenness centrality and their generic computation*. Social Networks, 30(2), 136-145. +- McLaughlin, A., & Bader, D. A. (2018). *Accelerating GPU betweenness centrality*. Communications of the ACM, 61(8), 85-92. Katz - J. Cohen, *Trusses: Cohesive subgraphs for social network analysis* National security agency technical report, 2008 - O. Green, J. Fox, E. Kim, F. Busato, et al. *Quickly Finding a Truss in a Haystack* IEEE High Performance Extreme Computing Conference (HPEC), 2017 https://doi.org/10.1109/HPEC.2017.8091038 -- O. Green, P. Yalamanchili, L.M. Munguia, β€œ*ast Triangle Counting on GPU* Irregular Applications: Architectures and Algorithms (IA3), 2014 +- O. Green, P. Yalamanchili, L.M. Munguia, *Fast Triangle Counting on GPU* Irregular Applications: Architectures and Algorithms (IA3), 2014 Hungarian Algorithm @@ -27,6 +40,15 @@ Hungarian Algorithm | +************* +Other Papers +************* +- Hricik, T., Bader, D., & Green, O. (2020, September). *Using RAPIDS AI to Accelerate Graph Data Science Workflows*. In 2020 IEEE High Performance Extreme Computing Conference (HPEC) (pp. 1-4). IEEE. + +| + +| + ********** Data Sets ********** diff --git a/docs/cugraph/source/dask-cugraph.rst b/docs/cugraph/source/dask-cugraph.rst new file mode 100644 index 00000000000..51487bfbf05 --- /dev/null +++ b/docs/cugraph/source/dask-cugraph.rst @@ -0,0 +1,53 @@ +~~~~~~~~~~~~~~~~~~~~~~ +Multi-GPU with cuGraph +~~~~~~~~~~~~~~~~~~~~~~ + +cuGraph supports multi-GPU leveraging `Dask `_. Dask is a flexible library for parallel computing in Python which makes scaling out your workflow smooth and simple. cuGraph also uses other Dask-based RAPIDS projects such as `dask-cuda `_. The maximum graph size is currently limited to 2 Billion vertices (to be waived in the next versions). + +Distributed graph analytics +=========================== + +The current solution is able to scale across multiple GPUs on multiple machines. Distributing the graph and computation lets you analyze datasets far larger than a single GPU’s memory. + +With cuGraph and Dask, whether you’re using a single NVIDIA GPU or multiple nodes, your RAPIDS workflow will run smoothly, intelligently distributing the workload across the available resources. + +If your graph comfortably fits in memory on a single GPU, you would want to use the single-GPU version of cuGraph. If you want to distribute your workflow across multiple GPUs and have more data than you can fit in memory on a single GPU, you would want to use cuGraph's multi-GPU features. + +Example +======== + +.. code-block:: python + + from dask.distributed import Client, wait + from dask_cuda import LocalCUDACluster + import cugraph.comms as Comms + import cugraph.dask as dask_cugraph + + cluster = LocalCUDACluster() + client = Client(cluster) + Comms.initialize(p2p=True) + + # Helper function to set the reader chunk size to automatically get one partition per GPU + chunksize = dask_cugraph.get_chunksize(input_data_path) + + # Multi-GPU CSV reader + e_list = dask_cudf.read_csv(input_data_path, + chunksize = chunksize, + delimiter=' ', + names=['src', 'dst'], + dtype=['int32', 'int32']) + + G = cugraph.DiGraph() + G.from_dask_cudf_edgelist(e_list, source='src', destination='dst') + + # now run PageRank + pr_df = dask_cugraph.pagerank(G, tol=1e-4) + + # All done, clean up + Comms.destroy() + client.close() + cluster.close() + + +| + diff --git a/docs/source/images/Nx_Cg_1.png b/docs/cugraph/source/images/Nx_Cg_1.png similarity index 100% rename from docs/source/images/Nx_Cg_1.png rename to docs/cugraph/source/images/Nx_Cg_1.png diff --git a/docs/source/images/Nx_Cg_2.png b/docs/cugraph/source/images/Nx_Cg_2.png similarity index 100% rename from docs/source/images/Nx_Cg_2.png rename to docs/cugraph/source/images/Nx_Cg_2.png diff --git a/docs/source/index.rst b/docs/cugraph/source/index.rst similarity index 100% rename from docs/source/index.rst rename to docs/cugraph/source/index.rst diff --git a/docs/source/nx_transition.rst b/docs/cugraph/source/nx_transition.rst similarity index 100% rename from docs/source/nx_transition.rst rename to docs/cugraph/source/nx_transition.rst diff --git a/docs/source/sphinxext/github_link.py b/docs/cugraph/source/sphinxext/github_link.py similarity index 88% rename from docs/source/sphinxext/github_link.py rename to docs/cugraph/source/sphinxext/github_link.py index a7a46fdd9df..fa8fe3f5fe3 100644 --- a/docs/source/sphinxext/github_link.py +++ b/docs/cugraph/source/sphinxext/github_link.py @@ -1,3 +1,17 @@ +# Copyright (c) 2019-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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# NOTE: # This contains code with copyright by the scikit-learn project, subject to the # license in /thirdparty/LICENSES/LICENSE.scikit_learn diff --git a/docs/source/cugraph_intro.md b/docs/source/cugraph_intro.md deleted file mode 100644 index 5bf2b715462..00000000000 --- a/docs/source/cugraph_intro.md +++ /dev/null @@ -1,22 +0,0 @@ - -# cuGraph Introduction - - -## Terminology - -cuGraph is a collection of GPU accelerated graph algorithms and graph utility -functions. The application of graph analysis covers a lot of areas. -For Example: -* [Network Science](https://en.wikipedia.org/wiki/Network_science) -* [Complex Network](https://en.wikipedia.org/wiki/Complex_network) -* [Graph Theory](https://en.wikipedia.org/wiki/Graph_theory) -* [Social Network Analysis](https://en.wikipedia.org/wiki/Social_network_analysis) - -cuGraph does not favor one field over another. Our developers span the -breadth of fields with the focus being to produce the best graph library -possible. However, each field has its own argot (jargon) for describing the -graph (or network). In our documentation, we try to be consistent. In Python -documentation we will mostly use the terms __Node__ and __Edge__ to better -match NetworkX preferred term use, as well as other Python-based tools. At -the CUDA/C layer, we favor the mathematical terms of __Vertex__ and __Edge__. - diff --git a/docs/source/dask-cugraph.rst b/docs/source/dask-cugraph.rst deleted file mode 100644 index b27ad382809..00000000000 --- a/docs/source/dask-cugraph.rst +++ /dev/null @@ -1,70 +0,0 @@ -~~~~~~~~~~~~~~~~~~~~~~ -Multi-GPU with cuGraph -~~~~~~~~~~~~~~~~~~~~~~ - -cuGraph supports multi-GPU leveraging `Dask `_. Dask is a flexible library for parallel computing in Python which makes scaling out your workflow smooth and simple. cuGraph also uses other Dask-based RAPIDS projects such as `dask-cuda `_. The maximum graph size is currently limited to 2 Billion vertices (to be waived in the next versions). - -Distributed graph analytics -=========================== - -The current solution is able to scale across multiple GPUs on multiple machines. Distributing the graph and computation lets you analyze datasets far larger than a single GPU’s memory. - -With cuGraph and Dask, whether you’re using a single NVIDIA GPU or multiple nodes, your RAPIDS workflow will run smoothly, intelligently distributing the workload across the available resources. - -If your graph comfortably fits in memory on a single GPU, you would want to use the single-GPU version of cuGraph. If you want to distribute your workflow across multiple GPUs and have more data than you can fit in memory on a single GPU, you would want to use cuGraph's multi-GPU features. - - -Distributed Graph Algorithms ----------------------------- - -.. automodule:: cugraph.dask.link_analysis.pagerank - :members: pagerank - :undoc-members: - -.. automodule:: cugraph.dask.traversal.bfs - :members: bfs - :undoc-members: - - -Helper functions ----------------- - -.. automodule:: cugraph.comms.comms - :members: initialize - :undoc-members: - -.. automodule:: cugraph.comms.comms - :members: destroy - :undoc-members: - -.. automodule:: cugraph.dask.common.read_utils - :members: get_chunksize - :undoc-members: - -Consolidation -============= - -cuGraph can transparently interpret the Dask cuDF Dataframe as a regular Dataframe when loading the edge list. This is particularly helpful for workflows extracting a single GPU sized edge list from a distributed dataset. From there any existing single GPU feature will just work on this input. - -For instance, consolidation allows leveraging Dask cuDF CSV reader to load file(s) on multiple GPUs and consolidate this input to a single GPU graph. Reading is often the time and memory bottleneck, with this feature users can call the Multi-GPU version of the reader without changing anything else. - -Batch Processing -================ - -cuGraph can leverage multi GPUs to increase processing speed for graphs that fit on a single GPU, providing faster analytics on such graphs. -You will be able to use the Graph the same way as you used to in a Single GPU environment, but analytics that support batch processing will automatically use the GPUs available to the dask client. -For example, Betweenness Centrality scores can be slow to obtain depending on the number of vertices used in the approximation. Thank to Multi GPUs Batch Processing, -you can create Single GPU graph as you would regularly do it using cuDF CSV reader, enable Batch analytics on it, and obtain scores much faster as each GPU will handle a sub-set of the sources. -In order to use Batch Analytics you need to set up a Dask Cluster and Client in addition to the cuGraph communicator, then you can simply call `enable_batch()` on you graph, and algorithms supporting batch processing will use multiple GPUs. - -Algorithms supporting Batch Processing --------------------------------------- -.. automodule:: cugraph.centrality - :members: betweenness_centrality - :undoc-members: - :noindex: - -.. automodule:: cugraph.centrality - :members: edge_betweenness_centrality - :undoc-members: - :noindex: diff --git a/github/workflows/labeler.yml b/github/workflows/labeler.yml new file mode 100644 index 00000000000..23956a02fbd --- /dev/null +++ b/github/workflows/labeler.yml @@ -0,0 +1,11 @@ +name: "Pull Request Labeler" +on: +- pull_request_target + +jobs: + triage: + runs-on: ubuntu-latest + steps: + - uses: actions/labeler@main + with: + repo-token: "${{ secrets.GITHUB_TOKEN }}" diff --git a/notebooks/README.md b/notebooks/README.md index a5706720235..3769ceb6957 100644 --- a/notebooks/README.md +++ b/notebooks/README.md @@ -10,6 +10,7 @@ This repository contains a collection of Jupyter Notebooks that outline how to r | Folder | Notebook | Description | | --------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | | Centrality | | | +| | [Centrality](centrality/Centrality.ipynb) | Compute and compare multiple centrality scores | | | [Katz](centrality/Katz.ipynb) | Compute the Katz centrality for every vertex | | | [Betweenness](centrality/Betweenness.ipynb) | Compute both Edge and Vertex Betweenness centrality | | Community | | | @@ -33,6 +34,8 @@ This repository contains a collection of Jupyter Notebooks that outline how to r | Traversal | | | | | [BFS](traversal/BFS.ipynb) | Compute the Breadth First Search path from a starting vertex to every other vertex in a graph | | | [SSSP](traversal/SSSP.ipynb) | Single Source Shortest Path - compute the shortest path from a starting vertex to every other vertex | +| Sampling | +| | [Random Walk](sampling/RandomWalk.ipynb) | Compute Random Walk for a various number of seeds and path lengths | | Structure | | | | | [Renumbering](structure/Renumber.ipynb)
[Renumbering 2](structure/Renumber-2.ipynb) | Renumber the vertex IDs in a graph (two sample notebooks) | | | [Symmetrize](structure/Symmetrize.ipynb) | Symmetrize the edges in a graph | @@ -49,22 +52,21 @@ Running the example in these notebooks requires: * Download via Docker, Conda (See [__Getting Started__](https://rapids.ai/start.html)) * cuGraph is dependent on the latest version of cuDF. Please install all components of RAPIDS -* Python 3.6+ +* Python 3.7+ * A system with an NVIDIA GPU: Pascal architecture or better -* CUDA 9.2+ -* NVIDIA driver 396.44+ +* CUDA 11.0+ +* NVIDIA driver 450.51+ #### Notebook Credits - Original Authors: Bradley Rees -- Last Edit: 04/24/2020 +- Last Edit: 04/19/2021 -RAPIDS Versions: 0.14 +RAPIDS Versions: 0.19 Test Hardware - - GV100 32G, CUDA 9,2 diff --git a/notebooks/centrality/Centrality.ipynb b/notebooks/centrality/Centrality.ipynb new file mode 100644 index 00000000000..591c27419ba --- /dev/null +++ b/notebooks/centrality/Centrality.ipynb @@ -0,0 +1,443 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Centrality" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In this notebook, we will compute vertex centrality scores using the various cuGraph algorithms. We will then compare the similarities and differences.\n", + "\n", + "| Author Credit | Date | Update | cuGraph Version | Test Hardware |\n", + "| --------------|------------|--------------|-----------------|----------------|\n", + "| Brad Rees | 04/16/2021 | created | 0.19 | GV100, CUDA 11.0\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Centrality is measure of how important, or central, a node or edge is within a graph. It is useful for identifying influencer in social networks, key routing nodes in communication/computer network infrastructures, \n", + "\n", + "The seminal paper on centrality is: Freeman, L. C. (1978). Centrality in social networks conceptual clarification. Social networks, 1(3), 215-239.\n", + "\n", + "\n", + "__Degree centrality – done but need new API__
\n", + "Degree centrality is based on the notion that whoever has the most connects must be important. \n", + "\n", + "
\n", + " Cd(v) = degree(v)\n", + "
\n", + "\n", + "cuGraph currently does not have a Degree Centrality function call. However, since Degree Centrality is just the degree of a node, we can use _G.degree()_ function.\n", + "Degree Centrality for a Directed graph can be further divided in _indegree centrality_ and _outdegree centrality_ and can be obtained using _G.degrees()_\n", + "\n", + "\n", + "__Closeness centrality – coming soon__
\n", + "Closeness is a measure of the shortest path to every other node in the graph. A node that is close to every other node, can reach over other node in the fewest number of hops, means that it has greater influence on the network versus a node that is not close.\n", + "\n", + "__Betweenness Centrality__
\n", + "Betweenness is a measure of the number of shortest paths that cross through a node, or over an edge. A node with high betweenness means that it had a greater influence on the flow of information. \n", + "\n", + "Betweenness centrality of a node 𝑣 is the sum of the fraction of all-pairs shortest paths that pass through 𝑣\n", + "\n", + "
\n", + " \n", + "
\n", + "\n", + "To speedup runtime of betweenness centrailty, the metric can be computed on a limited number of nodes (randomly selected) and then used to estimate the other scores. For this example, the graphs are relatively smalled (under 5,000 nodes) so betweenness on every node will be computed.\n", + "\n", + "__Eigenvector Centrality - coming soon__
\n", + "Eigenvectors can be thought of as the balancing points of a graph, or center of gravity of a 3D object. High centrality means that more of the graph is balanced around that node.\n", + "\n", + "__Katz Centrality__
\n", + "Katz is a variant of degree centrality and of eigenvector centrality. \n", + "Katz centrality is a measure of the relative importance of a node within the graph based on measuring the influence across the total number of walks between vertex pairs. \n", + "\n", + "
\n", + " \n", + "
\n", + "\n", + "See:\n", + "* [Katz on Wikipedia](https://en.wikipedia.org/wiki/Katz_centrality) for more details on the algorithm.\n", + "* https://www.sci.unich.it/~francesc/teaching/network/katz.html\n", + "\n", + "__PageRank Centrality__
\n", + "PageRank is classified as both a Link Analysis tool and a centrality measure. PageRank is based on the assumption that important nodes point (directed edge) to other important nodes. From a social network perspective, the question is who do you seek for an answer and then who does that person seek. PageRank is good when there is implied importance in the data, for example a citation network, web page linkages, or trust networks. \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Test Data\n", + "We will be using the Zachary Karate club dataset \n", + "*W. W. Zachary, An information flow model for conflict and fission in small groups, Journal of\n", + "Anthropological Research 33, 452-473 (1977).*\n", + "\n", + "\n", + "![Karate Club](../img/zachary_black_lines.png)\n", + "\n", + "\n", + "Because the test data has vertex IDs starting at 1, the auto-renumber feature of cuGraph (mentioned above) will be used so the starting vertex ID is zero for maximum efficiency. The resulting data will then be auto-unrenumbered, making the entire renumbering process transparent to users." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Import the modules\n", + "import cugraph\n", + "import cudf" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "import pandas as pd \n", + "from IPython.display import display_html " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Functions\n", + "using underscore variable names to avoid collisions. \n", + "non-underscore names are expected to be global names" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Compute Centrality\n", + "# the centrality calls are very straight forward with the graph being the primary argument\n", + "# we are using the default argument values for all centrality functions\n", + "\n", + "def compute_centrality(_graph) :\n", + " # Compute Degree Centrality\n", + " _d = _graph.degree()\n", + " \n", + " # Compute the Betweenness Centrality\n", + " _b = cugraph.betweenness_centrality(_graph)\n", + "\n", + " # Compute Katz Centrality\n", + " _k = cugraph.katz_centrality(_graph)\n", + " \n", + " # Compute PageRank Centrality\n", + " _p = cugraph.pagerank(_graph)\n", + " \n", + " return _d, _b, _k, _p" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Print function\n", + "# being lazy and requiring that the dataframe names are not changed versus passing them in\n", + "def print_centrality(_n):\n", + " dc_top = dc.sort_values(by='degree', ascending=False).head(_n).to_pandas()\n", + " bc_top = bc.sort_values(by='betweenness_centrality', ascending=False).head(_n).to_pandas()\n", + " katz_top = katz.sort_values(by='katz_centrality', ascending=False).head(_n).to_pandas()\n", + " pr_top = pr.sort_values(by='pagerank', ascending=False).head(_n).to_pandas()\n", + " \n", + " df1_styler = dc_top.style.set_table_attributes(\"style='display:inline'\").set_caption('Degree').hide_index()\n", + " df2_styler = bc_top.style.set_table_attributes(\"style='display:inline'\").set_caption('Betweenness').hide_index()\n", + " df3_styler = katz_top.style.set_table_attributes(\"style='display:inline'\").set_caption('Katz').hide_index()\n", + " df4_styler = pr_top.style.set_table_attributes(\"style='display:inline'\").set_caption('PageRank').hide_index()\n", + "\n", + " display_html(df1_styler._repr_html_()+df2_styler._repr_html_()+df3_styler._repr_html_()+df4_styler._repr_html_(), raw=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Read the data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Define the path to the test data \n", + "datafile='../data/karate-data.csv'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "cuGraph does not do any data reading or writing and is dependent on other tools for that, with cuDF being the preferred solution. \n", + "\n", + "The data file contains an edge list, which represents the connection of a vertex to another. The `source` to `destination` pairs is in what is known as Coordinate Format (COO). In this test case, the data is just two columns. However a third, `weight`, column is also possible" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "gdf = cudf.read_csv(datafile, delimiter='\\t', names=['src', 'dst'], dtype=['int32', 'int32'] )" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "it was that easy to load data" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Create a Graph" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# create a Graph using the source (src) and destination (dst) vertex pairs from the Dataframe \n", + "G = cugraph.Graph()\n", + "G.from_cudf_edgelist(gdf, source='src', destination='dst')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Compute Centrality" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dc, bc, katz, pr = compute_centrality(G)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Results\n", + "Typically, analyst look just at the top 10% of results. Basically just those vertices that are the most central or important. \n", + "The karate data has 32 vertices, so let's round a little and look at the top 5 vertices" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print_centrality(5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### A Different Dataset\n", + "The Karate dataset is not that large or complex, which makes it a perfect test dataset since it is easy to visually verify results. Let's look at a larger dataset with a lot more edges" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Define the path to the test data \n", + "datafile='../data/netscience.csv'\n", + "\n", + "gdf = cudf.read_csv(datafile, delimiter=' ', names=['src', 'dst', 'wt'], dtype=['int32', 'int32', 'float'] )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# create a Graph using the source (src) and destination (dst) vertex pairs from the Dataframe \n", + "G = cugraph.Graph()\n", + "G.from_cudf_edgelist(gdf, source='src', destination='dst')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "(G.number_of_nodes(), G.number_of_edges())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dc, bc, katz, pr = compute_centrality(G)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print_centrality(5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can now see a larger discrepancy between the centrality scores and which nodes rank highest.\n", + "Which centrality measure to use is left to the analyst to decide and does require insight into the difference algorithms and graph structure." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### And One More Dataset\n", + "Let's look at a Cyber dataset. The vertex ID are IP addresses" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Define the path to the test data \n", + "datafile='../data/cyber.csv'\n", + "\n", + "gdf = cudf.read_csv(datafile, delimiter=',', names=['idx', 'src', 'dst'], dtype=['int32', 'str', 'str'] )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# create a Graph using the source (src) and destination (dst) vertex pairs from the Dataframe \n", + "G = cugraph.Graph()\n", + "G.from_cudf_edgelist(gdf, source='src', destination='dst')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "(G.number_of_nodes(), G.number_of_edges())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dc, bc, katz, pr = compute_centrality(G)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print_centrality(5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "There are differences in how each centrality measure ranks the nodes. In some cases, every algorithm returns similar results, and in others, the results are different. Understanding how the centrality measure is computed and what edge represent is key to selecting the right centrality metric." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "----\n", + "Copyright (c) 2019-2021, NVIDIA CORPORATION.\n", + "\n", + "Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0\n", + "\n", + "Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "cugraph_dev", + "language": "python", + "name": "cugraph_dev" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/notebooks/community/ECG.ipynb b/notebooks/community/ECG.ipynb index d7595dadb26..4a9eedd3c3a 100644 --- a/notebooks/community/ECG.ipynb +++ b/notebooks/community/ECG.ipynb @@ -5,6 +5,7 @@ "metadata": {}, "source": [ "# Ensemble Clustering for Graphs (ECG)\n", + "# Does not run on Pascal\n", "In this notebook, we will use cuGraph to identify the cluster in a test graph using the Ensemble Clustering for Graph approach. \n", "\n", "\n", diff --git a/notebooks/community/Louvain.ipynb b/notebooks/community/Louvain.ipynb index e5e5e6a04ed..bfb8e299f49 100755 --- a/notebooks/community/Louvain.ipynb +++ b/notebooks/community/Louvain.ipynb @@ -5,6 +5,7 @@ "metadata": {}, "source": [ "# Louvain Community Detection\n", + "# Does not run on Pascal\n", "\n", "\n", "In this notebook, we will use cuGraph to identify the cluster in a test graph using the Louvain algorithm \n", diff --git a/notebooks/community/Subgraph-Extraction.ipynb b/notebooks/community/Subgraph-Extraction.ipynb index e068ef53aa5..cac52262d4d 100755 --- a/notebooks/community/Subgraph-Extraction.ipynb +++ b/notebooks/community/Subgraph-Extraction.ipynb @@ -5,6 +5,7 @@ "metadata": {}, "source": [ "# Subgraph Extraction\n", + "# Does not run on Pascal\n", "\n", "In this notebook, we will use cuGraph to extract a subgraph from the test graph. \n", "\n", diff --git a/notebooks/community/Triangle-Counting.ipynb b/notebooks/community/Triangle-Counting.ipynb index 09d7906a526..19d3f838fc6 100755 --- a/notebooks/community/Triangle-Counting.ipynb +++ b/notebooks/community/Triangle-Counting.ipynb @@ -21,7 +21,7 @@ "\n", "\n", "## Introduction\n", - "Triancle Counting, as the name implies, finds the number of triangles in a graph. Triangles are important in computing the clustering Coefficient and can be used for clustering. \n", + "Triangle Counting, as the name implies, finds the number of triangles in a graph. Triangles are important in computing the clustering Coefficient and can be used for clustering. \n", "\n", "\n", "To compute the Pagerank scores for a graph in cuGraph we use:
\n", diff --git a/notebooks/cores/ktruss.ipynb b/notebooks/cores/ktruss.ipynb index e6470110666..3f283558f27 100644 --- a/notebooks/cores/ktruss.ipynb +++ b/notebooks/cores/ktruss.ipynb @@ -5,6 +5,7 @@ "metadata": {}, "source": [ "# K-Truss\n", + "# Does not run on CUDA 11.4\n", "\n", "\n", "In this notebook, we will use cuGraph to identify the K-Truss clusters in a test graph \n", @@ -12,18 +13,13 @@ "Notebook Credits\n", "* Original Authors: Bradley Rees\n", "* Created: 10/28/2019\n", - "* Last Edit: 08/16/2020\n", - "\n", - "RAPIDS Versions: 0.13\n", - "\n", - "Test Hardware\n", - "* GV100 32G, CUDA 10.2\n", - "\n", + "* Last Edit: 08/13/2021\n", "\n", "\n", "## Introduction\n", "\n", "Compute the k-truss of the graph G. A K-Truss is a relaxed cliques where every vertex is supported by at least k-2 triangle.\n", + "NOTE: k-truss is currently not supported on CUDA 11.4 systems.\n", "\n", "Ref:\n", "\n", diff --git a/notebooks/cugraph_benchmarks/bfs_benchmark.ipynb b/notebooks/cugraph_benchmarks/bfs_benchmark.ipynb index 58eb94bf0ee..6ae695e206e 100644 --- a/notebooks/cugraph_benchmarks/bfs_benchmark.ipynb +++ b/notebooks/cugraph_benchmarks/bfs_benchmark.ipynb @@ -5,6 +5,7 @@ "metadata": {}, "source": [ "# BFS Performance Benchmarking\n", + "# Skip notebook test\n", "\n", "This notebook benchmarks performance of running BFS within cuGraph against NetworkX. \n", "\n", diff --git a/notebooks/cugraph_benchmarks/louvain_benchmark.ipynb b/notebooks/cugraph_benchmarks/louvain_benchmark.ipynb index a12b7c4bcc2..00e99a28617 100644 --- a/notebooks/cugraph_benchmarks/louvain_benchmark.ipynb +++ b/notebooks/cugraph_benchmarks/louvain_benchmark.ipynb @@ -5,6 +5,7 @@ "metadata": {}, "source": [ "# Louvain Performance Benchmarking\n", + "# Skip notebook test\n", "\n", "This notebook benchmarks performance improvement of running the Louvain clustering algorithm within cuGraph against NetworkX. The test is run over eight test networks (graphs) and then results plotted. \n", "

\n", diff --git a/notebooks/cugraph_benchmarks/nx_cugraph_bc_benchmarking.ipynb b/notebooks/cugraph_benchmarks/nx_cugraph_bc_benchmarking.ipynb index 6f76868f9a4..403c317ac0a 100644 --- a/notebooks/cugraph_benchmarks/nx_cugraph_bc_benchmarking.ipynb +++ b/notebooks/cugraph_benchmarks/nx_cugraph_bc_benchmarking.ipynb @@ -5,6 +5,7 @@ "metadata": {}, "source": [ "# Benchmarking NetworkX compatibility\n", + "# Skip notebook test\n", "This notebook benchmark the use of a NetworkX Graph object as input into algorithms.

\n", "The intention of the feature is to be able to drop cuGraph into existing NetworkX code in spot where performance is not optimal.\n", "\n", diff --git a/notebooks/cugraph_benchmarks/pagerank_benchmark.ipynb b/notebooks/cugraph_benchmarks/pagerank_benchmark.ipynb index c2933a10c7d..d0416efdd87 100644 --- a/notebooks/cugraph_benchmarks/pagerank_benchmark.ipynb +++ b/notebooks/cugraph_benchmarks/pagerank_benchmark.ipynb @@ -5,6 +5,7 @@ "metadata": {}, "source": [ "# PageRank Performance Benchmarking\n", + "# Skip notebook test\n", "\n", "This notebook benchmarks performance of running PageRank within cuGraph against NetworkX. NetworkX contains several implementations of PageRank. This benchmark will compare cuGraph versus the defaukt Nx implementation as well as the SciPy version\n", "\n", diff --git a/notebooks/cugraph_benchmarks/random_walk_benchmark.ipynb b/notebooks/cugraph_benchmarks/random_walk_benchmark.ipynb new file mode 100644 index 00000000000..65cf9fb59eb --- /dev/null +++ b/notebooks/cugraph_benchmarks/random_walk_benchmark.ipynb @@ -0,0 +1,545 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Random Walk Performance\n", + "# Skip notebook testΒΆ \n", + "\n", + "Random walk performance is governed by the length of the paths to find, the number of seeds, and the size or structure of the graph.\n", + "This benchmark will use several test graphs of increasingly larger sizes. While not even multiples in scale, the four test graphs should give an indication of how well Random Walk performs as data size increases. \n", + "\n", + "### Test Data\n", + "Users must run the _dataPrep.sh_ script before running this notebook so that the test files are downloaded\n", + "\n", + "| File Name | Num of Vertices | Num of Edges |\n", + "| ---------------------- | --------------: | -----------: |\n", + "| preferentialAttachment | 100,000 | 999,970 |\n", + "| dblp-2010 | 326,186 | 1,615,400 |\n", + "| coPapersCiteseer | 434,102 | 32,073,440 |\n", + "| as-Skitter | 1,696,415 | 22,190,596 |" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# Import the modules\n", + "import cugraph\n", + "import cudf" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "# system and other\n", + "import gc\n", + "import os\n", + "import time\n", + "import random\n", + "\n", + "# MTX file reader\n", + "from scipy.io import mmread" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "try: \n", + " import matplotlib\n", + "except ModuleNotFoundError:\n", + " os.system('pip install matplotlib')\n", + "\n", + "import matplotlib.pyplot as plt; plt.rcdefaults()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "# Test File\n", + "data = {\n", + " 'preferentialAttachment' : './data/preferentialAttachment.mtx',\n", + " 'dblp' : './data/dblp-2010.mtx',\n", + " 'coPapersCiteseer' : './data/coPapersCiteseer.mtx',\n", + " 'as-Skitter' : './data/as-Skitter.mtx'\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Read the data and create a graph" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "# Data reader - the file format is MTX, so we will use the reader from SciPy\n", + "def read_and_create(datafile):\n", + " print('Reading ' + str(datafile) + '...')\n", + " M = mmread(datafile).asfptype()\n", + "\n", + " _gdf = cudf.DataFrame()\n", + " _gdf['src'] = M.row\n", + " _gdf['dst'] = M.col\n", + " _gdf['wt'] = 1.0\n", + " \n", + " _g = cugraph.Graph()\n", + " _g.from_cudf_edgelist(_gdf, source='src', destination='dst', edge_attr='wt', renumber=False)\n", + " \n", + " print(\"\\t{:,} nodes, {:,} edges\".format(_g.number_of_nodes(), _g.number_of_edges() ))\n", + " \n", + " return _g" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Define the call to RandomWalk\n", + "We are only interested in the runtime, so throw away the results" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "def run_rw(_G, _seeds, _depth):\n", + " t1 = time.time()\n", + " # cugraph.random_walks() returns a 3-tuple, which is being ignored here.\n", + " cugraph.random_walks(_G, _seeds, _depth)\n", + " t2 = time.time() - t1\n", + " return t2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test 1: Runtime versus path depth" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Reading ./data/preferentialAttachment.mtx...\n", + "\t100,000 nodes, 499,985 edges\n", + "update i\n", + "Reading ./data/dblp-2010.mtx...\n", + "\t326,183 nodes, 807,700 edges\n", + "update i\n", + "Reading ./data/coPapersCiteseer.mtx...\n", + "\t434,102 nodes, 16,036,720 edges\n", + "update i\n", + "Reading ./data/as-Skitter.mtx...\n", + "\t1,696,415 nodes, 11,095,298 edges\n", + "update i\n" + ] + } + ], + "source": [ + "# some parameters\n", + "max_depth = 6\n", + "num_seeds = 500\n", + "\n", + "# arrays to capture performance gains\n", + "names = []\n", + "\n", + "# Two dimension data\n", + "time_algo_cu = [] # will be two dimensional\n", + "\n", + "i = 0\n", + "for k,v in data.items():\n", + " time_algo_cu.append([])\n", + " \n", + " # Saved the file Name\n", + " names.append(k)\n", + "\n", + " # read data\n", + " G = read_and_create(v)\n", + " \n", + " num_nodes = G.number_of_nodes()\n", + " nodes = G.nodes().to_array().tolist()\n", + "\n", + " seeds = random.sample(nodes, num_seeds)\n", + "\n", + " for j in range (2, max_depth+1) :\n", + " t = run_rw(G, seeds, j)\n", + " time_algo_cu[i].append(t)\n", + "\n", + " # update i\n", + " i = i + 1\n", + " print(\"update i\")\n", + " \n", + " del G\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(nodes)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAmEAAAFNCAYAAABIc7ibAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Z1A+gAAAACXBIWXMAAAsTAAALE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EjTZs2Zfv27ZSUlNwQrxBCiPuX2WJm85nNHLpyqHoD0VrfU3/at2+vr5eQkHDDc1Xt9OnTGtA7duzQWmv90EMP6Q8++EDXr19f/+1vf9Naa33lyhXdo0cPnZeXp7XW+v3339dvvfWW1lrr6dOn62XLlmmttd64caN+9NFHtcVi0WazWQ8dOlR///33+vTp01oppXfv3n3LcXZ2dvrHH3/UWms9duxYvXDhQq211r169dL79u0ri7v84/T0dK211iaTSffq1UsfOnTohjE7duzQffr00VprPXHiRL18+XKttdbfffedHjp0aNlxv/jiC/3000+XPc7OztZGo1FrrfU333yjR40apbXW+rPPPtOjR48u23Y1hvr16+uPPvpIa631p59+qh955BGttdZvvPGG7tatmy4pKdEHDx7ULi4uesOGDVprrR988EG9atUqXVJSort06aJTU1O11lrHxsbqhx56qOy1vPDCC1prrdevX6/79u1bYbzXuxs+Y0IIIX6dvJI8vTB+oR64fKBusaCFfm37azY/JxCnb5LT3BctKsp7a208CRdyKvWYzWt78sbwsFuOq1evHt26dQNgypQpfPTRRwCMHz8egD179pCQkFA2pqSkhC5dutxwnE2bNrFp0ybatm0LQF5eHsePHycoKIj69evTuXPnW45r0KABbdq0AaB9+/YkJyffMv6lS5cye/ZsTCYTFy9eJCEhgVatWl0zJiYmhgkTJgAwYcIEoqKiGD169C2PnZ2dzfTp0zl+/DhKqbIZq82bN/PEE09gb2/9KPr6+pbtM2rUqLL4V65cWfb84MGDcXBwoGXLlpjNZgYNGgRAy5YtSU5O5ujRoxw5coT+/fsD1tm2WrVqVXjc23lfhBBC3Nsu5F0gOjGaFcdXkGfMo11gO14Mf5He9XpXa1z3XRJWna6/iu7qYzc3N8A669i/f/9raqkqorXm1Vdf5fHHH7/m+eTk5LJj3Wqck5NT2WM7O7tbFvufPn2af/zjH+zbtw8fHx9mzJhxQ1sGs9nMihUrWL16Ne+++25ZD63c3NyfPTbA66+/Tu/evVm1ahXJyclERETccp+rr8HOzg6TyXTD8waDAQcHh7L32WAwYDKZ0FoTFhbG7t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JVFGPps8CyvOGYWT/xlTAzMzOzDLwmzMzMzCwDT8LMzMzMMvAkzMzMzCwDT8LMzMzMMvAkzMzMzCwDT8LMzMzMMvg3QJhXpIQLGN8AAAAASUVORK5CYII=\n", + "text/plain": [ + "

" + ] + }, + "metadata": { + "needs_background": "light" + }, + "output_type": "display_data" + } + ], + "source": [ + "%matplotlib inline\n", + "seed_idx = list(range(2,max_depth +1))\n", + "\n", + "plt.figure(figsize=(10,5))\n", + "\n", + "for i in range(len(data)):\n", + " plt.plot(seed_idx, time_algo_cu[i], label = names[i])\n", + "\n", + "\n", + "plt.title(f'Runtime vs. Path Length ({num_seeds} Seeds)')\n", + "plt.xlabel('Path length')\n", + "plt.ylabel('Runtime')\n", + "plt.legend()\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "12979" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "del time_algo_cu\n", + "gc.collect()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test 2: Runtime versus number of seeds\n", + "The number of seeds will be increased over a range in increments of 10. \n", + "The runtime will be the sum of runtime per increment. Increaing number of seeds by 1 would make for very long execution times " + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Reading ./data/preferentialAttachment.mtx...\n", + "\t100,000 nodes, 499,985 edges\n", + "\t.................................................................................................... \n", + "Reading ./data/dblp-2010.mtx...\n", + "\t326,183 nodes, 807,700 edges\n", + "\t.................................................................................................... \n", + "Reading ./data/coPapersCiteseer.mtx...\n", + "\t434,102 nodes, 16,036,720 edges\n", + "\t.................................................................................................... \n", + "Reading ./data/as-Skitter.mtx...\n", + "\t1,696,415 nodes, 11,095,298 edges\n", + "\t.................................................................................................... \n" + ] + } + ], + "source": [ + "# some parameters\n", + "rw_depth = 4\n", + "max_seeds = 1000\n", + "\n", + "# arrays to capture performance gains\n", + "names = []\n", + "\n", + "# Two dimension data\n", + "time_algo_cu = [] # will be two dimensional\n", + "\n", + "i = 0\n", + "for k,v in data.items():\n", + " time_algo_cu.append([])\n", + " \n", + " # Saved the file Name\n", + " names.append(k)\n", + "\n", + " # read data\n", + " G = read_and_create(v)\n", + " \n", + " num_nodes = G.number_of_nodes()\n", + " nodes = G.nodes().to_array().tolist()\n", + " \n", + " print('\\t', end='')\n", + " for j in range (10, max_seeds +1, 10) :\n", + " print('.', end='')\n", + " seeds = random.sample(nodes, j+1)\n", + " t = run_rw(G, seeds, rw_depth)\n", + " time_algo_cu[i].append(t)\n", + "\n", + " # update i\n", + " i = i + 1\n", + " print(\" \")\n", + " \n", + " del G\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAmcAAAFNCAYAAABFbcjcAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Z1A+gAAAACXBIWXMAAAsTAAALEwEAmpwYAAByJUlEQVR4nO3dd3gU1dvG8e/ZFFJJp/cOoQQIHaRI71WQInbAiqgodn3t+rNhQUTEBgoICkjvvQQIvYQSIKETCAmpu/u8f2SJCR1N2BCez3VxsTtzzsw5O9lwM3PmjBERlFJKKaVU3mBxdgOUUkoppdQ/NJwppZRSSuUhGs6UUkoppfIQDWdKKaWUUnmIhjOllFJKqTxEw5lSSimlVB6i4UwpddOMMaWMMYnGGBdntyUvMsaUMcaIMcbVSftvYoyJchyj7s5og6Md9xtjVjpr/0rdrjScKZVPGGOijTHJjn+QjxtjJhhjfHJw260vvheRwyLiIyK2nNh+bnOEBDHGjLxkeYwxpoVzWpWr3gK+dByjPy9daYxpaoxZbYyJN8bEGWNWGWPq3fpmKqWuRMOZUv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s/xNMZEOl6vIONZrJWB3x0PL3YHDl6j/p8iYgd2GmMKX2dfbR1/Njve+5ARyg6T8VDri+3YCJQxxvgDviKyxrF8Ihkh8Gr+FpFUINUYcxIoTMaDzJVSdygNZ0qp21Gy46xUJmPMaOATEZnhePbnG9eon5q16nX2ZYD3ROTbS/ZX5pLt2ADP62zrem2xob+Xlbrj6WVNpVR+4QfEOl4PzrI8AfD9D9udBzxojPEBMMYUN8YUulphETkHJBhjGjgW9cvBtiil7gAazpRS+cUbwBRjzEbgdJblM4Eel9wQcMNEZD4ZlybXGGO2AVO5fsB6CPjOcenVG4h3LF9Cxg0AWW8IUEqpbIyIOLsNSimVrxhjfEQk0fH6RaCoiDzt5GYppW4TOrZBKaVyXidjzCgyfsceAu53bnOUUrcTPXOmlFJKKZWH6JgzpZRSSqk8RMOZUkoppVQeouFMKaWUUioP0XCmlFJKKZWHaDhTSimllMpDNJwppZRSSuUh/w8lHpCtWvBwwAAAAABJRU5ErkJggg==\n", + "text/plain": [ + "
" + ] + }, + "metadata": { + "needs_background": "light" + }, + "output_type": "display_data" + } + ], + "source": [ + "%matplotlib inline\n", + "seed_idx = list(range (10, max_seeds +1, 10))\n", + "\n", + "plt.figure(figsize=(10,5))\n", + "\n", + "for i in range(len(data)):\n", + " plt.plot(seed_idx, time_algo_cu[i], label = names[i])\n", + "\n", + "\n", + "plt.title('Runtime vs. Number of Seeds')\n", + "plt.xlabel('Number of Seeds')\n", + "plt.ylabel('Runtime')\n", + "plt.legend()\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4094" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "del time_algo_cu\n", + "gc.collect()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test 3: Multi-seed versus Sequential\n", + "This test uses a single files since sequential execution is slow" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Reading ./data/coPapersCiteseer.mtx...\n", + "\t434,102 nodes, 16,036,720 edges\n" + ] + } + ], + "source": [ + "G = read_and_create('./data/coPapersCiteseer.mtx')\n", + "nodes = G.nodes().to_array().tolist()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "rw_depth = 4\n", + "max_seeds = 100\n", + "num_nodes = G.number_of_nodes()\n", + "runtime_seq = [0] * max_seeds" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "# sequential = so also get a single random seed\n", + "for i in range (max_seeds) :\n", + " for j in range(i):\n", + " seeds = random.sample(nodes, 1)\n", + " t = run_rw(G, seeds, rw_depth)\n", + " runtime_seq[i] = runtime_seq[i] + t" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "runtime = [None] * max_seeds\n", + "\n", + "for i in range (max_seeds) :\n", + " seeds = random.sample(nodes, i+1)\n", + " t = run_rw(G, seeds, rw_depth)\n", + " runtime[i] = t" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAbAAAAEWCAYAAAAHC8LZAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Z1A+gAAAACXBIWXMAAAsTAAALEwEAmpwYAAA3DklEQVR4nO3dd5xU1f3/8ddnZndZOgpYEBFUUASkq4gFC2DBkhiNDaMmMc0Yf4nELho1aiyJvcSer72bxIKoKBa6CCgGUVFAVEA6bJv5/P44d5dhZWGBnb07u+/n47EPdsq99zN3h/uee+6Zc8zdERERyTWJuAsQERHZHAowERHJSQowERHJSQowERHJSQowERHJSQowERHJSQowyQlm1sHMVppZMu5a6iIz62hmbmZ5M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f2VV16hXbt2/Pe//wVg2bJllJaW8vvf/54XXniBtm3b8sQTT3DxxRdz//33c9ZZZ3HXXXfRuXNnxo8fz29/+1veeOMN/vCHP/Cb3/yG0047jdtvv71i/Y8++ihDhw7l4osvJpVKsXr16mq9dhGpZ74aD69eCPMnw/Y94cf/hI4Da7WErAWYmTUFEu6+Ivp9CPCXbG0vmzp16kSPHj0A6NatG4cccghmRo8ePX5wlrUljjnmGBo3bkzjxo056KCDmDBhAr169ap4fNSoUUybNo2nn34aCOH06aefrhNgPXr04E9/+hPnn38+w4YNY//992fGjBnMmDGDwYMHA+FMbvvtt2flypW89957HH/88RXLFxcXA/Duu+/yzDPPADB8+HDOP/98APr378+ZZ55JaWkpxx577Dr1iUgDsPgzGH05zHwRmm8Px94Je54IidrvUpHNM7BtgeeibtV5wKPu/sqWrHBjZ0rZ0qhRo4rfE4lExe1EIkFZWRkAeXl5pNPpiudtznehKndBr3zb3bn11lsZOnRolevo0qULU6ZM4aWXXuKSSy7hkEMO4Uc/+hHdunXj/fffX+e5y5cvp1WrVkydOrVa9QAccMABvP322/z3v//l9NNP549//COnnXZaNV+hiOSs1d/DW3+DifdCsgAGXQT7ng0FTWMrKWuR6e6fu3vP6Kebu1+drW3VBR07dmTKlCkATJkyhS+++OIHz2nevDkrVqyoch0vvPACRUVFLF68mDFjxtC/f/91Hh86dCh33nknpaWha+qsWbNYtWrVOs/5+uuvadKkCaeeeiojRoxgypQp7LbbbixcuLAiwEpLS/noo49o0aIFnTp14qmnngJCQH744YcADBw4kMcffxyARx55pGL9X375Jdtuuy2//OUv+cUvflHxmkWknipdA+/8A27uBRPuhl4nwTlTYND5sYYX5MBQUrniuOOO4+GHH6Zbt27svffedOnS5QfP2XPPPUkmk/Ts2ZPTTz+d3r17/+Dxgw46iEWLFnHppZfSrl27dZoof/GLXzBnzhz69OmDu9O2bdsfXH+bPn06I0aMIJFIkJ+fz5133klBQQFPP/0055xzDsuWLaOsrIxzzz2Xbt268cgjj/Cb3/yGq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gLu7ewlhKpDrotc8Fdg3WtcZwO1mNpVwBlnuBGBGdH934OGN1CSSNRqNXkREcpLOwEREJCcpwEREJCcpwEREJCcpwEREJCcpwEREJCcpwEREJCcpwEREJCf9fyf+/1JKxIZnAAAAAElFTkSuQmCC\n", + "text/plain": [ + "
" + ] + }, + "metadata": { + "needs_background": "light" + }, + "output_type": "display_data" + } + ], + "source": [ + "%matplotlib inline\n", + "seed_idx = list(range(1,max_seeds +1))\n", + "\n", + "\n", + "plt.figure(figsize=(7,4))\n", + "plt.plot(seed_idx, runtime, label = \"multiple seeds\")\n", + "plt.plot(seed_idx, runtime_seq, label = \"sequential\")\n", + "\n", + "\n", + "plt.title('Runtime vs. Number of Seeds')\n", + "plt.xlabel('Number of Seeds')\n", + "plt.ylabel('Runtime')\n", + "plt.legend()\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "-----\n", + "Copyright (c) 2021, NVIDIA CORPORATION.\n", + "\n", + "Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0\n", + "\n", + "Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "cugraph_dev", + "language": "python", + "name": "cugraph_dev" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/notebooks/cugraph_benchmarks/random_walk_perf.ipynb b/notebooks/cugraph_benchmarks/random_walk_perf.ipynb new file mode 100644 index 00000000000..738298767c5 --- /dev/null +++ b/notebooks/cugraph_benchmarks/random_walk_perf.ipynb @@ -0,0 +1,621 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Random Walk Performance\n", + "# Skip notebook testΒΆ \n", + "\n", + "Random walk performance is governed by the length of the paths to find, the number of seeds, and the size or structure of the graph.\n", + "This benchmark will use several test graphs of increasingly larger sizes. While not even multiples in scale, the four test graphs should give an indication of how well Random Walk performs as data size increases. \n", + "\n", + "### Test Data\n", + "Users must run the _dataPrep.sh_ script before running this notebook so that the test files are downloaded\n", + "\n", + "| File Name | Num of Vertices | Num of Edges |\n", + "| ---------------------- | --------------: | -----------: |\n", + "| preferentialAttachment | 100,000 | 999,970 |\n", + "| dblp-2010 | 326,186 | 1,615,400 |\n", + "| coPapersCiteseer | 434,102 | 32,073,440 |\n", + "| as-Skitter | 1,696,415 | 22,190,596 |" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# Import the modules\n", + "import cugraph\n", + "import cudf" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "# system and other\n", + "import gc\n", + "import os\n", + "import time\n", + "import random\n", + "\n", + "# MTX file reader\n", + "from scipy.io import mmread\n", + "\n", + "import networkx as nx" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "try: \n", + " import matplotlib\n", + "except ModuleNotFoundError:\n", + " os.system('pip install matplotlib')\n", + "\n", + "import matplotlib.pyplot as plt; plt.rcdefaults()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "try: \n", + " import pybind11\n", + "except ModuleNotFoundError:\n", + " os.system('pip install pybind11')\n", + " \n", + "import pybind11" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "try: \n", + " import walker\n", + "except ModuleNotFoundError:\n", + " os.system('pip install graph-walker')\n", + "\n", + "import walker" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "# Test File\n", + "data = {\n", + " 'preferentialAttachment' : './data/preferentialAttachment.mtx',\n", + " 'dblp' : './data/dblp-2010.mtx',\n", + " 'coPapersCiteseer' : './data/coPapersCiteseer.mtx',\n", + " 'as-Skitter' : './data/as-Skitter.mtx'\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Read the data and create a graph" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "# Data reader - the file format is MTX, so we will use the reader from SciPy\n", + "def read_data(datafile):\n", + " print('Reading ' + str(datafile) + '...')\n", + " M = mmread(datafile).asfptype()\n", + "\n", + " _gdf = cudf.DataFrame()\n", + " _gdf['src'] = M.row\n", + " _gdf['dst'] = M.col\n", + " _gdf['wt'] = 1.0\n", + " \n", + " return _gdf" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "def create_cu_ugraph(_df):\n", + " _g = cugraph.Graph()\n", + " _g.from_cudf_edgelist(_df, source='src', destination='dst', edge_attr='wt', renumber=False)\n", + " return _g" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "def create_nx_ugraph(_df):\n", + " _gnx = nx.from_pandas_edgelist(_df, source='src', target='dst', edge_attr='wt', create_using=nx.Graph)\n", + " return _gnx" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Define the call to RandomWalk\n", + "We are only interested in the runtime, so throw away the results" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "def run_cu_rw(_G, _seeds, _depth):\n", + " t1 = time.time()\n", + " _, _ = cugraph.random_walks(_G, _seeds, _depth)\n", + " t2 = time.time() - t1\n", + " return t2" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "def run_wk_rw(_G, _seeds, _depth):\n", + " t1 = time.time()\n", + " _ = walker.random_walks(_G, n_walks=1, walk_len=_depth, start_nodes=_seeds)\n", + " t2 = time.time() - t1\n", + " return t2 \n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test 1: Runtime versus path depth" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Reading ./data/preferentialAttachment.mtx...\n", + "\tcuGraph\n", + "\tWalkerRandom walks - T=2.23s\n", + "\tcuGraph\n", + "\tWalkerRandom walks - T=2.48s\n", + "\tcuGraph\n", + "\tWalkerRandom walks - T=2.02s\n", + "\tcuGraph\n", + "\tWalkerRandom walks - T=2.31s\n", + "\tcuGraph\n", + "\tWalkerRandom walks - T=2.01s\n", + "update i\n", + "Reading ./data/dblp-2010.mtx...\n", + "\tcuGraph\n", + "\tWalkerRandom walks - T=4.21s\n", + "\tcuGraph\n", + "\tWalkerRandom walks - T=4.03s\n", + "\tcuGraph\n", + "\tWalkerRandom walks - T=3.59s\n", + "\tcuGraph\n", + "\tWalkerRandom walks - T=3.95s\n", + "\tcuGraph\n", + "\tWalkerRandom walks - T=3.68s\n", + "update i\n", + "Reading ./data/coPapersCiteseer.mtx...\n", + "\tcuGraph\n", + "\tWalkerRandom walks - T=59.64s\n", + "\tcuGraph\n", + "\tWalkerRandom walks - T=49.43s\n", + "\tcuGraph\n", + "\tWalkerRandom walks - T=47.45s\n", + "\tcuGraph\n", + "\tWalkerRandom walks - T=54.66s\n", + "\tcuGraph\n", + "\tWalkerRandom walks - T=46.96s\n", + "update i\n", + "Reading ./data/as-Skitter.mtx...\n", + "\tcuGraph\n", + "\tWalkerRandom walks - T=53.14s\n", + "\tcuGraph\n", + "\tWalkerRandom walks - T=44.36s\n", + "\tcuGraph\n", + "\tWalkerRandom walks - T=46.38s\n", + "\tcuGraph\n", + "\tWalkerRandom walks - T=41.96s\n", + "\tcuGraph\n", + "\tWalkerRandom walks - T=53.18s\n", + "update i\n" + ] + } + ], + "source": [ + "# some parameters\n", + "max_depth = 6\n", + "num_seeds = 100\n", + "\n", + "# arrays to capture performance gains\n", + "names = []\n", + "\n", + "# Two dimension data\n", + "time_algo_cu = [] # will be two dimensional\n", + "time_algo_wk = [] # will be two dimensional\n", + "\n", + "i = 0\n", + "for k,v in data.items():\n", + " time_algo_cu.append([])\n", + " time_algo_wk.append([])\n", + " \n", + " # Saved the file Name\n", + " names.append(k)\n", + "\n", + " # read data\n", + " gdf = read_data(v)\n", + " pdf = gdf.to_pandas()\n", + " \n", + " # Create the Graphs\n", + " Gcg = create_cu_ugraph(gdf)\n", + " Gnx = create_nx_ugraph(pdf)\n", + " \n", + " num_nodes = Gcg.number_of_nodes()\n", + " nodes = Gcg.nodes().to_array().tolist()\n", + "\n", + " seeds = random.sample(nodes, num_seeds)\n", + "\n", + " for j in range (2, max_depth+1) :\n", + " print(\"\\tcuGraph\")\n", + " tc = run_cu_rw(Gcg, seeds, j)\n", + " time_algo_cu[i].append(tc)\n", + " \n", + " print(\"\\tWalker\", end='')\n", + " tw = run_wk_rw(Gnx, seeds, j)\n", + " time_algo_wk[i].append(tw)\n", + "\n", + " # update i\n", + " i = i + 1\n", + " print(\"update i\")\n", + " \n", + " del Gcg\n", + " del Gnx\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAl4AAAFNCAYAAADRi2EuAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Z1A+gAAAACXBIWXMAAAsTAAALEwEAmpwYAACN0ElEQVR4nOzdd3hUxf748ffsphcgQIBQEyAJ6QlJ6CACAopgQYoVrGDv2Lv+roUrKnq/qAjoVRABEfUiIgoSejO0BAgl9B5Ib7s7vz/O5pCQEIKShPJ5Pc8+u2fPzJw5Z9tn58yZUVprhBBCCCFE9bPUdgWEEE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t2YNm0a/fv3B04FXrfccgtgBDJr1qzhmmuuOefya8P5ru8999xD69atSUtLw2KxcPToUSZNmlQmTcmpQHd393JDHmitsdvtZU4XWq1W8/V2OBxkZmZy+PBhlFJmC9npLWYXqvz8fDZv3sz69evZu3cvAK1bt6Znz560a9dOhoAQ567kS/FCvsXHx2shLgUpKSm1XQW9a9cuHRoaqm+55Rbdrl07PXjwYJ2bm6tbtWqlx4wZo+Pi4vS0adP0r7/+qjt16qTj4uL0TTfdpLOzs/Xnn3+u/fz8dGBgoL7lllu01lq/++67OiEhQUdFRemXX37Z3EZISIi+/fbbdXh4uE5PTz9junbt2ul77rlHh4eH66uuukrn5eXpGTNmaG9vbx0SEqJjYmJ0Xl6evuKKK/Tq1au11lqPHj1ax8fH6/DwcLMsrXWZNA6HQwcFBent27frgIAAnZ+fr7XWumPHjrpOnTo6JiZGv/3227pFixa6YcOGOiYmRn/77bd65cqVulOnTjo2NlZ37txZb9myRWuttc1m008++aSOiIjQUVFR+qOPPtJaa92qVSv98ssv67i4OB0ZGalTU1O11lq/8sor+o477tDdunXTLVu21LNmzdJPP/20joyM1P369dNFRUVaa63XrFmje/Toodu3b6/79u2rDxw4YO7LmDFjdGJiog4ODtaLFy/WhYWF5ep7ulWrVunOnTvr6OhonZiYqLOysvTkyZP1gw8+aKYZMGCAXrhwod6+fbsOCgrSdru9wvfKwoULdbdu3fTAgQN1cHCw1lrr6667Trdv316Hh4frTz/91Ezr7e2tH3vsMR0eHq579eqljxw5orXWukePHvqJJ57Q8fHxuk2bNvqnn37Shw4d0vv37y9z27hxo9Za6x07duiFCxeax8dms1X+hq5GP/30k3799df1K6+8osePH6+TkpL0yZMna60+4uIBrNFniGmkxUuIWvLaT5tJOZB19oTnILxpHV4ZGHHWdFu3buWLL76ga9eu3HXXXfznP/8BjEFX161bx7Fjx7jxxhtZsGAB3t7evPPOO7z//vu8/PLLLFmyhGuvvZabbrqJ+fPnk5aWxqpVq9BaM2jQIBYvXkzLli1JS0vjyy+/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mF/165djB07ltWrV+Pn58fIkSMpKCgok8ZutzNr1izmzJnDW2+9ZQ7SmZ2dXWnZAC+99BJXXnkls2fPJj09nZ49e541T8k+lHQ4P/15i8WCq6ureZwtFos5unxERATLly8/p3JL69evH4cPHyYhIYFHH320wjQuLi5lBgkuOV7h4eGsX78eh8OBxWLhhRde4IUXXigzll3p13HRokUsWLCA5cuX4+XlRc+ePcsd+xKl32NV2Y/T61vi9MnWi4qKys17Wdovv/yCi4sLI0eOBOCHH37AYrGUaSXz8fEhLS2NDRs2sHPnTq666iq6dOlCeHg4ISEh0kFeVCu5qlGIy9CePXvMH/upU6fSrVu3Mus7derE0qVL2b59OwC5ubnmSP+l9evXj0mTJpGTkwMYrQ1Hjhz52+lK8/X1rTBQysrKwtvbm7p163L48GF++eWXcml+//13oqOj2bt3L+np6ezevZvBgwcze/bscuWevpyZmWkOBTBlyhTz+auuuopPP/3UDBxO/8H/O0JDQzl69Kj5WhQXF7N58+ZK85xe319//ZXk5GQmTpxIaGgoBw8eZPXq1QBkZ2djs9kIDAwkOTkZh8PB3r17WbVqFQBt27YlISGBF1980Zz6qqCg4IxXH2ZmZuLn54eXlxdbtmxhxYoV5jqHw8HMmTOBit9T54ubmxtNmjQhPDyc7t27c91113HdddeZ6wcOHGi2IJbUecuWLfz2229Mnz6d//znP7z77rvMnj2b48eP0717d9q1awdgtnwJUZ2kxUuIy1BoaCiffPIJd911F+Hh4dx///2MHz/eXO/v78+UKVO4+eabzWEB3nzzTUJCQsqU07dvX1JTU83TkD4+Pnz99dflrlKrarrSRo4cyejRo/H09CzTIlQyx127du3KnDItbdq0adxwww1lnhs8eDD/93//x48//ojVaiUmJoaRI0cyYsQI3n77bWJjY3nuuecYM2YMI0aM4M0332TAgAFm/nvuuYdt27YRHR2Nq6sr9957rzm11N/l5ubGzJkzeeSRR8jMzMRms/HYY48REXHm1tArr7yyTH1LTu+VlDd9+nQefvhh8vPz8fT0ZMGCBXTt2pWgoCDCw8MJCwujffv2Zp6JEy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VFR9O/f35x8e+3atVxxxRXEx8fTr18/Dh48CBitTs888wwdOnQgJCSEpKQkioqKytW3tNWrV5sTUs+ZMwdPT0+KioooKCigdevWAHz++eckJiYSExPD4MGDycvLO+N7xeFwMHLkSHNKoaefftp8DT/99FPAmL+xe/fuDBo06JwDYyEuS1rrar8BPYGfnY9bA6uA7cAMwP1s+ePj47UQl4KUlJSyT0y65uy3JR+WTb/ua+NxzrHyaatg165dGtBLlizRWmt955136vfee0+3atVKv/POO1prrY8ePaq7d++uc3JytNZav/322/q1117TWms9YsQIPWPGDK211r/++qu+9957tcPh0Ha7XQ8YMED/+eefeteuXVoppZcvX37WdFarVf/1119aa62HDBmi//vf/2qttb7iiiv06tWrzXqXXj5+/LjWWmubzaavuOIKvX79+nJplixZonv16qW11vrmm2/WM2fO1FprvXDhQj1gwACz3MmTJ+sHH3zQXM7MzNTFxcVaa61/++03feONN2qttf7Pf/6jBw8ebK4rqUOrVq30Rx99pLXW+pNPPtF333231lrrV155RXft2lUXFRXp5ORk7enpqefOnau11vr666/Xs2fP1kVFRbpz5876yJEjWmutv/32W33nnXea+/LEE09orbX+3//+p3v37l1hfUsrLi7WQUFBWmutn3zySZ2QkKCXLFmiFy1apIcPH6611vrYsWNm+hdeeMGs+yuvvKLfe+89c9vLly/Xw4cP12+++abWWutPP/1Uv/HGG1prrQsKCnR8fLzeuXOnXrhwofby8tI7d+6ssE5VVe6zIcRFDFijzxDT1MhcjVrrRcAi5+OdQIea2K4QomKl5zi87bbb+OijjwDMef9WrFhBSkqKmaaoqMicZ7G0+fPnM3/+fOLi4gBjEM20tDRatmxJq1atzLn8KksXFBREbGwsAPHx8aSnp5+1/t999x2fffYZNpuNgwcPkpKSQnR0dJk006ZNY/jw4QAMHz6cr776isGDB5+17MzMTEaMGEFaWhpKKbNlasGCBYwePRoXF+Nrs379+maeklam+Ph4vv/+e/P5q6++GldXV6KiorDb7fTv3x+AqKgo0tPT2bp1K5s2bTIndbbb7QQEBFRYblWOi4uLC23atCE1NZVVq1bxxBNPsHjxYux2O927dwdg06ZNvPjii5w8eZKcnBz69etXYVmjRo1i6NChvPDCC4DxGm7YsMGcCDszM5O0tDTc3Nzo0KEDQUFBZ62fEEImyRaidt35v7+f3rvBued3MkZ2Kb/s7e0NGC3hV111VZm+URXRWvPcc88xatSoMs+np6ebZZ0tnbu7u7lstVrP2mF/165djB07ltWrV+Pn58fIkSPN04gl7HY7s2bNYs6cObz11ltorTl+/DjZ2dmVlg3w0ksvceWVVzJ79mzS09Pp2bPnWfOU7IPVai3Tz6nkeYvFgqurq3mcLRYLNpsNrTURERFlJgGvSrml9evXj8OHD5OQkMDEiRPp0aMHv/zyC66urvTp04eRI0dit9t57733AGPy8R9++IGYmBimTJnCokWLKiy3S5cuLFy4kCeffBIPDw+01owfP75coLZo0aIyr7UQonIyZZAQl6E9e/aYP/ZTp06lW7duZdZ36tSJpUuXsn37dgByc3PZtm1buXL69evHpEmTzOli9u/fz5EjR/52utJ8fX0rDJSysrLw9vambt26HD58mF9++aVcmt9//53o6Gj27t1Leno6u3fvZvDgwcyePbtcuacvZ2Zm0qyZMcrNlClTzOevuuoqPv30UzMAysjIqLT+VREaGsrRo0fN16K4uJjNmzdXmuf0+v76668kJyczceJEALp3784HH3xA586d8ff35/jx42zdupXIyEgAsrOzCQgIoLi4mG+++eaM27n77ru55pprGDp0KDabjX79+vF///d/Zgvgtm3byM3N/Uf7L8TlSAIvIS5DoaGhfPLJJ4SFhXHixAnuv//+Muv9/f2ZMmUKN998M9HR0XTu3NnsNF5a3759ueWWW+jcuTNRUVHcdNNNFQZLVU1X2siRIxk9enS5YStiYmKIi4ujXbt23HLLLebp0NKmTZvGDTfcUOa5wYMHM23aNKKjo7FarcTExDBu3DiuvPJKUlJSzM7qY8aM4bnnniMuLq5MK9M999xDy5YtiY6OJiYmpsyVkX+Xm5sbM2fO5JlnniEmJobY2FiWLVtWaZ7T63u6jh07cvjwYXr06AFAdHQ0UVFRZmvbG2+8QceOHenatSvt2rWrdF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m/ylwS7qicRvwoanS7r5zvZmZmVlFhupQo5mZmVmdnHiZmZmZVcSJl5mZmVlFnHiZmZmZVcSJl5mZmVlFnHiZ2ZQhaUzSI5Iek3S7pPYH2+bLnivp7Nz7b0q6pMQyXuxXZqIkLZf0ntz76yX9xWQvx8ymPydeZjaVvBwRyyNiGbAfuKpH2XOBs3uMr9JysnsCmZn15MTLzKaq+4GTJf2upA3pYbc/kDQ/Pej9KuDjqYfsHWmaFZL+Q9K2kr1ffynpwfSA6M+kYaOStkj6qqTHJd2T7viPpN9KZR+R9IXUMzcb+CxwaRp+aZr9Ekn/nury0cn+cMxsenLiZWZTTnpm4kpgE/Aj4Mz0sNvbgOsi4kngRuBvUw/Z/WnShcA5wIXAX/dZxvnAKWTPr1sOvDX3EOFTgK9ExFLgeeADafg3gD9ODxwfA4iI/cCngdWpLqtT2TcCv5Pm/1fpWalmNuSG7ZFBZja1zck90up+ssf5nAqsTs8UnA080WP6OyPiILBZ0vw+yzo//f04vT+KLOH6GfBERLTqsREYTc+7PDoiHkjDbyVL8Ir8a3r49D5Ju8ge4bKjT53MrOGceJnZVPJy6k16haQvA1+MiLWSzgWu7zH9vvykfZYl4HMR8Q9tyxttm88YMKfPvPrVZQxvb80MH2o0s6nvGGBnen15bvhe4OgjmO/3gT+UdBSApBMkHV9UOCKeB/ZKelsalH+Y9pHWxcyGhBMvM5vqrgdul7QR+GVu+HeB97WdXF9aRNxDdrjwAUmbgDX0T54+DHw1HQ6dC+xJw+8jO5k+f3K9mVkHRUTddTAzmxYkHRURL6bXnwAWRsS1NVfLzKYRn3NgZlbeeyV9kmzb+RRwRb3VMbPpxj1eZmZmZhXxOV5mZmZmFXHiZWZmZlYRJ15mZmZmFXHiZWZmZlYRJ15mZmZmFXHiZWZmZlaR/wd+5LmtinetbwAAAABJRU5ErkJggg==\n", + "text/plain": [ + "
" + ] + }, + "metadata": { + "needs_background": "light" + }, + "output_type": "display_data" + } + ], + "source": [ + "%matplotlib inline\n", + "seed_idx = list(range(2,max_depth +1))\n", + "\n", + "plt.figure(figsize=(10,5))\n", + "\n", + "for i in range(len(data)):\n", + " plt.plot(seed_idx, time_algo_cu[i], label = (str(names[i] + \"-cuGraph\")))\n", + "\n", + " plt.plot(seed_idx, time_algo_wk[i], label = (str(names[i] + \"-walker\")), linestyle='-.')\n", + "\n", + "\n", + "plt.title(f'Runtime vs. Path Length ({num_seeds} Seeds)')\n", + "plt.xlabel('Path length')\n", + "plt.ylabel('Runtime')\n", + "plt.legend()\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5164" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "del time_algo_cu\n", + "del time_algo_wk\n", + "gc.collect()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test 2: Runtime Speedup versus number of seeds\n", + "The number of seeds will be increased over a range in increments of 50. \n", + "The runtime will be the sum of runtime per increment. Increaing number of seeds by 1 would make for very long execution times " + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Reading ./data/preferentialAttachment.mtx...\n", + "\t.Random walks - T=2.28s\n", + ".Random walks - T=2.29s\n", + ".Random walks - T=2.28s\n", + ".Random walks - T=2.21s\n", + ".Random walks - T=1.95s\n", + ".Random walks - T=2.38s\n", + ".Random walks - T=2.00s\n", + ".Random walks - T=2.19s\n", + ".Random walks - T=1.99s\n", + ".Random walks - T=2.40s\n", + ".Random walks - T=1.95s\n", + ".Random walks - T=2.17s\n", + ".Random walks - T=1.95s\n", + ".Random walks - T=2.39s\n", + ".Random walks - T=1.97s\n", + ".Random walks - T=2.23s\n", + ".Random walks - T=2.63s\n", + ".Random walks - T=4.08s\n", + ".Random walks - T=3.44s\n", + ".Random walks - T=3.77s\n", + " \n", + "Reading ./data/dblp-2010.mtx...\n", + "\t.Random walks - T=6.61s\n", + ".Random walks - T=6.57s\n", + ".Random walks - T=6.48s\n", + ".Random walks - T=6.69s\n", + ".Random walks - T=6.11s\n", + ".Random walks - T=6.18s\n", + ".Random walks - T=4.98s\n", + ".Random walks - T=5.64s\n", + ".Random walks - T=3.83s\n", + ".Random walks - T=4.28s\n", + ".Random walks - T=4.34s\n", + ".Random walks - T=4.14s\n", + ".Random walks - T=3.79s\n", + ".Random walks - T=4.37s\n", + ".Random walks - T=4.00s\n", + ".Random walks - T=3.66s\n", + ".Random walks - T=4.01s\n", + ".Random walks - T=3.67s\n", + ".Random walks - T=4.32s\n", + ".Random walks - T=3.70s\n", + " \n", + "Reading ./data/coPapersCiteseer.mtx...\n", + "\t.Random walks - T=56.64s\n", + ".Random walks - T=52.26s\n", + ".Random walks - T=45.66s\n", + ".Random walks - T=48.81s\n", + ".Random walks - T=56.16s\n", + ".Random walks - T=56.73s\n", + ".Random walks - T=45.43s\n", + ".Random walks - T=44.96s\n", + ".Random walks - T=51.77s\n", + ".Random walks - T=58.39s\n", + ".Random walks - T=43.35s\n", + ".Random walks - T=42.89s\n", + ".Random walks - T=57.96s\n", + ".Random walks - T=45.03s\n", + ".Random walks - T=64.27s\n", + ".Random walks - T=52.57s\n", + ".Random walks - T=46.91s\n", + ".Random walks - T=55.62s\n", + ".Random walks - T=46.85s\n", + ".Random walks - T=44.84s\n", + " \n", + "Reading ./data/as-Skitter.mtx...\n", + "\t.Random walks - T=51.36s\n", + ".Random walks - T=52.06s\n", + ".Random walks - T=44.91s\n", + ".Random walks - T=49.73s\n", + ".Random walks - T=47.45s\n", + ".Random walks - T=52.21s\n", + ".Random walks - T=47.65s\n", + ".Random walks - T=45.49s\n", + ".Random walks - T=47.84s\n", + ".Random walks - T=43.48s\n", + ".Random walks - T=45.67s\n", + ".Random walks - T=45.75s\n", + ".Random walks - T=55.03s\n", + ".Random walks - T=46.39s\n", + ".Random walks - T=50.64s\n", + ".Random walks - T=43.87s\n", + ".Random walks - T=40.98s\n", + ".Random walks - T=49.42s\n", + ".Random walks - T=51.94s\n", + ".Random walks - T=49.28s\n", + " \n" + ] + } + ], + "source": [ + "# some parameters\n", + "rw_depth = 4\n", + "max_seeds = 1000\n", + "\n", + "# arrays to capture performance gains\n", + "names = []\n", + "\n", + "# Two dimension data\n", + "time_algo_cu = [] # will be two dimensional\n", + "time_algo_wk = [] # will be two dimensional\n", + "perf = [] # will be two dimensional\n", + "\n", + "i = 0\n", + "for k,v in data.items():\n", + " time_algo_cu.append([])\n", + " time_algo_wk.append([])\n", + " perf.append([])\n", + " \n", + " # Saved the file Name\n", + " names.append(k)\n", + "\n", + " # read data\n", + " gdf = read_data(v)\n", + " pdf = gdf.to_pandas()\n", + " \n", + " # Create the Graphs\n", + " Gcg = create_cu_ugraph(gdf)\n", + " Gnx = create_nx_ugraph(pdf)\n", + " \n", + " num_nodes = Gcg.number_of_nodes()\n", + " nodes = Gcg.nodes().to_array().tolist()\n", + " \n", + " print('\\t', end='')\n", + " for j in range (50, max_seeds +1, 50) :\n", + " print('.', end='')\n", + " seeds = random.sample(nodes, j)\n", + " tc = run_cu_rw(Gcg, seeds, rw_depth)\n", + " tw = run_wk_rw(Gnx, seeds, rw_depth)\n", + " \n", + " time_algo_cu[i].append(tc)\n", + " time_algo_wk[i].append(tw) \n", + " perf[i].append(tw/tc)\n", + " \n", + "\n", + " # update i\n", + " i = i + 1\n", + " print(\" \")\n", + " \n", + " del Gcg\n", + " del Gnx\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAmQAAAFNCAYAAACuWnPfAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Z1A+gAAAACXBIWXMAAAsTAAALEwEAmpwYAABhrElEQVR4nO3deXhU5d3/8fd3JvtKQsK+KyBbCGFRFNndqlZbta4t1Nra/uxma936W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+PPvookyZN4tFHH422tXYga28KZO1oe+V2/rHyH9QGdRaOiIi0rk2bNnHCCSdw+eWXM2zYMC688EKqq6sZMGAAN9xwA0VFRTzxxBO88sorTJw4kaKiIi666CIqKyv585//zD//+U9uueUWLr/8cgB+/etfM378eAoKCrj11lujrzF06FC+8pWvMHLkSDZv3nzA9YYNG8bXv/51RowYwemnn05NTQ1PPvkkixYt4vLLL4/2xMX2fn3rW99i3LhxjBgxIrqvppxzPPHEE8yZM4f//Oc/0bFVN954I/PmzaOwsJA777yTn/zkJzz++OMUFhby+OOPs3DhQiZOnMiYMWM4+eSTWb16NRAJqddddx0jR46koKCA++67L/pa9913H0VFRYwaNYpVq1YB8NOf/pRZs2Zx6qmn0r9/f5566imuv/56Ro0axZlnnkl9fT0AixcvZsqUKYwdO5YzzjiD7du3A5HevhtuuIEJEyYwZMgQ5s2bRyAQ2K/ez8w512kfY8eOdW1p7qdz3cg5I93C7Qvb9HVERKR9rVy5Mt4luI0bNzrAvf32284557761a+6X//6165///7uzjvvdM45V1xc7E499VRXWVnpnHPujjvucD/72c+cc87NmjXLPfHEE845515++WX39a9/3YXDYRcKhdzZZ5/t3nzzTbdx40ZnZm7+/PmHXM/v97sPPvjAOefcRRdd5B566CHnnHNTpkxx77//frTu2Pndu3c755wLBoNuypQpbtmyZfut8/bbb7vp06c755y79NJL3ZNPPumcc27u3Lnu7LPPju73b3/7m7vmmmui82VlZa6+vt4559x//vMf98UvftE559wf/vAHd8EFF0SXNdTQv39/d++99zrnnPv973/vvva1rznnnLv11lvdKaec4gKBgFu6dKlLTU11L774onPOufPPP989/fTTLhAIuIkTJ7pdu3Y555x77LHH3Fe/+tXoe/nBD37gnHPuhRdecDNmzGi23qaa+xkDFrkDZBpd9uIgxnQbA8CSnUsY32N8nKsREZG28LPnPmLltvJW3efwXlnceu6IQ67Xt29fTjnlFACuuOIK7r33XgAuvvhiABYsWMDKlSuj6wQCASZOnLjffl555RVeeeUVxoyJ/L9VWVnJ2rVr6devH/379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SPsWPHuvaw7Se3ulVjilyotjbatm7POjdyzkj31Jqn2qUGERFpPStXrox3CW7FihVu8ODBrri42Dnn3O7du90555zj5syZ45xz7i9/+Ys777zznHPO3Xrrre7Xv/61c865BQsWuJ49e7pwOOx2797tnHOuurrajRgxwpWUlDjnnAPcP/7xD+eccz/72c/cNddc45xzbvr06W7NmjXR/UybNs0559ysWbPc2Wef7YLBoHPOuXPOOce9/fbbzjnnKioqXH19vbv22mvdb3/722bfy9y5c93ZZ5+9X63OOXfppZe6efPmOeec++STT9wJJ5xwwNd4+eWX3de//nUXDoddKBRyZ599tnvzzTfdypUr3TnnnOMCgYBzzrlvfetb7sEHH4y+18cff/ywP/+21tzPGLDIHSDT6MKwLZA5cwZ7H3+c6gULyJgyBYBB2YPoktyFxTsX84XBX4hzhSIicqTuXHgnq0pXteo+T8g9gRsm3HDQdV5//XUuuugi8vLyAMjNzWX+/Pk89dRTAHz5y1/m+uuvj65/9913849//IPMzEwef/xxzIx7772Xp59+GoDNmzezdu1aunbtis/n4+KLLwbgiiuu4Itf/CKVlZW8++67XHTRRdF91tXVRacvuugi/H4/AKeccgo/+MEPuPzyy/niF79Inz59jvizePXVV1m5cmV0vry8nMrKymZf45VXXuGVV15hzJjIRdgrKytZu3Yty5cvZ/HixdH7fdbU1NCtWzcA/H4/F1xwwRHX11EokLVA2okn4ktPp+LV16KBzMwY022MBvaLiEi7uPbaa7nuuuui82+88Qavvvoq8+fPJy0tjalTp1JbW9vstmZGOBymS5cuLF26tNl10tPTo9M33ngjZ599Ni+++CKnnHIKL7/8MiNGjODNN9887LrD4TALFizY7+bnzb2Gc46bbrqJq6++utG69913H7NmzeKXv/zlfvtPSUmJBsnOTIGsBXxJSWRMmUzF3Ln0CIcxX2To3djuY5m7eS7F1cXkp+XHuUoRETkSh+rJaivTp0/nC1/4Aj/4wQ/o2rUrpaWlnHzyyTz22GN8+ctf5uGHH+bUU0894PZlZWXk5OSQlpbGqlWrWLBgQXRZOBzmySef5JJLLuGRR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fzquvvkrfvn0Pus27774LtE4gCwaDh1+0HJIC2RHImDED6uupfOstALqkdOG47ON0xX4RETks559/PmPHjmXEiBE88MADhEIhZs+ezciRIxk1ahR33333ftts376dvLw8kpOTAcjLy6NXr16N1qmpqeGss87iT3/6ExC5UCxE7h85b948CgsLufPOO/nJT37C448/TmFhIY8//jhVVVVceeWVTJgwgTFjxkSv5D9nzhw+//nPM336dGbMmNGWH8kxS/eyPAKpo0fjz8uj8rXXyPbu51XUvYh/b/w3oXAIv6/z3+RURETa3l//+ldyc3Opqalh/PjxjB07lq1bt7JiRWQIzN69e/fb5vTTT+e2225jyJAhzJw5k4svvpgpU6ZEl1dWVnLJJZfwla98ha985SuNtr3jjju46667eP755wHo3r07ixYt4ne/+x0AN998M9OnT+evf/0re/fuZcKECcycORMg2iPX9N6V0joUyI6A+XxkTptG+YsvEg4E8CUlUdS9iCfWPMGaPWsY1vXovR2HiMjRZscvfkHdx6tadZ/Jw06gx82HvqXevffey9NPPw3A5s2bCQQCbNiwge985zucffbZnH766fttk5GRweLFi5k3bx5z587l4osv5o477mD27NkAnHfeeVx//fVcfvnlh133K6+8wrPPPstdd90FQG1tLZ9++ikAp512msJYG9IhyyOUOXMG4aoqqr2BlGO7jQVgyS6NIxMRkUN74403ePXVV5k/fz7Lli1jzJgx1NXVsWzZMqZOncr999/PVVddxebNmyksLKSwsDB6Y26/38/UqVP52c9+xu9+9zv+9a9/Rfd7yimn8NJLL3Ek96p2zvGvf/2LpUuXsnTpUj799NPo/RjT09Nb541Ls9RDdoTSTjoJX1oaFa++Rsapp9Izoyc903uyeOdiLh92+H+ViIhIfLSkJ6stlJWVkZOTQ1paGqtWrWLBggWUlJQQDoe54IILGDp0KFdccQV9+/Zl6dKl0e1Wr16Nz+dj8ODBQGRgfv/+/aPLb7vtNm677TauueYa/vCHPzR6zczMTCq8E9Kamz/jjDO47777uO+++zAzPvjgA8aMGdNGn4DEUg/ZEfIlJ5M+eTIVr7+GC4eByDiyJTuXHNFfJSI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cvHgBcIqZHe8tSzezIV5t+WY20WtPNLMRzrm9wF4zm+Tt+vKY1xgEbHDO3Qs8472GiBwFFMhEJF5+A8SebfknIiFoGTCRI+u9+pRImPo38E3nXC2RcWYrgSXe5SP+yCGODniHR28E5gLLgMXOuWcO8drdgOe811hOpMfvd95hz9nAo2a2nMhhxxOccwHgQuBO7z0vBU729vVV4PdmtpRID1+DLwErvPaRwN8PUZOIdBLmXNMefhERERFpT+ohExEREYkzBTIRERGROFMgExEREYkzBTIRERGROFMgExEREYkzBTIRERGROFMgExEREYkzBTIRERGROPv/4A3x8Y6Wx8YAAAAASUVORK5CYII=\n", + "text/plain": [ + "
" + ] + }, + "metadata": { + "needs_background": "light" + }, + "output_type": "display_data" + } + ], + "source": [ + "%matplotlib inline\n", + "seed_idx = list(range (50, max_seeds +1, 50))\n", + "\n", + "plt.figure(figsize=(10,5))\n", + "\n", + "for i in range(len(data)):\n", + " plt.plot(seed_idx, perf[i], label = names[i] )\n", + "\n", + "plt.title('Speedup vs. Number of Seeds')\n", + "plt.xlabel('Number of Seeds')\n", + "plt.ylabel('Speedup')\n", + "plt.legend()\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3786" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "del time_algo_cu\n", + "gc.collect()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "-----\n", + "Copyright (c) 2021, NVIDIA CORPORATION.\n", + "\n", + "Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0\n", + "\n", + "Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "cugraph_dev", + "language": "python", + "name": "cugraph_dev" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/notebooks/cugraph_benchmarks/release.ipynb b/notebooks/cugraph_benchmarks/release.ipynb index d3110da3621..a6eeeb65cdf 100644 --- a/notebooks/cugraph_benchmarks/release.ipynb +++ b/notebooks/cugraph_benchmarks/release.ipynb @@ -5,6 +5,7 @@ "metadata": {}, "source": [ "# Release Benchmarking\n", + "# Skip notebook test\n", "\n", "With every release, RAPIDS publishes a release slide deck that includes the current performance state of cuGraph. \n", "This notebook, starting with release 0.15, runs all the various algorithms to computes the performance gain. \n", @@ -21,6 +22,7 @@ "| Triangle Counting | X | |\n", "\n", "### Test Data\n", + "Users must run the _dataPrep.sh_ script before running this notebook so that the test files are downloaded\n", "\n", "| File Name | Num of Vertices | Num of Edges |\n", "| ---------------------- | --------------: | -----------: |\n", @@ -593,7 +595,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.5" + "version": "3.8.8" } }, "nbformat": 4, diff --git a/notebooks/cugraph_benchmarks/sssp_benchmark.ipynb b/notebooks/cugraph_benchmarks/sssp_benchmark.ipynb index 2d040e0acaf..32b562e7a1e 100644 --- a/notebooks/cugraph_benchmarks/sssp_benchmark.ipynb +++ b/notebooks/cugraph_benchmarks/sssp_benchmark.ipynb @@ -5,6 +5,7 @@ "metadata": {}, "source": [ "# SSSP Performance Benchmarking\n", + "# Skip notebook test\n", "\n", "This notebook benchmarks performance of running SSSP within cuGraph against NetworkX. \n", "\n", diff --git a/notebooks/demo/batch_betweenness.ipynb b/notebooks/demo/batch_betweenness.ipynb index e2ad83ff1c4..885d26c9523 100644 --- a/notebooks/demo/batch_betweenness.ipynb +++ b/notebooks/demo/batch_betweenness.ipynb @@ -138,7 +138,7 @@ "outputs": [], "source": [ "t_start_read_sg = time.perf_counter()\n", - "e_list = cudf.read_csv(input_data_path, delimiter='\\t', names=['src', 'dst'], dtype=['int32', 'int32'])\n", + "e_list = cudf.read_csv(input_data_path, delimiter='\\t', names=['src', 'dst'], dtype=['int32', 'int32'], comment='#')\n", "t_stop_read_sg = time.perf_counter()" ] }, diff --git a/notebooks/demo/uvm.ipynb b/notebooks/demo/uvm.ipynb index d279be8ed54..8fa2b08b6d1 100644 --- a/notebooks/demo/uvm.ipynb +++ b/notebooks/demo/uvm.ipynb @@ -6,6 +6,7 @@ "source": [ "# Oversubscribing GPU memory in cuGraph\n", "#### Author : Alex Fender\n", + "# Skip notebook test\n", "\n", "In this notebook, we will show how to **scale to 4x larger graphs than before** without incurring a performance drop using managed memory features in cuGraph. We will compute the PageRank of each user in Twitter's dataset on a single GPU as an example. This technique applies to all features.\n", "\n", diff --git a/notebooks/layout/Force-Atlas2.ipynb b/notebooks/layout/Force-Atlas2.ipynb index fa9ec0fd180..456af3c62de 100644 --- a/notebooks/layout/Force-Atlas2.ipynb +++ b/notebooks/layout/Force-Atlas2.ipynb @@ -4,7 +4,8 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Force Atlas 2" + "# Force Atlas 2\n", + "# Skip notebook test" ] }, { @@ -521,4 +522,4 @@ }, "nbformat": 4, "nbformat_minor": 4 -} +} \ No newline at end of file diff --git a/notebooks/link_analysis/Pagerank.ipynb b/notebooks/link_analysis/Pagerank.ipynb index c43561ff48c..a81e1ccf6c3 100755 --- a/notebooks/link_analysis/Pagerank.ipynb +++ b/notebooks/link_analysis/Pagerank.ipynb @@ -11,7 +11,7 @@ "Notebook Credits\n", "* Original Authors: Bradley Rees and James Wyles\n", "* Created: 08/13/2019\n", - "* Updated: 08/16/2020\n", + "* Updated: 01/17/2021\n", "\n", "RAPIDS Versions: 0.14 \n", "\n", @@ -190,7 +190,7 @@ "metadata": {}, "source": [ "### Read in the data - GPU\n", - "cuGraph depends on cuDF for data loading and the initial Dataframe creation\n", + "cuGraph graphs can be created from cuDF, dask_cuDF and Pandas dataframes\n", "\n", "The data file contains an edge list, which represents the connection of a vertex to another. The `source` to `destination` pairs is in what is known as Coordinate Format (COO). In this test case, the data is just two columns. However a third, `weight`, column is also possible" ] @@ -219,8 +219,7 @@ "outputs": [], "source": [ "# create a Graph using the source (src) and destination (dst) vertex pairs from the Dataframe \n", - "G = cugraph.Graph()\n", - "G.from_cudf_edgelist(gdf, source='src', destination='dst')" + "G = cugraph.from_edgelist(gdf, source='src', destination='dst')" ] }, { diff --git a/notebooks/link_prediction/Jaccard-Similarity.ipynb b/notebooks/link_prediction/Jaccard-Similarity.ipynb index 21835da1cce..7003bdbc98e 100755 --- a/notebooks/link_prediction/Jaccard-Similarity.ipynb +++ b/notebooks/link_prediction/Jaccard-Similarity.ipynb @@ -451,8 +451,9 @@ "metadata": {}, "outputs": [], "source": [ + "pr_df.rename(columns={'pagerank': 'weight'}, inplace=True)", "# Call weighted Jaccard using the Pagerank scores as weights:\n", - "wdf = cugraph.jaccard_w(G, pr_df['pagerank'])" + "wdf = cugraph.jaccard_w(G, pr_df)" ] }, { diff --git a/notebooks/sampling/RandomWalk.ipynb b/notebooks/sampling/RandomWalk.ipynb new file mode 100644 index 00000000000..84f8e1db07f --- /dev/null +++ b/notebooks/sampling/RandomWalk.ipynb @@ -0,0 +1,195 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Random Walk Sampling\n", + "\n", + "In this notebook, we will compute the Random Walk from a set of seeds using cuGraph. \n", + "\n", + "\n", + "| Author Credit | Date | Update | cuGraph Version | Test Hardware |\n", + "| --------------|------------|--------------|-----------------|----------------|\n", + "| Brad Rees | 04/20/2021 | created | 0.19 | GV100, CUDA 11.0\n", + "\n", + "Currently NetworkX does not have a random walk function. There is code on StackOverflow that generats a random walk by getting a vertice and then randomly selection a neighbor and then repeating the process. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Test Data\n", + "We will be using the Zachary Karate club dataset \n", + "*W. W. Zachary, An information flow model for conflict and fission in small groups, Journal of\n", + "Anthropological Research 33, 452-473 (1977).*\n", + "\n", + "\n", + "![Karate Club](../img/zachary_black_lines.png)\n", + "\n", + "\n", + "Because the test data has vertex IDs starting at 1, the auto-renumber feature of cuGraph (mentioned above) will be used so the starting vertex ID is zero for maximum efficiency. The resulting data will then be auto-unrenumbered, making the entire renumbering process transparent to users." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Import the modules\n", + "import cugraph\n", + "import cudf" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Read The Data\n", + "# Define the path to the test data \n", + "datafile='../data/karate-data.csv'\n", + "\n", + "gdf = cudf.read_csv(datafile, delimiter='\\t', names=['src', 'dst'], dtype=['int32', 'int32'] )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "gdf['wt'] = 1.0" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Create a Graph - using the source (src) and destination (dst) vertex pairs from the Dataframe \n", + "G = cugraph.Graph()\n", + "G.from_cudf_edgelist(gdf, source='src', destination='dst', edge_attr='wt')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# some stats on the graph\n", + "(G.number_of_nodes(), G.number_of_edges() )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# create a list with the seeds\n", + "seeds = [17,19]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# random walk path length\n", + "path_length = 4" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "rw, so, sz = cugraph.random_walks(G, seeds, path_length, use_padding=True)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A random walk generates a path from a seed vertex. At each step on the random walk (starting from the seed), the random walker picks a random departing edge to traverse. The random walk will terminate in two situations, when the maximum path length is reached, or when the current vertex on the path has no departing edges to traverse. The result of a single random walk will be a path of some length less than or equal to the maximum path length.\n", + "\n", + "cugraph.random_walks performs a random walk from each of the specified seeds. The output will be a path for each of the seeds. Because the path lengths might be variable length, the return value consists of a pair of outputs.\n", + "\n", + "The first output provides the edges used on the paths.\n", + "\n", + "The second output represents the seed offset, which is a cuDF Series. The seed offset identifies the offset of the first entry in the first output for a particular seed." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "rw.head(10)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "idx = 0\n", + "for i in range(len(seeds)):\n", + " for j in range(path_length):\n", + " print(f\"{rw[idx]}\", end=\" \")\n", + " idx += 1\n", + " print(\" \")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "-----\n", + "Copyright (c) 2021, NVIDIA CORPORATION.\n", + "\n", + "Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0\n", + "\n", + "Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "cugraph_dev", + "language": "python", + "name": "cugraph_dev" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.10" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/python/cugraph/__init__.py b/python/cugraph/__init__.py index d752c868237..9421bee6869 100644 --- a/python/cugraph/__init__.py +++ b/python/cugraph/__init__.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -24,11 +24,17 @@ analyzeClustering_ratio_cut, subgraph, triangles, + ego_graph, + batched_ego_graphs, ) from cugraph.structure import ( Graph, DiGraph, + MultiGraph, + MultiDiGraph, + BiPartiteGraph, + BiPartiteDiGraph, from_edgelist, from_cudf_edgelist, from_pandas_edgelist, @@ -44,7 +50,11 @@ symmetrize, symmetrize_df, symmetrize_ddf, -) + is_weighted, + is_directed, + is_multigraph, + is_bipartite, + is_multipartite) from cugraph.centrality import ( betweenness_centrality, @@ -77,23 +87,26 @@ sssp, shortest_path, filter_unreachable, - shortest_path_length + shortest_path_length, + traveling_salesperson, + concurrent_bfs, + multi_source_bfs, ) from cugraph.tree import minimum_spanning_tree, maximum_spanning_tree from cugraph.utilities import utils -from cugraph.bsp.traversal import bfs_df_pregel - from cugraph.proto.components import strong_connected_component from cugraph.proto.structure import find_bicliques -from cugraph.linear_assignment import hungarian +from cugraph.linear_assignment import hungarian, dense_hungarian from cugraph.layout import force_atlas2 from cugraph.raft import raft_include_test from cugraph.comms import comms +from cugraph.sampling import random_walks, rw_path + # Versioneer from ._version import get_versions diff --git a/python/cugraph/bsp/traversal/bfs_bsp.py b/python/cugraph/bsp/traversal/bfs_bsp.py deleted file mode 100644 index 28a71631443..00000000000 --- a/python/cugraph/bsp/traversal/bfs_bsp.py +++ /dev/null @@ -1,141 +0,0 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -import cudf -from collections import OrderedDict - - -def bfs_df_pregel(_df, start, src_col='src', dst_col='dst', copy_data=True): - """ - This function executes an unwieghted Breadth-First-Search (BFS) traversal - to find the distances and predecessors from a specified starting vertex - - NOTE: Only reachable vertices are returned - NOTE: data is not sorted - - Parameters - ---------- - _df : cudf.dataframe - a dataframe containing the source and destination edge list - - start : same type as 'src' and 'dst' - The index of the graph vertex from which the traversal begins - - src : string - the source column name - - dst : string - the destination column name - - copy_data : Bool - whether we can manipulate the dataframe or if a copy should be made - - - Returns - ------- - df : cudf.DataFrame - df['vertex'][i] gives the vertex id of the i'th vertex - df['distance'][i] gives the path distance for the i'th vertex - from the starting vertex - df['predecessor'][i] gives for the i'th vertex the vertex it was - reached from in the traversal - - Examples - -------- - >>> data_df = - cudf.read_csv('datasets/karate.csv', delimiter=' ', header=None) - >>> df = cugraph.pregel_bfs(data_df, 1, '0', '1') - - """ - - # extract the src and dst into a dataframe that can be modified - if copy_data: - coo_data = _df[[src_col, dst_col]] - else: - coo_data = _df - - coo_data.rename(columns={src_col: 'src', dst_col: 'dst'}, inplace=True) - - # convert the "start" vertex into a series - frontier = cudf.Series(start).to_frame('dst') - - # create the answer DF - answer = cudf.DataFrame() - answer['vertex'] = start - answer['distance'] = 0 - answer['predecessor'] = -1 - - # init some variables - distance = 0 - done = False - - while not done: - - # --------------------------------- - # update the distance and add it to the dataframe - distance = distance + 1 - frontier['distance'] = distance - - # ----------------------------------- - # Removed all instances of the frontier vertices from 'dst' side - # we do not want to hop to a vertex that has already been seen - coo_data = coo_data.merge(frontier, on=['dst'], how='left') - coo_data = coo_data[coo_data.distance.isnull()] - coo_data.drop('distance', inplace=True) - - # now update column names for finding source vertices - frontier.rename(columns={'dst': 'src'}, inplace=True) - - # --------------------------------- - # merge the list of vertices and distances with the COO list - # there are two sets of results that we get from the "hop_df" merge - # (A) the set of edges that start with a vertice in the frontier set - # - this goes into the answer set - # - this also forms the next frontier set - # (B) the set of edges that did not start with a frontier vertex - # - this form the new set of coo_data - hop_df = coo_data.merge(frontier, on=['src'], how='left') - - # --------------------------------- - # (A) get the data where the 'src' was in the frontier list - # create a new dataframe of vertices to hop out from (the 'dst') - one_hop = hop_df.query("distance == @distance") - frontier = one_hop['dst'].to_frame('dst') - - # --------------------------------- - # (B) get all the edges that where not touched - coo_data = hop_df[hop_df.distance.isnull()] - coo_data.drop('distance', inplace=True) - - # --------------------------------- - # update the answer - one_hop.rename( - columns={'dst': 'vertex', 'src': 'predecessor'}, inplace=True) - - # remote duplicates. smallest vertex wins - aggsOut = OrderedDict() - aggsOut['predecessor'] = 'min' - aggsOut['distance'] = 'min' - _a = one_hop.groupby(['vertex'], as_index=False).agg(aggsOut) - - answer = cudf.concat([answer, _a]) - - if len(coo_data) == 0: - done = True - - if not done and len(frontier) == 0: - done = True - - # all done, return the answer - return answer diff --git a/python/cugraph/centrality/__init__.py b/python/cugraph/centrality/__init__.py index da882a61850..f33df2fe61a 100644 --- a/python/cugraph/centrality/__init__.py +++ b/python/cugraph/centrality/__init__.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -12,7 +12,7 @@ # limitations under the License. from cugraph.centrality.katz_centrality import katz_centrality -from cugraph.centrality.betweenness_centrality import betweenness_centrality from cugraph.centrality.betweenness_centrality import ( + betweenness_centrality, edge_betweenness_centrality, ) diff --git a/python/cugraph/centrality/betweenness_centrality.pxd b/python/cugraph/centrality/betweenness_centrality.pxd index 829d7be37d9..7abc9009cc8 100644 --- a/python/cugraph/centrality/betweenness_centrality.pxd +++ b/python/cugraph/centrality/betweenness_centrality.pxd @@ -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. # You may obtain a copy of the License at @@ -19,7 +19,7 @@ from cugraph.structure.graph_primtypes cimport * from libcpp cimport bool -cdef extern from "algorithms.hpp" namespace "cugraph": +cdef extern from "cugraph/algorithms.hpp" namespace "cugraph": cdef void betweenness_centrality[VT, ET, WT, result_t]( const handle_t &handle, diff --git a/python/cugraph/centrality/betweenness_centrality.py b/python/cugraph/centrality/betweenness_centrality.py index 634cc2aa7a2..3b7cfe6b68f 100644 --- a/python/cugraph/centrality/betweenness_centrality.py +++ b/python/cugraph/centrality/betweenness_centrality.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -36,8 +36,10 @@ def betweenness_centrality( Betweenness centrality is a measure of the number of shortest paths that pass through a vertex. A vertex with a high betweenness centrality score has more paths passing through it and is therefore believed to be more - important. Rather than doing an all-pair shortest path, a sample of k - starting vertices can be used. + important. + + To improve performance. rather than doing an all-pair shortest path, + a sample of k starting vertices can be used. CuGraph does not currently support the 'endpoints' and 'weight' parameters as seen in the corresponding networkX call. @@ -52,19 +54,18 @@ def betweenness_centrality( k : int or list or None, optional, default=None If k is not None, use k node samples to estimate betweenness. Higher - values give better approximation - If k is a list, use the content of the list for estimation: the list - should contain vertices identifiers. - If k is None (the default), all the vertices are used to estimate - betweenness. - Vertices obtained through sampling or defined as a list will be used as - sources for traversals inside the algorithm. + values give better approximation. If k is a list, use the content + of the list for estimation: the list should contain vertex + identifiers. If k is None (the default), all the vertices are used + to estimate betweenness. Vertices obtained through sampling or + defined as a list will be used assources for traversals inside the + algorithm. normalized : bool, optional Default is True. If true, the betweenness values are normalized by - 2 / ((n - 1) * (n - 2)) for Graphs (undirected), and - 1 / ((n - 1) * (n - 2)) for DiGraphs (directed graphs) + __2 / ((n - 1) * (n - 2))__ for Graphs (undirected), and + __1 / ((n - 1) * (n - 2))__ for DiGraphs (directed graphs) where n is the number of nodes in G. Normalization will ensure that values are in [0, 1], this normalization scales for the highest possible value where one @@ -145,15 +146,22 @@ def betweenness_centrality( def edge_betweenness_centrality( - G, k=None, normalized=True, weight=None, seed=None, result_dtype=np.float64 + G, + k=None, + normalized=True, + weight=None, + seed=None, + result_dtype=np.float64 ): """ Compute the edge betweenness centrality for all edges of the graph G. Betweenness centrality is a measure of the number of shortest paths that pass over an edge. An edge with a high betweenness centrality score has more paths passing over it and is therefore believed to be - more important. Rather than doing an all-pair shortest path, a sample - of k starting vertices can be used. + more important. + + To improve performance, rather than doing an all-pair shortest path, + a sample of k starting vertices can be used. CuGraph does not currently support the 'weight' parameter as seen in the corresponding networkX call. @@ -168,7 +176,7 @@ def edge_betweenness_centrality( k : int or list or None, optional, default=None If k is not None, use k node samples to estimate betweenness. Higher - values give better approximation + values give better approximation. If k is a list, use the content of the list for estimation: the list should contain vertices identifiers. Vertices obtained through sampling or defined as a list will be used as @@ -233,7 +241,6 @@ def edge_betweenness_centrality( >>> G.from_cudf_edgelist(gdf, source='0', destination='1') >>> ebc = cugraph.edge_betweenness_centrality(G) """ - if weight is not None: raise NotImplementedError( "weighted implementation of betweenness " @@ -254,8 +261,16 @@ def edge_betweenness_centrality( df = G.unrenumber(df, "dst") if type(G) is cugraph.Graph: + # select the lower triangle of the df based on src/dst vertex value lower_triangle = df['src'] >= df['dst'] - df[["src", "dst"]][lower_triangle] = df[["dst", "src"]][lower_triangle] + # swap the src and dst vertices for the lower triangle only. Because + # this is a symmeterized graph, this operation results in a df with + # multiple src/dst entries. + df['src'][lower_triangle], df['dst'][lower_triangle] = \ + df['dst'][lower_triangle], df['src'][lower_triangle] + # overwrite the df with the sum of the values for all alike src/dst + # vertex pairs, resulting in half the edges of the original df from the + # symmeterized graph. df = df.groupby(by=["src", "dst"]).sum().reset_index() if isNx is True: diff --git a/python/cugraph/centrality/betweenness_centrality_wrapper.pyx b/python/cugraph/centrality/betweenness_centrality_wrapper.pyx index e3d6e04006f..e63b6996816 100644 --- a/python/cugraph/centrality/betweenness_centrality_wrapper.pyx +++ b/python/cugraph/centrality/betweenness_centrality_wrapper.pyx @@ -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. # You may obtain a copy of the License at @@ -17,18 +17,12 @@ # cython: language_level = 3 from cugraph.centrality.betweenness_centrality cimport betweenness_centrality as c_betweenness_centrality -from cugraph.centrality.betweenness_centrality cimport handle_t -from cugraph.structure.graph import DiGraph +from cugraph.structure.graph_classes import DiGraph from cugraph.structure.graph_primtypes cimport * from libc.stdint cimport uintptr_t from libcpp cimport bool import cudf import numpy as np -import numpy.ctypeslib as ctypeslib - -import dask_cudf -import dask_cuda - import cugraph.comms.comms as Comms from cugraph.dask.common.mg_utils import get_client import dask.distributed diff --git a/python/cugraph/centrality/edge_betweenness_centrality_wrapper.pyx b/python/cugraph/centrality/edge_betweenness_centrality_wrapper.pyx index 3c14d590750..095d291c45e 100644 --- a/python/cugraph/centrality/edge_betweenness_centrality_wrapper.pyx +++ b/python/cugraph/centrality/edge_betweenness_centrality_wrapper.pyx @@ -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. # You may obtain a copy of the License at @@ -18,14 +18,12 @@ from cugraph.centrality.betweenness_centrality cimport edge_betweenness_centrality as c_edge_betweenness_centrality from cugraph.structure import graph_primtypes_wrapper -from cugraph.structure.graph import DiGraph, Graph +from cugraph.structure.graph_classes import DiGraph, Graph from cugraph.structure.graph_primtypes cimport * from libc.stdint cimport uintptr_t from libcpp cimport bool import cudf import numpy as np -import numpy.ctypeslib as ctypeslib - from cugraph.dask.common.mg_utils import get_client import cugraph.comms.comms as Comms import dask.distributed diff --git a/python/cugraph/centrality/katz_centrality.pxd b/python/cugraph/centrality/katz_centrality.pxd index ebf94c78263..c48a90904da 100644 --- a/python/cugraph/centrality/katz_centrality.pxd +++ b/python/cugraph/centrality/katz_centrality.pxd @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -16,10 +16,10 @@ # cython: embedsignature = True # cython: language_level = 3 -from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * from libcpp cimport bool -cdef extern from "utilities/cython.hpp" namespace "cugraph::cython": +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": cdef void call_katz_centrality[VT,WT]( const handle_t &handle, diff --git a/python/cugraph/centrality/katz_centrality.py b/python/cugraph/centrality/katz_centrality.py index 3e2680a196f..a1e7c1b2349 100644 --- a/python/cugraph/centrality/katz_centrality.py +++ b/python/cugraph/centrality/katz_centrality.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -39,14 +39,16 @@ def katz_centrality( Attenuation factor defaulted to None. If alpha is not specified then it is internally calculated as 1/(degree_max) where degree_max is the maximum out degree. - NOTE : The maximum acceptable value of alpha for convergence - alpha_max = 1/(lambda_max) where lambda_max is the largest eigenvalue - of the graph. - Since lambda_max is always lesser than or equal to degree_max for a - graph, alpha_max will always be greater than or equal to - (1/degree_max). Therefore, setting alpha to (1/degree_max) will - guarantee that it will never exceed alpha_max thus in turn fulfilling - the requirement for convergence. + + NOTE + The maximum acceptable value of alpha for convergence + alpha_max = 1/(lambda_max) where lambda_max is the largest + eigenvalue of the graph. + Since lambda_max is always lesser than or equal to degree_max for a + graph, alpha_max will always be greater than or equal to + (1/degree_max). Therefore, setting alpha to (1/degree_max) will + guarantee that it will never exceed alpha_max thus in turn + fulfilling the requirement for convergence. beta : None A weight scalar - currently Not Supported max_iter : int @@ -104,7 +106,11 @@ def katz_centrality( if nstart is not None: if G.renumbered is True: - nstart = G.add_internal_vertex_id(nstart, 'vertex', 'vertex') + if len(G.renumber_map.implementation.col_names) > 1: + cols = nstart.columns[:-1].to_list() + else: + cols = 'vertex' + nstart = G.add_internal_vertex_id(nstart, 'vertex', cols) df = katz_centrality_wrapper.katz_centrality( G, alpha, max_iter, tol, nstart, normalized diff --git a/python/cugraph/centrality/katz_centrality_wrapper.pyx b/python/cugraph/centrality/katz_centrality_wrapper.pyx index 088042395fd..038723ad9bf 100644 --- a/python/cugraph/centrality/katz_centrality_wrapper.pyx +++ b/python/cugraph/centrality/katz_centrality_wrapper.pyx @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -17,13 +17,10 @@ # cython: language_level = 3 from cugraph.centrality.katz_centrality cimport call_katz_centrality -from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * from cugraph.structure import graph_primtypes_wrapper -from libcpp cimport bool from libc.stdint cimport uintptr_t - import cudf -import rmm import numpy as np @@ -37,7 +34,7 @@ def get_output_df(input_graph, nstart): if len(nstart) != num_verts: raise ValueError('nstart must have initial guess for all vertices') - nstart['values'] = graph_primtypes_wrapper.datatype_cast([nstart['values']], [np.float64]) + nstart['values'] = graph_primtypes_wrapper.datatype_cast([nstart['values']], [np.float64])[0] df['katz_centrality'][nstart['vertex']] = nstart['values'] return df diff --git a/python/cugraph/comms/comms.pxd b/python/cugraph/comms/comms.pxd index 44f7ee77562..5bc24c0d639 100644 --- a/python/cugraph/comms/comms.pxd +++ b/python/cugraph/comms/comms.pxd @@ -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. # You may obtain a copy of the License at @@ -16,10 +16,10 @@ # cython: embedsignature = True # cython: language_level = 3 -from cugraph.structure.graph_primtypes cimport handle_t +from cugraph.raft.common.handle cimport * -cdef extern from "utilities/cython.hpp" namespace "cugraph::cython": +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": cdef void init_subcomms(handle_t &handle, size_t row_comm_size) diff --git a/python/cugraph/comms/comms.py b/python/cugraph/comms/comms.py index 925f4a1a060..85fc426f373 100644 --- a/python/cugraph/comms/comms.py +++ b/python/cugraph/comms/comms.py @@ -1,4 +1,4 @@ -# Copyright (c) 2018-2020, NVIDIA CORPORATION. +# Copyright (c) 2018-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. # You may obtain a copy of the License at @@ -12,7 +12,7 @@ # limitations under the License. from cugraph.raft.dask.common.comms import Comms as raftComms -from cugraph.raft.dask.common.comms import worker_state +from cugraph.raft.dask.common.comms import get_raft_comm_state from cugraph.raft.common.handle import Handle from cugraph.comms.comms_wrapper import init_subcomms as c_init_subcomms from dask.distributed import default_client @@ -196,12 +196,12 @@ def get_default_handle(): # Functions to be called from within workers def get_handle(sID): - sessionstate = worker_state(sID) + sessionstate = get_raft_comm_state(sID) return sessionstate['handle'] def get_worker_id(sID): - sessionstate = worker_state(sID) + sessionstate = get_raft_comm_state(sID) return sessionstate['wid'] @@ -216,5 +216,5 @@ def get_n_workers(sID=None): if sID is None: return read_utils.get_n_workers() else: - sessionstate = worker_state(sID) + sessionstate = get_raft_comm_state(sID) return sessionstate['nworkers'] diff --git a/python/cugraph/comms/comms_wrapper.pyx b/python/cugraph/comms/comms_wrapper.pyx index c1148b4c887..09fa3b1c5c7 100644 --- a/python/cugraph/comms/comms_wrapper.pyx +++ b/python/cugraph/comms/comms_wrapper.pyx @@ -1,5 +1,23 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. -from cugraph.structure.graph_primtypes cimport handle_t +# cython: profile=False +# distutils: language = c++ +# cython: embedsignature = True +# cython: language_level = 3 + + +from cugraph.raft.common.handle cimport * from cugraph.comms.comms cimport init_subcomms as c_init_subcomms diff --git a/python/cugraph/community/__init__.py b/python/cugraph/community/__init__.py index d3bb6472894..9cc92637e20 100644 --- a/python/cugraph/community/__init__.py +++ b/python/cugraph/community/__init__.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -25,3 +25,5 @@ from cugraph.community.triangle_count import triangles from cugraph.community.ktruss_subgraph import ktruss_subgraph from cugraph.community.ktruss_subgraph import k_truss +from cugraph.community.egonet import ego_graph +from cugraph.community.egonet import batched_ego_graphs diff --git a/python/cugraph/community/ecg.pxd b/python/cugraph/community/ecg.pxd index 9f1dc269b6f..4f13237eac7 100644 --- a/python/cugraph/community/ecg.pxd +++ b/python/cugraph/community/ecg.pxd @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -19,7 +19,7 @@ from cugraph.structure.graph_primtypes cimport * -cdef extern from "algorithms.hpp" namespace "cugraph": +cdef extern from "cugraph/algorithms.hpp" namespace "cugraph": cdef void ecg[VT,ET,WT]( const handle_t &handle, diff --git a/python/cugraph/community/egonet.pxd b/python/cugraph/community/egonet.pxd new file mode 100644 index 00000000000..acf93330447 --- /dev/null +++ b/python/cugraph/community/egonet.pxd @@ -0,0 +1,23 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +from cugraph.structure.graph_utilities cimport * + +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": + cdef unique_ptr[cy_multi_edgelists_t] call_egonet[vertex_t, weight_t]( + const handle_t &handle, + const graph_container_t &g, + vertex_t* source_vertex, + vertex_t n_subgraphs, + vertex_t radius) except + diff --git a/python/cugraph/community/egonet.py b/python/cugraph/community/egonet.py new file mode 100644 index 00000000000..5ae025f1203 --- /dev/null +++ b/python/cugraph/community/egonet.py @@ -0,0 +1,166 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from cugraph.community import egonet_wrapper +import cudf +from cugraph.utilities import ( + ensure_cugraph_obj, + import_optional, +) +from cugraph.utilities import cugraph_to_nx + +# optional dependencies used for handling different input types +nx = import_optional("networkx") + + +def _convert_graph_to_output_type(G, input_type): + """ + Given a cugraph.Graph, convert it to a new type appropriate for the + graph algos in this module, based on input_type. + """ + if (nx is not None) and (input_type in [nx.Graph, nx.DiGraph]): + return cugraph_to_nx(G) + + else: + return G + + +def _convert_df_series_to_output_type(df, offsets, input_type): + """ + Given a cudf.DataFrame df, convert it to a new type appropriate for the + graph algos in this module, based on input_type. + """ + if (nx is not None) and (input_type in [nx.Graph, nx.DiGraph]): + return df.to_pandas(), offsets.values_host.tolist() + + else: + return df, offsets + + +def ego_graph(G, n, radius=1, center=True, undirected=False, distance=None): + """ + Compute the induced subgraph of neighbors centered at node n, + within a given radius. + + Parameters + ---------- + G : cugraph.Graph, networkx.Graph, CuPy or SciPy sparse matrix + Graph or matrix object, which should contain the connectivity + information. Edge weights, if present, should be single or double + precision floating point values. + n : integer or cudf.DataFrame + A single node as integer or a cudf.DataFrame if nodes are + represented with multiple columns. If a cudf.DataFrame is provided, + only the first row is taken as the node input. + radius: integer, optional + Include all neighbors of distance<=radius from n. + center: bool, optional + Defaults to True. False is not supported + undirected: bool, optional + Defaults to False. True is not supported + distance: key, optional + Distances are counted in hops from n. Other cases are not supported. + + Returns + ------- + G_ego : cuGraph.Graph or networkx.Graph + A graph descriptor with a minimum spanning tree or forest. + The networkx graph will not have all attributes copied over + + Examples + -------- + >>> M = cudf.read_csv('datasets/karate.csv', + delimiter = ' ', + dtype=['int32', 'int32', 'float32'], + header=None) + >>> G = cugraph.Graph() + >>> G.from_cudf_edgelist(M, source='0', destination='1') + >>> ego_graph = cugraph.ego_graph(G, seed, radius=2) + + """ + + (G, input_type) = ensure_cugraph_obj(G, nx_weight_attr="weight") + result_graph = type(G)() + + if G.renumbered is True: + if isinstance(n, cudf.DataFrame): + n = G.lookup_internal_vertex_id(n, n.columns) + else: + n = G.lookup_internal_vertex_id(cudf.Series([n])) + + df, offsets = egonet_wrapper.egonet(G, n, radius) + + if G.renumbered: + df, src_names = G.unrenumber(df, "src", get_column_names=True) + df, dst_names = G.unrenumber(df, "dst", get_column_names=True) + + if G.edgelist.weights: + result_graph.from_cudf_edgelist( + df, source=src_names, destination=dst_names, + edge_attr="weight" + ) + else: + result_graph.from_cudf_edgelist(df, source=src_names, + destination=dst_names) + return _convert_graph_to_output_type(result_graph, input_type) + + +def batched_ego_graphs( + G, seeds, radius=1, center=True, undirected=False, distance=None +): + """ + Compute the induced subgraph of neighbors for each node in seeds + within a given radius. + + Parameters + ---------- + G : cugraph.Graph, networkx.Graph, CuPy or SciPy sparse matrix + Graph or matrix object, which should contain the connectivity + information. Edge weights, if present, should be single or double + precision floating point values. + seeds : cudf.Series or list or cudf.DataFrame + Specifies the seeds of the induced egonet subgraphs. + radius: integer, optional + Include all neighbors of distance<=radius from n. + center: bool, optional + Defaults to True. False is not supported + undirected: bool, optional + Defaults to False. True is not supported + distance: key, optional + Distances are counted in hops from n. Other cases are not supported. + + Returns + ------- + ego_edge_lists : cudf.DataFrame or pandas.DataFrame + GPU data frame containing all induced sources identifiers, + destination identifiers, edge weights + seeds_offsets: cudf.Series + Series containing the starting offset in the returned edge list + for each seed. + """ + + (G, input_type) = ensure_cugraph_obj(G, nx_weight_attr="weight") + + if G.renumbered is True: + if isinstance(seeds, cudf.DataFrame): + seeds = G.lookup_internal_vertex_id(seeds, seeds.columns) + else: + seeds = G.lookup_internal_vertex_id(cudf.Series(seeds)) + + df, offsets = egonet_wrapper.egonet(G, seeds, radius) + + if G.renumbered: + df = G.unrenumber(df, "src", preserve_order=True) + df = G.unrenumber(df, "dst", preserve_order=True) + + return _convert_df_series_to_output_type(df, offsets, input_type) diff --git a/python/cugraph/community/egonet_wrapper.pyx b/python/cugraph/community/egonet_wrapper.pyx new file mode 100644 index 00000000000..418fc50b712 --- /dev/null +++ b/python/cugraph/community/egonet_wrapper.pyx @@ -0,0 +1,124 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from cugraph.community.egonet cimport call_egonet +from cugraph.structure.graph_utilities cimport * +from libcpp cimport bool +from libc.stdint cimport uintptr_t +from cugraph.structure import graph_primtypes_wrapper +import cudf +import numpy as np +from rmm._lib.device_buffer cimport DeviceBuffer +from cudf.core.buffer import Buffer + + +def egonet(input_graph, vertices, radius=1): + """ + Call egonet + """ + # FIXME: Offsets and indices are currently hardcoded to int, but this may + # not be acceptable in the future. + numberTypeMap = {np.dtype("int32") : numberTypeEnum.int32Type, + np.dtype("int64") : numberTypeEnum.int64Type, + np.dtype("float32") : numberTypeEnum.floatType, + np.dtype("double") : numberTypeEnum.doubleType} + + [src, dst] = graph_primtypes_wrapper.datatype_cast([input_graph.edgelist.edgelist_df['src'], input_graph.edgelist.edgelist_df['dst']], [np.int32]) + vertex_t = src.dtype + edge_t = np.dtype("int32") + weights = None + if input_graph.edgelist.weights: + weights = input_graph.edgelist.edgelist_df['weights'] + + num_verts = input_graph.number_of_vertices() + num_edges = input_graph.number_of_edges(directed_edges=True) + num_local_edges = num_edges + + cdef uintptr_t c_src_vertices = src.__cuda_array_interface__['data'][0] + cdef uintptr_t c_dst_vertices = dst.__cuda_array_interface__['data'][0] + cdef uintptr_t c_edge_weights = NULL + if weights is not None: + c_edge_weights = weights.__cuda_array_interface__['data'][0] + weight_t = weights.dtype + is_weighted = True + else: + weight_t = np.dtype("float32") + is_weighted = False + + is_symmetric = not input_graph.is_directed() + + # Pointers for egonet + vertices = vertices.astype('int32') + cdef uintptr_t c_source_vertex_ptr = vertices.__cuda_array_interface__['data'][0] + n_subgraphs = vertices.size + n_streams = 1 + if n_subgraphs > 1 : + n_streams = min(n_subgraphs, 32) + cdef unique_ptr[handle_t] handle_ptr + handle_ptr.reset(new handle_t(n_streams)) + handle_ = handle_ptr.get(); + + cdef graph_container_t graph_container + populate_graph_container(graph_container, + handle_[0], + c_src_vertices, c_dst_vertices, c_edge_weights, + NULL, + NULL, + 0, + ((numberTypeMap[vertex_t])), + ((numberTypeMap[edge_t])), + ((numberTypeMap[weight_t])), + num_local_edges, + num_verts, + num_edges, + is_weighted, + is_symmetric, + False, False) + + if(weight_t==np.dtype("float32")): + el_struct_ptr = move(call_egonet[int, float](handle_[0], + graph_container, + c_source_vertex_ptr, + n_subgraphs, + radius)) + else: + el_struct_ptr = move(call_egonet[int, double](handle_[0], + graph_container, + c_source_vertex_ptr, + n_subgraphs, + radius)) + + el_struct = move(el_struct_ptr.get()[0]) + src = DeviceBuffer.c_from_unique_ptr(move(el_struct.src_indices)) + dst = DeviceBuffer.c_from_unique_ptr(move(el_struct.dst_indices)) + wgt = DeviceBuffer.c_from_unique_ptr(move(el_struct.edge_data)) + src = Buffer(src) + dst = Buffer(dst) + wgt = Buffer(wgt) + + src = cudf.Series(data=src, dtype=vertex_t) + dst = cudf.Series(data=dst, dtype=vertex_t) + + df = cudf.DataFrame() + df['src'] = src + df['dst'] = dst + if wgt.nbytes != 0: + wgt = cudf.Series(data=wgt, dtype=weight_t) + df['weight'] = wgt + + offsets = DeviceBuffer.c_from_unique_ptr(move(el_struct.subgraph_offsets)) + offsets = Buffer(offsets) + offsets = cudf.Series(data=offsets, dtype="int") + + return df, offsets + diff --git a/python/cugraph/community/ktruss_subgraph.pxd b/python/cugraph/community/ktruss_subgraph.pxd index ab3a5189414..d993c31c375 100644 --- a/python/cugraph/community/ktruss_subgraph.pxd +++ b/python/cugraph/community/ktruss_subgraph.pxd @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -19,7 +19,7 @@ from cugraph.structure.graph_primtypes cimport * -cdef extern from "algorithms.hpp" namespace "cugraph": +cdef extern from "cugraph/algorithms.hpp" namespace "cugraph": cdef unique_ptr[GraphCOO[VT,ET,WT]] k_truss_subgraph[VT,ET,WT]( const GraphCOOView[VT,ET,WT] &graph, diff --git a/python/cugraph/community/ktruss_subgraph.py b/python/cugraph/community/ktruss_subgraph.py index 8e4f1471955..afa7d66d31d 100644 --- a/python/cugraph/community/ktruss_subgraph.py +++ b/python/cugraph/community/ktruss_subgraph.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -12,15 +12,36 @@ # limitations under the License. from cugraph.community import ktruss_subgraph_wrapper -from cugraph.structure.graph import Graph +from cugraph.structure.graph_classes import Graph from cugraph.utilities import check_nx_graph from cugraph.utilities import cugraph_to_nx +from numba import cuda + + +# FIXME: special case for ktruss on CUDA 11.4: an 11.4 bug causes ktruss to +# crash in that environment. Allow ktruss to import on non-11.4 systems, but +# raise an exception if ktruss is directly imported on 11.4. +def _ensure_compatible_cuda_version(): + try: + cuda_version = cuda.runtime.get_version() + except cuda.cudadrv.runtime.CudaRuntimeAPIError: + cuda_version = "n/a" + + unsupported_cuda_version = (11, 4) + + if cuda_version == unsupported_cuda_version: + ver_string = ".".join([str(n) for n in unsupported_cuda_version]) + raise NotImplementedError("k_truss is not currently supported in CUDA" + f" {ver_string} environments.") + def k_truss(G, k): """ Returns the K-Truss subgraph of a graph for a specific k. + NOTE: this function is currently not available on CUDA 11.4 systems. + The k-truss of a graph is a subgraph where each edge is part of at least (kβˆ’2) triangles. K-trusses are used for finding tighlty knit groups of vertices in a graph. A k-truss is a relaxation of a k-clique in the graph @@ -44,6 +65,8 @@ def k_truss(G, k): The networkx graph will NOT have all attributes copied over """ + _ensure_compatible_cuda_version() + G, isNx = check_nx_graph(G) if isNx is True: @@ -60,6 +83,8 @@ def ktruss_subgraph(G, k, use_weights=True): """ Returns the K-Truss subgraph of a graph for a specific k. + NOTE: this function is currently not available on CUDA 11.4 systems. + The k-truss of a graph is a subgraph where each edge is part of at least (kβˆ’2) triangles. K-trusses are used for finding tighlty knit groups of vertices in a graph. A k-truss is a relaxation of a k-clique in the graph @@ -117,6 +142,8 @@ def ktruss_subgraph(G, k, use_weights=True): >>> k_subgraph = cugraph.ktruss_subgraph(G, 3) """ + _ensure_compatible_cuda_version() + KTrussSubgraph = Graph() if type(G) is not Graph: raise Exception("input graph must be undirected") diff --git a/python/cugraph/community/ktruss_subgraph_wrapper.pyx b/python/cugraph/community/ktruss_subgraph_wrapper.pyx index 9f8138f4d57..d3b7a38ba41 100644 --- a/python/cugraph/community/ktruss_subgraph_wrapper.pyx +++ b/python/cugraph/community/ktruss_subgraph_wrapper.pyx @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -19,12 +19,6 @@ from cugraph.community.ktruss_subgraph cimport * from cugraph.structure.graph_primtypes cimport * from cugraph.structure import graph_primtypes_wrapper -from libcpp cimport bool -from libc.stdint cimport uintptr_t -from libc.float cimport FLT_MAX_EXP - -import cudf -import rmm import numpy as np @@ -39,6 +33,10 @@ def ktruss_subgraph_double(input_graph, k, use_weights): def ktruss_subgraph(input_graph, k, use_weights): + [input_graph.edgelist.edgelist_df['src'], + input_graph.edgelist.edgelist_df['dst']] = graph_primtypes_wrapper.datatype_cast([input_graph.edgelist.edgelist_df['src'], + input_graph.edgelist.edgelist_df['dst']], + [np.int32]) if graph_primtypes_wrapper.weight_type(input_graph) == np.float64 and use_weights: return ktruss_subgraph_double(input_graph, k, use_weights) else: diff --git a/python/cugraph/community/leiden.pxd b/python/cugraph/community/leiden.pxd index 80e0e12f65a..871dc826c06 100644 --- a/python/cugraph/community/leiden.pxd +++ b/python/cugraph/community/leiden.pxd @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -21,7 +21,7 @@ from libcpp.utility cimport pair from cugraph.structure.graph_primtypes cimport * -cdef extern from "algorithms.hpp" namespace "cugraph": +cdef extern from "cugraph/algorithms.hpp" namespace "cugraph": cdef pair[size_t, weight_t] leiden[vertex_t,edge_t,weight_t]( const handle_t &handle, diff --git a/python/cugraph/community/leiden.py b/python/cugraph/community/leiden.py index 8c1b79b8b63..641cf552192 100644 --- a/python/cugraph/community/leiden.py +++ b/python/cugraph/community/leiden.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019 - 2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -12,7 +12,7 @@ # limitations under the License. from cugraph.community import leiden_wrapper -from cugraph.structure.graph import Graph +from cugraph.structure.graph_classes import Graph from cugraph.utilities import check_nx_graph from cugraph.utilities import df_score_to_dictionary diff --git a/python/cugraph/community/leiden_wrapper.pyx b/python/cugraph/community/leiden_wrapper.pyx index 70fcfcf701b..1b41134c625 100644 --- a/python/cugraph/community/leiden_wrapper.pyx +++ b/python/cugraph/community/leiden_wrapper.pyx @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -22,7 +22,6 @@ from cugraph.structure import graph_primtypes_wrapper from libc.stdint cimport uintptr_t import cudf -import rmm import numpy as np diff --git a/python/cugraph/community/louvain.pxd b/python/cugraph/community/louvain.pxd index eca15ba3d20..08625047285 100644 --- a/python/cugraph/community/louvain.pxd +++ b/python/cugraph/community/louvain.pxd @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -18,10 +18,10 @@ from libcpp.utility cimport pair -from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * -cdef extern from "utilities/cython.hpp" namespace "cugraph::cython": +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": cdef pair[size_t, weight_t] call_louvain[weight_t]( const handle_t &handle, diff --git a/python/cugraph/community/louvain.py b/python/cugraph/community/louvain.py index d4d56a1100c..a761e060038 100644 --- a/python/cugraph/community/louvain.py +++ b/python/cugraph/community/louvain.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -12,7 +12,7 @@ # limitations under the License. from cugraph.community import louvain_wrapper -from cugraph.structure.graph import Graph +from cugraph.structure.graph_classes import Graph from cugraph.utilities import check_nx_graph from cugraph.utilities import df_score_to_dictionary diff --git a/python/cugraph/community/louvain_wrapper.pyx b/python/cugraph/community/louvain_wrapper.pyx index 6b218a0b962..c7ce4e8db66 100644 --- a/python/cugraph/community/louvain_wrapper.pyx +++ b/python/cugraph/community/louvain_wrapper.pyx @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -17,12 +17,11 @@ # cython: language_level = 3 from cugraph.community cimport louvain as c_louvain -from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * from cugraph.structure import graph_primtypes_wrapper from libc.stdint cimport uintptr_t import cudf -import rmm import numpy as np diff --git a/python/cugraph/community/spectral_clustering.pxd b/python/cugraph/community/spectral_clustering.pxd index 27ce6130b05..346eb50a157 100644 --- a/python/cugraph/community/spectral_clustering.pxd +++ b/python/cugraph/community/spectral_clustering.pxd @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -19,7 +19,7 @@ from cugraph.structure.graph_primtypes cimport * -cdef extern from "algorithms.hpp" namespace "cugraph::ext_raft": +cdef extern from "cugraph/algorithms.hpp" namespace "cugraph::ext_raft": cdef void balancedCutClustering[VT,ET,WT]( const GraphCSRView[VT,ET,WT] &graph, diff --git a/python/cugraph/community/spectral_clustering.py b/python/cugraph/community/spectral_clustering.py index b5f175e8237..06294af00c9 100644 --- a/python/cugraph/community/spectral_clustering.py +++ b/python/cugraph/community/spectral_clustering.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -32,23 +32,23 @@ def spectralBalancedCutClustering( Parameters ---------- G : cugraph.Graph or networkx.Graph - cuGraph graph descriptor + graph descriptor num_clusters : integer - Specifies the number of clusters to find + Specifies the number of clusters to find, must be greater than 1 num_eigen_vects : integer Specifies the number of eigenvectors to use. Must be lower or equal to - num_clusters. + num_clusters. Default is 2 evs_tolerance: float - Specifies the tolerance to use in the eigensolver + Specifies the tolerance to use in the eigensolver. Default is 0.00001 evs_max_iter: integer - Specifies the maximum number of iterations for the eigensolver + Specifies the maximum number of iterations for the eigensolver. Default is 100 kmean_tolerance: float - Specifies the tolerance to use in the k-means solver + Specifies the tolerance to use in the k-means solver. Default is 0.00001 kmean_max_iter: integer - Specifies the maximum number of iterations for the k-means solver + Specifies the maximum number of iterations for the k-means solver. Default is 100 Returns @@ -73,6 +73,8 @@ def spectralBalancedCutClustering( >>> df = cugraph.spectralBalancedCutClustering(G, 5) """ + # Error checking in C++ code + G, isNx = check_nx_graph(G) df = spectral_clustering_wrapper.spectralBalancedCutClustering( @@ -109,24 +111,24 @@ def spectralModularityMaximizationClustering( Parameters ---------- - G : cugraph.Graph + G : cugraph.Graph or networkx.Graph cuGraph graph descriptor. This graph should have edge weights. num_clusters : integer Specifies the number of clusters to find num_eigen_vects : integer Specifies the number of eigenvectors to use. Must be lower or equal to - num_clusters + num_clusters. Default is 2 evs_tolerance: float - Specifies the tolerance to use in the eigensolver + Specifies the tolerance to use in the eigensolver. Default is 0.00001 evs_max_iter: integer - Specifies the maximum number of iterations for the eigensolver + Specifies the maximum number of iterations for the eigensolver. Default is 100 kmean_tolerance: float - Specifies the tolerance to use in the k-means solver + Specifies the tolerance to use in the k-means solver. Default is 0.00001 kmean_max_iter: integer - Specifies the maximum number of iterations for the k-means solver + Specifies the maximum number of iterations for the k-means solver. Default is 100 Returns @@ -148,6 +150,8 @@ def spectralModularityMaximizationClustering( >>> df = cugraph.spectralModularityMaximizationClustering(G, 5) """ + # Error checking in C++ code + G, isNx = check_nx_graph(G) df = spectral_clustering_wrapper.spectralModularityMaximizationClustering( @@ -173,18 +177,21 @@ def analyzeClustering_modularity(G, n_clusters, clustering, vertex_col_name='vertex', cluster_col_name='cluster'): """ - Compute the modularity score for a partitioning/clustering + Compute the modularity score for a given partitioning/clustering. + The assumption is that β€œclustering” is the results from a call + from a special clustering algorithm and contains columns named + β€œvertex” and β€œcluster”. Parameters ---------- - G : cugraph.Graph - cuGraph graph descriptor. This graph should have edge weights. + G : cugraph.Graph or networkx.Graph + graph descriptor. This graph should have edge weights. n_clusters : integer Specifies the number of clusters in the given clustering clustering : cudf.DataFrame The cluster assignment to analyze. - vertex_col_name : str - The name of the column in the clustering dataframe identifying + vertex_col_name : str or list of str + The names of the column in the clustering dataframe identifying the external vertex id cluster_col_name : str The name of the column in the clustering dataframe identifying @@ -204,17 +211,26 @@ def analyzeClustering_modularity(G, n_clusters, clustering, >>> G = cugraph.Graph() >>> G.from_cudf_edgelist(M, source='0', destination='1', edge_attr='2') >>> df = cugraph.spectralBalancedCutClustering(G, 5) - >>> score = cugraph.analyzeClustering_modularity(G, 5, df, - >>> 'vertex', 'cluster') + >>> score = cugraph.analyzeClustering_modularity(G, 5, df) """ + if type(vertex_col_name) is list: + if not all(isinstance(name, str) for name in vertex_col_name): + raise Exception("vertex_col_name must be list of string") + elif type(vertex_col_name) is not str: + raise Exception("vertex_col_name must be a string") + + if type(cluster_col_name) is not str: + raise Exception("cluster_col_name must be a string") + + G, isNx = check_nx_graph(G) if G.renumbered: clustering = G.add_internal_vertex_id(clustering, - vertex_col_name, + 'vertex', vertex_col_name, drop=True) - clustering = clustering.sort_values(vertex_col_name) + clustering = clustering.sort_values('vertex') score = spectral_clustering_wrapper.analyzeClustering_modularity( G, n_clusters, clustering[cluster_col_name] @@ -228,6 +244,9 @@ def analyzeClustering_edge_cut(G, n_clusters, clustering, cluster_col_name='cluster'): """ Compute the edge cut score for a partitioning/clustering + The assumption is that β€œclustering” is the results from a call + from a special clustering algorithm and contains columns named + β€œvertex” and β€œcluster”. Parameters ---------- @@ -258,19 +277,26 @@ def analyzeClustering_edge_cut(G, n_clusters, clustering, >>> G = cugraph.Graph() >>> G.from_cudf_edgelist(M, source='0', destination='1', edge_attr=None) >>> df = cugraph.spectralBalancedCutClustering(G, 5) - >>> score = cugraph.analyzeClustering_edge_cut(G, 5, df, - >>> 'vertex', 'cluster') + >>> score = cugraph.analyzeClustering_edge_cut(G, 5, df) """ + if type(vertex_col_name) is list: + if not all(isinstance(name, str) for name in vertex_col_name): + raise Exception("vertex_col_name must be list of string") + elif type(vertex_col_name) is not str: + raise Exception("vertex_col_name must be a string") + + if type(cluster_col_name) is not str: + raise Exception("cluster_col_name must be a string") G, isNx = check_nx_graph(G) if G.renumbered: clustering = G.add_internal_vertex_id(clustering, - vertex_col_name, + 'vertex', vertex_col_name, drop=True) - clustering = clustering.sort_values(vertex_col_name).reset_index(drop=True) + clustering = clustering.sort_values('vertex').reset_index(drop=True) score = spectral_clustering_wrapper.analyzeClustering_edge_cut( G, n_clusters, clustering[cluster_col_name] @@ -317,14 +343,22 @@ def analyzeClustering_ratio_cut(G, n_clusters, clustering, >>> score = cugraph.analyzeClustering_ratio_cut(G, 5, df, >>> 'vertex', 'cluster') """ + if type(vertex_col_name) is list: + if not all(isinstance(name, str) for name in vertex_col_name): + raise Exception("vertex_col_name must be list of string") + elif type(vertex_col_name) is not str: + raise Exception("vertex_col_name must be a string") + + if type(cluster_col_name) is not str: + raise Exception("cluster_col_name must be a string") if G.renumbered: clustering = G.add_internal_vertex_id(clustering, - vertex_col_name, + 'vertex', vertex_col_name, drop=True) - clustering = clustering.sort_values(vertex_col_name) + clustering = clustering.sort_values('vertex') score = spectral_clustering_wrapper.analyzeClustering_ratio_cut( G, n_clusters, clustering[cluster_col_name] diff --git a/python/cugraph/community/spectral_clustering_wrapper.pyx b/python/cugraph/community/spectral_clustering_wrapper.pyx index 0593d987c0d..7934a386bb7 100644 --- a/python/cugraph/community/spectral_clustering_wrapper.pyx +++ b/python/cugraph/community/spectral_clustering_wrapper.pyx @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -23,12 +23,9 @@ from cugraph.community.spectral_clustering cimport analyzeClustering_edge_cut as from cugraph.community.spectral_clustering cimport analyzeClustering_ratio_cut as c_analyze_clustering_ratio_cut from cugraph.structure.graph_primtypes cimport * from cugraph.structure import graph_primtypes_wrapper -from libcpp cimport bool from libc.stdint cimport uintptr_t - import cugraph import cudf -import rmm import numpy as np diff --git a/python/cugraph/community/subgraph_extraction.pxd b/python/cugraph/community/subgraph_extraction.pxd index 97a71056006..583e220327d 100644 --- a/python/cugraph/community/subgraph_extraction.pxd +++ b/python/cugraph/community/subgraph_extraction.pxd @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -20,7 +20,7 @@ from cugraph.structure.graph_primtypes cimport * from libcpp.memory cimport unique_ptr -cdef extern from "algorithms.hpp" namespace "cugraph::subgraph": +cdef extern from "cugraph/algorithms.hpp" namespace "cugraph::subgraph": cdef unique_ptr[GraphCOO[VT,ET,WT]] extract_subgraph_vertex[VT,ET,WT]( const GraphCOOView[VT,ET,WT] &graph, diff --git a/python/cugraph/community/subgraph_extraction.py b/python/cugraph/community/subgraph_extraction.py index 8c702c2f58f..2df6e037d71 100644 --- a/python/cugraph/community/subgraph_extraction.py +++ b/python/cugraph/community/subgraph_extraction.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -12,8 +12,8 @@ # limitations under the License. from cugraph.community import subgraph_extraction_wrapper -from cugraph.structure.graph import null_check from cugraph.utilities import check_nx_graph +import cudf from cugraph.utilities import cugraph_to_nx @@ -28,8 +28,9 @@ def subgraph(G, vertices): ---------- G : cugraph.Graph cuGraph graph descriptor - vertices : cudf.Series - Specifies the vertices of the induced subgraph + vertices : cudf.Series or cudf.DataFrame + Specifies the vertices of the induced subgraph. For multi-column + vertices, vertices should be provided as a cudf.DataFrame Returns ------- @@ -52,27 +53,30 @@ def subgraph(G, vertices): >>> Sg = cugraph.subgraph(G, sverts) """ - null_check(vertices) - G, isNx = check_nx_graph(G) if G.renumbered: - vertices = G.lookup_internal_vertex_id(vertices) + if isinstance(vertices, cudf.DataFrame): + vertices = G.lookup_internal_vertex_id(vertices, vertices.columns) + else: + vertices = G.lookup_internal_vertex_id(vertices) result_graph = type(G)() df = subgraph_extraction_wrapper.subgraph(G, vertices) if G.renumbered: - df = G.unrenumber(df, "src") - df = G.unrenumber(df, "dst") + df, src_names = G.unrenumber(df, "src", get_column_names=True) + df, dst_names = G.unrenumber(df, "dst", get_column_names=True) if G.edgelist.weights: result_graph.from_cudf_edgelist( - df, source="src", destination="dst", edge_attr="weight" + df, source=src_names, destination=dst_names, + edge_attr="weight" ) else: - result_graph.from_cudf_edgelist(df, source="src", destination="dst") + result_graph.from_cudf_edgelist(df, source=src_names, + destination=dst_names) if isNx is True: result_graph = cugraph_to_nx(result_graph) diff --git a/python/cugraph/community/subgraph_extraction_wrapper.pyx b/python/cugraph/community/subgraph_extraction_wrapper.pyx index 5dbb6ce1e27..46dc5c07eaf 100644 --- a/python/cugraph/community/subgraph_extraction_wrapper.pyx +++ b/python/cugraph/community/subgraph_extraction_wrapper.pyx @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -20,9 +20,7 @@ from cugraph.community.subgraph_extraction cimport extract_subgraph_vertex as c_ from cugraph.structure.graph_primtypes cimport * from cugraph.structure import graph_primtypes_wrapper from libc.stdint cimport uintptr_t - import cudf -import rmm import numpy as np @@ -45,6 +43,10 @@ def subgraph(input_graph, vertices): if weights.dtype == np.float64: use_float = False + num_verts = input_graph.number_of_vertices() + num_edges = len(src) + num_input_vertices = len(vertices) + cdef GraphCOOView[int,int,float] in_graph_float cdef GraphCOOView[int,int,double] in_graph_double cdef unique_ptr[GraphCOO[int,int,float]] out_graph_float @@ -57,12 +59,9 @@ def subgraph(input_graph, vertices): if weights is not None: c_weights = weights.__cuda_array_interface__['data'][0] + [vertices] = graph_primtypes_wrapper.datatype_cast([vertices], [np.int32]) cdef uintptr_t c_vertices = vertices.__cuda_array_interface__['data'][0] - num_verts = input_graph.number_of_vertices() - num_edges = len(src) - num_input_vertices = len(vertices) - if use_float: in_graph_float = GraphCOOView[int,int,float](c_src, c_dst, c_weights, num_verts, num_edges); df = coo_to_df(move(c_extract_subgraph_vertex(in_graph_float, c_vertices, num_input_vertices))); diff --git a/python/cugraph/community/triangle_count.pxd b/python/cugraph/community/triangle_count.pxd index 70795a3f43a..55e8114ccbf 100644 --- a/python/cugraph/community/triangle_count.pxd +++ b/python/cugraph/community/triangle_count.pxd @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -20,7 +20,7 @@ from cugraph.structure.graph_primtypes cimport * from libc.stdint cimport uint64_t -cdef extern from "algorithms.hpp" namespace "cugraph::triangle": +cdef extern from "cugraph/algorithms.hpp" namespace "cugraph::triangle": cdef uint64_t triangle_count[VT,ET,WT]( const GraphCSRView[VT,ET,WT] &graph) except + diff --git a/python/cugraph/community/triangle_count.py b/python/cugraph/community/triangle_count.py index ff4dc9a5c5f..d28424a513e 100644 --- a/python/cugraph/community/triangle_count.py +++ b/python/cugraph/community/triangle_count.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -12,7 +12,7 @@ # limitations under the License. from cugraph.community import triangle_count_wrapper -from cugraph.structure.graph import Graph +from cugraph.structure.graph_classes import Graph from cugraph.utilities import check_nx_graph diff --git a/python/cugraph/community/triangle_count_wrapper.pyx b/python/cugraph/community/triangle_count_wrapper.pyx index d7cabd4676f..f1e842f9de4 100644 --- a/python/cugraph/community/triangle_count_wrapper.pyx +++ b/python/cugraph/community/triangle_count_wrapper.pyx @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -21,9 +21,7 @@ from cugraph.structure.graph_primtypes cimport * from cugraph.structure import graph_primtypes_wrapper from libc.stdint cimport uintptr_t import numpy as np - import cudf -import rmm def triangles(input_graph): diff --git a/python/cugraph/components/connectivity.pxd b/python/cugraph/components/connectivity.pxd index 94fa165969d..678836216b9 100644 --- a/python/cugraph/components/connectivity.pxd +++ b/python/cugraph/components/connectivity.pxd @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -17,9 +17,10 @@ # cython: language_level = 3 from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * -cdef extern from "algorithms.hpp" namespace "cugraph": +cdef extern from "cugraph/algorithms.hpp" namespace "cugraph": ctypedef enum cugraph_cc_t: CUGRAPH_WEAK "cugraph::cugraph_cc_t::CUGRAPH_WEAK" @@ -30,3 +31,9 @@ cdef extern from "algorithms.hpp" namespace "cugraph": const GraphCSRView[VT,ET,WT] &graph, cugraph_cc_t connect_type, VT *labels) except + + +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": + cdef void call_wcc[vertex_t, weight_t]( + const handle_t &handle, + const graph_container_t &g, + vertex_t *identifiers) except + diff --git a/python/cugraph/components/connectivity.py b/python/cugraph/components/connectivity.py index 7c68afd7ced..94eea312fb9 100644 --- a/python/cugraph/components/connectivity.py +++ b/python/cugraph/components/connectivity.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -138,8 +138,10 @@ def weakly_connected_components(G, directed : bool, optional - NOTE: For non-Graph-type (eg. sparse matrix) values of G only. Raises - TypeError if used with a Graph object. + NOTE + For non-Graph-type (eg. sparse matrix) values of G only. + Raises TypeError if used with a Graph object. + If True (default), then convert the input matrix to a cugraph.DiGraph and only move from point i to point j along paths csgraph[i, j]. If False, then find the shortest path on an undirected graph: the @@ -154,8 +156,10 @@ def weakly_connected_components(G, return_labels : bool, optional - NOTE: For non-Graph-type (eg. sparse matrix) values of G only. Raises - TypeError if used with a Graph object. + NOTE + For non-Graph-type (eg. sparse matrix) values of G only. Raises + TypeError if used with a Graph object. + If True (default), then return the labels for each of the connected components. @@ -231,8 +235,10 @@ def strongly_connected_components(G, directed : bool, optional - NOTE: For non-Graph-type (eg. sparse matrix) values of G only. Raises - TypeError if used with a Graph object. + NOTE + For non-Graph-type (eg. sparse matrix) values of G only. + Raises TypeError if used with a Graph object. + If True (default), then convert the input matrix to a cugraph.DiGraph and only move from point i to point j along paths csgraph[i, j]. If False, then find the shortest path on an undirected graph: the @@ -247,8 +253,10 @@ def strongly_connected_components(G, return_labels : bool, optional - NOTE: For non-Graph-type (eg. sparse matrix) values of G only. Raises - TypeError if used with a Graph object. + NOTE + For non-Graph-type (eg. sparse matrix) values of G only. Raises + TypeError if used with a Graph object. + If True (default), then return the labels for each of the connected components. @@ -325,8 +333,10 @@ def connected_components(G, directed : bool, optional - NOTE: For non-Graph-type (eg. sparse matrix) values of G only. Raises - TypeError if used with a Graph object. + NOTE + For non-Graph-type (eg. sparse matrix) values of G only. Raises + TypeError if used with a Graph object. + If True (default), then convert the input matrix to a cugraph.DiGraph and only move from point i to point j along paths csgraph[i, j]. If False, then find the shortest path on an undirected graph: the @@ -340,8 +350,10 @@ def connected_components(G, return_labels : bool, optional - NOTE: For non-Graph-type (eg. sparse matrix) values of G only. Raises - TypeError if used with a Graph object. + NOTE + For non-Graph-type (eg. sparse matrix) values of G only. Raises + TypeError if used with a Graph object. + If True (default), then return the labels for each of the connected components. @@ -378,7 +390,7 @@ def connected_components(G, header=None) >>> G = cugraph.Graph() >>> G.from_cudf_edgelist(M, source='0', destination='1', edge_attr=None) - >>> df = cugraph.strongly_connected_components(G) + >>> df = cugraph.connected_components(G, connection="weak") """ if connection == "weak": return weakly_connected_components(G, directed, diff --git a/python/cugraph/components/connectivity_wrapper.pyx b/python/cugraph/components/connectivity_wrapper.pyx index 8b678d16ff8..28227bd1c07 100644 --- a/python/cugraph/components/connectivity_wrapper.pyx +++ b/python/cugraph/components/connectivity_wrapper.pyx @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -18,12 +18,12 @@ from cugraph.components.connectivity cimport * from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * from cugraph.structure import utils_wrapper from cugraph.structure import graph_primtypes_wrapper from libc.stdint cimport uintptr_t from cugraph.structure.symmetrize import symmetrize -from cugraph.structure.graph import Graph as type_Graph - +from cugraph.structure.graph_classes import Graph as type_Graph import cudf import numpy as np @@ -31,47 +31,60 @@ def weakly_connected_components(input_graph): """ Call connected_components """ - offsets = None - indices = None + cdef unique_ptr[handle_t] handle_ptr + handle_ptr.reset(new handle_t()) + handle_ = handle_ptr.get() + + numberTypeMap = {np.dtype("int32") : numberTypeEnum.int32Type, + np.dtype("int64") : numberTypeEnum.int64Type, + np.dtype("float32") : numberTypeEnum.floatType, + np.dtype("double") : numberTypeEnum.doubleType} + + [src, dst] = graph_primtypes_wrapper.datatype_cast([input_graph.edgelist.edgelist_df['src'], + input_graph.edgelist.edgelist_df['dst']], + [np.int32]) if type(input_graph) is not type_Graph: # - # Need to create a symmetrized CSR for this local - # computation, don't want to keep it. - # - [src, dst] = graph_primtypes_wrapper.datatype_cast([input_graph.edgelist.edgelist_df['src'], - input_graph.edgelist.edgelist_df['dst']], - [np.int32]) - src, dst = symmetrize(src, dst) - [offsets, indices] = utils_wrapper.coo2csr(src, dst)[0:2] - else: - if not input_graph.adjlist: - input_graph.view_adj_list() - - [offsets, indices] = graph_primtypes_wrapper.datatype_cast([input_graph.adjlist.offsets, - input_graph.adjlist.indices], - [np.int32]) + # Need to create a symmetrized COO for this local + # computation + src, dst = symmetrize(src, dst) + weight_t = np.dtype("float32") + weights = None + num_verts = input_graph.number_of_vertices() num_edges = input_graph.number_of_edges(directed_edges=True) df = cudf.DataFrame() - df['vertex'] = cudf.Series(np.zeros(num_verts, dtype=np.int32)) + df['vertex'] = cudf.Series(np.arange(num_verts, dtype=np.int32)) df['labels'] = cudf.Series(np.zeros(num_verts, dtype=np.int32)) - cdef uintptr_t c_offsets = offsets.__cuda_array_interface__['data'][0] - cdef uintptr_t c_indices = indices.__cuda_array_interface__['data'][0] - cdef uintptr_t c_identifier = df['vertex'].__cuda_array_interface__['data'][0]; + cdef uintptr_t c_src_vertices = src.__cuda_array_interface__['data'][0] + cdef uintptr_t c_dst_vertices = dst.__cuda_array_interface__['data'][0] + cdef uintptr_t c_edge_weights = NULL cdef uintptr_t c_labels_val = df['labels'].__cuda_array_interface__['data'][0]; - cdef GraphCSRView[int,int,float] g - - g = GraphCSRView[int,int,float](c_offsets, c_indices, NULL, num_verts, num_edges) - - cdef cugraph_cc_t connect_type=CUGRAPH_WEAK - connected_components(g, connect_type, c_labels_val) - - g.get_vertex_identifiers(c_identifier) + cdef graph_container_t graph_container + populate_graph_container(graph_container, + handle_[0], + c_src_vertices, c_dst_vertices, c_edge_weights, + NULL, + NULL, + 0, + ((numberTypeEnum.int32Type)), + ((numberTypeEnum.int32Type)), + ((numberTypeMap[weight_t])), + num_edges, + num_verts, num_edges, + False, + True, + False, + False) + + call_wcc[int, float](handle_ptr.get()[0], + graph_container, + c_labels_val) return df diff --git a/python/cugraph/cores/core_number.pxd b/python/cugraph/cores/core_number.pxd index cf28720a3e8..17dc1118a5e 100644 --- a/python/cugraph/cores/core_number.pxd +++ b/python/cugraph/cores/core_number.pxd @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -18,7 +18,7 @@ from cugraph.structure.graph_primtypes cimport * -cdef extern from "algorithms.hpp" namespace "cugraph": +cdef extern from "cugraph/algorithms.hpp" namespace "cugraph": cdef void core_number[VT,ET,WT]( const GraphCSRView[VT,ET,WT] &graph, diff --git a/python/cugraph/cores/core_number_wrapper.pyx b/python/cugraph/cores/core_number_wrapper.pyx index 3df1df5f8e9..9fcc3b4746c 100644 --- a/python/cugraph/cores/core_number_wrapper.pyx +++ b/python/cugraph/cores/core_number_wrapper.pyx @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -20,9 +20,7 @@ cimport cugraph.cores.core_number as c_core from cugraph.structure.graph_primtypes cimport * from cugraph.structure import graph_primtypes_wrapper from libc.stdint cimport uintptr_t - import cudf -import rmm import numpy as np diff --git a/python/cugraph/cores/k_core.pxd b/python/cugraph/cores/k_core.pxd index 556dbc95ed9..1d22e7ac4d2 100644 --- a/python/cugraph/cores/k_core.pxd +++ b/python/cugraph/cores/k_core.pxd @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -18,7 +18,7 @@ from cugraph.structure.graph_primtypes cimport * -cdef extern from "algorithms.hpp" namespace "cugraph": +cdef extern from "cugraph/algorithms.hpp" namespace "cugraph": cdef unique_ptr[GraphCOO[VT,ET,WT]] k_core[VT,ET,WT]( const GraphCOOView[VT,ET,WT] &in_graph, diff --git a/python/cugraph/cores/k_core.py b/python/cugraph/cores/k_core.py index ce67665764b..17a3baf9c4c 100644 --- a/python/cugraph/cores/k_core.py +++ b/python/cugraph/cores/k_core.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -14,7 +14,7 @@ from cugraph.cores import k_core_wrapper, core_number_wrapper from cugraph.utilities import cugraph_to_nx from cugraph.utilities import check_nx_graph -from cugraph.structure.graph import Graph +from cugraph.structure.graph_classes import Graph def k_core(G, k=None, core_number=None): @@ -69,31 +69,38 @@ def k_core(G, k=None, core_number=None): if core_number is not None: if G.renumbered is True: - core_number = G.add_internal_vertex_id( - core_number, "vertex", "vertex", drop=True - ) + if len(G.renumber_map.implementation.col_names) > 1: + cols = core_number.columns[:-1].to_list() + else: + cols = 'vertex' + core_number = G.add_internal_vertex_id(core_number, 'vertex', + cols) + else: core_number = core_number_wrapper.core_number(G) core_number = core_number.rename( columns={"core_number": "values"}, copy=False ) - print(core_number) + if k is None: k = core_number["values"].max() k_core_df = k_core_wrapper.k_core(G, k, core_number) if G.renumbered: - k_core_df = G.unrenumber(k_core_df, "src") - k_core_df = G.unrenumber(k_core_df, "dst") + k_core_df, src_names = G.unrenumber(k_core_df, "src", + get_column_names=True) + k_core_df, dst_names = G.unrenumber(k_core_df, "dst", + get_column_names=True) if G.edgelist.weights: KCoreGraph.from_cudf_edgelist( - k_core_df, source="src", destination="dst", edge_attr="weight" + k_core_df, source=src_names, destination=dst_names, + edge_attr="weight" ) else: KCoreGraph.from_cudf_edgelist( - k_core_df, source="src", destination="dst" + k_core_df, source=src_names, destination=dst_names, ) if isNx is True: diff --git a/python/cugraph/cores/k_core_wrapper.pyx b/python/cugraph/cores/k_core_wrapper.pyx index 51ecec09dc5..28bb191f4f4 100644 --- a/python/cugraph/cores/k_core_wrapper.pyx +++ b/python/cugraph/cores/k_core_wrapper.pyx @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -19,12 +19,7 @@ from cugraph.cores.k_core cimport k_core as c_k_core from cugraph.structure.graph_primtypes cimport * from cugraph.structure import graph_primtypes_wrapper -from libcpp cimport bool from libc.stdint cimport uintptr_t -from libc.float cimport FLT_MAX_EXP - -import cudf -import rmm import numpy as np @@ -54,6 +49,10 @@ def k_core(input_graph, k, core_number): """ Call k_core """ + [input_graph.edgelist.edgelist_df['src'], + input_graph.edgelist.edgelist_df['dst']] = graph_primtypes_wrapper.datatype_cast([input_graph.edgelist.edgelist_df['src'], + input_graph.edgelist.edgelist_df['dst']], + [np.int32]) if graph_primtypes_wrapper.weight_type(input_graph) == np.float64: return k_core_double(input_graph, k, core_number) else: diff --git a/python/cugraph/dask/__init__.py b/python/cugraph/dask/__init__.py index 830de45c50b..60aebaf19b0 100644 --- a/python/cugraph/dask/__init__.py +++ b/python/cugraph/dask/__init__.py @@ -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. # You may obtain a copy of the License at @@ -17,3 +17,4 @@ from .common.read_utils import get_chunksize from .community.louvain import louvain from .centrality.katz_centrality import katz_centrality +from .components.connectivity import weakly_connected_components diff --git a/python/cugraph/dask/centrality/katz_centrality.py b/python/cugraph/dask/centrality/katz_centrality.py index cf6ad95f974..61c1869f974 100644 --- a/python/cugraph/dask/centrality/katz_centrality.py +++ b/python/cugraph/dask/centrality/katz_centrality.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. @@ -14,8 +14,8 @@ # from dask.distributed import wait, default_client -from cugraph.dask.common.input_utils import get_distributed_data -from cugraph.structure.shuffle import shuffle +from cugraph.dask.common.input_utils import (get_distributed_data, + get_vertex_partition_offsets) from cugraph.dask.centrality import\ mg_katz_centrality_wrapper as mg_katz_centrality import cugraph.comms.comms as Comms @@ -27,6 +27,7 @@ def call_katz_centrality(sID, num_verts, num_edges, vertex_partition_offsets, + aggregate_segment_offsets, alpha, beta, max_iter, @@ -35,12 +36,16 @@ def call_katz_centrality(sID, normalized): wid = Comms.get_worker_id(sID) handle = Comms.get_handle(sID) + local_size = len(aggregate_segment_offsets) // Comms.get_n_workers(sID) + segment_offsets = \ + aggregate_segment_offsets[local_size * wid: local_size * (wid + 1)] return mg_katz_centrality.mg_katz_centrality(data[0], num_verts, num_edges, vertex_partition_offsets, wid, handle, + segment_offsets, alpha, beta, max_iter, @@ -68,14 +73,16 @@ def katz_centrality(input_graph, Attenuation factor defaulted to None. If alpha is not specified then it is internally calculated as 1/(degree_max) where degree_max is the maximum out degree. - NOTE : The maximum acceptable value of alpha for convergence - alpha_max = 1/(lambda_max) where lambda_max is the largest eigenvalue - of the graph. - Since lambda_max is always lesser than or equal to degree_max for a - graph, alpha_max will always be greater than or equal to - (1/degree_max). Therefore, setting alpha to (1/degree_max) will - guarantee that it will never exceed alpha_max thus in turn fulfilling - the requirement for convergence. + + NOTE + The maximum acceptable value of alpha for convergence + alpha_max = 1/(lambda_max) where lambda_max is the largest + eigenvalue of the graph. + Since lambda_max is always lesser than or equal to degree_max for a + graph, alpha_max will always be greater than or equal to + (1/degree_max). Therefore, setting alpha to (1/degree_max) will + guarantee that it will never exceed alpha_max thus in turn + fulfilling the requirement for convergence. beta : None A weight scalar - currently Not Supported max_iter : int @@ -94,6 +101,7 @@ def katz_centrality(input_graph, acceptable. nstart : dask_cudf.Dataframe GPU Dataframe containing the initial guess for katz centrality + nstart['vertex'] : dask_cudf.Series Contains the vertex identifiers nstart['values'] : dask_cudf.Series @@ -115,7 +123,8 @@ def katz_centrality(input_graph, Examples -------- >>> import cugraph.dask as dcg - >>> Comms.initialize(p2p=True) + >>> ... Init a DASK Cluster + >> see https://docs.rapids.ai/api/cugraph/stable/dask-cugraph.html >>> chunksize = dcg.get_chunksize(input_data_path) >>> ddf = dask_cudf.read_csv(input_data_path, chunksize=chunksize, delimiter=' ', @@ -125,7 +134,6 @@ def katz_centrality(input_graph, >>> dg.from_dask_cudf_edgelist(ddf, source='src', destination='dst', edge_attr='value') >>> pr = dcg.katz_centrality(dg) - >>> Comms.destroy() """ nstart = None @@ -133,11 +141,9 @@ def katz_centrality(input_graph, client = default_client() input_graph.compute_renumber_edge_list(transposed=True) - (ddf, - num_verts, - partition_row_size, - partition_col_size, - vertex_partition_offsets) = shuffle(input_graph, transposed=True) + ddf = input_graph.edgelist.edgelist_df + vertex_partition_offsets = get_vertex_partition_offsets(input_graph) + num_verts = vertex_partition_offsets.iloc[-1] num_edges = len(ddf) data = get_distributed_data(ddf) @@ -147,6 +153,7 @@ def katz_centrality(input_graph, num_verts, num_edges, vertex_partition_offsets, + input_graph.aggregate_segment_offsets, alpha, beta, max_iter, diff --git a/python/cugraph/dask/centrality/mg_katz_centrality.pxd b/python/cugraph/dask/centrality/mg_katz_centrality.pxd index 345457b1963..5e30530e92b 100644 --- a/python/cugraph/dask/centrality/mg_katz_centrality.pxd +++ b/python/cugraph/dask/centrality/mg_katz_centrality.pxd @@ -1,5 +1,5 @@ # -# 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. @@ -14,11 +14,11 @@ # limitations under the License. # -from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * from libcpp cimport bool -cdef extern from "utilities/cython.hpp" namespace "cugraph::cython": +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": cdef void call_katz_centrality[vertex_t, weight_t]( const handle_t &handle, diff --git a/python/cugraph/dask/centrality/mg_katz_centrality_wrapper.pyx b/python/cugraph/dask/centrality/mg_katz_centrality_wrapper.pyx index b8cab4e4286..6160d13507f 100644 --- a/python/cugraph/dask/centrality/mg_katz_centrality_wrapper.pyx +++ b/python/cugraph/dask/centrality/mg_katz_centrality_wrapper.pyx @@ -1,5 +1,5 @@ # -# 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. @@ -17,7 +17,7 @@ from cugraph.structure.utils_wrapper import * from cugraph.dask.centrality cimport mg_katz_centrality as c_katz_centrality import cudf -from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * import cugraph.structure.graph_primtypes_wrapper as graph_primtypes_wrapper from libc.stdint cimport uintptr_t from cython.operator cimport dereference as deref @@ -30,6 +30,7 @@ def mg_katz_centrality(input_df, vertex_partition_offsets, rank, handle, + segment_offsets, alpha=None, beta=None, max_iter=100, @@ -48,12 +49,16 @@ def mg_katz_centrality(input_df, if num_global_edges > (2**31 - 1): edge_t = np.dtype("int64") else: - edge_t = np.dtype("int32") + edge_t = vertex_t if "value" in input_df.columns: weights = input_df['value'] weight_t = weights.dtype + is_weighted = True + raise NotImplementedError # FIXME: c_edge_weights is always set to NULL else: + weights = None weight_t = np.dtype("float32") + is_weighted = False if alpha is None: alpha = 0.1 @@ -67,27 +72,43 @@ def mg_katz_centrality(input_df, np.dtype("double") : numberTypeEnum.doubleType} # FIXME: needs to be edge_t type not int - cdef int num_partition_edges = len(src) + cdef int num_local_edges = len(src) cdef uintptr_t c_src_vertices = src.__cuda_array_interface__['data'][0] cdef uintptr_t c_dst_vertices = dst.__cuda_array_interface__['data'][0] cdef uintptr_t c_edge_weights = NULL + if weights is not None: + c_edge_weights = weights.__cuda_array_interface__['data'][0] # FIXME: data is on device, move to host (to_pandas()), convert to np array and access pointer to pass to C vertex_partition_offsets_host = vertex_partition_offsets.values_host cdef uintptr_t c_vertex_partition_offsets = vertex_partition_offsets_host.__array_interface__['data'][0] + cdef vector[int] v_segment_offsets_32 + cdef vector[long] v_segment_offsets_64 + cdef uintptr_t c_segment_offsets + if (vertex_t == np.dtype("int32")): + v_segment_offsets_32 = segment_offsets + c_segment_offsets = v_segment_offsets_32.data() + else: + v_segment_offsets_64 = segment_offsets + c_segment_offsets = v_segment_offsets_64.data() + cdef graph_container_t graph_container + populate_graph_container(graph_container, handle_[0], c_src_vertices, c_dst_vertices, c_edge_weights, c_vertex_partition_offsets, + c_segment_offsets, + len(segment_offsets) - 1, ((numberTypeMap[vertex_t])), ((numberTypeMap[edge_t])), ((numberTypeMap[weight_t])), - num_partition_edges, + num_local_edges, num_global_verts, num_global_edges, - True, + is_weighted, + False, True, True) df = cudf.DataFrame() @@ -97,11 +118,18 @@ def mg_katz_centrality(input_df, cdef uintptr_t c_identifier = df['vertex'].__cuda_array_interface__['data'][0] cdef uintptr_t c_katz_centralities = df['katz_centrality'].__cuda_array_interface__['data'][0] - if (df['katz_centrality'].dtype == np.float32): - c_katz_centrality.call_katz_centrality[int, float](handle_[0], graph_container, c_identifier, c_katz_centralities, - alpha, beta, tol, max_iter, 0, normalize) + if vertex_t == np.int32: + if (df['katz_centrality'].dtype == np.float32): + c_katz_centrality.call_katz_centrality[int, float](handle_[0], graph_container, c_identifier, c_katz_centralities, + alpha, beta, tol, max_iter, 0, normalize) + else: + c_katz_centrality.call_katz_centrality[int, double](handle_[0], graph_container, c_identifier, c_katz_centralities, + alpha, beta, tol, max_iter, 0, normalize) else: - c_katz_centrality.call_katz_centrality[int, double](handle_[0], graph_container, c_identifier, c_katz_centralities, - alpha, beta, tol, max_iter, 0, normalize) - + if (df['katz_centrality'].dtype == np.float32): + c_katz_centrality.call_katz_centrality[long, float](handle_[0], graph_container, c_identifier, c_katz_centralities, + alpha, beta, tol, max_iter, 0, normalize) + else: + c_katz_centrality.call_katz_centrality[long, double](handle_[0], graph_container, c_identifier, c_katz_centralities, + alpha, beta, tol, max_iter, 0, normalize) return df diff --git a/python/cugraph/dask/common/input_utils.py b/python/cugraph/dask/common/input_utils.py index 0140c9f06f9..9d1c28b6c4e 100644 --- a/python/cugraph/dask/common/input_utils.py +++ b/python/cugraph/dask/common/input_utils.py @@ -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. @@ -21,8 +21,7 @@ import cugraph.comms.comms as Comms from cugraph.raft.dask.common.utils import get_client -from cugraph.dask.common.part_utils import (_extract_partitions, - load_balance_func) +from cugraph.dask.common.part_utils import _extract_partitions from dask.distributed import default_client from toolz import first from functools import reduce @@ -67,7 +66,7 @@ def get_client(cls, client=None): """ Class methods for initalization """ @classmethod - def create(cls, data, client=None): + def create(cls, data, client=None, batch_enabled=False): """ Creates a distributed data handler instance with the given distributed data set(s). @@ -91,7 +90,8 @@ def create(cls, data, client=None): else: raise Exception("Graph data must be dask-cudf dataframe") - gpu_futures = client.sync(_extract_partitions, data, client) + gpu_futures = client.sync( + _extract_partitions, data, client, batch_enabled=batch_enabled) workers = tuple(OrderedDict.fromkeys(map(lambda x: x[0], gpu_futures))) return DistributedDataHandler(gpu_futures=gpu_futures, workers=workers, datatype=datatype, multiple=multiple, @@ -174,6 +174,14 @@ def calculate_local_data(self, comms, by): self.max_vertex_id = max_vid +def _get_local_data(df, by): + df = df[0] + num_local_edges = len(df) + local_by_max = df[by].iloc[-1] + local_max = df[['src', 'dst']].max().max() + return num_local_edges, local_by_max, local_max + + """ Internal methods, API subject to change """ @@ -184,11 +192,14 @@ def _workers_to_parts(futures): :param futures: list of (worker, part) tuples :return: """ - w_to_p_map = OrderedDict() + w_to_p_map = OrderedDict.fromkeys(Comms.get_workers()) for w, p in futures: - if w not in w_to_p_map: + if w_to_p_map[w] is None: w_to_p_map[w] = [] w_to_p_map[w].append(p) + keys_to_delete = [w for (w, p) in w_to_p_map.items() if p is None] + for k in keys_to_delete: + del w_to_p_map[k] return w_to_p_map @@ -198,30 +209,9 @@ def get_obj(x): return x[0] if multiple else x return total, reduce(lambda a, b: a + b, total) -def _get_local_data(df, by): - df = df[0] - num_local_edges = len(df) - local_by_max = df[by].iloc[-1] - local_max = df[['src', 'dst']].max().max() - return num_local_edges, local_by_max, local_max - - -def get_local_data(input_graph, by, load_balance=True): - input_graph.compute_renumber_edge_list(transposed=(by == 'dst')) - _ddf = input_graph.edgelist.edgelist_df - ddf = _ddf.sort_values(by=by, ignore_index=True) - - if load_balance: - ddf = load_balance_func(ddf, by=by) - - comms = Comms.get_comms() - data = DistributedDataHandler.create(data=ddf) - data.calculate_local_data(comms, by) - return data - - -def get_mg_batch_data(dask_cudf_data): - data = DistributedDataHandler.create(data=dask_cudf_data) +def get_mg_batch_data(dask_cudf_data, batch_enabled=False): + data = DistributedDataHandler.create( + data=dask_cudf_data, batch_enabled=batch_enabled) return data @@ -232,3 +222,15 @@ def get_distributed_data(input_ddf): if data.worker_info is None and comms is not None: data.calculate_worker_and_rank_info(comms) return data + + +def get_vertex_partition_offsets(input_graph): + import cudf + renumber_vertex_count = input_graph.renumber_map.implementation.ddf.\ + map_partitions(len).compute() + renumber_vertex_cumsum = renumber_vertex_count.cumsum() + vertex_dtype = input_graph.edgelist.edgelist_df['src'].dtype + vertex_partition_offsets = cudf.Series([0], dtype=vertex_dtype) + vertex_partition_offsets = vertex_partition_offsets.append(cudf.Series( + renumber_vertex_cumsum, dtype=vertex_dtype)) + return vertex_partition_offsets diff --git a/python/cugraph/dask/common/mg_utils.py b/python/cugraph/dask/common/mg_utils.py index 7556afb122a..1651a9e800c 100644 --- a/python/cugraph/dask/common/mg_utils.py +++ b/python/cugraph/dask/common/mg_utils.py @@ -10,9 +10,21 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from cugraph.raft.dask.common.utils import default_client + +import os + import numba.cuda +from dask_cuda import LocalCUDACluster +from dask.distributed import Client + +from cugraph.raft.dask.common.utils import default_client +# FIXME: cugraph/__init__.py also imports the comms module, but +# depending on the import environment, cugraph/comms/__init__.py +# may be imported instead. The following imports the comms.py +# module directly +from cugraph.comms import comms as Comms + # FIXME: We currently look for the default client from dask, as such is the # if there is a dask client running without any GPU we will still try @@ -41,3 +53,36 @@ def is_single_gpu(): return False else: return True + + +def get_visible_devices(): + _visible_devices = os.environ.get("CUDA_VISIBLE_DEVICES") + if _visible_devices is None: + # FIXME: We assume that if the variable is unset there is only one GPU + visible_devices = ["0"] + else: + visible_devices = _visible_devices.strip().split(",") + return visible_devices + + +def setup_local_dask_cluster(p2p=True): + """ + Performs steps to setup a Dask cluster using LocalCUDACluster and returns + the LocalCUDACluster and corresponding client instance. + """ + cluster = LocalCUDACluster() + client = Client(cluster) + client.wait_for_workers(len(get_visible_devices())) + Comms.initialize(p2p=p2p) + + return (cluster, client) + + +def teardown_local_dask_cluster(cluster, client): + """ + Performs steps to destroy a Dask cluster and a corresponding client + instance. + """ + Comms.destroy() + client.close() + cluster.close() diff --git a/python/cugraph/dask/common/part_utils.py b/python/cugraph/dask/common/part_utils.py index 505272fa563..2bff490d35c 100644 --- a/python/cugraph/dask/common/part_utils.py +++ b/python/cugraph/dask/common/part_utils.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. @@ -78,12 +78,18 @@ def persist_distributed_data(dask_df, client): return parts -async def _extract_partitions(dask_obj, client=None): - +async def _extract_partitions(dask_obj, client=None, batch_enabled=False): client = default_client() if client is None else client + worker_list = Comms.get_workers() # dask.dataframe or dask.array if isinstance(dask_obj, (daskDataFrame, daskArray, daskSeries)): - parts = persist_distributed_data(dask_obj, client) + # parts = persist_distributed_data(dask_obj, client) + # FIXME: persist data to the same worker when batch_enabled=True + if batch_enabled: + persisted = client.persist(dask_obj, workers=worker_list[0]) + else: + persisted = client.persist(dask_obj) + parts = futures_of(persisted) # iterable of dask collections (need to colocate them) elif isinstance(dask_obj, collections.Sequence): # NOTE: We colocate (X, y) here by zipping delayed diff --git a/python/cugraph/dask/community/louvain.pxd b/python/cugraph/dask/community/louvain.pxd index b6b4cd23143..ab990330028 100644 --- a/python/cugraph/dask/community/louvain.pxd +++ b/python/cugraph/dask/community/louvain.pxd @@ -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. # You may obtain a copy of the License at @@ -17,10 +17,10 @@ # cython: language_level = 3 from libcpp.utility cimport pair -from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * -cdef extern from "utilities/cython.hpp" namespace "cugraph::cython": +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": cdef pair[size_t, weight_t] call_louvain[weight_t]( const handle_t &handle, diff --git a/python/cugraph/dask/community/louvain.py b/python/cugraph/dask/community/louvain.py index 11ecb78375f..c4db00ab27a 100644 --- a/python/cugraph/dask/community/louvain.py +++ b/python/cugraph/dask/community/louvain.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -16,8 +16,8 @@ from dask.distributed import wait, default_client import cugraph.comms.comms as Comms -from cugraph.dask.common.input_utils import get_distributed_data -from cugraph.structure.shuffle import shuffle +from cugraph.dask.common.input_utils import (get_distributed_data, + get_vertex_partition_offsets) from cugraph.dask.community import louvain_wrapper as c_mg_louvain from cugraph.utilities.utils import is_cuda_version_less_than @@ -29,20 +29,21 @@ def call_louvain(sID, num_verts, num_edges, vertex_partition_offsets, - sorted_by_degree, + aggregate_segment_offsets, max_level, resolution): - wid = Comms.get_worker_id(sID) handle = Comms.get_handle(sID) - + local_size = len(aggregate_segment_offsets) // Comms.get_n_workers(sID) + segment_offsets = \ + aggregate_segment_offsets[local_size * wid: local_size * (wid + 1)] return c_mg_louvain.louvain(data[0], num_verts, num_edges, vertex_partition_offsets, wid, handle, - sorted_by_degree, + segment_offsets, max_level, resolution) @@ -55,7 +56,8 @@ def louvain(input_graph, max_iter=100, resolution=1.0): Examples -------- >>> import cugraph.dask as dcg - >>> Comms.initialize(p2p=True) + >>> ... Init a DASK Cluster + >> see https://docs.rapids.ai/api/cugraph/stable/dask-cugraph.html >>> chunksize = dcg.get_chunksize(input_data_path) >>> ddf = dask_cudf.read_csv('datasets/karate.csv', chunksize=chunksize, delimiter=' ', @@ -83,14 +85,10 @@ def louvain(input_graph, max_iter=100, resolution=1.0): client = default_client() # Calling renumbering results in data that is sorted by degree input_graph.compute_renumber_edge_list(transposed=False) - sorted_by_degree = True - - (ddf, - num_verts, - partition_row_size, - partition_col_size, - vertex_partition_offsets) = shuffle(input_graph, transposed=False) + ddf = input_graph.edgelist.edgelist_df + vertex_partition_offsets = get_vertex_partition_offsets(input_graph) + num_verts = vertex_partition_offsets.iloc[-1] num_edges = len(ddf) data = get_distributed_data(ddf) @@ -100,7 +98,7 @@ def louvain(input_graph, max_iter=100, resolution=1.0): num_verts, num_edges, vertex_partition_offsets, - sorted_by_degree, + input_graph.aggregate_segment_offsets, max_iter, resolution, workers=[wf[0]]) diff --git a/python/cugraph/dask/community/louvain_wrapper.pyx b/python/cugraph/dask/community/louvain_wrapper.pyx index c2a12cf81f3..c3c3f1ad373 100644 --- a/python/cugraph/dask/community/louvain_wrapper.pyx +++ b/python/cugraph/dask/community/louvain_wrapper.pyx @@ -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. # You may obtain a copy of the License at @@ -17,10 +17,10 @@ # cython: language_level = 3 from libc.stdint cimport uintptr_t +from libcpp.vector cimport vector from cugraph.dask.community cimport louvain as c_louvain -from cugraph.structure.graph_primtypes cimport * - +from cugraph.structure.graph_utilities cimport * import cudf import numpy as np @@ -38,7 +38,7 @@ def louvain(input_df, vertex_partition_offsets, rank, handle, - sorted_by_degree, + segment_offsets, max_level, resolution): """ @@ -57,18 +57,18 @@ def louvain(input_df, src = input_df['src'] dst = input_df['dst'] - num_partition_edges = len(src) + num_local_edges = len(src) if "value" in input_df.columns: weights = input_df['value'] else: - weights = cudf.Series(np.full(num_partition_edges, 1.0, dtype=np.float32)) + weights = cudf.Series(np.full(num_local_edges, 1.0, dtype=np.float32)) vertex_t = src.dtype if num_global_edges > (2**31 - 1): edge_t = np.dtype("int64") else: - edge_t = np.dtype("int32") + edge_t = vertex_t weight_t = weights.dtype # COO @@ -83,6 +83,16 @@ def louvain(input_df, num_local_verts = vertex_partition_offsets_host[rank+1] - vertex_partition_offsets_host[rank] + cdef vector[int] v_segment_offsets_32 + cdef vector[long] v_segment_offsets_64 + cdef uintptr_t c_segment_offsets + if (vertex_t == np.dtype("int32")): + v_segment_offsets_32 = segment_offsets + c_segment_offsets = v_segment_offsets_32.data() + else: + v_segment_offsets_64 = segment_offsets + c_segment_offsets = v_segment_offsets_64.data() + cdef graph_container_t graph_container # FIXME: The excessive casting for the enum arg is needed to make cython @@ -92,12 +102,15 @@ def louvain(input_df, handle_[0], c_src_vertices, c_dst_vertices, c_edge_weights, c_vertex_partition_offsets, + c_segment_offsets, + len(segment_offsets) - 1, ((numberTypeMap[vertex_t])), ((numberTypeMap[edge_t])), ((numberTypeMap[weight_t])), - num_partition_edges, + num_local_edges, num_global_verts, num_global_edges, - sorted_by_degree, + True, + False, False, True) # store_transposed, multi_gpu # Create the output dataframe, column lengths must be equal to the number of diff --git a/python/cugraph/dask/components/__init__.py b/python/cugraph/dask/components/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/python/cugraph/dask/components/connectivity.py b/python/cugraph/dask/components/connectivity.py new file mode 100644 index 00000000000..81200e7383e --- /dev/null +++ b/python/cugraph/dask/components/connectivity.py @@ -0,0 +1,69 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from dask.distributed import wait, default_client +from cugraph.dask.common.input_utils import (get_distributed_data, + get_vertex_partition_offsets) +from cugraph.dask.components import mg_connectivity_wrapper as mg_connectivity +import cugraph.comms.comms as Comms +import dask_cudf + + +def call_wcc(sID, + data, + num_verts, + num_edges, + vertex_partition_offsets, + aggregate_segment_offsets): + wid = Comms.get_worker_id(sID) + handle = Comms.get_handle(sID) + local_size = len(aggregate_segment_offsets) // Comms.get_n_workers(sID) + segment_offsets = \ + aggregate_segment_offsets[local_size * wid: local_size * (wid + 1)] + return mg_connectivity.mg_wcc(data[0], + num_verts, + num_edges, + vertex_partition_offsets, + wid, + handle, + segment_offsets) + + +def weakly_connected_components(input_graph): + + client = default_client() + + input_graph.compute_renumber_edge_list() + + ddf = input_graph.edgelist.edgelist_df + vertex_partition_offsets = get_vertex_partition_offsets(input_graph) + num_verts = vertex_partition_offsets.iloc[-1] + num_edges = len(ddf) + data = get_distributed_data(ddf) + + result = [client.submit(call_wcc, + Comms.get_session_id(), + wf[1], + num_verts, + num_edges, + vertex_partition_offsets, + input_graph.aggregate_segment_offsets, + workers=[wf[0]]) + for idx, wf in enumerate(data.worker_to_parts.items())] + wait(result) + ddf = dask_cudf.from_delayed(result) + + if input_graph.renumbered: + return input_graph.unrenumber(ddf, 'vertex') + + return ddf diff --git a/python/cugraph/dask/components/mg_connectivity.pxd b/python/cugraph/dask/components/mg_connectivity.pxd new file mode 100644 index 00000000000..04f04a9665e --- /dev/null +++ b/python/cugraph/dask/components/mg_connectivity.pxd @@ -0,0 +1,26 @@ +# +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from cugraph.structure.graph_utilities cimport * +from libcpp cimport bool + + +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": + + cdef void call_wcc[vertex_t, weight_t]( + const handle_t &handle, + const graph_container_t &g, + vertex_t * components) diff --git a/python/cugraph/dask/components/mg_connectivity_wrapper.pyx b/python/cugraph/dask/components/mg_connectivity_wrapper.pyx new file mode 100644 index 00000000000..b7bad1b6277 --- /dev/null +++ b/python/cugraph/dask/components/mg_connectivity_wrapper.pyx @@ -0,0 +1,111 @@ +# +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from cugraph.structure.utils_wrapper import * +from cugraph.dask.components cimport mg_connectivity as c_connectivity +import cudf +from cugraph.structure.graph_utilities cimport * +import cugraph.structure.graph_primtypes_wrapper as graph_primtypes_wrapper +from libc.stdint cimport uintptr_t +from cython.operator cimport dereference as deref +import numpy as np + + +def mg_wcc(input_df, + num_global_verts, + num_global_edges, + vertex_partition_offsets, + rank, + handle, + segment_offsets): + + cdef size_t handle_size_t = handle.getHandle() + handle_ = handle_size_t + + src = input_df['src'] + dst = input_df['dst'] + vertex_t = src.dtype + if num_global_edges > (2**31 - 1): + edge_t = np.dtype("int64") + else: + edge_t = vertex_t + + weights = None + weight_t = np.dtype("float32") + is_weighted = False + + # FIXME: Offsets and indices are currently hardcoded to int, but this may + # not be acceptable in the future. + numberTypeMap = {np.dtype("int32") : numberTypeEnum.int32Type, + np.dtype("int64") : numberTypeEnum.int64Type, + np.dtype("float32") : numberTypeEnum.floatType, + np.dtype("double") : numberTypeEnum.doubleType} + + # FIXME: needs to be edge_t type not int + cdef int num_local_edges = len(src) + + cdef uintptr_t c_src_vertices = src.__cuda_array_interface__['data'][0] + cdef uintptr_t c_dst_vertices = dst.__cuda_array_interface__['data'][0] + cdef uintptr_t c_edge_weights = NULL + + # FIXME: data is on device, move to host (to_pandas()), convert to np array and access pointer to pass to C + vertex_partition_offsets_host = vertex_partition_offsets.values_host + cdef uintptr_t c_vertex_partition_offsets = vertex_partition_offsets_host.__array_interface__['data'][0] + + cdef vector[int] v_segment_offsets_32 + cdef vector[long] v_segment_offsets_64 + cdef uintptr_t c_segment_offsets + if (vertex_t == np.dtype("int32")): + v_segment_offsets_32 = segment_offsets + c_segment_offsets = v_segment_offsets_32.data() + else: + v_segment_offsets_64 = segment_offsets + c_segment_offsets = v_segment_offsets_64.data() + + cdef graph_container_t graph_container + + populate_graph_container(graph_container, + handle_[0], + c_src_vertices, c_dst_vertices, c_edge_weights, + c_vertex_partition_offsets, + c_segment_offsets, + len(segment_offsets) - 1, + ((numberTypeMap[vertex_t])), + ((numberTypeMap[edge_t])), + ((numberTypeMap[weight_t])), + num_local_edges, + num_global_verts, num_global_edges, + is_weighted, + True, + False, + True) + + df = cudf.DataFrame() + df['vertex'] = cudf.Series(np.arange(vertex_partition_offsets.iloc[rank], vertex_partition_offsets.iloc[rank+1]), dtype=vertex_t) + df['labels'] = cudf.Series(np.zeros(len(df['vertex']), dtype=vertex_t)) + + cdef uintptr_t c_labels_val = df['labels'].__cuda_array_interface__['data'][0]; + + if vertex_t == np.int32: + c_connectivity.call_wcc[int, float](handle_[0], + graph_container, + c_labels_val) + else: + c_connectivity.call_wcc[long, float](handle_[0], + graph_container, + c_labels_val) + + return df diff --git a/python/cugraph/dask/link_analysis/mg_pagerank.pxd b/python/cugraph/dask/link_analysis/mg_pagerank.pxd index 91104d9127c..4b47f43dd87 100644 --- a/python/cugraph/dask/link_analysis/mg_pagerank.pxd +++ b/python/cugraph/dask/link_analysis/mg_pagerank.pxd @@ -1,5 +1,5 @@ # -# 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. @@ -14,11 +14,11 @@ # limitations under the License. # -from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * from libcpp cimport bool -cdef extern from "utilities/cython.hpp" namespace "cugraph::cython": +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": cdef void call_pagerank[vertex_t, weight_t]( const handle_t &handle, @@ -31,4 +31,4 @@ cdef extern from "utilities/cython.hpp" namespace "cugraph::cython": double alpha, double tolerance, long long max_iter, - bool has_guess) except + \ No newline at end of file + bool has_guess) except + diff --git a/python/cugraph/dask/link_analysis/mg_pagerank_wrapper.pyx b/python/cugraph/dask/link_analysis/mg_pagerank_wrapper.pyx index 1cd80397b17..43ef3d7e31f 100644 --- a/python/cugraph/dask/link_analysis/mg_pagerank_wrapper.pyx +++ b/python/cugraph/dask/link_analysis/mg_pagerank_wrapper.pyx @@ -1,5 +1,5 @@ # -# 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. @@ -17,7 +17,7 @@ from cugraph.structure.utils_wrapper import * from cugraph.dask.link_analysis cimport mg_pagerank as c_pagerank import cudf -from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * import cugraph.structure.graph_primtypes_wrapper as graph_primtypes_wrapper from libc.stdint cimport uintptr_t from cython.operator cimport dereference as deref @@ -30,6 +30,7 @@ def mg_pagerank(input_df, vertex_partition_offsets, rank, handle, + segment_offsets, alpha=0.85, max_iter=100, tol=1.0e-5, @@ -47,12 +48,16 @@ def mg_pagerank(input_df, if num_global_edges > (2**31 - 1): edge_t = np.dtype("int64") else: - edge_t = np.dtype("int32") + edge_t = vertex_t if "value" in input_df.columns: weights = input_df['value'] weight_t = weights.dtype + is_weighted = True + raise NotImplementedError # FIXME: c_edge_weights is always set to NULL else: + weights = None weight_t = np.dtype("float32") + is_weighted = False # FIXME: Offsets and indices are currently hardcoded to int, but this may # not be acceptable in the future. @@ -62,28 +67,43 @@ def mg_pagerank(input_df, np.dtype("double") : numberTypeEnum.doubleType} # FIXME: needs to be edge_t type not int - cdef int num_partition_edges = len(src) + cdef int num_local_edges = len(src) cdef uintptr_t c_src_vertices = src.__cuda_array_interface__['data'][0] cdef uintptr_t c_dst_vertices = dst.__cuda_array_interface__['data'][0] cdef uintptr_t c_edge_weights = NULL + if weights is not None: + c_edge_weights = weights.__cuda_array_interface__['data'][0] # FIXME: data is on device, move to host (to_pandas()), convert to np array and access pointer to pass to C vertex_partition_offsets_host = vertex_partition_offsets.values_host cdef uintptr_t c_vertex_partition_offsets = vertex_partition_offsets_host.__array_interface__['data'][0] + cdef vector[int] v_segment_offsets_32 + cdef vector[long] v_segment_offsets_64 + cdef uintptr_t c_segment_offsets + if (vertex_t == np.dtype("int32")): + v_segment_offsets_32 = segment_offsets + c_segment_offsets = v_segment_offsets_32.data() + else: + v_segment_offsets_64 = segment_offsets + c_segment_offsets = v_segment_offsets_64.data() + cdef graph_container_t graph_container populate_graph_container(graph_container, handle_[0], c_src_vertices, c_dst_vertices, c_edge_weights, c_vertex_partition_offsets, + c_segment_offsets, + len(segment_offsets) - 1, ((numberTypeMap[vertex_t])), ((numberTypeMap[edge_t])), ((numberTypeMap[weight_t])), - num_partition_edges, + num_local_edges, num_global_verts, num_global_edges, - True, + is_weighted, + False, True, True) df = cudf.DataFrame() @@ -104,11 +124,19 @@ def mg_pagerank(input_df, c_pers_vtx = personalization['vertex'].__cuda_array_interface__['data'][0] c_pers_val = personalization['values'].__cuda_array_interface__['data'][0] - if (df['pagerank'].dtype == np.float32): - c_pagerank.call_pagerank[int, float](handle_[0], graph_container, c_identifier, c_pagerank_val, sz, c_pers_vtx, c_pers_val, - alpha, tol, max_iter, 0) + if vertex_t == np.int32: + if (df['pagerank'].dtype == np.float32): + c_pagerank.call_pagerank[int, float](handle_[0], graph_container, c_identifier, c_pagerank_val, sz, c_pers_vtx, c_pers_val, + alpha, tol, max_iter, 0) + else: + c_pagerank.call_pagerank[int, double](handle_[0], graph_container, c_identifier, c_pagerank_val, sz, c_pers_vtx, c_pers_val, + alpha, tol, max_iter, 0) else: - c_pagerank.call_pagerank[int, double](handle_[0], graph_container, c_identifier, c_pagerank_val, sz, c_pers_vtx, c_pers_val, - alpha, tol, max_iter, 0) - + if (df['pagerank'].dtype == np.float32): + c_pagerank.call_pagerank[long, float](handle_[0], graph_container, c_identifier, c_pagerank_val, sz, c_pers_vtx, c_pers_val, + alpha, tol, max_iter, 0) + else: + c_pagerank.call_pagerank[long, double](handle_[0], graph_container, c_identifier, c_pagerank_val, sz, c_pers_vtx, c_pers_val, + alpha, tol, max_iter, 0) + return df diff --git a/python/cugraph/dask/link_analysis/pagerank.py b/python/cugraph/dask/link_analysis/pagerank.py index 1e9d79e0aa6..87ef94d1600 100644 --- a/python/cugraph/dask/link_analysis/pagerank.py +++ b/python/cugraph/dask/link_analysis/pagerank.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. @@ -14,8 +14,8 @@ # from dask.distributed import wait, default_client -from cugraph.dask.common.input_utils import get_distributed_data -from cugraph.structure.shuffle import shuffle +from cugraph.dask.common.input_utils import (get_distributed_data, + get_vertex_partition_offsets) from cugraph.dask.link_analysis import mg_pagerank_wrapper as mg_pagerank import cugraph.comms.comms as Comms import dask_cudf @@ -26,6 +26,7 @@ def call_pagerank(sID, num_verts, num_edges, vertex_partition_offsets, + aggregate_segment_offsets, alpha, max_iter, tol, @@ -33,12 +34,16 @@ def call_pagerank(sID, nstart): wid = Comms.get_worker_id(sID) handle = Comms.get_handle(sID) + local_size = len(aggregate_segment_offsets) // Comms.get_n_workers(sID) + segment_offsets = \ + aggregate_segment_offsets[local_size * wid: local_size * (wid + 1)] return mg_pagerank.mg_pagerank(data[0], num_verts, num_edges, vertex_partition_offsets, wid, handle, + segment_offsets, alpha, max_iter, tol, @@ -73,6 +78,7 @@ def pagerank(input_graph, personalization : cudf.Dataframe GPU Dataframe containing the personalization information. Currently not supported. + personalization['vertex'] : cudf.Series Subset of vertices of graph for personalization personalization['values'] : cudf.Series @@ -91,6 +97,7 @@ def pagerank(input_graph, acceptable. nstart : not supported initial guess for pagerank + Returns ------- PageRank : dask_cudf.DataFrame @@ -105,7 +112,8 @@ def pagerank(input_graph, Examples -------- >>> import cugraph.dask as dcg - >>> Comms.initialize(p2p=True) + >>> ... Init a DASK Cluster + >> see https://docs.rapids.ai/api/cugraph/stable/dask-cugraph.html >>> chunksize = dcg.get_chunksize(input_data_path) >>> ddf = dask_cudf.read_csv(input_data_path, chunksize=chunksize, delimiter=' ', @@ -115,20 +123,18 @@ def pagerank(input_graph, >>> dg.from_dask_cudf_edgelist(ddf, source='src', destination='dst', edge_attr='value') >>> pr = dcg.pagerank(dg) - >>> Comms.destroy() """ - from cugraph.structure.graph import null_check + from cugraph.structure.graph_classes import null_check nstart = None client = default_client() input_graph.compute_renumber_edge_list(transposed=True) - (ddf, - num_verts, - partition_row_size, - partition_col_size, - vertex_partition_offsets) = shuffle(input_graph, transposed=True) + + ddf = input_graph.edgelist.edgelist_df + vertex_partition_offsets = get_vertex_partition_offsets(input_graph) + num_verts = vertex_partition_offsets.iloc[-1] num_edges = len(ddf) data = get_distributed_data(ddf) @@ -147,6 +153,7 @@ def pagerank(input_graph, num_verts, num_edges, vertex_partition_offsets, + input_graph.aggregate_segment_offsets, alpha, max_iter, tol, @@ -161,6 +168,7 @@ def pagerank(input_graph, num_verts, num_edges, vertex_partition_offsets, + input_graph.aggregate_segment_offsets, alpha, max_iter, tol, diff --git a/python/cugraph/dask/structure/replication.pyx b/python/cugraph/dask/structure/replication.pyx index 6d579e126bf..417300f806f 100644 --- a/python/cugraph/dask/structure/replication.pyx +++ b/python/cugraph/dask/structure/replication.pyx @@ -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. # You may obtain a copy of the License at @@ -38,7 +38,7 @@ def replicate_cudf_dataframe(cudf_dataframe, client=None, comms=None): dask_cudf_df = dask_cudf.from_cudf(cudf_dataframe, npartitions=1) df_length = len(dask_cudf_df) - _df_data = get_mg_batch_data(dask_cudf_df) + _df_data = get_mg_batch_data(dask_cudf_df, batch_enabled=True) df_data = mg_utils.prepare_worker_to_parts(_df_data, client) workers_to_futures = {worker: client.submit(_replicate_cudf_dataframe, @@ -90,7 +90,7 @@ def replicate_cudf_series(cudf_series, client=None, comms=None): dask_cudf_series = dask_cudf.from_cudf(cudf_series, npartitions=1) series_length = len(dask_cudf_series) - _series_data = get_mg_batch_data(dask_cudf_series) + _series_data = get_mg_batch_data(dask_cudf_series, batch_enabled=True) series_data = mg_utils.prepare_worker_to_parts(_series_data) dtype = cudf_series.dtype diff --git a/python/cugraph/dask/traversal/bfs.py b/python/cugraph/dask/traversal/bfs.py index 7a2c50a3bc0..06cbf64782a 100644 --- a/python/cugraph/dask/traversal/bfs.py +++ b/python/cugraph/dask/traversal/bfs.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. @@ -14,8 +14,8 @@ # from dask.distributed import wait, default_client -from cugraph.dask.common.input_utils import get_distributed_data -from cugraph.structure.shuffle import shuffle +from cugraph.dask.common.input_utils import (get_distributed_data, + get_vertex_partition_offsets) from cugraph.dask.traversal import mg_bfs_wrapper as mg_bfs import cugraph.comms.comms as Comms import cudf @@ -27,23 +27,31 @@ def call_bfs(sID, num_verts, num_edges, vertex_partition_offsets, + aggregate_segment_offsets, start, + depth_limit, return_distances): wid = Comms.get_worker_id(sID) handle = Comms.get_handle(sID) + local_size = len(aggregate_segment_offsets) // Comms.get_n_workers(sID) + segment_offsets = \ + aggregate_segment_offsets[local_size * wid: local_size * (wid + 1)] return mg_bfs.mg_bfs(data[0], num_verts, num_edges, vertex_partition_offsets, wid, handle, + segment_offsets, start, + depth_limit, return_distances) def bfs(graph, start, - return_distances=False): + depth_limit=None, + return_distances=True): """ Find the distances and predecessors for a breadth first traversal of a graph. @@ -59,7 +67,9 @@ def bfs(graph, start : Integer Specify starting vertex for breadth-first search; this function iterates over edges in the component reachable from this node. - return_distances : bool, optional, default=False + depth_limit : Integer or None + Limit the depth of the search + return_distances : bool, optional, default=True Indicates if distances should be returned Returns @@ -76,7 +86,8 @@ def bfs(graph, Examples -------- >>> import cugraph.dask as dcg - >>> Comms.initialize(p2p=True) + >>> ... Init a DASK Cluster + >> see https://docs.rapids.ai/api/cugraph/stable/dask-cugraph.html >>> chunksize = dcg.get_chunksize(input_data_path) >>> ddf = dask_cudf.read_csv(input_data_path, chunksize=chunksize, delimiter=' ', @@ -85,24 +96,28 @@ def bfs(graph, >>> dg = cugraph.DiGraph() >>> dg.from_dask_cudf_edgelist(ddf, 'src', 'dst') >>> df = dcg.bfs(dg, 0) - >>> Comms.destroy() """ client = default_client() graph.compute_renumber_edge_list(transposed=False) - (ddf, - num_verts, - partition_row_size, - partition_col_size, - vertex_partition_offsets) = shuffle(graph, transposed=False) + ddf = graph.edgelist.edgelist_df + vertex_partition_offsets = get_vertex_partition_offsets(graph) + num_verts = vertex_partition_offsets.iloc[-1] + num_edges = len(ddf) data = get_distributed_data(ddf) if graph.renumbered: - start = graph.lookup_internal_vertex_id(cudf.Series([start], - dtype='int32')).compute() - start = start.iloc[0] + if isinstance(start, dask_cudf.DataFrame)\ + or isinstance(start, cudf.DataFrame): + start = graph.lookup_internal_vertex_id(start, start.columns).\ + compute() + start = start.iloc[0] + else: + start = graph.lookup_internal_vertex_id(cudf.Series([start]) + ).compute() + start = start.iloc[0] result = [client.submit( call_bfs, @@ -111,7 +126,9 @@ def bfs(graph, num_verts, num_edges, vertex_partition_offsets, + graph.aggregate_segment_offsets, start, + depth_limit, return_distances, workers=[wf[0]]) for idx, wf in enumerate(data.worker_to_parts.items())] @@ -121,5 +138,5 @@ def bfs(graph, if graph.renumbered: ddf = graph.unrenumber(ddf, 'vertex') ddf = graph.unrenumber(ddf, 'predecessor') - ddf["predecessor"] = ddf["predecessor"].fillna(-1) + ddf = ddf.fillna(-1) return ddf diff --git a/python/cugraph/dask/traversal/mg_bfs.pxd b/python/cugraph/dask/traversal/mg_bfs.pxd index 82c6e97d668..d4f399bf689 100644 --- a/python/cugraph/dask/traversal/mg_bfs.pxd +++ b/python/cugraph/dask/traversal/mg_bfs.pxd @@ -1,5 +1,5 @@ # -# 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. @@ -14,11 +14,14 @@ # limitations under the License. # -from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * from libcpp cimport bool +cdef extern from "limits.h": + cdef int INT_MAX + cdef long LONG_MAX -cdef extern from "utilities/cython.hpp" namespace "cugraph::cython": +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": cdef void call_bfs[vertex_t, weight_t]( const handle_t &handle, @@ -26,6 +29,6 @@ cdef extern from "utilities/cython.hpp" namespace "cugraph::cython": vertex_t *identifiers, vertex_t *distances, vertex_t *predecessors, - double *sp_counters, + vertex_t depth_limit, const vertex_t start_vertex, - bool directed) except + + bool direction_optimizing) except + diff --git a/python/cugraph/dask/traversal/mg_bfs_wrapper.pyx b/python/cugraph/dask/traversal/mg_bfs_wrapper.pyx index c92f28eb407..a15d6704ac8 100644 --- a/python/cugraph/dask/traversal/mg_bfs_wrapper.pyx +++ b/python/cugraph/dask/traversal/mg_bfs_wrapper.pyx @@ -1,5 +1,5 @@ # -# 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. @@ -17,7 +17,7 @@ from cugraph.structure.utils_wrapper import * from cugraph.dask.traversal cimport mg_bfs as c_bfs import cudf -from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * import cugraph.structure.graph_primtypes_wrapper as graph_primtypes_wrapper from libc.stdint cimport uintptr_t @@ -27,12 +27,13 @@ def mg_bfs(input_df, vertex_partition_offsets, rank, handle, + segment_offsets, start, + depth_limit, return_distances=False): """ - Call pagerank + Call BFS """ - cdef size_t handle_size_t = handle.getHandle() handle_ = handle_size_t @@ -43,13 +44,14 @@ def mg_bfs(input_df, if num_global_edges > (2**31 - 1): edge_t = np.dtype("int64") else: - edge_t = np.dtype("int32") + edge_t = vertex_t if "value" in input_df.columns: weights = input_df['value'] weight_t = weights.dtype else: weight_t = np.dtype("float32") + # FIXME: Offsets and indices are currently hardcoded to int, but this may # not be acceptable in the future. numberTypeMap = {np.dtype("int32") : numberTypeEnum.int32Type, @@ -58,7 +60,7 @@ def mg_bfs(input_df, np.dtype("double") : numberTypeEnum.doubleType} # FIXME: needs to be edge_t type not int - cdef int num_partition_edges = len(src) + cdef int num_local_edges = len(src) cdef uintptr_t c_src_vertices = src.__cuda_array_interface__['data'][0] cdef uintptr_t c_dst_vertices = dst.__cuda_array_interface__['data'][0] @@ -68,26 +70,39 @@ def mg_bfs(input_df, vertex_partition_offsets_host = vertex_partition_offsets.values_host cdef uintptr_t c_vertex_partition_offsets = vertex_partition_offsets_host.__array_interface__['data'][0] + cdef vector[int] v_segment_offsets_32 + cdef vector[long] v_segment_offsets_64 + cdef uintptr_t c_segment_offsets + if (vertex_t == np.dtype("int32")): + v_segment_offsets_32 = segment_offsets + c_segment_offsets = v_segment_offsets_32.data() + else: + v_segment_offsets_64 = segment_offsets + c_segment_offsets = v_segment_offsets_64.data() + cdef graph_container_t graph_container populate_graph_container(graph_container, handle_[0], c_src_vertices, c_dst_vertices, c_edge_weights, c_vertex_partition_offsets, + c_segment_offsets, + len(segment_offsets) - 1, ((numberTypeMap[vertex_t])), ((numberTypeMap[edge_t])), ((numberTypeMap[weight_t])), - num_partition_edges, + num_local_edges, num_global_verts, num_global_edges, - True, + False, # BFS runs on unweighted graphs + False, False, True) # Generate the cudf.DataFrame result df = cudf.DataFrame() df['vertex'] = cudf.Series(np.arange(vertex_partition_offsets.iloc[rank], vertex_partition_offsets.iloc[rank+1]), dtype=vertex_t) - df['predecessor'] = cudf.Series(np.zeros(len(df['vertex']), dtype=np.int32)) + df['predecessor'] = cudf.Series(np.zeros(len(df['vertex']), dtype=vertex_t)) if (return_distances): - df['distance'] = cudf.Series(np.zeros(len(df['vertex']), dtype=np.int32)) + df['distance'] = cudf.Series(np.zeros(len(df['vertex']), dtype=vertex_t)) # Associate to cudf Series cdef uintptr_t c_distance_ptr = NULL # Pointer to the DataFrame 'distance' Series @@ -95,14 +110,28 @@ def mg_bfs(input_df, if (return_distances): c_distance_ptr = df['distance'].__cuda_array_interface__['data'][0] - cdef bool direction = 1 - # MG BFS path assumes directed is true - c_bfs.call_bfs[int, float](handle_[0], - graph_container, - NULL, - c_distance_ptr, - c_predecessor_ptr, - NULL, - start, - direction) + cdef bool direction_optimizing = 0 + + if vertex_t == np.int32: + if depth_limit is None: + depth_limit = c_bfs.INT_MAX + c_bfs.call_bfs[int, float](handle_[0], + graph_container, + NULL, + c_distance_ptr, + c_predecessor_ptr, + depth_limit, + start, + direction_optimizing) + else: + if depth_limit is None: + depth_limit = c_bfs.LONG_MAX + c_bfs.call_bfs[long, float](handle_[0], + graph_container, + NULL, + c_distance_ptr, + c_predecessor_ptr, + depth_limit, + start, + direction_optimizing) return df diff --git a/python/cugraph/dask/traversal/mg_sssp.pxd b/python/cugraph/dask/traversal/mg_sssp.pxd index f846facd269..937b42147e6 100644 --- a/python/cugraph/dask/traversal/mg_sssp.pxd +++ b/python/cugraph/dask/traversal/mg_sssp.pxd @@ -1,5 +1,5 @@ # -# 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. @@ -13,11 +13,11 @@ # See the License for the specific language governing permissions and # limitations under the License. # -from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * from libcpp cimport bool -cdef extern from "utilities/cython.hpp" namespace "cugraph::cython": +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": cdef void call_sssp[vertex_t, weight_t]( const handle_t &handle, diff --git a/python/cugraph/dask/traversal/mg_sssp_wrapper.pyx b/python/cugraph/dask/traversal/mg_sssp_wrapper.pyx index b7aec103098..63ac2942cfa 100644 --- a/python/cugraph/dask/traversal/mg_sssp_wrapper.pyx +++ b/python/cugraph/dask/traversal/mg_sssp_wrapper.pyx @@ -1,5 +1,5 @@ # -# 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. @@ -17,7 +17,7 @@ from cugraph.structure.utils_wrapper import * from cugraph.dask.traversal cimport mg_sssp as c_sssp import cudf -from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * import cugraph.structure.graph_primtypes_wrapper as graph_primtypes_wrapper from libc.stdint cimport uintptr_t @@ -27,6 +27,7 @@ def mg_sssp(input_df, vertex_partition_offsets, rank, handle, + segment_offsets, start): """ Call sssp @@ -42,13 +43,15 @@ def mg_sssp(input_df, if num_global_edges > (2**31 - 1): edge_t = np.dtype("int64") else: - edge_t = np.dtype("int32") + edge_t = vertex_t if "value" in input_df.columns: weights = input_df['value'] weight_t = weights.dtype + is_weighted = True else: weights = None weight_t = np.dtype("float32") + is_weighted = False # FIXME: Offsets and indices are currently hardcoded to int, but this may # not be acceptable in the future. @@ -58,7 +61,7 @@ def mg_sssp(input_df, np.dtype("double") : numberTypeEnum.doubleType} # FIXME: needs to be edge_t type not int - cdef int num_partition_edges = len(src) + cdef int num_local_edges = len(src) cdef uintptr_t c_src_vertices = src.__cuda_array_interface__['data'][0] cdef uintptr_t c_dst_vertices = dst.__cuda_array_interface__['data'][0] @@ -70,18 +73,31 @@ def mg_sssp(input_df, vertex_partition_offsets_host = vertex_partition_offsets.values_host cdef uintptr_t c_vertex_partition_offsets = vertex_partition_offsets_host.__array_interface__['data'][0] + cdef vector[int] v_segment_offsets_32 + cdef vector[long] v_segment_offsets_64 + cdef uintptr_t c_segment_offsets + if (vertex_t == np.dtype("int32")): + v_segment_offsets_32 = segment_offsets + c_segment_offsets = v_segment_offsets_32.data() + else: + v_segment_offsets_64 = segment_offsets + c_segment_offsets = v_segment_offsets_64.data() + cdef graph_container_t graph_container populate_graph_container(graph_container, handle_[0], c_src_vertices, c_dst_vertices, c_edge_weights, c_vertex_partition_offsets, + c_segment_offsets, + len(segment_offsets) - 1, ((numberTypeMap[vertex_t])), ((numberTypeMap[edge_t])), ((numberTypeMap[weight_t])), - num_partition_edges, + num_local_edges, num_global_verts, num_global_edges, - True, + is_weighted, + False, False, True) # Generate the cudf.DataFrame result @@ -95,21 +111,34 @@ def mg_sssp(input_df, cdef uintptr_t c_distance_ptr = df['distance'].__cuda_array_interface__['data'][0] # MG BFS path assumes directed is true - if weight_t == np.float32: - c_sssp.call_sssp[int, float](handle_[0], - graph_container, - NULL, - c_distance_ptr, - c_predecessor_ptr, - start) - elif weight_t == np.float64: - c_sssp.call_sssp[int, double](handle_[0], - graph_container, - NULL, - c_distance_ptr, - c_predecessor_ptr, - start) - else: # This case should not happen - raise NotImplementedError - + if vertex_t == np.int32: + if weight_t == np.float32: + c_sssp.call_sssp[int, float](handle_[0], + graph_container, + NULL, + c_distance_ptr, + c_predecessor_ptr, + start) + elif weight_t == np.float64: + c_sssp.call_sssp[int, double](handle_[0], + graph_container, + NULL, + c_distance_ptr, + c_predecessor_ptr, + start) + else: + if weight_t == np.float32: + c_sssp.call_sssp[long, float](handle_[0], + graph_container, + NULL, + c_distance_ptr, + c_predecessor_ptr, + start) + elif weight_t == np.float64: + c_sssp.call_sssp[long, double](handle_[0], + graph_container, + NULL, + c_distance_ptr, + c_predecessor_ptr, + start) return df diff --git a/python/cugraph/dask/traversal/sssp.py b/python/cugraph/dask/traversal/sssp.py index ce0c7908664..fbaee901d65 100644 --- a/python/cugraph/dask/traversal/sssp.py +++ b/python/cugraph/dask/traversal/sssp.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. @@ -14,8 +14,8 @@ # from dask.distributed import wait, default_client -from cugraph.dask.common.input_utils import get_distributed_data -from cugraph.structure.shuffle import shuffle +from cugraph.dask.common.input_utils import (get_distributed_data, + get_vertex_partition_offsets) from cugraph.dask.traversal import mg_sssp_wrapper as mg_sssp import cugraph.comms.comms as Comms import cudf @@ -27,15 +27,20 @@ def call_sssp(sID, num_verts, num_edges, vertex_partition_offsets, + aggregate_segment_offsets, start): wid = Comms.get_worker_id(sID) handle = Comms.get_handle(sID) + local_size = len(aggregate_segment_offsets) // Comms.get_n_workers(sID) + segment_offsets = \ + aggregate_segment_offsets[local_size * wid: local_size * (wid + 1)] return mg_sssp.mg_sssp(data[0], num_verts, num_edges, vertex_partition_offsets, wid, handle, + segment_offsets, start) @@ -76,7 +81,8 @@ def sssp(graph, Examples -------- >>> import cugraph.dask as dcg - >>> Comms.initialize(p2p=True) + >>> ... Init a DASK Cluster + >> see https://docs.rapids.ai/api/cugraph/stable/dask-cugraph.html >>> chunksize = dcg.get_chunksize(input_data_path) >>> ddf = dask_cudf.read_csv(input_data_path, chunksize=chunksize, delimiter=' ', @@ -85,23 +91,20 @@ def sssp(graph, >>> dg = cugraph.DiGraph() >>> dg.from_dask_cudf_edgelist(ddf, 'src', 'dst') >>> df = dcg.sssp(dg, 0) - >>> Comms.destroy() """ client = default_client() graph.compute_renumber_edge_list(transposed=False) - (ddf, - num_verts, - partition_row_size, - partition_col_size, - vertex_partition_offsets) = shuffle(graph, transposed=False) + ddf = graph.edgelist.edgelist_df + vertex_partition_offsets = get_vertex_partition_offsets(graph) + num_verts = vertex_partition_offsets.iloc[-1] num_edges = len(ddf) data = get_distributed_data(ddf) if graph.renumbered: - source = graph.lookup_internal_vertex_id(cudf.Series([source], - dtype='int32')).compute() + source = graph.lookup_internal_vertex_id(cudf.Series([source]) + ).compute() source = source.iloc[0] result = [client.submit( @@ -111,6 +114,7 @@ def sssp(graph, num_verts, num_edges, vertex_partition_offsets, + graph.aggregate_segment_offsets, source, workers=[wf[0]]) for idx, wf in enumerate(data.worker_to_parts.items())] diff --git a/python/cugraph/bsp/__init__.py b/python/cugraph/generators/__init__.py similarity index 85% rename from python/cugraph/bsp/__init__.py rename to python/cugraph/generators/__init__.py index dbb94895cec..74ecc2384bd 100644 --- a/python/cugraph/bsp/__init__.py +++ b/python/cugraph/generators/__init__.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -11,5 +11,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -from cugraph.bsp.traversal import bfs_df_pregel - +from .rmat import rmat, multi_rmat diff --git a/python/cugraph/generators/rmat.pxd b/python/cugraph/generators/rmat.pxd new file mode 100644 index 00000000000..3c51108c778 --- /dev/null +++ b/python/cugraph/generators/rmat.pxd @@ -0,0 +1,45 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from libcpp cimport bool +from cugraph.structure.graph_utilities cimport * +from libcpp.vector cimport vector + +cdef extern from "cugraph/graph_generators.hpp" namespace "cugraph": + ctypedef enum generator_distribution_t: + POWER_LAW "cugraph::generator_distribution_t::POWER_LAW" + UNIFORM "cugraph::generator_distribution_t::UNIFORM" + + +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": + cdef unique_ptr[graph_generator_t] call_generate_rmat_edgelist[vertex_t] ( + const handle_t &handle, + size_t scale, + size_t num_edges, + double a, + double b, + double c, + int seed, + bool clip_and_flip, + bool scramble_vertex_ids) except + + + cdef vector[pair[unique_ptr[device_buffer], unique_ptr[device_buffer]]] call_generate_rmat_edgelists[vertex_t]( + const handle_t &handle, + size_t n_edgelists, + size_t min_scale, + size_t max_scale, + size_t edge_factor, + generator_distribution_t size_distribution, + generator_distribution_t edge_distribution, + int seed, + bool clip_and_flip, + bool scramble_vertex_ids) except + diff --git a/python/cugraph/generators/rmat.py b/python/cugraph/generators/rmat.py new file mode 100644 index 00000000000..d93ceb34cd1 --- /dev/null +++ b/python/cugraph/generators/rmat.py @@ -0,0 +1,396 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from dask.distributed import default_client +import dask_cudf + +from cugraph.generators import rmat_wrapper +from cugraph.comms import comms as Comms +import cugraph + + +def _ensure_args_rmat( + scale, + num_edges, + a, + b, + c, + seed, + clip_and_flip, + scramble_vertex_ids, + create_using, + mg +): + """ + Ensures the args passed in are usable for the rmat() API, raises the + appropriate exception if incorrect, else returns None. + """ + if mg and create_using not in [None, cugraph.DiGraph]: + raise TypeError("Only cugraph.DiGraph and None are supported types " + "for `create_using` for multi-GPU R-MAT") + if create_using not in [None, cugraph.Graph, cugraph.DiGraph]: + raise TypeError("Only cugraph.Graph, cugraph.DiGraph, and None are " + "supported types for 'create_using'") + if not isinstance(scale, int): + raise TypeError("'scale' must be an int") + if not isinstance(num_edges, int): + raise TypeError("'num_edges' must be an int") + if (a+b+c > 1): + raise ValueError( + "a + b + c should be non-negative and no larger than 1.0") + if (clip_and_flip not in [True, False]): + raise ValueError("'clip_and_flip' must be a bool") + if (scramble_vertex_ids not in [True, False]): + raise ValueError("'clip_and_flip' must be a bool") + if not isinstance(seed, int): + raise TypeError("'seed' must be an int") + + +def _ensure_args_multi_rmat( + n_edgelists, + min_scale, + max_scale, + edge_factor, + size_distribution, + edge_distribution, + seed, + clip_and_flip, + scramble_vertex_ids +): + """ + Ensures the args passed in are usable for the multi_rmat() API, raises the + appropriate exception if incorrect, else returns None. + """ + if not isinstance(n_edgelists, int): + raise TypeError("'n_edgelists' must be an int") + if not isinstance(min_scale, int): + raise TypeError("'min_scale' must be an int") + if not isinstance(max_scale, int): + raise TypeError("'max_scale' must be an int") + if not isinstance(edge_factor, int): + raise TypeError("'edge_factor' must be an int") + if (size_distribution not in [0, 1]): + raise TypeError("'size_distribution' must be either 0 or 1") + if (edge_distribution not in [0, 1]): + raise TypeError("'edge_distribution' must be either 0 or 1") + if (clip_and_flip not in [True, False]): + raise ValueError("'clip_and_flip' must be a bool") + if (scramble_vertex_ids not in [True, False]): + raise ValueError("'clip_and_flip' must be a bool") + if not isinstance(seed, int): + raise TypeError("'seed' must be an int") + + +def _sg_rmat( + scale, + num_edges, + a, + b, + c, + seed, + clip_and_flip, + scramble_vertex_ids, + create_using=cugraph.DiGraph +): + """ + Calls RMAT on a single GPU and uses the resulting cuDF DataFrame + to initialize and return a cugraph Graph object specified with + create_using. If create_using is None, returns the edgelist df as-is. + """ + df = rmat_wrapper.generate_rmat_edgelist(scale, + num_edges, + a, + b, + c, + seed, + clip_and_flip, + scramble_vertex_ids) + if create_using is None: + return df + + G = create_using() + G.from_cudf_edgelist(df, source='src', destination='dst', renumber=False) + + return G + + +def _mg_rmat( + scale, + num_edges, + a, + b, + c, + seed, + clip_and_flip, + scramble_vertex_ids, + create_using=cugraph.DiGraph +): + """ + Calls RMAT on multiple GPUs and uses the resulting Dask cuDF DataFrame to + initialize and return a cugraph Graph object specified with create_using. + If create_using is None, returns the Dask DataFrame edgelist as-is. + + seed is used as the initial seed for the first worker used (worker 0), then + each subsequent worker will receive seed+ as the seed value. + """ + client = default_client() + worker_list = list(client.scheduler_info()['workers'].keys()) + num_workers = len(worker_list) + num_edges_list = _calc_num_edges_per_worker(num_workers, num_edges) + futures = [] + for (i, worker_num_edges) in enumerate(num_edges_list): + unique_worker_seed = seed + i + future = client.submit( + _call_rmat, + Comms.get_session_id(), + scale, + worker_num_edges, + a, + b, + c, + unique_worker_seed, + clip_and_flip, + scramble_vertex_ids, + workers=worker_list[i] + ) + futures.append(future) + + ddf = dask_cudf.from_delayed(futures) + + if create_using is None: + return ddf + + G = create_using() + G.from_dask_cudf_edgelist(ddf, source="src", destination="dst") + + return G + + +def _call_rmat( + sID, + scale, + num_edges_for_worker, + a, + b, + c, + unique_worker_seed, + clip_and_flip, + scramble_vertex_ids +): + """ + Callable passed to dask client.submit calls that extracts the individual + worker handle based on the dask session ID + """ + handle = Comms.get_handle(sID) + + return rmat_wrapper.generate_rmat_edgelist( + scale, + num_edges_for_worker, + a, + b, + c, + unique_worker_seed, + clip_and_flip, + scramble_vertex_ids, + handle=handle + ) + + +def _calc_num_edges_per_worker(num_workers, num_edges): + """ + Returns a list of length num_workers with the individual number of edges + each worker should generate. The sum of all edges in the list is num_edges. + """ + L = [] + w = num_edges // num_workers + r = num_edges % num_workers + for i in range(num_workers): + if (i < r): + L.append(w+1) + else: + L.append(w) + return L + + +############################################################################### + +def rmat( + scale, + num_edges, + a, + b, + c, + seed, + clip_and_flip, + scramble_vertex_ids, + create_using=cugraph.DiGraph, + mg=False +): + """ + Generate a Graph object using a Recursive MATrix (R-MAT) graph generation + algorithm. + + Parameters + ---------- + scale : int + Scale factor to set the number of vertices in the graph Vertex IDs have + values in [0, V), where V = 1 << 'scale' + + num_edges : int + Number of edges to generate + + a : float + Probability of the first partition + + b : float + Probability of the second partition + + c : float + Probability of the thrid partition + + seed : int + Seed value for the random number generator + + clip_and_flip : bool + Flag controlling whether to generate edges only in the lower triangular + part (including the diagonal) of the graph adjacency matrix + (if set to 'true') or not (if set to 'false). + + scramble_vertex_ids : bool + Flag controlling whether to scramble vertex ID bits (if set to `true`) + or not (if set to `false`); scrambling vertex ID bits breaks + correlation between vertex ID values and vertex degrees. + + create_using : cugraph Graph type or None The graph type to construct + containing the generated edges and vertices. If None is specified, the + edgelist cuDF DataFrame (or dask_cudf DataFrame for MG) is returned + as-is. This is useful for benchmarking Graph construction steps that + require raw data that includes potential self-loops, isolated vertices, + and duplicated edges. Default is cugraph.DiGraph. + NOTE: only the cugraph.DiGraph type is supported for multi-GPU + + mg : bool + If True, R-MAT generation occurs across multiple GPUs. If False, only a + single GPU is used. Default is False (single-GPU) + + Returns + ------- + instance of cugraph.Graph + + Examples + -------- + import cugraph + from cugraph.generators import rmat + + df = rmat( + scale, + (2**scale)*edgefactor, + 0.1, + 0.2, + 0.3, + seed or 42, + clip_and_flip=False, + scramble_vertex_ids=True, + create_using=None, # return edgelist instead of Graph instance + mg=False + ) + + """ + _ensure_args_rmat(scale, num_edges, a, b, c, seed, clip_and_flip, + scramble_vertex_ids, create_using, mg) + + if mg: + return _mg_rmat(scale, num_edges, a, b, c, seed, clip_and_flip, + scramble_vertex_ids, create_using) + else: + return _sg_rmat(scale, num_edges, a, b, c, seed, clip_and_flip, + scramble_vertex_ids, create_using) + + +def multi_rmat( + n_edgelists, + min_scale, + max_scale, + edge_factor, + size_distribution, + edge_distribution, + seed, + clip_and_flip, + scramble_vertex_ids +): + """ + Generate multiple Graph objects using a Recursive MATrix (R-MAT) graph + generation algorithm. + + Parameters + ---------- + n_edgelists : int + Number of edge lists (graphs) to generate + + min_scale : int + Scale factor to set the minimum number of vertices in the graph + + max_scale : int + Scale factor to set the maximum number of vertices in the graph + + edge_factor : int + Average number of edges per vertex to generate + + size_distribution : int + Distribution of the graph sizes, impacts the scale parameter of the R-MAT + generator. + '0' for POWER_LAW distribution and '1' for UNIFORM distribution + + edge_distribution : int + Edges distribution for each graph, impacts how R-MAT parameters a,b,c,d, + are set. + '0' for POWER_LAW distribution and '1' for UNIFORM distribution + + seed : int + Seed value for the random number generator + + clip_and_flip : bool + Flag controlling whether to generate edges only in the lower triangular + part (including the diagonal) of the graph adjacency matrix + (if set to 'true') or not (if set to 'false') + + scramble_vertex_ids : bool + Flag controlling whether to scramble vertex ID bits (if set to 'true') or + not (if set to 'false'); scrambling vertx ID bits breaks correlation + between vertex ID values and vertex degrees + + Returns + ------- + list of cugraph.Graph instances + """ + _ensure_args_multi_rmat(n_edgelists, min_scale, max_scale, edge_factor, + size_distribution, edge_distribution, seed, + clip_and_flip, scramble_vertex_ids) + + dfs = rmat_wrapper.generate_rmat_edgelists( + n_edgelists, min_scale, + max_scale, + edge_factor, + size_distribution, + edge_distribution, + seed, + clip_and_flip, + scramble_vertex_ids) + list_G = [] + + for df in dfs: + G = cugraph.Graph() + G.from_cudf_edgelist(df, source='src', destination='dst') + list_G.append(G) + + return list_G diff --git a/python/cugraph/generators/rmat_wrapper.pyx b/python/cugraph/generators/rmat_wrapper.pyx new file mode 100644 index 00000000000..26f3772ad32 --- /dev/null +++ b/python/cugraph/generators/rmat_wrapper.pyx @@ -0,0 +1,171 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from libcpp cimport bool +from libc.stdint cimport uintptr_t +import numpy as np +import numpy.ctypeslib as ctypeslib +from cython.operator cimport dereference as deref + +import rmm +from rmm._lib.device_buffer cimport DeviceBuffer +import cudf +from cudf.core.buffer import Buffer + +from cugraph.structure.graph_utilities cimport * +from cugraph.generators.rmat cimport * +from libcpp.utility cimport move # This must be imported after graph_utilities + # since graph_utilities also defines move + + +def generate_rmat_edgelist( + scale, + num_edges, + a, + b, + c, + seed, + clip_and_flip, + scramble_vertex_ids, + handle=None +): + + vertex_t = np.dtype("int32") + if (2**scale) > (2**31 - 1): + vertex_t = np.dtype("int64") + + cdef unique_ptr[handle_t] handle_ptr + cdef size_t handle_size_t + + if handle is None: + handle_ptr.reset(new handle_t()) + handle_ = handle_ptr.get() + else: + handle_size_t = handle.getHandle() + handle_ = handle_size_t + + cdef unique_ptr[graph_generator_t] gg_ret_ptr + + if (vertex_t==np.dtype("int32")): + gg_ret_ptr = move(call_generate_rmat_edgelist[int]( deref(handle_), + scale, + num_edges, + a, + b, + c, + seed, + clip_and_flip, + scramble_vertex_ids)) + else: # (vertex_t == np.dtype("int64")) + gg_ret_ptr = move(call_generate_rmat_edgelist[long]( deref(handle_), + scale, + num_edges, + a, + b, + c, + seed, + clip_and_flip, + scramble_vertex_ids)) + + gg_ret = move(gg_ret_ptr.get()[0]) + source_set = DeviceBuffer.c_from_unique_ptr(move(gg_ret.d_source)) + destination_set = DeviceBuffer.c_from_unique_ptr(move(gg_ret.d_destination)) + source_set = Buffer(source_set) + destination_set = Buffer(destination_set) + + set_source = cudf.Series(data=source_set, dtype=vertex_t) + set_destination = cudf.Series(data=destination_set, dtype=vertex_t) + + df = cudf.DataFrame() + df['src'] = set_source + df['dst'] = set_destination + + return df + + +def generate_rmat_edgelists( + n_edgelists, + min_scale, + max_scale, + edge_factor, + size_distribution, + edge_distribution, + seed, + clip_and_flip, + scramble_vertex_ids + ): + + vertex_t = np.dtype("int32") + if (2**max_scale) > (2**31 - 1): + vertex_t = np.dtype("int64") + + cdef unique_ptr[handle_t] handle_ptr + handle_ptr.reset(new handle_t()) + handle_ = handle_ptr.get() + + cdef generator_distribution_t s_distribution + cdef generator_distribution_t e_distribution + if size_distribution == 0: + s_distribution= POWER_LAW + else : + s_distribution= UNIFORM + if edge_distribution == 0: + e_distribution= POWER_LAW + else : + e_distribution= UNIFORM + #cdef unique_ptr[graph_generator_t*] gg_ret_ptr + cdef vector[pair[unique_ptr[device_buffer], unique_ptr[device_buffer]]] gg_ret_ptr + + if (vertex_t==np.dtype("int32")): + #gg_ret_ptr = move(call_generate_rmat_edgelists[int]( deref(handle_), + gg_ret_ptr = move(call_generate_rmat_edgelists[int]( deref(handle_), + n_edgelists, + min_scale, + max_scale, + edge_factor, + s_distribution, + e_distribution, + seed, + clip_and_flip, + scramble_vertex_ids)) + else: # (vertex_t == np.dtype("int64")) + #gg_ret_ptr = move(call_generate_rmat_edgelists[long]( deref(handle_), + gg_ret_ptr = move(call_generate_rmat_edgelists[long]( deref(handle_), + n_edgelists, + min_scale, + max_scale, + edge_factor, + s_distribution, + e_distribution, + seed, + clip_and_flip, + scramble_vertex_ids)) + list_df = [] + + for i in range(n_edgelists): + source_set = DeviceBuffer.c_from_unique_ptr(move(gg_ret_ptr[i].first)) + destination_set = DeviceBuffer.c_from_unique_ptr(move(gg_ret_ptr[i].second)) + source_set = Buffer(source_set) + destination_set = Buffer(destination_set) + + set_source = cudf.Series(data=source_set, dtype=vertex_t) + set_destination = cudf.Series(data=destination_set, dtype=vertex_t) + + df = cudf.DataFrame() + df['src'] = set_source + df['dst'] = set_destination + + list_df.append(df) + + #Return a list of dataframes + return list_df diff --git a/python/cugraph/internals/callbacks_implems.hpp b/python/cugraph/internals/callbacks_implems.hpp index 7b3a27f6bff..79fab937965 100644 --- a/python/cugraph/internals/callbacks_implems.hpp +++ b/python/cugraph/internals/callbacks_implems.hpp @@ -1,5 +1,5 @@ /* - * 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. @@ -17,7 +17,7 @@ #pragma once #include -#include +#include #include diff --git a/python/cugraph/layout/force_atlas2.pxd b/python/cugraph/layout/force_atlas2.pxd index cda55cda5c5..5496d1b655e 100644 --- a/python/cugraph/layout/force_atlas2.pxd +++ b/python/cugraph/layout/force_atlas2.pxd @@ -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. # You may obtain a copy of the License at @@ -19,12 +19,13 @@ from cugraph.structure.graph_primtypes cimport * from libcpp cimport bool -cdef extern from "internals.hpp" namespace "cugraph::internals": +cdef extern from "cugraph/internals.hpp" namespace "cugraph::internals": cdef cppclass GraphBasedDimRedCallback -cdef extern from "algorithms.hpp" namespace "cugraph": +cdef extern from "cugraph/algorithms.hpp" namespace "cugraph": cdef void force_atlas2[vertex_t, edge_t, weight_t]( + const handle_t &handle, GraphCOOView[vertex_t, edge_t, weight_t] &graph, float *pos, const int max_iter, diff --git a/python/cugraph/layout/force_atlas2.py b/python/cugraph/layout/force_atlas2.py index 4c6859c6c03..d15109249e5 100644 --- a/python/cugraph/layout/force_atlas2.py +++ b/python/cugraph/layout/force_atlas2.py @@ -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. # You may obtain a copy of the License at @@ -12,7 +12,6 @@ # limitations under the License. from cugraph.layout import force_atlas2_wrapper -from cugraph.structure.graph import null_check def force_atlas2( @@ -109,13 +108,14 @@ def on_train_end(self, positions): """ if pos_list is not None: - null_check(pos_list["vertex"]) - null_check(pos_list["x"]) - null_check(pos_list["y"]) if input_graph.renumbered is True: + if input_graph.vertex_column_size() > 1: + cols = pos_list.columns[:-2].to_list() + else: + cols = 'vertex' pos_list = input_graph.add_internal_vertex_id(pos_list, "vertex", - "vertex") + cols) if prevent_overlapping: raise Exception("Feature not supported") diff --git a/python/cugraph/layout/force_atlas2_wrapper.pyx b/python/cugraph/layout/force_atlas2_wrapper.pyx index 785ddda47bd..1644875f034 100644 --- a/python/cugraph/layout/force_atlas2_wrapper.pyx +++ b/python/cugraph/layout/force_atlas2_wrapper.pyx @@ -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. # You may obtain a copy of the License at @@ -19,17 +19,13 @@ from cugraph.layout.force_atlas2 cimport force_atlas2 as c_force_atlas2 from cugraph.structure import graph_primtypes_wrapper from cugraph.structure.graph_primtypes cimport * -from cugraph.structure import utils_wrapper from libcpp cimport bool from libc.stdint cimport uintptr_t - import cudf -import cudf._lib as libcudf from numba import cuda import numpy as np -import numpy.ctypeslib as ctypeslib -cdef extern from "internals.hpp" namespace "cugraph::internals": +cdef extern from "cugraph/internals.hpp" namespace "cugraph::internals": cdef cppclass GraphBasedDimRedCallback @@ -53,6 +49,10 @@ def force_atlas2(input_graph, Call force_atlas2 """ + cdef unique_ptr[handle_t] handle_ptr + handle_ptr.reset(new handle_t()) + handle_ = handle_ptr.get(); + if not input_graph.edgelist: input_graph.view_edge_list() @@ -65,12 +65,19 @@ def force_atlas2(input_graph, df = cudf.DataFrame() df['vertex'] = cudf.Series(np.arange(num_verts, dtype=np.int32)) - cdef uintptr_t c_src_indices = input_graph.edgelist.edgelist_df['src'].__cuda_array_interface__['data'][0] - cdef uintptr_t c_dst_indices = input_graph.edgelist.edgelist_df['dst'].__cuda_array_interface__['data'][0] + src = input_graph.edgelist.edgelist_df['src'] + dst = input_graph.edgelist.edgelist_df['dst'] + + [src, dst] = graph_primtypes_wrapper.datatype_cast([src, dst], [np.int32]) + + cdef uintptr_t c_src_indices = src.__cuda_array_interface__['data'][0] + cdef uintptr_t c_dst_indices = dst.__cuda_array_interface__['data'][0] cdef uintptr_t c_weights = NULL if input_graph.edgelist.weights: - c_weights = input_graph.edgelist.edgelist_df['weights'].__cuda_array_interface__['data'][0] + weights = input_graph.edgelist.edgelist_df["weights"] + [weights] = graph_primtypes_wrapper.datatype_cast([weights], [np.float32, np.float64]) + c_weights = weights.__cuda_array_interface__['data'][0] cdef uintptr_t x_start = NULL cdef uintptr_t y_start = NULL @@ -104,7 +111,8 @@ def force_atlas2(input_graph, graph_double = GraphCOOView[int,int, double](c_src_indices, c_dst_indices, c_weights, num_verts, num_edges) - c_force_atlas2[int, int, double](graph_double, + c_force_atlas2[int, int, double](handle_[0], + graph_double, pos_ptr, max_iter, x_start, @@ -125,7 +133,8 @@ def force_atlas2(input_graph, graph_float = GraphCOOView[int,int,float](c_src_indices, c_dst_indices, c_weights, num_verts, num_edges) - c_force_atlas2[int, int, float](graph_float, + c_force_atlas2[int, int, float](handle_[0], + graph_float, pos_ptr, max_iter, x_start, diff --git a/python/cugraph/linear_assignment/__init__.py b/python/cugraph/linear_assignment/__init__.py index 9bf09b67ed9..557bbbdf170 100644 --- a/python/cugraph/linear_assignment/__init__.py +++ b/python/cugraph/linear_assignment/__init__.py @@ -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. # You may obtain a copy of the License at @@ -11,4 +11,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -from cugraph.linear_assignment.lap import hungarian +from cugraph.linear_assignment.lap import hungarian, dense_hungarian diff --git a/python/cugraph/linear_assignment/lap.pxd b/python/cugraph/linear_assignment/lap.pxd index f7991405b7f..9f65e215891 100644 --- a/python/cugraph/linear_assignment/lap.pxd +++ b/python/cugraph/linear_assignment/lap.pxd @@ -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. # You may obtain a copy of the License at @@ -18,11 +18,36 @@ from cugraph.structure.graph_primtypes cimport * -cdef extern from "algorithms.hpp" namespace "cugraph": +cdef extern from "cugraph/algorithms.hpp" namespace "cugraph": - cdef void hungarian[VT,ET,WT]( + cdef weight_t hungarian[vertex_t,edge_t,weight_t]( const handle_t &handle, - const GraphCOOView[VT,ET,WT] &graph, - VT num_workers, - const VT *workers, - VT *assignment) except + + const GraphCOOView[vertex_t,edge_t,weight_t] &graph, + vertex_t num_workers, + const vertex_t *workers, + vertex_t *assignments, + weight_t epsilon) except + + + cdef weight_t hungarian[vertex_t,edge_t,weight_t]( + const handle_t &handle, + const GraphCOOView[vertex_t,edge_t,weight_t] &graph, + vertex_t num_workers, + const vertex_t *workers, + vertex_t *assignments) except + + +cdef extern from "cugraph/algorithms.hpp": + + cdef weight_t dense_hungarian "cugraph::dense::hungarian" [vertex_t,weight_t]( + const handle_t &handle, + const weight_t *costs, + vertex_t num_rows, + vertex_t num_columns, + vertex_t *assignments, + weight_t epsilon) except + + + cdef weight_t dense_hungarian "cugraph::dense::hungarian" [vertex_t,weight_t]( + const handle_t &handle, + const weight_t *costs, + vertex_t num_rows, + vertex_t num_columns, + vertex_t *assignments) except + diff --git a/python/cugraph/linear_assignment/lap.py b/python/cugraph/linear_assignment/lap.py index 5c501d17935..ed40e96fb47 100644 --- a/python/cugraph/linear_assignment/lap.py +++ b/python/cugraph/linear_assignment/lap.py @@ -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. # You may obtain a copy of the License at @@ -11,10 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +import cudf from cugraph.linear_assignment import lap_wrapper -def hungarian(G, workers): +def hungarian(G, workers, epsilon=None): """ Execute the Hungarian algorithm against a symmetric, weighted, bipartite graph. @@ -39,13 +40,21 @@ def hungarian(G, workers): as an an edge list. Edge weights are required. If an edge list is not provided then it will be computed. - workers : cudf.Series + workers : cudf.Series or cudf.DataFrame A series or column that identifies the vertex ids of the vertices - in the workers set. All vertices in G that are not in the workers + in the workers set. In case of multi-column vertices, it should be a + cudf.DataFrame. All vertices in G that are not in the workers set are implicitly assigned to the jobs set. + epsilon : float or double (matching weight type in graph) + Used for determining when value is close enough to zero to consider 0. + Defaults (if not specified) to 1e-6 in the C++ code. Unused for + integer weight types. + Returns ------- + cost : matches costs.dtype + The cost of the overall assignment df : cudf.DataFrame df['vertex'][i] gives the vertex id of the i'th vertex. Only vertices in the workers list are defined in this column. @@ -60,18 +69,64 @@ def hungarian(G, workers): >>> dtype=['int32', 'int32', 'float32'], header=None) >>> G = cugraph.Graph() >>> G.from_cudf_edgelist(M, source='0', destination='1', edge_attr='2') - >>> df = cugraph.hungarian(G, workers) + >>> cost, df = cugraph.hungarian(G, workers) """ if G.renumbered: - local_workers = G.lookup_internal_vertex_id(workers) + if isinstance(workers, cudf.DataFrame): + local_workers = G.lookup_internal_vertex_id(workers, + workers.columns) + else: + local_workers = G.lookup_internal_vertex_id(workers) else: local_workers = workers - df = lap_wrapper.hungarian(G, local_workers) + cost, df = lap_wrapper.sparse_hungarian(G, local_workers, epsilon) if G.renumbered: df = G.unrenumber(df, 'vertex') - return df + return cost, df + + +def dense_hungarian(costs, num_rows, num_columns, epsilon=None): + """ + Execute the Hungarian algorithm against a dense bipartite + graph representation. + + *NOTE*: This API is unstable and subject to change + + The Hungarian algorithm identifies the lowest cost matching of vertices + such that all workers that can be assigned work are assigned exactly + on job. + + Parameters + ---------- + costs : cudf.Series + A dense representation (row major order) of the bipartite + graph. Each row represents a worker, each column represents + a task, cost[i][j] represents the cost of worker i performing + task j. + num_rows : int + Number of rows in the matrix + num_columns : int + Number of columns in the matrix + epsilon : float or double (matching weight type in graph) + Used for determining when value is close enough to zero to consider 0. + Defaults (if not specified) to 1e-6 in the C++ code. Unused for + integer weight types. + + Returns + ------- + cost : matches costs.dtype + The cost of the overall assignment + assignment : cudf.Series + assignment[i] gives the vertex id of the task assigned to the + worker i + + FIXME: Update this with a real example... + + """ + + return lap_wrapper.dense_hungarian(costs, num_rows, num_columns, epsilon) diff --git a/python/cugraph/linear_assignment/lap_wrapper.pyx b/python/cugraph/linear_assignment/lap_wrapper.pyx index caaa837e859..c173f45fa3f 100644 --- a/python/cugraph/linear_assignment/lap_wrapper.pyx +++ b/python/cugraph/linear_assignment/lap_wrapper.pyx @@ -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. # You may obtain a copy of the License at @@ -17,15 +17,15 @@ # cython: language_level = 3 from cugraph.linear_assignment.lap cimport hungarian as c_hungarian +from cugraph.linear_assignment.lap cimport dense_hungarian as c_dense_hungarian from cugraph.structure.graph_primtypes cimport * from cugraph.structure import graph_primtypes_wrapper from libc.stdint cimport uintptr_t -from cugraph.structure.graph import Graph as type_Graph - import cudf import numpy as np -def hungarian(input_graph, workers): + +def sparse_hungarian(input_graph, workers, epsilon): """ Call the hungarian algorithm """ @@ -62,6 +62,9 @@ def hungarian(input_graph, workers): df['vertex'] = workers df['assignment'] = cudf.Series(np.zeros(len(workers), dtype=np.int32)) + if epsilon == None: + epsilon = 1e-6 + cdef uintptr_t c_src = src.__cuda_array_interface__['data'][0] cdef uintptr_t c_dst = dst.__cuda_array_interface__['data'][0] cdef uintptr_t c_weights = weights.__cuda_array_interface__['data'][0] @@ -69,6 +72,8 @@ def hungarian(input_graph, workers): cdef uintptr_t c_identifier = df['vertex'].__cuda_array_interface__['data'][0]; cdef uintptr_t c_assignment = df['assignment'].__cuda_array_interface__['data'][0]; + cdef float c_epsilon_float = epsilon + cdef double c_epsilon_double = epsilon cdef GraphCOOView[int,int,float] g_float cdef GraphCOOView[int,int,double] g_double @@ -76,10 +81,43 @@ def hungarian(input_graph, workers): if weights.dtype == np.float32: g_float = GraphCOOView[int,int,float](c_src, c_dst, c_weights, num_verts, num_edges) - c_hungarian[int,int,float](handle_[0], g_float, len(workers), c_workers, c_assignment) + cost = c_hungarian[int,int,float](handle_[0], g_float, len(workers), c_workers, c_assignment, c_epsilon_float) else: g_double = GraphCOOView[int,int,double](c_src, c_dst, c_weights, num_verts, num_edges) - c_hungarian[int,int,double](handle_[0], g_double, len(workers), c_workers, c_assignment) + cost = c_hungarian[int,int,double](handle_[0], g_double, len(workers), c_workers, c_assignment, c_epsilon_double) + + return cost, df + + +def dense_hungarian(costs, num_rows, num_columns, epsilon): + """ + Call the dense hungarian algorithm + """ + if type(costs) is not cudf.Series: + raise("costs must be a cudf.Series") + + cdef unique_ptr[handle_t] handle_ptr + handle_ptr.reset(new handle_t()) + handle_ = handle_ptr.get(); + + assignment = cudf.Series(np.zeros(num_rows, dtype=np.int32)) + + if epsilon == None: + epsilon = 1e-6 + + cdef uintptr_t c_costs = costs.__cuda_array_interface__['data'][0] + cdef uintptr_t c_assignment = assignment.__cuda_array_interface__['data'][0] + cdef float c_epsilon_float = epsilon + cdef double c_epsilon_double = epsilon + + if costs.dtype == np.float32: + cost = c_dense_hungarian[int,float](handle_[0], c_costs, num_rows, num_columns, c_assignment, c_epsilon_float) + elif costs.dtype == np.float64: + cost = c_dense_hungarian[int,double](handle_[0], c_costs, num_rows, num_columns, c_assignment, c_epsilon_double) + elif costs.dtype == np.int32: + cost = c_dense_hungarian[int,double](handle_[0], c_costs, num_rows, num_columns, c_assignment) + else: + raise("unsported type: ", costs.dtype) - return df + return cost, assignment diff --git a/python/cugraph/link_analysis/hits.pxd b/python/cugraph/link_analysis/hits.pxd index 60d25fd3cdb..9e40f7444f9 100644 --- a/python/cugraph/link_analysis/hits.pxd +++ b/python/cugraph/link_analysis/hits.pxd @@ -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. # You may obtain a copy of the License at @@ -20,7 +20,7 @@ from cugraph.structure.graph_primtypes cimport * from libcpp cimport bool -cdef extern from "algorithms.hpp" namespace "cugraph::gunrock": +cdef extern from "cugraph/algorithms.hpp" namespace "cugraph::gunrock": cdef void hits[VT,ET,WT]( const GraphCSRView[VT,ET,WT] &graph, diff --git a/python/cugraph/link_analysis/hits_wrapper.pyx b/python/cugraph/link_analysis/hits_wrapper.pyx index 3e19e38a023..2a2d33dea0b 100644 --- a/python/cugraph/link_analysis/hits_wrapper.pyx +++ b/python/cugraph/link_analysis/hits_wrapper.pyx @@ -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. # You may obtain a copy of the License at @@ -18,13 +18,10 @@ from cugraph.link_analysis.hits cimport hits as c_hits from cugraph.structure.graph_primtypes cimport * -from libcpp cimport bool from libc.stdint cimport uintptr_t from cugraph.structure import graph_primtypes_wrapper import cudf -import rmm import numpy as np -import numpy.ctypeslib as ctypeslib def hits(input_graph, max_iter=100, tol=1.0e-5, nstart=None, normalized=True): @@ -48,8 +45,6 @@ def hits(input_graph, max_iter=100, tol=1.0e-5, nstart=None, normalized=True): df['hubs'] = cudf.Series(np.zeros(num_verts, dtype=np.float32)) df['authorities'] = cudf.Series(np.zeros(num_verts, dtype=np.float32)) - #cdef bool normalized = 1 - cdef uintptr_t c_identifier = df['vertex'].__cuda_array_interface__['data'][0]; cdef uintptr_t c_hubs = df['hubs'].__cuda_array_interface__['data'][0]; cdef uintptr_t c_authorities = df['authorities'].__cuda_array_interface__['data'][0]; diff --git a/python/cugraph/link_analysis/pagerank.pxd b/python/cugraph/link_analysis/pagerank.pxd index 79cb033f74b..ed8f763b3ca 100644 --- a/python/cugraph/link_analysis/pagerank.pxd +++ b/python/cugraph/link_analysis/pagerank.pxd @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -16,11 +16,11 @@ # cython: embedsignature = True # cython: language_level = 3 -from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * from libcpp cimport bool -cdef extern from "utilities/cython.hpp" namespace "cugraph::cython": +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": cdef void call_pagerank[VT,WT]( const handle_t &handle, diff --git a/python/cugraph/link_analysis/pagerank.py b/python/cugraph/link_analysis/pagerank.py index 69133d62af7..94b1491e944 100644 --- a/python/cugraph/link_analysis/pagerank.py +++ b/python/cugraph/link_analysis/pagerank.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -12,7 +12,6 @@ # limitations under the License. from cugraph.link_analysis import pagerank_wrapper -from cugraph.structure.graph import null_check import cugraph @@ -32,7 +31,7 @@ def pagerank( ---------- graph : cugraph.Graph or networkx.Graph cuGraph graph descriptor, should contain the connectivity information - as an edge list (edge weights are not used for this algorithm). + as an edge list. The transposed adjacency list will be computed if not already present. alpha : float The damping factor alpha represents the probability to follow an @@ -46,7 +45,6 @@ def pagerank( Subset of vertices of graph for personalization personalization['values'] : cudf.Series Personalization values for vertices - max_iter : int The maximum number of iterations before an answer is returned. This can be used to limit the execution time and do an early exit before the @@ -68,11 +66,10 @@ def pagerank( Subset of vertices of graph for initial guess for pagerank values nstart['values'] : cudf.Series Pagerank values for vertices - - weight : str - Edge data column to use. Default is None - This version of PageRank current does not use edge weight. - This parameter is here for NetworkX compatibility + weight: str + The attribute column to be used as edge weights if Graph is a NetworkX + Graph. This parameter is here for NetworkX compatibility and is ignored + in case of a cugraph.Graph dangling : dict This parameter is here for NetworkX compatibility and ignored @@ -100,17 +97,23 @@ def pagerank( G, isNx = cugraph.utilities.check_nx_graph(G, weight) if personalization is not None: - null_check(personalization["vertex"]) - null_check(personalization["values"]) if G.renumbered is True: + if len(G.renumber_map.implementation.col_names) > 1: + cols = personalization.columns[:-1].to_list() + else: + cols = 'vertex' personalization = G.add_internal_vertex_id( - personalization, "vertex", "vertex" + personalization, "vertex", cols ) if nstart is not None: if G.renumbered is True: + if len(G.renumber_map.implementation.col_names) > 1: + cols = nstart.columns[:-1].to_list() + else: + cols = 'vertex' nstart = G.add_internal_vertex_id( - nstart, "vertex", "vertex" + nstart, "vertex", cols ) df = pagerank_wrapper.pagerank( diff --git a/python/cugraph/link_analysis/pagerank_wrapper.pyx b/python/cugraph/link_analysis/pagerank_wrapper.pyx index a8c1c9faee8..7198ccabc9e 100644 --- a/python/cugraph/link_analysis/pagerank_wrapper.pyx +++ b/python/cugraph/link_analysis/pagerank_wrapper.pyx @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -16,16 +16,13 @@ # cython: embedsignature = True # cython: language_level = 3 -#cimport cugraph.link_analysis.pagerank as c_pagerank from cugraph.link_analysis.pagerank cimport call_pagerank -from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * from libcpp cimport bool from libc.stdint cimport uintptr_t from cugraph.structure import graph_primtypes_wrapper import cudf -import rmm import numpy as np -import numpy.ctypeslib as ctypeslib def pagerank(input_graph, alpha=0.85, personalization=None, max_iter=100, tol=1.0e-5, nstart=None): @@ -33,21 +30,22 @@ def pagerank(input_graph, alpha=0.85, personalization=None, max_iter=100, tol=1. Call pagerank """ - if not input_graph.transposedadjlist: - input_graph.view_transposed_adj_list() - cdef unique_ptr[handle_t] handle_ptr handle_ptr.reset(new handle_t()) handle_ = handle_ptr.get(); - [offsets, indices] = graph_primtypes_wrapper.datatype_cast([input_graph.transposedadjlist.offsets, input_graph.transposedadjlist.indices], [np.int32]) - [weights] = graph_primtypes_wrapper.datatype_cast([input_graph.transposedadjlist.weights], [np.float32, np.float64]) + [src, dst] = graph_primtypes_wrapper.datatype_cast([input_graph.edgelist.edgelist_df['src'], input_graph.edgelist.edgelist_df['dst']], [np.int32]) + weights = None + if input_graph.edgelist.weights: + [weights] = graph_primtypes_wrapper.datatype_cast([input_graph.edgelist.edgelist_df['weights']], [np.float32, np.float64]) num_verts = input_graph.number_of_vertices() num_edges = input_graph.number_of_edges(directed_edges=True) + # FIXME: needs to be edge_t type not int + cdef int num_local_edges = len(src) df = cudf.DataFrame() - df['vertex'] = cudf.Series(np.zeros(num_verts, dtype=np.int32)) + df['vertex'] = cudf.Series(np.arange(num_verts, dtype=np.int32)) df['pagerank'] = cudf.Series(np.zeros(num_verts, dtype=np.float32)) cdef bool has_guess = 0 @@ -62,22 +60,23 @@ def pagerank(input_graph, alpha=0.85, personalization=None, max_iter=100, tol=1. cdef uintptr_t c_pers_vtx = NULL cdef uintptr_t c_pers_val = NULL - cdef sz = 0 - - cdef uintptr_t c_offsets = offsets.__cuda_array_interface__['data'][0] - cdef uintptr_t c_indices = indices.__cuda_array_interface__['data'][0] - cdef uintptr_t c_weights = NULL - cdef uintptr_t c_local_verts = NULL; - cdef uintptr_t c_local_edges = NULL; - cdef uintptr_t c_local_offsets = NULL; + cdef int sz = 0 + cdef uintptr_t c_src_vertices = src.__cuda_array_interface__['data'][0] + cdef uintptr_t c_dst_vertices = dst.__cuda_array_interface__['data'][0] + cdef uintptr_t c_edge_weights = NULL + personalization_id_series = None if weights is not None: - c_weights = weights.__cuda_array_interface__['data'][0] + c_edge_weights = weights.__cuda_array_interface__['data'][0] weight_t = weights.dtype + is_weighted = True else: weight_t = np.dtype("float32") + is_weighted = False + + is_symmetric = not input_graph.is_directed() # FIXME: Offsets and indices are currently hardcoded to int, but this may # not be acceptable in the future. @@ -94,15 +93,21 @@ def pagerank(input_graph, alpha=0.85, personalization=None, max_iter=100, tol=1. c_pers_val = personalization['values'].__cuda_array_interface__['data'][0] cdef graph_container_t graph_container - populate_graph_container_legacy(graph_container, - ((graphTypeEnum.LegacyCSC)), - handle_[0], - c_offsets, c_indices, c_weights, - ((numberTypeEnum.int32Type)), - ((numberTypeEnum.int32Type)), - ((numberTypeMap[weight_t])), - num_verts, num_edges, - c_local_verts, c_local_edges, c_local_offsets) + populate_graph_container(graph_container, + handle_[0], + c_src_vertices, c_dst_vertices, c_edge_weights, + NULL, + NULL, + 0, + ((numberTypeEnum.int32Type)), + ((numberTypeEnum.int32Type)), + ((numberTypeMap[weight_t])), + num_local_edges, + num_verts, num_edges, + is_weighted, + is_symmetric, + True, + False) if (df['pagerank'].dtype == np.float32): call_pagerank[int, float](handle_[0], graph_container, diff --git a/python/cugraph/link_prediction/jaccard.pxd b/python/cugraph/link_prediction/jaccard.pxd index bc55bb2cdf0..9e8c82ec3d8 100644 --- a/python/cugraph/link_prediction/jaccard.pxd +++ b/python/cugraph/link_prediction/jaccard.pxd @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -19,7 +19,7 @@ from cugraph.structure.graph_primtypes cimport * -cdef extern from "algorithms.hpp" namespace "cugraph": +cdef extern from "cugraph/algorithms.hpp" namespace "cugraph": cdef void jaccard[VT,ET,WT]( const GraphCSRView[VT,ET,WT] &graph, diff --git a/python/cugraph/link_prediction/jaccard.py b/python/cugraph/link_prediction/jaccard.py index 71cf0925342..e69308ac595 100644 --- a/python/cugraph/link_prediction/jaccard.py +++ b/python/cugraph/link_prediction/jaccard.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -13,11 +13,11 @@ import pandas as pd import cudf -from cugraph.structure.graph import Graph +from cugraph.structure.graph_classes import Graph from cugraph.link_prediction import jaccard_wrapper -from cugraph.structure.graph import null_check from cugraph.utilities import check_nx_graph from cugraph.utilities import df_edge_score_to_dictionary +from cugraph.utilities import renumber_vertex_pair def jaccard(input_graph, vertex_pair=None): @@ -109,15 +109,8 @@ def jaccard(input_graph, vertex_pair=None): if type(input_graph) is not Graph: raise Exception("input graph must be undirected") - # FIXME: Add support for multi-column vertices if type(vertex_pair) == cudf.DataFrame: - for col in vertex_pair.columns: - null_check(vertex_pair[col]) - if input_graph.renumbered: - vertex_pair = input_graph.add_internal_vertex_id( - vertex_pair, col, col - ) - + vertex_pair = renumber_vertex_pair(input_graph, vertex_pair) elif vertex_pair is None: pass else: diff --git a/python/cugraph/link_prediction/jaccard_wrapper.pyx b/python/cugraph/link_prediction/jaccard_wrapper.pyx index cacd13dec65..8d236c60ee2 100644 --- a/python/cugraph/link_prediction/jaccard_wrapper.pyx +++ b/python/cugraph/link_prediction/jaccard_wrapper.pyx @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -21,8 +21,6 @@ from cugraph.link_prediction.jaccard cimport jaccard_list as c_jaccard_list from cugraph.structure.graph_primtypes cimport * from cugraph.structure import graph_primtypes_wrapper from libc.stdint cimport uintptr_t -from cython cimport floating - import cudf import numpy as np diff --git a/python/cugraph/link_prediction/overlap.pxd b/python/cugraph/link_prediction/overlap.pxd index 970032b56eb..f0654472587 100644 --- a/python/cugraph/link_prediction/overlap.pxd +++ b/python/cugraph/link_prediction/overlap.pxd @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -19,7 +19,7 @@ from cugraph.structure.graph_primtypes cimport * -cdef extern from "algorithms.hpp" namespace "cugraph": +cdef extern from "cugraph/algorithms.hpp" namespace "cugraph": cdef void overlap[VT,ET,WT]( const GraphCSRView[VT,ET,WT] &graph, diff --git a/python/cugraph/link_prediction/overlap.py b/python/cugraph/link_prediction/overlap.py index a5ca1e22979..4650f24f181 100644 --- a/python/cugraph/link_prediction/overlap.py +++ b/python/cugraph/link_prediction/overlap.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -13,10 +13,10 @@ import pandas as pd from cugraph.link_prediction import overlap_wrapper -from cugraph.structure.graph import null_check import cudf from cugraph.utilities import check_nx_graph from cugraph.utilities import df_edge_score_to_dictionary +from cugraph.utilities import renumber_vertex_pair def overlap_coefficient(G, ebunch=None): @@ -91,14 +91,8 @@ def overlap(input_graph, vertex_pair=None): >>> df = cugraph.overlap(G) """ - # FIXME: Add support for multi-column vertices if type(vertex_pair) == cudf.DataFrame: - for col in vertex_pair.columns: - null_check(vertex_pair[col]) - if input_graph.renumbered: - vertex_pair = input_graph.add_internal_vertex_id( - vertex_pair, col, col, - ) + vertex_pair = renumber_vertex_pair(input_graph, vertex_pair) elif vertex_pair is None: pass else: diff --git a/python/cugraph/link_prediction/overlap_wrapper.pyx b/python/cugraph/link_prediction/overlap_wrapper.pyx index 9e2f3ba49d7..ec0274716fb 100644 --- a/python/cugraph/link_prediction/overlap_wrapper.pyx +++ b/python/cugraph/link_prediction/overlap_wrapper.pyx @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -21,8 +21,6 @@ from cugraph.link_prediction.overlap cimport overlap_list as c_overlap_list from cugraph.structure.graph_primtypes cimport * from cugraph.structure import graph_primtypes_wrapper from libc.stdint cimport uintptr_t -from cython cimport floating - import cudf import numpy as np @@ -70,8 +68,9 @@ def overlap(input_graph, weights_arr=None, vertex_pair=None): df = cudf.DataFrame() df['overlap_coeff'] = result - first = vertex_pair['first'] - second = vertex_pair['second'] + cols = vertex_pair.columns.to_list() + first = vertex_pair[cols[0]] + second = vertex_pair[cols[1]] # FIXME: multi column support df['source'] = first diff --git a/python/cugraph/link_prediction/wjaccard.py b/python/cugraph/link_prediction/wjaccard.py index 2a4e2417102..9616bfd49a8 100644 --- a/python/cugraph/link_prediction/wjaccard.py +++ b/python/cugraph/link_prediction/wjaccard.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -11,10 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -from cugraph.structure.graph import Graph +from cugraph.structure.graph_classes import Graph from cugraph.link_prediction import jaccard_wrapper -from cugraph.structure.graph import null_check import cudf +import numpy as np +from cugraph.utilities import renumber_vertex_pair def jaccard_w(input_graph, weights, vertex_pair=None): @@ -36,8 +37,15 @@ def jaccard_w(input_graph, weights, vertex_pair=None): as an edge list (edge weights are not used for this algorithm). The adjacency list will be computed if not already present. - weights : cudf.Series + weights : cudf.DataFrame Specifies the weights to be used for each vertex. + Vertex should be represented by multiple columns for multi-column + vertices. + + weights['vertex'] : cudf.Series + Contains the vertex identifiers + weights['weight'] : cudf.Series + Contains the weights of vertices vertex_pair : cudf.DataFrame A GPU dataframe consisting of two columns representing pairs of @@ -71,20 +79,28 @@ def jaccard_w(input_graph, weights, vertex_pair=None): if type(input_graph) is not Graph: raise Exception("input graph must be undirected") - # FIXME: Add support for multi-column vertices if type(vertex_pair) == cudf.DataFrame: - for col in vertex_pair.columns: - null_check(vertex_pair[col]) - if input_graph.renumbered: - vertex_pair = input_graph.add_internal_vertex_id( - vertex_pair, col, col, - ) + vertex_pair = renumber_vertex_pair(input_graph, vertex_pair) elif vertex_pair is None: pass else: raise ValueError("vertex_pair must be a cudf dataframe") - df = jaccard_wrapper.jaccard(input_graph, weights, vertex_pair) + if input_graph.renumbered: + vertex_size = input_graph.vertex_column_size() + if vertex_size == 1: + weights = input_graph.add_internal_vertex_id( + weights, 'vertex', 'vertex' + ) + else: + cols = weights.columns[:vertex_size].to_list() + weights = input_graph.add_internal_vertex_id( + weights, 'vertex', cols + ) + jaccard_weights = cudf.Series(np.ones(len(weights))) + for i in range(len(weights)): + jaccard_weights[weights['vertex'].iloc[i]] = weights['weight'].iloc[i] + df = jaccard_wrapper.jaccard(input_graph, jaccard_weights, vertex_pair) if input_graph.renumbered: df = input_graph.unrenumber(df, "source") diff --git a/python/cugraph/link_prediction/woverlap.py b/python/cugraph/link_prediction/woverlap.py index c93ad28ea54..920d3e3f80d 100644 --- a/python/cugraph/link_prediction/woverlap.py +++ b/python/cugraph/link_prediction/woverlap.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -12,8 +12,9 @@ # limitations under the License. from cugraph.link_prediction import overlap_wrapper -from cugraph.structure.graph import null_check import cudf +import numpy as np +from cugraph.utilities import renumber_vertex_pair def overlap_w(input_graph, weights, vertex_pair=None): @@ -67,20 +68,33 @@ def overlap_w(input_graph, weights, vertex_pair=None): >>> G.from_cudf_edgelist(M, source='0', destination='1') >>> df = cugraph.overlap_w(G, M[2]) """ - # FIXME: Add support for multi-column vertices + if type(vertex_pair) == cudf.DataFrame: - for col in vertex_pair.columns: - null_check(vertex_pair[col]) - if input_graph.renumbered: - vertex_pair = input_graph.add_internal_vertex_id( - vertex_pair, col, col - ) + vertex_pair = renumber_vertex_pair(input_graph, vertex_pair) elif vertex_pair is None: pass else: raise ValueError("vertex_pair must be a cudf dataframe") - df = overlap_wrapper.overlap(input_graph, weights, vertex_pair) + if input_graph.renumbered: + vertex_size = input_graph.vertex_column_size() + if vertex_size == 1: + weights = input_graph.add_internal_vertex_id( + weights, 'vertex', 'vertex' + ) + else: + cols = weights.columns[:vertex_size].to_list() + weights = input_graph.add_internal_vertex_id( + weights, 'vertex', cols + ) + + overlap_weights = cudf.Series(np.ones(len(weights))) + for i in range(len(weights)): + overlap_weights[weights['vertex'].iloc[i]] = weights['weight'].iloc[i] + + overlap_weights = overlap_weights.astype('float32') + + df = overlap_wrapper.overlap(input_graph, overlap_weights, vertex_pair) if input_graph.renumbered: df = input_graph.unrenumber(df, "source") diff --git a/python/cugraph/bsp/traversal/__init__.py b/python/cugraph/sampling/__init__.py similarity index 83% rename from python/cugraph/bsp/traversal/__init__.py rename to python/cugraph/sampling/__init__.py index 061d1d7e3a1..ab0bfab0c66 100644 --- a/python/cugraph/bsp/traversal/__init__.py +++ b/python/cugraph/sampling/__init__.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -11,5 +11,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -from cugraph.bsp.traversal.bfs_bsp import bfs_df_pregel - +from cugraph.sampling.random_walks import random_walks, rw_path diff --git a/python/cugraph/sampling/random_walks.pxd b/python/cugraph/sampling/random_walks.pxd new file mode 100644 index 00000000000..f86d6396c98 --- /dev/null +++ b/python/cugraph/sampling/random_walks.pxd @@ -0,0 +1,28 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * + +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": + cdef unique_ptr[random_walk_ret_t] call_random_walks[vertex_t, edge_t]( + const handle_t &handle, + const graph_container_t &g, + const vertex_t *ptr_d_start, + edge_t num_paths, + edge_t max_depth, + bool use_padding) except + + + cdef unique_ptr[random_walk_path_t] call_rw_paths[index_t]( + const handle_t &handle, + index_t num_paths, + const index_t* sizes) except + diff --git a/python/cugraph/sampling/random_walks.py b/python/cugraph/sampling/random_walks.py new file mode 100644 index 00000000000..fc21abd3bc4 --- /dev/null +++ b/python/cugraph/sampling/random_walks.py @@ -0,0 +1,113 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import cudf +from cugraph.sampling import random_walks_wrapper +import cugraph + + +def random_walks(G, + start_vertices, + max_depth=None, + use_padding=False): + """ + compute random walks for each nodes in 'start_vertices' + + parameters + ---------- + G : cuGraph.Graph or networkx.Graph + The graph can be either directed (DiGraph) or undirected (Graph). + Weights in the graph are ignored. + Use weight parameter if weights need to be considered + (currently not supported) + + start_vertices : int or list or cudf.Series or cudf.DataFrame + A single node or a list or a cudf.Series of nodes from which to run + the random walks. In case of multi-column vertices it should be + a cudf.DataFrame + + max_depth : int + The maximum depth of the random walks + + use_padding : bool + If True, padded paths are returned else coalesced paths are returned. + + Returns + ------- + vertex_paths : cudf.Series or cudf.DataFrame + Series containing the vertices of edges/paths in the random walk. + + edge_weight_paths: cudf.Series + Series containing the edge weights of edges represented by the + returned vertex_paths + + sizes: int + The path size in case of coalesced paths. + """ + if max_depth is None: + raise TypeError("must specify a 'max_depth'") + + G, _ = cugraph.utilities.check_nx_graph(G) + + if start_vertices is int: + start_vertices = [start_vertices] + + if isinstance(start_vertices, list): + start_vertices = cudf.Series(start_vertices) + + if G.renumbered is True: + if isinstance(start_vertices, cudf.DataFrame): + start_vertices = G.lookup_internal_vertex_id( + start_vertices, + start_vertices.columns) + else: + start_vertices = G.lookup_internal_vertex_id(start_vertices) + + vertex_set, edge_set, sizes = random_walks_wrapper.random_walks( + G, start_vertices, max_depth, use_padding) + + if G.renumbered: + df_ = cudf.DataFrame() + df_['vertex_set'] = vertex_set + df_ = G.unrenumber(df_, 'vertex_set', preserve_order=True) + vertex_set = cudf.Series(df_['vertex_set']) + + if use_padding: + edge_set_sz = (max_depth-1)*len(start_vertices) + return vertex_set, edge_set[:edge_set_sz], sizes + + vertex_set_sz = sizes.sum() + edge_set_sz = vertex_set_sz - len(start_vertices) + return vertex_set[:vertex_set_sz], edge_set[:edge_set_sz], sizes + + +def rw_path(num_paths, sizes): + """ + Retrieve more information on the obtained paths in case use_padding + is False. + + parameters + ---------- + num_paths: int + Number of paths in the random walk output. + + sizes: int + Path size returned in random walk output. + + Returns + ------- + path_data : cudf.DataFrame + Dataframe containing vetex path offsets, edge weight offsets and + edge weight sizes for each path. + """ + return random_walks_wrapper.rw_path_retrieval(num_paths, sizes) diff --git a/python/cugraph/sampling/random_walks_wrapper.pyx b/python/cugraph/sampling/random_walks_wrapper.pyx new file mode 100644 index 00000000000..688ece9595b --- /dev/null +++ b/python/cugraph/sampling/random_walks_wrapper.pyx @@ -0,0 +1,162 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from cugraph.sampling.random_walks cimport call_random_walks, call_rw_paths +#from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * +from libcpp cimport bool +from libcpp.utility cimport move +from libc.stdint cimport uintptr_t +from cugraph.structure import graph_primtypes_wrapper +import cudf +import rmm +import numpy as np +import numpy.ctypeslib as ctypeslib +from rmm._lib.device_buffer cimport DeviceBuffer +from cudf.core.buffer import Buffer +from cython.operator cimport dereference as deref + + +def random_walks(input_graph, start_vertices, max_depth, use_padding): + """ + Call random_walks + """ + # FIXME: Offsets and indices are currently hardcoded to int, but this may + # not be acceptable in the future. + numberTypeMap = {np.dtype("int32") : numberTypeEnum.int32Type, + np.dtype("int64") : numberTypeEnum.int64Type, + np.dtype("float32") : numberTypeEnum.floatType, + np.dtype("double") : numberTypeEnum.doubleType} + [src, dst] = [input_graph.edgelist.edgelist_df['src'], input_graph.edgelist.edgelist_df['dst']] + vertex_t = src.dtype + edge_t = np.dtype("int32") + weights = None + if input_graph.edgelist.weights: + weights = input_graph.edgelist.edgelist_df['weights'] + num_verts = input_graph.number_of_vertices() + num_edges = input_graph.number_of_edges(directed_edges=True) + num_partition_edges = num_edges + + if num_edges > (2**31 - 1): + edge_t = np.dtype("int64") + cdef unique_ptr[random_walk_ret_t] rw_ret_ptr + + cdef uintptr_t c_src_vertices = src.__cuda_array_interface__['data'][0] + cdef uintptr_t c_dst_vertices = dst.__cuda_array_interface__['data'][0] + cdef uintptr_t c_edge_weights = NULL + if weights is not None: + c_edge_weights = weights.__cuda_array_interface__['data'][0] + weight_t = weights.dtype + is_weighted = True + else: + weight_t = np.dtype("float32") + is_weighted = False + + is_symmetric = not input_graph.is_directed() + + # Pointers for random_walks + start_vertices = start_vertices.astype('int32') + cdef uintptr_t c_start_vertex_ptr = start_vertices.__cuda_array_interface__['data'][0] + num_paths = start_vertices.size + cdef unique_ptr[handle_t] handle_ptr + handle_ptr.reset(new handle_t()) + handle_ = handle_ptr.get() + cdef graph_container_t graph_container + populate_graph_container(graph_container, + handle_[0], + c_src_vertices, c_dst_vertices, c_edge_weights, + NULL, + NULL, + 0, + ((numberTypeMap[vertex_t])), + ((numberTypeMap[edge_t])), + ((numberTypeMap[weight_t])), + num_partition_edges, + num_verts, + num_edges, + is_weighted, + is_symmetric, + False, False) + if(vertex_t == np.dtype("int32")): + if(edge_t == np.dtype("int32")): + rw_ret_ptr = move(call_random_walks[int, int]( deref(handle_), + graph_container, + c_start_vertex_ptr, + num_paths, + max_depth, + use_padding)) + else: # (edge_t == np.dtype("int64")): + rw_ret_ptr = move(call_random_walks[int, long]( deref(handle_), + graph_container, + c_start_vertex_ptr, + num_paths, + max_depth, + use_padding)) + else: # (vertex_t == edge_t == np.dtype("int64")): + rw_ret_ptr = move(call_random_walks[long, long]( deref(handle_), + graph_container, + c_start_vertex_ptr, + num_paths, + max_depth, + use_padding)) + + + rw_ret= move(rw_ret_ptr.get()[0]) + vertex_set = DeviceBuffer.c_from_unique_ptr(move(rw_ret.d_coalesced_v_)) + edge_set = DeviceBuffer.c_from_unique_ptr(move(rw_ret.d_coalesced_w_)) + vertex_set = Buffer(vertex_set) + edge_set = Buffer(edge_set) + + set_vertex = cudf.Series(data=vertex_set, dtype=vertex_t) + set_edge = cudf.Series(data=edge_set, dtype=weight_t) + + if not use_padding: + sizes = DeviceBuffer.c_from_unique_ptr(move(rw_ret.d_sizes_)) + sizes = Buffer(sizes) + set_sizes = cudf.Series(data=sizes, dtype=edge_t) + else: + set_sizes = None + + return set_vertex, set_edge, set_sizes + + +def rw_path_retrieval(num_paths, sizes): + cdef unique_ptr[handle_t] handle_ptr + handle_ptr.reset(new handle_t()) + handle_ = handle_ptr.get() + index_t = sizes.dtype + + cdef unique_ptr[random_walk_path_t] rw_path_ptr + cdef uintptr_t c_sizes = sizes.__cuda_array_interface__['data'][0] + + if index_t == np.dtype("int32"): + rw_path_ptr = move(call_rw_paths[int](deref(handle_), + num_paths, + c_sizes)) + else: # index_t == np.dtype("int64"): + rw_path_ptr = move(call_rw_paths[long](deref(handle_), + num_paths, + c_sizes)) + + rw_path = move(rw_path_ptr.get()[0]) + vertex_offsets = DeviceBuffer.c_from_unique_ptr(move(rw_path.d_v_offsets)) + weight_sizes = DeviceBuffer.c_from_unique_ptr(move(rw_path.d_w_sizes)) + weight_offsets = DeviceBuffer.c_from_unique_ptr(move(rw_path.d_w_offsets)) + vertex_offsets = Buffer(vertex_offsets) + weight_sizes = Buffer(weight_sizes) + weight_offsets = Buffer(weight_offsets) + + df = cudf.DataFrame() + df['vertex_offsets'] = cudf.Series(data=vertex_offsets, dtype=index_t) + df['weight_sizes'] = cudf.Series(data=weight_sizes, dtype=index_t) + df['weight_offsets'] = cudf.Series(data=weight_offsets, dtype=index_t) + return df diff --git a/python/cugraph/structure/__init__.py b/python/cugraph/structure/__init__.py index 34447e80ee9..b70854d61ce 100644 --- a/python/cugraph/structure/__init__.py +++ b/python/cugraph/structure/__init__.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -11,7 +11,17 @@ # See the License for the specific language governing permissions and # limitations under the License. -from cugraph.structure.graph import Graph, DiGraph +from cugraph.structure.graph_classes import (Graph, + DiGraph, + MultiGraph, + MultiDiGraph, + BiPartiteGraph, + BiPartiteDiGraph) +from cugraph.structure.graph_classes import (is_weighted, + is_directed, + is_multigraph, + is_bipartite, + is_multipartite) from cugraph.structure.number_map import NumberMap from cugraph.structure.symmetrize import symmetrize, symmetrize_df , symmetrize_ddf from cugraph.structure.convert_matrix import (from_edgelist, diff --git a/python/cugraph/structure/convert_matrix.py b/python/cugraph/structure/convert_matrix.py index edd1c630185..5b3c375ea9d 100644 --- a/python/cugraph/structure/convert_matrix.py +++ b/python/cugraph/structure/convert_matrix.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -18,7 +18,7 @@ import cudf import dask_cudf -from cugraph.structure.graph import DiGraph, Graph +from cugraph.structure.graph_classes import DiGraph, Graph # optional dependencies used for handling different input types try: diff --git a/python/cugraph/structure/graph.py b/python/cugraph/structure/graph.py deleted file mode 100644 index 53c3a4e656c..00000000000 --- a/python/cugraph/structure/graph.py +++ /dev/null @@ -1,1527 +0,0 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from cugraph.structure import graph_primtypes_wrapper -from cugraph.structure.symmetrize import symmetrize -from cugraph.structure.number_map import NumberMap -from cugraph.dask.common.input_utils import get_local_data -import cugraph.dask.common.mg_utils as mg_utils -import cudf -import dask_cudf -import cugraph.comms.comms as Comms -import pandas as pd -import numpy as np -from cugraph.dask.structure import replication - - -def null_check(col): - if col.null_count != 0: - raise ValueError("Series contains NULL values") - - -class Graph: - class EdgeList: - def __init__(self, *args): - if len(args) == 1: - self.__from_dask_cudf(*args) - else: - self.__from_cudf(*args) - - def __from_cudf(self, source, destination, edge_attr=None): - self.edgelist_df = cudf.DataFrame() - self.edgelist_df["src"] = source - self.edgelist_df["dst"] = destination - self.weights = False - if edge_attr is not None: - self.weights = True - if type(edge_attr) is dict: - for k in edge_attr.keys(): - self.edgelist_df[k] = edge_attr[k] - else: - self.edgelist_df["weights"] = edge_attr - - def __from_dask_cudf(self, ddf): - self.edgelist_df = ddf - self.weights = False - # FIXME: Edge Attribute not handled - - class AdjList: - def __init__(self, offsets, indices, value=None): - self.offsets = offsets - self.indices = indices - self.weights = value # Should be a dataframe for multiple weights - - class transposedAdjList: - def __init__(self, offsets, indices, value=None): - Graph.AdjList.__init__(self, offsets, indices, value) - - """ - cuGraph graph class containing basic graph creation and transformation - operations. - """ - - def __init__( - self, - m_graph=None, - edge_attr=None, - symmetrized=False, - bipartite=False, - multi=False, - dynamic=False, - ): - """ - Returns - ------- - G : cuGraph.Graph. - - Examples - -------- - >>> import cuGraph - >>> G = cuGraph.Graph() - - """ - self.symmetrized = symmetrized - self.renumbered = False - self.renumber_map = None - self.bipartite = False - self.multipartite = False - self._nodes = {} - self.multi = multi - self.distributed = False - self.dynamic = dynamic - self.self_loop = False - self.edgelist = None - self.adjlist = None - self.transposedadjlist = None - self.edge_count = None - self.node_count = None - - # MG - Batch - self.batch_enabled = False - self.batch_edgelists = None - self.batch_adjlists = None - self.batch_transposed_adjlists = None - - if m_graph is not None: - if (type(self) is Graph and type(m_graph) is MultiGraph) or ( - type(self) is DiGraph and type(m_graph) is MultiDiGraph - ): - self.from_cudf_edgelist( - m_graph.edgelist.edgelist_df, - source="src", - destination="dst", - edge_attr=edge_attr, - ) - self.renumbered = m_graph.renumbered - self.renumber_map = m_graph.renumber_map - else: - msg = ( - "Graph can be initialized using MultiGraph " - "and DiGraph can be initialized using MultiDiGraph" - ) - raise Exception(msg) - # self.number_of_vertices = None - - def enable_batch(self): - client = mg_utils.get_client() - comms = Comms.get_comms() - - if client is None or comms is None: - msg = ( - "MG Batch needs a Dask Client and the " - "Communicator needs to be initialized." - ) - raise Exception(msg) - - self.batch_enabled = True - - if self.edgelist is not None: - if self.batch_edgelists is None: - self._replicate_edgelist() - - if self.adjlist is not None: - if self.batch_adjlists is None: - self._replicate_adjlist() - - if self.transposedadjlist is not None: - if self.batch_transposed_adjlists is None: - self._replicate_transposed_adjlist() - - def _replicate_edgelist(self): - client = mg_utils.get_client() - comms = Comms.get_comms() - - # FIXME: There might be a better way to control it - if client is None: - return - work_futures = replication.replicate_cudf_dataframe( - self.edgelist.edgelist_df, client=client, comms=comms - ) - - self.batch_edgelists = work_futures - - def _replicate_adjlist(self): - client = mg_utils.get_client() - comms = Comms.get_comms() - - # FIXME: There might be a better way to control it - if client is None: - return - - weights = None - offsets_futures = replication.replicate_cudf_series( - self.adjlist.offsets, client=client, comms=comms - ) - indices_futures = replication.replicate_cudf_series( - self.adjlist.indices, client=client, comms=comms - ) - - if self.adjlist.weights is not None: - weights = replication.replicate_cudf_series(self.adjlist.weights) - else: - weights = {worker: None for worker in offsets_futures} - - merged_futures = { - worker: [ - offsets_futures[worker], - indices_futures[worker], - weights[worker], - ] - for worker in offsets_futures - } - self.batch_adjlists = merged_futures - - # FIXME: Not implemented yet - def _replicate_transposed_adjlist(self): - self.batch_transposed_adjlists = True - - def clear(self): - """ - Empty this graph. This function is added for NetworkX compatibility. - """ - self.edgelist = None - self.adjlist = None - self.transposedadjlist = None - - self.batch_edgelists = None - self.batch_adjlists = None - self.batch_transposed_adjlists = None - - def add_nodes_from(self, nodes, bipartite=None, multipartite=None): - """ - Add nodes information to the Graph. - - Parameters - ---------- - nodes : list or cudf.Series - The nodes of the graph to be stored. If bipartite and multipartite - arguments are not passed, the nodes are considered to be a list of - all the nodes present in the Graph. - bipartite : str - Sets the Graph as bipartite. The nodes are stored as a set of nodes - of the partition named as bipartite argument. - multipartite : str - Sets the Graph as multipartite. The nodes are stored as a set of - nodes of the partition named as multipartite argument. - """ - if bipartite is None and multipartite is None: - self._nodes["all_nodes"] = cudf.Series(nodes) - else: - set_names = [i for i in self._nodes.keys() if i != "all_nodes"] - if multipartite is not None: - if self.bipartite: - raise Exception( - "The Graph is already set as bipartite. " - "Use bipartite option instead." - ) - self.multipartite = True - elif bipartite is not None: - if self.multipartite: - raise Exception( - "The Graph is set as multipartite. " - "Use multipartite option instead." - ) - self.bipartite = True - multipartite = bipartite - if multipartite not in set_names and len(set_names) == 2: - raise Exception( - "The Graph is set as bipartite and " - "already has two partitions initialized." - ) - self._nodes[multipartite] = cudf.Series(nodes) - - def is_bipartite(self): - """ - Checks if Graph is bipartite. This solely relies on the user call of - add_nodes_from with the bipartite parameter. This does not parse the - graph to check if it is bipartite. - """ - # TO DO: Call coloring algorithm - return self.bipartite - - def is_multipartite(self): - """ - Checks if Graph is multipartite. This solely relies on the user call - of add_nodes_from with the partition parameter. This does not parse - the graph to check if it is multipartite. - """ - # TO DO: Call coloring algorithm - return self.multipartite or self.bipartite - - def sets(self): - """ - Returns the bipartite set of nodes. This solely relies on the user's - call of add_nodes_from with the bipartite parameter. This does not - parse the graph to compute bipartite sets. If bipartite argument was - not provided during add_nodes_from(), it raise an exception that the - graph is not bipartite. - """ - # TO DO: Call coloring algorithm - set_names = [i for i in self._nodes.keys() if i != "all_nodes"] - if self.bipartite: - top = self._nodes[set_names[0]] - if len(set_names) == 2: - bottom = self._nodes[set_names[1]] - else: - bottom = cudf.Series( - set(self.nodes().values_host) - set(top.values_host) - ) - return top, bottom - else: - return {k: self._nodes[k] for k in set_names} - - def from_cudf_edgelist( - self, - input_df, - source="source", - destination="destination", - edge_attr=None, - renumber=True, - ): - """ - Initialize a graph from the edge list. It is an error to call this - method on an initialized Graph object. The passed input_df argument - wraps gdf_column objects that represent a graph using the edge list - format. source argument is source column name and destination argument - is destination column name. - - By default, renumbering is enabled to map the source and destination - vertices into an index in the range [0, V) where V is the number - of vertices. If the input vertices are a single column of integers - in the range [0, V), renumbering can be disabled and the original - external vertex ids will be used. - - If weights are present, edge_attr argument is the weights column name. - - Parameters - ---------- - input_df : cudf.DataFrame or dask_cudf.DataFrame - A DataFrame that contains edge information - If a dask_cudf.DataFrame is passed it will be reinterpreted as - a cudf.DataFrame. For the distributed path please use - from_dask_cudf_edgelist. - source : str or array-like - source column name or array of column names - destination : str or array-like - destination column name or array of column names - edge_attr : str or None - the weights column name. Default is None - renumber : bool - Indicate whether or not to renumber the source and destination - vertex IDs. Default is True. - - Examples - -------- - >>> df = cudf.read_csv('datasets/karate.csv', delimiter=' ', - >>> dtype=['int32', 'int32', 'float32'], header=None) - >>> G = cugraph.Graph() - >>> G.from_cudf_edgelist(df, source='0', destination='1', - edge_attr='2', renumber=False) - - """ - if self.edgelist is not None or self.adjlist is not None: - raise Exception("Graph already has values") - - s_col = source - d_col = destination - if not isinstance(s_col, list): - s_col = [s_col] - if not isinstance(d_col, list): - d_col = [d_col] - if not ( - set(s_col).issubset(set(input_df.columns)) - and set(d_col).issubset(set(input_df.columns)) - ): - raise Exception( - "source column names and/or destination column " - "names not found in input. Recheck the source and " - "destination parameters" - ) - - # FIXME: update for smaller GPUs - # Consolidation - if isinstance(input_df, cudf.DataFrame): - if len(input_df[source]) > 2147483100: - raise Exception( - "cudf dataFrame edge list is too big " - "to fit in a single GPU" - ) - elist = input_df - elif isinstance(input_df, dask_cudf.DataFrame): - if len(input_df[source]) > 2147483100: - raise Exception( - "dask_cudf dataFrame edge list is too big " - "to fit in a single GPU" - ) - elist = input_df.compute().reset_index(drop=True) - else: - raise Exception( - "input should be a cudf.DataFrame or " - "a dask_cudf dataFrame" - ) - - renumber_map = None - if renumber: - # FIXME: Should SG do lazy evaluation like MG? - elist, renumber_map = NumberMap.renumber( - elist, source, destination, store_transposed=False - ) - source = "src" - destination = "dst" - self.renumbered = True - self.renumber_map = renumber_map - else: - if type(source) is list and type(destination) is list: - raise Exception("set renumber to True for multi column ids") - - if (elist[source] == elist[destination]).any(): - self.self_loop = True - source_col = elist[source] - dest_col = elist[destination] - - if self.multi: - if type(edge_attr) is not list: - raise Exception("edge_attr should be a list of column names") - value_col = {} - for col_name in edge_attr: - value_col[col_name] = elist[col_name] - elif edge_attr is not None: - value_col = elist[edge_attr] - else: - value_col = None - - if not self.symmetrized and not self.multi: - if value_col is not None: - source_col, dest_col, value_col = symmetrize( - source_col, dest_col, value_col - ) - else: - source_col, dest_col = symmetrize(source_col, dest_col) - - self.edgelist = Graph.EdgeList(source_col, dest_col, value_col) - - if self.batch_enabled: - self._replicate_edgelist() - - self.renumber_map = renumber_map - - def from_pandas_edgelist( - self, - pdf, - source="source", - destination="destination", - edge_attr=None, - renumber=True, - ): - """ - Initialize a graph from the edge list. It is an error to call this - method on an initialized Graph object. Source argument is source - column name and destination argument is destination column name. - - By default, renumbering is enabled to map the source and destination - vertices into an index in the range [0, V) where V is the number - of vertices. If the input vertices are a single column of integers - in the range [0, V), renumbering can be disabled and the original - external vertex ids will be used. - - If weights are present, edge_attr argument is the weights column name. - - Parameters - ---------- - input_df : pandas.DataFrame - A DataFrame that contains edge information - source : str or array-like - source column name or array of column names - destination : str or array-like - destination column name or array of column names - edge_attr : str or None - the weights column name. Default is None - renumber : bool - Indicate whether or not to renumber the source and destination - vertex IDs. Default is True. - - Examples - -------- - >>> df = pandas.read_csv('datasets/karate.csv', delimiter=' ', - >>> dtype=['int32', 'int32', 'float32'], header=None) - >>> G = cugraph.Graph() - >>> G.from_pandas_edgelist(df, source='0', destination='1', - edge_attr='2', renumber=False) - - """ - gdf = cudf.DataFrame.from_pandas(pdf) - self.from_cudf_edgelist(gdf, source=source, destination=destination, - edge_attr=edge_attr, renumber=renumber) - - def to_pandas_edgelist(self, source='source', destination='destination'): - """ - Returns the graph edge list as a Pandas DataFrame. - - Parameters - ---------- - source : str or array-like - source column name or array of column names - destination : str or array-like - destination column name or array of column names - - Returns - ------- - df : pandas.DataFrame - """ - - gdf = self.view_edge_list() - return gdf.to_pandas() - - def from_pandas_adjacency(self, pdf): - """ - Initializes the graph from pandas adjacency matrix - """ - np_array = pdf.to_numpy() - columns = pdf.columns - self.from_numpy_array(np_array, columns) - - def to_pandas_adjacency(self): - """ - Returns the graph adjacency matrix as a Pandas DataFrame. - """ - - np_array_data = self.to_numpy_array() - pdf = pd.DataFrame(np_array_data) - if self.renumbered: - nodes = self.renumber_map.implementation.df['0'].\ - values_host.tolist() - pdf.columns = nodes - pdf.index = nodes - return pdf - - def to_numpy_array(self): - """ - Returns the graph adjacency matrix as a NumPy array. - """ - - nlen = self.number_of_nodes() - elen = self.number_of_edges() - df = self.edgelist.edgelist_df - np_array = np.full((nlen, nlen), 0.0) - for i in range(0, elen): - np_array[df['src'].iloc[i], df['dst'].iloc[i]] = df['weights'].\ - iloc[i] - return np_array - - def to_numpy_matrix(self): - """ - Returns the graph adjacency matrix as a NumPy matrix. - """ - np_array = self.to_numpy_array() - return np.asmatrix(np_array) - - def from_numpy_array(self, np_array, nodes=None): - """ - Initializes the graph from numpy array containing adjacency matrix. - """ - src, dst = np_array.nonzero() - weight = np_array[src, dst] - df = cudf.DataFrame() - if nodes is not None: - df['src'] = nodes[src] - df['dst'] = nodes[dst] - else: - df['src'] = src - df['dst'] = dst - df['weight'] = weight - self.from_cudf_edgelist(df, 'src', 'dst', edge_attr='weight') - - def from_numpy_matrix(self, np_matrix): - """ - Initializes the graph from numpy matrix containing adjacency matrix. - """ - np_array = np.asarray(np_matrix) - self.from_numpy_array(np_array) - - def from_dask_cudf_edgelist( - self, - input_ddf, - source="source", - destination="destination", - edge_attr=None, - renumber=True, - ): - """ - Initializes the distributed graph from the dask_cudf.DataFrame - edgelist. Undirected Graphs are not currently supported. - - By default, renumbering is enabled to map the source and destination - vertices into an index in the range [0, V) where V is the number - of vertices. If the input vertices are a single column of integers - in the range [0, V), renumbering can be disabled and the original - external vertex ids will be used. - - Note that the graph object will store a reference to the - dask_cudf.DataFrame provided. - - Parameters - ---------- - input_ddf : dask_cudf.DataFrame - The edgelist as a dask_cudf.DataFrame - source : str or array-like - source column name or array of column names - destination : str - destination column name or array of column names - edge_attr : str - weights column name. - renumber : bool - If source and destination indices are not in range 0 to V where V - is number of vertices, renumber argument should be True. - """ - if self.edgelist is not None or self.adjlist is not None: - raise Exception("Graph already has values") - if not isinstance(input_ddf, dask_cudf.DataFrame): - raise Exception("input should be a dask_cudf dataFrame") - if type(self) is Graph: - raise Exception("Undirected distributed graph not supported") - - s_col = source - d_col = destination - if not isinstance(s_col, list): - s_col = [s_col] - if not isinstance(d_col, list): - d_col = [d_col] - if not ( - set(s_col).issubset(set(input_ddf.columns)) - and set(d_col).issubset(set(input_ddf.columns)) - ): - raise Exception( - "source column names and/or destination column " - "names not found in input. Recheck the source " - "and destination parameters" - ) - ddf_columns = s_col + d_col - if edge_attr is not None: - if not (set([edge_attr]).issubset(set(input_ddf.columns))): - raise Exception( - "edge_attr column name not found in input." - "Recheck the edge_attr parameter") - ddf_columns = ddf_columns + [edge_attr] - input_ddf = input_ddf[ddf_columns] - - if edge_attr is not None: - input_ddf = input_ddf.rename(columns={edge_attr: 'value'}) - - # - # Keep all of the original parameters so we can lazily - # evaluate this function - # - - # FIXME: Edge Attribute not handled - self.distributed = True - self.local_data = None - self.edgelist = None - self.adjlist = None - self.renumbered = renumber - self.input_df = input_ddf - self.source_columns = source - self.destination_columns = destination - self.store_tranposed = None - - def compute_local_data(self, by, load_balance=True): - """ - Compute the local edges, vertices and offsets for a distributed - graph stored as a dask-cudf dataframe and initialize the - communicator. Performs global sorting and load_balancing. - - Parameters - ---------- - by : str - by argument is the column by which we want to sort and - partition. It should be the source column name for generating - CSR format and destination column name for generating CSC - format. - load_balance : bool - Set as True to perform load_balancing after global sorting of - dask-cudf DataFrame. This ensures that the data is uniformly - distributed among multiple GPUs to avoid over-loading. - """ - if self.distributed: - data = get_local_data(self, by, load_balance) - self.local_data = {} - self.local_data["data"] = data - self.local_data["by"] = by - else: - raise Exception("Graph should be a distributed graph") - - def view_edge_list(self): - """ - Display the edge list. Compute it if needed. - - NOTE: If the graph is of type Graph() then the displayed undirected - edges are the same as displayed by networkx Graph(), but the direction - could be different i.e. an edge displayed by cugraph as (src, dst) - could be displayed as (dst, src) by networkx. - - cugraph.Graph stores symmetrized edgelist internally. For displaying - undirected edgelist for a Graph the upper trianglar matrix of the - symmetrized edgelist is returned. - - networkx.Graph renumbers the input and stores the upper triangle of - this renumbered input. Since the internal renumbering of networx and - cugraph is different, the upper triangular matrix of networkx - renumbered input may not be the same as cugraph's upper trianglar - matrix of the symmetrized edgelist. Hence the displayed source and - destination pairs in both will represent the same edge but node values - could be swapped. - - Returns - ------- - df : cudf.DataFrame - This cudf.DataFrame wraps source, destination and weight - - df[src] : cudf.Series - contains the source index for each edge - df[dst] : cudf.Series - contains the destination index for each edge - df[weight] : cusd.Series - Column is only present for weighted Graph, - then containing the weight value for each edge - """ - if self.distributed: - if self.edgelist is None: - raise Exception("Graph has no Edgelist.") - return self.edgelist.edgelist_df - if self.edgelist is None: - src, dst, weights = graph_primtypes_wrapper.view_edge_list(self) - self.edgelist = self.EdgeList(src, dst, weights) - - edgelist_df = self.edgelist.edgelist_df - - if self.renumbered: - edgelist_df = self.unrenumber(edgelist_df, "src") - edgelist_df = self.unrenumber(edgelist_df, "dst") - - if type(self) is Graph: - edgelist_df = edgelist_df[edgelist_df["src"] <= edgelist_df["dst"]] - edgelist_df = edgelist_df.reset_index(drop=True) - self.edge_count = len(edgelist_df) - - return edgelist_df - - def delete_edge_list(self): - """ - Delete the edge list. - """ - # decrease reference count to free memory if the referenced objects are - # no longer used. - self.edgelist = None - - def from_cudf_adjlist(self, offset_col, index_col, value_col=None): - """ - Initialize a graph from the adjacency list. It is an error to call this - method on an initialized Graph object. The passed offset_col and - index_col arguments wrap gdf_column objects that represent a graph - using the adjacency list format. - If value_col is None, an unweighted graph is created. If value_col is - not None, a weighted graph is created. - Undirected edges must be stored as directed edges in both directions. - - Parameters - ---------- - offset_col : cudf.Series - This cudf.Series wraps a gdf_column of size V + 1 (V: number of - vertices). - The gdf column contains the offsets for the vertices in this graph. - Offsets must be in the range [0, E] (E: number of edges). - index_col : cudf.Series - This cudf.Series wraps a gdf_column of size E (E: number of edges). - The gdf column contains the destination index for each edge. - Destination indices must be in the range [0, V) (V: number of - vertices). - value_col : cudf.Series, optional - This pointer can be ``None``. - If not, this cudf.Series wraps a gdf_column of size E (E: number of - edges). - The gdf column contains the weight value for each edge. - The expected type of the gdf_column element is floating point - number. - - Examples - -------- - >>> gdf = cudf.read_csv('datasets/karate.csv', delimiter=' ', - >>> dtype=['int32', 'int32', 'float32'], header=None) - >>> M = gdf.to_pandas() - >>> M = scipy.sparse.coo_matrix((M['2'],(M['0'],M['1']))) - >>> M = M.tocsr() - >>> offsets = cudf.Series(M.indptr) - >>> indices = cudf.Series(M.indices) - >>> G = cugraph.Graph() - >>> G.from_cudf_adjlist(offsets, indices, None) - - """ - if self.edgelist is not None or self.adjlist is not None: - raise Exception("Graph already has values") - self.adjlist = Graph.AdjList(offset_col, index_col, value_col) - - if self.batch_enabled: - self._replicate_adjlist() - - def compute_renumber_edge_list(self, transposed=False): - """ - Compute a renumbered edge list - - This function works in the MNMG pipeline and will transform - the input dask_cudf.DataFrame into a renumbered edge list - in the prescribed direction. - - This function will be called by the algorithms to ensure - that the graph is renumbered properly. The graph object will - cache the most recent renumbering attempt. For benchmarking - purposes, this function can be called prior to calling a - graph algorithm so we can measure the cost of computing - the renumbering separately from the cost of executing the - algorithm. - - When creating a CSR-like structure, set transposed to False. - When creating a CSC-like structure, set transposed to True. - - Parameters - ---------- - transposed : (optional) bool - If True, renumber with the intent to make a CSC-like - structure. If False, renumber with the intent to make - a CSR-like structure. Defaults to False. - """ - # FIXME: What to do about edge_attr??? - # currently ignored for MNMG - - if not self.distributed: - raise Exception( - "compute_renumber_edge_list should only be used " - "for distributed graphs" - ) - - if not self.renumbered: - self.edgelist = self.EdgeList(self.input_df) - self.renumber_map = None - else: - if self.edgelist is not None: - if type(self) is Graph: - return - - if self.store_transposed == transposed: - return - - del self.edgelist - - renumbered_ddf, number_map = NumberMap.renumber( - self.input_df, - self.source_columns, - self.destination_columns, - store_transposed=transposed, - ) - self.edgelist = self.EdgeList(renumbered_ddf) - self.renumber_map = number_map - self.store_transposed = transposed - - def view_adj_list(self): - """ - Display the adjacency list. Compute it if needed. - - Returns - ------- - offset_col : cudf.Series - This cudf.Series wraps a gdf_column of size V + 1 (V: number of - vertices). - The gdf column contains the offsets for the vertices in this graph. - Offsets are in the range [0, E] (E: number of edges). - index_col : cudf.Series - This cudf.Series wraps a gdf_column of size E (E: number of edges). - The gdf column contains the destination index for each edge. - Destination indices are in the range [0, V) (V: number of - vertices). - value_col : cudf.Series or ``None`` - This pointer is ``None`` for unweighted graphs. - For weighted graphs, this cudf.Series wraps a gdf_column of size E - (E: number of edges). - The gdf column contains the weight value for each edge. - The expected type of the gdf_column element is floating point - number. - """ - if self.distributed: - raise Exception("Not supported for distributed graph") - - if self.adjlist is None: - if self.transposedadjlist is not None and type(self) is Graph: - off, ind, vals = ( - self.transposedadjlist.offsets, - self.transposedadjlist.indices, - self.transposedadjlist.weights, - ) - else: - off, ind, vals = graph_primtypes_wrapper.view_adj_list(self) - self.adjlist = self.AdjList(off, ind, vals) - - if self.batch_enabled: - self._replicate_adjlist() - - return self.adjlist.offsets, self.adjlist.indices, self.adjlist.weights - - def view_transposed_adj_list(self): - """ - Display the transposed adjacency list. Compute it if needed. - - Returns - ------- - offset_col : cudf.Series - This cudf.Series wraps a gdf_column of size V + 1 (V: number of - vertices). - The gdf column contains the offsets for the vertices in this graph. - Offsets are in the range [0, E] (E: number of edges). - index_col : cudf.Series - This cudf.Series wraps a gdf_column of size E (E: number of edges). - The gdf column contains the destination index for each edge. - Destination indices are in the range [0, V) (V: number of - vertices). - value_col : cudf.Series or ``None`` - This pointer is ``None`` for unweighted graphs. - For weighted graphs, this cudf.Series wraps a gdf_column of size E - (E: number of edges). - The gdf column contains the weight value for each edge. - The expected type of the gdf_column element is floating point - number. - - """ - if self.distributed: - raise Exception("Not supported for distributed graph") - if self.transposedadjlist is None: - if self.adjlist is not None and type(self) is Graph: - off, ind, vals = ( - self.adjlist.offsets, - self.adjlist.indices, - self.adjlist.weights, - ) - else: - ( - off, - ind, - vals, - ) = graph_primtypes_wrapper.view_transposed_adj_list(self) - self.transposedadjlist = self.transposedAdjList(off, ind, vals) - - if self.batch_enabled: - self._replicate_transposed_adjlist() - - return ( - self.transposedadjlist.offsets, - self.transposedadjlist.indices, - self.transposedadjlist.weights, - ) - - def delete_adj_list(self): - """ - Delete the adjacency list. - """ - self.adjlist = None - - def get_two_hop_neighbors(self): - """ - Compute vertex pairs that are two hops apart. The resulting pairs are - sorted before returning. - - Returns - ------- - df : cudf.DataFrame - df[first] : cudf.Series - the first vertex id of a pair, if an external vertex id - is defined by only one column - df[second] : cudf.Series - the second vertex id of a pair, if an external vertex id - is defined by only one column - """ - if self.distributed: - raise Exception("Not supported for distributed graph") - df = graph_primtypes_wrapper.get_two_hop_neighbors(self) - if self.renumbered is True: - df = self.unrenumber(df, "first") - df = self.unrenumber(df, "second") - - return df - - def number_of_vertices(self): - """ - Get the number of nodes in the graph. - - """ - if self.node_count is None: - if self.distributed: - if self.edgelist is not None: - ddf = self.edgelist.edgelist_df[["src", "dst"]] - self.node_count = ddf.max().max().compute() + 1 - else: - raise Exception("Graph is Empty") - elif self.adjlist is not None: - self.node_count = len(self.adjlist.offsets) - 1 - elif self.transposedadjlist is not None: - self.node_count = len(self.transposedadjlist.offsets) - 1 - elif self.edgelist is not None: - df = self.edgelist.edgelist_df[["src", "dst"]] - self.node_count = df.max().max() + 1 - else: - raise Exception("Graph is Empty") - return self.node_count - - def number_of_nodes(self): - """ - An alias of number_of_vertices(). This function is added for NetworkX - compatibility. - - """ - return self.number_of_vertices() - - def number_of_edges(self, directed_edges=False): - """ - Get the number of edges in the graph. - - """ - if self.distributed: - if self.edgelist is not None: - return len(self.edgelist.edgelist_df) - else: - raise ValueError("Graph is Empty") - if directed_edges and self.edgelist is not None: - return len(self.edgelist.edgelist_df) - if self.edge_count is None: - if self.edgelist is not None: - if type(self) is Graph: - self.edge_count = len( - self.edgelist.edgelist_df[ - self.edgelist.edgelist_df["src"] - >= self.edgelist.edgelist_df["dst"] - ] - ) - else: - self.edge_count = len(self.edgelist.edgelist_df) - elif self.adjlist is not None: - self.edge_count = len(self.adjlist.indices) - elif self.transposedadjlist is not None: - self.edge_count = len(self.transposedadjlist.indices) - else: - raise ValueError("Graph is Empty") - return self.edge_count - - def in_degree(self, vertex_subset=None): - """ - Compute vertex in-degree. Vertex in-degree is the number of edges - pointing into the vertex. By default, this method computes vertex - degrees for the entire set of vertices. If vertex_subset is provided, - this method optionally filters out all but those listed in - vertex_subset. - - Parameters - ---------- - vertex_subset : cudf.Series or iterable container, optional - A container of vertices for displaying corresponding in-degree. - If not set, degrees are computed for the entire set of vertices. - - Returns - ------- - df : cudf.DataFrame - GPU DataFrame of size N (the default) or the size of the given - vertices (vertex_subset) containing the in_degree. The ordering is - relative to the adjacency list, or that given by the specified - vertex_subset. - - df[vertex] : cudf.Series - The vertex IDs (will be identical to vertex_subset if - specified). - df[degree] : cudf.Series - The computed in-degree of the corresponding vertex. - - Examples - -------- - >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', - >>> dtype=['int32', 'int32', 'float32'], header=None) - >>> G = cugraph.Graph() - >>> G.from_cudf_edgelist(M, '0', '1') - >>> df = G.in_degree([0,9,12]) - - """ - return self._degree(vertex_subset, x=1) - - def out_degree(self, vertex_subset=None): - """ - Compute vertex out-degree. Vertex out-degree is the number of edges - pointing out from the vertex. By default, this method computes vertex - degrees for the entire set of vertices. If vertex_subset is provided, - this method optionally filters out all but those listed in - vertex_subset. - - Parameters - ---------- - vertex_subset : cudf.Series or iterable container, optional - A container of vertices for displaying corresponding out-degree. - If not set, degrees are computed for the entire set of vertices. - - Returns - ------- - df : cudf.DataFrame - GPU DataFrame of size N (the default) or the size of the given - vertices (vertex_subset) containing the out_degree. The ordering is - relative to the adjacency list, or that given by the specified - vertex_subset. - - df[vertex] : cudf.Series - The vertex IDs (will be identical to vertex_subset if - specified). - df[degree] : cudf.Series - The computed out-degree of the corresponding vertex. - - Examples - -------- - >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', - >>> dtype=['int32', 'int32', 'float32'], header=None) - >>> G = cugraph.Graph() - >>> G.from_cudf_edgelist(M, '0', '1') - >>> df = G.out_degree([0,9,12]) - - """ - if self.distributed: - raise Exception("Not supported for distributed graph") - return self._degree(vertex_subset, x=2) - - def degree(self, vertex_subset=None): - """ - Compute vertex degree, which is the total number of edges incident - to a vertex (both in and out edges). By default, this method computes - degrees for the entire set of vertices. If vertex_subset is provided, - then this method optionally filters out all but those listed in - vertex_subset. - - Parameters - ---------- - vertex_subset : cudf.Series or iterable container, optional - a container of vertices for displaying corresponding degree. If not - set, degrees are computed for the entire set of vertices. - - Returns - ------- - df : cudf.DataFrame - GPU DataFrame of size N (the default) or the size of the given - vertices (vertex_subset) containing the degree. The ordering is - relative to the adjacency list, or that given by the specified - vertex_subset. - - df['vertex'] : cudf.Series - The vertex IDs (will be identical to vertex_subset if - specified). - df['degree'] : cudf.Series - The computed degree of the corresponding vertex. - - Examples - -------- - >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', - >>> dtype=['int32', 'int32', 'float32'], header=None) - >>> G = cugraph.Graph() - >>> G.from_cudf_edgelist(M, '0', '1') - >>> all_df = G.degree() - >>> subset_df = G.degree([0,9,12]) - - """ - if self.distributed: - raise Exception("Not supported for distributed graph") - return self._degree(vertex_subset) - - # FIXME: vertex_subset could be a DataFrame for multi-column vertices - def degrees(self, vertex_subset=None): - """ - Compute vertex in-degree and out-degree. By default, this method - computes vertex degrees for the entire set of vertices. If - vertex_subset is provided, this method optionally filters out all but - those listed in vertex_subset. - - Parameters - ---------- - vertex_subset : cudf.Series or iterable container, optional - A container of vertices for displaying corresponding degree. If not - set, degrees are computed for the entire set of vertices. - - Returns - ------- - df : cudf.DataFrame - GPU DataFrame of size N (the default) or the size of the given - vertices (vertex_subset) containing the degrees. The ordering is - relative to the adjacency list, or that given by the specified - vertex_subset. - - df['vertex'] : cudf.Series - The vertex IDs (will be identical to vertex_subset if - specified). - df['in_degree'] : cudf.Series - The in-degree of the vertex. - df['out_degree'] : cudf.Series - The out-degree of the vertex. - - Examples - -------- - >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', - >>> dtype=['int32', 'int32', 'float32'], header=None) - >>> G = cugraph.Graph() - >>> G.from_cudf_edgelist(M, '0', '1') - >>> df = G.degrees([0,9,12]) - - """ - if self.distributed: - raise Exception("Not supported for distributed graph") - ( - vertex_col, - in_degree_col, - out_degree_col, - ) = graph_primtypes_wrapper._degrees(self) - - df = cudf.DataFrame() - df["vertex"] = vertex_col - df["in_degree"] = in_degree_col - df["out_degree"] = out_degree_col - - if self.renumbered is True: - df = self.unrenumber(df, "vertex") - - if vertex_subset is not None: - df = df[df['vertex'].isin(vertex_subset)] - - return df - - def _degree(self, vertex_subset, x=0): - vertex_col, degree_col = graph_primtypes_wrapper._degree(self, x) - df = cudf.DataFrame() - df["vertex"] = vertex_col - df["degree"] = degree_col - - if self.renumbered is True: - df = self.unrenumber(df, "vertex") - - if vertex_subset is not None: - df = df[df['vertex'].isin(vertex_subset)] - - return df - - def to_directed(self): - """ - Return a directed representation of the graph. - This function sets the type of graph as DiGraph() and returns the - directed view. - - Returns - ------- - G : DiGraph - A directed graph with the same nodes, and each edge (u,v,weights) - replaced by two directed edges (u,v,weights) and (v,u,weights). - - Examples - -------- - >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', - >>> dtype=['int32', 'int32', 'float32'], header=None) - >>> G = cugraph.Graph() - >>> G.from_cudf_edgelist(M, '0', '1') - >>> DiG = G.to_directed() - - """ - if self.distributed: - raise Exception("Not supported for distributed graph") - if type(self) is DiGraph: - return self - if type(self) is Graph: - DiG = DiGraph() - DiG.renumbered = self.renumbered - DiG.renumber_map = self.renumber_map - DiG.edgelist = self.edgelist - DiG.adjlist = self.adjlist - DiG.transposedadjlist = self.transposedadjlist - return DiG - - def to_undirected(self): - """ - Return an undirected copy of the graph. - - Returns - ------- - G : Graph - A undirected graph with the same nodes, and each directed edge - (u,v,weights) replaced by an undirected edge (u,v,weights). - - Examples - -------- - >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', - >>> dtype=['int32', 'int32', 'float32'], header=None) - >>> DiG = cugraph.DiGraph() - >>> DiG.from_cudf_edgelist(M, '0', '1') - >>> G = DiG.to_undirected() - - """ - - if type(self) is Graph: - return self - if type(self) is DiGraph: - G = Graph() - df = self.edgelist.edgelist_df - G.renumbered = self.renumbered - G.renumber_map = self.renumber_map - G.multi = self.multi - if self.edgelist.weights: - source_col, dest_col, value_col = symmetrize( - df["src"], df["dst"], df["weights"] - ) - else: - source_col, dest_col = symmetrize(df["src"], df["dst"]) - value_col = None - G.edgelist = Graph.EdgeList(source_col, dest_col, value_col) - - return G - - def is_directed(self): - if type(self) is DiGraph: - return True - else: - return False - - def has_node(self, n): - """ - Returns True if the graph contains the node n. - """ - if self.edgelist is None: - raise Exception("Graph has no Edgelist.") - if self.distributed: - ddf = self.edgelist.edgelist_df[["src", "dst"]] - return (ddf == n).any().any().compute() - if self.renumbered: - tmp = self.renumber_map.to_internal_vertex_id(cudf.Series([n])) - return tmp[0] is not cudf.NA and tmp[0] >= 0 - else: - df = self.edgelist.edgelist_df[["src", "dst"]] - return (df == n).any().any() - - def has_edge(self, u, v): - """ - Returns True if the graph contains the edge (u,v). - """ - if self.edgelist is None: - raise Exception("Graph has no Edgelist.") - if self.renumbered: - tmp = cudf.DataFrame({"src": [u, v]}) - tmp = tmp.astype({"src": "int"}) - tmp = self.add_internal_vertex_id( - tmp, "id", "src", preserve_order=True - ) - - u = tmp["id"][0] - v = tmp["id"][1] - - df = self.edgelist.edgelist_df - if self.distributed: - return ((df["src"] == u) & (df["dst"] == v)).any().compute() - return ((df["src"] == u) & (df["dst"] == v)).any() - - def edges(self): - """ - Returns all the edges in the graph as a cudf.DataFrame containing - sources and destinations. It does not return the edge weights. - For viewing edges with weights use view_edge_list() - """ - return self.view_edge_list()[["src", "dst"]] - - def nodes(self): - """ - Returns all the nodes in the graph as a cudf.Series - """ - if self.distributed: - raise Exception("Not supported for distributed graph") - if self.edgelist is not None: - df = self.edgelist.edgelist_df - if self.renumbered: - # FIXME: If vertices are multicolumn - # this needs to return a dataframe - # FIXME: This relies on current implementation - # of NumberMap, should not really expose - # this, perhaps add a method to NumberMap - return self.renumber_map.implementation.df["0"] - else: - return cudf.concat([df["src"], df["dst"]]).unique() - if "all_nodes" in self._nodes.keys(): - return self._nodes["all_nodes"] - else: - n = cudf.Series(dtype="int") - set_names = [i for i in self._nodes.keys() if i != "all_nodes"] - for k in set_names: - n = n.append(self._nodes[k]) - return n - - def neighbors(self, n): - if self.edgelist is None: - raise Exception("Graph has no Edgelist.") - if self.distributed: - ddf = self.edgelist.edgelist_df - return ddf[ddf["src"] == n]["dst"].reset_index(drop=True) - if self.renumbered: - node = self.renumber_map.to_internal_vertex_id(cudf.Series([n])) - if len(node) == 0: - return cudf.Series(dtype="int") - n = node[0] - - df = self.edgelist.edgelist_df - neighbors = df[df["src"] == n]["dst"].reset_index(drop=True) - if self.renumbered: - # FIXME: Multi-column vertices - return self.renumber_map.from_internal_vertex_id(neighbors)["0"] - else: - return neighbors - - def unrenumber(self, df, column_name, preserve_order=False): - """ - Given a DataFrame containing internal vertex ids in the identified - column, replace this with external vertex ids. If the renumbering - is from a single column, the output dataframe will use the same - name for the external vertex identifiers. If the renumbering is from - a multi-column input, the output columns will be labeled 0 through - n-1 with a suffix of _column_name. - - Note that this function does not guarantee order in single GPU mode, - and does not guarantee order or partitioning in multi-GPU mode. If you - wish to preserve ordering, add an index column to df and sort the - return by that index column. - - Parameters - ---------- - df: cudf.DataFrame or dask_cudf.DataFrame - A DataFrame containing internal vertex identifiers that will be - converted into external vertex identifiers. - - column_name: string - Name of the column containing the internal vertex id. - - preserve_order: (optional) bool - If True, preserve the order of the rows in the output - DataFrame to match the input DataFrame - - Returns - --------- - df : cudf.DataFrame or dask_cudf.DataFrame - The original DataFrame columns exist unmodified. The external - vertex identifiers are added to the DataFrame, the internal - vertex identifier column is removed from the dataframe. - """ - return self.renumber_map.unrenumber(df, column_name, preserve_order) - - def lookup_internal_vertex_id(self, df, column_name=None): - """ - Given a DataFrame containing external vertex ids in the identified - columns, or a Series containing external vertex ids, return a - Series with the internal vertex ids. - - Note that this function does not guarantee order in single GPU mode, - and does not guarantee order or partitioning in multi-GPU mode. - - Parameters - ---------- - df: cudf.DataFrame, cudf.Series, dask_cudf.DataFrame, dask_cudf.Series - A DataFrame containing external vertex identifiers that will be - converted into internal vertex identifiers. - - column_name: (optional) string - Name of the column containing the external vertex ids - - Returns - --------- - series : cudf.Series or dask_cudf.Series - The internal vertex identifiers - """ - return self.renumber_map.to_internal_vertex_id(df, column_name) - - def add_internal_vertex_id( - self, - df, - internal_column_name, - external_column_name, - drop=True, - preserve_order=False, - ): - """ - Given a DataFrame containing external vertex ids in the identified - columns, return a DataFrame containing the internal vertex ids as the - specified column name. Optionally drop the external vertex id columns. - Optionally preserve the order of the original DataFrame. - - Parameters - ---------- - df: cudf.DataFrame or dask_cudf.DataFrame - A DataFrame containing external vertex identifiers that will be - converted into internal vertex identifiers. - - internal_column_name: string - Name of column to contain the internal vertex id - - external_column_name: string or list of strings - Name of the column(s) containing the external vertex ids - - drop: (optional) bool, defaults to True - Drop the external columns from the returned DataFrame - - preserve_order: (optional) bool, defaults to False - Preserve the order of the data frame (requires an extra sort) - - Returns - --------- - df : cudf.DataFrame or dask_cudf.DataFrame - Original DataFrame with new column containing internal vertex - id - """ - return self.renumber_map.add_internal_vertex_id( - df, - internal_column_name, - external_column_name, - drop, - preserve_order, - ) - - -class DiGraph(Graph): - def __init__(self, m_graph=None, edge_attr=None): - super().__init__( - m_graph=m_graph, edge_attr=edge_attr, symmetrized=True - ) - - -class MultiGraph(Graph): - def __init__(self, renumbered=True): - super().__init__(multi=True) - - -class MultiDiGraph(Graph): - def __init__(self, renumbered=True): - super().__init__(symmetrized=True, multi=True) diff --git a/python/cugraph/structure/graph_classes.py b/python/cugraph/structure/graph_classes.py new file mode 100644 index 00000000000..0fc8b454138 --- /dev/null +++ b/python/cugraph/structure/graph_classes.py @@ -0,0 +1,790 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import numpy as np +from .graph_implementation import (simpleGraphImpl, + simpleDistributedGraphImpl, + npartiteGraphImpl) +import cudf +import warnings + + +# TODO: Move to utilities +def null_check(col): + if col.null_count != 0: + raise ValueError("Series contains NULL values") + + +class Graph: + class Properties: + def __init__(self, directed): + self.directed = directed + self.weights = False + + def __init__(self, m_graph=None, directed=False): + self._Impl = None + self.graph_properties = Graph.Properties(directed) + if m_graph is not None: + if m_graph.is_multigraph(): + elist = m_graph.view_edge_list() + if m_graph.is_weighted(): + weights = "weights" + else: + weights = None + self.from_cudf_edgelist(elist, + source="src", + destination="dst", + edge_attr=weights) + else: + msg = ( + "Graph can only be initialized using MultiGraph " + "or MultiDiGraph" + ) + raise Exception(msg) + + def __getattr__(self, name): + if self._Impl is None: + raise AttributeError(name) + if hasattr(self._Impl, name): + return getattr(self._Impl, name) + # FIXME: Remove access to Impl properties + elif hasattr(self._Impl.properties, name): + return getattr(self._Impl.properties, name) + else: + raise AttributeError(name) + + def __dir__(self): + return dir(self._Impl) + + def from_cudf_edgelist( + self, + input_df, + source="source", + destination="destination", + edge_attr=None, + renumber=True + ): + """ + Initialize a graph from the edge list. It is an error to call this + method on an initialized Graph object. The passed input_df argument + wraps gdf_column objects that represent a graph using the edge list + format. source argument is source column name and destination argument + is destination column name. + By default, renumbering is enabled to map the source and destination + vertices into an index in the range [0, V) where V is the number + of vertices. If the input vertices are a single column of integers + in the range [0, V), renumbering can be disabled and the original + external vertex ids will be used. + If weights are present, edge_attr argument is the weights column name. + + Parameters + ---------- + input_df : cudf.DataFrame or dask_cudf.DataFrame + A DataFrame that contains edge information If a dask_cudf.DataFrame is + passed it will be reinterpreted as a cudf.DataFrame. For the + distributed path please use from_dask_cudf_edgelist. + + source : str or array-like + source column name or array of column names + + destination : str or array-like + destination column name or array of column names + + edge_attr : str or None + the weights column name. Default is None + + renumber : bool + Indicate whether or not to renumber the source and destination vertex + IDs. Default is True. + + Examples + -------- + >>> df = cudf.read_csv('datasets/karate.csv', delimiter=' ', + >>> dtype=['int32', 'int32', 'float32'], header=None) + >>> G = cugraph.Graph() + >>> G.from_cudf_edgelist(df, source='0', destination='1', + edge_attr='2', renumber=False) + """ + if self._Impl is None: + self._Impl = simpleGraphImpl(self.graph_properties) + elif type(self._Impl) is not simpleGraphImpl: + raise Exception("Graph is already initialized") + elif (self._Impl.edgelist is not None or + self._Impl.adjlist is not None): + raise Exception("Graph already has values") + self._Impl._simpleGraphImpl__from_edgelist(input_df, + source=source, + destination=destination, + edge_attr=edge_attr, + renumber=renumber) + + def from_cudf_adjlist(self, offset_col, index_col, value_col=None): + """ + Initialize a graph from the adjacency list. It is an error to call this + method on an initialized Graph object. The passed offset_col and + index_col arguments wrap gdf_column objects that represent a graph + using the adjacency list format. + If value_col is None, an unweighted graph is created. If value_col is + not None, a weighted graph is created. + Undirected edges must be stored as directed edges in both directions. + Parameters + ---------- + offset_col : cudf.Series + This cudf.Series wraps a gdf_column of size V + 1 (V: number of + vertices). The gdf column contains the offsets for the vertices in + this graph. Offsets must be in the range [0, E] (E: number of edges). + + index_col : cudf.Series + This cudf.Series wraps a gdf_column of size E (E: number of edges). + The gdf column contains the destination index for each edge. + Destination indices must be in the range [0, V) + (V: number of vertices). + + value_col : cudf.Series, optional + This pointer can be ``None``. If not, this cudf.Series wraps a + gdf_column of size E (E: number of edges). The gdf column contains the + weight value for each edge. The expected type of the gdf_column + element is floating point number. + + Examples + -------- + >>> gdf = cudf.read_csv('datasets/karate.csv', delimiter=' ', + >>> dtype=['int32', 'int32', 'float32'], header=None) + >>> M = gdf.to_pandas() + >>> M = scipy.sparse.coo_matrix((M['2'],(M['0'],M['1']))) + >>> M = M.tocsr() + >>> offsets = cudf.Series(M.indptr) + >>> indices = cudf.Series(M.indices) + >>> G = cugraph.Graph() + >>> G.from_cudf_adjlist(offsets, indices, None) + """ + if self._Impl is None: + self._Impl = simpleGraphImpl(self.graph_properties) + elif type(self._Impl) is not simpleGraphImpl: + raise Exception("Graph is already initialized") + elif (self._Impl.edgelist is not None or + self._Impl.adjlist is not None): + raise Exception("Graph already has values") + self._Impl._simpleGraphImpl__from_adjlist(offset_col, + index_col, + value_col) + + def from_dask_cudf_edgelist( + self, + input_ddf, + source="source", + destination="destination", + edge_attr=None, + renumber=True, + ): + """ + Initializes the distributed graph from the dask_cudf.DataFrame + edgelist. Undirected Graphs are not currently supported. + By default, renumbering is enabled to map the source and destination + vertices into an index in the range [0, V) where V is the number + of vertices. If the input vertices are a single column of integers + in the range [0, V), renumbering can be disabled and the original + external vertex ids will be used. + Note that the graph object will store a reference to the + dask_cudf.DataFrame provided. + + Parameters + ---------- + input_ddf : dask_cudf.DataFrame + The edgelist as a dask_cudf.DataFrame + + source : str or array-like + source column name or array of column names + + destination : str + destination column name or array of column names + + edge_attr : str + weights column name. + + renumber : bool + If source and destination indices are not in range 0 to V where V is + number of vertices, renumber argument should be True. + """ + if self._Impl is None: + self._Impl = simpleDistributedGraphImpl(self.graph_properties) + elif type(self._Impl) is not simpleDistributedGraphImpl: + raise Exception("Graph is already initialized") + elif (self._Impl.edgelist is not None): + raise Exception("Graph already has values") + self._Impl._simpleDistributedGraphImpl__from_edgelist(input_ddf, + source, + destination, + edge_attr, + renumber) + + # Move to Compat Module + def from_pandas_edgelist( + self, + pdf, + source="source", + destination="destination", + edge_attr=None, + renumber=True, + ): + """ + Initialize a graph from the edge list. It is an error to call this + method on an initialized Graph object. Source argument is source + column name and destination argument is destination column name. + By default, renumbering is enabled to map the source and destination + vertices into an index in the range [0, V) where V is the number + of vertices. If the input vertices are a single column of integers + in the range [0, V), renumbering can be disabled and the original + external vertex ids will be used. + If weights are present, edge_attr argument is the weights column name. + + Parameters + ---------- + input_df : pandas.DataFrame + A DataFrame that contains edge information + + source : str or array-like + source column name or array of column names + + destination : str or array-like + destination column name or array of column names + + edge_attr : str or None + the weights column name. Default is None + + renumber : bool + Indicate whether or not to renumber the source and destination vertex + IDs. Default is True. + + Examples + -------- + >>> df = pandas.read_csv('datasets/karate.csv', delimiter=' ', + >>> dtype=['int32', 'int32', 'float32'], header=None) + >>> G = cugraph.Graph() + >>> G.from_pandas_edgelist(df, source='0', destination='1', + edge_attr='2', renumber=False) + """ + gdf = cudf.DataFrame.from_pandas(pdf) + self.from_cudf_edgelist(gdf, source=source, destination=destination, + edge_attr=edge_attr, renumber=renumber) + + def from_pandas_adjacency(self, pdf): + """ + Initializes the graph from pandas adjacency matrix + """ + np_array = pdf.to_numpy() + columns = pdf.columns + self.from_numpy_array(np_array, columns) + + def from_numpy_array(self, np_array, nodes=None): + """ + Initializes the graph from numpy array containing adjacency matrix. + """ + src, dst = np_array.nonzero() + weight = np_array[src, dst] + df = cudf.DataFrame() + if nodes is not None: + df['src'] = nodes[src] + df['dst'] = nodes[dst] + else: + df['src'] = src + df['dst'] = dst + df['weight'] = weight + self.from_cudf_edgelist(df, 'src', 'dst', edge_attr='weight') + + def from_numpy_matrix(self, np_matrix): + """ + Initializes the graph from numpy matrix containing adjacency matrix. + """ + np_array = np.asarray(np_matrix) + self.from_numpy_array(np_array) + + def unrenumber(self, df, column_name, preserve_order=False, + get_column_names=False): + """ + Given a DataFrame containing internal vertex ids in the identified + column, replace this with external vertex ids. If the renumbering + is from a single column, the output dataframe will use the same + name for the external vertex identifiers. If the renumbering is from + a multi-column input, the output columns will be labeled 0 through + n-1 with a suffix of _column_name. + Note that this function does not guarantee order in single GPU mode, + and does not guarantee order or partitioning in multi-GPU mode. If you + wish to preserve ordering, add an index column to df and sort the + return by that index column. + + Parameters + ---------- + df: cudf.DataFrame or dask_cudf.DataFrame + A DataFrame containing internal vertex identifiers that will be + converted into external vertex identifiers. + + column_name: string + Name of the column containing the internal vertex id. + + preserve_order: (optional) bool + If True, preserve the order of the rows in the output DataFrame to + match the input DataFrame + + Returns + --------- + df : cudf.DataFrame or dask_cudf.DataFrame + The original DataFrame columns exist unmodified. The external vertex + identifiers are added to the DataFrame, the internal vertex identifier + column is removed from the dataframe. + """ + return self.renumber_map.unrenumber(df, column_name, preserve_order, + get_column_names) + + def lookup_internal_vertex_id(self, df, column_name=None): + """ + Given a DataFrame containing external vertex ids in the identified + columns, or a Series containing external vertex ids, return a + Series with the internal vertex ids. + Note that this function does not guarantee order in single GPU mode, + and does not guarantee order or partitioning in multi-GPU mode. + + Parameters + ---------- + df: cudf.DataFrame, cudf.Series, dask_cudf.DataFrame, dask_cudf.Series + A DataFrame containing external vertex identifiers that will be + converted into internal vertex identifiers. + + column_name: (optional) string + Name of the column containing the external vertex ids + + Returns + --------- + series : cudf.Series or dask_cudf.Series + The internal vertex identifiers + """ + return self.renumber_map.to_internal_vertex_id(df, column_name) + + def add_internal_vertex_id( + self, + df, + internal_column_name, + external_column_name, + drop=True, + preserve_order=False, + ): + """ + Given a DataFrame containing external vertex ids in the identified + columns, return a DataFrame containing the internal vertex ids as the + specified column name. Optionally drop the external vertex id columns. + Optionally preserve the order of the original DataFrame. + Parameters + ---------- + df: cudf.DataFrame or dask_cudf.DataFrame + A DataFrame containing external vertex identifiers that will be + converted into internal vertex identifiers. + + internal_column_name: string + Name of column to contain the internal vertex id + + external_column_name: string or list of strings + Name of the column(s) containing the external vertex ids + + drop: (optional) bool, defaults to True + Drop the external columns from the returned DataFrame + + preserve_order: (optional) bool, defaults to False + Preserve the order of the data frame (requires an extra sort) + + Returns + --------- + df : cudf.DataFrame or dask_cudf.DataFrame + Original DataFrame with new column containing internal vertex + id + + """ + return self.renumber_map.add_internal_vertex_id( + df, + internal_column_name, + external_column_name, + drop, + preserve_order, + ) + + def clear(self): + """ + Empty the graph. + """ + self._Impl = None + + def is_bipartite(self): + """ + Checks if Graph is bipartite. This solely relies on the user call of + add_nodes_from with the bipartite parameter. This does not parse the + graph to check if it is bipartite. + """ + # TO DO: Call coloring algorithm + return False + + def is_multipartite(self): + """ + Checks if Graph is multipartite. This solely relies on the user call + of add_nodes_from with the partition parameter. This does not parse + the graph to check if it is multipartite. + """ + # TO DO: Call coloring algorithm + return False + + def is_multigraph(self): + """ + Returns True if the graph is a multigraph. Else returns False. + """ + # TO DO: Call coloring algorithm + return False + + def is_directed(self): + """ + Returns True if the graph is a directed graph. + Returns False if the graph is an undirected graph. + """ + return self.graph_properties.directed + + def is_renumbered(self): + """ + Returns True if the graph is renumbered. + """ + return self.properties.renumbered + + def is_weighted(self): + """ + Returns True if the graph has edge weights. + """ + return self.properties.weighted + + def has_isolated_vertices(self): + """ + Returns True if the graph has isolated vertices. + """ + return self.properties.isolated_vertices + + def to_directed(self): + """ + Return a directed representation of the graph. + This function sets the type of graph as DiGraph() and returns the + directed view. + + Returns + ------- + G : DiGraph + A directed graph with the same nodes, and each edge (u,v,weights) + replaced by two directed edges (u,v,weights) and (v,u,weights). + + Examples + -------- + >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', + >>> dtype=['int32', 'int32', 'float32'], header=None) + >>> G = cugraph.Graph() + >>> G.from_cudf_edgelist(M, '0', '1') + >>> DiG = G.to_directed() + """ + + directed_graph = type(self)() + directed_graph.graph_properties.directed = True + directed_graph._Impl = type(self._Impl)(directed_graph. + graph_properties) + self._Impl.to_directed(directed_graph._Impl) + return directed_graph + + def to_undirected(self): + """ + Return an undirected copy of the graph. + + Returns + ------- + G : Graph + A undirected graph with the same nodes, and each directed edge + (u,v,weights) replaced by an undirected edge (u,v,weights). + + Examples + -------- + >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', + >>> dtype=['int32', 'int32', 'float32'], header=None) + >>> DiG = cugraph.DiGraph() + >>> DiG.from_cudf_edgelist(M, '0', '1') + >>> G = DiG.to_undirected() + """ + + if self.graph_properties.directed is False: + undirected_graph = type(self)() + elif self.__class__.__bases__[0] == object: + undirected_graph = type(self)() + else: + undirected_graph = self.__class__.__bases__[0]() + undirected_graph._Impl = type(self._Impl)(undirected_graph. + graph_properties) + self._Impl.to_undirected(undirected_graph._Impl) + return undirected_graph + + def add_nodes_from(self, nodes): + """ + Add nodes information to the Graph. + Parameters + ---------- + nodes : list or cudf.Series + The nodes of the graph to be stored. + """ + self._Impl._nodes["all_nodes"] = cudf.Series(nodes) + + # TODO: Add function + # def properties(): + + +class DiGraph(Graph): + def __init__(self, m_graph=None): + warnings.warn( + "DiGraph is deprecated, use Graph(directed=True) instead", + DeprecationWarning + ) + super(DiGraph, self).__init__(m_graph, directed=True) + + +class MultiGraph(Graph): + def __init__(self, directed=False): + super(MultiGraph, self).__init__(directed=directed) + self.graph_properties.multi_edge = True + + def is_multigraph(self): + """ + Returns True if the graph is a multigraph. Else returns False. + """ + # TO DO: Call coloring algorithm + return True + + +class MultiDiGraph(MultiGraph): + def __init__(self): + warnings.warn( + "MultiDiGraph is deprecated,\ + use MultiGraph(directed=True) instead", + DeprecationWarning + ) + super(MultiDiGraph, self).__init__(directed=True) + + +class Tree(Graph): + def __init__(self, directed=False): + super(Tree, self).__init__(directed=directed) + self.graph_properties.tree = True + + +class NPartiteGraph(Graph): + def __init__(self, bipartite=False, directed=False): + super(NPartiteGraph, self).__init__(directed=directed) + self.graph_properties.bipartite = bipartite + self.graph_properties.multipartite = True + + def from_cudf_edgelist( + self, + input_df, + source="source", + destination="destination", + edge_attr=None, + renumber=True + ): + """ + Initialize a graph from the edge list. It is an error to call this + method on an initialized Graph object. The passed input_df argument + wraps gdf_column objects that represent a graph using the edge list + format. source argument is source column name and destination argument + is destination column name. + By default, renumbering is enabled to map the source and destination + vertices into an index in the range [0, V) where V is the number + of vertices. If the input vertices are a single column of integers + in the range [0, V), renumbering can be disabled and the original + external vertex ids will be used. + If weights are present, edge_attr argument is the weights column name. + + Parameters + ---------- + input_df : cudf.DataFrame or dask_cudf.DataFrame + A DataFrame that contains edge information. If a dask_cudf.DataFrame is + passed it will be reinterpreted as a cudf.DataFrame. For the + distributed path please use from_dask_cudf_edgelist. + + source : str or array-like + source column name or array of column names + + destination : str or array-like + destination column name or array of column names + + edge_attr : str or None + the weights column name. Default is None + + renumber : bool + Indicate whether or not to renumber the source and destination vertex + IDs. Default is True. + + Examples + -------- + >>> df = cudf.read_csv('datasets/karate.csv', delimiter=' ', + >>> dtype=['int32', 'int32', 'float32'], header=None) + >>> G = cugraph.BiPartiteGraph() + >>> G.from_cudf_edgelist(df, source='0', destination='1', + edge_attr='2', renumber=False) + """ + if self._Impl is None: + self._Impl = npartiteGraphImpl(self.graph_properties) + # API may change in future + self._Impl._npartiteGraphImpl__from_edgelist(input_df, + source=source, + destination=destination, + edge_attr=edge_attr, + renumber=renumber) + + def from_dask_cudf_edgelist( + self, + input_ddf, + source="source", + destination="destination", + edge_attr=None, + renumber=True, + ): + """ + Initializes the distributed graph from the dask_cudf.DataFrame + edgelist. Undirected Graphs are not currently supported. + By default, renumbering is enabled to map the source and destination + vertices into an index in the range [0, V) where V is the number + of vertices. If the input vertices are a single column of integers + in the range [0, V), renumbering can be disabled and the original + external vertex ids will be used. + Note that the graph object will store a reference to the + dask_cudf.DataFrame provided. + + Parameters + ---------- + input_ddf : dask_cudf.DataFrame + The edgelist as a dask_cudf.DataFrame + + source : str or array-like + source column name or array of column names + + destination : str + destination column name or array of column names + + edge_attr : str + weights column name. + + renumber : bool + If source and destination indices are not in range 0 to V where V is + number of vertices, renumber argument should be True. + """ + raise Exception("Distributed N-partite graph not supported") + + def add_nodes_from(self, nodes, bipartite=None, multipartite=None): + """ + Add nodes information to the Graph. + Parameters + ---------- + nodes : list or cudf.Series + The nodes of the graph to be stored. If bipartite and multipartite + arguments are not passed, the nodes are considered to be a list of + all the nodes present in the Graph. + bipartite : str + Sets the Graph as bipartite. The nodes are stored as a set of nodes + of the partition named as bipartite argument. + multipartite : str + Sets the Graph as multipartite. The nodes are stored as a set of + nodes of the partition named as multipartite argument. + + """ + if self._Impl is None: + self._Impl = npartiteGraphImpl(self.graph_properties) + if bipartite is None and multipartite is None: + self._Impl._nodes["all_nodes"] = cudf.Series(nodes) + else: + self._Impl.add_nodes_from(nodes, bipartite=bipartite, + multipartite=multipartite) + + def is_multipartite(self): + """ + Checks if Graph is multipartite. This solely relies on the user call + of add_nodes_from with the partition parameter and the Graph created. + This does not parse the graph to check if it is multipartite. + """ + return True + + +class BiPartiteGraph(NPartiteGraph): + def __init__(self, directed=False): + super(BiPartiteGraph, self).__init__(directed=directed, bipartite=True) + + def is_bipartite(self): + """ + Checks if Graph is bipartite. This solely relies on the user call of + add_nodes_from with the bipartite parameter and the Graph created. + This does not parse the graph to check if it is bipartite. + """ + return True + + +class BiPartiteDiGraph(BiPartiteGraph): + def __init__(self): + warnings.warn( + "BiPartiteDiGraph is deprecated,\ + use BiPartiteGraph(directed=True) instead", + DeprecationWarning + ) + super(BiPartiteDiGraph, self).__init__(directed=True) + + +class NPartiteDiGraph(NPartiteGraph): + def __init__(self): + warnings.warn( + "NPartiteDiGraph is deprecated,\ + use NPartiteGraph(directed=True) instead", + DeprecationWarning + ) + super(NPartiteGraph, self).__init__(directed=True) + + +def is_directed(G): + """ + Returns True if the graph is a directed graph. + Returns False if the graph is an undirected graph. + """ + return G.is_directed() + + +def is_multigraph(G): + """ + Returns True if the graph is a multigraph. Else returns False. + """ + return G.is_multigraph() + + +def is_multipartite(G): + """ + Checks if Graph is multipartite. This solely relies on the Graph + type. This does not parse the graph to check if it is multipartite. + """ + return G.is_multipatite() + + +def is_bipartite(G): + """ + Checks if Graph is bipartite. This solely relies on the Graph type. + This does not parse the graph to check if it is bipartite. + """ + return G.is_bipartite() + + +def is_weighted(G): + """ + Returns True if the graph has edge weights. + """ + return G.is_weighted() diff --git a/python/cugraph/structure/graph_implementation/__init__.py b/python/cugraph/structure/graph_implementation/__init__.py new file mode 100644 index 00000000000..eeef73c0f64 --- /dev/null +++ b/python/cugraph/structure/graph_implementation/__init__.py @@ -0,0 +1,17 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from .simpleGraph import simpleGraphImpl +from .simpleDistributedGraph import simpleDistributedGraphImpl +from .npartiteGraph import npartiteGraphImpl + diff --git a/python/cugraph/structure/graph_implementation/npartiteGraph.py b/python/cugraph/structure/graph_implementation/npartiteGraph.py new file mode 100644 index 00000000000..111d9f792fa --- /dev/null +++ b/python/cugraph/structure/graph_implementation/npartiteGraph.py @@ -0,0 +1,100 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from .simpleGraph import simpleGraphImpl +import cudf + + +class npartiteGraphImpl(simpleGraphImpl): + def __init__(self, properties): + super(npartiteGraphImpl, self).__init__(properties) + self.properties.bipartite = properties.bipartite + + # API may change in future + def __from_edgelist( + self, + input_df, + source="source", + destination="destination", + edge_attr=None, + renumber=True, + ): + self._simpleGraphImpl__from_edgelist( + input_df, + source=source, + destination=destination, + edge_attr=edge_attr, + renumber=renumber, + ) + + def sets(self): + """ + Returns the bipartite set of nodes. This solely relies on the user's + call of add_nodes_from with the bipartite parameter. This does not + parse the graph to compute bipartite sets. If bipartite argument was + not provided during add_nodes_from(), it raise an exception that the + graph is not bipartite. + """ + # TO DO: Call coloring algorithm + set_names = [i for i in self._nodes.keys() if i != "all_nodes"] + if self.properties.bipartite: + top = self._nodes[set_names[0]] + if len(set_names) == 2: + bottom = self._nodes[set_names[1]] + else: + bottom = cudf.Series( + set(self.nodes().values_host) - set(top.values_host) + ) + return top, bottom + else: + return {k: self._nodes[k] for k in set_names} + + # API may change in future + def add_nodes_from(self, nodes, bipartite=None, multipartite=None): + """ + Add nodes information to the Graph. + Parameters + ---------- + nodes : list or cudf.Series + The nodes of the graph to be stored. If bipartite and multipartite + arguments are not passed, the nodes are considered to be a list of + all the nodes present in the Graph. + bipartite : str + Sets the Graph as bipartite. The nodes are stored as a set of nodes + of the partition named as bipartite argument. + multipartite : str + Sets the Graph as multipartite. The nodes are stored as a set of + nodes of the partition named as multipartite argument. + """ + if bipartite is None and multipartite is None: + raise Exception("Partition not provided") + else: + set_names = [i for i in self._nodes.keys() if i != "all_nodes"] + if multipartite is not None: + if self.properties.bipartite: + raise Exception( + "The Graph is bipartite. " + "Use bipartite option instead." + ) + elif bipartite is not None: + if not self.properties.bipartite: + raise Exception( + "The Graph is set as npartite. " + "Use multipartite option instead.") + multipartite = bipartite + if multipartite not in set_names and len(set_names) == 2: + raise Exception( + "The Graph is set as bipartite and " + "already has two partitions initialized." + ) + self._nodes[multipartite] = cudf.Series(nodes) diff --git a/python/cugraph/structure/graph_implementation/simpleDistributedGraph.py b/python/cugraph/structure/graph_implementation/simpleDistributedGraph.py new file mode 100644 index 00000000000..10d46cc3fed --- /dev/null +++ b/python/cugraph/structure/graph_implementation/simpleDistributedGraph.py @@ -0,0 +1,481 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from cugraph.structure import graph_primtypes_wrapper +from cugraph.structure.graph_primtypes_wrapper import Direction +from cugraph.structure.number_map import NumberMap +import cudf +import dask_cudf + + +class simpleDistributedGraphImpl: + class EdgeList: + def __init__(self, ddf): + self.edgelist_df = ddf + self.weights = False + # FIXME: Edge Attribute not handled + + # class AdjList: + # Not Supported + + # class transposedAdjList: + # Not Supported + + class Properties: + def __init__(self, properties): + self.multi_edge = getattr(properties, 'multi_edge', False) + self.directed = properties.directed + self.renumbered = False + self.store_transposed = False + self.self_loop = None + self.isolated_vertices = None + self.node_count = None + self.edge_count = None + self.weighted = False + + def __init__(self, properties): + # Structure + self.edgelist = None + self.renumber_map = None + self.aggregate_segment_offsets = None + self.properties = simpleDistributedGraphImpl.Properties(properties) + self.source_columns = None + self.destination_columns = None + + # Functions + def __from_edgelist( + self, + input_ddf, + source="source", + destination="destination", + edge_attr=None, + renumber=True, + store_transposed=False, + ): + if not isinstance(input_ddf, dask_cudf.DataFrame): + raise Exception("input should be a dask_cudf dataFrame") + if self.properties.directed is False: + raise Exception("Undirected distributed graph not supported") + + s_col = source + d_col = destination + if not isinstance(s_col, list): + s_col = [s_col] + if not isinstance(d_col, list): + d_col = [d_col] + if not ( + set(s_col).issubset(set(input_ddf.columns)) + and set(d_col).issubset(set(input_ddf.columns)) + ): + raise Exception( + "source column names and/or destination column " + "names not found in input. Recheck the source " + "and destination parameters" + ) + ddf_columns = s_col + d_col + if edge_attr is not None: + if not (set([edge_attr]).issubset(set(input_ddf.columns))): + raise Exception( + "edge_attr column name not found in input." + "Recheck the edge_attr parameter") + self.weighted = True + ddf_columns = ddf_columns + [edge_attr] + input_ddf = input_ddf[ddf_columns] + + if edge_attr is not None: + input_ddf = input_ddf.rename(columns={edge_attr: 'value'}) + + # + # Keep all of the original parameters so we can lazily + # evaluate this function + # + + # FIXME: Edge Attribute not handled + self.properties.renumbered = renumber + self.input_df = input_ddf + self.source_columns = source + self.destination_columns = destination + + def view_edge_list(self): + """ + Display the edge list. Compute it if needed. + NOTE: If the graph is of type Graph() then the displayed undirected + edges are the same as displayed by networkx Graph(), but the direction + could be different i.e. an edge displayed by cugraph as (src, dst) + could be displayed as (dst, src) by networkx. + cugraph.Graph stores symmetrized edgelist internally. For displaying + undirected edgelist for a Graph the upper trianglar matrix of the + symmetrized edgelist is returned. + networkx.Graph renumbers the input and stores the upper triangle of + this renumbered input. Since the internal renumbering of networx and + cugraph is different, the upper triangular matrix of networkx + renumbered input may not be the same as cugraph's upper trianglar + matrix of the symmetrized edgelist. Hence the displayed source and + destination pairs in both will represent the same edge but node values + could be swapped. + Returns + ------- + df : cudf.DataFrame + This cudf.DataFrame wraps source, destination and weight + df[src] : cudf.Series + contains the source index for each edge + df[dst] : cudf.Series + contains the destination index for each edge + df[weight] : cusd.Series + Column is only present for weighted Graph, + then containing the weight value for each edge + """ + if self.edgelist is None: + raise Exception("Graph has no Edgelist.") + return self.edgelist.edgelist_df + + def delete_edge_list(self): + """ + Delete the edge list. + """ + # decrease reference count to free memory if the referenced objects are + # no longer used. + self.edgelist = None + + def clear(self): + """ + Empty this graph. This function is added for NetworkX compatibility. + """ + self.edgelist = None + + def number_of_vertices(self): + """ + Get the number of nodes in the graph. + """ + if self.properties.node_count is None: + if self.edgelist is not None: + ddf = self.edgelist.edgelist_df[["src", "dst"]] + self.properties.node_count = ddf.max().max().compute() + 1 + else: + raise Exception("Graph is Empty") + return self.properties.node_count + + def number_of_nodes(self): + """ + An alias of number_of_vertices(). This function is added for NetworkX + compatibility. + """ + return self.number_of_vertices() + + def number_of_edges(self, directed_edges=False): + """ + Get the number of edges in the graph. + """ + if self.edgelist is not None: + return len(self.edgelist.edgelist_df) + else: + raise Exception("Graph is Empty") + + def in_degree(self, vertex_subset=None): + """ + Compute vertex in-degree. Vertex in-degree is the number of edges + pointing into the vertex. By default, this method computes vertex + degrees for the entire set of vertices. If vertex_subset is provided, + this method optionally filters out all but those listed in + vertex_subset. + Parameters + ---------- + vertex_subset : cudf.Series or iterable container, optional + A container of vertices for displaying corresponding in-degree. + If not set, degrees are computed for the entire set of vertices. + Returns + ------- + df : cudf.DataFrame + GPU DataFrame of size N (the default) or the size of the given + vertices (vertex_subset) containing the in_degree. The ordering is + relative to the adjacency list, or that given by the specified + vertex_subset. + df[vertex] : cudf.Series + The vertex IDs (will be identical to vertex_subset if + specified). + df[degree] : cudf.Series + The computed in-degree of the corresponding vertex. + Examples + -------- + >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', + >>> dtype=['int32', 'int32', 'float32'], header=None) + >>> G = cugraph.Graph() + >>> G.from_cudf_edgelist(M, '0', '1') + >>> df = G.in_degree([0,9,12]) + """ + return self._degree(vertex_subset, direction=Direction.IN) + + def out_degree(self, vertex_subset=None): + """ + Compute vertex out-degree. Vertex out-degree is the number of edges + pointing out from the vertex. By default, this method computes vertex + degrees for the entire set of vertices. If vertex_subset is provided, + this method optionally filters out all but those listed in + vertex_subset. + Parameters + ---------- + vertex_subset : cudf.Series or iterable container, optional + A container of vertices for displaying corresponding out-degree. + If not set, degrees are computed for the entire set of vertices. + Returns + ------- + df : cudf.DataFrame + GPU DataFrame of size N (the default) or the size of the given + vertices (vertex_subset) containing the out_degree. The ordering is + relative to the adjacency list, or that given by the specified + vertex_subset. + df[vertex] : cudf.Series + The vertex IDs (will be identical to vertex_subset if + specified). + df[degree] : cudf.Series + The computed out-degree of the corresponding vertex. + Examples + -------- + >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', + >>> dtype=['int32', 'int32', 'float32'], header=None) + >>> G = cugraph.Graph() + >>> G.from_cudf_edgelist(M, '0', '1') + >>> df = G.out_degree([0,9,12]) + """ + return self._degree(vertex_subset, direction=Direction.OUT) + + def degree(self, vertex_subset=None): + """ + Compute vertex degree, which is the total number of edges incident + to a vertex (both in and out edges). By default, this method computes + degrees for the entire set of vertices. If vertex_subset is provided, + then this method optionally filters out all but those listed in + vertex_subset. + Parameters + ---------- + vertex_subset : cudf.Series or iterable container, optional + a container of vertices for displaying corresponding degree. If not + set, degrees are computed for the entire set of vertices. + Returns + ------- + df : cudf.DataFrame + GPU DataFrame of size N (the default) or the size of the given + vertices (vertex_subset) containing the degree. The ordering is + relative to the adjacency list, or that given by the specified + vertex_subset. + df['vertex'] : cudf.Series + The vertex IDs (will be identical to vertex_subset if + specified). + df['degree'] : cudf.Series + The computed degree of the corresponding vertex. + Examples + -------- + >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', + >>> dtype=['int32', 'int32', 'float32'], header=None) + >>> G = cugraph.Graph() + >>> G.from_cudf_edgelist(M, '0', '1') + >>> all_df = G.degree() + >>> subset_df = G.degree([0,9,12]) + """ + raise Exception("Not supported for distributed graph") + + # FIXME: vertex_subset could be a DataFrame for multi-column vertices + def degrees(self, vertex_subset=None): + """ + Compute vertex in-degree and out-degree. By default, this method + computes vertex degrees for the entire set of vertices. If + vertex_subset is provided, this method optionally filters out all but + those listed in vertex_subset. + Parameters + ---------- + vertex_subset : cudf.Series or iterable container, optional + A container of vertices for displaying corresponding degree. If not + set, degrees are computed for the entire set of vertices. + Returns + ------- + df : cudf.DataFrame + GPU DataFrame of size N (the default) or the size of the given + vertices (vertex_subset) containing the degrees. The ordering is + relative to the adjacency list, or that given by the specified + vertex_subset. + df['vertex'] : cudf.Series + The vertex IDs (will be identical to vertex_subset if + specified). + df['in_degree'] : cudf.Series + The in-degree of the vertex. + df['out_degree'] : cudf.Series + The out-degree of the vertex. + Examples + -------- + >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', + >>> dtype=['int32', 'int32', 'float32'], header=None) + >>> G = cugraph.Graph() + >>> G.from_cudf_edgelist(M, '0', '1') + >>> df = G.degrees([0,9,12]) + """ + raise Exception("Not supported for distributed graph") + + def _degree(self, vertex_subset, direction=Direction.ALL): + vertex_col, degree_col = graph_primtypes_wrapper._mg_degree(self, + direction) + df = cudf.DataFrame() + df["vertex"] = vertex_col + df["degree"] = degree_col + + if self.properties.renumbered is True: + df = self.renumber_map.unrenumber(df, "vertex") + + if vertex_subset is not None: + df = df[df['vertex'].isin(vertex_subset)] + + return df + + def to_directed(self, DiG): + """ + Return a directed representation of the graph. + This function sets the type of graph as DiGraph() and returns the + directed view. + Returns + ------- + G : DiGraph + A directed graph with the same nodes, and each edge (u,v,weights) + replaced by two directed edges (u,v,weights) and (v,u,weights). + Examples + -------- + >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', + >>> dtype=['int32', 'int32', 'float32'], header=None) + >>> G = cugraph.Graph() + >>> G.from_cudf_edgelist(M, '0', '1') + >>> DiG = G.to_directed() + """ + # TODO: Add support + raise Exception("Not supported for distributed graph") + + def to_undirected(self, G): + """ + Return an undirected copy of the graph. + Returns + ------- + G : Graph + A undirected graph with the same nodes, and each directed edge + (u,v,weights) replaced by an undirected edge (u,v,weights). + Examples + -------- + >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', + >>> dtype=['int32', 'int32', 'float32'], header=None) + >>> DiG = cugraph.DiGraph() + >>> DiG.from_cudf_edgelist(M, '0', '1') + >>> G = DiG.to_undirected() + """ + + # TODO: Add support + raise Exception("Not supported for distributed graph") + + def has_node(self, n): + """ + Returns True if the graph contains the node n. + """ + if self.edgelist is None: + raise Exception("Graph has no Edgelist.") + # FIXME: Check renumber map + ddf = self.edgelist.edgelist_df[["src", "dst"]] + return (ddf == n).any().any().compute() + + def has_edge(self, u, v): + """ + Returns True if the graph contains the edge (u,v). + """ + # TODO: Verify Correctness + if self.properties.renumbered: + tmp = cudf.DataFrame({"src": [u, v]}) + tmp = tmp.astype({"src": "int"}) + tmp = self.add_internal_vertex_id( + tmp, "id", "src", preserve_order=True + ) + + u = tmp["id"][0] + v = tmp["id"][1] + + df = self.edgelist.edgelist_df + return ((df["src"] == u) & (df["dst"] == v)).any().compute() + + def edges(self): + """ + Returns all the edges in the graph as a cudf.DataFrame containing + sources and destinations. It does not return the edge weights. + For viewing edges with weights use view_edge_list() + """ + return self.view_edge_list()[["src", "dst"]] + + def nodes(self): + """ + Returns all the nodes in the graph as a cudf.Series + """ + # FIXME: Return renumber map nodes + raise Exception("Not supported for distributed graph") + + def neighbors(self, n): + if self.edgelist is None: + raise Exception("Graph has no Edgelist.") + # FIXME: Add renumbering of node n + ddf = self.edgelist.edgelist_df + return ddf[ddf["src"] == n]["dst"].reset_index(drop=True) + + def compute_renumber_edge_list(self, transposed=False): + """ + Compute a renumbered edge list + This function works in the MNMG pipeline and will transform + the input dask_cudf.DataFrame into a renumbered edge list + in the prescribed direction. + This function will be called by the algorithms to ensure + that the graph is renumbered properly. The graph object will + cache the most recent renumbering attempt. For benchmarking + purposes, this function can be called prior to calling a + graph algorithm so we can measure the cost of computing + the renumbering separately from the cost of executing the + algorithm. + When creating a CSR-like structure, set transposed to False. + When creating a CSC-like structure, set transposed to True. + Parameters + ---------- + transposed : (optional) bool + If True, renumber with the intent to make a CSC-like + structure. If False, renumber with the intent to make + a CSR-like structure. Defaults to False. + """ + # FIXME: What to do about edge_attr??? + # currently ignored for MNMG + + if not self.properties.renumbered: + self.edgelist = self.EdgeList(self.input_df) + self.renumber_map = None + else: + if self.edgelist is not None: + if self.properties.directed is False: + return + + if self.properties.store_transposed == transposed: + return + + del self.edgelist + + renumbered_ddf, number_map, aggregate_segment_offsets = \ + NumberMap.renumber_and_segment(self.input_df, + self.source_columns, + self.destination_columns, + store_transposed=transposed) + self.edgelist = self.EdgeList(renumbered_ddf) + self.renumber_map = number_map + self.aggregate_segment_offsets = aggregate_segment_offsets + self.properties.store_transposed = transposed + + def vertex_column_size(self): + if self.properties.renumbered: + return self.renumber_map.vertex_column_size() + else: + return 1 diff --git a/python/cugraph/structure/graph_implementation/simpleGraph.py b/python/cugraph/structure/graph_implementation/simpleGraph.py new file mode 100644 index 00000000000..e74b04c00b5 --- /dev/null +++ b/python/cugraph/structure/graph_implementation/simpleGraph.py @@ -0,0 +1,831 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from cugraph.structure import graph_primtypes_wrapper +from cugraph.structure.graph_primtypes_wrapper import Direction +from cugraph.structure.symmetrize import symmetrize +from cugraph.structure.number_map import NumberMap +import cugraph.dask.common.mg_utils as mg_utils +import cudf +import dask_cudf +import cugraph.comms.comms as Comms +import pandas as pd +import numpy as np +from cugraph.dask.structure import replication + + +# FIXME: Change to consistent camel case naming +class simpleGraphImpl: + + class EdgeList: + def __init__(self, source, destination, edge_attr=None): + self.edgelist_df = cudf.DataFrame() + self.edgelist_df["src"] = source + self.edgelist_df["dst"] = destination + self.weights = False + if edge_attr is not None: + self.weights = True + if type(edge_attr) is dict: + for k in edge_attr.keys(): + self.edgelist_df[k] = edge_attr[k] + else: + self.edgelist_df["weights"] = edge_attr + + class AdjList: + def __init__(self, offsets, indices, value=None): + self.offsets = offsets + self.indices = indices + self.weights = value # Should be a dataframe for multiple weights + + class transposedAdjList: + def __init__(self, offsets, indices, value=None): + simpleGraphImpl.AdjList.__init__(self, offsets, indices, value) + + class Properties: + def __init__(self, properties): + self.multi_edge = getattr(properties, 'multi_edge', False) + self.directed = properties.directed + self.renumbered = False + self.self_loop = None + self.isolated_vertices = None + self.node_count = None + self.edge_count = None + self.weighted = False + + def __init__(self, properties): + # Structure + self.edgelist = None + self.adjlist = None + self.transposedadjlist = None + self.renumber_map = None + self.properties = simpleGraphImpl.Properties(properties) + self._nodes = {} + + # TODO: Move to new batch class + # MG - Batch + self.batch_enabled = False + self.batch_edgelists = None + self.batch_adjlists = None + self.batch_transposed_adjlists = None + + # Functions + # FIXME: Change to public function + # FIXME: Make function more modular + def __from_edgelist( + self, + input_df, + source="source", + destination="destination", + edge_attr=None, + renumber=True, + ): + + # Verify column names present in input DataFrame + s_col = source + d_col = destination + if not isinstance(s_col, list): + s_col = [s_col] + if not isinstance(d_col, list): + d_col = [d_col] + if not ( + set(s_col).issubset(set(input_df.columns)) + and set(d_col).issubset(set(input_df.columns)) + ): + # FIXME: Raise concrete Exceptions + raise Exception( + "source column names and/or destination column " + "names not found in input. Recheck the source and " + "destination parameters" + ) + + # FIXME: check if the consolidated graph fits on the + # device before gathering all the edge lists + + # Consolidation + if isinstance(input_df, cudf.DataFrame): + if len(input_df[source]) > 2147483100: + raise Exception( + "cudf dataFrame edge list is too big " + "to fit in a single GPU" + ) + elist = input_df + elif isinstance(input_df, dask_cudf.DataFrame): + if len(input_df[source]) > 2147483100: + raise Exception( + "dask_cudf dataFrame edge list is too big " + "to fit in a single GPU" + ) + elist = input_df.compute().reset_index(drop=True) + else: + raise Exception( + "input should be a cudf.DataFrame or " + "a dask_cudf dataFrame" + ) + + # Renumbering + self.renumber_map = None + if renumber: + # FIXME: Should SG do lazy evaluation like MG? + elist, renumber_map = NumberMap.renumber( + elist, source, destination, store_transposed=False + ) + source = "src" + destination = "dst" + self.properties.renumbered = True + self.renumber_map = renumber_map + else: + if type(source) is list and type(destination) is list: + raise Exception("set renumber to True for multi column ids") + + # Populate graph edgelist + source_col = elist[source] + dest_col = elist[destination] + + if edge_attr is not None: + self.weighted = True + value_col = elist[edge_attr] + else: + value_col = None + + # TODO: Update Symmetrize to work on Graph and/or DataFrame + if value_col is not None: + source_col, dest_col, value_col = symmetrize( + source_col, dest_col, value_col, + multi=self.properties.multi_edge, + symmetrize=not self.properties.directed) + if isinstance(value_col, cudf.DataFrame): + value_dict = {} + for i in value_col.columns: + value_dict[i] = value_col[i] + value_col = value_dict + else: + source_col, dest_col = symmetrize( + source_col, dest_col, multi=self.properties.multi_edge, + symmetrize=not self.properties.directed) + + self.edgelist = simpleGraphImpl.EdgeList(source_col, dest_col, + value_col) + + if self.batch_enabled: + self._replicate_edgelist() + + def to_pandas_edgelist(self, source='source', destination='destination'): + """ + Returns the graph edge list as a Pandas DataFrame. + Parameters + ---------- + source : str or array-like + source column name or array of column names + destination : str or array-like + destination column name or array of column names + Returns + ------- + df : pandas.DataFrame + """ + + gdf = self.view_edge_list() + return gdf.to_pandas() + + def to_pandas_adjacency(self): + """ + Returns the graph adjacency matrix as a Pandas DataFrame. + """ + + np_array_data = self.to_numpy_array() + pdf = pd.DataFrame(np_array_data) + if self.properties.renumbered: + nodes = self.renumber_map.implementation.df['0'].\ + values_host.tolist() + pdf.columns = nodes + pdf.index = nodes + return pdf + + def to_numpy_array(self): + """ + Returns the graph adjacency matrix as a NumPy array. + """ + + nlen = self.number_of_nodes() + elen = self.number_of_edges() + df = self.edgelist.edgelist_df + np_array = np.full((nlen, nlen), 0.0) + for i in range(0, elen): + np_array[df['src'].iloc[i], df['dst'].iloc[i]] = df['weights'].\ + iloc[i] + return np_array + + def to_numpy_matrix(self): + """ + Returns the graph adjacency matrix as a NumPy matrix. + """ + np_array = self.to_numpy_array() + return np.asmatrix(np_array) + + def view_edge_list(self): + """ + Display the edge list. Compute it if needed. + NOTE: If the graph is of type Graph() then the displayed undirected + edges are the same as displayed by networkx Graph(), but the direction + could be different i.e. an edge displayed by cugraph as (src, dst) + could be displayed as (dst, src) by networkx. + cugraph.Graph stores symmetrized edgelist internally. For displaying + undirected edgelist for a Graph the upper trianglar matrix of the + symmetrized edgelist is returned. + networkx.Graph renumbers the input and stores the upper triangle of + this renumbered input. Since the internal renumbering of networx and + cugraph is different, the upper triangular matrix of networkx + renumbered input may not be the same as cugraph's upper trianglar + matrix of the symmetrized edgelist. Hence the displayed source and + destination pairs in both will represent the same edge but node values + could be swapped. + Returns + ------- + df : cudf.DataFrame + This cudf.DataFrame wraps source, destination and weight + df[src] : cudf.Series + contains the source index for each edge + df[dst] : cudf.Series + contains the destination index for each edge + df[weight] : cusd.Series + Column is only present for weighted Graph, + then containing the weight value for each edge + """ + if self.edgelist is None: + src, dst, weights = graph_primtypes_wrapper.view_edge_list(self) + self.edgelist = self.EdgeList(src, dst, weights) + + edgelist_df = self.edgelist.edgelist_df + + if self.properties.renumbered: + edgelist_df = self.renumber_map.unrenumber(edgelist_df, "src") + edgelist_df = self.renumber_map.unrenumber(edgelist_df, "dst") + + if not self.properties.directed: + edgelist_df = edgelist_df[edgelist_df["src"] <= edgelist_df["dst"]] + edgelist_df = edgelist_df.reset_index(drop=True) + self.properties.edge_count = len(edgelist_df) + + return edgelist_df + + def delete_edge_list(self): + """ + Delete the edge list. + """ + # decrease reference count to free memory if the referenced objects are + # no longer used. + self.edgelist = None + + def __from_adjlist(self, offset_col, index_col, value_col=None): + self.adjlist = simpleGraphImpl.AdjList(offset_col, index_col, + value_col) + + if self.batch_enabled: + self._replicate_adjlist() + + def view_adj_list(self): + """ + Display the adjacency list. Compute it if needed. + Returns + ------- + offset_col : cudf.Series + This cudf.Series wraps a gdf_column of size V + 1 (V: number of + vertices). + The gdf column contains the offsets for the vertices in this graph. + Offsets are in the range [0, E] (E: number of edges). + index_col : cudf.Series + This cudf.Series wraps a gdf_column of size E (E: number of edges). + The gdf column contains the destination index for each edge. + Destination indices are in the range [0, V) (V: number of + vertices). + value_col : cudf.Series or ``None`` + This pointer is ``None`` for unweighted graphs. + For weighted graphs, this cudf.Series wraps a gdf_column of size E + (E: number of edges). + The gdf column contains the weight value for each edge. + The expected type of the gdf_column element is floating point + number. + """ + + if self.adjlist is None: + if self.transposedadjlist is not None and\ + self.properties.directed is False: + off, ind, vals = ( + self.transposedadjlist.offsets, + self.transposedadjlist.indices, + self.transposedadjlist.weights, + ) + else: + off, ind, vals = graph_primtypes_wrapper.view_adj_list(self) + self.adjlist = self.AdjList(off, ind, vals) + + if self.batch_enabled: + self._replicate_adjlist() + + return self.adjlist.offsets, self.adjlist.indices, self.adjlist.weights + + def view_transposed_adj_list(self): + """ + Display the transposed adjacency list. Compute it if needed. + Returns + ------- + offset_col : cudf.Series + This cudf.Series wraps a gdf_column of size V + 1 (V: number of + vertices). + The gdf column contains the offsets for the vertices in this graph. + Offsets are in the range [0, E] (E: number of edges). + index_col : cudf.Series + This cudf.Series wraps a gdf_column of size E (E: number of edges). + The gdf column contains the destination index for each edge. + Destination indices are in the range [0, V) (V: number of + vertices). + value_col : cudf.Series or ``None`` + This pointer is ``None`` for unweighted graphs. + For weighted graphs, this cudf.Series wraps a gdf_column of size E + (E: number of edges). + The gdf column contains the weight value for each edge. + The expected type of the gdf_column element is floating point + number. + """ + + if self.transposedadjlist is None: + if self.adjlist is not None and self.properties.directed is False: + off, ind, vals = ( + self.adjlist.offsets, + self.adjlist.indices, + self.adjlist.weights, + ) + else: + ( + off, + ind, + vals, + ) = graph_primtypes_wrapper.view_transposed_adj_list(self) + self.transposedadjlist = self.transposedAdjList(off, ind, vals) + + if self.batch_enabled: + self._replicate_transposed_adjlist() + + return ( + self.transposedadjlist.offsets, + self.transposedadjlist.indices, + self.transposedadjlist.weights, + ) + + def delete_adj_list(self): + """ + Delete the adjacency list. + """ + self.adjlist = None + + # FIXME: Update batch workflow and refactor to suitable file + def enable_batch(self): + client = mg_utils.get_client() + comms = Comms.get_comms() + + if client is None or comms is None: + msg = ( + "MG Batch needs a Dask Client and the " + "Communicator needs to be initialized." + ) + raise Exception(msg) + + self.batch_enabled = True + + if self.edgelist is not None: + if self.batch_edgelists is None: + self._replicate_edgelist() + + if self.adjlist is not None: + if self.batch_adjlists is None: + self._replicate_adjlist() + + if self.transposedadjlist is not None: + if self.batch_transposed_adjlists is None: + self._replicate_transposed_adjlist() + + def _replicate_edgelist(self): + client = mg_utils.get_client() + comms = Comms.get_comms() + + # FIXME: There might be a better way to control it + if client is None: + return + work_futures = replication.replicate_cudf_dataframe( + self.edgelist.edgelist_df, client=client, comms=comms + ) + + self.batch_edgelists = work_futures + + def _replicate_adjlist(self): + client = mg_utils.get_client() + comms = Comms.get_comms() + + # FIXME: There might be a better way to control it + if client is None: + return + + weights = None + offsets_futures = replication.replicate_cudf_series( + self.adjlist.offsets, client=client, comms=comms + ) + indices_futures = replication.replicate_cudf_series( + self.adjlist.indices, client=client, comms=comms + ) + + if self.adjlist.weights is not None: + weights = replication.replicate_cudf_series(self.adjlist.weights) + else: + weights = {worker: None for worker in offsets_futures} + + merged_futures = { + worker: [ + offsets_futures[worker], + indices_futures[worker], + weights[worker], + ] + for worker in offsets_futures + } + self.batch_adjlists = merged_futures + + # FIXME: Not implemented yet + def _replicate_transposed_adjlist(self): + self.batch_transposed_adjlists = True + + def get_two_hop_neighbors(self): + """ + Compute vertex pairs that are two hops apart. The resulting pairs are + sorted before returning. + Returns + ------- + df : cudf.DataFrame + df[first] : cudf.Series + the first vertex id of a pair, if an external vertex id + is defined by only one column + df[second] : cudf.Series + the second vertex id of a pair, if an external vertex id + is defined by only one column + """ + + df = graph_primtypes_wrapper.get_two_hop_neighbors(self) + + if self.properties.renumbered is True: + df = self.renumber_map.unrenumber(df, "first") + df = self.renumber_map.unrenumber(df, "second") + + return df + + def number_of_vertices(self): + """ + Get the number of nodes in the graph. + """ + if self.properties.node_count is None: + if self.adjlist is not None: + self.properties.node_count = len(self.adjlist.offsets) - 1 + elif self.transposedadjlist is not None: + self.properties.node_count = len( + self.transposedadjlist.offsets) - 1 + elif self.edgelist is not None: + df = self.edgelist.edgelist_df[["src", "dst"]] + self.properties.node_count = df.max().max() + 1 + else: + raise Exception("Graph is Empty") + return self.properties.node_count + + def number_of_nodes(self): + """ + An alias of number_of_vertices(). This function is added for NetworkX + compatibility. + """ + return self.number_of_vertices() + + def number_of_edges(self, directed_edges=False): + """ + Get the number of edges in the graph. + """ + # TODO: Move to Outer graphs? + if directed_edges and self.edgelist is not None: + return len(self.edgelist.edgelist_df) + if self.properties.edge_count is None: + if self.edgelist is not None: + if self.properties.directed is False: + self.properties.edge_count = len( + self.edgelist.edgelist_df[ + self.edgelist.edgelist_df["src"] + >= self.edgelist.edgelist_df["dst"] + ] + ) + else: + self.properties.edge_count = len(self.edgelist.edgelist_df) + elif self.adjlist is not None: + self.properties.edge_count = len(self.adjlist.indices) + elif self.transposedadjlist is not None: + self.properties.edge_count = len( + self.transposedadjlist.indices) + else: + raise ValueError("Graph is Empty") + return self.properties.edge_count + + def in_degree(self, vertex_subset=None): + """ + Compute vertex in-degree. Vertex in-degree is the number of edges + pointing into the vertex. By default, this method computes vertex + degrees for the entire set of vertices. If vertex_subset is provided, + this method optionally filters out all but those listed in + vertex_subset. + Parameters + ---------- + vertex_subset : cudf.Series or iterable container, optional + A container of vertices for displaying corresponding in-degree. + If not set, degrees are computed for the entire set of vertices. + Returns + ------- + df : cudf.DataFrame + GPU DataFrame of size N (the default) or the size of the given + vertices (vertex_subset) containing the in_degree. The ordering is + relative to the adjacency list, or that given by the specified + vertex_subset. + df[vertex] : cudf.Series + The vertex IDs (will be identical to vertex_subset if + specified). + df[degree] : cudf.Series + The computed in-degree of the corresponding vertex. + Examples + -------- + >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', + >>> dtype=['int32', 'int32', 'float32'], header=None) + >>> G = cugraph.Graph() + >>> G.from_cudf_edgelist(M, '0', '1') + >>> df = G.in_degree([0,9,12]) + """ + return self._degree(vertex_subset, direction=Direction.IN) + + def out_degree(self, vertex_subset=None): + """ + Compute vertex out-degree. Vertex out-degree is the number of edges + pointing out from the vertex. By default, this method computes vertex + degrees for the entire set of vertices. If vertex_subset is provided, + this method optionally filters out all but those listed in + vertex_subset. + Parameters + ---------- + vertex_subset : cudf.Series or iterable container, optional + A container of vertices for displaying corresponding out-degree. + If not set, degrees are computed for the entire set of vertices. + Returns + ------- + df : cudf.DataFrame + GPU DataFrame of size N (the default) or the size of the given + vertices (vertex_subset) containing the out_degree. The ordering is + relative to the adjacency list, or that given by the specified + vertex_subset. + df[vertex] : cudf.Series + The vertex IDs (will be identical to vertex_subset if + specified). + df[degree] : cudf.Series + The computed out-degree of the corresponding vertex. + Examples + -------- + >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', + >>> dtype=['int32', 'int32', 'float32'], header=None) + >>> G = cugraph.Graph() + >>> G.from_cudf_edgelist(M, '0', '1') + >>> df = G.out_degree([0,9,12]) + """ + return self._degree(vertex_subset, direction=Direction.OUT) + + def degree(self, vertex_subset=None): + """ + Compute vertex degree, which is the total number of edges incident + to a vertex (both in and out edges). By default, this method computes + degrees for the entire set of vertices. If vertex_subset is provided, + then this method optionally filters out all but those listed in + vertex_subset. + Parameters + ---------- + vertex_subset : cudf.Series or iterable container, optional + a container of vertices for displaying corresponding degree. If not + set, degrees are computed for the entire set of vertices. + Returns + ------- + df : cudf.DataFrame + GPU DataFrame of size N (the default) or the size of the given + vertices (vertex_subset) containing the degree. The ordering is + relative to the adjacency list, or that given by the specified + vertex_subset. + df['vertex'] : cudf.Series + The vertex IDs (will be identical to vertex_subset if + specified). + df['degree'] : cudf.Series + The computed degree of the corresponding vertex. + Examples + -------- + >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', + >>> dtype=['int32', 'int32', 'float32'], header=None) + >>> G = cugraph.Graph() + >>> G.from_cudf_edgelist(M, '0', '1') + >>> all_df = G.degree() + >>> subset_df = G.degree([0,9,12]) + """ + return self._degree(vertex_subset) + + # FIXME: vertex_subset could be a DataFrame for multi-column vertices + def degrees(self, vertex_subset=None): + """ + Compute vertex in-degree and out-degree. By default, this method + computes vertex degrees for the entire set of vertices. If + vertex_subset is provided, this method optionally filters out all but + those listed in vertex_subset. + Parameters + ---------- + vertex_subset : cudf.Series or iterable container, optional + A container of vertices for displaying corresponding degree. If not + set, degrees are computed for the entire set of vertices. + Returns + ------- + df : cudf.DataFrame + GPU DataFrame of size N (the default) or the size of the given + vertices (vertex_subset) containing the degrees. The ordering is + relative to the adjacency list, or that given by the specified + vertex_subset. + df['vertex'] : cudf.Series + The vertex IDs (will be identical to vertex_subset if + specified). + df['in_degree'] : cudf.Series + The in-degree of the vertex. + df['out_degree'] : cudf.Series + The out-degree of the vertex. + Examples + -------- + >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', + >>> dtype=['int32', 'int32', 'float32'], header=None) + >>> G = cugraph.Graph() + >>> G.from_cudf_edgelist(M, '0', '1') + >>> df = G.degrees([0,9,12]) + """ + ( + vertex_col, + in_degree_col, + out_degree_col, + ) = graph_primtypes_wrapper._degrees(self) + + df = cudf.DataFrame() + df["vertex"] = vertex_col + df["in_degree"] = in_degree_col + df["out_degree"] = out_degree_col + + if self.properties.renumbered is True: + df = self.renumber_map.unrenumber(df, "vertex") + + if vertex_subset is not None: + df = df[df['vertex'].isin(vertex_subset)] + + return df + + def _degree(self, vertex_subset, direction=Direction.ALL): + vertex_col, degree_col = graph_primtypes_wrapper._degree(self, + direction) + df = cudf.DataFrame() + df["vertex"] = vertex_col + df["degree"] = degree_col + + if self.properties.renumbered is True: + df = self.renumber_map.unrenumber(df, "vertex") + + if vertex_subset is not None: + df = df[df['vertex'].isin(vertex_subset)] + + return df + + def to_directed(self, DiG): + """ + Return a directed representation of the graph Implementation. + This function copies the internal structures and returns the + directed view. + """ + DiG.properties.renumbered = self.properties.renumbered + DiG.renumber_map = self.renumber_map + DiG.edgelist = self.edgelist + DiG.adjlist = self.adjlist + DiG.transposedadjlist = self.transposedadjlist + + def to_undirected(self, G): + """ + Return an undirected copy of the graph. + """ + G.properties.renumbered = self.properties.renumbered + G.renumber_map = self.renumber_map + if self.properties.directed is False: + G.edgelist = self.edgelist + G.adjlist = self.adjlist + G.transposedadjlist = self.transposedadjlist + else: + df = self.edgelist.edgelist_df + if self.edgelist.weights: + source_col, dest_col, value_col = symmetrize( + df["src"], df["dst"], df["weights"] + ) + else: + source_col, dest_col = symmetrize(df["src"], df["dst"]) + value_col = None + G.edgelist = simpleGraphImpl.EdgeList(source_col, dest_col, + value_col) + + def has_node(self, n): + """ + Returns True if the graph contains the node n. + """ + if self.properties.renumbered: + tmp = self.renumber_map.to_internal_vertex_id(cudf.Series([n])) + return tmp[0] is not cudf.NA and tmp[0] >= 0 + else: + df = self.edgelist.edgelist_df[["src", "dst"]] + return (df == n).any().any() + + def has_edge(self, u, v): + """ + Returns True if the graph contains the edge (u,v). + """ + if self.properties.renumbered: + tmp = cudf.DataFrame({"src": [u, v]}) + tmp = tmp.astype({"src": "int"}) + tmp = self.renumber_map.add_internal_vertex_id( + tmp, "id", "src", preserve_order=True + ) + + u = tmp["id"][0] + v = tmp["id"][1] + + df = self.edgelist.edgelist_df + return ((df["src"] == u) & (df["dst"] == v)).any() + + def has_self_loop(self): + """ + Returns True if the graph has self loop. + """ + # Detect self loop + if self.properties.self_loop is None: + elist = self.edgelist.edgelist_df + if (elist["src"] == elist["dst"]).any(): + self.properties.self_loop = True + else: + self.properties.self_loop = False + return self.properties.self_loop + + def edges(self): + """ + Returns all the edges in the graph as a cudf.DataFrame containing + sources and destinations. It does not return the edge weights. + For viewing edges with weights use view_edge_list() + """ + return self.view_edge_list()[["src", "dst"]] + + def nodes(self): + """ + Returns all the nodes in the graph as a cudf.Series + """ + if self.edgelist is not None: + df = self.edgelist.edgelist_df + if self.properties.renumbered: + # FIXME: If vertices are multicolumn + # this needs to return a dataframe + # FIXME: This relies on current implementation + # of NumberMap, should not really expose + # this, perhaps add a method to NumberMap + return self.renumber_map.implementation.df["0"] + else: + return cudf.concat([df["src"], df["dst"]]).unique() + if self.adjlist is not None: + return cudf.Series(np.arange(0, self.number_of_nodes())) + + def neighbors(self, n): + if self.edgelist is None: + raise Exception("Graph has no Edgelist.") + if self.properties.renumbered: + node = self.renumber_map.to_internal_vertex_id(cudf.Series([n])) + if len(node) == 0: + return cudf.Series(dtype="int") + n = node[0] + + df = self.edgelist.edgelist_df + neighbors = df[df["src"] == n]["dst"].reset_index(drop=True) + if self.properties.renumbered: + # FIXME: Multi-column vertices + return self.renumber_map.from_internal_vertex_id(neighbors)["0"] + else: + return neighbors + + def vertex_column_size(self): + if self.properties.renumbered: + return self.renumber_map.vertex_column_size() + else: + return 1 diff --git a/python/cugraph/structure/graph_primtypes.pxd b/python/cugraph/structure/graph_primtypes.pxd index e46f4092dd4..20581a8ecc0 100644 --- a/python/cugraph/structure/graph_primtypes.pxd +++ b/python/cugraph/structure/graph_primtypes.pxd @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -18,24 +18,22 @@ from libcpp cimport bool from libcpp.memory cimport unique_ptr - +from libcpp.utility cimport pair +from libcpp.vector cimport vector +from cugraph.raft.common.handle cimport * from rmm._lib.device_buffer cimport device_buffer -cdef extern from "raft/handle.hpp" namespace "raft": - cdef cppclass handle_t: - handle_t() except + - -cdef extern from "graph.hpp" namespace "cugraph": +cdef extern from "cugraph/legacy/graph.hpp" namespace "cugraph::legacy": ctypedef enum PropType: - PROP_UNDEF "cugraph::PROP_UNDEF" - PROP_FALSE "cugraph::PROP_FALSE" - PROP_TRUE "cugraph::PROP_TRUE" + PROP_UNDEF "cugraph::legacy::PROP_UNDEF" + PROP_FALSE "cugraph::legacy::PROP_FALSE" + PROP_TRUE "cugraph::legacy::PROP_TRUE" ctypedef enum DegreeDirection: - DIRECTION_IN_PLUS_OUT "cugraph::DegreeDirection::IN_PLUS_OUT" - DIRECTION_IN "cugraph::DegreeDirection::IN" - DIRECTION_OUT "cugraph::DegreeDirection::OUT" + DIRECTION_IN_PLUS_OUT "cugraph::legacy::DegreeDirection::IN_PLUS_OUT" + DIRECTION_IN "cugraph::legacy::DegreeDirection::IN" + DIRECTION_OUT "cugraph::legacy::DegreeDirection::OUT" struct GraphProperties: bool directed @@ -125,22 +123,11 @@ cdef extern from "graph.hpp" namespace "cugraph": GraphCSRView[VT,ET,WT] view() - -cdef extern from "algorithms.hpp" namespace "cugraph": +cdef extern from "cugraph/algorithms.hpp" namespace "cugraph": cdef unique_ptr[GraphCOO[VT, ET, WT]] get_two_hop_neighbors[VT,ET,WT]( const GraphCSRView[VT, ET, WT] &graph) except + -cdef extern from "functions.hpp" namespace "cugraph": - - cdef unique_ptr[device_buffer] renumber_vertices[VT_IN,VT_OUT,ET]( - ET number_of_edges, - const VT_IN *src, - const VT_IN *dst, - VT_OUT *src_renumbered, - VT_OUT *dst_renumbered, - ET *map_size) except + - cdef extern from "" namespace "std" nogil: cdef unique_ptr[GraphCOO[int,int,float]] move(unique_ptr[GraphCOO[int,int,float]]) @@ -190,54 +177,3 @@ ctypedef fused GraphViewType: cdef coo_to_df(GraphCOOPtrType graph) cdef csr_to_series(GraphCSRPtrType graph) cdef GraphViewType get_graph_view(input_graph, bool weightless=*, GraphViewType* dummy=*) - - -# C++ utilities specifically for Cython -cdef extern from "utilities/cython.hpp" namespace "cugraph::cython": - - ctypedef enum numberTypeEnum: - int32Type "cugraph::cython::numberTypeEnum::int32Type" - int64Type "cugraph::cython::numberTypeEnum::int64Type" - floatType "cugraph::cython::numberTypeEnum::floatType" - doubleType "cugraph::cython::numberTypeEnum::doubleType" - - cdef cppclass graph_container_t: - pass - - cdef void populate_graph_container( - graph_container_t &graph_container, - handle_t &handle, - void *src_vertices, - void *dst_vertices, - void *weights, - void *vertex_partition_offsets, - numberTypeEnum vertexType, - numberTypeEnum edgeType, - numberTypeEnum weightType, - size_t num_partition_edges, - size_t num_global_vertices, - size_t num_global_edges, - bool sorted_by_degree, - bool transposed, - bool multi_gpu) except + - - ctypedef enum graphTypeEnum: - LegacyCSR "cugraph::cython::graphTypeEnum::LegacyCSR" - LegacyCSC "cugraph::cython::graphTypeEnum::LegacyCSC" - LegacyCOO "cugraph::cython::graphTypeEnum::LegacyCOO" - - cdef void populate_graph_container_legacy( - graph_container_t &graph_container, - graphTypeEnum legacyType, - const handle_t &handle, - void *offsets, - void *indices, - void *weights, - numberTypeEnum offsetType, - numberTypeEnum indexType, - numberTypeEnum weightType, - size_t num_global_vertices, - size_t num_global_edges, - int *local_vertices, - int *local_edges, - int *local_offsets) except + diff --git a/python/cugraph/structure/graph_primtypes.pyx b/python/cugraph/structure/graph_primtypes.pyx index f3f0fd9b9a6..da16f8f4c8a 100644 --- a/python/cugraph/structure/graph_primtypes.pyx +++ b/python/cugraph/structure/graph_primtypes.pyx @@ -93,6 +93,9 @@ cdef GraphCOOViewType get_coo_graph_view(input_graph, bool weighted=True, GraphC if not input_graph.edgelist: input_graph.view_edge_list() + num_edges = input_graph.number_of_edges(directed_edges=True) + num_verts = input_graph.number_of_vertices() + cdef uintptr_t c_src = input_graph.edgelist.edgelist_df['src'].__cuda_array_interface__['data'][0] cdef uintptr_t c_dst = input_graph.edgelist.edgelist_df['dst'].__cuda_array_interface__['data'][0] cdef uintptr_t c_weights = NULL @@ -101,8 +104,6 @@ cdef GraphCOOViewType get_coo_graph_view(input_graph, bool weighted=True, GraphC if input_graph.edgelist.weights and weighted: c_weights = input_graph.edgelist.edgelist_df['weights'].__cuda_array_interface__['data'][0] - num_verts = input_graph.number_of_vertices() - num_edges = input_graph.number_of_edges(directed_edges=True) cdef GraphCOOViewType in_graph if GraphCOOViewType is GraphCOOViewFloat: in_graph = GraphCOOViewFloat(c_src, c_dst, c_weights, num_verts, num_edges) diff --git a/python/cugraph/structure/graph_primtypes_wrapper.pyx b/python/cugraph/structure/graph_primtypes_wrapper.pyx index 7bc62b9a1af..95de1d70732 100644 --- a/python/cugraph/structure/graph_primtypes_wrapper.pyx +++ b/python/cugraph/structure/graph_primtypes_wrapper.pyx @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -18,9 +18,9 @@ from cugraph.structure.graph_primtypes cimport * from cugraph.structure.graph_primtypes cimport get_two_hop_neighbors as c_get_two_hop_neighbors -from cugraph.structure.graph_primtypes cimport renumber_vertices as c_renumber_vertices from cugraph.structure.utils_wrapper import * from libcpp cimport bool +import enum from libc.stdint cimport uintptr_t from rmm._lib.device_buffer cimport device_buffer, DeviceBuffer @@ -45,43 +45,10 @@ def datatype_cast(cols, dtypes): return cols_out -def renumber(source_col, dest_col): - num_edges = len(source_col) - - src_renumbered = cudf.Series(np.zeros(num_edges), dtype=np.int32) - dst_renumbered = cudf.Series(np.zeros(num_edges), dtype=np.int32) - - cdef uintptr_t c_src = source_col.__cuda_array_interface__['data'][0] - cdef uintptr_t c_dst = dest_col.__cuda_array_interface__['data'][0] - cdef uintptr_t c_src_renumbered = src_renumbered.__cuda_array_interface__['data'][0] - cdef uintptr_t c_dst_renumbered = dst_renumbered.__cuda_array_interface__['data'][0] - cdef int map_size = 0 - cdef int n_edges = num_edges - - cdef unique_ptr[device_buffer] numbering_map - - if (source_col.dtype == np.int32): - numbering_map = move(c_renumber_vertices[int,int,int](n_edges, - c_src, - c_dst, - c_src_renumbered, - c_dst_renumbered, - &map_size)) - else: - numbering_map = move(c_renumber_vertices[long,int,int](n_edges, - c_src, - c_dst, - c_src_renumbered, - c_dst_renumbered, - &map_size)) - - - map = DeviceBuffer.c_from_unique_ptr(move(numbering_map)) - map = Buffer(map) - - output_map = cudf.Series(data=map, dtype=source_col.dtype) - - return src_renumbered, dst_renumbered, output_map +class Direction(enum.Enum): + ALL = 0 + IN = 1 + OUT = 2 def view_adj_list(input_graph): @@ -137,7 +104,7 @@ def view_edge_list(input_graph): return src_indices, indices, weights -def _degree_coo(edgelist_df, src_name, dst_name, x=0, num_verts=None, sID=None): +def _degree_coo(edgelist_df, src_name, dst_name, direction=Direction.ALL, num_verts=None, sID=None): # # Computing the degree of the input graph from COO # @@ -146,11 +113,11 @@ def _degree_coo(edgelist_df, src_name, dst_name, x=0, num_verts=None, sID=None): src = edgelist_df[src_name] dst = edgelist_df[dst_name] - if x == 0: + if direction == Direction.ALL: dir = DIRECTION_IN_PLUS_OUT - elif x == 1: + elif direction == Direction.IN: dir = DIRECTION_IN - elif x == 2: + elif direction == Direction.OUT: dir = DIRECTION_OUT else: raise Exception("x should be 0, 1 or 2") @@ -185,17 +152,17 @@ def _degree_coo(edgelist_df, src_name, dst_name, x=0, num_verts=None, sID=None): return vertex_col, degree_col -def _degree_csr(offsets, indices, x=0): +def _degree_csr(offsets, indices, direction=Direction.ALL): cdef DegreeDirection dir - if x == 0: + if direction == Direction.ALL: dir = DIRECTION_IN_PLUS_OUT - elif x == 1: + elif direction == Direction.IN: dir = DIRECTION_IN - elif x == 2: + elif direction == Direction.OUT: dir = DIRECTION_OUT else: - raise Exception("x should be 0, 1 or 2") + raise Exception("direction should be 0, 1 or 2") [offsets, indices] = datatype_cast([offsets, indices], [np.int32]) @@ -220,44 +187,48 @@ def _degree_csr(offsets, indices, x=0): return vertex_col, degree_col -def _degree(input_graph, x=0): - transpose_x = { 0: 0, - 2: 1, - 1: 2 } +def _mg_degree(input_graph, direction=Direction.ALL): + if input_graph.edgelist is None: + input_graph.compute_renumber_edge_list(transposed=False) + input_ddf = input_graph.edgelist.edgelist_df + num_verts = input_ddf[['src', 'dst']].max().max().compute() + 1 + data = DistributedDataHandler.create(data=input_ddf) + comms = Comms.get_comms() + client = default_client() + data.calculate_parts_to_sizes(comms) + if direction==Direction.IN: + degree_ddf = [client.submit(_degree_coo, wf[1][0], 'src', 'dst', Direction.IN, num_verts, comms.sessionId, workers=[wf[0]]) for idx, wf in enumerate(data.worker_to_parts.items())] + if direction==Direction.OUT: + degree_ddf = [client.submit(_degree_coo, wf[1][0], 'dst', 'src', Direction.IN, num_verts, comms.sessionId, workers=[wf[0]]) for idx, wf in enumerate(data.worker_to_parts.items())] + wait(degree_ddf) + return degree_ddf[0].result() + + +def _degree(input_graph, direction=Direction.ALL): + transpose_direction = { Direction.ALL: Direction.ALL, + Direction.IN: Direction.OUT, + Direction.OUT: Direction.IN } if input_graph.adjlist is not None: return _degree_csr(input_graph.adjlist.offsets, input_graph.adjlist.indices, - x) + direction) if input_graph.transposedadjlist is not None: return _degree_csr(input_graph.transposedadjlist.offsets, input_graph.transposedadjlist.indices, - transpose_x[x]) - - if input_graph.edgelist is None and input_graph.distributed: - input_graph.compute_renumber_edge_list(transposed=False) + transpose_direction[direction]) if input_graph.edgelist is not None: - if isinstance(input_graph.edgelist.edgelist_df, dc.DataFrame): - input_ddf = input_graph.edgelist.edgelist_df - num_verts = input_ddf[['src', 'dst']].max().max().compute() + 1 - data = DistributedDataHandler.create(data=input_ddf) - comms = Comms.get_comms() - client = default_client() - data.calculate_parts_to_sizes(comms) - degree_ddf = [client.submit(_degree_coo, wf[1][0], 'src', 'dst', x, num_verts, comms.sessionId, workers=[wf[0]]) for idx, wf in enumerate(data.worker_to_parts.items())] - wait(degree_ddf) - return degree_ddf[0].result() return _degree_coo(input_graph.edgelist.edgelist_df, - 'src', 'dst', x) + 'src', 'dst', direction) raise Exception("input_graph not COO, CSR or CSC") def _degrees(input_graph): - verts, indegrees = _degree(input_graph,1) - verts, outdegrees = _degree(input_graph, 2) + verts, indegrees = _degree(input_graph, Direction.IN) + verts, outdegrees = _degree(input_graph, Direction.OUT) return verts, indegrees, outdegrees diff --git a/python/cugraph/structure/graph_utilities.pxd b/python/cugraph/structure/graph_utilities.pxd new file mode 100644 index 00000000000..b701ba8b400 --- /dev/null +++ b/python/cugraph/structure/graph_utilities.pxd @@ -0,0 +1,195 @@ +# Copyright (c) 2019-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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# cython: profile=False +# distutils: language = c++ +# cython: embedsignature = True +# cython: language_level = 3 + + +from cugraph.raft.common.handle cimport * +from libcpp cimport bool +from libcpp.memory cimport unique_ptr +from libcpp.utility cimport pair +from libcpp.vector cimport vector +from rmm._lib.device_buffer cimport device_buffer + +# C++ graph utilities +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": + + ctypedef enum numberTypeEnum: + int32Type "cugraph::cython::numberTypeEnum::int32Type" + int64Type "cugraph::cython::numberTypeEnum::int64Type" + floatType "cugraph::cython::numberTypeEnum::floatType" + doubleType "cugraph::cython::numberTypeEnum::doubleType" + + cdef cppclass graph_container_t: + pass + + cdef void populate_graph_container( + graph_container_t &graph_container, + handle_t &handle, + void *src_vertices, + void *dst_vertices, + void *weights, + void *vertex_partition_offsets, + void *segment_offsets, + size_t num_segments, + numberTypeEnum vertexType, + numberTypeEnum edgeType, + numberTypeEnum weightType, + size_t num_local_edges, + size_t num_global_vertices, + size_t num_global_edges, + bool is_weighted, + bool is_symmetric, + bool transposed, + bool multi_gpu) except + + + ctypedef enum graphTypeEnum: + LegacyCSR "cugraph::cython::graphTypeEnum::LegacyCSR" + LegacyCSC "cugraph::cython::graphTypeEnum::LegacyCSC" + LegacyCOO "cugraph::cython::graphTypeEnum::LegacyCOO" + + cdef void populate_graph_container_legacy( + graph_container_t &graph_container, + graphTypeEnum legacyType, + const handle_t &handle, + void *offsets, + void *indices, + void *weights, + numberTypeEnum offsetType, + numberTypeEnum indexType, + numberTypeEnum weightType, + size_t num_global_vertices, + size_t num_global_edges, + int *local_vertices, + int *local_edges, + int *local_offsets) except + + + cdef cppclass cy_multi_edgelists_t: + size_t number_of_vertices + size_t number_of_edges + size_t number_of_subgraph + unique_ptr[device_buffer] src_indices + unique_ptr[device_buffer] dst_indices + unique_ptr[device_buffer] edge_data + unique_ptr[device_buffer] subgraph_offsets + + cdef cppclass random_walk_ret_t: + size_t coalesced_sz_v_ + size_t coalesced_sz_w_ + size_t num_paths_ + size_t max_depth_ + unique_ptr[device_buffer] d_coalesced_v_ + unique_ptr[device_buffer] d_coalesced_w_ + unique_ptr[device_buffer] d_sizes_ + + cdef cppclass random_walk_path_t: + unique_ptr[device_buffer] d_v_offsets + unique_ptr[device_buffer] d_w_sizes + unique_ptr[device_buffer] d_w_offsets + + cdef cppclass graph_generator_t: + unique_ptr[device_buffer] d_source + unique_ptr[device_buffer] d_destination + +cdef extern from "" namespace "std" nogil: + cdef device_buffer move(device_buffer) + cdef unique_ptr[device_buffer] move(unique_ptr[device_buffer]) + cdef cy_multi_edgelists_t move(cy_multi_edgelists_t) + cdef unique_ptr[cy_multi_edgelists_t] move(unique_ptr[cy_multi_edgelists_t]) + +# renumber_edgelist() interface utilities: +# +# +# 1. `cdef extern partition_t`: +# +cdef extern from "cugraph/experimental/graph_view.hpp" namespace "cugraph::experimental": + + cdef cppclass partition_t[vertex_t]: + pass + + +# 2. return type for shuffle: +# +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": + + cdef cppclass major_minor_weights_t[vertex_t, edge_t, weight_t]: + major_minor_weights_t(const handle_t &handle) + pair[unique_ptr[device_buffer], size_t] get_major_wrap() + pair[unique_ptr[device_buffer], size_t] get_minor_wrap() + pair[unique_ptr[device_buffer], size_t] get_weights_wrap() + unique_ptr[vector[edge_t]] get_edge_counts_wrap() + + +ctypedef fused shuffled_vertices_t: + major_minor_weights_t[int, int, float] + major_minor_weights_t[int, int, double] + major_minor_weights_t[int, long, float] + major_minor_weights_t[int, long, double] + major_minor_weights_t[long, long, float] + major_minor_weights_t[long, long, double] + +# 3. return type for renumber: +# +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": + + cdef cppclass renum_tuple_t[vertex_t, edge_t]: + renum_tuple_t(const handle_t &handle) + pair[unique_ptr[device_buffer], size_t] get_dv_wrap() + vertex_t& get_num_vertices() + edge_t& get_num_edges() + vector[vertex_t]& get_segment_offsets() + unique_ptr[vector[vertex_t]] get_segment_offsets_wrap() + int get_part_row_size() + int get_part_col_size() + int get_part_comm_rank() + unique_ptr[vector[vertex_t]] get_partition_offsets_wrap() + pair[vertex_t, vertex_t] get_part_local_vertex_range() + vertex_t get_part_local_vertex_first() + vertex_t get_part_local_vertex_last() + pair[vertex_t, vertex_t] get_part_vertex_partition_range(size_t vertex_partition_idx) + vertex_t get_part_vertex_partition_first(size_t vertex_partition_idx) + vertex_t get_part_vertex_partition_last(size_t vertex_partition_idx) + vertex_t get_part_vertex_partition_size(size_t vertex_partition_idx) + size_t get_part_number_of_matrix_partitions() + vertex_t get_part_matrix_partition_major_first(size_t partition_idx) + vertex_t get_part_matrix_partition_major_last(size_t partition_idx) + vertex_t get_part_matrix_partition_major_value_start_offset(size_t partition_idx) + pair[vertex_t, vertex_t] get_part_matrix_partition_minor_range() + vertex_t get_part_matrix_partition_minor_first() + vertex_t get_part_matrix_partition_minor_last() + +# 4. `sort_and_shuffle_values()` wrapper: +# +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": + + cdef unique_ptr[major_minor_weights_t[vertex_t, edge_t, weight_t]] call_shuffle[vertex_t, edge_t, weight_t]( + const handle_t &handle, + vertex_t *edgelist_major_vertices, + vertex_t *edgelist_minor_vertices, + weight_t* edgelist_weights, + edge_t num_edges) except + + +# 5. `renumber_edgelist()` wrapper +# +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": + + cdef unique_ptr[renum_tuple_t[vertex_t, edge_t]] call_renumber[vertex_t, edge_t]( + const handle_t &handle, + vertex_t *edgelist_major_vertices, + vertex_t *edgelist_minor_vertices, + const vector[edge_t]& edge_counts, + bool do_check, + bool multi_gpu) except + diff --git a/python/cugraph/structure/hypergraph.py b/python/cugraph/structure/hypergraph.py index a11c937d83d..c5a1ac39e4f 100644 --- a/python/cugraph/structure/hypergraph.py +++ b/python/cugraph/structure/hypergraph.py @@ -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. # You may obtain a copy of the License at @@ -36,7 +36,7 @@ import cudf import numpy as np -from cugraph.structure.graph import Graph +from cugraph.structure.graph_classes import Graph def hypergraph( @@ -66,24 +66,20 @@ def hypergraph( components as dataframes. The transform reveals relationships between the rows and unique values. This transform is useful for lists of events, samples, relationships, and other structured high-dimensional data. - The transform creates a node for every row, and turns a row's column entries into node attributes. If direct=False (default), every unique value within a column is also turned into a node. Edges are added to connect a row's nodes to each of its column nodes, or if direct=True, to one another. Nodes are given the attribute specified by ``NODETYPE`` that corresponds to the originating column name, or if a row ``EVENTID``. - Consider a list of events. Each row represents a distinct event, and each column some metadata about an event. If multiple events have common metadata, they will be transitively connected through those metadata values. Conversely, if an event has unique metadata, the unique metadata will turn into nodes that only have connections to the event node. - For best results, set ``EVENTID`` to a row's unique ID, ``SKIP`` to all non-categorical columns (or ``columns`` to all categorical columns), and ``categories`` to group columns with the same kinds of values. - Parameters ---------- values : cudf.DataFrame @@ -130,7 +126,6 @@ def hypergraph( The name to use as the node type column in the graph and node DFs. EDGETYPE : str, optional, default "edge_type" The name to use as the edge type column in the graph and edge DF. - Returns ------- result : dict {"nodes", "edges", "graph", "events", "entities"} diff --git a/python/cugraph/structure/number_map.py b/python/cugraph/structure/number_map.py index b9ed8eb2e58..1e3281ca33b 100644 --- a/python/cugraph/structure/number_map.py +++ b/python/cugraph/structure/number_map.py @@ -1,4 +1,5 @@ -# 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. # You may obtain a copy of the License at @@ -10,100 +11,45 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# -import cudf +from dask.distributed import wait, default_client +from cugraph.dask.common.input_utils import get_distributed_data +from cugraph.structure import renumber_wrapper as c_renumber +import cugraph.comms.comms as Comms import dask_cudf import numpy as np -import bisect +import cudf + + +def call_renumber(sID, + data, + num_edges, + is_mnmg, + store_transposed): + wid = Comms.get_worker_id(sID) + handle = Comms.get_handle(sID) + return c_renumber.renumber(data[0], + num_edges, + wid, + handle, + is_mnmg, + store_transposed) class NumberMap: - """ - Class used to translate external vertex ids to internal vertex ids - in the cuGraph framework. - - Internal vertex ids are assigned by hashing the external vertex ids - into a structure to eliminate duplicates, and the resulting list - of unique vertices are assigned integers from [0, V) where V is - the number of unique vertices. - - In Single GPU mode, internal vertex ids are constructed using - cudf functions, with a cudf.DataFrame containing the mapping - from external vertex identifiers and internal vertex identifiers - allowing for mapping vertex identifiers in either direction. In - this mode, the order of the output from the mapping functions is - non-deterministic. cudf makes no guarantees about order. If - matching the input order is required set the preserve_order - to True. - - In Multi GPU mode, internal vertex ids are constucted using - dask_cudf functions, with a dask_cudf.DataFrame containing - the mapping from external vertex identifiers and internal - vertex identifiers allowing for mapping vertex identifiers - in either direction. In this mode, the partitioning of - the number_map and the output from any of the mapping functions - are non-deterministic. dask_cudf makes no guarantees about the - partitioning or order of the output. As of this release, - there is no mechanism for controlling that, this will be - addressed at some point. - """ class SingleGPU: def __init__(self, df, src_col_names, dst_col_names, id_type, store_transposed): self.col_names = NumberMap.compute_vals(src_col_names) - self.df = cudf.DataFrame() + self.src_col_names = src_col_names + self.dst_col_names = dst_col_names + self.df = df self.id_type = id_type self.store_transposed = store_transposed - - source_count = 0 - dest_count = 0 - - if store_transposed: - dest_count = 1 - else: - source_count = 1 - - tmp = ( - df[src_col_names] - .assign(count=source_count) - .groupby(src_col_names) - .sum() - .reset_index() - .rename( - columns=dict(zip(src_col_names, self.col_names)), - copy=False, - ) - ) - - if dst_col_names is not None: - tmp_dst = ( - df[dst_col_names] - .assign(count=dest_count) - .groupby(dst_col_names) - .sum() - .reset_index() - ) - for newname, oldname in zip(self.col_names, dst_col_names): - self.df[newname] = tmp[newname].append(tmp_dst[oldname]) - self.df['count'] = tmp['count'].append(tmp_dst['count']) - else: - for newname, oldname in zip(self.col_names, dst_col_names): - self.df[newname] = tmp[newname] - self.df['count'] = tmp['count'] - self.numbered = False - def compute(self): - if not self.numbered: - tmp = self.df.groupby(self.col_names).sum().sort_values( - 'count', ascending=False - ).reset_index().drop(columns='count') - - tmp["id"] = tmp.index.astype(self.id_type) - self.df = tmp - self.numbered = True - def to_internal_vertex_id(self, df, col_names): tmp_df = df[col_names].rename( columns=dict(zip(col_names, self.col_names)), copy=False @@ -117,6 +63,25 @@ def to_internal_vertex_id(self, df, col_names): .reset_index()["id"] ) + def from_internal_vertex_id( + self, df, internal_column_name, external_column_names + ): + tmp_df = self.df.merge( + df, + right_on=internal_column_name, + left_on="id", + how="right", + ) + if internal_column_name != "id": + tmp_df = tmp_df.drop(columns=["id"]) + if external_column_names is None: + return tmp_df + else: + return tmp_df.rename( + columns=dict(zip(self.col_names, external_column_names)), + copy=False, + ) + def add_internal_vertex_id(self, df, id_column_name, col_names, drop, preserve_order): ret = None @@ -162,76 +127,39 @@ def add_internal_vertex_id(self, df, id_column_name, col_names, return ret - def from_internal_vertex_id( - self, df, internal_column_name, external_column_names - ): - tmp_df = self.df.merge( - df, - right_on=internal_column_name, - left_on="id", - how="right", - ) - if internal_column_name != "id": - tmp_df = tmp_df.drop(columns=["id"]) - if external_column_names is None: - return tmp_df - else: - return tmp_df.rename( - columns=dict(zip(self.col_names, external_column_names)), - copy=False, - ) - - class MultiGPU: - def extract_vertices( - df, src_col_names, dst_col_names, - internal_col_names, store_transposed - ): - source_count = 0 - dest_count = 0 - - if store_transposed: - dest_count = 1 - else: - source_count = 1 + def indirection_map(self, df, src_col_names, dst_col_names): + tmp_df = cudf.DataFrame() - s = ( + tmp = ( df[src_col_names] - .assign(count=source_count) .groupby(src_col_names) - .sum() + .count() .reset_index() .rename( - columns=dict(zip(src_col_names, internal_col_names)), + columns=dict(zip(src_col_names, self.col_names)), copy=False, ) ) - d = None if dst_col_names is not None: - d = ( + tmp_dst = ( df[dst_col_names] - .assign(count=dest_count) .groupby(dst_col_names) - .sum() + .count() .reset_index() - .rename( - columns=dict(zip(dst_col_names, internal_col_names)), - copy=False, - ) ) + for newname, oldname in zip(self.col_names, dst_col_names): + tmp_df[newname] = tmp[newname].append(tmp_dst[oldname]) + else: + for newname in self.col_names: + tmp_df[newname] = tmp[newname] - reply = cudf.DataFrame() - - for i in internal_col_names: - if d is None: - reply[i] = s[i] - else: - reply[i] = s[i].append(d[i]) - - reply['count'] = s['count'].append(d['count']) - - return reply + tmp_df = tmp_df.groupby(self.col_names).count().reset_index() + tmp_df["id"] = tmp_df.index.astype(self.id_type) + self.df = tmp_df + return tmp_df + class MultiGPU: def __init__( self, ddf, src_col_names, dst_col_names, id_type, store_transposed ): @@ -239,118 +167,45 @@ def __init__( self.val_types = NumberMap.compute_vals_types(ddf, src_col_names) self.val_types["count"] = np.int32 self.id_type = id_type + self.ddf = ddf self.store_transposed = store_transposed - self.ddf = ddf.map_partitions( - NumberMap.MultiGPU.extract_vertices, - src_col_names, - dst_col_names, - self.col_names, - store_transposed, - meta=self.val_types, - ) self.numbered = False - # Function to compute partitions based on known divisions of the - # hash value - def compute_partition(df, divisions): - sample = df.index[0] - partition_id = bisect.bisect_right(divisions, sample) - 1 - return df.assign(partition=partition_id) - - def assign_internal_identifiers_kernel( - local_id, partition, global_id, base_addresses - ): - for i in range(len(local_id)): - global_id[i] = local_id[i] + base_addresses[partition[i]] - - def assign_internal_identifiers(df, base_addresses, id_type): - df = df.assign(local_id=df.index.astype(np.int64)) - df = df.apply_rows( - NumberMap.MultiGPU.assign_internal_identifiers_kernel, - incols=["local_id", "partition"], - outcols={"global_id": id_type}, - kwargs={"base_addresses": base_addresses}, - ) - - return df.drop(columns=["local_id", "hash", "partition"]) - - def assign_global_id(self, ddf, base_addresses, val_types): - val_types["global_id"] = self.id_type - del val_types["hash"] - del val_types["partition"] - - ddf = ddf.map_partitions( - lambda df: NumberMap.MultiGPU.assign_internal_identifiers( - df, base_addresses, self.id_type - ), - meta=val_types, + def to_internal_vertex_id(self, ddf, col_names): + tmp_ddf = ddf[col_names].rename( + columns=dict(zip(col_names, self.col_names))) + for name in self.col_names: + tmp_ddf[name] = tmp_ddf[name].astype(self.ddf[name].dtype) + x = self.ddf.merge( + tmp_ddf, + on=self.col_names, + how="right", ) - return ddf + return x['global_id'] - def compute(self): - if not self.numbered: - val_types = self.val_types - val_types["hash"] = np.int32 - - vertices = self.ddf.map_partitions( - lambda df: df.assign(hash=df.hash_columns(self.col_names)), - meta=val_types, - ) - - # Redistribute the ddf based on the hash values - rehashed = vertices.set_index("hash", drop=False) - - # Compute the local partition id (obsolete once - # https://github.com/dask/dask/issues/3707 is completed) - val_types["partition"] = np.int32 - - rehashed_with_partition_id = rehashed.map_partitions( - NumberMap.MultiGPU.compute_partition, - rehashed.divisions, - meta=val_types, - ) - - val_types.pop('count') - - numbering_map = rehashed_with_partition_id.map_partitions( - lambda df: df.groupby( - self.col_names + ["hash", "partition"] - ).sum() - .sort_values('count', ascending=False) - .reset_index() - .drop(columns='count'), - meta=val_types - ) + def from_internal_vertex_id( + self, df, internal_column_name, external_column_names + ): + tmp_df = self.ddf.merge( + df, + right_on=internal_column_name, + left_on="global_id", + how="right" + ).map_partitions(lambda df: df.drop(columns="global_id")) - # - # Compute base address for each partition - # - counts = numbering_map.map_partitions( - lambda df: df.groupby("partition").count() - ).compute()["hash"].to_pandas() - base_addresses = np.zeros(len(counts) + 1, self.id_type) - - for i in range(len(counts)): - base_addresses[i + 1] = base_addresses[i] + counts[i] - - # - # Update each partition with the base address - # - numbering_map = self.assign_global_id( - numbering_map, cudf.Series(base_addresses), val_types + if external_column_names is None: + return tmp_df + else: + return tmp_df.map_partitions( + lambda df: + df.rename( + columns=dict( + zip(self.col_names, external_column_names) + ), + copy=False + ) ) - self.ddf = numbering_map.persist() - self.numbered = True - - def to_internal_vertex_id(self, ddf, col_names): - return self.ddf.merge( - ddf, - right_on=col_names, - left_on=self.col_names, - how="right", - )["global_id"] - def add_internal_vertex_id(self, ddf, id_column_name, col_names, drop, preserve_order): # At the moment, preserve_order cannot be done on @@ -385,39 +240,49 @@ def add_internal_vertex_id(self, ddf, id_column_name, col_names, drop, return ret - def from_internal_vertex_id( - self, df, internal_column_name, external_column_names - ): - tmp_df = self.ddf.merge( - df, - right_on=internal_column_name, - left_on="global_id", - how="right" - ).map_partitions(lambda df: df.drop(columns="global_id")) + def indirection_map(self, ddf, src_col_names, dst_col_names): - if external_column_names is None: - return tmp_df - else: - return tmp_df.map_partitions( - lambda df: - df.rename( - columns=dict( - zip(self.col_names, external_column_names) - ), - copy=False - ) + tmp = ( + ddf[src_col_names] + .groupby(src_col_names) + .count() + .reset_index() + .rename( + columns=dict(zip(src_col_names, self.col_names)), ) + ) + + if dst_col_names is not None: + tmp_dst = ( + ddf[dst_col_names] + .groupby(dst_col_names) + .count() + .reset_index() + ) + for i, (newname, oldname) in enumerate(zip(self.col_names, + dst_col_names)): + if i == 0: + tmp_df = tmp[newname].append(tmp_dst[oldname]).\ + to_frame(name=newname) + else: + tmp_df[newname] = tmp[newname].append(tmp_dst[oldname]) + else: + for newname in self.col_names: + tmp_df[newname] = tmp[newname] + tmp_ddf = tmp_df.groupby(self.col_names).count().reset_index() + + # Set global index + tmp_ddf = tmp_ddf.assign(idx=1) + tmp_ddf['global_id'] = tmp_ddf.idx.cumsum() - 1 + tmp_ddf = tmp_ddf.drop(columns='idx') + tmp_ddf = tmp_ddf.persist() + self.ddf = tmp_ddf + return tmp_ddf def __init__(self, id_type=np.int32): self.implementation = None self.id_type = id_type - def aggregate_count_and_partition(df): - d = {} - d['count'] = df['count'].sum() - d['partition'] = df['partition'].min() - return cudf.Series(d, index=['count', 'partition']) - def compute_vals_types(df, column_names): """ Helper function to compute internal column names and types @@ -443,125 +308,19 @@ def compute_vals(column_names): """ return [str(i) for i in range(len(column_names))] - def from_dataframe( - self, df, src_col_names, dst_col_names=None, store_transposed=False - ): - """ - Populate the numbering map with vertices from the specified - columns of the provided DataFrame. - - Parameters - ---------- - df : cudf.DataFrame or dask_cudf.DataFrame - Contains a list of external vertex identifiers that will be - numbered by the NumberMap class. - src_col_names: list of strings - This list of 1 or more strings contain the names - of the columns that uniquely identify an external - vertex identifier for source vertices - dst_col_names: list of strings - This list of 1 or more strings contain the names - of the columns that uniquely identify an external - vertex identifier for destination vertices - store_transposed : bool - Identify how the graph adjacency will be used. - If True, the graph will be organized by destination. - If False, the graph will be organized by source - - """ - if self.implementation is not None: - raise Exception("NumberMap is already populated") - - if dst_col_names is not None and len(src_col_names) != len( - dst_col_names - ): - raise Exception( - "src_col_names must have same length as dst_col_names" - ) - - if type(df) is cudf.DataFrame: - self.implementation = NumberMap.SingleGPU( - df, src_col_names, dst_col_names, self.id_type, - store_transposed - ) - elif type(df) is dask_cudf.DataFrame: - self.implementation = NumberMap.MultiGPU( - df, src_col_names, dst_col_names, self.id_type, - store_transposed - ) - else: - raise Exception("df must be cudf.DataFrame or dask_cudf.DataFrame") - - self.implementation.compute() - - def from_series(self, src_series, dst_series=None, store_transposed=False): - """ - Populate the numbering map with vertices from the specified - pair of series objects, one for the source and one for - the destination - - Parameters - ---------- - src_series: cudf.Series or dask_cudf.Series - Contains a list of external vertex identifiers that will be - numbered by the NumberMap class. - dst_series: cudf.Series or dask_cudf.Series - Contains a list of external vertex identifiers that will be - numbered by the NumberMap class. - store_transposed : bool - Identify how the graph adjacency will be used. - If True, the graph will be organized by destination. - If False, the graph will be organized by source - """ - if self.implementation is not None: - raise Exception("NumberMap is already populated") - - if dst_series is not None and type(src_series) != type(dst_series): - raise Exception("src_series and dst_series must have same type") - - if type(src_series) is cudf.Series: - dst_series_list = None - df = cudf.DataFrame() - df["s"] = src_series - if dst_series is not None: - df["d"] = dst_series - dst_series_list = ["d"] - self.implementation = NumberMap.SingleGPU( - df, ["s"], dst_series_list, self.id_type, store_transposed - ) - elif type(src_series) is dask_cudf.Series: - dst_series_list = None - df = dask_cudf.DataFrame() - df["s"] = src_series - if dst_series is not None: - df["d"] = dst_series - dst_series_list = ["d"] - self.implementation = NumberMap.MultiGPU( - df, ["s"], dst_series_list, self.id_type, store_transposed - ) - else: - raise Exception( - "src_series must be cudf.Series or " "dask_cudf.Series" - ) - - self.implementation.compute() - def to_internal_vertex_id(self, df, col_names=None): """ Given a collection of external vertex ids, return the internal vertex ids - Parameters ---------- df: cudf.DataFrame, cudf.Series, dask_cudf.DataFrame, dask_cudf.Series Contains a list of external vertex identifiers that will be converted into internal vertex identifiers - col_names: (optional) list of strings This list of 1 or more strings contain the names of the columns that uniquely identify an external vertex identifier - Returns --------- vertex_ids : cudf.Series or dask_cudf.Series @@ -569,7 +328,6 @@ def to_internal_vertex_id(self, df, col_names=None): does not guarantee order or partitioning (in the case of dask_cudf) of vertex ids. If order matters use add_internal_vertex_id - """ tmp_df = None tmp_col_names = None @@ -587,11 +345,7 @@ def to_internal_vertex_id(self, df, col_names=None): reply = self.implementation.to_internal_vertex_id(tmp_df, tmp_col_names) - - if type(df) in [cudf.DataFrame, dask_cudf.DataFrame]: - return reply["0"] - else: - return reply + return reply def add_internal_vertex_id( self, df, id_column_name="id", col_names=None, drop=False, @@ -600,34 +354,27 @@ def add_internal_vertex_id( """ Given a collection of external vertex ids, return the internal vertex ids combined with the input data. - If a series-type input is provided then the series will be in a column named '0'. Otherwise the input column names in the DataFrame will be preserved. - Parameters ---------- df: cudf.DataFrame, cudf.Series, dask_cudf.DataFrame, dask_cudf.Series Contains a list of external vertex identifiers that will be converted into internal vertex identifiers - id_column_name: (optional) string The name to be applied to the column containing the id (defaults to 'id') - col_names: (optional) list of strings This list of 1 or more strings contain the names of the columns that uniquely identify an external vertex identifier - drop: (optional) boolean If True, drop the column names specified in col_names from the returned DataFrame. Defaults to False. - preserve_order: (optional) boolean If True, do extra sorting work to preserve the order of the input DataFrame. Defaults to False. - Returns --------- df : cudf.DataFrame or dask_cudf.DataFrame @@ -635,7 +382,6 @@ def add_internal_vertex_id( with an additional column containing the internal vertex id. Note that there is no guarantee of the order or partitioning of elements in the returned DataFrame. - """ tmp_df = None tmp_col_names = None @@ -671,7 +417,6 @@ def from_internal_vertex_id( """ Given a collection of internal vertex ids, return a DataFrame of the external vertex ids - Parameters ---------- df: cudf.DataFrame, cudf.Series, dask_cudf.DataFrame, dask_cudf.Series @@ -681,20 +426,16 @@ def from_internal_vertex_id( in a column labeled 'id'. If df is a dataframe type object then internal_column_name should identify which column corresponds the the internal vertex id that should be converted - internal_column_name: (optional) string Name of the column containing the internal vertex id. If df is a series then this parameter is ignored. If df is a DataFrame this parameter is required. - external_column_names: (optional) string or list of strings Name of the columns that define an external vertex id. If not specified, columns will be labeled '0', '1,', ..., 'n-1' - drop: (optional) boolean If True the internal column name will be dropped from the DataFrame. Defaults to False. - Returns --------- df : cudf.DataFrame or dask_cudf.DataFrame @@ -727,109 +468,143 @@ def from_internal_vertex_id( return output_df - def column_names(self): - """ - Return the list of internal column names - - Returns - ---------- - List of column names ('0', '1', ..., 'n-1') - """ - return self.implementation.col_names - - def renumber(df, src_col_names, dst_col_names, preserve_order=False, - store_transposed=False): - """ - Given a single GPU or distributed DataFrame, use src_col_names and - dst_col_names to identify the source vertex identifiers and destination - vertex identifiers, respectively. - - Internal vertex identifiers will be created, numbering vertices as - integers starting from 0. - - The function will return a DataFrame containing the original dataframe - contents with a new column labeled 'src' containing the renumbered - source vertices and a new column labeled 'dst' containing the - renumbered dest vertices, along with a NumberMap object that contains - the number map for the numbering that was used. - - Note that this function does not guarantee order in single GPU mode, - and does not guarantee order or partitioning in multi-GPU mode. If you - wish to preserve ordering, add an index column to df and sort the - return by that index column. - - Parameters - ---------- - df: cudf.DataFrame or dask_cudf.DataFrame - Contains a list of external vertex identifiers that will be - numbered by the NumberMap class. - src_col_names: string or list of strings - This list of 1 or more strings contain the names - of the columns that uniquely identify an external - vertex identifier for source vertices - dst_col_names: string or list of strings - This list of 1 or more strings contain the names - of the columns that uniquely identify an external - vertex identifier for destination vertices - store_transposed : bool - Identify how the graph adjacency will be used. - If True, the graph will be organized by destination. - If False, the graph will be organized by source - - Returns - --------- - df : cudf.DataFrame or dask_cudf.DataFrame - The original DataFrame columns exist unmodified. Columns - are added to the DataFrame to identify the external vertex - identifiers. If external_columns is specified, these names - are used as the names of the output columns. If external_columns - is not specifed the columns are labeled '0', ... 'n-1' based on - the number of columns identifying the external vertex identifiers. - - number_map : NumberMap - The number map object object that retains the mapping between - internal vertex identifiers and external vertex identifiers. + def renumber_and_segment( + df, src_col_names, dst_col_names, preserve_order=False, + store_transposed=False + ): + if isinstance(src_col_names, list): + renumber_type = 'legacy' + elif not (df[src_col_names].dtype == np.int32 or + df[src_col_names].dtype == np.int64): + renumber_type = 'legacy' + else: + renumber_type = 'experimental' - Examples - -------- - >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', - >>> dtype=['int32', 'int32', 'float32'], header=None) - >>> - >>> df, number_map = NumberMap.renumber(df, '0', '1') - >>> - >>> G = cugraph.Graph() - >>> G.from_cudf_edgelist(df, 'src', 'dst') - """ renumber_map = NumberMap() - - if isinstance(src_col_names, list): - renumber_map.from_dataframe(df, src_col_names, dst_col_names) - df = renumber_map.add_internal_vertex_id( - df, "src", src_col_names, drop=True, - preserve_order=preserve_order + if not isinstance(src_col_names, list): + src_col_names = [src_col_names] + dst_col_names = [dst_col_names] + if type(df) is cudf.DataFrame: + renumber_map.implementation = NumberMap.SingleGPU( + df, src_col_names, dst_col_names, renumber_map.id_type, + store_transposed ) - df = renumber_map.add_internal_vertex_id( - df, "dst", dst_col_names, drop=True, - preserve_order=preserve_order + elif type(df) is dask_cudf.DataFrame: + renumber_map.implementation = NumberMap.MultiGPU( + df, src_col_names, dst_col_names, renumber_map.id_type, + store_transposed ) else: - renumber_map.from_dataframe(df, [src_col_names], [dst_col_names]) + raise Exception("df must be cudf.DataFrame or dask_cudf.DataFrame") + + if renumber_type == 'legacy': + indirection_map = renumber_map.implementation.\ + indirection_map(df, + src_col_names, + dst_col_names) df = renumber_map.add_internal_vertex_id( df, "src", src_col_names, drop=True, preserve_order=preserve_order ) - df = renumber_map.add_internal_vertex_id( df, "dst", dst_col_names, drop=True, preserve_order=preserve_order ) + else: + df = df.rename(columns={src_col_names[0]: "src", + dst_col_names[0]: "dst"}) - if type(df) is dask_cudf.DataFrame: - df = df.persist() + num_edges = len(df) - return df, renumber_map + if isinstance(df, dask_cudf.DataFrame): + is_mnmg = True + else: + is_mnmg = False + + if is_mnmg: + client = default_client() + data = get_distributed_data(df) + result = [(client.submit(call_renumber, + Comms.get_session_id(), + wf[1], + num_edges, + is_mnmg, + store_transposed, + workers=[wf[0]]), wf[0]) + for idx, wf in enumerate(data.worker_to_parts.items())] + wait(result) + + def get_renumber_map(data): + return data[0] + + def get_segment_offsets(data): + return data[1] + + def get_renumbered_df(data): + return data[2] + + renumbering_map = dask_cudf.from_delayed( + [client.submit(get_renumber_map, + data, + workers=[wf]) + for (data, wf) in result]) + + list_of_segment_offsets = client.gather( + [client.submit(get_segment_offsets, + data, + workers=[wf]) + for (data, wf) in result]) + aggregate_segment_offsets = [] + for segment_offsets in list_of_segment_offsets: + aggregate_segment_offsets.extend(segment_offsets) + + renumbered_df = dask_cudf.from_delayed( + [client.submit(get_renumbered_df, + data, + workers=[wf]) + for (data, wf) in result]) + if renumber_type == 'legacy': + renumber_map.implementation.ddf = indirection_map.merge( + renumbering_map, + right_on='original_ids', left_on='global_id', + how='right').\ + drop(columns=['global_id', 'original_ids'])\ + .rename(columns={'new_ids': 'global_id'}) + else: + renumber_map.implementation.ddf = renumbering_map.rename( + columns={'original_ids': '0', 'new_ids': 'global_id'}) + renumber_map.implementation.numbered = True + return renumbered_df, renumber_map, aggregate_segment_offsets + + else: + renumbering_map, segment_offsets, renumbered_df = \ + c_renumber.renumber(df, + num_edges, + 0, + Comms.get_default_handle(), + is_mnmg, + store_transposed) + if renumber_type == 'legacy': + renumber_map.implementation.df = indirection_map.\ + merge(renumbering_map, + right_on='original_ids', left_on='id').\ + drop(columns=['id', 'original_ids'])\ + .rename(columns={'new_ids': 'id'}, copy=False) + else: + renumber_map.implementation.df = renumbering_map.rename( + columns={'original_ids': '0', 'new_ids': 'id'}, copy=False) + + renumber_map.implementation.numbered = True + return renumbered_df, renumber_map, segment_offsets + + def renumber(df, src_col_names, dst_col_names, preserve_order=False, + store_transposed=False): + return NumberMap.renumber_and_segment( + df, src_col_names, dst_col_names, + preserve_order, store_transposed)[0:2] - def unrenumber(self, df, column_name, preserve_order=False): + def unrenumber(self, df, column_name, preserve_order=False, + get_column_names=False): """ Given a DataFrame containing internal vertex ids in the identified column, replace this with external vertex ids. If the renumbering @@ -837,30 +612,29 @@ def unrenumber(self, df, column_name, preserve_order=False): name for the external vertex identifiers. If the renumbering is from a multi-column input, the output columns will be labeled 0 through n-1 with a suffix of _column_name. - Note that this function does not guarantee order or partitioning in multi-GPU mode. - Parameters ---------- df: cudf.DataFrame or dask_cudf.DataFrame A DataFrame containing internal vertex identifiers that will be converted into external vertex identifiers. - column_name: string Name of the column containing the internal vertex id. - preserve_order: (optional) bool If True, preserve the ourder of the rows in the output DataFrame to match the input DataFrame - + get_column_names: (optional) bool + If True, the unrenumbered column names are returned. Returns --------- df : cudf.DataFrame or dask_cudf.DataFrame The original DataFrame columns exist unmodified. The external vertex identifiers are added to the DataFrame, the internal vertex identifier column is removed from the dataframe. - + column_names: string or list of strings + If get_column_names is True, the unrenumbered column names are + returned. Examples -------- >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', @@ -880,11 +654,13 @@ def unrenumber(self, df, column_name, preserve_order=False): if len(self.implementation.col_names) == 1: # Output will be renamed to match input mapping = {"0": column_name} + col_names = column_name else: # Output will be renamed to ${i}_${column_name} mapping = {} for nm in self.implementation.col_names: mapping[nm] = nm + "_" + column_name + col_names = list(mapping.values()) if preserve_order: index_name = NumberMap.generate_unused_column_name(df) @@ -895,11 +671,18 @@ def unrenumber(self, df, column_name, preserve_order=False): if preserve_order: df = df.sort_values( index_name - ).drop(index_name).reset_index(drop=True) + ).drop(columns=index_name).reset_index(drop=True) if type(df) is dask_cudf.DataFrame: - return df.map_partitions( + df = df.map_partitions( lambda df: df.rename(columns=mapping, copy=False) ) else: - return df.rename(columns=mapping, copy=False) + df = df.rename(columns=mapping, copy=False) + if get_column_names: + return df, col_names + else: + return df + + def vertex_column_size(self): + return len(self.implementation.col_names) diff --git a/python/cugraph/structure/renumber_wrapper.pyx b/python/cugraph/structure/renumber_wrapper.pyx new file mode 100644 index 00000000000..4433bbb09cb --- /dev/null +++ b/python/cugraph/structure/renumber_wrapper.pyx @@ -0,0 +1,566 @@ +# +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from cugraph.structure.utils_wrapper import * +import cudf +from cugraph.structure.graph_utilities cimport * +import cugraph.structure.graph_primtypes_wrapper as graph_primtypes_wrapper +from libc.stdint cimport uintptr_t +from cython.operator cimport dereference as deref +import numpy as np + +from libcpp.memory cimport make_unique +from libcpp.utility cimport move +from rmm._lib.device_buffer cimport device_buffer, DeviceBuffer + +cdef renumber_helper(shuffled_vertices_t* ptr_maj_min_w, vertex_t, weights): + # extract shuffled result: + # + cdef pair[unique_ptr[device_buffer], size_t] pair_s_major = deref(ptr_maj_min_w).get_major_wrap() + cdef pair[unique_ptr[device_buffer], size_t] pair_s_minor = deref(ptr_maj_min_w).get_minor_wrap() + cdef pair[unique_ptr[device_buffer], size_t] pair_s_weights = deref(ptr_maj_min_w).get_weights_wrap() + + shuffled_major_buffer = DeviceBuffer.c_from_unique_ptr(move(pair_s_major.first)) + shuffled_major_buffer = Buffer(shuffled_major_buffer) + + shuffled_major_series = cudf.Series(data=shuffled_major_buffer, dtype=vertex_t) + + shuffled_minor_buffer = DeviceBuffer.c_from_unique_ptr(move(pair_s_minor.first)) + shuffled_minor_buffer = Buffer(shuffled_minor_buffer) + + shuffled_minor_series = cudf.Series(data=shuffled_minor_buffer, dtype=vertex_t) + + shuffled_df = cudf.DataFrame() + shuffled_df['major_vertices']=shuffled_major_series + shuffled_df['minor_vertices']=shuffled_minor_series + + if weights is not None: + weight_t = weights.dtype + shuffled_weights_buffer = DeviceBuffer.c_from_unique_ptr(move(pair_s_weights.first)) + shuffled_weights_buffer = Buffer(shuffled_weights_buffer) + + shuffled_weights_series = cudf.Series(data=shuffled_weights_buffer, dtype=weight_t) + + shuffled_df['value']= shuffled_weights_series + + return shuffled_df + + +def renumber(input_df, # maybe use cpdef ? + num_global_edges, + rank, + handle, + is_multi_gpu, + transposed): + """ + Call MNMG renumber + """ + cdef size_t handle_size_t = handle.getHandle() + # TODO: get handle_t out of handle... + handle_ptr = handle_size_t + + if not transposed: + major_vertices = input_df['src'] + minor_vertices = input_df['dst'] + else: + major_vertices = input_df['dst'] + minor_vertices = input_df['src'] + + cdef uintptr_t c_edge_weights = NULL # set below... + + vertex_t = major_vertices.dtype + if num_global_edges > (2**31 - 1): + edge_t = np.dtype("int64") + else: + edge_t = vertex_t + if "value" in input_df.columns: + weights = input_df['value'] + weight_t = weights.dtype + c_edge_weights = weights.__cuda_array_interface__['data'][0] + else: + weights = None + weight_t = np.dtype("float32") + + if (vertex_t != np.dtype("int32") and vertex_t != np.dtype("int64")): + raise Exception("Incorrect vertex_t type.") + if (edge_t != np.dtype("int32") and edge_t != np.dtype("int64")): + raise Exception("Incorrect edge_t type.") + if (weight_t != np.dtype("float32") and weight_t != np.dtype("float64")): + raise Exception("Incorrect weight_t type.") + if (vertex_t != np.dtype("int32") and edge_t != np.dtype("int64")): + raise Exception("Incompatible vertex_t and edge_t types.") + + # FIXME: needs to be edge_t type not int + cdef int num_local_edges = len(major_vertices) + + cdef uintptr_t c_major_vertices = major_vertices.__cuda_array_interface__['data'][0] + cdef uintptr_t c_minor_vertices = minor_vertices.__cuda_array_interface__['data'][0] + + cdef uintptr_t shuffled_major = NULL + cdef uintptr_t shuffled_minor = NULL + + # FIXME: Fix fails when do_check = True + cdef bool do_check = False # ? for now... + cdef bool mg_flag = is_multi_gpu # run Single-GPU or MNMG + + cdef pair[unique_ptr[device_buffer], size_t] pair_original + + # tparams: vertex_t, edge_t, weight_t: + # + cdef unique_ptr[major_minor_weights_t[int, int, float]] ptr_shuffled_32_32_32 + cdef unique_ptr[major_minor_weights_t[int, int, double]] ptr_shuffled_32_32_64 + cdef unique_ptr[major_minor_weights_t[int, long, float]] ptr_shuffled_32_64_32 + cdef unique_ptr[major_minor_weights_t[int, long, double]] ptr_shuffled_32_64_64 + cdef unique_ptr[major_minor_weights_t[long, long, float]] ptr_shuffled_64_64_32 + cdef unique_ptr[major_minor_weights_t[long, long, double]] ptr_shuffled_64_64_64 + + # tparams: vertex_t, edge_t: + # + cdef unique_ptr[renum_tuple_t[int, int]] ptr_renum_tuple_32_32 + cdef unique_ptr[renum_tuple_t[int, long]] ptr_renum_tuple_32_64 + cdef unique_ptr[renum_tuple_t[long, long]] ptr_renum_tuple_64_64 + + # tparam: vertex_t: + # + cdef unique_ptr[vector[int]] edge_counts_32 + cdef unique_ptr[vector[long]] edge_counts_64 + + # tparam: vertex_t: + # + cdef unique_ptr[vector[int]] uniq_partition_vector_32 + cdef unique_ptr[vector[long]] uniq_partition_vector_64 + + # tparam: vertex_t: + # + cdef unique_ptr[vector[int]] uniq_segment_vector_32 + cdef unique_ptr[vector[long]] uniq_segment_vector_64 + + cdef size_t rank_indx = rank + + if (vertex_t == np.dtype("int32")): + if ( edge_t == np.dtype("int32")): + if( weight_t == np.dtype("float32")): + if(is_multi_gpu): + ptr_shuffled_32_32_32.reset(call_shuffle[int, int, float](deref(handle_ptr), + c_major_vertices, + c_minor_vertices, + c_edge_weights, + num_local_edges).release()) + shuffled_df = renumber_helper(ptr_shuffled_32_32_32.get(), vertex_t, weights) + major_vertices = shuffled_df['major_vertices'] + minor_vertices = shuffled_df['minor_vertices'] + num_local_edges = len(shuffled_df) + if not transposed: + major = 'src'; minor = 'dst' + else: + major = 'dst'; minor = 'src' + shuffled_df = shuffled_df.rename(columns={'major_vertices':major, 'minor_vertices':minor}, copy=False) + edge_counts_32 = move(ptr_shuffled_32_32_32.get().get_edge_counts_wrap()) + else: + shuffled_df = input_df + edge_counts_32 = make_unique[vector[int]](1, num_local_edges) + + shuffled_major = major_vertices.__cuda_array_interface__['data'][0] + shuffled_minor = minor_vertices.__cuda_array_interface__['data'][0] + + ptr_renum_tuple_32_32.reset(call_renumber[int, int](deref(handle_ptr), + shuffled_major, + shuffled_minor, + deref(edge_counts_32.get()), + do_check, + mg_flag).release()) + + pair_original = ptr_renum_tuple_32_32.get().get_dv_wrap() # original vertices: see helper + + + original_buffer = DeviceBuffer.c_from_unique_ptr(move(pair_original.first)) + original_buffer = Buffer(original_buffer) + + original_series = cudf.Series(data=original_buffer, dtype=vertex_t) + + # extract unique_ptr[partition_offsets]: + # + uniq_partition_vector_32 = move(ptr_renum_tuple_32_32.get().get_partition_offsets_wrap()) + + # create series out of a partition range from rank to rank+1: + # + if is_multi_gpu: + new_series = cudf.Series(np.arange(uniq_partition_vector_32.get()[0].at(rank_indx), + uniq_partition_vector_32.get()[0].at(rank_indx+1)), + dtype=vertex_t) + else: + new_series = cudf.Series(np.arange(0, ptr_renum_tuple_32_32.get().get_num_vertices()), + dtype=vertex_t) + # create new cudf df + # + # and add the previous series to it: + # + renumbered_map = cudf.DataFrame() + renumbered_map['original_ids'] = original_series + renumbered_map['new_ids'] = new_series + + uniq_segment_vector_32 = move(ptr_renum_tuple_32_32.get().get_segment_offsets_wrap()) + segment_offsets = [None] * (deref(uniq_segment_vector_32).size()) + for i in range(len(segment_offsets)): + segment_offsets[i] = deref(uniq_segment_vector_32)[i] + + return renumbered_map, segment_offsets, shuffled_df + + elif( weight_t == np.dtype("float64")): + if(is_multi_gpu): + ptr_shuffled_32_32_64.reset(call_shuffle[int, int, double](deref(handle_ptr), + c_major_vertices, + c_minor_vertices, + c_edge_weights, + num_local_edges).release()) + + shuffled_df = renumber_helper(ptr_shuffled_32_32_64.get(), vertex_t, weights) + major_vertices = shuffled_df['major_vertices'] + minor_vertices = shuffled_df['minor_vertices'] + num_local_edges = len(shuffled_df) + if not transposed: + major = 'src'; minor = 'dst' + else: + major = 'dst'; minor = 'src' + shuffled_df = shuffled_df.rename(columns={'major_vertices':major, 'minor_vertices':minor}, copy=False) + edge_counts_32 = move(ptr_shuffled_32_32_64.get().get_edge_counts_wrap()) + else: + shuffled_df = input_df + edge_counts_32 = make_unique[vector[int]](1, num_local_edges) + + shuffled_major = major_vertices.__cuda_array_interface__['data'][0] + shuffled_minor = minor_vertices.__cuda_array_interface__['data'][0] + + ptr_renum_tuple_32_32.reset(call_renumber[int, int](deref(handle_ptr), + shuffled_major, + shuffled_minor, + deref(edge_counts_32.get()), + do_check, + mg_flag).release()) + + pair_original = ptr_renum_tuple_32_32.get().get_dv_wrap() # original vertices: see helper + + + original_buffer = DeviceBuffer.c_from_unique_ptr(move(pair_original.first)) + original_buffer = Buffer(original_buffer) + + original_series = cudf.Series(data=original_buffer, dtype=vertex_t) + + # extract unique_ptr[partition_offsets]: + # + uniq_partition_vector_32 = move(ptr_renum_tuple_32_32.get().get_partition_offsets_wrap()) + + # create series out of a partition range from rank to rank+1: + # + if is_multi_gpu: + new_series = cudf.Series(np.arange(uniq_partition_vector_32.get()[0].at(rank_indx), + uniq_partition_vector_32.get()[0].at(rank_indx+1)), + dtype=vertex_t) + else: + new_series = cudf.Series(np.arange(0, ptr_renum_tuple_32_32.get().get_num_vertices()), + dtype=vertex_t) + + # create new cudf df + # + # and add the previous series to it: + # + renumbered_map = cudf.DataFrame() + renumbered_map['original_ids'] = original_series + renumbered_map['new_ids'] = new_series + + uniq_segment_vector_32 = move(ptr_renum_tuple_32_32.get().get_segment_offsets_wrap()) + segment_offsets = [None] * (deref(uniq_segment_vector_32).size()) + for i in range(len(segment_offsets)): + segment_offsets[i] = deref(uniq_segment_vector_32)[i] + + return renumbered_map, segment_offsets, shuffled_df + + elif ( edge_t == np.dtype("int64")): + if( weight_t == np.dtype("float32")): + if(is_multi_gpu): + ptr_shuffled_32_64_32.reset(call_shuffle[int, long, float](deref(handle_ptr), + c_major_vertices, + c_minor_vertices, + c_edge_weights, + num_local_edges).release()) + + shuffled_df = renumber_helper(ptr_shuffled_32_64_32.get(), vertex_t, weights) + major_vertices = shuffled_df['major_vertices'] + minor_vertices = shuffled_df['minor_vertices'] + num_local_edges = len(shuffled_df) + if not transposed: + major = 'src'; minor = 'dst' + else: + major = 'dst'; minor = 'src' + shuffled_df = shuffled_df.rename(columns={'major_vertices':major, 'minor_vertices':minor}, copy=False) + edge_counts_64 = move(ptr_shuffled_32_64_32.get().get_edge_counts_wrap()) + else: + shuffled_df = input_df + edge_counts_64 = make_unique[vector[long]](1, num_local_edges) + + shuffled_major = major_vertices.__cuda_array_interface__['data'][0] + shuffled_minor = minor_vertices.__cuda_array_interface__['data'][0] + + ptr_renum_tuple_32_64.reset(call_renumber[int, long](deref(handle_ptr), + shuffled_major, + shuffled_minor, + deref(edge_counts_64.get()), + do_check, + mg_flag).release()) + + pair_original = ptr_renum_tuple_32_64.get().get_dv_wrap() # original vertices: see helper + + + original_buffer = DeviceBuffer.c_from_unique_ptr(move(pair_original.first)) + original_buffer = Buffer(original_buffer) + + original_series = cudf.Series(data=original_buffer, dtype=vertex_t) + + # extract unique_ptr[partition_offsets]: + # + uniq_partition_vector_32 = move(ptr_renum_tuple_32_64.get().get_partition_offsets_wrap()) + + # create series out of a partition range from rank to rank+1: + # + if is_multi_gpu: + new_series = cudf.Series(np.arange(uniq_partition_vector_32.get()[0].at(rank_indx), + uniq_partition_vector_32.get()[0].at(rank_indx+1)), + dtype=vertex_t) + else: + new_series = cudf.Series(np.arange(0, ptr_renum_tuple_32_64.get().get_num_vertices()), + dtype=vertex_t) + + # create new cudf df + # + # and add the previous series to it: + # + renumbered_map = cudf.DataFrame() + renumbered_map['original_ids'] = original_series + renumbered_map['new_ids'] = new_series + + uniq_segment_vector_32 = move(ptr_renum_tuple_32_64.get().get_segment_offsets_wrap()) + segment_offsets = [None] * (deref(uniq_segment_vector_32).size()) + for i in range(len(segment_offsets)): + segment_offsets[i] = deref(uniq_segment_vector_32)[i] + + return renumbered_map, segment_offsets, shuffled_df + elif( weight_t == np.dtype("float64")): + if(is_multi_gpu): + ptr_shuffled_32_64_64.reset(call_shuffle[int, long, double](deref(handle_ptr), + c_major_vertices, + c_minor_vertices, + c_edge_weights, + num_local_edges).release()) + + shuffled_df = renumber_helper(ptr_shuffled_32_64_64.get(), vertex_t, weights) + major_vertices = shuffled_df['major_vertices'] + minor_vertices = shuffled_df['minor_vertices'] + num_local_edges = len(shuffled_df) + if not transposed: + major = 'src'; minor = 'dst' + else: + major = 'dst'; minor = 'src' + shuffled_df = shuffled_df.rename(columns={'major_vertices':major, 'minor_vertices':minor}, copy=False) + edge_counts_64 = move(ptr_shuffled_32_64_64.get().get_edge_counts_wrap()) + else: + shuffled_df = input_df + edge_counts_64 = make_unique[vector[long]](1, num_local_edges) + + shuffled_major = major_vertices.__cuda_array_interface__['data'][0] + shuffled_minor = minor_vertices.__cuda_array_interface__['data'][0] + + ptr_renum_tuple_32_64.reset(call_renumber[int, long](deref(handle_ptr), + shuffled_major, + shuffled_minor, + deref(edge_counts_64.get()), + do_check, + mg_flag).release()) + + pair_original = ptr_renum_tuple_32_64.get().get_dv_wrap() # original vertices: see helper + + + original_buffer = DeviceBuffer.c_from_unique_ptr(move(pair_original.first)) + original_buffer = Buffer(original_buffer) + + original_series = cudf.Series(data=original_buffer, dtype=vertex_t) + + # extract unique_ptr[partition_offsets]: + # + uniq_partition_vector_32 = move(ptr_renum_tuple_32_64.get().get_partition_offsets_wrap()) + + # create series out of a partition range from rank to rank+1: + # + if is_multi_gpu: + new_series = cudf.Series(np.arange(uniq_partition_vector_32.get()[0].at(rank_indx), + uniq_partition_vector_32.get()[0].at(rank_indx+1)), + dtype=vertex_t) + else: + new_series = cudf.Series(np.arange(0, ptr_renum_tuple_32_64.get().get_num_vertices()), + dtype=vertex_t) + # create new cudf df + # + # and add the previous series to it: + # + renumbered_map = cudf.DataFrame() + renumbered_map['original_ids'] = original_series + renumbered_map['new_ids'] = new_series + + uniq_segment_vector_32 = move(ptr_renum_tuple_32_64.get().get_segment_offsets_wrap()) + segment_offsets = [None] * (deref(uniq_segment_vector_32).size()) + for i in range(len(segment_offsets)): + segment_offsets[i] = deref(uniq_segment_vector_32)[i] + + return renumbered_map, segment_offsets, shuffled_df + + elif (vertex_t == np.dtype("int64")): + if ( edge_t == np.dtype("int64")): + if( weight_t == np.dtype("float32")): + if(is_multi_gpu): + ptr_shuffled_64_64_32.reset(call_shuffle[long, long, float](deref(handle_ptr), + c_major_vertices, + c_minor_vertices, + c_edge_weights, + num_local_edges).release()) + + shuffled_df = renumber_helper(ptr_shuffled_64_64_32.get(), vertex_t, weights) + major_vertices = shuffled_df['major_vertices'] + minor_vertices = shuffled_df['minor_vertices'] + num_local_edges = len(shuffled_df) + if not transposed: + major = 'src'; minor = 'dst' + else: + major = 'dst'; minor = 'src' + shuffled_df = shuffled_df.rename(columns={'major_vertices':major, 'minor_vertices':minor}, copy=False) + edge_counts_64 = move(ptr_shuffled_64_64_32.get().get_edge_counts_wrap()) + else: + shuffled_df = input_df + edge_counts_64 = make_unique[vector[long]](1, num_local_edges) + + shuffled_major = major_vertices.__cuda_array_interface__['data'][0] + shuffled_minor = minor_vertices.__cuda_array_interface__['data'][0] + + ptr_renum_tuple_64_64.reset(call_renumber[long, long](deref(handle_ptr), + shuffled_major, + shuffled_minor, + deref(edge_counts_64.get()), + do_check, + mg_flag).release()) + + pair_original = ptr_renum_tuple_64_64.get().get_dv_wrap() # original vertices: see helper + + + original_buffer = DeviceBuffer.c_from_unique_ptr(move(pair_original.first)) + original_buffer = Buffer(original_buffer) + + original_series = cudf.Series(data=original_buffer, dtype=vertex_t) + + # extract unique_ptr[partition_offsets]: + # + uniq_partition_vector_64 = move(ptr_renum_tuple_64_64.get().get_partition_offsets_wrap()) + + # create series out of a partition range from rank to rank+1: + # + if is_multi_gpu: + new_series = cudf.Series(np.arange(uniq_partition_vector_64.get()[0].at(rank_indx), + uniq_partition_vector_64.get()[0].at(rank_indx+1)), + dtype=vertex_t) + else: + new_series = cudf.Series(np.arange(0, ptr_renum_tuple_64_64.get().get_num_vertices()), + dtype=vertex_t) + + # create new cudf df + # + # and add the previous series to it: + # + renumbered_map = cudf.DataFrame() + renumbered_map['original_ids'] = original_series + renumbered_map['new_ids'] = new_series + + uniq_segment_vector_64 = move(ptr_renum_tuple_64_64.get().get_segment_offsets_wrap()) + segment_offsets = [None] * (deref(uniq_segment_vector_64).size()) + for i in range(len(segment_offsets)): + segment_offsets[i] = deref(uniq_segment_vector_64)[i] + + return renumbered_map, segment_offsets, shuffled_df + + elif( weight_t == np.dtype("float64")): + if(is_multi_gpu): + ptr_shuffled_64_64_64.reset(call_shuffle[long, long, double](deref(handle_ptr), + c_major_vertices, + c_minor_vertices, + c_edge_weights, + num_local_edges).release()) + + shuffled_df = renumber_helper(ptr_shuffled_64_64_64.get(), vertex_t, weights) + major_vertices = shuffled_df['major_vertices'] + minor_vertices = shuffled_df['minor_vertices'] + num_local_edges = len(shuffled_df) + if not transposed: + major = 'src'; minor = 'dst' + else: + major = 'dst'; minor = 'src' + shuffled_df = shuffled_df.rename(columns={'major_vertices':major, 'minor_vertices':minor}, copy=False) + edge_counts_64 = move(ptr_shuffled_64_64_64.get().get_edge_counts_wrap()) + else: + shuffled_df = input_df + edge_counts_64 = make_unique[vector[long]](1, num_local_edges) + + shuffled_major = major_vertices.__cuda_array_interface__['data'][0] + shuffled_minor = minor_vertices.__cuda_array_interface__['data'][0] + + ptr_renum_tuple_64_64.reset(call_renumber[long, long](deref(handle_ptr), + shuffled_major, + shuffled_minor, + deref(edge_counts_64.get()), + do_check, + mg_flag).release()) + + pair_original = ptr_renum_tuple_64_64.get().get_dv_wrap() # original vertices: see helper + + + original_buffer = DeviceBuffer.c_from_unique_ptr(move(pair_original.first)) + original_buffer = Buffer(original_buffer) + + original_series = cudf.Series(data=original_buffer, dtype=vertex_t) + + # extract unique_ptr[partition_offsets]: + # + uniq_partition_vector_64 = move(ptr_renum_tuple_64_64.get().get_partition_offsets_wrap()) + + # create series out of a partition range from rank to rank+1: + # + if is_multi_gpu: + new_series = cudf.Series(np.arange(uniq_partition_vector_64.get()[0].at(rank_indx), + uniq_partition_vector_64.get()[0].at(rank_indx+1)), + dtype=vertex_t) + else: + new_series = cudf.Series(np.arange(0, ptr_renum_tuple_64_64.get().get_num_vertices()), + dtype=vertex_t) + + # create new cudf df + # + # and add the previous series to it: + # + renumbered_map = cudf.DataFrame() + renumbered_map['original_ids'] = original_series + renumbered_map['new_ids'] = new_series + + uniq_segment_vector_64 = move(ptr_renum_tuple_64_64.get().get_segment_offsets_wrap()) + segment_offsets = [None] * (deref(uniq_segment_vector_64).size()) + for i in range(len(segment_offsets)): + segment_offsets[i] = deref(uniq_segment_vector_64)[i] + + return renumbered_map, segment_offsets, shuffled_df diff --git a/python/cugraph/structure/symmetrize.py b/python/cugraph/structure/symmetrize.py index e7fd15144aa..13116eabb07 100644 --- a/python/cugraph/structure/symmetrize.py +++ b/python/cugraph/structure/symmetrize.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -11,12 +11,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -from cugraph.structure import graph as csg +from cugraph.structure import graph_classes as csg import cudf import dask_cudf +from cugraph.comms import comms as Comms -def symmetrize_df(df, src_name, dst_name): +def symmetrize_df(df, src_name, dst_name, multi=False, symmetrize=True): """ Take a COO stored in a DataFrame, along with the column names of the source and destination columns and create a new data frame @@ -32,6 +33,7 @@ def symmetrize_df(df, src_name, dst_name): != data2 then this code will arbitrarily pick the smaller data element to keep, if this is not desired then the caller should should correct the data prior to calling symmetrize. + Parameters ---------- df : cudf.DataFrame @@ -42,6 +44,13 @@ def symmetrize_df(df, src_name, dst_name): Name of the column in the data frame containing the source ids dst_name : string Name of the column in the data frame containing the destination ids + multi : bool + Set to True if graph is a Multi(Di)Graph. This allows multiple + edges instead of dropping them. + symmetrize : bool + Default is True to perform symmetrization. If False only duplicate + edges are dropped. + Examples -------- >>> import cugraph.dask as dcg @@ -54,26 +63,30 @@ def symmetrize_df(df, src_name, dst_name): >>> sym_ddf = cugraph.symmetrize_ddf(ddf, "src", "dst", "weight") >>> Comms.destroy() """ - gdf = cudf.DataFrame() - # # Now append the columns. We add sources to the end of destinations, # and destinations to the end of sources. Otherwise we append a # column onto itself. # - for idx, name in enumerate(df.columns): - if name == src_name: - gdf[src_name] = df[src_name].append( - df[dst_name], ignore_index=True - ) - elif name == dst_name: - gdf[dst_name] = df[dst_name].append( - df[src_name], ignore_index=True - ) - else: - gdf[name] = df[name].append(df[name], ignore_index=True) - - return gdf.groupby(by=[src_name, dst_name], as_index=False).min() + if symmetrize: + gdf = cudf.DataFrame() + for idx, name in enumerate(df.columns): + if name == src_name: + gdf[src_name] = df[src_name].append( + df[dst_name], ignore_index=True + ) + elif name == dst_name: + gdf[dst_name] = df[dst_name].append( + df[src_name], ignore_index=True + ) + else: + gdf[name] = df[name].append(df[name], ignore_index=True) + else: + gdf = df + if multi: + return gdf + else: + return gdf.groupby(by=[src_name, dst_name], as_index=False).min() def symmetrize_ddf(df, src_name, dst_name, weight_name=None): @@ -105,6 +118,12 @@ def symmetrize_ddf(df, src_name, dst_name, weight_name=None): Name of the column in the data frame containing the source ids dst_name : string Name of the column in the data frame containing the destination ids + multi : bool + Set to True if graph is a Multi(Di)Graph. This allows multiple + edges instead of dropping them. + symmetrize : bool + Default is True to perform symmetrization. If False only duplicate + edges are dropped. Examples -------- @@ -118,18 +137,19 @@ def symmetrize_ddf(df, src_name, dst_name, weight_name=None): else: ddf2 = df[[dst_name, src_name]] ddf2.columns = [src_name, dst_name] - + worker_list = Comms.get_workers() + num_workers = len(worker_list) ddf = df.append(ddf2).reset_index(drop=True) - result = ( - ddf.groupby(by=[src_name, dst_name], as_index=False) - .min() - .reset_index() - ) + result = ddf.shuffle(on=[ + src_name, dst_name], ignore_index=True, npartitions=num_workers) + result = result.map_partitions(lambda x: x.groupby( + by=[src_name, dst_name], as_index=False).min().reset_index(drop=True)) return result -def symmetrize(source_col, dest_col, value_col=None): +def symmetrize(source_col, dest_col, value_col=None, multi=False, + symmetrize=True): """ Take a COO set of source destination pairs along with associated values stored in a single GPU or distributed @@ -182,20 +202,31 @@ def symmetrize(source_col, dest_col, value_col=None): csg.null_check(source_col) csg.null_check(dest_col) if value_col is not None: - weight_name = "value" - input_df.insert(len(input_df.columns), "value", value_col) + if isinstance(value_col, cudf.Series): + weight_name = "value" + input_df.insert(len(input_df.columns), "value", value_col) + elif isinstance(value_col, cudf.DataFrame): + input_df = cudf.concat([input_df, value_col], axis=1) + output_df = None if type(source_col) is dask_cudf.Series: output_df = symmetrize_ddf( input_df, "source", "destination", weight_name ).persist() else: - output_df = symmetrize_df(input_df, "source", "destination") - + output_df = symmetrize_df(input_df, "source", "destination", multi, + symmetrize) if value_col is not None: - return ( - output_df["source"], - output_df["destination"], - output_df["value"], - ) + if isinstance(value_col, cudf.Series): + return ( + output_df["source"], + output_df["destination"], + output_df["value"], + ) + elif isinstance(value_col, cudf.DataFrame): + return ( + output_df["source"], + output_df["destination"], + output_df[value_col.columns], + ) return output_df["source"], output_df["destination"] diff --git a/python/cugraph/structure/utils.pxd b/python/cugraph/structure/utils.pxd index 0ec9c914347..350b5890149 100644 --- a/python/cugraph/structure/utils.pxd +++ b/python/cugraph/structure/utils.pxd @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -19,11 +19,8 @@ from cugraph.structure.graph_primtypes cimport * from libcpp.memory cimport unique_ptr -cdef extern from "raft/handle.hpp" namespace "raft": - cdef cppclass handle_t: - handle_t() except + -cdef extern from "functions.hpp" namespace "cugraph": +cdef extern from "cugraph/functions.hpp" namespace "cugraph": cdef unique_ptr[GraphCSR[VT,ET,WT]] coo_to_csr[VT,ET,WT]( const GraphCOOView[VT,ET,WT] &graph) except + diff --git a/python/cugraph/structure/utils_wrapper.pyx b/python/cugraph/structure/utils_wrapper.pyx index 00af5813056..65c1ca09750 100644 --- a/python/cugraph/structure/utils_wrapper.pyx +++ b/python/cugraph/structure/utils_wrapper.pyx @@ -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. # You may obtain a copy of the License at @@ -22,7 +22,6 @@ from cugraph.structure.graph_primtypes cimport * from libc.stdint cimport uintptr_t import cudf -import rmm import numpy as np from rmm._lib.device_buffer cimport DeviceBuffer from cudf.core.buffer import Buffer diff --git a/python/cugraph/tests/dask/mg_context.py b/python/cugraph/tests/dask/mg_context.py index a72cf1c4b04..45dc75767fa 100644 --- a/python/cugraph/tests/dask/mg_context.py +++ b/python/cugraph/tests/dask/mg_context.py @@ -12,12 +12,15 @@ # limitations under the License. import time -import os + +import pytest from dask.distributed import Client + +from cugraph.dask.common.mg_utils import get_visible_devices from dask_cuda import LocalCUDACluster as CUDACluster import cugraph.comms as Comms -import pytest + # Maximal number of verifications of the number of workers DEFAULT_MAX_ATTEMPT = 100 @@ -26,22 +29,13 @@ DEFAULT_WAIT_TIME = 0.5 -def get_visible_devices(): - _visible_devices = os.environ.get("CUDA_VISIBLE_DEVICES") - if _visible_devices is None: - # FIXME: We assume that if the variable is unset there is only one GPU - visible_devices = ["0"] - else: - visible_devices = _visible_devices.strip().split(",") - return visible_devices - - def skip_if_not_enough_devices(required_devices): - visible_devices = get_visible_devices() - number_of_visible_devices = len(visible_devices) - if required_devices > number_of_visible_devices: - pytest.skip("Not enough devices available to " - "test MG({})".format(required_devices)) + if required_devices is not None: + visible_devices = get_visible_devices() + number_of_visible_devices = len(visible_devices) + if required_devices > number_of_visible_devices: + pytest.skip("Not enough devices available to " + "test MG({})".format(required_devices)) class MGContext: diff --git a/python/cugraph/tests/dask/test_mg_batch_betweenness_centrality.py b/python/cugraph/tests/dask/test_mg_batch_betweenness_centrality.py index 4d04bf6df85..02696f589e3 100644 --- a/python/cugraph/tests/dask/test_mg_batch_betweenness_centrality.py +++ b/python/cugraph/tests/dask/test_mg_batch_betweenness_centrality.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -16,6 +16,7 @@ from cugraph.tests.dask.mg_context import MGContext, skip_if_not_enough_devices from cugraph.dask.common.mg_utils import is_single_gpu +from cugraph.tests import utils # Get parameters from standard betwenness_centrality_test from cugraph.tests.test_betweenness_centrality import ( @@ -36,8 +37,12 @@ # ============================================================================= # Parameters # ============================================================================= -DATASETS = ["../datasets/karate.csv"] -MG_DEVICE_COUNT_OPTIONS = [1, 2, 3, 4] +DATASETS = [utils.DATASETS_UNDIRECTED[0]] +MG_DEVICE_COUNT_OPTIONS = [pytest.param(1, marks=pytest.mark.preset_gpu_count), + pytest.param(2, marks=pytest.mark.preset_gpu_count), + pytest.param(3, marks=pytest.mark.preset_gpu_count), + pytest.param(4, marks=pytest.mark.preset_gpu_count), + None] RESULT_DTYPE_OPTIONS = [np.float64] @@ -46,7 +51,8 @@ @pytest.mark.skipif( is_single_gpu(), reason="skipping MG testing on Single GPU system" ) -@pytest.mark.parametrize("graph_file", DATASETS) +@pytest.mark.parametrize("graph_file", DATASETS, + ids=[f"dataset={d.as_posix()}" for d in DATASETS]) @pytest.mark.parametrize("directed", DIRECTED_GRAPH_OPTIONS) @pytest.mark.parametrize("subset_size", SUBSET_SIZE_OPTIONS) @pytest.mark.parametrize("normalized", NORMALIZED_OPTIONS) diff --git a/python/cugraph/tests/dask/test_mg_batch_edge_betweenness_centrality.py b/python/cugraph/tests/dask/test_mg_batch_edge_betweenness_centrality.py index 1e4a1950c53..89844797807 100644 --- a/python/cugraph/tests/dask/test_mg_batch_edge_betweenness_centrality.py +++ b/python/cugraph/tests/dask/test_mg_batch_edge_betweenness_centrality.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -37,14 +37,19 @@ # Parameters # ============================================================================= DATASETS = ["../datasets/karate.csv"] -MG_DEVICE_COUNT_OPTIONS = [1, 2, 4] +MG_DEVICE_COUNT_OPTIONS = [pytest.param(1, marks=pytest.mark.preset_gpu_count), + pytest.param(2, marks=pytest.mark.preset_gpu_count), + pytest.param(3, marks=pytest.mark.preset_gpu_count), + pytest.param(4, marks=pytest.mark.preset_gpu_count), + None] RESULT_DTYPE_OPTIONS = [np.float64] @pytest.mark.skipif( is_single_gpu(), reason="skipping MG testing on Single GPU system" ) -@pytest.mark.parametrize("graph_file", DATASETS) +@pytest.mark.parametrize("graph_file", DATASETS, + ids=[f"dataset={d}" for d in DATASETS]) @pytest.mark.parametrize("directed", DIRECTED_GRAPH_OPTIONS) @pytest.mark.parametrize("subset_size", SUBSET_SIZE_OPTIONS) @pytest.mark.parametrize("normalized", NORMALIZED_OPTIONS) diff --git a/python/cugraph/tests/dask/test_mg_bfs.py b/python/cugraph/tests/dask/test_mg_bfs.py index 553bbc698ff..3e83491c87a 100644 --- a/python/cugraph/tests/dask/test_mg_bfs.py +++ b/python/cugraph/tests/dask/test_mg_bfs.py @@ -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. # You may obtain a copy of the License at @@ -12,28 +12,21 @@ # limitations under the License. import cugraph.dask as dcg -import cugraph.comms as Comms -from dask.distributed import Client import gc import pytest import cugraph import dask_cudf import cudf -from dask_cuda import LocalCUDACluster -from cugraph.dask.common.mg_utils import is_single_gpu +from cugraph.dask.common.mg_utils import (is_single_gpu, + setup_local_dask_cluster, + teardown_local_dask_cluster) -@pytest.fixture +@pytest.fixture(scope="module") def client_connection(): - cluster = LocalCUDACluster() - client = Client(cluster) - Comms.initialize(p2p=True) - + (cluster, client) = setup_local_dask_cluster(p2p=True) yield client - - Comms.destroy() - client.close() - cluster.close() + teardown_local_dask_cluster(cluster, client) @pytest.mark.skipif( @@ -42,7 +35,10 @@ def client_connection(): def test_dask_bfs(client_connection): gc.collect() + # FIXME: update this to allow dataset to be parameterized and have dataset + # part of test param id (see other tests) input_data_path = r"../datasets/netscience.csv" + print(f"dataset={input_data_path}") chunksize = dcg.get_chunksize(input_data_path) ddf = dask_cudf.read_csv( @@ -67,9 +63,8 @@ def test_dask_bfs(client_connection): dg.from_dask_cudf_edgelist(ddf, "src", "dst") expected_dist = cugraph.bfs(g, 0) - result_dist = dcg.bfs(dg, 0, True) + result_dist = dcg.bfs(dg, 0, depth_limit=2) result_dist = result_dist.compute() - compare_dist = expected_dist.merge( result_dist, on="vertex", suffixes=["_local", "_dask"] ) @@ -83,3 +78,65 @@ def test_dask_bfs(client_connection): ): err = err + 1 assert err == 0 + + +@pytest.mark.skipif( + is_single_gpu(), reason="skipping MG testing on Single GPU system" +) +def test_dask_bfs_multi_column_depthlimit(client_connection): + gc.collect() + + # FIXME: update this to allow dataset to be parameterized and have dataset + # part of test param id (see other tests) + input_data_path = r"../datasets/netscience.csv" + print(f"dataset={input_data_path}") + chunksize = dcg.get_chunksize(input_data_path) + + ddf = dask_cudf.read_csv( + input_data_path, + chunksize=chunksize, + delimiter=" ", + names=["src_a", "dst_a", "value"], + dtype=["int32", "int32", "float32"], + ) + ddf['src_b'] = ddf['src_a'] + 1000 + ddf['dst_b'] = ddf['dst_a'] + 1000 + + df = cudf.read_csv( + input_data_path, + delimiter=" ", + names=["src_a", "dst_a", "value"], + dtype=["int32", "int32", "float32"], + ) + df['src_b'] = df['src_a'] + 1000 + df['dst_b'] = df['dst_a'] + 1000 + + g = cugraph.DiGraph() + g.from_cudf_edgelist(df, ["src_a", "src_b"], ["dst_a", "dst_b"]) + + dg = cugraph.DiGraph() + dg.from_dask_cudf_edgelist(ddf, ["src_a", "src_b"], ["dst_a", "dst_b"]) + + start = cudf.DataFrame() + start['a'] = [0] + start['b'] = [1000] + + depth_limit = 18 + expected_dist = cugraph.bfs(g, start, depth_limit=depth_limit) + result_dist = dcg.bfs(dg, start, depth_limit=depth_limit) + result_dist = result_dist.compute() + + compare_dist = expected_dist.merge( + result_dist, on=["0_vertex", "1_vertex"], suffixes=["_local", "_dask"] + ) + + err = 0 + for i in range(len(compare_dist)): + if ( + compare_dist["distance_local"].iloc[i] <= depth_limit and + compare_dist["distance_dask"].iloc[i] <= depth_limit and + compare_dist["distance_local"].iloc[i] + != compare_dist["distance_dask"].iloc[i] + ): + err = err + 1 + assert err == 0 diff --git a/python/cugraph/tests/dask/test_mg_comms.py b/python/cugraph/tests/dask/test_mg_comms.py index 29789461018..03a0a5d73d2 100644 --- a/python/cugraph/tests/dask/test_mg_comms.py +++ b/python/cugraph/tests/dask/test_mg_comms.py @@ -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. # You may obtain a copy of the License at @@ -12,28 +12,21 @@ # limitations under the License. import cugraph.dask as dcg -import cugraph.comms as Comms -from dask.distributed import Client import gc import pytest import cugraph import dask_cudf import cudf -from dask_cuda import LocalCUDACluster -from cugraph.dask.common.mg_utils import is_single_gpu +from cugraph.dask.common.mg_utils import (is_single_gpu, + setup_local_dask_cluster, + teardown_local_dask_cluster) -@pytest.fixture +@pytest.fixture(scope="module") def client_connection(): - cluster = LocalCUDACluster() - client = Client(cluster) - Comms.initialize(p2p=True) - + (cluster, client) = setup_local_dask_cluster(p2p=True) yield client - - Comms.destroy() - client.close() - cluster.close() + teardown_local_dask_cluster(cluster, client) @pytest.mark.skipif( @@ -45,10 +38,14 @@ def test_dask_pagerank(client_connection): # Initialize and run pagerank on two distributed graphs # with same communicator + # FIXME: update this to allow dataset to be parameterized and have dataset + # part of test param id (see other tests) input_data_path1 = r"../datasets/karate.csv" + print(f"dataset1={input_data_path1}") chunksize1 = dcg.get_chunksize(input_data_path1) input_data_path2 = r"../datasets/dolphins.csv" + print(f"dataset2={input_data_path2}") chunksize2 = dcg.get_chunksize(input_data_path2) ddf1 = dask_cudf.read_csv( diff --git a/python/cugraph/tests/dask/test_mg_connectivity.py b/python/cugraph/tests/dask/test_mg_connectivity.py new file mode 100644 index 00000000000..2f946789471 --- /dev/null +++ b/python/cugraph/tests/dask/test_mg_connectivity.py @@ -0,0 +1,78 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import cugraph.dask as dcg +import gc +import pytest +import cugraph +import dask_cudf +import cudf +from cugraph.dask.common.mg_utils import (is_single_gpu, + setup_local_dask_cluster, + teardown_local_dask_cluster) + + +@pytest.fixture(scope="module") +def client_connection(): + (cluster, client) = setup_local_dask_cluster(p2p=True) + yield client + teardown_local_dask_cluster(cluster, client) + + +@pytest.mark.skipif( + is_single_gpu(), reason="skipping MG testing on Single GPU system" +) +def test_dask_wcc(client_connection): + gc.collect() + + # FIXME: update this to allow dataset to be parameterized and have dataset + # part of test param id (see other tests) + input_data_path = r"../datasets/netscience.csv" + print(f"dataset={input_data_path}") + chunksize = dcg.get_chunksize(input_data_path) + + ddf = dask_cudf.read_csv( + input_data_path, + chunksize=chunksize, + delimiter=" ", + names=["src", "dst", "value"], + dtype=["int32", "int32", "float32"], + ) + + df = cudf.read_csv( + input_data_path, + delimiter=" ", + names=["src", "dst", "value"], + dtype=["int32", "int32", "float32"], + ) + + g = cugraph.DiGraph() + g.from_cudf_edgelist(df, "src", "dst", renumber=True) + + dg = cugraph.DiGraph() + dg.from_dask_cudf_edgelist(ddf, "src", "dst") + + expected_dist = cugraph.weakly_connected_components(g) + result_dist = dcg.weakly_connected_components(dg) + + result_dist = result_dist.compute() + compare_dist = expected_dist.merge( + result_dist, on="vertex", suffixes=["_local", "_dask"] + ) + + unique_local_labels = compare_dist['labels_local'].unique() + + for label in unique_local_labels.values.tolist(): + dask_labels_df = compare_dist[compare_dist['labels_local'] == label] + dask_labels = dask_labels_df['labels_dask'] + assert (dask_labels.iloc[0] == dask_labels).all() diff --git a/python/cugraph/tests/dask/test_mg_degree.py b/python/cugraph/tests/dask/test_mg_degree.py index a6600104bc8..bad55df1ca9 100644 --- a/python/cugraph/tests/dask/test_mg_degree.py +++ b/python/cugraph/tests/dask/test_mg_degree.py @@ -1,4 +1,4 @@ -# Copyright (c) 2018-2020, NVIDIA CORPORATION. +# Copyright (c) 2018-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. # You may obtain a copy of the License at @@ -11,30 +11,21 @@ # See the License for the specific language governing permissions and # limitations under the License. -from dask.distributed import Client import gc import pytest import cudf -import cugraph.comms as Comms import cugraph import dask_cudf -from cugraph.dask.common.mg_utils import is_single_gpu +from cugraph.dask.common.mg_utils import (is_single_gpu, + setup_local_dask_cluster, + teardown_local_dask_cluster) -# Move to conftest -from dask_cuda import LocalCUDACluster - -@pytest.fixture +@pytest.fixture(scope="module") def client_connection(): - cluster = LocalCUDACluster() - client = Client(cluster) - Comms.initialize(p2p=True) - + (cluster, client) = setup_local_dask_cluster(p2p=True) yield client - - Comms.destroy() - client.close() - cluster.close() + teardown_local_dask_cluster(cluster, client) @pytest.mark.skipif( @@ -43,7 +34,10 @@ def client_connection(): def test_dask_mg_degree(client_connection): gc.collect() - input_data_path = r"../datasets/karate.csv" + # FIXME: update this to allow dataset to be parameterized and have dataset + # part of test param id (see other tests) + input_data_path = r"../datasets/karate-asymmetric.csv" + print(f"dataset={input_data_path}") chunksize = cugraph.dask.get_chunksize(input_data_path) @@ -68,10 +62,18 @@ def test_dask_mg_degree(client_connection): g = cugraph.DiGraph() g.from_cudf_edgelist(df, "src", "dst") - merge_df = ( + merge_df_in = ( dg.in_degree() .merge(g.in_degree(), on="vertex", suffixes=["_dg", "_g"]) .compute() ) - assert merge_df["degree_dg"].equals(merge_df["degree_g"]) + merge_df_out = ( + dg.out_degree() + .merge(g.out_degree(), on="vertex", suffixes=["_dg", "_g"]) + .compute() + ) + + assert merge_df_in["degree_dg"].equals(merge_df_in["degree_g"]) + assert merge_df_out["degree_dg"].equals( + merge_df_out["degree_g"]) diff --git a/python/cugraph/tests/dask/test_mg_katz_centrality.py b/python/cugraph/tests/dask/test_mg_katz_centrality.py index 43d63f2fd5d..8ed604954f4 100644 --- a/python/cugraph/tests/dask/test_mg_katz_centrality.py +++ b/python/cugraph/tests/dask/test_mg_katz_centrality.py @@ -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. # You may obtain a copy of the License at @@ -14,30 +14,20 @@ # import numpy as np import pytest import cugraph.dask as dcg -import cugraph.comms as Comms -from dask.distributed import Client import gc import cugraph import dask_cudf import cudf -from dask_cuda import LocalCUDACluster -from cugraph.dask.common.mg_utils import is_single_gpu +from cugraph.dask.common.mg_utils import (is_single_gpu, + setup_local_dask_cluster, + teardown_local_dask_cluster) -# The function selects personalization_perc% of accessible vertices in graph M -# and randomly assigns them personalization values - -@pytest.fixture +@pytest.fixture(scope="module") def client_connection(): - cluster = LocalCUDACluster() - client = Client(cluster) - Comms.initialize(p2p=True) - + (cluster, client) = setup_local_dask_cluster(p2p=True) yield client - - Comms.destroy() - client.close() - cluster.close() + teardown_local_dask_cluster(cluster, client) @pytest.mark.skipif( @@ -46,7 +36,10 @@ def client_connection(): def test_dask_katz_centrality(client_connection): gc.collect() + # FIXME: update this to allow dataset to be parameterized and have dataset + # part of test param id (see other tests) input_data_path = r"../datasets/karate.csv" + print(f"dataset={input_data_path}") chunksize = dcg.get_chunksize(input_data_path) ddf = dask_cudf.read_csv( @@ -57,21 +50,12 @@ def test_dask_katz_centrality(client_connection): dtype=["int32", "int32", "float32"], ) - df = cudf.read_csv( - input_data_path, - delimiter=" ", - names=["src", "dst", "value"], - dtype=["int32", "int32", "float32"], - ) - - g = cugraph.DiGraph() - g.from_cudf_edgelist(df, "src", "dst") - dg = cugraph.DiGraph() dg.from_dask_cudf_edgelist(ddf, "src", "dst") - largest_out_degree = g.degrees().nlargest(n=1, columns="out_degree") - largest_out_degree = largest_out_degree["out_degree"].iloc[0] + largest_out_degree = dg.out_degree().compute().\ + nlargest(n=1, columns="degree") + largest_out_degree = largest_out_degree["degree"].iloc[0] katz_alpha = 1 / (largest_out_degree + 1) mg_res = dcg.katz_centrality(dg, alpha=katz_alpha, tol=1e-6) diff --git a/python/cugraph/tests/dask/test_mg_louvain.py b/python/cugraph/tests/dask/test_mg_louvain.py index 56401e338a4..c67d8fcb1f9 100644 --- a/python/cugraph/tests/dask/test_mg_louvain.py +++ b/python/cugraph/tests/dask/test_mg_louvain.py @@ -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. # You may obtain a copy of the License at @@ -14,13 +14,12 @@ import pytest import cugraph.dask as dcg -import cugraph.comms as Comms -from dask.distributed import Client import cugraph import dask_cudf -from dask_cuda import LocalCUDACluster from cugraph.tests import utils -from cugraph.dask.common.mg_utils import is_single_gpu +from cugraph.dask.common.mg_utils import (is_single_gpu, + setup_local_dask_cluster, + teardown_local_dask_cluster) try: from rapids_pytest_benchmark import setFixtureParamNames @@ -44,23 +43,18 @@ def setFixtureParamNames(*args, **kwargs): # Fixtures @pytest.fixture(scope="module") def client_connection(): - # setup - cluster = LocalCUDACluster() - client = Client(cluster) - Comms.initialize(p2p=True) - + (cluster, client) = setup_local_dask_cluster(p2p=True) yield client - - # teardown - Comms.destroy() - client.close() - cluster.close() + teardown_local_dask_cluster(cluster, client) @pytest.mark.skipif( is_single_gpu(), reason="skipping MG testing on Single GPU system" ) -@pytest.fixture(scope="module", params=utils.DATASETS_UNDIRECTED) +@pytest.fixture(scope="module", + params=utils.DATASETS_UNDIRECTED, + ids=[f"dataset={d.as_posix()}" + for d in utils.DATASETS_UNDIRECTED]) def daskGraphFromDataset(request, client_connection): """ Returns a new dask dataframe created from the dataset file param. @@ -96,7 +90,7 @@ def test_mg_louvain_with_edgevals(daskGraphFromDataset): parts, mod = dcg.louvain(daskGraphFromDataset) # FIXME: either call Nx with the same dataset and compare results, or - # hadcode golden results to compare to. + # hardcode golden results to compare to. print() print(parts.compute()) print(mod) diff --git a/python/cugraph/tests/dask/test_mg_pagerank.py b/python/cugraph/tests/dask/test_mg_pagerank.py index f6416903b89..9cb00010311 100644 --- a/python/cugraph/tests/dask/test_mg_pagerank.py +++ b/python/cugraph/tests/dask/test_mg_pagerank.py @@ -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. # You may obtain a copy of the License at @@ -13,19 +13,18 @@ import numpy as np import pytest import cugraph.dask as dcg -import cugraph.comms as Comms -from dask.distributed import Client import gc import cugraph import dask_cudf import cudf -from dask_cuda import LocalCUDACluster -from cugraph.dask.common.mg_utils import is_single_gpu +from cugraph.dask.common.mg_utils import (is_single_gpu, + setup_local_dask_cluster, + teardown_local_dask_cluster) + # The function selects personalization_perc% of accessible vertices in graph M # and randomly assigns them personalization values - def personalize(vertices, personalization_perc): personalization = None if personalization_perc != 0: @@ -52,17 +51,11 @@ def personalize(vertices, personalization_perc): PERSONALIZATION_PERC = [0, 10, 50] -@pytest.fixture +@pytest.fixture(scope="module") def client_connection(): - cluster = LocalCUDACluster() - client = Client(cluster) - Comms.initialize(p2p=True) - + (cluster, client) = setup_local_dask_cluster(p2p=True) yield client - - Comms.destroy() - client.close() - cluster.close() + teardown_local_dask_cluster(cluster, client) @pytest.mark.skipif( @@ -72,7 +65,10 @@ def client_connection(): def test_dask_pagerank(client_connection, personalization_perc): gc.collect() + # FIXME: update this to allow dataset to be parameterized and have dataset + # part of test param id (see other tests) input_data_path = r"../datasets/karate.csv" + print(f"dataset={input_data_path}") chunksize = dcg.get_chunksize(input_data_path) ddf = dask_cudf.read_csv( diff --git a/python/cugraph/tests/dask/test_mg_renumber.py b/python/cugraph/tests/dask/test_mg_renumber.py index 8456241ff26..de6d1ea4587 100644 --- a/python/cugraph/tests/dask/test_mg_renumber.py +++ b/python/cugraph/tests/dask/test_mg_renumber.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -20,36 +20,31 @@ import numpy as np import cugraph.dask as dcg -import cugraph.comms as Comms -from dask.distributed import Client import cugraph import dask_cudf import dask import cudf -from dask_cuda import LocalCUDACluster from cugraph.tests import utils from cugraph.structure.number_map import NumberMap -from cugraph.dask.common.mg_utils import is_single_gpu +from cugraph.dask.common.mg_utils import (is_single_gpu, + setup_local_dask_cluster, + teardown_local_dask_cluster, + get_visible_devices) -@pytest.fixture +@pytest.fixture(scope="module") def client_connection(): - cluster = LocalCUDACluster() - client = Client(cluster) - Comms.initialize(p2p=True) - + (cluster, client) = setup_local_dask_cluster(p2p=True) yield client - - Comms.destroy() - client.close() - cluster.close() + teardown_local_dask_cluster(cluster, client) -# Test all combinations of default/managed and pooled/non-pooled allocation @pytest.mark.skipif( is_single_gpu(), reason="skipping MG testing on Single GPU system" ) -@pytest.mark.parametrize("graph_file", utils.DATASETS_UNRENUMBERED) +@pytest.mark.parametrize("graph_file", utils.DATASETS_UNRENUMBERED, + ids=[f"dataset={d.as_posix()}" + for d in utils.DATASETS_UNRENUMBERED]) def test_mg_renumber(graph_file, client_connection): gc.collect() @@ -65,35 +60,40 @@ def test_mg_renumber(graph_file, client_connection): gdf["src"] = sources + translate gdf["dst"] = destinations + translate - ddf = dask.dataframe.from_pandas(gdf, npartitions=2) - - numbering = NumberMap() - numbering.from_dataframe(ddf, ["src", "src_old"], ["dst", "dst_old"]) - renumbered_df = numbering.add_internal_vertex_id( - numbering.add_internal_vertex_id(ddf, "src_id", ["src", "src_old"]), - "dst_id", - ["dst", "dst_old"], - ) - - check_src = numbering.from_internal_vertex_id( - renumbered_df, "src_id" - ).compute() - check_dst = numbering.from_internal_vertex_id( - renumbered_df, "dst_id" - ).compute() + ddf = dask.dataframe.from_pandas( + gdf, npartitions=len(get_visible_devices())) + + # preserve_order is not supported for MG + renumbered_df, renumber_map = NumberMap.renumber(ddf, + ["src", "src_old"], + ["dst", "dst_old"], + preserve_order=False) + unrenumbered_df = renumber_map.unrenumber(renumbered_df, "src", + preserve_order=False) + unrenumbered_df = renumber_map.unrenumber(unrenumbered_df, "dst", + preserve_order=False) + + # sort needed only for comparisons, since preserve_order is False + gdf = gdf.sort_values(by=["src", "src_old", "dst", "dst_old"]) + gdf = gdf.reset_index() + unrenumbered_df = unrenumbered_df.compute() + unrenumbered_df = unrenumbered_df.sort_values(by=["0_src", "1_src", + "0_dst", "1_dst"]) + unrenumbered_df = unrenumbered_df.reset_index() + + assert gdf["src"].equals(unrenumbered_df["0_src"]) + assert gdf["src_old"].equals(unrenumbered_df["1_src"]) + assert gdf["dst"].equals(unrenumbered_df["0_dst"]) + assert gdf["dst_old"].equals(unrenumbered_df["1_dst"]) - assert check_src["0"].to_pandas().equals(check_src["src"].to_pandas()) - assert check_src["1"].to_pandas().equals(check_src["src_old"].to_pandas()) - assert check_dst["0"].to_pandas().equals(check_dst["dst"].to_pandas()) - assert check_dst["1"].to_pandas().equals(check_dst["dst_old"].to_pandas()) - -# Test all combinations of default/managed and pooled/non-pooled allocation @pytest.mark.skipif( is_single_gpu(), reason="skipping MG testing on Single GPU system" ) -@pytest.mark.parametrize("graph_file", utils.DATASETS_UNRENUMBERED) -def test_mg_renumber2(graph_file, client_connection): +@pytest.mark.parametrize("graph_file", utils.DATASETS_UNRENUMBERED, + ids=[f"dataset={d.as_posix()}" + for d in utils.DATASETS_UNRENUMBERED]) +def test_mg_renumber_add_internal_vertex_id(graph_file, client_connection): gc.collect() M = utils.read_csv_for_nx(graph_file) @@ -109,45 +109,8 @@ def test_mg_renumber2(graph_file, client_connection): gdf["dst"] = destinations + translate gdf["weight"] = gdf.index.astype(np.float) - ddf = dask.dataframe.from_pandas(gdf, npartitions=2) - - ren2, num2 = NumberMap.renumber( - ddf, ["src", "src_old"], ["dst", "dst_old"] - ) - - check_src = num2.from_internal_vertex_id(ren2, "src").compute() - check_src = check_src.sort_values("weight").reset_index(drop=True) - check_dst = num2.from_internal_vertex_id(ren2, "dst").compute() - check_dst = check_dst.sort_values("weight").reset_index(drop=True) - - assert check_src["0"].to_pandas().equals(gdf["src"].to_pandas()) - assert check_src["1"].to_pandas().equals(gdf["src_old"].to_pandas()) - assert check_dst["0"].to_pandas().equals(gdf["dst"].to_pandas()) - assert check_dst["1"].to_pandas().equals(gdf["dst_old"].to_pandas()) - - -# Test all combinations of default/managed and pooled/non-pooled allocation -@pytest.mark.skipif( - is_single_gpu(), reason="skipping MG testing on Single GPU system" -) -@pytest.mark.parametrize("graph_file", utils.DATASETS_UNRENUMBERED) -def test_mg_renumber3(graph_file, client_connection): - gc.collect() - - M = utils.read_csv_for_nx(graph_file) - sources = cudf.Series(M["0"]) - destinations = cudf.Series(M["1"]) - - translate = 1000 - - gdf = cudf.DataFrame() - gdf["src_old"] = sources - gdf["dst_old"] = destinations - gdf["src"] = sources + translate - gdf["dst"] = destinations + translate - gdf["weight"] = gdf.index.astype(np.float) - - ddf = dask.dataframe.from_pandas(gdf, npartitions=2) + ddf = dask.dataframe.from_pandas( + gdf, npartitions=len(get_visible_devices())) ren2, num2 = NumberMap.renumber( ddf, ["src", "src_old"], ["dst", "dst_old"] @@ -195,9 +158,6 @@ def test_dask_pagerank(client_connection): dg = cugraph.DiGraph() dg.from_dask_cudf_edgelist(ddf, "src", "dst") - # Pre compute local data - # dg.compute_local_data(by='dst') - expected_pr = cugraph.pagerank(g) result_pr = dcg.pagerank(dg).compute() diff --git a/python/cugraph/tests/dask/test_mg_replication.py b/python/cugraph/tests/dask/test_mg_replication.py index 2b8510cd9ff..3974cf9ed82 100644 --- a/python/cugraph/tests/dask/test_mg_replication.py +++ b/python/cugraph/tests/dask/test_mg_replication.py @@ -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. # You may obtain a copy of the License at @@ -24,14 +24,19 @@ DATASETS_OPTIONS = utils.DATASETS_SMALL DIRECTED_GRAPH_OPTIONS = [False, True] -# MG_DEVICE_COUNT_OPTIONS = [1, 2, 3, 4] -MG_DEVICE_COUNT_OPTIONS = [1] +MG_DEVICE_COUNT_OPTIONS = [pytest.param(1, marks=pytest.mark.preset_gpu_count), + pytest.param(2, marks=pytest.mark.preset_gpu_count), + pytest.param(3, marks=pytest.mark.preset_gpu_count), + pytest.param(4, marks=pytest.mark.preset_gpu_count), + None] @pytest.mark.skipif( is_single_gpu(), reason="skipping MG testing on Single GPU system" ) -@pytest.mark.parametrize("input_data_path", DATASETS_OPTIONS) +@pytest.mark.parametrize("input_data_path", DATASETS_OPTIONS, + ids=[f"dataset={d.as_posix()}" + for d in DATASETS_OPTIONS]) @pytest.mark.parametrize("mg_device_count", MG_DEVICE_COUNT_OPTIONS) def test_replicate_cudf_dataframe_with_weights( input_data_path, mg_device_count @@ -57,7 +62,9 @@ def test_replicate_cudf_dataframe_with_weights( @pytest.mark.skipif( is_single_gpu(), reason="skipping MG testing on Single GPU system" ) -@pytest.mark.parametrize("input_data_path", DATASETS_OPTIONS) +@pytest.mark.parametrize("input_data_path", DATASETS_OPTIONS, + ids=[f"dataset={d.as_posix()}" + for d in DATASETS_OPTIONS]) @pytest.mark.parametrize("mg_device_count", MG_DEVICE_COUNT_OPTIONS) def test_replicate_cudf_dataframe_no_weights(input_data_path, mg_device_count): gc.collect() @@ -81,7 +88,9 @@ def test_replicate_cudf_dataframe_no_weights(input_data_path, mg_device_count): @pytest.mark.skipif( is_single_gpu(), reason="skipping MG testing on Single GPU system" ) -@pytest.mark.parametrize("input_data_path", DATASETS_OPTIONS) +@pytest.mark.parametrize("input_data_path", DATASETS_OPTIONS, + ids=[f"dataset={d.as_posix()}" + for d in DATASETS_OPTIONS]) @pytest.mark.parametrize("mg_device_count", MG_DEVICE_COUNT_OPTIONS) def test_replicate_cudf_series(input_data_path, mg_device_count): gc.collect() @@ -111,7 +120,9 @@ def test_replicate_cudf_series(input_data_path, mg_device_count): @pytest.mark.skipif( is_single_gpu(), reason="skipping MG testing on Single GPU system" ) -@pytest.mark.parametrize("graph_file", DATASETS_OPTIONS) +@pytest.mark.parametrize("graph_file", DATASETS_OPTIONS, + ids=[f"dataset={d.as_posix()}" + for d in DATASETS_OPTIONS]) @pytest.mark.parametrize("directed", DIRECTED_GRAPH_OPTIONS) @pytest.mark.parametrize("mg_device_count", MG_DEVICE_COUNT_OPTIONS) def test_enable_batch_no_context(graph_file, directed, mg_device_count): @@ -126,7 +137,9 @@ def test_enable_batch_no_context(graph_file, directed, mg_device_count): @pytest.mark.skipif( is_single_gpu(), reason="skipping MG testing on Single GPU system" ) -@pytest.mark.parametrize("graph_file", DATASETS_OPTIONS) +@pytest.mark.parametrize("graph_file", DATASETS_OPTIONS, + ids=[f"dataset={d.as_posix()}" + for d in DATASETS_OPTIONS]) @pytest.mark.parametrize("directed", DIRECTED_GRAPH_OPTIONS) @pytest.mark.parametrize("mg_device_count", MG_DEVICE_COUNT_OPTIONS) def test_enable_batch_no_context_view_adj( @@ -142,7 +155,9 @@ def test_enable_batch_no_context_view_adj( @pytest.mark.skipif( is_single_gpu(), reason="skipping MG testing on Single GPU system" ) -@pytest.mark.parametrize("graph_file", DATASETS_OPTIONS) +@pytest.mark.parametrize("graph_file", DATASETS_OPTIONS, + ids=[f"dataset={d.as_posix()}" + for d in DATASETS_OPTIONS]) @pytest.mark.parametrize("directed", DIRECTED_GRAPH_OPTIONS) @pytest.mark.parametrize("mg_device_count", MG_DEVICE_COUNT_OPTIONS) def test_enable_batch_context_then_views( @@ -171,7 +186,9 @@ def test_enable_batch_context_then_views( @pytest.mark.skipif( is_single_gpu(), reason="skipping MG testing on Single GPU system" ) -@pytest.mark.parametrize("graph_file", DATASETS_OPTIONS) +@pytest.mark.parametrize("graph_file", DATASETS_OPTIONS, + ids=[f"dataset={d.as_posix()}" + for d in DATASETS_OPTIONS]) @pytest.mark.parametrize("directed", DIRECTED_GRAPH_OPTIONS) @pytest.mark.parametrize("mg_device_count", MG_DEVICE_COUNT_OPTIONS) def test_enable_batch_view_then_context(graph_file, directed, mg_device_count): @@ -202,7 +219,9 @@ def test_enable_batch_view_then_context(graph_file, directed, mg_device_count): @pytest.mark.skipif( is_single_gpu(), reason="skipping MG testing on Single GPU system" ) -@pytest.mark.parametrize("graph_file", DATASETS_OPTIONS) +@pytest.mark.parametrize("graph_file", DATASETS_OPTIONS, + ids=[f"dataset={d.as_posix()}" + for d in DATASETS_OPTIONS]) @pytest.mark.parametrize("directed", DIRECTED_GRAPH_OPTIONS) @pytest.mark.parametrize("mg_device_count", MG_DEVICE_COUNT_OPTIONS) def test_enable_batch_context_no_context_views( @@ -227,7 +246,9 @@ def test_enable_batch_context_no_context_views( @pytest.mark.skipif( is_single_gpu(), reason="skipping MG testing on Single GPU system" ) -@pytest.mark.parametrize("graph_file", DATASETS_OPTIONS) +@pytest.mark.parametrize("graph_file", DATASETS_OPTIONS, + ids=[f"dataset={d.as_posix()}" + for d in DATASETS_OPTIONS]) @pytest.mark.parametrize("directed", DIRECTED_GRAPH_OPTIONS) @pytest.mark.parametrize("mg_device_count", MG_DEVICE_COUNT_OPTIONS) def test_enable_batch_edgelist_replication( @@ -248,7 +269,9 @@ def test_enable_batch_edgelist_replication( @pytest.mark.skipif( is_single_gpu(), reason="skipping MG testing on Single GPU system" ) -@pytest.mark.parametrize("graph_file", DATASETS_OPTIONS) +@pytest.mark.parametrize("graph_file", DATASETS_OPTIONS, + ids=[f"dataset={d.as_posix()}" + for d in DATASETS_OPTIONS]) @pytest.mark.parametrize("directed", DIRECTED_GRAPH_OPTIONS) @pytest.mark.parametrize("mg_device_count", MG_DEVICE_COUNT_OPTIONS) def test_enable_batch_adjlist_replication_weights( @@ -290,7 +313,9 @@ def test_enable_batch_adjlist_replication_weights( @pytest.mark.skipif( is_single_gpu(), reason="skipping MG testing on Single GPU system" ) -@pytest.mark.parametrize("graph_file", DATASETS_OPTIONS) +@pytest.mark.parametrize("graph_file", DATASETS_OPTIONS, + ids=[f"dataset={d.as_posix()}" + for d in DATASETS_OPTIONS]) @pytest.mark.parametrize("directed", DIRECTED_GRAPH_OPTIONS) @pytest.mark.parametrize("mg_device_count", MG_DEVICE_COUNT_OPTIONS) def test_enable_batch_adjlist_replication_no_weights( diff --git a/python/cugraph/tests/dask/test_mg_sssp.py b/python/cugraph/tests/dask/test_mg_sssp.py index ac4a60f1bdc..9e1fd1ec82f 100644 --- a/python/cugraph/tests/dask/test_mg_sssp.py +++ b/python/cugraph/tests/dask/test_mg_sssp.py @@ -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. # You may obtain a copy of the License at @@ -12,28 +12,21 @@ # limitations under the License. import cugraph.dask as dcg -import cugraph.comms as Comms -from dask.distributed import Client import gc import pytest import cugraph import dask_cudf import cudf -from dask_cuda import LocalCUDACluster -from cugraph.dask.common.mg_utils import is_single_gpu +from cugraph.dask.common.mg_utils import (is_single_gpu, + setup_local_dask_cluster, + teardown_local_dask_cluster) -@pytest.fixture +@pytest.fixture(scope="module") def client_connection(): - cluster = LocalCUDACluster() - client = Client(cluster) - Comms.initialize(p2p=True) - + (cluster, client) = setup_local_dask_cluster(p2p=True) yield client - - Comms.destroy() - client.close() - cluster.close() + teardown_local_dask_cluster(cluster, client) @pytest.mark.skipif( @@ -42,7 +35,10 @@ def client_connection(): def test_dask_sssp(client_connection): gc.collect() + # FIXME: update this to allow dataset to be parameterized and have dataset + # part of test param id (see other tests) input_data_path = r"../datasets/netscience.csv" + print(f"dataset={input_data_path}") chunksize = dcg.get_chunksize(input_data_path) ddf = dask_cudf.read_csv( diff --git a/python/cugraph/tests/dask/test_mg_utility.py b/python/cugraph/tests/dask/test_mg_utility.py index 808f1bcfa70..150fa0137f5 100644 --- a/python/cugraph/tests/dask/test_mg_utility.py +++ b/python/cugraph/tests/dask/test_mg_utility.py @@ -1,4 +1,4 @@ -# Copyright (c) 2018-2020, NVIDIA CORPORATION. +# Copyright (c) 2018-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. # You may obtain a copy of the License at @@ -12,16 +12,16 @@ # limitations under the License. import cugraph.dask as dcg -from dask.distributed import Client, default_client, futures_of, wait +from dask.distributed import default_client, futures_of, wait import gc import cugraph import dask_cudf -import cugraph.comms as Comms -from dask_cuda import LocalCUDACluster import pytest from cugraph.dask.common.part_utils import concat_within_workers from cugraph.dask.common.read_utils import get_n_workers -from cugraph.dask.common.mg_utils import is_single_gpu +from cugraph.dask.common.mg_utils import (is_single_gpu, + setup_local_dask_cluster, + teardown_local_dask_cluster) import os import time import numpy as np @@ -35,24 +35,21 @@ def setup_function(): gc.collect() -@pytest.fixture +@pytest.fixture(scope="module") def client_connection(): - cluster = LocalCUDACluster() - client = Client(cluster) - Comms.initialize(p2p=True) - + (cluster, client) = setup_local_dask_cluster(p2p=True) yield client - - Comms.destroy() - client.close() - cluster.close() + teardown_local_dask_cluster(cluster, client) @pytest.mark.skipif( is_single_gpu(), reason="skipping MG testing on Single GPU system" ) def test_from_edgelist(client_connection): + # FIXME: update this to allow dataset to be parameterized and have dataset + # part of test param id (see other tests) input_data_path = r"../datasets/karate.csv" + print(f"dataset={input_data_path}") chunksize = dcg.get_chunksize(input_data_path) ddf = dask_cudf.read_csv( input_data_path, @@ -74,40 +71,6 @@ def test_from_edgelist(client_connection): assert dg1.EdgeList == dg2.EdgeList -@pytest.mark.skipif( - is_single_gpu(), reason="skipping MG testing on Single GPU system" -) -def test_compute_local_data(client_connection): - - input_data_path = r"../datasets/karate.csv" - chunksize = dcg.get_chunksize(input_data_path) - ddf = dask_cudf.read_csv( - input_data_path, - chunksize=chunksize, - delimiter=" ", - names=["src", "dst", "value"], - dtype=["int32", "int32", "float32"], - ) - - dg = cugraph.DiGraph() - dg.from_dask_cudf_edgelist( - ddf, source="src", destination="dst", edge_attr="value" - ) - - # Compute_local_data - dg.compute_local_data(by="dst") - data = dg.local_data["data"] - by = dg.local_data["by"] - - assert by == "dst" - assert Comms.is_initialized() - - global_num_edges = data.local_data["edges"].sum() - assert global_num_edges == dg.number_of_edges() - global_num_verts = data.local_data["verts"].sum() - assert global_num_verts == dg.number_of_nodes() - - @pytest.mark.skipif( is_single_gpu(), reason="skipping MG testing on Single GPU system" ) diff --git a/python/cugraph/tests/generators/test_rmat.py b/python/cugraph/tests/generators/test_rmat.py new file mode 100644 index 00000000000..a7c8701095e --- /dev/null +++ b/python/cugraph/tests/generators/test_rmat.py @@ -0,0 +1,122 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import pytest + +import cudf +import dask_cudf + +from cugraph.dask.common.mg_utils import (is_single_gpu, + get_visible_devices, + setup_local_dask_cluster, + teardown_local_dask_cluster) +from cugraph.generators import rmat +import cugraph + + +############################################################################## +_cluster = None +_client = None +_is_single_gpu = is_single_gpu() +_visible_devices = get_visible_devices() +_scale_values = [2, 4, 16] +_scale_test_ids = [f"scale={x}" for x in _scale_values] +_mg_values = [False, True] +_mg_test_ids = [f"mg={x}" for x in _mg_values] +_graph_types = [cugraph.Graph, cugraph.DiGraph, None, int] +_graph_test_ids = [f"create_using={getattr(x,'__name__',str(x))}" + for x in _graph_types] + + +def _call_rmat(scale, num_edges, create_using, mg): + """ + Simplifies calling RMAT by requiring only specific args that are varied by + these tests and hard-coding all others. + """ + return rmat(scale=scale, + num_edges=num_edges, + a=0.1, + b=0.2, + c=0.3, + seed=24, + clip_and_flip=False, + scramble_vertex_ids=True, + create_using=create_using, + mg=mg) + + +############################################################################### +def setup_module(): + global _cluster + global _client + if not _is_single_gpu: + (_cluster, _client) = setup_local_dask_cluster(p2p=True) + + +def teardown_module(): + if not _is_single_gpu: + teardown_local_dask_cluster(_cluster, _client) + + +############################################################################### +@pytest.mark.parametrize("scale", _scale_values, ids=_scale_test_ids) +@pytest.mark.parametrize("mg", _mg_values, ids=_mg_test_ids) +def test_rmat_edgelist(scale, mg): + """ + Verifies that the edgelist returned by rmat() is valid based on inputs. + """ + if mg and _is_single_gpu: + pytest.skip("skipping MG testing on Single GPU system") + + num_edges = (2**scale)*4 + create_using = None # Returns the edgelist from RMAT + + df = _call_rmat(scale, num_edges, create_using, mg) + + if mg: + assert df.npartitions == len(_visible_devices) + df_to_check = df.compute() + else: + df_to_check = df + + assert len(df_to_check) == num_edges + + +@pytest.mark.parametrize("graph_type", _graph_types, ids=_graph_test_ids) +@pytest.mark.parametrize("mg", _mg_values, ids=_mg_test_ids) +def test_rmat_return_type(graph_type, mg): + """ + Verifies that the return type returned by rmat() is valid (or the proper + exception is raised) based on inputs. + """ + if mg and _is_single_gpu: + pytest.skip("skipping MG testing on Single GPU system") + + scale = 2 + num_edges = (2**scale)*4 + + if (mg and (graph_type not in [cugraph.DiGraph, None])) or \ + (graph_type not in [cugraph.Graph, cugraph.DiGraph, None]): + + with pytest.raises(TypeError): + _call_rmat(scale, num_edges, graph_type, mg) + + else: + G_or_df = _call_rmat(scale, num_edges, graph_type, mg) + + if graph_type is None: + assert type(G_or_df) is dask_cudf.DataFrame if mg \ + else cudf.DataFrame + else: + assert type(G_or_df) is graph_type diff --git a/python/cugraph/tests/test_balanced_cut.py b/python/cugraph/tests/test_balanced_cut.py index f0fc7152e56..2492017511a 100644 --- a/python/cugraph/tests/test_balanced_cut.py +++ b/python/cugraph/tests/test_balanced_cut.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at diff --git a/python/cugraph/tests/test_betweenness_centrality.py b/python/cugraph/tests/test_betweenness_centrality.py old mode 100644 new mode 100755 index 33b2842645d..ee1a269e532 --- a/python/cugraph/tests/test_betweenness_centrality.py +++ b/python/cugraph/tests/test_betweenness_centrality.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -68,49 +68,36 @@ def calc_betweenness_centrality( edgevals=False, ): """ Generate both cugraph and networkx betweenness centrality - Parameters ---------- graph_file : string Path to COO Graph representation in .csv format - directed : bool, optional, default=True - k : int or None, optional, default=None int: Number of sources to sample from None: All sources are used to compute - normalized : bool True: Normalize Betweenness Centrality scores False: Scores are left unnormalized - weight : cudf.DataFrame: Not supported as of 06/2020 - endpoints : bool True: Endpoints are included when computing scores False: Endpoints are not considered - seed : int or None, optional, default=None Seed for random sampling of the starting point - result_dtype : numpy.dtype Expected type of the result, either np.float32 or np.float64 - use_k_full : bool When True, if k is None replaces k by the number of sources of the Graph - multi_gpu_batch : bool When True, enable mg batch after constructing the graph - edgevals: bool When True, enable tests with weighted graph, should be ignored during computation. - Returns ------- - sorted_df : cudf.DataFrame Contains 'vertex' and 'cu_bc' 'ref_bc' columns, where 'cu_bc' and 'ref_bc' are the two betweenness centrality scores to compare. @@ -347,6 +334,7 @@ def test_betweenness_centrality( @pytest.mark.parametrize("result_dtype", RESULT_DTYPE_OPTIONS) @pytest.mark.parametrize("use_k_full", [True]) @pytest.mark.parametrize("edgevals", WEIGHTED_GRAPH_OPTIONS) +@pytest.mark.skip(reason="Skipping large tests") def test_betweenness_centrality_k_full( graph_file, directed, @@ -390,6 +378,7 @@ def test_betweenness_centrality_k_full( @pytest.mark.parametrize("subset_seed", [None]) @pytest.mark.parametrize("result_dtype", RESULT_DTYPE_OPTIONS) @pytest.mark.parametrize("edgevals", WEIGHTED_GRAPH_OPTIONS) +@pytest.mark.skip(reason="Skipping large tests") def test_betweenness_centrality_fixed_sample( graph_file, directed, @@ -402,7 +391,6 @@ def test_betweenness_centrality_fixed_sample( edgevals ): """Test Betweenness Centrality using a subset - Only k sources are considered for an approximate Betweenness Centrality """ prepare_test() @@ -429,6 +417,7 @@ def test_betweenness_centrality_fixed_sample( @pytest.mark.parametrize("subset_seed", SUBSET_SEED_OPTIONS) @pytest.mark.parametrize("result_dtype", RESULT_DTYPE_OPTIONS) @pytest.mark.parametrize("edgevals", WEIGHTED_GRAPH_OPTIONS) +@pytest.mark.skip(reason="Skipping large tests") def test_betweenness_centrality_weight_except( graph_file, directed, @@ -441,7 +430,6 @@ def test_betweenness_centrality_weight_except( edgevals ): """Calls betwenness_centrality with weight - As of 05/28/2020, weight is not supported and should raise a NotImplementedError """ diff --git a/python/cugraph/tests/test_bfs.py b/python/cugraph/tests/test_bfs.py index 0070a34248c..a8547d692c2 100644 --- a/python/cugraph/tests/test_bfs.py +++ b/python/cugraph/tests/test_bfs.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -51,6 +51,7 @@ DEFAULT_EPSILON = 1e-6 +DEPTH_LIMITS = [None, 1, 5, 18] # Map of cuGraph input types to the expected output type for cuGraph # connected_components calls. @@ -148,28 +149,14 @@ def compare_single_sp_counter(result, expected, epsilon=DEFAULT_EPSILON): return np.isclose(result, expected, rtol=epsilon) -def compare_bfs(benchmark_callable, G, nx_values, start_vertex, - return_sp_counter=False): +def compare_bfs(benchmark_callable, G, nx_values, start_vertex, depth_limit): """ Genereate both cugraph and reference bfs traversal. """ if isinstance(start_vertex, int): - result = benchmark_callable(cugraph.bfs_edges, G, start_vertex, - return_sp_counter=return_sp_counter) + result = benchmark_callable(cugraph.bfs_edges, G, start_vertex) cugraph_df = convert_output_to_cudf(G, result) - - if return_sp_counter: - # This call should only contain 3 columns: - # 'vertex', 'distance', 'predecessor', 'sp_counter' - assert len(cugraph_df.columns) == 4, ( - "The result of the BFS has an invalid " "number of columns" - ) - - if return_sp_counter: - compare_func = _compare_bfs_spc - - else: - compare_func = _compare_bfs + compare_func = _compare_bfs # NOTE: We need to take 2 different path for verification as the nx # functions used as reference return dictionaries that might @@ -185,18 +172,15 @@ def compare_bfs(benchmark_callable, G, nx_values, start_vertex, def func_to_benchmark(): for sv in start_vertex: cugraph_df = cugraph.bfs_edges( - G, sv, return_sp_counter=return_sp_counter) + G, sv, depth_limit=depth_limit) all_cugraph_distances.append(cugraph_df) benchmark_callable(func_to_benchmark) - compare_func = _compare_bfs_spc if return_sp_counter else _compare_bfs + compare_func = _compare_bfs for (i, sv) in enumerate(start_vertex): cugraph_df = convert_output_to_cudf(G, all_cugraph_distances[i]) - if return_sp_counter: - assert len(cugraph_df.columns) == 4, ( - "The result of the BFS has an invalid " "number of columns" - ) + compare_func(cugraph_df, all_nx_values[i], sv) else: # Unknown type given to seed @@ -272,55 +256,6 @@ def _compare_bfs(cugraph_df, nx_distances, source): assert invalid_predecessor_error == 0, "There are invalid predecessors" -def _compare_bfs_spc(cugraph_df, nx_sp_counter, unused): - """ - Compare BFS with shortest path counters. - """ - sorted_nx = [nx_sp_counter[key] for key in sorted(nx_sp_counter.keys())] - # We are not checking for distances / predecessors here as we assume - # that these have been checked in the _compare_bfs tests - # We focus solely on shortest path counting - - # cugraph return a dataframe that should contain exactly one time each - # vertex - # We could us isin to filter only vertices that are common to both - # But it would slow down the comparison, and in this specific case - # nxacb._single_source_shortest_path_basic is a dictionary containing all - # the vertices. - # There is no guarantee when we get `df` that the vertices are sorted - # thus we enforce the order so that we can leverage faster comparison after - sorted_df = cugraph_df.sort_values("vertex").rename( - columns={"sp_counter": "cu_spc"}, copy=False - ) - - # This allows to detect vertices identifier that could have been - # wrongly present multiple times - cu_vertices = set(sorted_df['vertex'].values_host) - nx_vertices = nx_sp_counter.keys() - assert len(cu_vertices.intersection(nx_vertices)) == len( - nx_vertices - ), "There are missing vertices" - - # We add the nx shortest path counter in the cudf.DataFrame, both the - # the DataFrame and `sorted_nx` are sorted base on vertices identifiers - sorted_df["nx_spc"] = sorted_nx - - # We could use numpy.isclose or cupy.isclose, we can then get the entries - # in the cudf.DataFrame where there are is a mismatch. - # numpy / cupy allclose would get only a boolean and we might want the - # extra information about the discrepancies - shortest_path_counter_errors = sorted_df[ - ~cupy.isclose( - sorted_df["cu_spc"], sorted_df["nx_spc"], rtol=DEFAULT_EPSILON - ) - ] - if len(shortest_path_counter_errors) > 0: - print(shortest_path_counter_errors) - assert len(shortest_path_counter_errors) == 0, ( - "Shortest path counters " "are too different" - ) - - def get_nx_graph_and_params(dataset, directed): """ Helper for fixtures returning a Nx graph obj and params. @@ -329,21 +264,17 @@ def get_nx_graph_and_params(dataset, directed): utils.generate_nx_graph_from_file(dataset, directed)) -def get_nx_results_and_params(seed, use_spc, dataset, directed, Gnx): +def get_nx_results_and_params(seed, depth_limit, dataset, directed, Gnx): """ Helper for fixtures returning Nx results and params. """ random.seed(seed) start_vertex = random.sample(Gnx.nodes(), 1)[0] - if use_spc: - _, _, nx_sp_counter = \ - nxacb._single_source_shortest_path_basic(Gnx, start_vertex) - nx_values = nx_sp_counter - else: - nx_values = nx.single_source_shortest_path_length(Gnx, start_vertex) + nx_values = nx.single_source_shortest_path_length(Gnx, start_vertex, + cutoff=depth_limit) - return (dataset, directed, nx_values, start_vertex, use_spc) + return (dataset, directed, nx_values, start_vertex, depth_limit) # ============================================================================= @@ -353,7 +284,7 @@ def get_nx_results_and_params(seed, use_spc, dataset, directed, Gnx): DIRECTED = [pytest.param(d) for d in DIRECTED_GRAPH_OPTIONS] DATASETS = [pytest.param(d) for d in utils.DATASETS] DATASETS_SMALL = [pytest.param(d) for d in utils.DATASETS_SMALL] -USE_SHORTEST_PATH_COUNTER = [pytest.param(False), pytest.param(True)] +DEPTH_LIMIT = [pytest.param(d) for d in DEPTH_LIMITS] # Call genFixtureParamsProduct() to caluculate the cartesian product of # multiple lists of params. This is required since parameterized fixtures do @@ -362,7 +293,7 @@ def get_nx_results_and_params(seed, use_spc, dataset, directed, Gnx): # full test name. algo_test_fixture_params = utils.genFixtureParamsProduct( (SEEDS, "seed"), - (USE_SHORTEST_PATH_COUNTER, "spc")) + (DEPTH_LIMIT, "depth_limit")) graph_fixture_params = utils.genFixtureParamsProduct( (DATASETS, "ds"), @@ -377,7 +308,7 @@ def get_nx_results_and_params(seed, use_spc, dataset, directed, Gnx): # was covered elsewhere). single_algo_test_fixture_params = utils.genFixtureParamsProduct( ([SEEDS[0]], "seed"), - ([USE_SHORTEST_PATH_COUNTER[0]], "spc")) + ([DEPTH_LIMIT[0]], "depth_limit")) single_small_graph_fixture_params = utils.genFixtureParamsProduct( ([DATASETS_SMALL[0]], "ds"), @@ -446,7 +377,7 @@ def test_bfs(gpubenchmark, dataset_nxresults_startvertex_spc, """ Test BFS traversal on random source with distance and predecessors """ - (dataset, directed, nx_values, start_vertex, use_spc) = \ + (dataset, directed, nx_values, start_vertex, depth_limit) = \ dataset_nxresults_startvertex_spc # special case: ensure cugraph and Nx Graph types are DiGraphs if @@ -463,8 +394,7 @@ def test_bfs(gpubenchmark, dataset_nxresults_startvertex_spc, compare_bfs( gpubenchmark, - G_or_matrix, nx_values, start_vertex, return_sp_counter=use_spc - ) + G_or_matrix, nx_values, start_vertex, depth_limit) @pytest.mark.parametrize("cugraph_input_type", @@ -477,36 +407,6 @@ def test_bfs_nonnative_inputs(gpubenchmark, cugraph_input_type) -@pytest.mark.parametrize("cugraph_input_type", utils.CUGRAPH_INPUT_TYPES) -def test_bfs_spc_full(gpubenchmark, dataset_nxresults_allstartvertices_spc, - cugraph_input_type): - """ - Test BFS traversal on every vertex with shortest path counting - """ - (dataset, directed, all_nx_values, start_vertices, use_spc) = \ - dataset_nxresults_allstartvertices_spc - - # use_spc is currently always True - - # special case: ensure cugraph and Nx Graph types are DiGraphs if - # "directed" is set, since the graph type parameterization is currently - # independent of the directed parameter. Unfortunately this does not - # change the "id" in the pytest output. - if directed: - if cugraph_input_type is cugraph.Graph: - cugraph_input_type = cugraph.DiGraph - elif cugraph_input_type is nx.Graph: - cugraph_input_type = nx.DiGraph - - G_or_matrix = utils.create_obj_from_csv(dataset, cugraph_input_type) - - compare_bfs( - gpubenchmark, - G_or_matrix, all_nx_values, start_vertex=start_vertices, - return_sp_counter=use_spc - ) - - def test_scipy_api_compat(): graph_file = utils.DATASETS[0] @@ -522,7 +422,7 @@ def test_scipy_api_compat(): # Ensure cugraph-compatible options work as expected cugraph.bfs(input_cugraph_graph, i_start=0) - cugraph.bfs(input_cugraph_graph, i_start=0, return_sp_counter=True) + cugraph.bfs(input_cugraph_graph, i_start=0) # cannot have start and i_start with pytest.raises(TypeError): cugraph.bfs(input_cugraph_graph, start=0, i_start=0) @@ -531,7 +431,6 @@ def test_scipy_api_compat(): cugraph.bfs(input_coo_matrix, i_start=0) cugraph.bfs(input_coo_matrix, i_start=0, directed=True) cugraph.bfs(input_coo_matrix, i_start=0, directed=False) - result = cugraph.bfs(input_coo_matrix, i_start=0, - return_sp_counter=True) + result = cugraph.bfs(input_coo_matrix, i_start=0) assert type(result) is tuple - assert len(result) == 3 + assert len(result) == 2 diff --git a/python/cugraph/tests/test_connectivity.py b/python/cugraph/tests/test_connectivity.py index f957c4b417b..194147ab620 100644 --- a/python/cugraph/tests/test_connectivity.py +++ b/python/cugraph/tests/test_connectivity.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at diff --git a/python/cugraph/tests/test_convert_matrix.py b/python/cugraph/tests/test_convert_matrix.py index d418dd7ce2e..1dbf51910ea 100644 --- a/python/cugraph/tests/test_convert_matrix.py +++ b/python/cugraph/tests/test_convert_matrix.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at diff --git a/python/cugraph/tests/test_core_number.py b/python/cugraph/tests/test_core_number.py index edbc7b0597b..9cfc37ba1c5 100644 --- a/python/cugraph/tests/test_core_number.py +++ b/python/cugraph/tests/test_core_number.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at diff --git a/python/cugraph/tests/test_ecg.py b/python/cugraph/tests/test_ecg.py index 4dc01c389cc..e51ef9b7a98 100644 --- a/python/cugraph/tests/test_ecg.py +++ b/python/cugraph/tests/test_ecg.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -14,34 +14,39 @@ import gc import pytest - import networkx as nx import cugraph + from cugraph.tests import utils +from pathlib import PurePath + def cugraph_call(G, min_weight, ensemble_size): df = cugraph.ecg(G, min_weight, ensemble_size) num_parts = df["partition"].max() + 1 - score = cugraph.analyzeClustering_modularity(G, num_parts, df, - 'vertex', 'partition') + score = cugraph.analyzeClustering_modularity( + G, num_parts, df, "vertex", "partition" + ) return score, num_parts def golden_call(graph_file): - if graph_file == "../datasets/dolphins.csv": + if graph_file == PurePath(utils.RAPIDS_DATASET_ROOT_DIR) / "dolphins.csv": return 0.4962422251701355 - if graph_file == "../datasets/karate.csv": + if graph_file == PurePath(utils.RAPIDS_DATASET_ROOT_DIR) / "karate.csv": return 0.38428664207458496 - if graph_file == "../datasets/netscience.csv": + if ( + graph_file + == PurePath(utils.RAPIDS_DATASET_ROOT_DIR) / "netscience.csv" + ): return 0.9279554486274719 DATASETS = [ - "../datasets/karate.csv", - "../datasets/dolphins.csv", - "../datasets/netscience.csv", + PurePath(utils.RAPIDS_DATASET_ROOT_DIR) / f + for f in ["karate.csv", "dolphins.csv", "netscience.csv"] ] MIN_WEIGHTS = [0.05, 0.10, 0.15] @@ -78,9 +83,10 @@ def test_ecg_clustering_nx(graph_file, min_weight, ensemble_size): # Read in the graph and get a NetworkX graph M = utils.read_csv_for_nx(graph_file, read_weights_in_sp=True) G = nx.from_pandas_edgelist( - M, source="0", target="1", edge_attr="weight", - create_using=nx.Graph() + M, source="0", target="1", edge_attr="weight", create_using=nx.Graph() ) # Get the modularity score for partitioning versus random assignment - _ = cugraph.ecg(G, min_weight, ensemble_size, "weight") + df_dict = cugraph.ecg(G, min_weight, ensemble_size, "weight") + + assert isinstance(df_dict, dict) diff --git a/python/cugraph/tests/test_edge_betweenness_centrality.py b/python/cugraph/tests/test_edge_betweenness_centrality.py index 529b0b9de9c..6caad0d9fad 100644 --- a/python/cugraph/tests/test_edge_betweenness_centrality.py +++ b/python/cugraph/tests/test_edge_betweenness_centrality.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION.: +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -341,6 +341,7 @@ def test_edge_betweenness_centrality( @pytest.mark.parametrize("result_dtype", RESULT_DTYPE_OPTIONS) @pytest.mark.parametrize("use_k_full", [True]) @pytest.mark.parametrize("edgevals", WEIGHTED_GRAPH_OPTIONS) +@pytest.mark.skip(reason="Skipping large tests") def test_edge_betweenness_centrality_k_full( graph_file, directed, @@ -381,6 +382,7 @@ def test_edge_betweenness_centrality_k_full( @pytest.mark.parametrize("subset_seed", [None]) @pytest.mark.parametrize("result_dtype", RESULT_DTYPE_OPTIONS) @pytest.mark.parametrize("edgevals", WEIGHTED_GRAPH_OPTIONS) +@pytest.mark.skip(reason="Skipping large tests") def test_edge_betweenness_centrality_fixed_sample( graph_file, directed, @@ -417,6 +419,7 @@ def test_edge_betweenness_centrality_fixed_sample( @pytest.mark.parametrize("subset_seed", SUBSET_SEED_OPTIONS) @pytest.mark.parametrize("result_dtype", RESULT_DTYPE_OPTIONS) @pytest.mark.parametrize("edgevals", WEIGHTED_GRAPH_OPTIONS) +@pytest.mark.skip(reason="Skipping large tests") def test_edge_betweenness_centrality_weight_except( graph_file, directed, diff --git a/python/cugraph/tests/test_egonet.py b/python/cugraph/tests/test_egonet.py new file mode 100644 index 00000000000..fc0ce38eb9c --- /dev/null +++ b/python/cugraph/tests/test_egonet.py @@ -0,0 +1,117 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import gc + +import pytest + +import cudf +import cugraph +from cugraph.tests import utils + +# Temporarily suppress warnings till networkX fixes deprecation warnings +# (Using or importing the ABCs from 'collections' instead of from +# 'collections.abc' is deprecated, and in 3.8 it will stop working) for +# python 3.7. Also, this import networkx needs to be relocated in the +# third-party group once this gets fixed. +import warnings + +with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=DeprecationWarning) + import networkx as nx + +print("Networkx version : {} ".format(nx.__version__)) + +SEEDS = [0, 5, 13] +RADIUS = [1, 2, 3] + + +@pytest.mark.parametrize("graph_file", utils.DATASETS) +@pytest.mark.parametrize("seed", SEEDS) +@pytest.mark.parametrize("radius", RADIUS) +def test_ego_graph_nx(graph_file, seed, radius): + gc.collect() + + # Nx + df = utils.read_csv_for_nx(graph_file, read_weights_in_sp=True) + Gnx = nx.from_pandas_edgelist( + df, create_using=nx.Graph(), source="0", target="1", edge_attr="weight" + ) + ego_nx = nx.ego_graph(Gnx, seed, radius=radius) + + # cugraph + ego_cugraph = cugraph.ego_graph(Gnx, seed, radius=radius) + + assert nx.is_isomorphic(ego_nx, ego_cugraph) + + +@pytest.mark.parametrize("graph_file", utils.DATASETS) +@pytest.mark.parametrize("seeds", [[0, 5, 13]]) +@pytest.mark.parametrize("radius", [1, 2, 3]) +def test_batched_ego_graphs(graph_file, seeds, radius): + gc.collect() + + # Nx + df = utils.read_csv_for_nx(graph_file, read_weights_in_sp=True) + Gnx = nx.from_pandas_edgelist( + df, create_using=nx.Graph(), source="0", target="1", edge_attr="weight" + ) + + # cugraph + df, offsets = cugraph.batched_ego_graphs(Gnx, seeds, radius=radius) + for i in range(len(seeds)): + ego_nx = nx.ego_graph(Gnx, seeds[i], radius=radius) + ego_df = df[offsets[i]:offsets[i + 1]] + ego_cugraph = nx.from_pandas_edgelist( + ego_df, source="src", target="dst", edge_attr="weight" + ) + assert nx.is_isomorphic(ego_nx, ego_cugraph) + + +@pytest.mark.parametrize("graph_file", utils.DATASETS) +@pytest.mark.parametrize("seed", SEEDS) +@pytest.mark.parametrize("radius", RADIUS) +def test_multi_column_ego_graph(graph_file, seed, radius): + gc.collect() + + df = utils.read_csv_file(graph_file, read_weights_in_sp=True) + df.rename(columns={'0': 'src_0', '1': 'dst_0'}, inplace=True) + df['src_1'] = df['src_0'] + 1000 + df['dst_1'] = df['dst_0'] + 1000 + + G1 = cugraph.Graph() + G1.from_cudf_edgelist( + df, source=["src_0", "src_1"], destination=["dst_0", "dst_1"], + edge_attr="2" + ) + + seed_df = cudf.DataFrame() + seed_df['v_0'] = [seed] + seed_df['v_1'] = [seed + 1000] + + ego_cugraph_res = cugraph.ego_graph(G1, seed_df, radius=radius) + + G2 = cugraph.Graph() + G2.from_cudf_edgelist( + df, source="src_0", destination="dst_0", + edge_attr="2" + ) + ego_cugraph_exp = cugraph.ego_graph(G2, seed, radius=radius) + + # FIXME: Replace with multi-column view_edge_list() + edgelist_df = ego_cugraph_res.edgelist.edgelist_df + edgelist_df_res = ego_cugraph_res.unrenumber(edgelist_df, "src") + edgelist_df_res = ego_cugraph_res.unrenumber(edgelist_df_res, "dst") + for i in range(len(edgelist_df_res)): + assert ego_cugraph_exp.has_edge(edgelist_df_res["0_src"].iloc[i], + edgelist_df_res["0_dst"].iloc[i]) diff --git a/python/cugraph/tests/test_filter_unreachable.py b/python/cugraph/tests/test_filter_unreachable.py index 29b862f0285..6c00461d234 100644 --- a/python/cugraph/tests/test_filter_unreachable.py +++ b/python/cugraph/tests/test_filter_unreachable.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at diff --git a/python/cugraph/tests/test_force_atlas2.py b/python/cugraph/tests/test_force_atlas2.py index 4de49cb4088..1128f52904a 100644 --- a/python/cugraph/tests/test_force_atlas2.py +++ b/python/cugraph/tests/test_force_atlas2.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -12,14 +12,15 @@ # limitations under the License. import time - import pytest +import cudf import cugraph from cugraph.internals import GraphBasedDimRedCallback from cugraph.tests import utils from sklearn.manifold import trustworthiness import scipy.io +from pathlib import PurePath # Temporarily suppress warnings till networkX fixes deprecation warnings # (Using or importing the ABCs from 'collections' instead of from @@ -61,11 +62,14 @@ def cugraph_call(cu_M, max_iter, pos_list, outbound_attraction_distribution, DATASETS = [ - ("../datasets/karate.csv", 0.70), - ("../datasets/polbooks.csv", 0.75), - ("../datasets/dolphins.csv", 0.66), - ("../datasets/netscience.csv", 0.66), + (PurePath(utils.RAPIDS_DATASET_ROOT_DIR)/f,)+(d,) for (f, d) in [ + ("karate.csv", 0.70), + ("polbooks.csv", 0.75), + ("dolphins.csv", 0.66), + ("netscience.csv", 0.66)] ] + + MAX_ITERATIONS = [500] BARNES_HUT_OPTIMIZE = [False, True] @@ -120,7 +124,7 @@ def test_force_atlas2(graph_file, score, max_iter, iterations on a given graph. """ - matrix_file = graph_file[:-4] + ".mtx" + matrix_file = graph_file.with_suffix(".mtx") M = scipy.io.mmread(matrix_file) M = M.todense() cu_trust = trustworthiness(M, cu_pos[["x", "y"]].to_pandas()) @@ -132,3 +136,70 @@ def test_force_atlas2(graph_file, score, max_iter, assert test_callback.on_epoch_end_called_count == max_iter # verify `on_train_end` was only called once assert test_callback.on_train_end_called_count == 1 + + +# FIXME: this test occasionally fails - skipping to prevent CI failures but +# need to revisit ASAP +@pytest.mark.skip(reason="non-deterministric - needs fixing!") +@pytest.mark.parametrize('graph_file, score', DATASETS[:-1]) +@pytest.mark.parametrize('max_iter', MAX_ITERATIONS) +@pytest.mark.parametrize('barnes_hut_optimize', BARNES_HUT_OPTIMIZE) +def test_force_atlas2_multi_column_pos_list(graph_file, score, max_iter, + barnes_hut_optimize): + cu_M = utils.read_csv_file(graph_file) + test_callback = TestCallback() + pos = cugraph_call(cu_M, + max_iter=max_iter, + pos_list=None, + outbound_attraction_distribution=True, + lin_log_mode=False, + prevent_overlapping=False, + edge_weight_influence=1.0, + jitter_tolerance=1.0, + barnes_hut_optimize=False, + barnes_hut_theta=0.5, + scaling_ratio=2.0, + strong_gravity_mode=False, + gravity=1.0, + callback=test_callback) + + cu_M.rename(columns={'0': 'src_0', '1': 'dst_0'}, inplace=True) + cu_M['src_1'] = cu_M['src_0'] + 1000 + cu_M['dst_1'] = cu_M['dst_0'] + 1000 + + G = cugraph.Graph() + G.from_cudf_edgelist( + cu_M, source=["src_0", "src_1"], + destination=["dst_0", "dst_1"], + edge_attr="2" + ) + + pos_list = cudf.DataFrame() + pos_list['vertex_0'] = pos['vertex'] + pos_list['vertex_1'] = pos_list['vertex_0'] + 1000 + pos_list['x'] = pos['x'] + pos_list['y'] = pos['y'] + + cu_pos = cugraph.force_atlas2( + G, + max_iter=max_iter, + pos_list=pos_list, + outbound_attraction_distribution=True, + lin_log_mode=False, + prevent_overlapping=False, + edge_weight_influence=1.0, + jitter_tolerance=1.0, + barnes_hut_optimize=False, + barnes_hut_theta=0.5, + scaling_ratio=2.0, + strong_gravity_mode=False, + gravity=1.0, + callback=test_callback) + + cu_pos = cu_pos.sort_values('0_vertex') + matrix_file = graph_file.with_suffix(".mtx") + M = scipy.io.mmread(matrix_file) + M = M.todense() + cu_trust = trustworthiness(M, cu_pos[["x", "y"]].to_pandas()) + print(cu_trust, score) + assert cu_trust > score diff --git a/python/cugraph/tests/test_graph.py b/python/cugraph/tests/test_graph.py index d8d5a504070..fa68a50c952 100644 --- a/python/cugraph/tests/test_graph.py +++ b/python/cugraph/tests/test_graph.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -20,7 +20,7 @@ import scipy import cudf -from cudf.tests.utils import assert_eq +from cudf.testing.testing import assert_frame_equal import cugraph from cugraph.tests import utils @@ -200,6 +200,7 @@ def test_add_adj_list_to_edge_list(graph_file): # cugraph add_adj_list to_edge_list call G = cugraph.DiGraph() G.from_cudf_adjlist(offsets, indices, None) + edgelist = G.view_edge_list() sources_cu = edgelist["src"] destinations_cu = edgelist["dst"] @@ -326,7 +327,7 @@ def test_edges_for_Graph(graph_file): else: edges.append([edge[0], edge[1]]) nx_edge_list = cudf.DataFrame(list(edges), columns=['src', 'dst']) - assert_eq( + assert_frame_equal( nx_edge_list.sort_values(by=['src', 'dst']).reset_index(drop=True), cu_edge_list.sort_values(by=['src', 'dst']).reset_index(drop=True), check_dtype=False @@ -535,6 +536,7 @@ def test_to_directed(graph_file): DiG = G.to_directed() DiGnx = Gnx.to_directed() + assert DiG.is_directed() assert DiG.number_of_nodes() == DiGnx.number_of_nodes() assert DiG.number_of_edges() == DiGnx.number_of_edges() @@ -569,6 +571,7 @@ def test_to_undirected(graph_file): G = DiG.to_undirected() Gnx = DiGnx.to_undirected() + assert not G.is_directed() assert G.number_of_nodes() == Gnx.number_of_nodes() assert G.number_of_edges() == Gnx.number_of_edges() @@ -627,17 +630,13 @@ def test_bipartite_api(graph_file): set2_exp = cudf.Series(set(nodes.values_host) - set(set1_exp.values_host)) - G = cugraph.Graph() - assert not G.is_bipartite() + G = cugraph.BiPartiteGraph() + assert G.is_bipartite() # Add a set of nodes present in one partition G.add_nodes_from(set1_exp, bipartite='set1') G.from_cudf_edgelist(cu_M, source='0', destination='1') - # Check if Graph is bipartite. It should return True since we have - # added the partition in add_nodes_from() - assert G.is_bipartite() - # Call sets() to get the bipartite set of nodes. set1, set2 = G.sets() diff --git a/python/cugraph/tests/test_hits.py b/python/cugraph/tests/test_hits.py index 6b6f54937a6..9229f3734f8 100644 --- a/python/cugraph/tests/test_hits.py +++ b/python/cugraph/tests/test_hits.py @@ -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. # You may obtain a copy of the License at diff --git a/python/cugraph/tests/test_hungarian.py b/python/cugraph/tests/test_hungarian.py index 280903bc303..4183bcc2c89 100644 --- a/python/cugraph/tests/test_hungarian.py +++ b/python/cugraph/tests/test_hungarian.py @@ -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. # You may obtain a copy of the License at @@ -12,7 +12,6 @@ # limitations under the License. import gc -from itertools import product from timeit import default_timer as timer import numpy as np @@ -21,7 +20,6 @@ import cudf import cugraph from scipy.optimize import linear_sum_assignment -import rmm def create_random_bipartite(v1, v2, size, dtype): @@ -54,33 +52,23 @@ def create_random_bipartite(v1, v2, size, dtype): return df1['src'], g, a -SPARSE_SIZES = [[5, 5, 100], [500, 500, 10000], [5000, 5000, 100000]] +SPARSE_SIZES = [[5, 5, 100], [500, 500, 10000]] +DENSE_SIZES = [[5, 100], [500, 10000]] def setup_function(): gc.collect() -# Test all combinations of default/managed and pooled/non-pooled allocation -@pytest.mark.parametrize('managed, pool', - list(product([False, True], [False, True]))) @pytest.mark.parametrize('v1_size, v2_size, weight_limit', SPARSE_SIZES) -def test_hungarian(managed, pool, v1_size, v2_size, weight_limit): - rmm.reinitialize( - managed_memory=managed, - pool_allocator=pool, - initial_pool_size=2 << 27 - ) - - assert(rmm.is_initialized()) - +def test_hungarian(v1_size, v2_size, weight_limit): v1, g, m = create_random_bipartite(v1_size, v2_size, weight_limit, np.float) start = timer() - matching = cugraph.hungarian(g, v1) + cugraph_cost, matching = cugraph.hungarian(g, v1) end = timer() print('cugraph time: ', (end - start)) @@ -93,14 +81,29 @@ def test_hungarian(managed, pool, v1_size, v2_size, weight_limit): scipy_cost = m[np_matching[0], np_matching[1]].sum() - cugraph_df = matching.merge(g.edgelist.edgelist_df, - left_on=['vertex', 'assignment'], - right_on=['src', 'dst'], - how='left') + assert(scipy_cost == cugraph_cost) + - cugraph_cost = cugraph_df['weights'].sum() +@pytest.mark.parametrize('n, weight_limit', DENSE_SIZES) +def test_dense_hungarian(n, weight_limit): + C = np.random.uniform( + 0, weight_limit, size=(n, n) + ).round().astype(np.float32) + + C_series = cudf.Series(C.flatten()) + + start = timer() + cugraph_cost, matching = cugraph.dense_hungarian(C_series, n, n) + end = timer() + + print('cugraph time: ', (end - start)) + + start = timer() + np_matching = linear_sum_assignment(C) + end = timer() + + print('scipy time: ', (end - start)) - print('scipy_cost = ', scipy_cost) - print('cugraph_cost = ', cugraph_cost) + scipy_cost = C[np_matching[0], np_matching[1]].sum() assert(scipy_cost == cugraph_cost) diff --git a/python/cugraph/tests/test_hypergraph.py b/python/cugraph/tests/test_hypergraph.py index dbce89905cd..be48168e834 100644 --- a/python/cugraph/tests/test_hypergraph.py +++ b/python/cugraph/tests/test_hypergraph.py @@ -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. # You may obtain a copy of the License at @@ -34,12 +34,13 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -import cudf -from cudf.tests.utils import assert_eq -import cugraph import datetime as dt + import pandas as pd import pytest +import cudf +from cudf.testing.testing import assert_frame_equal +import cugraph simple_df = cudf.DataFrame.from_pandas(pd.DataFrame({ @@ -107,11 +108,10 @@ def test_hyperedges(categorical_metadata): h = cugraph.hypergraph(simple_df, categorical_metadata=categorical_metadata) - assert_eq( - len(h.keys()), len(["entities", "nodes", "edges", "events", "graph"]) - ) + assert len(h.keys()) == len( + ["entities", "nodes", "edges", "events", "graph"]) - edges = pd.DataFrame({ + edges = cudf.from_pandas(pd.DataFrame({ "event_id": [ "event_id::0", "event_id::1", @@ -158,25 +158,24 @@ def test_hyperedges(categorical_metadata): "a1": [1, 2, 3] * 4, "a2": ["red", "blue", "green"] * 4, "πŸ™ˆ": ["Γ¦ski Δ“ΛˆmōjΔ“", "πŸ˜‹", "s"] * 4, - }) + })) if categorical_metadata: edges = edges.astype({"edge_type": "category"}) - assert_eq(edges, h["edges"]) - + assert_frame_equal(edges, h["edges"], check_dtype=False) for (k, v) in [ ("entities", 12), ("nodes", 15), ("edges", 12), ("events", 3) ]: - assert_eq(len(h[k]), v) + assert len(h[k]) == v def test_hyperedges_direct(): h = cugraph.hypergraph(hyper_df, direct=True) - assert_eq(len(h["edges"]), 9) - assert_eq(len(h["nodes"]), 9) + assert len(h["edges"]) == 9 + assert len(h["nodes"]) == 9 def test_hyperedges_direct_categories(): @@ -191,8 +190,8 @@ def test_hyperedges_direct_categories(): }, ) - assert_eq(len(h["edges"]), 9) - assert_eq(len(h["nodes"]), 6) + assert len(h["edges"]) == 9 + assert len(h["nodes"]) == 6 def test_hyperedges_direct_manual_shaping(): @@ -202,14 +201,14 @@ def test_hyperedges_direct_manual_shaping(): direct=True, EDGES={"aa": ["cc"], "cc": ["cc"]}, ) - assert_eq(len(h1["edges"]), 6) + assert len(h1["edges"]) == 6 h2 = cugraph.hypergraph( hyper_df, direct=True, EDGES={"aa": ["cc", "bb", "aa"], "cc": ["cc"]}, ) - assert_eq(len(h2["edges"]), 12) + assert len(h2["edges"]) == 12 @pytest.mark.parametrize("categorical_metadata", [False, True]) @@ -220,9 +219,8 @@ def test_drop_edge_attrs(categorical_metadata): drop_edge_attrs=True, categorical_metadata=categorical_metadata) - assert_eq( - len(h.keys()), len(["entities", "nodes", "edges", "events", "graph"]) - ) + assert len(h.keys()) == len( + ["entities", "nodes", "edges", "events", "graph"]) edges = cudf.DataFrame.from_pandas(pd.DataFrame({ "event_id": [ @@ -255,12 +253,12 @@ def test_drop_edge_attrs(categorical_metadata): if categorical_metadata: edges = edges.astype({"edge_type": "category"}) - assert_eq(edges, h["edges"]) + assert_frame_equal(edges, h["edges"], check_dtype=False) for (k, v) in [ ("entities", 9), ("nodes", 12), ("edges", 9), ("events", 3) ]: - assert_eq(len(h[k]), v) + assert len(h[k]) == v @pytest.mark.parametrize("categorical_metadata", [False, True]) @@ -275,9 +273,8 @@ def test_drop_edge_attrs_direct(categorical_metadata): categorical_metadata=categorical_metadata, ) - assert_eq( - len(h.keys()), len(["entities", "nodes", "edges", "events", "graph"]) - ) + assert len(h.keys()) == len( + ["entities", "nodes", "edges", "events", "graph"]) edges = cudf.DataFrame.from_pandas(pd.DataFrame({ "event_id": [ @@ -298,10 +295,10 @@ def test_drop_edge_attrs_direct(categorical_metadata): if categorical_metadata: edges = edges.astype({"edge_type": "category"}) - assert_eq(edges, h["edges"]) + assert_frame_equal(edges, h["edges"], check_dtype=False) for (k, v) in [("entities", 9), ("nodes", 9), ("edges", 6), ("events", 0)]: - assert_eq(len(h[k]), v) + assert len(h[k]) == v def test_skip_hyper(): @@ -397,10 +394,10 @@ def test_skip_na_hyperedge(): nans_df, drop_edge_attrs=True )["edges"] - assert_eq(len(skip_attr_h_edges), len(expected_hits)) + assert len(skip_attr_h_edges) == len(expected_hits) default_h_edges = cugraph.hypergraph(nans_df)["edges"] - assert_eq(len(default_h_edges), len(expected_hits)) + assert len(default_h_edges) == len(expected_hits) def test_hyper_to_pa_vanilla(): diff --git a/python/cugraph/tests/test_jaccard.py b/python/cugraph/tests/test_jaccard.py index 3c3f6224d83..cc2795cb464 100644 --- a/python/cugraph/tests/test_jaccard.py +++ b/python/cugraph/tests/test_jaccard.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -13,11 +13,12 @@ import gc import time - import pytest +import cudf import cugraph from cugraph.tests import utils +from pathlib import PurePath # Temporarily suppress warnings till networkX fixes deprecation warnings # (Using or importing the ABCs from 'collections' instead of from @@ -113,7 +114,9 @@ def test_jaccard(graph_file): assert err == 0 -@pytest.mark.parametrize("graph_file", ["../datasets/netscience.csv"]) +@pytest.mark.parametrize("graph_file", [PurePath( + utils.RAPIDS_DATASET_ROOT_DIR)/"netscience.csv"] +) def test_jaccard_edgevals(graph_file): gc.collect() @@ -220,3 +223,32 @@ def test_jaccard_nx(graph_file): # FIXME: Nx does a full all-pair Jaccard. # cuGraph does a limited 1-hop Jaccard # assert nx_j == cg_j + + +@pytest.mark.parametrize("graph_file", utils.DATASETS_UNDIRECTED) +def test_jaccard_multi_column(graph_file): + gc.collect() + + M = utils.read_csv_for_nx(graph_file) + + cu_M = cudf.DataFrame() + cu_M["src_0"] = cudf.Series(M["0"]) + cu_M["dst_0"] = cudf.Series(M["1"]) + cu_M["src_1"] = cu_M["src_0"] + 1000 + cu_M["dst_1"] = cu_M["dst_0"] + 1000 + G1 = cugraph.Graph() + G1.from_cudf_edgelist(cu_M, source=["src_0", "src_1"], + destination=["dst_0", "dst_1"]) + + vertex_pair = cu_M[["src_0", "src_1", "dst_0", "dst_1"]] + vertex_pair = vertex_pair[:5] + + df_res = cugraph.jaccard(G1, vertex_pair) + + G2 = cugraph.Graph() + G2.from_cudf_edgelist(cu_M, source="src_0", + destination="dst_0") + df_exp = cugraph.jaccard(G2, vertex_pair[["src_0", "dst_0"]]) + + # Calculating mismatch + assert df_res["jaccard_coeff"].equals(df_exp["jaccard_coeff"]) diff --git a/python/cugraph/tests/test_k_core.py b/python/cugraph/tests/test_k_core.py index 5e3220dcfb1..d09b719ab79 100644 --- a/python/cugraph/tests/test_k_core.py +++ b/python/cugraph/tests/test_k_core.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -57,7 +57,6 @@ def calc_k_cores(graph_file, directed=True): def compare_edges(cg, nxg): edgelist_df = cg.view_edge_list() src, dest = edgelist_df["src"], edgelist_df["dst"] - assert cg.edgelist.weights is False assert len(src) == nxg.size() for i in range(len(src)): @@ -66,7 +65,7 @@ def compare_edges(cg, nxg): @pytest.mark.parametrize("graph_file", utils.DATASETS_UNDIRECTED) -def test_core_number_Graph(graph_file): +def test_k_core_Graph(graph_file): gc.collect() cu_kcore, nx_kcore = calc_k_cores(graph_file, False) @@ -75,7 +74,7 @@ def test_core_number_Graph(graph_file): @pytest.mark.parametrize("graph_file", utils.DATASETS_UNDIRECTED) -def test_core_number_Graph_nx(graph_file): +def test_k_core_Graph_nx(graph_file): gc.collect() NM = utils.read_csv_for_nx(graph_file) @@ -86,3 +85,35 @@ def test_core_number_Graph_nx(graph_file): cc = cugraph.k_core(Gnx) assert nx.is_isomorphic(nc, cc) + + +@pytest.mark.parametrize("graph_file", utils.DATASETS_UNDIRECTED) +def test_k_core_corenumber_multicolumn(graph_file): + gc.collect() + + cu_M = utils.read_csv_file(graph_file) + cu_M.rename(columns={'0': 'src_0', '1': 'dst_0'}, inplace=True) + cu_M['src_1'] = cu_M['src_0'] + 1000 + cu_M['dst_1'] = cu_M['dst_0'] + 1000 + + G1 = cugraph.Graph() + G1.from_cudf_edgelist(cu_M, source=["src_0", "src_1"], + destination=["dst_0", "dst_1"]) + + corenumber_G1 = cugraph.core_number(G1) + corenumber_G1.rename(columns={'core_number': 'values'}, inplace=True) + corenumber_G1 = corenumber_G1[['0_vertex', '1_vertex', 'values']] + + ck_res = cugraph.k_core(G1, core_number=corenumber_G1) + G2 = cugraph.Graph() + G2.from_cudf_edgelist(cu_M, source="src_0", + destination="dst_0") + ck_exp = cugraph.k_core(G2) + + # FIXME: Replace with multi-column view_edge_list() + edgelist_df = ck_res.edgelist.edgelist_df + edgelist_df_res = ck_res.unrenumber(edgelist_df, "src") + edgelist_df_res = ck_res.unrenumber(edgelist_df_res, "dst") + for i in range(len(edgelist_df_res)): + assert ck_exp.has_edge(edgelist_df_res["0_src"].iloc[i], + edgelist_df_res["0_dst"].iloc[i]) diff --git a/python/cugraph/tests/test_k_truss_subgraph.py b/python/cugraph/tests/test_k_truss_subgraph.py index e9ccac81cf6..1a1f5c66693 100644 --- a/python/cugraph/tests/test_k_truss_subgraph.py +++ b/python/cugraph/tests/test_k_truss_subgraph.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -19,6 +19,7 @@ from cugraph.tests import utils import numpy as np +from numba import cuda # Temporarily suppress warnings till networkX fixes deprecation warnings # (Using or importing the ABCs from 'collections' instead of from @@ -41,7 +42,11 @@ # currently in networkx master and will hopefully will make it to a release # soon. def ktruss_ground_truth(graph_file): - G = nx.read_edgelist(graph_file, nodetype=int, data=(("weights", float),)) + G = nx.read_edgelist( + str(graph_file), + nodetype=int, + data=(("weights", float),) + ) df = nx.to_pandas_edgelist(G) return df @@ -69,6 +74,31 @@ def compare_k_truss(k_truss_cugraph, k, ground_truth_file): return True +__cuda_version = cuda.runtime.get_version() +__unsupported_cuda_version = (11, 4) + + +# FIXME: remove when ktruss is supported on CUDA 11.4 +def test_unsupported_cuda_version(): + """ + Ensures the proper exception is raised when ktruss is called in an + unsupported env, and not when called in a supported env. + """ + k = 5 + cu_M = utils.read_csv_file(utils.DATASETS_KTRUSS[0][0]) + G = cugraph.Graph() + G.from_cudf_edgelist(cu_M, source="0", destination="1", edge_attr="2") + + if __cuda_version == __unsupported_cuda_version: + with pytest.raises(NotImplementedError): + cugraph.k_truss(G, k) + else: + cugraph.k_truss(G, k) + + +@pytest.mark.skipif((__cuda_version == __unsupported_cuda_version), + reason="skipping on unsupported CUDA " + f"{__unsupported_cuda_version} environment.") @pytest.mark.parametrize("graph_file, nx_ground_truth", utils.DATASETS_KTRUSS) def test_ktruss_subgraph_Graph(graph_file, nx_ground_truth): gc.collect() @@ -82,6 +112,9 @@ def test_ktruss_subgraph_Graph(graph_file, nx_ground_truth): compare_k_truss(k_subgraph, k, nx_ground_truth) +@pytest.mark.skipif((__cuda_version == __unsupported_cuda_version), + reason="skipping on unsupported CUDA " + f"{__unsupported_cuda_version} environment.") @pytest.mark.parametrize("graph_file, nx_ground_truth", utils.DATASETS_KTRUSS) def test_ktruss_subgraph_Graph_nx(graph_file, nx_ground_truth): gc.collect() @@ -93,9 +126,6 @@ def test_ktruss_subgraph_Graph_nx(graph_file, nx_ground_truth): create_using=nx.Graph() ) k_subgraph = cugraph.k_truss(G, k) - df = nx.to_pandas_edgelist(k_subgraph) - k_truss_nx = nx.k_truss(G, k) - nx_df = nx.to_pandas_edgelist(k_truss_nx) - assert len(df) == len(nx_df) + assert nx.is_isomorphic(k_subgraph, k_truss_nx) diff --git a/python/cugraph/tests/test_katz_centrality.py b/python/cugraph/tests/test_katz_centrality.py index a2a03c1518b..ef2f45c08a4 100644 --- a/python/cugraph/tests/test_katz_centrality.py +++ b/python/cugraph/tests/test_katz_centrality.py @@ -1,4 +1,5 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. + +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -15,6 +16,7 @@ import pytest +import cudf import cugraph from cugraph.tests import utils @@ -112,3 +114,38 @@ def test_katz_centrality_nx(graph_file): err = err + 1 print("Mismatches:", err) assert err < (0.1 * len(ck)) + + +@pytest.mark.parametrize("graph_file", utils.DATASETS_UNDIRECTED) +def test_katz_centrality_multi_column(graph_file): + gc.collect() + + cu_M = utils.read_csv_file(graph_file) + cu_M.rename(columns={'0': 'src_0', '1': 'dst_0'}, inplace=True) + cu_M['src_1'] = cu_M['src_0'] + 1000 + cu_M['dst_1'] = cu_M['dst_0'] + 1000 + + G1 = cugraph.DiGraph() + G1.from_cudf_edgelist(cu_M, source=["src_0", "src_1"], + destination=["dst_0", "dst_1"]) + + G2 = cugraph.DiGraph() + G2.from_cudf_edgelist(cu_M, source="src_0", destination="dst_0") + + k_df_exp = cugraph.katz_centrality(G2, alpha=None, max_iter=1000) + k_df_exp = k_df_exp.sort_values("vertex").reset_index(drop=True) + + nstart = cudf.DataFrame() + nstart['vertex_0'] = k_df_exp['vertex'] + nstart['vertex_1'] = nstart['vertex_0'] + 1000 + nstart['values'] = k_df_exp['katz_centrality'] + + k_df_res = cugraph.katz_centrality(G1, nstart=nstart, + alpha=None, max_iter=1000) + k_df_res = k_df_res.sort_values("0_vertex").reset_index(drop=True) + k_df_res.rename(columns={'0_vertex': 'vertex'}, inplace=True) + + top_res = topKVertices(k_df_res, "katz_centrality", 10) + top_exp = topKVertices(k_df_exp, "katz_centrality", 10) + + assert top_res.equals(top_exp) diff --git a/python/cugraph/tests/test_leiden.py b/python/cugraph/tests/test_leiden.py index d6a7f86b5c5..b6c23dad6f2 100644 --- a/python/cugraph/tests/test_leiden.py +++ b/python/cugraph/tests/test_leiden.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -81,18 +81,13 @@ def test_leiden_nx(graph_file): NM = utils.read_csv_for_nx(graph_file) if edgevals: - G = nx.from_pandas_edgelist(NM, - create_using=nx.Graph(), - source="0", - target="1" - ) + G = nx.from_pandas_edgelist( + NM, create_using=nx.Graph(), source="0", target="1" + ) else: - G = nx.from_pandas_edgelist(NM, - create_using=nx.Graph(), - source="0", - target="1", - edge_attr="2" - ) + G = nx.from_pandas_edgelist( + NM, create_using=nx.Graph(), source="0", target="1", edge_attr="2" + ) leiden_parts, leiden_mod = cugraph_leiden(G, edgevals=True) louvain_parts, louvain_mod = cugraph_louvain(G, edgevals=True) diff --git a/python/cugraph/tests/test_louvain.py b/python/cugraph/tests/test_louvain.py index d6b0030eb73..fc112b8d657 100644 --- a/python/cugraph/tests/test_louvain.py +++ b/python/cugraph/tests/test_louvain.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -78,11 +78,12 @@ def test_louvain_with_edgevals(graph_file): nx_parts = networkx_call(M) # Calculating modularity scores for comparison Gnx = nx.from_pandas_edgelist( - M, source="0", target="1", edge_attr="weight", create_using=nx.Graph() + M, source="0", target="1", + edge_attr="weight", create_using=nx.Graph() ) cu_parts = cu_parts.to_pandas() - cu_map = dict(zip(cu_parts['vertex'], cu_parts['partition'])) + cu_map = dict(zip(cu_parts["vertex"], cu_parts["partition"])) assert set(nx_parts.keys()) == set(cu_map.keys()) @@ -105,11 +106,12 @@ def test_louvain(graph_file): # Calculating modularity scores for comparison Gnx = nx.from_pandas_edgelist( - M, source="0", target="1", edge_attr="weight", create_using=nx.Graph() + M, source="0", target="1", + edge_attr="weight", create_using=nx.Graph() ) cu_parts = cu_parts.to_pandas() - cu_map = dict(zip(cu_parts['vertex'], cu_parts['partition'])) + cu_map = dict(zip(cu_parts["vertex"], cu_parts["partition"])) assert set(nx_parts.keys()) == set(cu_map.keys()) diff --git a/python/cugraph/tests/test_maximum_spanning_tree.py b/python/cugraph/tests/test_maximum_spanning_tree.py index e20e2f72267..311f28bd6f8 100644 --- a/python/cugraph/tests/test_maximum_spanning_tree.py +++ b/python/cugraph/tests/test_maximum_spanning_tree.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -11,16 +11,17 @@ # See the License for the specific language governing permissions and # limitations under the License. +import time import gc import pytest +import numpy as np +import rmm +import cudf import cugraph from cugraph.tests import utils -import rmm -import cudf -import time -import numpy as np + # Temporarily suppress warnings till networkX fixes deprecation warnings # (Using or importing the ABCs from 'collections' instead of from diff --git a/python/cugraph/tests/test_minimum_spanning_tree.py b/python/cugraph/tests/test_minimum_spanning_tree.py index 55ebdcfda08..d1588507bce 100644 --- a/python/cugraph/tests/test_minimum_spanning_tree.py +++ b/python/cugraph/tests/test_minimum_spanning_tree.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -11,16 +11,17 @@ # See the License for the specific language governing permissions and # limitations under the License. +import time import gc import pytest +import numpy as np +import rmm +import cudf import cugraph from cugraph.tests import utils -import rmm -import cudf -import time -import numpy as np + # Temporarily suppress warnings till networkX fixes deprecation warnings # (Using or importing the ABCs from 'collections' instead of from diff --git a/python/cugraph/tests/test_modularity.py b/python/cugraph/tests/test_modularity.py index 7a7d42d1592..21b8adae6e6 100644 --- a/python/cugraph/tests/test_modularity.py +++ b/python/cugraph/tests/test_modularity.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -71,6 +71,43 @@ def test_modularity_clustering(graph_file, partitions): assert cu_score > rand_score +# Test all combinations of default/managed and pooled/non-pooled allocation +@pytest.mark.parametrize("graph_file", utils.DATASETS) +@pytest.mark.parametrize("partitions", PARTITIONS) +def test_modularity_clustering_multi_column(graph_file, partitions): + gc.collect() + + # Read in the graph and get a cugraph object + cu_M = utils.read_csv_file(graph_file, read_weights_in_sp=False) + cu_M.rename(columns={'0': 'src_0', '1': 'dst_0'}, inplace=True) + cu_M['src_1'] = cu_M['src_0'] + 1000 + cu_M['dst_1'] = cu_M['dst_0'] + 1000 + + G1 = cugraph.Graph() + G1.from_cudf_edgelist(cu_M, source=["src_0", "src_1"], + destination=["dst_0", "dst_1"], + edge_attr="2") + + df1 = cugraph.spectralModularityMaximizationClustering( + G1, partitions, num_eigen_vects=(partitions - 1) + ) + + cu_score = cugraph.analyzeClustering_modularity(G1, partitions, df1, + ['0_vertex', + '1_vertex'], + 'cluster') + + G2 = cugraph.Graph() + G2.from_cudf_edgelist(cu_M, source="src_0", + destination="dst_0", + edge_attr="2") + + rand_score = random_call(G2, partitions) + # Assert that the partitioning has better modularity than the random + # assignment + assert cu_score > rand_score + + # Test to ensure DiGraph objs are not accepted # Test all combinations of default/managed and pooled/non-pooled allocation diff --git a/python/cugraph/tests/test_multigraph.py b/python/cugraph/tests/test_multigraph.py new file mode 100644 index 00000000000..57be3eb34e8 --- /dev/null +++ b/python/cugraph/tests/test_multigraph.py @@ -0,0 +1,119 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import gc + +import pytest +import networkx as nx +import numpy as np + +import cugraph +from cugraph.tests import utils + + +# ============================================================================= +# Pytest Setup / Teardown - called for each test function +# ============================================================================= +def setup_function(): + gc.collect() + + +@pytest.mark.parametrize("graph_file", utils.DATASETS) +def test_multigraph(graph_file): + # FIXME: Migrate to new test fixtures for Graph setup once available + cuM = utils.read_csv_file(graph_file) + G = cugraph.MultiDiGraph() + G.from_cudf_edgelist(cuM, source="0", destination="1", edge_attr="2") + + nxM = utils.read_csv_for_nx(graph_file, read_weights_in_sp=True) + Gnx = nx.from_pandas_edgelist( + nxM, + source="0", + target="1", + edge_attr="weight", + create_using=nx.MultiDiGraph(), + ) + + assert G.number_of_edges() == Gnx.number_of_edges() + assert G.number_of_nodes() == Gnx.number_of_nodes() + cuedges = cugraph.to_pandas_edgelist(G) + cuedges.rename(columns={"src": "source", "dst": "target", + "weights": "weight"}, inplace=True) + cuedges["weight"] = cuedges["weight"].round(decimals=3) + nxedges = nx.to_pandas_edgelist(Gnx).astype(dtype={"source": "int32", + "target": "int32", + "weight": "float32"}) + cuedges = cuedges.sort_values(by=["source", "target"]).\ + reset_index(drop=True) + nxedges = nxedges.sort_values(by=["source", "target"]).\ + reset_index(drop=True) + nxedges["weight"] = nxedges["weight"].round(decimals=3) + assert nxedges.equals(cuedges[["source", "target", "weight"]]) + + +@pytest.mark.parametrize("graph_file", utils.DATASETS) +def test_Graph_from_MultiGraph(graph_file): + # FIXME: Migrate to new test fixtures for Graph setup once available + cuM = utils.read_csv_file(graph_file) + GM = cugraph.MultiGraph() + GM.from_cudf_edgelist(cuM, source="0", destination="1", edge_attr="2") + nxM = utils.read_csv_for_nx(graph_file, read_weights_in_sp=True) + GnxM = nx.from_pandas_edgelist( + nxM, + source="0", + target="1", + edge_attr="weight", + create_using=nx.MultiGraph(), + ) + + G = cugraph.Graph(GM) + Gnx = nx.Graph(GnxM) + assert Gnx.number_of_edges() == G.number_of_edges() + + GdM = cugraph.MultiDiGraph() + GdM.from_cudf_edgelist(cuM, source="0", destination="1", edge_attr="2") + GnxdM = nx.from_pandas_edgelist( + nxM, + source="0", + target="1", + edge_attr="weight", + create_using=nx.MultiGraph(), + ) + Gd = cugraph.DiGraph(GdM) + Gnxd = nx.DiGraph(GnxdM) + assert Gnxd.number_of_edges() == Gd.number_of_edges() + + +@pytest.mark.parametrize("graph_file", utils.DATASETS) +def test_multigraph_sssp(graph_file): + # FIXME: Migrate to new test fixtures for Graph setup once available + cuM = utils.read_csv_file(graph_file) + G = cugraph.MultiDiGraph() + G.from_cudf_edgelist(cuM, source="0", destination="1", edge_attr="2") + cu_paths = cugraph.sssp(G, 0) + max_val = np.finfo(cu_paths["distance"].dtype).max + cu_paths = cu_paths[cu_paths["distance"] != max_val] + nxM = utils.read_csv_for_nx(graph_file, read_weights_in_sp=True) + Gnx = nx.from_pandas_edgelist( + nxM, + source="0", + target="1", + edge_attr="weight", + create_using=nx.MultiDiGraph(), + ) + nx_paths = nx.single_source_dijkstra_path_length(Gnx, 0) + + cu_dist = cu_paths.sort_values(by='vertex')['distance'].to_array() + nx_dist = [i[1] for i in sorted(nx_paths.items())] + + assert (cu_dist == nx_dist).all() diff --git a/python/cugraph/tests/test_nx_convert.py b/python/cugraph/tests/test_nx_convert.py index 08a96a801e2..98cc8a11dc7 100644 --- a/python/cugraph/tests/test_nx_convert.py +++ b/python/cugraph/tests/test_nx_convert.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -12,11 +12,14 @@ # limitations under the License. import gc + import pytest import cudf + import cugraph from cugraph.tests import utils + # Temporarily suppress warnings till networkX fixes deprecation warnings # (Using or importing the ABCs from 'collections' instead of from # 'collections.abc' is deprecated, and in 3.8 it will stop working) for @@ -77,7 +80,6 @@ def test_networkx_compatibility(graph_file): _compare_graphs(nxG, cuG) -# Test @pytest.mark.parametrize("graph_file", utils.DATASETS) def test_nx_convert(graph_file): gc.collect() diff --git a/python/cugraph/tests/test_overlap.py b/python/cugraph/tests/test_overlap.py index 53d279478f7..42bc3ea9808 100644 --- a/python/cugraph/tests/test_overlap.py +++ b/python/cugraph/tests/test_overlap.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -17,6 +17,8 @@ import pytest import numpy as np import scipy + +import cudf import cugraph from cugraph.tests import utils @@ -147,3 +149,32 @@ def test_overlap_edge_vals(graph_file): else: diff = abs(cpu_coeff[i] - cu_coeff[i]) assert diff < 1.0e-6 + + +@pytest.mark.parametrize("graph_file", utils.DATASETS_UNDIRECTED) +def test_overlap_multi_column(graph_file): + gc.collect() + + M = utils.read_csv_for_nx(graph_file) + + cu_M = cudf.DataFrame() + cu_M["src_0"] = cudf.Series(M["0"]) + cu_M["dst_0"] = cudf.Series(M["1"]) + cu_M["src_1"] = cu_M["src_0"] + 1000 + cu_M["dst_1"] = cu_M["dst_0"] + 1000 + G1 = cugraph.Graph() + G1.from_cudf_edgelist(cu_M, source=["src_0", "src_1"], + destination=["dst_0", "dst_1"]) + + vertex_pair = cu_M[["src_0", "src_1", "dst_0", "dst_1"]] + vertex_pair = vertex_pair[:5] + + df_res = cugraph.overlap(G1, vertex_pair) + + G2 = cugraph.Graph() + G2.from_cudf_edgelist(cu_M, source="src_0", + destination="dst_0") + df_exp = cugraph.overlap(G2, vertex_pair[["src_0", "dst_0"]]) + + # Calculating mismatch + assert df_res["overlap_coeff"].equals(df_exp["overlap_coeff"]) diff --git a/python/cugraph/tests/test_pagerank.py b/python/cugraph/tests/test_pagerank.py index 1ab370041b5..50be1cd5230 100644 --- a/python/cugraph/tests/test_pagerank.py +++ b/python/cugraph/tests/test_pagerank.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -21,6 +21,7 @@ import cugraph from cugraph.tests import utils + # Temporarily suppress warnings till networkX fixes deprecation warnings # (Using or importing the ABCs from 'collections' instead of from # 'collections.abc' is deprecated, and in 3.8 it will stop working) for @@ -143,7 +144,7 @@ def networkx_call(Gnx, max_iter, tol, alpha, personalization_perc, nnz_vtx): # # https://github.com/rapidsai/cugraph/issues/533 # -# @pytest.mark.parametrize("graph_file", utils.DATASETS) + @pytest.mark.parametrize("graph_file", utils.DATASETS) @pytest.mark.parametrize("max_iter", MAX_ITERATIONS) @pytest.mark.parametrize("tol", TOLERANCE) @@ -159,7 +160,8 @@ def test_pagerank( M = utils.read_csv_for_nx(graph_file) nnz_vtx = np.unique(M[['0', '1']]) Gnx = nx.from_pandas_edgelist( - M, source="0", target="1", create_using=nx.DiGraph() + M, source="0", target="1", edge_attr="weight", + create_using=nx.DiGraph() ) networkx_pr, networkx_prsn = networkx_call( @@ -169,13 +171,13 @@ def test_pagerank( cu_nstart = None if has_guess == 1: cu_nstart = cudify(networkx_pr) - max_iter = 5 + max_iter = 20 cu_prsn = cudify(networkx_prsn) # cuGraph PageRank cu_M = utils.read_csv_file(graph_file) G = cugraph.DiGraph() - G.from_cudf_edgelist(cu_M, source="0", destination="1") + G.from_cudf_edgelist(cu_M, source="0", destination="1", edge_attr="2") cugraph_pr = cugraph_call(G, max_iter, tol, alpha, cu_prsn, cu_nstart) @@ -218,7 +220,7 @@ def test_pagerank_nx( cu_nstart = None if has_guess == 1: cu_nstart = cudify(networkx_pr) - max_iter = 5 + max_iter = 20 cu_prsn = cudify(networkx_prsn) # cuGraph PageRank with Nx Graph @@ -239,3 +241,88 @@ def test_pagerank_nx( print(f"{cugraph_pr[i][1]} and {cugraph_pr[i][1]}") print("Mismatches:", err) assert err < (0.01 * len(cugraph_pr)) + + +@pytest.mark.parametrize("graph_file", utils.DATASETS) +@pytest.mark.parametrize("max_iter", MAX_ITERATIONS) +@pytest.mark.parametrize("tol", TOLERANCE) +@pytest.mark.parametrize("alpha", ALPHA) +@pytest.mark.parametrize("personalization_perc", PERSONALIZATION_PERC) +@pytest.mark.parametrize("has_guess", HAS_GUESS) +def test_pagerank_multi_column( + graph_file, max_iter, tol, alpha, personalization_perc, has_guess +): + gc.collect() + + # NetworkX PageRank + M = utils.read_csv_for_nx(graph_file) + nnz_vtx = np.unique(M[['0', '1']]) + + Gnx = nx.from_pandas_edgelist( + M, source="0", target="1", edge_attr="weight", + create_using=nx.DiGraph() + ) + + networkx_pr, networkx_prsn = networkx_call( + Gnx, max_iter, tol, alpha, personalization_perc, nnz_vtx + ) + + cu_nstart = None + if has_guess == 1: + cu_nstart_temp = cudify(networkx_pr) + max_iter = 100 + cu_nstart = cudf.DataFrame() + cu_nstart["vertex_0"] = cu_nstart_temp["vertex"] + cu_nstart["vertex_1"] = cu_nstart["vertex_0"] + 1000 + cu_nstart["values"] = cu_nstart_temp["values"] + + cu_prsn_temp = cudify(networkx_prsn) + if cu_prsn_temp is not None: + cu_prsn = cudf.DataFrame() + cu_prsn["vertex_0"] = cu_prsn_temp["vertex"] + cu_prsn["vertex_1"] = cu_prsn["vertex_0"] + 1000 + cu_prsn["values"] = cu_prsn_temp["values"] + else: + cu_prsn = cu_prsn_temp + + cu_M = cudf.DataFrame() + cu_M["src_0"] = cudf.Series(M["0"]) + cu_M["dst_0"] = cudf.Series(M["1"]) + cu_M["src_1"] = cu_M["src_0"] + 1000 + cu_M["dst_1"] = cu_M["dst_0"] + 1000 + cu_M["weights"] = cudf.Series(M["weight"]) + + cu_G = cugraph.DiGraph() + cu_G.from_cudf_edgelist(cu_M, source=["src_0", "src_1"], + destination=["dst_0", "dst_1"], + edge_attr="weights") + + df = cugraph.pagerank( + cu_G, + alpha=alpha, + max_iter=max_iter, + tol=tol, + personalization=cu_prsn, + nstart=cu_nstart, + ) + + cugraph_pr = [] + + df = df.sort_values("0_vertex").reset_index(drop=True) + + pr_scores = df["pagerank"].to_array() + for i, rank in enumerate(pr_scores): + cugraph_pr.append((i, rank)) + + # Calculating mismatch + networkx_pr = sorted(networkx_pr.items(), key=lambda x: x[0]) + err = 0 + assert len(cugraph_pr) == len(networkx_pr) + for i in range(len(cugraph_pr)): + if ( + abs(cugraph_pr[i][1] - networkx_pr[i][1]) > tol * 1.1 + and cugraph_pr[i][0] == networkx_pr[i][0] + ): + err = err + 1 + print("Mismatches:", err) + assert err < (0.01 * len(cugraph_pr)) diff --git a/python/cugraph/tests/test_paths.py b/python/cugraph/tests/test_paths.py index 7467d024051..56cc9b3cd50 100644 --- a/python/cugraph/tests/test_paths.py +++ b/python/cugraph/tests/test_paths.py @@ -1,11 +1,27 @@ +# Copyright (c) 2019-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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import sys +from tempfile import NamedTemporaryFile + import cudf -import cugraph from cupy.sparse import coo_matrix as cupy_coo_matrix import cupy import networkx as nx import pytest -import sys -from tempfile import NamedTemporaryFile + +import cugraph + CONNECTED_GRAPH = """1,5,3 1,4,1 diff --git a/python/cugraph/tests/test_random_walks.py b/python/cugraph/tests/test_random_walks.py new file mode 100644 index 00000000000..302a93cd02a --- /dev/null +++ b/python/cugraph/tests/test_random_walks.py @@ -0,0 +1,221 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import gc + +import pytest + +from cugraph.tests import utils +import cugraph +import random + + +# ============================================================================= +# Parameters +# ============================================================================= +DIRECTED_GRAPH_OPTIONS = [False, True] +WEIGHTED_GRAPH_OPTIONS = [False, True] +DATASETS = [pytest.param(d) for d in utils.DATASETS] +DATASETS_SMALL = [pytest.param(d) for d in utils.DATASETS_SMALL] + + +def calc_random_walks(graph_file, + directed=False, + max_depth=None, + use_padding=False): + """ + compute random walks for each nodes in 'start_vertices' + + parameters + ---------- + G : cuGraph.Graph or networkx.Graph + The graph can be either directed (DiGraph) or undirected (Graph). + Weights in the graph are ignored. + Use weight parameter if weights need to be considered + (currently not supported) + + start_vertices : int or list or cudf.Series + A single node or a list or a cudf.Series of nodes from which to run + the random walks + + max_depth : int + The maximum depth of the random walks + + use_padding : bool + If True, padded paths are returned else coalesced paths are returned. + + Returns + ------- + vertex_paths : cudf.Series or cudf.DataFrame + Series containing the vertices of edges/paths in the random walk. + + edge_weight_paths: cudf.Series + Series containing the edge weights of edges represented by the + returned vertex_paths + + sizes: int + The path size in case of coalesced paths. + """ + G = utils.generate_cugraph_graph_from_file( + graph_file, directed=directed, edgevals=True) + assert G is not None + + k = random.randint(1, 10) + start_vertices = random.sample(range(G.number_of_vertices()), k) + vertex_paths, edge_weights, vertex_path_sizes = cugraph.random_walks( + G, start_vertices, max_depth, use_padding) + + return (vertex_paths, edge_weights, vertex_path_sizes), start_vertices + + +def check_random_walks(path_data, seeds, df_G=None): + invalid_edge = 0 + invalid_seeds = 0 + offsets_idx = 0 + next_path_idx = 0 + v_paths = path_data[0] + sizes = path_data[2].to_array().tolist() + + for s in sizes: + for i in range(next_path_idx, next_path_idx+s-1): + src, dst = v_paths.iloc[i], v_paths.iloc[i+1] + if i == next_path_idx and src != seeds[offsets_idx]: + invalid_seeds += 1 + print( + "[ERR] Invalid seed: " + " src {} != src {}" + .format(src, seeds[offsets_idx]) + ) + offsets_idx += 1 + next_path_idx += s + + exp_edge = df_G.loc[ + (df_G['src'] == (src)) & ( + df_G['dst'] == (dst))].reset_index(drop=True) + + if not (exp_edge['src'].loc[0], exp_edge['dst'].loc[0]) == (src, dst): + print( + "[ERR] Invalid edge: " + "There is no edge src {} dst {}" + .format(src, dst) + ) + invalid_edge += 1 + + assert invalid_edge == 0 + assert invalid_seeds == 0 + +# ============================================================================= +# Pytest Setup / Teardown - called for each test function +# ============================================================================= + + +def prepare_test(): + gc.collect() + + +@pytest.mark.parametrize("graph_file", utils.DATASETS_SMALL) +@pytest.mark.parametrize("directed", DIRECTED_GRAPH_OPTIONS) +@pytest.mark.parametrize("max_depth", [None]) +def test_random_walks_invalid_max_dept(graph_file, + directed, + max_depth): + prepare_test() + with pytest.raises(TypeError): + df, offsets, seeds = calc_random_walks( + graph_file, + directed=directed, + max_depth=max_depth + ) + + +@pytest.mark.parametrize("graph_file", utils.DATASETS_SMALL) +@pytest.mark.parametrize("directed", DIRECTED_GRAPH_OPTIONS) +def test_random_walks_coalesced( + graph_file, + directed +): + max_depth = random.randint(2, 10) + df_G = utils.read_csv_file(graph_file) + df_G.rename( + columns={"0": "src", "1": "dst", "2": "weight"}, inplace=True) + path_data, seeds = calc_random_walks( + graph_file, + directed, + max_depth=max_depth + ) + check_random_walks(path_data, seeds, df_G) + + # Check path query output + df = cugraph.rw_path(len(seeds), path_data[2]) + v_offsets = [0] + path_data[2].cumsum()[:-1].to_array().tolist() + w_offsets = [0] + (path_data[2]-1).cumsum()[:-1].to_array().tolist() + + assert df['weight_sizes'].equals(path_data[2]-1) + assert df['vertex_offsets'].to_array().tolist() == v_offsets + assert df['weight_offsets'].to_array().tolist() == w_offsets + + +@pytest.mark.parametrize("graph_file", utils.DATASETS_SMALL) +@pytest.mark.parametrize("directed", DIRECTED_GRAPH_OPTIONS) +def test_random_walks_padded( + graph_file, + directed +): + max_depth = random.randint(2, 10) + df_G = utils.read_csv_file(graph_file) + df_G.rename( + columns={"0": "src", "1": "dst", "2": "weight"}, inplace=True) + path_data, seeds = calc_random_walks( + graph_file, + directed, + max_depth=max_depth, + use_padding=True + ) + v_paths = path_data[0] + e_weights = path_data[1] + assert len(v_paths) == max_depth*len(seeds) + assert len(e_weights) == (max_depth - 1)*len(seeds) + + +"""@pytest.mark.parametrize("graph_file", utils.DATASETS_SMALL) +@pytest.mark.parametrize("directed", DIRECTED_GRAPH_OPTIONS) +def test_random_walks( + graph_file, + directed +): + max_depth = random.randint(2, 10) + df_G = utils.read_csv_file(graph_file) + df_G.rename( + columns={"0": "src", "1": "dst", "2": "weight"}, inplace=True) + df_G['src_0'] = df_G['src'] + 1000 + df_G['dst_0'] = df_G['dst'] + 1000 + + if directed: + G = cugraph.DiGraph() + else: + G = cugraph.Graph() + G.from_cudf_edgelist(df_G, source=['src', 'src_0'], + destination=['dst', 'dst_0'], + edge_attr="weight") + + k = random.randint(1, 10) + start_vertices = random.sample(G.nodes().to_array().tolist(), k) + + seeds = cudf.DataFrame() + seeds['v'] = start_vertices + seeds['v_0'] = seeds['v'] + 1000 + + df, offsets = cugraph.random_walks(G, seeds, max_depth) + + check_random_walks(df, offsets, seeds, df_G) +""" diff --git a/python/cugraph/tests/test_renumber.py b/python/cugraph/tests/test_renumber.py index 91416942429..129bd667621 100644 --- a/python/cugraph/tests/test_renumber.py +++ b/python/cugraph/tests/test_renumber.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -17,8 +17,8 @@ import pandas as pd import pytest - import cudf + from cugraph.structure.number_map import NumberMap from cugraph.tests import utils @@ -44,13 +44,14 @@ def test_renumber_ips(): gdf["source_as_int"] = gdf["source_list"].str.ip2int() gdf["dest_as_int"] = gdf["dest_list"].str.ip2int() - numbering = NumberMap() - numbering.from_series(gdf["source_as_int"], gdf["dest_as_int"]) - src = numbering.to_internal_vertex_id(gdf["source_as_int"]) - dst = numbering.to_internal_vertex_id(gdf["dest_as_int"]) + renumbered_gdf, renumber_map = NumberMap.renumber(gdf, + "source_as_int", + "dest_as_int") - check_src = numbering.from_internal_vertex_id(src)["0"] - check_dst = numbering.from_internal_vertex_id(dst)["0"] + check_src = renumber_map.from_internal_vertex_id(renumbered_gdf['src'] + )["0"] + check_dst = renumber_map.from_internal_vertex_id(renumbered_gdf['dst'] + )["0"] assert check_src.equals(gdf["source_as_int"]) assert check_dst.equals(gdf["dest_as_int"]) @@ -78,13 +79,14 @@ def test_renumber_ips_cols(): gdf["source_as_int"] = gdf["source_list"].str.ip2int() gdf["dest_as_int"] = gdf["dest_list"].str.ip2int() - numbering = NumberMap() - numbering.from_dataframe(gdf, ["source_as_int"], ["dest_as_int"]) - src = numbering.to_internal_vertex_id(gdf["source_as_int"]) - dst = numbering.to_internal_vertex_id(gdf["dest_as_int"]) + renumbered_gdf, renumber_map = NumberMap.renumber(gdf, + ["source_as_int"], + ["dest_as_int"]) - check_src = numbering.from_internal_vertex_id(src)["0"] - check_dst = numbering.from_internal_vertex_id(dst)["0"] + check_src = renumber_map.from_internal_vertex_id(renumbered_gdf['src'] + )["0"] + check_dst = renumber_map.from_internal_vertex_id(renumbered_gdf['dst'] + )["0"] assert check_src.equals(gdf["source_as_int"]) assert check_dst.equals(gdf["dest_as_int"]) @@ -110,13 +112,14 @@ def test_renumber_ips_str_cols(): gdf = cudf.from_pandas(pdf) - numbering = NumberMap() - numbering.from_dataframe(gdf, ["source_list"], ["dest_list"]) - src = numbering.to_internal_vertex_id(gdf["source_list"]) - dst = numbering.to_internal_vertex_id(gdf["dest_list"]) + renumbered_gdf, renumber_map = NumberMap.renumber(gdf, + ["source_as_int"], + ["dest_as_int"]) - check_src = numbering.from_internal_vertex_id(src)["0"] - check_dst = numbering.from_internal_vertex_id(dst)["0"] + check_src = renumber_map.from_internal_vertex_id(renumbered_gdf['src'] + )["0"] + check_dst = renumber_map.from_internal_vertex_id(renumbered_gdf['dst'] + )["0"] assert check_src.equals(gdf["source_list"]) assert check_dst.equals(gdf["dest_list"]) @@ -130,13 +133,14 @@ def test_renumber_negative(): gdf = cudf.DataFrame.from_pandas(df[["source_list", "dest_list"]]) - numbering = NumberMap() - numbering.from_dataframe(gdf, ["source_list"], ["dest_list"]) - src = numbering.to_internal_vertex_id(gdf["source_list"]) - dst = numbering.to_internal_vertex_id(gdf["dest_list"]) + renumbered_gdf, renumber_map = NumberMap.renumber(gdf, + "source_list", + "dest_list") - check_src = numbering.from_internal_vertex_id(src)["0"] - check_dst = numbering.from_internal_vertex_id(dst)["0"] + check_src = renumber_map.from_internal_vertex_id(renumbered_gdf['src'] + )["0"] + check_dst = renumber_map.from_internal_vertex_id(renumbered_gdf['dst'] + )["0"] assert check_src.equals(gdf["source_list"]) assert check_dst.equals(gdf["dest_list"]) @@ -150,19 +154,54 @@ def test_renumber_negative_col(): gdf = cudf.DataFrame.from_pandas(df[["source_list", "dest_list"]]) - numbering = NumberMap() - numbering.from_dataframe(gdf, ["source_list"], ["dest_list"]) - src = numbering.to_internal_vertex_id(gdf["source_list"]) - dst = numbering.to_internal_vertex_id(gdf["dest_list"]) + renumbered_gdf, renumber_map = NumberMap.renumber(gdf, + "source_list", + "dest_list") - check_src = numbering.from_internal_vertex_id(src)["0"] - check_dst = numbering.from_internal_vertex_id(dst)["0"] + check_src = renumber_map.from_internal_vertex_id(renumbered_gdf['src'] + )["0"] + check_dst = renumber_map.from_internal_vertex_id(renumbered_gdf['dst'] + )["0"] assert check_src.equals(gdf["source_list"]) assert check_dst.equals(gdf["dest_list"]) -# Test all combinations of default/managed and pooled/non-pooled allocation +@pytest.mark.skip(reason="dropped renumbering from series support") +@pytest.mark.parametrize("graph_file", utils.DATASETS) +def test_renumber_series(graph_file): + gc.collect() + + M = utils.read_csv_for_nx(graph_file) + sources = cudf.Series(M["0"]) + destinations = cudf.Series(M["1"]) + + translate = 1000 + + df = cudf.DataFrame() + df["src"] = cudf.Series([x + translate for x in sources. + values_host]) + df["dst"] = cudf.Series([x + translate for x in destinations. + values_host]) + + numbering_series_1 = NumberMap() + numbering_series_1.from_series(df["src"]) + + numbering_series_2 = NumberMap() + numbering_series_2.from_series(df["dst"]) + + renumbered_src = numbering_series_1.add_internal_vertex_id( + df["src"], "src_id") + renumbered_dst = numbering_series_2.add_internal_vertex_id( + df["dst"], "dst_id") + + check_src = numbering_series_1.from_internal_vertex_id(renumbered_src, + "src_id") + check_dst = numbering_series_2.from_internal_vertex_id(renumbered_dst, + "dst_id") + + assert check_src["0_y"].equals(check_src["0_x"]) + assert check_dst["0_y"].equals(check_dst["0_x"]) @pytest.mark.parametrize("graph_file", utils.DATASETS) @@ -181,22 +220,23 @@ def test_renumber_files(graph_file): df["dst"] = cudf.Series([x + translate for x in destinations. values_host]) - numbering = NumberMap() - numbering.from_series(df["src"], df["dst"]) + exp_src = cudf.Series([x + translate for x in sources. + values_host]) + exp_dst = cudf.Series([x + translate for x in destinations. + values_host]) - renumbered_df = numbering.add_internal_vertex_id( - numbering.add_internal_vertex_id(df, "src_id", ["src"]), - "dst_id", ["dst"] - ) + renumbered_df, renumber_map = NumberMap.renumber(df, "src", "dst", + preserve_order=True) - check_src = numbering.from_internal_vertex_id(renumbered_df, "src_id") - check_dst = numbering.from_internal_vertex_id(renumbered_df, "dst_id") + unrenumbered_df = renumber_map.unrenumber(renumbered_df, "src", + preserve_order=True) + unrenumbered_df = renumber_map.unrenumber(unrenumbered_df, "dst", + preserve_order=True) - assert check_src["src"].equals(check_src["0"]) - assert check_dst["dst"].equals(check_dst["0"]) + assert exp_src.equals(unrenumbered_df["src"]) + assert exp_dst.equals(unrenumbered_df["dst"]) -# Test all combinations of default/managed and pooled/non-pooled allocation @pytest.mark.parametrize("graph_file", utils.DATASETS) def test_renumber_files_col(graph_file): gc.collect() @@ -212,22 +252,23 @@ def test_renumber_files_col(graph_file): gdf['dst'] = cudf.Series([x + translate for x in destinations. values_host]) - numbering = NumberMap() - numbering.from_dataframe(gdf, ["src"], ["dst"]) + exp_src = cudf.Series([x + translate for x in sources. + values_host]) + exp_dst = cudf.Series([x + translate for x in destinations. + values_host]) - renumbered_df = numbering.add_internal_vertex_id( - numbering.add_internal_vertex_id(gdf, "src_id", ["src"]), - "dst_id", ["dst"] - ) + renumbered_df, renumber_map = NumberMap.renumber(gdf, ["src"], ["dst"], + preserve_order=True) - check_src = numbering.from_internal_vertex_id(renumbered_df, "src_id") - check_dst = numbering.from_internal_vertex_id(renumbered_df, "dst_id") + unrenumbered_df = renumber_map.unrenumber(renumbered_df, "src", + preserve_order=True) + unrenumbered_df = renumber_map.unrenumber(unrenumbered_df, "dst", + preserve_order=True) - assert check_src["src"].equals(check_src["0"]) - assert check_dst["dst"].equals(check_dst["0"]) + assert exp_src.equals(unrenumbered_df["src"]) + assert exp_dst.equals(unrenumbered_df["dst"]) -# Test all combinations of default/managed and pooled/non-pooled allocation @pytest.mark.parametrize("graph_file", utils.DATASETS) def test_renumber_files_multi_col(graph_file): gc.collect() @@ -244,21 +285,17 @@ def test_renumber_files_multi_col(graph_file): gdf["src"] = sources + translate gdf["dst"] = destinations + translate - numbering = NumberMap() - numbering.from_dataframe(gdf, ["src", "src_old"], ["dst", "dst_old"]) - - renumbered_df = numbering.add_internal_vertex_id( - numbering.add_internal_vertex_id( - gdf, "src_id", ["src", "src_old"] - ), - "dst_id", - ["dst", "dst_old"], - ) + renumbered_df, renumber_map = NumberMap.renumber(gdf, + ["src", "src_old"], + ["dst", "dst_old"], + preserve_order=True) - check_src = numbering.from_internal_vertex_id(renumbered_df, "src_id") - check_dst = numbering.from_internal_vertex_id(renumbered_df, "dst_id") + unrenumbered_df = renumber_map.unrenumber(renumbered_df, "src", + preserve_order=True) + unrenumbered_df = renumber_map.unrenumber(unrenumbered_df, "dst", + preserve_order=True) - assert check_src["src"].equals(check_src["0"]) - assert check_src["src_old"].equals(check_src["1"]) - assert check_dst["dst"].equals(check_dst["0"]) - assert check_dst["dst_old"].equals(check_dst["1"]) + assert gdf["src"].equals(unrenumbered_df["0_src"]) + assert gdf["src_old"].equals(unrenumbered_df["1_src"]) + assert gdf["dst"].equals(unrenumbered_df["0_dst"]) + assert gdf["dst_old"].equals(unrenumbered_df["1_dst"]) diff --git a/python/cugraph/tests/test_sssp.py b/python/cugraph/tests/test_sssp.py index 0a5347a6290..9230b7a7b96 100644 --- a/python/cugraph/tests/test_sssp.py +++ b/python/cugraph/tests/test_sssp.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -29,6 +29,7 @@ import cugraph from cugraph.tests import utils + # Temporarily suppress warnings till networkX fixes deprecation warnings # (Using or importing the ABCs from 'collections' instead of from # 'collections.abc' is deprecated, and in 3.8 it will stop working) for diff --git a/python/cugraph/tests/test_subgraph_extraction.py b/python/cugraph/tests/test_subgraph_extraction.py index a4f36af994a..389a7716e48 100644 --- a/python/cugraph/tests/test_subgraph_extraction.py +++ b/python/cugraph/tests/test_subgraph_extraction.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -20,6 +20,7 @@ import cugraph from cugraph.tests import utils + # Temporarily suppress warnings till networkX fixes deprecation warnings # (Using or importing the ABCs from 'collections' instead of from # 'collections.abc' is deprecated, and in 3.8 it will stop working) for @@ -70,7 +71,6 @@ def nx_call(M, verts, directed=True): return nx.subgraph(G, verts) -# Test all combinations of default/managed and pooled/non-pooled allocation @pytest.mark.parametrize("graph_file", utils.DATASETS) def test_subgraph_extraction_DiGraph(graph_file): gc.collect() @@ -85,9 +85,6 @@ def test_subgraph_extraction_DiGraph(graph_file): assert compare_edges(cu_sg, nx_sg) -# Test all combinations of default/managed and pooled/non-pooled allocation - - @pytest.mark.parametrize("graph_file", utils.DATASETS) def test_subgraph_extraction_Graph(graph_file): gc.collect() @@ -123,10 +120,45 @@ def test_subgraph_extraction_Graph_nx(graph_file): ) nx_sub = nx.subgraph(G, verts) - nx_df = nx.to_pandas_edgelist(nx_sub).to_dict() cu_verts = cudf.Series(verts) cu_sub = cugraph.subgraph(G, cu_verts) - cu_df = nx.to_pandas_edgelist(cu_sub).to_dict() - assert nx_df == cu_df + for (u, v) in cu_sub.edges(): + assert nx_sub.has_edge(u, v) + + +@pytest.mark.parametrize("graph_file", utils.DATASETS) +def test_subgraph_extraction_multi_column(graph_file): + gc.collect() + + M = utils.read_csv_for_nx(graph_file) + + cu_M = cudf.DataFrame() + cu_M["src_0"] = cudf.Series(M["0"]) + cu_M["dst_0"] = cudf.Series(M["1"]) + cu_M["src_1"] = cu_M["src_0"] + 1000 + cu_M["dst_1"] = cu_M["dst_0"] + 1000 + G1 = cugraph.Graph() + G1.from_cudf_edgelist(cu_M, source=["src_0", "src_1"], + destination=["dst_0", "dst_1"]) + + verts = cudf.Series([0, 1, 17]) + verts_G1 = cudf.DataFrame() + verts_G1['v_0'] = verts + verts_G1['v_1'] = verts + 1000 + + sG1 = cugraph.subgraph(G1, verts_G1) + + G2 = cugraph.Graph() + G2.from_cudf_edgelist(cu_M, source="src_0", destination="dst_0") + + sG2 = cugraph.subgraph(G2, verts) + + # FIXME: Replace with multi-column view_edge_list() + edgelist_df = sG1.edgelist.edgelist_df + edgelist_df_res = sG1.unrenumber(edgelist_df, "src") + edgelist_df_res = sG1.unrenumber(edgelist_df_res, "dst") + for i in range(len(edgelist_df_res)): + assert sG2.has_edge(edgelist_df_res["0_src"].iloc[i], + edgelist_df_res["0_dst"].iloc[i]) diff --git a/python/cugraph/tests/test_symmetrize.py b/python/cugraph/tests/test_symmetrize.py index 7ef8b33e97f..4080362ddfa 100644 --- a/python/cugraph/tests/test_symmetrize.py +++ b/python/cugraph/tests/test_symmetrize.py @@ -19,10 +19,9 @@ import cudf import cugraph from cugraph.tests import utils -import cugraph.comms as Comms -from dask.distributed import Client -from dask_cuda import LocalCUDACluster -from cugraph.dask.common.mg_utils import is_single_gpu +from cugraph.dask.common.mg_utils import (is_single_gpu, + setup_local_dask_cluster, + teardown_local_dask_cluster) def test_version(): @@ -188,17 +187,11 @@ def test_symmetrize_weighted(graph_file): compare(cu_M["0"], cu_M["1"], cu_M["2"], sym_src, sym_dst, sym_w) -@pytest.fixture +@pytest.fixture(scope="module") def client_connection(): - cluster = LocalCUDACluster() - client = Client(cluster) - Comms.initialize(p2p=True) - + (cluster, client) = setup_local_dask_cluster(p2p=True) yield client - - Comms.destroy() - client.close() - cluster.close() + teardown_local_dask_cluster(cluster, client) @pytest.mark.skipif( diff --git a/python/cugraph/tests/test_traveling_salesperson.py b/python/cugraph/tests/test_traveling_salesperson.py new file mode 100644 index 00000000000..d43b55c43d0 --- /dev/null +++ b/python/cugraph/tests/test_traveling_salesperson.py @@ -0,0 +1,81 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from cugraph.tests import utils +import cudf +import cugraph +import gc +import numpy as np +import pytest + +# Temporarily suppress warnings till networkX fixes deprecation warnings +# (Using or importing the ABCs from 'collections' instead of from +# 'collections.abc' is deprecated, and in 3.8 it will stop working) for +# python 3.7. Also, this import networkx needs to be relocated in the +# third-party group once this gets fixed. +import warnings + +with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=DeprecationWarning) + import networkx as nx + +print("Networkx version : {} ".format(nx.__version__)) + + +# ============================================================================= +# Pytest Setup / Teardown - called for each test function +# ============================================================================= +def setup_function(): + gc.collect() + + +# ============================================================================= +# Helper functions +# ============================================================================= +def load_tsp(filename=None): + gdf = cudf.read_csv(filename, + delim_whitespace=True, + skiprows=6, + names=["vertex", "x", "y"], + dtypes={"vertex": "int32", + "x": "float32", + "y": "float32"} + ) + gdf = gdf.dropna() + gdf['vertex'] = gdf['vertex'].str.strip() + gdf['vertex'] = gdf['vertex'].astype("int32") + return gdf + + +# ============================================================================= +# Tests +# ============================================================================= +@pytest.mark.parametrize("tsplib_file, ref_cost", utils.DATASETS_TSPLIB) +def test_traveling_salesperson(gpubenchmark, tsplib_file, ref_cost): + pos_list = load_tsp(tsplib_file) + + cu_route, cu_cost = gpubenchmark(cugraph.traveling_salesperson, + pos_list, + restarts=4096) + + print("Cugraph cost: ", cu_cost) + print("Ref cost: ", ref_cost) + error = np.abs(cu_cost - ref_cost) / ref_cost + print("Approximation error is: {:.2f}%".format(error * 100)) + # Check we are within 5% of TSPLIB + assert(error * 100 < 5.) + assert(cu_route.nunique() == pos_list.shape[0]) + assert(cu_route.shape[0] == pos_list.shape[0]) + min_val = pos_list["vertex"].min() + max_val = pos_list["vertex"].max() + assert(cu_route.clip(min_val, max_val).shape[0] == cu_route.shape[0]) diff --git a/python/cugraph/tests/test_triangle_count.py b/python/cugraph/tests/test_triangle_count.py index ff28f55838d..917a4f320a7 100644 --- a/python/cugraph/tests/test_triangle_count.py +++ b/python/cugraph/tests/test_triangle_count.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -19,6 +19,7 @@ import cugraph from cugraph.tests import utils + # Temporarily suppress warnings till networkX fixes deprecation warnings # (Using or importing the ABCs from 'collections' instead of from # 'collections.abc' is deprecated, and in 3.8 it will stop working) for diff --git a/python/cugraph/tests/test_utils.py b/python/cugraph/tests/test_utils.py index 22af649ea2e..175cf389d16 100644 --- a/python/cugraph/tests/test_utils.py +++ b/python/cugraph/tests/test_utils.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -12,17 +12,21 @@ # limitations under the License. import gc +from pathlib import PurePath + import pytest import cugraph +import cudf from cugraph.tests import utils +import numpy as np def test_bfs_paths(): with pytest.raises(ValueError) as ErrorMsg: gc.collect() - graph_file = '../datasets/karate.csv' + graph_file = PurePath(utils.RAPIDS_DATASET_ROOT_DIR)/"karate.csv" cu_M = utils.read_csv_file(graph_file) @@ -47,7 +51,7 @@ def test_bfs_paths_array(): with pytest.raises(ValueError) as ErrorMsg: gc.collect() - graph_file = '../datasets/karate.csv' + graph_file = PurePath(utils.RAPIDS_DATASET_ROOT_DIR)/"karate.csv" cu_M = utils.read_csv_file(graph_file) @@ -66,3 +70,30 @@ def test_bfs_paths_array(): answer = cugraph.utils.get_traversed_path_list(df, 100) assert "not in the result set" in str(ErrorMsg) + + +@pytest.mark.parametrize("graph_file", utils.DATASETS) +@pytest.mark.skip(reason="Skipping large tests") +def test_get_traversed_cost(graph_file): + cu_M = utils.read_csv_file(graph_file) + + noise = cudf.Series(np.random.randint(10, size=(cu_M.shape[0]))) + cu_M['info'] = cu_M['2'] + noise + + G = cugraph.Graph() + G.from_cudf_edgelist(cu_M, source='0', destination='1', edge_attr='info') + + # run SSSP starting at vertex 17 + df = cugraph.sssp(G, 16) + + answer = cugraph.utilities.path_retrieval.get_traversed_cost(df, 16, + cu_M['0'], + cu_M['1'], + cu_M['info'] + ) + + df = df.sort_values(by='vertex').reset_index() + answer = answer.sort_values(by='vertex').reset_index() + + assert df.shape[0] == answer.shape[0] + assert np.allclose(df['distance'], answer['info']) diff --git a/python/cugraph/tests/test_wjaccard.py b/python/cugraph/tests/test_wjaccard.py index c5cab18484c..f3b3fb9efd6 100644 --- a/python/cugraph/tests/test_wjaccard.py +++ b/python/cugraph/tests/test_wjaccard.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -21,6 +21,7 @@ import cugraph from cugraph.tests import utils + # Temporarily suppress warnings till networkX fixes deprecation warnings # (Using or importing the ABCs from 'collections' instead of from # 'collections.abc' is deprecated, and in 3.8 it will stop working) for @@ -37,16 +38,19 @@ def cugraph_call(cu_M): # Device data - weights_arr = cudf.Series( + weight_arr = cudf.Series( np.ones(max(cu_M["0"].max(), cu_M["1"].max()) + 1, dtype=np.float32) ) + weights = cudf.DataFrame() + weights['vertex'] = np.arange(len(weight_arr), dtype=np.int32) + weights['weight'] = weight_arr G = cugraph.Graph() G.from_cudf_edgelist(cu_M, source="0", destination="1") # cugraph Jaccard Call t1 = time.time() - df = cugraph.jaccard_w(G, weights_arr) + df = cugraph.jaccard_w(G, weights) t2 = time.time() - t1 print("Time : " + str(t2)) @@ -99,3 +103,56 @@ def test_wjaccard(graph_file): for i in range(len(cu_coeff)): diff = abs(nx_coeff[i] - cu_coeff[i]) assert diff < 1.0e-6 + + +@pytest.mark.parametrize("graph_file", utils.DATASETS_UNDIRECTED) +def test_wjaccard_multi_column_weights(graph_file): + gc.collect() + + M = utils.read_csv_for_nx(graph_file) + cu_M = utils.read_csv_file(graph_file) + # suppress F841 (local variable is assigned but never used) in flake8 + # no networkX equivalent to compare cu_coeff against... + cu_coeff = cugraph_call(cu_M) # noqa: F841 + nx_coeff = networkx_call(M) + for i in range(len(cu_coeff)): + diff = abs(nx_coeff[i] - cu_coeff[i]) + assert diff < 1.0e-6 + + +@pytest.mark.parametrize("graph_file", utils.DATASETS_UNDIRECTED) +def test_wjaccard_multi_column(graph_file): + gc.collect() + + M = utils.read_csv_for_nx(graph_file) + + cu_M = cudf.DataFrame() + cu_M["src_0"] = cudf.Series(M["0"]) + cu_M["dst_0"] = cudf.Series(M["1"]) + cu_M["src_1"] = cu_M["src_0"] + 1000 + cu_M["dst_1"] = cu_M["dst_0"] + 1000 + G1 = cugraph.Graph() + G1.from_cudf_edgelist(cu_M, source=["src_0", "src_1"], + destination=["dst_0", "dst_1"]) + + G2 = cugraph.Graph() + G2.from_cudf_edgelist(cu_M, source="src_0", + destination="dst_0") + + vertex_pair = cu_M[["src_0", "src_1", "dst_0", "dst_1"]] + vertex_pair = vertex_pair[:5] + + weight_arr = cudf.Series(np.ones(G2.number_of_vertices(), + dtype=np.float32)) + weights = cudf.DataFrame() + weights['vertex'] = G2.nodes() + weights['vertex_'] = weights['vertex'] + 1000 + weights['weight'] = weight_arr + + df_res = cugraph.jaccard_w(G1, weights, vertex_pair) + + weights = weights[['vertex', 'weight']] + df_exp = cugraph.jaccard_w(G2, weights, vertex_pair[["src_0", "dst_0"]]) + + # Calculating mismatch + assert df_res["jaccard_coeff"].equals(df_exp["jaccard_coeff"]) diff --git a/python/cugraph/tests/test_woverlap.py b/python/cugraph/tests/test_woverlap.py index e7da21014ba..66032ac3f48 100644 --- a/python/cugraph/tests/test_woverlap.py +++ b/python/cugraph/tests/test_woverlap.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -16,10 +16,11 @@ import pytest import scipy +import numpy as np import cudf + import cugraph from cugraph.tests import utils -import numpy as np def cugraph_call(cu_M, pairs): @@ -27,13 +28,16 @@ def cugraph_call(cu_M, pairs): weights_arr = cudf.Series( np.ones(max(cu_M["0"].max(), cu_M["1"].max()) + 1, dtype=np.float32) ) + weights = cudf.DataFrame() + weights['vertex'] = np.arange(len(weights_arr), dtype=np.int32) + weights['weight'] = weights_arr G = cugraph.DiGraph() G.from_cudf_edgelist(cu_M, source="0", destination="1") # cugraph Overlap Call t1 = time.time() - df = cugraph.overlap_w(G, weights_arr, pairs) + df = cugraph.overlap_w(G, weights, pairs) t2 = time.time() - t1 print("Time : " + str(t2)) df = df.sort_values(by=["source", "destination"]) @@ -83,7 +87,6 @@ def cpu_call(M, first, second): return result -# Test @pytest.mark.parametrize("graph_file", utils.DATASETS_UNDIRECTED) def test_woverlap(graph_file): gc.collect() @@ -114,3 +117,42 @@ def test_woverlap(graph_file): else: diff = abs(cpu_coeff[i] - cu_coeff[i]) assert diff < 1.0e-6 + + +@pytest.mark.parametrize("graph_file", utils.DATASETS_UNDIRECTED) +def test_woverlap_multi_column(graph_file): + gc.collect() + + M = utils.read_csv_for_nx(graph_file) + + cu_M = cudf.DataFrame() + cu_M["src_0"] = cudf.Series(M["0"]) + cu_M["dst_0"] = cudf.Series(M["1"]) + cu_M["src_1"] = cu_M["src_0"] + 1000 + cu_M["dst_1"] = cu_M["dst_0"] + 1000 + G1 = cugraph.Graph() + G1.from_cudf_edgelist(cu_M, source=["src_0", "src_1"], + destination=["dst_0", "dst_1"]) + + G2 = cugraph.Graph() + G2.from_cudf_edgelist(cu_M, source="src_0", + destination="dst_0") + + vertex_pair = cu_M[["src_0", "src_1", "dst_0", "dst_1"]] + vertex_pair = vertex_pair[:5] + + weight_arr = cudf.Series(np.ones(G2.number_of_vertices(), + dtype=np.float32)) + + weights = cudf.DataFrame() + weights['vertex'] = G2.nodes() + weights['vertex_'] = weights['vertex'] + 1000 + weights['weight'] = weight_arr + + df_res = cugraph.overlap_w(G1, weights, vertex_pair) + + weights = weights[['vertex', 'weight']] + df_exp = cugraph.overlap_w(G2, weights, vertex_pair[["src_0", "dst_0"]]) + + # Calculating mismatch + assert df_res["overlap_coeff"].equals(df_exp["overlap_coeff"]) diff --git a/python/cugraph/tests/utils.py b/python/cugraph/tests/utils.py old mode 100644 new mode 100755 index 164c6efb084..56e90b1f6bb --- a/python/cugraph/tests/utils.py +++ b/python/cugraph/tests/utils.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -26,7 +26,7 @@ from scipy.sparse.coo import coo_matrix as sp_coo_matrix from scipy.sparse.csr import csr_matrix as sp_csr_matrix from scipy.sparse.csc import csc_matrix as sp_csc_matrix - +from pathlib import PurePath import cudf import dask_cudf @@ -40,37 +40,74 @@ # # Datasets # -DATASETS_UNDIRECTED = ["../datasets/karate.csv", "../datasets/dolphins.csv"] + + +RAPIDS_DATASET_ROOT_DIR = os.getenv("RAPIDS_DATASET_ROOT_DIR", "../datasets") + +DATASETS_UNDIRECTED = [PurePath(RAPIDS_DATASET_ROOT_DIR)/f for + f in ["karate.csv", "dolphins.csv"]] DATASETS_UNDIRECTED_WEIGHTS = [ - "../datasets/netscience.csv", + PurePath(RAPIDS_DATASET_ROOT_DIR)/"netscience.csv" ] -DATASETS_UNRENUMBERED = ["../datasets/karate-disjoint.csv"] +DATASETS_UNRENUMBERED = [PurePath( + RAPIDS_DATASET_ROOT_DIR)/"karate-disjoint.csv" +] + +DATASETS = [PurePath(RAPIDS_DATASET_ROOT_DIR)/f for f in [ + "karate-disjoint.csv", + "dolphins.csv", + "netscience.csv"] +] + +DATASETS_MULTI_EDGES = [PurePath(RAPIDS_DATASET_ROOT_DIR)/f for f in [ + "karate_multi_edge.csv", + "dolphins_multi_edge.csv"] +] -DATASETS = [ - "../datasets/karate-disjoint.csv", - "../datasets/dolphins.csv", - "../datasets/netscience.csv", +DATASETS_STR_ISLT_V = [PurePath(RAPIDS_DATASET_ROOT_DIR)/f for f in [ + "karate_mod.mtx", + "karate_str.mtx"] ] + +DATASETS_SELF_LOOPS = [PurePath(RAPIDS_DATASET_ROOT_DIR)/f for f in [ + "karate_s_loop.csv", + "dolphins_s_loop.csv"] +] + + # '../datasets/email-Eu-core.csv'] STRONGDATASETS = [ - "../datasets/dolphins.csv", - "../datasets/netscience.csv", - "../datasets/email-Eu-core.csv", + PurePath(RAPIDS_DATASET_ROOT_DIR)/f for f in [ + "dolphins.csv", + "netscience.csv", + "email-Eu-core.csv"] ] -DATASETS_KTRUSS = [ - ("../datasets/polbooks.csv", "../datasets/ref/ktruss/polbooks.csv") + +DATASETS_KTRUSS = [( + PurePath(RAPIDS_DATASET_ROOT_DIR)/"polbooks.csv", + PurePath(RAPIDS_DATASET_ROOT_DIR)/"ref/ktruss/polbooks.csv") +] + +DATASETS_TSPLIB = [ + (PurePath(RAPIDS_DATASET_ROOT_DIR)/f,) + (d,) for (f, d) in [ + ("gil262.tsp", 2378), + ("eil51.tsp", 426), + ("kroA100.tsp", 21282), + ("tsp225.tsp", 3916)] ] DATASETS_SMALL = [ - "../datasets/karate.csv", - "../datasets/dolphins.csv", - "../datasets/polbooks.csv", + PurePath(RAPIDS_DATASET_ROOT_DIR)/f for f in [ + "karate.csv", + "dolphins.csv", + "polbooks.csv"] ] + MATRIX_INPUT_TYPES = [ pytest.param( cp_coo_matrix, marks=pytest.mark.matrix_types, id="CuPy.coo_matrix" diff --git a/python/cugraph/traversal/__init__.py b/python/cugraph/traversal/__init__.py index 58e37a7add0..e74266d29fc 100644 --- a/python/cugraph/traversal/__init__.py +++ b/python/cugraph/traversal/__init__.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -17,5 +17,8 @@ sssp, shortest_path, filter_unreachable, - shortest_path_length -) \ No newline at end of file + shortest_path_length, +) +from cugraph.traversal.traveling_salesperson import traveling_salesperson + +from cugraph.traversal.ms_bfs import concurrent_bfs, multi_source_bfs diff --git a/python/cugraph/traversal/bfs.pxd b/python/cugraph/traversal/bfs.pxd index 5b73d23045c..ac825deffa6 100644 --- a/python/cugraph/traversal/bfs.pxd +++ b/python/cugraph/traversal/bfs.pxd @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -16,17 +16,19 @@ # cython: embedsignature = True # cython: language_level = 3 -from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * from libcpp cimport bool +cdef extern from "limits.h": + cdef int INT_MAX -cdef extern from "utilities/cython.hpp" namespace "cugraph::cython": +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": cdef void call_bfs[vertex_t, weight_t]( const handle_t &handle, const graph_container_t &g, vertex_t *identifiers, vertex_t *distances, vertex_t *predecessors, - double *sp_counters, + vertex_t depth_limit, const vertex_t start_vertex, - bool directed) except + + bool direction_optimizing) except + diff --git a/python/cugraph/traversal/bfs.py b/python/cugraph/traversal/bfs.py index efbae095676..d397b5a4241 100644 --- a/python/cugraph/traversal/bfs.py +++ b/python/cugraph/traversal/bfs.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -14,7 +14,7 @@ import cudf from cugraph.traversal import bfs_wrapper -from cugraph.structure.graph import Graph, DiGraph +from cugraph.structure.graph_classes import Graph, DiGraph from cugraph.utilities import (ensure_cugraph_obj, is_matrix_type, is_cp_matrix_type, @@ -41,7 +41,7 @@ import_from="scipy.sparse.csc") -def _ensure_args(G, start, return_sp_counter, i_start, directed): +def _ensure_args(G, start, i_start, directed): """ Ensures the args passed in are usable for the API api_name and returns the args with proper defaults if not specified, or raises TypeError or @@ -52,9 +52,6 @@ def _ensure_args(G, start, return_sp_counter, i_start, directed): raise TypeError("cannot specify both 'start' and 'i_start'") if (start is None) and (i_start is None): raise TypeError("must specify 'start' or 'i_start', but not both") - if (return_sp_counter is not None) and \ - (return_sp_counter not in [True, False]): - raise ValueError("'return_sp_counter' must be a bool") G_type = type(G) # Check for Graph-type inputs @@ -67,10 +64,8 @@ def _ensure_args(G, start, return_sp_counter, i_start, directed): start = start if start is not None else i_start if directed is None: directed = True - if return_sp_counter is None: - return_sp_counter = False - return (start, return_sp_counter, directed) + return (start, directed) def _convert_df_to_output_type(df, input_type): @@ -92,30 +87,23 @@ def _convert_df_to_output_type(df, input_type): if is_cp_matrix_type(input_type): distances = cp.fromDlpack(sorted_df["distance"].to_dlpack()) preds = cp.fromDlpack(sorted_df["predecessor"].to_dlpack()) - if "sp_counter" in df.columns: - return (distances, preds, - cp.fromDlpack(sorted_df["sp_counter"].to_dlpack())) - else: - return (distances, preds) + return (distances, preds) else: distances = sorted_df["distance"].to_array() preds = sorted_df["predecessor"].to_array() - if "sp_counter" in df.columns: - return (distances, preds, - sorted_df["sp_counter"].to_array()) - else: - return (distances, preds) + return (distances, preds) else: raise TypeError(f"input type {input_type} is not a supported type.") def bfs(G, start=None, - return_sp_counter=None, + depth_limit=None, i_start=None, directed=None, return_predecessors=None): - """Find the distances and predecessors for a breadth first traversal of a + """ + Find the distances and predecessors for a breadth first traversal of a graph. Parameters @@ -128,16 +116,18 @@ def bfs(G, start : Integer The index of the graph vertex from which the traversal begins - return_sp_counter : bool, optional, default=False - Indicates if shortest path counters should be returned - i_start : Integer, optional Identical to start, added for API compatibility. Only start or i_start can be set, not both. + depth_limit : Integer or None + Limit the depth of the search + directed : bool, optional - NOTE: For non-Graph-type (eg. sparse matrix) values of G only. Raises - TypeError if used with a Graph object. + NOTE + For non-Graph-type (eg. sparse matrix) values of G only. Raises + TypeError if used with a Graph object. + If True (default), then convert the input matrix to a cugraph.DiGraph, otherwise a cugraph.Graph object will be used. @@ -154,10 +144,6 @@ def bfs(G, df['predecessor'] for each i'th position in the column, the vertex ID immediately preceding the vertex at position i in the 'vertex' column - df['sp_counter'] for each i'th position in the column, the number of - shortest paths leading to the vertex at position i in the 'vertex' - column (Only if retrun_sp_counter is True) - If G is a networkx.Graph, returns: pandas.DataFrame with contents equivalent to the cudf.DataFrame @@ -189,34 +175,30 @@ def bfs(G, >>> df = cugraph.bfs(G, 0) """ - (start, return_sp_counter, directed) = \ - _ensure_args(G, start, return_sp_counter, i_start, directed) + (start, directed) = \ + _ensure_args(G, start, i_start, directed) # FIXME: allow nx_weight_attr to be specified (G, input_type) = ensure_cugraph_obj( G, nx_weight_attr="weight", matrix_graph_type=DiGraph if directed else Graph) - if type(G) is Graph: - is_directed = False - else: - is_directed = True - if G.renumbered is True: - start = G.lookup_internal_vertex_id(cudf.Series([start]))[0] - - df = bfs_wrapper.bfs(G, start, is_directed, return_sp_counter) + if isinstance(start, cudf.DataFrame): + start = G.lookup_internal_vertex_id(start, start.columns).iloc[0] + else: + start = G.lookup_internal_vertex_id(cudf.Series([start]))[0] + df = bfs_wrapper.bfs(G, start, depth_limit) if G.renumbered: df = G.unrenumber(df, "vertex") df = G.unrenumber(df, "predecessor") - df["predecessor"].fillna(-1, inplace=True) + df.fillna(-1, inplace=True) return _convert_df_to_output_type(df, input_type) -def bfs_edges(G, source, reverse=False, depth_limit=None, sort_neighbors=None, - return_sp_counter=False): +def bfs_edges(G, source, reverse=False, depth_limit=None, sort_neighbors=None): """ Find the distances and predecessors for a breadth first traversal of a graph. @@ -237,14 +219,10 @@ def bfs_edges(G, source, reverse=False, depth_limit=None, sort_neighbors=None, depth_limit : Int or None Limit the depth of the search - Currently not implemented sort_neighbors : None or Function Currently not implemented - return_sp_counter : bool, optional, default=False - Indicates if shortest path counters should be returned - Returns ------- Return value type is based on the input type. If G is a cugraph.Graph, @@ -258,10 +236,6 @@ def bfs_edges(G, source, reverse=False, depth_limit=None, sort_neighbors=None, df['predecessor'] for each i'th position in the column, the vertex ID immediately preceding the vertex at position i in the 'vertex' column - df['sp_counter'] for each i'th position in the column, the number of - shortest paths leading to the vertex at position i in the 'vertex' - column (Only if retrun_sp_counter is True) - If G is a networkx.Graph, returns: pandas.DataFrame with contents equivalent to the cudf.DataFrame @@ -298,9 +272,4 @@ def bfs_edges(G, source, reverse=False, depth_limit=None, sort_neighbors=None, "reverse processing of graph is currently not supported" ) - if depth_limit is not None: - raise NotImplementedError( - "depth limit implementation of BFS is not currently supported" - ) - - return bfs(G, source, return_sp_counter) + return bfs(G, source, depth_limit) diff --git a/python/cugraph/traversal/bfs_wrapper.pyx b/python/cugraph/traversal/bfs_wrapper.pyx index ae346aea953..1896415b1e3 100644 --- a/python/cugraph/traversal/bfs_wrapper.pyx +++ b/python/cugraph/traversal/bfs_wrapper.pyx @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -17,64 +17,51 @@ # cython: language_level = 3 cimport cugraph.traversal.bfs as c_bfs -from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * from cugraph.structure import graph_primtypes_wrapper from libcpp cimport bool from libc.stdint cimport uintptr_t -from libc.float cimport FLT_MAX_EXP - import cudf -import rmm import numpy as np -def bfs(input_graph, start, directed=True, - return_sp_counter=False): +def bfs(input_graph, start, depth_limit, direction_optimizing=False): """ Call bfs """ # Step 1: Declare the different varibales cdef graph_container_t graph_container - # FIXME: Offsets and indices are currently hardcoded to int, but this may - # not be acceptable in the future. + numberTypeMap = {np.dtype("int32") : numberTypeEnum.int32Type, np.dtype("int64") : numberTypeEnum.int64Type, np.dtype("float32") : numberTypeEnum.floatType, np.dtype("double") : numberTypeEnum.doubleType} - # Pointers required for CSR Graph - cdef uintptr_t c_offsets_ptr = NULL # Pointer to the CSR offsets - cdef uintptr_t c_indices_ptr = NULL # Pointer to the CSR indices - cdef uintptr_t c_weights = NULL - cdef uintptr_t c_local_verts = NULL; - cdef uintptr_t c_local_edges = NULL; - cdef uintptr_t c_local_offsets = NULL; weight_t = np.dtype("float32") + [src, dst] = graph_primtypes_wrapper.datatype_cast([input_graph.edgelist.edgelist_df['src'], input_graph.edgelist.edgelist_df['dst']], [np.int32]) + weights = None # Pointers for SSSP / BFS cdef uintptr_t c_identifier_ptr = NULL # Pointer to the DataFrame 'vertex' Series cdef uintptr_t c_distance_ptr = NULL # Pointer to the DataFrame 'distance' Series cdef uintptr_t c_predecessor_ptr = NULL # Pointer to the DataFrame 'predecessor' Series - cdef uintptr_t c_sp_counter_ptr = NULL # Pointer to the DataFrame 'sp_counter' Series + if depth_limit is None: + depth_limit = c_bfs.INT_MAX # Step 2: Verifiy input_graph has the expected format - if input_graph.adjlist is None: - input_graph.view_adj_list() cdef unique_ptr[handle_t] handle_ptr handle_ptr.reset(new handle_t()) handle_ = handle_ptr.get(); - # Step 3: Extract CSR offsets, indices, weights are not expected - # - offsets: int (signed, 32-bit) - # - indices: int (signed, 32-bit) - [offsets, indices] = graph_primtypes_wrapper.datatype_cast([input_graph.adjlist.offsets, input_graph.adjlist.indices], [np.int32]) - c_offsets_ptr = offsets.__cuda_array_interface__['data'][0] - c_indices_ptr = indices.__cuda_array_interface__['data'][0] - - # Step 4: Setup number of vertices and edges + # Step 3: Setup number of vertices and edges num_verts = input_graph.number_of_vertices() num_edges = input_graph.number_of_edges(directed_edges=True) + # Step 4: Extract COO + cdef uintptr_t c_src_vertices = src.__cuda_array_interface__['data'][0] + cdef uintptr_t c_dst_vertices = dst.__cuda_array_interface__['data'][0] + cdef uintptr_t c_edge_weights = NULL + # Step 5: Check if source index is valid if not 0 <= start < num_verts: raise ValueError("Starting vertex should be between 0 to number of vertices") @@ -82,30 +69,33 @@ def bfs(input_graph, start, directed=True, # Step 6: Generate the cudf.DataFrame result # Current implementation expects int (signed 32-bit) for distance df = cudf.DataFrame() - df['vertex'] = cudf.Series(np.zeros(num_verts, dtype=np.int32)) + df['vertex'] = cudf.Series(np.arange(num_verts), dtype=np.int32) df['distance'] = cudf.Series(np.zeros(num_verts, dtype=np.int32)) df['predecessor'] = cudf.Series(np.zeros(num_verts, dtype=np.int32)) - if (return_sp_counter): - df['sp_counter'] = cudf.Series(np.zeros(num_verts, dtype=np.double)) # Step 7: Associate to cudf Series c_identifier_ptr = df['vertex'].__cuda_array_interface__['data'][0] c_distance_ptr = df['distance'].__cuda_array_interface__['data'][0] c_predecessor_ptr = df['predecessor'].__cuda_array_interface__['data'][0] - if return_sp_counter: - c_sp_counter_ptr = df['sp_counter'].__cuda_array_interface__['data'][0] + + is_symmetric = not input_graph.is_directed() # Step 8: Proceed to BFS - # FIXME: [int, int, float] or may add an explicit [int, int, int] in graph.cu? - populate_graph_container_legacy(graph_container, - ((graphTypeEnum.LegacyCSR)), - handle_[0], - c_offsets_ptr, c_indices_ptr, c_weights, - ((numberTypeEnum.int32Type)), - ((numberTypeEnum.int32Type)), - ((numberTypeMap[weight_t])), - num_verts, num_edges, - c_local_verts, c_local_edges, c_local_offsets) + populate_graph_container(graph_container, + handle_[0], + c_src_vertices, c_dst_vertices, c_edge_weights, + NULL, + NULL, + 0, + ((numberTypeEnum.int32Type)), + ((numberTypeEnum.int32Type)), + ((numberTypeMap[weight_t])), + num_edges, + num_verts, num_edges, + False, + is_symmetric, + False, + False) # Different pathing wether shortest_path_counting is required or not c_bfs.call_bfs[int, float](handle_ptr.get()[0], @@ -113,8 +103,8 @@ def bfs(input_graph, start, directed=True, c_identifier_ptr, c_distance_ptr, c_predecessor_ptr, - c_sp_counter_ptr, + depth_limit, start, - directed) + direction_optimizing) return df diff --git a/python/cugraph/traversal/ms_bfs.py b/python/cugraph/traversal/ms_bfs.py new file mode 100644 index 00000000000..3d158524751 --- /dev/null +++ b/python/cugraph/traversal/ms_bfs.py @@ -0,0 +1,280 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import numpy as np +import cudf + +import warnings + + +def _get_feasibility(G, sources, components=None, depth_limit=None): + """ + Evaluate the feasibility for breadth first traversal from multiple sources + in a graph. + + Parameters + ---------- + G : cugraph.Graph or cugraph.DiGraph + The adjacency list will be computed if not already present. + + sources : cudf.Series + Subset of vertices from which the traversals start. A BFS is run for + each source in the Series. + The size of the series should be at least one and cannot exceed + the size of the graph. + + depth_limit : Integer, optional, default=None + Limit the depth of the search. Terminates if no more vertices are + reachable within the distance of depth_limit + + components : cudf.DataFrame, optional, default=None + GPU Dataframe containing the component information. + Passing this information may impact the return type. + When no component information is passed BFS uses one component + behavior settings. + + components['vertex'] : cudf.Series + vertex IDs + components['color'] : cudf.Series + component IDs/color for vertices. + + Returns + ------- + mem_footprint : integer + Estimated memory foot print size in Bytes + """ + + # Fixme not implemented in RMM yet + # using 96GB upper bound for now + # mem = get_device_memory_info() + mem = 9.6e10 + n_sources = sources.size + V = G.number_of_vertices() + E = G.number_of_edges() + mean_component_sz = V + n_components = 1 + + # Retreive types + size_of_v = 4 + size_of_e = 4 + size_of_w = 0 + if G.adjlist.weights is not None: + if G.adjlist.weights.dtype is np.float64: + size_of_w = 8 + else: + size_of_w = 4 + if G.adjlist.offsets.dtype is np.float64: + size_of_v = 8 + if G.adjlist.indices.dtype is np.float64: + size_of_e = 8 + + # Graph size + G_sz = E * size_of_e + E * size_of_w + V * size_of_v + + # The impact of depth limit depends on the sparsity + # pattern and diameter. We cannot leverage it without + # traversing the full dataset a the moment. + + # dense output + output_sz = n_sources * 2 * V * size_of_v + + # sparse output + if components is not None: + tmp = components["color"].value_counts() + n_components = tmp.size + if n_sources / n_components > 100: + warnings.warn( + "High number of seeds per component result in large output." + ) + mean_component_sz = tmp.mean() + output_sz = mean_component_sz * n_sources * 2 * size_of_e + + # counting 10% for context, handle and temporary allocations + mem_footprint = (G_sz + output_sz) * 1.1 + if mem_footprint > mem: + warnings.warn(f"Cannot execute in-memory :{mem_footprint} Bytes") + + return mem_footprint + + +def concurrent_bfs(Graphs, sources, depth_limit=None, offload=False): + """ + Find the breadth first traversals of multiple graphs with multiple sources + in each graph. + + Parameters + ---------- + Graphs : list of cugraph.Graph or cugraph.DiGraph + The adjacency lists will be computed if not already present. + + sources : list of cudf.Series + For each graph, subset of vertices from which the traversals start. + A BFS is run in Graphs[i] for each source in the Series at sources[i]. + The size of this list must match the size of the graph list. + The size of each Series (ie. the number of sources per graph) + is flexible, but cannot exceed the size of the corresponding graph. + + + depth_limit : Integer, optional, default=None + Limit the depth of the search. Terminates if no more vertices are + reachable within the distance of depth_limit + + offload : boolean, optional, default=False + Indicates if output should be written to the disk. + When not provided, the algorithms decides if offloading is needed + based on the input parameters. + + Returns + ------- + Return type is decided based on the input parameters (size of + sources, size of the graph, number of graphs and offload setting) + + If G is a cugraph.Graph and output fits in memory: + BFS_edge_lists : cudf.DataFrame + GPU data frame containing all BFS edges + source_offsets: cudf.Series + Series containing the starting offset in the returned edge list + for each source. + + If offload is True, or if the output does not fit in memory : + Writes csv files containing BFS output to the disk. + """ + raise NotImplementedError( + "concurrent_bfs is coming soon! Please up vote the github issue 1465\ + to help us prioritize" + ) + if not isinstance(Graphs, list): + raise TypeError( + "Graphs should be a list of cugraph.Graph or cugraph.DiGraph" + ) + if not isinstance(sources, list): + raise TypeError("sources should be a list of cudf.Series") + if len(Graphs) != len(sources): + raise ValueError( + "The size of the sources list must match\ + the size of the graph list." + ) + if offload is True: + raise NotImplementedError( + "Offloading is coming soon! Please up vote the github issue 1461\ + to help us prioritize" + ) + + # Consolidate graphs in a single graph and record components + + # Renumber and concatenate sources in a single df + + # Call multi_source_bfs + # multi_source_bfs( + # G, + # sources, + # components=components, + # depth_limit=depth_limit, + # offload=offload, + # ) + + +def multi_source_bfs( + G, sources, components=None, depth_limit=None, offload=False +): + """ + Find the breadth first traversal from multiple sources in a graph. + + Parameters + ---------- + G : cugraph.Graph or cugraph.DiGraph + The adjacency list will be computed if not already present. + + sources : cudf.Series + Subset of vertices from which the traversals start. A BFS is run for + each source in the Series. + The size of the series should be at least one and cannot exceed the + size of the graph. + + depth_limit : Integer, optional, default=None + Limit the depth of the search. Terminates if no more vertices are + reachable within the distance of depth_limit + + components : cudf.DataFrame, optional, default=None + GPU Dataframe containing the component information. + Passing this information may impact the return type. + When no component information is passed BFS uses one component + behavior settings. + + components['vertex'] : cudf.Series + vertex IDs + components['color'] : cudf.Series + component IDs/color for vertices. + + offload : boolean, optional, default=False + Indicates if output should be written to the disk. + When not provided, the algorithms decides if offloading is needed + based on the input parameters. + + Returns + ------- + Return value type is decided based on the input parameters (size of + sources, size of the graph, number of components and offload setting) + If G is a cugraph.Graph, returns : + cudf.DataFrame + df['vertex'] vertex IDs + + df['distance_'] path distance for each vertex from the + starting vertex. One column per source. + + df['predecessor_'] for each i'th position in the column, + the vertex ID immediately preceding the vertex at position i in + the 'vertex' column. One column per source. + + If G is a cugraph.Graph and component information is present returns : + BFS_edge_lists : cudf.DataFrame + GPU data frame containing all BFS edges + source_offsets: cudf.Series + Series containing the starting offset in the returned edge list + for each source. + + If offload is True, or if the output does not fit in memory : + Writes csv files containing BFS output to the disk. + """ + raise NotImplementedError( + "concurrent_bfs is coming soon! Please up vote the github issue 1465\ + to help us prioritize" + ) + # if components is not None: + # null_check(components["vertex"]) + # null_check(components["colors"]) + # + # if depth_limit is not None: + # raise NotImplementedError( + # "depth limit implementation of BFS is not currently supported" + # ) + + # if offload is True: + # raise NotImplementedError( + # "Offloading is coming soon! Please up vote the github issue 1461 + # to help us prioritize" + # ) + if isinstance(sources, list): + sources = cudf.Series(sources) + if G.renumbered is True: + sources = G.lookup_internal_vertex_id(cudf.Series(sources)) + if not G.adjlist: + G.view_adj_list() + # Memory footprint check + footprint = _get_feasibility( + G, sources, components=components, depth_limit=depth_limit + ) + print(footprint) + # Call multi_source_bfs + # FIXME remove when implemented + # raise NotImplementedError("Commming soon") diff --git a/python/cugraph/traversal/sssp.pxd b/python/cugraph/traversal/sssp.pxd index e4b709cb879..3109668d747 100644 --- a/python/cugraph/traversal/sssp.pxd +++ b/python/cugraph/traversal/sssp.pxd @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -16,9 +16,9 @@ # cython: embedsignature = True # cython: language_level = 3 -from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * -cdef extern from "utilities/cython.hpp" namespace "cugraph::cython": +cdef extern from "cugraph/utilities/cython.hpp" namespace "cugraph::cython": cdef void call_sssp[vertex_t, weight_t]( const handle_t &handle, diff --git a/python/cugraph/traversal/sssp.py b/python/cugraph/traversal/sssp.py index 4ba754ad4ed..f3aebaf43bf 100644 --- a/python/cugraph/traversal/sssp.py +++ b/python/cugraph/traversal/sssp.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -14,7 +14,7 @@ import numpy as np import cudf -from cugraph.structure import Graph, DiGraph +from cugraph.structure import Graph, DiGraph, MultiGraph, MultiDiGraph from cugraph.traversal import sssp_wrapper from cugraph.utilities import (ensure_cugraph_obj, is_matrix_type, @@ -104,7 +104,7 @@ def _convert_df_to_output_type(df, input_type, return_predecessors): return_predecessors is only used for return values from cupy/scipy input types. """ - if input_type in [Graph, DiGraph]: + if input_type in [Graph, DiGraph, MultiGraph, MultiDiGraph]: return df elif (nx is not None) and (input_type in [nx.Graph, nx.DiGraph]): @@ -212,7 +212,11 @@ def sssp(G, matrix_graph_type=DiGraph if directed else Graph) if G.renumbered: - source = G.lookup_internal_vertex_id(cudf.Series([source]))[0] + if isinstance(source, cudf.DataFrame): + source = G.lookup_internal_vertex_id( + source, source.columns).iloc[0] + else: + source = G.lookup_internal_vertex_id(cudf.Series([source]))[0] if source is cudf.NA: raise ValueError( @@ -223,7 +227,7 @@ def sssp(G, if G.renumbered: df = G.unrenumber(df, "vertex") df = G.unrenumber(df, "predecessor") - df["predecessor"].fillna(-1, inplace=True) + df.fillna(-1, inplace=True) return _convert_df_to_output_type(df, input_type, return_predecessors) diff --git a/python/cugraph/traversal/sssp_wrapper.pyx b/python/cugraph/traversal/sssp_wrapper.pyx index 730fe0db94e..46966cd3e99 100644 --- a/python/cugraph/traversal/sssp_wrapper.pyx +++ b/python/cugraph/traversal/sssp_wrapper.pyx @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -18,17 +18,14 @@ cimport cugraph.traversal.sssp as c_sssp cimport cugraph.traversal.bfs as c_bfs -from cugraph.structure.graph_primtypes cimport * +from cugraph.structure.graph_utilities cimport * from cugraph.structure import graph_primtypes_wrapper - from libcpp cimport bool from libc.stdint cimport uintptr_t -from libc.float cimport FLT_MAX_EXP - import cudf -import rmm import numpy as np + def sssp(input_graph, source): """ Call sssp @@ -49,7 +46,7 @@ def sssp(input_graph, source): cdef uintptr_t c_local_verts = NULL; cdef uintptr_t c_local_edges = NULL; cdef uintptr_t c_local_offsets = NULL; - weight_t = np.dtype("int32") + weight_t = np.dtype("float32") # Pointers for SSSP / BFS cdef uintptr_t c_identifier_ptr = NULL # Pointer to the DataFrame 'vertex' Series @@ -113,31 +110,21 @@ def sssp(input_graph, source): num_verts, num_edges, c_local_verts, c_local_edges, c_local_offsets) - if weights is not None: - if weight_t == np.float32: - c_sssp.call_sssp[int, float](handle_[0], - graph_container, - c_identifier_ptr, - c_distance_ptr, - c_predecessor_ptr, - source) - elif weight_t == np.float64: - c_sssp.call_sssp[int, double](handle_[0], - graph_container, - c_identifier_ptr, - c_distance_ptr, - c_predecessor_ptr, - source) - else: # This case should not happen - raise NotImplementedError - else: - c_bfs.call_bfs[int, float](handle_[0], - graph_container, - c_identifier_ptr, - c_distance_ptr, - c_predecessor_ptr, - NULL, - source, - 1) + if weight_t == np.float32: + c_sssp.call_sssp[int, float](handle_[0], + graph_container, + c_identifier_ptr, + c_distance_ptr, + c_predecessor_ptr, + source) + elif weight_t == np.float64: + c_sssp.call_sssp[int, double](handle_[0], + graph_container, + c_identifier_ptr, + c_distance_ptr, + c_predecessor_ptr, + source) + else: # This case should not happen + raise NotImplementedError return df diff --git a/python/cugraph/traversal/traveling_salesperson.pxd b/python/cugraph/traversal/traveling_salesperson.pxd new file mode 100644 index 00000000000..b38c18c7633 --- /dev/null +++ b/python/cugraph/traversal/traveling_salesperson.pxd @@ -0,0 +1,33 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# cython: profile=False +# distutils: language = c++ +# cython: embedsignature = True +# cython: language_level = 3 + +from cugraph.structure.graph_primtypes cimport * + +cdef extern from "cugraph/algorithms.hpp" namespace "cugraph": + + cdef float traveling_salesperson(const handle_t &handle, + int *vtx_ptr, + float *x_pos, + float *y_pos, + int nodes, + int restarts, + bool beam_search, + int k, + int nstart, + bool verbose, + int *route) except + diff --git a/python/cugraph/traversal/traveling_salesperson.py b/python/cugraph/traversal/traveling_salesperson.py new file mode 100644 index 00000000000..53d411c92ae --- /dev/null +++ b/python/cugraph/traversal/traveling_salesperson.py @@ -0,0 +1,76 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from cugraph.traversal import traveling_salesperson_wrapper +from cugraph.structure.graph_classes import null_check +import cudf + + +def traveling_salesperson(pos_list, + restarts=100000, + beam_search=True, + k=4, + nstart=None, + verbose=False, + ): + """ + Finds an approximate solution to the traveling salesperson problem (TSP). + cuGraph computes an approximation of the TSP problem using hill climbing + optimization. + + The current implementation does not support a weighted graph. + + Parameters + ---------- + pos_list: cudf.DataFrame + Data frame with initial vertex positions containing three columns: + 'vertex' ids and 'x', 'y' positions. + restarts: int + Number of starts to try. The more restarts, the better the solution + will be approximated. The number of restarts depends on the problem + size and should be kept low for instances above 2k cities. + beam_search: bool + Specify if the initial solution should use KNN for an approximation + solution. + k: int + Beam width to use in the search. + nstart: int + Vertex id to use as starting position. + verbose: bool + Logs configuration and iterative improvement. + + Returns + ------- + route : cudf.Series + cudf.Series of size V containing the ordered list of vertices + than needs to be visited. + """ + + if not isinstance(pos_list, cudf.DataFrame): + raise TypeError("Instance should be cudf.DataFrame") + + null_check(pos_list['vertex']) + null_check(pos_list['x']) + null_check(pos_list['y']) + + if nstart is not None and not pos_list[pos_list['vertex'] == nstart].index: + raise ValueError("nstart should be in vertex ids") + + route, cost = traveling_salesperson_wrapper.traveling_salesperson( + pos_list, + restarts, + beam_search, + k, + nstart, + verbose) + return route, cost diff --git a/python/cugraph/traversal/traveling_salesperson_wrapper.pyx b/python/cugraph/traversal/traveling_salesperson_wrapper.pyx new file mode 100644 index 00000000000..6eccce57a37 --- /dev/null +++ b/python/cugraph/traversal/traveling_salesperson_wrapper.pyx @@ -0,0 +1,82 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# cython: profile=False +# distutils: language = c++ +# cython: embedsignature = True +# cython: language_level = 3 + +from cugraph.traversal.traveling_salesperson cimport traveling_salesperson as c_traveling_salesperson +from cugraph.structure import graph_primtypes_wrapper +from cugraph.structure.graph_primtypes cimport * +from libcpp cimport bool +from libc.stdint cimport uintptr_t +from numba import cuda +import cudf +import numpy as np + + +def traveling_salesperson(pos_list, + restarts=100000, + beam_search=True, + k=4, + nstart=None, + verbose=False, + renumber=True, +): + """ + Call traveling_salesperson + """ + + nodes = pos_list.shape[0] + cdef uintptr_t x_pos = NULL + cdef uintptr_t y_pos = NULL + + pos_list['vertex'] = pos_list['vertex'].astype(np.int32) + pos_list['x'] = pos_list['x'].astype(np.float32) + pos_list['y'] = pos_list['y'].astype(np.float32) + x_pos = pos_list['x'].__cuda_array_interface__['data'][0] + y_pos = pos_list['y'].__cuda_array_interface__['data'][0] + + cdef unique_ptr[handle_t] handle_ptr + handle_ptr.reset(new handle_t()) + handle_ = handle_ptr.get(); + + cdef float final_cost = 0.0 + + cdef uintptr_t route_ptr = NULL + route_arr = cuda.device_array(nodes, dtype=np.int32) + route_ptr = route_arr.device_ctypes_pointer.value + + cdef uintptr_t vtx_ptr = NULL + vtx_ptr = pos_list['vertex'].__cuda_array_interface__['data'][0] + + if nstart is None: + renumbered_nstart = 0 + else: + renumbered_nstart = pos_list[pos_list['vertex'] == nstart].index[0] + + final_cost = c_traveling_salesperson(handle_[0], + vtx_ptr, + x_pos, + y_pos, + nodes, + restarts, + beam_search, + k, + renumbered_nstart, + verbose, + route_ptr) + + route = cudf.Series(route_arr) + return route, final_cost diff --git a/python/cugraph/tree/minimum_spanning_tree.pxd b/python/cugraph/tree/minimum_spanning_tree.pxd index 8cea2bee0cc..32c76ede554 100644 --- a/python/cugraph/tree/minimum_spanning_tree.pxd +++ b/python/cugraph/tree/minimum_spanning_tree.pxd @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -19,7 +19,7 @@ from cugraph.structure.graph_primtypes cimport * -cdef extern from "algorithms.hpp" namespace "cugraph": +cdef extern from "cugraph/algorithms.hpp" namespace "cugraph": cdef unique_ptr[GraphCOO[VT,ET,WT]] minimum_spanning_tree[VT,ET,WT](const handle_t &handle, const GraphCSRView[VT,ET,WT] &graph) except + diff --git a/python/cugraph/tree/minimum_spanning_tree.py b/python/cugraph/tree/minimum_spanning_tree.py index 25a365665df..6a5f7b5bf38 100644 --- a/python/cugraph/tree/minimum_spanning_tree.py +++ b/python/cugraph/tree/minimum_spanning_tree.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -12,12 +12,12 @@ # limitations under the License. from cugraph.tree import minimum_spanning_tree_wrapper -from cugraph.structure.graph import Graph +from cugraph.structure.graph_classes import Graph from cugraph.utilities import check_nx_graph from cugraph.utilities import cugraph_to_nx -def minimum_spanning_tree_subgraph(G): +def _minimum_spanning_tree_subgraph(G): mst_subgraph = Graph() if type(G) is not Graph: raise Exception("input graph must be undirected") @@ -32,7 +32,7 @@ def minimum_spanning_tree_subgraph(G): return mst_subgraph -def maximum_spanning_tree_subgraph(G): +def _maximum_spanning_tree_subgraph(G): mst_subgraph = Graph() if type(G) is not Graph: raise Exception("input graph must be undirected") @@ -68,28 +68,33 @@ def minimum_spanning_tree( ---------- G : cuGraph.Graph or networkx.Graph cuGraph graph descriptor with connectivity information. + weight : string default to the weights in the graph, if the graph edges do not have a weight attribute a default weight of 1 will be used. + algorithm : string Default to 'boruvka'. The parallel algorithm to use when finding a minimum spanning tree. + ignore_nan : bool Default to False + Returns ------- G_mst : cuGraph.Graph or networkx.Graph A graph descriptor with a minimum spanning tree or forest. The networkx graph will not have all attributes copied over + """ G, isNx = check_nx_graph(G) if isNx is True: - mst = minimum_spanning_tree_subgraph(G) + mst = _minimum_spanning_tree_subgraph(G) return cugraph_to_nx(mst) else: - return minimum_spanning_tree_subgraph(G) + return _minimum_spanning_tree_subgraph(G) def maximum_spanning_tree( @@ -103,25 +108,30 @@ def maximum_spanning_tree( ---------- G : cuGraph.Graph or networkx.Graph cuGraph graph descriptor with connectivity information. + weight : string default to the weights in the graph, if the graph edges do not have a weight attribute a default weight of 1 will be used. + algorithm : string Default to 'boruvka'. The parallel algorithm to use when finding a maximum spanning tree. + ignore_nan : bool Default to False + Returns ------- G_mst : cuGraph.Graph or networkx.Graph A graph descriptor with a maximum spanning tree or forest. The networkx graph will not have all attributes copied over + """ G, isNx = check_nx_graph(G) if isNx is True: - mst = maximum_spanning_tree_subgraph(G) + mst = _maximum_spanning_tree_subgraph(G) return cugraph_to_nx(mst) else: - return maximum_spanning_tree_subgraph(G) + return _maximum_spanning_tree_subgraph(G) diff --git a/python/cugraph/utilities/__init__.py b/python/cugraph/utilities/__init__.py index 61f5596eee6..6dc23ff53b7 100644 --- a/python/cugraph/utilities/__init__.py +++ b/python/cugraph/utilities/__init__.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2020, NVIDIA CORPORATION. +# Copyright (c) 2019-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. # You may obtain a copy of the License at @@ -24,4 +24,6 @@ is_matrix_type, is_cp_matrix_type, is_sp_matrix_type, + renumber_vertex_pair ) +from cugraph.utilities.path_retrieval import get_traversed_cost diff --git a/python/cugraph/utilities/path_retrieval.pxd b/python/cugraph/utilities/path_retrieval.pxd new file mode 100644 index 00000000000..dcbbef5127d --- /dev/null +++ b/python/cugraph/utilities/path_retrieval.pxd @@ -0,0 +1,29 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# cython: profile=False +# distutils: language = c++ +# cython: embedsignature = True +# cython: language_level = 3 + +from cugraph.structure.graph_primtypes cimport * + +cdef extern from "cugraph/utilities/path_retrieval.hpp" namespace "cugraph": + + cdef void get_traversed_cost[vertex_t, weight_t](const handle_t &handle, + const vertex_t *vertices, + const vertex_t *preds, + const weight_t *info_weights, + weight_t *out, + vertex_t stop_vertex, + vertex_t num_vertices) except + diff --git a/python/cugraph/utilities/path_retrieval.py b/python/cugraph/utilities/path_retrieval.py new file mode 100644 index 00000000000..b9baadc2f21 --- /dev/null +++ b/python/cugraph/utilities/path_retrieval.py @@ -0,0 +1,100 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import numpy as np +import cudf + +from cugraph.structure.symmetrize import symmetrize +from cugraph.structure.number_map import NumberMap +from cugraph.utilities import path_retrieval_wrapper + + +def get_traversed_cost(df, source, source_col, dest_col, value_col): + """ + Take the DataFrame result from a BFS or SSSP function call and sums + the given weights along the path to the starting vertex. + The source_col, dest_col identifiers need to match with the vertex and + predecessor columns of df. + + Input Parameters + ---------- + df : cudf.DataFrame + The dataframe containing the results of a BFS or SSSP call + source: int + Index of the source vertex. + source_col : cudf.DataFrame + This cudf.Series wraps a gdf_column of size E (E: number of edges). + The gdf column contains the source index for each edge. + Source indices must be an integer type. + dest_col : cudf.Series + This cudf.Series wraps a gdf_column of size E (E: number of edges). + The gdf column contains the destination index for each edge. + Destination indices must be an integer type. + value_col : cudf.Series + This cudf.Series wraps a gdf_column of size E (E: number of edges). + The gdf column contains values associated with this edge. + Weight should be a floating type. + + Returns + --------- + df : cudf.DataFrame + DataFrame containing two columns 'vertex' and 'info'. + Unreachable vertices will have value the max value of the weight type. + """ + + if 'vertex' not in df.columns: + raise ValueError("DataFrame does not appear to be a BFS or " + "SSP result - 'vertex' column missing") + if 'distance' not in df.columns: + raise ValueError("DataFrame does not appear to be a BFS or " + "SSP result - 'distance' column missing") + if 'predecessor' not in df.columns: + raise ValueError("DataFrame does not appear to be a BFS or " + "SSP result - 'predecessor' column missing") + + src, dst, val = symmetrize(source_col, + dest_col, + value_col) + + symmetrized_df = cudf.DataFrame() + symmetrized_df['source'] = src + symmetrized_df['destination'] = dst + symmetrized_df['weights'] = val + + input_df = df.merge(symmetrized_df, + left_on=['vertex', 'predecessor'], + right_on=['source', 'destination'], + how="left" + ) + + # Set unreachable vertex weights to max float and source vertex weight to 0 + max_val = np.finfo(val.dtype).max + input_df[['weights']] = input_df[['weights']].fillna(max_val) + input_df.loc[input_df['vertex'] == source, 'weights'] = 0 + + # Renumber + renumbered_gdf, renumber_map = NumberMap.renumber(input_df, + ["vertex"], + ["predecessor"], + preserve_order=True) + renumbered_gdf = renumbered_gdf.rename(columns={'src': 'vertex', + 'dst': 'predecessor'}) + stop_vertex = renumber_map.to_internal_vertex_id(cudf.Series(-1)).values[0] + + out_df = path_retrieval_wrapper.get_traversed_cost(renumbered_gdf, + stop_vertex) + + # Unrenumber + out_df['vertex'] = renumber_map.unrenumber(renumbered_gdf, 'vertex', + preserve_order=True)["vertex"] + return out_df diff --git a/python/cugraph/utilities/path_retrieval_wrapper.pyx b/python/cugraph/utilities/path_retrieval_wrapper.pyx new file mode 100644 index 00000000000..98d11ad07df --- /dev/null +++ b/python/cugraph/utilities/path_retrieval_wrapper.pyx @@ -0,0 +1,72 @@ +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# cython: profile=False +# distutils: language = c++ +# cython: embedsignature = True +# cython: language_level = 3 + +from cugraph.utilities.path_retrieval cimport get_traversed_cost as c_get_traversed_cost +from cugraph.structure.graph_primtypes cimport * +from libc.stdint cimport uintptr_t +from numba import cuda +import cudf +import numpy as np + + +def get_traversed_cost(input_df, stop_vertex): + """ + Call get_traversed_cost + """ + num_verts = input_df.shape[0] + vertex_t = input_df.vertex.dtype + weight_t = input_df.weights.dtype + + df = cudf.DataFrame() + df['vertex'] = input_df['vertex'] + df['info'] = cudf.Series(np.zeros(num_verts, dtype=weight_t)) + + cdef unique_ptr[handle_t] handle_ptr + handle_ptr.reset(new handle_t()) + handle_ = handle_ptr.get(); + + cdef uintptr_t vertices = NULL + cdef uintptr_t preds = NULL + cdef uintptr_t out = NULL + cdef uintptr_t info_weights = NULL + + vertices = input_df['vertex'].__cuda_array_interface__['data'][0] + preds = input_df['predecessor'].__cuda_array_interface__['data'][0] + info_weights = input_df['weights'].__cuda_array_interface__['data'][0] + out = df['info'].__cuda_array_interface__['data'][0] + + if weight_t == np.float32: + c_get_traversed_cost(handle_[0], + vertices, + preds, + info_weights, + out, + stop_vertex, + num_verts) + elif weight_t == np.float64: + c_get_traversed_cost(handle_[0], + vertices, + preds, + info_weights, + out, + stop_vertex, + num_verts) + else: + raise NotImplementedError + + return df diff --git a/python/cugraph/utilities/utils.py b/python/cugraph/utilities/utils.py index f1a320cd1ef..e4e93513630 100644 --- a/python/cugraph/utilities/utils.py +++ b/python/cugraph/utilities/utils.py @@ -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. # You may obtain a copy of the License at @@ -14,6 +14,11 @@ from numba import cuda import cudf +from rmm._cuda.gpu import ( + getDeviceAttribute, + cudaDeviceAttr, +) + # optional dependencies try: @@ -21,6 +26,7 @@ from cupyx.scipy.sparse.coo import coo_matrix as cp_coo_matrix from cupyx.scipy.sparse.csr import csr_matrix as cp_csr_matrix from cupyx.scipy.sparse.csc import csc_matrix as cp_csc_matrix + CP_MATRIX_TYPES = [cp_coo_matrix, cp_csr_matrix, cp_csc_matrix] CP_COMPRESSED_MATRIX_TYPES = [cp_csr_matrix, cp_csc_matrix] except ModuleNotFoundError: @@ -33,6 +39,7 @@ from scipy.sparse.coo import coo_matrix as sp_coo_matrix from scipy.sparse.csr import csr_matrix as sp_csr_matrix from scipy.sparse.csc import csc_matrix as sp_csc_matrix + SP_MATRIX_TYPES = [sp_coo_matrix, sp_csr_matrix, sp_csc_matrix] SP_COMPRESSED_MATRIX_TYPES = [sp_csr_matrix, sp_csc_matrix] except ModuleNotFoundError: @@ -55,8 +62,8 @@ def get_traversed_path(df, id): ---------- df : cudf.DataFrame The dataframe containing the results of a BFS or SSSP call - id : Int - The vertex ID + id : vertex ID + most be the same data types as what is in the dataframe Returns --------- @@ -75,33 +82,40 @@ def get_traversed_path(df, id): >>> path = cugraph.utils.get_traversed_path(sssp_df, 32) """ - if 'vertex' not in df.columns: - raise ValueError("DataFrame does not appear to be a BFS or " - "SSP result - 'vertex' column missing") - if 'distance' not in df.columns: - raise ValueError("DataFrame does not appear to be a BFS or " - "SSP result - 'distance' column missing") - if 'predecessor' not in df.columns: - raise ValueError("DataFrame does not appear to be a BFS or " - "SSP result - 'predecessor' column missing") - if type(id) != int: - raise ValueError("The vertex 'id' needs to be an integer") + if "vertex" not in df.columns: + raise ValueError( + "DataFrame does not appear to be a BFS or " + "SSP result - 'vertex' column missing" + ) + if "distance" not in df.columns: + raise ValueError( + "DataFrame does not appear to be a BFS or " + "SSP result - 'distance' column missing" + ) + if "predecessor" not in df.columns: + raise ValueError( + "DataFrame does not appear to be a BFS or " + "SSP result - 'predecessor' column missing" + ) + if isinstance(id, type(df['vertex'].iloc[0])): + raise ValueError( + "The vertex 'id' needs to be the same as df['vertex']") # There is no guarantee that the dataframe has not been filtered # or edited. Therefore we cannot assume that using the vertex ID # as an index will work - ddf = df[df['vertex'] == id] + ddf = df[df["vertex"] == id] if len(ddf) == 0: raise ValueError("The vertex (", id, " is not in the result set") - pred = ddf['predecessor'].iloc[0] + pred = ddf["predecessor"].iloc[0] answer = [] answer.append(ddf) while pred != -1: - ddf = df[df['vertex'] == pred] - pred = ddf['predecessor'].iloc[0] + ddf = df[df["vertex"] == pred] + pred = ddf["predecessor"].iloc[0] answer.append(ddf) return cudf.concat(answer) @@ -133,17 +147,24 @@ def get_traversed_path_list(df, id): >>> path = cugraph.utils.get_traversed_path_list(sssp_df, 32) """ - if 'vertex' not in df.columns: - raise ValueError("DataFrame does not appear to be a BFS or " - "SSP result - 'vertex' column missing") - if 'distance' not in df.columns: - raise ValueError("DataFrame does not appear to be a BFS or " - "SSP result - 'distance' column missing") - if 'predecessor' not in df.columns: - raise ValueError("DataFrame does not appear to be a BFS or " - "SSP result - 'predecessor' column missing") - if type(id) != int: - raise ValueError("The vertex 'id' needs to be an integer") + if "vertex" not in df.columns: + raise ValueError( + "DataFrame does not appear to be a BFS or " + "SSP result - 'vertex' column missing" + ) + if "distance" not in df.columns: + raise ValueError( + "DataFrame does not appear to be a BFS or " + "SSP result - 'distance' column missing" + ) + if "predecessor" not in df.columns: + raise ValueError( + "DataFrame does not appear to be a BFS or " + "SSP result - 'predecessor' column missing" + ) + if isinstance(id, type(df['vertex'].iloc[0])): + raise ValueError( + "The vertex 'id' needs to be the same as df['vertex']") # There is no guarantee that the dataframe has not been filtered # or edited. Therefore we cannot assume that using the vertex ID @@ -153,17 +174,17 @@ def get_traversed_path_list(df, id): answer = [] answer.append(id) - ddf = df[df['vertex'] == id] + ddf = df[df["vertex"] == id] if len(ddf) == 0: raise ValueError("The vertex (", id, " is not in the result set") - pred = ddf['predecessor'].iloc[0] + pred = ddf["predecessor"].iloc[0] while pred != -1: answer.append(pred) - ddf = df[df['vertex'] == pred] - pred = ddf['predecessor'].iloc[0] + ddf = df[df["vertex"] == pred] + pred = ddf["predecessor"].iloc[0] return answer @@ -182,6 +203,33 @@ def is_cuda_version_less_than(min_version=(10, 2)): return False +def is_device_version_less_than(min_version=(7, 0)): + """ + Returns True if the version of CUDA being used is less than min_version + """ + major_version = getDeviceAttribute( + cudaDeviceAttr.cudaDevAttrComputeCapabilityMajor, 0 + ) + minor_version = getDeviceAttribute( + cudaDeviceAttr.cudaDevAttrComputeCapabilityMinor, 0 + ) + if major_version > min_version[0]: + return False + if major_version < min_version[0]: + return True + if minor_version < min_version[1]: + return True + return False + + +def get_device_memory_info(): + """ + Returns the total amount of global memory on the device in bytes + """ + meminfo = cuda.current_context().get_memory_info() + return meminfo[1] + + # FIXME: if G is a Nx type, the weight attribute is assumed to be "weight", if # set. An additional optional parameter for the weight attr name when accepting # Nx graphs may be needed. From the Nx docs: @@ -195,39 +243,45 @@ def ensure_cugraph_obj(obj, nx_weight_attr=None, matrix_graph_type=None): cugraph Graph-type obj to create when converting from a matrix type. """ # FIXME: importing here to avoid circular import - from cugraph.structure import Graph, DiGraph + from cugraph.structure import Graph, DiGraph, MultiGraph, MultiDiGraph from cugraph.utilities.nx_factory import convert_from_nx input_type = type(obj) - if input_type in [Graph, DiGraph]: + if input_type in [Graph, DiGraph, MultiGraph, MultiDiGraph]: return (obj, input_type) elif (nx is not None) and (input_type in [nx.Graph, nx.DiGraph]): return (convert_from_nx(obj, weight=nx_weight_attr), input_type) - elif (input_type in CP_MATRIX_TYPES) or \ - (input_type in SP_MATRIX_TYPES): + elif (input_type in CP_MATRIX_TYPES) or (input_type in SP_MATRIX_TYPES): if matrix_graph_type is None: matrix_graph_type = Graph elif matrix_graph_type not in [Graph, DiGraph]: - raise TypeError(f"matrix_graph_type must be either a cugraph " - f"Graph or DiGraph, got: {matrix_graph_type}") - - if input_type in (CP_COMPRESSED_MATRIX_TYPES + - SP_COMPRESSED_MATRIX_TYPES): + raise TypeError( + f"matrix_graph_type must be either a cugraph " + f"Graph or DiGraph, got: {matrix_graph_type}" + ) + + if input_type in ( + CP_COMPRESSED_MATRIX_TYPES + SP_COMPRESSED_MATRIX_TYPES + ): coo = obj.tocoo(copy=False) else: coo = obj if input_type in CP_MATRIX_TYPES: - df = cudf.DataFrame({"source": cp.ascontiguousarray(coo.row), - "destination": cp.ascontiguousarray(coo.col), - "weight": cp.ascontiguousarray(coo.data)}) + df = cudf.DataFrame( + { + "source": cp.ascontiguousarray(coo.row), + "destination": cp.ascontiguousarray(coo.col), + "weight": cp.ascontiguousarray(coo.data), + } + ) else: - df = cudf.DataFrame({"source": coo.row, - "destination": coo.col, - "weight": coo.data}) + df = cudf.DataFrame( + {"source": coo.row, "destination": coo.col, "weight": coo.data} + ) # FIXME: # * do a quick check that symmetry is stored explicitly in the cupy # data for sym matrices (ie. for each uv, check vu is there) @@ -281,3 +335,23 @@ def import_optional(mod, import_from=None): pass return namespace.get(mod) + + +def renumber_vertex_pair(input_graph, vertex_pair): + vertex_size = input_graph.vertex_column_size() + columns = vertex_pair.columns.to_list() + if vertex_size == 1: + for col in vertex_pair.columns: + if input_graph.renumbered: + vertex_pair = input_graph.add_internal_vertex_id( + vertex_pair, col, col + ) + else: + if input_graph.renumbered: + vertex_pair = input_graph.add_internal_vertex_id( + vertex_pair, "src", columns[:vertex_size] + ) + vertex_pair = input_graph.add_internal_vertex_id( + vertex_pair, "dst", columns[vertex_size:] + ) + return vertex_pair diff --git a/python/pytest.ini b/python/pytest.ini index 33c82fe48f7..a1933ea34aa 100644 --- a/python/pytest.ini +++ b/python/pytest.ini @@ -5,12 +5,15 @@ addopts = --benchmark-min-rounds=1 --benchmark-columns="mean, rounds" --benchmark-gpu-disable + --cov=cugraph + --cov-report term-missing:skip-covered markers = managedmem_on: RMM managed memory enabled managedmem_off: RMM managed memory disabled poolallocator_on: RMM pool allocator enabled poolallocator_off: RMM pool allocator disabled + preset_gpu_count: Use a hard-coded number of GPUs for specific MG tests ETL: benchmarks for ETL steps small: small datasets tiny: tiny datasets diff --git a/python/setup.py b/python/setup.py index d99ff12cfa1..a2dde239cca 100644 --- a/python/setup.py +++ b/python/setup.py @@ -1,4 +1,4 @@ -# Copyright (c) 2018-2020, NVIDIA CORPORATION. +# Copyright (c) 2018-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. # You may obtain a copy of the License at @@ -13,29 +13,51 @@ import os import sys +import sysconfig import shutil -from setuptools import setup, find_packages, Command -from setuptools.extension import Extension -from setuputils import use_raft_package, get_environment_option +# Must import in this order: +# setuptools -> Cython.Distutils.build_ext -> setuptools.command.build_ext +# Otherwise, setuptools.command.build_ext ends up inheriting from +# Cython.Distutils.old_build_ext which we do not want +import setuptools try: - from Cython.Distutils.build_ext import new_build_ext as build_ext + from Cython.Distutils.build_ext import new_build_ext as _build_ext except ImportError: - from setuptools.command.build_ext import build_ext + from setuptools.command.build_ext import build_ext as _build_ext -import versioneer from distutils.sysconfig import get_python_lib +import setuptools.command.build_ext +from setuptools import find_packages, setup, Command +from setuptools.extension import Extension + +from setuputils import use_raft_package, get_environment_option + +import versioneer + INSTALL_REQUIRES = ['numba', 'cython'] +CYTHON_FILES = ['cugraph/**/*.pyx'] + +UCX_HOME = get_environment_option("UCX_HOME") +CUDA_HOME = get_environment_option('CUDA_HOME') +CONDA_PREFIX = get_environment_option('CONDA_PREFIX') conda_lib_dir = os.path.normpath(sys.prefix) + '/lib' conda_include_dir = os.path.normpath(sys.prefix) + '/include' -CYTHON_FILES = ['cugraph/**/*.pyx'] +if CONDA_PREFIX: + conda_include_dir = CONDA_PREFIX + '/include' + conda_lib_dir = CONDA_PREFIX + '/lib' + +if not UCX_HOME: + UCX_HOME = CONDA_PREFIX if CONDA_PREFIX else os.sys.prefix + +ucx_include_dir = os.path.join(UCX_HOME, "include") +ucx_lib_dir = os.path.join(UCX_HOME, "lib") -CUDA_HOME = os.environ.get("CUDA_HOME", False) if not CUDA_HOME: path_to_cuda_gdb = shutil.which("cuda-gdb") if path_to_cuda_gdb is None: @@ -53,11 +75,7 @@ ) cuda_include_dir = os.path.join(CUDA_HOME, "include") - -if (os.environ.get('CONDA_PREFIX', None)): - conda_prefix = os.environ.get('CONDA_PREFIX') - conda_include_dir = conda_prefix + '/include' - conda_lib_dir = conda_prefix + '/lib' +cuda_lib_dir = os.path.join(CUDA_HOME, "lib64") # Optional location of C++ build folder that can be configured by the user libcugraph_path = get_environment_option('CUGRAPH_BUILD_PATH') @@ -65,8 +83,38 @@ raft_path = get_environment_option('RAFT_PATH') # FIXME: This could clone RAFT, even if it's not needed (eg. running --clean). -raft_include_dir = use_raft_package(raft_path, libcugraph_path, - git_info_file='../cpp/CMakeLists.txt') +# deprecated: This functionality will go away after +# https://github.com/rapidsai/raft/issues/83 +raft_include_dir = use_raft_package(raft_path, libcugraph_path) + +if not libcugraph_path: + libcugraph_path = conda_lib_dir + +extensions = [ + Extension("*", + sources=CYTHON_FILES, + include_dirs=[ + conda_include_dir, + ucx_include_dir, + '../cpp/include', + "../thirdparty/cub", + raft_include_dir, + os.path.join(conda_include_dir, "libcudacxx"), + cuda_include_dir, + os.path.dirname(sysconfig.get_path("include")) + ], + library_dirs=[ + get_python_lib(), + conda_lib_dir, + libcugraph_path, + ucx_lib_dir, + cuda_lib_dir, + os.path.join(os.sys.prefix, "lib") + ], + libraries=['cudart', 'cusparse', 'cusolver', 'cugraph', 'nccl'], + language='c++', + extra_compile_args=['-std=c++17']) +] class CleanCommand(Command): @@ -92,33 +140,50 @@ def run(self): os.system('find . -name "*.cpython*.so" -type f -delete') +class build_ext_no_debug(_build_ext): + + def build_extensions(self): + def remove_flags(compiler, *flags): + for flag in flags: + try: + compiler.compiler_so = list( + filter((flag).__ne__, compiler.compiler_so) + ) + except Exception: + pass + # Full optimization + self.compiler.compiler_so.append("-O3") + # No debug symbols, full optimization, no '-Wstrict-prototypes' warning + remove_flags( + self.compiler, "-g", "-G", "-O1", "-O2", "-Wstrict-prototypes" + ) + super().build_extensions() + + def finalize_options(self): + if self.distribution.ext_modules: + # Delay import this to allow for Cython-less installs + from Cython.Build.Dependencies import cythonize + + nthreads = getattr(self, "parallel", None) # -j option in Py3.5+ + nthreads = int(nthreads) if nthreads else None + self.distribution.ext_modules = cythonize( + self.distribution.ext_modules, + nthreads=nthreads, + force=self.force, + gdb_debug=False, + compiler_directives=dict( + profile=False, language_level=3, embedsignature=True + ), + ) + # Skip calling super() and jump straight to setuptools + setuptools.command.build_ext.build_ext.finalize_options(self) + + cmdclass = dict() cmdclass.update(versioneer.get_cmdclass()) -cmdclass["build_ext"] = build_ext +cmdclass["build_ext"] = build_ext_no_debug cmdclass["clean"] = CleanCommand -EXTENSIONS = [ - Extension("*", - sources=CYTHON_FILES, - include_dirs=[conda_include_dir, - '../cpp/include', - "../thirdparty/cub", - raft_include_dir, - os.path.join( - conda_include_dir, "libcudf", "libcudacxx"), - cuda_include_dir], - library_dirs=[get_python_lib()], - runtime_library_dirs=[conda_lib_dir], - libraries=['cugraph', 'cudf', 'nccl'], - language='c++', - extra_compile_args=['-std=c++14']) -] - -for e in EXTENSIONS: - e.cython_directives = dict( - profile=False, language_level=3, embedsignature=True - ) - setup(name='cugraph', description="cuGraph - GPU Graph Analytics", version=versioneer.get_version(), @@ -132,8 +197,8 @@ def run(self): ], # Include the separately-compiled shared library author="NVIDIA Corporation", - setup_requires=['cython'], - ext_modules=EXTENSIONS, + setup_requires=['Cython>=0.29,<0.30'], + ext_modules=extensions, packages=find_packages(include=['cugraph', 'cugraph.*']), install_requires=INSTALL_REQUIRES, license="Apache", diff --git a/python/setuputils.py b/python/setuputils.py index 360526c2b56..a64e60e1c32 100644 --- a/python/setuputils.py +++ b/python/setuputils.py @@ -1,5 +1,5 @@ # -# Copyright (c) 2018-2020, NVIDIA CORPORATION. +# Copyright (c) 2018-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. @@ -20,7 +20,6 @@ import shutil import subprocess import sys -import warnings from pathlib import Path @@ -71,7 +70,7 @@ def clean_folder(path): def use_raft_package(raft_path, cpp_build_path, - git_info_file='../cpp/cmake/Dependencies.cmake'): + git_info_file=None): """ Function to use the python code in RAFT in package.raft @@ -79,8 +78,8 @@ def use_raft_package(raft_path, cpp_build_path, if you want to change RAFT location. - Uses RAFT located in $RAFT_PATH if $RAFT_PATH exists. - Otherwise it will look for RAFT in the libcugraph build folder, - located either in the default location ../cpp/build or in - $CUGRAPH_BUILD_PATH. + located either in the default locations ../cpp/build/raft, + ../cpp/build/_deps/raft-src, or in $CUGRAPH_BUILD_PATH. -Otherwise it will clone RAFT into _external_repositories. - Branch/git tag cloned is located in git_info_file in this case. @@ -88,17 +87,25 @@ def use_raft_package(raft_path, cpp_build_path, ------- raft_include_path: Str Path to the C++ include folder of RAFT + """ if os.path.isdir('cugraph/raft'): raft_path = os.path.realpath('cugraph/raft') # walk up two dirs from `python/raft` raft_path = os.path.join(raft_path, '..', '..') print("-- Using existing RAFT folder") + elif cpp_build_path and os.path.isdir(os.path.join(cpp_build_path, + '_deps/raft-src')): + raft_path = os.path.join(cpp_build_path, '_deps/raft-src') + raft_path = os.path.realpath(raft_path) + print("-- Using existing RAFT folder in CPP build dir from cmake " + "FetchContent") elif cpp_build_path and os.path.isdir(os.path.join(cpp_build_path, 'raft/src/raft')): raft_path = os.path.join(cpp_build_path, 'raft/src/raft') raft_path = os.path.realpath(raft_path) - print("-- Using existing RAFT folder in CPP build dir") + print("-- Using existing RAFT folder in CPP build dir from cmake " + "ExternalProject") elif isinstance(raft_path, (str, os.PathLike)): print('-- Using RAFT_PATH argument') elif os.environ.get('RAFT_PATH', False) is not False: @@ -131,10 +138,13 @@ def use_raft_package(raft_path, cpp_build_path, def clone_repo_if_needed(name, cpp_build_path=None, git_info_file=None): if git_info_file is None: - git_info_file = _get_repo_path() + '/cpp/CMakeLists.txt' + git_info_file = \ + _get_repo_path() + '/cpp/cmake/thirdparty/get_{}.cmake'.format( + name + ) if cpp_build_path is None or cpp_build_path is False: - cpp_build_path = _get_repo_path() + '/cpp/build/' + cpp_build_path = _get_repo_path() + '/cpp/build/_deps/' repo_cloned = get_submodule_dependency(name, cpp_build_path=cpp_build_path, @@ -144,7 +154,7 @@ def clone_repo_if_needed(name, cpp_build_path=None, repo_path = ( _get_repo_path() + '/python/_external_repositories/' + name + '/') else: - repo_path = os.path.join(cpp_build_path, name + '/src/' + name + '/') + repo_path = os.path.join(cpp_build_path, name + '-src/') return repo_path, repo_cloned @@ -184,7 +194,7 @@ def get_submodule_dependency(repo, repo_info = get_repo_cmake_info(repos, git_info_file) - if os.path.exists(cpp_build_path): + if os.path.exists(os.path.join(cpp_build_path, repos[0] + '-src/')): print("-- Third party modules found succesfully in the libcugraph++ " "build folder.") @@ -192,11 +202,11 @@ def get_submodule_dependency(repo, else: - warnings.warn("-- Third party repositories have not been found so they" - "will be cloned. To avoid this set the environment " - "variable CUGRAPH_BUILD_PATH, containing the relative " - "path of the root of the repository to the folder " - "where libcugraph++ was built.") + print("-- Third party repositories have not been found so they" + "will be cloned. To avoid this set the environment " + "variable CUGRAPH_BUILD_PATH, containing the relative " + "path of the root of the repository to the folder " + "where libcugraph++ was built.") for repo in repos: clone_repo(repo, repo_info[repo][0], repo_info[repo][1]) @@ -266,20 +276,30 @@ def get_repo_cmake_info(names, file_path): specified by cmake. """ - with open(file_path, encoding='utf-8') as f: + with open(file_path) as f: s = f.read() results = {} for name in names: - res = re.findall(r'ExternalProject_Add\(' + re.escape(name) - + '\s.*GIT_REPOSITORY.*\s.*GIT_TAG.*', # noqa: W605 - s) - - res = re.sub(' +', ' ', res[0]) - res = res.split(' ') - res = [res[2][:-1], res[4]] - results[name] = res + repo = re.findall(r'\s.*GIT_REPOSITORY.*', s) + repo = repo[-1].split()[-1] + fork = re.findall(r'\s.*FORK.*', s) + fork = fork[-1].split()[-1] + repo = repo.replace("${PKG_FORK}", fork) + tag = re.findall(r'\s.*PINNED_TAG.*', s) + tag = tag[-1].split()[-1] + results[name] = [repo, tag] + if tag == 'branch-${CUGRAPH_BRANCH_VERSION_raft}': + loc = _get_repo_path() + '/cpp/CMakeLists.txt' + with open(loc) as f: + cmakelists = f.read() + tag = re.findall(r'\s.*project\(CUGRAPH VERSION.*', cmakelists) + print(tag) + tag = tag[-1].split()[2].split('.') + tag = 'branch-{}.{}'.format(tag[0], tag[1]) + + results[name] = [repo, tag] return results diff --git a/python/utils/ECG_Golden.ipynb b/python/utils/ECG_Golden.ipynb deleted file mode 100644 index 0da04869d78..00000000000 --- a/python/utils/ECG_Golden.ipynb +++ /dev/null @@ -1,487 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This notebook was used to generate the golden data results for ECG. It requires that the python-igraph package be installed to run. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from scipy.io import mmread\n", - "import networkx as nx\n", - "#mmFile='/datasets/kron_g500-logn21/kron_g500-logn21.mtx'\n", - "mmFile='/datasets/golden_data/graphs/dblp.mtx'\n", - "#mmFile='/datasets/networks/karate.mtx'\n", - "#mmFile='/home/jwyles/code/mycugraph/datasets/dolphins.mtx'\n", - "#mmFile='/home/jwyles/code/mycugraph/datasets/netscience.mtx'\n", - "M = mmread(mmFile).asfptype()\n", - "import cugraph\n", - "import cudf\n", - "import numpy as np\n", - "rows = cudf.Series(M.row)\n", - "cols = cudf.Series(M.col)\n", - "values = cudf.Series(M.data)\n", - "G = cugraph.Graph()\n", - "G.add_edge_list(rows, cols, values)" - ] - }, - { - "cell_type": "code", - "execution_count": 46, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "CPU times: user 326 ms, sys: 400 ms, total: 726 ms\n", - "Wall time: 796 ms\n" - ] - } - ], - "source": [ - "%%time\n", - "parts = cugraph.ecg(G, .05, 16)" - ] - }, - { - "cell_type": "code", - "execution_count": 47, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "49204" - ] - }, - "execution_count": 47, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "numParts = parts['partition'].max() + 1\n", - "numParts" - ] - }, - { - "cell_type": "code", - "execution_count": 48, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0.850147008895874" - ] - }, - "execution_count": 48, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "mod = cugraph.analyzeClustering_modularity(G, numParts, parts['partition'])\n", - "mod" - ] - }, - { - "cell_type": "code", - "execution_count": 49, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0.7506256512679915" - ] - }, - "execution_count": 49, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "parts2, mod2 = cugraph.louvain(G)\n", - "mod2" - ] - }, - { - "cell_type": "code", - "execution_count": 50, - "metadata": {}, - "outputs": [], - "source": [ - "import igraph as ig\n", - "import numpy as np\n", - "\n", - "def community_ecg(self, weights=None, ens_size=16, min_weight=0.05):\n", - " W = [0]*self.ecount()\n", - " ## Ensemble of level-1 Louvain \n", - " for i in range(ens_size):\n", - " p = np.random.permutation(self.vcount()).tolist()\n", - " g = self.permute_vertices(p)\n", - " l = g.community_multilevel(weights=weights, return_levels=True)[0].membership\n", - " b = [l[p[x.tuple[0]]]==l[p[x.tuple[1]]] for x in self.es]\n", - " W = [W[i]+b[i] for i in range(len(W))]\n", - " W = [min_weight + (1-min_weight)*W[i]/ens_size for i in range(len(W))]\n", - " part = self.community_multilevel(weights=W)\n", - " ## Force min_weight outside 2-core\n", - " core = self.shell_index()\n", - " ecore = [min(core[x.tuple[0]],core[x.tuple[1]]) for x in self.es]\n", - " part.W = [W[i] if ecore[i]>1 else min_weight for i in range(len(ecore))]\n", - " return part\n", - "\n", - "ig.Graph.community_ecg = community_ecg" - ] - }, - { - "cell_type": "code", - "execution_count": 52, - "metadata": {}, - "outputs": [], - "source": [ - "Gi = ig.Graph.Read_Edgelist('./dblp2.txt', directed=False)" - ] - }, - { - "cell_type": "code", - "execution_count": 53, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "CPU times: user 3min 49s, sys: 1.67 s, total: 3min 51s\n", - "Wall time: 3min 50s\n" - ] - } - ], - "source": [ - "%%time\n", - "ec = Gi.community_ecg()" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": {}, - "outputs": [], - "source": [ - "ecg = np.zeros(len(Gi.vs), dtype=np.int32)" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([0, 0, 0, ..., 0, 0, 0], dtype=int32)" - ] - }, - "execution_count": 37, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "ecg" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": {}, - "outputs": [], - "source": [ - "for i in range(len(ec)):\n", - " for j in ec[i]:\n", - " ecg[j] = i" - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([275, 275, 0, ..., 435, 435, 107], dtype=int32)" - ] - }, - "execution_count": 39, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "ecg" - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": {}, - "outputs": [], - "source": [ - "ecg_col = cudf.Series(ecg)" - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "metadata": {}, - "outputs": [], - "source": [ - "numParts = ecg_col.max() + 1" - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "metadata": {}, - "outputs": [], - "source": [ - "mod4 = cugraph.analyzeClustering_modularity(G, numParts, ecg_col)" - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0.9279554486274719" - ] - }, - "execution_count": 43, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "mod4" - ] - }, - { - "cell_type": "code", - "execution_count": 94, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "34" - ] - }, - "execution_count": 94, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "maxId = 0\n", - "for i in range(len(ec)):\n", - " for j in ec[i]:\n", - " if j > maxId:\n", - " maxId = j\n", - "maxId" - ] - }, - { - "cell_type": "code", - "execution_count": 79, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "156" - ] - }, - "execution_count": 79, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(Gi.es)" - ] - }, - { - "cell_type": "code", - "execution_count": 80, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "156" - ] - }, - "execution_count": 80, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(M.row)" - ] - }, - { - "cell_type": "code", - "execution_count": 81, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "156" - ] - }, - "execution_count": 81, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "78 *2" - ] - }, - { - "cell_type": "code", - "execution_count": 51, - "metadata": {}, - "outputs": [], - "source": [ - "filename = \"dblp2.txt\"\n", - "f = open(filename, 'w')\n", - "for i in range(len(M.row)):\n", - " f.write(str(M.row[i]) + ' ' + str(M.col[i]) + '\\n')\n", - "f.close()" - ] - }, - { - "cell_type": "code", - "execution_count": 82, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "igraph.Edge(, 1, {})" - ] - }, - "execution_count": 82, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "Gi.es[1]" - ] - }, - { - "cell_type": "code", - "execution_count": 84, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "igraph.Edge(, 1, {})" - ] - }, - "execution_count": 84, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "Gi.es.select()[1]" - ] - }, - { - "cell_type": "code", - "execution_count": 85, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 85, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "Gi.es[0].source" - ] - }, - { - "cell_type": "code", - "execution_count": 88, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 88, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "Gi.es[0].target" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.7.3" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/thirdparty/LICENSES/LICENSE.boost b/thirdparty/LICENSES/LICENSE.boost new file mode 100644 index 00000000000..36b7cd93cdf --- /dev/null +++ b/thirdparty/LICENSES/LICENSE.boost @@ -0,0 +1,23 @@ +Boost Software License - Version 1.0 - August 17th, 2003 + +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by +this license (the "Software") to use, reproduce, display, distribute, +execute, and transmit the Software, and to prepare derivative works of the +Software, and to permit third-parties to whom the Software is furnished to +do so, all subject to the following: + +The copyright notices in the Software and this entire statement, including +the above license grant, this restriction and the following disclaimer, +must be included in all copies of the Software, in whole or in part, and +all derivative works of the Software, unless such copies or derivative +works are solely in the form of machine-executable object code generated by +a source language processor. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/thirdparty/LICENSES/LICENSE.texas_state_university b/thirdparty/LICENSES/LICENSE.texas_state_university new file mode 100644 index 00000000000..7862557ac87 --- /dev/null +++ b/thirdparty/LICENSES/LICENSE.texas_state_university @@ -0,0 +1,24 @@ +Copyright (c) 2014-2020, Texas State University. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Texas State University nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL TEXAS STATE UNIVERSITY BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.