This guide is modified from section (d) of the worksheet inside Module 10 of Phys 6810 Computational Physics (Spring 2023).
NOTE: gprof does not work on macOS. Please use a linux machine (such as OSC)
To use gprof, compile and link the relevant codes with the -pg option:
Take a look at the Makefile make_hello_world and modify both the CFLAGS and LDFLAGS lines to include -pg
Compile and link the script by typing
make -f make_hello_world
Execute the program
./hello_world.x
With the -pg flags, the execution will generate a file called gmon.out that is used by gprof.
The program has to exit normally (e.g. we can't stop with a ctrl-C).
Warning: Any existing gmon.out file will be overwritten.
Run gprof and save the output to a file (e.g., gprof.out) by
gprof hello_world.x > gprof.out
We should at this point see a text file called gprof.out which contains the profile of hello_world.cpp
vim gprof.out |
SHELL=/bin/sh
# Note: Comments start with #. $(FOOBAR) means: evaluate the variable
# defined by FOOBAR= (something).
# This file contains a set of rules used by the "make" command.
# This makefile $(MAKEFILE) tells "make" how the executable $(COMMAND)
# should be create from the source files $(SRCS) and the header files
# $(HDRS) via the object files $(OBJS); type the command:
# "make -f make_program"
# where make_program should be replaced by the name of the makefile.
#
# Programmer: Dick Furnstahl (furnstahl.1@osu.edu)
# Latest revision: 12-Jan-2016
#
# Notes:
# * If you are ok with the default options for compiling and linking, you
# only need to change the entries in section 1.
#
# * Defining BASE determines the name for the makefile (prepend "make_"),
# executable (append ".x"), zip archive (append ".zip") and gzipped
# tar file (append ".tar.gz").
#
# * To remove the executable and object files, type the command:
# "make -f $(MAKEFILE) clean"
#
# * To create a zip archive with name $(BASE).zip containing this
# makefile and the SRCS and HDRS files, type the command:
# "make -f $(MAKEFILE) zip"
#
# * To create a gzipped tar file with name $(BASE).tar.gz containing this
# makefile and the source and header files, type the command:
# "make -f $(MAKEFILE) tarz"
#
# * Continuation lines are indicated by \ with no space after it.
# If you get a "missing separator" error, it is probably because there
# is a space after a \ somewhere.
#
###########################################################################
# 1. Specify base name, source files, header files, input files
###########################################################################
# The base for the names of the makefile, executable command, etc.
BASE= hello_world
# Put all C++ (or other) source files here. NO SPACES after continuation \'s.
SRCS= \
hello_world.cpp
# Put all header files here. NO SPACES after continuation \'s.
HDRS= \
# Put any input files you want to be saved in tarballs (e.g., sample files).
INPFILE= \
###########################################################################
# 2. Generate names for object files, makefile, command to execute, tar file
###########################################################################
# *** YOU should not edit these lines unless to change naming conventions ***
OBJS= $(addsuffix .o, $(basename $(SRCS)))
MAKEFILE= make_$(BASE)
COMMAND= $(BASE).x
TARFILE= $(BASE).tar.gz
ZIPFILE= $(BASE).zip
###########################################################################
# 3. Commands and options for different compilers
###########################################################################
#
# Compiler parameters
#
# CXX Name of the C++ compiler to use
# CFLAGS Flags to the C++ compiler
# CWARNS Warning options for C++ compiler
# F90 Name of the fortran compiler to use (if relevant)
# FFLAGS Flags to the fortran compiler
# LDFLAGS Flags to the loader
# LIBS A list of libraries
#
CXX= g++
CFLAGS= -g
CWARNS= -Wall -W -Wshadow -fno-common
MOREFLAGS= -Wpedantic -Wpointer-arith -Wcast-qual -Wcast-align \
-Wwrite-strings -fshort-enums
# add relevant libraries and link options
LIBS=
# LDFLAGS= -lgsl -lgslcblas
LDFLAGS=
###########################################################################
# 4. Instructions to compile and link, with dependencies
###########################################################################
all: $(COMMAND)
.SUFFIXES:
.SUFFIXES: .o .mod .f90 .f .cpp
#%.o: %.mod
# This is the command to link all of the object files together.
# For fortran, replace CXX by F90.
$(COMMAND): $(OBJS) $(MAKEFILE)
$(CXX) -o $(COMMAND) $(OBJS) $(LDFLAGS) $(LIBS)
# Command to make object (.o) files from C++ source files (assumed to be .cpp).
# Add $(MOREFLAGS) if you want additional warning options.
%.o: %.cpp $(HDRS) $(MAKEFILE)
$(CXX) -c $(CFLAGS) $(CWARNS) -o $@ $<
# Commands to make object (.o) files from Fortran-90 (or beyond) and
# Fortran-77 source files (.f90 and .f, respectively).
.f90.mod:
$(F90) -c $(F90FLAGS) -o $@ $<
.f90.o:
$(F90) -c $(F90FLAGS) -o $@ $<
.f.o:
$(F90) -c $(FFLAGS) -o $@ $<
##########################################################################
# 5. Additional tasks
##########################################################################
# Delete the program and the object files (and any module files)
clean:
/bin/rm -f $(COMMAND) $(OBJS)
/bin/rm -f $(MODIR)/*.mod
# Pack up the code in a compressed gnu tar file
tarz:
tar cfvz $(TARFILE) $(MAKEFILE) $(SRCS) $(HDRS) $(MODIR) $(INPFILE)
# Pack up the code in a zip archive
zip:
zip -r $(ZIPFILE) $(MAKEFILE) $(SRCS) $(HDRS) $(MODIR) $(INPFILE)
##########################################################################
# That's all, folks!
##########################################################################
|