• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2023 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "nnacl/kernel/init_exec_env.h"
18 
19 #define NNACLMaxAllocSize (2000 * 1024 * 1024)
20 ExecEnv nnacl_default_env;
21 
NNACLDefaultAlloc(void * allocator,size_t sz)22 void *NNACLDefaultAlloc(void *allocator, size_t sz) {
23   if (sz == 0 || sz > NNACLMaxAllocSize) {
24     return NULL;
25   }
26   return malloc(sz);
27 }
28 
NNACLDefaultFree(void * allocator,void * ptr)29 void NNACLDefaultFree(void *allocator, void *ptr) { return free(ptr); }
30 
NNACLDefaultParallelLunch(void * threadPool,void * task,void * param,int taskNr)31 int NNACLDefaultParallelLunch(void *threadPool, void *task, void *param, int taskNr) {
32   int (*function)(void *cdata, int task_id, float l, float r) = task;
33   int ret = 0;
34   for (int i = 0; i < taskNr; i++) {
35     ret += function(param, i, 0, 1);
36   }
37   return ret == NNACL_OK ? NNACL_OK : NNACL_ERR;
38 }
39 
InitDefaultExecEnv(void)40 void InitDefaultExecEnv(void) {
41   nnacl_default_env.Free = NNACLDefaultFree;
42   nnacl_default_env.Alloc = NNACLDefaultAlloc;
43   nnacl_default_env.ParallelLaunch = NNACLDefaultParallelLunch;
44 }
45 
CheckExecEnv(KernelBase * base)46 void CheckExecEnv(KernelBase *base) {
47   if (base->env_ == NULL) {
48     base->env_ = &nnacl_default_env;
49   }
50   return;
51 }
52