-
Notifications
You must be signed in to change notification settings - Fork 648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LZ4 dependency fix and CMake fix #523
Open
AndersHogqvist
wants to merge
6
commits into
flann-lib:master
Choose a base branch
from
AndersHogqvist:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2509c56
LZ4 dependency fix
0c26fbd
Merge pull request #1 from AndersHogqvist/LZ4_dependency_fix
AndersHogqvist 25d1112
HDF5 only if building tests
825cacc
Merge pull request #2 from AndersHogqvist/HDF5_only_if_building_tests
AndersHogqvist e2a7b41
Removed renaming static libs
3902131
Merge pull request #3 from AndersHogqvist/Removed_renaming_static_libs
AndersHogqvist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,276 @@ | ||
#include "serialization.h" | ||
|
||
#include <lz4.h> | ||
#include <lz4hc.h> | ||
|
||
namespace { | ||
LZ4_streamHC_t lz4Stream_body; | ||
LZ4_streamHC_t *lz4Stream; | ||
|
||
LZ4_streamDecode_t lz4StreamDecode_body; | ||
LZ4_streamDecode_t *lz4StreamDecode; | ||
|
||
} // namespace | ||
|
||
namespace flann { | ||
namespace serialization { | ||
void SaveArchive::initBlock() { | ||
// Alloc the space for both buffer blocks (each compressed block | ||
// references the previous) | ||
buffer_ = buffer_blocks_ = (char *)malloc(BLOCK_BYTES * 2); | ||
compressed_buffer_ = | ||
(char *)malloc(LZ4_COMPRESSBOUND(BLOCK_BYTES) + sizeof(size_t)); | ||
if (buffer_ == NULL || compressed_buffer_ == NULL) { | ||
throw FLANNException("Error allocating compression buffer"); | ||
} | ||
|
||
// Init the LZ4 stream | ||
lz4Stream = &lz4Stream_body; | ||
LZ4_resetStreamHC(lz4Stream, 9); | ||
first_block_ = true; | ||
|
||
offset_ = 0; | ||
} | ||
|
||
void SaveArchive::flushBlock() { | ||
size_t compSz = 0; | ||
// Handle header | ||
if (first_block_) { | ||
// Copy & set the header | ||
IndexHeaderStruct *head = (IndexHeaderStruct *)buffer_; | ||
size_t headSz = sizeof(IndexHeaderStruct); | ||
|
||
assert(head->compression == 0); | ||
head->compression = 1; // Bool now, enum later | ||
|
||
// Do the compression for the block | ||
compSz = LZ4_compress_HC_continue( | ||
lz4Stream, buffer_ + headSz, compressed_buffer_ + headSz, | ||
offset_ - headSz, LZ4_COMPRESSBOUND(BLOCK_BYTES)); | ||
|
||
if (compSz <= 0) { | ||
throw FLANNException("Error compressing (first block)"); | ||
} | ||
|
||
// Handle header | ||
head->first_block_size = compSz; | ||
memcpy(compressed_buffer_, buffer_, headSz); | ||
|
||
compSz += headSz; | ||
first_block_ = false; | ||
} else { | ||
size_t headSz = sizeof(compSz); | ||
|
||
// Do the compression for the block | ||
compSz = LZ4_compress_HC_continue(lz4Stream, buffer_, | ||
compressed_buffer_ + headSz, offset_, | ||
LZ4_COMPRESSBOUND(BLOCK_BYTES)); | ||
|
||
if (compSz <= 0) { | ||
throw FLANNException("Error compressing"); | ||
} | ||
|
||
// Save the size of the compressed block as the header | ||
memcpy(compressed_buffer_, &compSz, headSz); | ||
compSz += headSz; | ||
} | ||
|
||
// Write the compressed buffer | ||
fwrite(compressed_buffer_, compSz, 1, stream_); | ||
|
||
// Switch the buffer to the *other* block | ||
if (buffer_ == buffer_blocks_) | ||
buffer_ = &buffer_blocks_[BLOCK_BYTES]; | ||
else | ||
buffer_ = buffer_blocks_; | ||
offset_ = 0; | ||
} | ||
|
||
void SaveArchive::endBlock() { | ||
// Cleanup memory | ||
free(buffer_blocks_); | ||
buffer_blocks_ = NULL; | ||
buffer_ = NULL; | ||
free(compressed_buffer_); | ||
compressed_buffer_ = NULL; | ||
|
||
// Write a '0' size for next block | ||
size_t z = 0; | ||
fwrite(&z, sizeof(z), 1, stream_); | ||
} | ||
|
||
void LoadArchive::decompressAndLoadV10(FILE *stream) { | ||
buffer_ = NULL; | ||
|
||
// Find file size | ||
size_t pos = ftell(stream); | ||
fseek(stream, 0, SEEK_END); | ||
size_t fileSize = ftell(stream) - pos; | ||
fseek(stream, pos, SEEK_SET); | ||
size_t headSz = sizeof(IndexHeaderStruct); | ||
|
||
// Read the (compressed) file to a buffer | ||
char *compBuffer = (char *)malloc(fileSize); | ||
if (compBuffer == NULL) { | ||
throw FLANNException("Error allocating file buffer space"); | ||
} | ||
if (fread(compBuffer, fileSize, 1, stream) != 1) { | ||
free(compBuffer); | ||
throw FLANNException( | ||
"Invalid index file, cannot read from disk (compressed)"); | ||
} | ||
|
||
// Extract header | ||
IndexHeaderStruct *head = (IndexHeaderStruct *)(compBuffer); | ||
|
||
// Backward compatability | ||
size_t compressedSz = fileSize - headSz; | ||
size_t uncompressedSz = head->first_block_size - headSz; | ||
|
||
// Check for compression type | ||
if (head->compression != 1) { | ||
free(compBuffer); | ||
throw FLANNException("Compression type not supported"); | ||
} | ||
|
||
// Allocate a decompressed buffer | ||
ptr_ = buffer_ = (char *)malloc(uncompressedSz + headSz); | ||
if (buffer_ == NULL) { | ||
free(compBuffer); | ||
throw FLANNException("Error (re)allocating decompression buffer"); | ||
} | ||
|
||
// Extract body | ||
size_t usedSz = LZ4_decompress_safe(compBuffer + headSz, buffer_ + headSz, | ||
compressedSz, uncompressedSz); | ||
|
||
// Check if the decompression was the expected size. | ||
if (usedSz != uncompressedSz) { | ||
free(compBuffer); | ||
throw FLANNException("Unexpected decompression size"); | ||
} | ||
|
||
// Copy header data | ||
memcpy(buffer_, compBuffer, headSz); | ||
free(compBuffer); | ||
|
||
// Put the file pointer at the end of the data we've read | ||
if (compressedSz + headSz + pos != fileSize) | ||
fseek(stream, compressedSz + headSz + pos, SEEK_SET); | ||
block_sz_ = uncompressedSz + headSz; | ||
} | ||
|
||
void LoadArchive::initBlock(FILE *stream) { | ||
size_t pos = ftell(stream); | ||
buffer_ = NULL; | ||
buffer_blocks_ = NULL; | ||
compressed_buffer_ = NULL; | ||
size_t headSz = sizeof(IndexHeaderStruct); | ||
|
||
// Read the file header to a buffer | ||
IndexHeaderStruct *head = (IndexHeaderStruct *)malloc(headSz); | ||
if (head == NULL) { | ||
throw FLANNException("Error allocating header buffer space"); | ||
} | ||
if (fread(head, headSz, 1, stream) != 1) { | ||
free(head); | ||
throw FLANNException("Invalid index file, cannot read from disk (header)"); | ||
} | ||
|
||
// Backward compatability | ||
if (head->signature[13] == '1' && head->signature[15] == '0') { | ||
free(head); | ||
fseek(stream, pos, SEEK_SET); | ||
return decompressAndLoadV10(stream); | ||
} | ||
|
||
// Alloc the space for both buffer blocks (each block | ||
// references the previous) | ||
buffer_ = buffer_blocks_ = (char *)malloc(BLOCK_BYTES * 2); | ||
compressed_buffer_ = (char *)malloc(LZ4_COMPRESSBOUND(BLOCK_BYTES)); | ||
if (buffer_ == NULL || compressed_buffer_ == NULL) { | ||
free(head); | ||
throw FLANNException("Error allocating compression buffer"); | ||
} | ||
|
||
// Init the LZ4 stream | ||
lz4StreamDecode = &lz4StreamDecode_body; | ||
LZ4_setStreamDecode(lz4StreamDecode, NULL, 0); | ||
|
||
// Read first block | ||
memcpy(buffer_, head, headSz); | ||
loadBlock(buffer_ + headSz, head->first_block_size, stream); | ||
block_sz_ += headSz; | ||
ptr_ = buffer_; | ||
free(head); | ||
} | ||
|
||
void loadBlock(char *buffer_, size_t compSz, FILE *stream) { | ||
if (compSz >= LZ4_COMPRESSBOUND(BLOCK_BYTES)) { | ||
throw FLANNException("Requested block size too large"); | ||
} | ||
|
||
// Read the block into the compressed buffer | ||
if (fread(compressed_buffer_, compSz, 1, stream) != 1) { | ||
throw FLANNException("Invalid index file, cannot read from disk (block)"); | ||
} | ||
|
||
// Decompress into the regular buffer | ||
const int decBytes = LZ4_decompress_safe_continue( | ||
lz4StreamDecode, compressed_buffer_, buffer_, compSz, BLOCK_BYTES); | ||
if (decBytes <= 0) { | ||
throw FLANNException("Invalid index file, cannot decompress block"); | ||
} | ||
block_sz_ = decBytes; | ||
} | ||
|
||
void LoadArchive::preparePtr(size_t size) { | ||
// Return if the new size is less than (or eq) the size of a block | ||
if (ptr_ + size <= buffer_ + block_sz_) | ||
return; | ||
|
||
// Switch the buffer to the *other* block | ||
if (buffer_ == buffer_blocks_) | ||
buffer_ = &buffer_blocks_[BLOCK_BYTES]; | ||
else | ||
buffer_ = buffer_blocks_; | ||
|
||
// Find the size of the next block | ||
size_t cmpSz = 0; | ||
size_t readCnt = fread(&cmpSz, sizeof(cmpSz), 1, stream_); | ||
if (cmpSz <= 0 || readCnt != 1) { | ||
throw FLANNException("Requested to read next block past end of file"); | ||
} | ||
|
||
// Load block & init ptr | ||
loadBlock(buffer_, cmpSz, stream_); | ||
ptr_ = buffer_; | ||
} | ||
|
||
void LoadArchive::endBlock() { | ||
// If not v1.0 format hack... | ||
if (buffer_blocks_ != NULL) { | ||
// Read the last '0' in the file | ||
size_t zero = 1; | ||
if (fread(&zero, sizeof(zero), 1, stream_) != 1) { | ||
throw FLANNException("Invalid index file, cannot read from disk (end)"); | ||
} | ||
if (zero != 0) { | ||
throw FLANNException("Invalid index file, last block not zero length"); | ||
} | ||
} | ||
|
||
// Free resources | ||
if (buffer_blocks_ != NULL) { | ||
free(buffer_blocks_); | ||
buffer_blocks_ = NULL; | ||
} | ||
if (compressed_buffer_ != NULL) { | ||
free(compressed_buffer_); | ||
compressed_buffer_ = NULL; | ||
} | ||
ptr_ = NULL; | ||
} | ||
|
||
} // namespace serialization | ||
} // namespace flann |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you only include hdf5 iff you build tests,
flann_mpi_server
/flann_mpi_client
fromsrc/cpp/CMakeLists.txt#L71
andflann_example_cpp
/flann_example_mpi
fromexamples/CMakeLists.txt#L13
are never build unless you also build the tests.Something like this would follow the other options.
You'd also need to add this after L62:
And this at the end.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or you use something like: