-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwcbatch.C
174 lines (151 loc) · 5.24 KB
/
wcbatch.C
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
/****************************** -*- C++ -*- *****************************/
/* */
/* WordClust -- Word Clustering */
/* Version 2.00 */
/* by Ralf Brown */
/* */
/* File: wcbatch.C batch-of-lines processing */
/* LastEdit: 21sep2018 */
/* */
/* (c) Copyright 2015,2016,2017,2018 Carnegie Mellon University */
/* This program may be redistributed and/or modified under the */
/* terms of the GNU General Public License, version 3, or an */
/* alternative license agreement as detailed in the accompanying */
/* file LICENSE. You should also have received a copy of the */
/* GPL (file COPYING) along with this program. If not, see */
/* http://www.gnu.org/licenses/ */
/* */
/* This program is distributed in the hope that it will be */
/* useful, but WITHOUT ANY WARRANTY; without even the implied */
/* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR */
/* PURPOSE. See the GNU General Public License for more details. */
/* */
/************************************************************************/
#include "wordclus.h"
#include "wcbatch.h"
#include "wcparam.h"
#include "framepac/file.h"
#include "framepac/texttransforms.h"
#include "framepac/threadpool.h"
using namespace Fr ;
#include <chrono>
using namespace std ;
/************************************************************************/
/* Types for this module */
/************************************************************************/
class WcWorkOrder
{
public:
LineBatch* lines ;
WcProcessFileFunc* fn ;
const WcParameters* params ;
va_list args ;
bool success ;
volatile bool in_use ;
public:
WcWorkOrder() { lines = nullptr ; markAvailable() ; }
~WcWorkOrder() { reset() ; }
void reset() { delete lines ; lines = nullptr ; }
bool inUse() const ;
void markAvailable() ;
void markUsed() ;
} ;
/************************************************************************/
/* Globals */
/************************************************************************/
/************************************************************************/
/* External Functions */
/************************************************************************/
/************************************************************************/
/* Methods for class WcWorkOrder */
/************************************************************************/
bool WcWorkOrder::inUse() const
{
atomic_thread_fence(std::memory_order_acquire) ; // load barrier
return in_use ;
}
//----------------------------------------------------------------------
void WcWorkOrder::markAvailable()
{
in_use = false ;
atomic_thread_fence(std::memory_order_release) ; // store barrier
return ;
}
//----------------------------------------------------------------------
void WcWorkOrder::markUsed()
{
in_use = true ;
atomic_thread_fence(std::memory_order_release) ; // store barrier
return ;
}
/************************************************************************/
/************************************************************************/
static void process_file_segment(const void *input, void * /*output*/)
{
WcWorkOrder *order = (WcWorkOrder*)input ;
if (order->fn)
order->success = order->fn(*order->lines,order->params,order->args) ;
else
order->success = false ;
delete order->lines ;
order->lines = nullptr ;
order->markAvailable() ;
return ;
}
//----------------------------------------------------------------------
static int available_order(const WcWorkOrder *orders, size_t num_orders)
{
for (size_t i = 0 ; i < num_orders ; ++i)
{
if (!orders[i].inUse())
return i ;
}
return -1 ;
}
//----------------------------------------------------------------------
bool WcProcessFile(CFile& fp, const WcParameters *params, WcProcessFileFunc *fn, ...)
{
if (!fp || !fn || !params)
return false ;
va_list args ;
va_start(args,fn) ;
ThreadPool *tpool = ThreadPool::defaultPool() ;
// allocate enough request packets to allow a queue to form for
// each thread to avoid task switches, but not so many that we end
// up wasting memory
size_t num_orders = 3 * tpool->numThreads() + 4 ;
WcWorkOrder orders[num_orders] ;
bool success = true ;
std::locale* encoding = WcCurrentCharEncoding() ;
while (!fp.eof())
{
LineBatch *lines = fp.getLines(WcLINES_PER_BATCH,params->monoSkip()) ;
if (!lines)
break ;
if (params->downcaseSource())
{
for (char* line : *lines)
{
lowercase_string(line,encoding) ;
}
}
int ordernum ;
while ((ordernum = available_order(orders,num_orders)) < 0)
{
// wait 1/20 second, then retry
this_thread::sleep_for(chrono::milliseconds(50)) ;
}
orders[ordernum].reset() ;
orders[ordernum].lines = lines ;
orders[ordernum].params = params ;
orders[ordernum].fn = fn ;
va_copy(orders[ordernum].args,args) ;
orders[ordernum].success = false ;
orders[ordernum].markUsed() ;
tpool->dispatch(&process_file_segment,&orders[ordernum],nullptr) ;
}
tpool->waitUntilIdle() ;
va_end(args) ;
return success ;
}
// end of file wcbatch.C //