File: test_invoke.h

package info (click to toggle)
onetbb 2022.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,440 kB
  • sloc: cpp: 129,228; ansic: 9,745; python: 808; xml: 183; objc: 176; makefile: 66; sh: 66; awk: 41; javascript: 37
file content (145 lines) | stat: -rw-r--r-- 4,686 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
    Copyright (c) 2023 Intel Corporation

    Licensed under the Apache License, Version 2.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 __TBB_test_common_test_invoke_H
#define __TBB_test_common_test_invoke_H

#include "test.h"
#include "oneapi/tbb/flow_graph.h"
#include "oneapi/tbb/blocked_range.h"

#if __TBB_CPP17_INVOKE_PRESENT
namespace test_invoke {

// Can be customized
template <typename T>
std::size_t get_real_index(const T& obj) {
    return obj;
}

template <typename Value>
class SmartRange : public oneapi::tbb::blocked_range<Value> {
    using base_range = oneapi::tbb::blocked_range<Value>;
public:
    SmartRange(const Value& first, const Value& last) : base_range(first, last), change_vector(nullptr) {}
    SmartRange(const Value& first, const Value& last, std::vector<std::size_t>& cv)
        : base_range(first, last), change_vector(&cv) {}

    SmartRange(const SmartRange&) = default;
    SmartRange(SmartRange& other, oneapi::tbb::split)
        : base_range(other, oneapi::tbb::split{}), change_vector(other.change_vector) {}

    void increase() const {
        CHECK_MESSAGE(change_vector, "Attempt to operate with no associated vector");
        for (std::size_t index = get_real_index(this->begin()); index != get_real_index(this->end()); ++index) {
            ++(*change_vector)[index];
        }
    }

    Value reduction(const Value& idx) const {
        Value result = idx;
        for (std::size_t index = get_real_index(this->begin()); index != get_real_index(this->end()); ++index) {
            result = result + Value(index);
        }
        return Value(result);
    }

    Value scan(const Value& idx, bool is_final_scan) const {
        CHECK_MESSAGE(change_vector, "Attempt to operate with no associated vector");
        Value result = idx;
        for (std::size_t index = get_real_index(this->begin()); index != get_real_index(this->end()); ++index) {
            result = result + Value(index);
            if (is_final_scan) (*change_vector)[index] = get_real_index(result);
        }
        return result;
    }
private:
    std::vector<std::size_t>* change_vector;
};

template <typename IDType>
class SmartID {
public:
    SmartID() : id(999), operate_signal_point(nullptr) {}
    SmartID(std::size_t* sp) : id(999), operate_signal_point(sp) {}

    SmartID(const IDType& n) : id(n), operate_signal_point(nullptr) {}
    SmartID(const IDType& n, std::size_t* sp) : id(n), operate_signal_point(sp) {}

    IDType get_id() const { return id; }
    const IDType& get_id_ref() const { return id; }

private:
    template <typename TupleOfPorts, std::size_t... Is>
    void send_id_impl(TupleOfPorts& ports, std::index_sequence<Is...>) const {
        (std::get<Is>(ports).try_put(id) , ...);
    }
public:
    template <typename TupleOfPorts>
    void send_id(TupleOfPorts& ports) const {
        send_id_impl(ports, std::make_index_sequence<std::tuple_size<TupleOfPorts>::value>());
    }

    template <typename GatewayType>
    void send_id_to_gateway(GatewayType& gateway) const {
        gateway.reserve_wait();
        gateway.try_put(id);
        gateway.release_wait();
    }

    void operate() const {
        CHECK_MESSAGE(operate_signal_point, "incorrect test setup");
        ++(*operate_signal_point);
    }

    IDType id;
private:
    std::size_t* operate_signal_point;
};

class SmartValue {
public:
    SmartValue(std::size_t rv) : real_value(rv) {}
    SmartValue(const SmartValue&) = default;
    SmartValue& operator=(const SmartValue&) = default;

    SmartValue operator+(const SmartValue& other) const {
        return SmartValue{real_value + other.real_value};
    }
    std::size_t operator-(const SmartValue& other) const {
        return real_value - other.real_value;
    }

    std::size_t get() const { return real_value; }

    bool operator<(const SmartValue& other) const {
        return real_value < other.real_value;
    }

    SmartValue& operator++() { ++real_value; return *this; }
private:
    std::size_t real_value;
};

std::size_t get_real_index(const SmartValue& value) {
    return value.get();
}


} // namespace test_invoke

#endif // __TBB_CPP17_INVOKE_PRESENT
#endif // __TBB_test_common_test_invoke_H