Skip to content

Commit

Permalink
Fix #31 - Detect NVCC and CUDA usage
Browse files Browse the repository at this point in the history
  • Loading branch information
jfalcou committed Mar 5, 2024
1 parent b176049 commit cf54075
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 7 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,34 @@ jobs:
- name: Running Unit Tests
run: cd build && ctest --output-on-failure -j 2

##################################################################################################
## NVCC targets
##################################################################################################
nvcc:
runs-on: ubuntu-latest
container:
image: nvidia/cuda:12.3.2-devel-ubi8
steps:
- name: Fetch current branch
uses: actions/checkout@v4.1.1
- name: Compiling Unit Tests
run: |
mkdir build && cd build
nvcc ../test/accelerator.cu -std=c++17 -I../include -o accelerator.device.exe
nvcc ../test/accelerator.cpp -std=c++17 -I../include -o accelerator.host.exe
nvcc ../test/arch.cpp -std=c++17 -I../include -o arch.exe
nvcc ../test/compiler.cpp -std=c++17 -I../include -o compiler.exe
nvcc ../test/data_model.cpp -std=c++17 -I../include -o data_model.exe
nvcc ../test/libc.cpp -std=c++17 -I../include -o libc.exe
nvcc ../test/os.cpp -std=c++17 -I../include -o os.exe
nvcc ../test/simd.cpp -std=c++17 -I../include -o simd.exe
nvcc ../test/stdlib.cpp -std=c++17 -I../include -o stdlib.exe
- name: Running Unit Tests
run: |
cd build
./accelerator.device.exe && ./accelerator.host.exe && ./arch.exe && ./compiler.exe && ./data_model.exe && \
./libc.exe && ./os.exe && ./simd.exe && ./stdlib.exe
##################################################################################################
## DPC++ Target
##################################################################################################
Expand Down
40 changes: 38 additions & 2 deletions include/spy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,38 @@ namespace spy::supports
{
return !(a==b);
}
template<int M, int N, int P> struct cuda_t
{
explicit constexpr operator bool() const noexcept { return M>0 && N>0; }
friend std::ostream& operator<<(std::ostream& os, cuda_t)
{
os << "NVCC CUDA v" << M << '.' << N;
if(P>0) os << '.' << P;
return os;
}
};
template<int M0, int N0, int P0, int M1, int N1, int P1>
constexpr inline bool operator==(cuda_t<M0,N0,P0> const&, cuda_t<M1,N1,P1> const&) noexcept
{
return M0==M1 && N0==N1 && P0==P1;
}
template<int M0, int N0, int P0, int M1, int N1, int P1>
constexpr inline bool operator!=(cuda_t<M0,N0,P0> const& a, cuda_t<M1,N1,P1> const& b) noexcept
{
return !(a==b);
}
#if defined(SYCL_LANGUAGE_VERSION) && defined (__INTEL_LLVM_COMPILER)
# define SPY_ACCELERATOR_SUPPORTS_SYCL
constexpr inline auto sycl = sycl_t<SYCL_LANGUAGE_VERSION/100, SYCL_LANGUAGE_VERSION%100, 0>{};
#else
constexpr inline auto sycl = sycl_t<-1,-1,-1>{};
#endif
#if defined(__CUDACC__)
# define SPY_ACCELERATOR_SUPPORTS_CUDA
constexpr inline auto cuda = cuda_t<__CUDACC_VER_MAJOR__, __CUDACC_VER_MINOR__, 0>{};
#else
constexpr inline auto cuda = cuda_t<-1,-1,-1>{};
#endif
}
#include <ostream>
namespace spy::detail
Expand Down Expand Up @@ -235,7 +261,7 @@ constexpr bool operator<=( TYPE<C2,M2,N2,P2> const& c2 ) const noexcept \
#endif
namespace spy::detail
{
enum class compilers { undefined_ = - 1, msvc_, intel_, clang_, gcc_, emscripten_, dpcpp_ };
enum class compilers { undefined_ = - 1, msvc_, intel_, clang_, gcc_, emscripten_, dpcpp_, nvcc_ };
template<compilers Compiler, int M, int N, int P> struct compilers_info
{
static constexpr compilers vendor = Compiler;
Expand All @@ -251,6 +277,7 @@ namespace spy::detail
template<compilers C, int M, int N, int P>
std::ostream& operator<<(std::ostream& os, compilers_info<C, M, N, P> const& c)
{
if(C == compilers::nvcc_ ) return os << "NVIDIA CUDA Compiler " << c.version;
if(C == compilers::msvc_ ) return os << "Microsoft Visual Studio " << c.version;
if(C == compilers::intel_) return os << "Intel(R) C++ Compiler " << c.version;
if(C == compilers::dpcpp_) return os << "Intel(R) oneAPI DPC++/C++ Compiler " << c.version;
Expand All @@ -262,13 +289,17 @@ namespace spy::detail
template<int M, int N, int P> using msvc_t = compilers_info<compilers::msvc_ ,M,N,P>;
template<int M, int N, int P> using intel_t = compilers_info<compilers::intel_,M,N,P>;
template<int M, int N, int P> using dpcpp_t = compilers_info<compilers::dpcpp_,M,N,P>;
template<int M, int N, int P> using nvcc_t = compilers_info<compilers::nvcc_ ,M,N,P>;
template<int M, int N, int P> using clang_t = compilers_info<compilers::clang_,M,N,P>;
template<int M, int N, int P> using gcc_t = compilers_info<compilers::gcc_ ,M,N,P>;
template<int M, int N, int P> using emscripten_t = compilers_info<compilers::emscripten_,M,N,P>;
}
namespace spy
{
#if defined(_MSC_VER)
#if defined(__NVCC__)
#define SPY_COMPILER_IS_NVCC
using compiler_type = detail::nvcc_t<__CUDACC_VER_MAJOR__, __CUDACC_VER_MINOR__, 0>;
#elif defined(_MSC_VER)
#define SPY_COMPILER_IS_MSVC
using compiler_type = detail::msvc_t<_MSC_VER / 100, _MSC_VER % 100, _MSC_FULL_VER % 100000>;
#elif defined(__INTEL_LLVM_COMPILER)
Expand Down Expand Up @@ -307,6 +338,7 @@ namespace spy::detail
}
namespace spy
{
constexpr inline auto nvcc_ = detail::nvcc_t<-1,0,0>{};
constexpr inline auto msvc_ = detail::msvc_t<-1,0,0>{};
constexpr inline auto intel_ = detail::intel_t<-1,0,0>{};
constexpr inline auto dpcpp_ = detail::dpcpp_t<-1,0,0>{};
Expand All @@ -316,6 +348,10 @@ namespace spy
}
namespace spy::literal
{
template<char ...c> constexpr auto operator"" _nvcc()
{
return detail::literal_wrap<detail::nvcc_t,c...>();
}
template<char ...c> constexpr auto operator"" _msvc()
{
return detail::literal_wrap<detail::msvc_t,c...>();
Expand Down
31 changes: 31 additions & 0 deletions src/spy/accelerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,41 @@ namespace spy::supports
return !(a==b);
}

template<int M, int N, int P> struct cuda_t
{
explicit constexpr operator bool() const noexcept { return M>0 && N>0; }

friend std::ostream& operator<<(std::ostream& os, cuda_t)
{
os << "NVCC CUDA v" << M << '.' << N;
if(P>0) os << '.' << P;
return os;
}
};

template<int M0, int N0, int P0, int M1, int N1, int P1>
constexpr inline bool operator==(cuda_t<M0,N0,P0> const&, cuda_t<M1,N1,P1> const&) noexcept
{
return M0==M1 && N0==N1 && P0==P1;
}

template<int M0, int N0, int P0, int M1, int N1, int P1>
constexpr inline bool operator!=(cuda_t<M0,N0,P0> const& a, cuda_t<M1,N1,P1> const& b) noexcept
{
return !(a==b);
}

#if defined(SYCL_LANGUAGE_VERSION) && defined (__INTEL_LLVM_COMPILER)
# define SPY_ACCELERATOR_SUPPORTS_SYCL
constexpr inline auto sycl = sycl_t<SYCL_LANGUAGE_VERSION/100, SYCL_LANGUAGE_VERSION%100, 0>{};
#else
constexpr inline auto sycl = sycl_t<-1,-1,-1>{};
#endif

#if defined(__CUDACC__)
# define SPY_ACCELERATOR_SUPPORTS_CUDA
constexpr inline auto cuda = cuda_t<__CUDACC_VER_MAJOR__, __CUDACC_VER_MINOR__, 0>{};
#else
constexpr inline auto cuda = cuda_t<-1,-1,-1>{};
#endif
}
15 changes: 13 additions & 2 deletions src/spy/compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace spy::detail
{
enum class compilers { undefined_ = - 1, msvc_, intel_, clang_, gcc_, emscripten_, dpcpp_ };
enum class compilers { undefined_ = - 1, msvc_, intel_, clang_, gcc_, emscripten_, dpcpp_, nvcc_ };

template<compilers Compiler, int M, int N, int P> struct compilers_info
{
Expand All @@ -38,6 +38,7 @@ namespace spy::detail
template<compilers C, int M, int N, int P>
std::ostream& operator<<(std::ostream& os, compilers_info<C, M, N, P> const& c)
{
if(C == compilers::nvcc_ ) return os << "NVIDIA CUDA Compiler " << c.version;
if(C == compilers::msvc_ ) return os << "Microsoft Visual Studio " << c.version;
if(C == compilers::intel_) return os << "Intel(R) C++ Compiler " << c.version;
if(C == compilers::dpcpp_) return os << "Intel(R) oneAPI DPC++/C++ Compiler " << c.version;
Expand All @@ -51,6 +52,7 @@ namespace spy::detail
template<int M, int N, int P> using msvc_t = compilers_info<compilers::msvc_ ,M,N,P>;
template<int M, int N, int P> using intel_t = compilers_info<compilers::intel_,M,N,P>;
template<int M, int N, int P> using dpcpp_t = compilers_info<compilers::dpcpp_,M,N,P>;
template<int M, int N, int P> using nvcc_t = compilers_info<compilers::nvcc_ ,M,N,P>;
template<int M, int N, int P> using clang_t = compilers_info<compilers::clang_,M,N,P>;
template<int M, int N, int P> using gcc_t = compilers_info<compilers::gcc_ ,M,N,P>;
template<int M, int N, int P> using emscripten_t = compilers_info<compilers::emscripten_,M,N,P>;
Expand All @@ -61,7 +63,10 @@ namespace spy
//================================================================================================
// Compiler detection object type
//================================================================================================
#if defined(_MSC_VER)
#if defined(__NVCC__)
#define SPY_COMPILER_IS_NVCC
using compiler_type = detail::nvcc_t<__CUDACC_VER_MAJOR__, __CUDACC_VER_MINOR__, 0>;
#elif defined(_MSC_VER)
#define SPY_COMPILER_IS_MSVC
using compiler_type = detail::msvc_t<_MSC_VER / 100, _MSC_VER % 100, _MSC_FULL_VER % 100000>;
#elif defined(__INTEL_LLVM_COMPILER)
Expand Down Expand Up @@ -109,6 +114,7 @@ namespace spy
//================================================================================================
// Compilers detector stand-alone instances
//================================================================================================
constexpr inline auto nvcc_ = detail::nvcc_t<-1,0,0>{};
constexpr inline auto msvc_ = detail::msvc_t<-1,0,0>{};
constexpr inline auto intel_ = detail::intel_t<-1,0,0>{};
constexpr inline auto dpcpp_ = detail::dpcpp_t<-1,0,0>{};
Expand All @@ -119,6 +125,11 @@ namespace spy

namespace spy::literal
{
template<char ...c> constexpr auto operator"" _nvcc()
{
return detail::literal_wrap<detail::nvcc_t,c...>();
}

template<char ...c> constexpr auto operator"" _msvc()
{
return detail::literal_wrap<detail::msvc_t,c...>();
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ add_dependencies(unit spy.unit)
##==================================================================================================
# All tests
##==================================================================================================
generate_test("spy" "accelerator.cpp")
generate_test("spy" "arch.cpp")
generate_test("spy" "compiler.cpp")
generate_test("spy" "data_model.cpp")
Expand Down
33 changes: 33 additions & 0 deletions test/accelerator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//==================================================================================================
/**
SPY - C++ Informations Broker
Copyright : SPY Project Contributors
SPDX-License-Identifier: BSL-1.0
**/
//==================================================================================================
#include <spy.hpp>
#include <iostream>

int main()
{
std::cout << "Check that specified accelerator is supported: " << std::endl;
{
#if defined(SYCL_LANGUAGE_VERSION) && defined (__INTEL_LLVM_COMPILER)
static_assert( spy::supports::sycl );
std::cout << "Currently compiling with " << spy::supports::sycl << " enabled\n";
#else
static_assert( !spy::supports::sycl );
std::cout << "Currently compiling without SYCL enabled\n";
#endif
}
{
#if defined(__NVCC__) && defined (__CUDACC__)
static_assert( spy::supports::cuda );
std::cout << "Currently compiling with " << spy::supports::cuda << " enabled\n";
#else
static_assert( !spy::supports::cuda );
std::cout << "Currently compiling without CUDA enabled\n";
#endif
}
std::cout << "Done." << std::endl;
}
33 changes: 33 additions & 0 deletions test/accelerator.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//==================================================================================================
/**
SPY - C++ Informations Broker
Copyright : SPY Project Contributors
SPDX-License-Identifier: BSL-1.0
**/
//==================================================================================================
#include <spy.hpp>
#include <iostream>

int main()
{
std::cout << "Check that specified accelerator is supported: " << std::endl;
{
#if defined(SYCL_LANGUAGE_VERSION) && defined (__INTEL_LLVM_COMPILER)
static_assert( spy::supports::sycl );
std::cout << "Currently compiling with " << spy::supports::sycl << " enabled\n";
#else
static_assert( !spy::supports::sycl );
std::cout << "Currently compiling without SYCL enabled\n";
#endif
}
{
#if defined(__NVCC__) && defined (__CUDACC__)
static_assert( spy::supports::cuda );
std::cout << "Currently compiling with " << spy::supports::cuda << " enabled\n";
#else
static_assert( !spy::supports::cuda );
std::cout << "Currently compiling without CUDA enabled\n";
#endif
}
std::cout << "Done." << std::endl;
}
Loading

0 comments on commit cf54075

Please sign in to comment.