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 8750601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:53:46+00:00 2026-06-13T12:53:46+00:00

I am working on CUDA and facing the following problem. I have a following

  • 0

I am working on CUDA and facing the following problem.
I have a following structure of arrays:

typedef struct Edge{
    int *from, *to, *weight;
}

I want to sort this structure on weight array such that the corresponding “from” and “to” arrays get updated too. I thought of using Thrust library but it works only on vectors is what I understood. I can sort_by_key and get two arrays sorted but I am not able to understand how to sort three arrays? I even looked at zip_iterator but did not understand how to use it to serve my purpose. Please help

  • 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-13T12:53:47+00:00Added an answer on June 13, 2026 at 12:53 pm

    First decouple the structure into 1) keys, and 2) paddings. Then sort the keys and reorder paddings accordingly. For example, break this structure:

    typedef struct Edge{
        int from, to, weight;
    }
    

    into:

    int weight[N];
    typedef struct Edge{
        int from, to;
    }
    

    The full code is here:

    #include <thrust/device_vector.h>
    #include <thrust/host_vector.h>
    #include <cmath>
    #include <thrust/sort.h>
    
    typedef struct pad {
            int from;
            int to;
    } padd;
    
    __host__ padd randPad() {
            padd p;
            p.from = rand();
            p.to = rand();
            return p;
    }
    
    __host__ std::ostream& operator<< (std::ostream& os, const padd& p) {
            os << "(" << p.to << " , " << p.from << " )";
            return os;
    }
    
    int main(void)
    {
      // allocation
      #define N 4
      thrust::host_vector<int> h_keys(4);
      thrust::host_vector<padd> h_pad(4);
    
      // initilization
      thrust::generate(h_keys.begin(), h_keys.end(), rand);
      thrust::generate(h_pad.begin(), h_pad.end(), randPad);
    
      // print unsorted data
      std::cout<<"Unsorted keys\n";
      thrust::copy(h_keys.begin(), h_keys.end(), std::ostream_iterator<int>(std::cout, "\n"));
      std::cout<<"\nUnsorted paddings\n";
      thrust::copy(h_pad.begin(), h_pad.end(), std::ostream_iterator<padd>(std::cout, "\n"));
    
      // transfer to device
      thrust::device_vector<int> d_keys = h_keys;
      thrust::device_vector<padd> d_pad = h_pad;
      //thrust::sort(d_keys.begin(), d_keys.end());
    
      // sort
      thrust::sort_by_key(d_keys.begin(), d_keys.end(), d_pad.begin());
    
      // transfer back to host
      thrust::copy(d_keys.begin(), d_keys.end(), h_keys.begin());
      thrust::copy(d_pad.begin(), d_pad.end(), h_pad.begin());
    
      // print the results
      std::cout<<"\nSorted keys\n";
      thrust::copy(h_keys.begin(), h_keys.end(), std::ostream_iterator<int>(std::cout, "\n"));
      std::cout<<"\nSorted paddings\n";
      thrust::copy(h_pad.begin(), h_pad.end(), std::ostream_iterator<padd>(std::cout, "\n"));
    
      return 0;
    }
    

    The output would be something like this:

    Unsorted keys
    1804289383
    846930886
    1681692777
    1714636915
    
    Unsorted paddings
    (424238335 , 1957747793 )
    (1649760492 , 719885386 )
    (1189641421 , 596516649 )
    (1350490027 , 1025202362 )
    
    Sorted keys
    846930886
    1681692777
    1714636915
    1804289383
    
    Sorted paddings
    (1649760492 , 719885386 )
    (1189641421 , 596516649 )
    (1350490027 , 1025202362 )
    (424238335 , 1957747793 )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on CUDA and I have a problem related to thread synchronization.
I have four CUDA kernels working on matrices in the following way: convolution<<<>>>(A,B); multiplybyElement1<<<>>>(B);
I am new to CUDA programming, and I am working on a problem that
I have the following code written to make my lazy second CPU core working.
My problem is the following: I have an image in which I detect some
Do you have any experience working with CUDA on an enabled GPU that uses
I have a CUDA application I'm working on with an array of Objects; each
Working in iOS 5 I have read a list of file names from my
Working in C11, the following struct: struct S { unsigned a : 4; _Bool
I have just updated to the latest (release) CUDA 4.1 from 4.0, and none

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.