aboutsummaryrefslogtreecommitdiff
path: root/src/cuda.cu
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-10 08:29:58 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-10 08:33:56 +0000
commit776276d5c7e67356a880f3e31076d32cdc1c1882 (patch)
treecbde131f3d46338adee9c67ac0f988295736bdd4 /src/cuda.cu
downloadcoursework-master.tar.gz
publishHEADmaster
Diffstat (limited to 'src/cuda.cu')
-rw-r--r--src/cuda.cu157
1 files changed, 157 insertions, 0 deletions
diff --git a/src/cuda.cu b/src/cuda.cu
new file mode 100644
index 0000000..fb59c57
--- /dev/null
+++ b/src/cuda.cu
@@ -0,0 +1,157 @@
1// cuda.cu -*- C++ -*-
2#include <cuda_runtime.h>
3#include <cuda.h>
4#include <cub/device/device_radix_sort.cuh>
5#include <cub/device/device_reduce.cuh>
6#include <stdint.h>
7#include "utils.h"
8
9// #define K 100
10
11extern "C" int
12process_transactions (uint64_t *h_keys, float *h_vals, size_t count)
13{
14 // CUresult cuerr = cuInit(0);
15 // if (cuerr != CUDA_SUCCESS) {
16 // DEBUG ("cuInit: %d\n", cuerr);
17 // return 0;
18 // }
19
20 cudaError_t err;
21 // int device_count = 0;
22 // err = cudaGetDeviceCount (&device_count);
23 // if (err != cudaSuccess) {
24 // DEBUG ("cudaGetDeviceCount: %s", cudaGetErrorString (err));
25 // return 0;
26 // }
27 // printf ("CUDA Device Count: %d\n", device_count);
28
29 // if (device_count == 0) {
30 // printf("ERROR: No CUDA devices found!\n");
31 // return 0;
32 // }
33
34 // cudaSetDevice (0);
35
36 uint64_t *d_in_keys = nullptr, *d_out_keys = nullptr;
37 float *d_in_vals = nullptr, *d_out_vals = nullptr;
38
39 cudaMalloc (&d_in_keys, count*sizeof (*d_in_keys));
40 cudaMalloc (&d_in_vals, count*sizeof (*d_in_vals));
41
42 cudaMemcpy (d_in_keys, h_keys, count*sizeof (*h_keys), cudaMemcpyHostToDevice);
43 cudaMemcpy (d_in_vals, h_vals, count*sizeof (*h_vals), cudaMemcpyHostToDevice);
44
45 cudaMalloc (&d_out_keys, count*sizeof (*d_out_keys));
46 cudaMalloc (&d_out_vals, count*sizeof (*d_out_vals));
47
48 void *d_temp = nullptr;
49 size_t temp_size = 0;
50 err = cub::DeviceRadixSort::SortPairs (d_temp, temp_size,
51 d_in_keys, d_out_keys,
52 d_in_vals, d_out_vals, count);
53 // if (err != cudaSuccess) {
54 // DEBUG ("SortPairs: %s", cudaGetErrorString (err));
55 // return 0;
56 // }
57 cudaMalloc (&d_temp, temp_size);
58 err = cub::DeviceRadixSort::SortPairs (d_temp, temp_size,
59 d_in_keys, d_out_keys,
60 d_in_vals, d_out_vals, count);
61 // if (err != cudaSuccess) {
62 // DEBUG ("SortPairs: %s", cudaGetErrorString (err));
63 // return 0;
64 // }
65 cudaFree (d_in_keys);
66 cudaFree (d_in_vals);
67 cudaFree (d_temp);
68
69 uint64_t *d_unique_keys = nullptr;
70 float *d_unique_vals = nullptr;
71 int *d_unique_count = nullptr;
72 cudaMalloc (&d_unique_keys, count*sizeof (*d_unique_keys));
73 cudaMalloc (&d_unique_vals, count*sizeof (*d_unique_vals));
74 cudaMalloc (&d_unique_count, sizeof (*d_unique_count));
75 d_temp = nullptr;
76 temp_size = 0;
77 err = cub::DeviceReduce::ReduceByKey (d_temp, temp_size,
78 d_out_keys,
79 d_unique_keys,
80 d_out_vals,
81 d_unique_vals,
82 d_unique_count, cub::Sum (), count);
83 // if (err != cudaSuccess) {
84 // DEBUG ("SortPairs: %s", cudaGetErrorString (err));
85 // return 0;
86 // }
87 cudaMalloc (&d_temp, temp_size);
88 err = cub::DeviceReduce::ReduceByKey (d_temp, temp_size,
89 d_out_keys,
90 d_unique_keys,
91 d_out_vals,
92 d_unique_vals,
93 d_unique_count, cub::Sum (), count);
94 // if (err != cudaSuccess) {
95 // DEBUG ("SortPairs: %s", cudaGetErrorString (err));
96 // return 0;
97 // }
98 cudaFree (d_out_keys);
99 cudaFree (d_out_vals);
100 cudaFree (d_temp);
101
102 int h_unique_count;
103 cudaMemcpy (&h_unique_count, d_unique_count,
104 sizeof (h_unique_count), cudaMemcpyDeviceToHost);
105 cudaFree (d_unique_count);
106
107 cudaMemcpy (h_keys, d_unique_keys, h_unique_count*sizeof (*h_keys), cudaMemcpyDeviceToHost);
108 cudaMemcpy (h_vals, d_unique_vals, h_unique_count*sizeof (*h_vals), cudaMemcpyDeviceToHost);
109
110 cudaFree (d_unique_keys);
111 cudaFree (d_unique_vals);
112
113 return h_unique_count;
114
115 // requires cuda 13...
116#if 0
117 if (h_unique_count <= K) {
118 cudaMemcpy (h_keys, d_unique_keys,
119 h_unique_count*sizeof (*h_keys), cudaMemcpyDeviceToHost);
120 cudaFree (d_unique_keys);
121
122 cudaMemcpy (h_vals, d_unique_vals,
123 h_unique_count*sizeof (*h_vals), cudaMemcpyDeviceToHost);
124 cudaFree (d_unique_vals);
125
126 return h_unique_count;
127 }
128
129 uint64_t *d_top_keys = nullptr;
130 float *d_top_vals = nullptr;
131 cudaMalloc (&d_top_keys, K*sizeof (*d_unique_keys));
132 cudaMalloc (&d_top_vals, K*sizeof (*d_unique_vals));
133 d_temp = nullptr;
134 temp_size = 0;
135 cub::DeviceTopK::MaxPairs (d_temp, temp_size,
136 d_unique_vals, d_top_vals,
137 d_unique_keys, d_top_keys,
138 h_unique_count, K, env);
139 cudaMalloc (&d_temp, temp_size);
140 cub::DeviceTopK::MaxPairs (d_temp, temp_size,
141 d_unique_vals, d_top_vals,
142 d_unique_keys, d_top_keys,
143 h_unique_count, K);
144 cudaFree (d_unique_keys);
145 cudaFree (d_unique_vals);
146 cudaFree (d_temp);
147
148 cudaMemcpy (h_keys, d_top_keys, K*sizeof (*h_keys), cudaMemcpyDeviceToHost);
149 cudaMemcpy (h_vals, d_top_vals, K*sizeof (*h_vals), cudaMemcpyDeviceToHost);
150
151 cudaFree (d_top_keys);
152 cudaFree (d_top_vals);
153
154 return K;
155#endif
156}
157// cuda.cu ends here