-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from gjbex/development
Add notebook on Julia Set and numba
- Loading branch information
Showing
116 changed files
with
1,125 additions
and
237 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
source-code/interfaciing-c-c++-fortran/Pybind11/Stats/CMakeLists.txt
This file was deleted.
Oops, something went wrong.
47 changes: 0 additions & 47 deletions
47
source-code/interfaciing-c-c++-fortran/Pybind11/Stats/src/statistics/statistics.h
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
source-code/interfaciing-c-c++-fortran/Pybind11/Stats/test.py
This file was deleted.
Oops, something went wrong.
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions
10
source-code/interfacing-c-c++-fortran/Pybind11/Convolution/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
31 changes: 31 additions & 0 deletions
31
source-code/interfacing-c-c++-fortran/Pybind11/Convolution/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
16 changes: 16 additions & 0 deletions
16
source-code/interfacing-c-c++-fortran/Pybind11/Convolution/src/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
57 changes: 57 additions & 0 deletions
57
source-code/interfacing-c-c++-fortran/Pybind11/Convolution/src/benchmark_convolution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
9 changes: 9 additions & 0 deletions
9
source-code/interfacing-c-c++-fortran/Pybind11/Convolution/src/bindings/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
38 changes: 38 additions & 0 deletions
38
source-code/interfacing-c-c++-fortran/Pybind11/Convolution/src/bindings/convolve_bind.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
31 changes: 31 additions & 0 deletions
31
source-code/interfacing-c-c++-fortran/Pybind11/Convolution/src/bindings/matrix_bind.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
2 changes: 2 additions & 0 deletions
2
source-code/interfacing-c-c++-fortran/Pybind11/Convolution/src/convolution/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
add_library(convolution SHARED | ||
convolution.cpp matrices.cpp) |
Oops, something went wrong.