-
Notifications
You must be signed in to change notification settings - Fork 1
/
kernel_MemLPA.cuh
304 lines (261 loc) · 9.41 KB
/
kernel_MemLPA.cuh
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#pragma once
#include "assert.h"
#include "stdio.h"
#include <algorithm>
#include <cuda.h>
#include <cuda_runtime.h>
#include "utils/logger.cuh"
#include "utils/rng.cuh"
#include "utils/timer.h"
#include <iostream>
#include <vector>
#include <ordered_map>
#include <random>
#include <cmath>
#include <bits/stdc++.h>
using namespace std;
/*! \brief File containing student code
students should only modify this file
*/
// You can use this kernel defination
/*
__global__ void GMM(float *A, float *B, float *C, size_t Arow, size_t Acol, size_t Bcol){
}
*/
// Define cuda kernel here
// Define cuda kernel here
// Define cuda kernel here
// Define cuda kernel here
__global__ void kernel_MemLPA(ordered_map::device_ptr<float> mem, float *d_Adj, int iteration, int n_nodes, float *current_labels, float *d_label_history_0, float *d_label_history_1, float *d_label_history_2, float *mean_label_score, float *stddev_label_score)
{
/*
mem stores the memory/scoreboard of the community labels for each node -- node->comm_label->score
current_labels is the previous label for current node
mean_label_score & stddev_label_score track the mean & std dev of the scores of all communities for each node for the last iteration
*/
int node = blockIdx.x * blockDim.x + threadIdx.x; // assuming number of threads = no of nodes
//int n_nodes = d_Adj.size();
if(node < n_nodes):
{
ordered_map<int, float> community_labels = mem[node]; // scoreboard for the node
// change this to adjacency list for faster access
for (int neighbor = 0; neighbor < n_nodes; neighbor++)
{
float edge = d_Adj[node][neighbor];
if (edge != 0)
{
community_labels[current_labels[neighbor]] += edge; // update scoreboard
}
}
// Code to find max scored label and assign it to labels vector
int max_key = -1;
float max_value = 0;
float M2 = 0;
float delta;
float rolling_mean = 0;
int n = 0;
flag_delete = 0; // turns to 1 if any community label has to be deleted
del_key = -1; //init
for (auto& kv : community_labels)
{
if (iteration!=1 && (kv.second < (mean_label_score[node]-stddev_label_score[node]))) // more than one std-dev away from mean, in the negative direction
{
if(flag_delete==1)
{
community_labels.erase(del_key);
}
flag_delete = 1;
del_key = kv.first;
continue;
}
delta = kv.second - rolling_mean;
rolling_mean += delta / (n + 1);
M2 += delta * (kv.second - rolling_mean); // finding mean & std dev on a rolling basis
n++; // counts no of community labels in the particular node's memory/scoreboard.
if (kv.second > max_value)
{
max_key = kv.first;
max_value = kv.second;
}
}
if(flag_delete==1)
{
community_labels.erase(del_key);
}
current_labels[node] = max_key;
mean_label_score[node] = rolling_mean;
stddev_label_score[node] = sqrtf(M2 / (n));
switch(iteration % 3) {
case 0:
d_label_history_0[node] = current_labels[node]// edit d_label_history_0
break;
case 1:
d_label_history_1[node] = current_labels[node]// edit d_label_history_1
break;
default:
d_label_history_2[node] = current_labels[node]// edit d_label_history_2
}
}
}
int main()
{
const int N = 100;
const int threadsPerBlock = 256;
const int blocksPerGrid = (N + threadsPerBlock - 1) / threadsPerBlock;
float* a, * b, * c, *d;
float* d_a, * d_b, * d_c, *d_d;
// Allocate memory on the host
a = (float*)malloc(N * sizeof(float));
b = (float*)malloc(N * sizeof(float));
c = (float*)malloc(N * sizeof(float));
d = (float*)malloc(N * sizeof(float));
// Allocate memory on the device
cudaMalloc(&d_a, N * sizeof(float));
cudaMalloc(&d_b, N * sizeof(float));
cudaMalloc(&d_c, N * sizeof(float));
cudaMalloc(&d_d, N * sizeof(float));
// Initialize arrays a and b
for (int i = 0; i < N; i++) {
a[i] = i;
b[i] = i;
c[i] = i;
}
// raw pointer to device memory
float* raw_ptr;
cudaMalloc((void**)&raw_ptr, N * sizeof(float));
// wrap raw pointer with a device_ptr
thrust::device_ptr<float> dev_ptr(raw_ptr);
// Copy arrays a and b to the device
cudaMemcpy(d_a, a, N * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_b, b, N * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_c, c, N * sizeof(float), cudaMemcpyHostToDevice);
// Perform vector addition on the device
vecAdd << <blocksPerGrid, threadsPerBlock >> > (d_a, d_b, d_c, d_d,N, dev_ptr);
float sum = thrust::reduce(dev_ptr, dev_ptr + N, (float)0);
printf("\n%f\n", sum);
// Copy the result back to the host
cudaMemcpy(d, d_d, N * sizeof(float), cudaMemcpyDeviceToHost);
for (int i = 0; i < N; i++) {
printf("%f\t", d[i]);
}
/*
int flag = 1;
// Verify the result
for (int i = 0; i < N; i++) {
if (c[i] != a[i] + b[i]) {
printf("Error: c[%d] = %f, expected %f\n", i, c[i], a[i] + b[i]);
flag = 0;
break;
}
}
printf("HELLO\n");
printf("%f\n",c[0]);
printf("%f\n", c[77]);
printf("%f\n", c[9999999]);
printf("%d", flag);
*/
// Free memory
free(a);
free(b);
free(c);
free(d);
cudaFree(d_a);
cudaFree(d_b);
cudaFree(d_c);
cudaFree(d_d);
return 0;
}
////////////////////////////////////////////////// 4/20/2023 5:47 PM
/*
void BasicGMM() //size_t matArow, size_t matAcol, size_t matBrow, size_t matBcol, float *hostA, float *hostB, float *hostC)
{
// &Adj points to adjcancy metrix on the host memory
cudaMalloc((void**)&d_Adj,n*m*sizeof(float)); // d_Adj is pointer to device Adjacency matrix
cudaMemcpy(&d_Adj,&Adj,n*m*sizeof(float),cudaMemcpyHostToDevice);
// cout<<matArow<<endl;
// cout<<hostA<<endl;
if (matAcol != matBrow)
{
Log(critical, "Incorrect matrix configuration");
return;
}
// device memory pointers
float *deviceA = nullptr, *deviceB = nullptr, *deviceC = nullptr;
size_t aSz = matArow * matAcol;
size_t bSz = matBrow * matBcol;
size_t cSz = matArow * matBcol;
//////////////////////////////////////////
// Allocate GPU Memory
//////////////////////////////////////////
Timer t;
Log(debug, "Allocating GPU memory");
// Fill this part
// Fill this part
// Fill this part
// Fill this part
// Fill this part
cudaMalloc((void**)&deviceA, aSz * sizeof(float));
cudaMalloc((void**)&deviceB, bSz * sizeof(float));
cudaMalloc((void**)&deviceC, cSz * sizeof(float));
double seconds = t.elapsed();
Log(debug, "done... %f sec\n\n", seconds);
//////////////////////////////////////////
// Copy Host Data to GPU
//////////////////////////////////////////
Log(debug, "Copying host memory to the GPU");
t.reset();
// Fill this part
// Fill this part
// Fill this part
// Fill this part
cudaMemcpy(deviceA, hostA, aSz * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(deviceB, hostB, bSz * sizeof(float), cudaMemcpyHostToDevice);
CUDA_RUNTIME(cudaMalloc((void **)&deviceC, cSz * sizeof(float)));
seconds = t.elapsed();
Log(debug, "done... %f sec\n\n", seconds);
//////////////////////////////////////////
// GPU M-M multiplication computation
//////////////////////////////////////////
Log(debug, "Performing GPU Matrix-Multiplication");
t.reset();
// Fill this part; call the kernel here (Remember: <<< >>>)
// Fill this part; call the kernel here (Remember: <<< >>>)
// Fill this part; call the kernel here (Remember: <<< >>>)
// Fill this part; call the kernel here (Remember: <<< >>>)
// Fill this part; call the kernel here (Remember: <<< >>>)
// referenced https://github.com/lzhengchun/matrix-cuda/blob/master/matrix_cuda.cu for understanding
int block_size = 16;
int rows = (matArow + block_size - 1) / block_size;
int cols = (matBcol + block_size - 1) / block_size;
dim3 dimGrid(cols, rows);
dim3 dimBlock(block_size, block_size);
kernel_MemLPA<<<dimBlock, dimGrid>>>(deviceA, deviceB, deviceC, matArow, matAcol, matBcol);
CUDA_RUNTIME(cudaDeviceSynchronize());
seconds = t.elapsed();
Log(debug, "done... %f sec\n\n", seconds);
//////////////////////////////////////////
// Copy GPU Data to Host
//////////////////////////////////////////
Log(debug, "Copying GPU memory to the host");
t.reset();
// Let me do this part for you
CUDA_RUNTIME(cudaMemcpy(hostC, deviceC, cSz * sizeof(float), cudaMemcpyDeviceToHost));
seconds = t.elapsed();
Log(debug, "done... %f sec\n\n", seconds);
for (int i = 0; i < matArow; i++) {
for (int j = 0; j < matBcol; j++) {
cout<<hostC[(i * matBcol) + j];
}
}
//////////////////////////////////////////
// Delete GPU memory
//////////////////////////////////////////
Log(debug, "Deleting GPU memory");
t.reset();
// Let me do this part for you
CUDA_RUNTIME(cudaFree(deviceA));
CUDA_RUNTIME(cudaFree(deviceB));
CUDA_RUNTIME(cudaFree(deviceC));
seconds = t.elapsed();
Log(debug, "done... %f sec\n\n", seconds);
}*/