Peano
Loading...
Searching...
No Matches
EnclaveBookkeeping.cpp
Go to the documentation of this file.
2#include "EnclaveTask.h"
3
4#include "tarch/Assertions.h"
9
10#include <algorithm>
11
12
13tarch::logging::Log exahype2::EnclaveBookkeeping::_log("exahype2::EnclaveBookkeeping");
14
15
17 "exahype2::EnclaveBookkeeping::memory-allocations"
18);
19
20
25
26
28 logInfo("dumpStatistics()", "active tasks=" << exahype2::EnclaveTask::getNumberOfActiveTasks());
29
30 std::ostringstream finishedTasksMsg;
31 finishedTasksMsg << "(#" << _finishedTasks.size();
32 for (auto& p : _finishedTasks) {
33 finishedTasksMsg << "," << p.first << "->" << p.second.toString();
34 }
35 finishedTasksMsg << ")";
36 logInfo("dumpStatistics()", "finished tasks=" << finishedTasksMsg.str());
37}
38
39
41 tarch::multicore::Lock finishedTasksLock(_finishedTasksSemaphore);
42
43 if (_finishedTasks.count(number) > 0) {
44 Entry storedData = _finishedTasks.at(number);
45 _finishedTasks.erase(number);
48 } else {
49 _tasksThatHaveToBeCancelled.insert(number);
50 }
51}
52
53
56
57 tarch::multicore::Lock finishedTasksLock(_finishedTasksSemaphore);
58 assertionEquals(_finishedTasks.count(number), 1);
59 Entry storedData = _finishedTasks.at(number);
60 _finishedTasks.erase(number);
61 finishedTasksLock.free();
62
64
65 return storedData;
66}
67
68
70 logTraceInWith1Argument("waitForTaskAndDiscardResult(int)", number);
71
72 Entry storedData = waitForTaskToTerminateAndReturnResult(number);
74
75 logTraceOut("waitForTaskAndDiscardResult(int)");
76}
77
78
80 int number, double* destination, double& maxEigenvalue
81) {
82 logTraceInWith1Argument("waitForTaskToTerminateAndCopyResultOver(int,double*,double&)", number);
83
84 Entry storedData = waitForTaskToTerminateAndReturnResult(number);
85
86 if ( destination!=storedData.data ) {
87 std::copy_n(storedData.data, storedData.numberOfResultValues, destination);
89 }
90 else {
91 logDebug( "waitForTaskToTerminateAndCopyResultOver(int,double*,double&)", "target memory and enclave memory region seem to be the same, so no need to free memory on bookkeeping's side")
92 }
93
94 maxEigenvalue = storedData.maxEigenvalue;
95
97 "waitForTaskToTerminateAndCopyResultOver(int,double*,double&)", maxEigenvalue, storedData.toString()
98 );
99}
100
101
103 int taskNumber, int numberOfResultValues, double* data, double maxEigenvalue
104) {
105 logDebug("finishedTask()", "task " << taskNumber << " has terminated. Bookkeep results");
106
107 tarch::multicore::Lock lockFinishedTasks(_finishedTasksSemaphore);
108 assertionEquals1(_finishedTasks.count(taskNumber), 0, taskNumber);
109 if (_tasksThatHaveToBeCancelled.count(taskNumber)) {
110 logWarning( "finishedTask(...)", "task entry for task " << taskNumber << " does exist already" );
111 _tasksThatHaveToBeCancelled.erase(taskNumber);
112 delete[] data;
113 lockFinishedTasks.free();
115 } else {
116 logDebug( "finishedTask(...)", "bookmarked new outcome for task " << taskNumber << " at memory location " << data );
117 auto oldBucketCount = _finishedTasks.bucket_count();
118 Entry newEntry(numberOfResultValues, data, maxEigenvalue);
119 _finishedTasks.insert(std::pair<int, Entry>(taskNumber, newEntry));
120 if (_finishedTasks.bucket_count() > oldBucketCount) {
121 ::tarch::logging::Statistics::getInstance().inc(MemoryAllocationsInLookupTableIdentifier);
122 }
123 lockFinishedTasks.free();
124 }
125}
126
127
128exahype2::EnclaveBookkeeping::Entry::Entry(int numberOfResultValues_, double* data_, double maxEigenvalue_):
129 numberOfResultValues(numberOfResultValues_),
130 data(data_),
131 maxEigenvalue(maxEigenvalue_) {}
132
133
135 std::ostringstream msg;
136 msg << "(#" << numberOfResultValues << ",\\lambda_max=" << maxEigenvalue << ")";
137 return msg.str();
138}
#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 logWarning(methodName, logMacroMessageStream)
Wrapper macro around tarch::tarch::logging::Log to improve logging.
Definition Log.h:440
#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 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, bool clearAfterDatabaseDump=false)
Definition Statistics.h:107
static Statistics & getInstance()
This is not the canonical realisation of singletons as I use it usually for stats in Peano.
Create a lock around a boolean semaphore region.
Definition Lock.h:19
void free()
Free the lock.
Definition Lock.cpp:37
void waitForTask(const int taskNumber)
Wrapper around waitForTasks() with a single-element set.
Definition multicore.cpp:85
void freeMemory(void *data, MemoryLocation location, int device=accelerator::Device::HostDevice)
Free memory.
@ ManagedSharedAcceleratorDeviceMemory
To be used on host only.
Entry(int numberOfResultValues_, double *data_, double maxEigenvalue_)