开发者

How do you make a pair vector out of two arrays and then sort by the first element of the pair using CUDA/Thrust?

开发者 https://www.devze.com 2023-03-27 12:53 出处:网络
Okay, this is going to be a mouthful. I have a pointer to serialized 2D array, pointer arithmetic aside, I want to make a vector of pairs out of (essentially) two arrays(that I plan to get out of the

Okay, this is going to be a mouthful.

I have a pointer to serialized 2D array, pointer arithmetic aside, I want to make a vector of pairs out of (essentially) two arrays(that I plan to get out of the 2D array). H开发者_JS百科owever, I need to use the Thrust library.

This is so that I can use thrust::sort() on the vector, based on the values of the first element of the pair. On the Device.

So, I need a vector(preferably thrust::device_vector) of pairs (of size to match the size of arrayOne and arrayTwo obviously), where the first element of the pair is from arrayOne, the second from arrayTwo. So that I can then use thrust::sort() in order to sort by the first element of the pair.

Sorry, for the lack of code, but I'm still trying to figure out the finer details of how to implement this, hence the question. I'll post my attempts as I go along. Thank you in advance!

#UPDATE

I think I actually got lucky and found the solution since I posted the question(sort of, it a better solution than using pair - which in turn was suggested to me from a previous question I asked), turns out Thrust actually provides exactly what I'm looking for by default:

#include <thrust/sort.h>
  ...
  const int N = 6;
  int    keys[N] = {  1,   4,   2,   8,   5,   7};
  char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};
  thrust::sort_by_key(keys, keys + N, values);
  // keys is now   {  1,   2,   4,   5,   7,   8}
  // values is now {'a', 'c', 'b', 'e', 'f', 'd'}

*taken from http://code.google.com/p/thrust/wiki/QuickStartGuide#Fancy_Iterators*

So, now all I have to do is get two thrust::device_vectors out of the two arrays (that I have to get out of the 2D array). Happy.


The original poster found a solution using thrust::sort_by_key as follows:

#include <thrust/sort.h>
  ...
  const int N = 6;
  int    keys[N] = {  1,   4,   2,   8,   5,   7};
  char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};
  thrust::sort_by_key(keys, keys + N, values);
  // keys is now   {  1,   2,   4,   5,   7,   8}
  // values is now {'a', 'c', 'b', 'e', 'f', 'd'}

which was taken from http://code.google.com/p/thrust/wiki/QuickStartGuide#Fancy_Iterators

The application in the question will require creating a pair of input iterators providing the key and value vectors from the original pitched array mentioned in the question.

This answers was added as a community wiki answer to get this question off the unanswered list

0

精彩评论

暂无评论...
验证码 换一张
取 消