Skip to content

Commit 51f3eda

Browse files
committed
axcl 兼容link模式
1 parent 893cffe commit 51f3eda

9 files changed

Lines changed: 181 additions & 61 deletions

File tree

CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,28 @@ set(SIMPLECV_BUILD_TESTS OFF)
2121
add_subdirectory(third-party/SimpleCV)
2222
include_directories(third-party/SimpleCV/include)
2323

24+
option(AXCL_USE_STATIC_LINK "Use static link for axcl" OFF)
25+
26+
# 如果用户没有显式指定,就在 Windows 上默认开启
27+
if(WIN32 AND NOT DEFINED AXCL_USE_STATIC_LINK)
28+
set(AXCL_USE_STATIC_LINK ON)
29+
endif()
30+
31+
message(STATUS "AXCL_USE_STATIC_LINK: ${AXCL_USE_STATIC_LINK}")
32+
33+
if(AXCL_USE_STATIC_LINK)
34+
if (AXCL_DIR)
35+
message(STATUS "AXCL_DIR Path: ${AXCL_DIR}")
36+
include_directories(${AXCL_DIR}/include)
37+
link_directories(${AXCL_DIR}/lib)
38+
include_directories(${AXCL_DIR}/include/axcl)
39+
link_directories(${AXCL_DIR}/lib/axcl)
40+
else()
41+
include_directories(/usr/include/axcl/)
42+
link_directories(/usr/lib/axcl/)
43+
endif()
44+
endif()
45+
2446
include_directories(include)
2547
include_directories(src)
2648
include_directories(src/utils)
@@ -35,6 +57,12 @@ add_library(clip SHARED
3557
src/ax_devices.cpp
3658
src/tokenizer/tokenizer.cpp
3759
)
60+
61+
if(AXCL_USE_STATIC_LINK)
62+
target_compile_definitions(clip PRIVATE AXCL_USE_STATIC_LINK)
63+
target_link_libraries(clip axcl_rt)
64+
endif()
65+
3866
if(WIN32)
3967
target_link_libraries(clip SimpleCV::simplecv leveldb)
4068
else()

src/ax_devices.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct gInit
2424
std::vector<std::string> supported_backends;
2525
if (getLoader().is_init())
2626
{
27-
auto ret = axclInit();
27+
auto ret = axcl_Init();
2828
if (ret != 0)
2929
{
3030
printf("axclInit failed\n");
@@ -52,10 +52,10 @@ struct gInit
5252
{
5353
if (getLoader().is_init())
5454
{
55-
auto ret = axclFinalize();
55+
auto ret = axcl_Finalize();
5656
if (ret != 0)
5757
{
58-
printf("axclFinalize failed\n");
58+
printf("axcl_Finalize failed\n");
5959
}
6060
}
6161
}

src/runner/ax650/ax_api_loader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class AxEngineApiLoader
175175
private:
176176
void *handle_ = nullptr;
177177

178-
bool open(const std::string &lib_path = "/usr/lib/axcl/libaxcl_rt.so")
178+
bool open(const std::string &lib_path)
179179
{
180180
#if _WIN32
181181
return false;

src/runner/ax650/ax_model_runner_ax650.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@
77

88
#include "ax_api_loader.h"
99

10-
static AxSysApiLoader ax_sys_loader;
11-
static AxEngineApiLoader ax_engine_loader;
12-
1310
AxSysApiLoader &get_ax_sys_loader()
1411
{
12+
static AxSysApiLoader ax_sys_loader;
1513
return ax_sys_loader;
1614
}
1715

1816
AxEngineApiLoader &get_ax_engine_loader()
1917
{
18+
static AxEngineApiLoader ax_engine_loader;
2019
return ax_engine_loader;
2120
}
2221

@@ -81,7 +80,7 @@ void free_io_index(AX_ENGINE_IO_BUFFER_T *io_buf, int index)
8180
for (int i = 0; i < index; ++i)
8281
{
8382
AX_ENGINE_IO_BUFFER_T *pBuf = io_buf + i;
84-
ax_sys_loader.AX_SYS_MemFree(pBuf->phyAddr, pBuf->pVirAddr);
83+
get_ax_sys_loader().AX_SYS_MemFree(pBuf->phyAddr, pBuf->pVirAddr);
8584
}
8685
}
8786

@@ -90,12 +89,12 @@ void free_io(AX_ENGINE_IO_T *io)
9089
for (size_t j = 0; j < io->nInputSize; ++j)
9190
{
9291
AX_ENGINE_IO_BUFFER_T *pBuf = io->pInputs + j;
93-
ax_sys_loader.AX_SYS_MemFree(pBuf->phyAddr, pBuf->pVirAddr);
92+
get_ax_sys_loader().AX_SYS_MemFree(pBuf->phyAddr, pBuf->pVirAddr);
9493
}
9594
for (size_t j = 0; j < io->nOutputSize; ++j)
9695
{
9796
AX_ENGINE_IO_BUFFER_T *pBuf = io->pOutputs + j;
98-
ax_sys_loader.AX_SYS_MemFree(pBuf->phyAddr, pBuf->pVirAddr);
97+
get_ax_sys_loader().AX_SYS_MemFree(pBuf->phyAddr, pBuf->pVirAddr);
9998
}
10099
delete[] io->pInputs;
101100
delete[] io->pOutputs;
@@ -176,11 +175,11 @@ static inline int prepare_io(AX_ENGINE_IO_INFO_T *info, AX_ENGINE_IO_T *io_data,
176175
auto buffer = &io_data->pInputs[i];
177176
if (strategy.first == AX_ENGINE_ABST_CACHED)
178177
{
179-
ret = ax_sys_loader.AX_SYS_MemAllocCached((AX_U64 *)(&buffer->phyAddr), &buffer->pVirAddr, meta.nSize, AX_CMM_ALIGN_SIZE, (const AX_S8 *)(AX_CMM_SESSION_NAME));
178+
ret = get_ax_sys_loader().AX_SYS_MemAllocCached((AX_U64 *)(&buffer->phyAddr), &buffer->pVirAddr, meta.nSize, AX_CMM_ALIGN_SIZE, (const AX_S8 *)(AX_CMM_SESSION_NAME));
180179
}
181180
else
182181
{
183-
ret = ax_sys_loader.AX_SYS_MemAlloc((AX_U64 *)(&buffer->phyAddr), &buffer->pVirAddr, meta.nSize, AX_CMM_ALIGN_SIZE, (const AX_S8 *)(AX_CMM_SESSION_NAME));
182+
ret = get_ax_sys_loader().AX_SYS_MemAlloc((AX_U64 *)(&buffer->phyAddr), &buffer->pVirAddr, meta.nSize, AX_CMM_ALIGN_SIZE, (const AX_S8 *)(AX_CMM_SESSION_NAME));
184183
}
185184

186185
if (ret != 0)
@@ -202,11 +201,11 @@ static inline int prepare_io(AX_ENGINE_IO_INFO_T *info, AX_ENGINE_IO_T *io_data,
202201
buffer->nSize = meta.nSize;
203202
if (strategy.second == AX_ENGINE_ABST_CACHED)
204203
{
205-
ret = ax_sys_loader.AX_SYS_MemAllocCached((AX_U64 *)(&buffer->phyAddr), &buffer->pVirAddr, meta.nSize, AX_CMM_ALIGN_SIZE, (const AX_S8 *)(AX_CMM_SESSION_NAME));
204+
ret = get_ax_sys_loader().AX_SYS_MemAllocCached((AX_U64 *)(&buffer->phyAddr), &buffer->pVirAddr, meta.nSize, AX_CMM_ALIGN_SIZE, (const AX_S8 *)(AX_CMM_SESSION_NAME));
206205
}
207206
else
208207
{
209-
ret = ax_sys_loader.AX_SYS_MemAlloc((AX_U64 *)(&buffer->phyAddr), &buffer->pVirAddr, meta.nSize, AX_CMM_ALIGN_SIZE, (const AX_S8 *)(AX_CMM_SESSION_NAME));
208+
ret = get_ax_sys_loader().AX_SYS_MemAlloc((AX_U64 *)(&buffer->phyAddr), &buffer->pVirAddr, meta.nSize, AX_CMM_ALIGN_SIZE, (const AX_S8 *)(AX_CMM_SESSION_NAME));
210209
}
211210
if (ret != 0)
212211
{
@@ -245,7 +244,7 @@ int ax_runner_ax650::init(const void *model_data, unsigned int model_size, int d
245244

246245
// 3. create handle
247246

248-
ret = ax_engine_loader.AX_ENGINE_CreateHandle(&m_handle->handle, model_data, model_size);
247+
ret = get_ax_engine_loader().AX_ENGINE_CreateHandle(&m_handle->handle, model_data, model_size);
249248
if (0 != ret)
250249
{
251250
ALOGE("AX_ENGINE_CreateHandle");
@@ -254,13 +253,13 @@ int ax_runner_ax650::init(const void *model_data, unsigned int model_size, int d
254253
// fprintf(stdout, "Engine creating handle is done.\n");
255254

256255
// 4. create context
257-
ret = ax_engine_loader.AX_ENGINE_CreateContext(m_handle->handle);
256+
ret = get_ax_engine_loader().AX_ENGINE_CreateContext(m_handle->handle);
258257
if (0 != ret)
259258
{
260259
ALOGE("AX_ENGINE_CreateContext");
261260
return ret;
262261
}
263-
ret = ax_engine_loader.AX_ENGINE_CreateContextV2(m_handle->handle, &m_handle->context);
262+
ret = get_ax_engine_loader().AX_ENGINE_CreateContextV2(m_handle->handle, &m_handle->context);
264263
if (0 != ret)
265264
{
266265
ALOGE("AX_ENGINE_CreateContextV2");
@@ -270,7 +269,7 @@ int ax_runner_ax650::init(const void *model_data, unsigned int model_size, int d
270269

271270
// 5. set io
272271
AX_U32 io_count = 0;
273-
ret = ax_engine_loader.AX_ENGINE_GetGroupIOInfoCount(m_handle->handle, &io_count);
272+
ret = get_ax_engine_loader().AX_ENGINE_GetGroupIOInfoCount(m_handle->handle, &io_count);
274273
if (0 != ret)
275274
{
276275
ALOGE("AX_ENGINE_GetGroupIOInfoCount");
@@ -286,7 +285,7 @@ int ax_runner_ax650::init(const void *model_data, unsigned int model_size, int d
286285
for (int grpid = 0; grpid < io_count; grpid++)
287286
{
288287
AX_ENGINE_IO_INFO_T *io_info = nullptr;
289-
ret = ax_engine_loader.AX_ENGINE_GetGroupIOInfo(m_handle->handle, grpid, &io_info);
288+
ret = get_ax_engine_loader().AX_ENGINE_GetGroupIOInfo(m_handle->handle, grpid, &io_info);
290289
if (0 != ret)
291290
{
292291
ALOGE("AX_ENGINE_GetIOInfo");
@@ -375,16 +374,16 @@ void ax_runner_ax650::deinit()
375374
{
376375
free_io(&m_handle->io_data[i]);
377376
}
378-
ax_engine_loader.AX_ENGINE_DestroyHandle(m_handle->handle);
377+
get_ax_engine_loader().AX_ENGINE_DestroyHandle(m_handle->handle);
379378
}
380379
delete m_handle;
381380
m_handle = nullptr;
382-
ax_engine_loader.AX_ENGINE_Deinit();
381+
get_ax_engine_loader().AX_ENGINE_Deinit();
383382
}
384383

385384
int ax_runner_ax650::set_affinity(int id)
386385
{
387-
return ax_engine_loader.AX_ENGINE_SetAffinity(m_handle->handle, id);
386+
return get_ax_engine_loader().AX_ENGINE_SetAffinity(m_handle->handle, id);
388387
}
389388

390389
// int ax_runner_ax650::mem_sync_input(int idx)
@@ -423,22 +422,22 @@ int ax_runner_ax650::set_affinity(int id)
423422

424423
int ax_runner_ax650::inference()
425424
{
426-
int ret = ax_engine_loader.AX_ENGINE_RunSync(m_handle->handle, &m_handle->io_data[0]);
425+
int ret = get_ax_engine_loader().AX_ENGINE_RunSync(m_handle->handle, &m_handle->io_data[0]);
427426
for (size_t i = 0; i < get_num_outputs(); i++)
428427
{
429428
auto &tensor = get_output(i);
430-
ax_sys_loader.AX_SYS_MinvalidateCache(tensor.phyAddr, tensor.pVirAddr, tensor.nSize);
429+
get_ax_sys_loader().AX_SYS_MinvalidateCache(tensor.phyAddr, tensor.pVirAddr, tensor.nSize);
431430
}
432431
return ret;
433432
}
434433
int ax_runner_ax650::inference(int grpid)
435434
{
436-
int ret = ax_engine_loader.AX_ENGINE_RunGroupIOSync(m_handle->handle, m_handle->context, grpid, &m_handle->io_data[grpid]);
435+
int ret = get_ax_engine_loader().AX_ENGINE_RunGroupIOSync(m_handle->handle, m_handle->context, grpid, &m_handle->io_data[grpid]);
437436

438437
for (size_t i = 0; i < get_num_outputs(); i++)
439438
{
440439
auto &tensor = get_output(grpid, i);
441-
ax_sys_loader.AX_SYS_MinvalidateCache(tensor.phyAddr, tensor.pVirAddr, tensor.nSize);
440+
get_ax_sys_loader().AX_SYS_MinvalidateCache(tensor.phyAddr, tensor.pVirAddr, tensor.nSize);
442441
}
443442
return ret;
444443
}

src/runner/axcl/axcl_manager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ AxclApiLoader &getLoader()
1717
return gloader;
1818
}
1919

20-
axclError axclInit()
20+
axclError axcl_Init()
2121
{
2222
return getLoader().axclInit(0);
2323
}
2424

25-
axclError axclFinalize()
25+
axclError axcl_Finalize()
2626
{
2727
return getLoader().axclFinalize();
2828
}

src/runner/axcl/axcl_manager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
extern "C"
77
{
88
#endif
9-
axclError axclInit();
10-
axclError axclFinalize();
9+
axclError axcl_Init();
10+
axclError axcl_Finalize();
1111

1212
axclError axcl_Dev_Init(int devid);
1313
bool axcl_Dev_IsInit(int devid);

0 commit comments

Comments
 (0)