-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcuda.h
More file actions
180 lines (119 loc) · 4.53 KB
/
cuda.h
File metadata and controls
180 lines (119 loc) · 4.53 KB
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
#ifndef CUDA_H
#define CUDA_H
//#define PRINT_INPUT 1 //If true prints the input used on the screen
//#define IS_LOGGING 1 //If true does logging for detailed statements
#define PRINT_RESULT 1 //If true will print final results
//#define DO_CPU_COMPUTATION //If true will calculate the histogram on the CPU
#include <thrust/device_vector.h>
#include <vector>
#include <iostream>
#include "cudaTimer.h"
#include "windowsCpuTimer.h"
using namespace std;
typedef thrust::device_vector<int>::iterator DVI;
typedef thrust::device_vector<long long>::iterator DVL;
void printData(int rows, int printWidth, thrust::host_vector<int> & data);
void printDataNoZeroes(int rows, int printWidth, thrust::host_vector<int> & data);
void printData(int rows, int cols, int printWidth, thrust::device_vector<int> & data);
void printData(int rows, int cols, int printWidth, thrust::host_vector<int> & data);
void printData(int rows, int cols, int printWidth, thrust::host_vector<float> & data);
bool generateRandomData(int rows, int cols, int max, thrust::host_vector<int> & data);
bool loadTextFile(FILE *infile, int xSize, int ySize, int zSize, int numvars, int maxVars, thrust::host_vector<float> & h_data, int bufferSize, int & xPos, int & yPos, int & zPos);
void doHistogramGPU(int xSize, int ySize, int zSize, int numvars, thrust::host_vector<float> & h_buffer, thrust::host_vector<long long> & h_data, thrust::host_vector<long long> & h_data2, int numBins, CudaTimer & cudaTimer, WindowsCpuTimer & cpuTimer);
void histogramMapReduceGPU(thrust::host_vector<long long> & h_data, thrust::host_vector<long long> & h_data2, thrust::pair<DVL, DVL> & endPosition, int numVars, int numBins, CudaTimer & cudaTimer, WindowsCpuTimer & cpuTimer);
void doHistogramCPU(int xSize, int ySize, int zSize, int numVars, int numBins, thrust::host_vector<float> & h_data);
void printHistoData(int rows, int cols, int printWidth, thrust::host_vector<long long> & multiDimKeys, thrust::host_vector<long long> & counts);
int printMinMaxes(string & fileName, int numRecords, int numvars);
typedef thrust::tuple<int, int, int> Int3;
typedef thrust::tuple<int, int> Int2;
typedef thrust::tuple<int> Int;
typedef thrust::tuple<float, float, float> Float3;
typedef thrust::tuple<float, float> Float2;
typedef thrust::tuple<float> Float;
struct BinFinder
{
float * rawMinVector;
float * rawMaxVector;
int numVars;
int numBins;
BinFinder(float * rawMinVector, float * rawMaxVector, int numVars, int numBins)
{
this -> rawMinVector = rawMinVector;
this -> rawMaxVector = rawMaxVector;
this -> numVars = numVars;
this -> numBins = numBins;
}
//This kernel assigns each element to a bin group
__host__ __device__ Int operator()(const Float & param1, const int & param2) const
{
float value = thrust::get<0>(param1);
int id = param2;
float min = rawMinVector[id % numVars];
float max = rawMaxVector[id % numVars];
float percentage = (value - min) / float(max - min);
int bin = percentage * numBins;
if (bin == numBins)
{
bin--;
}
return thrust::make_tuple(bin);
}
};
struct MultiToSingleDim
{
int * rawVector;
int numBins;
MultiToSingleDim(int * rawVector, int numBins)
{
this -> rawVector = rawVector;
this -> numBins = numBins;
}
//This kernel converts the multidimensional bin representation to a single dimensional representation
template <typename Tuple>
__device__ void operator()( Tuple param)
{
int singleDimIndex = thrust::get<0>(param);
int cols = thrust::get<1>(param);
long long newValue = 0;
long long factor = 1;
for (int j = cols - 1; j >= 0; j--)
{
newValue += (rawVector[singleDimIndex * cols + j]) * factor;
factor *= numBins;
}
thrust::get<2>(param) = newValue;
}
};
struct SingleToMultiDim
{
long long * rawVector;
int numBins;
SingleToMultiDim(long long * rawVector, int numBins)
{
this -> rawVector = rawVector;
this -> numBins = numBins;
}
//This kernel converts a single dimensional bin representation back to a multidimensional representation
template <typename Tuple>
__device__ void operator()( Tuple param)
{
int singleDimIndex = thrust::get<0>(param);
int cols = thrust::get<1>(param);
long long dataValue = thrust::get<2>(param);
for (int j = cols - 1; j >= 0; j--)
{
int moddedValue = dataValue % numBins;
rawVector[singleDimIndex * cols + j] = moddedValue;
dataValue /= numBins;
}
}
};
struct ZipComparator
{
__host__ __device__
inline bool operator() (const Int & a, const Int & b)
{
return thrust::get<0>(a) < thrust::get<0>(b);
}
};
#endif