Skip to content

Commit

Permalink
corrected frame_index to be unsigned long
Browse files Browse the repository at this point in the history
  • Loading branch information
lrlunin committed Nov 11, 2024
1 parent d37fc21 commit 5321334
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 16 deletions.
18 changes: 15 additions & 3 deletions tango-moenchzmq/src/backend/CPUComputationBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,19 @@ void CPUComputationBackend::allocateIndividualStorage() {
* either nullptr or valid allocation address.
*/
deleteIndividualStorage();
frameindex_storage_ptr = new int[individual_frame_buffer_capacity];
/***
* The frameindex is actually is the "unsigned long" type
* so there migiht be a problem when the frameindex will
* exceed the "long" type capacity. I rather doubt that
* the frameindex will exceed the max "long" pos. value (2^63-1).
* The usage of "signed long" allows us to use the -1 as a
* flag for the empty frameindex_storage_ptr. While with the "unsigned long"
* it would be more complicated to use the -1 as a flag.
*
* So if once you face the problem when you would like to have > (2^63-1)
* frames - let me know :-)
*/
frameindex_storage_ptr = new long[individual_frame_buffer_capacity];
// fill with -1 to take the max value later
// those frames which are not arrived will be -1
std::fill(frameindex_storage_ptr,
Expand Down Expand Up @@ -121,7 +133,7 @@ void CPUComputationBackend::dumpAccumulators() {
*
* if there was none frame -> the max_frame_index will be = -1
*/
int max_frame_index = *std::max_element(
long max_frame_index = *std::max_element(
frameindex_storage_ptr,
frameindex_storage_ptr + individual_frame_buffer_capacity);
/***
Expand Down Expand Up @@ -185,7 +197,7 @@ void CPUComputationBackend::processFrame(FullFrame *ff_ptr) {
frames_sums.unlock();
}
if (saveIndividualFrames) {
int frameindex = ff_ptr->m.frameIndex;
unsigned long frameindex = ff_ptr->m.frameIndex;
/*
* safe comparasion of signed int and unsigned size_t would be
* std::cmp_less(frameindex, individual_frame_buffer_capacity)
Expand Down
2 changes: 1 addition & 1 deletion tango-moenchzmq/src/backend/CPUComputationBackend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class CPUComputationBackend {
long updatePedestalPeriod = 1;
std::atomic_bool saveIndividualFrames = true;
std::atomic_bool saveRawFrames = true;
int *frameindex_storage_ptr = nullptr;
long *frameindex_storage_ptr = nullptr;
float *individual_analog_storage_ptr = nullptr;
unsigned short *individual_raw_storage_ptr = nullptr;
#ifdef SINGLE_FRAMES_DEBUG
Expand Down
2 changes: 1 addition & 1 deletion tango-moenchzmq/src/backend/FileWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class FileWriter {
size_t frame_stack_length)
= 0;
virtual void write1DArray(const std::string group_name,
const std::string array_name, int *array,
const std::string array_name, long *array,
size_t array_length)
= 0;
};
4 changes: 2 additions & 2 deletions tango-moenchzmq/src/backend/Frames.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ template <typename T, unsigned int V> struct OrderedFrame {
};

struct Metadata {
int bitmode;
int frameIndex;
unsigned int bitmode;
unsigned long frameIndex;
};

struct FullFrame {
Expand Down
4 changes: 2 additions & 2 deletions tango-moenchzmq/src/backend/HDFWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ void HDFWriter::writeFrameStack(const std::string group_name,
};

void HDFWriter::write1DArray(const std::string group_name,
const std::string array_name, int *array_ptr,
const std::string array_name, long *array_ptr,
size_t array_length) {
const H5::DataType array_datatype(H5::PredType::NATIVE_INT);
const H5::DataType array_datatype(H5::PredType::NATIVE_LONG);
const hsize_t array_dimension[1] = { array_length };
const H5::DataSpace array_dataspace(1, array_dimension);

Expand Down
2 changes: 1 addition & 1 deletion tango-moenchzmq/src/backend/HDFWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ class HDFWriter : public FileWriter {
unsigned short *frame_stack_ptr,
size_t frame_stack_length) override;
void write1DArray(const std::string group_name, const std::string array_name,
int *array_ptr, size_t array_length) override;
long *array_ptr, size_t array_length) override;
};
12 changes: 6 additions & 6 deletions tango-moenchzmq/test/hdfwriter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,30 +199,30 @@ TEST_F(HDFWriterTest, FileWriteUnsignedShortFrameStack) {
delete[] data;
}

TEST_F(HDFWriterTest, FileWrite1DIntArray) {
TEST_F(HDFWriterTest, FileWrite1DLongArray) {
const size_t array_length = 256;
int *array = new int[array_length];
long *array = new long[array_length];
for (size_t x = 0; x < array_length; x++) {
array[x] = std::rand();
}

// write frame to file
file_writer_ptr->openFile();
file_writer_ptr->write1DArray("group", "int_array", array, array_length);
file_writer_ptr->write1DArray("group", "long_array", array, array_length);
file_writer_ptr->closeFile();

// read frame from file and compare values
H5::H5File file("/tmp/" + folder_name + "/"
+ fmt::format("{:%Y%m%d}_run_{:06d}.h5", now,
file_writer_ptr->file_index),
H5F_ACC_RDONLY);
H5::DataSet dataset = file.openDataSet("group/int_array");
H5::DataSet dataset = file.openDataSet("group/long_array");
H5::DataSpace dataspace = dataset.getSpace();
hsize_t dims_out[1];
dataspace.getSimpleExtentDims(dims_out, NULL);
EXPECT_EQ(dims_out[0], array_length);
int *data = new int[array_length];
dataset.read(data, H5::PredType::NATIVE_INT);
long *data = new long[array_length];
dataset.read(data, H5::PredType::NATIVE_LONG);
for (size_t x = 0; x < array_length; x++) {
EXPECT_EQ(data[x], array[x]);
}
Expand Down

0 comments on commit 5321334

Please sign in to comment.