Skip to content

Casting routine from files/header#49

Open
frheault wants to merge 3 commits into
tee-ar-ex:mainfrom
frheault:fixes_for_benchmark
Open

Casting routine from files/header#49
frheault wants to merge 3 commits into
tee-ar-ex:mainfrom
frheault:fixes_for_benchmark

Conversation

@frheault

Copy link
Copy Markdown
Contributor

(Similar description to trx-rs PR)

Proposed modifications to allow fair comparison for benchmarking across languages, the biggest modification is to up/down-cast to uint32 offsets since it is not completely illegal. Some of the benchmark files add offsets as uint64. I believe this is mostly to smooth operations between languages, I personally think that virtually no one will create a tractogram with 40M streamlines with 100 points each, but it is possible.

I apologize for the formatting, I believe my VScode did some automatic formatting, @mattcieslak if you could tell me what standard/linter/tool to use (or maybe I should manually revert the identical lines?).

I believe some unit testing is needed to verify if everything is alright, but I tested single language round-trip (load/save) and between language compatibility. Then I benchmarked on big files: https://github.com/tee-ar-ex/trx-manuscript-2026-benchmark

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates AnyTrxFile::_create_from_pointer()’s handling of groups/ entries to allow loading group membership arrays stored in additional integer dtypes by converting them into the internal uint32 representation, enabling more consistent cross-language benchmarking/interoperability.

Changes:

  • Allow groups/ arrays with integer dtypes beyond uint32 (e.g., uint64, int64, int32, uint16, etc.) by casting to uint32.
  • Add a guard intended to prevent unsafe downcasting when NB_STREAMLINES exceeds 32-bit capacity.
  • Materialize group arrays to owned memory before storing in trx.groups.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/trx.cpp
Comment on lines +537 to +558
if (ext == "int64") {
const int64_t* src = reinterpret_cast<const int64_t*>(tmp_arr.owned.data());
for (size_t i = 0; i < size; ++i) dst[i] = static_cast<uint32_t>(src[i]);
} else if (ext == "uint64") {
const uint64_t* src = reinterpret_cast<const uint64_t*>(tmp_arr.owned.data());
for (size_t i = 0; i < size; ++i) dst[i] = static_cast<uint32_t>(src[i]);
} else if (ext == "uint8") {
const uint8_t* src = reinterpret_cast<const uint8_t*>(tmp_arr.owned.data());
for (size_t i = 0; i < size; ++i) dst[i] = static_cast<uint32_t>(src[i]);
} else if (ext == "int8") {
const int8_t* src = reinterpret_cast<const int8_t*>(tmp_arr.owned.data());
for (size_t i = 0; i < size; ++i) dst[i] = static_cast<uint32_t>(src[i]);
} else if (ext == "uint16") {
const uint16_t* src = reinterpret_cast<const uint16_t*>(tmp_arr.owned.data());
for (size_t i = 0; i < size; ++i) dst[i] = static_cast<uint32_t>(src[i]);
} else if (ext == "int16") {
const int16_t* src = reinterpret_cast<const int16_t*>(tmp_arr.owned.data());
for (size_t i = 0; i < size; ++i) dst[i] = static_cast<uint32_t>(src[i]);
} else if (ext == "int32") {
const int32_t* src = reinterpret_cast<const int32_t*>(tmp_arr.owned.data());
for (size_t i = 0; i < size; ++i) dst[i] = static_cast<uint32_t>(src[i]);
}
Comment thread src/trx.cpp
Comment on lines +523 to +528
if (ext == "int64" || ext == "uint64") {
uint64_t num_strs = static_cast<uint64_t>(header["NB_STREAMLINES"].number_value());
if (num_strs > 4294967295ULL) {
throw TrxFormatError("downcasting is unsafe because the number of streamlines exceeds the 32-bit limit");
}
}
Comment thread src/trx.cpp
Comment on lines +519 to +523
} else if (ext == "int64" || ext == "uint64" || ext == "int32" || ext == "uint8" || ext == "int8" || ext == "uint16" || ext == "int16") {
if (ext == "int32" || ext == "uint8" || ext == "int8" || ext == "uint16" || ext == "int16") {
std::cerr << "Warning: Upcasting group from " << ext << " to uint32\n";
}
if (ext == "int64" || ext == "uint64") {
@36000 36000 mentioned this pull request Jun 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants