Skip to content

Commit

Permalink
Use pthread key on Mac
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnycase committed Nov 14, 2024
1 parent 9245339 commit 78dffe6
Show file tree
Hide file tree
Showing 20 changed files with 271 additions and 308 deletions.
4 changes: 2 additions & 2 deletions modules/Nncase.Modules.CPU/CodeGen/CPU/CSourceBuiltn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public static string TopoAwareRuntimeDef(CpuTargetOptions options, ulong dataAli

public static string CMakeDef(string name)
{
var cmakePath = CMakePath(Path.Combine(Path.GetDirectoryName(typeof(CSourceBuiltn).Assembly.Location)!, "Runtime", "cmake", "cpu_runtime.cmake"));
var cmakePath = CMakePath(Path.Combine(Path.GetDirectoryName(typeof(CSourceBuiltn).Assembly.Location)!, "Runtime", "cmake", "ntt_module.cmake"));
var content = RazorTemplateEngine.RenderAsync("~/CodeGen/CPU/Templates/CMakeLists.txt.cshtml", new { CMakePath = cmakePath }).Result;
return content;
}

public static string MakeMain(TIR.PrimFunction primFunction, ulong dataAlign, ulong dataUsage, ulong rdataPoolSize, IEnumerable<TIR.Buffer> rdataBuffers, CpuTargetOptions options)
{
var content = RazorTemplateEngine.RenderAsync("~/CodeGen/CPU/Templates/main.cpp.cshtml", new KernelMainModel(primFunction, rdataBuffers.ToArray(), options, dataAlign, dataUsage, rdataPoolSize)).Result;
var content = RazorTemplateEngine.RenderAsync("~/CodeGen/CPU/Templates/thread_main.cpp.cshtml", new KernelMainModel(primFunction, rdataBuffers.ToArray(), options, dataAlign, dataUsage, rdataPoolSize)).Result;
return content;
}

Expand Down
4 changes: 1 addition & 3 deletions modules/Nncase.Modules.CPU/CodeGen/CPU/CSourceCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ private void ArchSpecific()
private string ArgumentsSpecific(string sourcePath, string outPath)
{
var archConfig = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
"-DCMAKE_C_COMPILER=clang-cl -DCMAKE_CXX_COMPILER=clang-cl -DBUILD_SHARED=ON" :
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "-DBUILD_SHARED=ON" :
string.Empty;
"-DCMAKE_C_COMPILER=clang-cl -DCMAKE_CXX_COMPILER=clang-cl" : string.Empty;

#if DEBUG
var config = "Debug";
Expand Down
4 changes: 2 additions & 2 deletions modules/Nncase.Modules.CPU/CodeGen/CPU/LinkableModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public ILinkedModule Link(ILinkContext linkContext)
Directory.CreateDirectory(dumpPath);
}

using (var fs = File.Open(Path.Join(dumpPath, "main.cpp"), FileMode.Create))
using (var fs = File.Open(Path.Join(dumpPath, "thread_main.cpp"), FileMode.Create))
{
using (var writer = new StreamWriter(fs))
{
Expand Down Expand Up @@ -112,7 +112,7 @@ public ILinkedModule Link(ILinkContext linkContext)
private string CompileCSource(string sourcePath)
{
var compiler = new CSourceCompiler();
var binDir = Path.Join(sourcePath, "build", "nncase_cpu_module");
var binDir = Path.Join(sourcePath, "build", "nncase_ntt_module");
return compiler.Compile(sourcePath, binDir);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,4 @@ endif()

include(@Html.Raw(Model.CMakePath))

if(NOT APPLE AND BUILD_SHARED)
add_library(nncase_cpu_module SHARED main.cpp)
set_target_properties(nncase_cpu_module PROPERTIES PREFIX "" SUFFIX "")
set_target_properties(nncase_cpu_runtime PROPERTIES POSITION_INDEPENDENT_CODE ON)
else()
add_executable(nncase_cpu_module main.cpp)
endif()
target_compile_features(nncase_cpu_module PUBLIC cxx_std_20)
target_link_libraries(nncase_cpu_module PRIVATE nncase_cpu_runtime)
target_compile_definitions(nncase_cpu_module PUBLIC -DNNCASE_CPU_MODULE=1)

if (MSVC)
set_target_properties(nncase_cpu_module PROPERTIES LINK_FLAGS /SUBSYSTEM:CONSOLE)
target_link_options(nncase_cpu_module PRIVATE /NODEFAULTLIB)
target_link_libraries(nncase_cpu_module PRIVATE libvcruntime msvcrt ucrt "libcpmt$<$<CONFIG:Debug>:d>")
set_property(TARGET nncase_cpu_module PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
else()
if (APPLE)
if(BUILD_STANDALONE)
target_link_options(nncase_cpu_module PRIVATE -ld_classic -lc)
else()
target_link_options(nncase_cpu_module PRIVATE -static)
target_link_options(nncase_cpu_module PRIVATE -e _module_entry -bundle -ld_classic -lc)
target_compile_options(nncase_cpu_module PRIVATE "$<$<CONFIG:Debug>:-O1>")
endif(BUILD_STANDALONE)
else()
if (BUILD_SHARED)
target_link_options(nncase_cpu_module PRIVATE -e module_entry)
else()
if(NOT BUILD_STANDALONE)
target_link_options(nncase_cpu_module PRIVATE -static)
target_link_options(nncase_cpu_module PRIVATE -e module_entry -nostdlib)
endif(NOT BUILD_STANDALONE)
endif()
target_link_libraries(nncase_cpu_module PRIVATE gcc)
endif()
endif()
target_sources(nncase_ntt_module PRIVATE thread_main.cpp)
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
#include "kernel.h"

alignas(@(Model.Alignment)) static thread_local uint8_t local_data[@(Model.DataSize)];
//alignas(@(Model.Alignment)) static thread_local uint8_t local_data[@(Model.DataSize)];

extern "C" void thread_main(std::byte *const *inouts, const std::byte *rdata) {
/* prepare inputs */
Expand All @@ -41,7 +41,9 @@ extern "C" void thread_main(std::byte *const *inouts, const std::byte *rdata) {
throw new NotSupportedException($"not support multi form topology!");
}

auto local_data = (uint8_t *)nncase::ntt::runtime::thread_alloc(@Model.DataSize, @Model.Alignment);
@(Model.PrimFunction.Name)(@(string.Join(", ", names)), local_data);
nncase::ntt::runtime::thread_free(local_data);
}

#ifdef NNCASE_STANDALONE
Expand Down
12 changes: 0 additions & 12 deletions ntt/cmake/cpu_runtime.cmake

This file was deleted.

33 changes: 33 additions & 0 deletions ntt/cmake/ntt_module.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
cmake_minimum_required(VERSION 3.15)

include(${CMAKE_CURRENT_LIST_DIR}/compile_flags.cmake)

if (BUILD_STANDALONE)
add_executable(nncase_ntt_module ${CMAKE_CURRENT_LIST_DIR}/../src/dummy.cpp)
else()
add_library(nncase_ntt_module SHARED ${CMAKE_CURRENT_LIST_DIR}/../src/dummy.cpp)
endif()

target_compile_features(nncase_ntt_module PUBLIC cxx_std_20)
target_include_directories(nncase_ntt_module PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../include)
set_target_properties(nncase_ntt_module PROPERTIES PREFIX "" SUFFIX "")
set_target_properties(nncase_ntt_module PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_compile_definitions(nncase_ntt_module PUBLIC -DNNCASE_CPU_MODULE=1)

target_sources(nncase_ntt_module PRIVATE ${CMAKE_CURRENT_LIST_DIR}/../src/cpu_runtime.cpp)

if (BUILD_STANDALONE)
target_compile_definitions(nncase_ntt_module PUBLIC -DNNCASE_STANDALONE=1)
endif()

if (MSVC)
set_property(TARGET nncase_ntt_module PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
set_target_properties(nncase_ntt_module PROPERTIES LINK_FLAGS /SUBSYSTEM:CONSOLE)
target_link_options(nncase_ntt_module PRIVATE /NODEFAULTLIB)
target_link_libraries(nncase_ntt_module PRIVATE libvcruntime msvcrt ucrt "libcpmt$<$<CONFIG:Debug>:d>")
elseif(APPLE)
target_link_options(nncase_ntt_module PRIVATE -ld_classic -lc)
else()
target_link_options(nncase_ntt_module PRIVATE -static)
endif()
16 changes: 4 additions & 12 deletions ntt/include/nncase/ntt/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,8 @@
#endif

namespace nncase::ntt::runtime {
enum class module_main_reason {
block_main,
thread_main,
};
}
void *thread_alloc(size_t bytes, size_t alignment);
void thread_free(void *ptr);
} // namespace nncase::ntt::runtime

extern "C" {
extern void thread_main(std::byte *const *inouts, const std::byte *rdata);

extern NTT_RUNTIME_API void
module_entry(nncase::ntt::runtime::module_main_reason reason, void *params);
using module_entry_t = decltype(module_entry) *;
}
extern "C" void thread_main(std::byte *const *inouts, const std::byte *rdata);
73 changes: 28 additions & 45 deletions ntt/include/nncase/ntt/runtime/cpu_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,78 +17,61 @@
#include "../runtime.h"
#include <cstdarg>

extern "C" {
struct nncase_runtime_cpu_mt_t {
float (*acosf)(float v);
float (*acoshf)(float v);
float (*asinf)(float v);
float (*asinhf)(float v);
float (*copysignf)(float mag, float sgn);
float (*cosf)(float v);
float (*coshf)(float v);
float (*erff)(float v);
float (*expf)(float v);
float (*fmodf)(float x, float y);
float (*logf)(float v);
float (*nearbyintf)(float v);
float (*powf)(float x, float y);
float (*roundf)(float v);
float (*sinf)(float v);
float (*sinhf)(float v);
float (*sqrtf)(float v);
float (*tanhf)(float v);

uint8_t *(*sram_address)(int bid, int tid);

void (*failfast)(const char *format, va_list args);

#ifndef WIN32
void *(*memcpy)(void *dst, const void *src, size_t len);
void *(*memmove)(void *dst, const void *src, size_t len);
void *(*memset)(void *b, int c, size_t len);
#ifdef __APPLE__
#include <pthread.h>
#endif
};

struct nncase_runtime_cpu_block_params_t {
const nncase_runtime_cpu_mt_t *cpu_mt;
namespace nncase::ntt::runtime {
struct cpu_block_entry_params_t {
size_t tdim;
size_t bdim;
size_t cdim;
size_t bid;
size_t cid;
size_t cpu_id_offset;
std::byte *const *inouts;
const std::byte *rdata;
#ifdef __APPLE__
pthread_key_t cpu_thread_context_key;
#endif
};

struct nncase_runtime_cpu_thread_params_t {
struct cpu_thread_context_t {
size_t tid;
size_t bid;
size_t cid;
std::byte *const *inouts;
const std::byte *rdata;

static cpu_thread_context_t &current() noexcept;
};
}

namespace nncase::ntt::runtime {
extern const nncase_runtime_cpu_mt_t *cpu_mt;
extern size_t tdim;
extern size_t bdim;
extern size_t cdim;

extern thread_local size_t tid;
extern thread_local size_t bid;
extern thread_local size_t cid;
} // namespace nncase::ntt::runtime

namespace nncase::ntt {
template <> struct program_id_getter<0> {
static size_t id() noexcept { return runtime::tid; }
static size_t id() noexcept {
return runtime::cpu_thread_context_t::current().tid;
}
static size_t dim() noexcept { return runtime::tdim; }
};

template <> struct program_id_getter<1> {
static size_t id() noexcept { return runtime::bid; }
static size_t id() noexcept {
return runtime::cpu_thread_context_t::current().bid;
}
static size_t dim() noexcept { return runtime::bdim; }
};

template <> struct program_id_getter<2> {
static size_t id() noexcept { return runtime::cid; }
static size_t id() noexcept {
return runtime::cpu_thread_context_t::current().cid;
}
static size_t dim() noexcept { return runtime::cdim; }
};
} // namespace nncase::ntt

extern "C" void
block_entry(const nncase::ntt::runtime::cpu_block_entry_params_t &params);
using block_entry_t = decltype(block_entry) *;
Loading

0 comments on commit 78dffe6

Please sign in to comment.