Graphite
Loading...
Searching...
No Matches
active.hpp
Go to the documentation of this file.
1
2#pragma once
3#include <thrust/copy.h>
4#include <thrust/count.h>
5#include <thrust/device_vector.h>
6#include <thrust/execution_policy.h>
7
8namespace graphite {
9
10// For factors, ignores the MSB
11__host__ __device__ bool is_factor_active(const uint8_t active_val,
12 const uint8_t level) {
13 constexpr uint8_t NOT_MSB = 0x7F; // 01111111
14 return (active_val & NOT_MSB) <= level && ((active_val & 0x80) == 0);
15}
16
17// For vertex
18__host__ __device__ bool is_vertex_active(const uint8_t *active_state,
19 const size_t vertex_id) {
20 return !(active_state[vertex_id] > 0);
21}
22
23// Returns the number of active constraints and fills the active_indices vector
24size_t build_active_indices(const thrust::device_vector<uint8_t> &active,
25 thrust::device_vector<size_t> &active_indices,
26 const size_t count, const uint8_t level) {
27
28 // Count active constraints
29 const size_t active_count =
30 thrust::count_if(thrust::device, active.begin(), active.end(),
31 [level] __device__(const uint8_t a) {
32 return is_factor_active(a, level);
33 });
34
35 // Resize active indices to the number of active constraints
36 active_indices.clear();
37 active_indices.resize(active_count);
38
39 // Fill active indices with the indices of active constraints
40 thrust::copy_if(thrust::device, thrust::make_counting_iterator<size_t>(0),
41 thrust::make_counting_iterator<size_t>(count), active.begin(),
42 active_indices.begin(), [level] __device__(const uint8_t a) {
43 return is_factor_active(a, level);
44 });
45 return active_count;
46}
47
48} // namespace graphite