File indexing completed on 2026-04-09 07:49:32
0001 #pragma once
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <cuda_runtime.h>
0011 #include <string>
0012 #include <sstream>
0013
0014
0015 struct SCU_Exception : public std::runtime_error
0016 {
0017 SCU_Exception( const char* msg ) : std::runtime_error( msg ) {}
0018 };
0019
0020 #define SCU_CHECK( call ) \
0021 do \
0022 { \
0023 cudaError_t error = call; \
0024 if( error != cudaSuccess ) \
0025 { \
0026 std::stringstream ss; \
0027 ss << "CUDA call (" << #call << " ) failed with error: '" \
0028 << cudaGetErrorString( error ) \
0029 << "' (" __FILE__ << ":" << __LINE__ << ")\n"; \
0030 throw SCU_Exception( ss.str().c_str() ); \
0031 } \
0032 } while( 0 )
0033
0034
0035
0036 struct SCU_
0037 {
0038 using ULL = unsigned long long ;
0039 static void _cudaMalloc( void** p2p, size_t size, const char* label );
0040
0041 template <typename T>
0042 static T* device_alloc( unsigned num_items, const char* label ) ;
0043
0044 template<typename T>
0045 static void copy_host_to_device( T* d, const T* h, unsigned num_items);
0046
0047 };
0048
0049
0050 inline void SCU_::_cudaMalloc( void** p2p, size_t size, const char* label )
0051 {
0052 cudaError_t err = cudaMalloc(p2p, size ) ;
0053 if( err != cudaSuccess )
0054 {
0055 std::stringstream ss;
0056 ss << "CUDA call (" << label << " ) failed with error: '"
0057 << cudaGetErrorString( err )
0058 << "' (" __FILE__ << ":" << __LINE__ << ")\n";
0059
0060 throw SCU_Exception( ss.str().c_str() );
0061 }
0062 }
0063
0064
0065 template<typename T>
0066 inline T* SCU_::device_alloc( unsigned num_items, const char* label )
0067 {
0068 size_t size = num_items*sizeof(T) ;
0069
0070 T* d ;
0071 _cudaMalloc( reinterpret_cast<void**>( &d ), size, label );
0072
0073 return d ;
0074 }
0075
0076
0077 template<typename T>
0078 inline void SCU_::copy_host_to_device( T* d, const T* h, unsigned num_items)
0079 {
0080 size_t size = num_items*sizeof(T) ;
0081 SCU_CHECK( cudaMemcpy(reinterpret_cast<void*>( d ), h , size, cudaMemcpyHostToDevice ));
0082 }
0083
0084