Skip to content

Commit 05d1eb4

Browse files
authored
Merge pull request #2 from arsnyder16/main
add reduction, unnamed/named critical sections, extra compilation uni…
2 parents 8e7831d + e2dba79 commit 05d1eb4

File tree

7 files changed

+96
-3
lines changed

7 files changed

+96
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
build
22
*.wasm
3+
build-em
4+
wasi-sdk*

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
url = https://github.com/llvm/llvm-project
44
branch = main
55
shallow = true
6+
[submodule "emsdk"]
7+
path = emsdk
8+
url = https://github.com/emscripten-core/emsdk

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ example.wasm: example.c $(OPENMP_LIB)
3030
make check-wasi-sdk
3131
$(WASI_CC) -fopenmp=libomp -g --sysroot=$(WASI_SYSROOT) --target=wasm32-wasi-threads \
3232
-I$(BUILD_DIR)/runtime/src -I$(WASI_SYSROOT)/include -pthread \
33-
-Wl,--import-memory,--export-memory,--max-memory=67108864 example.c \
33+
-Wl,--import-memory,--export-memory,--max-memory=67108864 example.c other.c \
3434
-L$(BUILD_DIR)/runtime/src -lomp \
3535
-lwasi-emulated-getpid \
3636
-o example.wasm

emscripten.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash -e
2+
set -e
3+
4+
PROJECT_DIR=`realpath .`
5+
EMSDK_DIR=$PROJECT_DIR/emsdk
6+
BUILD_DIR=build-em
7+
OPENMP_DIR=${OPENMP_DIR:=$PROJECT_DIR/llvm-project/openmp}
8+
OPENMP_LIB=$BUILD_DIR/lib/libomp.a
9+
10+
mkdir -p $BUILD_DIR
11+
12+
./emsdk/emsdk install latest
13+
./emsdk/emsdk activate latest
14+
source $EMSDK_DIR/emsdk_env.sh
15+
16+
export CFLAGS="-pthread"
17+
export CXXFLAGS="-pthread"
18+
emcmake cmake \
19+
-DOPENMP_STANDALONE_BUILD=ON \
20+
-DOPENMP_ENABLE_OMPT_TOOLS=OFF \
21+
-DOPENMP_ENABLE_LIBOMPTARGET=OFF \
22+
-DLIBOMP_HAVE_OMPT_SUPPORT=OFF \
23+
-DLIBOMP_OMPT_SUPPORT=OFF \
24+
-DLIBOMP_OMPD_SUPPORT=OFF \
25+
-DLIBOMP_USE_DEBUGGER=OFF \
26+
-DLIBOMP_FORTRAN_MODULES=OFF \
27+
-DLIBOMP_ENABLE_SHARED=OFF \
28+
-DCMAKE_INSTALL_PREFIX=$BUILD_DIR \
29+
-B $BUILD_DIR \
30+
-S $OPENMP_DIR
31+
32+
emmake make -C $BUILD_DIR -j
33+
emmake make install -C $BUILD_DIR
34+
35+
echo
36+
echo "!!! adjust clang being used you will need to update the clang in $EMSDK_DIR/upstream/bin !!!"
37+
echo
38+
39+
echo "Building example..."
40+
41+
emcc -I$BUILD_DIR/include $OPENMP_LIB -fopenmp=libomp -pthread example.c other.c -o $BUILD_DIR/example.js
42+
43+
echo "Running example..."
44+
node $BUILD_DIR/example.js

emsdk

Submodule emsdk added at ca7b40a

example.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
11
#include <omp.h>
22
#include <stdio.h>
33

4+
extern void InvokeOther();
5+
46
int main(int argc, char** argv) {
5-
#pragma omp parallel num_threads(8)
6-
{ printf("Hello World... from thread = %d\n", omp_get_thread_num()); }
7+
int critical_unamed = 0;
8+
int critical_named = 0;
9+
int reduction = 0;
10+
#pragma omp parallel num_threads(8) reduction(+ : reduction)
11+
{
12+
printf("Hello World... from thread = %d\n", omp_get_thread_num());
13+
#pragma omp critical
14+
{
15+
++critical_unamed;
16+
}
17+
#pragma omp critical (NAME)
18+
{
19+
++critical_named;
20+
}
21+
++reduction;
22+
}
23+
printf("example.c critical_unamed: %d\n", critical_unamed);
24+
printf("example.c critical_named: %d\n", critical_named);
25+
printf("example.c reduction: %d\n", reduction);
26+
InvokeOther();
727
}

other.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <omp.h>
2+
#include <stdio.h>
3+
4+
void InvokeOther() {
5+
int critical_unamed = 0;
6+
int critical_named = 0;
7+
int reduction = 0;
8+
#pragma omp parallel num_threads(8) reduction(+ : reduction)
9+
{
10+
#pragma omp critical
11+
{
12+
++critical_unamed;
13+
}
14+
#pragma omp critical (NAME)
15+
{
16+
++critical_named;
17+
}
18+
++reduction;
19+
}
20+
printf("other.c critical_unamed: %d\n", critical_unamed);
21+
printf("other.c critical_named: %d\n", critical_named);
22+
printf("other.c reduction: %d\n", reduction);
23+
}

0 commit comments

Comments
 (0)