Skip to content

Commit

Permalink
omp is working?
Browse files Browse the repository at this point in the history
  • Loading branch information
victoryang00 committed Feb 9, 2024
1 parent 2959a2f commit 2de51f4
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 32 deletions.
4 changes: 1 addition & 3 deletions bench/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,14 @@ ExternalProject_Add(nas
CONFIGURE_COMMAND ${CMAKE_COMMAND}
-DCMAKE_BUILD_TYPE=Release
-GNinja -DWASI_SDK_DIR=${WASI_SDK} -DWASI_SDK_PREFIX=${WASI_SDK}
-DCMAKE_TOOLCHAIN_FILE=${WASI_SDK}/share/cmake/wasi-sdk.cmake
-DCMAKE_TOOLCHAIN_FILE=${WASI_SDK}/share/cmake/wasi-sdk-pthread.cmake
-DCMAKE_SYSROOT=${WASI_SDK}/share/wasi-sysroot -DCMAKE_C_COMPILER_WORK=1 -DCMAKE_CXX_COMPILER_WORK=1
${CMAKE_CURRENT_SOURCE_DIR}/nas
BUILD_COMMAND ${CMAKE_COMMAND} --build .
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different ./bt ${CMAKE_CURRENT_BINARY_DIR}/bt.wasm &&
${CMAKE_COMMAND} -E copy_if_different ./cg ${CMAKE_CURRENT_BINARY_DIR}/cg.wasm &&
${CMAKE_COMMAND} -E copy_if_different ./ep ${CMAKE_CURRENT_BINARY_DIR}/ep.wasm &&
${CMAKE_COMMAND} -E copy_if_different ./ft ${CMAKE_CURRENT_BINARY_DIR}/ft.wasm &&
${CMAKE_COMMAND} -E copy_if_different ./is ${CMAKE_CURRENT_BINARY_DIR}/is.wasm &&
${CMAKE_COMMAND} -E copy_if_different ./lu ${CMAKE_CURRENT_BINARY_DIR}/lu.wasm &&
${CMAKE_COMMAND} -E copy_if_different ./mg ${CMAKE_CURRENT_BINARY_DIR}/mg.wasm &&
${CMAKE_COMMAND} -E copy_if_different ./sp ${CMAKE_CURRENT_BINARY_DIR}/sp.wasm
Expand Down Expand Up @@ -197,7 +196,6 @@ if (WAMR_BUILD_AOT)
bench_wamr_app(cg)
bench_wamr_app(ep)
bench_wamr_app(ft)
bench_wamr_app(is)
bench_wamr_app(lu)
bench_wamr_app(mg)
bench_wamr_app(sp)
Expand Down
1 change: 1 addition & 0 deletions include/wamr.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class WAMRInstance {
std::mutex sync_op_mutex;
std::condition_variable sync_op_cv;
std::map<uint64, uint64> tid_map;
std::map<korp_tid, korp_tid> korp_tid_map;
std::map<uint64, uint64> child_tid_map;
std::map<uint64, std::pair<int, int>> tid_start_arg_map;
uint32 id{};
Expand Down
5 changes: 4 additions & 1 deletion include/wamr_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ void insert_fd(int, const char *, int, int, enum fd_op op);
void remove_fd(int);
void rename_fd(int, char const *, int, char const *);
bool is_atomic_checkpointable();
void wamr_handle_map(ssize_t old_tid, ssize_t new_tid);
void wamr_handle_map(uint64_t old_tid, uint64_t new_tid);
korp_tid wamr_get_new_korp_tid(korp_tid new_tid);
korp_tid wamr_get_korp_tid(korp_tid new_tid);
void lightweight_checkpoint(WASMExecEnv *);
void lightweight_uncheckpoint(WASMExecEnv *);
void wamr_wait(wasm_exec_env_t);
Expand All @@ -84,6 +86,7 @@ extern bool checkpoint;
extern bool is_debug;
extern int stop_func_index;
extern int cur_func_count;
void print_memory(WASMExecEnv *);

#ifdef __cplusplus
}
Expand Down
24 changes: 22 additions & 2 deletions src/wamr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,19 @@ void WAMRInstance::recover(std::vector<std::unique_ptr<WAMRExecEnv>> *e_) {
// get duration in us
auto dur = std::chrono::duration_cast<std::chrono::microseconds>(end - this->time);
fprintf(stderr, "Recover time: %f\n", dur.count() / 1000000.0);
// put things back
// fprintf(stderr, "invoke 1%p\n",((WASMModuleInstance *)exec_env->module_inst)->global_data);

((WASMModuleInstance *)exec_env->module_inst)->global_data =
(uint8 *)malloc(((WASMModuleInstance *)exec_env->module_inst)->global_data_size);

memcpy(((WASMModuleInstance *)exec_env->module_inst)->global_data,
execEnv.front()->module_inst.global_data.data(),
((WASMModuleInstance *)exec_env->module_inst)->global_data_size);
// for (int i = 0; i < ((WASMModuleInstance *)exec_env->module_inst)->global_data_size; i++) {
// fprintf(stderr, "%d", ((WASMModuleInstance *)exec_env->module_inst)->global_data[i]);
// }
// fprintf(stderr, "invoke 2%p\n",((WASMModuleInstance *)exec_env->module_inst)->global_data);
invoke_main();
}
}
Expand Down Expand Up @@ -818,7 +831,6 @@ void WAMRInstance::set_wasi_args(WAMRWASIContext &context) {
extern WAMRInstance *wamr;
extern "C" { // stop name mangling so it can be linked externally
void wamr_wait(wasm_exec_env_t exec_env) {

LOGV(DEBUG) << fmt::format("child getting ready to wait {}", fmt::ptr(exec_env));
thread_init.release(1);
wamr->spawn_child(exec_env, false);
Expand All @@ -833,7 +845,15 @@ void wamr_wait(wasm_exec_env_t exec_env) {
// finished restoring
exec_env->is_restore = true;
// setting back handle
exec_env->handle = wamr->tid_map[exec_env->handle];
// exec_env->handle = wamr->tid_map[exec_env->handle];
// ((WASMModuleInstance *)exec_env->module_inst)->memories = wamr->tmp_buf;
// ((WASMModuleInstance *)exec_env->module_inst)->memory_count = wamr->tmp_buf_size;

// for (int i = 0; i < ((WASMModuleInstance *)exec_env->module_inst)->global_data_size; i++) {
// fprintf(stderr, "%d", ((WASMModuleInstance *)exec_env->module_inst)->global_data[i]);
// }
// sleep(10);
fprintf(stderr, "invoke side%p\n", ((WASMModuleInstance *)exec_env->module_inst)->global_data);
}

WASMExecEnv *restore_env() {
Expand Down
1 change: 1 addition & 0 deletions src/wamr_exec_env.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "wamr_exec_env.h"
#include "aot_runtime.h"
#include "wamr.h"

extern WAMRInstance *wamr;
void WAMRExecEnv::dump_impl(WASMExecEnv *env) {
this->cur_count = env->handle;
Expand Down
19 changes: 16 additions & 3 deletions src/wamr_export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ int get_sock_fd(int fd) {
/** fopen, fseek, fwrite, fread */
void insert_fd(int fd, const char *path, int flags, int offset, enum fd_op op) {
if (fd > 2) {
// LOGV(INFO) << "insert_fd(fd,filename,flags, offset) fd:" << fd << " flags:" << flags << " offset:" << offset
// << " op:" << op;
LOGV(INFO) << "insert_fd(fd,filename,flags, offset) fd:" << fd << " flags:" << flags << " offset:" << offset
<< " op:" << op;
std::string path_;
std::vector<std::tuple<int, int, enum fd_op>> ops_;
std::tie(path_, ops_) = wamr->fd_map_[fd];
Expand Down Expand Up @@ -320,11 +320,24 @@ void change_thread_id_to_child(ssize_t tid, ssize_t child_tid) {
}
}
};
void wamr_handle_map(ssize_t old_tid, ssize_t tid) {
void wamr_handle_map(uint64_t old_tid, uint64_t tid) {
LOGV(ERROR) << fmt::format("wamr_handle_map old:< {} > new:< {} >", old_tid, tid);
wamr->tid_map[old_tid] = tid;
};

korp_tid wamr_get_new_korp_tid(korp_tid new_tid){
return wamr->tid_map[new_tid];
}

korp_tid wamr_get_korp_tid(korp_tid new_tid){
for (auto &[old_tid, new_tid_v] : wamr->tid_map) {
if(new_tid == new_tid_v){
return old_tid;
}
}
return 0;
}

void insert_parent_child(ssize_t tid, ssize_t child_tid) {
LOGV(ERROR) << fmt::format("insert_parent_child {} {}", tid, child_tid);
wamr->child_tid_map[child_tid] = tid;
Expand Down
18 changes: 9 additions & 9 deletions src/wamr_mmeory_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ void WAMRMemoryInstance::restore_impl(WASMMemoryInstance *env) {
env->module_type = module_type;
env->ref_count = ref_count + 1;
LOGV(ERROR) << "ref_count:" << env->ref_count;
env->is_shared_memory = is_shared_memory;
env->is_shared_memory = true;
env->num_bytes_per_page = num_bytes_per_page;
env->cur_page_count = cur_page_count;
env->max_page_count = max_page_count;
env->memory_data_size = memory_data.size();
#if !defined(_WIN32)
if (env->ref_count > 0) // shared memory
env->memory_data =
(uint8 *)mmap(NULL, wamr->heap_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
else
#endif
env->memory_data = (uint8 *)malloc(env->memory_data_size);
// #if !defined(_WIN32)
// if (env->ref_count > 0) // shared memory
// env->memory_data =
// (uint8 *)mmap(NULL, wamr->heap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
// else
// #endif
env->memory_data = (uint8 *)malloc(env->memory_data_size);
memcpy(env->memory_data, memory_data.data(), env->memory_data_size);
env->memory_data_end = env->memory_data + memory_data.size();
env->memory_data_end = env->memory_data + (memory_data.size());
env->heap_data = (uint8 *)malloc(heap_data.size());
memcpy(env->heap_data, heap_data.data(), heap_data.size());
env->heap_data_end = env->heap_data + heap_data.size();
Expand Down
41 changes: 29 additions & 12 deletions src/wamr_module_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ void WAMRModuleInstance::dump_impl(WASMModuleInstance *env) {
// }
global_data = std::vector<uint8>(env->global_data, env->global_data + env->global_data_size);
dump(&wasi_ctx, &env->module->wasi_args);

// for (int i = 0; i < env->global_data_size; i++) {
// fprintf(stderr, "%d", env->global_data[i]);
// }
fprintf(stderr, "\nDumped global data ptr: %p\n", env->global_data);
if (wamr->is_aot) {
auto module = (AOTModule *)env->module;
aux_data_end_global_index = module->aux_data_end_global_index;
Expand All @@ -37,28 +40,42 @@ void WAMRModuleInstance::dump_impl(WASMModuleInstance *env) {
aux_stack_size = module->aux_stack_size;
}
dump(&global_table_data, env->global_table_data.memory_instances);
fprintf(stderr, "\nDumped global data size: %d\n", env->table_count);
}

void WAMRModuleInstance::restore_impl(WASMModuleInstance *env) {
if (!wamr->tmp_buf) {
env->memory_count = memories.size();
for (int i = 0; i < env->memory_count; i++) {
restore(&memories[i], env->memories[i]);
}
wamr->tmp_buf = env->memories;
wamr->tmp_buf_size = env->memory_count;
restore(&global_table_data, env->global_table_data.memory_instances);
// auto m_ = (WASMMemoryInstance **)malloc(env->memory_count * sizeof(WASMMemoryInstance *));
// wamr->tmp_buf = m_;
// wamr->tmp_buf_size = env->memory_count;
env->memory_count = memories.size();
// for (int i = 0; i < env->memory_count; i++) {
// m_[i] = (WASMMemoryInstance *)malloc(sizeof(WASMMemoryInstance));
// }
for (int i = 0; i < env->memory_count; i++) {
restore(&memories[i], env->memories[i]);
// restore(&memories[i], m_[i]);
}
wamr->tmp_buf = env->memories;
// env->memories = m_;
wamr->tmp_buf_size = env->memory_count;

env->global_data_size = global_data.size();
// restore(&global_table_data, env->global_table_data.memory_instances);

} else {
env->memory_count = wamr->tmp_buf_size;
env->memories = wamr->tmp_buf;
}
env->global_table_data.memory_instances[0] = **env->memories;
env->global_data = (uint8 *)malloc(env->global_data_size);
fprintf(stderr, "\nRestored global data ptr: %p\n", env->global_data);
memcpy(env->global_data, global_data.data(), global_data.size());
env->global_data_size = global_data.size();
// env->global_data = global_data.data();
// env->global_data_size = global_data.size() - 1;

LOGV(DEBUG) << env->global_data_size;
LOGV(DEBUG) << env->global_data;
for (int i = 0; i < env->global_data_size; i++) {
fprintf(stderr, "%d", env->global_data[i]);
}
if (wamr->is_aot) {
auto module = (AOTModule *)env->module;
module->aux_data_end_global_index = aux_data_end_global_index;
Expand Down
3 changes: 3 additions & 0 deletions test/multi-thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
int g_count = 0;
pthread_mutex_t g_count_lock;
static void *thread(void *arg) {
while(true){
getpid();
}
for (int i = 0; i < NUM_ITER; i++) {
__atomic_fetch_add(&g_count, 1, __ATOMIC_SEQ_CST);
// if (i % 100 == 0)
Expand Down
1 change: 1 addition & 0 deletions test/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ static void *thread(void *arg) {
for (int i = 0; i < NUM_ITER; i++) {
pthread_mutex_lock(&m);
g_count++;
printf("print!!!%d\n", getpid());
pthread_mutex_unlock(&m);
}
printf("Value of g_count is %d\n", g_count);
Expand Down
2 changes: 1 addition & 1 deletion test/openmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ int main(int argc, char **argv) {
#pragma omp parallel
{
printf("Hello World... from thread = %d\n", omp_get_thread_num());
for (int i = 0; i < 1000; i++) {
for (int i = 0; i < 10000; i++) {
__atomic_fetch_add(&g_count, 1, __ATOMIC_SEQ_CST);
printf("print!!!%d\n", i);
}
Expand Down

0 comments on commit 2de51f4

Please sign in to comment.