Skip to content

Conversation

@DavidLapous
Copy link
Contributor

Fixes cuda file detection for linux cuda users.
Without this patch, pykeops fails to find cuda.h and switches back to cpu only mode.

From conda-nvcc's activate.sh file, the relevent variables are the following:

#...

[[ "@cross_target_platform@" == "linux-64" ]] && targetsDir="targets/x86_64-linux"
[[ "@cross_target_platform@" == "linux-ppc64le" ]] && targetsDir="targets/ppc64le-linux"
[[ "@cross_target_platform@" == "linux-aarch64" ]] && targetsDir="targets/sbsa-linux"


CUDA_CFLAGS=""
CUDA_LDFLAGS=""
if [ "${CONDA_BUILD:-0}" = "1" ]; then
    CUDA_CFLAGS="${CUDA_CFLAGS} -I${PREFIX}/${targetsDir}/include"
    CUDA_CFLAGS="${CUDA_CFLAGS} -I${BUILD_PREFIX}/${targetsDir}/include"
    CUDA_LDFLAGS="${CUDA_LDFLAGS} -L${PREFIX}/${targetsDir}/lib"
    CUDA_LDFLAGS="${CUDA_LDFLAGS} -L${PREFIX}/${targetsDir}/lib/stubs"
    CUDA_LDFLAGS="${CUDA_LDFLAGS} -L${BUILD_PREFIX}/${targetsDir}/lib"
    CUDA_LDFLAGS="${CUDA_LDFLAGS} -L${BUILD_PREFIX}/${targetsDir}/lib/stubs"
    # Needed to fix cross compilation.
    # $PREFIX and $PREFIX/${targetsDir} are needed to properly find all host components
    # $BUILD_PREFIX/$HOST/sysroot is needed to find compiler bits and is placed before any `targetsDir` for priority.
    # $BUILD_PREFIX/${targetsDir} is needed for projects that don't enable the CUDA language but use FindCUDAToolkit
    export CMAKE_ARGS="${CMAKE_ARGS} -DCMAKE_FIND_ROOT_PATH=$PREFIX;$BUILD_PREFIX/$HOST/sysroot;$PREFIX/${targetsDir};$BUILD_PREFIX/${targetsDir}"
else
    CUDA_CFLAGS="${CUDA_CFLAGS} -I${CONDA_PREFIX}/${targetsDir}/include"
    CUDA_LDFLAGS="${CUDA_LDFLAGS} -L${CONDA_PREFIX}/${targetsDir}/lib"
    CUDA_LDFLAGS="${CUDA_LDFLAGS} -L${CONDA_PREFIX}/${targetsDir}/lib/stubs"
fi
export CFLAGS="${CFLAGS} ${CUDA_CFLAGS}"
export CPPFLAGS="${CPPFLAGS} ${CUDA_CFLAGS}"
export CXXFLAGS="${CXXFLAGS} ${CUDA_CFLAGS}"
export LDFLAGS="${LDFLAGS} ${CUDA_LDFLAGS}"

# ...

https://github.com/conda-forge/cuda-nvcc-feedstock/blob/e4330bbc991b31e6f3e775524667b806f5495dcf/recipe/activate.sh#L5-L31

So I just added the different possible targetDirs to the keopscore file detection pipeline, and it worked on my laptop.

@Nizben
Copy link
Collaborator

Nizben commented Apr 30, 2025

Thank you for the PR!
Yes indeed, you are right, i guess things were working for me because I was using cudatoolkit-dev instead of cudatoolkit, which needs the patch you added (as opposed to cudatoolkit-dev which automatically puts the cuda headers in the env/include path).
For instance this is my current cuda_include_path: /home/nizarben/miniconda3/envs/pykeops_dev/include in which I can find cuda.h, no need for adding/specifiying the arch in the path.
Thanks for pointing that out, will fix very soon!

@DavidLapous
Copy link
Contributor Author

Ah yes, I'm not using cudatoolkit-dev indeed, thanks for the info.
TBH I just noticed this now since the upgrade from fedora 41 to 42 killed cuda on my system (but the conda version seems to work fine with keops indeed).
I also see a link to lib/stubs that I didn't see in your cuda parser (only the lib part) ; it works fine on my system / code, but this may be problematic in another situation.

@Nizben
Copy link
Collaborator

Nizben commented Apr 30, 2025

Can you give me more context on the 'lib/stubs' please? Which cuda parser are you talking about? What situation could it be problematic in?
Thanks

@DavidLapous
Copy link
Contributor Author

Sorry this wasn't very clear ; I was talking about this specific line in the conda recipe that I shared in the first message

    CUDA_LDFLAGS="${CUDA_LDFLAGS} -L${CONDA_PREFIX}/${targetsDir}/lib/stubs"

which contains "stubs" that I didn't check

and the cuda parser I was thinking about are lines similar to :

self.nvrtc_flags = (
compile_options
+ f" -fpermissive -L{libcuda_folder} -L{libnvrtc_folder} -lcuda -lnvrtc"
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants