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

  • SEARCH
  • Home
  • 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 7187173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:47:13+00:00 2026-05-28T18:47:13+00:00

In my project, I have implemented a custom memory allocator to avoid unneccessary calls

  • 0

In my project, I have implemented a custom memory allocator to avoid unneccessary calls to cudaMalloc once the application has “warmed up”. Moreover, I use custom kernels for basic array filling, arithmetic operations between arrays, etc. and would like to simplify my code by using Thrust and getting rid of these kernels. Every array on the device is created and accessed through raw pointers (for now) and I’d like to use device_vector and Thrusts methods on these objects, but I find myself converting between raw pointers and device_ptr<> all the time, somewhat cluttering up my code.

My rather vague question: How would/do you organize the usage of custom memory management, Thrusts array methods and calls to custom kernels in the most readable way?

  • 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-05-28T18:47:13+00:00Added an answer on May 28, 2026 at 6:47 pm

    Like all standard c++ containers, you can customize how thrust::device_vector allocates storage by providing it with your own “allocator”. By default, thrust::device_vector‘s allocator is thrust::device_malloc_allocator, which allocates (deallocates) storage with cudaMalloc (cudaFree) when Thrust’s backend system is CUDA.

    Occasionally, it is desirable to customize the way device_vector allocates memory, such as in the OP’s case, who would like to sub-allocate storage within a single large allocation performed at program initialization. This can avoid overhead which may be incurred by many individual calls to the underlying allocation scheme, in this case, cudaMalloc.

    A simple way to provide device_vector a custom allocator is to inherit from device_malloc_allocator. One could in principle author an entire allocator from scratch, but with an inheritance approach, only the allocate and deallocate member functions need to be provided. Once the custom allocator is defined, it can be provided to device_vector as its second template parameter.

    This example code demonstrates how to provide a custom allocator which prints a message upon allocation and deallocation:

    #include <thrust/device_malloc_allocator.h>
    #include <thrust/device_vector.h>
    #include <iostream>
    
    template<typename T>
      struct my_allocator : thrust::device_malloc_allocator<T>
    {
      // shorthand for the name of the base class
      typedef thrust::device_malloc_allocator<T> super_t;
    
      // get access to some of the base class's typedefs
    
      // note that because we inherited from device_malloc_allocator,
      // pointer is actually thrust::device_ptr<T>
      typedef typename super_t::pointer   pointer;
    
      typedef typename super_t::size_type size_type;
    
      // customize allocate
      pointer allocate(size_type n)
      {
        std::cout << "my_allocator::allocate(): Hello, world!" << std::endl;
    
        // defer to the base class to allocate storage for n elements of type T
        // in practice, you'd do something more interesting here
        return super_t::allocate(n);
      }
    
      // customize deallocate
      void deallocate(pointer p, size_type n)
      {
        std::cout << "my_allocator::deallocate(): Hello, world!" << std::endl;
    
        // defer to the base class to deallocate n elements of type T at address p
        // in practice, you'd do something more interesting here
        super_t::deallocate(p,n);
      }
    };
    
    int main()
    {
      // create a device_vector which uses my_allocator
      thrust::device_vector<int, my_allocator<int> > vec;
    
      // create 10 ints
      vec.resize(10, 13);
    
      return 0;
    }
    

    Here’s the output:

    $ nvcc my_allocator_test.cu -arch=sm_20 -run
    my_allocator::allocate(): Hello, world!
    my_allocator::deallocate(): Hello, world!
    

    In this example, note that we hear from my_allocator::allocate() once upon vec.resize(10,13). my_allocator::deallocate() is invoked once when vec goes out of scope as it destroys its elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a c++ custom action for a setup-project application in VS Team
I have a Web Application Project and implemented profile properties in the web.config. When
In my project I have the following three interfaces, which are implemented by classes
I have a school project in which I have to implement a chat application,
I have a custom tool that uses svn.exe for committing files, and my project
I have implemented a custom membership provider using LINQ to SQL. When I added
I have a custom project type based on the C# Class Library project type
I have a flex project that is made of up several custom components that
n00b here (first Android project). I have been given a custom video codec that
I´m having a problem with my Android Project. I have a screen filling custom

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.