Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
weaklyConnectedComponentsCugraph.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file weaklyConnectedComponentsCugraph.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2022 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
12 
13 #include <cerrno>
14 #include <cstring>
15 #include <fstream>
16 #include <iostream>
17 #include <string>
18 #include <tuple>
19 #include <vector>
20 
21 #include <boost/range/combine.hpp>
22 #include <cugraph/algorithms.hpp>
23 #include <cugraph/graph.hpp>
24 #include <cugraph/graph_functions.hpp>
25 #include <cugraph/graph_view.hpp>
26 #include <cugraph/partition_manager.hpp>
27 #include <cugraph/utilities/error.hpp>
28 #include <raft/cudart_utils.h>
29 #include <raft/handle.hpp>
30 
31 #ifndef CUDA_RT_CALL
32 #define CUDA_RT_CALL(call) \
33  { \
34  cudaError_t cudaStatus = call; \
35  if (cudaSuccess != cudaStatus) { \
36  fprintf(stderr, \
37  "ERROR: CUDA RT call \"%s\" in line %d of file %s failed with " \
38  "%s (%d).\n", \
39  #call, __LINE__, __FILE__, cudaGetErrorString(cudaStatus), \
40  cudaStatus); \
41  } \
42  }
43 #endif // CUDA_RT_CALL
44 
45 template <typename vertex_t, typename edge_t, typename weight_t>
46 __global__ void weaklyConnectedComponents(std::vector<vertex_t>& rowIndices,
47  std::vector<vertex_t>& colIndices,
48  std::vector<weight_t>& edgeWeights,
49  std::vector<vertex_t>& trackLabels,
50  const Acts::Logger& logger) {
51  cudaStream_t stream;
52  CUDA_RT_CALL(cudaStreamCreate(&stream));
53 
54  ACTS_VERBOSE("Weakly components Start");
55  ACTS_VERBOSE("edge size: " << rowIndices.size() << " " << colIndices.size());
56  raft::handle_t handle{stream};
57 
58  cugraph::graph_t<vertex_t, edge_t, weight_t, false, false> graph(handle);
59 
60  // learn from matrix_market_file_utilities.cu
61  vertex_t maxVertexID_row =
62  *std::max_element(rowIndices.begin(), rowIndices.end());
63  vertex_t maxVertexID_col =
64  *std::max_element(colIndices.begin(), colIndices.end());
65  vertex_t maxVertex = std::max(maxVertexID_row, maxVertexID_col);
66 
67  vertex_t number_of_vertices = maxVertex;
68  rmm::device_uvector<vertex_t> d_vertices(number_of_vertices,
69  handle.get_stream());
70  std::vector<vertex_t> vertex_idx(number_of_vertices);
71  for (vertex_t idx = 0; idx < number_of_vertices; idx++) {
72  vertex_idx[idx] = idx;
73  }
74 
75  rmm::device_uvector<vertex_t> src_v(rowIndices.size(), handle.get_stream());
76  rmm::device_uvector<vertex_t> dst_v(colIndices.size(), handle.get_stream());
77  rmm::device_uvector<weight_t> weights_v(edgeWeights.size(),
78  handle.get_stream());
79 
80  raft::update_device(src_v.data(), rowIndices.data(), rowIndices.size(),
81  handle.get_stream());
82  raft::update_device(dst_v.data(), colIndices.data(), colIndices.size(),
83  handle.get_stream());
84  raft::update_device(weights_v.data(), edgeWeights.data(), edgeWeights.size(),
85  handle.get_stream());
86  raft::update_device(d_vertices.data(), vertex_idx.data(), vertex_idx.size(),
87  handle.get_stream());
88 
89  std::tie(graph, std::ignore) =
90  cugraph::create_graph_from_edgelist<vertex_t, edge_t, weight_t, false,
91  false>(
92  handle, std::move(d_vertices), std::move(src_v), std::move(dst_v),
93  std::move(weights_v), cugraph::graph_properties_t{true, false},
94  false);
95 
96  auto graph_view = graph.view();
97  CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement
98 
99  rmm::device_uvector<vertex_t> d_components(
100  graph_view.get_number_of_vertices(), handle.get_stream());
101 
102  ACTS_VERBOSE("2back from construct_graph");
103  cugraph::weakly_connected_components(handle, graph_view, d_components.data());
104 
105  ACTS_VERBOSE("number of components: " << d_components.size());
106  raft::update_host(trackLabels.data(), d_components.data(),
107  d_components.size(), handle.get_stream());
108 }