graph
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
coo-impl.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <set>
4 
5 #include "gpu_csr.hpp"
6 #include "pangolin/logger.hpp"
7 
8 #ifdef __CUDACC__
9 #define HOST __host__
10 #define DEVICE __device__
11 #else
12 #define HOST
13 #define DEVICE
14 #endif
15 
17 
18 template<typename Index>
19 COO<Index>::COO() {
20 }
21 
22 template<typename Index>
23 HOST DEVICE uint64_t COO<Index>::num_rows() const {
24  if (rowPtr_.size() == 0) {
25  return 0;
26  } else {
27  return rowPtr_.size() - 1;
28  }
29 }
30 
31 template<typename Index>
32 uint64_t COO<Index>::num_nodes() const {
33  std::set<Index> nodes;
34  // add all dsts
35  for (Index ci = 0; ci < colInd_.size(); ++ci) {
36  nodes.insert(colInd_[ci]);
37  }
38  // add non-zero sources
39  for (Index i = 0; i < rowPtr_.size() - 1; ++i) {
40  Index row_start = rowPtr_[i];
41  Index row_end = rowPtr_[i+1];
42  if (row_start != row_end) {
43  nodes.insert(i);
44  }
45  }
46  return nodes.size();
47 }
48 
49 template<typename Index>
50 COO<Index> COO<Index>::from_edgelist(const EdgeList &es, bool (*edgeFilter)(const Edge &)) {
51  COO<Index> csr;
52 
53 
54  if (es.size() == 0) {
55  LOG(warn, "constructing from empty edge list");
56  return csr;
57  }
58 
59 
60  for (const auto &edge : es) {
61 
62  const Index src = static_cast<Index>(edge.first);
63  const Index dst = static_cast<Index>(edge.second);
64 
65  // edge has a new src and should be in a new row
66  // even if the edge is filtered out, we need to add empty rows
67  while (csr.rowPtr_.size() != size_t(src + 1))
68  {
69  // expecting inputs to be sorted by src, so it should be at least
70  // as big as the current largest row we have recored
71  assert(src >= csr.rowPtr_.size());
72  // SPDLOG_TRACE(logger::console, "node {} edges start at {}", edge.src_, csr.edgeSrc_.size());
73  csr.rowPtr_.push_back(csr.colInd_.size());
74  }
75 
76  // filter or add the edge
77  if (nullptr != edgeFilter && edgeFilter(edge)) {
78  continue;
79  } else {
80  csr.rowInd_.push_back(src);
81  csr.colInd_.push_back(dst);
82  }
83  }
84 
85  // add the final length of the non-zeros to the offset array
86  csr.rowPtr_.push_back(csr.colInd_.size());
87 
88  assert(csr.rowInd_.size() == csr.colInd_.size());
89  return csr;
90 }
91 
92 template<typename Index>
94  COOView<Index> view;
95  view.nnz_ = nnz();
96  view.num_rows_ = num_rows();
97  view.rowPtr_ = rowPtr_.data();
98  view.colInd_ = colInd_.data();
99  view.rowInd_ = rowInd_.data();
100  return view;
101 }
102 
103 
105 
106 #undef HOST
107 #undef DEVICE
uint64_t num_nodes() const
number of unique row/col indices
Definition: coo-impl.hpp:32
HOST DEVICE uint64_t num_rows() const
number of matrix rows
Definition: coo-impl.hpp:23
a read-only view of a COO suitable for passing to a GPU kernel by value.
Definition: coo.hpp:25
#define DEVICE
Definition: coo-impl.hpp:13
COOView< Index > view() const
create a COOView for this COO
Definition: coo-impl.hpp:93
Vector< Index > rowInd_
non-zero row indices
Definition: coo.hpp:58
PANGOLIN_BEGIN_NAMESPACE()
std::vector< Edge > EdgeList
Definition: edge_list.hpp:9
const Index * colInd_
non-zero column indices
Definition: coo.hpp:34
#define LOG(level,...)
Definition: logger.hpp:25
A COO matrix backed by CUDA Unified Memory, with a CSR rowPtr.
Definition: coo.hpp:18
Vector< Index > rowPtr_
offset in col_ that each row starts at
Definition: coo.hpp:56
Vector< Index > colInd_
non-zero column indices
Definition: coo.hpp:57
uint64_t nnz_
number of non-zeros
Definition: coo.hpp:28
const Index * rowInd_
non-zero row indices
Definition: coo.hpp:33
static COO< Index > from_edgelist(const EdgeList &es, bool(*edgeFilter)(const Edge &)=nullptr)
Definition: coo-impl.hpp:50
uint64_t num_rows_
length of rowOffset - 1
Definition: coo.hpp:29
const Index * rowPtr_
offset in col_ that each row starts at
Definition: coo.hpp:32
#define PANGOLIN_END_NAMESPACE()
#define HOST
Definition: coo-impl.hpp:12
std::pair< Uint, Uint > Edge
Definition: edge.hpp:14