-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeckoDataTypeGenerator.h
More file actions
189 lines (154 loc) · 4.72 KB
/
geckoDataTypeGenerator.h
File metadata and controls
189 lines (154 loc) · 4.72 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
181
182
183
184
185
186
187
188
189
//
// Created by millad on 7/23/18.
//
#ifndef GECKO_GECKODATATYPEGENERATOR_H
#define GECKO_GECKODATATYPEGENERATOR_H
#include <vector>
#include <stdlib.h>
//#define CUDA_ENABLED
#ifdef CUDA_ENABLED
#include <cuda_runtime.h>
#endif
#include <openacc.h>
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include "geckoUtils.h"
using namespace std;
typedef enum {
GECKO_GENERATOR_HOST = 0,
GECKO_GENERATOR_GPU,
GECKO_GENERATOR_UNIFIED,
GECKO_GENERATOR_UNKNOWN,
GECKO_GENERATOR_LEN
}GeckoGeneratorMemoryType;
class gecko_type_base {
protected:
size_t *total_count;
size_t *count_per_dev;
int *dev_count;
GeckoGeneratorMemoryType mem_type;
int sizes_in_byte[2];
int *dev_list;
void *alloc(GeckoGeneratorMemoryType mem_type, int datasize, int count);
void freeMemBase(void **arr);
public:
void setDevList(vector<int> dl);
virtual void allocateMemOnlyHost(size_t count) = 0;
virtual void allocateMemOnlyGPU(size_t count) = 0;
virtual void allocateMemUnifiedMem(size_t count) = 0;
virtual void allocateMem(size_t count, vector<int> &dl) = 0;
virtual void freeMem() = 0;
};
template<class Type>
class gecko_generator : public gecko_type_base {
public:
Type **arr;
gecko_generator<Type>() : arr(NULL) {
mem_type = GECKO_GENERATOR_UNKNOWN;
sizes_in_byte[0] = sizeof(Type);
sizes_in_byte[1] = sizeof(Type*);
GECKO_CUDA_CHECK(cudaMallocManaged((void**) &total_count, sizeof(size_t)));
cudaMallocManaged((void**) &count_per_dev, sizeof(size_t));
cudaMallocManaged((void**) &dev_count, sizeof(int));
}
~gecko_generator<Type>() {
}
//#pragma acc routine seq
Type &operator[] (size_t index) {
int new_index = index % (*count_per_dev);
int dev_id = index / (*count_per_dev);
if(dev_id>=*dev_count) {
dev_id = *dev_count - 1;
//i new_index += *count_per_dev;
new_index = index - dev_id*(*count_per_dev);
}
Type *d = arr[dev_id];
return d[new_index];
//#endif
}
Type *operator+ (int index) {
return &operator[](index);
}
void allocateMemOnlyHost(size_t count) {
mem_type = GECKO_GENERATOR_HOST;
*count_per_dev = count;
*total_count = count;
*dev_count = 1;
arr = (Type**) malloc(sizeof(Type*) * *dev_count);
arr[0] = (Type*) malloc(sizeof(Type) * *count_per_dev);
}
void allocateMemOnlyGPU(size_t count) {
if(true) {
allocateMemUnifiedMem(count);
return;
}
// mem_type = GECKO_GENERATOR_GPU;
// count_per_dev = count;
// dev_count = 1;
// arr = (Type**) malloc(sizeof(Type*) * dev_count);
// cudaMalloc((void**) &arr[0], sizeof(Type) * count_per_dev);
}
void allocateMemUnifiedMem(size_t count) {
mem_type = GECKO_GENERATOR_UNIFIED;
*count_per_dev = count / *dev_count;
*total_count = count;
int curr_dev = acc_get_device_num(acc_device_nvidia);
acc_set_device_num(0, acc_device_nvidia);
GECKO_CUDA_CHECK(cudaMallocManaged((void***) &arr, sizeof(Type*) * *dev_count));
//for(int i=0;i<*dev_count;i++) {
#pragma omp parallel num_threads(*dev_count)
{
int i = omp_get_thread_num();
int dev_id = dev_list[i];
size_t count_per_dev_refined = *count_per_dev;
if(i == *dev_count-1)
count_per_dev_refined = *total_count - i*(*count_per_dev);
if(dev_id != cudaCpuDeviceId)
acc_set_device_num(dev_id, acc_device_nvidia);
else
acc_set_device_num(0, acc_device_host);
void *a = NULL;
#ifdef CUDA_ENABLED
// printf("==============================FROM HELL\n");
GECKO_CUDA_CHECK(cudaMallocManaged((void**) &a, sizeof(Type) * count_per_dev_refined));
// printf("==============================FROM HELL 2 - array_count: %d - DEV_COUNT: %d\n", count_per_dev_refined, *dev_count);
#endif
#pragma acc wait
arr[i] = (Type*) a;
// printf("==============================FROM HELL 2 - A\n");
#ifdef INFO
fprintf(stderr, "===GECKO: COUNT_PER_DEV - %s: %d\n", (dev_id == -1 ? "CPU" : "GPU"), count_per_dev_refined);
#endif
}
// printf("==============================FROM HELL 3\n");
/*
for(int i=0;i<*dev_count;i++) {
int dev_id = dev_list[i];
GECKO_CUDA_CHECK(cudaMemAdvise(&arr[dev_id], sizeof(Type **) * *dev_count, cudaMemAdviseSetReadMostly, dev_id));
}
*/
// printf("==============================FROM HELL 4\n");
acc_set_device_num(curr_dev, acc_device_nvidia);
// printf("==============================FROM HELL 5\n");
}
void allocateMem(size_t count, vector<int> &dl) {
setDevList(dl);
allocateMemUnifiedMem(count);
}
void freeMem() {
cudaFree(total_count);
cudaFree(count_per_dev);
cudaFree(dev_count);
freeMemBase((void**)arr);
}
};
#define G_GENERATOR(TYPE) typedef gecko_generator<TYPE> gecko_##TYPE
G_GENERATOR(char);
G_GENERATOR(int);
G_GENERATOR(long);
G_GENERATOR(float);
G_GENERATOR(double);
G_GENERATOR(bool);
typedef gecko_generator<unsigned int> gecko_unsigned_int;
#endif //GECKO_GECKODATATYPEGENERATOR_H