File: permutation_iterator.cu

package info (click to toggle)
cccl 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,248 kB
  • sloc: cpp: 264,457; python: 6,421; sh: 2,762; perl: 460; makefile: 114; xml: 13
file content (37 lines) | stat: -rw-r--r-- 906 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <thrust/device_vector.h>
#include <thrust/iterator/permutation_iterator.h>
#include <thrust/reduce.h>

#include <iostream>

// this example fuses a gather operation with a reduction for
// greater efficiency than separate gather() and reduce() calls

int main()
{
  // gather locations
  thrust::device_vector<int> map(4);
  map[0] = 3;
  map[1] = 1;
  map[2] = 0;
  map[3] = 5;

  // array to gather from
  thrust::device_vector<int> source(6);
  source[0] = 10;
  source[1] = 20;
  source[2] = 30;
  source[3] = 40;
  source[4] = 50;
  source[5] = 60;

  // fuse gather with reduction:
  //   sum = source[map[0]] + source[map[1]] + ...
  int sum = thrust::reduce(thrust::make_permutation_iterator(source.begin(), map.begin()),
                           thrust::make_permutation_iterator(source.begin(), map.end()));

  // print sum
  std::cout << "sum is " << sum << std::endl;

  return 0;
}