-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworkerthread.cpp
More file actions
80 lines (65 loc) · 1.76 KB
/
workerthread.cpp
File metadata and controls
80 lines (65 loc) · 1.76 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
#include <omp.h>
#include "workerthread.h"
#include "world.h"
WorkerThread::WorkerThread(QObject *parent) :
QThread(parent)
{
}
void WorkerThread::setState(const QString& State)
{
m_State = State;
}
void WorkerThread::run()
{/*
QTime t;
t.start();
int t0 = t.elapsed();*/
World world;
world.set(m_State);
// Get all possible move candidates
world.getCandidates(m_CandidatesSet);
// Convert candidates set to vector
m_Canditates.clear();
for (int c : m_CandidatesSet)
m_Canditates.push_back(c);
const int iNumThreads = omp_get_num_threads();
int Min[20];
int iMin[20];
for (int i = 0; i < iNumThreads; i++)
{
Min[i] = world.count();
iMin[i] = -1;
}
#pragma omp parallel for
for (int k = 0; k < (int)m_Canditates.size(); k++)
{
int iCandidate = m_Canditates[k];
int i0 = iCandidate / Width;
int j0 = iCandidate % Width;
World TestWorld = world;
TestWorld.set(0, i0, j0, true);
for (int t = 1; t < Interval; t++)
{
for (int i = i0 - t; i <= i0 + t; i++)
for (int j = j0 - t; j <= j0 + t; j++)
TestWorld.simulate(t, i, j);
}
int iThread = omp_get_thread_num();
int Count = TestWorld.count();
if (Count < Min[iThread])
{
Min[iThread] = Count;
iMin[iThread] = iCandidate;
}
}
for (int i = 1; i < iNumThreads; i++)
{
if (Min[i] < Min[0])
{
Min[0] = Min[i];
iMin[0] = iMin[i];
}
}
if (iMin[0] != -1)
emit ready(iMin[0] % Width, iMin[0] / Width, m_State);
}