Skip to content

Commit

Permalink
Merge pull request #180 from nlesc-recruit/174-time
Browse files Browse the repository at this point in the history
Replace openmp time by chrono
  • Loading branch information
loostrum authored Nov 15, 2024
2 parents fa52290 + a15b7c4 commit d17d3ca
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
10 changes: 6 additions & 4 deletions host/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
find_package(OpenMP REQUIRED)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

add_library(PowerSensor src/PowerSensor.cc src/sensors.cc)
add_executable(psconfig src/psconfig.cc)
add_executable(pstest src/pstest.cc)
add_executable(psrun src/psrun.cc)

target_link_libraries(PowerSensor Threads::Threads)
set_property(TARGET PowerSensor PROPERTY POSITION_INDEPENDENT_CODE ON)

include_directories(include/)
target_link_libraries(psconfig PowerSensor OpenMP::OpenMP_CXX)
target_link_libraries(psconfig PowerSensor)
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0)
target_link_libraries(psconfig stdc++fs)
endif()
target_link_libraries(pstest PowerSensor OpenMP::OpenMP_CXX)
target_link_libraries(psrun PowerSensor OpenMP::OpenMP_CXX)
target_link_libraries(pstest PowerSensor)
target_link_libraries(psrun PowerSensor)

install(TARGETS psconfig pstest psrun DESTINATION bin)
install(FILES include/PowerSensor.hpp include/Semaphore.hpp DESTINATION include)
Expand Down
7 changes: 4 additions & 3 deletions host/include/PowerSensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <inttypes.h>

#include <array>
#include <chrono>
#include <thread>
#include <fstream>
#include <queue>
Expand Down Expand Up @@ -31,7 +32,7 @@ struct State {
/** @brief Voltage, per sensor */
std::array<double, MAX_PAIRS> voltage;
/** @brief Timestamp */
double timeAtRead;
std::chrono::time_point<std::chrono::high_resolution_clock> timeAtRead;
};

double Joules(const State &firstState, const State &secondState, int pairID = -1 /* default: all sensor pairs */);
Expand Down Expand Up @@ -97,7 +98,7 @@ class PowerSensor {
Semaphore threadStarted;
std::thread* thread;
mutable std::mutex mutex, dumpFileMutex;
double startTime;
std::chrono::time_point<std::chrono::high_resolution_clock> startTime;
unsigned int timestamp;
void IOThread();
void startIOThread();
Expand Down Expand Up @@ -145,7 +146,7 @@ class PowerSensor {
double currentAtLastMeasurement;
double voltageAtLastMeasurement;
double wattAtLastMeasurement;
double timeAtLastMeasurement;
std::chrono::time_point<std::chrono::high_resolution_clock> timeAtLastMeasurement;
double consumedEnergy;
bool inUse;
} sensorPairs[MAX_PAIRS];
Expand Down
37 changes: 25 additions & 12 deletions host/src/PowerSensor.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <fcntl.h>
#include <omp.h>
#include <termios.h>
#include <unistd.h>
#include <sys/file.h>
Expand All @@ -10,6 +9,19 @@

#include "PowerSensor.hpp"

namespace {
/**
* @brief Helper function to get seconds between two chrono time points
*
* @param tstart
* @param tend
*/
double elapsedSeconds(const std::chrono::time_point<std::chrono::high_resolution_clock> &tstart,
const std::chrono::time_point<std::chrono::high_resolution_clock> &tend) {
return (std::chrono::duration_cast<std::chrono::nanoseconds>(tend - tstart).count()) / 1e9;
}
} // namespace


namespace PowerSensor3 {
/**
Expand Down Expand Up @@ -57,7 +69,7 @@ double Joules(const State &firstState, const State &secondState, int pairID) {
* @return double
*/
double seconds(const State &firstState, const State &secondState) {
return secondState.timeAtRead - firstState.timeAtRead;
return elapsedSeconds(firstState.timeAtRead, secondState.timeAtRead);
}

/**
Expand All @@ -81,7 +93,7 @@ PowerSensor::PowerSensor(std::string device):
fd(openDevice(device)),
pipe_fd(startCleanupProcess()),
thread(nullptr),
startTime(omp_get_wtime()) {
startTime(std::chrono::high_resolution_clock::now()) {
readSensorsFromEEPROM();
initializeSensorPairs();
startIOThread();
Expand Down Expand Up @@ -352,7 +364,8 @@ void PowerSensor::writeMarker() {
void PowerSensor::mark(const State &startState, const State &stopState, std::string name, unsigned int tag) const {
if (dumpFile != nullptr) {
std::unique_lock<std::mutex> lock(dumpFileMutex);
*dumpFile << "M " << startState.timeAtRead - startTime << ' ' << stopState.timeAtRead - startTime << ' ' \
*dumpFile << "M " << elapsedSeconds(startTime, startState.timeAtRead) << ' ' \
<< elapsedSeconds(startTime, stopState.timeAtRead) << ' ' \
<< tag << " \"" << name << '"' << std::endl;
}
}
Expand Down Expand Up @@ -451,11 +464,11 @@ void PowerSensor::dump(std::string dumpFileName) {
void PowerSensor::dumpCurrentWattToFile() {
std::unique_lock<std::mutex> lock(dumpFileMutex);
double totalWatt = 0;
double time = omp_get_wtime();
static double previousTime = startTime;
auto time = std::chrono::high_resolution_clock::now();
static auto previousTime = startTime;

*dumpFile << "S " << time - startTime;
*dumpFile << ' ' << static_cast<int>(1e6 * (time - previousTime));
*dumpFile << "S " << elapsedSeconds(startTime, time);
*dumpFile << ' ' << static_cast<int>(1e6 * elapsedSeconds(previousTime, time));
*dumpFile << ' ' << timestamp;
previousTime = time;

Expand All @@ -475,7 +488,7 @@ void PowerSensor::dumpCurrentWattToFile() {
*
*/
void PowerSensor::updateSensorPairs() {
double now = omp_get_wtime();
auto now = std::chrono::high_resolution_clock::now();
for (unsigned int pairID=0; pairID < MAX_PAIRS; pairID++) {
if (sensorPairs[pairID].inUse) {
Sensor currentSensor = sensors[2*pairID];
Expand All @@ -485,7 +498,8 @@ void PowerSensor::updateSensorPairs() {
sensorPair.currentAtLastMeasurement = currentSensor.valueAtLastMeasurement;
sensorPair.voltageAtLastMeasurement = voltageSensor.valueAtLastMeasurement;
sensorPair.wattAtLastMeasurement = currentSensor.valueAtLastMeasurement * voltageSensor.valueAtLastMeasurement;
sensorPair.consumedEnergy += sensorPair.wattAtLastMeasurement * (now - sensorPair.timeAtLastMeasurement);
sensorPair.consumedEnergy += sensorPair.wattAtLastMeasurement *
elapsedSeconds(sensorPair.timeAtLastMeasurement, now);
sensorPair.timeAtLastMeasurement = now;
}
}
Expand Down Expand Up @@ -546,8 +560,7 @@ int PowerSensor::startCleanupProcess() {
*/
double PowerSensor::totalEnergy(unsigned int pairID) const {
double energy = sensorPairs[pairID].wattAtLastMeasurement *
(omp_get_wtime() - sensorPairs[pairID].timeAtLastMeasurement);

elapsedSeconds(sensorPairs[pairID].timeAtLastMeasurement, std::chrono::high_resolution_clock::now());
return sensorPairs[pairID].consumedEnergy + energy;
}

Expand Down
1 change: 0 additions & 1 deletion host/src/sensors.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <omp.h>
#include <unistd.h>
#include <string.h>

Expand Down

0 comments on commit d17d3ca

Please sign in to comment.