Peano 4
Loading...
Searching...
No Matches
EnclaveBookkeeping.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4
5#include "EnclaveTask.h"
6#include "tarch/Assertions.h"
13
14
15tarch::logging::Log exahype2::EnclaveBookkeeping::_log("exahype2::EnclaveBookkeeping");
16
18 "exahype2::EnclaveBookkeeping::memory-allocations"
19);
20const std::string exahype2::EnclaveBookkeeping::LookupMissesIdentifier("exahype2::EnclaveBookkeeping::lookup-misses");
21
22
27
28
30 logInfo("dumpStatistics()", "active tasks=" << exahype2::EnclaveTask::getNumberOfActiveTasks());
31
32 std::ostringstream finishedTasksMsg;
33 finishedTasksMsg << "(#" << _finishedTasks.size();
34 for (auto& p : _finishedTasks) {
35 finishedTasksMsg << "," << p.first << "->" << p.second.toString();
36 }
37 finishedTasksMsg << ")";
38 logInfo("dumpStatistics()", "finished tasks=" << finishedTasksMsg.str());
39}
40
41
43 tarch::multicore::Lock finishedTasksLock(_finishedTasksSemaphore);
44
45 if (_finishedTasks.count(number) > 0) {
46 Entry storedData = _finishedTasks.at(number);
47 _finishedTasks.erase(number);
50 } else {
51 _tasksThatHaveToBeCancelled.insert(number);
52 }
53}
54
55
57 tarch::multicore::Lock finishedTasksLock(_finishedTasksSemaphore);
58 bool isContained = _finishedTasks.count(number) > 0;
59 finishedTasksLock.free();
60
61 while (not isContained) {
62 ::tarch::logging::Statistics::getInstance().inc(LookupMissesIdentifier);
63
64 bool foundTask = ::tarch::multicore::processPendingTasks(1, false);
65 if (not foundTask) {
67 }
68
69 finishedTasksLock.lock();
70 isContained = _finishedTasks.count(number) > 0;
71 finishedTasksLock.free();
72 }
73
74 finishedTasksLock.lock();
75 assertionEquals(_finishedTasks.count(number), 1);
76 Entry storedData = _finishedTasks.at(number);
77 _finishedTasks.erase(number);
78 finishedTasksLock.free();
79
81
82 return storedData;
83}
84
85
87 logTraceInWith1Argument("waitForTaskAndDiscardResult(int)", number);
88
89 Entry storedData = waitForTaskToTerminateAndReturnResult(number);
91
92 logTraceOut("waitForTaskAndDiscardResult(int)");
93}
94
95
97 int number, double* destination, double& maxEigenvalue
98) {
99 logTraceInWith1Argument("waitForTaskToTerminateAndCopyResultOver(int,double,double&)", number);
100
101 Entry storedData = waitForTaskToTerminateAndReturnResult(number);
102
103 std::copy_n(storedData.data, storedData.numberOfResultValues, destination);
104 maxEigenvalue = storedData.maxEigenvalue;
105
108 "waitForTaskToTerminateAndCopyResultOver(int,double,double&)", maxEigenvalue, storedData.toString()
109 );
110}
111
112
114 int taskNumber, int numberOfResultValues, double* data, double maxEigenvalue
115) {
116 logDebug("finishedTask()", "task " << taskNumber << " has terminated. Bookkeep results");
117
118 tarch::multicore::Lock lockFinishedTasks(_finishedTasksSemaphore);
119 assertionEquals1(_finishedTasks.count(taskNumber), 0, taskNumber);
120 if (_tasksThatHaveToBeCancelled.count(taskNumber)) {
121 _tasksThatHaveToBeCancelled.erase(taskNumber);
122 delete[] data;
123 lockFinishedTasks.free();
125 } else {
126 auto oldBucketCount = _finishedTasks.bucket_count();
127 Entry newEntry(numberOfResultValues, data, maxEigenvalue);
128 _finishedTasks.insert(std::pair<int, Entry>(taskNumber, newEntry));
129 if (_finishedTasks.bucket_count() > oldBucketCount) {
130 ::tarch::logging::Statistics::getInstance().inc(MemoryAllocationsInLookupTableIdentifier);
131 }
132 lockFinishedTasks.free();
133 }
134}
135
136
137exahype2::EnclaveBookkeeping::Entry::Entry(int numberOfResultValues_, double* data_, double maxEigenvalue_):
138 numberOfResultValues(numberOfResultValues_),
139 data(data_),
140 maxEigenvalue(maxEigenvalue_) {}
141
142
144 std::ostringstream msg;
145 msg << "(#" << numberOfResultValues << ",\\lambda_max=" << maxEigenvalue << ")";
146 return msg.str();
147}
#define assertionEquals1(lhs, rhs, a)
#define assertionEquals(lhs, rhs)
#define logDebug(methodName, logMacroMessageStream)
Definition Log.h:50
#define logTraceOut(methodName)
Definition Log.h:379
#define logTraceOutWith2Arguments(methodName, argument0, argument1)
Definition Log.h:381
#define logTraceInWith1Argument(methodName, argument0)
Definition Log.h:370
#define logInfo(methodName, logMacroMessageStream)
Wrapper macro around tarch::tarch::logging::Log to improve logging.
Definition Log.h:411
static EnclaveBookkeeping & getInstance()
static tarch::logging::Log _log
void dumpStatistics()
For debugging only.
void waitForTaskAndDiscardResult(int number)
Wait for a task and copy outcome into destination.
static const std::string LookupMissesIdentifier
static const std::string MemoryAllocationsInLookupTableIdentifier
void finishedTask(int taskNumber, int numberOfResultValues, double *data, double maxEigenvalue)
Usually called directly by EnclaveTask.
Entry waitForTaskToTerminateAndReturnResult(int number)
Wait for a task result to become available.
void waitForTaskToTerminateAndCopyResultOver(int number, double *destination, double &maxEigenvalue)
Wait for a task and copy outcome into destination.
static int getNumberOfActiveTasks()
static void releaseTaskNumber(int number)
Log Device.
Definition Log.h:516
void inc(const std::string &identifier, double value=1.0, bool disableSampling=false)
Definition Statistics.h:92
static Statistics & getInstance()
This is not the canonical realisation of singletons as I use it usually for stats in Peano.
static Core & getInstance()
Definition Core.cpp:55
void yield()
Wrapper around backend-specific yield.
Definition Core.cpp:79
Create a lock around a boolean semaphore region.
Definition Lock.h:19
bool processPendingTasks(int maxTasks=std::numeric_limits< int >::max(), bool fifo=true)
Process a few tasks from my backlog of tasks.
Definition Tasks.cpp:240
void freeMemory(void *data, MemoryLocation location)
@ ManagedSharedAcceleratorDeviceMemory
To be used on host only.
Entry(int numberOfResultValues_, double *data_, double maxEigenvalue_)