forked from OAID/Tengine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.c
375 lines (315 loc) · 8.73 KB
/
cpu.c
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* License); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Parts of the following code in this file refs to
* https://github.com/Tencent/ncnn/blob/master/src/cpu.cpp
* Tencent is pleased to support the open source community by making ncnn
* available.
*
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the
* License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
/*
* Copyright (c) 2020, OPEN AI LAB
* Author: [email protected]
*/
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include "tengine_c_api.h"
//#ifndef __ANDROID__
#include <sys/syscall.h>
#include <sched.h>
#include <unistd.h>
#include <stdint.h>
//#endif
#if __APPLE__
#include "TargetConditionals.h"
#if TARGET_OS_IPHONE
#include <sys/types.h>
#include <sys/sysctl.h>
#include <mach/machine.h>
#define __APPLE_IOS__ 1
#endif
#endif
#ifdef _OPENMP
#include <omp.h>
#endif
static size_t core_count = 0;
static size_t affinity_mask_all_cluster = 0;
static size_t affinity_mask_big_cluster = 0;
static size_t affinity_mask_medium_cluster = 0;
static size_t affinity_mask_little_cluster = 0;
int init_cpu_count()
{
if (0 < core_count)
return core_count;
#ifdef __ANDROID__
{
FILE* cpu_info = fopen("/proc/cpuinfo", "rb");
if (!cpu_info)
return -1;
char buffer[1024];
while (!feof(cpu_info))
{
char* s = fgets(buffer, 1024, cpu_info);
if (!s)
break;
if (memcmp(buffer, "processor", 9) == 0)
core_count++;
}
fclose(cpu_info);
};
#elif __APPLE_IOS__
{
size_t len = sizeof(core_count);
sysctlbyname("hw.ncpu", &core_count, &len, NULL, 0);
};
#else
{
#ifdef _OPENMP
core_count = omp_get_max_threads();
#else
core_count = 1;
#endif
}
#endif
// check count range
if (core_count < 1)
core_count = 1;
// TODO: deal with this conditions
if (core_count > sizeof(size_t) * 8)
core_count = sizeof(size_t) * 8;
return core_count;
}
static int get_max_freq_khz(int cpuid)
{
// first try, for all possible cpu
char path[256];
sprintf(path, "/sys/devices/system/cpu/cpufreq/stats/cpu%d/time_in_state", cpuid);
FILE* fp = fopen(path, "rb");
if (!fp)
{
// second try, for online cpu
sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", cpuid);
fp = fopen(path, "rb");
if (fp)
{
int max_freq_khz = 0;
while (!feof(fp))
{
int freq_khz = 0;
int nscan = fscanf(fp, "%d %*d", &freq_khz);
if (nscan != 1)
break;
if (freq_khz > max_freq_khz)
max_freq_khz = freq_khz;
}
fclose(fp);
if (max_freq_khz != 0)
return max_freq_khz;
fp = NULL;
}
if (!fp)
{
// third try, for online cpu
sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpuid);
fp = fopen(path, "rb");
if (!fp)
return -1;
int max_freq_khz = -1;
int ret = fscanf(fp, "%d", &max_freq_khz);
fclose(fp);
if (max_freq_khz <=0 && EOF == ret)
return -1;
else
return max_freq_khz;
}
}
int max_freq_khz = 0;
while (!feof(fp))
{
int freq_khz = 0;
int nscan = fscanf(fp, "%d %*d", &freq_khz);
if (nscan != 1)
break;
if (freq_khz > max_freq_khz)
max_freq_khz = freq_khz;
}
fclose(fp);
return max_freq_khz;
}
static int set_sched_affinity(size_t thread_affinity_mask)
{
// cpu_set_t definition
// ref http://stackoverflow.com/questions/16319725/android-set-thread-affinity
#ifndef CPU_SETSIZE
#define CPU_SETSIZE 1024
#endif
#ifndef __NCPUBITS
#define __NCPUBITS (8 * sizeof (unsigned long))
#endif
typedef struct
{
unsigned long __bits[CPU_SETSIZE / __NCPUBITS];
} cpu_set_t;
#define CPU_SET(cpu, cpusetp) ((cpusetp)->__bits[(cpu) / __NCPUBITS] |= (1UL << ((cpu) % __NCPUBITS)))
#define CPU_ZERO(cpusetp) memset((cpusetp), 0, sizeof(cpu_set_t))
// set affinity for thread
#ifdef __GLIBC__
pid_t pid = syscall(SYS_gettid);
#else
#ifdef PI3
pid_t pid = getpid();
#else
pid_t pid = gettid();
#endif
#endif
cpu_set_t mask;
CPU_ZERO(&mask);
for (int i = 0; i < ( int )sizeof(size_t) * 8; i++)
{
if (thread_affinity_mask & (1 << i))
CPU_SET(i, &mask);
}
int syscallret = syscall(__NR_sched_setaffinity, pid, sizeof(mask), &mask);
if (syscallret)
{
fprintf(stderr, "syscall error %d\n", syscallret);
return -1;
}
return 0;
}
int init_cluster_mask()
{
if (0 != affinity_mask_all_cluster)
return 0;
affinity_mask_all_cluster = ((size_t)(1) << core_count) - 1;
//#ifdef __ANDROID__
int max_freq_min_val = INT_MAX;
int max_freq_max_val = 0;
// TODO: deal with very large count of cores
int max_freq_array[sizeof(size_t) * 8];
for (int i = 0; i < core_count; i++)
{
int max_freq_khz = get_max_freq_khz(i);
// fprintf(stderr, "cpu %d, max_freq_khz %d\n", i, max_freq_khz);
max_freq_array[i] = max_freq_khz;
if (max_freq_khz > max_freq_max_val)
max_freq_max_val = max_freq_khz;
if (max_freq_khz < max_freq_min_val)
max_freq_min_val = max_freq_khz;
}
if (max_freq_max_val == max_freq_min_val)
{
affinity_mask_big_cluster = affinity_mask_all_cluster;
affinity_mask_medium_cluster = 0;
affinity_mask_little_cluster = 0;
}
else
{
for (int i = 0; i < core_count; i++)
{
if (max_freq_array[i] == max_freq_max_val)
affinity_mask_big_cluster |= (1 << i);
else if (max_freq_array[i] == max_freq_min_val)
affinity_mask_little_cluster |= (1 << i);
else
affinity_mask_medium_cluster |= (1 << i);
}
}
//#else
// // TODO implement me for other platforms
// affinity_mask_big_cluster = affinity_mask_all_cluster;
//#endif
return 0;
}
int check_cpu()
{
init_cpu_count();
init_cluster_mask();
return 0;
}
int get_mask_count(size_t mask)
{
int count = 0;
for (int i = 0; i < sizeof(size_t) * 8; i++)
if (mask & (1 << i))
count++;
return count;
}
int set_cpu_affine(size_t mask)
{
#ifdef __ANDROID__
int count = get_mask_count(mask);
#ifdef _OPENMP
// set affinity for each thread
omp_set_num_threads(count);
int status[sizeof(size_t) * 8] = {0};
for (int i = 0; i < count; i++)
{
status[i] = set_sched_affinity(mask);
}
for (int i = 0; i < count; i++)
{
if (status[i] != 0)
return -1;
}
#else
int status = set_sched_affinity(mask);
if (0 != status)
return -1;
#endif
return 0;
#elif __APPLE_IOS__
// thread affinity not supported on ios
( void )mask;
return -1;
#else
int status = set_sched_affinity(mask);
if (0 != status)
return -1;
#endif
}
size_t get_cluster_mask(int cluster)
{
switch (cluster)
{
case TENGINE_CLUSTER_BIG:
if (0 != affinity_mask_big_cluster)
return affinity_mask_big_cluster;
break;
case TENGINE_CLUSTER_MEDIUM:
if (0 != affinity_mask_medium_cluster)
return affinity_mask_medium_cluster;
break;
case TENGINE_CLUSTER_LITTLE:
if (0 != affinity_mask_little_cluster)
return affinity_mask_little_cluster;
break;
default:
break;
}
return affinity_mask_all_cluster;
}