Skip to content

add reduction, unnamed/named critical sections, extra compilation uni… #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
build
*.wasm
build-em
wasi-sdk*
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
url = https://github.com/llvm/llvm-project
branch = main
shallow = true
[submodule "emsdk"]
path = emsdk
url = https://github.com/emscripten-core/emsdk
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ example.wasm: example.c $(OPENMP_LIB)
make check-wasi-sdk
$(WASI_CC) -fopenmp=libomp -g --sysroot=$(WASI_SYSROOT) --target=wasm32-wasi-threads \
-I$(BUILD_DIR)/runtime/src -I$(WASI_SYSROOT)/include -pthread \
-Wl,--import-memory,--export-memory,--max-memory=67108864 example.c \
-Wl,--import-memory,--export-memory,--max-memory=67108864 example.c other.c \
-L$(BUILD_DIR)/runtime/src -lomp \
-lwasi-emulated-getpid \
-o example.wasm
Expand Down
44 changes: 44 additions & 0 deletions emscripten.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash -e
set -e

PROJECT_DIR=`realpath .`
EMSDK_DIR=$PROJECT_DIR/emsdk
BUILD_DIR=build-em
OPENMP_DIR=${OPENMP_DIR:=$PROJECT_DIR/llvm-project/openmp}
OPENMP_LIB=$BUILD_DIR/lib/libomp.a

mkdir -p $BUILD_DIR

./emsdk/emsdk install latest
./emsdk/emsdk activate latest
source $EMSDK_DIR/emsdk_env.sh

export CFLAGS="-pthread"
export CXXFLAGS="-pthread"
emcmake cmake \
-DOPENMP_STANDALONE_BUILD=ON \
-DOPENMP_ENABLE_OMPT_TOOLS=OFF \
-DOPENMP_ENABLE_LIBOMPTARGET=OFF \
-DLIBOMP_HAVE_OMPT_SUPPORT=OFF \
-DLIBOMP_OMPT_SUPPORT=OFF \
-DLIBOMP_OMPD_SUPPORT=OFF \
-DLIBOMP_USE_DEBUGGER=OFF \
-DLIBOMP_FORTRAN_MODULES=OFF \
-DLIBOMP_ENABLE_SHARED=OFF \
-DCMAKE_INSTALL_PREFIX=$BUILD_DIR \
-B $BUILD_DIR \
-S $OPENMP_DIR

emmake make -C $BUILD_DIR -j
emmake make install -C $BUILD_DIR

echo
echo "!!! adjust clang being used you will need to update the clang in $EMSDK_DIR/upstream/bin !!!"
echo

echo "Building example..."

emcc -I$BUILD_DIR/include $OPENMP_LIB -fopenmp=libomp -pthread example.c other.c -o $BUILD_DIR/example.js

echo "Running example..."
node $BUILD_DIR/example.js
1 change: 1 addition & 0 deletions emsdk
Submodule emsdk added at ca7b40
24 changes: 22 additions & 2 deletions example.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
#include <omp.h>
#include <stdio.h>

extern void InvokeOther();

int main(int argc, char** argv) {
#pragma omp parallel num_threads(8)
{ printf("Hello World... from thread = %d\n", omp_get_thread_num()); }
int critical_unamed = 0;
int critical_named = 0;
int reduction = 0;
#pragma omp parallel num_threads(8) reduction(+ : reduction)
{
printf("Hello World... from thread = %d\n", omp_get_thread_num());
#pragma omp critical
{
++critical_unamed;
}
#pragma omp critical (NAME)
{
++critical_named;
}
++reduction;
}
printf("example.c critical_unamed: %d\n", critical_unamed);
printf("example.c critical_named: %d\n", critical_named);
printf("example.c reduction: %d\n", reduction);
InvokeOther();
}
23 changes: 23 additions & 0 deletions other.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <omp.h>
#include <stdio.h>

void InvokeOther() {
int critical_unamed = 0;
int critical_named = 0;
int reduction = 0;
#pragma omp parallel num_threads(8) reduction(+ : reduction)
{
#pragma omp critical
{
++critical_unamed;
}
#pragma omp critical (NAME)
{
++critical_named;
}
++reduction;
}
printf("other.c critical_unamed: %d\n", critical_unamed);
printf("other.c critical_named: %d\n", critical_named);
printf("other.c reduction: %d\n", reduction);
}