Open chococandy63 opened 3 months ago
Tested another example,
using cppdef
:
import cppyy
import os
os.environ['CLING_ENABLE_CUDA'] = '1'
os.environ['CLING_CUDA_PATH'] = '/usr/local/cuda'
os.environ['CLING_CUDA_ARCH'] = 'sm_75'
cppyy.add_include_path('/usr/local/cuda/include')
cppyy.add_library_path('/usr/local/cuda/lib64')
cppyy.include('iostream')
cppyy.include("cuda.h")
cppyy.include("cuda_runtime.h")
cppyy.include("cmath")
cppyy.load_library("cudart")
# Define the CUDA kernel
cppyy.cppdef('''
extern "C" __global__ void foo_kernel(float *result, float a) {
*result = fabs(a);
}
''')
# Function to test loading the kernel
def test_load_kernel():
try:
# Attempt to get the function from the module
foo_kernel = cppyy.gbl.foo_kernel
print("Kernel loaded successfully:", foo_kernel)
except Exception as e:
print("Error loading kernel:", e)
# Test loading the kernel
test_load_kernel()
Output:
Kernel loaded successfully: <C++ overload "foo_kernel" at 0x7ff591f32780>
using cudadef
:
import cppyy
import os
os.environ['CLING_ENABLE_CUDA'] = '1'
os.environ['CLING_CUDA_PATH'] = '/usr/local/cuda'
os.environ['CLING_CUDA_ARCH'] = 'sm_75'
cppyy.add_include_path('/usr/local/cuda/include')
cppyy.add_library_path('/usr/local/cuda/lib64')
cppyy.include('iostream')
cppyy.include("cuda.h")
cppyy.include("cuda_runtime.h")
cppyy.include("cmath")
cppyy.load_library("cudart")
# Define the CUDA kernel
cppyy.cudadef('''
extern "C" __global__ void foo_kernel(float *result, float a) {
*result = fabs(a);
}
''')
# Function to test loading the kernel
def test_load_kernel():
try:
# Attempt to get the function from the module
foo_kernel = cppyy.gbl.foo_kernel
print("Kernel loaded successfully:", foo_kernel)
except Exception as e:
print("Error loading kernel:", e)
# Test loading the kernel
test_load_kernel()
Output:
Error loading kernel: <namespace cppyy.gbl at 0x15a0800> has no attribute 'foo_kernel'. Full details:
type object '' has no attribute 'foo_kernel'
'foo_kernel' is not a known C++ class
'foo_kernel' is not a known C++ template
'foo_kernel' is not a known C++ enum
Trying to document all the errors that I faced while working with the existing
cudadef
andcppdef
definitions. The definitions given below:This is the example I tried:
Output:
When I tried to print the macro
__CUDA__
insidecppdef
by reverting the#ifndef
in the definition ofcppdef
, it showedmacro undefined
. As we discussed on discord before, this attribute error means that the meta code isn't searching the CUDA enabled Cling for symbols. This needs to be fixed in order to work withcudadef
or Cling/JITed kernels.