Skip to content

Commit

Permalink
Merge remote-tracking branch 'JeffersonLab/master' into 355-jana1-2-d…
Browse files Browse the repository at this point in the history
…ocumentation-side-menu
  • Loading branch information
DraTeots committed Sep 11, 2024
2 parents d06a45c + e1d4745 commit 67d4419
Show file tree
Hide file tree
Showing 19 changed files with 161 additions and 115 deletions.
10 changes: 7 additions & 3 deletions docs/jana1to2/jana1-to-jana2.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# Transition Guides for JANA1 to JANA2

Welcome to the transition guides for JANA1 to JANA2! This page provides an overview of the changes and directs you to detailed documentation to help you navigate the updates effectively.
Welcome to the transition guides for JANA1 to JANA2! This page provides an overview of the changes and directs you to detailed documentation to help you navigate the updates effectively. As JANA transitions from version 1 (JANA1) to version 2 (JANA2), several important changes have been made. To assist you in adapting to these changes, we have created two comprehensive guides:

## Overview
## Developers Transition Guide
This guide focuses on key syntax and structural changes between JANA1 and JANA2. It is intended for developers who need to understand and update their codebase according to the new standards.
- [Developers Transition Guide: Key Syntax Changes](jana1to2/developers-transition-guide.md)

As JANA transitions from version 1 (JANA1) to version 2 (JANA2), several important changes have been made. To assist you in adapting to these changes, we have created two comprehensive guides:
## Parameter Changes Guide
This guide details the changes in parameters between JANA1 and JANA2. It provides user-focused information on how these changes impact configurations and usage.
- [Parameter Changes Guide](jana1to2/parameter-changes-guide.md)

1. **Developers Transition Guide:** This guide focuses on key syntax and structural changes between JANA1 and JANA2. It is intended for developers who need to understand and update their codebase according to the new standards.
- [Developers Transition Guide: Key Syntax Changes](#developers-transition-guide)
Expand Down
7 changes: 4 additions & 3 deletions src/examples/EventGroupExample/GroupedEventProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
#include <JANA/JEventProcessor.h>
#include <JANA/JLogger.h>
#include <JANA/Services/JEventGroupTracker.h>
#include <JANA/Utils/JPerfUtils.h>
#include <JANA/Utils/JBenchUtils.h>

#include "TridasEvent.h"

/// GroupedEventProcessor demonstrates basic usage of JEventGroups

class GroupedEventProcessor : public JEventProcessor {

JBenchUtils m_bench_utils = JBenchUtils();
public:
GroupedEventProcessor() {
SetTypeName(NAME_OF_THIS);
Expand All @@ -26,8 +26,9 @@ class GroupedEventProcessor : public JEventProcessor {

void Process(const std::shared_ptr<const JEvent>& event) override {

m_bench_utils.set_seed(event->GetEventNumber(), NAME_OF_THIS);
// In parallel, perform a random amount of (slow) computation
consume_cpu_ms(100, 1.0);
m_bench_utils.consume_cpu_ms(100, 1.0);

auto tridas_event = event->GetSingle<TridasEvent>();
tridas_event->should_keep = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#define _ADCSampleFactory_h_

#include <JANA/JFactoryT.h>
#include <JANA/Utils/JPerfUtils.h>

#include "ADCSample.h"
#include "INDRAMessage.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <JANA/Streaming/JStreamingEventSource.h>
#include <JANA/JEventSourceGeneratorT.h>
#include <JANA/Utils/JBenchUtils.h>


#include "RootProcessor.h"
#include "MonitoringProcessor.h"
Expand All @@ -16,8 +18,10 @@

void dummy_publisher_loop(JApplication* app) {

JBenchUtils bench_utils = JBenchUtils();
size_t delay_ms = 1;
auto logger = app->GetService<JLoggingService>()->get_logger("dummy_publisher_loop");
bench_utils.set_seed(7, "InteractiveStreamingExample.cc:dummy_publisher_loop");

std::this_thread::yield();
//std::this_thread::sleep_for(std::chrono::milliseconds(10)); // Wait for JANA to fire up so we don't lose data
Expand Down Expand Up @@ -50,7 +54,7 @@ void dummy_publisher_loop(JApplication* app) {
//LOG_DEBUG(logger) << "Send: " << message << " (" << message.get_buffer_size() << " bytes)" << LOG_END;
std::cout << "dummy_producer_loop: Sending '" << message << "' (" << message.get_buffer_size() << " bytes)" << std::endl;
transport.send(message);
consume_cpu_ms(delay_ms, 0, false);
bench_utils.consume_cpu_ms(delay_ms, 0, false);
std::this_thread::yield();
}

Expand Down
8 changes: 4 additions & 4 deletions src/examples/RootDatamodelExample/JTestRootEventSource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

#include <JANA/JApplication.h>
#include <JANA/JEvent.h>
#include <JANA/Utils/JPerfUtils.h>

JTestRootEventSource::JTestRootEventSource() {
SetTypeName(NAME_OF_THIS); // Provide JANA with class name
Expand All @@ -24,13 +23,14 @@ JEventSource::Result JTestRootEventSource::Emit(JEvent& event) {
/// Generate an event by inserting objects into "event".
/// (n.b. a normal event source would read these from a file or stream)

// Spin the CPU a bit to limit the rate
consume_cpu_ms(5);

// Configure event and run numbers
static size_t current_event_number = 1;
event.SetEventNumber(current_event_number++);
event.SetRunNumber(222);

m_bench_utils.set_seed(event.GetEventNumber(), NAME_OF_THIS);
// Spin the CPU a bit to limit the rate
m_bench_utils.consume_cpu_ms(5);

// Generate hit objects. We use random numbers to give some variation
// and make things look a little more realistic
Expand Down
3 changes: 3 additions & 0 deletions src/examples/RootDatamodelExample/JTestRootEventSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

#include <JANA/JEventSource.h>
#include <JANA/JEventSourceGeneratorT.h>
#include <JANA/Utils/JBenchUtils.h>


class JTestRootEventSource : public JEventSource {

JBenchUtils m_bench_utils = JBenchUtils();
public:
JTestRootEventSource();
virtual ~JTestRootEventSource() = default;
Expand Down
6 changes: 4 additions & 2 deletions src/examples/StreamingExample/AHitAnomalyDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <JANA/JEventProcessor.h>
#include <JANA/JEvent.h>
#include <JANA/Utils/JPerfUtils.h>
#include <JANA/Utils/JBenchUtils.h>
#include "AHit.h"

class AHitAnomalyDetector : public JEventProcessor {
Expand All @@ -33,13 +33,15 @@ class AHitAnomalyDetector : public JEventProcessor {
}
ss << "}" << std::endl;
std::cout << ss.str();
consume_cpu_ms(m_delay_ms);
m_bench_utils.set_seed(event.GetEventNumber(), NAME_OF_THIS);
m_bench_utils.consume_cpu_ms(m_delay_ms);
}
void Finish() override {
std::cout << "Anomaly detection: Done!" << std::endl;
}
private:
size_t m_delay_ms;
JBenchUtils m_bench_utils = JBenchUtils();

};

Expand Down
9 changes: 6 additions & 3 deletions src/examples/StreamingExample/AHitBHitFuser.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <JANA/JEventProcessor.h>
#include <JANA/JEvent.h>
#include <JANA/JPerfUtils.h>
#include <JANA/JBenchUtils.h>
#include "AHit.h"

/// AHitBHitFuser
Expand Down Expand Up @@ -39,7 +39,9 @@ class AHitBHitFuser : public JEventProcessor {
}
ss << "}" << std::endl;
std::cout << ss.str();
consume_cpu_ms(m_delay_ms);

m_bench_utils.set_seed(event.GetEventNumber(), NAME_OF_THIS);
m_bench_utils.consume_cpu_ms(m_delay_ms);


auto raw_hits = event.Get<AHit>("raw_hits");
Expand All @@ -52,13 +54,14 @@ class AHitBHitFuser : public JEventProcessor {
calibrated_hit->V += 7;
std::cout << serializer.serialize(*calibrated_hit) << std::endl;
}
consume_cpu_ms(m_delay_ms);
m_bench_utils.consume_cpu_ms(m_delay_ms);
}
void Finish() override {
std::cout << "Done!" << std::endl;
}
private:
size_t m_delay_ms;
JBenchUtils m_bench_utils = JBenchUtils();

};

Expand Down
16 changes: 10 additions & 6 deletions src/examples/StreamingExample/ZmqMain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#include <JANA/JApplication.h>
#include <JANA/JFactoryGenerator.h>
#include <JANA/JEventSourceGeneratorT.h>
#include <JANA/Utils/JBenchUtils.h>
#include <JANA/Streaming/JEventBuilder.h>
#include <JANA/Streaming/JStreamingEventSource.h>


#include "ReadoutMessageAuto.h"
#include "ZmqTransport.h"
#include "AHitParser.h"
Expand All @@ -18,7 +20,9 @@

void dummy_publisher_loop() {

consume_cpu_ms(3000, 0, false);
JBenchUtils bench_utils = JBenchUtils();
bench_utils.set_seed(6, "ZmqMain.cc:dummy_publisher_loop");
bench_utils.consume_cpu_ms(3000, 0, false);

auto transport = ZmqTransport("tcp://127.0.0.1:5555", true);
transport.initialize();
Expand All @@ -30,14 +34,14 @@ void dummy_publisher_loop() {
message.event_number = counter;

message.payload_size = 4;
message.payload[0] = randfloat(0,1);
message.payload[1] = randfloat(-100,100);
message.payload[2] = randfloat(-100,100);
message.payload[3] = randfloat(-100,100);
message.payload[0] = bench_utils.randfloat(0,1);
message.payload[1] = bench_utils.randfloat(-100,100);
message.payload[2] = bench_utils.randfloat(-100,100);
message.payload[3] = bench_utils.randfloat(-100,100);

transport.send(message);
std::cout << "Send: " << message << "(" << message.get_buffer_capacity() << " bytes)" << std::endl;
consume_cpu_ms(1000, 0, false);
bench_utils.consume_cpu_ms(1000, 0, false);
}

// Send end-of-stream message so that JANA knows to shut down
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/JANA/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ set(JANA2_SOURCES

Utils/JCpuInfo.cc
Utils/JProcessorMapping.cc
Utils/JPerfUtils.cc
Utils/JBenchUtils.cc
Utils/JStringification.cc
Utils/JAutoActivator.cc
Utils/JTablePrinter.cc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,40 @@
// Subject to the terms in the LICENSE file found in the top-level directory.


#include <thread>
#include <algorithm>
#include "JBenchUtils.h"

#include "JPerfUtils.h"

void JBenchUtils::set_seed(size_t event_number, std::string caller_name)
{
std::hash<std::string> hasher;
long seed = event_number ^ hasher(caller_name);
m_generator = std::mt19937(seed);
}


size_t JBenchUtils::rand_size(size_t avg, double spread) {
auto delta = static_cast<size_t>(avg*spread);
std::uniform_int_distribution<size_t> distribution(avg-delta, avg+delta);
return distribution(m_generator);
}

thread_local std::mt19937* generator = nullptr;

uint64_t consume_cpu_ms(uint64_t millisecs, double spread, bool fix_flops) {
int JBenchUtils::randint(int min, int max) {
std::uniform_int_distribution<int> distribution(min, max);
return distribution(m_generator);
}

double JBenchUtils::randdouble(double min, double max) {
std::uniform_real_distribution<double> dist(min, max);
return dist(m_generator);
}

float JBenchUtils::randfloat(float min, float max) {
std::uniform_real_distribution<float> dist(min, max);
return dist(m_generator);
}

uint64_t JBenchUtils::consume_cpu_ms(uint64_t millisecs, double spread, bool fix_flops) {

uint64_t sampled = rand_size(millisecs, spread);
uint64_t result = 0;
Expand All @@ -22,7 +47,7 @@ uint64_t consume_cpu_ms(uint64_t millisecs, double spread, bool fix_flops) {
sampled *= appx_iters_per_millisec;

for (uint64_t i=0; i<sampled; ++i) {
double a = (*generator)();
double a = (m_generator)();
double b = sqrt(a * pow(1.23, -a)) / a;
result += long(b);
}
Expand All @@ -33,15 +58,15 @@ uint64_t consume_cpu_ms(uint64_t millisecs, double spread, bool fix_flops) {
auto start_time = std::chrono::steady_clock::now();
while ((std::chrono::steady_clock::now() - start_time) < duration) {

double a = (*generator)();
double a = (m_generator)();
double b = sqrt(a * pow(1.23, -a)) / a;
result += long(b);
}
}
return result;
}

uint64_t read_memory(const std::vector<char>& buffer) {
uint64_t JBenchUtils::read_memory(const std::vector<char>& buffer) {

auto length = buffer.size();
uint64_t sum = 0;
Expand All @@ -51,7 +76,7 @@ uint64_t read_memory(const std::vector<char>& buffer) {
return sum;
}

uint64_t write_memory(std::vector<char>& buffer, uint64_t bytes, double spread) {
uint64_t JBenchUtils::write_memory(std::vector<char>& buffer, uint64_t bytes, double spread) {

uint64_t sampled = rand_size(bytes, spread);
for (unsigned i=0; i<sampled; ++i) {
Expand All @@ -60,40 +85,7 @@ uint64_t write_memory(std::vector<char>& buffer, uint64_t bytes, double spread)
return sampled*2;
}

void init_generator() {
if (!generator) {
std::hash<std::thread::id> hasher;
long now = std::chrono::steady_clock::now().time_since_epoch().count();
long seed = now + hasher(std::this_thread::get_id());
generator = new std::mt19937(seed);
}
}

size_t rand_size(size_t avg, double spread) {
auto delta = static_cast<size_t>(avg*spread);
init_generator();
std::uniform_int_distribution<size_t> distribution(avg-delta, avg+delta);
return distribution(*generator);
}


int randint(int min, int max) {
init_generator();
std::uniform_int_distribution<int> distribution(min, max);
return distribution(*generator);
}

double randdouble(double min, double max) {
init_generator();
std::uniform_real_distribution<double> dist(min, max);
return dist(*generator);
}

float randfloat(float min, float max) {
init_generator();
std::uniform_real_distribution<float> dist(min, max);
return dist(*generator);
}



35 changes: 35 additions & 0 deletions src/libraries/JANA/Utils/JBenchUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

// Copyright 2020, Jefferson Science Associates, LLC.
// Subject to the terms in the LICENSE file found in the top-level directory.

#pragma once

#include <vector>
#include <random>
#include <string>
#include <algorithm>
#include <string>
#include <typeinfo>
#include <chrono>


class JBenchUtils {

std::mt19937 m_generator;

public:

JBenchUtils(){}

void set_seed(size_t event_number, std::string caller_name);

size_t rand_size(size_t avg, double spread);
int randint(int min, int max);
double randdouble(double min=0.0, double max=1000.0);
float randfloat(float min=0.0, float max=1000.0);

uint64_t consume_cpu_ms(uint64_t millisecs, double spread=0.0, bool fix_flops=true);
uint64_t read_memory(const std::vector<char>& buffer);
uint64_t write_memory(std::vector<char>& buffer, uint64_t bytes, double spread=0.0);

};
Loading

0 comments on commit 67d4419

Please sign in to comment.