forked from hschwenk/cslm-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTools.cpp
223 lines (196 loc) · 6.01 KB
/
Tools.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* This file is part of the continuous space language and translation model toolkit
* for statistical machine translation and large vocabulary speech recognition.
*
* Copyright 2015, Holger Schwenk, LIUM, University of Le Mans, France
*
* The CSLM toolkit is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3 as
* published by the Free Software Foundation
*
* This library 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*
*/
#include <iostream>
#include <sstream>
#include <signal.h>
#include <cstdarg>
#include "Tools.h"
#ifdef BLAS_CUDA
# include <cuda_runtime_api.h>
#endif
extern void GpuUnlock(); // forward declaration
void Error(void)
{
GpuUnlock();
exit(1);
}
//******************************************
void Error(const char *txt)
{
ErrorN(txt);
}
//******************************************
void Error(const char *txt, int ipar)
{
fprintf(stderr,"ERROR: ");
fprintf(stderr,txt,ipar);
fprintf(stderr,"\n");
GpuUnlock();
exit(1);
}
//******************************************
__attribute__((noreturn))
void vErrorN(const char* msg, va_list args)
{
char message[ERROR_MSG_SIZE];
#if !defined(ULTRIX) && !defined(_MINGW_) && !defined(WIN32)
vsnprintf(message, ERROR_MSG_SIZE,msg,args);
#else
vsprintf(message,msg,args);
#endif
cerr <<" ERROR: "<<message<<endl;
#ifdef BLAS_CUDA
int dev;
cudaGetDevice(&dev);
cerr <<" Current GPU device: "<<dev<<endl;
#endif
GpuUnlock();
exit(1);
}
//******************************************
void ErrorN(const char* msg, ...){
va_list args;
va_start(args,msg);
vErrorN(msg, args);
va_end(args);
}
//******************************************
/**
* parses parameters written in one line like "param1=val1 param2=val2"
* @param isInput input stream
* @param voParams out vector of parameters
*/
void ParseParametersLine(istream& isInput, vector<boost::program_options::option>& voParams)
{
string sRead;
short iReadStep = 0;
vector<boost::program_options::option>::iterator iParamIter;
do {
sRead.clear();
if (iReadStep < 3)
// read next token (name, equal character or start of value)
isInput >> sRead;
else {
// read end of value in quotes
stringbuf sbRead;
isInput.get(sbRead, '\"');
if (isInput.peek() != char_traits<char>::eof())
sbRead.sputc(isInput.get());
sRead = sbRead.str();
}
if (sRead.empty() || ('#' == sRead[0]) || isInput.bad())
// stop in case of no more data, start of comment or stream error
break;
size_t stPos = 0;
size_t stLen = sRead.length();
// read equal character
if (iReadStep == 1) {
if (sRead[stPos] == '=') {
stPos++;
iReadStep = 2; // next step: read parameter value
if (stPos >= stLen)
continue;
}
else
iReadStep = 0; // next step: read new option
}
// read parameter name
if (iReadStep <= 0) {
size_t stNPos = sRead.find('=');
iParamIter = voParams.insert(voParams.end(), boost::program_options::option());
iParamIter->string_key = sRead.substr(stPos, stNPos - stPos);
iParamIter->value.push_back(string());
stPos = stNPos;
if (stPos != string::npos) {
stPos++;
iReadStep = 2; // next step: read parameter value
if (stPos >= stLen)
continue;
}
else {
iReadStep = 1; // next step: read equal character
continue;
}
}
// read parameter value
if (iReadStep == 2) {
iReadStep = 0; // next loop: read new option (if value is not in quotes)
size_t stNPos = stLen;
if (sRead[stPos] == '\"') { // parameter value in quotes
stPos++;
if ((stPos < stLen) && (sRead[stLen - 1] == '\"'))
stNPos--;
else
iReadStep = 3; // next loop: end reading value in quotes
}
iParamIter->value.back() = sRead.substr(stPos, stNPos - stPos);
continue;
}
// end reading value in quotes
if (iReadStep >= 3) {
iParamIter->value.back().append(sRead, stPos, stLen - stPos - 1);
iReadStep = 0; // next step: read new parameter
}
} while (!(sRead.empty() || isInput.bad()));
}
//******************************************
int ReadInt(ifstream &inpf, const string &name, int minval,int maxval)
{
string buf;
inpf >> buf;
if (buf!=name)
ErrorN("FileRead: found field '%s' while looking for '%s'", buf.c_str(), name.c_str());
int val;
inpf >> val;
if (val<minval || val>maxval)
ErrorN("FileRead: values for %s must be in [%d,%d]", name.c_str(), minval, maxval);
return val;
}
//******************************************
#ifdef BLAS_CUDA
void DebugMachInp(string txt, REAL *iptr, int idim, int odim, int eff_bsize) {
Error("debugging of input data not supported for CUDA"); }
void DebugMachOutp(string txt, REAL *optr, int idim, int odim, int eff_bsize) {
Error("debugging of output data not supported for CUDA"); }
#else
void DebugMachInp(string txt, REAL *iptr, int idim, int odim, int eff_bsize)
{
cout <<"\n" << txt;
printf(" %dx%d bs=%d: input\n",idim,odim,eff_bsize);
for (int bs=0;bs<eff_bsize;bs++) {
printf("%3d: ",bs);
for (int i=0;i<idim;i++) printf(" %4.2f", *iptr++);
printf("\n");
}
}
//******************************************
void DebugMachOutp(string txt, REAL *optr, int idim, int odim, int eff_bsize)
{
cout <<"\n" << txt;
printf(" %dx%d bs=%d: output\n",idim,odim,eff_bsize);
for (int bs=0;bs<eff_bsize;bs++) {
printf("%3d: ",bs);
for (int o=0;o<odim;o++) printf(" %4.2f", *optr++);
printf("\n");
}
}
#endif