| 1 |
# sc-membench Makefile |
| 2 |
# |
| 3 |
# Portable build system for Linux, macOS, and BSD |
| 4 |
# Uses OpenMP for parallel bandwidth measurement |
| 5 |
# |
| 6 |
# Build options: |
| 7 |
# make - Auto-detect features and build universal binary |
| 8 |
# make basic - Build minimal version (no optional dependencies) |
| 9 |
# make hwloc - Build with hwloc (portable cache detection) |
| 10 |
# make numa - Build with NUMA support (Linux only) |
| 11 |
# make full - Build with all available features |
| 12 |
# make clean - Remove built files |
| 13 |
# make test - Quick test run |
| 14 |
|
| 15 |
# ============================================================================= |
| 16 |
# Platform and Compiler Detection |
| 17 |
# ============================================================================= |
| 18 |
|
| 19 |
# Detect OS |
| 20 |
UNAME_S := $(shell uname -s) |
| 21 |
|
| 22 |
# Auto-detect compiler: prefer gcc, fall back to clang, then cc |
| 23 |
CC ?= $(shell command -v gcc 2>/dev/null || command -v clang 2>/dev/null || echo cc) |
| 24 |
|
| 25 |
# Base flags (portable across gcc/clang) |
| 26 |
CFLAGS_BASE = -g -O3 -Wall -Wextra -std=c11 |
| 27 |
|
| 28 |
# OpenMP flag (same for gcc and clang) |
| 29 |
OPENMP_FLAG = -fopenmp |
| 30 |
|
| 31 |
# ============================================================================= |
| 32 |
# Source Files and Targets |
| 33 |
# ============================================================================= |
| 34 |
|
| 35 |
SRC = membench.c |
| 36 |
TARGET = membench |
| 37 |
TARGET_BASIC = membench-basic |
| 38 |
TARGET_HWLOC = membench-hwloc |
| 39 |
TARGET_NUMA = membench-numa |
| 40 |
TARGET_FULL = membench-full |
| 41 |
|
| 42 |
# ============================================================================= |
| 43 |
# Platform-Specific Universal Optimization Flags |
| 44 |
# ============================================================================= |
| 45 |
|
| 46 |
# Platform-specific adjustments with UNIVERSAL compatibility |
| 47 |
ifeq ($(UNAME_S),Darwin) |
| 48 |
# macOS: packages typically in /opt/homebrew (ARM) or /usr/local (Intel) |
| 49 |
ARCH := $(shell uname -m) |
| 50 |
ifeq ($(ARCH),arm64) |
| 51 |
# ARM64 macOS: Use generic ARMv8-A (works on all Apple Silicon) |
| 52 |
CFLAGS_ARCH = -mcpu=generic |
| 53 |
CFLAGS_PATHS = -I/opt/homebrew/include |
| 54 |
LDFLAGS_PATHS = -L/opt/homebrew/lib |
| 55 |
else |
| 56 |
# x86_64 macOS: Use baseline x86-64 (works on all Intel Macs) |
| 57 |
CFLAGS_ARCH = -march=x86-64 -mtune=generic |
| 58 |
CFLAGS_PATHS = -I/usr/local/include |
| 59 |
LDFLAGS_PATHS = -L/usr/local/lib |
| 60 |
endif |
| 61 |
# macOS with clang needs libomp |
| 62 |
LDFLAGS_BASE = -lm |
| 63 |
# Check if using clang (needs -lomp for OpenMP) |
| 64 |
IS_CLANG := $(shell $(CC) --version 2>/dev/null | grep -q clang && echo yes) |
| 65 |
ifeq ($(IS_CLANG),yes) |
| 66 |
OPENMP_LIBS = -lomp |
| 67 |
else |
| 68 |
OPENMP_LIBS = |
| 69 |
endif |
| 70 |
else ifeq ($(UNAME_S),FreeBSD) |
| 71 |
# FreeBSD: packages in /usr/local, use baseline x86-64 |
| 72 |
CFLAGS_ARCH = -march=x86-64 -mtune=generic |
| 73 |
CFLAGS_PATHS = -I/usr/local/include |
| 74 |
LDFLAGS_PATHS = -L/usr/local/lib |
| 75 |
LDFLAGS_BASE = -lm |
| 76 |
OPENMP_LIBS = |
| 77 |
else ifeq ($(UNAME_S),OpenBSD) |
| 78 |
CFLAGS_ARCH = -march=x86-64 -mtune=generic |
| 79 |
CFLAGS_PATHS = -I/usr/local/include |
| 80 |
LDFLAGS_PATHS = -L/usr/local/lib |
| 81 |
LDFLAGS_BASE = -lm |
| 82 |
OPENMP_LIBS = |
| 83 |
else ifeq ($(UNAME_S),NetBSD) |
| 84 |
CFLAGS_ARCH = -march=x86-64 -mtune=generic |
| 85 |
CFLAGS_PATHS = -I/usr/local/include -I/usr/pkg/include |
| 86 |
LDFLAGS_PATHS = -L/usr/local/lib -L/usr/pkg/lib |
| 87 |
LDFLAGS_BASE = -lm |
| 88 |
OPENMP_LIBS = |
| 89 |
else |
| 90 |
# Linux (default) - Use conservative, universally compatible flags |
| 91 |
ARCH := $(shell uname -m) |
| 92 |
ifeq ($(ARCH),aarch64) |
| 93 |
# ARM64: Use generic ARMv8-A with CRC (universally supported) |
| 94 |
# This works on all ARM64 CPUs from Cortex-A53 to Neoverse-V2 |
| 95 |
CFLAGS_ARCH = -mcpu=generic+crc |
| 96 |
else ifeq ($(ARCH),x86_64) |
| 97 |
# x86_64: Use baseline x86-64 with SSE2 (universally supported since 2003) |
| 98 |
# This works on all x86_64 CPUs from Opteron/Pentium 4 to latest Xeon/EPYC |
| 99 |
CFLAGS_ARCH = -march=x86-64 -mtune=generic |
| 100 |
else |
| 101 |
# Other architectures: use generic optimization |
| 102 |
#CFLAGS_ARCH = -mtune=generic |
| 103 |
endif |
| 104 |
CFLAGS_PATHS = |
| 105 |
LDFLAGS_PATHS = |
| 106 |
LDFLAGS_BASE = -lm |
| 107 |
OPENMP_LIBS = |
| 108 |
endif |
| 109 |
|
| 110 |
CFLAGS = $(CFLAGS_BASE) $(CFLAGS_ARCH) $(CFLAGS_PATHS) $(OPENMP_FLAG) |
| 111 |
LDFLAGS = $(OPENMP_FLAG) $(LDFLAGS_BASE) $(LDFLAGS_PATHS) $(OPENMP_LIBS) |
| 112 |
|
| 113 |
# ============================================================================= |
| 114 |
# Library Detection |
| 115 |
# ============================================================================= |
| 116 |
|
| 117 |
# Check for hwloc (cross-platform) |
| 118 |
HAVE_HWLOC := $(shell pkg-config --exists hwloc 2>/dev/null && echo yes || \ |
| 119 |
(test -f /usr/include/hwloc.h && echo yes) || \ |
| 120 |
(test -f /usr/local/include/hwloc.h && echo yes) || \ |
| 121 |
(test -f /opt/homebrew/include/hwloc.h && echo yes)) |
| 122 |
|
| 123 |
# Check for libnuma (Linux only) |
| 124 |
ifeq ($(UNAME_S),Linux) |
| 125 |
HAVE_NUMA := $(shell pkg-config --exists numa 2>/dev/null && echo yes || \ |
| 126 |
(test -f /usr/include/numa.h && echo yes)) |
| 127 |
else |
| 128 |
HAVE_NUMA := no |
| 129 |
endif |
| 130 |
|
| 131 |
# Check for libhugetlbfs (Linux only) |
| 132 |
ifeq ($(UNAME_S),Linux) |
| 133 |
HAVE_HUGETLBFS := $(shell pkg-config --exists hugetlbfs 2>/dev/null && echo yes || \ |
| 134 |
(test -f /usr/include/hugetlbfs.h && echo yes)) |
| 135 |
else |
| 136 |
HAVE_HUGETLBFS := no |
| 137 |
endif |
| 138 |
|
| 139 |
# Auto-detect features and compile with all available |
| 140 |
DETECTED_DEFS = |
| 141 |
DETECTED_LIBS = |
| 142 |
|
| 143 |
ifeq ($(HAVE_HUGETLBFS),yes) |
| 144 |
DETECTED_DEFS += -DHAVE_HUGETLBFS |
| 145 |
DETECTED_LIBS += -lhugetlbfs |
| 146 |
endif |
| 147 |
|
| 148 |
ifeq ($(HAVE_HWLOC),yes) |
| 149 |
DETECTED_DEFS += -DUSE_HWLOC |
| 150 |
DETECTED_LIBS += -lhwloc |
| 151 |
endif |
| 152 |
|
| 153 |
ifeq ($(HAVE_NUMA),yes) |
| 154 |
DETECTED_DEFS += -DUSE_NUMA |
| 155 |
DETECTED_LIBS += -lnuma |
| 156 |
endif |
| 157 |
|
| 158 |
# ============================================================================= |
| 159 |
# Build Targets |
| 160 |
# ============================================================================= |
| 161 |
|
| 162 |
.PHONY: default all clean test basic hwloc numa full help info |
| 163 |
|
| 164 |
# Default: auto-detect and build universal binary |
| 165 |
default: $(TARGET) |
| 166 |
|
| 167 |
$(TARGET): $(SRC) |
| 168 |
@echo "Building universal binary for $(UNAME_S) $(ARCH)..." |
| 169 |
@echo " Compiler: $(CC)" |
| 170 |
@echo " Optimization: $(CFLAGS_ARCH) (universal compatibility)" |
| 171 |
@echo " OpenMP: enabled" |
| 172 |
@echo " hwloc: $(HAVE_HWLOC)" |
| 173 |
@echo " numa: $(HAVE_NUMA)" |
| 174 |
@echo " hugetlbfs: $(HAVE_HUGETLBFS)" |
| 175 |
$(CC) $(CFLAGS) $(DETECTED_DEFS) -o $@ $< $(LDFLAGS) $(DETECTED_LIBS) |
| 176 |
|
| 177 |
# Basic: minimal build, no optional dependencies |
| 178 |
basic: $(TARGET_BASIC) |
| 179 |
|
| 180 |
$(TARGET_BASIC): $(SRC) |
| 181 |
$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) |
| 182 |
|
| 183 |
# Build with hwloc support (portable cache/topology detection) |
| 184 |
hwloc: $(TARGET_HWLOC) |
| 185 |
|
| 186 |
$(TARGET_HWLOC): $(SRC) |
| 187 |
ifeq ($(HAVE_HWLOC),yes) |
| 188 |
$(CC) $(CFLAGS) -DUSE_HWLOC -o $@ $< $(LDFLAGS) -lhwloc |
| 189 |
else |
| 190 |
@echo "Error: hwloc 2.x not found. Install with:" |
| 191 |
@echo " Linux: apt install libhwloc-dev (or: yum install hwloc-devel)" |
| 192 |
@echo " macOS: brew install hwloc" |
| 193 |
@echo " BSD: pkg install hwloc2" |
| 194 |
@exit 1 |
| 195 |
endif |
| 196 |
|
| 197 |
# Build with NUMA support (Linux only) |
| 198 |
numa: $(TARGET_NUMA) |
| 199 |
|
| 200 |
$(TARGET_NUMA): $(SRC) |
| 201 |
ifeq ($(UNAME_S),Linux) |
| 202 |
ifeq ($(HAVE_NUMA),yes) |
| 203 |
$(CC) $(CFLAGS) -DUSE_NUMA -o $@ $< $(LDFLAGS) -lnuma |
| 204 |
else |
| 205 |
@echo "Error: libnuma not found. Install with:" |
| 206 |
@echo " apt install libnuma-dev (or: yum install numactl-devel)" |
| 207 |
@exit 1 |
| 208 |
endif |
| 209 |
else |
| 210 |
@echo "Error: NUMA support is only available on Linux" |
| 211 |
@exit 1 |
| 212 |
endif |
| 213 |
|
| 214 |
# Build with all features (recommended for production Linux servers) |
| 215 |
full: $(TARGET_FULL) |
| 216 |
|
| 217 |
$(TARGET_FULL): $(SRC) |
| 218 |
ifeq ($(UNAME_S),Linux) |
| 219 |
$(CC) $(CFLAGS) -DUSE_HWLOC -DUSE_NUMA $(if $(filter yes,$(HAVE_HUGETLBFS)),-DHAVE_HUGETLBFS) \ |
| 220 |
-o $@ $< $(LDFLAGS) -lhwloc -lnuma $(if $(filter yes,$(HAVE_HUGETLBFS)),-lhugetlbfs) |
| 221 |
else |
| 222 |
@echo "Note: Building without NUMA (not available on $(UNAME_S))" |
| 223 |
$(CC) $(CFLAGS) -DUSE_HWLOC -o $@ $< $(LDFLAGS) -lhwloc |
| 224 |
endif |
| 225 |
|
| 226 |
# Build all versions that can be built on this platform |
| 227 |
all: $(TARGET) $(TARGET_BASIC) |
| 228 |
ifeq ($(HAVE_HWLOC),yes) |
| 229 |
$(MAKE) hwloc |
| 230 |
endif |
| 231 |
ifeq ($(HAVE_NUMA),yes) |
| 232 |
$(MAKE) numa |
| 233 |
endif |
| 234 |
ifeq ($(UNAME_S),Linux) |
| 235 |
ifeq ($(HAVE_HWLOC),yes) |
| 236 |
ifeq ($(HAVE_NUMA),yes) |
| 237 |
$(MAKE) full |
| 238 |
endif |
| 239 |
endif |
| 240 |
endif |
| 241 |
|
| 242 |
# Quick test (30 seconds) |
| 243 |
test: $(TARGET) |
| 244 |
./$(TARGET) -v -t 30 |
| 245 |
|
| 246 |
clean: |
| 247 |
rm -f $(TARGET) $(TARGET_BASIC) $(TARGET_HWLOC) $(TARGET_NUMA) $(TARGET_FULL) |
| 248 |
|
| 249 |
# Install to /usr/local (override with: make install prefix=/usr) |
| 250 |
prefix ?= /usr/local |
| 251 |
mandir ?= $(prefix)/share/man |
| 252 |
|
| 253 |
install: $(TARGET) |
| 254 |
install -d $(DESTDIR)$(prefix)/bin |
| 255 |
install -m 755 $(TARGET) $(DESTDIR)$(prefix)/bin/membench |
| 256 |
install -d $(DESTDIR)$(mandir)/man1 |
| 257 |
install -m 644 membench.1 $(DESTDIR)$(mandir)/man1/membench.1 |
| 258 |
|
| 259 |
# Show detected configuration |
| 260 |
info: |
| 261 |
@echo "Platform Detection:" |
| 262 |
@echo " OS: $(UNAME_S)" |
| 263 |
@echo " Arch: $(ARCH)" |
| 264 |
@echo " Compiler: $(CC)" |
| 265 |
@echo " CFLAGS: $(CFLAGS)" |
| 266 |
@echo " LDFLAGS: $(LDFLAGS)" |
| 267 |
@echo "" |
| 268 |
@echo "Library Detection:" |
| 269 |
@echo " hwloc: $(HAVE_HWLOC)" |
| 270 |
@echo " numa: $(HAVE_NUMA)" |
| 271 |
@echo " hugetlbfs: $(HAVE_HUGETLBFS)" |
| 272 |
@echo "" |
| 273 |
@echo "Universal Optimization:" |
| 274 |
ifeq ($(ARCH),aarch64) |
| 275 |
@echo " ARM64: -mcpu=generic+crc (works on all ARM64 CPUs)" |
| 276 |
else ifeq ($(ARCH),x86_64) |
| 277 |
@echo " x86_64: -march=x86-64 (works on all x86_64 CPUs since 2003)" |
| 278 |
else |
| 279 |
@echo " Other: -mtune=generic" |
| 280 |
endif |
| 281 |
|
| 282 |
# Help target |
| 283 |
help: |
| 284 |
@echo "sc-membench - Universal Memory Benchmark (OpenMP)" |
| 285 |
@echo "" |
| 286 |
@echo "Build targets:" |
| 287 |
@echo " make - Auto-detect features and build universal binary" |
| 288 |
@echo " make basic - Minimal build (no optional dependencies)" |
| 289 |
@echo " make hwloc - With hwloc (portable cache detection)" |
| 290 |
@echo " make numa - With NUMA support (Linux only)" |
| 291 |
@echo " make full - With all features (hwloc + numa, Linux recommended)" |
| 292 |
@echo " make all - Build all available versions" |
| 293 |
@echo " make info - Show detected platform and libraries" |
| 294 |
@echo "" |
| 295 |
@echo "Universal Compatibility:" |
| 296 |
@echo " This build system uses conservative optimization flags that work" |
| 297 |
@echo " on ALL CPUs of the target architecture:" |
| 298 |
@echo " - ARM64: -mcpu=generic+crc (Cortex-A53 to Neoverse-V2)" |
| 299 |
@echo " - x86_64: -march=x86-64 (Opteron/P4 to latest Xeon/EPYC)" |
| 300 |
@echo " No illegal instruction errors, works in any Docker container." |
| 301 |
@echo "" |
| 302 |
@echo "OpenMP thread control (environment variables):" |
| 303 |
@echo " OMP_PROC_BIND=spread Distribute threads across NUMA nodes" |
| 304 |
@echo " OMP_PLACES=cores One thread per physical core" |
| 305 |
@echo " OMP_NUM_THREADS=N Override thread count" |
| 306 |
@echo "" |
| 307 |
@echo "Optional dependencies:" |
| 308 |
@echo " hwloc 2: Portable cache/topology detection (requires hwloc 2.x)" |
| 309 |
@echo " Linux: apt install libhwloc-dev" |
| 310 |
@echo " macOS: brew install hwloc libomp" |
| 311 |
@echo " BSD: pkg install hwloc2" |
| 312 |
@echo "" |
| 313 |
@echo " numa: NUMA-aware memory allocation (Linux only)" |
| 314 |
@echo " apt install libnuma-dev" |
| 315 |
@echo "" |
| 316 |
@echo " hugetlbfs: Better huge page detection (Linux only)" |
| 317 |
@echo " apt install libhugetlbfs-dev" |