forked from alpaka-group/mallocMC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mallocMC_example01.cu
158 lines (116 loc) · 4.35 KB
/
mallocMC_example01.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
/*
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 "mallocMC_example01_config.cu"
void run();
int main()
{
cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&deviceProp, 0);
if( deviceProp.major < 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(length*sizeof(int));
arB[id] = (int*) mMC.malloc(length*sizeof(int));
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;
std::cout << ScatterAllocator::info("\n") << 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);
mMC.getAvailableSlots(1024U*1024U); //get available megabyte-sized slots
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;
freeArrays<<<grid,block>>>( mMC );
freeArrayPointers<<<1,1>>>( mMC );
cudaFree(d);
//finalize the heap again
mMC.finalizeHeap();
}