-
Notifications
You must be signed in to change notification settings - Fork 3
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
6 changed files
with
129 additions
and
4 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,25 @@ | ||
project(example_pybind11) | ||
file(GLOB SRC LIST_DIRECTORIES false "*.cu") | ||
|
||
if(CUDAToolkit_FOUND) | ||
include_directories(${CUDAToolkit_INCLUDE_DIRS}) | ||
endif() | ||
python_add_library(${PROJECT_NAME} MODULE WITH_SOABI ${SRC}) | ||
target_include_directories(${PROJECT_NAME} PUBLIC | ||
"$<BUILD_INTERFACE:${${CMAKE_PROJECT_NAME}_INCLUDE_DIRS}>" | ||
"$<INSTALL_INTERFACE:${CMAKE_PROJECT_NAME}/include>") | ||
if(CUDAToolkit_FOUND) | ||
target_link_libraries(${PROJECT_NAME} PRIVATE pybind11::headers CUDA::cudart_static) | ||
else() | ||
set_source_files_properties(${SRC} PROPERTIES LANGUAGE CXX) | ||
target_link_libraries(${PROJECT_NAME} PRIVATE pybind11::headers) | ||
endif() | ||
|
||
set_target_properties(${PROJECT_NAME} PROPERTIES | ||
CXX_STANDARD 11 | ||
VERSION ${CMAKE_PROJECT_VERSION} SOVERSION ${CMAKE_PROJECT_VERSION_MAJOR} | ||
INTERFACE_${PROJECT_NAME}_MAJOR_VERSION ${CMAKE_PROJECT_VERSION_MAJOR}) | ||
set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY COMPATIBLE_INTERFACE_STRING ${PROJECT_NAME}_MAJOR_VERSION) | ||
install(TARGETS ${PROJECT_NAME} | ||
INCLUDES DESTINATION ${CMAKE_PROJECT_NAME}/include | ||
LIBRARY DESTINATION ${CMAKE_PROJECT_NAME}) |
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,72 @@ | ||
/** | ||
* Example external pybind11 extension module using CuVec. | ||
* | ||
* Copyright (2021) Casper da Costa-Luis | ||
*/ | ||
#include "cuvec_pybind11.cuh" // NDCuVec | ||
#include <pybind11/pybind11.h> // pybind11, PYBIND11_MODULE | ||
#include <stdexcept> // std::length_error | ||
#ifdef CUVEC_DISABLE_CUDA | ||
#include <chrono> // std::chrono | ||
#else | ||
/// dst = src + 1 | ||
__global__ void _d_incr(float *dst, float *src, int X, int Y) { | ||
int x = threadIdx.x + blockDim.x * blockIdx.x; | ||
if (x >= X) return; | ||
int y = threadIdx.y + blockDim.y * blockIdx.y; | ||
if (y >= Y) return; | ||
dst[y * X + x] = src[y * X + x] + 1; | ||
} | ||
#endif // CUVEC_DISABLE_CUDA | ||
NDCuVec<float> *increment2d_f(NDCuVec<float> &src, NDCuVec<float> *output, bool timing) { | ||
auto &N = src.shape; | ||
if (N.size() != 2) throw std::length_error("`src` must be 2D"); | ||
|
||
#ifndef CUVEC_DISABLE_CUDA | ||
cudaEvent_t eStart, eAlloc, eKern; | ||
cudaEventCreate(&eStart); | ||
cudaEventCreate(&eAlloc); | ||
cudaEventCreate(&eKern); | ||
cudaEventRecord(eStart); | ||
#else | ||
auto eStart = std::chrono::steady_clock::now(); | ||
#endif | ||
|
||
if (!output) | ||
output = new NDCuVec<float>(N); | ||
else if (N != output->shape) | ||
throw std::length_error("`output` must be same shape as `src`"); | ||
|
||
#ifndef CUVEC_DISABLE_CUDA | ||
cudaEventRecord(eAlloc); | ||
dim3 thrds((N[1] + 31) / 32, (N[0] + 31) / 32); | ||
dim3 blcks(32, 32); | ||
_d_incr<<<thrds, blcks>>>(output->vec.data(), src.vec.data(), N[1], N[0]); | ||
cuvec::HandleError(cudaGetLastError(), __FILE__, __LINE__); | ||
// cudaDeviceSynchronize(); | ||
cudaEventRecord(eKern); | ||
cudaEventSynchronize(eKern); | ||
float alloc_ms, kernel_ms; | ||
cudaEventElapsedTime(&alloc_ms, eStart, eAlloc); | ||
cudaEventElapsedTime(&kernel_ms, eAlloc, eKern); | ||
// fprintf(stderr, "%.3f ms, %.3f ms\n", alloc_ms, kernel_ms); | ||
#else | ||
auto eAlloc = std::chrono::steady_clock::now(); | ||
for (size_t i = 0; i < src.vec.size(); i++) output->vec[i] = src.vec[i] + 1; | ||
auto eKern = std::chrono::steady_clock::now(); | ||
double alloc_ms = std::chrono::duration<double, std::milli>(eAlloc - eStart).count(); | ||
double kernel_ms = std::chrono::duration<double, std::milli>(eKern - eAlloc).count(); | ||
// fprintf(stderr, "%.3lf ms, %.3lf ms\n", alloc_ms, kernel_ms); | ||
#endif | ||
if (timing) { | ||
// hack: store times in first two elements of output | ||
output->vec[0] = alloc_ms; | ||
output->vec[1] = kernel_ms; | ||
} | ||
return output; | ||
} | ||
|
||
using namespace pybind11::literals; // _a | ||
PYBIND11_MODULE(example_pybind11, m) { | ||
m.def("increment2d_f", &increment2d_f, "src"_a, "output"_a = nullptr, "timing"_a = false); | ||
} |
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