tilework-cuda 0.18.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ # mkmf does not support nvcc, so the Makefile is written directly.
4
+ #
5
+ # 1. CUDA Toolkit present -> link bridge.cpp with c++. cuBLAS / cuRAND /
6
+ # cuFFT / cuDNN are each optional: a library is linked (and
7
+ # TILEWORK_HAS_<LIB> defined) only when both its header and its
8
+ # shared library are found, so a Toolkit without cuDNN — the common
9
+ # case, since cuDNN is a separate download — still builds, and Ruby
10
+ # falls back for whatever is missing.
11
+ # 2. absent -> a no-op Makefile (so `bundle install` does not
12
+ # fail without CUDA; core's backend= turns the missing extension into a
13
+ # Tilework::DeviceError)
14
+ #
15
+ # The product goes to lib/tilework/cuda/bridge.<dlext> so it cannot collide
16
+ # with other backends.
17
+
18
+ require "rbconfig"
19
+
20
+ hdrdir = RbConfig::CONFIG["rubyhdrdir"]
21
+ archhdrdir = RbConfig::CONFIG["rubyarchhdrdir"]
22
+ dlext = RbConfig::CONFIG["DLEXT"] # linux: "so"
23
+ cc = RbConfig::CONFIG["CC"]
24
+ cxx = RbConfig::CONFIG["CXX"] || "c++"
25
+ srcdir = File.expand_path(__dir__)
26
+
27
+ cuda_home = ENV["CUDA_HOME"] || "/usr/local/cuda"
28
+ nvcc = "#{cuda_home}/bin/nvcc"
29
+
30
+ if File.exist?(nvcc)
31
+ # libcuda.so (Driver API) ships with the driver, not the CUDA Toolkit, so it
32
+ # may be missing from $CUDA_HOME/lib64 — typically on Jetson (aarch64), where
33
+ # it lives in /usr/lib/aarch64-linux-gnu. Only existing dirs are added to -L.
34
+ # /usr/lib64 is where Fedora/RHEL packages (cuDNN included) install.
35
+ lib_dirs = [
36
+ "#{cuda_home}/lib64",
37
+ "#{cuda_home}/lib64/stubs",
38
+ "/usr/lib/#{RbConfig::CONFIG["host_cpu"]}-linux-gnu",
39
+ "/usr/lib64",
40
+ ].select { |d| Dir.exist?(d) }
41
+ ldflags = lib_dirs.map { |d| "-L#{d}" }.join(" ")
42
+ include_dirs = ["#{cuda_home}/include", "/usr/include"]
43
+
44
+ # header, -l name, macro
45
+ optional_libs = {
46
+ "cuBLAS" => ["cublas_v2.h", "cublas", "TILEWORK_HAS_CUBLAS"],
47
+ "cuRAND" => ["curand.h", "curand", "TILEWORK_HAS_CURAND"],
48
+ "cuFFT" => ["cufft.h", "cufft", "TILEWORK_HAS_CUFFT"],
49
+ "cuDNN" => ["cudnn.h", "cudnn", "TILEWORK_HAS_CUDNN"],
50
+ }
51
+ found = optional_libs.select do |_, (header, lib, _)|
52
+ include_dirs.any? { |d| File.file?("#{d}/#{header}") } &&
53
+ lib_dirs.any? { |d| File.exist?("#{d}/lib#{lib}.so") }
54
+ end
55
+ lib_cxxflags = found.values.map { |(_, _, macro)| " -D#{macro}" }.join
56
+ lib_ldlibs = found.values.map { |(_, lib, _)| " -l#{lib}" }.join
57
+
58
+ # bridge.cpp is pure host code with no __global__ (NVRTC compiles kernels at
59
+ # run time), so it builds with the host C++ compiler rather than nvcc. nvcc
60
+ # would only inject fatbin registration code and a libcudart dependency.
61
+ # nvcc's presence is used only to detect whether the CUDA Toolkit is installed.
62
+ makefile = <<~MAKEFILE
63
+ TARGET = bridge.#{dlext}
64
+ CC = #{cc}
65
+ CXX = #{cxx}
66
+ CFLAGS = -O2 -Wall -fPIC -DTILEWORK_BRIDGE_MODULE=\\\"CUDABridge\\\" -I#{hdrdir} -I#{archhdrdir}
67
+ CXXFLAGS = -O2 -Wall -fPIC -pthread#{lib_cxxflags} -I#{cuda_home}/include -I/usr/include
68
+ LDLIBS = #{ldflags} -lcuda -lnvrtc#{lib_ldlibs} -pthread
69
+
70
+ SRCDIR = #{srcdir}
71
+
72
+ all: $(TARGET)
73
+
74
+ gpu_bridge.o: $(SRCDIR)/gpu_bridge.c
75
+ \t$(CC) $(CFLAGS) -c $(SRCDIR)/gpu_bridge.c -o $@
76
+
77
+ bridge_cuda.o: $(SRCDIR)/bridge.cpp
78
+ \t$(CXX) $(CXXFLAGS) -c $(SRCDIR)/bridge.cpp -o $@
79
+
80
+ # Ruby symbols resolve when the extension loads, so they may stay undefined
81
+ # here (Linux shared objects allow that by default).
82
+ $(TARGET): gpu_bridge.o bridge_cuda.o
83
+ \t$(CXX) -shared gpu_bridge.o bridge_cuda.o $(LDLIBS) -o $@
84
+
85
+ clean:
86
+ \trm -f $(TARGET) gpu_bridge.o bridge_cuda.o
87
+
88
+ # gem install calls install with sitearchdir overridden. Install to the
89
+ # namespaced path rather than a flat one so require
90
+ # "tilework/cuda/bridge" resolves.
91
+ sitearchdir = .
92
+ install: $(TARGET)
93
+ \tmkdir -p $(sitearchdir)/tilework/cuda
94
+ \tcp $(TARGET) $(sitearchdir)/tilework/cuda/$(TARGET)
95
+
96
+ .PHONY: all clean install
97
+ MAKEFILE
98
+
99
+ File.write("Makefile", makefile)
100
+ status = optional_libs.keys.map { |name| "#{name} #{found.key?(name) ? "enabled" : "not found"}" }
101
+ puts "created Makefile (CUDA backend, #{cuda_home}; #{status.join(", ")})"
102
+ else
103
+ File.write("Makefile", "all:\ninstall:\nclean:\n.PHONY: all install clean\n")
104
+ puts "skipping tilework/cuda build (no CUDA toolchain found; set CUDA_HOME if it's installed elsewhere)"
105
+ end