Skip to content

Commit

Permalink
Updating tracer, adding helper functions, fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Atraxus committed Jan 20, 2025
1 parent 3fdcbf4 commit cb144e7
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 29 deletions.
30 changes: 30 additions & 0 deletions Intern/rayx-core/src/Beamline/Beamline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ MaterialTables Group::calcMinimalMaterialTables() const {
return loadMaterialTables(relevantMaterials);
}

std::vector<OpticalElement> Group::compile() const {
std::vector<OpticalElement> elements;
traverse([&elements](const BeamlineNode& node) {
if (std::holds_alternative<DesignElement>(node)) {
elements.push_back(std::get<DesignElement>(node).compile());
}
});
return elements;
}

// Retrieve all DesignElements (deep)
std::vector<DesignElement> Group::getAllElements() const {
std::vector<DesignElement> elements;
Expand All @@ -102,6 +112,16 @@ std::vector<DesignElement> Group::getAllElements() const {
return elements;
}

size_t Group::numberOfElements() const {
size_t count = 0;
traverse([&count](const BeamlineNode& node) {
if (std::holds_alternative<DesignElement>(node)) {
count++;
}
});
return count;
}

// Retrieve all DesignSources (deep)
std::vector<DesignSource> Group::getAllSources() const {
std::vector<DesignSource> sources;
Expand All @@ -113,6 +133,16 @@ std::vector<DesignSource> Group::getAllSources() const {
return sources;
}

size_t Group::numberOfSources() const {
size_t count = 0;
traverse([&count](const BeamlineNode& node) {
if (std::holds_alternative<DesignSource>(node)) {
count++;
}
});
return count;
}

// Retrieve all Groups (deep)
std::vector<Group> Group::getAllGroups() const {
std::vector<Group> groups;
Expand Down
5 changes: 5 additions & 0 deletions Intern/rayx-core/src/Beamline/Beamline.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Group;
using BeamlineNode = std::variant<DesignElement, DesignSource, Group>;
enum class NodeType { OpticalElement, LightSource, Group };

// TODO: think about caching to avoid unnecessary recompilation/traversal
class Group {
public:
NodeType getNodeType() const;
Expand All @@ -30,10 +31,14 @@ class Group {
std::vector<Ray> getInputRays(int thread_count = 1) const;
// Returns the smallest possible MaterialTables which cover all materials of the elements in the group
MaterialTables calcMinimalMaterialTables() const;
// Compiles all elements and return vector of OpticalElements
std::vector<OpticalElement> compile() const;

// New methods for retrieving elements, sources, and groups
std::vector<DesignElement> getAllElements() const;
std::vector<DesignSource> getAllSources() const;
size_t numberOfElements() const;
size_t numberOfSources() const;
std::vector<Group> getAllGroups() const;

// Getter & Setter
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-core/src/Debug/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Exit::Exit(const std::string& filename, int line) : filename(filename), line(lin
formatDebugMsg(filename, line, std::cerr);
}

Exit::~Exit() {
[[noreturn]] Exit::~Exit() {
std::cerr << "\n";
formatDebugMsg(filename, line, std::cerr);
std::cerr << "Terminating...\033[0m" << std::endl; // color reset
Expand Down
12 changes: 5 additions & 7 deletions Intern/rayx-core/src/Debug/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ struct RAYX_API Exit {

Exit(const std::string& filename, int line);

~Exit();
[[noreturn]] ~Exit();

template <typename T>
Exit& operator<<(T t) {
Expand All @@ -115,12 +115,10 @@ struct RAYX_API Verb {
};

// An empty implementation used in release when using "debug-only" prints like RAYX_D_LOG.
struct RAYX_API IgnoreLog {
template <typename T>
IgnoreLog& operator<<(T) {
return *this;
}
};
struct RAYX_API IgnoreLog{template <typename T> IgnoreLog & operator<<(T){return *this;
} // namespace RAYX
}
;

// the function to be called after RAYX_EXIT happens.
// normally exit(1), but in the test suite it's ADD_FAILURE.
Expand Down
4 changes: 1 addition & 3 deletions Intern/rayx-core/src/Tracer/DeviceTracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

namespace RAYX {

class Beamline;

/// Expresses whether we force sequential tracing, or we use dynamic tracing.
/// We prefer this over a boolean, as calling eg. the trace function with an argument of `true` has no obvious meaning.
/// On the other hand calling it with `Sequential::Yes` makes the meaning more clear.
Expand All @@ -31,7 +29,7 @@ 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) = 0;
virtual BundleHistory trace(const Group&, Sequential sequential, uint64_t max_batch_size, int THREAD_COUNT = 1, uint32_t maxEvents = 1) = 0;

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

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

private:
struct TraceResult {
Expand Down Expand Up @@ -123,27 +123,21 @@ 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) {
BundleHistory SimpleTracer<Acc>::trace(const Group& group, Sequential seq, uint64_t maxBatchSize, int getInputRaysThreadCount, uint32_t maxEvents) {
RAYX_PROFILE_FUNCTION_STDOUT();
RAYX_VERB << "maxEvents: " << maxEvents;

// don't trace if there are no optical elements
if (b.m_DesignElements.size() == 0) {
if (group.numberOfElements() == 0) {
// an empty history suffices, nothing is happening to the rays!
BundleHistory result;
return result;
}

// prepare input data
auto extractElements = [&b] {
std::vector<OpticalElement> elements;
elements.reserve(b.m_DesignElements.size());
for (const auto& e : b.m_DesignElements) elements.push_back(e.compile());
return elements;
};
const auto elements = extractElements();
const auto rays = b.getInputRays(getInputRaysThreadCount);
const auto materialTables = b.calcMinimalMaterialTables();
const auto elements = group.compile();
const auto rays = group.getInputRays(getInputRaysThreadCount);
const auto materialTables = group.calcMinimalMaterialTables();
const auto randomSeed = randomDouble();

const auto cpu = getDevice<Cpu>(0);
Expand Down
8 changes: 4 additions & 4 deletions Intern/rayx-core/src/Tracer/Tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +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) {
return m_deviceTracer->trace(beamline, sequential, max_batch_size, THREAD_COUNT, maxEvents);
BundleHistory Tracer::trace(const Group& group, Sequential sequential, uint64_t max_batch_size, int THREAD_COUNT, uint32_t maxEvents) {
return m_deviceTracer->trace(group, sequential, max_batch_size, THREAD_COUNT, maxEvents);
}

/// Get the last event for each ray of the bundle.
Expand All @@ -75,8 +75,8 @@ BundleHistory convertToBundleHistory(const std::vector<Ray>& rays) {
return out;
}

int Tracer::defaultMaxEvents(const Beamline* beamline) {
if (beamline) return beamline->m_DesignElements.size() * 2 + 8;
int Tracer::defaultMaxEvents(const Group* group) {
if (group) return group->numberOfElements() * 2 + 8;
return 32;
}

Expand Down
4 changes: 2 additions & 2 deletions Intern/rayx-core/src/Tracer/Tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ 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);
BundleHistory trace(const Group&, Sequential sequential, uint64_t max_batch_size, int THREAD_COUNT = 1, uint32_t maxEvents = 1);

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

private:
std::shared_ptr<DeviceTracer> m_deviceTracer;
Expand Down

0 comments on commit cb144e7

Please sign in to comment.