Skip to content

Commit

Permalink
Perception: fix Thrust bug by using CPU (ApolloAuto#13620)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroldchen authored Mar 29, 2021
1 parent fa4599a commit abf6d99
Showing 1 changed file with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
*/

// headers in CUDA
#include <thrust/device_ptr.h>
#include <thrust/host_vector.h>
#include <thrust/sort.h>

// headers in local files
Expand Down Expand Up @@ -245,11 +247,25 @@ void PostprocessCuda::DoPostprocessCuda(
GPU_CHECK(
cudaMalloc(reinterpret_cast<void**>(&dev_sorted_box_for_nms),
num_box_corners_ * host_filter_count[0] * sizeof(float)));
thrust::sequence(thrust::device, dev_indexes,
dev_indexes + host_filter_count[0]);
thrust::sort_by_key(thrust::device, dev_filtered_score,
dev_filtered_score + size_t(host_filter_count[0]),
dev_indexes, thrust::greater<float>());

thrust::device_ptr<float> dev_ptr_filtered_score(dev_filtered_score);
thrust::host_vector<float> host_filtered_score(host_filter_count[0]);
thrust::copy(dev_ptr_filtered_score,
dev_ptr_filtered_score + size_t(host_filter_count[0]),
host_filtered_score.begin());

thrust::host_vector<int> host_indexes(host_filter_count[0]);
thrust::sequence(host_indexes.begin(), host_indexes.end());

// TODO(chenjiahao): using GPU may cause crash, so use CPU here to sort,
// temporarily. Will change to GPU after upgrading CUDA in the future.
thrust::sort_by_key(host_filtered_score.begin(),
host_filtered_score.end(),
host_indexes.begin(), thrust::greater<float>());
GPU_CHECK(cudaMemcpy(dev_indexes,
thrust::raw_pointer_cast(host_indexes.data()),
host_filter_count[0] * sizeof(int),
cudaMemcpyHostToDevice));

const int num_blocks = DIVUP(host_filter_count[0], num_threads_);
sort_boxes_by_indexes_kernel<<<num_blocks, num_threads_>>>(
Expand Down

0 comments on commit abf6d99

Please sign in to comment.