-
Notifications
You must be signed in to change notification settings - Fork 549
Expand file tree
/
Copy pathqueue.hpp
More file actions
148 lines (120 loc) · 3.76 KB
/
queue.hpp
File metadata and controls
148 lines (120 loc) · 3.76 KB
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
146
147
148
/*******************************************************
* Copyright (c) 2016, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#pragma once
#include <Param.hpp>
#include <common/util.hpp>
#include <memory.hpp>
#include <algorithm>
// FIXME: Is there a better way to check for std::future not being supported ?
#if defined(AF_DISABLE_CPU_ASYNC) || \
(defined(__GNUC__) && \
(__GCC_ATOMIC_INT_LOCK_FREE < 2 || __GCC_ATOMIC_POINTER_LOCK_FREE < 2))
#include <functional>
using std::function;
#include <err_cpu.hpp>
#define __SYNCHRONOUS_ARCH 1
class queue_impl {
public:
template<typename F, typename... Args>
void enqueue(const F func, Args... args) const {
AF_ERROR("Incorrectly configured", AF_ERR_INTERNAL);
}
void sync() const { AF_ERROR("Incorrectly configured", AF_ERR_INTERNAL); }
bool is_worker() const {
AF_ERROR("Incorrectly configured", AF_ERR_INTERNAL);
return false;
}
};
class event_impl {
public:
event_impl() noexcept = default;
~event_impl() noexcept = default;
explicit event_impl(const event_impl &other) = default;
event_impl(event_impl &&other) noexcept = default;
event_impl &operator=(event_impl &&other) noexcept = default;
event_impl &operator=(event_impl &other) noexcept = default;
explicit event_impl(const int val) {}
event_impl &operator=(int val) noexcept { return *this; }
int create() {
AF_ERROR("Incorrectly configured", AF_ERR_INTERNAL);
return 0;
}
int mark(queue_impl &queue) {
AF_ERROR("Incorrectly configured", AF_ERR_INTERNAL);
return 0;
}
int wait(queue_impl &queue) const {
AF_ERROR("Incorrectly configured", AF_ERR_INTERNAL);
return 0;
}
int sync() const noexcept {
AF_ERROR("Incorrectly configured", AF_ERR_INTERNAL);
return 0;
}
operator bool() const noexcept { return false; }
};
#else
#include <threads/async_queue.hpp>
#include <threads/event.hpp>
#define __SYNCHRONOUS_ARCH 0
using queue_impl = threads::async_queue;
using event_impl = threads::event;
#endif
namespace arrayfire {
namespace cpu {
/// Wraps the async_queue class
class queue {
public:
queue()
: count(0)
, sync_calls(__SYNCHRONOUS_ARCH == 1 ||
common::getEnvVar("AF_SYNCHRONOUS_CALLS") == "1") {}
template<typename F, typename... Args>
void enqueue(const F func, Args &&...args) {
count++;
if (sync_calls) {
func(toParam(std::forward<Args>(args))...);
} else {
aQueue.enqueue(func, toParam(std::forward<Args>(args))...);
}
#ifndef NDEBUG
sync();
#else
if (getMemoryPressure() >= getMemoryPressureThreshold() ||
count >= 25) {
sync();
}
#endif
}
void sync() {
count = 0;
if (!sync_calls) aQueue.sync();
}
bool is_worker() const {
return (!sync_calls) ? aQueue.is_worker() : false;
}
friend class queue_event;
private:
int count;
const bool sync_calls;
queue_impl aQueue;
};
class queue_event {
event_impl event_;
public:
queue_event() = default;
queue_event(int val) : event_(val) {}
int create() { return event_.create(); }
int mark(queue &q) { return event_.mark(q.aQueue); }
int wait(queue &q) { return event_.wait(q.aQueue); }
int sync() noexcept { return event_.sync(); }
operator bool() const noexcept { return event_; }
};
} // namespace cpu
} // namespace arrayfire