Skip to content

Commit

Permalink
only trigger full chunk lookup if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
philippwindischhofer committed Feb 22, 2024
1 parent 5065a5e commit 27100fd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/DistributedNDArray.hh
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ private:
// can think about turning this into a class
index_t m_chunk_index;

std::size_t m_chunk_last_accessed;

// The index may not start at {0, 0, 0}
IndexVector m_global_start_ind;

Expand Down
27 changes: 21 additions & 6 deletions src/DistributedNDArray.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace stor {
template <class T, std::size_t dims, template<class, std::size_t> class DenseT, template<class, std::size_t> class SparseT, class SerializerT>
DistributedNDArray<T, dims, DenseT, SparseT, SerializerT>::DistributedNDArray(std::string dirpath, std::size_t max_cache_size, SerializerT& ser) :
NDArray<T, dims>(), m_dirpath(dirpath), m_indexpath(dirpath + "/index.bin"), m_max_cache_size(max_cache_size),
m_global_start_ind(dims, 0), m_ser(ser) {
m_chunk_last_accessed(0), m_global_start_ind(dims, 0), m_ser(ser) {

// Create directory if it does not already exist
if(!std::filesystem::exists(m_dirpath)) {
Expand Down Expand Up @@ -436,11 +436,26 @@ bool DistributedNDArray<T, dims, DenseT, SparseT, SerializerT>::chunkContainsInd

template <class T, std::size_t dims, template<class, std::size_t> class DenseT, template<class, std::size_t> class SparseT, class SerializerT>
std::size_t DistributedNDArray<T, dims, DenseT, SparseT, SerializerT>::getChunkIndex(const IndexVector& inds) {
std::size_t chunk_ind = 0;
for(chunk_ind = 0; chunk_ind < m_chunk_index.size(); chunk_ind++) {
if(chunkContainsInds(m_chunk_index[chunk_ind], inds)) {
return chunk_ind;
}

if(m_chunk_index.size() == 0) {
[[unlikely]];
throw ChunkNotFoundError();
}

if(chunkContainsInds(m_chunk_index[m_chunk_last_accessed], inds)) {
[[likely]];
return m_chunk_last_accessed;
}
else {
// Trigger a full chunk lookup
// TODO: have a search tree here with logarithmic instead of linear complexity
std::size_t chunk_ind = 0;
for(chunk_ind = 0; chunk_ind < m_chunk_index.size(); chunk_ind++) {
if(chunkContainsInds(m_chunk_index[chunk_ind], inds)) {
m_chunk_last_accessed = chunk_ind;
return chunk_ind;
}
}
}

std::cout << "HHHHHHH" << std::endl;
Expand Down

0 comments on commit 27100fd

Please sign in to comment.