-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDevice.cpp
192 lines (168 loc) · 5.3 KB
/
Device.cpp
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* @file Device.cpp
* @brief Implementation of Device functions.
* @author Ankit Srivastava <asrivast@gatech.edu>
*
* Copyright 2018 Georgia Institute of Technology
*
* 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.
*/
#include "Device.hpp"
#include "APCall.hpp"
#include "Timer.hpp"
#include <algorithm>
#include <cstring>
#include <micron/ap/ap_load.h>
namespace ap {
/**
* @brief Default constructor.
*/
Device::Device(
) : m_device(0),
m_runtimeObject(0)
{
}
/**
* @brief Constructor for creating the object for a given device name.
*
* @param deviceName Name of the device to be opened.
* @param svReserve Number of state vectors to be reserved.
* Defaults to -1, which reserves all the vectors.
*/
Device::Device(
const std::string& deviceName,
const int svReserve
) : m_device(0),
m_runtimeObject(0)
{
// Reserve all the state vectors.
APCALL_CHECK(AP_OpenDevice)(&m_device, deviceName.c_str(), svReserve);
}
Device&
Device::operator=(
Device&& that
)
{
m_device = that.m_device;
m_runtimeObject = that.m_runtimeObject;
that.m_device = 0;
that.m_runtimeObject = 0;
return *this;
}
/**
* @brief Loads the given automaton into the given load region on the device.
*
* @param automaton The automaton to be loaded on the device.
* @param loadRegion The load region to be used. Defaults to 0.
*/
void
Device::load(
Automaton&& automaton,
const unsigned loadRegion
)
{
APCALL_CHECK(AP_Load)(m_device, &m_runtimeObject, loadRegion, *automaton, static_cast<void*>(0), static_cast<const char*>(0));
// Invalidate the underlying ap_automaton_t pointer so that AP_Destroy is not called on it.
automaton.m_automaton = 0;
}
/**
* @brief Searches the automaton using the given data.
*
* @param data The data to be streamed to the device.
* @param maxChunkSize Maximum size of the chunk to be streamed to the device.
*
* @returns Pairs of the byte offset of matches and the corresponding matched automaton refs.
*/
std::vector<std::pair<size_t, ElementRef> >
Device::search(
std::vector<unsigned char>& data,
const size_t maxChunkSize,
const bool printStats
)
{
std::vector<std::pair<size_t, ElementRef> > allResults;
const size_t dataSize = data.size();
ap_flow_t flow;
APCALL_CHECK(AP_OpenFlow)(m_device, &flow, m_runtimeObject, static_cast<void*>(0));
size_t index = 0;
std::vector<struct ap_match_result> matches(MAX_MATCHES);
std::vector<std::pair<size_t, ElementRef> > results(MAX_MATCHES);
Timer t1, t2;
do {
struct ap_flow_chunk flowChunk;
memset(&flowChunk, 0, sizeof(flowChunk));
flowChunk.data = &data[index];
flowChunk.length = ((dataSize - index) > maxChunkSize) ? maxChunkSize : (dataSize - index);
// Index for the next iteration.
index += flowChunk.length;
struct ap_flow_data flowData;
memset(&flowData, 0, sizeof(flowData));
flowData.flow = flow;
flowData.chunks = &flowChunk;
flowData.chunk_count = 1;
struct ap_completion complete;
t1.start();
APCALL_CHECK(AP_ScanFlows)(m_device, &flowData, 1, &complete);
APCALL_CHECK(AP_Wait)(m_device, &complete, 0);
t1.pause();
size_t numMatches;
do {
t2.start();
numMatches = APCALL_CHECK(AP_GetMatches)(m_device, &matches[0], matches.size());
t2.pause();
std::transform(matches.begin(), matches.begin() + numMatches, results.begin(),
[](const struct ap_match_result& match) { return std::make_pair(match.byte_offset, ElementRef(match.report_alias.elementRef)); }
);
allResults.insert(allResults.end(), results.begin(), results.begin() + numMatches);
} while (numMatches > 0);
} while (index < dataSize);
if (printStats) {
std::cout << "# of bytes scanned: " << data.size() << std::endl;
std::cout << "Time taken in scanning: " << t1.elapsed<Timer::MilliSeconds>() << " (effective streaming rate: " << (data.size() * 1000) / (1024 * 1024 * t1.elapsed<Timer::MilliSeconds>()) << " MBps)" << std::endl;
std::cout << "# of matches generated: " << allResults.size() << std::endl;
std::cout << "Time taken in getting the matches: " << t2.elapsed<Timer::MilliSeconds>() << " ms" << std::endl;
}
APCALL_CHECK(AP_CloseFlow)(m_device, flow);
return allResults;
}
/**
* @brief Unload the currently loaded runtime object from the device.
*/
void
Device::unload(
)
{
if (m_runtimeObject != 0) {
APCALL_CHECK(AP_Unload)(m_device, m_runtimeObject);
m_runtimeObject = 0;
}
}
/**
* @brief Default destructor.
*/
Device::~Device(
)
{
try {
unload();
if (m_device != 0) {
APCALL_CHECK(AP_CloseDevice)(m_device);
m_device = 0;
}
}
catch (const std::runtime_error& e) {
std::cerr << "Unable to close the device because of errors." << std::endl;
std::cerr << e.what() << std::endl;
}
}
} // namespace ap