Skip to content

Commit

Permalink
Merge pull request #337 from mimaric/mimaric/index-host-copy-rename
Browse files Browse the repository at this point in the history
[cudamapper] Renamed HostCache to IndexHostCopy
  • Loading branch information
mimaric committed Mar 26, 2020
2 parents 86a1149 + 914c5d5 commit 0cb8890
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 82 deletions.
2 changes: 1 addition & 1 deletion cudamapper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ target_compile_options(minimizer PRIVATE -Werror)
cuda_add_library(index_gpu
src/index.cu
src/index_gpu.cu
src/host_cache.cu
src/index_host_copy.cu
src/minimizer.cu)
target_include_directories(index_gpu PUBLIC include ${CUB_DIR})
target_link_libraries(index_gpu logging minimizer pthread utils cgaio)
Expand Down
19 changes: 11 additions & 8 deletions cudamapper/include/claragenomics/cudamapper/index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ class Index
const cudaStream_t cuda_stream = 0);
};

/// IndexHostCopy - Creates and maintains a copy of computed IndexGPU elements on the host, then allows to retrieve target
/// IndexHostCopyBase - Creates and maintains a copy of computed IndexGPU elements on the host, then allows to retrieve target
/// indices from host instead of recomputing them again
///
class IndexHostCopy
class IndexHostCopyBase
{
public:
/// \brief copy cached index vectors from the host and create an object of Index on GPU
Expand All @@ -134,6 +134,9 @@ class IndexHostCopy
virtual std::unique_ptr<Index> copy_index_to_device(DefaultDeviceAllocator allocator,
const cudaStream_t cuda_stream = 0) = 0;

/// \brief virtual destructor
virtual ~IndexHostCopyBase() = default;

/// \brief returns an array of representations of sketch elements (stored on host)
/// \return an array of representations of sketch elements
virtual const std::vector<representation_t>& representations() const = 0;
Expand Down Expand Up @@ -193,12 +196,12 @@ class IndexHostCopy
/// \param kmer_size - number of basepairs in a k-mer
/// \param window_size the number of adjacent k-mers in a window, adjacent = shifted by one basepair
/// \param cuda_stream D2H copy is done on this stream
/// \return - an instance of IndexHostCopy
static std::unique_ptr<IndexHostCopy> create_cache(const Index& index,
const read_id_t first_read_id,
const std::uint64_t kmer_size,
const std::uint64_t window_size,
const cudaStream_t cuda_stream = 0);
/// \return - an instance of IndexHostCopyBase
static std::unique_ptr<IndexHostCopyBase> create_cache(const Index& index,
const read_id_t first_read_id,
const std::uint64_t kmer_size,
const std::uint64_t window_size,
const cudaStream_t cuda_stream = 0);
};

} // namespace cudamapper
Expand Down
20 changes: 10 additions & 10 deletions cudamapper/src/index.cu
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ std::unique_ptr<Index> Index::create_index(DefaultDeviceAllocator allocator,
cuda_stream);
}

std::unique_ptr<IndexHostCopy> IndexHostCopy::create_cache(const Index& index,
const read_id_t first_read_id,
const std::uint64_t kmer_size,
const std::uint64_t window_size,
const cudaStream_t cuda_stream)
std::unique_ptr<IndexHostCopyBase> IndexHostCopyBase::create_cache(const Index& index,
const read_id_t first_read_id,
const std::uint64_t kmer_size,
const std::uint64_t window_size,
const cudaStream_t cuda_stream)
{
CGA_NVTX_RANGE(profiler, "cache_D2H");
return std::make_unique<HostCache>(index,
first_read_id,
kmer_size,
window_size,
cuda_stream);
return std::make_unique<IndexHostCopy>(index,
first_read_id,
kmer_size,
window_size,
cuda_stream);
}

} // namespace cudamapper
Expand Down
58 changes: 29 additions & 29 deletions cudamapper/src/index_gpu.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <claragenomics/utils/mathutils.hpp>
#include <claragenomics/utils/signed_integer_utils.hpp>

#include "host_cache.cuh"
#include "index_host_copy.cuh"

namespace claragenomics
{
Expand Down Expand Up @@ -75,10 +75,10 @@ public:
/// \brief Constructor which copies the index from host copy
///
/// \param allocator is pointer to asynchronous device allocator
/// \param host_cache is a copy of index for a set of reads which has been previously computed and stored on the host.
/// \param index_host_copy is a copy of index for a set of reads which has been previously computed and stored on the host.
/// \param cuda_stream CUDA stream on which the work is to be done. Device arrays are also associated with this stream and will not be freed at least until all work issued on this stream before calling their destructor is done
IndexGPU(DefaultDeviceAllocator allocator,
const HostCache& host_cache,
const IndexHostCopy& index_host_copy,
const cudaStream_t cuda_stream = 0);

/// \brief returns an array of representations of sketch elements
Expand Down Expand Up @@ -595,11 +595,11 @@ IndexGPU<SketchElementImpl>::IndexGPU(DefaultDeviceAllocator allocator,

template <typename SketchElementImpl>
IndexGPU<SketchElementImpl>::IndexGPU(DefaultDeviceAllocator allocator,
const HostCache& host_cache,
const IndexHostCopy& index_host_copy,
const cudaStream_t cuda_stream)
: first_read_id_(host_cache.first_read_id())
, kmer_size_(host_cache.kmer_size())
, window_size_(host_cache.window_size())
: first_read_id_(index_host_copy.first_read_id())
, kmer_size_(index_host_copy.kmer_size())
, window_size_(index_host_copy.window_size())
, allocator_(allocator)
, representations_d_(allocator)
, read_ids_d_(allocator)
Expand All @@ -609,41 +609,41 @@ IndexGPU<SketchElementImpl>::IndexGPU(DefaultDeviceAllocator allocator,
, first_occurrence_of_representations_d_(allocator)
, cuda_stream_(cuda_stream)
{
number_of_reads_ = host_cache.number_of_reads();
number_of_basepairs_in_longest_read_ = host_cache.number_of_basepairs_in_longest_read();
number_of_reads_ = index_host_copy.number_of_reads();
number_of_basepairs_in_longest_read_ = index_host_copy.number_of_basepairs_in_longest_read();

//H2D- representations_d_ = host_cache.representations();
representations_d_.resize(host_cache.representations().size());
//H2D- representations_d_ = index_host_copy.representations();
representations_d_.resize(index_host_copy.representations().size());
representations_d_.shrink_to_fit();
cudautils::device_copy_n(host_cache.representations().data(), host_cache.representations().size(), representations_d_.data(), cuda_stream);
cudautils::device_copy_n(index_host_copy.representations().data(), index_host_copy.representations().size(), representations_d_.data(), cuda_stream);

//H2D- read_ids_d_ = host_cache.read_ids();
read_ids_d_.resize(host_cache.read_ids().size());
//H2D- read_ids_d_ = index_host_copy.read_ids();
read_ids_d_.resize(index_host_copy.read_ids().size());
read_ids_d_.shrink_to_fit();
cudautils::device_copy_n(host_cache.read_ids().data(), host_cache.read_ids().size(), read_ids_d_.data(), cuda_stream);
cudautils::device_copy_n(index_host_copy.read_ids().data(), index_host_copy.read_ids().size(), read_ids_d_.data(), cuda_stream);

//H2D- positions_in_reads_d_ = host_cache.positions_in_reads();
positions_in_reads_d_.resize(host_cache.positions_in_reads().size());
//H2D- positions_in_reads_d_ = index_host_copy.positions_in_reads();
positions_in_reads_d_.resize(index_host_copy.positions_in_reads().size());
positions_in_reads_d_.shrink_to_fit();
cudautils::device_copy_n(host_cache.positions_in_reads().data(), host_cache.positions_in_reads().size(), positions_in_reads_d_.data(), cuda_stream);
cudautils::device_copy_n(index_host_copy.positions_in_reads().data(), index_host_copy.positions_in_reads().size(), positions_in_reads_d_.data(), cuda_stream);

//H2D- directions_of_reads_d_ = host_cache.directions_of_reads();
directions_of_reads_d_.resize(host_cache.directions_of_reads().size());
//H2D- directions_of_reads_d_ = index_host_copy.directions_of_reads();
directions_of_reads_d_.resize(index_host_copy.directions_of_reads().size());
directions_of_reads_d_.shrink_to_fit();
cudautils::device_copy_n(host_cache.directions_of_reads().data(), host_cache.directions_of_reads().size(), directions_of_reads_d_.data(), cuda_stream);
cudautils::device_copy_n(index_host_copy.directions_of_reads().data(), index_host_copy.directions_of_reads().size(), directions_of_reads_d_.data(), cuda_stream);

//H2D- unique_representations_d_ = host_cache.unique_representations();
unique_representations_d_.resize(host_cache.unique_representations().size());
//H2D- unique_representations_d_ = index_host_copy.unique_representations();
unique_representations_d_.resize(index_host_copy.unique_representations().size());
unique_representations_d_.shrink_to_fit();
cudautils::device_copy_n(host_cache.unique_representations().data(), host_cache.unique_representations().size(), unique_representations_d_.data(), cuda_stream);
cudautils::device_copy_n(index_host_copy.unique_representations().data(), index_host_copy.unique_representations().size(), unique_representations_d_.data(), cuda_stream);

//H2D- first_occurrence_of_representations_d_ = host_cache.first_occurrence_of_representations();
first_occurrence_of_representations_d_.resize(host_cache.first_occurrence_of_representations().size());
//H2D- first_occurrence_of_representations_d_ = index_host_copy.first_occurrence_of_representations();
first_occurrence_of_representations_d_.resize(index_host_copy.first_occurrence_of_representations().size());
first_occurrence_of_representations_d_.shrink_to_fit();
cudautils::device_copy_n(host_cache.first_occurrence_of_representations().data(), host_cache.first_occurrence_of_representations().size(), first_occurrence_of_representations_d_.data(), cuda_stream);
cudautils::device_copy_n(index_host_copy.first_occurrence_of_representations().data(), index_host_copy.first_occurrence_of_representations().size(), first_occurrence_of_representations_d_.data(), cuda_stream);

read_id_to_read_name_ = host_cache.read_id_to_read_names(); //H2H
read_id_to_read_length_ = host_cache.read_id_to_read_lengths(); //H2H
read_id_to_read_name_ = index_host_copy.read_id_to_read_names(); //H2H
read_id_to_read_length_ = index_host_copy.read_id_to_read_lengths(); //H2H

// This is not completely necessary, but if removed one has to make sure that the next step
// uses the same stream or that sync is done in caller
Expand Down
42 changes: 21 additions & 21 deletions cudamapper/src/host_cache.cu → cudamapper/src/index_host_copy.cu
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

#include <thrust/copy.h>
#include "host_cache.cuh"
#include "index_host_copy.cuh"
#include "index_gpu.cuh"
#include "minimizer.hpp"

Expand All @@ -18,11 +18,11 @@ namespace claragenomics
namespace cudamapper
{

HostCache::HostCache(const Index& index,
const read_id_t first_read_id,
const std::uint64_t kmer_size,
const std::uint64_t window_size,
const cudaStream_t cuda_stream)
IndexHostCopy::IndexHostCopy(const Index& index,
const read_id_t first_read_id,
const std::uint64_t kmer_size,
const std::uint64_t window_size,
const cudaStream_t cuda_stream)
: first_read_id_(first_read_id)
, kmer_size_(kmer_size)
, window_size_(window_size)
Expand Down Expand Up @@ -76,75 +76,75 @@ HostCache::HostCache(const Index& index,
CGA_CU_CHECK_ERR(cudaStreamSynchronize(cuda_stream));
}

std::unique_ptr<Index> HostCache::copy_index_to_device(DefaultDeviceAllocator allocator,
const cudaStream_t cuda_stream)
std::unique_ptr<Index> IndexHostCopy::copy_index_to_device(DefaultDeviceAllocator allocator,
const cudaStream_t cuda_stream)
{
return std::make_unique<IndexGPU<Minimizer>>(allocator,
*this,
cuda_stream);
}

const std::vector<representation_t>& HostCache::representations() const
const std::vector<representation_t>& IndexHostCopy::representations() const
{
return representations_;
}

const std::vector<read_id_t>& HostCache::read_ids() const
const std::vector<read_id_t>& IndexHostCopy::read_ids() const
{
return read_ids_;
}

const std::vector<position_in_read_t>& HostCache::positions_in_reads() const
const std::vector<position_in_read_t>& IndexHostCopy::positions_in_reads() const
{
return positions_in_reads_;
}

const std::vector<SketchElement::DirectionOfRepresentation>& HostCache::directions_of_reads() const
const std::vector<SketchElement::DirectionOfRepresentation>& IndexHostCopy::directions_of_reads() const
{
return directions_of_reads_;
}

const std::vector<representation_t>& HostCache::unique_representations() const
const std::vector<representation_t>& IndexHostCopy::unique_representations() const
{
return unique_representations_;
}

const std::vector<std::uint32_t>& HostCache::first_occurrence_of_representations() const
const std::vector<std::uint32_t>& IndexHostCopy::first_occurrence_of_representations() const
{
return first_occurrence_of_representations_;
}

const std::vector<std::string>& HostCache::read_id_to_read_names() const
const std::vector<std::string>& IndexHostCopy::read_id_to_read_names() const
{
return read_id_to_read_name_;
}

const std::vector<std::uint32_t>& HostCache::read_id_to_read_lengths() const
const std::vector<std::uint32_t>& IndexHostCopy::read_id_to_read_lengths() const
{
return read_id_to_read_length_;
}

read_id_t HostCache::number_of_reads() const
read_id_t IndexHostCopy::number_of_reads() const
{
return number_of_reads_;
}

position_in_read_t HostCache::number_of_basepairs_in_longest_read() const
position_in_read_t IndexHostCopy::number_of_basepairs_in_longest_read() const
{
return number_of_basepairs_in_longest_read_;
}

read_id_t HostCache::first_read_id() const
read_id_t IndexHostCopy::first_read_id() const
{
return first_read_id_;
}

std::uint64_t HostCache::kmer_size() const
std::uint64_t IndexHostCopy::kmer_size() const
{
return kmer_size_;
}

std::uint64_t HostCache::window_size() const
std::uint64_t IndexHostCopy::window_size() const
{
return window_size_;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ namespace claragenomics
{
namespace cudamapper
{
/// HostCache - Creates and maintains a copy of computed IndexGPU elements on the host
/// IndexHostCopy - Creates and maintains a copy of computed IndexGPU elements on the host
///
///
class HostCache : public IndexHostCopy
class IndexHostCopy : public IndexHostCopyBase
{
public:
/// \brief Constructor
Expand All @@ -30,11 +30,11 @@ public:
/// \param window_size the number of adjacent k-mers in a window, adjacent = shifted by one basepair
/// \param cuda_stream D2H copy is done on this stream
/// \return - pointer to claragenomics::cudamapper::IndexCache
explicit HostCache(const Index& index,
const read_id_t first_read_id,
const std::uint64_t kmer_size,
const std::uint64_t window_size,
const cudaStream_t cuda_stream);
IndexHostCopy(const Index& index,
const read_id_t first_read_id,
const std::uint64_t kmer_size,
const std::uint64_t window_size,
const cudaStream_t cuda_stream);

/// \brief copy cached index vectors from the host and create an object of Index on GPU
/// \param allocator pointer to asynchronous device allocator
Expand Down
12 changes: 6 additions & 6 deletions cudamapper/src/main.cu
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ int main(int argc, char* argv[])
}

// This is host cache, if it has the index it will copy it to device, if not it will generate on device and add it to host cache
std::map<std::pair<uint64_t, uint64_t>, std::shared_ptr<claragenomics::cudamapper::IndexHostCopy>> host_index_cache;
std::map<std::pair<uint64_t, uint64_t>, std::shared_ptr<claragenomics::cudamapper::IndexHostCopyBase>> host_index_cache;

// This is a per-device cache, if it has the index it will return it, if not it will generate it, store and return it.
std::vector<std::map<std::pair<uint64_t, uint64_t>, std::shared_ptr<claragenomics::cudamapper::Index>>> device_index_cache(parameters.num_devices);
Expand Down Expand Up @@ -399,11 +399,11 @@ int main(int argc, char* argv[])
else if (get_size<int32_t>(host_index_cache) < parameters.max_index_cache_size_on_host && allow_cache_index && device_id == 0)
{
// if not cached on device, update host cache; only done on device 0 to avoid any race conditions in updating the host cache
host_index_cache[key] = claragenomics::cudamapper::IndexHostCopy::create_cache(*index,
start_index,
k,
w,
cuda_stream);
host_index_cache[key] = claragenomics::cudamapper::IndexHostCopyBase::create_cache(*index,
start_index,
k,
w,
cuda_stream);
}
}
return index;
Expand Down

0 comments on commit 0cb8890

Please sign in to comment.