Skip to content

Commit

Permalink
Merge pull request #18 from gjbex/development
Browse files Browse the repository at this point in the history
Add notebook on Julia Set and numba
  • Loading branch information
gjbex authored Jan 30, 2024
2 parents 7a95c7d + e1c4f8e commit 943bdf5
Show file tree
Hide file tree
Showing 116 changed files with 1,125 additions and 237 deletions.
Binary file modified python_for_hpc.pptx
Binary file not shown.
2 changes: 1 addition & 1 deletion source-code/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ to create it. There is some material not covered in the presentation as well.

1. `cython`: illustrations of how to use Cython to speed up Python.
1. `dask`: examples of how to use dask for distributed computing.
1. `interfaciing-c-c++-fortran`: illustrations of ctypes, SWIG and
1. `interfacing-c-c++-fortran`: illustrations of ctypes, SWIG and
f2py to interface with C, C++ and Fortran code.
1. `ising`: example of speeding up a Python simulation by wrapping
C++ code using SWIG.
Expand Down
6 changes: 3 additions & 3 deletions source-code/cython/Classes/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
VERSION = cpython-311-x86_64-linux-gnu
POINTS_LIB = points.$(VERSION).so
POINTS_PURE_LIB = points_pure.$(VERSION).so
VERSION = $(shell python3-config --extension-suffix)
POINTS_LIB = points$(VERSION)
POINTS_PURE_LIB = points_pure$(VERSION)

all: $(POINTS_LIB)

Expand Down
6 changes: 3 additions & 3 deletions source-code/cython/Exceptions/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
VERSION = cpython-311-x86_64-linux-gnu
AVERAGE_LIB = average.$(VERSION).so
AVERAGE_PURE_LIB = average_pure.$(VERSION).so
VERSION = $(shell python3-config --extension-suffix)
AVERAGE_LIB = average$(VERSION)
AVERAGE_PURE_LIB = average_pure$(VERSION)

all: $(AVERAGE_LIB) $(AVERAGE_PURE_LIB)

Expand Down
2 changes: 1 addition & 1 deletion source-code/cython/HelloWorld/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = cpython-311-x86_64-linux-gnu
VERSION = $(sheel python3-config --extension-suffix)
HELLO_WORLD_LIB = hello_world.$(VERSION).so

all: $(HELLO_WORLD_LIB)
Expand Down
1 change: 1 addition & 0 deletions source-code/cython/Numpy/Convolution/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.c
2 changes: 1 addition & 1 deletion source-code/cython/Numpy/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = cpython-311-x86_64-linux-gnu
VERSION = $(shell python3-config --extension-suffix)
ARRAY_SUM_LIB = array_sum.$(VERSION).so
ARRAY_INIT_LIB = array_init.$(VERSION).so
ARRAY_SUM_PURE_LIB = array_sum_pure.$(VERSION).so
Expand Down
2 changes: 1 addition & 1 deletion source-code/cython/Pi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ $(LIB_FILES): $(PYX_FILES)

clean:
python setup.py clean
rm -f $(C_FILES) $(LIB_FILES)
$(RM) $(C_FILES) $(LIB_FILES)
2 changes: 1 addition & 1 deletion source-code/cython/Pointers/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = cpython-311-x86_64-linux-gnu
VERSION = $(shell python3-config --extension-suffix)
CYTHON_LIB = pointers_cython.$(VERSION).so
PURE_LIB = pointers_pure.$(VERSION).so

Expand Down
2 changes: 1 addition & 1 deletion source-code/cython/Primes/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = cpython-311-x86_64-linux-gnu
VERSION = $(shell python3-config --extension-suffix)
PRIMES_LIBS = primes_cython.$(VERSION).so primes_cython3.$(VERSION).so \
primes_pure_python.$(VERSION).so primes_pure_malloc.$(VERSION).so \
primes_malloc.$(VERSION).so
Expand Down
4 changes: 2 additions & 2 deletions source-code/cython/Profiling/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = cpython-35m-x86_64-linux-gnu
VERSION = $(shell python3-config --extension-suffix)

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

clean:
python setup.py clean
rm -f *.c $(QUAD_LIBS)
$(RM) *.c $(QUAD_LIBS)
4 changes: 2 additions & 2 deletions source-code/cython/PythonCompoundTypes/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = cpython-34m
VERSION = $(shell python3-config --extension-suffix)
COMPOUNDS_LIB = compounds.$(VERSION).so

all: $(COMPOUNDS_LIB)
Expand All @@ -8,4 +8,4 @@ $(COMPOUNDS_LIB): compounds.pyx

clean:
python setup.py clean
rm -f compounds.c $(COMPOUNDS_LIB)
$(RM) compounds.c $(COMPOUNDS_LIB)
2 changes: 1 addition & 1 deletion source-code/cython/Structures/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = cpython-311-x86_64-linux-gnu
VERSION = $(shell python3-config --extension-suffix)
CYTHON_LIB = point_cython.$(VERSION).so
PURE_LIB = point_pure.$(VERSION).so

Expand Down

This file was deleted.

This file was deleted.

15 changes: 0 additions & 15 deletions source-code/interfaciing-c-c++-fortran/Pybind11/Stats/test.py

This file was deleted.

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.18)
project(algorithms LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMaKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS NO)

add_compile_options(-Wall -Wextra -Wpedantic)

add_subdirectory(src)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Convolution

Illustration of various concepts using a naive convolution algorithm
as an example.


## What is it?

1. `src`: source directory of C++ code.
a. `convolution`: directory that contains the convolution and matrix code.
1. `matrices.h`: definition of the Matrix class.
1. `matrices.cpp` definition of methods for the Matrix class.
1. `convolution.h`: decleartion of the convolution functions.
1. `convolution.cpp`: implementation of the convolution functions.
1. `CMakeLists.txt`: CMake file to build the shared library.
a. `bindings`: directory that contains the pybind11 bindings
for Matrix and convolve.
1. `matrix_bind.cpp`: pybind11 bindings for the Matrix class.
1. `convolve_bind.cpp`: pybind11 bindings for the convolve function.
This uses the buffer protocol.
1. `CMakeLists.txt`: CMake file to build the Python modules.
a. `test_convolution.cpp`: C++ application to test the C++ convolution
implementation.
a. benchmark_convolution.cpp`: C++ application to benchmark the C++ convolution
implementation.
a. `CMakeLists.txt`: CMake file to build the library, modules and C++
applications.
1. `CMakeLists.txt`: CMake file to build the library, modules and C++
applications.
1. `test_convolution.py`: Python script to test and benchmark the generated
modules.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
add_subdirectory(convolution)
add_subdirectory(bindings)

add_executable(test_convolution.exe
test_convolution.cpp)
target_include_directories(test_convolution.exe
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/convolution)
target_link_libraries(test_convolution.exe
PRIVATE convolution)

add_executable(benchmark_convolution.exe
benchmark_convolution.cpp)
target_include_directories(benchmark_convolution.exe
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/convolution)
target_link_libraries(benchmark_convolution.exe
PRIVATE convolution)
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <convolution.h>
#include <chrono>
#include <iostream>
#include <numeric>
#include <random>

Matrix create_image(int rows, int cols) {
std::mt19937 gen(1234);
std::uniform_real_distribution<double> dis(0.0, 1.0);
Matrix image(rows, cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
image(i, j) = dis(gen);
}
}
return image;
}

Matrix create_kernel(int rows, int cols) {
Matrix kernel(rows, cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
kernel(i, j) = 1.0/(rows*cols);
}
}
return kernel;
}

double element_sum(const Matrix& matrix) {
return std::accumulate(matrix.data(), matrix.data() + matrix.rows()*matrix.cols(), 0.0);
}

int main(int argc, char** argv) {
int rows = 1000;
int cols = 1000;
int kernel_rows = 7;
int kernel_cols = 7;
if (argc > 1) {
rows = atoi(argv[1]);
cols = atoi(argv[1]);
}
if (argc > 2) {
kernel_rows = atoi(argv[2]);
kernel_cols = atoi(argv[2]);
}
std::cout << "Image size: " << rows << "x" << cols << "\n";
std::cout << "Kernel size: " << kernel_rows << "x" << kernel_cols << "\n";
Matrix image = create_image(rows, cols);
Matrix kernel = create_kernel(kernel_rows, kernel_cols);
auto start = std::chrono::high_resolution_clock::now();
auto result = convolve(image, kernel);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "Time: " << diff.count() << " s\n";
std::cout << "Sum: " << element_sum(result) << "\n";
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
find_package(pybind11 REQUIRED)

pybind11_add_module(matrices matrix_bind.cpp)
target_include_directories(matrices PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../convolution)
target_link_libraries(matrices PUBLIC convolution)

pybind11_add_module(convolve convolve_bind.cpp)
target_include_directories(convolve PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../convolution)
target_link_libraries(convolve PUBLIC convolution)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <convolution.h>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>

namespace py = pybind11;

void convolve_func(py::buffer image_buffer, py::buffer kernel_buffer,
py::buffer new_image_buffer) {
py::buffer_info image_info = image_buffer.request();
if (image_info.format != py::format_descriptor<double>::format())
throw std::runtime_error("Incompatible format: expected a double array");
if (image_info.ndim != 2)
throw std::runtime_error("Incompatible buffer dimension");
py::buffer_info kernel_info = kernel_buffer.request();
if (kernel_info.format != py::format_descriptor<double>::format())
throw std::runtime_error("Incompatible format: expected a double array");
if (kernel_info.ndim != 2)
throw std::runtime_error("Incompatible buffer dimension");
py::buffer_info result_info = new_image_buffer.request();
if (result_info.format != py::format_descriptor<double>::format())
throw std::runtime_error("Incompatible format: expected a double array");
if (result_info.ndim != 2)
throw std::runtime_error("Incompatible buffer dimension");
if (result_info.shape[0] != image_info.shape[0] + kernel_info.shape[0] - 1 ||
result_info.shape[1] != image_info.shape[1] + kernel_info.shape[1] - 1)
throw std::runtime_error("Incompatible result buffer shape");
Matrix image(image_info.shape[0], image_info.shape[1]);
Matrix kernel(kernel_info.shape[0], kernel_info.shape[1]);
std::memcpy(image.data(), image_info.ptr, sizeof(double)*image.rows()*image.cols());
std::memcpy(kernel.data(), kernel_info.ptr, sizeof(double)*kernel.rows()*kernel.cols());
Matrix result = convolve(image, kernel);
std::memcpy(result_info.ptr, result.data(), sizeof(double)*result.rows()*result.cols());
}

PYBIND11_MODULE(convolve, module) {
module.doc() = "pybind11 wrapper module for convolution.h";
module.def("convolve", &convolve_func, "compute convolution of image with kernel");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <matrices.h>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>

namespace py = pybind11;

PYBIND11_MODULE(matrices, module) {
module.doc() = "pybind11 wrapper module for matrices.h";
py::class_<Matrix>(module, "Matrix", py::buffer_protocol())
.def_buffer([](Matrix &m) -> py::buffer_info {
return py::buffer_info(
m.data(), /* Pointer to buffer */
sizeof(double), /* Size of one scalar */
py::format_descriptor<double>::format(), /* Python struct-style format descriptor */
2, /* Number of dimensions */
{m.rows(), m.cols()}, /* Buffer dimensions */
{sizeof(double)*m.cols(), /* Strides (in bytes) for each index */
sizeof(double)});
})
.def(py::init<int, int>(), "initialize matrix with given number of rows and columns")
.def(py::init([](const py::buffer b) {
py::buffer_info info = b.request();
if (info.format != py::format_descriptor<double>::format())
throw std::runtime_error("Incompatible format: expected a double array");
if (info.ndim != 2)
throw std::runtime_error("Incompatible buffer dimension!");
Matrix m(info.shape[0], info.shape[1]);
std::memcpy(m.data(), info.ptr, sizeof(double)*m.rows()*m.cols());
return m;
}), "initialize matrix from a numpy array");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_library(convolution SHARED
convolution.cpp matrices.cpp)
Loading

0 comments on commit 943bdf5

Please sign in to comment.