-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cu
More file actions
138 lines (123 loc) · 5.13 KB
/
utils.cu
File metadata and controls
138 lines (123 loc) · 5.13 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
#include <iostream>
#include <numeric>
#include <vector>
using std::vector;
#include <set>
using std::set;
#include <fstream>
#include <functional>
#include "definitions.cuh"
#include "runConfig.cuh"
#include "cudaConfig.cuh"
static set<int> getDifferences(int subgraphVertices[SUBGRAPH_VERTEX_COUNT]) {
set<int> differences;
for (int i = 0; i < SUBGRAPH_VERTEX_COUNT - 1; ++i) {
for (int j = i + 1; j < SUBGRAPH_VERTEX_COUNT; ++j) {
differences.insert(subgraphVertices[j] - subgraphVertices[i]);
}
}
return differences;
}
static unsigned long long getCondition(int subgraphVertices[SUBGRAPH_VERTEX_COUNT]) {
set<int> differences = getDifferences(subgraphVertices);
unsigned long long condition = 0;
for (int difference : differences) {
condition |= (1ull << (VERTEX_COUNT - difference - 1));
}
return condition;
}
static void markSubconditions(vector<unsigned long long> &conditions, const int currentConditionIndex) {
#pragma omp parallel for default(none) shared(conditions) firstprivate(currentConditionIndex)
for (int subconditionCandidateIndex = currentConditionIndex + 1; subconditionCandidateIndex < conditions.size(); ++subconditionCandidateIndex) {
const unsigned long long ¤tCondition = conditions[currentConditionIndex];
unsigned long long &subconditionCandidate = conditions[subconditionCandidateIndex];
if ((currentCondition & subconditionCandidate) == currentCondition) {
subconditionCandidate |= SUBCONDITION_FLAG;
}
}
}
static void removeExcessiveConditions(vector<unsigned long long> &conditions) {
std::cout << "Marking subconditions..." << std::endl;
for (int currentConditionIndex = 0; currentConditionIndex < conditions.size() - 1; ++currentConditionIndex) {
if (conditions[currentConditionIndex] & SUBCONDITION_FLAG) {
continue;
}
markSubconditions(conditions, currentConditionIndex);
}
std::cout << "Erasing excessive conditions..." << std::endl;
auto erasedStart = std::partition(conditions.begin(), conditions.end(), [](unsigned long long condition) {
return !(condition & SUBCONDITION_FLAG);
});
conditions.erase(erasedStart, conditions.end());
std::cout << "Excessive conditions erased." << std::endl;
}
void initConditions(vector<unsigned long long> &conditions) {
int subgraphVertices[SUBGRAPH_VERTEX_COUNT];
std::iota(std::begin(subgraphVertices), std::end(subgraphVertices), 0);
constexpr int lastVertexIndex = SUBGRAPH_VERTEX_COUNT - 1;
constexpr int firstSubgraphVertexMax = VERTEX_COUNT - SUBGRAPH_VERTEX_COUNT;
std::cout << "Generating conditions..." << std::endl;
int currentVertexIndex = lastVertexIndex;
while (true) {
unsigned long long condition = getCondition(subgraphVertices);
conditions.push_back(condition);
if (subgraphVertices[0] == firstSubgraphVertexMax) {
break;
}
++subgraphVertices[currentVertexIndex];
while (subgraphVertices[currentVertexIndex] == firstSubgraphVertexMax + currentVertexIndex + 1) {
--currentVertexIndex;
++subgraphVertices[currentVertexIndex];
}
while (currentVertexIndex < lastVertexIndex) {
++currentVertexIndex;
subgraphVertices[currentVertexIndex] = subgraphVertices[currentVertexIndex - 1] + 1;
}
}
std::cout << "Conditions generated." << std::endl;
removeExcessiveConditions(conditions);
}
void checkForSolution(unsigned long long *solutions_h, const unsigned long long *solutions_d, cudaStream_t stream) {
if (stream != nullptr) {
cudaMemcpyAsync(solutions_h, solutions_d, TOTAL_THREADS_COUNT * sizeof(unsigned long long), cudaMemcpyDeviceToHost, stream);
cudaStreamSynchronize(stream);
} else {
cudaMemcpy(solutions_h, solutions_d, TOTAL_THREADS_COUNT * sizeof(unsigned long long), cudaMemcpyDeviceToHost);
}
bool found = false;
for (int i = 0; i < TOTAL_THREADS_COUNT; ++i) {
if (solutions_h[i] != 0) {
std::cout << "Solution by thread[" << i << "]: " << solutions_h[i] << std::endl;
for (int j = VERTEX_COUNT - 2; j >= 0; --j) {
std::cout << ((solutions_h[i] >> j) & 1);
}
std::cout << std::endl;
found = true;
}
}
if (!found) {
std::cout << "No solution found" << std::endl;
}
}
[[maybe_unused]]
void readConditions(vector<unsigned long long> &conditions, const char *filename) {
std::ifstream file(filename);
unsigned long long condition;
while (file >> condition) {
conditions.push_back(condition);
}
file.close();
}
[[maybe_unused]]
void printConditions(vector<unsigned long long> &conditions, std::ostream &out, bool binary) {
for (auto condition: conditions) {
if (binary) {
for (int i = VERTEX_COUNT - 2; i >= 0; --i) {
out << ((condition >> i) & 1);
}
} else {
out << condition;
}
out << std::endl;
}
}