-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeckoRuntime.cpp
223 lines (157 loc) · 5.01 KB
/
geckoRuntime.cpp
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
222
223
#include "geckoRuntime.h"
#include "geckoStringUtils.h"
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <stdlib.h>
#include <unordered_set>
#include <algorithm>
using namespace std;
#include <openacc.h>
#ifdef CUDA_ENABLED
#include <cuda_runtime.h>
#endif
#include "geckoUtils.h"
#include "geckoDataTypeGenerator.h"
#include "geckoMemory.h"
#define GECKO_ACQUIRE_SLEEP_DURATION_NS 100 // in nanoseconds
/*
* Controlling how AcquireLocations function behaves.
*/
//#define GECKO_WAIT_ON_ALL_DEV_TO_BE_FREE
//class GeckoAddressInfo {
//public:
// void *ptr;
// size_t total_count;
// size_t startingIndex;
//
// explicit
// GeckoAddressInfo(void *p=NULL, size_t total_count=0, size_t startingIndex=0) :
// ptr(p), total_count(total_count), startingIndex(startingIndex)
// {}
//};
//
//static unordered_map<void*, GeckoAddressInfo> geckoAddressTable;
GeckoCUDAProp geckoCUDA;
unordered_map<string, GeckoLocationType> listOfAvailLocationTypes;
extern unordered_map<void*, GeckoMemory> geckoMemoryTable;
unordered_set<GeckoLocation*> freeResources;
omp_lock_t lock_freeResources;
GeckoLocation *geckoTreeHead = NULL;
int geckoStarted = 0;
int geckoCleanedup = 1;
bool geckoPolicyRunTimeExists = false;
char *geckoChosenPolicyRunTime = NULL;
extern GeckoError geckoMemoryDistribution(int loc_count, GeckoLocation **loc_list, int var_count, void **var_list,
int *beginIndex, int *endIndex);
void geckoCheckRunTimePolicy() {
char *policy_run_time = getenv("GECKO_POLICY");
if(policy_run_time != NULL) {
geckoPolicyRunTimeExists = true;
geckoChosenPolicyRunTime = strdup(policy_run_time);
#ifdef INFO
fprintf(stderr, "===GECKO: Execution policy will be overridden by the chosen policy at runtime: %s.\n", policy_run_time);
#endif
}
}
GeckoError geckoInit() {
if(geckoStarted)
return GECKO_SUCCESS;
geckoStarted = 1;
// for 'any' execution policy
srand (time(NULL));
// for nested OpenMP regions in case we target Multicore architectures
omp_set_nested(1);
omp_set_num_threads(omp_get_num_procs());
geckoTreeHead = NULL;
#ifdef CUDA_ENABLED
// GECKO_CUDA_CHECK(cudaGetDeviceCount(&geckoCUDA.deviceCountTotal));
geckoCUDA.deviceCountTotal = acc_get_num_devices(acc_device_nvidia);
geckoCUDA.deviceDeclared = 0;
#ifdef INFO
fprintf(stderr, "===GECKO: CUDA Devices available(%d)\n", geckoCUDA.deviceCountTotal);
#endif
#endif
// Defining Abstraction location type
GeckoLocationType d{};
d.type = GECKO_VIRTUAL;
d.numCores = 0;
d.mem_size = const_cast<char *>("");
d.mem_type = const_cast<char *>("");
listOfAvailLocationTypes[string("virtual")] = d;
// Finding chosen policy at run time
geckoCheckRunTimePolicy();
omp_init_lock(&lock_freeResources);
geckoCleanedup = 0;
atexit(geckoCleanup);
#ifdef INFO
fprintf(stderr, "===GECKO: Started...\n");
#endif
return GECKO_SUCCESS;
}
void geckoCleanup() {
if(geckoCleanedup)
return;
// #ifdef DEBUG_SHOW_CLEANUP
// chamListAllLocationtypes();
// chamListAllLocations();
// chamListAllVariables();
// chamListAllMemoryAllocations();
// #endif
omp_destroy_lock(&lock_freeResources);
// Finding chosen policy at run time
if(geckoChosenPolicyRunTime)
free(geckoChosenPolicyRunTime);
geckoStarted = 0;
geckoCleanedup = 1;
#ifdef INFO
fprintf(stderr, "===GECKO: Stopped...\n");
#endif
}
GeckoError geckoMemoryInternalTypeDeclare(gecko_type_base &Q, size_t dataSize, size_t count, char *location,
GeckoDistanceTypeEnum distance) {
GeckoLocationArchTypeEnum type;
GeckoLocation *const pLocation = GeckoLocation::find(location);
if(pLocation == NULL) {
fprintf(stderr, "===GECKO %s (%d): Unable to find the location (%s)\n", __FILE__, __LINE__, location);
exit(1);
}
geckoMemoryAllocationAlgorithm(pLocation, type);
vector<__geckoLocationIterationType> childrenList;
vector<int> childrenListFinal;
#ifdef INFO
fprintf(stderr, "===GECKO: Allocating internal data type: Location(%s) - LocationType(%s) - Count(%d) \n", location, geckoGetLocationTypeName(type), count);
#endif
switch(type) {
case GECKO_X32:
case GECKO_X64:
Q.allocateMemOnlyHost(count);
break;
case GECKO_NVIDIA:
Q.allocateMemOnlyGPU(count);
break;
case GECKO_UNIFIED_MEMORY:
geckoExtractChildrenFromLocation(pLocation, childrenList, 0);
for(int i=0;i<childrenList.size();i++) {
GeckoLocationArchTypeEnum type = childrenList[i].loc->getLocationType().type;
if(type == GECKO_X32 || type == GECKO_X64) {
childrenListFinal.push_back(cudaCpuDeviceId);
} else {
childrenListFinal.push_back(childrenList[i].loc->getLocationIndex());
}
}
#ifdef INFO
fprintf(stderr, "===GECKO: \tlocation list: ");
for(int i=0;i<childrenList.size();i++)
fprintf(stderr, "%s, ", childrenList[i].loc->getLocationName().c_str());
fprintf(stderr, "\n");
#endif
Q.allocateMem(count, childrenListFinal);
break;
default:
fprintf(stderr, "=== GECKO: Unrecognized architecture for memory allocation - Arch: %s\n",
geckoGetLocationTypeName(type));
exit(1);
}
return GECKO_SUCCESS;
}