-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cc
237 lines (208 loc) · 6.69 KB
/
input.cc
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "input.h"
#include "rapidxml/rapidxml_utils.hpp"
#include "rapidxml/rapidxml_print.hpp"
bool Input::s_bCompactXml = false;
Input::Input()
{
m_iAction = -1;
m_pAction = NULL;
}
Input::~Input()
{
clear();
}
void Input::clear() {
m_iAction = -1;
if (m_pAction != NULL)
delete m_pAction;
m_pAction = NULL;
}
bool Input::load(const char* pFilename) {
using namespace rapidxml;
using namespace strings;
if (pFilename[0] == '/')
m_sFileName = pFilename;
else {
char dir[500];
if (getcwd(dir, sizeof(dir)) != NULL) {
m_sFileName = dir;
m_sFileName.append("/").append(pFilename);
} else {
puts("Cannot get the current working directory.");
return false;
}
}
printf(ReadingFile, pFilename);
file<> xmlFile(pFilename);
return loadStr(xmlFile.data());
}
bool Input::loadStr(char* xml) {
using namespace rapidxml;
xml_document<> doc;
try {
doc.parse<parse_validate_closing_tags>(xml);
} catch (parse_error e) {
printf("XML parsing error: %s\n", e.what());
// printf(e.where());
return false;
}
return load(doc);
}
const char* Input::s_agml = "agml";
const char* Input::s_attributeNames[] = {"version", "xmlns" , "xmlns:xsi" , "xsi:schemaLocation"};
const bool Input::s_required[] = {true , false , false , false };
const char* Input::s_defaultValues[] = {"" , "http://sourceforge.net/projects/atomicglobalmin/", "http://www.w3.org/2001/XMLSchema-instance", "http://sourceforge.net/projects/atomicglobalmin/ agml.xsd"};
const char* Input::s_actionElementNames[] = {strings::xSimulatedAnnealing, strings::xRandomSearch, strings::xParticleSwarmOptimization, strings::xGeneticAlgorithm, strings::xBatch};
bool Input::load(rapidxml::xml_document<> &doc)
{
using namespace rapidxml;
using namespace strings;
clear();
const xml_node<>* pElem = doc.first_node();
if (!pElem || strcmp(s_agml,pElem->name()) != 0) {
printf(ElementNotFound, s_agml);
return false;
}
if (pElem->next_sibling()) {
puts(OneRootElement);
return false;
}
const char** rootAttributeValues;
XsdAttributeUtil rootAttUtil(s_attributeNames, s_required, s_defaultValues);
if (!rootAttUtil.process(pElem))
return false;
rootAttributeValues = rootAttUtil.getAllAttributes();
m_sVersion = rootAttributeValues[0];
XsdElementUtil agmlUtil(XSD_CHOICE, s_actionElementNames);
if (!agmlUtil.process(pElem))
return false;
pElem = agmlUtil.getChoiceElement();
m_iAction = agmlUtil.getChoiceElementIndex();
switch (m_iAction) {
case SIMULATED_ANNEALING:
m_pAction = new SimulatedAnnealing(this);
break;
case RANDOM_SEARCH:
case PARTICLE_SWARM_OPTIMIZATION:
case GENETIC_ALGORITHM:
return false;
case BATCH:
m_pAction = new Batch(this);
break;
}
return m_pAction->load(pElem);
}
bool Input::save(rapidxml::xml_document<> &doc)
{
using namespace rapidxml;
using namespace strings;
xml_node<>* decl = doc.allocate_node(node_declaration);
decl->append_attribute(doc.allocate_attribute("version", "1.0"));
decl->append_attribute(doc.allocate_attribute("encoding", "utf-8"));
doc.append_node(decl);
xml_node<>* agml = doc.allocate_node(node_element, s_agml);
doc.append_node(agml);
agml->append_attribute(doc.allocate_attribute(s_attributeNames[0], m_sVersion.c_str()));
agml->append_attribute(doc.allocate_attribute(s_attributeNames[1], s_defaultValues[1]));
agml->append_attribute(doc.allocate_attribute(s_attributeNames[2], s_defaultValues[2]));
agml->append_attribute(doc.allocate_attribute(s_attributeNames[3], s_defaultValues[3]));
if (NULL != m_pAction) {
const char* actionElementNames[] = {xSimulatedAnnealing, xRandomSearch, xParticleSwarmOptimization, xGeneticAlgorithm, xBatch};
xml_node<>* action = doc.allocate_node(node_element, actionElementNames[m_iAction]);
agml->append_node(action);
if (!m_pAction->save(doc, action))
return false;
}
return true;
}
bool Input::save(const char* pFilename)
{
m_sFileName = pFilename;
return save();
}
bool Input::save()
{
using namespace rapidxml;
using namespace strings;
printf(WritingFile, m_sFileName.c_str());
xml_document<> doc;
if (!save(doc))
return false;
std::string temporaryFileName;
temporaryFileName.append(m_sFileName).append(".").append(pTemp);
FILE *f = fopen(temporaryFileName.c_str(), "w");
if (f == NULL) {
printf(ErrorOpeningFile, temporaryFileName.c_str());
return false;
}
std::string s;
if (s_bCompactXml)
print(std::back_inserter(s), doc, print_no_indenting);
else
print(std::back_inserter(s), doc, 0);
fputs(s.c_str(), f);
fclose(f);
std::string mvCommand;
mvCommand.append("mv ").append(temporaryFileName).append(" ").append(m_sFileName);
return !system(mvCommand.c_str());
}
bool Input::save(std::string &buffer) {
rapidxml::xml_document<> doc;
if (!save(doc))
return false;
print(std::back_inserter(buffer), doc, 0);
return true;
}
bool Input::run(const char* fileName) {
int iRank;
int iMpiProcesses;
MPI_Comm_rank(MPI_COMM_WORLD, &iRank);
MPI_Comm_size(MPI_COMM_WORLD, &iMpiProcesses);
bool normalTermination = false;
if (0 == iRank) {
int size = 0;
if (!load(fileName)) {
MPI_Bcast(&size, 1, MPI_INT, 0, MPI_COMM_WORLD);
return false;
}
if (0 == m_pAction->m_iEnergyCalculations && m_pAction->energyXml.m_bExternalEnergy
&& m_pAction->energyXml.m_externalEnergyXml.m_sResultsDir.length() > 0
&& !FileUtils::directoryEmpty(m_pAction->energyXml.m_externalEnergyXml.m_sResultsDir.c_str())) {
printf(strings::ResultsShouldBeEmpty, m_pAction->energyXml.m_externalEnergyXml.m_sResultsDir.c_str());
MPI_Bcast(&size, 1, MPI_INT, 0, MPI_COMM_WORLD);
return false;
}
if (iMpiProcesses >= 2) {
#if MPI_DEBUG
printf("Master sending the loaded xml to %d other slave mpi processes.\n", (iMpiProcesses-1));
#endif
std::string xml;
save(xml);
size = xml.length()+1;
MPI_Bcast(&size, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast((void*)xml.c_str(), xml.length()+1, MPI_CHAR, 0, MPI_COMM_WORLD);
}
normalTermination = m_pAction->runMaster();
normalTermination = normalTermination && m_pAction->cleanupRun();
} else {
char* xml;
int size;
MPI_Bcast(&size, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (0 == size)
return false;
xml = new char[size];
MPI_Bcast(xml, size, MPI_CHAR, 0, MPI_COMM_WORLD);
#if MPI_DEBUG
int iRank;
MPI_Comm_rank(MPI_COMM_WORLD, &iRank);
printf("Slave %d received loaded xml from process 0.\n", iRank);
#endif
bool success = loadStr(xml);
delete[] xml; // Always delete
if (!success)
return false;
normalTermination = m_pAction->runSlave();
normalTermination = normalTermination && m_pAction->cleanupRun();
}
return normalTermination;
}