-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstable_sort_by_key_fault.cpp
51 lines (42 loc) · 1.5 KB
/
stable_sort_by_key_fault.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <thrust/sort.h>
#include <thrust/device_ptr.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
#include <hip/hip_runtime.h>
#include<iostream>
int main () {
const int N = 6;
int keys[N] = { 1, 4, 2, 8, 5, 7};
char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};
int* devKeys, *hostKeys;
char* devValues, *hostValues;
// allocations
hostKeys = (int *) malloc(sizeof(int) * 6);
hostValues = (char *) malloc(sizeof(char) * 6);
hipMalloc(&devKeys, sizeof(int) * 6);
hipMalloc(&devValues, sizeof(char) * 6);
// Copy input to device
hipMemcpy(devKeys, keys, sizeof(int) * 6, hipMemcpyHostToDevice);
hipMemcpy(devValues, values, sizeof(char) * 6, hipMemcpyHostToDevice);
thrust::device_ptr<int> Keys_(devKeys);
thrust::device_ptr<char> Values_(devValues);
thrust::stable_sort_by_key(thrust::device, Keys_, Keys_ + N, Values_, thrust::greater<int>());
// Copy results back to host
hipMemcpy(hostKeys, devKeys, sizeof(int) * 6, hipMemcpyDeviceToHost);
hipMemcpy(hostValues, devValues, sizeof(char) * 6, hipMemcpyDeviceToHost);
printf("\n Keys after sorting: \n");
for (int i = 0; i < 6; i++) {
printf("%d \t", hostKeys[i]);
}
printf("\n Values after sorting: \n");
for (int i = 0; i < 6; i++) {
printf("%c \t", hostValues[i]);
}
// keys is now { 8, 7, 5, 4, 2, 1}
// values is now {'d', 'f', 'e', 'b', 'c', 'a'}
// Free up resources
hipFree(devKeys);
hipFree(devValues);
free(hostKeys);
free(hostValues);
}