Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8829089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:45:18+00:00 2026-06-14T07:45:18+00:00

I have just started using thrust and one of the biggest issues I have

  • 0

I have just started using thrust and one of the biggest issues I have so far is that there seems to be no documentation as to how much memory operations require. So I am not sure why the code below is throwing bad_alloc when trying to sort (before the sorting I still have >50% of GPU memory available, and I have 70GB of RAM available on the CPU)–can anyone shed some light on this?

#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <thrust/random.h>

void initialize_data(thrust::device_vector<uint64_t>& data) {
  thrust::fill(data.begin(), data.end(), 10);
}

int main(void) {
  size_t N = 120 * 1024 * 1024;
  char line[256];
  try {
    std::cout << "device_vector" << std::endl;
    typedef thrust::device_vector<uint64_t>  vec64_t;

    // Each buffer is 900MB

    vec64_t c[3] = {vec64_t(N), vec64_t(N), vec64_t(N)};
    initialize_data(c[0]);
    initialize_data(c[1]);
    initialize_data(c[2]);

    std::cout << "initialize_data finished... Press enter";
    std::cin.getline(line, 0);

    // nvidia-smi reports 48% memory usage at this point (2959MB of                 
    // 6143MB)

    std::cout << "sort_by_key col 0" << std::endl;

    // throws bad_alloc

    thrust::sort_by_key(c[0].begin(), c[0].end(),
      thrust::make_zip_iterator(thrust::make_tuple(c[1].begin(),
      c[2].begin())));

    std::cout << "sort_by_key col 1" << std::endl;
    thrust::sort_by_key(c[1].begin(), c[1].end(),
        thrust::make_zip_iterator(thrust::make_tuple(c[0].begin(),
        c[2].begin())));
  } catch(thrust::system_error &e) {
    std::cerr << "Error: " << e.what() << std::endl;
    exit(-1);
  }
  return 0;
}

This is how I compiled the code

nvcc -o ./bad_alloc ./bad_alloc.cu
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:45:19+00:00Added an answer on June 14, 2026 at 7:45 am

    Taking into account Robert Crovella’s comment, this is how the code works for me using cudaMemGetInfo() to use 39% of the GPU RAM (this is on a nvidia tesla card with ECC disabled, otherwise the value would need to be lower).

    #include <thrust/device_vector.h>
    #include <thrust/sort.h>
    #include <thrust/random.h>
    
    void initialize_data(thrust::device_vector<uint64_t>& data) {
      thrust::fill(data.begin(), data.end(), 10); }
    
    #define BUFFERS 3
    
    int main(void) {                                                                  
      size_t total_gpu_bytes;
      cudaMemGetInfo(0, &total_gpu_bytes);
      size_t N = (total_gpu_bytes * .39) / sizeof(uint64_t) / BUFFERS;
      try {
        std::cout << "device_vector " << (N/1024.0/1024.0) << std::endl;
        typedef thrust::device_vector<uint64_t>  vec64_t;
        vec64_t c[BUFFERS] = {vec64_t(N), vec64_t(N), vec64_t(N)};
        initialize_data(c[0]);
        initialize_data(c[1]);
        initialize_data(c[2]);
        thrust::sort_by_key(c[0].begin(), c[0].end(),
            thrust::make_zip_iterator(thrust::make_tuple(c[1].begin(),
            c[2].begin())));
        thrust::sort_by_key(c[1].begin(), c[1].end(),
            thrust::make_zip_iterator(thrust::make_tuple(c[0].begin(),
            c[2].begin())));
      } catch(thrust::system_error &e) {
        std::cerr << "Error: " << e.what() << std::endl;
        exit(-1);
      }
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started using the Data Access Application Block from microsoft. There are
I have just started using Rhino mocks and I am having difficulty doing that.
I have just started using jqGrid , and I have some tables that I
At work we have just started using CodedUI, in our product there are a
I have just started using vc++ 2008. I just want to see one message
I have just started using UIDatePicker in my iPad app, but is there a
I have just started using cakePHP 2.1. After submiting a form. If there is
we have just started using a git account of our Django website project so
I have just started using RSpec and I copied the very simple test on
I have just started using flexigrid and wanted to use the resource string to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.