Skip to content

Commit 3216937

Browse files
committed
Merge branch 'omp_offloading_thread_example' into 'latest'
Omp offloading thread example See merge request ricos/allgebra!60
2 parents 20a8cf0 + b7a6ebe commit 3216937

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Unreleased
1212

1313
Added
1414
------
15+
- Add example of control gpu thread by openmp offloading https://gitlab.ritc.jp/ricos/allgebra/-/merge_requests/60
1516
- Add cmake, make, and ninja in doxygen container https://gitlab.ritc.jp/ricos/allgebra/-/merge_requests/58
1617

1718
21.10.1 - 2021/10/14

examples/clang_omp_offloading/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ CUDA_LIBS := -lcuda -lcublas -lcudart
2525

2626
.PHONY: test prof clean
2727

28-
TARGET := omp_offloading.out omp_offloading_cublas.out omp_offloading_math.out
28+
TARGET := omp_offloading.out omp_offloading_cublas.out omp_offloading_math.out omp_offloading_control_thread.out
2929
all: $(TARGET)
3030

3131
omp_offloading.out: omp_offloading.cpp
@@ -37,10 +37,14 @@ omp_offloading_cublas.out: omp_offloading_cublas.cpp
3737
omp_offloading_math.out: omp_offloading_math.cpp
3838
$(CXX) $(OMP_FLAGS) $(CXX_FLAGS) $< -o $@
3939

40+
omp_offloading_control_thread.out: omp_offloading_control_thread.cpp
41+
$(CXX) $(OMP_FLAGS) $(CXX_FLAGS) $< -o $@
42+
4043
test: $(TARGET)
4144
./omp_offloading.out 1000000
4245
./omp_offloading_cublas.out 1000000
4346
./omp_offloading_math.out 1000000
47+
./omp_offloading_control_thread.out 300
4448

4549
debug: $(TARGET)
4650
LIBOMPTARGET_DEBUG=1 ./omp_offloading_cublas.out 1000000
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright 2020 RICOS Co. Ltd.
3+
*
4+
* This file is a part of ricosjp/allgebra, distributed under Apache-2.0 License
5+
* https://github.com/ricosjp/allgebra
6+
*/
7+
8+
#include <cstdlib> // atoi, malloc
9+
#include <iostream>
10+
#include <omp.h>
11+
12+
int main(int argc, char **argv) {
13+
int size = 0;
14+
if (argc == 2) {
15+
size = std::atoi(argv[1]);
16+
} else {
17+
std::cout << "error, $1 is vector size" << std::endl;
18+
return 1;
19+
}
20+
21+
if (omp_is_initial_device()) {
22+
printf("CPU now\n");
23+
} else {
24+
return 1;
25+
}
26+
27+
double *x = (double *)malloc(sizeof(double) * size);
28+
double *y = (double *)malloc(sizeof(double) * size);
29+
30+
for (int i = 0; i < size; i++) {
31+
x[i] = 1.0;
32+
y[i] = 2.0;
33+
}
34+
35+
double dot = 0.0;
36+
37+
printf("devices %d / %d\n", omp_get_default_device(),
38+
omp_get_num_devices() - 1);
39+
40+
printf("GPU start\n");
41+
#pragma omp target teams distribute parallel for reduction(+ : dot) map (to: x[0:size], y[0:size]) map(tofrom: dot)
42+
for (int i = 0; i < size; i++) {
43+
44+
if (omp_is_initial_device()) {
45+
printf("omp offloading is not work\n");
46+
} else {
47+
int tid =
48+
omp_get_team_num() * omp_get_num_threads() + omp_get_thread_num();
49+
// cat use std::cout in omp target
50+
printf("i=%d: tid = %d (team %d/%d, thread %d/%d)\n", i, tid,
51+
omp_get_team_num(), omp_get_num_teams() - 1, omp_get_thread_num(),
52+
omp_get_num_threads() - 1);
53+
}
54+
55+
dot += x[i] * y[i];
56+
}
57+
58+
if (dot != 2.0 * size) {
59+
std::cout << "dot = " << dot << std::endl;
60+
std::cout << "error!" << std::endl;
61+
return 1;
62+
} else {
63+
std::cout << "dot = " << dot << std::endl;
64+
std::cout << "Pass!" << std::endl;
65+
return 0;
66+
}
67+
}

0 commit comments

Comments
 (0)