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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
// cuda.cu -*- C++ -*-
#include <cuda_runtime.h>
#include <cuda.h>
#include <cub/device/device_radix_sort.cuh>
#include <cub/device/device_reduce.cuh>
#include <stdint.h>
#include "utils.h"
// #define K 100
extern "C" int
process_transactions (uint64_t *h_keys, float *h_vals, size_t count)
{
// CUresult cuerr = cuInit(0);
// if (cuerr != CUDA_SUCCESS) {
// DEBUG ("cuInit: %d\n", cuerr);
// return 0;
// }
cudaError_t err;
// int device_count = 0;
// err = cudaGetDeviceCount (&device_count);
// if (err != cudaSuccess) {
// DEBUG ("cudaGetDeviceCount: %s", cudaGetErrorString (err));
// return 0;
// }
// printf ("CUDA Device Count: %d\n", device_count);
// if (device_count == 0) {
// printf("ERROR: No CUDA devices found!\n");
// return 0;
// }
// cudaSetDevice (0);
uint64_t *d_in_keys = nullptr, *d_out_keys = nullptr;
float *d_in_vals = nullptr, *d_out_vals = nullptr;
cudaMalloc (&d_in_keys, count*sizeof (*d_in_keys));
cudaMalloc (&d_in_vals, count*sizeof (*d_in_vals));
cudaMemcpy (d_in_keys, h_keys, count*sizeof (*h_keys), cudaMemcpyHostToDevice);
cudaMemcpy (d_in_vals, h_vals, count*sizeof (*h_vals), cudaMemcpyHostToDevice);
cudaMalloc (&d_out_keys, count*sizeof (*d_out_keys));
cudaMalloc (&d_out_vals, count*sizeof (*d_out_vals));
void *d_temp = nullptr;
size_t temp_size = 0;
err = cub::DeviceRadixSort::SortPairs (d_temp, temp_size,
d_in_keys, d_out_keys,
d_in_vals, d_out_vals, count);
// if (err != cudaSuccess) {
// DEBUG ("SortPairs: %s", cudaGetErrorString (err));
// return 0;
// }
cudaMalloc (&d_temp, temp_size);
err = cub::DeviceRadixSort::SortPairs (d_temp, temp_size,
d_in_keys, d_out_keys,
d_in_vals, d_out_vals, count);
// if (err != cudaSuccess) {
// DEBUG ("SortPairs: %s", cudaGetErrorString (err));
// return 0;
// }
cudaFree (d_in_keys);
cudaFree (d_in_vals);
cudaFree (d_temp);
uint64_t *d_unique_keys = nullptr;
float *d_unique_vals = nullptr;
int *d_unique_count = nullptr;
cudaMalloc (&d_unique_keys, count*sizeof (*d_unique_keys));
cudaMalloc (&d_unique_vals, count*sizeof (*d_unique_vals));
cudaMalloc (&d_unique_count, sizeof (*d_unique_count));
d_temp = nullptr;
temp_size = 0;
err = cub::DeviceReduce::ReduceByKey (d_temp, temp_size,
d_out_keys,
d_unique_keys,
d_out_vals,
d_unique_vals,
d_unique_count, cub::Sum (), count);
// if (err != cudaSuccess) {
// DEBUG ("SortPairs: %s", cudaGetErrorString (err));
// return 0;
// }
cudaMalloc (&d_temp, temp_size);
err = cub::DeviceReduce::ReduceByKey (d_temp, temp_size,
d_out_keys,
d_unique_keys,
d_out_vals,
d_unique_vals,
d_unique_count, cub::Sum (), count);
// if (err != cudaSuccess) {
// DEBUG ("SortPairs: %s", cudaGetErrorString (err));
// return 0;
// }
cudaFree (d_out_keys);
cudaFree (d_out_vals);
cudaFree (d_temp);
int h_unique_count;
cudaMemcpy (&h_unique_count, d_unique_count,
sizeof (h_unique_count), cudaMemcpyDeviceToHost);
cudaFree (d_unique_count);
cudaMemcpy (h_keys, d_unique_keys, h_unique_count*sizeof (*h_keys), cudaMemcpyDeviceToHost);
cudaMemcpy (h_vals, d_unique_vals, h_unique_count*sizeof (*h_vals), cudaMemcpyDeviceToHost);
cudaFree (d_unique_keys);
cudaFree (d_unique_vals);
return h_unique_count;
// requires cuda 13...
#if 0
if (h_unique_count <= K) {
cudaMemcpy (h_keys, d_unique_keys,
h_unique_count*sizeof (*h_keys), cudaMemcpyDeviceToHost);
cudaFree (d_unique_keys);
cudaMemcpy (h_vals, d_unique_vals,
h_unique_count*sizeof (*h_vals), cudaMemcpyDeviceToHost);
cudaFree (d_unique_vals);
return h_unique_count;
}
uint64_t *d_top_keys = nullptr;
float *d_top_vals = nullptr;
cudaMalloc (&d_top_keys, K*sizeof (*d_unique_keys));
cudaMalloc (&d_top_vals, K*sizeof (*d_unique_vals));
d_temp = nullptr;
temp_size = 0;
cub::DeviceTopK::MaxPairs (d_temp, temp_size,
d_unique_vals, d_top_vals,
d_unique_keys, d_top_keys,
h_unique_count, K, env);
cudaMalloc (&d_temp, temp_size);
cub::DeviceTopK::MaxPairs (d_temp, temp_size,
d_unique_vals, d_top_vals,
d_unique_keys, d_top_keys,
h_unique_count, K);
cudaFree (d_unique_keys);
cudaFree (d_unique_vals);
cudaFree (d_temp);
cudaMemcpy (h_keys, d_top_keys, K*sizeof (*h_keys), cudaMemcpyDeviceToHost);
cudaMemcpy (h_vals, d_top_vals, K*sizeof (*h_vals), cudaMemcpyDeviceToHost);
cudaFree (d_top_keys);
cudaFree (d_top_vals);
return K;
#endif
}
// cuda.cu ends here
|