|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, NVIDIA CORPORATION. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <rmm/thrust_rmm_allocator.h> |
| 18 | +#include <rmm/device_uvector.hpp> |
| 19 | + |
| 20 | +#include <raft/handle.hpp> |
| 21 | + |
| 22 | +#include <utilities/error.hpp> |
| 23 | +#include <utilities/path_retrieval.hpp> |
| 24 | + |
| 25 | +namespace cugraph { |
| 26 | +namespace detail { |
| 27 | + |
| 28 | +template <typename vertex_t, typename weight_t> |
| 29 | +__global__ void get_traversed_cost_kernel(vertex_t const *vertices, |
| 30 | + vertex_t const *preds, |
| 31 | + vertex_t const *vtx_map, |
| 32 | + weight_t const *info_weights, |
| 33 | + weight_t *out, |
| 34 | + vertex_t stop_vertex, |
| 35 | + vertex_t num_vertices) |
| 36 | +{ |
| 37 | + for (vertex_t i = threadIdx.x + blockIdx.x * blockDim.x; i < num_vertices; |
| 38 | + i += gridDim.x * blockDim.x) { |
| 39 | + weight_t sum = info_weights[i]; |
| 40 | + vertex_t pred = preds[i]; |
| 41 | + while (pred != stop_vertex) { |
| 42 | + vertex_t pos = vtx_map[pred]; |
| 43 | + sum += info_weights[pos]; |
| 44 | + pred = preds[pos]; |
| 45 | + } |
| 46 | + out[i] = sum; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +template <typename vertex_t, typename weight_t> |
| 51 | +void get_traversed_cost_impl(raft::handle_t const &handle, |
| 52 | + vertex_t const *vertices, |
| 53 | + vertex_t const *preds, |
| 54 | + weight_t const *info_weights, |
| 55 | + weight_t *out, |
| 56 | + vertex_t stop_vertex, |
| 57 | + vertex_t num_vertices) |
| 58 | +{ |
| 59 | + auto stream = handle.get_stream(); |
| 60 | + vertex_t max_blocks = handle.get_device_properties().maxGridSize[0]; |
| 61 | + vertex_t max_threads = handle.get_device_properties().maxThreadsPerBlock; |
| 62 | + |
| 63 | + dim3 nthreads, nblocks; |
| 64 | + nthreads.x = std::min<vertex_t>(num_vertices, max_threads); |
| 65 | + nthreads.y = 1; |
| 66 | + nthreads.z = 1; |
| 67 | + nblocks.x = std::min<vertex_t>((num_vertices + nthreads.x - 1) / nthreads.x, max_blocks); |
| 68 | + nblocks.y = 1; |
| 69 | + nblocks.z = 1; |
| 70 | + |
| 71 | + rmm::device_uvector<vertex_t> vtx_map_v(num_vertices, stream); |
| 72 | + rmm::device_uvector<vertex_t> vtx_keys_v(num_vertices, stream); |
| 73 | + vertex_t *vtx_map = vtx_map_v.data(); |
| 74 | + vertex_t *vtx_keys = vtx_keys_v.data(); |
| 75 | + raft::copy(vtx_keys, vertices, num_vertices, stream); |
| 76 | + |
| 77 | + thrust::sequence(rmm::exec_policy(stream)->on(stream), vtx_map, vtx_map + num_vertices); |
| 78 | + |
| 79 | + thrust::stable_sort_by_key( |
| 80 | + rmm::exec_policy(stream)->on(stream), vtx_keys, vtx_keys + num_vertices, vtx_map); |
| 81 | + |
| 82 | + get_traversed_cost_kernel<<<nblocks, nthreads>>>( |
| 83 | + vertices, preds, vtx_map, info_weights, out, stop_vertex, num_vertices); |
| 84 | +} |
| 85 | +} // namespace detail |
| 86 | + |
| 87 | +template <typename vertex_t, typename weight_t> |
| 88 | +void get_traversed_cost(raft::handle_t const &handle, |
| 89 | + vertex_t const *vertices, |
| 90 | + vertex_t const *preds, |
| 91 | + weight_t const *info_weights, |
| 92 | + weight_t *out, |
| 93 | + vertex_t stop_vertex, |
| 94 | + vertex_t num_vertices) |
| 95 | +{ |
| 96 | + CUGRAPH_EXPECTS(num_vertices > 0, "num_vertices should be strictly positive"); |
| 97 | + CUGRAPH_EXPECTS(out != nullptr, "out should be of size num_vertices"); |
| 98 | + cugraph::detail::get_traversed_cost_impl( |
| 99 | + handle, vertices, preds, info_weights, out, stop_vertex, num_vertices); |
| 100 | +} |
| 101 | + |
| 102 | +template void get_traversed_cost<int32_t, float>(raft::handle_t const &handle, |
| 103 | + int32_t const *vertices, |
| 104 | + int32_t const *preds, |
| 105 | + float const *info_weights, |
| 106 | + float *out, |
| 107 | + int32_t stop_vertex, |
| 108 | + int32_t num_vertices); |
| 109 | + |
| 110 | +template void get_traversed_cost<int32_t, double>(raft::handle_t const &handle, |
| 111 | + int32_t const *vertices, |
| 112 | + int32_t const *preds, |
| 113 | + double const *info_weights, |
| 114 | + double *out, |
| 115 | + int32_t stop_vertex, |
| 116 | + int32_t num_vertices); |
| 117 | + |
| 118 | +template void get_traversed_cost<int64_t, float>(raft::handle_t const &handle, |
| 119 | + int64_t const *vertices, |
| 120 | + int64_t const *preds, |
| 121 | + float const *info_weights, |
| 122 | + float *out, |
| 123 | + int64_t stop_vertex, |
| 124 | + int64_t num_vertices); |
| 125 | + |
| 126 | +template void get_traversed_cost<int64_t, double>(raft::handle_t const &handle, |
| 127 | + int64_t const *vertices, |
| 128 | + int64_t const *preds, |
| 129 | + double const *info_weights, |
| 130 | + double *out, |
| 131 | + int64_t stop_vertex, |
| 132 | + int64_t num_vertices); |
| 133 | +} // namespace cugraph |
0 commit comments