-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathWolframLanguageAPI.cpp
460 lines (390 loc) · 18 KB
/
WolframLanguageAPI.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include "WolframLanguageAPI.hpp"
// NOLINTNEXTLINE(build/c++11)
#include <chrono> // <chrono> is banned in Chromium, so cpplint flags it https://stackoverflow.com/a/33653404/905496
#include <limits>
#include <memory>
#include <random>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "HypergraphSubstitutionSystem.hpp"
namespace SetReplace {
namespace {
// These are global variables that keep all systems returned to Wolfram Language until they are no longer referenced.
// Pointers are not returned directly for security reasons.
using SystemID = mint;
// We use a pointer here because map key insertion (hypergraphManageInstance) is separate from map value insertion
// (hypergraphInitialize). Until the value is inserted, the set is nullptr.
std::unordered_map<SystemID, std::unique_ptr<HypergraphSubstitutionSystem>> hypergraphSubstitutionSystems_;
/** @brief Either acquires or a releases a set, depending on the mode.
*/
void hypergraphSubstitutionSystemManageInstance([[maybe_unused]] WolframLibraryData libData, mbool mode, mint id) {
if (mode == 0) {
hypergraphSubstitutionSystems_.emplace(id, nullptr);
} else {
hypergraphSubstitutionSystems_.erase(id);
}
}
mint getData(const mint* data, const mint& length, const mint& index) {
if (index >= length || index < 0) {
throw LIBRARY_FUNCTION_ERROR;
} else {
return data[index];
}
}
std::vector<AtomsVector> getNextHypergraph(const mint& tensorLength, const mint* tensorData, mint* startReadIndex) {
const auto getDataFunc = [&tensorData, &tensorLength, startReadIndex]() -> mint {
return getData(tensorData, tensorLength, (*startReadIndex)++);
};
const mint hypergraphLength = getDataFunc();
std::vector<AtomsVector> atomVectors(hypergraphLength);
for (mint tokenIndex = 0; tokenIndex < hypergraphLength; ++tokenIndex) {
const mint tokenLength = getDataFunc();
auto& currentToken = atomVectors[tokenIndex];
currentToken.reserve(tokenLength);
for (mint atomIndex = 0; atomIndex < tokenLength; ++atomIndex) {
currentToken.emplace_back(static_cast<Atom>(getDataFunc()));
}
}
return atomVectors;
}
std::vector<Rule> getRules(WolframLibraryData libData, MTensor rulesTensor, MTensor selectionFunctionsTensor) {
const mint rulesTensorLength = libData->MTensor_getFlattenedLength(rulesTensor);
const mint* rulesTensorData = libData->MTensor_getIntegerData(rulesTensor);
const mint selectionFunctionsTensorLength = libData->MTensor_getFlattenedLength(selectionFunctionsTensor);
const mint* selectionFunctionsTensorData = libData->MTensor_getIntegerData(selectionFunctionsTensor);
mint rulesReadIndex = 0;
const auto getRulesData = [&rulesTensorData, &rulesTensorLength, &rulesReadIndex]() -> mint {
return getData(rulesTensorData, rulesTensorLength, rulesReadIndex++);
};
const mint rulesCount = getRulesData();
if (rulesCount != selectionFunctionsTensorLength) {
throw LIBRARY_FUNCTION_ERROR;
}
std::vector<Rule> rules;
rules.reserve(rulesCount);
for (mint ruleIndex = 0; ruleIndex < rulesCount; ++ruleIndex) {
if (getRulesData() != 2) {
throw LIBRARY_FUNCTION_ERROR;
} else {
rules.emplace_back(Rule{getNextHypergraph(rulesTensorLength, rulesTensorData, &rulesReadIndex),
getNextHypergraph(rulesTensorLength, rulesTensorData, &rulesReadIndex),
static_cast<EventSelectionFunction>(
getData(selectionFunctionsTensorData, selectionFunctionsTensorLength, ruleIndex))});
}
}
return rules;
}
std::vector<AtomsVector> getHypergraph(WolframLibraryData libData, MTensor setTensor) {
mint readIndex = 0;
return getNextHypergraph(
libData->MTensor_getFlattenedLength(setTensor), libData->MTensor_getIntegerData(setTensor), &readIndex);
}
HypergraphMatcher::OrderingSpec getOrderingSpec(WolframLibraryData libData, MTensor orderingSpecTensor) {
mint tensorLength = libData->MTensor_getFlattenedLength(orderingSpecTensor);
mint* tensorData = libData->MTensor_getIntegerData(orderingSpecTensor);
HypergraphMatcher::OrderingSpec result;
result.reserve(tensorLength);
for (mint i = 0; i < tensorLength; i += 2) {
result.emplace_back(
std::make_pair(static_cast<HypergraphMatcher::OrderingFunction>(getData(tensorData, tensorLength, i)),
static_cast<HypergraphMatcher::OrderingDirection>(getData(tensorData, tensorLength, i + 1))));
}
return result;
}
// Seed is passed as two uint16_t because LibraryLink does not support unsigned ints, which becomes a problem on 32-bit
// architectures
uint32_t getSeed(WolframLibraryData libData, MTensor seedTensor) {
mint tensorLength = libData->MTensor_getFlattenedLength(seedTensor);
if (tensorLength != 2) throw LIBRARY_FUNCTION_ERROR;
mint* tensorData = libData->MTensor_getIntegerData(seedTensor);
uint32_t result = 0;
for (mint i = 0; i < tensorLength; ++i) {
result = (1 << 16) * result + static_cast<uint32_t>(getData(tensorData, tensorLength, i));
}
return result;
}
constexpr int64_t wlStepLimitDisabled = -1;
HypergraphSubstitutionSystem::StepSpecification getStepSpec(WolframLibraryData libData, MTensor stepsTensor) {
mint tensorLength = libData->MTensor_getFlattenedLength(stepsTensor);
constexpr mint specLength = 5;
if (tensorLength != specLength) {
throw LIBRARY_FUNCTION_ERROR;
} else {
mint* tensorData = libData->MTensor_getIntegerData(stepsTensor);
std::vector<int64_t> stepSpecElements(specLength);
for (mint k = 0; k < specLength; ++k) {
stepSpecElements[k] = static_cast<int64_t>(getData(tensorData, specLength, k));
if (stepSpecElements[k] == wlStepLimitDisabled) {
stepSpecElements[k] = HypergraphSubstitutionSystem::stepLimitDisabled;
}
if (stepSpecElements[k] < 0) throw LIBRARY_FUNCTION_ERROR;
}
return HypergraphSubstitutionSystem::StepSpecification{
stepSpecElements[0], stepSpecElements[1], stepSpecElements[2], stepSpecElements[3], stepSpecElements[4]};
}
}
MTensor putHypergraph(const std::vector<AtomsVector>& tokens, WolframLibraryData libData) {
// count + atoms list pointer for each token + an extra pointer at the end to the element one past the end
size_t tensorLength = 1 + (tokens.size() + 1);
// Atoms are next, positions to which are referenced in each token spec.
// This is where the first atom will be located.
size_t atomsPointer = tensorLength + 1;
for (const auto& token : tokens) {
tensorLength += token.size();
}
const mint dimensions[1] = {static_cast<mint>(tensorLength)};
MTensor output;
libData->MTensor_new(MType_Integer, 1, dimensions, &output);
mint writeIndex = 0;
mint position[1];
const auto appendToTensor = [libData, &writeIndex, &position, &output](const std::vector<mint>& numbers) {
for (const auto number : numbers) {
position[0] = ++writeIndex;
libData->MTensor_setInteger(output, position, number);
}
};
appendToTensor({static_cast<mint>(tokens.size())});
for (const auto& token : tokens) {
appendToTensor({static_cast<mint>(atomsPointer)});
atomsPointer += token.size();
}
appendToTensor({static_cast<mint>(atomsPointer)});
for (const auto& token : tokens) {
// Cannot do static_cast due to 32-bit Windows support
appendToTensor(std::vector<mint>(token.begin(), token.end()));
}
return output;
}
MTensor putEvents(const std::vector<Event>& events, WolframLibraryData libData) {
// ruleID + input tokens pointer + output tokens pointer + generation
// add fake rule ID and generation at the end to specify the length of the last token
size_t tensorLength = 1 + 4 * (events.size() + 1);
size_t inputsPointer = tensorLength + 1;
size_t outputsPointer = tensorLength + 1;
for (const auto& event : events) {
tensorLength += event.inputTokens.size() + event.outputTokens.size();
outputsPointer += event.inputTokens.size();
}
const mint dimensions[1] = {static_cast<mint>(tensorLength)};
MTensor output;
libData->MTensor_new(MType_Integer, 1, dimensions, &output);
mint writeIndex = 0;
mint position[1];
const auto appendToTensor = [libData, &writeIndex, &position, &output](const std::vector<mint>& numbers) {
for (const auto number : numbers) {
position[0] = ++writeIndex;
libData->MTensor_setInteger(output, position, number);
}
};
appendToTensor({static_cast<mint>(events.size())});
for (const auto& event : events) {
appendToTensor({static_cast<mint>(event.rule),
static_cast<mint>(inputsPointer),
static_cast<mint>(outputsPointer),
static_cast<mint>(event.generation)});
inputsPointer += event.inputTokens.size();
outputsPointer += event.outputTokens.size();
}
// Put fake event at the end so that the length of final token can be determined on WL side.
constexpr TokenID fakeRule = -2;
constexpr Generation fakeGeneration = -1;
appendToTensor({static_cast<mint>(fakeRule),
static_cast<mint>(inputsPointer),
static_cast<mint>(outputsPointer),
static_cast<mint>(fakeGeneration)});
for (const auto& event : events) {
// Cannot do static_cast due to 32-bit Windows support
appendToTensor(std::vector<mint>(event.inputTokens.begin(), event.inputTokens.end()));
outputsPointer += event.inputTokens.size();
}
for (const auto& event : events) {
// Cannot do static_cast due to 32-bit Windows support
appendToTensor(std::vector<mint>(event.outputTokens.begin(), event.outputTokens.end()));
}
return output;
}
int hypergraphSubstitutionSystemInitialize(WolframLibraryData libData,
mint argc,
const MArgument* argv,
[[maybe_unused]] MArgument result) {
if (argc != 8) {
return LIBRARY_FUNCTION_ERROR;
}
SystemID thisSystemID;
std::vector<Rule> rules;
std::vector<AtomsVector> initialTokens;
// WL passes wlStepLimitDisabled (-1) instead of HypergraphSubstitutionSystem::stepLimitDisabled (max int64) for
// infinity because passing 64-bit ints is not supported on 32-bit systems in LibraryLink
int64_t wlMaxDestroyerEvents;
uint64_t maxDestroyerEvents;
HypergraphMatcher::OrderingSpec orderingSpec;
HypergraphMatcher::EventDeduplication eventDeduplication;
unsigned int randomSeed;
try {
thisSystemID = MArgument_getInteger(argv[0]);
rules = getRules(libData, MArgument_getMTensor(argv[1]), MArgument_getMTensor(argv[2]));
initialTokens = getHypergraph(libData, MArgument_getMTensor(argv[3]));
wlMaxDestroyerEvents = MArgument_getInteger(argv[4]);
maxDestroyerEvents = wlMaxDestroyerEvents == wlStepLimitDisabled ? HypergraphSubstitutionSystem::stepLimitDisabled
: wlMaxDestroyerEvents;
orderingSpec = getOrderingSpec(libData, MArgument_getMTensor(argv[5]));
eventDeduplication = static_cast<HypergraphMatcher::EventDeduplication>(MArgument_getInteger(argv[6]));
randomSeed = getSeed(libData, MArgument_getMTensor(argv[7]));
} catch (...) {
return LIBRARY_FUNCTION_ERROR;
}
try {
hypergraphSubstitutionSystems_[thisSystemID] = std::make_unique<HypergraphSubstitutionSystem>(
rules, initialTokens, maxDestroyerEvents, orderingSpec, eventDeduplication, randomSeed);
} catch (...) {
return LIBRARY_FUNCTION_ERROR;
}
return LIBRARY_NO_ERROR;
}
std::function<bool()> shouldAbort(WolframLibraryData libData) {
return [libData]() { return static_cast<bool>(libData->AbortQ()); };
}
HypergraphSubstitutionSystem& hypergraphSubstitutionSystemFromID(const SystemID id) {
const auto setIDIterator = hypergraphSubstitutionSystems_.find(id);
if (setIDIterator != hypergraphSubstitutionSystems_.end()) {
return *setIDIterator->second;
} else {
throw LIBRARY_FUNCTION_ERROR;
}
}
int hypergraphSubstitutionSystemReplace(WolframLibraryData libData,
mint argc,
MArgument* argv,
[[maybe_unused]] MArgument result) {
if (argc != 3) {
return LIBRARY_FUNCTION_ERROR;
}
const SystemID systemID = MArgument_getInteger(argv[0]);
HypergraphSubstitutionSystem::StepSpecification stepSpec;
try {
stepSpec = getStepSpec(libData, MArgument_getMTensor(argv[1]));
} catch (...) {
return LIBRARY_FUNCTION_ERROR;
}
const double timeConstraintWL = MArgument_getReal(argv[2]);
auto timeConstraint = HypergraphSubstitutionSystem::timeConstraintDisabled;
if (timeConstraintWL > 0) {
timeConstraint = std::chrono::duration_cast<std::chrono::steady_clock::duration>(
std::chrono::duration<double>(timeConstraintWL));
}
try {
hypergraphSubstitutionSystemFromID(systemID).replace(stepSpec, shouldAbort(libData), timeConstraint);
} catch (...) {
return LIBRARY_FUNCTION_ERROR;
}
return LIBRARY_NO_ERROR;
}
int hypergraphSubstitutionSystemTokens(WolframLibraryData libData, mint argc, MArgument* argv, MArgument result) {
if (argc != 1) {
return LIBRARY_FUNCTION_ERROR;
}
const SystemID systemID = MArgument_getInteger(argv[0]);
std::vector<AtomsVector> tokens;
try {
tokens = hypergraphSubstitutionSystemFromID(systemID).tokens();
} catch (...) {
return LIBRARY_FUNCTION_ERROR;
}
MArgument_setMTensor(result, putHypergraph(tokens, libData));
return LIBRARY_NO_ERROR;
}
int hypergraphSubstitutionSystemEvents(WolframLibraryData libData, mint argc, MArgument* argv, MArgument result) {
if (argc != 1) {
return LIBRARY_FUNCTION_ERROR;
}
const SystemID systemID = MArgument_getInteger(argv[0]);
try {
const auto& events = hypergraphSubstitutionSystemFromID(systemID).events();
MArgument_setMTensor(result, putEvents(events, libData));
} catch (...) {
return LIBRARY_FUNCTION_ERROR;
}
return LIBRARY_NO_ERROR;
}
int hypergraphSubstitutionSystemMaxCompleteGeneration(WolframLibraryData libData,
mint argc,
MArgument* argv,
MArgument result) {
if (argc != 1) {
return LIBRARY_FUNCTION_ERROR;
}
const SystemID systemID = MArgument_getInteger(argv[0]);
Generation maxCompleteGeneration;
try {
maxCompleteGeneration = hypergraphSubstitutionSystemFromID(systemID).maxCompleteGeneration(shouldAbort(libData));
} catch (...) {
return LIBRARY_FUNCTION_ERROR;
}
MArgument_setInteger(result, maxCompleteGeneration);
return LIBRARY_NO_ERROR;
}
int hypergraphSubstitutionSystemTerminationReason([[maybe_unused]] WolframLibraryData,
mint argc,
MArgument* argv,
MArgument result) {
if (argc != 1) {
return LIBRARY_FUNCTION_ERROR;
}
const SystemID systemID = MArgument_getInteger(argv[0]);
HypergraphSubstitutionSystem::TerminationReason terminationReason;
try {
terminationReason = hypergraphSubstitutionSystemFromID(systemID).terminationReason();
} catch (...) {
return LIBRARY_FUNCTION_ERROR;
}
MArgument_setInteger(result, static_cast<int>(terminationReason));
return LIBRARY_NO_ERROR;
}
} // namespace
} // namespace SetReplace
EXTERN_C mint WolframLibrary_getVersion() { return WolframLibraryVersion; }
EXTERN_C int WolframLibrary_initialize(WolframLibraryData libData) {
return (*libData->registerLibraryExpressionManager)("SetReplace",
SetReplace::hypergraphSubstitutionSystemManageInstance);
}
EXTERN_C void WolframLibrary_uninitialize(WolframLibraryData libData) {
(*libData->unregisterLibraryExpressionManager)("SetReplace");
}
EXTERN_C int hypergraphSubstitutionSystemInitialize(WolframLibraryData libData,
mint argc,
MArgument* argv,
MArgument result) {
return SetReplace::hypergraphSubstitutionSystemInitialize(libData, argc, argv, result);
}
EXTERN_C int hypergraphSubstitutionSystemReplace(WolframLibraryData libData,
mint argc,
MArgument* argv,
MArgument result) {
return SetReplace::hypergraphSubstitutionSystemReplace(libData, argc, argv, result);
}
EXTERN_C int hypergraphSubstitutionSystemTokens(WolframLibraryData libData,
mint argc,
MArgument* argv,
MArgument result) {
return SetReplace::hypergraphSubstitutionSystemTokens(libData, argc, argv, result);
}
EXTERN_C int hypergraphSubstitutionSystemEvents(WolframLibraryData libData,
mint argc,
MArgument* argv,
MArgument result) {
return SetReplace::hypergraphSubstitutionSystemEvents(libData, argc, argv, result);
}
EXTERN_C int hypergraphSubstitutionSystemMaxCompleteGeneration(WolframLibraryData libData,
mint argc,
MArgument* argv,
MArgument result) {
return SetReplace::hypergraphSubstitutionSystemMaxCompleteGeneration(libData, argc, argv, result);
}
EXTERN_C int hypergraphSubstitutionSystemTerminationReason(WolframLibraryData libData,
mint argc,
MArgument* argv,
MArgument result) {
return SetReplace::hypergraphSubstitutionSystemTerminationReason(libData, argc, argv, result);
}