Skip to content

Commit

Permalink
[WAN] Power learned to emit power trace in NPY format.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud-de-Grandmaison-ARM committed May 29, 2024
1 parent bebbc10 commit 7e58e5b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion tools/wan-apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ add_paf_executable(merge

add_paf_executable(power
SOURCES power.cpp
LIBRARIES wan paf
LIBRARIES wan paf sca
EXEC_PREFIX "wan"
OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)
Expand Down
48 changes: 40 additions & 8 deletions tools/wan-apps/power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@
#include "libtarmac/reporter.hh"

#include "PAF/Error.h"
#include "PAF/SCA/NPArray.h"
#include "PAF/WAN/Signal.h"
#include "PAF/WAN/WaveFile.h"
#include "PAF/WAN/Waveform.h"

using namespace std;
using namespace PAF::WAN;
using PAF::SCA::NPArray;

namespace {

Expand Down Expand Up @@ -239,18 +241,18 @@ struct HammingVisitor : public Waveform::Visitor {
p += PowerNoiseDist(MT);
}

void dumpAsCSV(const string &Filename, size_t period, size_t offset) const {
void dumpAsCSV(const string &filename, size_t period, size_t offset) const {
ostream *os;
ofstream *ofs = nullptr;

check();

if (Filename.empty() || Filename == "-") {
if (filename.empty() || filename == "-") {
os = &cout;
} else {
ofs = new ofstream(Filename);
ofs = new ofstream(filename);
if (!*ofs)
DIE("Error opening output file ", Filename);
DIE("Error opening output file ", filename);
os = ofs;
}

Expand All @@ -273,6 +275,26 @@ struct HammingVisitor : public Waveform::Visitor {
}
}

void dumpAsNPY(const string &filename, size_t period, size_t offset) const {

check();

const size_t numCols = power.size() / period;
const size_t numRows = power.cbegin()->second.size();
NPArray<double> npy(numRows, numCols);

size_t col = 0;
for (const auto &H : power) {
if (col % period == offset) {
for (size_t row = 0; row < numRows; row++)
npy(row, col / period) = H.second[row];
}
col += 1;
}

npy.save(filename);
}

const RunInfo *runInfo = nullptr;
map<TimeTy, double> powerTmp;
map<TimeTy, vector<double>> power;
Expand Down Expand Up @@ -379,18 +401,25 @@ int main(int argc, char *argv[]) {
Waveform::Visitor::Options VisitOptions(
false /* skipRegs */, false /* skipWires */, false /* skipIntegers */);

enum class SaveFormat : uint8_t { CSV };
enum class SaveFormat : uint8_t { CSV, NPY };
SaveFormat SaveAs = SaveFormat::CSV;
string SaveFileName("-");

Hamming model = Hamming::WEIGHT;

Argparse ap("wan-power", argc, argv);
ap.optnoval({"--verbose"}, "verbose output", [&]() { Verbose++; });
ap.optval({"--csv"}, "CSV_FILE",
"Save power trace in csv format to file ('-' for stdout)",
ap.optval(
{"--csv"}, "CSV_FILE",
"Save power trace in csv format to file CSV_FILE ('-' for stdout)",
[&](const string &filename) {
SaveAs = SaveFormat::CSV;
SaveFileName = filename;
});
ap.optval({"--npy"}, "NPY_FILE",
"Save power trace in npy format to file NPY_FILE",
[&](const string &filename) {
SaveAs = SaveFormat::CSV;
SaveAs = SaveFormat::NPY;
SaveFileName = filename;
});
ap.optnoval({"--no-noise"}, "Don't add noise to the power trace",
Expand Down Expand Up @@ -501,6 +530,9 @@ int main(int argc, char *argv[]) {
case SaveFormat::CSV:
HV->dumpAsCSV(SaveFileName, Period, Offset);
break;
case SaveFormat::NPY:
HV->dumpAsNPY(SaveFileName, Period, Offset);
break;
}

return 0;
Expand Down

0 comments on commit 7e58e5b

Please sign in to comment.