-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLTL_BS_light.cu
More file actions
1387 lines (1148 loc) · 49.9 KB
/
LTL_BS_light.cu
File metadata and controls
1387 lines (1148 loc) · 49.9 KB
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <set>
#include <vector>
#include <chrono>
#include <fstream>
#include <iostream>
#include <thrust/copy.h>
#include <thrust/remove.h>
#include <thrust/device_ptr.h>
#include <warpcore/hash_set.cuh>
using namespace std;
const size_t maxNumOfTraces = 63;
set<char> alphabet; // Set of characters in the traces
int alphabetSize; // Size of the alphabet
int numOfTraces; // Number of traces
int numOfP; // Number of positive traces
int lenSum; // Sum of the lengths of all traces
__constant__ int d_alphabetSize;
__constant__ int d_numOfTraces;
__constant__ int d_numOfP;
__constant__ int d_lenSum;
__constant__ char d_traceLen[maxNumOfTraces]; // Length of each trace
enum class Op { Not, And, Or, Next, Finally, Globally, Until }; // Supported operators
inline
cudaError_t checkCuda(cudaError_t res) {
if (res != cudaSuccess) {
fprintf(stderr, "CUDA Runtime Error: %s\n", cudaGetErrorString(res));
assert(res == cudaSuccess);
}
return res;
}
// Making the hash values from the CSs
__device__ void generateCSHashs(
uint64_t* CS,
uint64_t& hash)
{
if (d_lenSum > 64) {
// We use a hash function on all CSs to create the final hash values
for (int i = 0; i < d_numOfTraces; ++i) {
uint64_t x = CS[i];
x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb);
x = x ^ (x >> 31);
hash ^= x;
}
}
else {
// We simply concatenate everything together
hash = CS[0];
for (int i = 1; i < d_numOfTraces; ++i) {
hash <<= d_traceLen[i - 1];
hash |= CS[i];
}
}
}
// Initialising the hashSet with the alphabet before starting the enumeration
template<class hash_set_t>
__global__ void hashSetsInit(
hash_set_t hashSet,
uint64_t* d_LTLcache)
{
const int tid = blockDim.x * blockIdx.x + threadIdx.x;
uint64_t CS[maxNumOfTraces];
for (int i = 0; i < d_numOfTraces; ++i)
CS[i] = d_LTLcache[tid * d_numOfTraces + i];
uint64_t hash{};
generateCSHashs(CS, hash);
const auto group = warpcore::cg::tiled_partition<1>(warpcore::cg::this_thread_block());
hashSet.insert(hash, group);
}
// Initialising the hashSet with the reduced LTL formulas before starting the enumeration
template<class hash_set_t>
__global__ void BSHashSetsInit(
hash_set_t hashSet,
uint64_t* d_BSCache)
{
const int tid = blockDim.x * blockIdx.x + threadIdx.x;
const auto group = warpcore::cg::tiled_partition<1>(warpcore::cg::this_thread_block());
hashSet.insert(d_BSCache[tid], group);
}
// Delete LTL formulas reducing to the same formula when switching to BS
void BSInitialisation(
int& LTLLastIdx,
uint64_t* d_BSCache)
{
thrust::device_ptr<uint64_t> new_end_ptr;
thrust::device_ptr<uint64_t> d_BSCache_ptr(d_BSCache);
new_end_ptr = thrust::remove(d_BSCache_ptr, d_BSCache_ptr + LTLLastIdx, (uint64_t)-1);
LTLLastIdx = static_cast<int>(new_end_ptr - d_BSCache_ptr);
}
// Applying op on the formulas indexed by ldx and rdx in the cache
template<Op op>
__device__ void applyOperator(
uint64_t* CS,
uint64_t* d_LTLcache,
int ldx, int rdx)
{
if constexpr (op == Op::Not) {
for (int i = 0; i < d_numOfTraces; ++i) {
uint64_t negationFixer = ((uint64_t)1 << d_traceLen[i]) - 1;
CS[i] = ~d_LTLcache[ldx * d_numOfTraces + i] & negationFixer;
}
} else if constexpr (op == Op::And) {
for (int i = 0; i < d_numOfTraces; ++i) {
CS[i] = d_LTLcache[ldx * d_numOfTraces + i] & d_LTLcache[rdx * d_numOfTraces + i];
}
} else if constexpr (op == Op::Or) {
for (int i = 0; i < d_numOfTraces; ++i) {
CS[i] = d_LTLcache[ldx * d_numOfTraces + i] | d_LTLcache[rdx * d_numOfTraces + i];
}
} else if constexpr (op == Op::Next) {
for (int i = 0; i < d_numOfTraces; ++i) {
CS[i] = d_LTLcache[ldx * d_numOfTraces + i] >> 1;
}
} else if constexpr (op == Op::Finally) {
for (int i = 0; i < d_numOfTraces; ++i) {
CS[i] = d_LTLcache[ldx * d_numOfTraces + i];
CS[i] |= CS[i] >> 1; CS[i] |= CS[i] >> 2; CS[i] |= CS[i] >> 4;
CS[i] |= CS[i] >> 8; CS[i] |= CS[i] >> 16; CS[i] |= CS[i] >> 32;
}
} else if constexpr (op == Op::Globally) {
for (int i = 0; i < d_numOfTraces; ++i) {
CS[i] = d_LTLcache[ldx * d_numOfTraces + i];
uint64_t cs = ~CS[i] & (((uint64_t)1 << d_traceLen[i]) - 1);
cs |= cs >> 1; cs |= cs >> 2; cs |= cs >> 4;
cs |= cs >> 8; cs |= cs >> 16; cs |= cs >> 32;
CS[i] &= ~cs;
}
} else if constexpr (op == Op::Until) {
for (int i = 0; i < d_numOfTraces; ++i) {
uint64_t l = d_LTLcache[ldx * d_numOfTraces + i];
uint64_t r = d_LTLcache[rdx * d_numOfTraces + i];
r |= l & (r >> 1); l &= l >> 1;
r |= l & (r >> 2); l &= l >> 2;
r |= l & (r >> 4); l &= l >> 4;
r |= l & (r >> 8); l &= l >> 8;
r |= l & (r >> 16); l &= l >> 16;
r |= l & (r >> 32);
CS[i] = r;
}
} else {
[] <bool flag = false>() { static_assert(flag, "Unhandled operator"); }();
}
}
template<Op op>
__device__ void BSApplyOperator(
uint64_t& CS,
uint64_t* d_BSCache,
int ldx, int rdx)
{
if constexpr (op == Op::Not) {
CS = ~d_BSCache[ldx] & (((uint64_t)1 << d_numOfTraces) - 1);
} else if constexpr (op == Op::And) {
CS = d_BSCache[ldx] & d_BSCache[rdx];
} else if constexpr (op == Op::Or) {
CS = d_BSCache[ldx] | d_BSCache[rdx];
} else {
[] <bool flag = false>() { static_assert(flag, "Unhandled operator"); }();
}
}
// Checking the uniqueness of the CSs
// hashSet.insert returns a negative value if CS is unique, positive if it is a duplicate
template <typename hash_set_t>
__device__ bool checkCSUniqueness(
uint64_t* CS,
hash_set_t& hashSet)
{
uint64_t hash{};
generateCSHashs(CS, hash);
const auto group = warpcore::cg::tiled_partition<1>(warpcore::cg::this_thread_block());
return hashSet.insert(hash, group) < 0;
}
template <typename hash_set_t>
__device__ bool BSCheckCSUniqueness(
uint64_t& CS,
hash_set_t& hashSet)
{
const auto group = warpcore::cg::tiled_partition<1>(warpcore::cg::this_thread_block());
return hashSet.insert(CS, group) < 0;
}
// Inserting the CSs into the temporary cache if they are unique
// Also stores the left and right indices to be able to reconstruct the formula later
// If the CS is not unique, it replaces it with -1 to mark that index as invalid
// That's why the MSB of CSs is not used and should not be used
// We also check if the CSs satisfies the positive and negative traces
__device__ void insertInCache(
bool isUnqCS,
uint64_t* CS,
int tid,
int ldx, int rdx,
uint64_t* d_temp_LTLcache,
int* d_temp_leftIdx, int* d_temp_rightIdx,
int* d_FinalLTLIdx)
{
if (isUnqCS) {
for (int i = 0; i < d_numOfTraces; ++i)
d_temp_LTLcache[tid * d_numOfTraces + i] = CS[i];
d_temp_leftIdx[tid] = ldx; d_temp_rightIdx[tid] = rdx;
bool found = true;
for (int i = 0; found && i < d_numOfP; ++i) if (!(CS[i] & 1)) found = false;
for (int i = d_numOfP; found && i < d_numOfTraces; ++i) if (CS[i] & 1) found = false;
if (found) atomicCAS(d_FinalLTLIdx, -1, tid);
} else {
for (int i = 0; i < d_numOfTraces; ++i)
d_temp_LTLcache[tid * d_numOfTraces + i] = (uint64_t)-1;
d_temp_leftIdx[tid] = -1; d_temp_rightIdx[tid] = -1;
}
}
__device__ void BSInsertInCache(
bool isUnqCS,
uint64_t& CS,
int tid,
int ldx, int rdx,
uint64_t* d_temp_BSCache,
int* d_temp_BSLeftIdx, int* d_temp_BSRightIdx,
int* d_FinalBSIdx)
{
if (isUnqCS) {
d_temp_BSCache[tid] = CS;
d_temp_BSLeftIdx[tid] = ldx; d_temp_BSRightIdx[tid] = rdx;
uint64_t mask = ((uint64_t)1 << d_numOfP) - 1;
if (CS == mask) atomicCAS(d_FinalBSIdx, -1, tid);
} else {
d_temp_BSCache[tid] = (uint64_t)-1;
d_temp_BSLeftIdx[tid] = -1; d_temp_BSRightIdx[tid] = -1;
}
}
// Combining all together : create CSs, check uniqueness, insert in cache
// There is a special case for Until since it is not commutative
template<Op op, class hash_set_t>
__global__ void processOperator(
const int idx1, const int idx2,
const int idx3, const int idx4,
uint64_t* d_LTLcache, uint64_t* d_temp_LTLcache,
int* d_temp_leftIdx, int* d_temp_rightIdx,
hash_set_t hashSet,
int* d_FinalLTLIdx)
{
const int realTid = (blockDim.x * blockIdx.x + threadIdx.x);
const int tid = (op == Op::Until) ? (realTid * 2) : realTid;
constexpr bool isUnary = (op == Op::Not || op == Op::Next || op == Op::Finally || op == Op::Globally);
int maxTid = isUnary ? (idx2 - idx1 + 1) : ((idx4 - idx3 + 1) * (idx2 - idx1 + 1));
if (tid < maxTid) {
int ldx = isUnary ? (idx1 + tid) : (idx1 + tid / (idx4 - idx3 + 1));
int rdx = isUnary ? 0 : (idx3 + tid % (idx4 - idx3 + 1));
uint64_t CS[maxNumOfTraces];
applyOperator<op>(CS, d_LTLcache, ldx, rdx);
bool isUnqCS = checkCSUniqueness(CS, hashSet);
insertInCache(isUnqCS, CS, tid, ldx, rdx, d_temp_LTLcache, d_temp_leftIdx, d_temp_rightIdx, d_FinalLTLIdx);
if (op == Op::Until) {
applyOperator<Op::Until>(CS, d_LTLcache, rdx, ldx);
bool isUnqCS = checkCSUniqueness(CS, hashSet);
insertInCache(isUnqCS, CS, tid + 1, rdx, ldx, d_temp_LTLcache, d_temp_leftIdx, d_temp_rightIdx, d_FinalLTLIdx);
}
}
}
template<Op op, class hash_set_t>
__global__ void BSProcessOperator(
const int idx1, const int idx2,
const int idx3, const int idx4,
uint64_t* d_BSCache, uint64_t* d_temp_BSCache,
int* d_temp_BSLeftIdx, int* d_temp_BSRightIdx,
hash_set_t hashSet,
int* d_FinalBSIdx)
{
const int tid = blockDim.x * blockIdx.x + threadIdx.x;
constexpr bool isUnary = op == Op::Not;
int maxTid = isUnary ? (idx2 - idx1 + 1) : ((idx4 - idx3 + 1) * (idx2 - idx1 + 1));
if (tid < maxTid) {
int ldx = isUnary ? (idx1 + tid) : (idx1 + tid / (idx4 - idx3 + 1));
int rdx = isUnary ? 0 : (idx3 + tid % (idx4 - idx3 + 1));
uint64_t CS;
BSApplyOperator<op>(CS, d_BSCache, ldx, rdx);
bool isUnqCS = BSCheckCSUniqueness(CS, hashSet);
BSInsertInCache(isUnqCS, CS, tid, ldx, rdx, d_temp_BSCache, d_temp_BSLeftIdx, d_temp_BSRightIdx, d_FinalBSIdx);
}
}
// Transfering the unique CSs from temporary to main cache
// This is where we delete the duplicates marked with -1
// If the main cache is full, we return true to stop the enumeration
bool storeUnqLTLs(
int N,
int& LTLLastIdx,
const int LTLCacheCapacity,
uint64_t* d_LTLcache, uint64_t* d_temp_LTLcache,
int* d_leftIdx, int* d_temp_leftIdx,
int* d_rightIdx, int* d_temp_rightIdx)
{
thrust::device_ptr<uint64_t> new_end_ptr;
thrust::device_ptr<uint64_t> d_LTLcache_ptr(d_LTLcache + numOfTraces * LTLLastIdx);
thrust::device_ptr<uint64_t> d_temp_LTLcache_ptr(d_temp_LTLcache);
thrust::device_ptr<int> d_leftIdx_ptr(d_leftIdx + LTLLastIdx);
thrust::device_ptr<int> d_rightIdx_ptr(d_rightIdx + LTLLastIdx);
thrust::device_ptr<int> d_temp_leftIdx_ptr(d_temp_leftIdx);
thrust::device_ptr<int> d_temp_rightIdx_ptr(d_temp_rightIdx);
new_end_ptr =
thrust::remove(d_temp_LTLcache_ptr, d_temp_LTLcache_ptr + numOfTraces * N, (uint64_t)-1);
thrust::remove(d_temp_leftIdx_ptr, d_temp_leftIdx_ptr + N, -1);
thrust::remove(d_temp_rightIdx_ptr, d_temp_rightIdx_ptr + N, -1);
int newLTLs = static_cast<int>(new_end_ptr - d_temp_LTLcache_ptr) / numOfTraces;
bool lastRound = false;
if (LTLLastIdx + newLTLs > LTLCacheCapacity) {
newLTLs = LTLCacheCapacity - LTLLastIdx;
lastRound = true;
}
thrust::copy_n(d_temp_LTLcache_ptr, numOfTraces * newLTLs, d_LTLcache_ptr);
thrust::copy_n(d_temp_leftIdx_ptr, newLTLs, d_leftIdx_ptr);
thrust::copy_n(d_temp_rightIdx_ptr, newLTLs, d_rightIdx_ptr);
LTLLastIdx += newLTLs;
return lastRound;
}
bool storeUnqBSs(
int N,
int& BSLastIdx,
const int BSCacheCapacity,
uint64_t* d_BSCache, uint64_t* d_temp_BSCache,
int* d_BSLeftIdx, int* d_temp_BSLeftIdx,
int* d_BSRightIdx, int* d_temp_BSRightIdx)
{
thrust::device_ptr<uint64_t> new_end_ptr;
thrust::device_ptr<uint64_t> d_BSCache_ptr(d_BSCache + BSLastIdx);
thrust::device_ptr<uint64_t> d_temp_BSCache_ptr(d_temp_BSCache);
thrust::device_ptr<int> d_BSLeftIdx_ptr(d_BSLeftIdx + BSLastIdx);
thrust::device_ptr<int> d_BSRightIdx_ptr(d_BSRightIdx + BSLastIdx);
thrust::device_ptr<int> d_temp_BSLeftIdx_ptr(d_temp_BSLeftIdx);
thrust::device_ptr<int> d_temp_BSRightIdx_ptr(d_temp_BSRightIdx);
new_end_ptr = thrust::remove(d_temp_BSCache_ptr, d_temp_BSCache_ptr + N, (uint64_t)-1);
thrust::remove(d_temp_BSLeftIdx_ptr, d_temp_BSLeftIdx_ptr + N, -1);
thrust::remove(d_temp_BSRightIdx_ptr, d_temp_BSRightIdx_ptr + N, -1);
int newBSs = static_cast<int>(new_end_ptr - d_temp_BSCache_ptr);
bool lastRound = false;
if (BSLastIdx + newBSs > BSCacheCapacity) {
newBSs = BSCacheCapacity - BSLastIdx;
lastRound = true;
}
thrust::copy_n(d_temp_BSCache_ptr, N, d_BSCache_ptr);
thrust::copy_n(d_temp_BSLeftIdx_ptr, N, d_BSLeftIdx_ptr);
thrust::copy_n(d_temp_BSRightIdx_ptr, N, d_BSRightIdx_ptr);
BSLastIdx += newBSs;
return lastRound;
}
// Bringing back the left and right indices from device to host
__global__ void generateResIndices(
const int index,
const int* d_leftIdx, const int* d_rightIdx,
int* d_FinalLTLIdx)
{
int resIdx = 0;
while (d_FinalLTLIdx[resIdx] != -1) resIdx++;
int queue[100];
queue[0] = index;
int head = 0;
int tail = 1;
while (head < tail) {
int ltl = queue[head];
int l = d_leftIdx[ltl];
int r = d_rightIdx[ltl];
d_FinalLTLIdx[resIdx++] = ltl;
d_FinalLTLIdx[resIdx++] = l;
d_FinalLTLIdx[resIdx++] = r;
if (l >= d_alphabetSize) queue[tail++] = l;
if (r >= d_alphabetSize) queue[tail++] = r;
head++;
}
}
__global__ void BSGenerateResIndices(
const int index,
const int LTLLastIdx,
const int* d_BSLeftIdx, const int* d_BSRightIdx,
int* d_FinalBSIdx)
{
int resIdx = 0;
while (d_FinalBSIdx[resIdx] != -1) resIdx++;
int queue[100];
queue[0] = index;
int head = 0;
int tail = 1;
while (head < tail) {
int ltl = queue[head];
int l = d_BSLeftIdx[ltl];
int r = d_BSRightIdx[ltl];
d_FinalBSIdx[resIdx++] = ltl;
d_FinalBSIdx[resIdx++] = l;
d_FinalBSIdx[resIdx++] = r;
if (l >= LTLLastIdx) queue[tail++] = l;
if (r >= LTLLastIdx) queue[tail++] = r;
head++;
}
}
// Convert indices to string representation of the formula
string LTLToString(
int index,
map<int, pair<int, int>>& indicesMap,
const int* LTLStartPoints)
{
if (index < alphabetSize) {
string s(1, *next(alphabet.begin(), index));
return s;
}
int i = 0;
while (index >= LTLStartPoints[i]) { i++; }
i--;
if (i % 7 == 0) {
string res = LTLToString(indicesMap[index].first, indicesMap, LTLStartPoints);
return "~(" + res + ")";
}
if (i % 7 == 1) {
string left = LTLToString(indicesMap[index].first, indicesMap, LTLStartPoints);
string right = LTLToString(indicesMap[index].second, indicesMap, LTLStartPoints);
return "(" + left + ")" + "&" + "(" + right + ")";
}
if (i % 7 == 2) {
string left = LTLToString(indicesMap[index].first, indicesMap, LTLStartPoints);
string right = LTLToString(indicesMap[index].second, indicesMap, LTLStartPoints);
return "(" + left + ")" + "|" + "(" + right + ")";
}
if (i % 7 == 3) {
string res = LTLToString(indicesMap[index].first, indicesMap, LTLStartPoints);
return "X(" + res + ")";
}
if (i % 7 == 4) {
string res = LTLToString(indicesMap[index].first, indicesMap, LTLStartPoints);
return "F(" + res + ")";
}
if (i % 7 == 5) {
string res = LTLToString(indicesMap[index].first, indicesMap, LTLStartPoints);
return "G(" + res + ")";
}
string left = LTLToString(indicesMap[index].first, indicesMap, LTLStartPoints);
string right = LTLToString(indicesMap[index].second, indicesMap, LTLStartPoints);
return "(" + left + ")" + "U" + "(" + right + ")";
}
// Constructing the string representation of the formula
string LTLString(
const int LTLLastIdx,
const int* LTLStartPoints,
const int* d_leftIdx, const int* d_rightIdx,
const int* d_temp_leftIdx, const int* d_temp_rightIdx,
const int FinalLTLIdx)
{
int* LIdx = new int[1];
int* RIdx = new int[1];
checkCuda(cudaMemcpy(LIdx, d_temp_leftIdx + FinalLTLIdx, sizeof(int), cudaMemcpyDeviceToHost));
checkCuda(cudaMemcpy(RIdx, d_temp_rightIdx + FinalLTLIdx, sizeof(int), cudaMemcpyDeviceToHost));
int* d_resIndices;
checkCuda(cudaMalloc(&d_resIndices, 100 * sizeof(int)));
thrust::device_ptr<int> d_resIndices_ptr(d_resIndices);
thrust::fill(d_resIndices_ptr, d_resIndices_ptr + 100, -1);
if (*LIdx >= alphabetSize) generateResIndices << <1, 1 >> > (*LIdx, d_leftIdx, d_rightIdx, d_resIndices);
if (*RIdx >= alphabetSize) generateResIndices << <1, 1 >> > (*RIdx, d_leftIdx, d_rightIdx, d_resIndices);
int resIndices[100];
checkCuda(cudaMemcpy(resIndices, d_resIndices, 100 * sizeof(int), cudaMemcpyDeviceToHost));
map<int, pair<int, int>> indicesMap;
indicesMap.insert(make_pair(INT_MAX - 1, make_pair(*LIdx, *RIdx)));
int i = 0;
while (resIndices[i] != -1 && i + 2 < 100) {
int ltl = resIndices[i];
int l = resIndices[i + 1];
int r = resIndices[i + 2];
indicesMap.insert(make_pair(ltl, make_pair(l, r)));
i += 3;
}
if (i + 2 >= 100) return "Size of the output is too big";
cudaFree(d_resIndices);
return LTLToString(INT_MAX - 1, indicesMap, LTLStartPoints);
}
string BSToString(
int index,
map<int, pair<int, int>>& indicesMap,
const uint64_t LTLLastIdx,
const int* LTLStartPoints, const int* BSStartPoints,
const int* d_leftIdx, const int* d_rightIdx)
{
if (index < LTLLastIdx) {
LTLString(LTLLastIdx, LTLStartPoints, d_leftIdx, d_rightIdx, d_leftIdx, d_rightIdx, index);
}
int i = 0;
while (index >= BSStartPoints[i]) { i++; }
i--;
if (i % 3 == 0) {
string res = BSToString(indicesMap[index].first, indicesMap, LTLLastIdx, LTLStartPoints, BSStartPoints, d_leftIdx, d_rightIdx);
return "~(" + res + ")";
}
if (i % 3 == 1) {
string left = BSToString(indicesMap[index].first, indicesMap, LTLLastIdx, LTLStartPoints, BSStartPoints, d_leftIdx, d_rightIdx);
string right = BSToString(indicesMap[index].second, indicesMap, LTLLastIdx, LTLStartPoints, BSStartPoints, d_leftIdx, d_rightIdx);
return "(" + left + ")" + "&" + "(" + right + ")";
}
else {
string left = BSToString(indicesMap[index].first, indicesMap, LTLLastIdx, LTLStartPoints, BSStartPoints, d_leftIdx, d_rightIdx);
string right = BSToString(indicesMap[index].second, indicesMap, LTLLastIdx, LTLStartPoints, BSStartPoints, d_leftIdx, d_rightIdx);
return "(" + left + ")" + "|" + "(" + right + ")";
}
}
string BSString(
const int FinalBSIdx,
const int LTLLastIdx,
const int* LTLStartPoints, const int* BSStartPoints,
const int* d_leftIdx, const int* d_rightIdx,
const int* d_BSLeftIdx, const int* d_BSRightIdx,
const int* d_temp_BSLeftIdx, const int* d_temp_BSRightIdx)
{
auto* LIdx = new int[1];
auto* RIdx = new int[1];
checkCuda(cudaMemcpy(LIdx, d_temp_BSLeftIdx + FinalBSIdx, sizeof(int), cudaMemcpyDeviceToHost));
checkCuda(cudaMemcpy(RIdx, d_temp_BSRightIdx + FinalBSIdx, sizeof(int), cudaMemcpyDeviceToHost));
int* d_resIndices;
checkCuda(cudaMalloc(&d_resIndices, 100 * sizeof(int)));
thrust::device_ptr<int> d_resIndices_ptr(d_resIndices);
thrust::fill(d_resIndices_ptr, d_resIndices_ptr + 100, -1);
if (*LIdx >= LTLLastIdx) BSGenerateResIndices << <1, 1 >> > (*LIdx, LTLLastIdx, d_leftIdx, d_rightIdx, d_resIndices);
if (*RIdx >= LTLLastIdx) BSGenerateResIndices << <1, 1 >> > (*RIdx, LTLLastIdx, d_leftIdx, d_rightIdx, d_resIndices);
int resIndices[100];
checkCuda(cudaMemcpy(resIndices, d_resIndices, 100 * sizeof(int), cudaMemcpyDeviceToHost));
map<int, pair<int, int>> indicesMap;
indicesMap.insert(make_pair(INT_MAX - 1, make_pair(*LIdx, *RIdx)));
int i = 0;
while (resIndices[i] != -1 && i + 2 < 100) {
int ltl = resIndices[i];
int l = resIndices[i + 1];
int r = resIndices[i + 2];
indicesMap.insert(make_pair(ltl, make_pair(l, r)));
i += 3;
}
cudaFree(d_resIndices);
if (i + 2 >= 100) return "Size of the output is too big";
else return BSToString(INT_MAX - 1, indicesMap, LTLLastIdx, LTLStartPoints, BSStartPoints, d_leftIdx, d_rightIdx);
}
// Transfer the LTLCache in the BSCache
__global__ void transfer(
const int LTLLastIdx,
const uint64_t* d_LTLcache,
uint64_t* d_BSCache)
{
int tid = blockDim.x * blockIdx.x + threadIdx.x;
if (tid < LTLLastIdx) {
uint64_t CS{};
for (int i = 0; i < d_numOfTraces; ++i)
CS |= (d_LTLcache[tid * d_numOfTraces + i] & (uint64_t)1) << i;
d_BSCache[tid] = CS;
}
}
// Enumerate all BS formulas
string BS(
const unsigned short BSMaxCost,
int LTLLastIdx,
int* LTLStartPoints,
int LTLCost,
uint64_t* d_BSCache,
const int BSCacheCapacity,
int* d_leftIdx, int* d_rightIdx) {
// ------------------------------------
// Memory allocation and initialisation
// ------------------------------------
int BSLastIdx = LTLLastIdx;
cout << LTLLastIdx << endl;
const int temp_BSCacheCapacity = BSCacheCapacity / 2;
// 3 for ~, &, |
int* BSStartPoints = new int[(BSMaxCost + 2) * 3]();
for (int i = 2; i <= LTLCost; ++i) BSStartPoints[i * 3] = LTLStartPoints[i * 7];
int* d_FinalBSIdx;
int* FinalBSIdx = new int[1]; *FinalBSIdx = -1;
checkCuda(cudaMalloc(&d_FinalBSIdx, sizeof(int)));
checkCuda(cudaMemcpy(d_FinalBSIdx, FinalBSIdx, sizeof(int), cudaMemcpyHostToDevice));
uint64_t* d_temp_BSCache;
int* d_BSLeftIdx, * d_BSRightIdx, * d_temp_BSLeftIdx, * d_temp_BSRightIdx;
checkCuda(cudaMalloc(&d_BSLeftIdx, BSCacheCapacity * sizeof(int)));
checkCuda(cudaMalloc(&d_BSRightIdx, BSCacheCapacity * sizeof(int)));
checkCuda(cudaMalloc(&d_temp_BSLeftIdx, temp_BSCacheCapacity * sizeof(int)));
checkCuda(cudaMalloc(&d_temp_BSRightIdx, temp_BSCacheCapacity * sizeof(int)));
checkCuda(cudaMalloc(&d_temp_BSCache, temp_BSCacheCapacity * sizeof(uint64_t)));
using hash_set_t = warpcore::HashSet<
uint64_t, // Key type
uint64_t(0) - 1, // Empty key
uint64_t(0) - 2, // Tombstone key
warpcore::probing_schemes::QuadraticProbing<warpcore::hashers::MurmurHash <uint64_t>>>;
hash_set_t hashSet(2 * BSCacheCapacity);
BSHashSetsInit<hash_set_t> << <1, LTLLastIdx >> > (hashSet, d_BSCache);
// ---------------------------
// Enumeration of the next BSs
// ---------------------------
int BSCost;
uint64_t allBSs{};
for (BSCost = LTLCost; BSCost <= BSMaxCost; ++BSCost) {
// Negation (~)
if (BSCost > 1) {
int idx1 = BSStartPoints[(BSCost - 1) * 3];
int idx2 = BSStartPoints[BSCost * 3] - 1;
int N = idx2 - idx1 + 1;
if (N) {
int x = idx1, y;
do {
y = x + min(temp_BSCacheCapacity - 1, idx2 - x);
N = y - x + 1;
printf("Cost %-2d | (~) | AllBSs: %-11lu | StoredBSs: %-10d | ToBeChecked: %-10d \n", BSCost, allBSs, BSLastIdx, N);
int numBlocks = (N + 1023) / 1024;
BSProcessOperator<Op::Not, hash_set_t> << <numBlocks, 1024 >> > (
x, y, 0, 0, d_BSCache, d_temp_BSCache,
d_temp_BSLeftIdx, d_temp_BSRightIdx, hashSet, d_FinalBSIdx);
checkCuda(cudaPeekAtLastError());
checkCuda(cudaMemcpy(FinalBSIdx, d_FinalBSIdx, sizeof(int), cudaMemcpyDeviceToHost));
allBSs += N;
if (*FinalBSIdx != -1) { BSStartPoints[BSCost * 3 + 1] = INT_MAX; goto exitEnumeration; }
bool lastRound = storeUnqBSs(
N, BSLastIdx, BSCacheCapacity, d_BSCache, d_temp_BSCache,
d_BSLeftIdx, d_BSRightIdx, d_temp_BSLeftIdx, d_temp_BSRightIdx);
if (lastRound) goto exitEnumeration;
x = y + 1;
} while (y < idx2);
}
}
BSStartPoints[BSCost * 3 + 1] = BSLastIdx;
// Intersection (&)
for (int i = 1; 2 * i <= BSCost - 1; ++i) {
int idx1 = BSStartPoints[i * 3];
int idx2 = BSStartPoints[(i + 1) * 3] - 1;
int idx3 = BSStartPoints[(BSCost - i - 1) * 3];
int idx4 = BSStartPoints[(BSCost - i) * 3] - 1;
int N = (idx4 - idx3 + 1) * (idx2 - idx1 + 1);
if (N) {
int x = idx3, y;
do {
y = x + min(temp_BSCacheCapacity / (idx2 - idx1 + 1) - 1, idx4 - x);
N = (y - x + 1) * (idx2 - idx1 + 1);
printf("Cost %-2d | (&) | AllBSs: %-11lu | StoredBSs: %-10d | ToBeChecked: %-10d \n", BSCost, allBSs, BSLastIdx, N);
int numBlocks = (N + 1023) / 1024;
BSProcessOperator<Op::And, hash_set_t> << <numBlocks, 1024 >> > (
idx1, idx2, x, y, d_BSCache, d_temp_BSCache,
d_temp_BSLeftIdx, d_temp_BSRightIdx, hashSet, d_FinalBSIdx);
checkCuda(cudaPeekAtLastError());
checkCuda(cudaMemcpy(FinalBSIdx, d_FinalBSIdx, sizeof(int), cudaMemcpyDeviceToHost));
allBSs += N;
if (*FinalBSIdx != -1) { BSStartPoints[BSCost * 3 + 2] = INT_MAX; goto exitEnumeration; }
bool lastRound = storeUnqBSs(
N, BSLastIdx, BSCacheCapacity, d_BSCache, d_temp_BSCache,
d_BSLeftIdx, d_BSRightIdx, d_temp_BSLeftIdx, d_temp_BSRightIdx);
if (lastRound) goto exitEnumeration;
x = y + 1;
} while (y < idx4);
}
}
BSStartPoints[BSCost * 3 + 2] = BSLastIdx;
// Union (|)
for (int i = 1; 2 * i <= BSCost - 1; ++i) {
int idx1 = BSStartPoints[i * 3];
int idx2 = BSStartPoints[(i + 1) * 3] - 1;
int idx3 = BSStartPoints[(BSCost - i - 1) * 3];
int idx4 = BSStartPoints[(BSCost - i) * 3] - 1;
int N = (idx4 - idx3 + 1) * (idx2 - idx1 + 1);
if (N) {
int x = idx3, y;
do {
y = x + min(temp_BSCacheCapacity / (idx2 - idx1 + 1) - 1, idx4 - x);
N = (y - x + 1) * (idx2 - idx1 + 1);
printf("Cost %-2d | (|) | AllBSs: %-11lu | StoredBSs: %-10d | ToBeChecked: %-10d \n", BSCost, allBSs, BSLastIdx, N);
int numBlocks = (N + 1023) / 1024;
BSProcessOperator<Op::Or, hash_set_t> << <numBlocks, 1024 >> > (
idx1, idx2, x, y, d_BSCache, d_temp_BSCache,
d_temp_BSLeftIdx, d_temp_BSRightIdx, hashSet, d_FinalBSIdx);
checkCuda(cudaPeekAtLastError());
checkCuda(cudaMemcpy(FinalBSIdx, d_FinalBSIdx, sizeof(int), cudaMemcpyDeviceToHost));
allBSs += N;
if (*FinalBSIdx != -1) { BSStartPoints[BSCost * 3 + 3] = INT_MAX; goto exitEnumeration; }
bool lastRound = storeUnqBSs(
N, BSLastIdx, BSCacheCapacity, d_BSCache, d_temp_BSCache,
d_BSLeftIdx, d_BSRightIdx, d_temp_BSLeftIdx, d_temp_BSRightIdx);
if (lastRound) goto exitEnumeration;
x = y + 1;
} while (y < idx4);
}
}
BSStartPoints[BSCost * 3 + 3] = BSLastIdx;
}
exitEnumeration:
string output;
if (*FinalBSIdx != -1) {
output = BSString(*FinalBSIdx, LTLLastIdx, LTLStartPoints, BSStartPoints,
d_leftIdx, d_rightIdx, d_BSLeftIdx, d_BSRightIdx, d_temp_BSLeftIdx, d_temp_BSRightIdx);
} else {
output = "not_found";
}
cudaFree(d_BSCache); cudaFree(d_temp_BSCache);
cudaFree(d_BSLeftIdx); cudaFree(d_temp_BSLeftIdx);
cudaFree(d_BSRightIdx); cudaFree(d_temp_BSRightIdx);
cudaFree(d_FinalBSIdx);
return output;
}
string LTLI(
const unsigned short LTLMaxCost,
const unsigned short BSMaxCost,
const vector<vector<string>> pos,
const vector<vector<string>> neg) {
// --------------------------------
// Generating and checking alphabet
// --------------------------------
int maxLenOfTraces{};
auto* traceLen = new char[numOfTraces];
int TLIdx{};
for (const auto& trace : pos) {
lenSum += trace.size();
traceLen[TLIdx++] = trace.size();
if (trace.size() > maxLenOfTraces) maxLenOfTraces = trace.size();
} for (const auto& trace : neg) {
lenSum += trace.size();
traceLen[TLIdx++] = trace.size();
if (trace.size() > maxLenOfTraces) maxLenOfTraces = trace.size();
}
if (numOfTraces > maxNumOfTraces || maxLenOfTraces > sizeof(uint64_t) * 8 - 1) {
printf("In this version, The input can have:\n");
printf("1) At most %zu traces. It is currently %d.\n", maxNumOfTraces, numOfTraces);
printf("2) Max(len(trace)) = %d. It is currently %d.\n", static_cast<int>(sizeof(uint64_t) * 8 - 1), maxLenOfTraces);
return "see_the_error";
}
checkCuda(cudaMemcpyToSymbol(d_lenSum, &lenSum, sizeof(int)));
checkCuda(cudaMemcpyToSymbol(d_traceLen, traceLen, numOfTraces * sizeof(char)));
uint64_t* LTLcache = new uint64_t[alphabetSize * numOfTraces];
int LTLLastIdx{}; uint64_t allLTLs{};
printf("Cost %-2d | (A) | AllLTLs: %-11lu | StoredLTLs: %-10d | ToBeChecked: %-10d \n", 1, allLTLs, 0, alphabetSize);
int index{};
for (int i = 0; i < alphabetSize; ++i) {
bool found = true;
string ch(1, *next(alphabet.begin(), i));
for (const auto& trace : pos) {
uint64_t binTrace{};
uint64_t idx = 1;
for (const auto& token : trace) {
for (const auto& c : token)
if (c == ch[0]) { binTrace |= idx; break; }
idx <<= 1;
}
LTLcache[index++] = binTrace;
if (!(binTrace & 1)) found = false;
}
for (const auto& trace : neg) {
uint64_t binTrace{};
uint64_t idx = 1;
for (const auto& token : trace) {
for (const auto& c : token)
if (c == ch[0]) { binTrace |= idx; break; }
idx <<= 1;
}
LTLcache[index++] = binTrace;
if (binTrace & 1) found = false;
}
allLTLs++; LTLLastIdx++;
if (found) return ch;
}
// ------------------------------------
// Memory allocation and initialisation
// ------------------------------------
int maxAllocationSize;
cudaDeviceGetAttribute(&maxAllocationSize, cudaDevAttrMaxPitch, 0);
const int LTLCacheCapacity = maxAllocationSize / (numOfTraces * sizeof(uint64_t));
const int temp_LTLCacheCapacity = LTLCacheCapacity / 2;
// 7 for ~, &, |, X, F, G, U
int* LTLStartPoints = new int[(LTLMaxCost + 2) * 7]();
LTLStartPoints[1 * 7 + 6] = LTLLastIdx;
LTLStartPoints[2 * 7] = LTLLastIdx;
int* d_FinalLTLIdx;
auto* FinalLTLIdx = new int[1]; *FinalLTLIdx = -1;
checkCuda(cudaMalloc(&d_FinalLTLIdx, sizeof(int)));
checkCuda(cudaMemcpy(d_FinalLTLIdx, FinalLTLIdx, sizeof(int), cudaMemcpyHostToDevice));
uint64_t* d_LTLcache, * d_temp_LTLcache;
int* d_leftIdx, * d_rightIdx, * d_temp_leftIdx, * d_temp_rightIdx;
checkCuda(cudaMalloc(&d_leftIdx, LTLCacheCapacity * sizeof(int)));
checkCuda(cudaMalloc(&d_rightIdx, LTLCacheCapacity * sizeof(int)));
checkCuda(cudaMalloc(&d_temp_leftIdx, temp_LTLCacheCapacity * sizeof(int)));
checkCuda(cudaMalloc(&d_temp_rightIdx, temp_LTLCacheCapacity * sizeof(int)));
checkCuda(cudaMalloc(&d_LTLcache, LTLCacheCapacity * numOfTraces * sizeof(uint64_t)));
checkCuda(cudaMemcpy(d_LTLcache, LTLcache, alphabetSize * numOfTraces * sizeof(uint64_t), cudaMemcpyHostToDevice));
checkCuda(cudaMalloc(&d_temp_LTLcache, temp_LTLCacheCapacity * numOfTraces * sizeof(uint64_t)));
using hash_set_t = warpcore::HashSet<
uint64_t, // key type
uint64_t(0) - 1, // empty key
uint64_t(0) - 2, // tombstone key
warpcore::probing_schemes::QuadraticProbing<warpcore::hashers::MurmurHash <uint64_t>>>;
hash_set_t hashSet(2 * LTLCacheCapacity);
hashSetsInit<hash_set_t> << <1, alphabetSize >> > (hashSet, d_LTLcache);
// ----------------------------
// Enumeration of the next LTLs
// ----------------------------
int LTLCost;
int idx1; int idx2; int idx3; int idx4;
int N;
for (LTLCost = 2; LTLCost <= LTLMaxCost; ++LTLCost) {
// Negation (~)
idx1 = LTLStartPoints[(LTLCost - 1) * 7];
idx2 = LTLStartPoints[LTLCost * 7] - 1;
N = idx2 - idx1 + 1;
if (N) {
int x = idx1, y;
do {
y = x + min(temp_LTLCacheCapacity - 1, idx2 - x);
N = (y - x + 1);
printf("Cost %-2d | (~) | AllLTLs: %-11lu | StoredLTLs: %-10d | ToBeChecked: %-10d \n", LTLCost, allLTLs, LTLLastIdx, N);
int numBlocks = (N + 1023) / 1024;
processOperator<Op::Not, hash_set_t> << <numBlocks, 1024 >> > (
x, y, 0, 0, d_LTLcache, d_temp_LTLcache,
d_temp_leftIdx, d_temp_rightIdx, hashSet, d_FinalLTLIdx);
checkCuda(cudaPeekAtLastError());
checkCuda(cudaMemcpy(FinalLTLIdx, d_FinalLTLIdx, sizeof(int), cudaMemcpyDeviceToHost));
allLTLs += N;
if (*FinalLTLIdx != -1) { LTLStartPoints[LTLCost * 7 + 1] = INT_MAX; goto exitEnumeration; }
bool lastRound = storeUnqLTLs(
N, LTLLastIdx, LTLCacheCapacity, d_LTLcache, d_temp_LTLcache,
d_leftIdx, d_rightIdx, d_temp_leftIdx, d_temp_rightIdx);
if (lastRound) goto exitEnumeration;
x = y + 1;
} while (y < idx2);
}
LTLStartPoints[LTLCost * 7 + 1] = LTLLastIdx;
// Intersection (&)
for (int i = 1; 2 * i <= LTLCost - 1; ++i) {