-
Notifications
You must be signed in to change notification settings - Fork 0
/
priorityQueue.cu
177 lines (141 loc) · 3.64 KB
/
priorityQueue.cu
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
#include "cuda_runtime.h"
#include <cuda.h>
#include <stdio.h>
#include <bits/stdc++.h>
#include "Helper.h"
using namespace std;
#define BLOCK_SIZE 512
#define CUDA_FUNC __host__ __device__
#define MAX_QUEUE_SIZE 1000
class PriorityQueue {
private:
Node heapArr[MAX_QUEUE_SIZE];
unsigned int length;
public:
CUDA_FUNC PriorityQueue() {
this->length = 0;
}
CUDA_FUNC int compareNode(int a, int b) {
return this->compareNode(this->heapArr[a], this->heapArr[b]);
}
CUDA_FUNC int compareNode(Node &left, Node &right) {
int leftSum = left.HD + left.DT;
int rightSum = right.HD + right.DT;
return leftSum < rightSum;
}
CUDA_FUNC void swap(int a, int b) {
Node temp = this->heapArr[a];
this->heapArr[a] = this->heapArr[b];
this->heapArr[b] = temp;
}
CUDA_FUNC void increase_val(int i, Node val) {
// printf("asasdsa %d %d %d %d\n", i, val, this->heapArr[i], val < this->heapArr[i]);
if(this->heapArr[i].DT != -1 && this->compareNode(val, this->heapArr[i]))
return;
this->heapArr[i] = val;
// printf("insertion %d", this->heapArr[i]);
while(i>1 && this->compareNode(i/2, i))
{
this->swap(i/2, i);
i = i/2;
}
}
CUDA_FUNC void insert(Node val) {
Node NULL_NODE;
NULL_NODE.DT = -1;
this->length = this->length + 1;
this->heapArr[this->length] = NULL_NODE;
// printf("sdsfsdfdsfsd\n");
this->increase_val(this->length, val);
// printf("sdsfsdfdsfsd\n");
}
CUDA_FUNC void max_heapify(int i) {
int left = 2*i;
int right = 2*i+1;
int largest = 1;
if(left <= this->length && ! this->compareNode(left, i))
largest = left;
else
largest = i;
if(right <= this->length && ! this->compareNode(right, largest))
largest = right;
if(largest != i)
{
this->swap(i, largest);
this->max_heapify(largest);
}
}
CUDA_FUNC Node top()
{
if(this->length == 0) {
Node NULL_NODE;
NULL_NODE.DT = -1;
return NULL_NODE;
}
Node max = this->heapArr[1];
this->heapArr[1] = this->heapArr[this->length];
this->length--;
max_heapify(1);
// for(int i=0; i<this->length; i++) {
// printf("%d", this->heapArr[i]);
// }
return max;
}
CUDA_FUNC int getLength() {
return this->length;
}
// CUDA_FUNC void printHeap() {
// for(int i=0; i<this->length; i++) {
// printf("%d,%d\t", i, this->heapArr[i]);
// }
// printf("\n");
// }
};
#define K 10
__global__ void parAStar(PriorityQueue *pqC) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
// i=i+1;
if(i < K) {
Node val = pqC[i].top();
printf("%d %d %d\n", i, val.DT, val.HD);
__syncthreads();
val = pqC[i].top();
printf("%d %d %d\n", i, val.DT, val.HD);
// printf("%d %d\n", i, pqC[i].top());
}
}
int main() {
int k = K;
int blocks = (k / BLOCK_SIZE) + 1;
printf("%d %d\n", blocks, BLOCK_SIZE);
PriorityQueue pq[k];
printf("%lu\n", sizeof(PriorityQueue));
// printf("sdfsddsfsd");
for(int i=0; i<k; i++) {
pq[i] = PriorityQueue();
for(int j=0; j<10; j++) {
Node val;
val.DT = i*k + j;
val.HD = i*k + j;
pq[i].insert(val);
}
}
// for(int i=0; i<k; i++) {
// pq[i].printHeap();
// // printf("%d %d\n", i, pq[i].top());
// }
cudaEvent_t start_kernel, stop_kernel;
cudaEventCreate(&start_kernel);
cudaEventCreate(&stop_kernel);
cudaEventRecord(start_kernel);
// int arr[5] = {1,2,3,4,5};
PriorityQueue *pqC;
cudaMalloc((void**)&pqC, sizeof(PriorityQueue)*k);
cudaMemcpy(pqC, pq, sizeof(PriorityQueue)*k, cudaMemcpyHostToDevice);
parAStar<<<1, 32>>> (pqC);
cudaMemcpy(pq, pqC, sizeof(PriorityQueue)*k, cudaMemcpyDeviceToHost);
cudaEventRecord(stop_kernel);
cudaEventSynchronize(stop_kernel);
// cudaEventElapsedTime(&totalTime, start_kernel, stop_kernel);
return 0;
}