Skip to content

Commit 506f73a

Browse files
committed
Completed profiling/tuning in makefiles
1 parent 2c0dd7b commit 506f73a

File tree

4 files changed

+101
-4
lines changed

4 files changed

+101
-4
lines changed

CMakeLists.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ include(sources.cmake)
3434
option(BUILD_SHARED_LIBS "Build shared library and only the shared library if \"ON\", default is static" OFF)
3535
option(BUILD_TUNING "Run a tuning program for the fast multiplication/squaring algorithms if \"ON\"" OFF)
3636
option(BUILD_GRAPHS "Run a benchmark of the fast multiplication/squaring algorithms and make graphics if \"ON\"" OFF)
37+
38+
option(BUILD_PROFILE "Run a profiler (branch optimization) if \"ON\"" OFF)
39+
option(BUILD_PROFILE_TUNED "Run a profiler (branch optimization) after tuning fast multiplication if \"ON\"" OFF)
40+
option(BUILD_PROFILE_TUNED_TUNED "Run a profiler (branch optimization) after tuning fast multiplication and again at the end if \"ON\"" OFF)
41+
42+
option(BUILD_PROF_CMP "Run benchmarks before and after profiling and generate plots and images\
43+
in \"logs/\" if \"ON\" Works on the amalgamated version" OFF)
44+
option(BUILD_PROF_CMP_TUNED "Run benchmarks before and after tuning and profiling and generate\
45+
plots and images in \"logs/\" if \"ON\" Works on the amalgamated version" OFF)
46+
option(BUILD_PROF_CMP_TUNED_TUNED "Run benchmarks before and after tuning, profiling, tuning in that order\
47+
and generate\ plots and images in \"logs/\" if \"ON\" Works on the amalgamated version" OFF)
48+
49+
# Set a common control variable for profiling
50+
if(BUILD_PROFILE OR BUILD_PROFILE_TUNED OR BUILD_PROFILE_TUNED_TUNED OR BUILD_PROF_CMP OR BUILD_PROF_CMP_TUNED OR BUILD_PROF_CMP_TUNED_TUNED)
51+
set(PREPARE_PROFILING 1);
52+
endif()
53+
3754
#-----------------------------------------------------------------------------
3855
# Compose CFLAGS
3956
#-----------------------------------------------------------------------------
@@ -142,10 +159,29 @@ endif()
142159
# tuning and benchmark targets
143160
#-----------------------------------------------------------------------------
144161

162+
# build dependencies, run etc/tune_it.sh and additionally run benchmarks if so advised
145163
if(BUILD_TUNING OR BUILD_GRAPHS)
146164
add_subdirectory(etc ${CMAKE_CURRENT_SOURCE_DIR}/etc)
147165
endif()
148166

167+
# Profiling (branch optimizing)
168+
if(PREPARE_PROFILING)
169+
170+
## "Normal" version make->profile->make
171+
172+
### Tuned "normal" version make->tune->profile->make
173+
174+
### Double tuned Tuned "normal" version make->tune->profile->tune->make
175+
176+
## Amalgamated version (all *c files concatenated into one large file) otherwise same as above
177+
178+
### Tuned "amalgamated" version make->tune->profile->make
179+
180+
### Double tuned "amalgamated" version make->tune->profile->tune->make
181+
182+
endif()
183+
184+
149185
#-----------------------------------------------------------------------------
150186
# Install/export targets and files
151187
#-----------------------------------------------------------------------------

demo/timing.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ int main(int argc, char **argv)
134134
int n, cnt, ix, old_kara_m, old_kara_s, old_toom_m, old_toom_s;
135135
unsigned rr;
136136

137+
#ifdef _WIN32
138+
LARGE_INTEGER Frequency;
139+
#else
140+
struct timespec ts;
141+
#endif
142+
137143
CHECK_OK(mp_init(&a));
138144
CHECK_OK(mp_init(&b));
139145
CHECK_OK(mp_init(&c));
@@ -143,10 +149,21 @@ int main(int argc, char **argv)
143149

144150
srand(LTM_TIMING_RAND_SEED);
145151

146-
152+
#ifdef _WIN32
153+
QueryPerformanceFrequency(&Frequency);
154+
CLK_PER_SEC = (uint64) Frequency;
155+
#elif _POSIX_C_SOURCE >= 199309L
156+
/* returns -1 for an error and 0 for okay, sets errno (not used here) */
157+
if (clock_getres(CLOCK_MONOTONIC, &ts)) {
158+
fprintf(stderr, "%d, clock_getres failed\n", __LINE__);
159+
exit(EXIT_FAILURE);
160+
}
161+
CLK_PER_SEC = LTM_BILLION / ts.tv_nsec;
162+
#else
147163
CLK_PER_SEC = TIMFUNC();
148164
sleep(1);
149165
CLK_PER_SEC = TIMFUNC() - CLK_PER_SEC;
166+
#endif
150167

151168
printf("CLK_PER_SEC == %" PRIu64 "\n", CLK_PER_SEC);
152169

makefile

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,29 @@ profiled:
6969
rm -f *.a *.o timing
7070
make CFLAGS="$(CFLAGS) -fbranch-probabilities"
7171

72+
# run tune first, than optimize branching
73+
profiled_tuned: tune
74+
make CFLAGS="$(CFLAGS) -fprofile-arcs" timing
75+
./timing
76+
rm -f *.a *.o timing
77+
make CFLAGS="$(CFLAGS) -fbranch-probabilities"
78+
79+
# run tune first, optimize branching, run tune again
80+
# (running it in a loop until the timings stabilize is indeed tempting)
81+
profiled_tuned_tuned: tune
82+
make CFLAGS="$(CFLAGS) -fprofile-arcs" timing
83+
./timing
84+
rm -f *.a *.o timing
85+
make CFLAGS="$(CFLAGS) -fbranch-probabilities"
86+
./etc/tune_it.sh
87+
7288
#make a single object profiled library
7389
amalgamated_timing: pre_gen
7490
$(CC) $(LTM_CFLAGS) -fprofile-arcs -c pre_gen/tommath_amalgam.c -o tommath_amalgam.o
7591
$(CC) $(LTM_CFLAGS) -DMP_VERSION=\"before\" demo/timing.c tommath_amalgam.o -lgcov -o timing
7692

7793

78-
amalgamated_timingtuned: tune pre_gen
94+
amalgamated_timing_tuned: tune pre_gen
7995
$(CC) $(LTM_CFLAGS) -fprofile-arcs -c pre_gen/tommath_amalgam.c -o tommath_amalgam.o
8096
$(CC) $(LTM_CFLAGS) -DMP_VERSION=\"before\" demo/timing.c tommath_amalgam.o -lgcov -o timing
8197

@@ -85,6 +101,30 @@ profiled_single: amalgamated_timing
85101
$(CC) $(LTM_CFLAGS) -fbranch-probabilities -c pre_gen/tommath_amalgam.c -o tommath_amalgam.o
86102
$(AR) $(ARFLAGS) $(LIBNAME) tommath_amalgam.o
87103

104+
# run tune first, than optimize branching
105+
profiled_single_tuned: amalgamated_timing_tuned
106+
./timing
107+
rm -f *.a *.o timing
108+
$(CC) $(LTM_CFLAGS) -fbranch-probabilities -c pre_gen/tommath_amalgam.c -o tommath_amalgam.o
109+
$(AR) $(ARFLAGS) $(LIBNAME) tommath_amalgam.o
110+
111+
# run tune first, optimize branching, run tune again
112+
# (running it in a loop until the timings stabilize is indeed tempting)
113+
profiled_single_tuned_tuned: amalgamated_timing_tuned
114+
./timing
115+
rm -f *.o timing
116+
$(CC) $(LTM_CFLAGS) -fbranch-probabilities -c pre_gen/tommath_amalgam.c -o tommath_amalgam.o
117+
$(AR) $(ARFLAGS) $(LIBNAME) tommath_amalgam.o
118+
rm -f etc/tune.o
119+
$(CC) $(LTM_CFLAGS) -c etc/tune.c -o etc/tune.o
120+
$(CC) $(LTM_CFLAGS) tommath_amalgam.o etc/tune.o -o etc/tune
121+
./etc/tune_it.sh
122+
rm -f *.a *.o
123+
$(CC) $(LTM_CFLAGS) -fprofile-arcs -c pre_gen/tommath_amalgam.c -o tommath_amalgam.o
124+
$(CC) $(LTM_CFLAGS) -fbranch-probabilities -c pre_gen/tommath_amalgam.c -o tommath_amalgam.o
125+
$(AR) $(ARFLAGS) $(LIBNAME) tommath_amalgam.o
126+
127+
88128
install: $(LIBNAME) .install_common
89129
install -m 644 $(LIBNAME) $(DESTDIR)$(LIBPATH)
90130
install -m 644 $(HEADERS_PUB) $(DESTDIR)$(INCPATH)
@@ -136,7 +176,12 @@ cmp: profiled_single
136176
./timing
137177
$(MAKE) -C logs/ cmp
138178

139-
cmptuned: profiled_single tune
179+
cmp_tuned: profiled_single_tuned
180+
$(CC) $(LTM_CFLAGS) -DMP_VERSION=\"after\" demo/timing.c $(LIBNAME) -lgcov -o timing
181+
./timing
182+
$(MAKE) -C logs/ cmp
183+
184+
cmp_tuned_tuned: profiled_single_tuned_tuned
140185
$(CC) $(LTM_CFLAGS) -DMP_VERSION=\"after\" demo/timing.c $(LIBNAME) -lgcov -o timing
141186
./timing
142187
$(MAKE) -C logs/ cmp

makefile_include.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ check: test
168168
./test
169169

170170
#make the code coverage of the library
171-
#
172171
coverage: LTM_CFLAGS += -fprofile-arcs -ftest-coverage -DTIMING_NO_LOGS
173172
coverage: LTM_LFLAGS += -lgcov
174173
coverage: LTM_LDFLAGS += -lgcov

0 commit comments

Comments
 (0)