CUDA cuFFT compilation error and suggested approach #11411
-
Hello, I'm trying to use meson to compile a library that needs to be linked with a static library from CUDA, In short, I need to:
How would I do this with meson? The existing Makefile has the following: ifeq ($(HAVE_CUDA),1)
# TODO: Need to deal with 32/64 detection here
LIBCUFFT_STATIC = $(CUDA_LIBDIR64)/libcufft_static.a
# All PTX archs included in the lib (typically only one)
CUFFT_PTX_ARCHS := $(shell @CUOBJDUMP@ --list-ptx $(LIBCUFFT_STATIC) | grep -Po "sm_[0-9]{2}" | cut -d_ -f2 | sort | uniq)
# Latest PTX arch included in the lib
CUFFT_PTX_LATEST_ARCH := $(shell echo $(CUFFT_PTX_ARCHS) | rev | cut -d' ' -f1 | rev)
CUFFT_STATIC_GENCODE = -gencode arch=compute_$(CUFFT_PTX_LATEST_ARCH),\"code=compute_$(CUFFT_PTX_LATEST_ARCH)\"
libcufft_static_pruned.a: $(LIBCUFFT_STATIC) Makefile
# We prune out all archs except those to be included in libbifrost *and*
# the latest PTX arch included in libcufft_static.
# E.g., We may have GPU_ARCHS="35 61" but libcufft_static might only
# include sm_60 and compute_60, so we need to keep compute_60 in order
# to support sm_61.
@NVPRUNE@ -o $@ $(NVCC_GENCODE) $(CUFFT_STATIC_GENCODE) $<
fft_kernels.o: fft_kernels.cu fft_kernels.h Makefile
# Note: This needs to be compiled with "-dc" to make CUFFT callbacks work
$(NVCC) $(NVCCFLAGS) $(CPPFLAGS) -Xcompiler "$(GCCFLAGS)" -dc $(OUTPUT_OPTION) $<
_cuda_device_link.o: Makefile fft_kernels.o libcufft_static_pruned.a
@echo "Linking _cuda_device_link.o"
# TODO: "nvcc -dlink ..." does not error or warn when a -lblah is not found
@ls ./libcufft_static_pruned.a > /dev/null
$(NVCC) -dlink -o $@ $(NVCCFLAGS) fft_kernels.o -L. -lcufft_static_pruned
CUDA_DEVICE_LINK_OBJ = _cuda_device_link.o
else
CUDA_DEVICE_LINK_OBJ =
endif Which runs something like:
Then this
Any guidance would be most appreciated! So far my approach to create libcufft_static with meson is:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
PS: The code is called bifrost, my meson branch is at https://github.com/bf-plugins/bifrost/tree/meson-build |
Beta Was this translation helpful? Give feedback.
-
For starters, I think your run_command should probably be a custom_target. Note that you can specify a custom_target output as a
Is it harmful to specify it for all of them? Meson doesn't let you define attributes on individual object files, but you could define it in an object library such as
Meson will automatically define this flag if the buildtype is debug. Even if the project is configured with debug, you can force it to something else for this one target. Use
Pass the
|
Beta Was this translation helpful? Give feedback.
-
Here's my working approach using the suggestions from @eli-schwartz, in case other users need to do something similar: ... snip ...
if cuda_dep.found()
# Get path to cuda lib64 dir (might be nicer way?)
nvcc = find_program('nvcc')
cuda_lib64_path = nvcc.full_path().replace('/bin/nvcc', '/lib64')
# Create pruned static library for CUFFT
# TODO: recreate CUFFT_PTX_LATEST_ARCH from Makefile
# (instead of hardcoding to known value as I've done here)
arch_flags_pruned = cuda.nvcc_arch_flags('11', '8.0')
arch_flags_pruned += cuda_arch_flags
cufft_pruned = custom_target('libcufft_static_pruned',
input: cuda_lib64_path / 'libcufft_static.a',
output: 'libcufft_static_pruned.a',
command: [nvprune, '@INPUT@', arch_flags_pruned, '-o', '@OUTPUT@'],
install: true,
install_dir: meson.build_root()
)
# Now, compile cuFFT static library
cufft_static = static_library('fft_kernels',
['src/fft_kernels.cu', 'src/fft.cu'],
cuda_args: ['-dc'],
include_directories: ['src'],
override_options: ['buildtype=release'],
build_by_default: true).extract_all_objects(recursive: false)
# Next create _device_link.o
device_link_flags = ['-dlink',
'-Xcompiler', '"-march=native"',
'-Xcompiler', '-fPIC',
'-Xcompiler', '-DPIC',
'-L.', '-lcufft_static_pruned']
device_link_flags += cuda_arch_flags
cufft_device_link = custom_target('_device_link',
input: cufft_static,
output: '_device_link.o',
command: [nvcc, '@INPUT@', device_link_flags, '-o', '@OUTPUT@'],
install: true,
install_dir: meson.build_root()
)
endif
# Compile main library
# Note cufft_device_link is added as a source (not as an objects:)
bifrost_lib = library('bifrost', [srcs_cpp, srcs_cu, cufft_device_link],
include_directories: ['src'],
dependencies: [cuda_dep, found_deps],
install: true,
override_options: ['buildtype=release']
) |
Beta Was this translation helpful? Give feedback.
For starters, I think your run_command should probably be a custom_target. Note that you can specify a custom_target output as a
link_with:
value, specifically to handle cases like this where libraries are created using specialized rules that are too complicated to handle withlibrary()
.Is it harmful to specify it for all of them? Meson doesn't let you define attributes on individual object files, but you could define it in an object library such as
static_library('fft_kernels_stlib', 'fft_kernels.c', cuda_args: [...], build_by_default: false).…