-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathconfigure
More file actions
executable file
·208 lines (184 loc) · 5.54 KB
/
configure
File metadata and controls
executable file
·208 lines (184 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/bin/sh
# Economiccomplexity's anticonf script by Pacha (2025)
# Detect number of cores
if [ -n "$ECONOMICCOMPLEXITY_NCORES" ]; then
num_cores="$ECONOMICCOMPLEXITY_NCORES"
else
num_cores=$(${R_HOME}/bin/Rscript -e "cat(as.integer(parallel::detectCores()/2))" 2>/dev/null || echo 0)
if [ "$num_cores" = "0" ] || [ -z "$num_cores" ]; then
if [ -f /proc/cpuinfo ]; then
num_cores=$(grep -c "^processor" /proc/cpuinfo 2>/dev/null || echo 1)
elif [ "$(uname)" = "Darwin" ]; then
num_cores=$(sysctl -n hw.ncpu 2>/dev/null || echo 1)
else
num_cores=1
fi
num_cores=$(($((num_cores / 2)) > 1 ? $((num_cores / 2)) : 1))
fi
fi
# Find compiler
CXX=$(${R_HOME}/bin/R CMD config CXX)
CXXFLAGS=$(${R_HOME}/bin/R CMD config CXXFLAGS)
SHLIB_OPENMP_CXXFLAGS=$(${R_HOME}/bin/R CMD config SHLIB_OPENMP_CXXFLAGS 2>/dev/null || echo "")
# Find BLAS/LAPACK
BLAS_LIBS=$(${R_HOME}/bin/R CMD config BLAS_LIBS)
LAPACK_LIBS=$(${R_HOME}/bin/R CMD config LAPACK_LIBS)
# OpenMP support
OPENMP_SUPPORT="no"
cat > testomp.cpp <<EOF
#include <omp.h>
int main() {
#pragma omp parallel
{
int tid = omp_get_thread_num();
}
return 0;
}
EOF
if $CXX $CXXFLAGS -fopenmp testomp.cpp -o testomp >/dev/null 2>&1; then
OPENMP_SUPPORT="yes"
fi
rm -f testomp testomp.cpp
# Detect CPU features for SIMD optimization
SIMD_SUPPORT=""
AVX2_SUPPORT="no"
SSE42_SUPPORT="no"
if [ -f /proc/cpuinfo ]; then
if grep -q avx2 /proc/cpuinfo 2>/dev/null; then
AVX2_SUPPORT="yes"
SIMD_SUPPORT="AVX2"
elif grep -q sse4_2 /proc/cpuinfo 2>/dev/null; then
SSE42_SUPPORT="yes"
SIMD_SUPPORT="SSE4.2"
echo "- [V] SSE4.2 support detected"
fi
elif [ "$(uname)" = "Darwin" ]; then
if sysctl machdep.cpu.features 2>/dev/null | grep -q AVX2; then
AVX2_SUPPORT="yes"
SIMD_SUPPORT="AVX2"
elif sysctl machdep.cpu.features 2>/dev/null | grep -q SSE4.2; then
SSE42_SUPPORT="yes"
SIMD_SUPPORT="SSE4.2"
echo "- [V] SSE4.2 support detected"
fi
fi
# Test compiler flag
# Usage: test_flag "-O3"
test_flag() {
echo 'int main(){return 0;}' > testrconf.cpp
if $CXX $CXXFLAGS $1 testrconf.cpp -o testrconf >/dev/null 2>&1; then
rm -f testrconf testrconf.cpp
return 0
else
rm -f testrconf testrconf.cpp
return 1
fi
}
# Remove any -std= from CXX and CXXFLAGS
CXX=$(echo "$CXX" | sed -E 's/ *-std=[^ ]+//g')
CXXFLAGS=$(echo "$CXXFLAGS" | sed -E 's/ *-std=[^ ]+//g')
# C++ standard detection (must come after test_flag is defined)
CXX_STD="CXX11" # Default fallback
STD_FLAG="-std=c++11"
if test_flag "-std=c++20"; then
CXX_STD="CXX20"
STD_FLAG="-std=c++20"
elif test_flag "-std=c++17"; then
CXX_STD="CXX17"
STD_FLAG="-std=c++17"
elif test_flag "-std=c++14"; then
CXX_STD="CXX14"
STD_FLAG="-std=c++14"
fi
# Add the detected standard to CXXFLAGS
CXXFLAGS="$CXXFLAGS $STD_FLAG"
# High-performance optimization flags
OPTFLAGS=""
if [ "$ECONOMICCOMPLEXITYOPTIMIZATIONS" = "yes" ]; then
# Core optimization flags
for flag in -O3 -ffast-math; do
if test_flag "$flag"; then
OPTFLAGS="$OPTFLAGS $flag"
fi
done
# Architecture-specific optimizations
for flag in -march=native -mtune=native; do
if test_flag "$flag"; then
OPTFLAGS="$OPTFLAGS $flag"
fi
done
# SIMD optimizations based on detected features
if [ "$AVX2_SUPPORT" = "yes" ]; then
for flag in -mavx -mavx2 -mfma; do
if test_flag "$flag"; then
OPTFLAGS="$OPTFLAGS $flag"
fi
done
elif [ "$SSE42_SUPPORT" = "yes" ]; then
for flag in -msse2 -msse3 -msse4.1 -msse4.2; do
if test_flag "$flag"; then
OPTFLAGS="$OPTFLAGS $flag"
fi
done
fi
# Loop optimization flags
for flag in -funroll-loops -fprefetch-loop-arrays; do
if test_flag "$flag"; then
OPTFLAGS="$OPTFLAGS $flag"
fi
done
# Vectorization flags
for flag in -ftree-vectorize -ftree-slp-vectorize; do
if test_flag "$flag"; then
OPTFLAGS="$OPTFLAGS $flag"
fi
done
# Link-time optimization
if test_flag "-flto"; then
OPTFLAGS="$OPTFLAGS -flto"
fi
fi
# Compose ARMA_FLAGS
ARMA_FLAGS="-DARMA_NO_DEBUG -DARMA_USE_BLAS -DARMA_USE_LAPACK -DARMA_USE_OPENMP -DARMA_OPENMP_THREADS=$num_cores"
# Add 64-bit word size for better SIMD alignment
ARMA_FLAGS="$ARMA_FLAGS -DARMA_64BIT_WORD"
# Compose Makevars
if [ ! -f "src/Makevars.in" ]; then
echo "ERROR: src/Makevars.in template not found"
exit 1
fi
sed -e "s|@CXX_STD@|${CXX_STD}|g" \
-e "s|@OPTFLAGS@|${OPTFLAGS}|g" \
-e "s|@ARMA_FLAGS@|${ARMA_FLAGS}|g" \
-e "s|@ncores@|${num_cores}|g" \
-e "s|@DEBUG_FLAGS@|${DEBUG_FLAGS}|g" \
src/Makevars.in > src/Makevars
# Print summary
cat <<EOF
==========================================================================
Economiccomplexity configuration summary
C++ Compiler: $CXX
C++ Standard: $CXX_STD
OpenMP Support: $OPENMP_SUPPORT
OpenMP Threads: $num_cores
SIMD Support: ${SIMD_SUPPORT:-None}
BLAS: $BLAS_LIBS
LAPACK: $LAPACK_LIBS
High-Performance Mode: ${ECONOMICCOMPLEXITYOPTIMIZATIONS:-no}
Optimization Flags: $OPTFLAGS
EOF
echo "For faster computation"
echo " "
if [ "${ECONOMICCOMPLEXITYOPTIMIZATIONS:-no}" != "yes" ]; then
echo " Enable high-performance optimizations:"
echo " export ECONOMICCOMPLEXITYOPTIMIZATIONS=yes"
echo " "
fi
echo " Change thread count:"
echo " export ECONOMICCOMPLEXITY_NCORES=<number>"
echo " "
echo " Then reinstall the package"
cat <<EOF
==========================================================================
EOF
exit 0