Skip to content
Open

Work #12

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ef3093f
Fix build for newer CUDA and conda-installed dependencies
LourensVeen Oct 30, 2023
194bead
Redo build system
LourensVeen Oct 30, 2023
18342fc
Make tests compile with the new build system and conda
LourensVeen Oct 30, 2023
ad2b2f2
Avoid clash with EasyBuild CUDA INCLUDEPATH variable
LourensVeen Oct 31, 2023
a7134e7
Keep the .ptx files, they're actually the ones that get used
LourensVeen Oct 31, 2023
cafc823
Fix symlinking error on repeated builds
LourensVeen Oct 31, 2023
ecabda6
Improve README a bit
LourensVeen Oct 31, 2023
5fde0ac
Fix error when building for newer GPU architectures
LourensVeen Oct 31, 2023
3347293
Add .gitignore for the test directory
LourensVeen Oct 31, 2023
9d9ea58
Replace old build system with new build system
LourensVeen Oct 31, 2023
54cae9f
Update build instructions in the README
LourensVeen Oct 31, 2023
feeb564
Make dependency of interfaces on libsapporo explicit
LourensVeen Nov 1, 2023
b3d4951
Enable make clean even if there's no CUDA or OpenGL available
LourensVeen Nov 1, 2023
674292c
Include kernels also when compiling with CUDA
LourensVeen Nov 1, 2023
19ac0b9
Make .gitignore only match the executables, not the source in the subdir
LourensVeen Nov 2, 2023
12e8f13
Also update the OpenCL test makefile
LourensVeen Nov 2, 2023
1bd149e
Rearrange directory structure
LourensVeen Nov 2, 2023
cc92791
Rename license file so that GitHub will pick it up
LourensVeen Nov 2, 2023
6c3f8fb
Add public API headers for G6 and 6th order
LourensVeen Nov 2, 2023
644c727
Add make install target
LourensVeen Nov 6, 2023
d4ff577
Improve CUDA builds
LourensVeen Nov 15, 2023
0730f7a
Add initial Conda build definition
LourensVeen Nov 15, 2023
6064eb5
Use CXX environment variable in OpenCL detection
LourensVeen Nov 17, 2023
6ef3114
Fix conda OpenCL dependencies
LourensVeen Nov 21, 2023
b615d28
Add build number to the conda build string
LourensVeen Nov 21, 2023
025dea9
Fix conda dso whitelist
LourensVeen Nov 21, 2023
512c2dd
Fix kernel names after directory rearrangement
LourensVeen Nov 21, 2023
0476d61
Fix OpenCL test link and clean up small things
LourensVeen Nov 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
*.o
*.a
*.so
*~
*.ptx
*.ptxh
*.cle
*.clh
File renamed without changes.
213 changes: 213 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
CXX ?= g++
CC ?= gcc
PREFIX ?= /usr/local


.PHONY: all
all: libsapporo.a libsapporo.so emulated_interfaces


# Detect CUDA
ifndef CUDA_TK
NVCC := $(shell which nvcc || echo NOTFOUND)
ifeq ($(NVCC), NOTFOUND)
$(info The nvcc command is not available in your shell.)
$(info To compile with CUDA, please install it, set up your environment)
$(info according to the CUDA installation instructions, and try again.)
$(info )
else
CUDA_TK := $(dir $(NVCC))..
CUDA_AVAILABLE := 1
endif
else
NVCC ?= $(CUDA_TK)/bin/nvcc
CUDA_AVAILABLE := 1
endif


# Detect OpenCL
OPENCL_LDFLAGS := -lOpenCL
ifdef OPENCL
OPENCL_LDFLAGS := -L$(OPENCL)/lib -lOpenCL
endif

OPENCL_STATUS := $(shell echo 'int main() {}' | $(CXX) -x c++ $(OPENCL_LDFLAGS) - && rm a.out || echo NOTFOUND)

ifeq ($(OPENCL_STATUS), NOTFOUND)
$(info OpenCL support was not detected on the system.)
$(info If it is installed in a non-standard location, then set OPENCL to)
$(info the installation prefix and try again.)
$(info )
else
OPENCL_AVAILABLE := 1
endif


# Select backend
ifeq ($(filter clean,$(MAKECMDGOALS)),)
ifndef BACKEND
ifdef CUDA_AVAILABLE
$(info BACKEND not set and CUDA was detected, using CUDA)
BACKEND := CUDA
else
ifdef OPENCL_AVAILABLE
$(info BACKEND not set and OpenCL was detected, using OpenCL)
BACKEND := OpenCL
else
$(error BACKEND not set and neither CUDA nor OpenGL was detected.)
endif
endif
else
ifeq ($(BACKEND), CUDA)
ifndef CUDA_AVAILABLE
$(error BACKEND set to CUDA but it was not found.)
endif
$(info Using selected backend CUDA)
else
ifeq ($(BACKEND), OpenCL)
ifndef OPENCL_AVAILABLE
$(error BACKEND set to OpenCL but it was not found.)
endif
else
$(error BACKEND set to unknown value "$(BACKEND)", please use CUDA or OpenCL)
endif
$(info Using selected backend OpenCL)
endif
endif
endif
$(info )

# Testing/optimisation support
ifdef NTHREADS
CXXFLAGS += -DNTHREADS=$(NTHREADS) -DTIMING_STATS=1
endif

ifdef NBLOCKS_PER_MULTI
CXXFLAGS += -DNBLOCKS_PER_MULTI=$(NBLOCKS_PER_MULTI) -DTIMING_STATS=1
endif


# CUDA kernels
ifeq ($(BACKEND), CUDA)

INCLUDES = -I$(CUDA_TK)
CXXFLAGS += -D__INCLUDE_KERNELS__
LDFLAGS += -lcuda -fopenmp

CUDA_SRC = $(wildcard src/CUDA/*.cu)
PTX = $(CUDA_SRC:src/CUDA/%.cu=src/CUDA/%.ptx)
PTXH = $(CUDA_SRC:src/CUDA/%.cu=src/CUDA/%.ptxh)
NVCCFLAGS += -Isrc

KERNELS = $(PTX) $(PTXH)

%.ptx: %.cu
$(NVCC) --forward-unknown-to-host-compiler $(CXXFLAGS) $(NVCCFLAGS) -ptx $< -o $@

src/CUDA/%.ptxh: src/CUDA/%.ptx
xxd -i $< $@

endif


# OpenCL kernels
ifeq ($(BACKEND), OpenCL)

ifdef OPENCL
CXXFLAGS += -I$(OPENCL)/include
LDFLAGS += -L$(OPENCL)/lib
endif

INCLUDES =
CXXFLAGS += -D_OCL_ -D__INCLUDE_KERNELS__
LDFLAGS += -lOpenCL -fopenmp

OPENCL_SRC = $(wildcard src/OpenCL/*.cl)
CLE = $(OPENCL_SRC:src/OpenCL/%.cl=src/OpenCL/%.cle)
CLH = $(OPENCL_SRC:src/OpenCL/%.cl=src/OpenCL/%.clh)

KERNELS = $(CLE) $(CLH)

%.cle: %.cl
$(CC) -E -Isrc -o $@ - <$<

# xxd names the variable after the file name argument, and we expect
# the variable to not have a src_ prefix, so we have to remove it.
src/OpenCL/%.clh: src/OpenCL/%.cle
cd src && xxd -i $(<:src/%=%) $(@:src/%=%)

endif


# Main implementation
CXX_SRC := $(wildcard src/*.cpp src/SSE_AVX/*.cpp)
OBJS := $(CXX_SRC:%.cpp=%.o)
INCLUDES += -Isrc
CXXFLAGS += $(INCLUDES) -fPIC -g -O3 -Wall -Wextra -Wstrict-aliasing=2 -fopenmp

src/sapporohostclass.o: $(KERNELS)

%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@

libsapporo.a: $(OBJS)
ar qv $@ $^

libsapporo.so: $(OBJS)
$(CXX) -o $@ -shared $^ $(LDFLAGS)


# API compatibility libraries
EMU_SRC := $(wildcard src/interfaces/*lib.cpp)
EMU_STATIC_LIBS := $(EMU_SRC:src/interfaces/%lib.cpp=lib%.a)
EMU_SHARED_LIBS := $(EMU_SRC:src/interfaces/%lib.cpp=lib%.so)

.PHONY: emulated_interfaces
emulated_interfaces: $(EMU_STATIC_LIBS) $(EMU_SHARED_LIBS)

$(EMU_STATIC_LIBS): libsapporo.a

$(EMU_SHARED_LIBS): libsapporo.so


lib%.a: src/interfaces/%lib.o
ar qv $@ $^

lib%.so: src/interfaces/%lib.o
$(CXX) -o $@ -shared $^ -L. -lsapporo $(LDFLAGS)


# Installation
INSTALLED_LIBS := $(PREFIX)/lib/libsapporo.a $(PREFIX)/lib/libsapporo.so
INSTALLED_LIBS += $(EMU_STATIC_LIBS:%.a=$(PREFIX)/lib/%.a)
INSTALLED_LIBS += $(EMU_SHARED_LIBS:%.so=$(PREFIX)/lib/%.so)

INSTALLED_LIBS: $(PREFIX)/lib

HEADERS := $(wildcard include/*)
INSTALLED_HEADERS := $(HEADERS:include/%=$(PREFIX)/include/%)

INSTALLED_HEADERS: $(PREFIX)/include

$(PREFIX)/include:
mkdir -p $(PREFIX)/include

$(PREFIX)/include/%: include/% $(PREFIX)/include
install -m 644 $< $@

$(PREFIX)/lib:
mkdir -p $(PREFIX)/lib

$(PREFIX)/lib/%: % $(PREFIX)/lib
install -m 644 $< $@

.PHONY: install
install: $(INSTALLED_LIBS) $(INSTALLED_HEADERS)


# Clean-up
.PHONY: clean
clean:
rm -f *.a *.so src/*.o src/SSE_AVX/SSE/*.o src/SSE_AVX/AVX/*.o
rm -f src/CUDA/*.ptx src/CUDA/*.ptxh src/OpenCL/*.cle src/OpenCL/*.clh

32 changes: 25 additions & 7 deletions README.extended
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,33 @@ With some luck a simple 'make' in the lib folder is sufficient to
build the library, if not then here are some pointers:

CUDA
To build the CUDA library; Set the 'CUDA_TK' path to the location
where the CUDA toolkit is installed e.g.. CUDA_TK = /usr/local/cuda and
type: 'make' .

If CUDA is installed via the nVidia installer, Conda, or HPC modules,
then it should be detected automatically by the build system. If it
somehow isn't, then you can try to set 'CUDA_TK' to the location where
it is installed, e.g.

CUDA_TK=/usr/local/cuda make

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand setting a path like

CUDA_TK=/usr/local/cuda

or better

CUDA_TK=$CUDA_HOME

but the extra make I do not understand.


OpenCL
To build the CUDA library; Set the 'CUDA_TK' path to the location
where the cuda or AMD OpenCL toolkit is installed eg. CUDA_TK =
/usr/local/cuda or CUDA_TK = /opt/AMDAPP/ and type: 'make -f
Makefile_ocl' .

If OpenCL is installed in a standard location (e.g. via apt or yum),
then it should be detected automatically by the build system. If it
isn't, then you can set the 'OPENCL' variable to the location where
it is installed, e.g.

OPENCL=/opt/opencl make

If both CUDA and OpenCL are detected, then CUDA is used by default.
To select OpenCL, set BACKEND to OpenCL:

BACKEND=OpenCL make

You can combine these options as well:

BACKEND=OpenCL OPENCL=/home/user/.local make


Interfaces:
The library has built-in support for a couple of default interfaces to
Expand Down
4 changes: 4 additions & 0 deletions conda/conda_build_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gpu_backend:
- cuda
- opencl

52 changes: 52 additions & 0 deletions conda/meta.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{% set name = "sapporo2" %}
{% set version = "0.0.1" %}

package:
name: {{ name|lower }}
version: {{ version }}

source:
path: ../
# git_rev: work
# git_url: https://github.com/LourensVeen/sapporo2.git

build:
number: 0
string: {{ gpu_backend }}_{{ PKG_BUILDNUM }}

script_env:
- BACKEND=CUDA # [gpu_backend == 'cuda']
- BACKEND=OpenCL # [gpu_backend == 'opencl']
script: make install

missing_dso_whitelist:
- "*/libcuda.so*"

requirements:
build:
- {{ compiler('cxx') }}
- git
- git-lfs
- make
- cuda-compiler # [linux and gpu_backend == 'cuda']
- pocl # [linux and gpu_backend == 'opencl']
- vim
# - conda-verify

host:
- ocl-icd # [linux and gpu_backend == 'opencl']

run:
- cuda-runtime # [linux and gpu_backend == 'cuda']
- libgcc-ng
- libstdcxx-ng
- _openmp_mutex

test:

about:
home: https://github.com/treecode/sapporo2
summary: Library for emulating GRAPE6 n-body calculations
license: GPL-3.0-only
license_family: GPL
license_file: LICENSE
Loading