View difference between Paste ID: f5f460aae and
SHOW: | | - or go back to the newest paste.
1-
1+
//Part of Laverna's Brute
2
3
#include "Director.h"
4
5
////////////////////////////////////////////
6
// Initialize our static variables /////////
7
////////////////////////////////////////////
8
9
processingPath** Director::workerPtrArray = 0;
10
int Director::numWorkers = 0;
11
12
////////////////////////////////////////////
13
14
Director::Director()
15
{
16
	numWorkers = masterThread::getNumWorkers();
17
18
	workerPtrArray = new processingPath*[numWorkers];
19
20
	for(int i = 0; i < numWorkers; i++)
21
		workerPtrArray[i] = NULL;
22
}
23
24
Director::Director(unsigned long long beginKeyspace, unsigned long long keyspaceEnd)
25
{
26
	// Used to search a certain section of the keyspace, rather than the entire thing.
27
}
28
29
Director::~Director()
30
{
31
	delete [] workerPtrArray;
32
}
33
34
void Director::operator()()
35
{
36
	boost::posix_time::seconds updateInterval(1);
37
	unsigned long long totalIterations;
38
39
	while(!masterThread::getSuccess())
40
	{
41
		boost::this_thread::sleep(updateInterval);
42
		totalIterations = 0;
43
44
		for(int i = 0; i < numWorkers; i++)
45
			std::cout << workerPtrArray[i]->getThreadID() << std::endl;
46
47
		masterThread::setIterations(totalIterations);
48
	}
49
}
50
51
processingPath* Director::getWorkerPtr(int id)
52
{
53
	return workerPtrArray[id];
54
}
55
56
void Director::manageWorker(processingPath* worker)
57
{
58
	int threadID = worker->getThreadID();
59
60
	if(workerPtrArray[threadID] == NULL)
61
		workerPtrArray[threadID] = worker;
62
	else
63
		std::cerr << "Worker ptr double assignment attempted!" << std::endl;
64
}
65
66
bool Director::reassignKeyspace(processingPath* worker)
67
{
68
	int id = 0;
69
70
	// Find the worker with the largest remaining section of the keyspace
71
	for(int i = 0; i < numWorkers; i++)
72
	{
73
		if(getRemainingKeyspace(i) > getRemainingKeyspace(id))
74
		{
75
			id = i;
76
		}
77
	}
78
79
	if(getRemainingKeyspace(id) > 0)
80
	{
81
		// Split the remaining section of the keyspace, and give it to the idle worker
82
		worker->moveKeyspaceEnd(workerPtrArray[id]->getKeyspaceEnd());
83
		workerPtrArray[id]->moveKeyspaceEnd((workerPtrArray[id]->getKeyspaceEnd() - workerPtrArray[id]->getKeyLocation()) / 2);
84
85
		worker->moveKeyspaceBegin(workerPtrArray[id]->getKeyspaceEnd() + 1);
86
		worker->moveKeylocation(worker->getKeyLocation());
87
88
		std::cout << workerPtrArray[id]->getKeyLocation() << " -- " << workerPtrArray[id]->getKeyspaceEnd() << std::endl;
89
90
		return true;
91
	}
92
	else
93
	{
94
		return false;
95
	}
96
}
97
98
void updateIterations()
99
{
100
}
101
102
unsigned long long Director::getRemainingKeyspace(int id)
103
{
104
	processingPath* worker = workerPtrArray[id];
105
	return worker->getKeyLocation() - worker->getKeyspaceBegin();
106
}
107
108
unsigned long long Director::getRemainingKeyspace(processingPath* worker)
109
{
110
	return worker->getKeyLocation() - worker->getKeyspaceBegin();
111
}