forked from alpaka-group/mallocMC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mallocMC_example02.cu
221 lines (169 loc) · 6.82 KB
/
mallocMC_example02.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
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
/*
mallocMC: Memory Allocator for Many Core Architectures.
https://www.hzdr.de/crp
Copyright 2014 Institute of Radiation Physics,
Helmholtz-Zentrum Dresden - Rossendorf
Author(s): Carlchristian Eckert - c.eckert ( at ) hzdr.de
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <iostream>
#include <cassert>
#include <vector>
#include <numeric>
#include <cuda.h>
#include <boost/mpl/int.hpp>
#include <boost/mpl/bool.hpp>
///////////////////////////////////////////////////////////////////////////////
// includes for mallocMC
///////////////////////////////////////////////////////////////////////////////
// basic files for mallocMC
#include "src/include/mallocMC/mallocMC_hostclass.hpp"
// Load all available policies for mallocMC
#include "src/include/mallocMC/CreationPolicies.hpp"
#include "src/include/mallocMC/DistributionPolicies.hpp"
#include "src/include/mallocMC/OOMPolicies.hpp"
#include "src/include/mallocMC/ReservePoolPolicies.hpp"
#include "src/include/mallocMC/AlignmentPolicies.hpp"
///////////////////////////////////////////////////////////////////////////////
// Configuration for mallocMC
///////////////////////////////////////////////////////////////////////////////
// configurate the CreationPolicy "Scatter"
struct ScatterConfig{
typedef boost::mpl::int_<4096> pagesize;
typedef boost::mpl::int_<8> accessblocks;
typedef boost::mpl::int_<16> regionsize;
typedef boost::mpl::int_<2> wastefactor;
typedef boost::mpl::bool_<false> resetfreedpages;
};
struct ScatterHashParams{
typedef boost::mpl::int_<38183> hashingK;
typedef boost::mpl::int_<17497> hashingDistMP;
typedef boost::mpl::int_<1> hashingDistWP;
typedef boost::mpl::int_<1> hashingDistWPRel;
};
// configure the DistributionPolicy "XMallocSIMD"
struct DistributionConfig{
typedef ScatterConfig::pagesize pagesize;
};
// configure the AlignmentPolicy "Shrink"
struct AlignmentConfig{
typedef boost::mpl::int_<16> dataAlignment;
};
// Define a new mMCator and call it ScatterAllocator
// which resembles the behaviour of ScatterAlloc
typedef mallocMC::Allocator<
mallocMC::CreationPolicies::Scatter<ScatterConfig,ScatterHashParams>,
mallocMC::DistributionPolicies::XMallocSIMD<DistributionConfig>,
mallocMC::OOMPolicies::ReturnNull,
mallocMC::ReservePoolPolicies::SimpleCudaMalloc,
mallocMC::AlignmentPolicies::Shrink<AlignmentConfig>
> ScatterAllocator;
///////////////////////////////////////////////////////////////////////////////
// End of mallocMC configuration
///////////////////////////////////////////////////////////////////////////////
void run();
int main()
{
cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&deviceProp, 0);
if( deviceProp.major < int(2) ) {
std::cerr << "Error: Compute Capability >= 2.0 required. (is ";
std::cerr << deviceProp.major << "."<< deviceProp.minor << ")" << std::endl;
return 1;
}
cudaSetDevice(0);
run();
cudaDeviceReset();
return 0;
}
__device__ int** arA;
__device__ int** arB;
__device__ int** arC;
__global__ void createArrayPointers(int x, int y, ScatterAllocator::AllocatorHandle mMC){
arA = (int**) mMC.malloc(sizeof(int*) * x*y);
arB = (int**) mMC.malloc(sizeof(int*) * x*y);
arC = (int**) mMC.malloc(sizeof(int*) * x*y);
}
__global__ void fillArrays(int length, int* d, ScatterAllocator::AllocatorHandle mMC){
int id = threadIdx.x + blockIdx.x*blockDim.x;
arA[id] = (int*) mMC.malloc(sizeof(int)*length);
arB[id] = (int*) mMC.malloc(sizeof(int)*length);
arC[id] = (int*) mMC.malloc(sizeof(int)*length);
for(int i=0 ; i<length; ++i){
arA[id][i] = id*length+i;
arB[id][i] = id*length+i;
}
}
__global__ void addArrays(int length, int* d){
int id = threadIdx.x + blockIdx.x*blockDim.x;
d[id] = 0;
for(int i=0 ; i<length; ++i){
arC[id][i] = arA[id][i] + arB[id][i];
d[id] += arC[id][i];
}
}
__global__ void freeArrays(ScatterAllocator::AllocatorHandle mMC){
int id = threadIdx.x + blockIdx.x*blockDim.x;
mMC.free(arA[id]);
mMC.free(arB[id]);
mMC.free(arC[id]);
}
__global__ void freeArrayPointers(ScatterAllocator::AllocatorHandle mMC){
mMC.free(arA);
mMC.free(arB);
mMC.free(arC);
}
void run()
{
size_t block = 32;
size_t grid = 32;
int length = 100;
assert((unsigned)length <= block*grid); //necessary for used algorithm
//init the heap
std::cerr << "initHeap...";
ScatterAllocator mMC(1U*1024U*1024U*1024U); //1GB for device-side malloc
std::cerr << "done" << std::endl;
// device-side pointers
int* d;
cudaMalloc((void**) &d, sizeof(int)*block*grid);
// host-side pointers
std::vector<int> array_sums(block*grid,0);
// create arrays of arrays on the device
createArrayPointers<<<1,1>>>(grid, block, mMC );
// fill 2 of them all with ascending values
fillArrays<<<grid,block>>>(length, d, mMC );
// add the 2 arrays (vector addition within each thread)
// and do a thread-wise reduce to d
addArrays<<<grid,block>>>(length, d);
cudaMemcpy(&array_sums[0], d, sizeof(int)*block*grid, cudaMemcpyDeviceToHost);
int sum = std::accumulate(array_sums.begin(), array_sums.end(), 0);
std::cout << "The sum of the arrays on GPU is " << sum << std::endl;
int n = block*grid*length;
int gaussian = n*(n-1);
std::cout << "The gaussian sum as comparison: " << gaussian << std::endl;
// checking the free memory of the allocator
if(mallocMC::Traits<ScatterAllocator>::providesAvailableSlots){
std::cout << "there are ";
std::cout << mMC.getAvailableSlots(1024U*1024U);
std::cout << " Slots of size 1MB available" << std::endl;
}
freeArrays<<<grid, block>>>( mMC );
freeArrayPointers<<<1, 1>>>( mMC );
cudaFree(d);
//finalize the heap again
mMC.finalizeHeap();
}