-
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.
- Loading branch information
Showing
4 changed files
with
100 additions
and
27 deletions.
There are no files selected for viewing
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,7 @@ | ||
cmake_minimum_required(VERSION 3.5) | ||
project(example) | ||
|
||
find_package(pybind11 REQUIRED) | ||
include_directories(SYSTEM ${pybind11_INCLUDE_DIRS}) | ||
|
||
pybind11_add_module(matrix_ops matrix_multiplication.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,34 @@ | ||
import matrix_ops | ||
import time | ||
import numpy as np | ||
|
||
# Create random matrices. | ||
n = 500 | ||
mat1 = np.random.rand(n, n) | ||
mat2 = np.random.rand(n, n) | ||
|
||
# Benchmark C++ implementation (using pybind11). | ||
start_time = time.time() | ||
result_cpp = matrix_ops.matrix_multiply(mat1, mat2) | ||
cpp_duration = time.time() - start_time | ||
print(f"C++ implementation took {cpp_duration:.4f} seconds.") | ||
|
||
def matrix_multiply_python(mat1, mat2): | ||
rows = len(mat1) | ||
cols = len(mat2[0]) | ||
inner_dim = len(mat2) | ||
|
||
result = [[0 for _ in range(cols)] for _ in range(rows)] | ||
for i in range(rows): | ||
for j in range(cols): | ||
for k in range(inner_dim): | ||
result[i][j] += mat1[i][k] * mat2[k][j] | ||
return result | ||
|
||
# Benchmark Python implementation. | ||
start_time = time.time() | ||
result_python = matrix_multiply_python(mat1, mat2) | ||
python_duration = time.time() - start_time | ||
print(f"Python implementation took {python_duration:.4f} seconds.") | ||
|
||
print(f"Speedup: {python_duration // cpp_duration}.") |
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 <pybind11/numpy.h> | ||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
|
||
#include <vector> | ||
|
||
std::vector<std::vector<double>> | ||
matrix_multiply(const std::vector<std::vector<double>> &mat1, | ||
const std::vector<std::vector<double>> &mat2) { | ||
const size_t rows = mat1.size(); | ||
const size_t cols = mat2[0].size(); | ||
const size_t inner_dim = mat2.size(); | ||
|
||
std::vector<std::vector<double>> result(rows, std::vector<double>(cols, 0)); | ||
for (size_t i = 0; i < rows; ++i) { | ||
for (size_t j = 0; j < cols; ++j) { | ||
for (size_t k = 0; k < inner_dim; ++k) { | ||
result[i][j] += mat1[i][k] * mat2[k][j]; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
namespace py = pybind11; | ||
|
||
PYBIND11_MODULE(matrix_ops, m) { | ||
m.def("matrix_multiply", &matrix_multiply, | ||
"A function which multiplies two NumPy matrices."); | ||
} |