Skip to content

Commit a12c625

Browse files
committed
First commit
0 parents  commit a12c625

File tree

6 files changed

+151
-0
lines changed

6 files changed

+151
-0
lines changed

.envrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
case $HOSTNAME in
2+
(minazo)
3+
use nix '<nixpkgs>' -A dev-shells.mrchem
4+
;;
5+
(*)
6+
;;
7+
esac
8+
#layout pipenv

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
build*
2+
install*
3+
debug*
4+
.idea
5+
.ipynb_checkpoints
6+
*.ipynb
7+
*.swp
8+
cmake-build-debug

CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# define minimum cmake version
2+
cmake_minimum_required(VERSION 3.11 FATAL_ERROR)
3+
4+
# project name and supported language
5+
project(ompybind11 LANGUAGES CXX)
6+
7+
# require C++11
8+
set(CMAKE_CXX_STANDARD 11)
9+
set(CMAKE_CXX_EXTENSIONS OFF)
10+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
11+
12+
option(ENABLE_OPENMP "Use OpenMP parallelization" OFF)
13+
14+
# for testing we will need the python interpreter
15+
find_package(PythonInterp REQUIRED)
16+
17+
add_subdirectory(ompybind11)
18+
19+
# turn on testing
20+
enable_testing()
21+
22+
# define test
23+
add_test(
24+
NAME
25+
python_test
26+
COMMAND
27+
${CMAKE_COMMAND} -E env OMPYBIND11_MODULE_PATH=$<TARGET_FILE_DIR:ompybind11>
28+
${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/ompybind11/test.py
29+
)

ompybind11/CMakeLists.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
include(FetchContent)
2+
3+
FetchContent_Declare(
4+
pybind11_sources
5+
GIT_REPOSITORY https://github.com/pybind/pybind11.git
6+
GIT_TAG v2.2
7+
)
8+
9+
FetchContent_GetProperties(pybind11_sources)
10+
11+
if(NOT pybind11_sources_POPULATED)
12+
FetchContent_Populate(pybind11_sources)
13+
14+
add_subdirectory(
15+
${pybind11_sources_SOURCE_DIR}
16+
${pybind11_sources_BINARY_DIR}
17+
)
18+
endif()
19+
20+
if(ENABLE_OPENMP)
21+
find_package(OpenMP REQUIRED)
22+
endif()
23+
24+
# create python module
25+
add_library(ompybind11
26+
MODULE
27+
ompybind11.cpp
28+
)
29+
30+
target_link_libraries(ompybind11
31+
PUBLIC
32+
pybind11::module
33+
$<$<AND:$<BOOL:ENABLE_OPENMP>,$<BOOL:OpenMP_FOUND>>:OpenMP::OpenMP_CXX>
34+
)
35+
36+
set_target_properties(ompybind11
37+
PROPERTIES
38+
PREFIX "${PYTHON_MODULE_PREFIX}"
39+
SUFFIX "${PYTHON_MODULE_EXTENSION}"
40+
)

ompybind11/ompybind11.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <functional>
2+
#include <iostream>
3+
4+
#include <omp.h>
5+
6+
#include <pybind11/functional.h>
7+
#include <pybind11/pybind11.h>
8+
9+
namespace py = pybind11;
10+
11+
double eval_func(int rank, std::function<double(double)> func, double x) {
12+
double out = 0.0;
13+
#pragma omp parallel shared(out)
14+
{
15+
int thread_num = omp_get_thread_num();
16+
if (thread_num == rank) {
17+
std::cout << "Thread " << thread_num << " working\n";
18+
out = func(x);
19+
}
20+
}
21+
return out;
22+
}
23+
24+
double calc_pi(int n) {
25+
double step = 1.0 / n;
26+
double s = 0.0;
27+
28+
#pragma omp parallel
29+
{
30+
double x;
31+
#pragma omp for reduction(+ : s)
32+
for (int i = 0; i < n; i++) {
33+
x = (i + 0.5) * step;
34+
s += 4.0 / (1 + x * x);
35+
}
36+
}
37+
return step * s;
38+
};
39+
40+
PYBIND11_MODULE(ompybind11, m) {
41+
m.def("eval_func", &eval_func, py::arg("rank"), py::arg("function"),
42+
py::arg("x"))
43+
.def("calc_pi", [](int n) {
44+
return calc_pi(n);
45+
});
46+
}

ompybind11/test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os
2+
import sys
3+
4+
sys.path.append(os.getenv('OMPYBIND11_MODULE_PATH'))
5+
import ompybind11 # isort:skip
6+
7+
8+
def f(x):
9+
return x*x
10+
11+
x = 2.0
12+
13+
pi = ompybind11.calc_pi(100)
14+
print('pi is {}'.format(pi))
15+
16+
foo = ompybind11.eval_func(0, f, x)
17+
print('Output of eval_func {}\n'.format(foo))
18+
19+
bar = ompybind11.eval_func(1, f, x)
20+
print('Output of eval_func {}\n'.format(bar))

0 commit comments

Comments
 (0)