Skip to content

Commit

Permalink
Add wasi support (#5534)
Browse files Browse the repository at this point in the history
* benchmark: Don't use std::thread when NCNN_THREADS is OFF

* Add wasi support

cmake -B build -DCMAKE_BUILD_TYPE=Release \
	--toolchain ${wasi_sdk}/share/cmake/wasi-sdk.cmake \
	-DNCNN_RUNTIME_CPU=OFF \
	-DNCNN_DISABLE_EXCEPTION=ON \
	-DNCNN_THREADS=OFF

cmake --build build

After build, you can run benchncnn on cmdline with wasmtime:
wasmtime --dir . benchncnn
  • Loading branch information
quink-black authored Jul 2, 2024
1 parent 317635b commit a05c113
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ else()
unset(CMAKE_REQUIRED_FLAGS)
endif()

if(NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten" AND NCNN_COMPILER_SUPPORT_X86_AVX)
if(NOT CMAKE_SYSTEM_NAME MATCHES "Emscripten|WASI" AND NCNN_COMPILER_SUPPORT_X86_AVX)
option(NCNN_AVX "optimize x86 platform with avx extension" ON)
if(NCNN_COMPILER_SUPPORT_X86_FMA)
if(NCNN_AVX)
Expand Down
6 changes: 4 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,10 @@ if(NCNN_TARGET_ARCH STREQUAL "x86")
endif()
target_compile_options(ncnn PRIVATE /D__SSE2__)
else()
target_compile_options(ncnn PRIVATE -msse2 -msse)
if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
if(NOT CMAKE_SYSTEM_NAME MATCHES "WASI")
target_compile_options(ncnn PRIVATE -msse2 -msse)
endif ()
if(CMAKE_SYSTEM_NAME MATCHES "Emscripten|WASI")
target_compile_options(ncnn PRIVATE -msimd128)
endif()
endif()
Expand Down
16 changes: 12 additions & 4 deletions src/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,27 @@
#include "benchmark.h"

#if (__cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)) && !defined(__riscv) && !NCNN_SIMPLESTL
#define USE_CXX11_CLOCK 1
#else
#define USE_CXX11_CLOCK 0
#endif

#if USE_CXX11_CLOCK
#include <chrono>
#if NCNN_THREADS
#include <thread>
#endif
#include <numeric>
#include <algorithm>
#else
#endif // USE_CXX11_CLOCK

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else // _WIN32
#include <sys/time.h> //gettimeofday()
#include <unistd.h> // sleep()
#endif // _WIN32
#endif

#if NCNN_BENCHMARK
#include "layer/convolution.h"
Expand All @@ -46,7 +54,7 @@ namespace ncnn {

double get_current_time()
{
#if (__cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)) && !defined(__riscv) && !NCNN_SIMPLESTL
#if USE_CXX11_CLOCK
auto now = std::chrono::high_resolution_clock::now();
auto usec = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());
return usec.count() / 1000.0;
Expand All @@ -69,7 +77,7 @@ double get_current_time()

void sleep(unsigned long long int milliseconds)
{
#if (__cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)) && !defined(__riscv) && !NCNN_SIMPLESTL
#if USE_CXX11_CLOCK && NCNN_THREADS
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
#else
#ifdef _WIN32
Expand Down
2 changes: 2 additions & 0 deletions src/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
#include "platform.h"

#include <limits.h>
#ifndef __wasi__
#include <setjmp.h>
#include <signal.h>
#endif // __wasi__
#include <stdio.h>
#include <string.h>

Expand Down

0 comments on commit a05c113

Please sign in to comment.