Skip to content

Commit

Permalink
Add backend shared lib build to cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
mwinter committed May 7, 2024
1 parent e87a6a5 commit 64f97a8
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 63 deletions.
4 changes: 2 additions & 2 deletions src/c/Cuda/CHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ float* createEllipsoidKernel(Vec<std::size_t> radii, Vec<std::size_t>& kernelDim
std::memset(kernel,0,sizeof(float)*kernelDims.product());

Vec<int> mid((kernelDims-1)/2);
Vec<float> dimScale = Vec<float>(1,1,1) / Vec<float>(radii.pwr(2));
Vec<float> dimScale = Vec<float>(1.0f) / Vec<float>(radii.pwr(2));

Vec<int> cur(0,0,0);
Vec<int> cur(0);
for (cur.z=0; cur.z<kernelDims.z; ++cur.z)
{
for (cur.y=0; cur.y<kernelDims.y; ++cur.y)
Expand Down
26 changes: 17 additions & 9 deletions src/c/Cuda/CHelpers.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
#pragma once

#include "Vec.h"
#include <memory.h>
#include <complex>
#ifdef _WIN32
#define DLL_EXPORT_API __declspec(dllexport)
#define DLL_IMPORT_API __declspec(dllimport)
#else
#define DLL_EXPORT_API __attribute__((__visibility("default")))
#define DLL_IMPORT_API
#endif

#ifdef IMAGE_PROCESSOR_DLL
#ifdef IMAGE_PROCESSOR_INTERNAL
#define IMAGE_PROCESSOR_API __declspec(dllexport)
#else
#define IMAGE_PROCESSOR_API __declspec(dllimport)
#endif // IMAGE_PROCESSOR_INTERNAL
#ifdef IMAGE_PROCESSOR_EXPORT
#define IMAGE_PROCESSOR_API DLL_EXPORT_API
#else
#define IMAGE_PROCESSOR_API DLL_IMPORT_API
#endif // IMAGE_PROCESSOR_EXPORT
#else
#define IMAGE_PROCESSOR_API
#define IMAGE_PROCESSOR_API
#endif // IMAGE_PROCESSOR_DLL

#include "Vec.h"
#include <memory.h>
#include <complex>

IMAGE_PROCESSOR_API float* createEllipsoidKernel(Vec<std::size_t> radii, Vec<std::size_t>& kernelDims);

IMAGE_PROCESSOR_API int calcOtsuThreshold(const double* normHistogram, int numBins);
114 changes: 104 additions & 10 deletions src/c/Cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Setup backend Hydra CUDA library (static) for cuda building
add_library(HydraCudaLib STATIC "")
add_library(HydraCudaStatic STATIC "")

target_compile_features(HydraCudaLib PRIVATE cxx_std_11)
target_link_libraries(HydraCudaLib PRIVATE OpenMP::OpenMP_CXX)
set_property(TARGET HydraCudaLib PROPERTY CUDA_STANDARD 11)
target_compile_features(HydraCudaStatic PRIVATE cxx_std_11)
target_link_libraries(HydraCudaStatic PRIVATE OpenMP::OpenMP_CXX)

# Set a variable to turn on/off PROCESS_MUTEX support
set(USE_PROCESS_MUTEX ON CACHE BOOL "Use process-level mutex to guard GPU calls")
Expand All @@ -18,15 +17,16 @@ set(USE_PROCESS_MUTEX ON CACHE BOOL "Use process-level mutex to guard GPU calls"
# string(APPEND CMAKE_CUDA_FLAGS " -gencode=arch=compute_61,code=sm_61")
# string(APPEND CMAKE_CUDA_FLAGS " -gencode=arch=compute_70,code=compute_70")

set_target_properties(HydraCudaLib PROPERTIES CUDA_ARCHITECTURES "52;60;61;70")

set_target_properties(HydraCudaLib
set_target_properties(HydraCudaStatic
PROPERTIES
POSITION_INDEPENDENT_CODE ON
CUDA_STANDARD 11
CUDA_SEPARABLE_COMPILATION ON
CUDA_RESOLVE_DEVICE_SYMBOLS ON)
CUDA_RESOLVE_DEVICE_SYMBOLS ON
CUDA_ARCHITECTURES "52;60;61;70"
)

target_sources(HydraCudaLib
target_sources(HydraCudaStatic
PUBLIC
BufferConversions.h
CHelpers.h
Expand Down Expand Up @@ -89,7 +89,101 @@ target_sources(HydraCudaLib
KernelIterator.cu
)

target_include_directories(HydraCudaLib
target_include_directories(HydraCudaStatic
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/..>
PRIVATE
${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}
)

# Backend shared lib for dynamic c++ linking
add_library(HydraCuda SHARED "")

target_compile_features(HydraCuda PRIVATE cxx_std_11)
target_link_libraries(HydraCuda
PRIVATE
OpenMP::OpenMP_CXX
)

# Set a variable to turn on/off PROCESS_MUTEX support
# set(USE_PROCESS_MUTEX ON CACHE BOOL "Use process-level mutex to guard GPU calls")
set_target_properties(HydraCuda
PROPERTIES
CUDA_STANDARD 11
CUDA_ARCHITECTURES "52;60;61;70"
CUDA_SEPARABLE_COMPILATION ON
CUDA_RESOLVE_DEVICE_SYMBOLS ON
CXX_VISIBILITY_PRESET hidden
POSITION_INDEPENDENT_CODE ON
LIBRARY_OUTPUT_DIRECTORY $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
LIBRARY_OUTPUT_DIRECTORY $<INSTALL_INTERFACE:lib>
)

target_sources(HydraCuda
PUBLIC
BufferConversions.h
CHelpers.h
CommandLineProgress.h
CudaDeviceInfo.h
CudaDeviceStats.h
CudaUtilities.h
CWrapperAutogen.h
CWrappers.h
Defines.h
ImageChunk.h
ImageView.h
KernelGenerators.h
Vec.h
PRIVATE
CudaAddTwoImages.cuh
CudaClosure.cuh
CudaDeviceCount.cuh
CudaDeviceImages.cuh
CudaElementWiseDifference.cuh
CudaEntropyFilter.cuh
CudaGaussian.cuh
CudaGetMinMax.cuh
CudaHighPassFilter.cuh
CudaIdentityFilter.cuh
CudaImageContainer.cuh
CudaImageContainerClean.cuh
CudaLoG.cuh
CudaMaxFilter.cuh
CudaMeanAndVariance.cuh
CudaMeanFilter.cuh
CudaMedianFilter.cuh
CudaMemoryStats.cuh
CudaMinFilter.cuh
CudaMinMax.cuh
CudaMultiplySum.cuh
CudaNLMeans.cuh
CudaOpener.cuh
CudaStdFilter.cuh
CudaSum.cuh
CudaVarFilter.cuh
CudaWienerFilter.cuh
ImageDimensions.cuh
Kernel.cuh
KernelIterator.cuh
SeparableMultiplySum.cuh

CHelpers.cpp
CudaDeviceInfo.cpp
CudaDeviceStats.cpp
CudaUtilities.cpp
GaussianKernel.cpp
ImageChunk.cpp
LoGKernel.cpp

CWrapperAutogen.cu
CWrappers.cu
Kernel.cu
KernelIterator.cu
)

target_include_directories(HydraCuda
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>
Expand Down
88 changes: 48 additions & 40 deletions src/c/Cuda/CWrappers.h
Original file line number Diff line number Diff line change
@@ -1,40 +1,48 @@
#pragma once
#include "Vec.h"
#include "CudaDeviceStats.h"
#include "ImageView.h"

#include <limits>
#include <algorithm>


#ifdef IMAGE_PROCESSOR_DLL
#ifdef IMAGE_PROCESSOR_INTERNAL
#define IMAGE_PROCESSOR_API __declspec(dllexport)
#else
#define IMAGE_PROCESSOR_API __declspec(dllimport)
#endif // IMAGE_PROCESSOR_INTERNAL
#else
#define IMAGE_PROCESSOR_API
#endif // IMAGE_PROCESSOR_DLL


#include "CWrapperAutogen.h"


IMAGE_PROCESSOR_API void clearDevice();

IMAGE_PROCESSOR_API int deviceCount();
IMAGE_PROCESSOR_API int deviceStats(DevStats** stats);
IMAGE_PROCESSOR_API int memoryStats(std::size_t** stats);


/// Example wrapper header calls
//IMAGE_PROCESSOR_API void fooFilter(const ImageView<bool> imageIn, ImageView<bool> imageOut, ImageView<float> kernel, int numIterations = 1, int device = -1);
//IMAGE_PROCESSOR_API void fooFilter(const ImageView<char> imageIn, ImageView<char> imageOut, ImageView<float> kernel, int numIterations = 1, int device = -1);
//IMAGE_PROCESSOR_API void fooFilter(const ImageView<unsigned char> imageIn, ImageView<unsigned char> imageOut, ImageView<float> kernel, int numIterations = 1, int device = -1);
//IMAGE_PROCESSOR_API void fooFilter(const ImageView<short> imageIn, ImageView<short> imageOut, ImageView<float> kernel, int numIterations = 1, int device = -1);
//IMAGE_PROCESSOR_API void fooFilter(const ImageView<unsigned short> imageIn, ImageView<unsigned short> imageOut, ImageView<float> kernel, int numIterations = 1, int device = -1);
//IMAGE_PROCESSOR_API void fooFilter(const ImageView<int> imageIn, ImageView<int> imageOut, ImageView<float> kernel, int numIterations = 1, int device = -1);
//IMAGE_PROCESSOR_API void fooFilter(const ImageView<unsigned int> imageIn, ImageView<unsigned int> imageOut, ImageView<float> kernel, int numIterations = 1, int device = -1);
//IMAGE_PROCESSOR_API void fooFilter(const ImageView<float> imageIn, ImageView<float> imageOut, ImageView<float> kernel, int numIterations = 1, int device = -1);
//IMAGE_PROCESSOR_API void fooFilter(const ImageView<double> imageIn, ImageView<double> imageOut, ImageView<float> kernel, int numIterations = 1, int device = -1);
#pragma once

#ifdef _WIN32
#define DLL_EXPORT_API __declspec(dllexport)
#define DLL_IMPORT_API __declspec(dllimport)
#else
#define DLL_EXPORT_API __attribute__((visibility("default")))
#define DLL_IMPORT_API
#endif

#ifdef IMAGE_PROCESSOR_DLL
#ifdef IMAGE_PROCESSOR_EXPORT
#define IMAGE_PROCESSOR_API DLL_EXPORT_API
#else
#define IMAGE_PROCESSOR_API DLL_IMPORT_API
#endif // IMAGE_PROCESSOR_EXPORT
#else
#define IMAGE_PROCESSOR_API
#endif // IMAGE_PROCESSOR_DLL


#include <limits>
#include <algorithm>

#include "Vec.h"
#include "ImageView.h"
#include "CudaDeviceStats.h"

#include "CWrapperAutogen.h"


IMAGE_PROCESSOR_API void clearDevice();

IMAGE_PROCESSOR_API int deviceCount();
IMAGE_PROCESSOR_API int deviceStats(DevStats** stats);
IMAGE_PROCESSOR_API int memoryStats(std::size_t** stats);


/// Example wrapper header calls
//IMAGE_PROCESSOR_API void fooFilter(const ImageView<bool> imageIn, ImageView<bool> imageOut, ImageView<float> kernel, int numIterations = 1, int device = -1);
//IMAGE_PROCESSOR_API void fooFilter(const ImageView<char> imageIn, ImageView<char> imageOut, ImageView<float> kernel, int numIterations = 1, int device = -1);
//IMAGE_PROCESSOR_API void fooFilter(const ImageView<unsigned char> imageIn, ImageView<unsigned char> imageOut, ImageView<float> kernel, int numIterations = 1, int device = -1);
//IMAGE_PROCESSOR_API void fooFilter(const ImageView<short> imageIn, ImageView<short> imageOut, ImageView<float> kernel, int numIterations = 1, int device = -1);
//IMAGE_PROCESSOR_API void fooFilter(const ImageView<unsigned short> imageIn, ImageView<unsigned short> imageOut, ImageView<float> kernel, int numIterations = 1, int device = -1);
//IMAGE_PROCESSOR_API void fooFilter(const ImageView<int> imageIn, ImageView<int> imageOut, ImageView<float> kernel, int numIterations = 1, int device = -1);
//IMAGE_PROCESSOR_API void fooFilter(const ImageView<unsigned int> imageIn, ImageView<unsigned int> imageOut, ImageView<float> kernel, int numIterations = 1, int device = -1);
//IMAGE_PROCESSOR_API void fooFilter(const ImageView<float> imageIn, ImageView<float> imageOut, ImageView<float> kernel, int numIterations = 1, int device = -1);
//IMAGE_PROCESSOR_API void fooFilter(const ImageView<double> imageIn, ImageView<double> imageOut, ImageView<float> kernel, int numIterations = 1, int device = -1);
2 changes: 1 addition & 1 deletion src/c/Mex/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if ( USE_PROCESS_MUTEX )
endif()

# Link against Matlab libraries
target_link_libraries(HydraMex PRIVATE HydraCudaLib ${Matlab_MX_LIBRARY} ${Matlab_MEX_LIBRARY})
target_link_libraries(HydraMex PRIVATE HydraCudaStatic ${Matlab_MX_LIBRARY} ${Matlab_MEX_LIBRARY})

# Change output library name to <ModuleName>.mex<plaform>
set_target_properties(HydraMex
Expand Down
2 changes: 1 addition & 1 deletion src/c/Python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if ( USE_PROCESS_MUTEX )
endif()

# Link against Python and NumPy libraries
target_link_libraries(HydraPy PRIVATE HydraCudaLib Python::Python Python::NumPy)
target_link_libraries(HydraPy PRIVATE HydraCudaStatic Python::Python Python::NumPy)

# Change output library name to Hydra.<dll/so/dylib>
set_target_properties(HydraPy
Expand Down

0 comments on commit 64f97a8

Please sign in to comment.