Skip to content

Commit

Permalink
remove startEventId
Browse files Browse the repository at this point in the history
  • Loading branch information
F Zotter committed Sep 24, 2024
1 parent 07daa3b commit 5c0e767
Show file tree
Hide file tree
Showing 22 changed files with 47 additions and 94 deletions.
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Shader/DynamicElements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void dynamicElements(int gid, InvState& inv) {
}

// store recorded events count
auto eventsCount = static_cast<int>(inv.nextEventIndex - inv.pushConstants.startEventID);
auto eventsCount = static_cast<int>(inv.nextEventIndex);
eventsCount = std::max(0, std::min(static_cast<int>(inv.pushConstants.maxEvents), eventsCount));
inv.outputRayCounts[gid] = eventsCount;
}
Expand Down
9 changes: 2 additions & 7 deletions Intern/rayx-core/src/Shader/Helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void init(InvState& inv) {

// TODO(Sven): dont waste time with initializing
// sets all output rays controlled by this shader call to ETYPE_UNINIT.
for (uint32_t i = uint32_t(inv.pushConstants.startEventID); i < inv.pushConstants.maxEvents; i++) {
for (uint32_t i = uint32_t(0); i < inv.pushConstants.maxEvents; i++) {
inv.outputRays[output_index(i, inv)].m_eventType = ETYPE_UNINIT;
}
inv.nextEventIndex = 0;
Expand All @@ -32,18 +32,13 @@ uint64_t rayId(InvState& inv) { return uint64_t(inv.pushConstants.rayIdStart) +
// Typically used as `outputRays[output_index(i)]`.
RAYX_FN_ACC
uint32_t output_index(uint32_t i, InvState& inv) {
return uint32_t(inv.globalInvocationId) * uint32_t(inv.pushConstants.maxEvents - inv.pushConstants.startEventID) + i -
uint32_t(inv.pushConstants.startEventID);
return uint32_t(inv.globalInvocationId) * uint32_t(inv.pushConstants.maxEvents) + i;
}

// record an event and store it in the next free spot in outputRays.
// `r` will typically be ray, or some related ray.
RAYX_FN_ACC
void recordEvent(Ray r, double w, InvState& inv) {
if (inv.nextEventIndex < inv.pushConstants.startEventID) {
inv.nextEventIndex += 1;
return;
}
if (inv.finalized) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Shader/InvocationState.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct PushConstants { // TODO(Jannis): PushConstants is not an expressive name
double randomSeed;
double maxEvents;
double sequential;
double startEventID;
//double missing?
};

struct _debug_struct {
Expand Down
4 changes: 2 additions & 2 deletions Intern/rayx-core/src/Tracer/DeviceTracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class RAYX_API DeviceTracer {
public:
virtual ~DeviceTracer() = default;

virtual BundleHistory trace(const Beamline&, Sequential sequential, uint64_t max_batch_size, int THREAD_COUNT = 1, uint32_t maxEvents = 1,
int startEventID = 0) = 0;
virtual BundleHistory trace(const Beamline&, Sequential sequential, uint64_t max_batch_size, int THREAD_COUNT = 1,
uint32_t maxEvents = 1) = 0;

protected:
PushConstants m_pushConstants;
Expand Down
11 changes: 4 additions & 7 deletions Intern/rayx-core/src/Tracer/SimpleTracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ class SimpleTracer : public DeviceTracer {
public:
SimpleTracer(int deviceIndex);

BundleHistory trace(const Beamline&, Sequential sequential, uint64_t maxBatchSize, int getInputRaysThreadCount, uint32_t maxEvents,
int startEventID) override;
BundleHistory trace(const Beamline&, Sequential sequential, uint64_t maxBatchSize, int getInputRaysThreadCount, uint32_t maxEvents) override;

private:
struct TraceResult {
Expand Down Expand Up @@ -124,8 +123,7 @@ template <typename Acc>
SimpleTracer<Acc>::SimpleTracer(int deviceIndex) : m_deviceIndex(deviceIndex) {}

template <typename Acc>
BundleHistory SimpleTracer<Acc>::trace(const Beamline& b, Sequential seq, uint64_t maxBatchSize, int getInputRaysThreadCount, uint32_t maxEvents,
int startEventID) {
BundleHistory SimpleTracer<Acc>::trace(const Beamline& b, Sequential seq, uint64_t maxBatchSize, int getInputRaysThreadCount, uint32_t maxEvents) {
RAYX_PROFILE_FUNCTION_STDOUT();
RAYX_VERB << "maxEvents: " << maxEvents;

Expand Down Expand Up @@ -153,7 +151,7 @@ BundleHistory SimpleTracer<Acc>::trace(const Beamline& b, Sequential seq, uint64
auto q = Queue(acc);

const auto firstBatchSize = static_cast<Idx>(glm::min(rays.size(), maxBatchSize));
const auto maxOutputEventsCount = static_cast<Idx>(maxBatchSize * (maxEvents - startEventID));
const auto maxOutputEventsCount = static_cast<Idx>(maxBatchSize * maxEvents);
const auto initialCompactEventsSize = static_cast<Idx>(maxBatchSize * glm::min(2u, maxEvents));
resizeBufferIfNeeded(q, m_batchOutput.compactEvents, initialCompactEventsSize);
resizeBufferIfNeeded(q, m_batchOutput.compactEventCounts, firstBatchSize);
Expand Down Expand Up @@ -184,8 +182,7 @@ BundleHistory SimpleTracer<Acc>::trace(const Beamline& b, Sequential seq, uint64
.numRays = (double)rays.size(),
.randomSeed = randomSeed,
.maxEvents = (double)maxEvents,
.sequential = sequential,
.startEventID = (double)startEventID};
.sequential = sequential};

const auto inputRays = rays.data() + rayIdStart;
transferToBuffer(q, cpu, m_batchInput.rays, inputRays, static_cast<Idx>(batchSize));
Expand Down
5 changes: 2 additions & 3 deletions Intern/rayx-core/src/Tracer/Tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ Tracer::Tracer(const DeviceConfig& deviceConfig) {
}
}

BundleHistory Tracer::trace(const Beamline& beamline, Sequential sequential, uint64_t max_batch_size, int THREAD_COUNT, uint32_t maxEvents,
int startEventID) {
return m_deviceTracer->trace(beamline, sequential, max_batch_size, THREAD_COUNT, maxEvents, startEventID);
BundleHistory Tracer::trace(const Beamline& beamline, Sequential sequential, uint64_t max_batch_size, int THREAD_COUNT, uint32_t maxEvents) {
return m_deviceTracer->trace(beamline, sequential, max_batch_size, THREAD_COUNT, maxEvents);
}

/// Get the last event for each ray of the bundle.
Expand Down
3 changes: 1 addition & 2 deletions Intern/rayx-core/src/Tracer/Tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class RAYX_API Tracer {
// This will call the trace implementation of a subclass
// See `BundleHistory` for information about the return value.
// `max_batch_size` corresponds to the maximal number of rays that will be put into `traceRaw` in one batch.
BundleHistory trace(const Beamline&, Sequential sequential, uint64_t max_batch_size, int THREAD_COUNT = 1, uint32_t maxEvents = 1,
int startEventID = 0);
BundleHistory trace(const Beamline&, Sequential sequential, uint64_t max_batch_size, int THREAD_COUNT = 1, uint32_t maxEvents = 1);

static int defaultMaxEvents(const Beamline* beamline = nullptr);

Expand Down
4 changes: 2 additions & 2 deletions Intern/rayx-core/src/Writer/CSVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Cell doubleToCell(double x) {
return strToCell(s.c_str());
}

void writeCSV(const RAYX::BundleHistory& hist, const std::string& filename, const Format& format, int startEventID) {
void writeCSV(const RAYX::BundleHistory& hist, const std::string& filename, const Format& format) {
std::ofstream file(filename);

// write the header of the CSV file:
Expand All @@ -79,7 +79,7 @@ void writeCSV(const RAYX::BundleHistory& hist, const std::string& filename, cons
if (i > 0) {
file << DELIMITER;
}
double d = format[i].get_double(static_cast<uint32_t>(ray_id), static_cast<int>(event_id) + startEventID, event);
double d = format[i].get_double(static_cast<uint32_t>(ray_id), static_cast<int>(event_id), event);
file << doubleToCell(d).buf;
}
file << '\n';
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Writer/CSVWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "Tracer/Tracer.h"
#include "Writer/Writer.h"

void RAYX_API writeCSV(const RAYX::BundleHistory&, const std::string& filename, const Format& format, int startEventID = 0);
void RAYX_API writeCSV(const RAYX::BundleHistory&, const std::string& filename, const Format& format);

// loadCSV only works for csv files created using FULL_FORMAT.
RAYX::BundleHistory RAYX_API loadCSV(const std::string& filename);
19 changes: 6 additions & 13 deletions Intern/rayx-core/src/Writer/H5Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int count(const RAYX::BundleHistory& hist) {
}

// Re-formats `hist` into a bunch of doubles using the format.
std::vector<double> toDoubles(const RAYX::BundleHistory& hist, const Format& format, int startEventID) {
std::vector<double> toDoubles(const RAYX::BundleHistory& hist, const Format& format) {
std::vector<double> output;
output.reserve(count(hist) * format.size());

Expand All @@ -29,19 +29,18 @@ std::vector<double> toDoubles(const RAYX::BundleHistory& hist, const Format& for
for (uint32_t event_id = 0; event_id < ray_hist.size(); event_id++) {
const RAYX::Ray& event = ray_hist[event_id];
for (uint32_t i = 0; i < format.size(); i++) {
double next = format[i].get_double(ray_id, event_id + startEventID, event);
double next = format[i].get_double(ray_id, event_id, event);
output.push_back(next);
}
}
}
return output;
}

void writeH5(const RAYX::BundleHistory& hist, const std::string& filename, const Format& format, std::vector<std::string> elementNames,
int startEventID) {
void writeH5(const RAYX::BundleHistory& hist, const std::string& filename, const Format& format, std::vector<std::string> elementNames) {
HighFive::File file(filename, HighFive::File::ReadWrite | HighFive::File::Create | HighFive::File::Truncate);

auto doubles = toDoubles(hist, format, startEventID);
auto doubles = toDoubles(hist, format);

try {
// write data
Expand Down Expand Up @@ -78,15 +77,12 @@ RAYX::BundleHistory fromDoubles(const std::vector<double>& doubles, const Format
RAYX::RayHistory rayHist;
rayHist.reserve(8); // Estimate: assume 8 events per ray on average

const double startEventID = doubles[1];
const double* data = doubles.data();

for (size_t i = 0; i < numRays; ++i) {
const double* rayData = data + i * formatSize;

double eventId = rayData[1];

if (eventId == startEventID && !rayHist.empty()) {
if (!rayHist.empty()) {
bundleHist.push_back(std::move(rayHist));
rayHist.clear();
rayHist.reserve(8);
Expand Down Expand Up @@ -120,7 +116,7 @@ RAYX::BundleHistory fromDoubles(const std::vector<double>& doubles, const Format
return bundleHist;
}

RAYX::BundleHistory raysFromH5(const std::string& filename, const Format& format, std::unique_ptr<uint32_t> startEventID) {
RAYX::BundleHistory raysFromH5(const std::string& filename, const Format& format) {
RAYX_PROFILE_FUNCTION_STDOUT();
RAYX::BundleHistory rays;

Expand All @@ -138,9 +134,6 @@ RAYX::BundleHistory raysFromH5(const std::string& filename, const Format& format
RAYX_WARN << "No rays found in " << filename;
return rays;
}
if (startEventID) {
*startEventID = static_cast<uint32_t>(doubles[1]);
}
rays = fromDoubles(doubles, format);
RAYX_VERB << "Loaded " << rays.size() << " rays from " << filename;

Expand Down
6 changes: 3 additions & 3 deletions Intern/rayx-core/src/Writer/H5Writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
#include "Tracer/Tracer.h"
#include "Writer/Writer.h"

RAYX_API void writeH5(const RAYX::BundleHistory&, const std::string& filename, const Format& format, std::vector<std::string> elementNames,
int startEventID);
RAYX_API RAYX::BundleHistory raysFromH5(const std::string& filename, const Format& format, std::unique_ptr<uint32_t> startEventID = nullptr);
RAYX_API void writeH5(const RAYX::BundleHistory&, const std::string& filename, const Format& format, std::vector<std::string> elementNames);

RAYX_API RAYX::BundleHistory raysFromH5(const std::string& filename, const Format& format);
2 changes: 1 addition & 1 deletion Intern/rayx-ui/src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ void Application::loadRays(const std::filesystem::path& rmlPath, const size_t nu
RAYX_PROFILE_FUNCTION_STDOUT();
#ifndef NO_H5
std::string rayFilePath = rmlPath.string().substr(0, rmlPath.string().size() - 4) + ".h5";
m_rays = raysFromH5(rayFilePath, FULL_FORMAT, std::make_unique<uint32_t>(m_UIParams.rayInfo.startEventID));
m_rays = raysFromH5(rayFilePath, FULL_FORMAT);

#else
std::string rayFilePath = rmlPath.string().substr(0, rmlPath.string().size() - 4) + ".csv";
Expand Down
6 changes: 3 additions & 3 deletions Intern/rayx-ui/src/RayProcessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ size_t getMaxEvents(const RAYX::BundleHistory& bundleHist) {
// Define the type of the filter function

std::vector<Line> getRays(const RAYX::BundleHistory& rayCache, const RAYX::Beamline& beamline, RayFilterFunction filterFunction,
uint32_t amountOfRays, int startEventID) {
uint32_t amountOfRays) {
RAYX_PROFILE_FUNCTION_STDOUT();
std::vector<Line> rays;

Expand Down Expand Up @@ -105,8 +105,8 @@ std::vector<Line> getRays(const RAYX::BundleHistory& rayCache, const RAYX::Beaml
: (event.m_eventType == RAYX::ETYPE_ABSORBED) ? RED
: WHITE;

if (!(isFirstEvent && startEventID > 0)) {
// Only execute if not the first event with startEventID > 0
if (!(isFirstEvent > 0)) { //deleted starteventID from here, if errors
// Only execute if not the first event with > 0
ColorVertex origin = {rayLastPos, originColor};
ColorVertex point = {worldPos, pointColor};

Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-ui/src/RayProcessing.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ using RayFilterFunction = std::function<std::vector<size_t>(const RAYX::BundleHi
*/

std::vector<Line> getRays(const RAYX::BundleHistory& rayCache, const RAYX::Beamline& beamline, RayFilterFunction filterFunction,
uint32_t amountOfRays, int startEventID = 0);
uint32_t amountOfRays);

void sortRaysByElement(const RAYX::BundleHistory& rays, std::vector<std::vector<RAYX::Ray>>& sortedRays, size_t numElements);

Expand Down
4 changes: 2 additions & 2 deletions Intern/rayx-ui/src/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ void Scene::buildRaysRObject(const RAYX::Beamline& beamline, UIRayInfo& rayInfo,
rayInfo.amountOfRays = rayInfo.maxAmountOfRays;
}
if (!rayInfo.renderAllRays) {
rays = getRays(m_rayCache, beamline, kMeansFilter, (uint32_t)rayInfo.amountOfRays, rayInfo.startEventID);
rays = getRays(m_rayCache, beamline, kMeansFilter, (uint32_t)rayInfo.amountOfRays);
} else {
rays = getRays(m_rayCache, beamline, noFilter, (uint32_t)rayInfo.maxAmountOfRays, rayInfo.startEventID);
rays = getRays(m_rayCache, beamline, noFilter, (uint32_t)rayInfo.maxAmountOfRays);
}
if (!rays.empty()) {
// Temporarily aggregate all vertices, then create a single RenderObject
Expand Down
20 changes: 4 additions & 16 deletions Intern/rayx-ui/src/Simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,11 @@ void Simulator::runSimulation() {
m_maxEvents = RAYX::Tracer::defaultMaxEvents(&m_Beamline);
}

auto rays = m_Tracer->trace(m_Beamline, m_seq, m_max_batch_size, 1, m_maxEvents, m_startEventID);
auto rays = m_Tracer->trace(m_Beamline, m_seq, m_max_batch_size, 1, m_maxEvents);

// check max EventID
uint32_t maxEventID = 0;
bool notEnoughEvents = false;

for (auto& ray : rays) {
if (ray.size() > (maxEventID)) {
maxEventID = static_cast<uint32_t>(ray.size()) + m_startEventID;
}

for (auto& event : ray) {
if (event.m_eventType == RAYX::ETYPE_TOO_MANY_EVENTS) {
notEnoughEvents = true;
Expand All @@ -39,12 +33,7 @@ void Simulator::runSimulation() {
if (notEnoughEvents) {
RAYX_LOG << "Not enough events (" << m_maxEvents << ")! Consider increasing m_maxEvents.";
}
if (maxEventID == 0) {
RAYX_LOG << "No events were recorded! If startEventID is set, it might need to be lowered.";
} else if (maxEventID < m_maxEvents) {
RAYX_LOG << "m_maxEvents is set to " << m_maxEvents << " but the maximum event ID is " << maxEventID << ". Consider setting m_maxEvents to "
<< maxEventID << " to increase performance.";
}


// Export Rays to external data.
std::string path = m_RMLPath.string();
Expand All @@ -65,9 +54,9 @@ void Simulator::runSimulation() {

path += ".h5";
#ifndef NO_H5
writeH5(rays, path, fmt, names, m_startEventID);
writeH5(rays, path, fmt, names);
#else
writeCSV(rays, path, fmt, m_startEventID);
writeCSV(rays, path, fmt);
#endif
}

Expand All @@ -83,7 +72,6 @@ void Simulator::setSimulationParameters(const std::filesystem::path& RMLPath, co
m_Beamline = std::move(beamline);
m_max_batch_size = simulationInfo.maxBatchSize;
m_seq = simulationInfo.sequential ? RAYX::Sequential::Yes : RAYX::Sequential::No;
m_startEventID = simulationInfo.startEventID;
m_maxEvents = simulationInfo.maxEvents;
if (simulationInfo.fixedSeed) {
if (simulationInfo.seed != -1) {
Expand Down
1 change: 0 additions & 1 deletion Intern/rayx-ui/src/Simulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class Simulator {
std::vector<std::string> getAvailableDevices();

private:
uint32_t m_startEventID = 0;
uint32_t m_maxEvents = 0;

std::filesystem::path m_RMLPath; ///< Path to the RML file
Expand Down
8 changes: 2 additions & 6 deletions Intern/rayx-ui/src/UserInterface/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ struct UIRayInfo {
bool renderAllRays;
size_t amountOfRays;
size_t maxAmountOfRays;
uint32_t startEventID = 0;
};

struct UISimulationInfo {
uint32_t startEventID = 0;
uint32_t maxEvents = 0;
uint32_t maxBatchSize = 100000;
bool sequential = false;
Expand All @@ -35,10 +33,9 @@ struct UISimulationInfo {
bool fixedSeed = false;
int seed;

UISimulationInfo(int startEventID, int maxEvents, int maxBatchSize, bool sequential, const std::vector<std::string>& availableDevices,
UISimulationInfo(int maxEvents, int maxBatchSize, bool sequential, const std::vector<std::string>& availableDevices,
int deviceIndex, bool fixedSeed = false, int seed = 0)
: startEventID(startEventID),
maxEvents(maxEvents),
: maxEvents(maxEvents),
maxBatchSize(maxBatchSize),
sequential(sequential),
availableDevices(availableDevices),
Expand Down Expand Up @@ -83,7 +80,6 @@ struct UIParameters {
runSimulation(false),
simulationSettingsReady(false),
simulationInfo({
0,
0,
100000,
false,
Expand Down
4 changes: 1 addition & 3 deletions Intern/rayx-ui/src/UserInterface/UIHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,9 +584,7 @@ void UIHandler::showSimulationSettingsPopupWindow(UIParameters& uiParams) {
ImGui::Combo("Device", reinterpret_cast<int*>(&uiParams.simulationInfo.deviceIndex), &deviceItems[0],
static_cast<int>(deviceItems.size()));

// startEventID selection
// ImGui::InputInt("Start Event ID", &uiParams.simulationInfo.startEventID);


// maxEvents selection
ImGui::InputScalar("Max Events", ImGuiDataType_U32, &uiParams.simulationInfo.maxEvents);

Expand Down
2 changes: 0 additions & 2 deletions Intern/rayx/src/CommandParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class CommandParser {
std::string m_format = defaultFormatString(); // --format
int m_setThreads = 1; // -T (dipolesource)
int m_maxEvents = -1; // -m (max events)
int m_startEventID = 0; // -e (start event id)
} m_args;

static inline void getVersion() {
Expand Down Expand Up @@ -91,6 +90,5 @@ class CommandParser {
{OptionType::INT, "setThreads", "Number of Threads for Lightsource-Parallelization'",
&(m_args.m_setThreads)}}, // TODO: understandable description
{'m', {OptionType::INT, "maxEvents", "Maximum number of events per ray", &(m_args.m_maxEvents)}},
{'e', {OptionType::INT, "startEventID", "Start event ID", &(m_args.m_startEventID)}},
};
};
Loading

0 comments on commit 5c0e767

Please sign in to comment.