Skip to content

Commit 943bdf5

Browse files
authored
Merge pull request #18 from gjbex/development
Add notebook on Julia Set and numba
2 parents 7a95c7d + e1c4f8e commit 943bdf5

File tree

116 files changed

+1125
-237
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+1125
-237
lines changed

python_for_hpc.pptx

-16 Bytes
Binary file not shown.

source-code/README.md

+1-1

source-code/cython/Classes/Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
VERSION = cpython-311-x86_64-linux-gnu
2-
POINTS_LIB = points.$(VERSION).so
3-
POINTS_PURE_LIB = points_pure.$(VERSION).so
1+
VERSION = $(shell python3-config --extension-suffix)
2+
POINTS_LIB = points$(VERSION)
3+
POINTS_PURE_LIB = points_pure$(VERSION)
44

55
all: $(POINTS_LIB)
66

source-code/cython/Exceptions/Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
VERSION = cpython-311-x86_64-linux-gnu
2-
AVERAGE_LIB = average.$(VERSION).so
3-
AVERAGE_PURE_LIB = average_pure.$(VERSION).so
1+
VERSION = $(shell python3-config --extension-suffix)
2+
AVERAGE_LIB = average$(VERSION)
3+
AVERAGE_PURE_LIB = average_pure$(VERSION)
44

55
all: $(AVERAGE_LIB) $(AVERAGE_PURE_LIB)
66

source-code/cython/HelloWorld/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = cpython-311-x86_64-linux-gnu
1+
VERSION = $(sheel python3-config --extension-suffix)
22
HELLO_WORLD_LIB = hello_world.$(VERSION).so
33

44
all: $(HELLO_WORLD_LIB)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.c

source-code/cython/Numpy/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = cpython-311-x86_64-linux-gnu
1+
VERSION = $(shell python3-config --extension-suffix)
22
ARRAY_SUM_LIB = array_sum.$(VERSION).so
33
ARRAY_INIT_LIB = array_init.$(VERSION).so
44
ARRAY_SUM_PURE_LIB = array_sum_pure.$(VERSION).so

source-code/cython/Pi/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ $(LIB_FILES): $(PYX_FILES)
99

1010
clean:
1111
python setup.py clean
12-
rm -f $(C_FILES) $(LIB_FILES)
12+
$(RM) $(C_FILES) $(LIB_FILES)

source-code/cython/Pointers/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = cpython-311-x86_64-linux-gnu
1+
VERSION = $(shell python3-config --extension-suffix)
22
CYTHON_LIB = pointers_cython.$(VERSION).so
33
PURE_LIB = pointers_pure.$(VERSION).so
44

source-code/cython/Primes/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = cpython-311-x86_64-linux-gnu
1+
VERSION = $(shell python3-config --extension-suffix)
22
PRIMES_LIBS = primes_cython.$(VERSION).so primes_cython3.$(VERSION).so \
33
primes_pure_python.$(VERSION).so primes_pure_malloc.$(VERSION).so \
44
primes_malloc.$(VERSION).so

source-code/cython/Profiling/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = cpython-35m-x86_64-linux-gnu
1+
VERSION = $(shell python3-config --extension-suffix)
22

33
QUAD_LIBS = quad.$(VERSION).so quad_prof.$(VERSION).so \
44
quad_prof_indiv.$(VERSION).so
@@ -10,4 +10,4 @@ all: $(QUAD_LIBS)
1010

1111
clean:
1212
python setup.py clean
13-
rm -f *.c $(QUAD_LIBS)
13+
$(RM) *.c $(QUAD_LIBS)
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = cpython-34m
1+
VERSION = $(shell python3-config --extension-suffix)
22
COMPOUNDS_LIB = compounds.$(VERSION).so
33

44
all: $(COMPOUNDS_LIB)
@@ -8,4 +8,4 @@ $(COMPOUNDS_LIB): compounds.pyx
88

99
clean:
1010
python setup.py clean
11-
rm -f compounds.c $(COMPOUNDS_LIB)
11+
$(RM) compounds.c $(COMPOUNDS_LIB)

source-code/cython/Structures/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = cpython-311-x86_64-linux-gnu
1+
VERSION = $(shell python3-config --extension-suffix)
22
CYTHON_LIB = point_cython.$(VERSION).so
33
PURE_LIB = point_pure.$(VERSION).so
44

source-code/interfaciing-c-c++-fortran/Pybind11/Stats/CMakeLists.txt

-5
This file was deleted.

source-code/interfaciing-c-c++-fortran/Pybind11/Stats/src/statistics/statistics.h

-47
This file was deleted.

source-code/interfaciing-c-c++-fortran/Pybind11/Stats/test.py

-15
This file was deleted.

source-code/interfaciing-c-c++-fortran/Pybind11/pybind11.yml

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cmake_minimum_required(VERSION 3.18)
2+
project(algorithms LANGUAGES CXX)
3+
4+
set(CMAKE_CXX_STANDARD 23)
5+
set(CMaKE_CXX_STANDARD_REQUIRED YES)
6+
set(CMAKE_CXX_EXTENSIONS NO)
7+
8+
add_compile_options(-Wall -Wextra -Wpedantic)
9+
10+
add_subdirectory(src)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
add_subdirectory(convolution)
2+
add_subdirectory(bindings)
3+
4+
add_executable(test_convolution.exe
5+
test_convolution.cpp)
6+
target_include_directories(test_convolution.exe
7+
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/convolution)
8+
target_link_libraries(test_convolution.exe
9+
PRIVATE convolution)
10+
11+
add_executable(benchmark_convolution.exe
12+
benchmark_convolution.cpp)
13+
target_include_directories(benchmark_convolution.exe
14+
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/convolution)
15+
target_link_libraries(benchmark_convolution.exe
16+
PRIVATE convolution)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <convolution.h>
2+
#include <chrono>
3+
#include <iostream>
4+
#include <numeric>
5+
#include <random>
6+
7+
Matrix create_image(int rows, int cols) {
8+
std::mt19937 gen(1234);
9+
std::uniform_real_distribution<double> dis(0.0, 1.0);
10+
Matrix image(rows, cols);
11+
for (int i = 0; i < rows; ++i) {
12+
for (int j = 0; j < cols; ++j) {
13+
image(i, j) = dis(gen);
14+
}
15+
}
16+
return image;
17+
}
18+
19+
Matrix create_kernel(int rows, int cols) {
20+
Matrix kernel(rows, cols);
21+
for (int i = 0; i < rows; ++i) {
22+
for (int j = 0; j < cols; ++j) {
23+
kernel(i, j) = 1.0/(rows*cols);
24+
}
25+
}
26+
return kernel;
27+
}
28+
29+
double element_sum(const Matrix& matrix) {
30+
return std::accumulate(matrix.data(), matrix.data() + matrix.rows()*matrix.cols(), 0.0);
31+
}
32+
33+
int main(int argc, char** argv) {
34+
int rows = 1000;
35+
int cols = 1000;
36+
int kernel_rows = 7;
37+
int kernel_cols = 7;
38+
if (argc > 1) {
39+
rows = atoi(argv[1]);
40+
cols = atoi(argv[1]);
41+
}
42+
if (argc > 2) {
43+
kernel_rows = atoi(argv[2]);
44+
kernel_cols = atoi(argv[2]);
45+
}
46+
std::cout << "Image size: " << rows << "x" << cols << "\n";
47+
std::cout << "Kernel size: " << kernel_rows << "x" << kernel_cols << "\n";
48+
Matrix image = create_image(rows, cols);
49+
Matrix kernel = create_kernel(kernel_rows, kernel_cols);
50+
auto start = std::chrono::high_resolution_clock::now();
51+
auto result = convolve(image, kernel);
52+
auto end = std::chrono::high_resolution_clock::now();
53+
std::chrono::duration<double> diff = end - start;
54+
std::cout << "Time: " << diff.count() << " s\n";
55+
std::cout << "Sum: " << element_sum(result) << "\n";
56+
return 0;
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
find_package(pybind11 REQUIRED)
2+
3+
pybind11_add_module(matrices matrix_bind.cpp)
4+
target_include_directories(matrices PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../convolution)
5+
target_link_libraries(matrices PUBLIC convolution)
6+
7+
pybind11_add_module(convolve convolve_bind.cpp)
8+
target_include_directories(convolve PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../convolution)
9+
target_link_libraries(convolve PUBLIC convolution)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <convolution.h>
2+
#include <pybind11/pybind11.h>
3+
#include <pybind11/numpy.h>
4+
5+
namespace py = pybind11;
6+
7+
void convolve_func(py::buffer image_buffer, py::buffer kernel_buffer,
8+
py::buffer new_image_buffer) {
9+
py::buffer_info image_info = image_buffer.request();
10+
if (image_info.format != py::format_descriptor<double>::format())
11+
throw std::runtime_error("Incompatible format: expected a double array");
12+
if (image_info.ndim != 2)
13+
throw std::runtime_error("Incompatible buffer dimension");
14+
py::buffer_info kernel_info = kernel_buffer.request();
15+
if (kernel_info.format != py::format_descriptor<double>::format())
16+
throw std::runtime_error("Incompatible format: expected a double array");
17+
if (kernel_info.ndim != 2)
18+
throw std::runtime_error("Incompatible buffer dimension");
19+
py::buffer_info result_info = new_image_buffer.request();
20+
if (result_info.format != py::format_descriptor<double>::format())
21+
throw std::runtime_error("Incompatible format: expected a double array");
22+
if (result_info.ndim != 2)
23+
throw std::runtime_error("Incompatible buffer dimension");
24+
if (result_info.shape[0] != image_info.shape[0] + kernel_info.shape[0] - 1 ||
25+
result_info.shape[1] != image_info.shape[1] + kernel_info.shape[1] - 1)
26+
throw std::runtime_error("Incompatible result buffer shape");
27+
Matrix image(image_info.shape[0], image_info.shape[1]);
28+
Matrix kernel(kernel_info.shape[0], kernel_info.shape[1]);
29+
std::memcpy(image.data(), image_info.ptr, sizeof(double)*image.rows()*image.cols());
30+
std::memcpy(kernel.data(), kernel_info.ptr, sizeof(double)*kernel.rows()*kernel.cols());
31+
Matrix result = convolve(image, kernel);
32+
std::memcpy(result_info.ptr, result.data(), sizeof(double)*result.rows()*result.cols());
33+
}
34+
35+
PYBIND11_MODULE(convolve, module) {
36+
module.doc() = "pybind11 wrapper module for convolution.h";
37+
module.def("convolve", &convolve_func, "compute convolution of image with kernel");
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <matrices.h>
2+
#include <pybind11/pybind11.h>
3+
#include <pybind11/numpy.h>
4+
5+
namespace py = pybind11;
6+
7+
PYBIND11_MODULE(matrices, module) {
8+
module.doc() = "pybind11 wrapper module for matrices.h";
9+
py::class_<Matrix>(module, "Matrix", py::buffer_protocol())
10+
.def_buffer([](Matrix &m) -> py::buffer_info {
11+
return py::buffer_info(
12+
m.data(), /* Pointer to buffer */
13+
sizeof(double), /* Size of one scalar */
14+
py::format_descriptor<double>::format(), /* Python struct-style format descriptor */
15+
2, /* Number of dimensions */
16+
{m.rows(), m.cols()}, /* Buffer dimensions */
17+
{sizeof(double)*m.cols(), /* Strides (in bytes) for each index */
18+
sizeof(double)});
19+
})
20+
.def(py::init<int, int>(), "initialize matrix with given number of rows and columns")
21+
.def(py::init([](const py::buffer b) {
22+
py::buffer_info info = b.request();
23+
if (info.format != py::format_descriptor<double>::format())
24+
throw std::runtime_error("Incompatible format: expected a double array");
25+
if (info.ndim != 2)
26+
throw std::runtime_error("Incompatible buffer dimension!");
27+
Matrix m(info.shape[0], info.shape[1]);
28+
std::memcpy(m.data(), info.ptr, sizeof(double)*m.rows()*m.cols());
29+
return m;
30+
}), "initialize matrix from a numpy array");
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_library(convolution SHARED
2+
convolution.cpp matrices.cpp)

0 commit comments

Comments
 (0)